diff --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h --- a/llvm/include/llvm/ObjectYAML/ELFYAML.h +++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h @@ -556,8 +556,9 @@ }; template <> -struct ScalarEnumerationTraits { - static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value); +struct ScalarEnumerationContextTraits { + static void enumeration(IO &io, ELFYAML::ELF_ELFDATA &Value, + ELFYAML::Object &Ctx); }; template <> diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -272,9 +272,24 @@ #undef ECase } -void ScalarEnumerationTraits::enumeration( - IO &IO, ELFYAML::ELF_ELFDATA &Value) { -#define ECase(X) IO.enumCase(Value, #X, ELF::X) +void ScalarEnumerationContextTraits:: + enumeration(IO &io, ELFYAML::ELF_ELFDATA &Value, ELFYAML::Object &Ctx) { + Optional OverriddenStr; + auto It = Ctx.Overridden.find("EI_DATA"); + if (It != Ctx.Overridden.end() && !io.outputting()) { + OverriddenStr = It->second; + Ctx.SeenOverridden.insert("EI_DATA"); + } + +#define ECase(X) \ + if (!OverriddenStr) \ + io.enumCase(Value, #X, ELF::X); \ + else if (*OverriddenStr == #X) { \ + Value = ELF::X; \ + io.matchEnumFallback(); \ + return; \ + } + // ELFDATANONE is an invalid data encoding, but we accept it because // we want to be able to produce invalid binaries for the tests. ECase(ELFDATANONE); @@ -866,7 +881,7 @@ ELFYAML::FileHeader &FileHdr) { auto &Ctx = *static_cast(IO.getContext()); IO.mapRequired("Class", FileHdr.Class, Ctx); - IO.mapRequired("Data", FileHdr.Data); + IO.mapRequired("Data", FileHdr.Data, Ctx); IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0)); IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0)); IO.mapRequired("Type", FileHdr.Type); 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 @@ -5,11 +5,11 @@ # RUN: yaml2obj --docnum=1 %s -o %t.64le # RUN: obj2yaml %t.64le | FileCheck %s --check-prefix=ELF64LE -# RUN: yaml2obj --docnum=2 %s -o %t.32le +# RUN: yaml2obj --docnum=1 -D EI_CLASS=ELFCLASS32 %s -o %t.32le # RUN: obj2yaml %t.32le | FileCheck %s --check-prefix=ELF32LE -# RUN: yaml2obj --docnum=3 %s -o %t.64be +# RUN: yaml2obj --docnum=1 -D EI_DATA=ELFDATA2MSB %s -o %t.64be # RUN: obj2yaml %t.64be | FileCheck %s --check-prefix=ELF64BE -# RUN: yaml2obj --docnum=4 %s -o %t.32be +# RUN: yaml2obj --docnum=1 -D EI_CLASS=ELFCLASS32 -D EI_DATA=ELFDATA2MSB %s -o %t.32be # RUN: obj2yaml %t.32be | FileCheck %s --check-prefix=ELF32BE # ELF64LE: Sections: @@ -47,42 +47,9 @@ Type: SHT_RELR Content: "1122334455667788" ---- !ELF -FileHeader: - Class: ELFCLASS32 - Data: ELFDATA2LSB - Type: ET_DYN - Machine: EM_386 -Sections: - - Name: .relr.dyn - Type: SHT_RELR - Content: "1122334455667788" - ---- !ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2MSB - Type: ET_DYN - Machine: EM_X86_64 -Sections: - - Name: .relr.dyn - Type: SHT_RELR - Content: "1122334455667788" - ---- !ELF -FileHeader: - Class: ELFCLASS32 - Data: ELFDATA2MSB - Type: ET_DYN - Machine: EM_386 -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=5 %s -o %t.content +# RUN: yaml2obj --docnum=2 %s -o %t.content # RUN: obj2yaml %t.content | FileCheck %s --check-prefix=CONTENT # CONTENT: - Name: .relr.dyn diff --git a/llvm/test/tools/yaml2obj/ELF/class-endianness.yaml b/llvm/test/tools/yaml2obj/ELF/class-endianness.yaml --- a/llvm/test/tools/yaml2obj/ELF/class-endianness.yaml +++ b/llvm/test/tools/yaml2obj/ELF/class-endianness.yaml @@ -5,6 +5,9 @@ # RUN: yaml2obj %s --docnum=1 -D EI_CLASS=ELFCLASS32 | llvm-readobj --file-headers - | FileCheck %s --check-prefix LE32 # RUN: yaml2obj %s --docnum=2 | llvm-readobj --file-headers - | FileCheck %s --check-prefix BE32 +# RUN: yaml2obj %s --docnum=1 -D EI_DATA=ELFDATA2MSB | llvm-readobj --file-headers - | FileCheck %s --check-prefix BE64 +# RUN: yaml2obj %s --docnum=2 -D EI_DATA=ELFDATA2LSB | llvm-readobj --file-headers - | FileCheck %s --check-prefix LE32 + # LE64: Class: 64-bit (0x2) # LE64-NEXT: DataEncoding: LittleEndian (0x1) @@ -21,6 +24,8 @@ # BAD: error: unknown enumerated scalar +# RUN: not yaml2obj --docnum=1 -D EI_DATA=invalid %s 2>&1 | FileCheck --check-prefix=BAD %s + --- !ELF FileHeader: !FileHeader Class: ELFCLASS64 diff --git a/llvm/test/tools/yaml2obj/ELF/relr-section.yaml b/llvm/test/tools/yaml2obj/ELF/relr-section.yaml --- a/llvm/test/tools/yaml2obj/ELF/relr-section.yaml +++ b/llvm/test/tools/yaml2obj/ELF/relr-section.yaml @@ -21,22 +21,8 @@ # LE64-NEXT: 0010: 66554433 00000010 AA998877 00000010 # LE64-NEXT: ) ---- !ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2LSB - Type: ET_DYN - Machine: EM_X86_64 -Sections: - - Name: .relr.dyn - Type: SHT_RELR -## Set an arbitrary flag to demonstrate flags are set when requested. - Flags: [ SHF_ALLOC ] - Entries: [ 0x00000000AABBCCDD, 0x00000000EEFF1122, - 0x1000000033445566, 0x10000000778899AA ] - ## Test that the content of SHT_RELR sections for 64-bit big endian targets is correct. -# RUN: yaml2obj --docnum=2 %s -o %t.be64 +# RUN: yaml2obj --docnum=1 -D EI_DATA=ELFDATA2MSB %s -o %t.be64 # RUN: llvm-readobj --sections --section-data %t.be64 | FileCheck %s --check-prefix=BE64 # BE64: Name: .relr.dyn @@ -48,7 +34,7 @@ --- !ELF FileHeader: Class: ELFCLASS64 - Data: ELFDATA2MSB + Data: ELFDATA2LSB Type: ET_DYN Machine: EM_X86_64 Sections: @@ -60,7 +46,7 @@ 0x1000000033445566, 0x10000000778899AA ] ## Test that the content of SHT_RELR sections for 32-bit little endian targets is correct. -# RUN: yaml2obj --docnum=3 %s -o %t.le32 +# RUN: yaml2obj --docnum=2 %s -o %t.le32 # RUN: llvm-readobj --sections --section-data %t.le32 | FileCheck %s --check-prefix=LE32 # LE32: Name: .relr.dyn @@ -79,22 +65,8 @@ # LE32-NEXT: 0000: DDCCBBAA BBAAFFEE BBAAFFEE BCAAFFEE # LE32-NEXT: ) ---- !ELF -FileHeader: - Class: ELFCLASS32 - Data: ELFDATA2LSB - Type: ET_DYN - Machine: EM_386 -Sections: - - Name: .relr.dyn - Type: SHT_RELR -## Set an arbitrary flag to demonstrate flags are set when requested. - Flags: [ SHF_ALLOC ] - Entries: [ 0xAABBCCDD, 0xEEFFAABB, - 0xEEFFAABB, 0xEEFFAABC ] - ## Test that the content of SHT_RELR sections for 32-bit big endian targets is correct. -# RUN: yaml2obj --docnum=4 %s -o %t.be32 +# RUN: yaml2obj --docnum=2 -D EI_DATA=ELFDATA2MSB %s -o %t.be32 # RUN: llvm-readobj --sections --section-data %t.be32 | FileCheck %s --check-prefix=BE32 # BE32: Name: .relr.dyn @@ -105,7 +77,7 @@ --- !ELF FileHeader: Class: ELFCLASS32 - Data: ELFDATA2MSB + Data: ELFDATA2LSB Type: ET_DYN Machine: EM_386 Sections: @@ -117,7 +89,7 @@ 0xEEFFAABB, 0xEEFFAABC ] ## Test we can use "Content" to describe SHT_RELR section. -# RUN: yaml2obj --docnum=5 %s -o %t.content +# RUN: yaml2obj --docnum=3 %s -o %t.content # RUN: llvm-readobj --sections --section-data %t.content | FileCheck %s --check-prefix=CONTENT # CONTENT: Name: .relr.dyn @@ -137,7 +109,7 @@ Content: "112233" ## Check we are able to set an arbitrary sh_entsize. -# RUN: yaml2obj --docnum=6 %s -o %t.entsize +# RUN: yaml2obj --docnum=4 %s -o %t.entsize # RUN: llvm-readelf --sections %t.entsize | FileCheck %s --check-prefix=ENTSIZE # ENTSIZE: [Nr] Name Type Address Off Size ES @@ -156,7 +128,7 @@ Content: "12" ## Test we can't use 64-bit offsets/bitmaps when creating a 32-bit object. -# RUN: yaml2obj --docnum=7 %s -o %t.nottoolarge +# RUN: yaml2obj --docnum=5 %s -o %t.nottoolarge # RUN: llvm-readobj --sections --section-data %t.nottoolarge | FileCheck %s --check-prefix=NOT-TOO-LARGE # NOT-TOO-LARGE: Name: .relr.dyn @@ -164,7 +136,7 @@ # NOT-TOO-LARGE-NEXT: 0000: FFFFFFFF # NOT-TOO-LARGE-NEXT: ) -# RUN: not yaml2obj --docnum=8 %s 2>&1 | FileCheck %s --check-prefix=TOO-LARGE +# RUN: not yaml2obj --docnum=6 %s 2>&1 | FileCheck %s --check-prefix=TOO-LARGE # TOO-LARGE: error: .relr.dyn: the value is too large for 32-bits: 0x100000000 --- !ELF @@ -190,7 +162,7 @@ Entries: [ 0x0000000100000000 ] ## Test we can't specify "Entries" and "Content" properties at the same time. -# RUN: not yaml2obj --docnum=9 %s 2>&1 | FileCheck %s --check-prefix=BOTH +# RUN: not yaml2obj --docnum=7 %s 2>&1 | FileCheck %s --check-prefix=BOTH # BOTH: error: "Entries" and "Content" can't be used together diff --git a/llvm/test/tools/yaml2obj/ELF/stack-sizes.yaml b/llvm/test/tools/yaml2obj/ELF/stack-sizes.yaml --- a/llvm/test/tools/yaml2obj/ELF/stack-sizes.yaml +++ b/llvm/test/tools/yaml2obj/ELF/stack-sizes.yaml @@ -64,14 +64,14 @@ ## Check we can describe .stack_sizes section using pairs. -# RUN: yaml2obj --docnum=2 %s -o %t2 -# RUN: llvm-readobj --sections --section-data %t2 | FileCheck %s --check-prefix=ENTRIES-LE64-BOTH -# RUN: yaml2obj --docnum=3 %s -o %t3 -# RUN: llvm-readobj --sections --section-data %t3 | FileCheck %s --check-prefix=ENTRIES-BE64-BOTH -# RUN: yaml2obj --docnum=4 %s -o %t4 -# RUN: llvm-readobj --sections --section-data %t4 | FileCheck %s --check-prefix=ENTRIES-LE32-BOTH -# RUN: yaml2obj --docnum=5 %s -o %t5 -# RUN: llvm-readobj --sections --section-data %t5 | FileCheck %s --check-prefix=ENTRIES-BE32-BOTH +# RUN: yaml2obj --docnum=2 %s -o %t2.le64 +# RUN: llvm-readobj --sections --section-data %t2.le64 | FileCheck %s --check-prefix=ENTRIES-LE64-BOTH +# RUN: yaml2obj --docnum=2 -D EI_DATA=ELFDATA2MSB %s -o %t2.be64 +# RUN: llvm-readobj --sections --section-data %t2.be64 | FileCheck %s --check-prefix=ENTRIES-BE64-BOTH +# RUN: yaml2obj --docnum=2 -D EI_CLASS=ELFCLASS32 %s -o %t2.le32 +# RUN: llvm-readobj --sections --section-data %t2.le32 | FileCheck %s --check-prefix=ENTRIES-LE32-BOTH +# RUN: yaml2obj --docnum=2 -D EI_CLASS=ELFCLASS32 -D EI_DATA=ELFDATA2MSB %s -o %t2.be32 +# RUN: llvm-readobj --sections --section-data %t2.be32 | FileCheck %s --check-prefix=ENTRIES-BE32-BOTH # ENTRIES-LE64-BOTH: Name: .stack_sizes # ENTRIES-LE64-BOTH: SectionData ( @@ -106,55 +106,10 @@ - Address: 0x30 Size: 0x40 ---- !ELF -FileHeader: - Class: ELFCLASS64 - Data: ELFDATA2MSB - Type: ET_EXEC - Machine: EM_X86_64 -Sections: - - Name: .stack_sizes - Type: SHT_PROGBITS - Entries: - - Address: 0x10 - Size: 0x20 - - Address: 0x30 - Size: 0x40 - ---- !ELF -FileHeader: - Class: ELFCLASS32 - Data: ELFDATA2LSB - Type: ET_EXEC - Machine: EM_386 -Sections: - - Name: .stack_sizes - Type: SHT_PROGBITS - Entries: - - Address: 0x10 - Size: 0x20 - - Address: 0x30 - Size: 0x40 - ---- !ELF -FileHeader: - Class: ELFCLASS32 - Data: ELFDATA2MSB - Type: ET_EXEC - Machine: EM_386 -Sections: - - Name: .stack_sizes - Type: SHT_PROGBITS - Entries: - - Address: 0x10 - Size: 0x20 - - Address: 0x30 - Size: 0x40 - ## Check we can omit the "Address" tag. In this case the address will be zero. -# RUN: yaml2obj --docnum=6 %s -o %t6 -# RUN: llvm-readobj --sections --section-data %t6 | FileCheck %s --check-prefix=ENTRIES-NOADDR +# RUN: yaml2obj --docnum=3 %s -o %t3 +# RUN: llvm-readobj --sections --section-data %t3 | FileCheck %s --check-prefix=ENTRIES-NOADDR # ENTRIES-NOADDR: Name: .stack_sizes # ENTRIES-NOADDR: SectionData ( @@ -176,7 +131,7 @@ ## Check that "Size" tag is mandatory when we describe .stack_sizes using "Entries". -# RUN: not yaml2obj --docnum=7 %s 2>&1 | FileCheck %s --check-prefix=ENTRIES-NOSIZE +# RUN: not yaml2obj --docnum=4 %s 2>&1 | FileCheck %s --check-prefix=ENTRIES-NOSIZE # ENTRIES-NOSIZE: error: missing required key 'Size' @@ -194,7 +149,7 @@ ## Check we can't use both "Content" and "Entries" tags at the same time. -# RUN: not yaml2obj --docnum=8 %s 2>&1 | FileCheck %s --check-prefix=ENTRIES-AND-CONTENT +# RUN: not yaml2obj --docnum=5 %s 2>&1 | FileCheck %s --check-prefix=ENTRIES-AND-CONTENT # ENTRIES-AND-CONTENT: error: .stack_sizes: Content and Entries cannot be used together @@ -214,7 +169,7 @@ ## Check we must specify either "Content", "Entries" or "Size" tag when describing .stack_sizes. -# RUN: not yaml2obj --docnum=9 %s 2>&1 | FileCheck %s --check-prefix=NO-TAGS +# RUN: not yaml2obj --docnum=6 %s 2>&1 | FileCheck %s --check-prefix=NO-TAGS # NO-TAGS: .stack_sizes: one of Content, Entries and Size must be specified @@ -230,7 +185,7 @@ ## Check we can't use both "Size" and "Entries" tags at the same time. -# RUN: not yaml2obj --docnum=10 %s 2>&1 | FileCheck %s --check-prefix=ENTRIES-AND-SIZE +# RUN: not yaml2obj --docnum=7 %s 2>&1 | FileCheck %s --check-prefix=ENTRIES-AND-SIZE # ENTRIES-AND-SIZE: .stack_sizes: Size and Entries cannot be used together @@ -250,8 +205,8 @@ ## Check we can use only "Size" to create .stack_sizes section. -# RUN: yaml2obj --docnum=11 %s -o %t11 -# RUN: llvm-readobj --sections --section-data %t11 | FileCheck %s --check-prefix=SIZE +# RUN: yaml2obj --docnum=8 %s -o %t8 +# RUN: llvm-readobj --sections --section-data %t8 | FileCheck %s --check-prefix=SIZE # SIZE: Name: .stack_sizes # SIZE: Size: @@ -274,8 +229,8 @@ ## Check we can use "Size" and "Content" together to create .stack_sizes section. -# RUN: yaml2obj --docnum=12 %s -o %t12 -# RUN: llvm-readobj --sections --section-data %t12 | FileCheck %s --check-prefix=SIZE-CONTENT +# RUN: yaml2obj --docnum=9 %s -o %t9 +# RUN: llvm-readobj --sections --section-data %t9 | FileCheck %s --check-prefix=SIZE-CONTENT # SIZE-CONTENT: Name: .stack_sizes # SIZE-CONTENT: Size: @@ -296,7 +251,7 @@ Size: 0x5 Content: "112233" -# RUN: not yaml2obj --docnum=13 %s 2>&1 | FileCheck %s --check-prefix=SIZE-CONTENT-ERR +# RUN: not yaml2obj --docnum=10 %s 2>&1 | FileCheck %s --check-prefix=SIZE-CONTENT-ERR # SIZE-CONTENT-ERR: error: .stack_sizes: Size must be greater than or equal to the content size @@ -314,7 +269,7 @@ ## Check we can describe multiple .stack_sizes sections using unique suffixes. -# RUN: yaml2obj --docnum=14 %s -o %t11 +# RUN: yaml2obj --docnum=11 %s -o %t11 # RUN: llvm-readobj --sections --section-data %t11 | FileCheck %s --check-prefix=UNIQUE # UNIQUE: Name: .stack_sizes