2017-08-01 02:33:58 +02:00
|
|
|
//===- Object.h -------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
|
|
|
|
#define LLVM_TOOLS_OBJCOPY_OBJECT_H
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2017-08-01 02:33:58 +02:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2018-01-25 23:46:17 +01:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2018-01-25 23:15:14 +01:00
|
|
|
#include "llvm/Support/JamCRC.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
2017-08-01 02:33:58 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
2017-11-01 22:16:06 +01:00
|
|
|
#include <vector>
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2017-09-25 22:37:28 +02:00
|
|
|
class SectionBase;
|
2018-01-25 23:46:17 +01:00
|
|
|
class Section;
|
|
|
|
class OwnedDataSection;
|
|
|
|
class StringTableSection;
|
|
|
|
class SymbolTableSection;
|
|
|
|
class RelocationSection;
|
|
|
|
class DynamicRelocationSection;
|
|
|
|
class GnuDebugLinkSection;
|
2017-11-01 22:16:06 +01:00
|
|
|
class Segment;
|
2018-01-25 23:46:17 +01:00
|
|
|
class Object;
|
2017-09-25 22:37:28 +02:00
|
|
|
|
|
|
|
class SectionTableRef {
|
|
|
|
private:
|
2018-01-25 23:46:17 +01:00
|
|
|
MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
|
2017-09-25 22:37:28 +02:00
|
|
|
|
|
|
|
public:
|
2018-01-25 23:46:17 +01:00
|
|
|
using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
|
|
|
|
|
|
|
|
SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
|
2017-09-25 22:37:28 +02:00
|
|
|
: Sections(Secs) {}
|
|
|
|
SectionTableRef(const SectionTableRef &) = default;
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
iterator begin() { return iterator(Sections.data()); }
|
|
|
|
iterator end() { return iterator(Sections.data() + Sections.size()); }
|
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
SectionBase *getSection(uint16_t Index, Twine ErrMsg);
|
2017-09-25 22:37:28 +02:00
|
|
|
|
|
|
|
template <class T>
|
2017-11-01 22:16:06 +01:00
|
|
|
T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
|
2017-09-25 22:37:28 +02:00
|
|
|
};
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
|
|
|
|
|
|
|
|
class SectionVisitor {
|
|
|
|
public:
|
|
|
|
virtual ~SectionVisitor();
|
|
|
|
|
|
|
|
virtual void visit(const Section &Sec) = 0;
|
|
|
|
virtual void visit(const OwnedDataSection &Sec) = 0;
|
|
|
|
virtual void visit(const StringTableSection &Sec) = 0;
|
|
|
|
virtual void visit(const SymbolTableSection &Sec) = 0;
|
|
|
|
virtual void visit(const RelocationSection &Sec) = 0;
|
|
|
|
virtual void visit(const DynamicRelocationSection &Sec) = 0;
|
|
|
|
virtual void visit(const GnuDebugLinkSection &Sec) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SectionWriter : public SectionVisitor {
|
|
|
|
protected:
|
|
|
|
FileOutputBuffer &Out;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~SectionWriter(){};
|
|
|
|
|
|
|
|
void visit(const Section &Sec) override;
|
|
|
|
void visit(const OwnedDataSection &Sec) override;
|
|
|
|
void visit(const StringTableSection &Sec) override;
|
|
|
|
void visit(const DynamicRelocationSection &Sec) override;
|
|
|
|
virtual void visit(const SymbolTableSection &Sec) override = 0;
|
|
|
|
virtual void visit(const RelocationSection &Sec) override = 0;
|
|
|
|
virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
|
|
|
|
|
|
|
|
SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT> class ELFSectionWriter : public SectionWriter {
|
|
|
|
private:
|
|
|
|
using Elf_Word = typename ELFT::Word;
|
|
|
|
using Elf_Rel = typename ELFT::Rel;
|
|
|
|
using Elf_Rela = typename ELFT::Rela;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~ELFSectionWriter() {}
|
|
|
|
void visit(const SymbolTableSection &Sec) override;
|
|
|
|
void visit(const RelocationSection &Sec) override;
|
|
|
|
void visit(const GnuDebugLinkSection &Sec) override;
|
|
|
|
|
|
|
|
ELFSectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAKE_SEC_WRITER_FRIEND \
|
|
|
|
friend class SectionWriter; \
|
|
|
|
template <class ELFT> friend class ELFSectionWriter;
|
|
|
|
|
|
|
|
class BinarySectionWriter : public SectionWriter {
|
|
|
|
public:
|
|
|
|
virtual ~BinarySectionWriter() {}
|
|
|
|
|
|
|
|
void visit(const SymbolTableSection &Sec) override;
|
|
|
|
void visit(const RelocationSection &Sec) override;
|
|
|
|
void visit(const GnuDebugLinkSection &Sec) override;
|
|
|
|
BinarySectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Writer {
|
|
|
|
protected:
|
|
|
|
StringRef File;
|
|
|
|
Object &Obj;
|
|
|
|
std::unique_ptr<FileOutputBuffer> BufPtr;
|
|
|
|
|
|
|
|
void createBuffer(uint64_t Size);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~Writer();
|
|
|
|
|
|
|
|
virtual void finalize() = 0;
|
|
|
|
virtual void write() = 0;
|
|
|
|
|
|
|
|
Writer(StringRef File, Object &Obj) : File(File), Obj(Obj) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ELFT> class ELFWriter : public Writer {
|
|
|
|
private:
|
|
|
|
using Elf_Shdr = typename ELFT::Shdr;
|
|
|
|
using Elf_Phdr = typename ELFT::Phdr;
|
|
|
|
using Elf_Ehdr = typename ELFT::Ehdr;
|
|
|
|
|
|
|
|
void writeEhdr();
|
|
|
|
void writePhdr(const Segment &Seg);
|
|
|
|
void writeShdr(const SectionBase &Sec);
|
|
|
|
|
|
|
|
void writePhdrs();
|
|
|
|
void writeShdrs();
|
|
|
|
void writeSectionData();
|
|
|
|
|
|
|
|
void assignOffsets();
|
|
|
|
|
|
|
|
std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
|
|
|
|
|
|
|
|
size_t totalSize() const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~ELFWriter() {}
|
|
|
|
bool WriteSectionHeaders = true;
|
|
|
|
|
|
|
|
void finalize() override;
|
|
|
|
void write() override;
|
|
|
|
ELFWriter(StringRef File, Object &Obj, bool WSH)
|
|
|
|
: Writer(File, Obj), WriteSectionHeaders(WSH) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BinaryWriter : public Writer {
|
|
|
|
private:
|
|
|
|
std::unique_ptr<BinarySectionWriter> SecWriter;
|
|
|
|
|
|
|
|
uint64_t TotalSize;
|
|
|
|
|
|
|
|
public:
|
|
|
|
~BinaryWriter() {}
|
|
|
|
void finalize() override;
|
|
|
|
void write() override;
|
|
|
|
BinaryWriter(StringRef File, Object &Obj) : Writer(File, Obj) {}
|
|
|
|
};
|
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
class SectionBase {
|
|
|
|
public:
|
2017-11-01 22:16:06 +01:00
|
|
|
StringRef Name;
|
2017-08-01 02:33:58 +02:00
|
|
|
Segment *ParentSegment = nullptr;
|
|
|
|
uint64_t HeaderOffset;
|
|
|
|
uint64_t OriginalOffset;
|
|
|
|
uint32_t Index;
|
|
|
|
|
|
|
|
uint64_t Addr = 0;
|
|
|
|
uint64_t Align = 1;
|
|
|
|
uint32_t EntrySize = 0;
|
|
|
|
uint64_t Flags = 0;
|
|
|
|
uint64_t Info = 0;
|
2017-11-01 22:16:06 +01:00
|
|
|
uint64_t Link = ELF::SHN_UNDEF;
|
2017-08-01 02:33:58 +02:00
|
|
|
uint64_t NameIndex = 0;
|
|
|
|
uint64_t Offset = 0;
|
|
|
|
uint64_t Size = 0;
|
2017-11-01 22:16:06 +01:00
|
|
|
uint64_t Type = ELF::SHT_NULL;
|
|
|
|
|
|
|
|
virtual ~SectionBase() = default;
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2017-09-25 22:37:28 +02:00
|
|
|
virtual void initialize(SectionTableRef SecTable);
|
2017-08-01 02:33:58 +02:00
|
|
|
virtual void finalize();
|
2017-10-10 20:47:09 +02:00
|
|
|
virtual void removeSectionReferences(const SectionBase *Sec);
|
2018-01-25 23:46:17 +01:00
|
|
|
virtual void accept(SectionVisitor &Visitor) const = 0;
|
2017-08-01 02:33:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Segment {
|
|
|
|
private:
|
|
|
|
struct SectionCompare {
|
|
|
|
bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
|
|
|
|
// Some sections might have the same address if one of them is empty. To
|
|
|
|
// fix this we can use the lexicographic ordering on ->Addr and the
|
|
|
|
// address of the actully stored section.
|
|
|
|
if (Lhs->OriginalOffset == Rhs->OriginalOffset)
|
|
|
|
return Lhs < Rhs;
|
|
|
|
return Lhs->OriginalOffset < Rhs->OriginalOffset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::set<const SectionBase *, SectionCompare> Sections;
|
2017-11-01 22:16:06 +01:00
|
|
|
ArrayRef<uint8_t> Contents;
|
2017-08-01 02:33:58 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
uint64_t Align;
|
|
|
|
uint64_t FileSize;
|
|
|
|
uint32_t Flags;
|
|
|
|
uint32_t Index;
|
|
|
|
uint64_t MemSize;
|
|
|
|
uint64_t Offset;
|
|
|
|
uint64_t PAddr;
|
|
|
|
uint64_t Type;
|
|
|
|
uint64_t VAddr;
|
|
|
|
|
2017-08-26 03:32:20 +02:00
|
|
|
uint64_t OriginalOffset;
|
2017-09-19 23:37:35 +02:00
|
|
|
Segment *ParentSegment = nullptr;
|
2017-08-26 03:32:20 +02:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
|
2018-02-15 00:31:33 +01:00
|
|
|
Segment() {}
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
const SectionBase *firstSection() const {
|
|
|
|
if (!Sections.empty())
|
|
|
|
return *Sections.begin();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2017-10-10 20:47:09 +02:00
|
|
|
void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
|
|
|
|
void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
|
2017-08-01 02:33:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Section : public SectionBase {
|
2018-01-25 23:46:17 +01:00
|
|
|
MAKE_SEC_WRITER_FRIEND
|
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
private:
|
2017-11-01 22:16:06 +01:00
|
|
|
ArrayRef<uint8_t> Contents;
|
2017-08-01 02:33:58 +02:00
|
|
|
|
|
|
|
public:
|
2017-11-01 22:16:06 +01:00
|
|
|
Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
void accept(SectionVisitor &Visitor) const override;
|
2017-08-01 02:33:58 +02:00
|
|
|
};
|
|
|
|
|
2017-12-19 01:47:30 +01:00
|
|
|
class OwnedDataSection : public SectionBase {
|
2018-01-25 23:46:17 +01:00
|
|
|
MAKE_SEC_WRITER_FRIEND
|
|
|
|
|
2017-12-19 01:47:30 +01:00
|
|
|
private:
|
|
|
|
std::vector<uint8_t> Data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
|
|
|
|
: Data(std::begin(Data), std::end(Data)) {
|
|
|
|
Name = SecName;
|
|
|
|
Type = ELF::SHT_PROGBITS;
|
|
|
|
Size = Data.size();
|
2018-01-25 23:46:17 +01:00
|
|
|
OriginalOffset = std::numeric_limits<uint64_t>::max();
|
2017-12-19 01:47:30 +01:00
|
|
|
}
|
2018-01-25 23:46:17 +01:00
|
|
|
|
|
|
|
void accept(SectionVisitor &Sec) const override;
|
2017-12-19 01:47:30 +01:00
|
|
|
};
|
|
|
|
|
2017-10-10 23:28:22 +02:00
|
|
|
// There are two types of string tables that can exist, dynamic and not dynamic.
|
|
|
|
// In the dynamic case the string table is allocated. Changing a dynamic string
|
|
|
|
// table would mean altering virtual addresses and thus the memory image. So
|
|
|
|
// dynamic string tables should not have an interface to modify them or
|
|
|
|
// reconstruct them. This type lets us reconstruct a string table. To avoid
|
|
|
|
// this class being used for dynamic string tables (which has happened) the
|
|
|
|
// classof method checks that the particular instance is not allocated. This
|
|
|
|
// then agrees with the makeSection method used to construct most sections.
|
2017-08-01 02:33:58 +02:00
|
|
|
class StringTableSection : public SectionBase {
|
2018-01-25 23:46:17 +01:00
|
|
|
MAKE_SEC_WRITER_FRIEND
|
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
private:
|
2017-11-01 22:16:06 +01:00
|
|
|
StringTableBuilder StrTabBuilder;
|
2017-08-01 02:33:58 +02:00
|
|
|
|
|
|
|
public:
|
2017-11-01 22:16:06 +01:00
|
|
|
StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
|
|
|
|
Type = ELF::SHT_STRTAB;
|
2017-08-01 02:33:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
void addString(StringRef Name);
|
|
|
|
uint32_t findIndex(StringRef Name) const;
|
2017-08-01 02:33:58 +02:00
|
|
|
void finalize() override;
|
2018-01-25 23:46:17 +01:00
|
|
|
void accept(SectionVisitor &Visitor) const override;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2017-08-01 02:33:58 +02:00
|
|
|
static bool classof(const SectionBase *S) {
|
2017-11-01 22:16:06 +01:00
|
|
|
if (S->Flags & ELF::SHF_ALLOC)
|
2017-10-10 23:28:22 +02:00
|
|
|
return false;
|
2017-11-01 22:16:06 +01:00
|
|
|
return S->Type == ELF::SHT_STRTAB;
|
2017-08-01 02:33:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-08 01:02:50 +02:00
|
|
|
// Symbols have a st_shndx field that normally stores an index but occasionally
|
|
|
|
// stores a different special value. This enum keeps track of what the st_shndx
|
|
|
|
// field means. Most of the values are just copies of the special SHN_* values.
|
|
|
|
// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
|
|
|
|
enum SymbolShndxType {
|
|
|
|
SYMBOL_SIMPLE_INDEX = 0,
|
2017-11-01 22:16:06 +01:00
|
|
|
SYMBOL_ABS = ELF::SHN_ABS,
|
|
|
|
SYMBOL_COMMON = ELF::SHN_COMMON,
|
|
|
|
SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
|
|
|
|
SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
|
|
|
|
SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
|
|
|
|
SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
|
2017-09-08 01:02:50 +02:00
|
|
|
};
|
|
|
|
|
2017-08-29 04:12:03 +02:00
|
|
|
struct Symbol {
|
|
|
|
uint8_t Binding;
|
2017-09-27 02:44:00 +02:00
|
|
|
SectionBase *DefinedIn = nullptr;
|
2017-09-08 01:02:50 +02:00
|
|
|
SymbolShndxType ShndxType;
|
2017-08-29 04:12:03 +02:00
|
|
|
uint32_t Index;
|
2017-11-01 22:16:06 +01:00
|
|
|
StringRef Name;
|
2017-08-29 04:12:03 +02:00
|
|
|
uint32_t NameIndex;
|
|
|
|
uint64_t Size;
|
|
|
|
uint8_t Type;
|
|
|
|
uint64_t Value;
|
2018-01-03 00:01:24 +01:00
|
|
|
uint8_t Visibility;
|
2017-09-08 01:02:50 +02:00
|
|
|
|
|
|
|
uint16_t getShndx() const;
|
2017-08-29 04:12:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class SymbolTableSection : public SectionBase {
|
2018-01-25 23:46:17 +01:00
|
|
|
MAKE_SEC_WRITER_FRIEND
|
|
|
|
|
2017-08-29 04:12:03 +02:00
|
|
|
protected:
|
|
|
|
std::vector<std::unique_ptr<Symbol>> Symbols;
|
2017-09-27 02:44:00 +02:00
|
|
|
StringTableSection *SymbolNames = nullptr;
|
2017-08-29 04:12:03 +02:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
using SymPtr = std::unique_ptr<Symbol>;
|
2017-10-10 20:47:09 +02:00
|
|
|
|
2017-08-29 04:12:03 +02:00
|
|
|
public:
|
|
|
|
void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
|
2017-11-01 22:16:06 +01:00
|
|
|
void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
|
2018-01-03 00:01:24 +01:00
|
|
|
SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
|
|
|
|
uint16_t Shndx, uint64_t Sz);
|
2017-08-29 04:12:03 +02:00
|
|
|
void addSymbolNames();
|
2017-11-30 21:14:53 +01:00
|
|
|
const SectionBase *getStrTab() const { return SymbolNames; }
|
2017-08-29 04:12:03 +02:00
|
|
|
const Symbol *getSymbolByIndex(uint32_t Index) const;
|
2017-10-10 20:47:09 +02:00
|
|
|
void removeSectionReferences(const SectionBase *Sec) override;
|
2018-01-05 20:19:09 +01:00
|
|
|
void localize(std::function<bool(const Symbol &)> ToLocalize);
|
2017-09-25 22:37:28 +02:00
|
|
|
void initialize(SectionTableRef SecTable) override;
|
2017-08-29 04:12:03 +02:00
|
|
|
void finalize() override;
|
2018-01-25 23:46:17 +01:00
|
|
|
void accept(SectionVisitor &Visitor) const override;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2017-08-29 04:12:03 +02:00
|
|
|
static bool classof(const SectionBase *S) {
|
2017-11-01 22:16:06 +01:00
|
|
|
return S->Type == ELF::SHT_SYMTAB;
|
2017-08-29 04:12:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-07 01:41:02 +02:00
|
|
|
struct Relocation {
|
2017-09-27 02:44:00 +02:00
|
|
|
const Symbol *RelocSymbol = nullptr;
|
2017-09-07 01:41:02 +02:00
|
|
|
uint64_t Offset;
|
|
|
|
uint64_t Addend;
|
|
|
|
uint32_t Type;
|
|
|
|
};
|
|
|
|
|
2017-10-10 20:47:09 +02:00
|
|
|
// All relocation sections denote relocations to apply to another section.
|
|
|
|
// However, some relocation sections use a dynamic symbol table and others use
|
|
|
|
// a regular symbol table. Because the types of the two symbol tables differ in
|
|
|
|
// our system (because they should behave differently) we can't uniformly
|
|
|
|
// represent all relocations with the same base class if we expose an interface
|
|
|
|
// that mentions the symbol table type. So we split the two base types into two
|
|
|
|
// different classes, one which handles the section the relocation is applied to
|
|
|
|
// and another which handles the symbol table type. The symbol table type is
|
|
|
|
// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
|
|
|
|
class RelocationSectionBase : public SectionBase {
|
|
|
|
protected:
|
|
|
|
SectionBase *SecToApplyRel = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
const SectionBase *getSection() const { return SecToApplyRel; }
|
|
|
|
void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
|
|
|
|
|
|
|
|
static bool classof(const SectionBase *S) {
|
2017-11-01 22:16:06 +01:00
|
|
|
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
|
2017-10-10 20:47:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Takes the symbol table type to use as a parameter so that we can deduplicate
|
|
|
|
// that code between the two symbol table types.
|
|
|
|
template <class SymTabType>
|
|
|
|
class RelocSectionWithSymtabBase : public RelocationSectionBase {
|
2017-09-26 20:02:25 +02:00
|
|
|
private:
|
2017-09-27 02:44:00 +02:00
|
|
|
SymTabType *Symbols = nullptr;
|
2017-10-10 20:47:09 +02:00
|
|
|
|
|
|
|
protected:
|
2017-11-01 22:16:06 +01:00
|
|
|
RelocSectionWithSymtabBase() = default;
|
2017-09-26 20:02:25 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
|
2017-10-10 20:47:09 +02:00
|
|
|
void removeSectionReferences(const SectionBase *Sec) override;
|
2017-09-26 20:02:25 +02:00
|
|
|
void initialize(SectionTableRef SecTable) override;
|
|
|
|
void finalize() override;
|
|
|
|
};
|
|
|
|
|
2017-10-10 20:47:09 +02:00
|
|
|
class RelocationSection
|
|
|
|
: public RelocSectionWithSymtabBase<SymbolTableSection> {
|
2018-01-25 23:46:17 +01:00
|
|
|
MAKE_SEC_WRITER_FRIEND
|
2018-01-25 22:03:38 +01:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
private:
|
2017-09-07 01:41:02 +02:00
|
|
|
std::vector<Relocation> Relocations;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
|
2018-01-25 23:46:17 +01:00
|
|
|
void accept(SectionVisitor &Visitor) const override;
|
2017-09-26 20:02:25 +02:00
|
|
|
|
2017-09-07 01:41:02 +02:00
|
|
|
static bool classof(const SectionBase *S) {
|
2017-11-01 22:16:06 +01:00
|
|
|
if (S->Flags & ELF::SHF_ALLOC)
|
2017-09-26 20:02:25 +02:00
|
|
|
return false;
|
2017-11-01 22:16:06 +01:00
|
|
|
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
|
2017-09-07 01:41:02 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-20 19:11:58 +02:00
|
|
|
class SectionWithStrTab : public Section {
|
|
|
|
private:
|
2017-10-10 23:28:22 +02:00
|
|
|
const SectionBase *StrTab = nullptr;
|
2017-09-20 19:11:58 +02:00
|
|
|
|
|
|
|
public:
|
2017-11-01 22:16:06 +01:00
|
|
|
SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
|
|
|
|
|
2017-10-10 23:28:22 +02:00
|
|
|
void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
|
2017-10-10 20:47:09 +02:00
|
|
|
void removeSectionReferences(const SectionBase *Sec) override;
|
2017-09-25 22:37:28 +02:00
|
|
|
void initialize(SectionTableRef SecTable) override;
|
2017-09-20 19:11:58 +02:00
|
|
|
void finalize() override;
|
|
|
|
static bool classof(const SectionBase *S);
|
|
|
|
};
|
|
|
|
|
|
|
|
class DynamicSymbolTableSection : public SectionWithStrTab {
|
|
|
|
public:
|
2017-11-01 22:16:06 +01:00
|
|
|
DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
|
|
|
|
|
2017-09-20 19:11:58 +02:00
|
|
|
static bool classof(const SectionBase *S) {
|
2017-11-01 22:16:06 +01:00
|
|
|
return S->Type == ELF::SHT_DYNSYM;
|
2017-09-20 19:11:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DynamicSection : public SectionWithStrTab {
|
|
|
|
public:
|
2017-11-01 22:16:06 +01:00
|
|
|
DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
|
|
|
|
|
2017-09-20 19:11:58 +02:00
|
|
|
static bool classof(const SectionBase *S) {
|
2017-11-01 22:16:06 +01:00
|
|
|
return S->Type == ELF::SHT_DYNAMIC;
|
2017-09-20 19:11:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-26 20:02:25 +02:00
|
|
|
class DynamicRelocationSection
|
2017-10-10 20:47:09 +02:00
|
|
|
: public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
|
2018-01-25 23:46:17 +01:00
|
|
|
MAKE_SEC_WRITER_FRIEND
|
|
|
|
|
2017-09-26 20:02:25 +02:00
|
|
|
private:
|
2017-11-01 22:16:06 +01:00
|
|
|
ArrayRef<uint8_t> Contents;
|
2017-09-26 20:02:25 +02:00
|
|
|
|
|
|
|
public:
|
2017-11-01 22:16:06 +01:00
|
|
|
DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
void accept(SectionVisitor &) const override;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2017-09-26 20:02:25 +02:00
|
|
|
static bool classof(const SectionBase *S) {
|
2017-11-01 22:16:06 +01:00
|
|
|
if (!(S->Flags & ELF::SHF_ALLOC))
|
2017-09-26 20:02:25 +02:00
|
|
|
return false;
|
2017-11-01 22:16:06 +01:00
|
|
|
return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
|
2017-09-26 20:02:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
class GnuDebugLinkSection : public SectionBase {
|
|
|
|
MAKE_SEC_WRITER_FRIEND
|
|
|
|
|
2018-01-25 23:15:14 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
StringRef FileName;
|
|
|
|
uint32_t CRC32;
|
|
|
|
|
|
|
|
void init(StringRef File, StringRef Data);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// If we add this section from an external source we can use this ctor.
|
|
|
|
GnuDebugLinkSection(StringRef File);
|
2018-01-25 23:46:17 +01:00
|
|
|
void accept(SectionVisitor &Visitor) const override;
|
2018-01-25 23:15:14 +01:00
|
|
|
};
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
class Reader {
|
|
|
|
public:
|
|
|
|
virtual ~Reader();
|
|
|
|
virtual std::unique_ptr<Object> create() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
using object::OwningBinary;
|
|
|
|
using object::Binary;
|
|
|
|
using object::ELFFile;
|
|
|
|
using object::ELFObjectFile;
|
2018-01-25 22:03:38 +01:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
template <class ELFT> class ELFBuilder {
|
|
|
|
private:
|
2018-02-15 00:31:33 +01:00
|
|
|
using Elf_Addr = typename ELFT::Addr;
|
2017-11-01 22:16:06 +01:00
|
|
|
using Elf_Shdr = typename ELFT::Shdr;
|
2018-02-15 00:31:33 +01:00
|
|
|
using Elf_Ehdr = typename ELFT::Ehdr;
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
const ELFFile<ELFT> &ElfFile;
|
|
|
|
Object &Obj;
|
2017-08-04 23:09:26 +02:00
|
|
|
|
2018-02-15 00:31:33 +01:00
|
|
|
void setParentSegment(Segment &Child);
|
2018-01-25 23:46:17 +01:00
|
|
|
void readProgramHeaders();
|
|
|
|
void initSymbolTable(SymbolTableSection *SymTab);
|
|
|
|
void readSectionHeaders();
|
|
|
|
SectionBase &makeSection(const Elf_Shdr &Shdr);
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2018-01-25 22:03:38 +01:00
|
|
|
public:
|
2018-01-25 23:46:17 +01:00
|
|
|
ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
|
|
|
|
: ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
|
2017-08-04 23:09:26 +02:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
void build();
|
2017-08-01 02:33:58 +02:00
|
|
|
};
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
class ELFReader : public Reader {
|
2017-08-04 23:09:26 +02:00
|
|
|
private:
|
2018-01-26 03:01:37 +01:00
|
|
|
std::unique_ptr<Binary> Bin;
|
2018-01-25 23:46:17 +01:00
|
|
|
std::shared_ptr<MemoryBuffer> Data;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2018-01-25 22:03:38 +01:00
|
|
|
public:
|
2018-01-25 23:46:17 +01:00
|
|
|
ElfType getElfType() const;
|
|
|
|
std::unique_ptr<Object> create() const override;
|
|
|
|
ELFReader(StringRef File);
|
2018-01-25 22:03:38 +01:00
|
|
|
};
|
2018-01-25 21:24:17 +01:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
class Object {
|
2018-01-25 22:03:38 +01:00
|
|
|
private:
|
|
|
|
using SecPtr = std::unique_ptr<SectionBase>;
|
|
|
|
using SegPtr = std::unique_ptr<Segment>;
|
2018-01-25 21:24:17 +01:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
std::shared_ptr<MemoryBuffer> OwnedData;
|
|
|
|
std::vector<SecPtr> Sections;
|
|
|
|
std::vector<SegPtr> Segments;
|
2018-01-25 21:24:17 +01:00
|
|
|
|
2018-01-25 22:03:38 +01:00
|
|
|
public:
|
2018-01-25 23:46:17 +01:00
|
|
|
template <class T>
|
|
|
|
using Range = iterator_range<
|
|
|
|
pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
|
2018-01-25 21:24:17 +01:00
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
template <class T>
|
|
|
|
using ConstRange = iterator_range<pointee_iterator<
|
|
|
|
typename std::vector<std::unique_ptr<T>>::const_iterator>>;
|
|
|
|
|
2018-02-15 00:31:33 +01:00
|
|
|
// It is often the case that the ELF header and the program header table are
|
|
|
|
// not present in any segment. This could be a problem during file layout,
|
|
|
|
// because other segments may get assigned an offset where either of the
|
|
|
|
// two should reside, which will effectively corrupt the resulting binary.
|
|
|
|
// Other than that we use these segments to track program header offsets
|
|
|
|
// when they may not follow the ELF header.
|
|
|
|
Segment ElfHdrSegment;
|
|
|
|
Segment ProgramHdrSegment;
|
|
|
|
|
2018-01-25 23:46:17 +01:00
|
|
|
uint8_t Ident[16];
|
|
|
|
uint64_t Entry;
|
|
|
|
uint64_t SHOffset;
|
|
|
|
uint32_t Type;
|
|
|
|
uint32_t Machine;
|
|
|
|
uint32_t Version;
|
|
|
|
uint32_t Flags;
|
|
|
|
|
|
|
|
StringTableSection *SectionNames = nullptr;
|
|
|
|
SymbolTableSection *SymbolTable = nullptr;
|
|
|
|
|
|
|
|
Object(std::shared_ptr<MemoryBuffer> Data) : OwnedData(Data) {}
|
|
|
|
virtual ~Object() = default;
|
|
|
|
|
|
|
|
void sortSections();
|
|
|
|
SectionTableRef sections() { return SectionTableRef(Sections); }
|
|
|
|
ConstRange<SectionBase> sections() const {
|
|
|
|
return make_pointee_range(Sections);
|
|
|
|
}
|
|
|
|
Range<Segment> segments() { return make_pointee_range(Segments); }
|
|
|
|
ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
|
|
|
|
|
|
|
|
void removeSections(std::function<bool(const SectionBase &)> ToRemove);
|
|
|
|
template <class T, class... Ts> T &addSection(Ts &&... Args) {
|
|
|
|
auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
|
|
|
|
auto Ptr = Sec.get();
|
|
|
|
Sections.emplace_back(std::move(Sec));
|
|
|
|
return *Ptr;
|
|
|
|
}
|
|
|
|
Segment &addSegment(ArrayRef<uint8_t> Data) {
|
|
|
|
Segments.emplace_back(llvm::make_unique<Segment>(Data));
|
|
|
|
return *Segments.back();
|
|
|
|
}
|
2017-08-04 23:09:26 +02:00
|
|
|
};
|
2017-11-01 22:16:06 +01:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H
|