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