Skip to content

Commit f835fcf

Browse files
author
George Rimar
committedMay 24, 2019
[llvm-readelf] - Allow dumping of the .dynamic section even if there is no PT_DYNAMIC header.
It is now possible after D61937 was landed and was discussed in it's review comments. It is not consistent with GNU, which does not output .dynamic section content in this case for no visible reason. Differential revision: https://reviews.llvm.org/D62179 llvm-svn: 361630
1 parent b7f2a2b commit f835fcf

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed
 

‎llvm/test/tools/llvm-readobj/elf-dynamic-no-pt-dynamic.test

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
# Show that no dumping occurs if there is no PT_DYNAMIC header.
1+
## Show that dumping occurs even if there is no PT_DYNAMIC header.
2+
## This is inconsistent with the GNU behavior, but seems to be more reasonable.
23
# RUN: yaml2obj %s -o %t.no-phdr
34
# RUN: llvm-readobj --dynamic-table %t.no-phdr | FileCheck %s --check-prefix=LLVM
4-
# RUN: llvm-readelf --dynamic-table %t.no-phdr | FileCheck %s --check-prefix=GNU --allow-empty
5+
# RUN: llvm-readelf --dynamic-table %t.no-phdr | FileCheck %s --check-prefix=GNU
56

67
# LLVM: File: {{.*}}.no-phdr
78
# LLVM-NEXT: Format: ELF64-x86-64
89
# LLVM-NEXT: Arch: x86_64
910
# LLVM-NEXT: AddressSize: 64bit
1011
# LLVM-NEXT: LoadName:{{ *}}
11-
# LLVM-NOT: {{.}}
12+
# LLVM-NEXT: DynamicSection [ (1 entries)
13+
# LLVM-NEXT: Tag Type Name/Value
14+
# LLVM-NEXT: 0x0000000000000000 NULL 0x0
15+
# LLVM-NEXT: ]
1216

13-
# GNU-NOT: {{.}}
17+
# GNU: DynamicSection [ (1 entries)
18+
# GNU-NEXT: Tag Type Name/Value
19+
# GNU-NEXT: 0x0000000000000000 NULL 0x0
20+
# GNU-NEXT: ]
1421

1522
--- !ELF
1623
FileHeader:

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ static const char *getElfMipsOptionsOdkType(unsigned Odk) {
13311331

13321332
template <typename ELFT>
13331333
void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
1334+
// Try to locate the PT_DYNAMIC header.
13341335
const Elf_Phdr *DynamicPhdr = nullptr;
13351336
for (const Elf_Phdr &Phdr : unwrapOrError(Obj->program_headers())) {
13361337
if (Phdr.p_type != ELF::PT_DYNAMIC)
@@ -1339,11 +1340,6 @@ void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
13391340
break;
13401341
}
13411342

1342-
// We do not want to dump dynamic section if we have no PT_DYNAMIC header.
1343-
// This matches GNU's behavior.
1344-
if (!DynamicPhdr)
1345-
return;
1346-
13471343
// Try to locate the .dynamic section in the sections header table.
13481344
const Elf_Shdr *DynamicSec = nullptr;
13491345
for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
@@ -1358,9 +1354,16 @@ void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
13581354
// Ignore sh_entsize and use the expected value for entry size explicitly.
13591355
// This allows us to dump the dynamic sections with a broken sh_entsize
13601356
// field.
1361-
if (DynamicSec)
1357+
if (DynamicSec) {
13621358
DynamicTable = checkDRI({ObjF->getELFFile()->base() + DynamicSec->sh_offset,
13631359
DynamicSec->sh_size, sizeof(Elf_Dyn)});
1360+
parseDynamicTable();
1361+
}
1362+
1363+
// If we have a PT_DYNAMIC header, we will either check the found dynamic
1364+
// section or take the dynamic table data directly from the header.
1365+
if (!DynamicPhdr)
1366+
return;
13641367

13651368
if (DynamicPhdr->p_offset + DynamicPhdr->p_filesz >
13661369
ObjF->getMemoryBufferRef().getBufferSize())
@@ -1374,7 +1377,6 @@ void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
13741377
}
13751378

13761379
StringRef Name = unwrapOrError(Obj->getSectionName(DynamicSec));
1377-
13781380
if (DynamicSec->sh_addr + DynamicSec->sh_size >
13791381
DynamicPhdr->p_vaddr + DynamicPhdr->p_memsz ||
13801382
DynamicSec->sh_addr < DynamicPhdr->p_vaddr)
@@ -1386,8 +1388,6 @@ void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
13861388
reportWarning("The SHT_DYNAMIC section '" + Name +
13871389
"' is not at the start of "
13881390
"PT_DYNAMIC segment");
1389-
1390-
parseDynamicTable();
13911391
}
13921392

13931393
template <typename ELFT>

0 commit comments

Comments
 (0)
Please sign in to comment.