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 @@ -816,6 +816,7 @@ IO.enumCase(Value, "MIPS5", 5); IO.enumCase(Value, "MIPS32", 32); IO.enumCase(Value, "MIPS64", 64); + IO.enumFallback(Value); } void ScalarBitSetTraits::bitset( @@ -1306,6 +1307,14 @@ IO.mapRequired("Type", Type); } + const auto &Obj = *static_cast(IO.getContext()); + if (Obj.getMachine() == ELF::EM_MIPS && Type == ELF::SHT_MIPS_ABIFLAGS) { + if (!IO.outputting()) + Section.reset(new ELFYAML::MipsABIFlags()); + sectionMapping(IO, *cast(Section.get())); + return; + } + switch (Type) { case ELF::SHT_DYNAMIC: if (!IO.outputting()) @@ -1348,11 +1357,6 @@ Section.reset(new ELFYAML::GnuHashSection()); sectionMapping(IO, *cast(Section.get())); break; - case ELF::SHT_MIPS_ABIFLAGS: - if (!IO.outputting()) - Section.reset(new ELFYAML::MipsABIFlags()); - sectionMapping(IO, *cast(Section.get())); - break; case ELF::SHT_GNU_verdef: if (!IO.outputting()) Section.reset(new ELFYAML::VerdefSection()); diff --git a/llvm/test/tools/obj2yaml/ELF/mips-abi-flags.yaml b/llvm/test/tools/obj2yaml/ELF/mips-abi-flags.yaml --- a/llvm/test/tools/obj2yaml/ELF/mips-abi-flags.yaml +++ b/llvm/test/tools/obj2yaml/ELF/mips-abi-flags.yaml @@ -39,3 +39,35 @@ CPR2Size: REG_NONE Flags1: [ ODDSPREG ] Flags2: 0x0 + +## Check how we dump the SHT_MIPS_ABIFLAGS (0x7000002a) section when +## the machine type is not EM_MIPS. It is dumped as a regular +## section of an unknown type. + +# RUN: yaml2obj %s --docnum=2 -DMACHINE=EM_NONE -o %t2.notmips +# RUN: obj2yaml %t2.notmips | FileCheck %s --check-prefix=NOT-MIPS + +# RUN: yaml2obj %s --docnum=2 -DMACHINE=EM_MIPS -o %t2.mips +# RUN: obj2yaml %t2.mips | FileCheck %s --check-prefix=MIPS + +# MIPS: - Name: .MIPS.abiflags +# MIPS-NEXT: Type: SHT_MIPS_ABIFLAGS +# MIPS-NEXT: ISA: 0x00000000 +# MIPS-NEXT: ... + +# NOT-MIPS: - Name: .MIPS.abiflags +# NOT-MIPS-NEXT: Type: 0x7000002A +# NOT-MIPS-NEXT: Content: '000000000000000000000000000000000000000000000000' +# NOT-MIPS-NEXT: ... + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2MSB + Type: ET_REL + Machine: [[MACHINE]] +Sections: + - Name: .MIPS.abiflags + Type: SHT_PROGBITS + ShType: 0x7000002a ## SHT_MIPS_ABIFLAGS. + Size: 0x18 diff --git a/llvm/test/tools/yaml2obj/ELF/mips-abi-flags.yaml b/llvm/test/tools/yaml2obj/ELF/mips-abi-flags.yaml --- a/llvm/test/tools/yaml2obj/ELF/mips-abi-flags.yaml +++ b/llvm/test/tools/yaml2obj/ELF/mips-abi-flags.yaml @@ -27,7 +27,7 @@ Class: ELFCLASS64 Data: ELFDATA2MSB Type: ET_REL - Machine: EM_MIPS + Machine: [[MACHINE=EM_MIPS]] Sections: - Name: .MIPS.abiflags Type: SHT_MIPS_ABIFLAGS @@ -43,3 +43,10 @@ CPR2Size: REG_NONE Flags1: [ ODDSPREG ] Flags2: 0x0 + +## Check we don't recognize the SHT_MIPS_ABIFLAGS section for non-MIPS targets. + +# RUN: not yaml2obj %s -DMACHINE=EM_NONE 2>&1 | FileCheck %s --check-prefix=ERR + +# ERR: error: invalid hex32 number +# ERR-NEXT: Type: SHT_MIPS_ABIFLAGS 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 @@ -460,6 +460,10 @@ auto GetDumper = [this](unsigned Type) -> std::function(const Elf_Shdr *)> { + if (Obj.getHeader().e_machine == ELF::EM_MIPS && + Type == ELF::SHT_MIPS_ABIFLAGS) + return [this](const Elf_Shdr *S) { return dumpMipsABIFlags(S); }; + switch (Type) { case ELF::SHT_DYNAMIC: return [this](const Elf_Shdr *S) { return dumpDynamicSection(S); }; @@ -472,8 +476,6 @@ return [this](const Elf_Shdr *S) { return dumpRelrSection(S); }; case ELF::SHT_GROUP: return [this](const Elf_Shdr *S) { return dumpGroup(S); }; - case ELF::SHT_MIPS_ABIFLAGS: - return [this](const Elf_Shdr *S) { return dumpMipsABIFlags(S); }; case ELF::SHT_NOBITS: return [this](const Elf_Shdr *S) { return dumpNoBitsSection(S); }; case ELF::SHT_NOTE: