Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1 | //===- Object.h -------------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_OBJCOPY_OBJECT_H |
| 11 | #define LLVM_OBJCOPY_OBJECT_H |
| 12 | |
| 13 | #include "llvm/MC/StringTableBuilder.h" |
| 14 | #include "llvm/Object/ELFObjectFile.h" |
| 15 | #include "llvm/Support/FileOutputBuffer.h" |
| 16 | |
| 17 | #include <memory> |
| 18 | #include <set> |
| 19 | |
| 20 | class Segment; |
| 21 | |
| 22 | class SectionBase { |
| 23 | public: |
| 24 | llvm::StringRef Name; |
| 25 | Segment *ParentSegment = nullptr; |
| 26 | uint64_t HeaderOffset; |
| 27 | uint64_t OriginalOffset; |
| 28 | uint32_t Index; |
| 29 | |
| 30 | uint64_t Addr = 0; |
| 31 | uint64_t Align = 1; |
| 32 | uint32_t EntrySize = 0; |
| 33 | uint64_t Flags = 0; |
| 34 | uint64_t Info = 0; |
| 35 | uint64_t Link = llvm::ELF::SHN_UNDEF; |
| 36 | uint64_t NameIndex = 0; |
| 37 | uint64_t Offset = 0; |
| 38 | uint64_t Size = 0; |
| 39 | uint64_t Type = llvm::ELF::SHT_NULL; |
| 40 | |
| 41 | virtual ~SectionBase() {} |
| 42 | virtual void finalize(); |
| 43 | template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const; |
| 44 | virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0; |
| 45 | }; |
| 46 | |
| 47 | class Segment { |
| 48 | private: |
| 49 | struct SectionCompare { |
| 50 | bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { |
| 51 | // Some sections might have the same address if one of them is empty. To |
| 52 | // fix this we can use the lexicographic ordering on ->Addr and the |
| 53 | // address of the actully stored section. |
| 54 | if (Lhs->OriginalOffset == Rhs->OriginalOffset) |
| 55 | return Lhs < Rhs; |
| 56 | return Lhs->OriginalOffset < Rhs->OriginalOffset; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | std::set<const SectionBase *, SectionCompare> Sections; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 61 | llvm::ArrayRef<uint8_t> Contents; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 62 | |
| 63 | public: |
| 64 | uint64_t Align; |
| 65 | uint64_t FileSize; |
| 66 | uint32_t Flags; |
| 67 | uint32_t Index; |
| 68 | uint64_t MemSize; |
| 69 | uint64_t Offset; |
| 70 | uint64_t PAddr; |
| 71 | uint64_t Type; |
| 72 | uint64_t VAddr; |
| 73 | |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 74 | uint64_t OriginalOffset; |
| 75 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 76 | Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 77 | void finalize(); |
| 78 | const SectionBase *firstSection() const { |
| 79 | if (!Sections.empty()) |
| 80 | return *Sections.begin(); |
| 81 | return nullptr; |
| 82 | } |
| 83 | void addSection(const SectionBase *sec) { Sections.insert(sec); } |
| 84 | template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 85 | void writeSegment(llvm::FileOutputBuffer &Out) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | class Section : public SectionBase { |
| 89 | private: |
| 90 | llvm::ArrayRef<uint8_t> Contents; |
| 91 | |
| 92 | public: |
| 93 | Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 94 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 95 | }; |
| 96 | |
| 97 | // This is just a wraper around a StringTableBuilder that implements SectionBase |
| 98 | class StringTableSection : public SectionBase { |
| 99 | private: |
| 100 | llvm::StringTableBuilder StrTabBuilder; |
| 101 | |
| 102 | public: |
| 103 | StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) { |
| 104 | Type = llvm::ELF::SHT_STRTAB; |
| 105 | } |
| 106 | |
| 107 | void addString(llvm::StringRef Name); |
| 108 | uint32_t findIndex(llvm::StringRef Name) const; |
| 109 | void finalize() override; |
| 110 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 111 | static bool classof(const SectionBase *S) { |
| 112 | return S->Type == llvm::ELF::SHT_STRTAB; |
| 113 | } |
| 114 | }; |
| 115 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 116 | struct Symbol { |
| 117 | uint8_t Binding; |
| 118 | SectionBase *DefinedIn; |
| 119 | uint32_t Index; |
| 120 | llvm::StringRef Name; |
| 121 | uint32_t NameIndex; |
| 122 | uint64_t Size; |
| 123 | uint8_t Type; |
| 124 | uint64_t Value; |
| 125 | }; |
| 126 | |
| 127 | class SymbolTableSection : public SectionBase { |
| 128 | protected: |
| 129 | std::vector<std::unique_ptr<Symbol>> Symbols; |
| 130 | StringTableSection *SymbolNames; |
| 131 | |
| 132 | public: |
| 133 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
| 134 | void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type, |
| 135 | SectionBase *DefinedIn, uint64_t Value, uint64_t Sz); |
| 136 | void addSymbolNames(); |
| 137 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
| 138 | void finalize() override; |
| 139 | static bool classof(const SectionBase *S) { |
| 140 | return S->Type == llvm::ELF::SHT_SYMTAB; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | // Only writeSection depends on the ELF type so we implement it in a subclass. |
| 145 | template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection { |
| 146 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 147 | }; |
| 148 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame^] | 149 | struct Relocation { |
| 150 | const Symbol *RelocSymbol; |
| 151 | uint64_t Offset; |
| 152 | uint64_t Addend; |
| 153 | uint32_t Type; |
| 154 | }; |
| 155 | |
| 156 | template <class ELFT> class RelocationSection : public SectionBase { |
| 157 | private: |
| 158 | typedef typename ELFT::Rel Elf_Rel; |
| 159 | typedef typename ELFT::Rela Elf_Rela; |
| 160 | |
| 161 | std::vector<Relocation> Relocations; |
| 162 | SymbolTableSection *Symbols; |
| 163 | SectionBase *SecToApplyRel; |
| 164 | |
| 165 | template <class T> void writeRel(T *Buf) const; |
| 166 | |
| 167 | public: |
| 168 | void setSymTab(SymbolTableSection *StrTab) { Symbols = StrTab; } |
| 169 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
| 170 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
| 171 | void finalize() override; |
| 172 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 173 | static bool classof(const SectionBase *S) { |
| 174 | return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; |
| 175 | } |
| 176 | }; |
| 177 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 178 | template <class ELFT> class Object { |
| 179 | private: |
| 180 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 181 | typedef std::unique_ptr<Segment> SegPtr; |
| 182 | |
| 183 | typedef typename ELFT::Shdr Elf_Shdr; |
| 184 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 185 | typedef typename ELFT::Phdr Elf_Phdr; |
| 186 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 187 | void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile, |
| 188 | SymbolTableSection *SymTab); |
Petr Hosek | b1bb3e5 | 2017-08-04 05:33:44 +0000 | [diff] [blame] | 189 | SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile, |
| 190 | const Elf_Shdr &Shdr); |
| 191 | void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
| 192 | void readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 193 | |
| 194 | protected: |
| 195 | StringTableSection *SectionNames; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 196 | SymbolTableSection *SymbolTable; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 197 | std::vector<SecPtr> Sections; |
| 198 | std::vector<SegPtr> Segments; |
| 199 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 200 | void writeHeader(llvm::FileOutputBuffer &Out) const; |
| 201 | void writeProgramHeaders(llvm::FileOutputBuffer &Out) const; |
| 202 | void writeSectionData(llvm::FileOutputBuffer &Out) const; |
| 203 | void writeSectionHeaders(llvm::FileOutputBuffer &Out) const; |
| 204 | |
| 205 | public: |
| 206 | uint8_t Ident[16]; |
| 207 | uint64_t Entry; |
| 208 | uint64_t SHOffset; |
| 209 | uint32_t Type; |
| 210 | uint32_t Machine; |
| 211 | uint32_t Version; |
| 212 | uint32_t Flags; |
| 213 | |
| 214 | Object(const llvm::object::ELFObjectFile<ELFT> &Obj); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 215 | virtual size_t totalSize() const = 0; |
| 216 | virtual void finalize() = 0; |
| 217 | virtual void write(llvm::FileOutputBuffer &Out) const = 0; |
| 218 | virtual ~Object() = default; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 221 | template <class ELFT> class ELFObject : public Object<ELFT> { |
| 222 | private: |
| 223 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 224 | typedef std::unique_ptr<Segment> SegPtr; |
| 225 | |
| 226 | typedef typename ELFT::Shdr Elf_Shdr; |
| 227 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 228 | typedef typename ELFT::Phdr Elf_Phdr; |
| 229 | |
| 230 | void sortSections(); |
| 231 | void assignOffsets(); |
| 232 | |
| 233 | public: |
| 234 | ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 235 | void finalize() override; |
| 236 | size_t totalSize() const override; |
| 237 | void write(llvm::FileOutputBuffer &Out) const override; |
| 238 | }; |
| 239 | |
| 240 | template <class ELFT> class BinaryObject : public Object<ELFT> { |
| 241 | private: |
| 242 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 243 | typedef std::unique_ptr<Segment> SegPtr; |
| 244 | |
| 245 | uint64_t TotalSize; |
| 246 | |
| 247 | public: |
| 248 | BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj) |
| 249 | : Object<ELFT>(Obj) {} |
| 250 | void finalize() override; |
| 251 | size_t totalSize() const override; |
| 252 | void write(llvm::FileOutputBuffer &Out) const override; |
| 253 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 254 | #endif |