summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-objcopy/Object.cpp
diff options
context:
space:
mode:
authorJake Ehrlich <jakehehrlich@google.com>2018-01-25 22:46:17 +0000
committerJake Ehrlich <jakehehrlich@google.com>2018-01-25 22:46:17 +0000
commit76e9110f3dd1c11876793673fe491606dbaaa5d1 (patch)
tree45a0c0af60c29147d8aed61044af247d60949ae0 /llvm/tools/llvm-objcopy/Object.cpp
parent8abbc96c188e8fce2e21540921dd5bbb5e189b19 (diff)
downloadbcm5719-llvm-76e9110f3dd1c11876793673fe491606dbaaa5d1.tar.gz
bcm5719-llvm-76e9110f3dd1c11876793673fe491606dbaaa5d1.zip
[llvm-objcopy] Refactor llvm-objcopy to use reader and writer objects
While writing code for input and output formats in llvm-objcopy it became apparent that there was a code health problem. This change attempts to solve that problem by refactoring the code to use Reader and Writer objects that can read in different objects in different formats, convert them to a single shared internal representation, and then write them to any other representation. New classes: Reader: the base class used to construct instances of the internal representation Writer: the base class used to write out instances of the internal representation ELFBuilder: a helper class for ELFWriter that takes an ELFFile and converts it to a Object SectionVisitor: it became necessary to remove writeSection from SectionBase because, under the new Reader/Writer scheme, it's possible to convert between ELF Types such as ELF32LE and ELF32BE. This isn't possible with writeSection because it (dynamically) depends on the underlying section type *and* (statically) depends on the ELF type. Bad things would happen if the underlying sections for ELF32LE were used for writing to ELF64BE. To avoid this code smell (which would have compiled, run, and output some nonsesnse) I decoupled writing of sections from a class. SectionWriter: This is just the ELFT templated implementation of SectionVisitor. Many classes now have this class as a friend so that the writing methods in this class can write out private data. ELFWriter: This is the Writer that outputs to ELF BinaryWriter: This is the Writer that outputs to Binary ElfType: Because the ELF Type is not a part of the Object anymore we need a way to construct the correct default Writer based on properties of the Reader. This enum just keeps track of the ELF type of the input so it can be used as the default output type as well. Object has correspondingly undergone some serious changes as well. It now has more generic methods for building and manipulating ELF binaries. This interface makes ELFBuilder easy enough to use and will make the BinaryReader/Builder easy to create as well. Most changes in this diff are cosmetic and deal with the fact that a method has been moved from one class to another or a change from a pointer to a reference. Almost no changes should result in a functional difference (this is after all a refactor). One minor functional change was made and the result can be seen in remove-shstrtab-error.test. The fact that it fails hasn't changed but the error message has changed because that failure is detected at a later point in the code now (because WriteSectionHeaders is a property of the ElfWriter *not* a property of the Object). I'd say roughly 80-90% of this code is cosmetically different, 10-19% is different but functionally the same, and 1-5% is functionally different despite not causing a change in tests. Differential Revision: https://reviews.llvm.org/D42222 llvm-svn: 323480
Diffstat (limited to 'llvm/tools/llvm-objcopy/Object.cpp')
-rw-r--r--llvm/tools/llvm-objcopy/Object.cpp602
1 files changed, 332 insertions, 270 deletions
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp
index 944075d6197..c7b4c1f3dd3 100644
--- a/llvm/tools/llvm-objcopy/Object.cpp
+++ b/llvm/tools/llvm-objcopy/Object.cpp
@@ -30,61 +30,73 @@ using namespace llvm;
using namespace object;
using namespace ELF;
-template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const {
+template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
using Elf_Ehdr = typename ELFT::Ehdr;
using Elf_Phdr = typename ELFT::Phdr;
- uint8_t *Buf = Out.getBufferStart();
- Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr);
+ uint8_t *Buf = BufPtr->getBufferStart();
+ Buf += sizeof(Elf_Ehdr) + Seg.Index * sizeof(Elf_Phdr);
Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(Buf);
- Phdr.p_type = Type;
- Phdr.p_flags = Flags;
- Phdr.p_offset = Offset;
- Phdr.p_vaddr = VAddr;
- Phdr.p_paddr = PAddr;
- Phdr.p_filesz = FileSize;
- Phdr.p_memsz = MemSize;
- Phdr.p_align = Align;
-}
-
-void Segment::writeSegment(FileOutputBuffer &Out) const {
- uint8_t *Buf = Out.getBufferStart() + Offset;
- // We want to maintain segments' interstitial data and contents exactly.
- // This lets us just copy segments directly.
- std::copy(std::begin(Contents), std::end(Contents), Buf);
+ Phdr.p_type = Seg.Type;
+ Phdr.p_flags = Seg.Flags;
+ Phdr.p_offset = Seg.Offset;
+ Phdr.p_vaddr = Seg.VAddr;
+ Phdr.p_paddr = Seg.PAddr;
+ Phdr.p_filesz = Seg.FileSize;
+ Phdr.p_memsz = Seg.MemSize;
+ Phdr.p_align = Seg.Align;
}
void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
void SectionBase::initialize(SectionTableRef SecTable) {}
void SectionBase::finalize() {}
-template <class ELFT>
-void SectionBase::writeHeader(FileOutputBuffer &Out) const {
- uint8_t *Buf = Out.getBufferStart();
- Buf += HeaderOffset;
+template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
+ uint8_t *Buf = BufPtr->getBufferStart();
+ Buf += Sec.HeaderOffset;
typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(Buf);
- Shdr.sh_name = NameIndex;
- Shdr.sh_type = Type;
- Shdr.sh_flags = Flags;
- Shdr.sh_addr = Addr;
- Shdr.sh_offset = Offset;
- Shdr.sh_size = Size;
- Shdr.sh_link = Link;
- Shdr.sh_info = Info;
- Shdr.sh_addralign = Align;
- Shdr.sh_entsize = EntrySize;
-}
-
-void Section::writeSection(FileOutputBuffer &Out) const {
- if (Type == SHT_NOBITS)
+ Shdr.sh_name = Sec.NameIndex;
+ Shdr.sh_type = Sec.Type;
+ Shdr.sh_flags = Sec.Flags;
+ Shdr.sh_addr = Sec.Addr;
+ Shdr.sh_offset = Sec.Offset;
+ Shdr.sh_size = Sec.Size;
+ Shdr.sh_link = Sec.Link;
+ Shdr.sh_info = Sec.Info;
+ Shdr.sh_addralign = Sec.Align;
+ Shdr.sh_entsize = Sec.EntrySize;
+}
+
+SectionVisitor::~SectionVisitor() {}
+
+void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
+ error("Cannot write symbol table '" + Sec.Name + "' out to binary");
+}
+
+void BinarySectionWriter::visit(const RelocationSection &Sec) {
+ error("Cannot write relocation section '" + Sec.Name + "' out to binary");
+}
+
+void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
+ error("Cannot write '.gnu_debuglink' out to binary");
+}
+
+void SectionWriter::visit(const Section &Sec) {
+ if (Sec.Type == SHT_NOBITS)
return;
- uint8_t *Buf = Out.getBufferStart() + Offset;
- std::copy(std::begin(Contents), std::end(Contents), Buf);
+ uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
+ std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf);
}
-void OwnedDataSection::writeSection(FileOutputBuffer &Out) const {
- uint8_t *Buf = Out.getBufferStart() + Offset;
- std::copy(std::begin(Data), std::end(Data), Buf);
+void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
+
+void SectionWriter::visit(const OwnedDataSection &Sec) {
+ uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
+ std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf);
+}
+
+void OwnedDataSection::accept(SectionVisitor &Visitor) const {
+ Visitor.visit(*this);
}
void StringTableSection::addString(StringRef Name) {
@@ -98,8 +110,12 @@ uint32_t StringTableSection::findIndex(StringRef Name) const {
void StringTableSection::finalize() { StrTabBuilder.finalize(); }
-void StringTableSection::writeSection(FileOutputBuffer &Out) const {
- StrTabBuilder.write(Out.getBufferStart() + Offset);
+void SectionWriter::visit(const StringTableSection &Sec) {
+ Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
+}
+
+void StringTableSection::accept(SectionVisitor &Visitor) const {
+ Visitor.visit(*this);
}
static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
@@ -234,12 +250,12 @@ const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
}
template <class ELFT>
-void SymbolTableSectionImpl<ELFT>::writeSection(FileOutputBuffer &Out) const {
+void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
uint8_t *Buf = Out.getBufferStart();
- Buf += Offset;
+ Buf += Sec.Offset;
typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
// Loop though symbols setting each entry of the symbol table.
- for (auto &Symbol : Symbols) {
+ for (auto &Symbol : Sec.Symbols) {
Sym->st_name = Symbol->NameIndex;
Sym->st_value = Symbol->Value;
Sym->st_size = Symbol->Size;
@@ -251,6 +267,10 @@ void SymbolTableSectionImpl<ELFT>::writeSection(FileOutputBuffer &Out) const {
}
}
+void SymbolTableSection::accept(SectionVisitor &Visitor) const {
+ Visitor.visit(*this);
+}
+
template <class SymTabType>
void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
const SectionBase *Sec) {
@@ -294,9 +314,8 @@ void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
Rela.r_addend = Addend;
}
-template <class ELFT>
-template <class T>
-void RelocationSection<ELFT>::writeRel(T *Buf) const {
+template <class RelRange, class T>
+void writeRel(const RelRange &Relocations, T *Buf) {
for (const auto &Reloc : Relocations) {
Buf->r_offset = Reloc.Offset;
setAddend(*Buf, Reloc.Addend);
@@ -306,17 +325,25 @@ void RelocationSection<ELFT>::writeRel(T *Buf) const {
}
template <class ELFT>
-void RelocationSection<ELFT>::writeSection(FileOutputBuffer &Out) const {
- uint8_t *Buf = Out.getBufferStart() + Offset;
- if (Type == SHT_REL)
- writeRel(reinterpret_cast<Elf_Rel *>(Buf));
+void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
+ uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
+ if (Sec.Type == SHT_REL)
+ writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
else
- writeRel(reinterpret_cast<Elf_Rela *>(Buf));
+ writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
+}
+
+void RelocationSection::accept(SectionVisitor &Visitor) const {
+ Visitor.visit(*this);
+}
+
+void SectionWriter::visit(const DynamicRelocationSection &Sec) {
+ std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
+ Out.getBufferStart() + Sec.Offset);
}
-void DynamicRelocationSection::writeSection(FileOutputBuffer &Out) const {
- std::copy(std::begin(Contents), std::end(Contents),
- Out.getBufferStart() + Offset);
+void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
+ Visitor.visit(*this);
}
void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) {
@@ -344,8 +371,7 @@ void SectionWithStrTab::initialize(SectionTableRef SecTable) {
void SectionWithStrTab::finalize() { this->Link = StrTab->Index; }
-template <class ELFT>
-void GnuDebugLinkSection<ELFT>::init(StringRef File, StringRef Data) {
+void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
FileName = sys::path::stem(File);
// The format for the .gnu_debuglink starts with the stemmed file name and is
// followed by a null terminator and then the CRC32 of the file. The CRC32
@@ -368,9 +394,7 @@ void GnuDebugLinkSection<ELFT>::init(StringRef File, StringRef Data) {
CRC32 = ~crc.getCRC();
}
-template <class ELFT>
-GnuDebugLinkSection<ELFT>::GnuDebugLinkSection(StringRef File)
- : FileName(File) {
+GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
// Read in the file to compute the CRC of it.
auto DebugOrErr = MemoryBuffer::getFile(File);
if (!DebugOrErr)
@@ -380,12 +404,17 @@ GnuDebugLinkSection<ELFT>::GnuDebugLinkSection(StringRef File)
}
template <class ELFT>
-void GnuDebugLinkSection<ELFT>::writeSection(FileOutputBuffer &Out) const {
- auto Buf = Out.getBufferStart() + Offset;
+void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
+ auto Buf = Out.getBufferStart() + Sec.Offset;
char *File = reinterpret_cast<char *>(Buf);
- Elf_Word *CRC = reinterpret_cast<Elf_Word *>(Buf + Size - sizeof(Elf_Word));
- *CRC = CRC32;
- std::copy(std::begin(FileName), std::end(FileName), File);
+ Elf_Word *CRC =
+ reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
+ *CRC = Sec.CRC32;
+ std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File);
+}
+
+void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
+ Visitor.visit(*this);
}
// Returns true IFF a section is wholly inside the range of a segment
@@ -427,14 +456,12 @@ static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
return A->Index < B->Index;
}
-template <class ELFT>
-void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
+template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
uint32_t Index = 0;
for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
(size_t)Phdr.p_filesz};
- Segments.emplace_back(llvm::make_unique<Segment>(Data));
- Segment &Seg = *Segments.back();
+ Segment &Seg = Obj.addSegment(Data);
Seg.Type = Phdr.p_type;
Seg.Flags = Phdr.p_flags;
Seg.OriginalOffset = Phdr.p_offset;
@@ -445,29 +472,29 @@ void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
Seg.MemSize = Phdr.p_memsz;
Seg.Align = Phdr.p_align;
Seg.Index = Index++;
- for (auto &Section : Sections) {
- if (sectionWithinSegment(*Section, Seg)) {
- Seg.addSection(&*Section);
- if (!Section->ParentSegment ||
- Section->ParentSegment->Offset > Seg.Offset) {
- Section->ParentSegment = &Seg;
+ for (auto &Section : Obj.sections()) {
+ if (sectionWithinSegment(Section, Seg)) {
+ Seg.addSection(&Section);
+ if (!Section.ParentSegment ||
+ Section.ParentSegment->Offset > Seg.Offset) {
+ Section.ParentSegment = &Seg;
}
}
}
}
// Now we do an O(n^2) loop through the segments in order to match up
// segments.
- for (auto &Child : Segments) {
- for (auto &Parent : Segments) {
+ for (auto &Child : Obj.segments()) {
+ for (auto &Parent : Obj.segments()) {
// Every segment will overlap with itself but we don't want a segment to
// be it's own parent so we avoid that situation.
- if (&Child != &Parent && segmentOverlapsSegment(*Child, *Parent)) {
+ if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
// We want a canonical "most parental" segment but this requires
// inspecting the ParentSegment.
- if (compareSegmentsByOffset(Parent.get(), Child.get()))
- if (Child->ParentSegment == nullptr ||
- compareSegmentsByOffset(Parent.get(), Child->ParentSegment)) {
- Child->ParentSegment = Parent.get();
+ if (compareSegmentsByOffset(&Parent, &Child))
+ if (Child.ParentSegment == nullptr ||
+ compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
+ Child.ParentSegment = &Parent;
}
}
}
@@ -475,9 +502,7 @@ void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) {
}
template <class ELFT>
-void Object<ELFT>::initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
- SymbolTableSection *SymTab,
- SectionTableRef SecTable) {
+void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
@@ -486,14 +511,14 @@ void Object<ELFT>::initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
StringRef Name = unwrapOrError(Sym.getName(StrTabData));
if (Sym.st_shndx >= SHN_LORESERVE) {
- if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) {
+ if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
error(
"Symbol '" + Name +
"' has unsupported value greater than or equal to SHN_LORESERVE: " +
Twine(Sym.st_shndx));
}
} else if (Sym.st_shndx != SHN_UNDEF) {
- DefSection = SecTable.getSection(
+ DefSection = Obj.sections().getSection(
Sym.st_shndx,
"Symbol '" + Name + "' is defined in invalid section with index " +
Twine(Sym.st_shndx));
@@ -512,9 +537,9 @@ static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
ToSet = Rela.r_addend;
}
-template <class ELFT, class T>
-void initRelocations(RelocationSection<ELFT> *Relocs,
- SymbolTableSection *SymbolTable, T RelRange) {
+template <class T>
+void initRelocations(RelocationSection *Relocs, SymbolTableSection *SymbolTable,
+ T RelRange) {
for (const auto &Rel : RelRange) {
Relocation ToAdd;
ToAdd.Offset = Rel.r_offset;
@@ -540,147 +565,190 @@ T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
}
template <class ELFT>
-std::unique_ptr<SectionBase>
-Object<ELFT>::makeSection(const object::ELFFile<ELFT> &ElfFile,
- const Elf_Shdr &Shdr) {
+SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
ArrayRef<uint8_t> Data;
switch (Shdr.sh_type) {
case SHT_REL:
case SHT_RELA:
if (Shdr.sh_flags & SHF_ALLOC) {
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
- return llvm::make_unique<DynamicRelocationSection>(Data);
+ return Obj.addSection<DynamicRelocationSection>(Data);
}
- return llvm::make_unique<RelocationSection<ELFT>>();
+ return Obj.addSection<RelocationSection>();
case SHT_STRTAB:
// If a string table is allocated we don't want to mess with it. That would
// mean altering the memory image. There are no special link types or
// anything so we can just use a Section.
if (Shdr.sh_flags & SHF_ALLOC) {
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
- return llvm::make_unique<Section>(Data);
+ return Obj.addSection<Section>(Data);
}
- return llvm::make_unique<StringTableSection>();
+ return Obj.addSection<StringTableSection>();
case SHT_HASH:
case SHT_GNU_HASH:
// Hash tables should refer to SHT_DYNSYM which we're not going to change.
// Because of this we don't need to mess with the hash tables either.
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
- return llvm::make_unique<Section>(Data);
+ return Obj.addSection<Section>(Data);
case SHT_DYNSYM:
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
- return llvm::make_unique<DynamicSymbolTableSection>(Data);
+ return Obj.addSection<DynamicSymbolTableSection>(Data);
case SHT_DYNAMIC:
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
- return llvm::make_unique<DynamicSection>(Data);
+ return Obj.addSection<DynamicSection>(Data);
case SHT_SYMTAB: {
- auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
- SymbolTable = SymTab.get();
- return std::move(SymTab);
+ auto &SymTab = Obj.addSection<SymbolTableSection>();
+ Obj.SymbolTable = &SymTab;
+ return SymTab;
}
case SHT_NOBITS:
- return llvm::make_unique<Section>(Data);
+ return Obj.addSection<Section>(Data);
default:
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
- return llvm::make_unique<Section>(Data);
+ return Obj.addSection<Section>(Data);
}
}
-template <class ELFT>
-SectionTableRef Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
+template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
uint32_t Index = 0;
for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
if (Index == 0) {
++Index;
continue;
}
- SecPtr Sec = makeSection(ElfFile, Shdr);
- Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
- Sec->Type = Shdr.sh_type;
- Sec->Flags = Shdr.sh_flags;
- Sec->Addr = Shdr.sh_addr;
- Sec->Offset = Shdr.sh_offset;
- Sec->OriginalOffset = Shdr.sh_offset;
- Sec->Size = Shdr.sh_size;
- Sec->Link = Shdr.sh_link;
- Sec->Info = Shdr.sh_info;
- Sec->Align = Shdr.sh_addralign;
- Sec->EntrySize = Shdr.sh_entsize;
- Sec->Index = Index++;
- Sections.push_back(std::move(Sec));
+ auto &Sec = makeSection(Shdr);
+ Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
+ Sec.Type = Shdr.sh_type;
+ Sec.Flags = Shdr.sh_flags;
+ Sec.Addr = Shdr.sh_addr;
+ Sec.Offset = Shdr.sh_offset;
+ Sec.OriginalOffset = Shdr.sh_offset;
+ Sec.Size = Shdr.sh_size;
+ Sec.Link = Shdr.sh_link;
+ Sec.Info = Shdr.sh_info;
+ Sec.Align = Shdr.sh_addralign;
+ Sec.EntrySize = Shdr.sh_entsize;
+ Sec.Index = Index++;
}
- SectionTableRef SecTable(Sections);
-
// Now that all of the sections have been added we can fill out some extra
// details about symbol tables. We need the symbol table filled out before
// any relocations.
- if (SymbolTable) {
- SymbolTable->initialize(SecTable);
- initSymbolTable(ElfFile, SymbolTable, SecTable);
+ if (Obj.SymbolTable) {
+ Obj.SymbolTable->initialize(Obj.sections());
+ initSymbolTable(Obj.SymbolTable);
}
// Now that all sections and symbols have been added we can add
// relocations that reference symbols and set the link and info fields for
// relocation sections.
- for (auto &Section : Sections) {
- if (Section.get() == SymbolTable)
+ for (auto &Section : Obj.sections()) {
+ if (&Section == Obj.SymbolTable)
continue;
- Section->initialize(SecTable);
- if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) {
+ Section.initialize(Obj.sections());
+ if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
if (RelSec->Type == SHT_REL)
- initRelocations(RelSec, SymbolTable, unwrapOrError(ElfFile.rels(Shdr)));
+ initRelocations(RelSec, Obj.SymbolTable,
+ unwrapOrError(ElfFile.rels(Shdr)));
else
- initRelocations(RelSec, SymbolTable,
+ initRelocations(RelSec, Obj.SymbolTable,
unwrapOrError(ElfFile.relas(Shdr)));
}
}
-
- return SecTable;
}
-template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
- const auto &ElfFile = *Obj.getELFFile();
+template <class ELFT> void ELFBuilder<ELFT>::build() {
const auto &Ehdr = *ElfFile.getHeader();
- std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident);
- Type = Ehdr.e_type;
- Machine = Ehdr.e_machine;
- Version = Ehdr.e_version;
- Entry = Ehdr.e_entry;
- Flags = Ehdr.e_flags;
-
- SectionTableRef SecTable = readSectionHeaders(ElfFile);
- readProgramHeaders(ElfFile);
-
- SectionNames = SecTable.getSectionOfType<StringTableSection>(
- Ehdr.e_shstrndx,
- "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
- " is invalid",
- "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
- " is not a string table");
+ std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Obj.Ident);
+ Obj.Type = Ehdr.e_type;
+ Obj.Machine = Ehdr.e_machine;
+ Obj.Version = Ehdr.e_version;
+ Obj.Entry = Ehdr.e_entry;
+ Obj.Flags = Ehdr.e_flags;
+
+ readSectionHeaders();
+ readProgramHeaders();
+
+ Obj.SectionNames =
+ Obj.sections().template getSectionOfType<StringTableSection>(
+ Ehdr.e_shstrndx,
+ "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
+ " in elf header " + " is invalid",
+ "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
+ " in elf header " + " is not a string table");
}
-template <class ELFT>
-void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const {
- uint8_t *Buf = Out.getBufferStart();
+// A generic size function which computes sizes of any random access range.
+template <class R> size_t size(R &&Range) {
+ return static_cast<size_t>(std::end(Range) - std::begin(Range));
+}
+
+Writer::~Writer() {}
+
+Reader::~Reader() {}
+
+ELFReader::ELFReader(StringRef File) {
+ auto BinaryOrErr = createBinary(File);
+ if (!BinaryOrErr)
+ reportError(File, BinaryOrErr.takeError());
+ auto Bin = std::move(BinaryOrErr.get());
+ std::tie(Binary, Data) = Bin.takeBinary();
+}
+
+ElfType ELFReader::getElfType() const {
+ if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Binary.get()))
+ return ELFT_ELF32LE;
+ if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Binary.get()))
+ return ELFT_ELF64LE;
+ if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Binary.get()))
+ return ELFT_ELF32BE;
+ if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Binary.get()))
+ return ELFT_ELF64BE;
+ llvm_unreachable("Invalid ELFType");
+}
+
+std::unique_ptr<Object> ELFReader::create() const {
+ auto Obj = llvm::make_unique<Object>(Data);
+ if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Binary.get())) {
+ ELFBuilder<ELF32LE> Builder(*o, *Obj);
+ Builder.build();
+ return Obj;
+ } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Binary.get())) {
+ ELFBuilder<ELF64LE> Builder(*o, *Obj);
+ Builder.build();
+ return Obj;
+ } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Binary.get())) {
+ ELFBuilder<ELF32BE> Builder(*o, *Obj);
+ Builder.build();
+ return Obj;
+ } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Binary.get())) {
+ ELFBuilder<ELF64BE> Builder(*o, *Obj);
+ Builder.build();
+ return Obj;
+ }
+ error("Invalid file type");
+}
+
+template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
+ uint8_t *Buf = BufPtr->getBufferStart();
Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf);
- std::copy(Ident, Ident + 16, Ehdr.e_ident);
- Ehdr.e_type = Type;
- Ehdr.e_machine = Machine;
- Ehdr.e_version = Version;
- Ehdr.e_entry = Entry;
+ std::copy(Obj.Ident, Obj.Ident + 16, Ehdr.e_ident);
+ Ehdr.e_type = Obj.Type;
+ Ehdr.e_machine = Obj.Machine;
+ Ehdr.e_version = Obj.Version;
+ Ehdr.e_entry = Obj.Entry;
Ehdr.e_phoff = sizeof(Elf_Ehdr);
- Ehdr.e_flags = Flags;
+ Ehdr.e_flags = Obj.Flags;
Ehdr.e_ehsize = sizeof(Elf_Ehdr);
Ehdr.e_phentsize = sizeof(Elf_Phdr);
- Ehdr.e_phnum = Segments.size();
+ Ehdr.e_phnum = size(Obj.segments());
Ehdr.e_shentsize = sizeof(Elf_Shdr);
if (WriteSectionHeaders) {
- Ehdr.e_shoff = SHOffset;
- Ehdr.e_shnum = Sections.size() + 1;
- Ehdr.e_shstrndx = SectionNames->Index;
+ Ehdr.e_shoff = Obj.SHOffset;
+ Ehdr.e_shnum = size(Obj.sections()) + 1;
+ Ehdr.e_shstrndx = Obj.SectionNames->Index;
} else {
Ehdr.e_shoff = 0;
Ehdr.e_shnum = 0;
@@ -688,15 +756,13 @@ void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const {
}
}
-template <class ELFT>
-void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const {
- for (auto &Phdr : Segments)
- Phdr->template writeHeader<ELFT>(Out);
+template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
+ for (auto &Seg : Obj.segments())
+ writePhdr(Seg);
}
-template <class ELFT>
-void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const {
- uint8_t *Buf = Out.getBufferStart() + SHOffset;
+template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
+ uint8_t *Buf = BufPtr->getBufferStart() + Obj.SHOffset;
// This reference serves to write the dummy section header at the begining
// of the file. It is not used for anything else
Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf);
@@ -711,19 +777,16 @@ void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const {
Shdr.sh_addralign = 0;
Shdr.sh_entsize = 0;
- for (auto &Section : Sections)
- Section->template writeHeader<ELFT>(Out);
+ for (auto &Sec : Obj.sections())
+ writeShdr(Sec);
}
-template <class ELFT>
-void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
- for (auto &Section : Sections)
- Section->writeSection(Out);
+template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
+ for (auto &Sec : Obj.sections())
+ Sec.accept(*SecWriter);
}
-template <class ELFT>
-void Object<ELFT>::removeSections(
- std::function<bool(const SectionBase &)> ToRemove) {
+void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
auto Iter = std::stable_partition(
std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
@@ -737,10 +800,7 @@ void Object<ELFT>::removeSections(
});
if (SymbolTable != nullptr && ToRemove(*SymbolTable))
SymbolTable = nullptr;
- if (ToRemove(*SectionNames)) {
- if (WriteSectionHeaders)
- error("Cannot remove " + SectionNames->Name +
- " because it is the section header string table.");
+ if (SectionNames != nullptr && ToRemove(*SectionNames)) {
SectionNames = nullptr;
}
// Now make sure there are no remaining references to the sections that will
@@ -756,18 +816,7 @@ void Object<ELFT>::removeSections(
Sections.erase(Iter, std::end(Sections));
}
-template <class ELFT>
-void Object<ELFT>::addSection(StringRef SecName, ArrayRef<uint8_t> Data) {
- auto Sec = llvm::make_unique<OwnedDataSection>(SecName, Data);
- Sec->OriginalOffset = ~0ULL;
- Sections.push_back(std::move(Sec));
-}
-
-template <class ELFT> void Object<ELFT>::addGnuDebugLink(StringRef File) {
- Sections.emplace_back(llvm::make_unique<GnuDebugLinkSection<ELFT>>(File));
-}
-
-template <class ELFT> void ELFObject<ELFT>::sortSections() {
+void Object::sortSections() {
// Put all sections in offset order. Maintain the ordering as closely as
// possible while meeting that demand however.
auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
@@ -832,8 +881,8 @@ static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
// does not have a ParentSegment. It returns either the offset given if all
// sections had a ParentSegment or an offset one past the last section if there
// was a section that didn't have a ParentSegment.
-template <class SecPtr>
-static uint64_t LayoutSections(std::vector<SecPtr> &Sections, uint64_t Offset) {
+template <class Range>
+static uint64_t LayoutSections(Range Sections, uint64_t Offset) {
// Now the offset of every segment has been set we can assign the offsets
// of each section. For sections that are covered by a segment we should use
// the segment's original offset and the section's original offset to compute
@@ -842,28 +891,28 @@ static uint64_t LayoutSections(std::vector<SecPtr> &Sections, uint64_t Offset) {
// covered by segments we can just bump Offset to the next valid location.
uint32_t Index = 1;
for (auto &Section : Sections) {
- Section->Index = Index++;
- if (Section->ParentSegment != nullptr) {
- auto Segment = Section->ParentSegment;
- Section->Offset =
- Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset);
+ Section.Index = Index++;
+ if (Section.ParentSegment != nullptr) {
+ auto Segment = *Section.ParentSegment;
+ Section.Offset =
+ Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
} else {
- Offset = alignTo(Offset, Section->Align == 0 ? 1 : Section->Align);
- Section->Offset = Offset;
- if (Section->Type != SHT_NOBITS)
- Offset += Section->Size;
+ Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
+ Section.Offset = Offset;
+ if (Section.Type != SHT_NOBITS)
+ Offset += Section.Size;
}
}
return Offset;
}
-template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
+template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
// We need a temporary list of segments that has a special order to it
// so that we know that anytime ->ParentSegment is set that segment has
// already had its offset properly set.
std::vector<Segment *> OrderedSegments;
- for (auto &Segment : this->Segments)
- OrderedSegments.push_back(Segment.get());
+ for (auto &Segment : Obj.segments())
+ OrderedSegments.push_back(&Segment);
OrderSegments(OrderedSegments);
// The size of ELF + program headers will not change so it is ok to assume
// that the first offset of the first segment is a good place to start
@@ -876,72 +925,88 @@ template <class ELFT> void ELFObject<ELFT>::assignOffsets() {
Offset = sizeof(Elf_Ehdr);
}
Offset = LayoutSegments(OrderedSegments, Offset);
- Offset = LayoutSections(this->Sections, Offset);
+ Offset = LayoutSections(Obj.sections(), Offset);
// If we need to write the section header table out then we need to align the
// Offset so that SHOffset is valid.
- if (this->WriteSectionHeaders)
+ if (WriteSectionHeaders)
Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
- this->SHOffset = Offset;
+ Obj.SHOffset = Offset;
}
-template <class ELFT> size_t ELFObject<ELFT>::totalSize() const {
+template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
// We already have the section header offset so we can calculate the total
// size by just adding up the size of each section header.
- auto NullSectionSize = this->WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
- return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) +
+ auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
+ return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
NullSectionSize;
}
-template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const {
- this->writeHeader(Out);
- this->writeProgramHeaders(Out);
- this->writeSectionData(Out);
- if (this->WriteSectionHeaders)
- this->writeSectionHeaders(Out);
+template <class ELFT> void ELFWriter<ELFT>::write() {
+ writeEhdr();
+ writePhdrs();
+ writeSectionData();
+ if (WriteSectionHeaders)
+ writeShdrs();
+ if (auto E = BufPtr->commit())
+ reportError(File, errorToErrorCode(std::move(E)));
}
-template <class ELFT> void ELFObject<ELFT>::finalize() {
+void Writer::createBuffer(uint64_t Size) {
+ auto BufferOrErr =
+ FileOutputBuffer::create(File, Size, FileOutputBuffer::F_executable);
+ handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &) {
+ error("failed to open " + File);
+ });
+ BufPtr = std::move(*BufferOrErr);
+}
+
+template <class ELFT> void ELFWriter<ELFT>::finalize() {
+ // It could happen that SectionNames has been removed and yet the user wants
+ // a section header table output. We need to throw an error if a user tries
+ // to do that.
+ if (Obj.SectionNames == nullptr && WriteSectionHeaders)
+ error("Cannot write section header table because section header string "
+ "table was removed.");
+
// Make sure we add the names of all the sections.
- if (this->SectionNames != nullptr)
- for (const auto &Section : this->Sections) {
- this->SectionNames->addString(Section->Name);
+ if (Obj.SectionNames != nullptr)
+ for (const auto &Section : Obj.sections()) {
+ Obj.SectionNames->addString(Section.Name);
}
// Make sure we add the names of all the symbols.
- if (this->SymbolTable != nullptr)
- this->SymbolTable->addSymbolNames();
+ if (Obj.SymbolTable != nullptr)
+ Obj.SymbolTable->addSymbolNames();
- sortSections();
+ Obj.sortSections();
assignOffsets();
// Finalize SectionNames first so that we can assign name indexes.
- if (this->SectionNames != nullptr)
- this->SectionNames->finalize();
+ if (Obj.SectionNames != nullptr)
+ Obj.SectionNames->finalize();
// Finally now that all offsets and indexes have been set we can finalize any
// remaining issues.
- uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr);
- for (auto &Section : this->Sections) {
- Section->HeaderOffset = Offset;
+ uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
+ for (auto &Section : Obj.sections()) {
+ Section.HeaderOffset = Offset;
Offset += sizeof(Elf_Shdr);
- if (this->WriteSectionHeaders)
- Section->NameIndex = this->SectionNames->findIndex(Section->Name);
- Section->finalize();
+ if (WriteSectionHeaders)
+ Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
+ Section.finalize();
}
-}
-template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const {
- return TotalSize;
+ createBuffer(totalSize());
+ SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(*BufPtr);
}
-template <class ELFT>
-void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const {
- for (auto &Section : this->Sections) {
- if ((Section->Flags & SHF_ALLOC) == 0)
+void BinaryWriter::write() {
+ for (auto &Section : Obj.sections()) {
+ if ((Section.Flags & SHF_ALLOC) == 0)
continue;
- Section->writeSection(Out);
+ Section.accept(*SecWriter);
}
}
-template <class ELFT> void BinaryObject<ELFT>::finalize() {
+void BinaryWriter::finalize() {
// TODO: Create a filter range to construct OrderedSegments from so that this
// code can be deduped with assignOffsets above. This should also solve the
// todo below for LayoutSections.
@@ -950,10 +1015,9 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() {
// already had it's offset properly set. We only want to consider the segments
// that will affect layout of allocated sections so we only add those.
std::vector<Segment *> OrderedSegments;
- for (auto &Section : this->Sections) {
- if ((Section->Flags & SHF_ALLOC) != 0 &&
- Section->ParentSegment != nullptr) {
- OrderedSegments.push_back(Section->ParentSegment);
+ for (auto &Section : Obj.sections()) {
+ if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
+ OrderedSegments.push_back(Section.ParentSegment);
}
}
@@ -1004,12 +1068,12 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() {
// not hold. Then pass such a range to LayoutSections instead of constructing
// AllocatedSections here.
std::vector<SectionBase *> AllocatedSections;
- for (auto &Section : this->Sections) {
- if ((Section->Flags & SHF_ALLOC) == 0)
+ for (auto &Section : Obj.sections()) {
+ if ((Section.Flags & SHF_ALLOC) == 0)
continue;
- AllocatedSections.push_back(Section.get());
+ AllocatedSections.push_back(&Section);
}
- LayoutSections(AllocatedSections, Offset);
+ LayoutSections(make_pointee_range(AllocatedSections), Offset);
// Now that every section has been laid out we just need to compute the total
// file size. This might not be the same as the offset returned by
@@ -1020,23 +1084,21 @@ template <class ELFT> void BinaryObject<ELFT>::finalize() {
if (Section->Type != SHT_NOBITS)
TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
}
+
+ createBuffer(TotalSize);
+ SecWriter = llvm::make_unique<BinarySectionWriter>(*BufPtr);
}
namespace llvm {
-template class Object<ELF64LE>;
-template class Object<ELF64BE>;
-template class Object<ELF32LE>;
-template class Object<ELF32BE>;
-
-template class ELFObject<ELF64LE>;
-template class ELFObject<ELF64BE>;
-template class ELFObject<ELF32LE>;
-template class ELFObject<ELF32BE>;
+template class ELFBuilder<ELF64LE>;
+template class ELFBuilder<ELF64BE>;
+template class ELFBuilder<ELF32LE>;
+template class ELFBuilder<ELF32BE>;
-template class BinaryObject<ELF64LE>;
-template class BinaryObject<ELF64BE>;
-template class BinaryObject<ELF32LE>;
-template class BinaryObject<ELF32BE>;
+template class ELFWriter<ELF64LE>;
+template class ELFWriter<ELF64BE>;
+template class ELFWriter<ELF32LE>;
+template class ELFWriter<ELF32BE>;
} // end namespace llvm
OpenPOWER on IntegriCloud