diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -1534,8 +1534,9 @@ // to register the unwind entry under same symbol. // This is not particularly efficient, but we should run into this case // infrequently (only when handling the output of `ld -r`). - funcSym = findSymbolAtOffset(cast(funcSym->isec), - funcSym->value); + if (funcSym->isec) + funcSym = findSymbolAtOffset(cast(funcSym->isec), + funcSym->value); } else { funcSym = findSymbolAtAddress(sections, funcAddr); ehRelocator.makePcRel(funcAddrOff, funcSym, target->p2WordSize); diff --git a/lld/test/MachO/Inputs/lto-obj-weak-def.yaml b/lld/test/MachO/Inputs/lto-obj-weak-def.yaml new file mode 100644 --- /dev/null +++ b/lld/test/MachO/Inputs/lto-obj-weak-def.yaml @@ -0,0 +1,144 @@ +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x1000007 + cpusubtype: 0x3 + filetype: 0x1 + ncmds: 3 + sizeofcmds: 352 + flags: 0x0 + reserved: 0x0 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 312 + segname: '' + vmaddr: 0 + vmsize: 96 + fileoff: 416 + filesize: 96 + maxprot: 7 + initprot: 7 + nsects: 3 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x0 + size: 1 + offset: 0x1A0 + align: 0 + reloff: 0x0 + nreloc: 0 + flags: 0x80000400 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: C3 + - sectname: __eh_frame + segname: __TEXT + addr: 0x8 + size: 56 + offset: 0x1A8 + align: 3 + reloff: 0x200 + nreloc: 4 + flags: 0x0 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: 1400000000000000017A520001781001100C0708900100001C00000004000000F8FFFFFFFFFFFFFF0100000000000000000E080000000000 + relocations: + - address: 0x1C + symbolnum: 0 + pcrel: false + length: 2 + extern: true + type: 5 + scattered: false + value: 0 + - address: 0x1C + symbolnum: 1 + pcrel: false + length: 2 + extern: true + type: 0 + scattered: false + value: 0 + - address: 0x20 + symbolnum: 1 + pcrel: false + length: 3 + extern: true + type: 5 + scattered: false + value: 0 + - address: 0x20 + symbolnum: 2 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - sectname: __compact_unwind + segname: __LD + addr: 0x40 + size: 32 + offset: 0x1E0 + align: 3 + reloff: 0x220 + nreloc: 1 + flags: 0x2000000 + reserved1: 0x0 + reserved2: 0x0 + reserved3: 0x0 + content: '0000000000000000010000000000000400000000000000000000000000000000' + relocations: + - address: 0x0 + symbolnum: 2 + pcrel: false + length: 3 + extern: true + type: 0 + scattered: false + value: 0 + - cmd: LC_SYMTAB + cmdsize: 24 + symoff: 552 + nsyms: 3 + stroff: 600 + strsize: 32 + - cmd: LC_DATA_IN_CODE + cmdsize: 16 + dataoff: 552 + datasize: 0 +LinkEditData: + NameList: + - n_strx: 7 + n_type: 0xE + n_sect: 2 + n_desc: 0 + n_value: 8 + - n_strx: 17 + n_type: 0xE + n_sect: 2 + n_desc: 0 + n_value: 32 + - n_strx: 2 + n_type: 0xF + n_sect: 1 + n_desc: 160 + n_value: 0 + StringTable: + - ' ' + - _foo + - EH_Frame1 + - func.eh + - '' + - '' + - '' + - '' + - '' + - '' + - '' +... diff --git a/lld/test/MachO/lto-obj-weak-def.ll b/lld/test/MachO/lto-obj-weak-def.ll new file mode 100644 --- /dev/null +++ b/lld/test/MachO/lto-obj-weak-def.ll @@ -0,0 +1,35 @@ +; REQUIRES: x86 + +;; Verify that we successfully merge weak definitions across bitcode and regular +;; assembly files, even when EH frames are present. We would previously +;; segfault. + +; RUN: rm -rf %t; split-file %s %t +; RUN: llvm-as %t/foo-1.ll -o %t/foo-1.o + +;; When changing the assembly input, uncomment these lines to re-generate the +;; YAML. +; COM: llvm-mc --emit-dwarf-unwind=always -filetype=obj -triple=x86_64-apple-darwin %t/foo-2.s -o %t/foo-2.o +; COM: ld -r %t/foo-2.o -o %t/foo-2-r.o +; COM: obj2yaml %t/foo-2-r.o -o %S/Inputs/lto-obj-weak-def.yaml + +; RUN: yaml2obj %S/Inputs/lto-obj-weak-def.yaml -o %t/foo-2-r.o +; RUN: %lld -lSystem -dylib %t/foo-1.o %t/foo-2-r.o -o /dev/null + +;--- foo-1.ll + +target triple = "x86_64-apple-macosx10.15.0" +target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" + +define weak void @foo() { + ret void +} + +;--- foo-2.s +.globl _foo +.weak_definition _foo +_foo: + .cfi_startproc + .cfi_def_cfa_offset 8 + ret + .cfi_endproc