[lld/mac] Mark private externs with GOT relocs as LOCAL in indirect symbtab
authorNico Weber <thakis@chromium.org>
Thu, 14 Oct 2021 22:32:10 +0000 (18:32 -0400)
committerNico Weber <thakis@chromium.org>
Fri, 15 Oct 2021 17:24:47 +0000 (13:24 -0400)
commit4e572db0c2ec4e96f8ddfade7384e8ee8ad97384
tree027855d84cf02a401672a62020f942fbcd50bd8f
parentbacddf47a8381f52baebaaa41a2c482c156b6f01
[lld/mac] Mark private externs with GOT relocs as LOCAL in indirect symbtab

prepareSymbolRelocation() in Writer.cpp adds both symbols that need binding and
symbols relocated with a pointer relocation to the got.

Pointer relocations are emitted for non-movq GOTPCREL(%rip) loads.  (movqs
become GOT_LOADs so that the linker knows they can be relaxed to leaqs, while
others, such as addq, become just GOT -- a pointer relocation -- since they
can't be relaxed in that way).

For example, this C file produces a private_extern GOT relocation when
compiled with -O2 with clang:

    extern const char kString[];
    const char* g(int a) { return kString + a; }

Linkers need to put pointer-relocated symbols into the GOT, but ld64 marks them
as LOCAL in the indirect symbol table. This matters, since `strip -x` looks at
the indirect symbol table when deciding what to strip.

The indirect symtab emitting code was assuming that only symbols that need
binding are in the GOT, but pointer relocations where there too. Hence, the
code needs to explicitly check if a symbol is a private extern.

Fixes https://crbug.com/1242638, which has some more information in comments 14
and 15. With this patch, the output of `nm -U` on Chromium Framework after
stripping now contains just two symbols when using lld, just like with ld64.

Differential Revision: https://reviews.llvm.org/D111852
lld/MachO/SyntheticSections.cpp
lld/test/MachO/compact-unwind-both-local-and-dylib-personality.s
lld/test/MachO/indirect-symtab.s