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" |
Eugene Leviant | a6fb183 | 2019-05-29 11:37:16 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Errc.h" |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileOutputBuffer.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 22 | #include <cstddef> |
| 23 | #include <cstdint> |
| 24 | #include <functional> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <set> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 27 | #include <vector> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 28 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 29 | namespace llvm { |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 30 | enum class DebugCompressionType; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 31 | namespace objcopy { |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 32 | namespace elf { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 33 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 34 | class SectionBase; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 35 | class Section; |
| 36 | class OwnedDataSection; |
| 37 | class StringTableSection; |
| 38 | class SymbolTableSection; |
| 39 | class RelocationSection; |
| 40 | class DynamicRelocationSection; |
| 41 | class GnuDebugLinkSection; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 42 | class GroupSection; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 43 | class SectionIndexSection; |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 44 | class CompressedSection; |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 45 | class DecompressedSection; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 46 | class Segment; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 47 | class Object; |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 48 | struct Symbol; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 49 | |
| 50 | class SectionTableRef { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 51 | MutableArrayRef<std::unique_ptr<SectionBase>> Sections; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 52 | |
| 53 | public: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 54 | using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>; |
| 55 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 56 | explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs) |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 57 | : Sections(Secs) {} |
| 58 | SectionTableRef(const SectionTableRef &) = default; |
| 59 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 60 | iterator begin() { return iterator(Sections.data()); } |
| 61 | iterator end() { return iterator(Sections.data() + Sections.size()); } |
Fangrui Song | 82b01e0 | 2019-03-30 14:08:59 +0000 | [diff] [blame] | 62 | size_t size() const { return Sections.size(); } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 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: |
Fangrui Song | a85bf87 | 2019-03-15 10:20:51 +0000 | [diff] [blame] | 111 | virtual ~SectionWriter() = default; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 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; \ |
Eugene Leviant | a6fb183 | 2019-05-29 11:37:16 +0000 | [diff] [blame] | 172 | friend class IHexSectionWriterBase; \ |
| 173 | friend class IHexSectionWriter; \ |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 174 | template <class ELFT> friend class ELFSectionWriter; \ |
| 175 | template <class ELFT> friend class ELFSectionSizer; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 176 | |
| 177 | class BinarySectionWriter : public SectionWriter { |
| 178 | public: |
| 179 | virtual ~BinarySectionWriter() {} |
| 180 | |
| 181 | void visit(const SymbolTableSection &Sec) override; |
| 182 | void visit(const RelocationSection &Sec) override; |
| 183 | void visit(const GnuDebugLinkSection &Sec) override; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 184 | void visit(const GroupSection &Sec) override; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 185 | void visit(const SectionIndexSection &Sec) override; |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 186 | void visit(const CompressedSection &Sec) override; |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 187 | void visit(const DecompressedSection &Sec) override; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 188 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 189 | explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {} |
| 190 | }; |
| 191 | |
Eugene Leviant | a6fb183 | 2019-05-29 11:37:16 +0000 | [diff] [blame] | 192 | using IHexLineData = SmallVector<char, 64>; |
| 193 | |
| 194 | struct IHexRecord { |
| 195 | // Memory address of the record. |
| 196 | uint16_t Addr; |
| 197 | // Record type (see below). |
| 198 | uint16_t Type; |
| 199 | // Record data in hexadecimal form. |
| 200 | StringRef HexData; |
| 201 | |
| 202 | // Helper method to get file length of the record |
| 203 | // including newline character |
| 204 | static size_t getLength(size_t DataSize) { |
| 205 | // :LLAAAATT[DD...DD]CC' |
| 206 | return DataSize * 2 + 11; |
| 207 | } |
| 208 | |
| 209 | // Gets length of line in a file (getLength + CRLF). |
| 210 | static size_t getLineLength(size_t DataSize) { |
| 211 | return getLength(DataSize) + 2; |
| 212 | } |
| 213 | |
| 214 | // Given type, address and data returns line which can |
| 215 | // be written to output file. |
| 216 | static IHexLineData getLine(uint8_t Type, uint16_t Addr, |
| 217 | ArrayRef<uint8_t> Data); |
| 218 | |
| 219 | // Calculates checksum of stringified record representation |
| 220 | // S must NOT contain leading ':' and trailing whitespace |
| 221 | // characters |
| 222 | static uint8_t getChecksum(StringRef S); |
| 223 | |
| 224 | enum Type { |
| 225 | // Contains data and a 16-bit starting address for the data. |
| 226 | // The byte count specifies number of data bytes in the record. |
| 227 | Data = 0, |
| 228 | // Must occur exactly once per file in the last line of the file. |
| 229 | // The data field is empty (thus byte count is 00) and the address |
| 230 | // field is typically 0000. |
| 231 | EndOfFile = 1, |
| 232 | // The data field contains a 16-bit segment base address (thus byte |
| 233 | // count is always 02) compatible with 80x86 real mode addressing. |
| 234 | // The address field (typically 0000) is ignored. The segment address |
| 235 | // from the most recent 02 record is multiplied by 16 and added to each |
| 236 | // subsequent data record address to form the physical starting address |
| 237 | // for the data. This allows addressing up to one megabyte of address |
| 238 | // space. |
| 239 | SegmentAddr = 2, |
| 240 | // or 80x86 processors, specifies the initial content of the CS:IP |
| 241 | // registers. The address field is 0000, the byte count is always 04, |
| 242 | // the first two data bytes are the CS value, the latter two are the |
| 243 | // IP value. |
| 244 | StartAddr80x86 = 3, |
| 245 | // Allows for 32 bit addressing (up to 4GiB). The record's address field |
| 246 | // is ignored (typically 0000) and its byte count is always 02. The two |
| 247 | // data bytes (big endian) specify the upper 16 bits of the 32 bit |
| 248 | // absolute address for all subsequent type 00 records |
| 249 | ExtendedAddr = 4, |
| 250 | // The address field is 0000 (not used) and the byte count is always 04. |
| 251 | // The four data bytes represent a 32-bit address value. In the case of |
| 252 | // 80386 and higher CPUs, this address is loaded into the EIP register. |
| 253 | StartAddr = 5, |
| 254 | // We have no other valid types |
| 255 | InvalidType = 6 |
| 256 | }; |
| 257 | }; |
| 258 | |
| 259 | // Base class for IHexSectionWriter. This class implements writing algorithm, |
| 260 | // but doesn't actually write records. It is used for output buffer size |
| 261 | // calculation in IHexWriter::finalize. |
| 262 | class IHexSectionWriterBase : public BinarySectionWriter { |
| 263 | // 20-bit segment address |
| 264 | uint32_t SegmentAddr = 0; |
| 265 | // Extended linear address |
| 266 | uint32_t BaseAddr = 0; |
| 267 | |
| 268 | // Write segment address corresponding to 'Addr' |
| 269 | uint64_t writeSegmentAddr(uint64_t Addr); |
| 270 | // Write extended linear (base) address corresponding to 'Addr' |
| 271 | uint64_t writeBaseAddr(uint64_t Addr); |
| 272 | |
| 273 | protected: |
| 274 | // Offset in the output buffer |
| 275 | uint64_t Offset = 0; |
| 276 | |
| 277 | void writeSection(const SectionBase *Sec, ArrayRef<uint8_t> Data); |
| 278 | virtual void writeData(uint8_t Type, uint16_t Addr, ArrayRef<uint8_t> Data); |
| 279 | |
| 280 | public: |
| 281 | explicit IHexSectionWriterBase(Buffer &Buf) : BinarySectionWriter(Buf) {} |
| 282 | |
| 283 | uint64_t getBufferOffset() const { return Offset; } |
| 284 | void visit(const Section &Sec) final; |
| 285 | void visit(const OwnedDataSection &Sec) final; |
| 286 | void visit(const StringTableSection &Sec) override; |
| 287 | void visit(const DynamicRelocationSection &Sec) final; |
| 288 | using BinarySectionWriter::visit; |
| 289 | }; |
| 290 | |
| 291 | // Real IHEX section writer |
| 292 | class IHexSectionWriter : public IHexSectionWriterBase { |
| 293 | public: |
| 294 | IHexSectionWriter(Buffer &Buf) : IHexSectionWriterBase(Buf) {} |
| 295 | |
| 296 | void writeData(uint8_t Type, uint16_t Addr, ArrayRef<uint8_t> Data) override; |
| 297 | void visit(const StringTableSection &Sec) override; |
| 298 | }; |
| 299 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 300 | class Writer { |
| 301 | protected: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 302 | Object &Obj; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 303 | Buffer &Buf; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 304 | |
| 305 | public: |
| 306 | virtual ~Writer(); |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 307 | virtual Error finalize() = 0; |
| 308 | virtual Error write() = 0; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 309 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 310 | Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {} |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | template <class ELFT> class ELFWriter : public Writer { |
| 314 | private: |
Jordan Rupprecht | de965ea | 2018-08-10 16:25:58 +0000 | [diff] [blame] | 315 | using Elf_Addr = typename ELFT::Addr; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 316 | using Elf_Shdr = typename ELFT::Shdr; |
| 317 | using Elf_Phdr = typename ELFT::Phdr; |
| 318 | using Elf_Ehdr = typename ELFT::Ehdr; |
| 319 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 320 | void initEhdrSegment(); |
| 321 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 322 | void writeEhdr(); |
| 323 | void writePhdr(const Segment &Seg); |
| 324 | void writeShdr(const SectionBase &Sec); |
| 325 | |
| 326 | void writePhdrs(); |
| 327 | void writeShdrs(); |
| 328 | void writeSectionData(); |
James Henderson | 1f44814 | 2019-03-25 16:36:26 +0000 | [diff] [blame] | 329 | void writeSegmentData(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 330 | |
| 331 | void assignOffsets(); |
| 332 | |
| 333 | std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter; |
| 334 | |
| 335 | size_t totalSize() const; |
| 336 | |
| 337 | public: |
| 338 | virtual ~ELFWriter() {} |
James Henderson | 38cb238 | 2019-04-02 14:11:13 +0000 | [diff] [blame] | 339 | bool WriteSectionHeaders; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 340 | |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 341 | Error finalize() override; |
| 342 | Error write() override; |
James Henderson | 38cb238 | 2019-04-02 14:11:13 +0000 | [diff] [blame] | 343 | ELFWriter(Object &Obj, Buffer &Buf, bool WSH); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | class BinaryWriter : public Writer { |
| 347 | private: |
| 348 | std::unique_ptr<BinarySectionWriter> SecWriter; |
| 349 | |
| 350 | uint64_t TotalSize; |
| 351 | |
| 352 | public: |
| 353 | ~BinaryWriter() {} |
Jordan Rupprecht | 881cae7 | 2019-01-22 23:49:16 +0000 | [diff] [blame] | 354 | Error finalize() override; |
| 355 | Error write() override; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 356 | BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {} |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
Eugene Leviant | a6fb183 | 2019-05-29 11:37:16 +0000 | [diff] [blame] | 359 | class IHexWriter : public Writer { |
| 360 | struct SectionCompare { |
| 361 | bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const; |
| 362 | }; |
| 363 | |
| 364 | std::set<const SectionBase *, SectionCompare> Sections; |
| 365 | size_t TotalSize; |
| 366 | |
| 367 | Error checkSection(const SectionBase &Sec); |
| 368 | uint64_t writeEntryPointRecord(uint8_t *Buf); |
| 369 | uint64_t writeEndOfFileRecord(uint8_t *Buf); |
| 370 | |
| 371 | public: |
| 372 | ~IHexWriter() {} |
| 373 | Error finalize() override; |
| 374 | Error write() override; |
| 375 | IHexWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {} |
| 376 | }; |
| 377 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 378 | class SectionBase { |
| 379 | public: |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 380 | std::string Name; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 381 | Segment *ParentSegment = nullptr; |
| 382 | uint64_t HeaderOffset; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 383 | uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 384 | uint32_t Index; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 385 | bool HasSymbol = false; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 386 | |
| 387 | uint64_t Addr = 0; |
| 388 | uint64_t Align = 1; |
| 389 | uint32_t EntrySize = 0; |
| 390 | uint64_t Flags = 0; |
| 391 | uint64_t Info = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 392 | uint64_t Link = ELF::SHN_UNDEF; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 393 | uint64_t NameIndex = 0; |
| 394 | uint64_t Offset = 0; |
| 395 | uint64_t Size = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 396 | uint64_t Type = ELF::SHT_NULL; |
Paul Semel | a42dec7 | 2018-08-09 17:05:21 +0000 | [diff] [blame] | 397 | ArrayRef<uint8_t> OriginalData; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 398 | |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 399 | SectionBase() = default; |
| 400 | SectionBase(const SectionBase &) = default; |
| 401 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 402 | virtual ~SectionBase() = default; |
| 403 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 404 | virtual void initialize(SectionTableRef SecTable); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 405 | virtual void finalize(); |
Jordan Rupprecht | 52d5781 | 2019-02-21 16:45:42 +0000 | [diff] [blame] | 406 | // Remove references to these sections. The list of sections must be sorted. |
| 407 | virtual Error |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 408 | removeSectionReferences(bool AllowBrokenLinks, |
| 409 | function_ref<bool(const SectionBase *)> ToRemove); |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 410 | virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 411 | virtual void accept(SectionVisitor &Visitor) const = 0; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 412 | virtual void accept(MutableSectionVisitor &Visitor) = 0; |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 413 | virtual void markSymbols(); |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 414 | virtual void |
| 415 | replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | class Segment { |
| 419 | private: |
| 420 | struct SectionCompare { |
| 421 | bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { |
| 422 | // Some sections might have the same address if one of them is empty. To |
| 423 | // fix this we can use the lexicographic ordering on ->Addr and the |
| 424 | // address of the actully stored section. |
| 425 | if (Lhs->OriginalOffset == Rhs->OriginalOffset) |
| 426 | return Lhs < Rhs; |
| 427 | return Lhs->OriginalOffset < Rhs->OriginalOffset; |
| 428 | } |
| 429 | }; |
| 430 | |
| 431 | std::set<const SectionBase *, SectionCompare> Sections; |
| 432 | |
| 433 | public: |
Fangrui Song | 967ce40 | 2018-12-12 22:46:37 +0000 | [diff] [blame] | 434 | uint32_t Type; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 435 | uint32_t Flags; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 436 | uint64_t Offset; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 437 | uint64_t VAddr; |
Fangrui Song | 967ce40 | 2018-12-12 22:46:37 +0000 | [diff] [blame] | 438 | uint64_t PAddr; |
| 439 | uint64_t FileSize; |
| 440 | uint64_t MemSize; |
| 441 | uint64_t Align; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 442 | |
Fangrui Song | 967ce40 | 2018-12-12 22:46:37 +0000 | [diff] [blame] | 443 | uint32_t Index; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 444 | uint64_t OriginalOffset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 445 | Segment *ParentSegment = nullptr; |
James Henderson | 1f44814 | 2019-03-25 16:36:26 +0000 | [diff] [blame] | 446 | ArrayRef<uint8_t> Contents; |
| 447 | |
| 448 | explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 449 | Segment() {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 450 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 451 | const SectionBase *firstSection() const { |
| 452 | if (!Sections.empty()) |
| 453 | return *Sections.begin(); |
| 454 | return nullptr; |
| 455 | } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 456 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 457 | void removeSection(const SectionBase *Sec) { Sections.erase(Sec); } |
| 458 | void addSection(const SectionBase *Sec) { Sections.insert(Sec); } |
James Henderson | 1f44814 | 2019-03-25 16:36:26 +0000 | [diff] [blame] | 459 | |
| 460 | ArrayRef<uint8_t> getContents() const { return Contents; } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | class Section : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 464 | MAKE_SEC_WRITER_FRIEND |
| 465 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 466 | ArrayRef<uint8_t> Contents; |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 467 | SectionBase *LinkSection = nullptr; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 468 | |
| 469 | public: |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 470 | explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 471 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 472 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 473 | void accept(MutableSectionVisitor &Visitor) override; |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 474 | Error removeSectionReferences(bool AllowBrokenLinks, |
Jordan Rupprecht | 52d5781 | 2019-02-21 16:45:42 +0000 | [diff] [blame] | 475 | function_ref<bool(const SectionBase *)> ToRemove) override; |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 476 | void initialize(SectionTableRef SecTable) override; |
| 477 | void finalize() override; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 478 | }; |
| 479 | |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 480 | class OwnedDataSection : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 481 | MAKE_SEC_WRITER_FRIEND |
| 482 | |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 483 | std::vector<uint8_t> Data; |
| 484 | |
| 485 | public: |
| 486 | OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data) |
| 487 | : Data(std::begin(Data), std::end(Data)) { |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 488 | Name = SecName.str(); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 489 | Type = ELF::SHT_PROGBITS; |
| 490 | Size = Data.size(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 491 | OriginalOffset = std::numeric_limits<uint64_t>::max(); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 492 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 493 | |
Eugene Leviant | a6fb183 | 2019-05-29 11:37:16 +0000 | [diff] [blame] | 494 | OwnedDataSection(const Twine &SecName, uint64_t SecAddr, uint64_t SecFlags, |
| 495 | uint64_t SecOff) { |
| 496 | Name = SecName.str(); |
| 497 | Type = ELF::SHT_PROGBITS; |
| 498 | Addr = SecAddr; |
| 499 | Flags = SecFlags; |
| 500 | OriginalOffset = SecOff; |
| 501 | } |
| 502 | |
| 503 | void appendHexData(StringRef HexData); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 504 | void accept(SectionVisitor &Sec) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 505 | void accept(MutableSectionVisitor &Visitor) override; |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 508 | class CompressedSection : public SectionBase { |
| 509 | MAKE_SEC_WRITER_FRIEND |
| 510 | |
| 511 | DebugCompressionType CompressionType; |
| 512 | uint64_t DecompressedSize; |
| 513 | uint64_t DecompressedAlign; |
| 514 | SmallVector<char, 128> CompressedData; |
| 515 | |
| 516 | public: |
| 517 | CompressedSection(const SectionBase &Sec, |
| 518 | DebugCompressionType CompressionType); |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 519 | CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize, |
| 520 | uint64_t DecompressedAlign); |
| 521 | |
| 522 | uint64_t getDecompressedSize() const { return DecompressedSize; } |
| 523 | uint64_t getDecompressedAlign() const { return DecompressedAlign; } |
| 524 | |
| 525 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 526 | void accept(MutableSectionVisitor &Visitor) override; |
Puyan Lotfi | af04864 | 2018-10-01 10:29:41 +0000 | [diff] [blame] | 527 | |
| 528 | static bool classof(const SectionBase *S) { |
| 529 | return (S->Flags & ELF::SHF_COMPRESSED) || |
| 530 | (StringRef(S->Name).startswith(".zdebug")); |
| 531 | } |
| 532 | }; |
| 533 | |
| 534 | class DecompressedSection : public SectionBase { |
| 535 | MAKE_SEC_WRITER_FRIEND |
| 536 | |
| 537 | public: |
| 538 | explicit DecompressedSection(const CompressedSection &Sec) |
| 539 | : SectionBase(Sec) { |
| 540 | Size = Sec.getDecompressedSize(); |
| 541 | Align = Sec.getDecompressedAlign(); |
| 542 | Flags = (Flags & ~ELF::SHF_COMPRESSED); |
| 543 | if (StringRef(Name).startswith(".zdebug")) |
| 544 | Name = "." + Name.substr(2); |
| 545 | } |
| 546 | |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 547 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 548 | void accept(MutableSectionVisitor &Visitor) override; |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame] | 549 | }; |
| 550 | |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 551 | // There are two types of string tables that can exist, dynamic and not dynamic. |
| 552 | // In the dynamic case the string table is allocated. Changing a dynamic string |
| 553 | // table would mean altering virtual addresses and thus the memory image. So |
| 554 | // dynamic string tables should not have an interface to modify them or |
| 555 | // reconstruct them. This type lets us reconstruct a string table. To avoid |
| 556 | // this class being used for dynamic string tables (which has happened) the |
| 557 | // classof method checks that the particular instance is not allocated. This |
| 558 | // then agrees with the makeSection method used to construct most sections. |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 559 | class StringTableSection : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 560 | MAKE_SEC_WRITER_FRIEND |
| 561 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 562 | StringTableBuilder StrTabBuilder; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 563 | |
| 564 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 565 | StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) { |
| 566 | Type = ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 569 | void addString(StringRef Name); |
| 570 | uint32_t findIndex(StringRef Name) const; |
George Rimar | faf308b | 2019-03-18 14:27:41 +0000 | [diff] [blame] | 571 | void prepareForLayout(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 572 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 573 | void accept(MutableSectionVisitor &Visitor) override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 574 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 575 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 576 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 577 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 578 | return S->Type == ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 579 | } |
| 580 | }; |
| 581 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 582 | // Symbols have a st_shndx field that normally stores an index but occasionally |
| 583 | // stores a different special value. This enum keeps track of what the st_shndx |
| 584 | // field means. Most of the values are just copies of the special SHN_* values. |
| 585 | // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. |
| 586 | enum SymbolShndxType { |
| 587 | SYMBOL_SIMPLE_INDEX = 0, |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 588 | SYMBOL_ABS = ELF::SHN_ABS, |
| 589 | SYMBOL_COMMON = ELF::SHN_COMMON, |
| 590 | SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON, |
| 591 | SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2, |
| 592 | SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4, |
| 593 | SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8, |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 594 | SYMBOL_XINDEX = ELF::SHN_XINDEX, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 597 | struct Symbol { |
| 598 | uint8_t Binding; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 599 | SectionBase *DefinedIn = nullptr; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 600 | SymbolShndxType ShndxType; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 601 | uint32_t Index; |
Paul Semel | 7a3dc2c | 2018-08-09 17:49:04 +0000 | [diff] [blame] | 602 | std::string Name; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 603 | uint32_t NameIndex; |
| 604 | uint64_t Size; |
| 605 | uint8_t Type; |
| 606 | uint64_t Value; |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 607 | uint8_t Visibility; |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 608 | bool Referenced = false; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 609 | |
| 610 | uint16_t getShndx() const; |
Jordan Rupprecht | b47475c | 2018-11-01 17:26:36 +0000 | [diff] [blame] | 611 | bool isCommon() const; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 612 | }; |
| 613 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 614 | class SectionIndexSection : public SectionBase { |
| 615 | MAKE_SEC_WRITER_FRIEND |
| 616 | |
| 617 | private: |
| 618 | std::vector<uint32_t> Indexes; |
| 619 | SymbolTableSection *Symbols = nullptr; |
| 620 | |
| 621 | public: |
| 622 | virtual ~SectionIndexSection() {} |
| 623 | void addIndex(uint32_t Index) { |
Eugene Leviant | 88089fe | 2019-04-12 11:59:30 +0000 | [diff] [blame] | 624 | assert(Size > 0); |
| 625 | Indexes.push_back(Index); |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 626 | } |
Eugene Leviant | 88089fe | 2019-04-12 11:59:30 +0000 | [diff] [blame] | 627 | |
| 628 | void reserve(size_t NumSymbols) { |
| 629 | Indexes.reserve(NumSymbols); |
| 630 | Size = NumSymbols * 4; |
| 631 | } |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 632 | void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; } |
| 633 | void initialize(SectionTableRef SecTable) override; |
| 634 | void finalize() override; |
| 635 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 636 | void accept(MutableSectionVisitor &Visitor) override; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 637 | |
| 638 | SectionIndexSection() { |
| 639 | Name = ".symtab_shndx"; |
| 640 | Align = 4; |
| 641 | EntrySize = 4; |
| 642 | Type = ELF::SHT_SYMTAB_SHNDX; |
| 643 | } |
| 644 | }; |
| 645 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 646 | class SymbolTableSection : public SectionBase { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 647 | MAKE_SEC_WRITER_FRIEND |
| 648 | |
Alexander Shaposhnikov | a8f1550 | 2018-02-24 00:41:01 +0000 | [diff] [blame] | 649 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 650 | void assignIndices(); |
Alexander Shaposhnikov | a8f1550 | 2018-02-24 00:41:01 +0000 | [diff] [blame] | 651 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 652 | protected: |
| 653 | std::vector<std::unique_ptr<Symbol>> Symbols; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 654 | StringTableSection *SymbolNames = nullptr; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 655 | SectionIndexSection *SectionIndexTable = nullptr; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 656 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 657 | using SymPtr = std::unique_ptr<Symbol>; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 658 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 659 | public: |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 660 | SymbolTableSection() { Type = ELF::SHT_SYMTAB; } |
| 661 | |
| 662 | void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn, |
| 663 | uint64_t Value, uint8_t Visibility, uint16_t Shndx, |
George Rimar | 17dbb19 | 2019-05-08 07:31:05 +0000 | [diff] [blame] | 664 | uint64_t SymbolSize); |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 665 | void prepareForLayout(); |
Paul Semel | 46201fb | 2018-06-01 16:19:46 +0000 | [diff] [blame] | 666 | // An 'empty' symbol table still contains a null symbol. |
| 667 | bool empty() const { return Symbols.size() == 1; } |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 668 | void setShndxTable(SectionIndexSection *ShndxTable) { |
| 669 | SectionIndexTable = ShndxTable; |
| 670 | } |
| 671 | const SectionIndexSection *getShndxTable() const { return SectionIndexTable; } |
Eugene Leviant | 88089fe | 2019-04-12 11:59:30 +0000 | [diff] [blame] | 672 | void fillShndxTable(); |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 673 | const SectionBase *getStrTab() const { return SymbolNames; } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 674 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 675 | Symbol *getSymbolByIndex(uint32_t Index); |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 676 | void updateSymbols(function_ref<void(Symbol &)> Callable); |
| 677 | |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 678 | Error removeSectionReferences(bool AllowBrokenLinks, |
Jordan Rupprecht | 52d5781 | 2019-02-21 16:45:42 +0000 | [diff] [blame] | 679 | function_ref<bool(const SectionBase *)> ToRemove) override; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 680 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 681 | void finalize() override; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 682 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 683 | void accept(MutableSectionVisitor &Visitor) override; |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 684 | Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override; |
George Rimar | 0373bed | 2019-03-20 13:57:47 +0000 | [diff] [blame] | 685 | void replaceSectionReferences( |
| 686 | const DenseMap<SectionBase *, SectionBase *> &FromTo) override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 687 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 688 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 689 | return S->Type == ELF::SHT_SYMTAB; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 690 | } |
| 691 | }; |
| 692 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 693 | struct Relocation { |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 694 | Symbol *RelocSymbol = nullptr; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 695 | uint64_t Offset; |
| 696 | uint64_t Addend; |
| 697 | uint32_t Type; |
| 698 | }; |
| 699 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 700 | // All relocation sections denote relocations to apply to another section. |
| 701 | // However, some relocation sections use a dynamic symbol table and others use |
| 702 | // a regular symbol table. Because the types of the two symbol tables differ in |
| 703 | // our system (because they should behave differently) we can't uniformly |
| 704 | // represent all relocations with the same base class if we expose an interface |
| 705 | // that mentions the symbol table type. So we split the two base types into two |
| 706 | // different classes, one which handles the section the relocation is applied to |
| 707 | // and another which handles the symbol table type. The symbol table type is |
| 708 | // taken as a type parameter to the class (see RelocSectionWithSymtabBase). |
| 709 | class RelocationSectionBase : public SectionBase { |
| 710 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 711 | SectionBase *SecToApplyRel = nullptr; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 712 | |
| 713 | public: |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 714 | const SectionBase *getSection() const { return SecToApplyRel; } |
Jake Ehrlich | c5ff727 | 2017-10-10 18:32:22 +0000 | [diff] [blame] | 715 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 716 | |
| 717 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 718 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 719 | } |
| 720 | }; |
| 721 | |
| 722 | // Takes the symbol table type to use as a parameter so that we can deduplicate |
| 723 | // that code between the two symbol table types. |
| 724 | template <class SymTabType> |
| 725 | class RelocSectionWithSymtabBase : public RelocationSectionBase { |
Alexander Shaposhnikov | a8f1550 | 2018-02-24 00:41:01 +0000 | [diff] [blame] | 726 | void setSymTab(SymTabType *SymTab) { Symbols = SymTab; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 727 | |
| 728 | protected: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 729 | RelocSectionWithSymtabBase() = default; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 730 | |
George Rimar | 79fb858 | 2019-02-27 11:18:27 +0000 | [diff] [blame] | 731 | SymTabType *Symbols = nullptr; |
| 732 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 733 | public: |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 734 | void initialize(SectionTableRef SecTable) override; |
| 735 | void finalize() override; |
| 736 | }; |
| 737 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 738 | class RelocationSection |
| 739 | : public RelocSectionWithSymtabBase<SymbolTableSection> { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 740 | MAKE_SEC_WRITER_FRIEND |
| 741 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 742 | std::vector<Relocation> Relocations; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 743 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 744 | public: |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 745 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 746 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 747 | void accept(MutableSectionVisitor &Visitor) override; |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 748 | Error removeSectionReferences(bool AllowBrokenLinks, |
George Rimar | 79fb858 | 2019-02-27 11:18:27 +0000 | [diff] [blame] | 749 | function_ref<bool(const SectionBase *)> ToRemove) override; |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 750 | Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override; |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 751 | void markSymbols() override; |
George Rimar | d8a5c6c | 2019-03-11 11:01:24 +0000 | [diff] [blame] | 752 | void replaceSectionReferences( |
| 753 | const DenseMap<SectionBase *, SectionBase *> &FromTo) override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 754 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 755 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 756 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 757 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 758 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 759 | } |
| 760 | }; |
| 761 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 762 | // TODO: The way stripping and groups interact is complicated |
| 763 | // and still needs to be worked on. |
| 764 | |
| 765 | class GroupSection : public SectionBase { |
| 766 | MAKE_SEC_WRITER_FRIEND |
| 767 | const SymbolTableSection *SymTab = nullptr; |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 768 | Symbol *Sym = nullptr; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 769 | ELF::Elf32_Word FlagWord; |
| 770 | SmallVector<SectionBase *, 3> GroupMembers; |
Alexander Shaposhnikov | 43b8acd | 2018-03-20 18:20:42 +0000 | [diff] [blame] | 771 | |
| 772 | public: |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 773 | // TODO: Contents is present in several classes of the hierarchy. |
| 774 | // This needs to be refactored to avoid duplication. |
| 775 | ArrayRef<uint8_t> Contents; |
Alexander Shaposhnikov | 3b24ed7 | 2018-03-20 19:46:00 +0000 | [diff] [blame] | 776 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 777 | explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 778 | |
| 779 | void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; } |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 780 | void setSymbol(Symbol *S) { Sym = S; } |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 781 | void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; } |
| 782 | void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); } |
| 783 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 784 | void accept(SectionVisitor &) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 785 | void accept(MutableSectionVisitor &Visitor) override; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 786 | void finalize() override; |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 787 | Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override; |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 788 | void markSymbols() override; |
George Rimar | 2725717 | 2019-03-24 14:41:45 +0000 | [diff] [blame] | 789 | void replaceSectionReferences( |
| 790 | const DenseMap<SectionBase *, SectionBase *> &FromTo) override; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 791 | |
| 792 | static bool classof(const SectionBase *S) { |
| 793 | return S->Type == ELF::SHT_GROUP; |
| 794 | } |
| 795 | }; |
| 796 | |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 797 | class DynamicSymbolTableSection : public Section { |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 798 | public: |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 799 | explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 800 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 801 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 802 | return S->Type == ELF::SHT_DYNSYM; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 803 | } |
| 804 | }; |
| 805 | |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 806 | class DynamicSection : public Section { |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 807 | public: |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 808 | explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {} |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 809 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 810 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 811 | return S->Type == ELF::SHT_DYNAMIC; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 812 | } |
| 813 | }; |
| 814 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 815 | class DynamicRelocationSection |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 816 | : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 817 | MAKE_SEC_WRITER_FRIEND |
| 818 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 819 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 820 | ArrayRef<uint8_t> Contents; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 821 | |
| 822 | public: |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 823 | explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {} |
George Rimar | 67f590e | 2019-04-30 11:02:09 +0000 | [diff] [blame] | 824 |
|
| 825 | void accept(SectionVisitor &) const override;
|
| 826 | void accept(MutableSectionVisitor &Visitor) override;
|
| 827 | Error removeSectionReferences(
|
| 828 | bool AllowBrokenLinks,
|
| 829 | function_ref<bool(const SectionBase *)> ToRemove) override;
|
| 830 |
|
| 831 | static bool classof(const SectionBase *S) {
|
| 832 | if (!(S->Flags & ELF::SHF_ALLOC))
|
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 833 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 834 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 835 | } |
| 836 | }; |
| 837 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 838 | class GnuDebugLinkSection : public SectionBase { |
| 839 | MAKE_SEC_WRITER_FRIEND |
| 840 | |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 841 | private: |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 842 | StringRef FileName; |
| 843 | uint32_t CRC32; |
| 844 | |
James Henderson | 9df3883 | 2019-05-14 10:59:04 +0000 | [diff] [blame] | 845 | void init(StringRef File); |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 846 | |
| 847 | public: |
| 848 | // 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] | 849 | explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 850 | void accept(SectionVisitor &Visitor) const override; |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 851 | void accept(MutableSectionVisitor &Visitor) override; |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 852 | }; |
| 853 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 854 | class Reader { |
| 855 | public: |
| 856 | virtual ~Reader(); |
| 857 | virtual std::unique_ptr<Object> create() const = 0; |
| 858 | }; |
| 859 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 860 | using object::Binary; |
| 861 | using object::ELFFile; |
| 862 | using object::ELFObjectFile; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 863 | using object::OwningBinary; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 864 | |
Jordan Rupprecht | 1f82176 | 2019-01-03 17:45:30 +0000 | [diff] [blame] | 865 | class BinaryELFBuilder { |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 866 | uint16_t EMachine; |
| 867 | MemoryBuffer *MemBuf; |
| 868 | std::unique_ptr<Object> Obj; |
| 869 | |
| 870 | void initFileHeader(); |
| 871 | void initHeaderSegment(); |
| 872 | StringTableSection *addStrTab(); |
| 873 | SymbolTableSection *addSymTab(StringTableSection *StrTab); |
| 874 | void addData(SymbolTableSection *SymTab); |
| 875 | void initSections(); |
| 876 | |
| 877 | public: |
| 878 | BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB) |
| 879 | : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {} |
| 880 | |
| 881 | std::unique_ptr<Object> build(); |
| 882 | }; |
| 883 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 884 | template <class ELFT> class ELFBuilder { |
| 885 | private: |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 886 | using Elf_Addr = typename ELFT::Addr; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 887 | using Elf_Shdr = typename ELFT::Shdr; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 888 | using Elf_Word = typename ELFT::Word; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 889 | |
| 890 | const ELFFile<ELFT> &ElfFile; |
| 891 | Object &Obj; |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame^] | 892 | uint64_t EhdrOffset = 0; |
| 893 | Optional<StringRef> ExtractPartition; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 894 | |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 895 | void setParentSegment(Segment &Child); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame^] | 896 | void readProgramHeaders(const ELFFile<ELFT> &HeadersFile); |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 897 | void initGroupSection(GroupSection *GroupSec); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 898 | void initSymbolTable(SymbolTableSection *SymTab); |
| 899 | void readSectionHeaders(); |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame^] | 900 | void readSections(); |
| 901 | void findEhdrOffset(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 902 | SectionBase &makeSection(const Elf_Shdr &Shdr); |
| 903 | |
| 904 | public: |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame^] | 905 | ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj, |
| 906 | Optional<StringRef> ExtractPartition) |
| 907 | : ElfFile(*ElfObj.getELFFile()), Obj(Obj), |
| 908 | ExtractPartition(ExtractPartition) {} |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 909 | |
| 910 | void build(); |
| 911 | }; |
| 912 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 913 | class BinaryReader : public Reader { |
| 914 | const MachineInfo &MInfo; |
| 915 | MemoryBuffer *MemBuf; |
| 916 | |
| 917 | public: |
| 918 | BinaryReader(const MachineInfo &MI, MemoryBuffer *MB) |
| 919 | : MInfo(MI), MemBuf(MB) {} |
| 920 | std::unique_ptr<Object> create() const override; |
| 921 | }; |
| 922 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 923 | class ELFReader : public Reader { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 924 | Binary *Bin; |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame^] | 925 | Optional<StringRef> ExtractPartition; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 926 | |
| 927 | public: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 928 | std::unique_ptr<Object> create() const override; |
Peter Collingbourne | 8d58a98 | 2019-06-07 17:57:48 +0000 | [diff] [blame^] | 929 | explicit ELFReader(Binary *B, Optional<StringRef> ExtractPartition) |
| 930 | : Bin(B), ExtractPartition(ExtractPartition) {} |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | class Object { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 934 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 935 | using SecPtr = std::unique_ptr<SectionBase>; |
| 936 | using SegPtr = std::unique_ptr<Segment>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 937 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 938 | std::vector<SecPtr> Sections; |
| 939 | std::vector<SegPtr> Segments; |
James Henderson | 1f44814 | 2019-03-25 16:36:26 +0000 | [diff] [blame] | 940 | std::vector<SecPtr> RemovedSections; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 941 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 942 | public: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 943 | template <class T> |
| 944 | using Range = iterator_range< |
| 945 | pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>; |
| 946 | |
| 947 | template <class T> |
| 948 | using ConstRange = iterator_range<pointee_iterator< |
| 949 | typename std::vector<std::unique_ptr<T>>::const_iterator>>; |
| 950 | |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 951 | // It is often the case that the ELF header and the program header table are |
| 952 | // not present in any segment. This could be a problem during file layout, |
| 953 | // because other segments may get assigned an offset where either of the |
| 954 | // two should reside, which will effectively corrupt the resulting binary. |
| 955 | // Other than that we use these segments to track program header offsets |
| 956 | // when they may not follow the ELF header. |
| 957 | Segment ElfHdrSegment; |
| 958 | Segment ProgramHdrSegment; |
| 959 | |
George Rimar | 4ded773 | 2018-12-20 10:51:42 +0000 | [diff] [blame] | 960 | uint8_t OSABI; |
| 961 | uint8_t ABIVersion; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 962 | uint64_t Entry; |
| 963 | uint64_t SHOffset; |
| 964 | uint32_t Type; |
| 965 | uint32_t Machine; |
| 966 | uint32_t Version; |
| 967 | uint32_t Flags; |
| 968 | |
James Henderson | 38cb238 | 2019-04-02 14:11:13 +0000 | [diff] [blame] | 969 | bool HadShdrs = true; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 970 | StringTableSection *SectionNames = nullptr; |
| 971 | SymbolTableSection *SymbolTable = nullptr; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 972 | SectionIndexSection *SectionIndexTable = nullptr; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 973 | |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 974 | void sortSections(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 975 | SectionTableRef sections() { return SectionTableRef(Sections); } |
| 976 | ConstRange<SectionBase> sections() const { |
| 977 | return make_pointee_range(Sections); |
| 978 | } |
Eugene Leviant | 51c1f64 | 2019-02-25 14:12:41 +0000 | [diff] [blame] | 979 | SectionBase *findSection(StringRef Name) { |
| 980 | auto SecIt = |
| 981 | find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; }); |
| 982 | return SecIt == Sections.end() ? nullptr : SecIt->get(); |
| 983 | } |
James Henderson | 1f44814 | 2019-03-25 16:36:26 +0000 | [diff] [blame] | 984 | SectionTableRef removedSections() { return SectionTableRef(RemovedSections); } |
| 985 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 986 | Range<Segment> segments() { return make_pointee_range(Segments); } |
| 987 | ConstRange<Segment> segments() const { return make_pointee_range(Segments); } |
Aaron Ballman | 09f46a7 | 2018-01-25 21:03:38 +0000 | [diff] [blame] | 988 | |
James Henderson | 66a9d0f | 2019-04-18 09:13:30 +0000 | [diff] [blame] | 989 | Error removeSections(bool AllowBrokenLinks, |
| 990 | std::function<bool(const SectionBase &)> ToRemove); |
Jordan Rupprecht | 971d4762 | 2019-02-01 15:20:36 +0000 | [diff] [blame] | 991 | Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 992 | template <class T, class... Ts> T &addSection(Ts &&... Args) { |
| 993 | auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...); |
| 994 | auto Ptr = Sec.get(); |
| 995 | Sections.emplace_back(std::move(Sec)); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 996 | Ptr->Index = Sections.size(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 997 | return *Ptr; |
| 998 | } |
James Henderson | 1f44814 | 2019-03-25 16:36:26 +0000 | [diff] [blame] | 999 | Segment &addSegment(ArrayRef<uint8_t> Data) { |
| 1000 | Segments.emplace_back(llvm::make_unique<Segment>(Data)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1001 | return *Segments.back(); |
| 1002 | } |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1003 | }; |
Alexander Shaposhnikov | 654d3a9 | 2018-10-24 22:49:06 +0000 | [diff] [blame] | 1004 | |
| 1005 | } // end namespace elf |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 1006 | } // end namespace objcopy |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 1007 | } // end namespace llvm |
| 1008 | |
| 1009 | #endif // LLVM_TOOLS_OBJCOPY_OBJECT_H |