Skip to content

Commit dbf552c

Browse files
committed
[llvm-readelf] Make llvm-readelf more compatible with GNU readelf.
Summary: This change adds a bunch of options that GNU readelf supports. There is one breaking change when invoked as `llvm-readobj`, and three breaking changes when invoked as `llvm-readelf`: - Add --all (implies --file-header, --program-headers, etc.) - [Breaking] -a is --all instead of --arm-attributes - Add --file-header as an alias for --file-headers - Replace --sections with --sections-headers, keeping --sections as an alias for it - Add --relocs as an alias for --relocations - Add --dynamic as an alias for --dynamic-table - Add --segments as an alias for --program-headers - Add --section-groups as an alias for --elf-section-groups - Add --dyn-syms as an alias for --dyn-symbols - Add --syms as an alias for --symbols - Add --histogram as an alias for --elf-hash-histogram - [Breaking] When invoked as `llvm-readelf`, -s is --symbols instead of --sections - [Breaking] When invoked as `llvm-readelf`, -t is no longer an alias for --symbols Reviewers: MaskRay, phosek, mcgrathr, jhenderson Reviewed By: MaskRay, jhenderson Subscribers: sbc100, aheejin, edd, jhenderson, silvas, echristo, compnerd, kristina, javed.absar, kristof.beyls, llvm-commits, Bigcheese Differential Revision: https://reviews.llvm.org/D54124 llvm-svn: 346685
1 parent ec3c085 commit dbf552c

12 files changed

+220
-94
lines changed

llvm/test/MC/ARM/elf-execute-only-section.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
; RUN: llc < %s -mtriple=thumbv8m.base-eabi -mattr=+execute-only -filetype=obj %s -o - | \
2-
; RUN: llvm-readelf -s | FileCheck %s
2+
; RUN: llvm-readelf -S | FileCheck %s
33
; RUN: llc < %s -mtriple=thumbv8m.main-eabi -mattr=+execute-only -filetype=obj %s -o - | \
4-
; RUN: llvm-readelf -s | FileCheck %s
4+
; RUN: llvm-readelf -S | FileCheck %s
55
; RUN: llc < %s -mtriple=thumbv7m-eabi -mattr=+execute-only -filetype=obj %s -o - | \
6-
; RUN: llvm-readelf -s | FileCheck %s
6+
; RUN: llvm-readelf -S | FileCheck %s
77

88
; CHECK-NOT: {{.text[ ]+PROGBITS[ ]+[0-9]+ [0-9]+ [0-9]+ [0-9]+ AX[^p]}}
99
; CHECK: {{.text[ ]+PROGBITS[ ]+[0-9]+ [0-9]+ [0-9]+ [0-9]+ AXp}}

llvm/test/tools/llvm-readobj/all.test

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
RUN: llvm-readobj -a %p/Inputs/trivial.obj.elf-i386 \
2+
RUN: | FileCheck %s -check-prefix ALL
3+
RUN: llvm-readobj --all %p/Inputs/trivial.obj.elf-i386 \
4+
RUN: | FileCheck %s -check-prefix ALL
5+
6+
ALL: Format: ELF32-i386
7+
ALL: Arch: i386
8+
ALL: AddressSize: 32bit
9+
ALL: LoadName:
10+
ALL: ElfHeader {
11+
ALL: Sections [
12+
ALL: Relocations [
13+
ALL: Symbols [
14+
ALL: ProgramHeaders [
15+
ALL: Notes [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# In llvm-readobj, -s is an alias for --sections.
2+
RUN: llvm-readobj -s %p/Inputs/trivial.obj.elf-i386 \
3+
RUN: | FileCheck %s -check-prefix SEC
4+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.elf-i386 \
5+
RUN: | FileCheck %s -check-prefix SEC
6+
7+
# In llvm-readelf, -s is an alias for --symbols.
8+
RUN: llvm-readelf -s %p/Inputs/trivial.obj.elf-i386 \
9+
RUN: | FileCheck %s -check-prefix SYM
10+
RUN: llvm-readelf --symbols %p/Inputs/trivial.obj.elf-i386 \
11+
RUN: | FileCheck %s -check-prefix SYM
12+
13+
SEC: Sections [
14+
SEC-NEXT: Section {
15+
SEC-NEXT: Index: 0
16+
SEC-NEXT: Name: (0)
17+
SEC-NEXT: Type: SHT_NULL (0x0)
18+
SEC-NEXT: Flags [ (0x0)
19+
SEC-NEXT: ]
20+
SEC-NEXT: Address: 0x0
21+
SEC-NEXT: Offset: 0x0
22+
SEC-NEXT: Size: 0
23+
SEC-NEXT: Link: 0
24+
SEC-NEXT: Info: 0
25+
SEC-NEXT: AddressAlignment: 0
26+
SEC-NEXT: EntrySize: 0
27+
SEC-NEXT: }
28+
SEC-NEXT: Section {
29+
SEC-NEXT: Index: 1
30+
SEC-NEXT: Name: .text (5)
31+
SEC-NEXT: Type: SHT_PROGBITS (0x1)
32+
SEC-NEXT: Flags [ (0x6)
33+
SEC-NEXT: SHF_ALLOC (0x2)
34+
SEC-NEXT: SHF_EXECINSTR (0x4)
35+
SEC-NEXT: ]
36+
SEC-NEXT: Address: 0x0
37+
SEC-NEXT: Offset: 0x40
38+
SEC-NEXT: Size: 42
39+
SEC-NEXT: Link: 0
40+
SEC-NEXT: Info: 0
41+
SEC-NEXT: AddressAlignment: 16
42+
SEC-NEXT: EntrySize: 0
43+
SEC-NEXT: }
44+
45+
SYM: Symbol table '.symtab' contains {{.*}} entries:
46+
SYM-NEXT: Num: Value Size Type Bind Vis Ndx Name
47+
SYM-NEXT: 0: {{.*}} NOTYPE {{.*}} UND
48+
SYM-NEXT: 1: {{.*}} FILE {{.*}} trivial.ll
49+
SYM-NEXT: 2: {{.*}} OBJECT {{.*}} .L.str

llvm/test/tools/llvm-readobj/sections.test

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.coff-i386 \
1+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.coff-i386 \
22
RUN: | FileCheck %s -check-prefix COFF
3-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.elf-i386 \
3+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.elf-i386 \
44
RUN: | FileCheck %s -check-prefix ELF
5-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.elf-mipsel \
5+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.elf-mipsel \
66
RUN: | FileCheck %s -check-prefix ELF-MIPSEL
7-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.macho-i386 \
7+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.macho-i386 \
88
RUN: | FileCheck %s -check-prefix MACHO-I386
9-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.macho-x86-64 \
9+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.macho-x86-64 \
1010
RUN: | FileCheck %s -check-prefix MACHO-X86-64
11-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.macho-ppc \
11+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.macho-ppc \
1212
RUN: | FileCheck %s -check-prefix MACHO-PPC
13-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.macho-ppc64 \
13+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.macho-ppc64 \
1414
RUN: | FileCheck %s -check-prefix MACHO-PPC64
15-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.macho-arm \
15+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.macho-arm \
1616
RUN: | FileCheck %s -check-prefix MACHO-ARM
17-
RUN: llvm-readobj -s %p/Inputs/trivial.obj.wasm \
17+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.wasm \
1818
RUN: | FileCheck %s -check-prefix WASM
1919

20+
# Check flag aliases produce identical output.
21+
RUN: llvm-readobj --sections %p/Inputs/trivial.obj.elf-i386 > %t.sections
22+
RUN: llvm-readobj -S %p/Inputs/trivial.obj.elf-i386 > %t.uppers
23+
RUN: cmp %t.sections %t.uppers
24+
RUN: llvm-readobj -s %p/Inputs/trivial.obj.elf-i386 > %t.lowers
25+
RUN: cmp %t.sections %t.lowers
26+
2027
COFF: Sections [
2128
COFF-NEXT: Section {
2229
COFF-NEXT: Number: 1

llvm/test/tools/llvm-readobj/symbols.test

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
RUN: llvm-readobj -t %p/Inputs/trivial.obj.coff-i386 \
1+
RUN: llvm-readobj --symbols %p/Inputs/trivial.obj.coff-i386 \
22
RUN: | FileCheck %s -check-prefix COFF
3-
RUN: llvm-readobj -t %p/Inputs/trivial.obj.elf-i386 \
3+
RUN: llvm-readobj --symbols %p/Inputs/trivial.obj.elf-i386 \
44
RUN: | FileCheck %s -check-prefix ELF
5-
RUN: llvm-readobj -t %p/Inputs/trivial.obj.wasm \
5+
RUN: llvm-readobj --symbols %p/Inputs/trivial.obj.wasm \
66
RUN: | FileCheck %s -check-prefix WASM
77

8+
# Check flag aliases produce identical output.
9+
RUN: llvm-readobj --symbols %p/Inputs/trivial.obj.elf-i386 > %t.symbols
10+
RUN: llvm-readobj --syms %p/Inputs/trivial.obj.elf-i386 > %t.syms
11+
RUN: cmp %t.symbols %t.syms
12+
RUN: llvm-readobj -t %p/Inputs/trivial.obj.elf-i386 > %t.t
13+
RUN: cmp %t.symbols %t.t
14+
RUN: llvm-readelf -s -elf-output-style LLVM %p/Inputs/trivial.obj.elf-i386 > %t.lowers
15+
RUN: cmp %t.symbols %t.lowers
16+
817
COFF: Symbols [
918
COFF-NEXT: Symbol {
1019
COFF-NEXT: Name: @comp.id

llvm/tools/llvm-readobj/COFFDumper.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class COFFDumper : public ObjDumper {
7979
: ObjDumper(Writer), Obj(Obj), Writer(Writer), Types(100) {}
8080

8181
void printFileHeaders() override;
82-
void printSections() override;
82+
void printSectionHeaders() override;
8383
void printRelocations() override;
8484
void printSymbols() override;
8585
void printDynamicSymbols() override;
@@ -959,7 +959,7 @@ void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
959959
StringMap<StringRef> FunctionLineTables;
960960

961961
ListScope D(W, "CodeViewDebugInfo");
962-
// Print the section to allow correlation with printSections.
962+
// Print the section to allow correlation with printSectionHeaders.
963963
W.printNumber("Section", SectionName, Obj->getSectionID(Section));
964964

965965
uint32_t Magic;
@@ -1279,7 +1279,7 @@ void COFFDumper::printCodeViewTypeSection(StringRef SectionName,
12791279
W.flush();
12801280
}
12811281

1282-
void COFFDumper::printSections() {
1282+
void COFFDumper::printSectionHeaders() {
12831283
ListScope SectionsD(W, "Sections");
12841284
int SectionNumber = 0;
12851285
for (const SectionRef &Sec : Obj->sections()) {

llvm/tools/llvm-readobj/ELFDumper.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ELFDumper : public ObjDumper {
143143
ELFDumper(const ELFFile<ELFT> *Obj, ScopedPrinter &Writer);
144144

145145
void printFileHeaders() override;
146-
void printSections() override;
146+
void printSectionHeaders() override;
147147
void printRelocations() override;
148148
void printDynamicRelocations() override;
149149
void printSymbols() override;
@@ -324,7 +324,7 @@ template <typename ELFT> class DumpStyle {
324324
virtual void printFileHeaders(const ELFFile<ELFT> *Obj) = 0;
325325
virtual void printGroupSections(const ELFFile<ELFT> *Obj) = 0;
326326
virtual void printRelocations(const ELFFile<ELFT> *Obj) = 0;
327-
virtual void printSections(const ELFFile<ELFT> *Obj) = 0;
327+
virtual void printSectionHeaders(const ELFFile<ELFT> *Obj) = 0;
328328
virtual void printSymbols(const ELFFile<ELFT> *Obj) = 0;
329329
virtual void printDynamicSymbols(const ELFFile<ELFT> *Obj) = 0;
330330
virtual void printDynamicRelocations(const ELFFile<ELFT> *Obj) = 0;
@@ -359,7 +359,7 @@ template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
359359
void printFileHeaders(const ELFO *Obj) override;
360360
void printGroupSections(const ELFFile<ELFT> *Obj) override;
361361
void printRelocations(const ELFO *Obj) override;
362-
void printSections(const ELFO *Obj) override;
362+
void printSectionHeaders(const ELFO *Obj) override;
363363
void printSymbols(const ELFO *Obj) override;
364364
void printDynamicSymbols(const ELFO *Obj) override;
365365
void printDynamicRelocations(const ELFO *Obj) override;
@@ -452,7 +452,7 @@ template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
452452
void printGroupSections(const ELFFile<ELFT> *Obj) override;
453453
void printRelocations(const ELFO *Obj) override;
454454
void printRelocations(const Elf_Shdr *Sec, const ELFO *Obj);
455-
void printSections(const ELFO *Obj) override;
455+
void printSectionHeaders(const ELFO *Obj) override;
456456
void printSymbols(const ELFO *Obj) override;
457457
void printDynamicSymbols(const ELFO *Obj) override;
458458
void printDynamicRelocations(const ELFO *Obj) override;
@@ -1592,9 +1592,8 @@ void ELFDumper<ELFT>::printFileHeaders() {
15921592
ELFDumperStyle->printFileHeaders(Obj);
15931593
}
15941594

1595-
template<class ELFT>
1596-
void ELFDumper<ELFT>::printSections() {
1597-
ELFDumperStyle->printSections(Obj);
1595+
template <class ELFT> void ELFDumper<ELFT>::printSectionHeaders() {
1596+
ELFDumperStyle->printSectionHeaders(Obj);
15981597
}
15991598

16001599
template<class ELFT>
@@ -2920,7 +2919,8 @@ std::string getSectionTypeString(unsigned Arch, unsigned Type) {
29202919
return "";
29212920
}
29222921

2923-
template <class ELFT> void GNUStyle<ELFT>::printSections(const ELFO *Obj) {
2922+
template <class ELFT>
2923+
void GNUStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
29242924
size_t SectionIndex = 0;
29252925
std::string Number, Type, Size, Address, Offset, Flags, Link, Info, EntrySize,
29262926
Alignment;
@@ -4213,7 +4213,8 @@ void LLVMStyle<ELFT>::printRelocation(const ELFO *Obj, Elf_Rela Rel,
42134213
}
42144214
}
42154215

4216-
template <class ELFT> void LLVMStyle<ELFT>::printSections(const ELFO *Obj) {
4216+
template <class ELFT>
4217+
void LLVMStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
42174218
ListScope SectionsD(W, "Sections");
42184219

42194220
int SectionIndex = -1;

llvm/tools/llvm-readobj/MachODumper.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class MachODumper : public ObjDumper {
3232
: ObjDumper(Writer), Obj(Obj) {}
3333

3434
void printFileHeaders() override;
35-
void printSections() override;
35+
void printSectionHeaders() override;
3636
void printRelocations() override;
3737
void printSymbols() override;
3838
void printDynamicSymbols() override;
@@ -59,7 +59,7 @@ class MachODumper : public ObjDumper {
5959

6060
void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
6161

62-
void printSections(const MachOObjectFile *Obj);
62+
void printSectionHeaders(const MachOObjectFile *Obj);
6363

6464
const MachOObjectFile *Obj;
6565
};
@@ -428,11 +428,9 @@ void MachODumper::printFileHeaders(const MachHeader &Header) {
428428
W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags));
429429
}
430430

431-
void MachODumper::printSections() {
432-
return printSections(Obj);
433-
}
431+
void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj); }
434432

435-
void MachODumper::printSections(const MachOObjectFile *Obj) {
433+
void MachODumper::printSectionHeaders(const MachOObjectFile *Obj) {
436434
ListScope Group(W, "Sections");
437435

438436
int SectionIndex = -1;

llvm/tools/llvm-readobj/ObjDumper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ObjDumper {
3333
virtual ~ObjDumper();
3434

3535
virtual void printFileHeaders() = 0;
36-
virtual void printSections() = 0;
36+
virtual void printSectionHeaders() = 0;
3737
virtual void printRelocations() = 0;
3838
virtual void printSymbols() = 0;
3939
virtual void printDynamicSymbols() = 0;

llvm/tools/llvm-readobj/WasmDumper.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class WasmDumper : public ObjDumper {
4848
: ObjDumper(Writer), Obj(Obj) {}
4949

5050
void printFileHeaders() override;
51-
void printSections() override;
51+
void printSectionHeaders() override;
5252
void printRelocations() override;
5353
void printSymbols() override;
5454
void printDynamicSymbols() override { llvm_unreachable("unimplemented"); }
@@ -148,7 +148,7 @@ void WasmDumper::printSymbols() {
148148
printSymbol(Symbol);
149149
}
150150

151-
void WasmDumper::printSections() {
151+
void WasmDumper::printSectionHeaders() {
152152
ListScope Group(W, "Sections");
153153
for (const SectionRef &Section : Obj->sections()) {
154154
const WasmSection &WasmSec = Obj->getWasmSection(Section);

0 commit comments

Comments
 (0)