[ELFYAML] Fix handling SHT_NOBITS sections by obj2yaml/yaml2obj tools

SHT_NOBITS sections do not have content in an object file. Now the yaml2obj
tool does not accept `Content` field for such sections, and the obj2yaml
tool does not attempt to read the section content from a file.

Restore r241350 and r241352.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241377 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Simon Atanasyan 2015-07-03 23:00:54 +00:00
parent b811509613
commit afc0340d96
6 changed files with 53 additions and 4 deletions

View File

@ -85,7 +85,13 @@ struct SectionOrType {
}; };
struct Section { struct Section {
enum class SectionKind { Group, RawContent, Relocation, MipsABIFlags }; enum class SectionKind {
Group,
RawContent,
Relocation,
NoBits,
MipsABIFlags
};
SectionKind Kind; SectionKind Kind;
StringRef Name; StringRef Name;
ELF_SHT Type; ELF_SHT Type;
@ -106,6 +112,14 @@ struct RawContentSection : Section {
} }
}; };
struct NoBitsSection : Section {
llvm::yaml::Hex64 Size;
NoBitsSection() : Section(SectionKind::NoBits) {}
static bool classof(const Section *S) {
return S->Kind == SectionKind::NoBits;
}
};
struct Group : Section { struct Group : Section {
// Members of a group contain a flag and a list of section indices // Members of a group contain a flag and a list of section indices
// that are part of the group. // that are part of the group.

View File

@ -627,6 +627,11 @@ static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size())); IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
} }
static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
commonSectionMapping(IO, Section);
IO.mapOptional("Size", Section.Size, Hex64(0));
}
static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) { static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
commonSectionMapping(IO, Section); commonSectionMapping(IO, Section);
IO.mapOptional("Relocations", Section.Relocations); IO.mapOptional("Relocations", Section.Relocations);
@ -682,6 +687,11 @@ void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
Section.reset(new ELFYAML::Group()); Section.reset(new ELFYAML::Group());
groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get())); groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
break; break;
case ELF::SHT_NOBITS:
if (!IO.outputting())
Section.reset(new ELFYAML::NoBitsSection());
sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
break;
case ELF::SHT_MIPS_ABIFLAGS: case ELF::SHT_MIPS_ABIFLAGS:
if (!IO.outputting()) if (!IO.outputting())
Section.reset(new ELFYAML::MipsABIFlags()); Section.reset(new ELFYAML::MipsABIFlags());

View File

@ -234,7 +234,7 @@ ELF-MIPSEL-NEXT: - Name: .bss
ELF-MIPSEL-NEXT: Type: SHT_NOBITS ELF-MIPSEL-NEXT: Type: SHT_NOBITS
ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-MIPSEL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ]
ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000004 ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000004
ELF-MIPSEL-NEXT: Content: 48656C6C ELF-MIPSEL-NEXT: Size: 0x0000000000000004
ELF-MIPSEL-NEXT: - Name: .mdebug.abi32 ELF-MIPSEL-NEXT: - Name: .mdebug.abi32
ELF-MIPSEL-NEXT: Type: SHT_PROGBITS ELF-MIPSEL-NEXT: Type: SHT_PROGBITS
ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000001 ELF-MIPSEL-NEXT: AddressAlign: 0x0000000000000001
@ -324,7 +324,6 @@ ELF-MIPS64EL-NEXT: - Name: .bss
ELF-MIPS64EL-NEXT: Type: SHT_NOBITS ELF-MIPS64EL-NEXT: Type: SHT_NOBITS
ELF-MIPS64EL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ] ELF-MIPS64EL-NEXT: Flags: [ SHF_WRITE, SHF_ALLOC ]
ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000010 ELF-MIPS64EL-NEXT: AddressAlign: 0x0000000000000010
ELF-MIPS64EL-NEXT: Content: ''
ELF-MIPS64EL-NEXT: - Name: .MIPS.options ELF-MIPS64EL-NEXT: - Name: .MIPS.options
ELF-MIPS64EL-NEXT: Type: SHT_MIPS_OPTIONS ELF-MIPS64EL-NEXT: Type: SHT_MIPS_OPTIONS
ELF-MIPS64EL-NEXT: Flags: [ SHF_ALLOC ] ELF-MIPS64EL-NEXT: Flags: [ SHF_ALLOC ]

View File

@ -32,7 +32,7 @@ Sections:
Type: SHT_NOBITS Type: SHT_NOBITS
Flags: [ SHF_WRITE, SHF_ALLOC ] Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x0000000000000001 AddressAlign: 0x0000000000000001
Content: '' Size: 0
- Name: .ARM.attributes - Name: .ARM.attributes
Type: SHT_ARM_ATTRIBUTES Type: SHT_ARM_ATTRIBUTES
AddressAlign: 0x0000000000000001 AddressAlign: 0x0000000000000001

View File

@ -40,6 +40,7 @@ class ELFDumper {
ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr); ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::RawContentSection *> ErrorOr<ELFYAML::RawContentSection *>
dumpContentSection(const Elf_Shdr *Shdr); dumpContentSection(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr); ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr); ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
@ -104,6 +105,13 @@ ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get())); Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
break; break;
} }
case ELF::SHT_NOBITS: {
ErrorOr<ELFYAML::NoBitsSection *> S = dumpNoBitsSection(&Sec);
if (std::error_code EC = S.getError())
return EC;
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
break;
}
default: { default: {
ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec); ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
if (std::error_code EC = S.getError()) if (std::error_code EC = S.getError())
@ -304,6 +312,18 @@ ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
return S.release(); return S.release();
} }
template <class ELFT>
ErrorOr<ELFYAML::NoBitsSection *>
ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
auto S = make_unique<ELFYAML::NoBitsSection>();
if (std::error_code EC = dumpCommonSection(Shdr, *S))
return EC;
S->Size = Shdr->sh_size;
return S.release();
}
template <class ELFT> template <class ELFT>
ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) { ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
auto S = make_unique<ELFYAML::Group>(); auto S = make_unique<ELFYAML::Group>();

View File

@ -241,6 +241,12 @@ bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
} else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) { } else if (auto S = dyn_cast<ELFYAML::MipsABIFlags>(Sec.get())) {
if (!writeSectionContent(SHeader, *S, CBA)) if (!writeSectionContent(SHeader, *S, CBA))
return false; return false;
} else if (auto S = dyn_cast<ELFYAML::NoBitsSection>(Sec.get())) {
SHeader.sh_entsize = 0;
SHeader.sh_size = S->Size;
// SHT_NOBITS section does not have content
// so just to setup the section offset.
CBA.getOSAndAlignedOffset(SHeader.sh_offset);
} else } else
llvm_unreachable("Unknown section type"); llvm_unreachable("Unknown section type");