Index: llvm/trunk/include/llvm/Object/COFF.h =================================================================== --- llvm/trunk/include/llvm/Object/COFF.h +++ llvm/trunk/include/llvm/Object/COFF.h @@ -250,6 +250,13 @@ support::ulittle16_t NumberOfRelocations; support::ulittle16_t NumberOfLinenumbers; support::ulittle32_t Characteristics; + + // Returns true if the actual number of relocations is stored in + // VirtualAddress field of the first relocation table entry. + bool hasExtendedRelocations() const { + return Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL && + NumberOfRelocations == UINT16_MAX; + }; }; struct coff_relocation { Index: llvm/trunk/lib/Object/COFFObjectFile.cpp =================================================================== --- llvm/trunk/lib/Object/COFFObjectFile.cpp +++ llvm/trunk/lib/Object/COFFObjectFile.cpp @@ -367,25 +367,46 @@ relocation_iterator COFFObjectFile::section_rel_begin(DataRefImpl Ref) const { const coff_section *Sec = toSec(Ref); DataRefImpl Ret; - if (Sec->NumberOfRelocations == 0) + if (Sec->NumberOfRelocations == 0) { Ret.p = 0; - else - Ret.p = reinterpret_cast(base() + Sec->PointerToRelocations); - + } else { + auto begin = reinterpret_cast( + base() + Sec->PointerToRelocations); + if (Sec->hasExtendedRelocations()) { + // Skip the first relocation entry repurposed to store the number of + // relocations. + begin++; + } + Ret.p = reinterpret_cast(begin); + } return relocation_iterator(RelocationRef(Ret, this)); } +static uint32_t getNumberOfRelocations(const coff_section *Sec, + const uint8_t *base) { + // The field for the number of relocations in COFF section table is only + // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to + // NumberOfRelocations field, and the actual relocation count is stored in the + // VirtualAddress field in the first relocation entry. + if (Sec->hasExtendedRelocations()) { + auto *FirstReloc = reinterpret_cast( + base + Sec->PointerToRelocations); + return FirstReloc->VirtualAddress; + } + return Sec->NumberOfRelocations; +} + relocation_iterator COFFObjectFile::section_rel_end(DataRefImpl Ref) const { const coff_section *Sec = toSec(Ref); DataRefImpl Ret; - if (Sec->NumberOfRelocations == 0) + if (Sec->NumberOfRelocations == 0) { Ret.p = 0; - else - Ret.p = reinterpret_cast( - reinterpret_cast( - base() + Sec->PointerToRelocations) - + Sec->NumberOfRelocations); - + } else { + auto begin = reinterpret_cast( + base() + Sec->PointerToRelocations); + uint32_t NumReloc = getNumberOfRelocations(Sec, base()); + Ret.p = reinterpret_cast(begin + NumReloc); + } return relocation_iterator(RelocationRef(Ret, this)); } Index: llvm/trunk/test/tools/llvm-objdump/coff-many-relocs.test =================================================================== --- llvm/trunk/test/tools/llvm-objdump/coff-many-relocs.test +++ llvm/trunk/test/tools/llvm-objdump/coff-many-relocs.test @@ -0,0 +1,14 @@ +// Test that llvm-objdump can handle IMAGE_SCN_LNK_NRELOC_OVFL. +// RUN: llvm-objdump -r %p/Inputs/many-relocs.obj-i386 | FileCheck %s + +CHECK: RELOCATION RECORDS FOR [.text]: +CHECK-NEXT: IMAGE_REL_I386_DIR16 foo +CHECK-NEXT: IMAGE_REL_I386_REL16 foo +CHECK-NEXT: IMAGE_REL_I386_DIR32 foo +CHECK-NEXT: IMAGE_REL_I386_DIR32NB foo +CHECK-NEXT: IMAGE_REL_I386_SEG12 foo +CHECK-NEXT: IMAGE_REL_I386_SECTION foo +CHECK-NEXT: IMAGE_REL_I386_SECREL foo +CHECK-NEXT: IMAGE_REL_I386_TOKEN foo +CHECK-NEXT: IMAGE_REL_I386_SECREL7 foo +CHECK-NEXT: IMAGE_REL_I386_REL32 foo