[llvm-readobj] [COFF] Print the symbol index for relocations

There can be multiple local symbols with the same name (for e.g.
comdat sections), and thus the symbol name itself isn't enough
to disambiguate symbols.

Differential Revision: https://reviews.llvm.org/D56140

llvm-svn: 350288
This commit is contained in:
Martin Storsjo 2019-01-03 08:08:23 +00:00
parent 5ef47ad82e
commit 74e7d26090
6 changed files with 26 additions and 3 deletions

View File

@ -1022,6 +1022,8 @@ public:
ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
uint32_t getSymbolIndex(COFFSymbolRef Symbol) const;
size_t getSymbolTableEntrySize() const {
if (COFFHeader)
return sizeof(coff_symbol16);

View File

@ -1065,6 +1065,16 @@ COFFObjectFile::getSymbolAuxData(COFFSymbolRef Symbol) const {
return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
}
uint32_t COFFObjectFile::getSymbolIndex(COFFSymbolRef Symbol) const {
uintptr_t Offset =
reinterpret_cast<uintptr_t>(Symbol.getRawPtr()) - getSymbolTable();
assert(Offset % getSymbolTableEntrySize() == 0 &&
"Symbol did not point to the beginning of a symbol");
size_t Index = Offset / getSymbolTableEntrySize();
assert(Index < getNumberOfSymbols());
return Index;
}
std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
StringRef &Res) const {
StringRef Name;

View File

@ -87,21 +87,25 @@ t6:
// READOBJ-NEXT: Offset: 0x0
// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
// READOBJ-NEXT: Symbol: g3
// READOBJ-NEXT: SymbolIndex: 12
// READOBJ-NEXT: }
// READOBJ-NEXT: Relocation {
// READOBJ-NEXT: Offset: 0x4
// READOBJ-NEXT: Type: IMAGE_REL_AMD64_ADDR32NB (3)
// READOBJ-NEXT: Symbol: g3
// READOBJ-NEXT: SymbolIndex: 12
// READOBJ-NEXT: }
// READOBJ-NEXT: Relocation {
// READOBJ-NEXT: Offset: 0x20
// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
// READOBJ-NEXT: Symbol: g3
// READOBJ-NEXT: SymbolIndex: 12
// READOBJ-NEXT: }
// READOBJ-NEXT: Relocation {
// READOBJ-NEXT: Offset: 0x28
// READOBJ-NEXT: Type: IMAGE_REL_AMD64_REL32 (4)
// READOBJ-NEXT: Symbol: foobar
// READOBJ-NEXT: SymbolIndex: 20
// READOBJ-NEXT: }
// READOBJ-NEXT: }
// READOBJ-NEXT:]

View File

@ -13,16 +13,19 @@ RUN: | FileCheck %s --check-prefix COFF-I386
// COFF-I386-NEXT: Offset: 0xE
// COFF-I386-NEXT: Type: IMAGE_REL_I386_DIR32 (6)
// COFF-I386-NEXT: Symbol: L_.str
// COFF-I386-NEXT: SymbolIndex: 5
// COFF-I386-NEXT: }
// COFF-I386-NEXT: Relocation {
// COFF-I386-NEXT: Offset: 0x13
// COFF-I386-NEXT: Type: IMAGE_REL_I386_REL32 (20)
// COFF-I386-NEXT: Symbol: _puts
// COFF-I386-NEXT: SymbolIndex: 6
// COFF-I386-NEXT: }
// COFF-I386-NEXT: Relocation {
// COFF-I386-NEXT: Offset: 0x18
// COFF-I386-NEXT: Type: IMAGE_REL_I386_REL32 (20)
// COFF-I386-NEXT: Symbol: _SomeOtherFunction
// COFF-I386-NEXT: SymbolIndex: 7
// COFF-I386-NEXT: }
// COFF-I386-NEXT: }
// COFF-I386-NEXT: ]

View File

@ -21,9 +21,9 @@ RUN: | FileCheck %s -check-prefix WASM
COFF: Relocations [
COFF-NEXT: Section (1) .text {
COFF-NEXT: 0x4 IMAGE_REL_I386_DIR32 .data
COFF-NEXT: 0x9 IMAGE_REL_I386_REL32 _puts
COFF-NEXT: 0xE IMAGE_REL_I386_REL32 _SomeOtherFunction
COFF-NEXT: 0x4 IMAGE_REL_I386_DIR32 .data (4)
COFF-NEXT: 0x9 IMAGE_REL_I386_REL32 _puts (7)
COFF-NEXT: 0xE IMAGE_REL_I386_REL32 _SomeOtherFunction (8)
COFF-NEXT: }
COFF-NEXT: ]

View File

@ -1365,10 +1365,12 @@ void COFFDumper::printRelocation(const SectionRef &Section,
StringRef SymbolName;
Reloc.getTypeName(RelocName);
symbol_iterator Symbol = Reloc.getSymbol();
int64_t SymbolIndex = -1;
if (Symbol != Obj->symbol_end()) {
Expected<StringRef> SymbolNameOrErr = Symbol->getName();
error(errorToErrorCode(SymbolNameOrErr.takeError()));
SymbolName = *SymbolNameOrErr;
SymbolIndex = Obj->getSymbolIndex(Obj->getCOFFSymbol(*Symbol));
}
if (opts::ExpandRelocs) {
@ -1376,11 +1378,13 @@ void COFFDumper::printRelocation(const SectionRef &Section,
W.printHex("Offset", Offset);
W.printNumber("Type", RelocName, RelocType);
W.printString("Symbol", SymbolName.empty() ? "-" : SymbolName);
W.printNumber("SymbolIndex", SymbolIndex);
} else {
raw_ostream& OS = W.startLine();
OS << W.hex(Offset)
<< " " << RelocName
<< " " << (SymbolName.empty() ? "-" : SymbolName)
<< " (" << SymbolIndex << ")"
<< "\n";
}
}