Skip to content

Commit 5829dc9

Browse files
author
Georgii Rymar
committed
[yaml2obj][elf2yaml] - Add a support for the EntSize field for SHT_HASH sections.
Specification for SHT_HASH table says (https://refspecs.linuxbase.org/elf/gabi4+/ch5.dynamic.html#hash) that it contains Elf32_Word entries for both 32/64 bit objects. Currently both GNU linkers and LLD sets the `sh_entsize` field to `4`. At the same time, `yaml2obj` ignores the `EntSize` field for SHT_HASH sections. This patch fixes this and also adds a support for obj2yaml: it will not dump this field when the `sh_entsize` contains the default value (`4`). Differential revision: https://reviews.llvm.org/D88652
1 parent bfd7ee9 commit 5829dc9

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,11 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
13961396
SN2I.lookup(".dynsym", Link))
13971397
SHeader.sh_link = Link;
13981398

1399+
if (Section.EntSize)
1400+
SHeader.sh_entsize = *Section.EntSize;
1401+
else
1402+
SHeader.sh_entsize = sizeof(typename ELFT::Word);
1403+
13991404
if (Section.Content || Section.Size) {
14001405
SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size);
14011406
return;

llvm/test/tools/obj2yaml/ELF/hash-section.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,40 @@ Sections:
7474
- Name: .oversized
7575
Type: SHT_HASH
7676
Content: '0100000002000000030000000400000000'
77+
78+
## Check how we dump the "EntSize" field. When the sh_entsize is 4,
79+
## we don't print it, because it is the default value for the SHT_HASH section.
80+
81+
# RUN: yaml2obj --docnum=3 %s -o %t3
82+
# RUN: obj2yaml %t3 | FileCheck %s --check-prefix=ENT-SIZE
83+
84+
# ENT-SIZE: - Name: .hash.entsize.0
85+
# ENT-SIZE-NEXT: Type: SHT_HASH
86+
# ENT-SIZE-NEXT: EntSize: 0x0000000000000000
87+
# ENT-SIZE-NEXT: Content: ''
88+
# ENT-SIZE-NEXT: - Name: .hash.entsize.4.default
89+
# ENT-SIZE-NEXT: Type: SHT_HASH
90+
# ENT-SIZE-NEXT: Content: ''
91+
# ENT-SIZE-NEXT: - Name: .hash.entsize.255
92+
# ENT-SIZE-NEXT: Type: SHT_HASH
93+
# ENT-SIZE-NEXT: EntSize: 0x00000000000000FF
94+
# ENT-SIZE-NEXT: Content: ''
95+
96+
--- !ELF
97+
FileHeader:
98+
Class: ELFCLASS32
99+
Data: ELFDATA2LSB
100+
Type: ET_DYN
101+
Sections:
102+
- Name: .hash.entsize.0
103+
Type: SHT_HASH
104+
EntSize: 0
105+
Size: 0
106+
- Name: .hash.entsize.4.default
107+
Type: SHT_HASH
108+
EntSize: 4
109+
Size: 0
110+
- Name: .hash.entsize.255
111+
Type: SHT_HASH
112+
EntSize: 255
113+
Size: 0

llvm/test/tools/yaml2obj/ELF/hash-section.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
## Check how yaml2obj produces SHT_HASH sections.
22

33
## Check we can describe a SHT_HASH section using the "Content" tag.
4+
## Check default values of section fields.
45

56
# RUN: yaml2obj --docnum=1 %s -o %t1
6-
# RUN: llvm-readobj --sections --section-data %t1 | FileCheck %s --check-prefix=CONTENT
7+
# RUN: llvm-readobj --sections --section-data %t1 | \
8+
# RUN: FileCheck %s -DENTSIZE=4 --check-prefix=CONTENT
79

810
# CONTENT: Name: .hash
911
# CONTENT-NEXT: Type: SHT_HASH
@@ -15,7 +17,7 @@
1517
# CONTENT-NEXT: Link: 1
1618
# CONTENT-NEXT: Info: 0
1719
# CONTENT-NEXT: AddressAlignment: 0
18-
# CONTENT-NEXT: EntrySize: 0
20+
# CONTENT-NEXT: EntrySize: [[ENTSIZE]]{{$}}
1921
# CONTENT-NEXT: SectionData (
2022
# CONTENT-NEXT: 0000: 01000000 02000000 03000000 04000000
2123
# CONTENT-NEXT: 0010: 05000000
@@ -33,6 +35,13 @@ Sections:
3335
- Name: .hash
3436
Type: SHT_HASH
3537
Content: '0100000002000000030000000400000005000000'
38+
EntSize: [[ENTSIZE=<none>]]
39+
40+
## Check we can set an arbitrary entry size for the SHT_HASH section.
41+
42+
# RUN: yaml2obj --docnum=1 -DENTSIZE=0xFF %s -o %t1.entsize
43+
# RUN: llvm-readobj --sections --section-data %t1.entsize | \
44+
# RUN: FileCheck %s -DENTSIZE=255 --check-prefix=CONTENT
3645

3746
## Check we can describe a SHT_HASH section using "Bucket" and "Chain" tags.
3847

@@ -280,7 +289,7 @@ Sections:
280289
# OVERRIDE-NEXT: Link: 0
281290
# OVERRIDE-NEXT: Info: 0
282291
# OVERRIDE-NEXT: AddressAlignment: 0
283-
# OVERRIDE-NEXT: EntrySize: 0
292+
# OVERRIDE-NEXT: EntrySize: 4
284293
# OVERRIDE-NEXT: SectionData (
285294
# OVERRIDE-NEXT: 0000: AA000000 BB000000 01000000 02000000
286295
# OVERRIDE-NEXT: 0010: 03000000 04000000 05000000

llvm/test/tools/yaml2obj/ELF/section-headers-exclude.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ SectionHeaderTable:
406406
# RUN: llvm-readelf %t8 --section-headers | FileCheck %s --check-prefix=LINK-HASH
407407

408408
# LINK-HASH: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
409-
# LINK-HASH: [ 1] .hash HASH 0000000000000000 000040 000000 00 0 0 0
409+
# LINK-HASH: [ 1] .hash HASH 0000000000000000 000040 000000 04 0 0 0
410410
# LINK-HASH-NEXT: [ 2] .gnu_hash GNU_HASH 0000000000000000 000040 000000 00 0 0 0
411411

412412
--- !ELF

llvm/tools/obj2yaml/elf2yaml.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ static unsigned getDefaultShEntSize(ELFYAML::ELF_SHT SecType,
659659
return sizeof(typename ELFT::Relr);
660660
case ELF::SHT_DYNAMIC:
661661
return sizeof(typename ELFT::Dyn);
662+
case ELF::SHT_HASH:
663+
return sizeof(typename ELFT::Word);
662664
default:
663665
if (SecName == ".debug_str")
664666
return 1;

0 commit comments

Comments
 (0)