diff --git a/lld/MachO/UnwindInfoSection.cpp b/lld/MachO/UnwindInfoSection.cpp --- a/lld/MachO/UnwindInfoSection.cpp +++ b/lld/MachO/UnwindInfoSection.cpp @@ -287,17 +287,43 @@ static void encodePersonalities(const std::vector *> &cuPtrVector, std::vector &personalities) { + // Map of personality symbol name to (personality, personalityIndex). + DenseMap> nameToPersonality; + const auto &entries = in.got->getEntries(); for (CompactUnwindEntry *cu : cuPtrVector) { - if (cu->personality == 0) + if (cu->personality == 0 || cu->personality == TombstoneValue) continue; // Linear search is fast enough for a small array. auto it = find(personalities, cu->personality); - uint32_t personalityIndex; // 1-based index + uint32_t personalityIndex = 0; // 1-based index if (it != personalities.end()) { personalityIndex = std::distance(personalities.begin(), it) + 1; } else { - personalities.push_back(cu->personality); - personalityIndex = personalities.size(); + const Symbol *sym = entries[cu->personality - 1]; // -1 because 1-based + auto it = nameToPersonality.find(sym->getName()); + if (it == nameToPersonality.end() || + (sym->kind() == Symbol::DefinedKind && sym->getFile() == nullptr)) { + // If we've never seen this symbol, or if it's one of those synthetic + // defined ones, then record it. + personalities.push_back(cu->personality); + personalityIndex = personalities.size(); + nameToPersonality[sym->getName()] = + std::pair(cu->personality, personalityIndex); + } else { + auto &pair = it->second; + const Symbol *prevSym = entries[pair.first - 1]; + // Can't have both Defined with the same name and different GOT index. + assert(sym->kind() != Symbol::DefinedKind || + prevSym->kind() != Symbol::DefinedKind); + // Prioritize Defined over others. + if (prevSym->kind() != Symbol::DefinedKind && + sym->kind() == Symbol::DefinedKind) { + personalities[pair.second] = cu->personality; + pair.first = cu->personality; + personalityIndex = pair.second; + } else + personalityIndex = pair.second; + } } cu->encoding |= personalityIndex << countTrailingZeros( 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 + - '' +...