Does MCUXpresso support __attribute__((constructor)) declaration?

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Does MCUXpresso support __attribute__((constructor)) declaration?

1,468件の閲覧回数
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 件の賞賛
返信
5 返答(返信)

1,069件の閲覧回数
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 件の賞賛
返信

1,443件の閲覧回数
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 件の賞賛
返信

1,425件の閲覧回数
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 件の賞賛
返信

1,323件の閲覧回数
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 件の賞賛
返信

1,345件の閲覧回数
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 件の賞賛
返信