Skip to content

Commit f80a432

Browse files
committed
[llvm-objdump] -r: print non-SHF_ALLOC relocations for non-ET_REL files
ET_EXEC and ET_DYN files may contain non-SHF_ALLOC relocation sections (e.g. ld --emit-relocs). Match GNU objdump by dumping them. * Remove Object/dynamic-reloc.test. Replace it with a -r RUN line in dynamic-relocs.test * Update relocations-in-nonreloc.test to set sh_link/sh_info. GNU objdump seems to ignore a SHT_REL/SHT_RELA section not linking to SHT_SYMTAB. The test did not test what it intended to test. Fix llvm#41246 Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D128959
1 parent 38bcd48 commit f80a432

File tree

5 files changed

+46
-75
lines changed

5 files changed

+46
-75
lines changed

llvm/test/Object/dynamic-reloc.test

Lines changed: 0 additions & 12 deletions
This file was deleted.

llvm/test/tools/llvm-objdump/ELF/dynamic-relocs.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ FileHeader:
2929
# CHECK-NEXT:0000000000000008 R_X86_64_NONE foo
3030
#CHECK-EMPTY:
3131

32+
## -r ignores dynamic relocations.
33+
# RUN: llvm-objdump -r %t2 | FileCheck %s --check-prefix=STATIC-RELOC
34+
35+
# STATIC-RELOC-NOT: RELOCATION RECORDS
36+
3237
--- !ELF
3338
FileHeader:
3439
Class: ELFCLASS64
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Check that llvm-objdump -r prints non-SHF_ALLOC relocations.
2+
3+
## Non-SHF_ALLOC relocations may be due to ld --emit-relocs.
4+
# RUN: yaml2obj -DTYPE=ET_EXEC %s -o %t1
5+
# RUN: llvm-objdump -r %t1 | FileCheck %s -DFILE=%t1
6+
# RUN: yaml2obj -DTYPE=ET_DYN %s -o %t2
7+
# RUN: llvm-objdump -r %t2 | FileCheck %s -DFILE=%t2
8+
## Non-SHF_ALLOC relocations are usually generated by compilers.
9+
# RUN: yaml2obj -DTYPE=ET_REL %s -o %t3
10+
# RUN: llvm-objdump -r %t3 | FileCheck %s -DFILE=%t3
11+
12+
# CHECK: [[FILE]]: file format elf64-x86-64
13+
# CHECK-EMPTY:
14+
# CHECK-NEXT: RELOCATION RECORDS FOR [.text]:
15+
# CHECK-NEXT: OFFSET TYPE VALUE
16+
# CHECK-NEXT: 0000000000000123 R_X86_64_NONE *ABS*+0x141
17+
18+
--- !ELF
19+
FileHeader:
20+
Class: ELFCLASS64
21+
Data: ELFDATA2LSB
22+
Type: [[TYPE]]
23+
Machine: EM_X86_64
24+
Sections:
25+
- Name: .text
26+
Type: SHT_PROGBITS
27+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
28+
- Name: .rela.text
29+
Type: SHT_RELA
30+
Link: .symtab
31+
Info: .text
32+
Relocations:
33+
- Offset: 0x123
34+
Type: R_X86_64_NONE
35+
Addend: 321
36+
Symbols:
37+
- Name: .text
38+
Type: STT_SECTION
39+
Section: .text

llvm/test/tools/llvm-objdump/relocations-in-nonreloc.test

Lines changed: 0 additions & 59 deletions
This file was deleted.

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,17 +1780,15 @@ static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
17801780
void objdump::printRelocations(const ObjectFile *Obj) {
17811781
StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
17821782
"%08" PRIx64;
1783-
// Regular objdump doesn't print relocations in non-relocatable object
1784-
// files.
1785-
if (!Obj->isRelocatableObject())
1786-
return;
17871783

17881784
// Build a mapping from relocation target to a vector of relocation
17891785
// sections. Usually, there is an only one relocation section for
17901786
// each relocated section.
17911787
MapVector<SectionRef, std::vector<SectionRef>> SecToRelSec;
17921788
uint64_t Ndx;
17931789
for (const SectionRef &Section : ToolSectionFilter(*Obj, &Ndx)) {
1790+
if (Obj->isELF() && (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC))
1791+
continue;
17941792
if (Section.relocation_begin() == Section.relocation_end())
17951793
continue;
17961794
Expected<section_iterator> SecOrErr = Section.getRelocatedSection();

0 commit comments

Comments
 (0)