[yaml2obj][ELF] Convert some static functions into class members to

reduce number of arguments.

No functional changes.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205434 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Simon Atanasyan 2014-04-02 16:34:54 +00:00
parent 4fd30e7850
commit be2ec9b091

View File

@ -134,32 +134,36 @@ static void zero(T &Obj) {
memset(&Obj, 0, sizeof(Obj)); memset(&Obj, 0, sizeof(Obj));
} }
/// \brief Create a string table in `SHeader`, which we assume is already
/// zero'd.
template <class Elf_Shdr>
static void createStringTableSectionHeader(Elf_Shdr &SHeader,
StringTableBuilder &STB,
ContiguousBlobAccumulator &CBA) {
SHeader.sh_type = ELF::SHT_STRTAB;
STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
SHeader.sh_size = STB.size();
SHeader.sh_addralign = 1;
}
namespace { namespace {
/// \brief "Single point of truth" for the ELF file construction. /// \brief "Single point of truth" for the ELF file construction.
/// TODO: This class still has a ways to go before it is truly a "single /// TODO: This class still has a ways to go before it is truly a "single
/// point of truth". /// point of truth".
template <class ELFT> template <class ELFT>
class ELFState { class ELFState {
typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
/// \brief The future ".strtab" section. /// \brief The future ".strtab" section.
StringTableBuilder DotStrtab; StringTableBuilder DotStrtab;
/// \brief The future ".shstrtab" section.
StringTableBuilder DotShStrtab;
SectionNameToIdxMap SN2I; SectionNameToIdxMap SN2I;
const ELFYAML::Object &Doc; const ELFYAML::Object &Doc;
public: bool buildSectionIndex();
ELFState(const ELFYAML::Object &D) : Doc(D) {} void initELFHeader(Elf_Ehdr &Header);
bool initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
ContiguousBlobAccumulator &CBA);
void initSymtabSectionHeader(Elf_Shdr &SHeader,
ContiguousBlobAccumulator &CBA);
void initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
StringTableBuilder &STB,
ContiguousBlobAccumulator &CBA);
void addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
std::vector<Elf_Sym> &Syms, unsigned SymbolBinding);
// - SHT_NULL entry (placed first, i.e. 0'th entry) // - SHT_NULL entry (placed first, i.e. 0'th entry)
// - symbol table (.symtab) (placed third to last) // - symbol table (.symtab) (placed third to last)
@ -170,48 +174,128 @@ public:
unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; } unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; }
unsigned getSectionCount() const { return Doc.Sections.size() + 4; } unsigned getSectionCount() const { return Doc.Sections.size() + 4; }
StringTableBuilder &getStringTable() { return DotStrtab; } ELFState(const ELFYAML::Object &D) : Doc(D) {}
SectionNameToIdxMap &getSN2I() { return SN2I; }
bool buildSectionIndex() { public:
SN2I.addName(".symtab", getDotSymTabSecNo()); static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
SN2I.addName(".strtab", getDotStrTabSecNo());
SN2I.addName(".shstrtab", getDotShStrTabSecNo());
for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
StringRef Name = Doc.Sections[i].Name;
if (Name.empty())
continue;
// "+ 1" to take into account the SHT_NULL entry.
if (SN2I.addName(Name, i + 1)) {
errs() << "error: Repeated section name: '" << Name
<< "' at YAML section number " << i << ".\n";
return false;
}
}
return true;
}
}; };
} // end anonymous namespace } // end anonymous namespace
// FIXME: At this point it is fairly clear that we need to refactor these
// static functions into methods of a class sharing some typedefs. These
// ELF type names are insane.
template <class ELFT> template <class ELFT>
static void void ELFState<ELFT>::initELFHeader(Elf_Ehdr &Header) {
addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, ELFState<ELFT> &State, using namespace llvm::ELF;
std::vector<typename object::ELFFile<ELFT>::Elf_Sym> &Syms, zero(Header);
unsigned SymbolBinding) { Header.e_ident[EI_MAG0] = 0x7f;
typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym; Header.e_ident[EI_MAG1] = 'E';
Header.e_ident[EI_MAG2] = 'L';
Header.e_ident[EI_MAG3] = 'F';
Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
bool IsLittleEndian = ELFT::TargetEndianness == support::little;
Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
Header.e_ident[EI_VERSION] = EV_CURRENT;
Header.e_ident[EI_OSABI] = Doc.Header.OSABI;
Header.e_ident[EI_ABIVERSION] = 0;
Header.e_type = Doc.Header.Type;
Header.e_machine = Doc.Header.Machine;
Header.e_version = EV_CURRENT;
Header.e_entry = Doc.Header.Entry;
Header.e_flags = Doc.Header.Flags;
Header.e_ehsize = sizeof(Elf_Ehdr);
Header.e_shentsize = sizeof(Elf_Shdr);
// Immediately following the ELF header.
Header.e_shoff = sizeof(Header);
Header.e_shnum = getSectionCount();
Header.e_shstrndx = getDotShStrTabSecNo();
}
template <class ELFT>
bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
ContiguousBlobAccumulator &CBA) {
// Ensure SHN_UNDEF entry is present. An all-zero section header is a
// valid SHN_UNDEF entry since SHT_NULL == 0.
Elf_Shdr SHeader;
zero(SHeader);
SHeaders.push_back(SHeader);
for (const auto &Sec : Doc.Sections) {
zero(SHeader);
SHeader.sh_name = DotShStrtab.addString(Sec.Name);
SHeader.sh_type = Sec.Type;
SHeader.sh_flags = Sec.Flags;
SHeader.sh_addr = Sec.Address;
Sec.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
SHeader.sh_size = Sec.Content.binary_size();
if (!Sec.Link.empty()) {
unsigned Index;
if (SN2I.lookupSection(Sec.Link, Index)) {
errs() << "error: Unknown section referenced: '" << Sec.Link
<< "' at YAML section '" << Sec.Name << "'.\n";
return false;;
}
SHeader.sh_link = Index;
}
SHeader.sh_info = 0;
SHeader.sh_addralign = Sec.AddressAlign;
SHeader.sh_entsize = 0;
SHeaders.push_back(SHeader);
}
return true;
}
template <class ELFT>
void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
ContiguousBlobAccumulator &CBA) {
zero(SHeader);
SHeader.sh_name = DotShStrtab.addString(StringRef(".symtab"));
SHeader.sh_type = ELF::SHT_SYMTAB;
SHeader.sh_link = getDotStrTabSecNo();
// One greater than symbol table index of the last local symbol.
SHeader.sh_info = Doc.Symbols.Local.size() + 1;
SHeader.sh_entsize = sizeof(Elf_Sym);
std::vector<Elf_Sym> Syms;
{
// Ensure STN_UNDEF is present
Elf_Sym Sym;
zero(Sym);
Syms.push_back(Sym);
}
addSymbols(Doc.Symbols.Local, Syms, ELF::STB_LOCAL);
addSymbols(Doc.Symbols.Global, Syms, ELF::STB_GLOBAL);
addSymbols(Doc.Symbols.Weak, Syms, ELF::STB_WEAK);
writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset),
makeArrayRef(Syms));
SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
}
template <class ELFT>
void ELFState<ELFT>::initStrtabSectionHeader(Elf_Shdr &SHeader, StringRef Name,
StringTableBuilder &STB,
ContiguousBlobAccumulator &CBA) {
zero(SHeader);
SHeader.sh_name = DotShStrtab.addString(Name);
SHeader.sh_type = ELF::SHT_STRTAB;
STB.writeToStream(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
SHeader.sh_size = STB.size();
SHeader.sh_addralign = 1;
}
template <class ELFT>
void ELFState<ELFT>::addSymbols(const std::vector<ELFYAML::Symbol> &Symbols,
std::vector<Elf_Sym> &Syms,
unsigned SymbolBinding) {
for (const auto &Sym : Symbols) { for (const auto &Sym : Symbols) {
Elf_Sym Symbol; Elf_Sym Symbol;
zero(Symbol); zero(Symbol);
if (!Sym.Name.empty()) if (!Sym.Name.empty())
Symbol.st_name = State.getStringTable().addString(Sym.Name); Symbol.st_name = DotStrtab.addString(Sym.Name);
Symbol.setBindingAndType(SymbolBinding, Sym.Type); Symbol.setBindingAndType(SymbolBinding, Sym.Type);
if (!Sym.Section.empty()) { if (!Sym.Section.empty()) {
unsigned Index; unsigned Index;
if (State.getSN2I().lookupSection(Sym.Section, Index)) { if (SN2I.lookupSection(Sym.Section, Index)) {
errs() << "error: Unknown section referenced: '" << Sym.Section errs() << "error: Unknown section referenced: '" << Sym.Section
<< "' by YAML symbol " << Sym.Name << ".\n"; << "' by YAML symbol " << Sym.Name << ".\n";
exit(1); exit(1);
@ -224,137 +308,63 @@ addSymbols(const std::vector<ELFYAML::Symbol> &Symbols, ELFState<ELFT> &State,
} }
} }
template <class ELFT> template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
static void SN2I.addName(".symtab", getDotSymTabSecNo());
handleSymtabSectionHeader(const ELFYAML::LocalGlobalWeakSymbols &Symbols, SN2I.addName(".strtab", getDotStrTabSecNo());
ELFState<ELFT> &State, SN2I.addName(".shstrtab", getDotShStrTabSecNo());
typename object::ELFFile<ELFT>::Elf_Shdr &SHeader,
ContiguousBlobAccumulator &CBA) {
typedef typename object::ELFFile<ELFT>::Elf_Sym Elf_Sym; for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
SHeader.sh_type = ELF::SHT_SYMTAB; StringRef Name = Doc.Sections[i].Name;
SHeader.sh_link = State.getDotStrTabSecNo(); if (Name.empty())
// One greater than symbol table index of the last local symbol. continue;
SHeader.sh_info = Symbols.Local.size() + 1; // "+ 1" to take into account the SHT_NULL entry.
SHeader.sh_entsize = sizeof(Elf_Sym); if (SN2I.addName(Name, i + 1)) {
errs() << "error: Repeated section name: '" << Name
std::vector<Elf_Sym> Syms; << "' at YAML section number " << i << ".\n";
{ return false;
// Ensure STN_UNDEF is present }
Elf_Sym Sym;
zero(Sym);
Syms.push_back(Sym);
} }
addSymbols(Symbols.Local, State, Syms, ELF::STB_LOCAL); return true;
addSymbols(Symbols.Global, State, Syms, ELF::STB_GLOBAL);
addSymbols(Symbols.Weak, State, Syms, ELF::STB_WEAK);
writeArrayData(CBA.getOSAndAlignedOffset(SHeader.sh_offset),
makeArrayRef(Syms));
SHeader.sh_size = arrayDataSize(makeArrayRef(Syms));
} }
template <class ELFT> template <class ELFT>
static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) { int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
using namespace llvm::ELF;
typedef typename object::ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
ELFState<ELFT> State(Doc); ELFState<ELFT> State(Doc);
if (!State.buildSectionIndex()) if (!State.buildSectionIndex())
return 1; return 1;
const ELFYAML::FileHeader &Hdr = Doc.Header;
Elf_Ehdr Header; Elf_Ehdr Header;
zero(Header); State.initELFHeader(Header);
Header.e_ident[EI_MAG0] = 0x7f;
Header.e_ident[EI_MAG1] = 'E';
Header.e_ident[EI_MAG2] = 'L';
Header.e_ident[EI_MAG3] = 'F';
Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
bool IsLittleEndian = ELFT::TargetEndianness == support::little;
Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
Header.e_ident[EI_VERSION] = EV_CURRENT;
Header.e_ident[EI_OSABI] = Hdr.OSABI;
Header.e_ident[EI_ABIVERSION] = 0;
Header.e_type = Hdr.Type;
Header.e_machine = Hdr.Machine;
Header.e_version = EV_CURRENT;
Header.e_entry = Hdr.Entry;
Header.e_flags = Hdr.Flags;
Header.e_ehsize = sizeof(Elf_Ehdr);
// TODO: Flesh out section header support. // TODO: Flesh out section header support.
// TODO: Program headers. // TODO: Program headers.
Header.e_shentsize = sizeof(Elf_Shdr);
// Immediately following the ELF header.
Header.e_shoff = sizeof(Header);
Header.e_shnum = State.getSectionCount();
Header.e_shstrndx = State.getDotShStrTabSecNo();
// XXX: This offset is tightly coupled with the order that we write // XXX: This offset is tightly coupled with the order that we write
// things to `OS`. // things to `OS`.
const size_t SectionContentBeginOffset = const size_t SectionContentBeginOffset =
Header.e_ehsize + Header.e_shentsize * Header.e_shnum; Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
ContiguousBlobAccumulator CBA(SectionContentBeginOffset); ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
StringTableBuilder SHStrTab;
std::vector<Elf_Shdr> SHeaders; std::vector<Elf_Shdr> SHeaders;
{ if(!State.initSectionHeaders(SHeaders, CBA))
// Ensure SHN_UNDEF entry is present. An all-zero section header is a return 1;
// valid SHN_UNDEF entry since SHT_NULL == 0.
Elf_Shdr SHdr;
zero(SHdr);
SHeaders.push_back(SHdr);
}
for (const auto &Sec : Doc.Sections) {
Elf_Shdr SHeader;
zero(SHeader);
SHeader.sh_name = SHStrTab.addString(Sec.Name);
SHeader.sh_type = Sec.Type;
SHeader.sh_flags = Sec.Flags;
SHeader.sh_addr = Sec.Address;
Sec.Content.writeAsBinary(CBA.getOSAndAlignedOffset(SHeader.sh_offset));
SHeader.sh_size = Sec.Content.binary_size();
if (!Sec.Link.empty()) {
unsigned Index;
if (State.getSN2I().lookupSection(Sec.Link, Index)) {
errs() << "error: Unknown section referenced: '" << Sec.Link
<< "' at YAML section '" << Sec.Name << "'.\n";
return 1;
}
SHeader.sh_link = Index;
}
SHeader.sh_info = 0;
SHeader.sh_addralign = Sec.AddressAlign;
SHeader.sh_entsize = 0;
SHeaders.push_back(SHeader);
}
// .symtab section. // .symtab section.
Elf_Shdr SymtabSHeader; Elf_Shdr SymtabSHeader;
zero(SymtabSHeader); State.initSymtabSectionHeader(SymtabSHeader, CBA);
SymtabSHeader.sh_name = SHStrTab.addString(StringRef(".symtab"));
handleSymtabSectionHeader<ELFT>(Doc.Symbols, State, SymtabSHeader, CBA);
SHeaders.push_back(SymtabSHeader); SHeaders.push_back(SymtabSHeader);
// .strtab string table header. // .strtab string table header.
Elf_Shdr DotStrTabSHeader; Elf_Shdr DotStrTabSHeader;
zero(DotStrTabSHeader); State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
DotStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".strtab")); CBA);
createStringTableSectionHeader(DotStrTabSHeader, State.getStringTable(), CBA);
SHeaders.push_back(DotStrTabSHeader); SHeaders.push_back(DotStrTabSHeader);
// Section header string table header. // .shstrtab string table header.
Elf_Shdr SHStrTabSHeader; Elf_Shdr ShStrTabSHeader;
zero(SHStrTabSHeader); State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
SHStrTabSHeader.sh_name = SHStrTab.addString(StringRef(".shstrtab")); CBA);
createStringTableSectionHeader(SHStrTabSHeader, SHStrTab, CBA); SHeaders.push_back(ShStrTabSHeader);
SHeaders.push_back(SHStrTabSHeader);
OS.write((const char *)&Header, sizeof(Header)); OS.write((const char *)&Header, sizeof(Header));
writeArrayData(OS, makeArrayRef(SHeaders)); writeArrayData(OS, makeArrayRef(SHeaders));
@ -385,13 +395,13 @@ int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
typedef ELFType<support::big, 4, false> BE32; typedef ELFType<support::big, 4, false> BE32;
if (is64Bit(Doc)) { if (is64Bit(Doc)) {
if (isLittleEndian(Doc)) if (isLittleEndian(Doc))
return writeELF<LE64>(outs(), Doc); return ELFState<LE64>::writeELF(outs(), Doc);
else else
return writeELF<BE64>(outs(), Doc); return ELFState<BE64>::writeELF(outs(), Doc);
} else { } else {
if (isLittleEndian(Doc)) if (isLittleEndian(Doc))
return writeELF<LE32>(outs(), Doc); return ELFState<LE32>::writeELF(outs(), Doc);
else else
return writeELF<BE32>(outs(), Doc); return ELFState<BE32>::writeELF(outs(), Doc);
} }
} }