Index: llvm/test/tools/llvm-objdump/ELF/exec-relocations.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objdump/ELF/exec-relocations.test @@ -0,0 +1,71 @@ +## Check that 'llvm-objdump -dr' correctly prints relocations in executables. + +# RUN: yaml2obj --docnum=1 %s -o %t +# RUN: llvm-objdump -dr %t | FileCheck %s + +# CHECK: 40058a: bf 28 06 40 00 movl $4195880, %edi +# CHECK-NEXT: 000000000040058b: R_X86_64_32 .rodata +# CHECK-NEXT: 40058f: e8 fc fe ff ff callq 0x400490 +# CHECK-NEXT: 0000000000400590: R_X86_64_PLT32 puts + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 + Entry: 0x4004A0 +ProgramHeaders: + - Type: PT_PHDR + Flags: [ PF_R ] + VAddr: 0x400040 + Align: 0x8 + - Type: PT_LOAD + Flags: [ PF_X, PF_R ] + FirstSec: .text + LastSec: .rodata + VAddr: 0x400000 + Align: 0x200000 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x400586 + AddressAlign: 0x10 + Content: 554889E5BF28064000E8FCFEFFFFB8000000005DC3 + - Name: .rodata + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Address: 0x400628 + AddressAlign: 0x8 + Content: 00 + - Name: .rela.text + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .text + Relocations: + - Offset: 0x40058B + Symbol: .rodata + Type: R_X86_64_32 + Addend: 0 + - Offset: 0x400590 + Symbol: 'puts' + Type: R_X86_64_PLT32 + Addend: -4 +Symbols: + - Name: .rodata + Type: STT_SECTION + Section: .rodata + Value: 0x400628 + - Name: 'puts' + Type: STT_FUNC + Binding: STB_GLOBAL + - Name: main + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Value: 0x400586 + Size: 0x15 +... Index: llvm/tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- llvm/tools/llvm-objdump/llvm-objdump.cpp +++ llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -1287,6 +1287,7 @@ uint64_t Size; uint64_t Index; + uint64_t RelAdjustment = Obj->isRelocatableObject() ? 0 : SectionAddr; bool PrintedSection = false; std::vector Rels = RelocMap[Section]; std::vector::const_iterator RelCur = Rels.begin(); @@ -1431,7 +1432,8 @@ // For --reloc: print zero blocks patched by relocations, so that // relocations can be shown in the dump. if (RelCur != RelEnd) - MaxOffset = RelCur->getOffset() - Index; + MaxOffset = std::min(RelCur->getOffset() - RelAdjustment - Index, + MaxOffset); if (size_t N = countSkippableZeroBytes(Bytes.slice(Index, MaxOffset))) { @@ -1580,7 +1582,7 @@ if (Obj->getArch() != Triple::hexagon) { // Print relocation for instruction and data. while (RelCur != RelEnd) { - uint64_t Offset = RelCur->getOffset(); + uint64_t Offset = RelCur->getOffset() - RelAdjustment; // If this relocation is hidden, skip it. if (getHidden(*RelCur) || SectionAddr + Offset < StartAddress) { ++RelCur;