diff --git a/llvm/test/Object/AArch64/yaml2obj-elf-aarch64-rel.yaml b/llvm/test/Object/AArch64/yaml2obj-elf-aarch64-rel.yaml --- a/llvm/test/Object/AArch64/yaml2obj-elf-aarch64-rel.yaml +++ b/llvm/test/Object/AArch64/yaml2obj-elf-aarch64-rel.yaml @@ -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 diff --git a/llvm/test/Object/obj2yaml.test b/llvm/test/Object/obj2yaml.test --- a/llvm/test/Object/obj2yaml.test +++ b/llvm/test/Object/obj2yaml.test @@ -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 @@ -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 @@ -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 diff --git a/llvm/test/tools/obj2yaml/dynamic-section.test b/llvm/test/tools/obj2yaml/dynamic-section.test --- a/llvm/test/tools/obj2yaml/dynamic-section.test +++ b/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 @@ -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 diff --git a/llvm/test/tools/obj2yaml/elf-ppc64-relocations.yaml b/llvm/test/tools/obj2yaml/elf-ppc64-relocations.yaml --- a/llvm/test/tools/obj2yaml/elf-ppc64-relocations.yaml +++ b/llvm/test/tools/obj2yaml/elf-ppc64-relocations.yaml @@ -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 diff --git a/llvm/test/tools/obj2yaml/elf-reladyn-section-shinfo.yaml b/llvm/test/tools/obj2yaml/elf-reladyn-section-shinfo.yaml new file mode 100644 --- /dev/null +++ b/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 diff --git a/llvm/test/tools/obj2yaml/no-symbol-reloc.test b/llvm/test/tools/obj2yaml/no-symbol-reloc.test --- a/llvm/test/tools/obj2yaml/no-symbol-reloc.test +++ b/llvm/test/tools/obj2yaml/no-symbol-reloc.test @@ -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 diff --git a/llvm/test/tools/obj2yaml/relocation-type.yaml b/llvm/test/tools/obj2yaml/relocation-type.yaml --- a/llvm/test/tools/obj2yaml/relocation-type.yaml +++ b/llvm/test/tools/obj2yaml/relocation-type.yaml @@ -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]] diff --git a/llvm/test/tools/obj2yaml/relr-section.yaml b/llvm/test/tools/obj2yaml/relr-section.yaml --- a/llvm/test/tools/obj2yaml/relr-section.yaml +++ b/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. diff --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp --- a/llvm/tools/obj2yaml/elf2yaml.cpp +++ b/llvm/tools/obj2yaml/elf2yaml.cpp @@ -522,6 +522,22 @@ } template +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 Error ELFDumper::dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S) { // Dump fields. We do not dump the ShOffset field. When not explicitly @@ -532,7 +548,8 @@ if (Shdr->sh_addr) S.Address = static_cast(Shdr->sh_addr); S.AddressAlign = Shdr->sh_addralign; - if (Shdr->sh_entsize) + + if (Shdr->sh_entsize != getDefaultShEntSize(S.Type)) S.EntSize = static_cast(Shdr->sh_entsize); auto NameOrErr = getUniquedSectionName(Shdr);