Skip to content

Commit

Permalink
[llvm-readobj] Don't print out section names for STABS symbols
Browse files Browse the repository at this point in the history
This diff is similar to what D71394 did for `llvm-objdump` -- it avoids
trying to look up a section name for STABS symbols, since some STABS
symbol types (like `N_OSO`) use the `n_sect` field to store other data
instead of a section index.

Differential Revision: https://reviews.llvm.org/D88468
  • Loading branch information
int3 committed Oct 13, 2020
1 parent 950ae43 commit 84cc39c
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 13 deletions.
12 changes: 6 additions & 6 deletions llvm/test/tools/llvm-objcopy/MachO/symbol-table.test
Expand Up @@ -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: ]
Expand All @@ -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: ]
Expand All @@ -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: ]
Expand All @@ -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: ]
Expand All @@ -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: ]
Expand All @@ -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: ]
Expand Down
129 changes: 129 additions & 0 deletions 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
...
21 changes: 14 additions & 7 deletions llvm/tools/llvm-readobj/MachODumper.cpp
Expand Up @@ -623,13 +623,20 @@ void MachODumper::printSymbol(const SymbolRef &Symbol) {
getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);

StringRef SectionName = "";
Expected<section_iterator> 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<section_iterator> 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);
Expand Down

0 comments on commit 84cc39c

Please sign in to comment.