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