How to prevent dead-stripping / GC of .o files?
Posted: 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?
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?