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