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