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