Page 1 of 1

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

Posted: Wed May 02, 2018 4:43 pm
by jens.alfke
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?

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

Posted: Thu May 03, 2018 12:57 am
by ESP_igrr
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