[SanitizerCoverage] Make __start_/__stop_ symbols extern_weak

Authored by MaskRay on Mar 18 2021, 4:46 PM.


[SanitizerCoverage] Make start_/stop_ symbols extern_weak

On ELF, we place the metadata sections (__sancov_guards, __sancov_cntrs,
__sancov_bools, __sancov_pcs in section groups (either comdat any or
comdat noduplicates).

With --gc-sections, LLD since D96753 and GNU ld -z start-stop-gc may garbage
collect such sections. If all __sancov_bools are discarded, LLD will error
error: undefined hidden symbol: __start___sancov_cntrs (other sections are similar).

% cat a.c
void discarded() {}
% clang -fsanitize-coverage=func,trace-pc-guard -fpic -fvisibility=hidden a.c -shared -fuse-ld=lld -Wl,--gc-sections
ld.lld: error: undefined hidden symbol: __start___sancov_guards
>>> referenced by a.c
>>>               /tmp/a-456662.o:(sancov.module_ctor_trace_pc_guard)

Use the extern_weak linkage (lowered to undefined weak symbols) to avoid the
undefined error.

Differential Revision: https://reviews.llvm.org/D98903