[llvm] 5829dc9 - [yaml2obj][elf2yaml] - Add a support for the `EntSize` field for `SHT_HASH` sections.
Georgii Rymar via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 2 02:09:31 PDT 2020
Author: Georgii Rymar
Date: 2020-10-02T12:01:50+03:00
New Revision: 5829dc925002aaf5e80189924e59d238d3d2a4d1
URL: https://github.com/llvm/llvm-project/commit/5829dc925002aaf5e80189924e59d238d3d2a4d1
DIFF: https://github.com/llvm/llvm-project/commit/5829dc925002aaf5e80189924e59d238d3d2a4d1.diff
LOG: [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
Added:
Modified:
llvm/lib/ObjectYAML/ELFEmitter.cpp
llvm/test/tools/obj2yaml/ELF/hash-section.yaml
llvm/test/tools/yaml2obj/ELF/hash-section.yaml
llvm/test/tools/yaml2obj/ELF/section-headers-exclude.yaml
llvm/tools/obj2yaml/elf2yaml.cpp
Removed:
################################################################################
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 04542ccaecff..10f31555005f 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -1396,6 +1396,11 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
SN2I.lookup(".dynsym", Link))
SHeader.sh_link = Link;
+ if (Section.EntSize)
+ SHeader.sh_entsize = *Section.EntSize;
+ else
+ SHeader.sh_entsize = sizeof(typename ELFT::Word);
+
if (Section.Content || Section.Size) {
SHeader.sh_size = writeContent(CBA, Section.Content, Section.Size);
return;
diff --git a/llvm/test/tools/obj2yaml/ELF/hash-section.yaml b/llvm/test/tools/obj2yaml/ELF/hash-section.yaml
index 1e867cbf4c98..7e0a0288a5f1 100644
--- a/llvm/test/tools/obj2yaml/ELF/hash-section.yaml
+++ b/llvm/test/tools/obj2yaml/ELF/hash-section.yaml
@@ -74,3 +74,40 @@ Sections:
- Name: .oversized
Type: SHT_HASH
Content: '0100000002000000030000000400000000'
+
+## Check how we dump the "EntSize" field. When the sh_entsize is 4,
+## we don't print it, because it is the default value for the SHT_HASH section.
+
+# RUN: yaml2obj --docnum=3 %s -o %t3
+# RUN: obj2yaml %t3 | FileCheck %s --check-prefix=ENT-SIZE
+
+# ENT-SIZE: - Name: .hash.entsize.0
+# ENT-SIZE-NEXT: Type: SHT_HASH
+# ENT-SIZE-NEXT: EntSize: 0x0000000000000000
+# ENT-SIZE-NEXT: Content: ''
+# ENT-SIZE-NEXT: - Name: .hash.entsize.4.default
+# ENT-SIZE-NEXT: Type: SHT_HASH
+# ENT-SIZE-NEXT: Content: ''
+# ENT-SIZE-NEXT: - Name: .hash.entsize.255
+# ENT-SIZE-NEXT: Type: SHT_HASH
+# ENT-SIZE-NEXT: EntSize: 0x00000000000000FF
+# ENT-SIZE-NEXT: Content: ''
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS32
+ Data: ELFDATA2LSB
+ Type: ET_DYN
+Sections:
+ - Name: .hash.entsize.0
+ Type: SHT_HASH
+ EntSize: 0
+ Size: 0
+ - Name: .hash.entsize.4.default
+ Type: SHT_HASH
+ EntSize: 4
+ Size: 0
+ - Name: .hash.entsize.255
+ Type: SHT_HASH
+ EntSize: 255
+ Size: 0
diff --git a/llvm/test/tools/yaml2obj/ELF/hash-section.yaml b/llvm/test/tools/yaml2obj/ELF/hash-section.yaml
index 3c2a2af2df49..35e086a49bd3 100644
--- a/llvm/test/tools/yaml2obj/ELF/hash-section.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/hash-section.yaml
@@ -1,9 +1,11 @@
## Check how yaml2obj produces SHT_HASH sections.
## Check we can describe a SHT_HASH section using the "Content" tag.
+## Check default values of section fields.
# RUN: yaml2obj --docnum=1 %s -o %t1
-# RUN: llvm-readobj --sections --section-data %t1 | FileCheck %s --check-prefix=CONTENT
+# RUN: llvm-readobj --sections --section-data %t1 | \
+# RUN: FileCheck %s -DENTSIZE=4 --check-prefix=CONTENT
# CONTENT: Name: .hash
# CONTENT-NEXT: Type: SHT_HASH
@@ -15,7 +17,7 @@
# CONTENT-NEXT: Link: 1
# CONTENT-NEXT: Info: 0
# CONTENT-NEXT: AddressAlignment: 0
-# CONTENT-NEXT: EntrySize: 0
+# CONTENT-NEXT: EntrySize: [[ENTSIZE]]{{$}}
# CONTENT-NEXT: SectionData (
# CONTENT-NEXT: 0000: 01000000 02000000 03000000 04000000
# CONTENT-NEXT: 0010: 05000000
@@ -33,6 +35,13 @@ Sections:
- Name: .hash
Type: SHT_HASH
Content: '0100000002000000030000000400000005000000'
+ EntSize: [[ENTSIZE=<none>]]
+
+## Check we can set an arbitrary entry size for the SHT_HASH section.
+
+# RUN: yaml2obj --docnum=1 -DENTSIZE=0xFF %s -o %t1.entsize
+# RUN: llvm-readobj --sections --section-data %t1.entsize | \
+# RUN: FileCheck %s -DENTSIZE=255 --check-prefix=CONTENT
## Check we can describe a SHT_HASH section using "Bucket" and "Chain" tags.
@@ -280,7 +289,7 @@ Sections:
# OVERRIDE-NEXT: Link: 0
# OVERRIDE-NEXT: Info: 0
# OVERRIDE-NEXT: AddressAlignment: 0
-# OVERRIDE-NEXT: EntrySize: 0
+# OVERRIDE-NEXT: EntrySize: 4
# OVERRIDE-NEXT: SectionData (
# OVERRIDE-NEXT: 0000: AA000000 BB000000 01000000 02000000
# OVERRIDE-NEXT: 0010: 03000000 04000000 05000000
diff --git a/llvm/test/tools/yaml2obj/ELF/section-headers-exclude.yaml b/llvm/test/tools/yaml2obj/ELF/section-headers-exclude.yaml
index b7c37ebcf6a2..45b339853cf1 100644
--- a/llvm/test/tools/yaml2obj/ELF/section-headers-exclude.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/section-headers-exclude.yaml
@@ -406,7 +406,7 @@ SectionHeaderTable:
# RUN: llvm-readelf %t8 --section-headers | FileCheck %s --check-prefix=LINK-HASH
# LINK-HASH: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
-# LINK-HASH: [ 1] .hash HASH 0000000000000000 000040 000000 00 0 0 0
+# LINK-HASH: [ 1] .hash HASH 0000000000000000 000040 000000 04 0 0 0
# LINK-HASH-NEXT: [ 2] .gnu_hash GNU_HASH 0000000000000000 000040 000000 00 0 0 0
--- !ELF
diff --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp
index a8eae03c0b78..f5ad6a0a4499 100644
--- a/llvm/tools/obj2yaml/elf2yaml.cpp
+++ b/llvm/tools/obj2yaml/elf2yaml.cpp
@@ -659,6 +659,8 @@ static unsigned getDefaultShEntSize(ELFYAML::ELF_SHT SecType,
return sizeof(typename ELFT::Relr);
case ELF::SHT_DYNAMIC:
return sizeof(typename ELFT::Dyn);
+ case ELF::SHT_HASH:
+ return sizeof(typename ELFT::Word);
default:
if (SecName == ".debug_str")
return 1;
More information about the llvm-commits
mailing list