Skip to content

Commit 496fb2f

Browse files
author
Igor Kudrin
committedOct 14, 2015
[llvm-readobj/ELF] Print GNU Hash section
Add a new command line switch, -gnu-hash-table, to print the content of that section. Differential Revision: http://reviews.llvm.org/D13696 llvm-svn: 250291
1 parent dee8834 commit 496fb2f

File tree

11 files changed

+132
-0
lines changed

11 files changed

+132
-0
lines changed
 

‎llvm/include/llvm/Object/ELF.h

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ELFFile {
5353
typedef Elf_Vernaux_Impl<ELFT> Elf_Vernaux;
5454
typedef Elf_Versym_Impl<ELFT> Elf_Versym;
5555
typedef Elf_Hash_Impl<ELFT> Elf_Hash;
56+
typedef Elf_GnuHash_Impl<ELFT> Elf_GnuHash;
5657
typedef iterator_range<const Elf_Dyn *> Elf_Dyn_Range;
5758
typedef iterator_range<const Elf_Shdr *> Elf_Shdr_Range;
5859
typedef iterator_range<const Elf_Sym *> Elf_Sym_Range;

‎llvm/include/llvm/Object/ELFTypes.h

+24
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,30 @@ struct Elf_Hash_Impl {
484484
}
485485
};
486486

487+
// .gnu.hash section
488+
template <class ELFT>
489+
struct Elf_GnuHash_Impl {
490+
LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
491+
Elf_Word nbuckets;
492+
Elf_Word symndx;
493+
Elf_Word maskwords;
494+
Elf_Word shift2;
495+
496+
ArrayRef<Elf_Off> filter() const {
497+
return ArrayRef<Elf_Off>(reinterpret_cast<const Elf_Off *>(&shift2 + 1),
498+
maskwords);
499+
}
500+
501+
ArrayRef<Elf_Word> buckets() const {
502+
return ArrayRef<Elf_Word>(
503+
reinterpret_cast<const Elf_Word *>(filter().end()), nbuckets);
504+
}
505+
506+
ArrayRef<Elf_Word> values(unsigned DynamicSymCount) const {
507+
return ArrayRef<Elf_Word>(buckets().end(), DynamicSymCount - symndx);
508+
}
509+
};
510+
487511
// MIPS .reginfo section
488512
template <class ELFT>
489513
struct Elf_Mips_RegInfo;

‎llvm/test/tools/llvm-readobj/Inputs/gnuhash.so.elf-i386

Whitespace-only changes.

‎llvm/test/tools/llvm-readobj/Inputs/gnuhash.so.elf-ppc

Whitespace-only changes.

‎llvm/test/tools/llvm-readobj/Inputs/gnuhash.so.elf-ppc64

Whitespace-only changes.

‎llvm/test/tools/llvm-readobj/Inputs/gnuhash.so.elf-x86_64

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Check dumping of the GNU Hash section
2+
// The input was generated using the following:
3+
// $ llvm-mc -filetype=obj -triple=i386-pc-linux -o example-i386.o example.s
4+
// $ llvm-mc -filetype=obj -triple=x86_64-pc-linux -o example-x86_64.o example.s
5+
// $ llvm-mc -filetype=obj -triple=powerpc-pc-linux -o example-ppc.o example.s
6+
// $ llvm-mc -filetype=obj -triple=powerpc64-pc-linux -o example-ppc64.o example.s
7+
// $ ld -shared -m elf_i386 -hash-style=gnu -o gnuhash.so.elf-i386 example-i386.o
8+
// $ ld -shared -m elf_x86_64 -hash-style=gnu -o gnuhash.so.elf-x86_64 example-x86_64.o
9+
// $ ld -shared -m elf32ppc -hash-style=gnu -o gnuhash.so.elf-ppc example-ppc.o
10+
// $ ld -shared -m elf64ppc -hash-style=gnu -o gnuhash.so.elf-ppc64 example-ppc64.o
11+
// $ cat example.s
12+
// .globl foo
13+
// foo:
14+
15+
RUN: llvm-readobj -gnu-hash-table %p/Inputs/gnuhash.so.elf-i386 | FileCheck %s -check-prefix I386
16+
RUN: llvm-readobj -gnu-hash-table %p/Inputs/gnuhash.so.elf-x86_64 | FileCheck %s -check-prefix X86_64
17+
RUN: llvm-readobj -gnu-hash-table %p/Inputs/gnuhash.so.elf-ppc | FileCheck %s -check-prefix PPC
18+
RUN: llvm-readobj -gnu-hash-table %p/Inputs/gnuhash.so.elf-ppc64 | FileCheck %s -check-prefix PPC64
19+
20+
I386: Arch: i386
21+
I386: GnuHashTable {
22+
I386-NEXT: Num Buckets: 3
23+
I386-NEXT: First Hashed Symbol Index: 1
24+
I386-NEXT: Num Mask Words: 1
25+
I386-NEXT: Shift Count: 5
26+
I386-NEXT: Bloom Filter: [0x39004608]
27+
I386-NEXT: Buckets: [1, 4, 0]
28+
I386-NEXT: Values: [0xB887388, 0xECD54542, 0x7C92E3BB, 0x1C5871D9]
29+
I386-NEXT: }
30+
31+
X86_64: Arch: x86_64
32+
X86_64: GnuHashTable {
33+
X86_64-NEXT: Num Buckets: 3
34+
X86_64-NEXT: First Hashed Symbol Index: 1
35+
X86_64-NEXT: Num Mask Words: 1
36+
X86_64-NEXT: Shift Count: 6
37+
X86_64-NEXT: Bloom Filter: [0x800000001204288]
38+
X86_64-NEXT: Buckets: [1, 4, 0]
39+
X86_64-NEXT: Values: [0xB887388, 0xECD54542, 0x7C92E3BB, 0x1C5871D9]
40+
X86_64-NEXT: }
41+
42+
PPC: Arch: powerpc
43+
PPC: GnuHashTable {
44+
PPC-NEXT: Num Buckets: 3
45+
PPC-NEXT: First Hashed Symbol Index: 1
46+
PPC-NEXT: Num Mask Words: 1
47+
PPC-NEXT: Shift Count: 5
48+
PPC-NEXT: Bloom Filter: [0x3D00460A]
49+
PPC-NEXT: Buckets: [1, 5, 0]
50+
PPC-NEXT: Values: [0xEEBEC3A, 0xB887388, 0xECD54542, 0x7C92E3BB, 0x1C5871D9]
51+
PPC-NEXT: }
52+
53+
PPC64: Arch: powerpc64
54+
PPC64: GnuHashTable {
55+
PPC64-NEXT: Num Buckets: 3
56+
PPC64-NEXT: First Hashed Symbol Index: 1
57+
PPC64-NEXT: Num Mask Words: 1
58+
PPC64-NEXT: Shift Count: 6
59+
PPC64-NEXT: Bloom Filter: [0x800000001204288]
60+
PPC64-NEXT: Buckets: [1, 4, 0]
61+
PPC64-NEXT: Values: [0xB887388, 0xECD54542, 0x7C92E3BB, 0x1C5871D9]
62+
PPC64-NEXT: }
63+

‎llvm/tools/llvm-readobj/ELFDumper.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class ELFDumper : public ObjDumper {
5656
void printNeededLibraries() override;
5757
void printProgramHeaders() override;
5858
void printHashTable() override;
59+
void printGnuHashTable() override;
5960
void printLoadName() override;
6061

6162
void printAttributes() override;
@@ -76,6 +77,7 @@ class ELFDumper : public ObjDumper {
7677
typedef typename ELFO::Elf_Rela_Range Elf_Rela_Range;
7778
typedef typename ELFO::Elf_Phdr Elf_Phdr;
7879
typedef typename ELFO::Elf_Hash Elf_Hash;
80+
typedef typename ELFO::Elf_GnuHash Elf_GnuHash;
7981
typedef typename ELFO::Elf_Ehdr Elf_Ehdr;
8082
typedef typename ELFO::Elf_Word Elf_Word;
8183
typedef typename ELFO::uintX_t uintX_t;
@@ -136,6 +138,7 @@ class ELFDumper : public ObjDumper {
136138
const Elf_Sym *DynSymStart = nullptr;
137139
StringRef SOName;
138140
const Elf_Hash *HashTable = nullptr;
141+
const Elf_GnuHash *GnuHashTable = nullptr;
139142
const Elf_Shdr *DotDynSymSec = nullptr;
140143
const Elf_Shdr *DotSymtabSec = nullptr;
141144
ArrayRef<Elf_Word> ShndxTable;
@@ -850,6 +853,10 @@ ELFDumper<ELFT>::ELFDumper(const ELFFile<ELFT> *Obj, StreamWriter &Writer)
850853
HashTable =
851854
reinterpret_cast<const Elf_Hash *>(toMappedAddr(Dyn.getPtr()));
852855
break;
856+
case ELF::DT_GNU_HASH:
857+
GnuHashTable =
858+
reinterpret_cast<const Elf_GnuHash *>(toMappedAddr(Dyn.getPtr()));
859+
break;
853860
case ELF::DT_RELA:
854861
DynRelaRegion.Addr = toMappedAddr(Dyn.getPtr());
855862
break;
@@ -1533,6 +1540,23 @@ void ELFDumper<ELFT>::printHashTable() {
15331540
W.printList("Chains", HashTable->chains());
15341541
}
15351542

1543+
template <typename ELFT>
1544+
void ELFDumper<ELFT>::printGnuHashTable() {
1545+
DictScope D(W, "GnuHashTable");
1546+
if (!GnuHashTable)
1547+
return;
1548+
W.printNumber("Num Buckets", GnuHashTable->nbuckets);
1549+
W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx);
1550+
W.printNumber("Num Mask Words", GnuHashTable->maskwords);
1551+
W.printNumber("Shift Count", GnuHashTable->shift2);
1552+
W.printHexList("Bloom Filter", GnuHashTable->filter());
1553+
W.printList("Buckets", GnuHashTable->buckets());
1554+
if (!DotDynSymSec)
1555+
reportError("No dynamic symbol section");
1556+
W.printHexList("Values",
1557+
GnuHashTable->values(DotDynSymSec->getEntityCount()));
1558+
}
1559+
15361560
template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
15371561
outs() << "LoadName: " << SOName << '\n';
15381562
}

‎llvm/tools/llvm-readobj/ObjDumper.h

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ObjDumper {
3939
virtual void printNeededLibraries() { }
4040
virtual void printProgramHeaders() { }
4141
virtual void printHashTable() { }
42+
virtual void printGnuHashTable() { }
4243
virtual void printLoadName() {}
4344

4445
// Only implemented for ARM ELF at this time.

‎llvm/tools/llvm-readobj/StreamWriter.h

+13
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ class StreamWriter {
194194
OS << "]\n";
195195
}
196196

197+
template <typename T>
198+
void printHexList(StringRef Label, const T &List) {
199+
startLine() << Label << ": [";
200+
bool Comma = false;
201+
for (const auto &Item : List) {
202+
if (Comma)
203+
OS << ", ";
204+
OS << hex(Item);
205+
Comma = true;
206+
}
207+
OS << "]\n";
208+
}
209+
197210
template<typename T>
198211
void printHex(StringRef Label, T Value) {
199212
startLine() << Label << ": " << hex(Value) << "\n";

‎llvm/tools/llvm-readobj/llvm-readobj.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ namespace opts {
132132
cl::opt<bool> HashTable("hash-table",
133133
cl::desc("Display ELF hash table"));
134134

135+
// -gnu-hash-table
136+
cl::opt<bool> GnuHashTable("gnu-hash-table",
137+
cl::desc("Display ELF .gnu.hash section"));
138+
135139
// -expand-relocs
136140
cl::opt<bool> ExpandRelocs("expand-relocs",
137141
cl::desc("Expand each shown relocation to multiple lines"));
@@ -322,6 +326,8 @@ static void dumpObject(const ObjectFile *Obj) {
322326
Dumper->printProgramHeaders();
323327
if (opts::HashTable)
324328
Dumper->printHashTable();
329+
if (opts::GnuHashTable)
330+
Dumper->printGnuHashTable();
325331
if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
326332
if (opts::ARMAttributes)
327333
Dumper->printAttributes();

0 commit comments

Comments
 (0)
Please sign in to comment.