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