ESP-IDF gcc static constructor

alb702
Posts: 9
Joined: Tue Dec 07, 2021 3:31 am

ESP-IDF gcc static constructor

Postby alb702 » Tue May 10, 2022 7:16 am

Hello.

ESP32 IDF gcc linker remove "static" symbols in module, even if have "constructor" attribute.
(use "Master" branch).

I.E. , i'm built sources in project :

------------ main.c ----------------------
#include <stdio.h>
void f(void) { printf ("test\n"); }
void app_main() {}
//void main() { app_main(); }
------------------------------------------


------------ test.c ----------------------
void f(void);
static void __attribute__ ((constructor (101))) test (void) { f(); }
-------------------------------------------

methods 'f()' and 'test()' do not called.
I'm checked it at xtensa - (ESP32) and risc-v - (ESP32-C3) projects.

How to fix it?

If build this on other platforms (Windows-, linux-, arm-linux- , none-eabi- gcc ), static constructors are called.

ESP_Sprite
Posts: 9016
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-IDF gcc static constructor

Postby ESP_Sprite » Tue May 10, 2022 7:49 am

You need to make sure the linker doesn't discard the object these functions are in; it normally does this if the objects do not contains any unresolved symbols. You can do this by defining a dummy function ('void dummy_for_inclusion() {}') in the source file, then modifying the CMakeLists.txt file like described here to mark that dummy function as an unresolved symbol.

alb702
Posts: 9
Joined: Tue Dec 07, 2021 3:31 am

Re: ESP-IDF gcc static constructor

Postby alb702 » Fri May 13, 2022 11:33 am

Thanks.

This is strange behavior of linker.

We are need to use in this project some dozens sources from our repository.
Need to put "dummy" function with different names in each source.

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: ESP-IDF gcc static constructor

Postby ESP_igrr » Fri May 13, 2022 9:49 pm

If you are using the master branch, you can take advantage of the new feature in IDF build system, which instructs the linker to take the whole content of the static library without discarding unused files. Please search https://docs.espressif.com/projects/esp ... ystem.html for WHOLE_ARCHIVE for details.

There is also an example demonstrating this feature at https://github.com/espressif/esp-idf/tr ... executable

Who is online

Users browsing this forum: No registered users and 133 guests