(The patch is intended for discussion purpose only)
With linker gc turned on, the linker will happily discard all __llvm_covmap sections from input objects so resulting executable file won't be usable with llvm-cov tool -- missing coverage data.
To demonstrate the problem, build a program with -fprofile-instr-generate -fcoverage-mapping -Wl,--gc-sections. The resulting exec won't be usable for coverage data dumping.
This patch shows one way to to fix the problem (demonstrated for Linux/FreeBSD only here). The idea is to force referencing coverage data module headers in profile dumper routines.
There are a couple of downsides:
- it works for linux/freeBSD, and can be extended for Darwin. Supporting on other platforms can be more complicated -- need runtime registration of coverage data start/end
- increases runtime overhead (physical memory and dumping time)
Another much better possible solution (verified manually on Linux) is quite simple -- set __llvm_covmap section with the right attributes such that they are not allocatable. When a section does not have the 'a' bit in attribute, the linker won't garbage collect it (just like debug sections).
This method has another added benefit --- process virtual memory size is reduced and the covmap section is now strippable. I think we should go with this approach instead (at least for ELF based systems). Thoughts?