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 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 10 | #ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H |
| 11 | #define LLVM_TOOLS_OBJCOPY_OBJECT_H |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 12 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/ArrayRef.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | #include "llvm/BinaryFormat/ELF.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 17 | #include "llvm/MC/StringTableBuilder.h" |
| 18 | #include "llvm/Object/ELFObjectFile.h" |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileOutputBuffer.h" |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 20 | #include "llvm/Support/JamCRC.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 21 | #include <cstddef> |
| 22 | #include <cstdint> |
| 23 | #include <functional> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 24 | #include <memory> |
| 25 | #include <set> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 26 | #include <vector> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 27 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 30 | class SectionBase; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 31 | class Section; |
| 32 | class OwnedDataSection; |
| 33 | class StringTableSection; |
| 34 | class SymbolTableSection; |
| 35 | class RelocationSection; |
| 36 | class DynamicRelocationSection; |
| 37 | class GnuDebugLinkSection; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 38 | class Segment; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 39 | class Object; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 40 | |
| 41 | class SectionTableRef { |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 42 | private: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 43 | MutableArrayRef<std::unique_ptr<SectionBase>> Sections; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 44 | |
| 45 | public: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 46 | using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>; |
| 47 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 48 | SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs) |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 49 | : Sections(Secs) {} |
| 50 | SectionTableRef(const SectionTableRef &) = default; |
| 51 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 52 | iterator begin() { return iterator(Sections.data()); } |
| 53 | iterator end() { return iterator(Sections.data() + Sections.size()); } |
| 54 | |
Jake Ehrlich | 8b831c1 | 2018-03-07 20:33:02 +0000 | [diff] [blame] | 55 | SectionBase *getSection(uint16_t Index, Twine ErrMsg); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 56 | |
| 57 | template <class T> |
Jake Ehrlich | 8b831c1 | 2018-03-07 20:33:02 +0000 | [diff] [blame] | 58 | T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 59 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 60 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 61 | enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE }; |
| 62 | |
| 63 | class SectionVisitor { |
| 64 | public: |
| 65 | virtual ~SectionVisitor(); |
| 66 | |
| 67 | virtual void visit(const Section &Sec) = 0; |
| 68 | virtual void visit(const OwnedDataSection &Sec) = 0; |
| 69 | virtual void visit(const StringTableSection &Sec) = 0; |
| 70 | virtual void visit(const SymbolTableSection &Sec) = 0; |
| 71 | virtual void visit(const RelocationSection &Sec) = 0; |
| 72 | virtual void visit(const DynamicRelocationSection &Sec) = 0; |
| 73 | virtual void visit(const GnuDebugLinkSection &Sec) = 0; |
| 74 | }; |
| 75 | |
| 76 | class SectionWriter : public SectionVisitor { |
| 77 | protected: |
| 78 | FileOutputBuffer &Out; |
| 79 | |
| 80 | public: |
| 81 | virtual ~SectionWriter(){}; |
| 82 | |
| 83 | void visit(const Section &Sec) override; |
| 84 | void visit(const OwnedDataSection &Sec) override; |
| 85 | void visit(const StringTableSection &Sec) override; |
| 86 | void visit(const DynamicRelocationSection &Sec) override; |
| 87 | virtual void visit(const SymbolTableSection &Sec) override = 0; |
| 88 | virtual void visit(const RelocationSection &Sec) override = 0; |
| 89 | virtual void visit(const GnuDebugLinkSection &Sec) override = 0; |
| 90 | |
| 91 | SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {} |
| 92 | }; |
| 93 | |
| 94 | template <class ELFT> class ELFSectionWriter : public SectionWriter { |
| 95 | private: |
| 96 | using Elf_Word = typename ELFT::Word; |
| 97 | using Elf_Rel = typename ELFT::Rel; |
| 98 | using Elf_Rela = typename ELFT::Rela; |
| 99 | |
| 100 | public: |
| 101 | virtual ~ELFSectionWriter() {} |
| 102 | void visit(const SymbolTableSection &Sec) override; |
| 103 | void visit(const RelocationSection &Sec) override; |
| 104 | void visit(const GnuDebugLinkSection &Sec) override; |
| 105 | |
| 106 | ELFSectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {} |
| 107 | }; |
| 108 | |
| 109 | #define MAKE_SEC_WRITER_FRIEND \ |
| 110 | friend class SectionWriter; \ |
| 111 | template <class ELFT> friend class ELFSectionWriter; |
| 112 | |
| 113 | class BinarySectionWriter : public SectionWriter { |
| 114 | public: |
| 115 | virtual ~BinarySectionWriter() {} |
| 116 | |
| 117 | void visit(const SymbolTableSection &Sec) override; |
| 118 | void visit(const RelocationSection &Sec) override; |
| 119 | void visit(const GnuDebugLinkSection &Sec) override; |
| 120 | BinarySectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {} |
| 121 | }; |
| 122 | |
| 123 | class Writer { |
| 124 | protected: |
| 125 | StringRef File; |
| 126 | Object &Obj; |
| 127 | std::unique_ptr<FileOutputBuffer> BufPtr; |
| 128 | |
| 129 | void createBuffer(uint64_t Size); |
| 130 | |
| 131 | public: |
| 132 | virtual ~Writer(); |
| 133 | |
| 134 | virtual void finalize() = 0; |
| 135 | virtual void write() = 0; |
| 136 | |
| 137 | Writer(StringRef File, Object &Obj) : File(File), Obj(Obj) {} |
| 138 | }; |
| 139 | |
| 140 | template <class ELFT> class ELFWriter : public Writer { |
| 141 | private: |
| 142 | using Elf_Shdr = typename ELFT::Shdr; |
| 143 | using Elf_Phdr = typename ELFT::Phdr; |
| 144 | using Elf_Ehdr = typename ELFT::Ehdr; |
| 145 | |
| 146 | void writeEhdr(); |
| 147 | void writePhdr(const Segment &Seg); |
| 148 | void writeShdr(const SectionBase &Sec); |
| 149 | |
| 150 | void writePhdrs(); |
| 151 | void writeShdrs(); |
| 152 | void writeSectionData(); |
| 153 | |
| 154 | void assignOffsets(); |
| 155 | |
| 156 | std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter; |
| 157 | |
| 158 | size_t totalSize() const; |
| 159 | |
| 160 | public: |
| 161 | virtual ~ELFWriter() {} |
| 162 | bool WriteSectionHeaders = true; |
| 163 | |
| 164 | void finalize() override; |
| 165 | void write() override; |
| 166 | ELFWriter(StringRef File, Object &Obj, bool WSH) |
| 167 | : Writer(File, Obj), WriteSectionHeaders(WSH) {} |
| 168 | }; |
| 169 | |
| 170 | class BinaryWriter : public Writer { |
| 171 | private: |
| 172 | std::unique_ptr<BinarySectionWriter> SecWriter; |
| 173 | |
| 174 | uint64_t TotalSize; |
| 175 | |
| 176 | public: |
| 177 | ~BinaryWriter() {} |
| 178 | void finalize() override; |
| 179 | void write() override; |
| 180 | BinaryWriter(StringRef File, Object &Obj) : Writer(File, Obj) {} |
| 181 | }; |
| 182 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 183 | class SectionBase { |
| 184 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 185 | StringRef Name; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 186 | Segment *ParentSegment = nullptr; |
| 187 | uint64_t HeaderOffset; |
| 188 | uint64_t OriginalOffset; |
| 189 | uint32_t Index; |
| 190 | |
| 191 | uint64_t Addr = 0; |
| 192 | uint64_t Align = 1; |
| 193 | uint32_t EntrySize = 0; |
| 194 | uint64_t Flags = 0; |
| 195 | uint64_t Info = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 196 | uint64_t Link = ELF::SHN_UNDEF; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 197 | uint64_t NameIndex = 0; |
| 198 | uint64_t Offset = 0; |
| 199 | uint64_t Size = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 200 | uint64_t Type = ELF::SHT_NULL; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 201 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 202 | virtual ~SectionBase() = default; |
| 203 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 204 | virtual void initialize(SectionTableRef SecTable); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 205 | virtual void finalize(); |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 206 | virtual void removeSectionReferences(const SectionBase *Sec); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 207 | virtual void accept(SectionVisitor &Visitor) const = 0; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | class Segment { |
| 211 | private: |
| 212 | struct SectionCompare { |
| 213 | bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { |
| 214 | // Some sections might have the same address if one of them is empty. To |
| 215 | // fix this we can use the lexicographic ordering on ->Addr and the |
| 216 | // address of the actully stored section. |
| 217 | if (Lhs->OriginalOffset == Rhs->OriginalOffset) |
| 218 | return Lhs < Rhs; |
| 219 | return Lhs->OriginalOffset < Rhs->OriginalOffset; |
| 220 | } |
| 221 | }; |
| 222 | |
| 223 | std::set<const SectionBase *, SectionCompare> Sections; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 224 | ArrayRef<uint8_t> Contents; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 225 | |
| 226 | public: |
| 227 | uint64_t Align; |
| 228 | uint64_t FileSize; |
| 229 | uint32_t Flags; |
| 230 | uint32_t Index; |
| 231 | uint64_t MemSize; |
| 232 | uint64_t Offset; |
| 233 | uint64_t PAddr; |
| 234 | uint64_t Type; |
| 235 | uint64_t VAddr; |
| 236 | |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 237 | uint64_t OriginalOffset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 238 | Segment *ParentSegment = nullptr; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 239 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 240 | Segment(ArrayRef<uint8_t> Data) : Contents(Data) {} |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 241 | Segment() {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 242 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 243 | const SectionBase *firstSection() const { |
| 244 | if (!Sections.empty()) |
| 245 | return *Sections.begin(); |
| 246 | return nullptr; |
| 247 | } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 248 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 249 | void removeSection(const SectionBase *Sec) { Sections.erase(Sec); } |
| 250 | void addSection(const SectionBase *Sec) { Sections.insert(Sec); } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | class Section : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 254 | MAKE_SEC_WRITER_FRIEND |
| 255 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 256 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 257 | ArrayRef<uint8_t> Contents; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 258 | |
| 259 | public: |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 260 | Section(ArrayRef<uint8_t> Data) : Contents(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 261 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 262 | void accept(SectionVisitor &Visitor) const override; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 265 | class OwnedDataSection : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 266 | MAKE_SEC_WRITER_FRIEND |
| 267 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 268 | private: |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 269 | std::vector<uint8_t> Data; |
| 270 | |
| 271 | public: |
| 272 | OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data) |
| 273 | : Data(std::begin(Data), std::end(Data)) { |
| 274 | Name = SecName; |
| 275 | Type = ELF::SHT_PROGBITS; |
| 276 | Size = Data.size(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 277 | OriginalOffset = std::numeric_limits<uint64_t>::max(); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 278 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 279 | |
| 280 | void accept(SectionVisitor &Sec) const override; |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 283 | // There are two types of string tables that can exist, dynamic and not dynamic. |
| 284 | // In the dynamic case the string table is allocated. Changing a dynamic string |
| 285 | // table would mean altering virtual addresses and thus the memory image. So |
| 286 | // dynamic string tables should not have an interface to modify them or |
| 287 | // reconstruct them. This type lets us reconstruct a string table. To avoid |
| 288 | // this class being used for dynamic string tables (which has happened) the |
| 289 | // classof method checks that the particular instance is not allocated. This |
| 290 | // then agrees with the makeSection method used to construct most sections. |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 291 | class StringTableSection : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 292 | MAKE_SEC_WRITER_FRIEND |
| 293 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 294 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 295 | StringTableBuilder StrTabBuilder; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 296 | |
| 297 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 298 | StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) { |
| 299 | Type = ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 302 | void addString(StringRef Name); |
| 303 | uint32_t findIndex(StringRef Name) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 304 | void finalize() override; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 305 | void accept(SectionVisitor &Visitor) const override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 306 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 307 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 308 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 309 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 310 | return S->Type == ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 311 | } |
| 312 | }; |
| 313 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 314 | // Symbols have a st_shndx field that normally stores an index but occasionally |
| 315 | // stores a different special value. This enum keeps track of what the st_shndx |
| 316 | // field means. Most of the values are just copies of the special SHN_* values. |
| 317 | // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. |
| 318 | enum SymbolShndxType { |
| 319 | SYMBOL_SIMPLE_INDEX = 0, |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 320 | SYMBOL_ABS = ELF::SHN_ABS, |
| 321 | SYMBOL_COMMON = ELF::SHN_COMMON, |
| 322 | SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON, |
| 323 | SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2, |
| 324 | SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4, |
| 325 | SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 326 | }; |
| 327 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 328 | struct Symbol { |
| 329 | uint8_t Binding; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 330 | SectionBase *DefinedIn = nullptr; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 331 | SymbolShndxType ShndxType; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 332 | uint32_t Index; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 333 | StringRef Name; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 334 | uint32_t NameIndex; |
| 335 | uint64_t Size; |
| 336 | uint8_t Type; |
| 337 | uint64_t Value; |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 338 | uint8_t Visibility; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 339 | |
| 340 | uint16_t getShndx() const; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 341 | }; |
| 342 | |
| 343 | class SymbolTableSection : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 344 | MAKE_SEC_WRITER_FRIEND |
| 345 | |
Alexander Shaposhnikov | a8f1550 | 2018-02-24 00:41:01 +0000 | [diff] [blame] | 346 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
| 347 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 348 | protected: |
| 349 | std::vector<std::unique_ptr<Symbol>> Symbols; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 350 | StringTableSection *SymbolNames = nullptr; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 351 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 352 | using SymPtr = std::unique_ptr<Symbol>; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 353 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 354 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 355 | void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type, |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 356 | SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility, |
| 357 | uint16_t Shndx, uint64_t Sz); |
Jake Ehrlich | 8b831c1 | 2018-03-07 20:33:02 +0000 | [diff] [blame] | 358 | void addSymbolNames(); |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 359 | const SectionBase *getStrTab() const { return SymbolNames; } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 360 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 361 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 362 | void localize(std::function<bool(const Symbol &)> ToLocalize); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 363 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 364 | void finalize() override; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 365 | void accept(SectionVisitor &Visitor) const override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 366 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 367 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 368 | return S->Type == ELF::SHT_SYMTAB; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 369 | } |
| 370 | }; |
| 371 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 372 | struct Relocation { |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 373 | const Symbol *RelocSymbol = nullptr; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 374 | uint64_t Offset; |
| 375 | uint64_t Addend; |
| 376 | uint32_t Type; |
| 377 | }; |
| 378 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 379 | // All relocation sections denote relocations to apply to another section. |
| 380 | // However, some relocation sections use a dynamic symbol table and others use |
| 381 | // a regular symbol table. Because the types of the two symbol tables differ in |
| 382 | // our system (because they should behave differently) we can't uniformly |
| 383 | // represent all relocations with the same base class if we expose an interface |
| 384 | // that mentions the symbol table type. So we split the two base types into two |
| 385 | // different classes, one which handles the section the relocation is applied to |
| 386 | // and another which handles the symbol table type. The symbol table type is |
| 387 | // taken as a type parameter to the class (see RelocSectionWithSymtabBase). |
| 388 | class RelocationSectionBase : public SectionBase { |
| 389 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 390 | SectionBase *SecToApplyRel = nullptr; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 391 | |
| 392 | public: |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 393 | const SectionBase *getSection() const { return SecToApplyRel; } |
Jake Ehrlich | c5ff727 | 2017-10-10 18:32:22 +0000 | [diff] [blame] | 394 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 395 | |
| 396 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 397 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 398 | } |
| 399 | }; |
| 400 | |
| 401 | // Takes the symbol table type to use as a parameter so that we can deduplicate |
| 402 | // that code between the two symbol table types. |
| 403 | template <class SymTabType> |
| 404 | class RelocSectionWithSymtabBase : public RelocationSectionBase { |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 405 | private: |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 406 | SymTabType *Symbols = nullptr; |
Alexander Shaposhnikov | a8f1550 | 2018-02-24 00:41:01 +0000 | [diff] [blame] | 407 | void setSymTab(SymTabType *SymTab) { Symbols = SymTab; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 408 | |
| 409 | protected: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 410 | RelocSectionWithSymtabBase() = default; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 411 | |
| 412 | public: |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 413 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 414 | void initialize(SectionTableRef SecTable) override; |
| 415 | void finalize() override; |
| 416 | }; |
| 417 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 418 | class RelocationSection |
| 419 | : public RelocSectionWithSymtabBase<SymbolTableSection> { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 420 | MAKE_SEC_WRITER_FRIEND |
| 421 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 422 | private: |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 423 | std::vector<Relocation> Relocations; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 424 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 425 | public: |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 426 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 427 | void accept(SectionVisitor &Visitor) const override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 428 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 429 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 430 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 431 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 432 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 433 | } |
| 434 | }; |
| 435 | |
Alexander Shaposhnikov | 43b8acd | 2018-03-20 18:20:42 +0000 | [diff] [blame] | 436 | class SectionWithStrTab : public Section { |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 437 | private: |
Alexander Shaposhnikov | 43b8acd | 2018-03-20 18:20:42 +0000 | [diff] [blame] | 438 | const SectionBase *StrTab = nullptr; |
Alexander Shaposhnikov | 43b8acd | 2018-03-20 18:20:42 +0000 | [diff] [blame] | 439 | |
| 440 | public: |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 441 | SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {} |
| 442 | |
| 443 | void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 444 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 445 | void initialize(SectionTableRef SecTable) override; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 446 | void finalize() override; |
| 447 | static bool classof(const SectionBase *S); |
| 448 | }; |
| 449 | |
| 450 | class DynamicSymbolTableSection : public SectionWithStrTab { |
| 451 | public: |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 452 | DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 453 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 454 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 455 | return S->Type == ELF::SHT_DYNSYM; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 456 | } |
| 457 | }; |
| 458 | |
| 459 | class DynamicSection : public SectionWithStrTab { |
| 460 | public: |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 461 | DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 462 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 463 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 464 | return S->Type == ELF::SHT_DYNAMIC; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 465 | } |
| 466 | }; |
| 467 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 468 | class DynamicRelocationSection |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 469 | : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 470 | MAKE_SEC_WRITER_FRIEND |
| 471 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 472 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 473 | ArrayRef<uint8_t> Contents; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 474 | |
| 475 | public: |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 476 | DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 477 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 478 | void accept(SectionVisitor &) const override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 479 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 480 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 481 | if (!(S->Flags & ELF::SHF_ALLOC)) |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 482 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 483 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 484 | } |
| 485 | }; |
| 486 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 487 | class GnuDebugLinkSection : public SectionBase { |
| 488 | MAKE_SEC_WRITER_FRIEND |
| 489 | |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 490 | private: |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 491 | |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 492 | StringRef FileName; |
| 493 | uint32_t CRC32; |
| 494 | |
| 495 | void init(StringRef File, StringRef Data); |
| 496 | |
| 497 | public: |
| 498 | // If we add this section from an external source we can use this ctor. |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 499 | GnuDebugLinkSection(StringRef File); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 500 | void accept(SectionVisitor &Visitor) const override; |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 501 | }; |
| 502 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 503 | class Reader { |
| 504 | public: |
| 505 | virtual ~Reader(); |
| 506 | virtual std::unique_ptr<Object> create() const = 0; |
| 507 | }; |
| 508 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 509 | using object::OwningBinary; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 510 | using object::Binary; |
| 511 | using object::ELFFile; |
| 512 | using object::ELFObjectFile; |
| 513 | |
| 514 | template <class ELFT> class ELFBuilder { |
| 515 | private: |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 516 | using Elf_Addr = typename ELFT::Addr; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 517 | using Elf_Shdr = typename ELFT::Shdr; |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 518 | using Elf_Ehdr = typename ELFT::Ehdr; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 519 | |
| 520 | const ELFFile<ELFT> &ElfFile; |
| 521 | Object &Obj; |
| 522 | |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 523 | void setParentSegment(Segment &Child); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 524 | void readProgramHeaders(); |
| 525 | void initSymbolTable(SymbolTableSection *SymTab); |
| 526 | void readSectionHeaders(); |
| 527 | SectionBase &makeSection(const Elf_Shdr &Shdr); |
| 528 | |
| 529 | public: |
| 530 | ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj) |
| 531 | : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {} |
| 532 | |
| 533 | void build(); |
| 534 | }; |
| 535 | |
| 536 | class ELFReader : public Reader { |
| 537 | private: |
Jake Ehrlich | 9634e18 | 2018-01-26 02:01:37 +0000 | [diff] [blame] | 538 | std::unique_ptr<Binary> Bin; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 539 | std::shared_ptr<MemoryBuffer> Data; |
| 540 | |
| 541 | public: |
| 542 | ElfType getElfType() const; |
| 543 | std::unique_ptr<Object> create() const override; |
| 544 | ELFReader(StringRef File); |
| 545 | }; |
| 546 | |
| 547 | class Object { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 548 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 549 | using SecPtr = std::unique_ptr<SectionBase>; |
| 550 | using SegPtr = std::unique_ptr<Segment>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 551 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 552 | std::shared_ptr<MemoryBuffer> OwnedData; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 553 | std::vector<SecPtr> Sections; |
| 554 | std::vector<SegPtr> Segments; |
| 555 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 556 | public: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 557 | template <class T> |
| 558 | using Range = iterator_range< |
| 559 | pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>; |
| 560 | |
| 561 | template <class T> |
| 562 | using ConstRange = iterator_range<pointee_iterator< |
| 563 | typename std::vector<std::unique_ptr<T>>::const_iterator>>; |
| 564 | |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 565 | // It is often the case that the ELF header and the program header table are |
| 566 | // not present in any segment. This could be a problem during file layout, |
| 567 | // because other segments may get assigned an offset where either of the |
| 568 | // two should reside, which will effectively corrupt the resulting binary. |
| 569 | // Other than that we use these segments to track program header offsets |
| 570 | // when they may not follow the ELF header. |
| 571 | Segment ElfHdrSegment; |
| 572 | Segment ProgramHdrSegment; |
| 573 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 574 | uint8_t Ident[16]; |
| 575 | uint64_t Entry; |
| 576 | uint64_t SHOffset; |
| 577 | uint32_t Type; |
| 578 | uint32_t Machine; |
| 579 | uint32_t Version; |
| 580 | uint32_t Flags; |
| 581 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 582 | StringTableSection *SectionNames = nullptr; |
| 583 | SymbolTableSection *SymbolTable = nullptr; |
| 584 | |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame^] | 585 | Object(std::shared_ptr<MemoryBuffer> Data) : OwnedData(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 586 | virtual ~Object() = default; |
| 587 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 588 | void sortSections(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 589 | SectionTableRef sections() { return SectionTableRef(Sections); } |
| 590 | ConstRange<SectionBase> sections() const { |
| 591 | return make_pointee_range(Sections); |
| 592 | } |
| 593 | Range<Segment> segments() { return make_pointee_range(Segments); } |
| 594 | ConstRange<Segment> segments() const { return make_pointee_range(Segments); } |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 595 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 596 | void removeSections(std::function<bool(const SectionBase &)> ToRemove); |
| 597 | template <class T, class... Ts> T &addSection(Ts &&... Args) { |
| 598 | auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...); |
| 599 | auto Ptr = Sec.get(); |
| 600 | Sections.emplace_back(std::move(Sec)); |
| 601 | return *Ptr; |
| 602 | } |
| 603 | Segment &addSegment(ArrayRef<uint8_t> Data) { |
| 604 | Segments.emplace_back(llvm::make_unique<Segment>(Data)); |
| 605 | return *Segments.back(); |
| 606 | } |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 607 | }; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 608 | |
| 609 | } // end namespace llvm |
| 610 | |
| 611 | #endif // LLVM_TOOLS_OBJCOPY_OBJECT_H |