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