Skip to content

Commit

Permalink
[obj2yaml] - SHT_DYNAMIC and SHT_REL* sections: stop dumping sh_entsi…
Browse files Browse the repository at this point in the history
…ze field when it has the default value.

Currently obj2yaml always emits the `EntSize` property when `sh_entsize != 0`.
It is not correct. For example, for `SHT_DYNAMIC` section, `EntSize == 0`
is abnormal, while `sizeof(ELFT::Dyn)` is the expected default.

To reduce the output produces we should not dump default values.

yaml2obj tests that shows `sh_entsize` values produced are:
1) For `SHT_REL*` sections: `yaml2obj\ELF\reloc-sec-entry-size.yaml`
2) For `SHT_DYNAMIC`: `yaml2obj\ELF\dynamic-section.yaml`

Differential revision: https://reviews.llvm.org/D76227
  • Loading branch information
Georgii Rymar committed Mar 19, 2020
1 parent cf1f8f9 commit a02b386
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 83 deletions.
1 change: 0 additions & 1 deletion llvm/test/Object/AArch64/yaml2obj-elf-aarch64-rel.yaml
Expand Up @@ -5,7 +5,6 @@
# CHECK-NEXT: Type: SHT_RELA
# CHECK-NEXT: Link: .symtab
# CHECK-NEXT: AddressAlign: 0x0000000000000008
# CHECK-NEXT: EntSize: 0x0000000000000018
# CHECK-NEXT: Info: .text
# CHECK-NEXT: Relocations:
# CHECK-NEXT: - Symbol: main
Expand Down
3 changes: 0 additions & 3 deletions llvm/test/Object/obj2yaml.test
Expand Up @@ -362,7 +362,6 @@
# ELF-MIPSEL-NEXT: Type: SHT_REL
# ELF-MIPSEL-NEXT: Link: .symtab
# ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000004
# ELF-MIPSEL-NEXT: EntSize: 0x0000000000000008
# ELF-MIPSEL-NEXT: Info: .text
# ELF-MIPSEL-NEXT: Relocations:
# ELF-MIPSEL-NEXT: - Symbol: _gp_disp
Expand Down Expand Up @@ -483,7 +482,6 @@
# ELF-MIPS64EL-NEXT: Type: SHT_RELA
# ELF-MIPS64EL-NEXT: Link: .symtab
# ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000008
# ELF-MIPS64EL-NEXT: EntSize: 0x0000000000000018
# ELF-MIPS64EL-NEXT: Info: .data
# ELF-MIPS64EL-NEXT: Relocations:
# ELF-MIPS64EL-NEXT: - Symbol: zed
Expand Down Expand Up @@ -552,7 +550,6 @@
# ELF-X86-64-NEXT: Address: 0x0000000000000038
# ELF-X86-64-NEXT: Link: .symtab
# ELF-X86-64-NEXT: AddressAlign: 0x0000000000000008
# ELF-X86-64-NEXT: EntSize: 0x0000000000000018
# ELF-X86-64-NEXT: Info: .text
# ELF-X86-64-NEXT: Relocations:
# ELF-X86-64-NEXT: - Offset: 0x000000000000000D
Expand Down
7 changes: 3 additions & 4 deletions llvm/test/tools/obj2yaml/dynamic-section.test
@@ -1,6 +1,6 @@
## Check we can use obj2yaml to yamalize the object containing
## .dynamic section. Check that resulting section has the
## proper attributes and dynamic tags.
## Check we can use obj2yaml to yamalize the object containing .dynamic
## section. Check that resulting section has the proper attributes and
## dynamic tags and that we do not dump the default sh_entsize.

# RUN: yaml2obj -DENTSIZE=0x10 %s -o %t1
# RUN: obj2yaml %t1 | FileCheck %s
Expand All @@ -10,7 +10,6 @@
# CHECK-NEXT: Type: SHT_DYNAMIC
# CHECK-NEXT: Address: 0x0000000000001000
# CHECK-NEXT: AddressAlign: 0x0000000000002000
# CHECK-NEXT: EntSize: 0x0000000000000010
# CHECK-NEXT: Entries:
# CHECK-NEXT: - Tag: DT_NULL
# CHECK-NEXT: Value: 0x0000000000000000
Expand Down
5 changes: 2 additions & 3 deletions llvm/test/tools/obj2yaml/elf-ppc64-relocations.yaml
Expand Up @@ -10,9 +10,8 @@
# CHECK-NEXT: Type: ET_REL
# CHECK-NEXT: Machine: EM_PPC64
# CHECK-NEXT: Sections:
# CHECK-NEXT: - Name: .rela.text
# CHECK-NEXT: Type: SHT_RELA
# CHECK-NEXT: EntSize: 0x0000000000000018
# CHECK-NEXT: - Name: .rela.text
# CHECK-NEXT: Type: SHT_RELA
# CHECK-NEXT: Relocations:
# CHECK-NEXT: - Type: R_PPC64_NONE
# CHECK-NEXT: - Type: R_PPC64_ADDR32
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/tools/obj2yaml/elf-reladyn-section-shinfo.yaml
@@ -0,0 +1,43 @@
# RUN: yaml2obj %s -o %t
# RUN: llvm-readobj --sections %t | FileCheck %s
# RUN: obj2yaml %t | FileCheck %s --check-prefix=YAML

## .rela.dyn is a dynamic relocation section that normally has
## no value in sh_info field. Check we are able to use
## yaml2obj/obj2yaml without needing to explicitly set it.

# CHECK: Name: .rela.dyn
# CHECK-NEXT: Type: SHT_RELA
# CHECK-NEXT: Flags [
# CHECK-NEXT: SHF_ALLOC
# CHECK-NEXT: ]
# CHECK-NEXT: Address:
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size:
# CHECK-NEXT: Link:
# CHECK-NEXT: Info: 0
# CHECK-NEXT: AddressAlignment:
# CHECK-NEXT: EntrySize:

# YAML: - Name: .rela.dyn
# YAML-NEXT: Type: SHT_RELA
# YAML-NEXT: Flags: [ SHF_ALLOC ]
# YAML-NEXT: Link: .dynsym
# YAML-NEXT: - Name:

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_DYN
Machine: EM_X86_64
Entry: 0x0000000000001000
Sections:
- Name: .rela.dyn
Type: SHT_RELA
Flags: [ SHF_ALLOC ]
Link: .dynsym
# Add at least one symbol to trigger the .dynsym emission.
DynamicSymbols:
- Name: bar
Binding: STB_GLOBAL
1 change: 0 additions & 1 deletion llvm/test/tools/obj2yaml/no-symbol-reloc.test
Expand Up @@ -16,7 +16,6 @@
# CHECK-NEXT: Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
# CHECK-NEXT: - Name: .rela.text
# CHECK-NEXT: Type: SHT_RELA
# CHECK-NEXT: EntSize: 0x0000000000000018
# CHECK-NEXT: Info: .text
# CHECK-NEXT: Relocations:
# CHECK-NEXT: - Type: R_X86_64_NONE
Expand Down
1 change: 0 additions & 1 deletion llvm/test/tools/obj2yaml/relocation-type.yaml
Expand Up @@ -13,7 +13,6 @@
# CHECK-NEXT: Sections:
# CHECK-NEXT: - Name: .rela.text
# CHECK-NEXT: Type: SHT_RELA
# CHECK-NEXT: EntSize: 0x0000000000000018
# CHECK-NEXT: Relocations:
# CHECK-NEXT: - Offset: 0x0000000000000009
# CHECK-NEXT: Type: [[FIRST]]
Expand Down
134 changes: 65 additions & 69 deletions llvm/test/tools/obj2yaml/relr-section.yaml
@@ -1,72 +1,68 @@
## Test how we dump SHT_RELR sections for 32 and 64-bit targets.

## Test we use the "Entries" property when it is possible do
## dump values correctly.

# RUN: yaml2obj --docnum=1 -D BITS=32 -D ENCODE=LSB %s -o %t.32le
# RUN: obj2yaml %t.32le | FileCheck %s --check-prefix=ELF32LE
# RUN: yaml2obj --docnum=1 -D BITS=32 -D ENCODE=MSB %s -o %t.32be
# RUN: obj2yaml %t.32be | FileCheck %s --check-prefix=ELF32BE
# RUN: yaml2obj --docnum=1 -D BITS=64 -D ENCODE=LSB %s -o %t.64le
# RUN: obj2yaml %t.64le | FileCheck %s --check-prefix=ELF64LE
# RUN: yaml2obj --docnum=1 -D BITS=64 -D ENCODE=MSB %s -o %t.64be
# RUN: obj2yaml %t.64be | FileCheck %s --check-prefix=ELF64BE

# ELF64LE: Sections:
# ELF64LE-NEXT: - Name: .relr.dyn
# ELF64LE-NEXT: Type: SHT_RELR
# ELF64LE-NEXT: EntSize: 0x0000000000000008
# ELF64LE-NEXT: Entries: [ 0x8877665544332211 ]

# ELF32LE: Sections:
# ELF32LE-NEXT: - Name: .relr.dyn
# ELF32LE-NEXT: Type: SHT_RELR
# ELF32LE-NEXT: EntSize: 0x0000000000000004
# ELF32LE-NEXT: Entries: [ 0x0000000044332211, 0x0000000088776655 ]

# ELF64BE: Sections:
# ELF64BE-NEXT: - Name: .relr.dyn
# ELF64BE-NEXT: Type: SHT_RELR
# ELF64BE-NEXT: EntSize: 0x0000000000000008
# ELF64BE-NEXT: Entries: [ 0x1122334455667788 ]

# ELF32BE: Sections:
# ELF32BE-NEXT: - Name: .relr.dyn
# ELF32BE-NEXT: Type: SHT_RELR
# ELF32BE-NEXT: EntSize: 0x0000000000000004
# ELF32BE-NEXT: Entries: [ 0x0000000011223344, 0x0000000055667788 ]

--- !ELF
FileHeader:
Class: ELFCLASS[[BITS]]
Data: ELFDATA2[[ENCODE]]
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .relr.dyn
Type: SHT_RELR
Content: "1122334455667788"

## Test we use the "Content" property when a SHT_RELR section is truncated.

# RUN: yaml2obj --docnum=2 %s -o %t.content
# RUN: obj2yaml %t.content | FileCheck %s --check-prefix=CONTENT

# CONTENT: - Name: .relr.dyn
# CONTENT-NEXT: Type: SHT_RELR
# CONTENT-NEXT: EntSize: 0x0000000000000008
# CONTENT-NEXT: Content: '11223344556677'

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2MSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .relr.dyn
Type: SHT_RELR
Content: "11223344556677"
## Test how we dump SHT_RELR sections for 32 and 64-bit targets.

## Test we use the "Entries" property when it is possible to
## dump values correctly. Also, check we do not dump sh_entsize when
## it has the default value.

# RUN: yaml2obj --docnum=1 -D BITS=32 -D ENCODE=LSB %s -o %t.32le
# RUN: obj2yaml %t.32le | FileCheck %s --check-prefix=ELF32LE
# RUN: yaml2obj --docnum=1 -D BITS=32 -D ENCODE=MSB %s -o %t.32be
# RUN: obj2yaml %t.32be | FileCheck %s --check-prefix=ELF32BE
# RUN: yaml2obj --docnum=1 -D BITS=64 -D ENCODE=LSB %s -o %t.64le
# RUN: obj2yaml %t.64le | FileCheck %s --check-prefix=ELF64LE
# RUN: yaml2obj --docnum=1 -D BITS=64 -D ENCODE=MSB %s -o %t.64be
# RUN: obj2yaml %t.64be | FileCheck %s --check-prefix=ELF64BE

# ELF64LE: Sections:
# ELF64LE-NEXT: - Name: .relr.dyn
# ELF64LE-NEXT: Type: SHT_RELR
# ELF64LE-NEXT: Entries: [ 0x8877665544332211 ]

# ELF32LE: Sections:
# ELF32LE-NEXT: - Name: .relr.dyn
# ELF32LE-NEXT: Type: SHT_RELR
# ELF32LE-NEXT: Entries: [ 0x0000000044332211, 0x0000000088776655 ]

# ELF64BE: Sections:
# ELF64BE-NEXT: - Name: .relr.dyn
# ELF64BE-NEXT: Type: SHT_RELR
# ELF64BE-NEXT: Entries: [ 0x1122334455667788 ]

# ELF32BE: Sections:
# ELF32BE-NEXT: - Name: .relr.dyn
# ELF32BE-NEXT: Type: SHT_RELR
# ELF32BE-NEXT: Entries: [ 0x0000000011223344, 0x0000000055667788 ]

--- !ELF
FileHeader:
Class: ELFCLASS[[BITS]]
Data: ELFDATA2[[ENCODE]]
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .relr.dyn
Type: SHT_RELR
Content: "1122334455667788"

## Test we use the "Content" property when a SHT_RELR section is truncated.

# RUN: yaml2obj --docnum=2 %s -o %t.content
# RUN: obj2yaml %t.content | FileCheck %s --check-prefix=CONTENT

# CONTENT: - Name: .relr.dyn
# CONTENT-NEXT: Type: SHT_RELR
# CONTENT-NEXT: Content: '11223344556677'

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2MSB
Type: ET_DYN
Machine: EM_X86_64
Sections:
- Name: .relr.dyn
Type: SHT_RELR
Content: "11223344556677"

## Test we are able to dump a SHT_RELR section when sh_entsize is invalid.
## Here we use 0xFE as a value instead of expected 0x8.
Expand Down
19 changes: 18 additions & 1 deletion llvm/tools/obj2yaml/elf2yaml.cpp
Expand Up @@ -521,6 +521,22 @@ Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
return Error::success();
}

template <class ELFT>
static unsigned getDefaultShEntSize(ELFYAML::ELF_SHT SecType) {
switch (SecType) {
case ELF::SHT_REL:
return sizeof(typename ELFT::Rel);
case ELF::SHT_RELA:
return sizeof(typename ELFT::Rela);
case ELF::SHT_RELR:
return sizeof(typename ELFT::Relr);
case ELF::SHT_DYNAMIC:
return sizeof(typename ELFT::Dyn);
default:
return 0;
}
}

template <class ELFT>
Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
ELFYAML::Section &S) {
Expand All @@ -532,7 +548,8 @@ Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
if (Shdr->sh_addr)
S.Address = static_cast<uint64_t>(Shdr->sh_addr);
S.AddressAlign = Shdr->sh_addralign;
if (Shdr->sh_entsize)

if (Shdr->sh_entsize != getDefaultShEntSize<ELFT>(S.Type))
S.EntSize = static_cast<llvm::yaml::Hex64>(Shdr->sh_entsize);

auto NameOrErr = getUniquedSectionName(Shdr);
Expand Down

0 comments on commit a02b386

Please sign in to comment.