blob: d74b5f410afb08fc49b6d704d17075401bcb3033 [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
Fangrui Songfe2da4e2019-09-04 09:35:32 +000060 iterator begin() const { return iterator(Sections.data()); }
61 iterator end() const { 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 Haehnle80660092019-06-26 19:16:35 +0000594 SYMBOL_LOPROC = ELF::SHN_LOPROC,
Nicolai Haehnle08e8cb52019-06-25 11:51:35 +0000595 SYMBOL_AMDGPU_LDS = ELF::SHN_AMDGPU_LDS,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000596 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 Haehnle80660092019-06-26 19:16:35 +0000600 SYMBOL_HIPROC = ELF::SHN_HIPROC,
601 SYMBOL_LOOS = ELF::SHN_LOOS,
602 SYMBOL_HIOS = ELF::SHN_HIOS,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000603 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000604};
605
Petr Hosek79cee9e2017-08-29 02:12:03 +0000606struct Symbol {
607 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000608 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000609 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000610 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000611 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000612 uint32_t NameIndex;
613 uint64_t Size;
614 uint8_t Type;
615 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000616 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000617 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000618
619 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000620 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000621};
622
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000623class SectionIndexSection : public SectionBase {
624 MAKE_SEC_WRITER_FRIEND
625
626private:
627 std::vector<uint32_t> Indexes;
628 SymbolTableSection *Symbols = nullptr;
629
630public:
631 virtual ~SectionIndexSection() {}
632 void addIndex(uint32_t Index) {
Eugene Leviant88089fe2019-04-12 11:59:30 +0000633 assert(Size > 0);
634 Indexes.push_back(Index);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000635 }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000636
637 void reserve(size_t NumSymbols) {
638 Indexes.reserve(NumSymbols);
639 Size = NumSymbols * 4;
640 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000641 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
642 void initialize(SectionTableRef SecTable) override;
643 void finalize() override;
644 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000645 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000646
647 SectionIndexSection() {
648 Name = ".symtab_shndx";
649 Align = 4;
650 EntrySize = 4;
651 Type = ELF::SHT_SYMTAB_SHNDX;
652 }
653};
654
Petr Hosek79cee9e2017-08-29 02:12:03 +0000655class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000656 MAKE_SEC_WRITER_FRIEND
657
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000658 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000659 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000660
Petr Hosek79cee9e2017-08-29 02:12:03 +0000661protected:
662 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000663 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000664 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000665
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000666 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000667
Petr Hosek79cee9e2017-08-29 02:12:03 +0000668public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000669 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 Rimar17dbb192019-05-08 07:31:05 +0000673 uint64_t SymbolSize);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000674 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000675 // An 'empty' symbol table still contains a null symbol.
676 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000677 void setShndxTable(SectionIndexSection *ShndxTable) {
678 SectionIndexTable = ShndxTable;
679 }
680 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000681 void fillShndxTable();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000682 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000683 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000684 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000685 void updateSymbols(function_ref<void(Symbol &)> Callable);
686
James Henderson66a9d0f2019-04-18 09:13:30 +0000687 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000688 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000689 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000690 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000691 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000692 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000693 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
George Rimar0373bed2019-03-20 13:57:47 +0000694 void replaceSectionReferences(
695 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000696
Petr Hosek79cee9e2017-08-29 02:12:03 +0000697 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000698 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000699 }
700};
701
Petr Hosekd7df9b22017-09-06 23:41:02 +0000702struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000703 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000704 uint64_t Offset;
705 uint64_t Addend;
706 uint32_t Type;
707};
708
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000709// 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).
718class RelocationSectionBase : public SectionBase {
719protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000720 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000721
722public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000723 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000724 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000725
726 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000727 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000728 }
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.
733template <class SymTabType>
734class RelocSectionWithSymtabBase : public RelocationSectionBase {
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000735 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000736
737protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000738 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000739
George Rimar79fb8582019-02-27 11:18:27 +0000740 SymTabType *Symbols = nullptr;
741
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000742public:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000743 void initialize(SectionTableRef SecTable) override;
744 void finalize() override;
745};
746
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000747class RelocationSection
748 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000749 MAKE_SEC_WRITER_FRIEND
750
Petr Hosekd7df9b22017-09-06 23:41:02 +0000751 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000752
Petr Hosekd7df9b22017-09-06 23:41:02 +0000753public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000754 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000755 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000756 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000757 Error removeSectionReferences(bool AllowBrokenLinks,
George Rimar79fb8582019-02-27 11:18:27 +0000758 function_ref<bool(const SectionBase *)> ToRemove) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000759 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000760 void markSymbols() override;
George Rimard8a5c6c2019-03-11 11:01:24 +0000761 void replaceSectionReferences(
762 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000763
Petr Hosekd7df9b22017-09-06 23:41:02 +0000764 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000765 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000766 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000767 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000768 }
769};
770
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000771// TODO: The way stripping and groups interact is complicated
772// and still needs to be worked on.
773
774class GroupSection : public SectionBase {
775 MAKE_SEC_WRITER_FRIEND
776 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000777 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000778 ELF::Elf32_Word FlagWord;
779 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000780
781public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000782 // 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 Shaposhnikov3b24ed72018-03-20 19:46:00 +0000785
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000786 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
787
788 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000789 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000790 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
791 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
792
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000793 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000794 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000795 void finalize() override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000796 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000797 void markSymbols() override;
George Rimar27257172019-03-24 14:41:45 +0000798 void replaceSectionReferences(
799 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000800
801 static bool classof(const SectionBase *S) {
802 return S->Type == ELF::SHT_GROUP;
803 }
804};
805
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000806class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000807public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000808 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000809
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000810 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000811 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000812 }
813};
814
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000815class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000816public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000817 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000818
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000819 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000820 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000821 }
822};
823
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000824class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000825 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000826 MAKE_SEC_WRITER_FRIEND
827
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000828private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000829 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000830
831public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000832 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Leviant86b7f862019-06-13 09:56:14 +0000833
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 Ehrlich9f1a3902017-09-26 18:02:25 +0000842 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000843 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000844 }
845};
846
Jake Ehrlich76e91102018-01-25 22:46:17 +0000847class GnuDebugLinkSection : public SectionBase {
848 MAKE_SEC_WRITER_FRIEND
849
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000850private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000851 StringRef FileName;
852 uint32_t CRC32;
853
James Henderson9df38832019-05-14 10:59:04 +0000854 void init(StringRef File);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000855
856public:
857 // If we add this section from an external source we can use this ctor.
James Henderson9df38832019-05-14 10:59:04 +0000858 explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000859 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000860 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000861};
862
Jake Ehrlich76e91102018-01-25 22:46:17 +0000863class Reader {
864public:
865 virtual ~Reader();
866 virtual std::unique_ptr<Object> create() const = 0;
867};
868
Jake Ehrlich76e91102018-01-25 22:46:17 +0000869using object::Binary;
870using object::ELFFile;
871using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000872using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000873
Eugene Leviant86b7f862019-06-13 09:56:14 +0000874class BasicELFBuilder {
875protected:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000876 std::unique_ptr<Object> Obj;
877
878 void initFileHeader();
879 void initHeaderSegment();
880 StringTableSection *addStrTab();
881 SymbolTableSection *addSymTab(StringTableSection *StrTab);
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000882 void initSections();
883
884public:
Fangrui Song2f519d72019-09-14 01:36:31 +0000885 BasicELFBuilder() : Obj(std::make_unique<Object>()) {}
Eugene Leviant86b7f862019-06-13 09:56:14 +0000886};
887
888class BinaryELFBuilder : public BasicELFBuilder {
889 MemoryBuffer *MemBuf;
Chris Jacksonfa1fe932019-08-30 10:17:16 +0000890 uint8_t NewSymbolVisibility;
Eugene Leviant86b7f862019-06-13 09:56:14 +0000891 void addData(SymbolTableSection *SymTab);
892
893public:
Fangrui Song2f519d72019-09-14 01:36:31 +0000894 BinaryELFBuilder(MemoryBuffer *MB, uint8_t NewSymbolVisibility)
895 : BasicELFBuilder(), MemBuf(MB),
Chris Jacksonfa1fe932019-08-30 10:17:16 +0000896 NewSymbolVisibility(NewSymbolVisibility) {}
Eugene Leviant86b7f862019-06-13 09:56:14 +0000897
898 std::unique_ptr<Object> build();
899};
900
901class IHexELFBuilder : public BasicELFBuilder {
902 const std::vector<IHexRecord> &Records;
903
904 void addDataSections();
905
906public:
907 IHexELFBuilder(const std::vector<IHexRecord> &Records)
Fangrui Song2f519d72019-09-14 01:36:31 +0000908 : BasicELFBuilder(), Records(Records) {}
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000909
910 std::unique_ptr<Object> build();
911};
912
Jake Ehrlich76e91102018-01-25 22:46:17 +0000913template <class ELFT> class ELFBuilder {
914private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000915 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000916 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000917 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000918
919 const ELFFile<ELFT> &ElfFile;
920 Object &Obj;
Peter Collingbourne9fa65382019-06-07 18:57:32 +0000921 size_t EhdrOffset = 0;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000922 Optional<StringRef> ExtractPartition;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000923
Jake Ehrlich6452b112018-02-14 23:31:33 +0000924 void setParentSegment(Segment &Child);
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000925 void readProgramHeaders(const ELFFile<ELFT> &HeadersFile);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000926 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000927 void initSymbolTable(SymbolTableSection *SymTab);
928 void readSectionHeaders();
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000929 void readSections();
930 void findEhdrOffset();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000931 SectionBase &makeSection(const Elf_Shdr &Shdr);
932
933public:
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000934 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj,
935 Optional<StringRef> ExtractPartition)
936 : ElfFile(*ElfObj.getELFFile()), Obj(Obj),
937 ExtractPartition(ExtractPartition) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000938
939 void build();
940};
941
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000942class BinaryReader : public Reader {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000943 MemoryBuffer *MemBuf;
Chris Jacksonfa1fe932019-08-30 10:17:16 +0000944 uint8_t NewSymbolVisibility;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000945
946public:
Fangrui Song2f519d72019-09-14 01:36:31 +0000947 BinaryReader(MemoryBuffer *MB, const uint8_t NewSymbolVisibility)
948 : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {}
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000949 std::unique_ptr<Object> create() const override;
950};
951
Eugene Leviant86b7f862019-06-13 09:56:14 +0000952class 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
968public:
969 IHexReader(MemoryBuffer *MB) : MemBuf(MB) {}
970
971 std::unique_ptr<Object> create() const override;
972};
973
Jake Ehrlich76e91102018-01-25 22:46:17 +0000974class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000975 Binary *Bin;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000976 Optional<StringRef> ExtractPartition;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000977
978public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000979 std::unique_ptr<Object> create() const override;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000980 explicit ELFReader(Binary *B, Optional<StringRef> ExtractPartition)
981 : Bin(B), ExtractPartition(ExtractPartition) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000982};
983
984class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000985private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000986 using SecPtr = std::unique_ptr<SectionBase>;
987 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000988
Petr Hosekc4df10e2017-08-04 21:09:26 +0000989 std::vector<SecPtr> Sections;
990 std::vector<SegPtr> Segments;
James Henderson1f448142019-03-25 16:36:26 +0000991 std::vector<SecPtr> RemovedSections;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000992
Fangrui Songfe2da4e2019-09-04 09:35:32 +0000993 static bool sectionIsAlloc(const SectionBase &Sec) {
994 return Sec.Flags & ELF::SHF_ALLOC;
995 };
996
Petr Hosek05a04cb2017-08-01 00:33:58 +0000997public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000998 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 Ehrlich6452b112018-02-14 23:31:33 +00001006 // 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 Rimar4ded7732018-12-20 10:51:42 +00001015 uint8_t OSABI;
1016 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001017 uint64_t Entry;
Fangrui Song78b69092019-09-07 01:38:56 +00001018 uint64_t SHOff;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001019 uint32_t Type;
1020 uint32_t Machine;
1021 uint32_t Version;
1022 uint32_t Flags;
1023
James Henderson38cb2382019-04-02 14:11:13 +00001024 bool HadShdrs = true;
Eugene Leviantc7e6d142019-07-23 08:03:30 +00001025 bool MustBeRelocatable = false;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001026 StringTableSection *SectionNames = nullptr;
1027 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001028 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001029
Aaron Ballman09f46a72018-01-25 21:03:38 +00001030 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001031 SectionTableRef sections() { return SectionTableRef(Sections); }
1032 ConstRange<SectionBase> sections() const {
1033 return make_pointee_range(Sections);
1034 }
Fangrui Songfe2da4e2019-09-04 09:35:32 +00001035 iterator_range<
1036 filter_iterator<pointee_iterator<std::vector<SecPtr>::const_iterator>,
1037 decltype(&sectionIsAlloc)>>
1038 allocSections() const {
1039 return make_filter_range(make_pointee_range(Sections), sectionIsAlloc);
1040 }
1041
Eugene Leviant51c1f642019-02-25 14:12:41 +00001042 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 Henderson1f448142019-03-25 16:36:26 +00001047 SectionTableRef removedSections() { return SectionTableRef(RemovedSections); }
1048
Jake Ehrlich76e91102018-01-25 22:46:17 +00001049 Range<Segment> segments() { return make_pointee_range(Segments); }
1050 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +00001051
James Henderson66a9d0f2019-04-18 09:13:30 +00001052 Error removeSections(bool AllowBrokenLinks,
1053 std::function<bool(const SectionBase &)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +00001054 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001055 template <class T, class... Ts> T &addSection(Ts &&... Args) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001056 auto Sec = std::make_unique<T>(std::forward<Ts>(Args)...);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001057 auto Ptr = Sec.get();
Eugene Leviantc7e6d142019-07-23 08:03:30 +00001058 MustBeRelocatable |= isa<RelocationSection>(*Ptr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001059 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001060 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001061 return *Ptr;
1062 }
James Henderson1f448142019-03-25 16:36:26 +00001063 Segment &addSegment(ArrayRef<uint8_t> Data) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001064 Segments.emplace_back(std::make_unique<Segment>(Data));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001065 return *Segments.back();
1066 }
Eugene Leviantc7e6d142019-07-23 08:03:30 +00001067 bool isRelocatable() const {
1068 return (Type != ELF::ET_DYN && Type != ELF::ET_EXEC) || MustBeRelocatable;
1069 }
Petr Hosekc4df10e2017-08-04 21:09:26 +00001070};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +00001071
1072} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001073} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001074} // end namespace llvm
1075
1076#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H