diff --git a/lld/MachO/UnwindInfoSection.h b/lld/MachO/UnwindInfoSection.h --- a/lld/MachO/UnwindInfoSection.h +++ b/lld/MachO/UnwindInfoSection.h @@ -40,6 +40,7 @@ protected: UnwindInfoSection(); virtual void prepareRelocations(ConcatInputSection *) = 0; + virtual void resolvePersonalitySymbols() = 0; ConcatOutputSection *compactUnwindSection; uint64_t unwindInfoSize = 0; diff --git a/lld/MachO/UnwindInfoSection.cpp b/lld/MachO/UnwindInfoSection.cpp --- a/lld/MachO/UnwindInfoSection.cpp +++ b/lld/MachO/UnwindInfoSection.cpp @@ -107,6 +107,7 @@ class UnwindInfoSectionImpl final : public UnwindInfoSection { public: void prepareRelocations(ConcatInputSection *) override; + void resolvePersonalitySymbols() override; void addInput(ConcatInputSection *) override; void finalize() override; void writeTo(uint8_t *buf) const override; @@ -126,6 +127,13 @@ std::vector *> cuPtrVector; std::vector secondLevelPages; uint64_t level2PagesOffset = 0; + + // Collections of personality symbols bucketed into different symbol kinds. + SmallDenseSet localSyms; + SmallDenseSet dylibSyms; + // Mapping of a local personality symbol to their corresponding dylib symbol. + // (ie., (local symbol's GOT index + 1) -> (dylib symbol's GOT index + 1)) + DenseMap oldToNew; }; UnwindInfoSection::UnwindInfoSection() @@ -138,8 +146,40 @@ void UnwindInfoSection::prepareRelocations() { for (ConcatInputSection *isec : compactUnwindSection->inputs) prepareRelocations(isec); + + resolvePersonalitySymbols(); } +// If symbol is a local Defined (ie 't'), then replace it with +// a Dylib symbol with the same name. +// We do this because sometimes personality symbols are intentionally +// defined as a local symbol as hack to get around another bug. As +// a result, often we see two of the same personality symbols but different +// type, causing "too many personalities to encode". +// See https://reviews.llvm.org/D107533 for full discussions. +template +void UnwindInfoSectionImpl::resolvePersonalitySymbols() { + for (Defined *lSym : localSyms) { + Symbol *found = nullptr; + for (DylibSymbol *dSym : dylibSyms) { + if (lSym->getName() == dSym->getName()) { + found = dSym; + break; + } + } + + // If there isn't a dylib personality with the same name, + // then treat the local symbol as usual. + // Otherwise, we recorded a mapping of their personalities + // to be used later in encoding. + if (found == nullptr) { + personalityTable[{lSym->isec, lSym->value}] = lSym; + in.got->addEntry(lSym); + } else { + oldToNew[lSym->gotIndex + 1] = found->gotIndex + 1; + } + } +} template void UnwindInfoSectionImpl::addInput(ConcatInputSection *isec) { assert(isec->getSegName() == segment_names::ld && @@ -195,14 +235,20 @@ Symbol *&personality = personalityTable[{defined->isec, defined->value}]; if (personality == nullptr) { - personality = defined; - in.got->addEntry(defined); + if (!defined->isExternal()) + // If this symbol is a local, we may need special treatment later. + localSyms.insert(defined); + else { + personality = defined; + in.got->addEntry(defined); + } } else if (personality != defined) { r.referent = personality; } continue; } assert(isa(s)); + dylibSyms.insert(dyn_cast(s)); in.got->addEntry(s); continue; } @@ -442,7 +488,15 @@ foldBegin = foldEnd; } cuPtrVector.erase(foldWrite, cuPtrVector.end()); - + // Update the personality to the right values before encoding. + if (!oldToNew.empty()) { + for (CompactUnwindEntry *&entry : cuPtrVector) { + auto it = oldToNew.find(entry->personality); + if (it != oldToNew.end()) { + entry->personality = it->second; + } + } + } encodePersonalities(cuPtrVector, personalities); // Count frequencies of the folded encodings diff --git a/lld/test/MachO/compact-unwind-both-local-and-dylib-personality.s b/lld/test/MachO/compact-unwind-both-local-and-dylib-personality.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/compact-unwind-both-local-and-dylib-personality.s @@ -0,0 +1,240 @@ +## Tests that lld-macho can handle the case where personality symbols with the same name +## are both from a dylib and locally defined in an object file. + +# REQUIRES: x86 +# RUN: rm -rf %t; split-file %s %t +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin19.0.0 %t/user_2.s -o %t/user_2.o +# RUN: yaml2obj %t/combined.yaml > %t/combined.o + +## Pre-condition: check that ___gxx_personality_v0 really is locally defined in combined.o before we proceed. +# RUN: llvm-nm %t/combined.o | grep '___gxx_personality_v0' | FileCheck %s --check-prefix=PRECHECK +# PRECHECK: {{.*}} t ___gxx_personality_v0 +# PRECHECK-NOT: {{.*}} U ___gxx_personality_v0 +# PRECHECK-NOT: {{.*}} T ___gxx_personality_v0 + +## check that we can link with 4 personalities without crashing: +## ___gxx_personality_v0 (from libc++.tbd) ___gxx_personality_v0(local), _personality_1 and _personality_2 +# RUN: %lld -pie -lSystem -lc++ %t/user_2.o -o %t/user_2.out +# RUN: %lld -pie -lSystem -lc++ %t/user_2.o %t/combined.o -o %t/a.out + +## postlink check: FIXME: maybe need a better verification here. +# RUN: llvm-nm %t/a.out | FileCheck %s --check-prefix=POSTCHECK +# POSTCHECK: {{.*}} U ___gxx_personality_v0 +# POSTCHECK: {{.*}} t ___gxx_personality_v0 + +#--- user_2.s +.globl _main, _personality_1, _personality_2 + +.text + +_bar: + .cfi_startproc + .cfi_personality 155, _personality_1 + .cfi_def_cfa_offset 16 + retq + .cfi_endproc + +_baz: + .cfi_startproc + .cfi_personality 155, _personality_2 + .cfi_def_cfa_offset 16 + retq + .cfi_endproc + +_main: + .cfi_startproc + .cfi_personality 155, ___gxx_personality_v0 + .cfi_def_cfa_offset 16 + retq + .cfi_endproc + +_personality_1: + retq +_personality_2: + retq + +## This yaml was created from the combined.o object file described in this comment: +## https://reviews.llvm.org/D107533#2935217 +#--- combined.yaml +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x1000007 + cpusubtype: 0x3 + filetype: 0x1 + ncmds: 4 + sizeofcmds: 384 + flags: 0x2000 + reserved: 0x0 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 312 + segname: '' + vmaddr: 0 + vmsize: 152 + fileoff: 448 + filesize: 152 + maxprot: 7 + initprot: 7 + nsects: 3 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x0 + size: 5 + offset: 0x1C0 + align: 2 + reloff: 0x0 + nreloc: 0 + flags: 0x80000400 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: 90909090C3 + - sectname: __eh_frame + segname: __TEXT + addr: 0x8 + size: 80 + offset: 0x1C8 + align: 3 + reloff: 0x258 + nreloc: 5 + flags: 0x0 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: 1400000000000000017A520001781001100C0708900100001800000000000000017A505200017810069B04000000100C070890011800000004000000F8FFFFFFFFFFFFFF0100000000000000000E1000 + relocations: + - address: 0x2A + symbolnum: 0 + pcrel: true + length: 2 + extern: true + type: 4 + scattered: false + value: 0 + - address: 0x38 + symbolnum: 2 + pcrel: false + length: 2 + extern: true + type: 5 + scattered: false + value: 0 + - address: 0x38 + symbolnum: 3 + pcrel: false + length: 2 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x3C + symbolnum: 3 + pcrel: false + length: 3 + extern: true + type: 5 + scattered: false + value: 0 + - address: 0x3C + symbolnum: 4 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - sectname: __compact_unwind + segname: __LD + addr: 0x58 + size: 64 + offset: 0x218 + align: 3 + reloff: 0x280 + nreloc: 3 + flags: 0x2000000 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '00000000000000000100000000000202000000000000000000000000000000000000000000000000010000000000020200000000000000000000000000000000' + relocations: + - address: 0x0 + symbolnum: 0 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x20 + symbolnum: 4 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x30 + symbolnum: 0 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - cmd: LC_SYMTAB + cmdsize: 24 + symoff: 664 + nsyms: 5 + stroff: 744 + strsize: 48 + - cmd: LC_BUILD_VERSION + cmdsize: 32 + platform: 1 + minos: 659200 + sdk: 720896 + ntools: 1 + Tools: + - tool: 3 + version: 39913472 + - cmd: LC_DATA_IN_CODE + cmdsize: 16 + dataoff: 664 + datasize: 0 +LinkEditData: + NameList: + - n_strx: 7 + n_type: 0x1E + n_sect: 1 + n_desc: 32 + n_value: 0 + - n_strx: 29 + n_type: 0xE + n_sect: 2 + n_desc: 0 + n_value: 8 + - n_strx: 29 + n_type: 0xE + n_sect: 2 + n_desc: 0 + n_value: 32 + - n_strx: 39 + n_type: 0xE + n_sect: 2 + n_desc: 0 + n_value: 60 + - n_strx: 2 + n_type: 0xF + n_sect: 1 + n_desc: 0 + n_value: 4 + StringTable: + - ' ' + - _foo + - ___gxx_personality_v0 + - EH_Frame1 + - func.eh + - '' +...