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