[obj2yaml/yaml2obj] Add SHT_GROUP support.

This adds section group support to the tools obj2yaml and yaml2obj.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230124 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Shankar Easwaran 2015-02-21 04:28:26 +00:00
parent a983427f67
commit 5872174fd8
6 changed files with 154 additions and 4 deletions

View File

@ -72,14 +72,20 @@ struct LocalGlobalWeakSymbols {
std::vector<Symbol> Global;
std::vector<Symbol> Weak;
};
struct SectionOrType {
StringRef sectionNameOrType;
};
struct Section {
enum class SectionKind { RawContent, Relocation };
enum class SectionKind { Group, RawContent, Relocation };
SectionKind Kind;
StringRef Name;
ELF_SHT Type;
ELF_SHF Flags;
llvm::yaml::Hex64 Address;
StringRef Link;
StringRef Info;
llvm::yaml::Hex64 AddressAlign;
Section(SectionKind Kind) : Kind(Kind) {}
virtual ~Section();
@ -92,6 +98,17 @@ struct RawContentSection : Section {
return S->Kind == SectionKind::RawContent;
}
};
struct Group : Section {
// Members of a group contain a flag and a list of section indices
// that are part of the group.
std::vector<SectionOrType> Members;
Group() : Section(SectionKind::Group) {}
static bool classof(const Section *S) {
return S->Kind == SectionKind::Group;
}
};
struct Relocation {
llvm::yaml::Hex64 Offset;
int64_t Addend;
@ -99,7 +116,6 @@ struct Relocation {
StringRef Symbol;
};
struct RelocationSection : Section {
StringRef Info;
std::vector<Relocation> Relocations;
RelocationSection() : Section(SectionKind::Relocation) {}
static bool classof(const Section *S) {
@ -122,6 +138,7 @@ struct Object {
LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
namespace llvm {
namespace yaml {
@ -221,6 +238,10 @@ struct MappingTraits<ELFYAML::Object> {
static void mapping(IO &IO, ELFYAML::Object &Object);
};
template <> struct MappingTraits<ELFYAML::SectionOrType> {
static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
};
} // end namespace yaml
} // end namespace llvm

View File

@ -506,6 +506,7 @@ static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
IO.mapOptional("Address", Section.Address, Hex64(0));
IO.mapOptional("Link", Section.Link, StringRef());
IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
IO.mapOptional("Info", Section.Info, StringRef());
}
static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
@ -516,10 +517,19 @@ static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
commonSectionMapping(IO, Section);
IO.mapOptional("Info", Section.Info, StringRef());
IO.mapOptional("Relocations", Section.Relocations);
}
static void groupSectionMapping(IO &IO, ELFYAML::Group &group) {
commonSectionMapping(IO, group);
IO.mapRequired("Members", group.Members);
}
void MappingTraits<ELFYAML::SectionOrType>::mapping(
IO &IO, ELFYAML::SectionOrType &sectionOrType) {
IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
}
void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
ELFYAML::ELF_SHT sectionType;
@ -535,6 +545,11 @@ void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
Section.reset(new ELFYAML::RelocationSection());
sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
break;
case ELF::SHT_GROUP:
if (!IO.outputting())
Section.reset(new ELFYAML::Group());
groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
break;
default:
if (!IO.outputting())
Section.reset(new ELFYAML::RawContentSection());

Binary file not shown.

View File

@ -0,0 +1,26 @@
# Checks that the tool is able to read section groups with ELF.
RUN: obj2yaml %p/Inputs/sectionGroup.elf.x86-64 > %t1.sectiongroup.yaml
RUN: FileCheck %s --check-prefix ELF-GROUP < %t1.sectiongroup.yaml
RUN: yaml2obj -format=elf %t1.sectiongroup.yaml -o %t2.o.elf
RUN: llvm-readobj -sections %t2.o.elf | FileCheck %s -check-prefix=SECTIONS
#ELF-GROUP: - Name: .group
#ELF-GROUP: Type: SHT_GROUP
#ELF-GROUP: Link: .symtab
#ELF-GROUP: Info: a
#ELF-GROUP: Members:
#ELF-GROUP: - SectionOrType: GRP_COMDAT
#ELF-GROUP: - SectionOrType: .rodata.a
#SECTIONS: Format: ELF64-x86-64
#SECTIONS: Arch: x86_64
#SECTIONS: AddressSize: 64bit
#SECTIONS: Section {
#SECTIONS: Index: 1
#SECTIONS: Name: .group (21)
#SECTIONS: Type: SHT_GROUP (0x11)
#SECTIONS: Flags [ (0x0)
#SECTIONS: ]
#SECTIONS: Address: 0x0
#SECTIONS: Size: 8
#SECTIONS: AddressAlignment: 4
#SECTIONS: EntrySize: 4
#SECTIONS: }

View File

@ -21,8 +21,10 @@ namespace {
template <class ELFT>
class ELFDumper {
typedef object::Elf_Sym_Impl<ELFT> Elf_Sym;
typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
typedef typename object::ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
const object::ELFFile<ELFT> &Obj;
@ -38,6 +40,7 @@ class ELFDumper {
ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::RawContentSection *>
dumpContentSection(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
public:
ELFDumper(const object::ELFFile<ELFT> &O);
@ -86,7 +89,13 @@ ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
break;
}
// FIXME: Support SHT_GROUP section format.
case ELF::SHT_GROUP: {
ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
if (std::error_code EC = G.getError())
return EC;
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
break;
}
default: {
ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
if (std::error_code EC = S.getError())
@ -274,6 +283,41 @@ ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
return S.release();
}
template <class ELFT>
ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
auto S = make_unique<ELFYAML::Group>();
if (std::error_code EC = dumpCommonSection(Shdr, *S))
return EC;
// Get sh_info which is the signature.
const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link);
auto sectionContents = Obj.getSectionContents(Shdr);
if (std::error_code ec = sectionContents.getError())
return ec;
ErrorOr<StringRef> symbolName = Obj.getSymbolName(symtab, symbol);
if (std::error_code EC = symbolName.getError())
return EC;
S->Info = *symbolName;
const Elf_Word *groupMembers =
reinterpret_cast<const Elf_Word *>(sectionContents->data());
const long count = (Shdr->sh_size) / sizeof(Elf_Word);
ELFYAML::SectionOrType s;
for (int i = 0; i < count; i++) {
if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
s.sectionNameOrType = "GRP_COMDAT";
} else {
const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]);
ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr);
if (std::error_code ec = sectionName.getError())
return ec;
s.sectionNameOrType = *sectionName;
}
S->Members.push_back(s);
}
return S.release();
}
template <class ELFT>
static std::error_code elf2yaml(raw_ostream &Out,
const object::ELFFile<ELFT> &Obj) {

View File

@ -131,6 +131,8 @@ class ELFState {
bool writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::RelocationSection &Section,
ContiguousBlobAccumulator &CBA);
bool writeSectionContent(Elf_Shdr &SHeader, const ELFYAML::Group &Group,
ContiguousBlobAccumulator &CBA);
// - SHT_NULL entry (placed first, i.e. 0'th entry)
// - symbol table (.symtab) (placed third to last)
@ -221,6 +223,16 @@ bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
}
SHeader.sh_info = Index;
if (!writeSectionContent(SHeader, *S, CBA))
return false;
} else if (auto S = dyn_cast<ELFYAML::Group>(Sec.get())) {
unsigned SymIdx;
if (SymN2I.lookup(S->Info, SymIdx)) {
errs() << "error: Unknown symbol referenced: '" << S->Info
<< "' at YAML section '" << S->Name << "'.\n";
return false;
}
SHeader.sh_info = SymIdx;
if (!writeSectionContent(SHeader, *S, CBA))
return false;
} else
@ -370,6 +382,38 @@ ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
return true;
}
template <class ELFT>
bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::Group &Section,
ContiguousBlobAccumulator &CBA) {
typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
if (Section.Type != llvm::ELF::SHT_GROUP) {
errs() << "error: Invalid section type.\n";
return false;
}
SHeader.sh_entsize = sizeof(Elf_Word);
SHeader.sh_size = SHeader.sh_entsize * Section.Members.size();
auto &OS = CBA.getOSAndAlignedOffset(SHeader.sh_offset);
for (auto member : Section.Members) {
Elf_Word SIdx;
unsigned int sectionIndex = 0;
if (member.sectionNameOrType == "GRP_COMDAT")
sectionIndex = llvm::ELF::GRP_COMDAT;
else if (SN2I.lookup(member.sectionNameOrType, sectionIndex)) {
errs() << "error: Unknown section referenced: '"
<< member.sectionNameOrType << "' at YAML section' "
<< Section.Name << "\n";
return false;
}
SIdx = sectionIndex;
OS.write((const char *)&SIdx, sizeof(SIdx));
}
return true;
}
template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
SN2I.addName(".symtab", getDotSymTabSecNo());
SN2I.addName(".strtab", getDotStrTabSecNo());