[lld/mac] Make binaries written by lld strippable

Be less clever when writing the indirect symbols in LC_DYSYMTAB:
lld used to make point __stubs and __la_symbol_ptr point at the
same bytes in the indirect symbol table in the __LINKEDIT segment.
That confused strip, so write the same bytes twice and make
__stubs and __la_symbol_ptr point at one copy each, so that they
don't share data. This unconfuses strip, and seems to be what ld64
does too, so hopefully tools are generally more used to this.

This makes the output binaries a bit larger, but not much: 4 bytes
for roughly each called function from a dylib and each weak function.
Chromium Framewoork grows by 6536 bytes, clang-format by a few hundred.

With this, `strip -x Chromium\ Framework` works (244 MB before stripping
to 171 MB after stripping, compared to 236 MB=>164 MB with ld64). Running
strip without `-x` produces the same error message now for lld-linked
Chromium Framework as for when using ld64 as a linker.

`strip clang-format` also works now but didn't previously.

Fixes PR50657.

Differential Revision: https://reviews.llvm.org/D104081
This commit is contained in:
Nico Weber 2021-06-10 20:17:54 -04:00
parent 081ae5fe1a
commit 54418c5a35
2 changed files with 16 additions and 4 deletions

View File

@ -872,7 +872,7 @@ IndirectSymtabSection::IndirectSymtabSection()
uint32_t IndirectSymtabSection::getNumSymbols() const { uint32_t IndirectSymtabSection::getNumSymbols() const {
return in.got->getEntries().size() + in.tlvPointers->getEntries().size() + return in.got->getEntries().size() + in.tlvPointers->getEntries().size() +
in.stubs->getEntries().size(); 2 * in.stubs->getEntries().size();
} }
bool IndirectSymtabSection::isNeeded() const { bool IndirectSymtabSection::isNeeded() const {
@ -886,9 +886,9 @@ void IndirectSymtabSection::finalizeContents() {
off += in.got->getEntries().size(); off += in.got->getEntries().size();
in.tlvPointers->reserved1 = off; in.tlvPointers->reserved1 = off;
off += in.tlvPointers->getEntries().size(); off += in.tlvPointers->getEntries().size();
// There is a 1:1 correspondence between stubs and LazyPointerSection in.stubs->reserved1 = off;
// entries, so they can share the same sub-array in the table. off += in.stubs->getEntries().size();
in.stubs->reserved1 = in.lazyPointers->reserved1 = off; in.lazyPointers->reserved1 = off;
} }
static uint32_t indirectValue(const Symbol *sym) { static uint32_t indirectValue(const Symbol *sym) {
@ -910,6 +910,15 @@ void IndirectSymtabSection::writeTo(uint8_t *buf) const {
write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
++off; ++off;
} }
// There is a 1:1 correspondence between stubs and LazyPointerSection
// entries. But giving __stubs and __la_symbol_ptr the same reserved1
// (the offset into the indirect symbol table) so that they both refer
// to the same range of offsets confuses `strip`, so write the stubs
// symbol table offsets a second time.
for (const Symbol *sym : in.stubs->getEntries()) {
write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
++off;
}
} }
StringTableSection::StringTableSection() StringTableSection::StringTableSection()

View File

@ -5,6 +5,7 @@
# RUN: %lld -dylib %t/libfoo.o -o %t/libfoo.dylib -lSystem # RUN: %lld -dylib %t/libfoo.o -o %t/libfoo.dylib -lSystem
# RUN: %lld %t/test.o %t/libfoo.dylib -o %t/test -lSystem # RUN: %lld %t/test.o %t/libfoo.dylib -o %t/test -lSystem
# RUN: llvm-objdump --macho -d --no-show-raw-insn --indirect-symbols %t/test | FileCheck %s # RUN: llvm-objdump --macho -d --no-show-raw-insn --indirect-symbols %t/test | FileCheck %s
# RUN: llvm-otool -l %t/test | FileCheck --check-prefix=DYSYMTAB %s
# CHECK: (__TEXT,__text) section # CHECK: (__TEXT,__text) section
# CHECK-NEXT: _main: # CHECK-NEXT: _main:
@ -34,6 +35,8 @@
# CHECK-NEXT: _bar_tlv # CHECK-NEXT: _bar_tlv
# CHECK-NEXT: _foo_tlv # CHECK-NEXT: _foo_tlv
# DYSYMTAB: nindirectsyms 9
#--- libfoo.s #--- libfoo.s
.globl _foo, _foo_fn, _bar, _bar_fn .globl _foo, _foo_fn, _bar, _bar_fn