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; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 21 | class SectionBase; |
| 22 | |
| 23 | class SectionTableRef { |
| 24 | private: |
| 25 | llvm::ArrayRef<std::unique_ptr<SectionBase>> Sections; |
| 26 | |
| 27 | public: |
| 28 | SectionTableRef(llvm::ArrayRef<std::unique_ptr<SectionBase>> Secs) |
| 29 | : Sections(Secs) {} |
| 30 | SectionTableRef(const SectionTableRef &) = default; |
| 31 | |
| 32 | SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg); |
| 33 | |
| 34 | template <class T> |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 35 | T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg, |
| 36 | llvm::Twine TypeErrMsg); |
| 37 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 38 | |
| 39 | class SectionBase { |
| 40 | public: |
| 41 | llvm::StringRef Name; |
| 42 | Segment *ParentSegment = nullptr; |
| 43 | uint64_t HeaderOffset; |
| 44 | uint64_t OriginalOffset; |
| 45 | uint32_t Index; |
| 46 | |
| 47 | uint64_t Addr = 0; |
| 48 | uint64_t Align = 1; |
| 49 | uint32_t EntrySize = 0; |
| 50 | uint64_t Flags = 0; |
| 51 | uint64_t Info = 0; |
| 52 | uint64_t Link = llvm::ELF::SHN_UNDEF; |
| 53 | uint64_t NameIndex = 0; |
| 54 | uint64_t Offset = 0; |
| 55 | uint64_t Size = 0; |
| 56 | uint64_t Type = llvm::ELF::SHT_NULL; |
| 57 | |
| 58 | virtual ~SectionBase() {} |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 59 | virtual void initialize(SectionTableRef SecTable); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 60 | virtual void finalize(); |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 61 | virtual void removeSectionReferences(const SectionBase *Sec); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 62 | template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const; |
| 63 | virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0; |
| 64 | }; |
| 65 | |
| 66 | class Segment { |
| 67 | private: |
| 68 | struct SectionCompare { |
| 69 | bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { |
| 70 | // Some sections might have the same address if one of them is empty. To |
| 71 | // fix this we can use the lexicographic ordering on ->Addr and the |
| 72 | // address of the actully stored section. |
| 73 | if (Lhs->OriginalOffset == Rhs->OriginalOffset) |
| 74 | return Lhs < Rhs; |
| 75 | return Lhs->OriginalOffset < Rhs->OriginalOffset; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | std::set<const SectionBase *, SectionCompare> Sections; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 80 | llvm::ArrayRef<uint8_t> Contents; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 81 | |
| 82 | public: |
| 83 | uint64_t Align; |
| 84 | uint64_t FileSize; |
| 85 | uint32_t Flags; |
| 86 | uint32_t Index; |
| 87 | uint64_t MemSize; |
| 88 | uint64_t Offset; |
| 89 | uint64_t PAddr; |
| 90 | uint64_t Type; |
| 91 | uint64_t VAddr; |
| 92 | |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 93 | uint64_t OriginalOffset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 94 | Segment *ParentSegment = nullptr; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 95 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 96 | Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 97 | const SectionBase *firstSection() const { |
| 98 | if (!Sections.empty()) |
| 99 | return *Sections.begin(); |
| 100 | return nullptr; |
| 101 | } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 102 | void removeSection(const SectionBase *Sec) { Sections.erase(Sec); } |
| 103 | void addSection(const SectionBase *Sec) { Sections.insert(Sec); } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 104 | template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 105 | void writeSegment(llvm::FileOutputBuffer &Out) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | class Section : public SectionBase { |
| 109 | private: |
| 110 | llvm::ArrayRef<uint8_t> Contents; |
| 111 | |
| 112 | public: |
| 113 | Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 114 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 115 | }; |
| 116 | |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 117 | // There are two types of string tables that can exist, dynamic and not dynamic. |
| 118 | // In the dynamic case the string table is allocated. Changing a dynamic string |
| 119 | // table would mean altering virtual addresses and thus the memory image. So |
| 120 | // dynamic string tables should not have an interface to modify them or |
| 121 | // reconstruct them. This type lets us reconstruct a string table. To avoid |
| 122 | // this class being used for dynamic string tables (which has happened) the |
| 123 | // classof method checks that the particular instance is not allocated. This |
| 124 | // then agrees with the makeSection method used to construct most sections. |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 125 | class StringTableSection : public SectionBase { |
| 126 | private: |
| 127 | llvm::StringTableBuilder StrTabBuilder; |
| 128 | |
| 129 | public: |
| 130 | StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) { |
| 131 | Type = llvm::ELF::SHT_STRTAB; |
| 132 | } |
| 133 | |
| 134 | void addString(llvm::StringRef Name); |
| 135 | uint32_t findIndex(llvm::StringRef Name) const; |
| 136 | void finalize() override; |
| 137 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 138 | static bool classof(const SectionBase *S) { |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 139 | if (S->Flags & llvm::ELF::SHF_ALLOC) |
| 140 | return false; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 141 | return S->Type == llvm::ELF::SHT_STRTAB; |
| 142 | } |
| 143 | }; |
| 144 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 145 | // Symbols have a st_shndx field that normally stores an index but occasionally |
| 146 | // stores a different special value. This enum keeps track of what the st_shndx |
| 147 | // field means. Most of the values are just copies of the special SHN_* values. |
| 148 | // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. |
| 149 | enum SymbolShndxType { |
| 150 | SYMBOL_SIMPLE_INDEX = 0, |
| 151 | SYMBOL_ABS = llvm::ELF::SHN_ABS, |
| 152 | SYMBOL_COMMON = llvm::ELF::SHN_COMMON, |
| 153 | SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON, |
| 154 | SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2, |
| 155 | SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4, |
| 156 | SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8, |
| 157 | }; |
| 158 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 159 | struct Symbol { |
| 160 | uint8_t Binding; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 161 | SectionBase *DefinedIn = nullptr; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 162 | SymbolShndxType ShndxType; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 163 | uint32_t Index; |
| 164 | llvm::StringRef Name; |
| 165 | uint32_t NameIndex; |
| 166 | uint64_t Size; |
| 167 | uint8_t Type; |
| 168 | uint64_t Value; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 169 | |
| 170 | uint16_t getShndx() const; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | class SymbolTableSection : public SectionBase { |
| 174 | protected: |
| 175 | std::vector<std::unique_ptr<Symbol>> Symbols; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 176 | StringTableSection *SymbolNames = nullptr; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 177 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 178 | typedef std::unique_ptr<Symbol> SymPtr; |
| 179 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 180 | public: |
| 181 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
| 182 | void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 183 | SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx, |
| 184 | uint64_t Sz); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 185 | void addSymbolNames(); |
| 186 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 187 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 188 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 189 | void finalize() override; |
| 190 | static bool classof(const SectionBase *S) { |
| 191 | return S->Type == llvm::ELF::SHT_SYMTAB; |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | // Only writeSection depends on the ELF type so we implement it in a subclass. |
| 196 | template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection { |
| 197 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 198 | }; |
| 199 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 200 | struct Relocation { |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 201 | const Symbol *RelocSymbol = nullptr; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 202 | uint64_t Offset; |
| 203 | uint64_t Addend; |
| 204 | uint32_t Type; |
| 205 | }; |
| 206 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 207 | // All relocation sections denote relocations to apply to another section. |
| 208 | // However, some relocation sections use a dynamic symbol table and others use |
| 209 | // a regular symbol table. Because the types of the two symbol tables differ in |
| 210 | // our system (because they should behave differently) we can't uniformly |
| 211 | // represent all relocations with the same base class if we expose an interface |
| 212 | // that mentions the symbol table type. So we split the two base types into two |
| 213 | // different classes, one which handles the section the relocation is applied to |
| 214 | // and another which handles the symbol table type. The symbol table type is |
| 215 | // taken as a type parameter to the class (see RelocSectionWithSymtabBase). |
| 216 | class RelocationSectionBase : public SectionBase { |
| 217 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 218 | SectionBase *SecToApplyRel = nullptr; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 219 | |
| 220 | public: |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 221 | const SectionBase *getSection() const { return SecToApplyRel; } |
Jake Ehrlich | c5ff727 | 2017-10-10 18:32:22 +0000 | [diff] [blame] | 222 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 223 | |
| 224 | static bool classof(const SectionBase *S) { |
| 225 | return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | // Takes the symbol table type to use as a parameter so that we can deduplicate |
| 230 | // that code between the two symbol table types. |
| 231 | template <class SymTabType> |
| 232 | class RelocSectionWithSymtabBase : public RelocationSectionBase { |
| 233 | private: |
| 234 | SymTabType *Symbols = nullptr; |
| 235 | |
| 236 | protected: |
| 237 | RelocSectionWithSymtabBase() {} |
| 238 | |
| 239 | public: |
| 240 | void setSymTab(SymTabType *StrTab) { Symbols = StrTab; } |
| 241 | |
| 242 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 243 | void initialize(SectionTableRef SecTable) override; |
| 244 | void finalize() override; |
| 245 | }; |
| 246 | |
| 247 | template <class ELFT> |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 248 | class RelocationSection |
| 249 | : public RelocSectionWithSymtabBase<SymbolTableSection> { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 250 | private: |
| 251 | typedef typename ELFT::Rel Elf_Rel; |
| 252 | typedef typename ELFT::Rela Elf_Rela; |
| 253 | |
| 254 | std::vector<Relocation> Relocations; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 255 | |
| 256 | template <class T> void writeRel(T *Buf) const; |
| 257 | |
| 258 | public: |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 259 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 260 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 261 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 262 | static bool classof(const SectionBase *S) { |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 263 | if (S->Flags & llvm::ELF::SHF_ALLOC) |
| 264 | return false; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 265 | return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; |
| 266 | } |
| 267 | }; |
| 268 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 269 | class SectionWithStrTab : public Section { |
| 270 | private: |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 271 | const SectionBase *StrTab = nullptr; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 272 | |
| 273 | public: |
| 274 | SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {} |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 275 | void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 276 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 277 | void initialize(SectionTableRef SecTable) override; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 278 | void finalize() override; |
| 279 | static bool classof(const SectionBase *S); |
| 280 | }; |
| 281 | |
| 282 | class DynamicSymbolTableSection : public SectionWithStrTab { |
| 283 | public: |
| 284 | DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data) |
| 285 | : SectionWithStrTab(Data) {} |
| 286 | static bool classof(const SectionBase *S) { |
| 287 | return S->Type == llvm::ELF::SHT_DYNSYM; |
| 288 | } |
| 289 | }; |
| 290 | |
| 291 | class DynamicSection : public SectionWithStrTab { |
| 292 | public: |
| 293 | DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
| 294 | static bool classof(const SectionBase *S) { |
| 295 | return S->Type == llvm::ELF::SHT_DYNAMIC; |
| 296 | } |
| 297 | }; |
| 298 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 299 | class DynamicRelocationSection |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 300 | : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> { |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 301 | private: |
| 302 | llvm::ArrayRef<uint8_t> Contents; |
| 303 | |
| 304 | public: |
| 305 | DynamicRelocationSection(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 306 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 307 | static bool classof(const SectionBase *S) { |
| 308 | if (!(S->Flags & llvm::ELF::SHF_ALLOC)) |
| 309 | return false; |
| 310 | return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; |
| 311 | } |
| 312 | }; |
| 313 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 314 | template <class ELFT> class Object { |
| 315 | private: |
| 316 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 317 | typedef std::unique_ptr<Segment> SegPtr; |
| 318 | |
| 319 | typedef typename ELFT::Shdr Elf_Shdr; |
| 320 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 321 | typedef typename ELFT::Phdr Elf_Phdr; |
| 322 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 323 | void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile, |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 324 | SymbolTableSection *SymTab, SectionTableRef SecTable); |
Petr Hosek | b1bb3e5 | 2017-08-04 05:33:44 +0000 | [diff] [blame] | 325 | SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile, |
| 326 | const Elf_Shdr &Shdr); |
| 327 | void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 328 | SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 329 | |
| 330 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 331 | StringTableSection *SectionNames = nullptr; |
| 332 | SymbolTableSection *SymbolTable = nullptr; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 333 | std::vector<SecPtr> Sections; |
| 334 | std::vector<SegPtr> Segments; |
| 335 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 336 | void writeHeader(llvm::FileOutputBuffer &Out) const; |
| 337 | void writeProgramHeaders(llvm::FileOutputBuffer &Out) const; |
| 338 | void writeSectionData(llvm::FileOutputBuffer &Out) const; |
| 339 | void writeSectionHeaders(llvm::FileOutputBuffer &Out) const; |
| 340 | |
| 341 | public: |
| 342 | uint8_t Ident[16]; |
| 343 | uint64_t Entry; |
| 344 | uint64_t SHOffset; |
| 345 | uint32_t Type; |
| 346 | uint32_t Machine; |
| 347 | uint32_t Version; |
| 348 | uint32_t Flags; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame^] | 349 | bool WriteSectionHeaders = true; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 350 | |
| 351 | Object(const llvm::object::ELFObjectFile<ELFT> &Obj); |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 352 | void removeSections(std::function<bool(const SectionBase &)> ToRemove); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 353 | virtual size_t totalSize() const = 0; |
| 354 | virtual void finalize() = 0; |
| 355 | virtual void write(llvm::FileOutputBuffer &Out) const = 0; |
| 356 | virtual ~Object() = default; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 359 | template <class ELFT> class ELFObject : public Object<ELFT> { |
| 360 | private: |
| 361 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 362 | typedef std::unique_ptr<Segment> SegPtr; |
| 363 | |
| 364 | typedef typename ELFT::Shdr Elf_Shdr; |
| 365 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 366 | typedef typename ELFT::Phdr Elf_Phdr; |
| 367 | |
| 368 | void sortSections(); |
| 369 | void assignOffsets(); |
| 370 | |
| 371 | public: |
| 372 | ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 373 | void finalize() override; |
| 374 | size_t totalSize() const override; |
| 375 | void write(llvm::FileOutputBuffer &Out) const override; |
| 376 | }; |
| 377 | |
| 378 | template <class ELFT> class BinaryObject : public Object<ELFT> { |
| 379 | private: |
| 380 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 381 | typedef std::unique_ptr<Segment> SegPtr; |
| 382 | |
| 383 | uint64_t TotalSize; |
| 384 | |
| 385 | public: |
| 386 | BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj) |
| 387 | : Object<ELFT>(Obj) {} |
| 388 | void finalize() override; |
| 389 | size_t totalSize() const override; |
| 390 | void write(llvm::FileOutputBuffer &Out) const override; |
| 391 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 392 | #endif |