Use pointers to iterate over symbols.

While here, don't report a dummy symbol for relocations that don't have symbols.
We used to says such relocations were for the first defined symbol, but now we
return end_symbols(). The llvm-readobj output change agrees with otool.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@180214 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2013-04-24 19:47:55 +00:00
parent d4ee3920c9
commit 802fe9340d
4 changed files with 92 additions and 84 deletions

View File

@ -216,23 +216,9 @@ static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
return O->getData().substr(Offset, 1).data();
}
static const char *getSymbolTableEntryPtr(const MachOObjectFile *O,
DataRefImpl DRI) {
macho::SymtabLoadCommand S = O->getSymtabLoadCommand();
unsigned Index = DRI.d.b;
unsigned SymbolTableEntrySize = O->is64Bit() ?
sizeof(macho::Symbol64TableEntry) :
sizeof(macho::SymbolTableEntry);
uint64_t Offset = S.SymbolTableOffset + Index * SymbolTableEntrySize;
return getPtr(O, Offset);
}
static SymbolTableEntryBase
getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
const char *P = getSymbolTableEntryPtr(O, DRI);
const char *P = reinterpret_cast<const char *>(DRI.p);
return getStruct<SymbolTableEntryBase>(O, P);
}
@ -420,7 +406,10 @@ MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
SymbolRef &Res) const {
Symb.d.b++;
unsigned SymbolTableEntrySize = is64Bit() ?
sizeof(macho::Symbol64TableEntry) :
sizeof(macho::SymbolTableEntry);
Symb.p += SymbolTableEntrySize;
Res = SymbolRef(Symb, this);
return object_error::success;
}
@ -494,15 +483,15 @@ error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
}
// Unfortunately symbols are unsorted so we need to touch all
// symbols from load command
macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
DRI.d.b = 0;
while (DRI.d.b <= Symtab.NumSymbolTableEntries) {
error_code ec;
for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
I.increment(ec)) {
DataRefImpl DRI = I->getRawDataRefImpl();
Entry = getSymbolTableEntryBase(this, DRI);
getSymbolAddress(DRI, Value);
if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
if (!EndOffset || Value < EndOffset)
EndOffset = Value;
DRI.d.b++;
}
if (!EndOffset) {
uint64_t Size;
@ -829,11 +818,18 @@ MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const {
macho::RelocationEntry RE = getRelocation(Rel);
uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
bool isExtern = getPlainRelocationExternal(RE);
DataRefImpl Sym;
if (isExtern) {
Sym.d.b = SymbolIdx;
if (!isExtern) {
Res = *end_symbols();
return object_error::success;
}
macho::SymtabLoadCommand S = getSymtabLoadCommand();
unsigned SymbolTableEntrySize = is64Bit() ?
sizeof(macho::Symbol64TableEntry) :
sizeof(macho::SymbolTableEntry);
uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
DataRefImpl Sym;
Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Res = SymbolRef(Sym, this);
return object_error::success;
}
@ -1160,17 +1156,27 @@ error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
}
symbol_iterator MachOObjectFile::begin_symbols() const {
// DRI.d.a = segment number; DRI.d.b = symbol index.
DataRefImpl DRI;
if (!SymtabLoadCmd)
return symbol_iterator(SymbolRef(DRI, this));
macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset));
return symbol_iterator(SymbolRef(DRI, this));
}
symbol_iterator MachOObjectFile::end_symbols() const {
DataRefImpl DRI;
if (SymtabLoadCmd) {
macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
DRI.d.b = Symtab.NumSymbolTableEntries;
}
if (!SymtabLoadCmd)
return symbol_iterator(SymbolRef(DRI, this));
macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
unsigned SymbolTableEntrySize = is64Bit() ?
sizeof(macho::Symbol64TableEntry) :
sizeof(macho::SymbolTableEntry);
unsigned Offset = Symtab.SymbolTableOffset +
Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
return symbol_iterator(SymbolRef(DRI, this));
}
@ -1369,13 +1375,13 @@ macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
macho::SymbolTableEntry
MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
const char *P = getSymbolTableEntryPtr(this, DRI);
const char *P = reinterpret_cast<const char *>(DRI.p);
return getStruct<macho::SymbolTableEntry>(this, P);
}
macho::Symbol64TableEntry
MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
const char *P = getSymbolTableEntryPtr(this, DRI);
const char *P = reinterpret_cast<const char *>(DRI.p);
return getStruct<macho::Symbol64TableEntry>(this, P);
}

View File

@ -34,8 +34,8 @@ MACHO-I386: Relocations [
MACHO-I386-NEXT: Section __text {
MACHO-I386-NEXT: 0x18 1 2 1 GENERIC_RELOC_VANILLA 0 _SomeOtherFunction
MACHO-I386-NEXT: 0x13 1 2 1 GENERIC_RELOC_VANILLA 0 _puts
MACHO-I386-NEXT: 0xB 0 2 n/a GENERIC_RELOC_LOCAL_SECTDIFF 1 _main
MACHO-I386-NEXT: 0x0 0 2 n/a GENERIC_RELOC_PAIR 1 _main
MACHO-I386-NEXT: 0xB 0 2 n/a GENERIC_RELOC_LOCAL_SECTDIFF 1 -
MACHO-I386-NEXT: 0x0 0 2 n/a GENERIC_RELOC_PAIR 1 -
MACHO-I386-NEXT: }
MACHO-I386-NEXT: ]
@ -49,17 +49,17 @@ MACHO-X86-64-NEXT:]
MACHO-PPC: Relocations [
MACHO-PPC-NEXT: Section __text {
MACHO-PPC-NEXT: 0x24 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0x1C 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x58 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0x18 1 2 0 PPC_RELOC_BR24 0 _b
MACHO-PPC-NEXT: 0x24 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: 0x1C 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x58 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: 0x18 1 2 0 PPC_RELOC_BR24 0 -
MACHO-PPC-NEXT: }
MACHO-PPC-NEXT: Section __picsymbolstub1 {
MACHO-PPC-NEXT: 0x14 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0xC 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x20 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0x14 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: 0xC 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x20 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: }
MACHO-PPC-NEXT: Section __la_symbol_ptr {
MACHO-PPC-NEXT: 0x0 0 2 1 PPC_RELOC_VANILLA 0 dyld_stub_binding_helper
@ -68,17 +68,17 @@ MACHO-PPC-NEXT: ]
MACHO-PPC64: Relocations [
MACHO-PPC64-NEXT: Section __text {
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x1C 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x58 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x18 1 2 0 0 _b
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x1C 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x58 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x18 1 2 0 0 -
MACHO-PPC64-NEXT: }
MACHO-PPC64-NEXT: Section __picsymbolstub1 {
MACHO-PPC64-NEXT: 0x14 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0xC 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x14 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0xC 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 -
MACHO-PPC64-NEXT: }
MACHO-PPC64-NEXT: Section __la_symbol_ptr {
MACHO-PPC64-NEXT: 0x0 0 3 1 0 dyld_stub_binding_helper
@ -94,7 +94,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_SECTDIFF (2)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -103,7 +103,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -130,7 +130,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 1
MACHO-ARM-NEXT: Extern: 0
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 0
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -148,7 +148,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 0
MACHO-ARM-NEXT: Extern: 0
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 0
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -157,7 +157,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_SECTDIFF (2)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -166,7 +166,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: }

View File

@ -161,8 +161,8 @@ MACHO-I386-NEXT: Reserved2: 0x0
MACHO-I386-NEXT: Relocations [
MACHO-I386-NEXT: 0x18 1 2 1 GENERIC_RELOC_VANILLA 0 _SomeOtherFunction
MACHO-I386-NEXT: 0x13 1 2 1 GENERIC_RELOC_VANILLA 0 _puts
MACHO-I386-NEXT: 0xB 0 2 n/a GENERIC_RELOC_LOCAL_SECTDIFF 1 _main
MACHO-I386-NEXT: 0x0 0 2 n/a GENERIC_RELOC_PAIR 1 _main
MACHO-I386-NEXT: 0xB 0 2 n/a GENERIC_RELOC_LOCAL_SECTDIFF 1 -
MACHO-I386-NEXT: 0x0 0 2 n/a GENERIC_RELOC_PAIR 1 -
MACHO-I386-NEXT: ]
MACHO-I386-NEXT: Symbols [
MACHO-I386-NEXT: Symbol {
@ -275,11 +275,11 @@ MACHO-PPC-NEXT: ]
MACHO-PPC-NEXT: Reserved1: 0x0
MACHO-PPC-NEXT: Reserved2: 0x0
MACHO-PPC-NEXT: Relocations [
MACHO-PPC-NEXT: 0x24 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0x1C 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x58 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0x18 1 2 0 PPC_RELOC_BR24 0 _b
MACHO-PPC-NEXT: 0x24 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: 0x1C 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x58 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: 0x18 1 2 0 PPC_RELOC_BR24 0 -
MACHO-PPC-NEXT: ]
MACHO-PPC-NEXT: Symbols [
MACHO-PPC-NEXT: Symbol {
@ -317,10 +317,10 @@ MACHO-PPC-NEXT: ]
MACHO-PPC-NEXT: Reserved1: 0x0
MACHO-PPC-NEXT: Reserved2: 0x20
MACHO-PPC-NEXT: Relocations [
MACHO-PPC-NEXT: 0x14 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0xC 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 _b
MACHO-PPC-NEXT: 0x20 0 2 n/a PPC_RELOC_PAIR 1 _b
MACHO-PPC-NEXT: 0x14 0 2 n/a PPC_RELOC_LO16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x0 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: 0xC 0 2 n/a PPC_RELOC_HA16_SECTDIFF 1 -
MACHO-PPC-NEXT: 0x20 0 2 n/a PPC_RELOC_PAIR 1 -
MACHO-PPC-NEXT: ]
MACHO-PPC-NEXT: Symbols [
MACHO-PPC-NEXT: ]
@ -430,11 +430,11 @@ MACHO-PPC64-NEXT: ]
MACHO-PPC64-NEXT: Reserved1: 0x0
MACHO-PPC64-NEXT: Reserved2: 0x0
MACHO-PPC64-NEXT: Relocations [
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x1C 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x58 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x18 1 2 0 0 _b
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x1C 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x58 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x18 1 2 0 0 -
MACHO-PPC64-NEXT: ]
MACHO-PPC64-NEXT: Symbols [
MACHO-PPC64-NEXT: Symbol {
@ -472,10 +472,10 @@ MACHO-PPC64-NEXT: ]
MACHO-PPC64-NEXT: Reserved1: 0x0
MACHO-PPC64-NEXT: Reserved2: 0x20
MACHO-PPC64-NEXT: Relocations [
MACHO-PPC64-NEXT: 0x14 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0xC 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 _b
MACHO-PPC64-NEXT: 0x14 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x0 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0xC 0 2 n/a 1 -
MACHO-PPC64-NEXT: 0x24 0 2 n/a 1 -
MACHO-PPC64-NEXT: ]
MACHO-PPC64-NEXT: Symbols [
MACHO-PPC64-NEXT: ]
@ -590,7 +590,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_SECTDIFF (2)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -599,7 +599,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -626,7 +626,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 1
MACHO-ARM-NEXT: Extern: 0
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 0
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -644,7 +644,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 0
MACHO-ARM-NEXT: Extern: 0
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 0
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -653,7 +653,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_SECTDIFF (2)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: Relocation {
@ -662,7 +662,7 @@ MACHO-ARM-NEXT: PCRel: 0
MACHO-ARM-NEXT: Length: 2
MACHO-ARM-NEXT: Extern: N/A
MACHO-ARM-NEXT: Type: ARM_RELOC_PAIR (1)
MACHO-ARM-NEXT: Symbol: _b
MACHO-ARM-NEXT: Symbol: -
MACHO-ARM-NEXT: Scattered: 1
MACHO-ARM-NEXT: }
MACHO-ARM-NEXT: ]

View File

@ -345,7 +345,9 @@ void MachODumper::printRelocation(const MachOObjectFile *Obj,
if (error(RelI->getOffset(Offset))) return;
if (error(RelI->getTypeName(RelocName))) return;
if (error(RelI->getSymbol(Symbol))) return;
if (error(Symbol.getName(SymbolName))) return;
if (symbol_iterator(Symbol) != Obj->end_symbols() &&
error(Symbol.getName(SymbolName)))
return;
DataRefImpl DR = RelI->getRawDataRefImpl();
macho::RelocationEntry RE = Obj->getRelocation(DR);