blob: 4936fd6dc321e4a7952ec5fa2b6d8607a8f2d8d7 [file] [log] [blame]
Petr Hosek05a04cb2017-08-01 00:33:58 +00001//===- Object.h -------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Hosek05a04cb2017-08-01 00:33:58 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00009#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
10#define LLVM_TOOLS_OBJCOPY_OBJECT_H
Petr Hosek05a04cb2017-08-01 00:33:58 +000011
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000012#include "Buffer.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000013#include "CopyConfig.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000014#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000018#include "llvm/MC/StringTableBuilder.h"
19#include "llvm/Object/ELFObjectFile.h"
Eugene Levianta6fb1832019-05-29 11:37:16 +000020#include "llvm/Support/Errc.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000021#include "llvm/Support/FileOutputBuffer.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000022#include <cstddef>
23#include <cstdint>
24#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000025#include <memory>
26#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000027#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000028
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000029namespace llvm {
Puyan Lotfi99124cc2018-09-07 08:10:22 +000030enum class DebugCompressionType;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000031namespace objcopy {
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +000032namespace elf {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000033
Jake Ehrlichf5a43772017-09-25 20:37:28 +000034class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000035class Section;
36class OwnedDataSection;
37class StringTableSection;
38class SymbolTableSection;
39class RelocationSection;
40class DynamicRelocationSection;
41class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000042class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000043class SectionIndexSection;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000044class CompressedSection;
Puyan Lotfiaf048642018-10-01 10:29:41 +000045class DecompressedSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000046class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000047class Object;
Paul Semel4246a462018-05-09 21:36:54 +000048struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000049
50class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000051 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000052
53public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000054 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
55
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000056 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000057 : Sections(Secs) {}
58 SectionTableRef(const SectionTableRef &) = default;
59
Jake Ehrlich76e91102018-01-25 22:46:17 +000060 iterator begin() { return iterator(Sections.data()); }
61 iterator end() { return iterator(Sections.data() + Sections.size()); }
Fangrui Song82b01e02019-03-30 14:08:59 +000062 size_t size() const { return Sections.size(); }
Jake Ehrlich76e91102018-01-25 22:46:17 +000063
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000064 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000065
66 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000067 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000068};
Petr Hosek05a04cb2017-08-01 00:33:58 +000069
Jake Ehrlich76e91102018-01-25 22:46:17 +000070enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
71
72class SectionVisitor {
73public:
Jordan Rupprecht1f821762019-01-03 17:45:30 +000074 virtual ~SectionVisitor() = default;
Jake Ehrlich76e91102018-01-25 22:46:17 +000075
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 Shaposhnikov6ecc6e62018-03-21 19:53:44 +000083 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000084 virtual void visit(const SectionIndexSection &Sec) = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000085 virtual void visit(const CompressedSection &Sec) = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +000086 virtual void visit(const DecompressedSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000087};
88
Jordan Rupprecht1f821762019-01-03 17:45:30 +000089class MutableSectionVisitor {
90public:
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 Ehrlich76e91102018-01-25 22:46:17 +0000106class SectionWriter : public SectionVisitor {
107protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000108 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109
110public:
Fangrui Songa85bf872019-03-15 10:20:51 +0000111 virtual ~SectionWriter() = default;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000112
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 Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000120 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000121 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000122 virtual void visit(const CompressedSection &Sec) override = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000123 virtual void visit(const DecompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000124
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000125 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000126};
127
128template <class ELFT> class ELFSectionWriter : public SectionWriter {
129private:
130 using Elf_Word = typename ELFT::Word;
131 using Elf_Rel = typename ELFT::Rel;
132 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000133 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000134
135public:
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 Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000140 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000141 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000142 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000143 void visit(const DecompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000144
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000145 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000146};
147
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000148template <class ELFT> class ELFSectionSizer : public MutableSectionVisitor {
149private:
150 using Elf_Rel = typename ELFT::Rel;
151 using Elf_Rela = typename ELFT::Rela;
152 using Elf_Sym = typename ELFT::Sym;
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +0000153 using Elf_Word = typename ELFT::Word;
154 using Elf_Xword = typename ELFT::Xword;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000155
156public:
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 Ehrlich76e91102018-01-25 22:46:17 +0000170#define MAKE_SEC_WRITER_FRIEND \
171 friend class SectionWriter; \
Eugene Levianta6fb1832019-05-29 11:37:16 +0000172 friend class IHexSectionWriterBase; \
173 friend class IHexSectionWriter; \
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000174 template <class ELFT> friend class ELFSectionWriter; \
175 template <class ELFT> friend class ELFSectionSizer;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000176
177class BinarySectionWriter : public SectionWriter {
178public:
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 Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000184 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000185 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000186 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000187 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000188
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000189 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
190};
191
Eugene Levianta6fb1832019-05-29 11:37:16 +0000192using IHexLineData = SmallVector<char, 64>;
193
194struct 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 Leviant86b7f862019-06-13 09:56:14 +0000219 // 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 Levianta6fb1832019-05-29 11:37:16 +0000223 // 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.
266class 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
277protected:
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
284public:
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
296class IHexSectionWriter : public IHexSectionWriterBase {
297public:
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 Ehrlich76e91102018-01-25 22:46:17 +0000304class Writer {
305protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000306 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000307 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000308
309public:
310 virtual ~Writer();
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000311 virtual Error finalize() = 0;
312 virtual Error write() = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000313
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000314 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000315};
316
317template <class ELFT> class ELFWriter : public Writer {
318private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000319 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000320 using Elf_Shdr = typename ELFT::Shdr;
321 using Elf_Phdr = typename ELFT::Phdr;
322 using Elf_Ehdr = typename ELFT::Ehdr;
323
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000324 void initEhdrSegment();
325
Jake Ehrlich76e91102018-01-25 22:46:17 +0000326 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 Henderson1f448142019-03-25 16:36:26 +0000333 void writeSegmentData();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000334
335 void assignOffsets();
336
337 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
338
339 size_t totalSize() const;
340
341public:
342 virtual ~ELFWriter() {}
James Henderson38cb2382019-04-02 14:11:13 +0000343 bool WriteSectionHeaders;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000344
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000345 Error finalize() override;
346 Error write() override;
James Henderson38cb2382019-04-02 14:11:13 +0000347 ELFWriter(Object &Obj, Buffer &Buf, bool WSH);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000348};
349
350class BinaryWriter : public Writer {
351private:
352 std::unique_ptr<BinarySectionWriter> SecWriter;
353
354 uint64_t TotalSize;
355
356public:
357 ~BinaryWriter() {}
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000358 Error finalize() override;
359 Error write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000360 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000361};
362
Eugene Levianta6fb1832019-05-29 11:37:16 +0000363class 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
375public:
376 ~IHexWriter() {}
377 Error finalize() override;
378 Error write() override;
379 IHexWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
380};
381
Petr Hosek05a04cb2017-08-01 00:33:58 +0000382class SectionBase {
383public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000384 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000385 Segment *ParentSegment = nullptr;
386 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000387 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000388 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000389 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000390
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 Zelenko0ad18f82017-11-01 21:16:06 +0000396 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000397 uint64_t NameIndex = 0;
398 uint64_t Offset = 0;
399 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000400 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000401 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000402
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000403 SectionBase() = default;
404 SectionBase(const SectionBase &) = default;
405
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000406 virtual ~SectionBase() = default;
407
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000408 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000409 virtual void finalize();
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000410 // Remove references to these sections. The list of sections must be sorted.
411 virtual Error
James Henderson66a9d0f2019-04-18 09:13:30 +0000412 removeSectionReferences(bool AllowBrokenLinks,
413 function_ref<bool(const SectionBase *)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000414 virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000415 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000416 virtual void accept(MutableSectionVisitor &Visitor) = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000417 virtual void markSymbols();
George Rimard8a5c6c2019-03-11 11:01:24 +0000418 virtual void
419 replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000420};
421
422class Segment {
423private:
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
437public:
Fangrui Song967ce402018-12-12 22:46:37 +0000438 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000439 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000440 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000441 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000442 uint64_t PAddr;
443 uint64_t FileSize;
444 uint64_t MemSize;
445 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000446
Fangrui Song967ce402018-12-12 22:46:37 +0000447 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000448 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000449 Segment *ParentSegment = nullptr;
James Henderson1f448142019-03-25 16:36:26 +0000450 ArrayRef<uint8_t> Contents;
451
452 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
453 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000454
Petr Hosek05a04cb2017-08-01 00:33:58 +0000455 const SectionBase *firstSection() const {
456 if (!Sections.empty())
457 return *Sections.begin();
458 return nullptr;
459 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000460
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000461 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
462 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
James Henderson1f448142019-03-25 16:36:26 +0000463
464 ArrayRef<uint8_t> getContents() const { return Contents; }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000465};
466
467class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000468 MAKE_SEC_WRITER_FRIEND
469
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000470 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000471 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000472
473public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000474 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000475
Jake Ehrlich76e91102018-01-25 22:46:17 +0000476 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000477 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000478 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000479 function_ref<bool(const SectionBase *)> ToRemove) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000480 void initialize(SectionTableRef SecTable) override;
481 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000482};
483
Jake Ehrliche8437de2017-12-19 00:47:30 +0000484class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000485 MAKE_SEC_WRITER_FRIEND
486
Jake Ehrliche8437de2017-12-19 00:47:30 +0000487 std::vector<uint8_t> Data;
488
489public:
490 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
491 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000492 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000493 Type = ELF::SHT_PROGBITS;
494 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000495 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000496 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000497
Eugene Levianta6fb1832019-05-29 11:37:16 +0000498 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 Ehrlich76e91102018-01-25 22:46:17 +0000508 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000509 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000510};
511
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000512class 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
520public:
521 CompressedSection(const SectionBase &Sec,
522 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000523 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 Rupprecht1f821762019-01-03 17:45:30 +0000530 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000531
532 static bool classof(const SectionBase *S) {
533 return (S->Flags & ELF::SHF_COMPRESSED) ||
534 (StringRef(S->Name).startswith(".zdebug"));
535 }
536};
537
538class DecompressedSection : public SectionBase {
539 MAKE_SEC_WRITER_FRIEND
540
541public:
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 Lotfi99124cc2018-09-07 08:10:22 +0000551 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000552 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000553};
554
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000555// 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 Hosek05a04cb2017-08-01 00:33:58 +0000563class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000564 MAKE_SEC_WRITER_FRIEND
565
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000566 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000567
568public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000569 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
570 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000571 }
572
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000573 void addString(StringRef Name);
574 uint32_t findIndex(StringRef Name) const;
George Rimarfaf308b2019-03-18 14:27:41 +0000575 void prepareForLayout();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000576 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000577 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000578
Petr Hosek05a04cb2017-08-01 00:33:58 +0000579 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000580 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000581 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000582 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000583 }
584};
585
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000586// 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.
590enum SymbolShndxType {
591 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000592 SYMBOL_ABS = ELF::SHN_ABS,
593 SYMBOL_COMMON = ELF::SHN_COMMON,
Nicolai Haehnle08e8cb52019-06-25 11:51:35 +0000594 SYMBOL_AMDGPU_LDS = ELF::SHN_AMDGPU_LDS,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000595 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
596 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
597 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
598 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000599 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000600};
601
Petr Hosek79cee9e2017-08-29 02:12:03 +0000602struct Symbol {
603 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000604 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000605 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000606 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000607 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000608 uint32_t NameIndex;
609 uint64_t Size;
610 uint8_t Type;
611 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000612 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000613 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000614
615 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000616 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000617};
618
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000619class SectionIndexSection : public SectionBase {
620 MAKE_SEC_WRITER_FRIEND
621
622private:
623 std::vector<uint32_t> Indexes;
624 SymbolTableSection *Symbols = nullptr;
625
626public:
627 virtual ~SectionIndexSection() {}
628 void addIndex(uint32_t Index) {
Eugene Leviant88089fe2019-04-12 11:59:30 +0000629 assert(Size > 0);
630 Indexes.push_back(Index);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000631 }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000632
633 void reserve(size_t NumSymbols) {
634 Indexes.reserve(NumSymbols);
635 Size = NumSymbols * 4;
636 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000637 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
638 void initialize(SectionTableRef SecTable) override;
639 void finalize() override;
640 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000641 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000642
643 SectionIndexSection() {
644 Name = ".symtab_shndx";
645 Align = 4;
646 EntrySize = 4;
647 Type = ELF::SHT_SYMTAB_SHNDX;
648 }
649};
650
Petr Hosek79cee9e2017-08-29 02:12:03 +0000651class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000652 MAKE_SEC_WRITER_FRIEND
653
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000654 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000655 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000656
Petr Hosek79cee9e2017-08-29 02:12:03 +0000657protected:
658 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000659 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000660 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000661
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000662 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000663
Petr Hosek79cee9e2017-08-29 02:12:03 +0000664public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000665 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
666
667 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
668 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
George Rimar17dbb192019-05-08 07:31:05 +0000669 uint64_t SymbolSize);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000670 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000671 // An 'empty' symbol table still contains a null symbol.
672 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000673 void setShndxTable(SectionIndexSection *ShndxTable) {
674 SectionIndexTable = ShndxTable;
675 }
676 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000677 void fillShndxTable();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000678 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000679 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000680 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000681 void updateSymbols(function_ref<void(Symbol &)> Callable);
682
James Henderson66a9d0f2019-04-18 09:13:30 +0000683 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000684 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000685 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000686 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000687 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000688 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000689 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
George Rimar0373bed2019-03-20 13:57:47 +0000690 void replaceSectionReferences(
691 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000692
Petr Hosek79cee9e2017-08-29 02:12:03 +0000693 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000694 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000695 }
696};
697
Petr Hosekd7df9b22017-09-06 23:41:02 +0000698struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000699 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000700 uint64_t Offset;
701 uint64_t Addend;
702 uint32_t Type;
703};
704
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000705// All relocation sections denote relocations to apply to another section.
706// However, some relocation sections use a dynamic symbol table and others use
707// a regular symbol table. Because the types of the two symbol tables differ in
708// our system (because they should behave differently) we can't uniformly
709// represent all relocations with the same base class if we expose an interface
710// that mentions the symbol table type. So we split the two base types into two
711// different classes, one which handles the section the relocation is applied to
712// and another which handles the symbol table type. The symbol table type is
713// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
714class RelocationSectionBase : public SectionBase {
715protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000716 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000717
718public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000719 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000720 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000721
722 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000723 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000724 }
725};
726
727// Takes the symbol table type to use as a parameter so that we can deduplicate
728// that code between the two symbol table types.
729template <class SymTabType>
730class RelocSectionWithSymtabBase : public RelocationSectionBase {
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000731 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000732
733protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000734 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000735
George Rimar79fb8582019-02-27 11:18:27 +0000736 SymTabType *Symbols = nullptr;
737
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000738public:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000739 void initialize(SectionTableRef SecTable) override;
740 void finalize() override;
741};
742
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000743class RelocationSection
744 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000745 MAKE_SEC_WRITER_FRIEND
746
Petr Hosekd7df9b22017-09-06 23:41:02 +0000747 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000748
Petr Hosekd7df9b22017-09-06 23:41:02 +0000749public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000750 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000751 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000752 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000753 Error removeSectionReferences(bool AllowBrokenLinks,
George Rimar79fb8582019-02-27 11:18:27 +0000754 function_ref<bool(const SectionBase *)> ToRemove) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000755 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000756 void markSymbols() override;
George Rimard8a5c6c2019-03-11 11:01:24 +0000757 void replaceSectionReferences(
758 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000759
Petr Hosekd7df9b22017-09-06 23:41:02 +0000760 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000761 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000762 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000763 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000764 }
765};
766
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000767// TODO: The way stripping and groups interact is complicated
768// and still needs to be worked on.
769
770class GroupSection : public SectionBase {
771 MAKE_SEC_WRITER_FRIEND
772 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000773 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000774 ELF::Elf32_Word FlagWord;
775 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000776
777public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000778 // TODO: Contents is present in several classes of the hierarchy.
779 // This needs to be refactored to avoid duplication.
780 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000781
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000782 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
783
784 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000785 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000786 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
787 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
788
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000789 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000790 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000791 void finalize() override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000792 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000793 void markSymbols() override;
George Rimar27257172019-03-24 14:41:45 +0000794 void replaceSectionReferences(
795 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000796
797 static bool classof(const SectionBase *S) {
798 return S->Type == ELF::SHT_GROUP;
799 }
800};
801
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000802class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000803public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000804 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000805
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000806 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000807 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000808 }
809};
810
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000811class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000812public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000813 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000814
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000815 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000816 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000817 }
818};
819
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000820class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000821 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000822 MAKE_SEC_WRITER_FRIEND
823
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000824private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000825 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000826
827public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000828 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Leviant86b7f862019-06-13 09:56:14 +0000829
830 void accept(SectionVisitor &) const override;
831 void accept(MutableSectionVisitor &Visitor) override;
832 Error removeSectionReferences(
833 bool AllowBrokenLinks,
834 function_ref<bool(const SectionBase *)> ToRemove) override;
835
836 static bool classof(const SectionBase *S) {
837 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000838 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000839 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000840 }
841};
842
Jake Ehrlich76e91102018-01-25 22:46:17 +0000843class GnuDebugLinkSection : public SectionBase {
844 MAKE_SEC_WRITER_FRIEND
845
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000846private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000847 StringRef FileName;
848 uint32_t CRC32;
849
James Henderson9df38832019-05-14 10:59:04 +0000850 void init(StringRef File);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000851
852public:
853 // If we add this section from an external source we can use this ctor.
James Henderson9df38832019-05-14 10:59:04 +0000854 explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000855 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000856 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000857};
858
Jake Ehrlich76e91102018-01-25 22:46:17 +0000859class Reader {
860public:
861 virtual ~Reader();
862 virtual std::unique_ptr<Object> create() const = 0;
863};
864
Jake Ehrlich76e91102018-01-25 22:46:17 +0000865using object::Binary;
866using object::ELFFile;
867using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000868using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000869
Eugene Leviant86b7f862019-06-13 09:56:14 +0000870class BasicELFBuilder {
871protected:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000872 uint16_t EMachine;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000873 std::unique_ptr<Object> Obj;
874
875 void initFileHeader();
876 void initHeaderSegment();
877 StringTableSection *addStrTab();
878 SymbolTableSection *addSymTab(StringTableSection *StrTab);
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000879 void initSections();
880
881public:
Eugene Leviant86b7f862019-06-13 09:56:14 +0000882 BasicELFBuilder(uint16_t EM)
883 : EMachine(EM), Obj(llvm::make_unique<Object>()) {}
884};
885
886class BinaryELFBuilder : public BasicELFBuilder {
887 MemoryBuffer *MemBuf;
888 void addData(SymbolTableSection *SymTab);
889
890public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000891 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
Eugene Leviant86b7f862019-06-13 09:56:14 +0000892 : BasicELFBuilder(EM), MemBuf(MB) {}
893
894 std::unique_ptr<Object> build();
895};
896
897class IHexELFBuilder : public BasicELFBuilder {
898 const std::vector<IHexRecord> &Records;
899
900 void addDataSections();
901
902public:
903 IHexELFBuilder(const std::vector<IHexRecord> &Records)
904 : BasicELFBuilder(ELF::EM_386), Records(Records) {}
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000905
906 std::unique_ptr<Object> build();
907};
908
Jake Ehrlich76e91102018-01-25 22:46:17 +0000909template <class ELFT> class ELFBuilder {
910private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000911 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000912 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000913 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000914
915 const ELFFile<ELFT> &ElfFile;
916 Object &Obj;
Peter Collingbourne9fa65382019-06-07 18:57:32 +0000917 size_t EhdrOffset = 0;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000918 Optional<StringRef> ExtractPartition;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000919
Jake Ehrlich6452b112018-02-14 23:31:33 +0000920 void setParentSegment(Segment &Child);
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000921 void readProgramHeaders(const ELFFile<ELFT> &HeadersFile);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000922 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000923 void initSymbolTable(SymbolTableSection *SymTab);
924 void readSectionHeaders();
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000925 void readSections();
926 void findEhdrOffset();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000927 SectionBase &makeSection(const Elf_Shdr &Shdr);
928
929public:
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000930 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj,
931 Optional<StringRef> ExtractPartition)
932 : ElfFile(*ElfObj.getELFFile()), Obj(Obj),
933 ExtractPartition(ExtractPartition) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000934
935 void build();
936};
937
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000938class BinaryReader : public Reader {
939 const MachineInfo &MInfo;
940 MemoryBuffer *MemBuf;
941
942public:
943 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
944 : MInfo(MI), MemBuf(MB) {}
945 std::unique_ptr<Object> create() const override;
946};
947
Eugene Leviant86b7f862019-06-13 09:56:14 +0000948class IHexReader : public Reader {
949 MemoryBuffer *MemBuf;
950
951 Expected<std::vector<IHexRecord>> parse() const;
952 Error parseError(size_t LineNo, Error E) const {
953 return LineNo == -1U
954 ? createFileError(MemBuf->getBufferIdentifier(), std::move(E))
955 : createFileError(MemBuf->getBufferIdentifier(), LineNo,
956 std::move(E));
957 }
958 template <typename... Ts>
959 Error parseError(size_t LineNo, char const *Fmt, const Ts &... Vals) const {
960 Error E = createStringError(errc::invalid_argument, Fmt, Vals...);
961 return parseError(LineNo, std::move(E));
962 }
963
964public:
965 IHexReader(MemoryBuffer *MB) : MemBuf(MB) {}
966
967 std::unique_ptr<Object> create() const override;
968};
969
Jake Ehrlich76e91102018-01-25 22:46:17 +0000970class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000971 Binary *Bin;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000972 Optional<StringRef> ExtractPartition;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000973
974public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000975 std::unique_ptr<Object> create() const override;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000976 explicit ELFReader(Binary *B, Optional<StringRef> ExtractPartition)
977 : Bin(B), ExtractPartition(ExtractPartition) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000978};
979
980class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000981private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000982 using SecPtr = std::unique_ptr<SectionBase>;
983 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000984
Petr Hosekc4df10e2017-08-04 21:09:26 +0000985 std::vector<SecPtr> Sections;
986 std::vector<SegPtr> Segments;
James Henderson1f448142019-03-25 16:36:26 +0000987 std::vector<SecPtr> RemovedSections;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000988
Petr Hosek05a04cb2017-08-01 00:33:58 +0000989public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000990 template <class T>
991 using Range = iterator_range<
992 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
993
994 template <class T>
995 using ConstRange = iterator_range<pointee_iterator<
996 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
997
Jake Ehrlich6452b112018-02-14 23:31:33 +0000998 // It is often the case that the ELF header and the program header table are
999 // not present in any segment. This could be a problem during file layout,
1000 // because other segments may get assigned an offset where either of the
1001 // two should reside, which will effectively corrupt the resulting binary.
1002 // Other than that we use these segments to track program header offsets
1003 // when they may not follow the ELF header.
1004 Segment ElfHdrSegment;
1005 Segment ProgramHdrSegment;
1006
George Rimar4ded7732018-12-20 10:51:42 +00001007 uint8_t OSABI;
1008 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001009 uint64_t Entry;
1010 uint64_t SHOffset;
1011 uint32_t Type;
1012 uint32_t Machine;
1013 uint32_t Version;
1014 uint32_t Flags;
1015
James Henderson38cb2382019-04-02 14:11:13 +00001016 bool HadShdrs = true;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001017 StringTableSection *SectionNames = nullptr;
1018 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001019 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001020
Aaron Ballman09f46a72018-01-25 21:03:38 +00001021 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001022 SectionTableRef sections() { return SectionTableRef(Sections); }
1023 ConstRange<SectionBase> sections() const {
1024 return make_pointee_range(Sections);
1025 }
Eugene Leviant51c1f642019-02-25 14:12:41 +00001026 SectionBase *findSection(StringRef Name) {
1027 auto SecIt =
1028 find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; });
1029 return SecIt == Sections.end() ? nullptr : SecIt->get();
1030 }
James Henderson1f448142019-03-25 16:36:26 +00001031 SectionTableRef removedSections() { return SectionTableRef(RemovedSections); }
1032
Jake Ehrlich76e91102018-01-25 22:46:17 +00001033 Range<Segment> segments() { return make_pointee_range(Segments); }
1034 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +00001035
James Henderson66a9d0f2019-04-18 09:13:30 +00001036 Error removeSections(bool AllowBrokenLinks,
1037 std::function<bool(const SectionBase &)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +00001038 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001039 template <class T, class... Ts> T &addSection(Ts &&... Args) {
1040 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
1041 auto Ptr = Sec.get();
1042 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001043 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001044 return *Ptr;
1045 }
James Henderson1f448142019-03-25 16:36:26 +00001046 Segment &addSegment(ArrayRef<uint8_t> Data) {
1047 Segments.emplace_back(llvm::make_unique<Segment>(Data));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001048 return *Segments.back();
1049 }
Petr Hosekc4df10e2017-08-04 21:09:26 +00001050};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +00001051
1052} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001053} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001054} // end namespace llvm
1055
1056#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H