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