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