diff --git a/llvm/test/tools/llvm-objcopy/MachO/symbol-table.test b/llvm/test/tools/llvm-objcopy/MachO/symbol-table.test --- a/llvm/test/tools/llvm-objcopy/MachO/symbol-table.test +++ b/llvm/test/tools/llvm-objcopy/MachO/symbol-table.test @@ -63,7 +63,7 @@ # CHECK-NEXT: Symbol { # CHECK-NEXT: Name: /var/folders/1d/zyfdpp7j2995h5hqspjy28bc0000gn/T/main-c5ac21.o (38) # CHECK-NEXT: Type: SymDebugTable (0x66) -# CHECK-NEXT: Section: __got (0x3) +# CHECK-NEXT: Section: (0x3) # CHECK-NEXT: RefType: ReferenceFlagUndefinedLazy (0x1) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] @@ -72,7 +72,7 @@ # CHECK-NEXT: Symbol { # CHECK-NEXT: Name: (207) # CHECK-NEXT: Type: SymDebugTable (0x2E) -# CHECK-NEXT: Section: __text (0x1) +# CHECK-NEXT: Section: (0x1) # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] @@ -81,7 +81,7 @@ # CHECK-NEXT: Symbol { # CHECK-NEXT: Name: _main (101) # CHECK-NEXT: Type: SymDebugTable (0x24) -# CHECK-NEXT: Section: __text (0x1) +# CHECK-NEXT: Section: (0x1) # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] @@ -99,7 +99,7 @@ # CHECK-NEXT: Symbol { # CHECK-NEXT: Name: (207) # CHECK-NEXT: Type: SymDebugTable (0x4E) -# CHECK-NEXT: Section: __text (0x1) +# CHECK-NEXT: Section: (0x1) # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] @@ -108,7 +108,7 @@ # CHECK-NEXT: Symbol { # CHECK-NEXT: Name: _PrivateSymbol (169) # CHECK-NEXT: Type: SymDebugTable (0x26) -# CHECK-NEXT: Section: __bss (0x4) +# CHECK-NEXT: Section: (0x4) # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] @@ -135,7 +135,7 @@ # CHECK-NEXT: Symbol { # CHECK-NEXT: Name: (207) # CHECK-NEXT: Type: SymDebugTable (0x64) -# CHECK-NEXT: Section: __text (0x1) +# CHECK-NEXT: Section: (0x1) # CHECK-NEXT: RefType: UndefinedNonLazy (0x0) # CHECK-NEXT: Flags [ (0x0) # CHECK-NEXT: ] diff --git a/llvm/test/tools/llvm-readobj/MachO/stabs.yaml b/llvm/test/tools/llvm-readobj/MachO/stabs.yaml new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-readobj/MachO/stabs.yaml @@ -0,0 +1,129 @@ +## Verify that llvm-readobj can dump files containing stabs symbols, +## particularly symbols of type N_OSO which have repurposed the n_sect field +## to store a non-section-index value. + +# RUN: yaml2obj %s -o %t +# RUN: llvm-readobj --syms %t | FileCheck %s + +# CHECK: Symbols [ +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: /Volumes/Sandbox/ (2) +# CHECK-NEXT: Type: SymDebugTable (0x64) +# CHECK-NEXT: Section: (0x0) +# CHECK-NEXT: RefType: UndefinedNonLazy (0x0) +# CHECK-NEXT: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: Value: 0x0 +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: /Volumes/Sandbox/test.o (20) +# CHECK-NEXT: Type: SymDebugTable (0x66) +# CHECK-NEXT: Section: (0x3) +# CHECK-NEXT: RefType: ReferenceFlagUndefinedLazy (0x1) +# CHECK-NEXT: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: Value: 0x5F72D5E2 +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: (1) +# CHECK-NEXT: Type: SymDebugTable (0x2E) +# CHECK-NEXT: Section: (0x1) +# CHECK-NEXT: RefType: UndefinedNonLazy (0x0) +# CHECK-NEXT: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: Value: 0x100000FA0 +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: _foo (44) +# CHECK-NEXT: Type: SymDebugTable (0x24) +# CHECK-NEXT: Section: (0x1) +# CHECK-NEXT: RefType: UndefinedNonLazy (0x0) +# CHECK-NEXT: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: Value: 0x100000FA0 +# CHECK-NEXT: } +# CHECK-NEXT: Symbol { +# CHECK-NEXT: Name: (1) +# CHECK-NEXT: Type: SymDebugTable (0x4E) +# CHECK-NEXT: Section: (0x1) +# CHECK-NEXT: RefType: UndefinedNonLazy (0x0) +# CHECK-NEXT: Flags [ (0x0) +# CHECK-NEXT: ] +# CHECK-NEXT: Value: 0xF +# CHECK-NEXT: } +# CHECK-NEXT: ] + +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x01000007 + cpusubtype: 0x00000003 + filetype: 0x00000002 + ncmds: 2 + sizeofcmds: 744 + flags: 0x00200085 + reserved: 0x00000000 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 232 + segname: __TEXT + vmaddr: 4294967296 + vmsize: 4096 + fileoff: 0 + filesize: 4096 + maxprot: 5 + initprot: 5 + nsects: 1 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x00000000 + size: 0 + offset: 0x00000000 + align: 4 + reloff: 0x00000000 + nreloc: 0 + flags: 0x80000400 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 + - cmd: LC_SYMTAB + cmdsize: 24 + symoff: 4152 + nsyms: 5 + stroff: 4328 + strsize: 49 +LinkEditData: + NameList: + - n_strx: 2 + n_type: 0x64 ## N_SO + n_sect: 0 + n_desc: 0 + n_value: 0 + - n_strx: 20 + n_type: 0x66 ## N_OSO + n_sect: 3 + n_desc: 1 + n_value: 1601361378 + - n_strx: 1 + n_type: 0x2E ## N_BNSYM + n_sect: 1 + n_desc: 0 + n_value: 4294971296 + - n_strx: 44 + n_type: 0x24 ## N_FUN + n_sect: 1 + n_desc: 0 + n_value: 4294971296 + - n_strx: 1 + n_type: 0x4E ## N_ENSYM + n_sect: 1 + n_desc: 0 + n_value: 15 + StringTable: + - ' ' + - '/Volumes/Sandbox/' + - '/Volumes/Sandbox/test.o' + - _foo +... diff --git a/llvm/tools/llvm-readobj/MachODumper.cpp b/llvm/tools/llvm-readobj/MachODumper.cpp --- a/llvm/tools/llvm-readobj/MachODumper.cpp +++ b/llvm/tools/llvm-readobj/MachODumper.cpp @@ -623,13 +623,20 @@ getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol); StringRef SectionName = ""; - Expected SecIOrErr = Symbol.getSection(); - if (!SecIOrErr) - reportError(SecIOrErr.takeError(), Obj->getFileName()); - - section_iterator SecI = *SecIOrErr; - if (SecI != Obj->section_end()) - SectionName = unwrapOrError(Obj->getFileName(), SecI->getName()); + // Don't ask a Mach-O STABS symbol for its section unless we know that + // STAB symbol's section field refers to a valid section index. Otherwise + // the symbol may error trying to load a section that does not exist. + // TODO: Add a whitelist of STABS symbol types that contain valid section + // indices. + if (!(MOSymbol.Type & MachO::N_STAB)) { + Expected SecIOrErr = Symbol.getSection(); + if (!SecIOrErr) + reportError(SecIOrErr.takeError(), Obj->getFileName()); + + section_iterator SecI = *SecIOrErr; + if (SecI != Obj->section_end()) + SectionName = unwrapOrError(Obj->getFileName(), SecI->getName()); + } DictScope D(W, "Symbol"); W.printNumber("Name", SymbolName, MOSymbol.StringIndex);