[SanitizerCoverage] Make __start_/__stop_ symbols extern_weak
authorFangrui Song <i@maskray.me>
Thu, 18 Mar 2021 23:46:04 +0000 (16:46 -0700)
committerFangrui Song <i@maskray.me>
Thu, 18 Mar 2021 23:46:04 +0000 (16:46 -0700)
commit9558456b5370e64560e76f6580b979fccadd4744
treec5d4eb9f9d38b52d6da0557b9cb0351d5dda41fc
parentc9861f722e375c419a07bcb70c54fe1384cd2999
[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
llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
llvm/test/Instrumentation/SanitizerCoverage/inline-8bit-counters.ll
llvm/test/Instrumentation/SanitizerCoverage/inline-bool-flag.ll
llvm/test/Instrumentation/SanitizerCoverage/pc-table.ll