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(); |
| 61 | template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const; |
| 62 | virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0; |
| 63 | }; |
| 64 | |
| 65 | class Segment { |
| 66 | private: |
| 67 | struct SectionCompare { |
| 68 | bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { |
| 69 | // Some sections might have the same address if one of them is empty. To |
| 70 | // fix this we can use the lexicographic ordering on ->Addr and the |
| 71 | // address of the actully stored section. |
| 72 | if (Lhs->OriginalOffset == Rhs->OriginalOffset) |
| 73 | return Lhs < Rhs; |
| 74 | return Lhs->OriginalOffset < Rhs->OriginalOffset; |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | std::set<const SectionBase *, SectionCompare> Sections; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 79 | llvm::ArrayRef<uint8_t> Contents; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 80 | |
| 81 | public: |
| 82 | uint64_t Align; |
| 83 | uint64_t FileSize; |
| 84 | uint32_t Flags; |
| 85 | uint32_t Index; |
| 86 | uint64_t MemSize; |
| 87 | uint64_t Offset; |
| 88 | uint64_t PAddr; |
| 89 | uint64_t Type; |
| 90 | uint64_t VAddr; |
| 91 | |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 92 | uint64_t OriginalOffset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 93 | Segment *ParentSegment = nullptr; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 94 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 95 | Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 96 | void finalize(); |
| 97 | const SectionBase *firstSection() const { |
| 98 | if (!Sections.empty()) |
| 99 | return *Sections.begin(); |
| 100 | return nullptr; |
| 101 | } |
| 102 | void addSection(const SectionBase *sec) { Sections.insert(sec); } |
| 103 | template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 104 | void writeSegment(llvm::FileOutputBuffer &Out) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | class Section : public SectionBase { |
| 108 | private: |
| 109 | llvm::ArrayRef<uint8_t> Contents; |
| 110 | |
| 111 | public: |
| 112 | Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 113 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 114 | }; |
| 115 | |
| 116 | // This is just a wraper around a StringTableBuilder that implements SectionBase |
| 117 | class StringTableSection : public SectionBase { |
| 118 | private: |
| 119 | llvm::StringTableBuilder StrTabBuilder; |
| 120 | |
| 121 | public: |
| 122 | StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) { |
| 123 | Type = llvm::ELF::SHT_STRTAB; |
| 124 | } |
| 125 | |
| 126 | void addString(llvm::StringRef Name); |
| 127 | uint32_t findIndex(llvm::StringRef Name) const; |
| 128 | void finalize() override; |
| 129 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 130 | static bool classof(const SectionBase *S) { |
| 131 | return S->Type == llvm::ELF::SHT_STRTAB; |
| 132 | } |
| 133 | }; |
| 134 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 135 | // Symbols have a st_shndx field that normally stores an index but occasionally |
| 136 | // stores a different special value. This enum keeps track of what the st_shndx |
| 137 | // field means. Most of the values are just copies of the special SHN_* values. |
| 138 | // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. |
| 139 | enum SymbolShndxType { |
| 140 | SYMBOL_SIMPLE_INDEX = 0, |
| 141 | SYMBOL_ABS = llvm::ELF::SHN_ABS, |
| 142 | SYMBOL_COMMON = llvm::ELF::SHN_COMMON, |
| 143 | SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON, |
| 144 | SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2, |
| 145 | SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4, |
| 146 | SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8, |
| 147 | }; |
| 148 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 149 | struct Symbol { |
| 150 | uint8_t Binding; |
| 151 | SectionBase *DefinedIn; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 152 | SymbolShndxType ShndxType; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 153 | uint32_t Index; |
| 154 | llvm::StringRef Name; |
| 155 | uint32_t NameIndex; |
| 156 | uint64_t Size; |
| 157 | uint8_t Type; |
| 158 | uint64_t Value; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 159 | |
| 160 | uint16_t getShndx() const; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | class SymbolTableSection : public SectionBase { |
| 164 | protected: |
| 165 | std::vector<std::unique_ptr<Symbol>> Symbols; |
| 166 | StringTableSection *SymbolNames; |
| 167 | |
| 168 | public: |
| 169 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
| 170 | void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 171 | SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx, |
| 172 | uint64_t Sz); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 173 | void addSymbolNames(); |
| 174 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 175 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 176 | void finalize() override; |
| 177 | static bool classof(const SectionBase *S) { |
| 178 | return S->Type == llvm::ELF::SHT_SYMTAB; |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | // Only writeSection depends on the ELF type so we implement it in a subclass. |
| 183 | template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection { |
| 184 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 185 | }; |
| 186 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 187 | struct Relocation { |
| 188 | const Symbol *RelocSymbol; |
| 189 | uint64_t Offset; |
| 190 | uint64_t Addend; |
| 191 | uint32_t Type; |
| 192 | }; |
| 193 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame^] | 194 | template <class SymTabType> class RelocationSectionBase : public SectionBase { |
| 195 | private: |
| 196 | SymTabType *Symbols; |
| 197 | SectionBase *SecToApplyRel; |
| 198 | |
| 199 | public: |
| 200 | void setSymTab(SymTabType *StrTab) { Symbols = StrTab; } |
| 201 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
| 202 | void initialize(SectionTableRef SecTable) override; |
| 203 | void finalize() override; |
| 204 | }; |
| 205 | |
| 206 | template <class ELFT> |
| 207 | class RelocationSection : public RelocationSectionBase<SymbolTableSection> { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 208 | private: |
| 209 | typedef typename ELFT::Rel Elf_Rel; |
| 210 | typedef typename ELFT::Rela Elf_Rela; |
| 211 | |
| 212 | std::vector<Relocation> Relocations; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 213 | |
| 214 | template <class T> void writeRel(T *Buf) const; |
| 215 | |
| 216 | public: |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 217 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 218 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame^] | 219 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 220 | static bool classof(const SectionBase *S) { |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame^] | 221 | if (S->Flags & llvm::ELF::SHF_ALLOC) |
| 222 | return false; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 223 | return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; |
| 224 | } |
| 225 | }; |
| 226 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 227 | class SectionWithStrTab : public Section { |
| 228 | private: |
| 229 | StringTableSection *StrTab; |
| 230 | |
| 231 | public: |
| 232 | SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {} |
| 233 | void setStrTab(StringTableSection *StringTable) { StrTab = StringTable; } |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 234 | void initialize(SectionTableRef SecTable) override; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 235 | void finalize() override; |
| 236 | static bool classof(const SectionBase *S); |
| 237 | }; |
| 238 | |
| 239 | class DynamicSymbolTableSection : public SectionWithStrTab { |
| 240 | public: |
| 241 | DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data) |
| 242 | : SectionWithStrTab(Data) {} |
| 243 | static bool classof(const SectionBase *S) { |
| 244 | return S->Type == llvm::ELF::SHT_DYNSYM; |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | class DynamicSection : public SectionWithStrTab { |
| 249 | public: |
| 250 | DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
| 251 | static bool classof(const SectionBase *S) { |
| 252 | return S->Type == llvm::ELF::SHT_DYNAMIC; |
| 253 | } |
| 254 | }; |
| 255 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame^] | 256 | class DynamicRelocationSection |
| 257 | : public RelocationSectionBase<DynamicSymbolTableSection> { |
| 258 | private: |
| 259 | llvm::ArrayRef<uint8_t> Contents; |
| 260 | |
| 261 | public: |
| 262 | DynamicRelocationSection(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 263 | void writeSection(llvm::FileOutputBuffer &Out) const override; |
| 264 | static bool classof(const SectionBase *S) { |
| 265 | if (!(S->Flags & llvm::ELF::SHF_ALLOC)) |
| 266 | return false; |
| 267 | return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA; |
| 268 | } |
| 269 | }; |
| 270 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 271 | template <class ELFT> class Object { |
| 272 | private: |
| 273 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 274 | typedef std::unique_ptr<Segment> SegPtr; |
| 275 | |
| 276 | typedef typename ELFT::Shdr Elf_Shdr; |
| 277 | typedef typename ELFT::Ehdr Elf_Ehdr; |
| 278 | typedef typename ELFT::Phdr Elf_Phdr; |
| 279 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 280 | void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile, |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 281 | SymbolTableSection *SymTab, SectionTableRef SecTable); |
Petr Hosek | b1bb3e5 | 2017-08-04 05:33:44 +0000 | [diff] [blame] | 282 | SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile, |
| 283 | const Elf_Shdr &Shdr); |
| 284 | void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 285 | SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 286 | |
| 287 | protected: |
| 288 | StringTableSection *SectionNames; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 289 | SymbolTableSection *SymbolTable; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 290 | std::vector<SecPtr> Sections; |
| 291 | std::vector<SegPtr> Segments; |
| 292 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 293 | void writeHeader(llvm::FileOutputBuffer &Out) const; |
| 294 | void writeProgramHeaders(llvm::FileOutputBuffer &Out) const; |
| 295 | void writeSectionData(llvm::FileOutputBuffer &Out) const; |
| 296 | void writeSectionHeaders(llvm::FileOutputBuffer &Out) const; |
| 297 | |
| 298 | public: |
| 299 | uint8_t Ident[16]; |
| 300 | uint64_t Entry; |
| 301 | uint64_t SHOffset; |
| 302 | uint32_t Type; |
| 303 | uint32_t Machine; |
| 304 | uint32_t Version; |
| 305 | uint32_t Flags; |
| 306 | |
| 307 | Object(const llvm::object::ELFObjectFile<ELFT> &Obj); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 308 | virtual size_t totalSize() const = 0; |
| 309 | virtual void finalize() = 0; |
| 310 | virtual void write(llvm::FileOutputBuffer &Out) const = 0; |
| 311 | virtual ~Object() = default; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 314 | template <class ELFT> class ELFObject : public Object<ELFT> { |
| 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 | |
| 323 | void sortSections(); |
| 324 | void assignOffsets(); |
| 325 | |
| 326 | public: |
| 327 | ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 328 | void finalize() override; |
| 329 | size_t totalSize() const override; |
| 330 | void write(llvm::FileOutputBuffer &Out) const override; |
| 331 | }; |
| 332 | |
| 333 | template <class ELFT> class BinaryObject : public Object<ELFT> { |
| 334 | private: |
| 335 | typedef std::unique_ptr<SectionBase> SecPtr; |
| 336 | typedef std::unique_ptr<Segment> SegPtr; |
| 337 | |
| 338 | uint64_t TotalSize; |
| 339 | |
| 340 | public: |
| 341 | BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj) |
| 342 | : Object<ELFT>(Obj) {} |
| 343 | void finalize() override; |
| 344 | size_t totalSize() const override; |
| 345 | void write(llvm::FileOutputBuffer &Out) const override; |
| 346 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 347 | #endif |