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