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