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> |
| 35 | |
| 36 | T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg, |
| 37 | llvm::Twine TypeErrMsg); |
| 38 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 39 | |
| 40 | class SectionBase { |
| 41 | public: |
| 42 | llvm::StringRef Name; |
| 43 | Segment *ParentSegment = nullptr; |
| 44 | uint64_t HeaderOffset; |
| 45 | uint64_t OriginalOffset; |
| 46 | uint32_t Index; |
| 47 | |
| 48 | uint64_t Addr = 0; |
| 49 | uint64_t Align = 1; |
| 50 | uint32_t EntrySize = 0; |
| 51 | uint64_t Flags = 0; |
| 52 | uint64_t Info = 0; |
| 53 | uint64_t Link = llvm::ELF::SHN_UNDEF; |
| 54 | uint64_t NameIndex = 0; |
| 55 | uint64_t Offset = 0; |
| 56 | uint64_t Size = 0; |
| 57 | uint64_t Type = llvm::ELF::SHT_NULL; |
| 58 | |
| 59 | virtual ~SectionBase() {} |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 60 | virtual void initialize(SectionTableRef SecTable); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 61 | virtual void finalize(); |
| 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 | void finalize(); |
| 98 | const SectionBase *firstSection() const { |
| 99 | if (!Sections.empty()) |
| 100 | return *Sections.begin(); |
| 101 | return nullptr; |
| 102 | } |
| 103 | void addSection(const SectionBase *sec) { Sections.insert(sec); } |
| 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 | |
| 117 | // This is just a wraper around a StringTableBuilder that implements SectionBase |
| 118 | class StringTableSection : public SectionBase { |
| 119 | private: |
| 120 | llvm::StringTableBuilder StrTabBuilder; |
| 121 | |
| 122 | public: |
| 123 | StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) { |
| 124 | Type = llvm::ELF::SHT_STRTAB; |
| 125 | } |
| 126 | |
| 127 | void addString(llvm::StringRef Name); |
| 128 | uint32_t findIndex(llvm::StringRef Name) const; |
| 129 | void finalize() override; |
| 130 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 131 | static bool classof(const SectionBase *S) { |
| 132 | return S->Type == llvm::ELF::SHT_STRTAB; |
| 133 | } |
| 134 | }; |
| 135 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 136 | // Symbols have a st_shndx field that normally stores an index but occasionally |
| 137 | // stores a different special value. This enum keeps track of what the st_shndx |
| 138 | // field means. Most of the values are just copies of the special SHN_* values. |
| 139 | // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. |
| 140 | enum SymbolShndxType { |
| 141 | SYMBOL_SIMPLE_INDEX = 0, |
| 142 | SYMBOL_ABS = llvm::ELF::SHN_ABS, |
| 143 | SYMBOL_COMMON = llvm::ELF::SHN_COMMON, |
| 144 | SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON, |
| 145 | SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2, |
| 146 | SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4, |
| 147 | SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8, |
| 148 | }; |
| 149 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 150 | struct Symbol { |
| 151 | uint8_t Binding; |
| 152 | SectionBase *DefinedIn; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 153 | SymbolShndxType ShndxType; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 154 | uint32_t Index; |
| 155 | llvm::StringRef Name; |
| 156 | uint32_t NameIndex; |
| 157 | uint64_t Size; |
| 158 | uint8_t Type; |
| 159 | uint64_t Value; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 160 | |
| 161 | uint16_t getShndx() const; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | class SymbolTableSection : public SectionBase { |
| 165 | protected: |
| 166 | std::vector<std::unique_ptr<Symbol>> Symbols; |
| 167 | StringTableSection *SymbolNames; |
| 168 | |
| 169 | public: |
| 170 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
| 171 | void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 172 | SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx, |
| 173 | uint64_t Sz); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 174 | void addSymbolNames(); |
| 175 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 176 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 177 | void finalize() override; |
| 178 | static bool classof(const SectionBase *S) { |
| 179 | return S->Type == llvm::ELF::SHT_SYMTAB; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | // Only writeSection depends on the ELF type so we implement it in a subclass. |
| 184 | template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection { |
| 185 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 186 | }; |
| 187 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 188 | struct Relocation { |
| 189 | const Symbol *RelocSymbol; |
| 190 | uint64_t Offset; |
| 191 | uint64_t Addend; |
| 192 | uint32_t Type; |
| 193 | }; |
| 194 | |
| 195 | template <class ELFT> class RelocationSection : public SectionBase { |
| 196 | private: |
| 197 | typedef typename ELFT::Rel Elf_Rel; |
| 198 | typedef typename ELFT::Rela Elf_Rela; |
| 199 | |
| 200 | std::vector<Relocation> Relocations; |
| 201 | SymbolTableSection *Symbols; |
| 202 | SectionBase *SecToApplyRel; |
| 203 | |
| 204 | template <class T> void writeRel(T *Buf) const; |
| 205 | |
| 206 | public: |
| 207 | void setSymTab(SymbolTableSection *StrTab) { Symbols = StrTab; } |
| 208 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
| 209 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 210 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 211 | void finalize() override; |
| 212 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 213 | static bool classof(const SectionBase *S) { |
| 214 | return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; |
| 215 | } |
| 216 | }; |
| 217 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 218 | class SectionWithStrTab : public Section { |
| 219 | private: |
| 220 | StringTableSection *StrTab; |
| 221 | |
| 222 | public: |
| 223 | SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {} |
| 224 | void setStrTab(StringTableSection *StringTable) { StrTab = StringTable; } |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 225 | void initialize(SectionTableRef SecTable) override; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 226 | void finalize() override; |
| 227 | static bool classof(const SectionBase *S); |
| 228 | }; |
| 229 | |
| 230 | class DynamicSymbolTableSection : public SectionWithStrTab { |
| 231 | public: |
| 232 | DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data) |
| 233 | : SectionWithStrTab(Data) {} |
| 234 | static bool classof(const SectionBase *S) { |
| 235 | return S->Type == llvm::ELF::SHT_DYNSYM; |
| 236 | } |
| 237 | }; |
| 238 | |
| 239 | class DynamicSection : public SectionWithStrTab { |
| 240 | public: |
| 241 | DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
| 242 | static bool classof(const SectionBase *S) { |
| 243 | return S->Type == llvm::ELF::SHT_DYNAMIC; |
| 244 | } |
| 245 | }; |
| 246 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 247 | template <class ELFT> class Object { |
| 248 | private: |
| 249 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 250 | typedef std::unique_ptr<Segment> SegPtr; |
| 251 | |
| 252 | typedef typename ELFT::Shdr Elf_Shdr; |
| 253 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 254 | typedef typename ELFT::Phdr Elf_Phdr; |
| 255 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 256 | void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile, |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 257 | SymbolTableSection *SymTab, SectionTableRef SecTable); |
Petr Hosek | b1bb3e5 | 2017-08-04 05:33:44 +0000 | [diff] [blame] | 258 | SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile, |
| 259 | const Elf_Shdr &Shdr); |
| 260 | void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 261 | SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 262 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 263 | SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg); |
| 264 | |
| 265 | template <class T> |
| 266 | T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg, |
| 267 | llvm::Twine TypeErrMsg); |
| 268 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 269 | protected: |
| 270 | StringTableSection *SectionNames; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 271 | SymbolTableSection *SymbolTable; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 272 | std::vector<SecPtr> Sections; |
| 273 | std::vector<SegPtr> Segments; |
| 274 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 275 | void writeHeader(llvm::FileOutputBuffer &Out) const; |
| 276 | void writeProgramHeaders(llvm::FileOutputBuffer &Out) const; |
| 277 | void writeSectionData(llvm::FileOutputBuffer &Out) const; |
| 278 | void writeSectionHeaders(llvm::FileOutputBuffer &Out) const; |
| 279 | |
| 280 | public: |
| 281 | uint8_t Ident[16]; |
| 282 | uint64_t Entry; |
| 283 | uint64_t SHOffset; |
| 284 | uint32_t Type; |
| 285 | uint32_t Machine; |
| 286 | uint32_t Version; |
| 287 | uint32_t Flags; |
| 288 | |
| 289 | Object(const llvm::object::ELFObjectFile<ELFT> &Obj); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 290 | virtual size_t totalSize() const = 0; |
| 291 | virtual void finalize() = 0; |
| 292 | virtual void write(llvm::FileOutputBuffer &Out) const = 0; |
| 293 | virtual ~Object() = default; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 296 | template <class ELFT> class ELFObject : public Object<ELFT> { |
| 297 | private: |
| 298 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 299 | typedef std::unique_ptr<Segment> SegPtr; |
| 300 | |
| 301 | typedef typename ELFT::Shdr Elf_Shdr; |
| 302 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 303 | typedef typename ELFT::Phdr Elf_Phdr; |
| 304 | |
| 305 | void sortSections(); |
| 306 | void assignOffsets(); |
| 307 | |
| 308 | public: |
| 309 | ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 310 | void finalize() override; |
| 311 | size_t totalSize() const override; |
| 312 | void write(llvm::FileOutputBuffer &Out) const override; |
| 313 | }; |
| 314 | |
| 315 | template <class ELFT> class BinaryObject : public Object<ELFT> { |
| 316 | private: |
| 317 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 318 | typedef std::unique_ptr<Segment> SegPtr; |
| 319 | |
| 320 | uint64_t TotalSize; |
| 321 | |
| 322 | public: |
| 323 | BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj) |
| 324 | : Object<ELFT>(Obj) {} |
| 325 | void finalize() override; |
| 326 | size_t totalSize() const override; |
| 327 | void write(llvm::FileOutputBuffer &Out) const override; |
| 328 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 329 | #endif |