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