How to prevent dead-stripping / GC of .o files?

jens.alfke
Posts: 17
Joined: Wed May 02, 2018 4:26 pm

How to prevent dead-stripping / GC of .o files?

Postby jens.alfke » Wed May 02, 2018 4:43 pm

tl;dr: How do I prevent the linker from dead-stripping .o files that aren't referenced by symbols from any other .o files?

I'm porting some code that uses the Catch unit-testing framework. Its test macros expand into C++ static initializers that register a test at startup time, instead of requiring the main() function to explicitly call each test. For this reason, the source files with the tests don't tend to have any external references to their symbols ... and that means they get stripped out by the linker. The end result is that the compiled binary has no tests in it :(

I'm not very familiar with the GCC linker, but it looks like the flag governing this dead-stripping is `-Wl,--gc-sections` (on line 243 of esp-idf/make/project.mk.) I've tried adding `COMPONENT_ADD_LDFLAGS += -Wl,--no-gc-sections` to my component's makefile, but it has no effect ... I think this may have something to do with the --start-group and --end-group that bracket it.

(I tried modifying project.mk to remove the gc-sections flag, but that breaks many components including the bootloader, so that's a non-starter.)

For now I'm using the crappy workaround of adding a dummy function to each unit-test source file, and then calling those functions from app_main, but I'm hoping for a better way. Any suggestions?

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

Re: How to prevent dead-stripping / GC of .o files?

Postby ESP_igrr » Thu May 03, 2018 12:57 am

Unity-based unit test framework used for IDF components also relies on a similar auto-registration approach. We add --whole-archive flag around static libraries which contain unit tests. See e.g. https://github.com/espressif/esp-idf/bl ... mponent.mk

Who is online

Users browsing this forum: Google [Bot], Nanazhang, selec1 and 62 guests