Does MCUXpresso support __attribute__((constructor)) declaration?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Does MCUXpresso support __attribute__((constructor)) declaration?

1,466 Views
mark_li
Contributor II

Hi,

We are using RT1050 with MCUXpresso v11.4.0, we recently introduced a 3rd part library, which declares his entry as

static void __attribute__((constructor)) fipsEntry(void);

The constructor attribute causes the function to be called automatically before execution enters main(), it works well with GCC compiler, but when I tried to build it in IDE, this function always gets discarded by the linker because nobody refers it, I cannot even call it explicitly because it's static.

My question is why the compiler in MCUXpresso doesn't support / recognize the constructor attribute?

0 Kudos
Reply
5 Replies

1,067 Views
ErichStyger
Specialist I

I wrote an article about using the constructor and destructor attribute here:

https://mcuoneclipse.com/2023/10/09/global-constructors-and-destructors-with-c-not-c/

It works perfectly, but you have to make sure that your startup code is calling the constructor and destructor hooks, which is not the case for the C startup code provided by the MCUXpresso SDK.

0 Kudos
Reply

1,441 Views
ErichStyger
Specialist I

I'm using the latest IDE 11.8.0 (see https://mcuoneclipse.com/2023/08/06/mcuxpresso-ide-11-8-0/ ), and here I see that it gets linked:

.text._ZL9fipsEntryv

0x00000c7c 0xe ./source/main.o

I had not a chance to see how it looks for your older IDE version, but for sure 11.4.0 includes older GNU tools.

Just few things to note:

- the code with the attribute needs to be compiled as C++

- the startup code needs to support C++ constructor/destructor calls. See https://mcuoneclipse.com/2020/07/11/from-c-to-c-converting-eclipse-c-projekts/ for details.

I hope this helps,

Erich

0 Kudos
Reply

1,423 Views
mark_li
Contributor II

Erich, thanks for your reply.

Unfortunately we cannot afford to switch the whole project to C++, it has already been in mass production. I tried IDE 11.8 and got the same result (compiled as C).

However I doubt that this can only be done in C++, we keep both GCC build (for firmware release) and IDE build (for bug diagnosis) environments for the same project, in GCC environment it's also compiled as C and works well, that entry function get linked in elf and called from startup code with the help of crt0.o

Below are the build result from both map files, in IDE it's thrown into discarded section.

GCC:

    .text.fipsEntry
    0x0000000060105f28 0x58 ./fips.c.obj

IDE:

    .text.fipsEntry
    0x00000000 0x58 ./fips.o

0 Kudos
Reply

1,321 Views
ErichStyger
Specialist I

Hi @mark_li ,

C++: I did a quick test with my standard C++ project and the updated C++ startup code, and it worked there. Yes, it very likely will work with C code too, as long as the startup code calls the needed initialization (which it does in my example for C++).

If time permits, I'll check with plain C in the next days and report back.

Would that help?

Erich

0 Kudos
Reply

1,343 Views
RaRo
NXP TechSupport
NXP TechSupport

Hello @mark_li,

Could you please try out enabling All Warnings (-Wall) directive in Properties -> C/C++ Build -> Settings -> MCU C Compiler -> Warnings? Which optimization level and dialect are you using?

RaulRomero_0-1694115606440.png

[MCUXpresso IDE Properties]

Also, if possible, could you please share a demo code with static void __attribute__((constructor)) fipsEntry(void); entry to show me the issue?

Best regards, Raul.

0 Kudos
Reply