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