[obj2yaml][yaml2obj] - Stop recognizing SHT_MIPS_ABIFLAGS on non-MIPS targets.

Currently we are always recognizing the `SHT_MIPS_ABIFLAGS` section,
even on non-MIPS targets.

The problem of doing this is briefly discussed in D88228 which does the same for `SHT_ARM_EXIDX`:

"The problem is that `SHT_ARM_EXIDX` shares the value with `SHT_X86_64_UNWIND (0x70000001U)`.
We might have other machine specific conflicts, e.g.
`SHT_ARM_ATTRIBUTES` vs `SHT_MSP430_ATTRIBUTES` vs `SHT_RISCV_ATTRIBUTES (0x70000003U)`."

I think we should only recognize target specific sections when the machine type
matches. I.e. `SHT_MIPS_*` should be recognized only on `MIPS`, `SHT_ARM_*`
only on `ARM` etc.

This patch stops recognizing `SHT_MIPS_ABIFLAGS` on `non-MIPS` targets.

Note: I had to update `ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration`, because
otherwise test crashes, calling `llvm_unreachable`.

Differential revision: https://reviews.llvm.org/D88294
This commit is contained in:
Georgii Rymar 2020-09-25 14:57:00 +03:00
parent 7e5a356d2b
commit ea0f66e848
4 changed files with 53 additions and 8 deletions

View File

@ -816,6 +816,7 @@ void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
IO.enumCase(Value, "MIPS5", 5);
IO.enumCase(Value, "MIPS32", 32);
IO.enumCase(Value, "MIPS64", 64);
IO.enumFallback<Hex32>(Value);
}
void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
@ -1306,6 +1307,14 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
IO.mapRequired("Type", Type);
}
const auto &Obj = *static_cast<ELFYAML::Object *>(IO.getContext());
if (Obj.getMachine() == ELF::EM_MIPS && Type == ELF::SHT_MIPS_ABIFLAGS) {
if (!IO.outputting())
Section.reset(new ELFYAML::MipsABIFlags());
sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
return;
}
switch (Type) {
case ELF::SHT_DYNAMIC:
if (!IO.outputting())
@ -1348,11 +1357,6 @@ void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping(
Section.reset(new ELFYAML::GnuHashSection());
sectionMapping(IO, *cast<ELFYAML::GnuHashSection>(Section.get()));
break;
case ELF::SHT_MIPS_ABIFLAGS:
if (!IO.outputting())
Section.reset(new ELFYAML::MipsABIFlags());
sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
break;
case ELF::SHT_GNU_verdef:
if (!IO.outputting())
Section.reset(new ELFYAML::VerdefSection());

View File

@ -39,3 +39,35 @@ Sections:
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

View File

@ -27,7 +27,7 @@ FileHeader:
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 @@ Sections:
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

View File

@ -460,6 +460,10 @@ ELFDumper<ELFT>::dumpSections() {
auto GetDumper = [this](unsigned Type)
-> std::function<Expected<ELFYAML::Chunk *>(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 @@ ELFDumper<ELFT>::dumpSections() {
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: