Page 1 of 1

How to prevent linking ALL libraries in ESP-IDF for ESP32-S2?

Posted: Thu Jul 08, 2021 8:02 am
by MickPF
Hello,

my code contains "large" data and I get a problem when linking the binary:

Code: Select all

section `.dram0.data' will not fit in region `dram0_0_seg'
Is there any way to prevent linking libraries I never use?

For few of them it's posssible to uncheck them in "menuconfig", but for most of them it's not possible!

I use the cmake "way" for compiling.

Thanks in advance,
Michael

Re: How to prevent linking ALL libraries in ESP-IDF for ESP32-S2?

Posted: Thu Jul 08, 2021 10:24 am
by ESP_igrr
Hi Michael,

Although ESP-IDF build system builds and links all libraries, unused modules will generally not be included into the final binary. There are a few mechanisms used to ensure this:

1. The linker will not include object files that aren't required to satisfy some reference from another object file. This means that if functions of some library are never called from the application or other libraries, the object files of this library will not be included by the linker.

2. ESP-IDF build system enables -ffunction-sections, -fdata-sections flags for the compiler and --gc-sections flag of the linker. With these flags, each function and static/global variable are placed into separate section. The linker can then gargbage-collect all the unused sections. As a result, if you are using some library, but only calling a few functions from it, other unused functions can be removed from the application during the linking process.

Regarding the issue you are facing, the overflow happens for the data segment. On the ESP32, reducing the amount of program code (located in Flash) does not make space for more data, since the code flash memory and data memory are not interchangeable. So excluding some of the libraries from your program will only free up as much space as they are using for static data. In ESP-IDF, most libraries prefer dynamic memory allocation, so there isn't a lot of space to reclaim this way.

If you have several large arrays, you may be hitting the 160kB limit of statically allocated data on the ESP32 in ESP-IDF: https://github.com/espressif/esp-idf/issues/3497. The workaround for this issue is to make these arrays dynamically allocated, please see https://github.com/espressif/esp-idf/is ... -493879020.

Edit: the limit mentioned above applies to the ESP32; I just noticed you were asking about ESP32-S2. What is the total size of large arrays you are allocating, by your estimate? Can you make the application link succeed by reducing the size of the arrays, and then use 'idf.py size' command to check static memory allocation? See more about 'idf.py size' here: https://docs.espressif.com/projects/esp ... atic-sizes

Re: How to prevent linking ALL libraries in ESP-IDF for ESP32-S2?

Posted: Thu Jul 08, 2021 1:39 pm
by MickPF
I understand...

I had this problem already on the ESP32 with some large static data and solved it by using a fatfs partition on the flash and loading/copying this data first from a SD card to the flash if it is inserted. I pass through these data from the flash to an EVE display via SPI. All things worked fine.

Few days ago I "moved" the display interface from VSPI to HSPI because the SD card slot uses (hard-wired) the VSPI and I'd like to have "clean" division. Since this time NOTHING works on the SPI buses and I don't know why! The logic analyzer displays dead lines! I rolled back the changes (it's only a definition and some precompiler conditionals) but they stay DEAD! Only a LED blinks fast hard-wired to GPIO5, but I don't use this GPIO in my code!

I'm very, very angry because I don't understand what "killed" my code. And that's why I "threw" ESP32 in the trash.

Now I try the same thing with a Saola board as my first step/attempt with ESP32-S2.

Re: How to prevent linking ALL libraries in ESP-IDF for ESP32-S2?

Posted: Thu Jul 08, 2021 1:48 pm
by MickPF
By the way:

Thank you for your answer!

I didn't know anything about this limit before, but I already figured it out.

Thanks