Index: include/llvm/Object/COFF.h =================================================================== --- include/llvm/Object/COFF.h +++ include/llvm/Object/COFF.h @@ -281,13 +281,15 @@ class COFFSymbolRef { public: COFFSymbolRef() = default; - COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS) {} - COFFSymbolRef(const coff_symbol32 *CS) : CS32(CS) {} + COFFSymbolRef(const coff_symbol16 *CS, size_t I) : CS16(CS), Index(I) {} + COFFSymbolRef(const coff_symbol32 *CS, size_t I) : CS32(CS), Index(I) {} const void *getRawPtr() const { return CS16 ? static_cast(CS16) : CS32; } + size_t getIndex() const { return Index; } + const coff_symbol_generic *getGeneric() const { if (CS16) return reinterpret_cast(CS16); @@ -433,6 +435,7 @@ const coff_symbol16 *CS16 = nullptr; const coff_symbol32 *CS32 = nullptr; + size_t Index = 0; }; struct coff_section { @@ -996,13 +999,13 @@ const coff_symbol16 *Symb = nullptr; if (std::error_code EC = getSymbol(index, Symb)) return errorCodeToError(EC); - return COFFSymbolRef(Symb); + return COFFSymbolRef(Symb, index); } if (SymbolTable32) { const coff_symbol32 *Symb = nullptr; if (std::error_code EC = getSymbol(index, Symb)) return errorCodeToError(EC); - return COFFSymbolRef(Symb); + return COFFSymbolRef(Symb, index); } return errorCodeToError(object_error::parse_failed); } Index: lib/Object/COFFObjectFile.cpp =================================================================== --- lib/Object/COFFObjectFile.cpp +++ lib/Object/COFFObjectFile.cpp @@ -1166,10 +1166,14 @@ } COFFSymbolRef COFFObjectFile::getCOFFSymbol(const DataRefImpl &Ref) const { + uintptr_t Offset = uintptr_t(Ref.p) - getSymbolTable(); + assert(Offset % getSymbolTableEntrySize() == 0 && + "Symbol did not point to the beginning of a symbol"); + size_t Index = Offset / getSymbolTableEntrySize(); if (SymbolTable16) - return toSymb(Ref); + return COFFSymbolRef(toSymb(Ref), Index); if (SymbolTable32) - return toSymb(Ref); + return COFFSymbolRef(toSymb(Ref), Index); llvm_unreachable("no symbol table pointer!"); } Index: test/MC/COFF/cross-section-relative.s =================================================================== --- test/MC/COFF/cross-section-relative.s +++ test/MC/COFF/cross-section-relative.s @@ -87,21 +87,25 @@ // 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:] Index: test/Object/yaml2obj-readobj.test =================================================================== --- test/Object/yaml2obj-readobj.test +++ test/Object/yaml2obj-readobj.test @@ -13,16 +13,19 @@ // 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: ] Index: test/tools/llvm-readobj/relocations.test =================================================================== --- test/tools/llvm-readobj/relocations.test +++ test/tools/llvm-readobj/relocations.test @@ -21,9 +21,9 @@ 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: ] Index: tools/llvm-readobj/COFFDumper.cpp =================================================================== --- tools/llvm-readobj/COFFDumper.cpp +++ tools/llvm-readobj/COFFDumper.cpp @@ -1365,10 +1365,12 @@ StringRef SymbolName; Reloc.getTypeName(RelocName); symbol_iterator Symbol = Reloc.getSymbol(); + size_t SymbolIndex = 0; if (Symbol != Obj->symbol_end()) { Expected SymbolNameOrErr = Symbol->getName(); error(errorToErrorCode(SymbolNameOrErr.takeError())); SymbolName = *SymbolNameOrErr; + SymbolIndex = Obj->getCOFFSymbol(*Symbol).getIndex(); } if (opts::ExpandRelocs) { @@ -1376,11 +1378,13 @@ 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"; } }