blob: af37a2bbc625db63782d7c8db5ec2f35e1024a88 [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
219 // Calculates checksum of stringified record representation
220 // S must NOT contain leading ':' and trailing whitespace
221 // characters
222 static uint8_t getChecksum(StringRef S);
223
224 enum Type {
225 // Contains data and a 16-bit starting address for the data.
226 // The byte count specifies number of data bytes in the record.
227 Data = 0,
228 // Must occur exactly once per file in the last line of the file.
229 // The data field is empty (thus byte count is 00) and the address
230 // field is typically 0000.
231 EndOfFile = 1,
232 // The data field contains a 16-bit segment base address (thus byte
233 // count is always 02) compatible with 80x86 real mode addressing.
234 // The address field (typically 0000) is ignored. The segment address
235 // from the most recent 02 record is multiplied by 16 and added to each
236 // subsequent data record address to form the physical starting address
237 // for the data. This allows addressing up to one megabyte of address
238 // space.
239 SegmentAddr = 2,
240 // or 80x86 processors, specifies the initial content of the CS:IP
241 // registers. The address field is 0000, the byte count is always 04,
242 // the first two data bytes are the CS value, the latter two are the
243 // IP value.
244 StartAddr80x86 = 3,
245 // Allows for 32 bit addressing (up to 4GiB). The record's address field
246 // is ignored (typically 0000) and its byte count is always 02. The two
247 // data bytes (big endian) specify the upper 16 bits of the 32 bit
248 // absolute address for all subsequent type 00 records
249 ExtendedAddr = 4,
250 // The address field is 0000 (not used) and the byte count is always 04.
251 // The four data bytes represent a 32-bit address value. In the case of
252 // 80386 and higher CPUs, this address is loaded into the EIP register.
253 StartAddr = 5,
254 // We have no other valid types
255 InvalidType = 6
256 };
257};
258
259// Base class for IHexSectionWriter. This class implements writing algorithm,
260// but doesn't actually write records. It is used for output buffer size
261// calculation in IHexWriter::finalize.
262class IHexSectionWriterBase : public BinarySectionWriter {
263 // 20-bit segment address
264 uint32_t SegmentAddr = 0;
265 // Extended linear address
266 uint32_t BaseAddr = 0;
267
268 // Write segment address corresponding to 'Addr'
269 uint64_t writeSegmentAddr(uint64_t Addr);
270 // Write extended linear (base) address corresponding to 'Addr'
271 uint64_t writeBaseAddr(uint64_t Addr);
272
273protected:
274 // Offset in the output buffer
275 uint64_t Offset = 0;
276
277 void writeSection(const SectionBase *Sec, ArrayRef<uint8_t> Data);
278 virtual void writeData(uint8_t Type, uint16_t Addr, ArrayRef<uint8_t> Data);
279
280public:
281 explicit IHexSectionWriterBase(Buffer &Buf) : BinarySectionWriter(Buf) {}
282
283 uint64_t getBufferOffset() const { return Offset; }
284 void visit(const Section &Sec) final;
285 void visit(const OwnedDataSection &Sec) final;
286 void visit(const StringTableSection &Sec) override;
287 void visit(const DynamicRelocationSection &Sec) final;
288 using BinarySectionWriter::visit;
289};
290
291// Real IHEX section writer
292class IHexSectionWriter : public IHexSectionWriterBase {
293public:
294 IHexSectionWriter(Buffer &Buf) : IHexSectionWriterBase(Buf) {}
295
296 void writeData(uint8_t Type, uint16_t Addr, ArrayRef<uint8_t> Data) override;
297 void visit(const StringTableSection &Sec) override;
298};
299
Jake Ehrlich76e91102018-01-25 22:46:17 +0000300class Writer {
301protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000302 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000303 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000304
305public:
306 virtual ~Writer();
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000307 virtual Error finalize() = 0;
308 virtual Error write() = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000309
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000310 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000311};
312
313template <class ELFT> class ELFWriter : public Writer {
314private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000315 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000316 using Elf_Shdr = typename ELFT::Shdr;
317 using Elf_Phdr = typename ELFT::Phdr;
318 using Elf_Ehdr = typename ELFT::Ehdr;
319
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000320 void initEhdrSegment();
321
Jake Ehrlich76e91102018-01-25 22:46:17 +0000322 void writeEhdr();
323 void writePhdr(const Segment &Seg);
324 void writeShdr(const SectionBase &Sec);
325
326 void writePhdrs();
327 void writeShdrs();
328 void writeSectionData();
James Henderson1f448142019-03-25 16:36:26 +0000329 void writeSegmentData();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000330
331 void assignOffsets();
332
333 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
334
335 size_t totalSize() const;
336
337public:
338 virtual ~ELFWriter() {}
James Henderson38cb2382019-04-02 14:11:13 +0000339 bool WriteSectionHeaders;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000340
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000341 Error finalize() override;
342 Error write() override;
James Henderson38cb2382019-04-02 14:11:13 +0000343 ELFWriter(Object &Obj, Buffer &Buf, bool WSH);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000344};
345
346class BinaryWriter : public Writer {
347private:
348 std::unique_ptr<BinarySectionWriter> SecWriter;
349
350 uint64_t TotalSize;
351
352public:
353 ~BinaryWriter() {}
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000354 Error finalize() override;
355 Error write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000356 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000357};
358
Eugene Levianta6fb1832019-05-29 11:37:16 +0000359class IHexWriter : public Writer {
360 struct SectionCompare {
361 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const;
362 };
363
364 std::set<const SectionBase *, SectionCompare> Sections;
365 size_t TotalSize;
366
367 Error checkSection(const SectionBase &Sec);
368 uint64_t writeEntryPointRecord(uint8_t *Buf);
369 uint64_t writeEndOfFileRecord(uint8_t *Buf);
370
371public:
372 ~IHexWriter() {}
373 Error finalize() override;
374 Error write() override;
375 IHexWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
376};
377
Petr Hosek05a04cb2017-08-01 00:33:58 +0000378class SectionBase {
379public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000380 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000381 Segment *ParentSegment = nullptr;
382 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000383 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000384 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000385 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000386
387 uint64_t Addr = 0;
388 uint64_t Align = 1;
389 uint32_t EntrySize = 0;
390 uint64_t Flags = 0;
391 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000392 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000393 uint64_t NameIndex = 0;
394 uint64_t Offset = 0;
395 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000396 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000397 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000398
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000399 SectionBase() = default;
400 SectionBase(const SectionBase &) = default;
401
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000402 virtual ~SectionBase() = default;
403
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000404 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000405 virtual void finalize();
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000406 // Remove references to these sections. The list of sections must be sorted.
407 virtual Error
James Henderson66a9d0f2019-04-18 09:13:30 +0000408 removeSectionReferences(bool AllowBrokenLinks,
409 function_ref<bool(const SectionBase *)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000410 virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000411 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000412 virtual void accept(MutableSectionVisitor &Visitor) = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000413 virtual void markSymbols();
George Rimard8a5c6c2019-03-11 11:01:24 +0000414 virtual void
415 replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000416};
417
418class Segment {
419private:
420 struct SectionCompare {
421 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
422 // Some sections might have the same address if one of them is empty. To
423 // fix this we can use the lexicographic ordering on ->Addr and the
424 // address of the actully stored section.
425 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
426 return Lhs < Rhs;
427 return Lhs->OriginalOffset < Rhs->OriginalOffset;
428 }
429 };
430
431 std::set<const SectionBase *, SectionCompare> Sections;
432
433public:
Fangrui Song967ce402018-12-12 22:46:37 +0000434 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000435 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000436 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000437 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000438 uint64_t PAddr;
439 uint64_t FileSize;
440 uint64_t MemSize;
441 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000442
Fangrui Song967ce402018-12-12 22:46:37 +0000443 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000444 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000445 Segment *ParentSegment = nullptr;
James Henderson1f448142019-03-25 16:36:26 +0000446 ArrayRef<uint8_t> Contents;
447
448 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
449 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000450
Petr Hosek05a04cb2017-08-01 00:33:58 +0000451 const SectionBase *firstSection() const {
452 if (!Sections.empty())
453 return *Sections.begin();
454 return nullptr;
455 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000456
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000457 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
458 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
James Henderson1f448142019-03-25 16:36:26 +0000459
460 ArrayRef<uint8_t> getContents() const { return Contents; }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000461};
462
463class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000464 MAKE_SEC_WRITER_FRIEND
465
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000466 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000467 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000468
469public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000470 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000471
Jake Ehrlich76e91102018-01-25 22:46:17 +0000472 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000473 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000474 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000475 function_ref<bool(const SectionBase *)> ToRemove) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000476 void initialize(SectionTableRef SecTable) override;
477 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000478};
479
Jake Ehrliche8437de2017-12-19 00:47:30 +0000480class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000481 MAKE_SEC_WRITER_FRIEND
482
Jake Ehrliche8437de2017-12-19 00:47:30 +0000483 std::vector<uint8_t> Data;
484
485public:
486 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
487 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000488 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000489 Type = ELF::SHT_PROGBITS;
490 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000491 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000492 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000493
Eugene Levianta6fb1832019-05-29 11:37:16 +0000494 OwnedDataSection(const Twine &SecName, uint64_t SecAddr, uint64_t SecFlags,
495 uint64_t SecOff) {
496 Name = SecName.str();
497 Type = ELF::SHT_PROGBITS;
498 Addr = SecAddr;
499 Flags = SecFlags;
500 OriginalOffset = SecOff;
501 }
502
503 void appendHexData(StringRef HexData);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000504 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000505 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000506};
507
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000508class CompressedSection : public SectionBase {
509 MAKE_SEC_WRITER_FRIEND
510
511 DebugCompressionType CompressionType;
512 uint64_t DecompressedSize;
513 uint64_t DecompressedAlign;
514 SmallVector<char, 128> CompressedData;
515
516public:
517 CompressedSection(const SectionBase &Sec,
518 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000519 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
520 uint64_t DecompressedAlign);
521
522 uint64_t getDecompressedSize() const { return DecompressedSize; }
523 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
524
525 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000526 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000527
528 static bool classof(const SectionBase *S) {
529 return (S->Flags & ELF::SHF_COMPRESSED) ||
530 (StringRef(S->Name).startswith(".zdebug"));
531 }
532};
533
534class DecompressedSection : public SectionBase {
535 MAKE_SEC_WRITER_FRIEND
536
537public:
538 explicit DecompressedSection(const CompressedSection &Sec)
539 : SectionBase(Sec) {
540 Size = Sec.getDecompressedSize();
541 Align = Sec.getDecompressedAlign();
542 Flags = (Flags & ~ELF::SHF_COMPRESSED);
543 if (StringRef(Name).startswith(".zdebug"))
544 Name = "." + Name.substr(2);
545 }
546
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000547 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000548 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000549};
550
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000551// There are two types of string tables that can exist, dynamic and not dynamic.
552// In the dynamic case the string table is allocated. Changing a dynamic string
553// table would mean altering virtual addresses and thus the memory image. So
554// dynamic string tables should not have an interface to modify them or
555// reconstruct them. This type lets us reconstruct a string table. To avoid
556// this class being used for dynamic string tables (which has happened) the
557// classof method checks that the particular instance is not allocated. This
558// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000559class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000560 MAKE_SEC_WRITER_FRIEND
561
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000562 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000563
564public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000565 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
566 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000567 }
568
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000569 void addString(StringRef Name);
570 uint32_t findIndex(StringRef Name) const;
George Rimarfaf308b2019-03-18 14:27:41 +0000571 void prepareForLayout();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000572 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000573 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000574
Petr Hosek05a04cb2017-08-01 00:33:58 +0000575 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000576 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000577 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000578 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000579 }
580};
581
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000582// Symbols have a st_shndx field that normally stores an index but occasionally
583// stores a different special value. This enum keeps track of what the st_shndx
584// field means. Most of the values are just copies of the special SHN_* values.
585// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
586enum SymbolShndxType {
587 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000588 SYMBOL_ABS = ELF::SHN_ABS,
589 SYMBOL_COMMON = ELF::SHN_COMMON,
590 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
591 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
592 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
593 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000594 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000595};
596
Petr Hosek79cee9e2017-08-29 02:12:03 +0000597struct Symbol {
598 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000599 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000600 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000601 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000602 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000603 uint32_t NameIndex;
604 uint64_t Size;
605 uint8_t Type;
606 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000607 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000608 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000609
610 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000611 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000612};
613
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000614class SectionIndexSection : public SectionBase {
615 MAKE_SEC_WRITER_FRIEND
616
617private:
618 std::vector<uint32_t> Indexes;
619 SymbolTableSection *Symbols = nullptr;
620
621public:
622 virtual ~SectionIndexSection() {}
623 void addIndex(uint32_t Index) {
Eugene Leviant88089fe2019-04-12 11:59:30 +0000624 assert(Size > 0);
625 Indexes.push_back(Index);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000626 }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000627
628 void reserve(size_t NumSymbols) {
629 Indexes.reserve(NumSymbols);
630 Size = NumSymbols * 4;
631 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000632 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
633 void initialize(SectionTableRef SecTable) override;
634 void finalize() override;
635 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000636 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000637
638 SectionIndexSection() {
639 Name = ".symtab_shndx";
640 Align = 4;
641 EntrySize = 4;
642 Type = ELF::SHT_SYMTAB_SHNDX;
643 }
644};
645
Petr Hosek79cee9e2017-08-29 02:12:03 +0000646class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000647 MAKE_SEC_WRITER_FRIEND
648
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000649 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000650 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000651
Petr Hosek79cee9e2017-08-29 02:12:03 +0000652protected:
653 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000654 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000655 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000656
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000657 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000658
Petr Hosek79cee9e2017-08-29 02:12:03 +0000659public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000660 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
661
662 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
663 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
George Rimar17dbb192019-05-08 07:31:05 +0000664 uint64_t SymbolSize);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000665 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000666 // An 'empty' symbol table still contains a null symbol.
667 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000668 void setShndxTable(SectionIndexSection *ShndxTable) {
669 SectionIndexTable = ShndxTable;
670 }
671 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000672 void fillShndxTable();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000673 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000674 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000675 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000676 void updateSymbols(function_ref<void(Symbol &)> Callable);
677
James Henderson66a9d0f2019-04-18 09:13:30 +0000678 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000679 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000680 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000681 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000682 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000683 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000684 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
George Rimar0373bed2019-03-20 13:57:47 +0000685 void replaceSectionReferences(
686 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000687
Petr Hosek79cee9e2017-08-29 02:12:03 +0000688 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000689 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000690 }
691};
692
Petr Hosekd7df9b22017-09-06 23:41:02 +0000693struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000694 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000695 uint64_t Offset;
696 uint64_t Addend;
697 uint32_t Type;
698};
699
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000700// All relocation sections denote relocations to apply to another section.
701// However, some relocation sections use a dynamic symbol table and others use
702// a regular symbol table. Because the types of the two symbol tables differ in
703// our system (because they should behave differently) we can't uniformly
704// represent all relocations with the same base class if we expose an interface
705// that mentions the symbol table type. So we split the two base types into two
706// different classes, one which handles the section the relocation is applied to
707// and another which handles the symbol table type. The symbol table type is
708// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
709class RelocationSectionBase : public SectionBase {
710protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000711 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000712
713public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000714 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000715 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000716
717 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000718 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000719 }
720};
721
722// Takes the symbol table type to use as a parameter so that we can deduplicate
723// that code between the two symbol table types.
724template <class SymTabType>
725class RelocSectionWithSymtabBase : public RelocationSectionBase {
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000726 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000727
728protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000729 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000730
George Rimar79fb8582019-02-27 11:18:27 +0000731 SymTabType *Symbols = nullptr;
732
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000733public:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000734 void initialize(SectionTableRef SecTable) override;
735 void finalize() override;
736};
737
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000738class RelocationSection
739 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000740 MAKE_SEC_WRITER_FRIEND
741
Petr Hosekd7df9b22017-09-06 23:41:02 +0000742 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000743
Petr Hosekd7df9b22017-09-06 23:41:02 +0000744public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000745 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000746 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000747 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000748 Error removeSectionReferences(bool AllowBrokenLinks,
George Rimar79fb8582019-02-27 11:18:27 +0000749 function_ref<bool(const SectionBase *)> ToRemove) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000750 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000751 void markSymbols() override;
George Rimard8a5c6c2019-03-11 11:01:24 +0000752 void replaceSectionReferences(
753 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000754
Petr Hosekd7df9b22017-09-06 23:41:02 +0000755 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000756 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000757 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000758 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000759 }
760};
761
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000762// TODO: The way stripping and groups interact is complicated
763// and still needs to be worked on.
764
765class GroupSection : public SectionBase {
766 MAKE_SEC_WRITER_FRIEND
767 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000768 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000769 ELF::Elf32_Word FlagWord;
770 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000771
772public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000773 // TODO: Contents is present in several classes of the hierarchy.
774 // This needs to be refactored to avoid duplication.
775 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000776
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000777 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
778
779 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000780 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000781 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
782 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
783
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000784 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000785 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000786 void finalize() override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000787 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000788 void markSymbols() override;
George Rimar27257172019-03-24 14:41:45 +0000789 void replaceSectionReferences(
790 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000791
792 static bool classof(const SectionBase *S) {
793 return S->Type == ELF::SHT_GROUP;
794 }
795};
796
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000797class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000798public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000799 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000800
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000801 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000802 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000803 }
804};
805
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000806class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000807public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000808 explicit DynamicSection(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_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000812 }
813};
814
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000815class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000816 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000817 MAKE_SEC_WRITER_FRIEND
818
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000819private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000820 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000821
822public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000823 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
George Rimar67f590e2019-04-30 11:02:09 +0000824
825 void accept(SectionVisitor &) const override;
826 void accept(MutableSectionVisitor &Visitor) override;
827 Error removeSectionReferences(
828 bool AllowBrokenLinks,
829 function_ref<bool(const SectionBase *)> ToRemove) override;
830
831 static bool classof(const SectionBase *S) {
832 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000833 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000834 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000835 }
836};
837
Jake Ehrlich76e91102018-01-25 22:46:17 +0000838class GnuDebugLinkSection : public SectionBase {
839 MAKE_SEC_WRITER_FRIEND
840
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000841private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000842 StringRef FileName;
843 uint32_t CRC32;
844
James Henderson9df38832019-05-14 10:59:04 +0000845 void init(StringRef File);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000846
847public:
848 // If we add this section from an external source we can use this ctor.
James Henderson9df38832019-05-14 10:59:04 +0000849 explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000850 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000851 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000852};
853
Jake Ehrlich76e91102018-01-25 22:46:17 +0000854class Reader {
855public:
856 virtual ~Reader();
857 virtual std::unique_ptr<Object> create() const = 0;
858};
859
Jake Ehrlich76e91102018-01-25 22:46:17 +0000860using object::Binary;
861using object::ELFFile;
862using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000863using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000864
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000865class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000866 uint16_t EMachine;
867 MemoryBuffer *MemBuf;
868 std::unique_ptr<Object> Obj;
869
870 void initFileHeader();
871 void initHeaderSegment();
872 StringTableSection *addStrTab();
873 SymbolTableSection *addSymTab(StringTableSection *StrTab);
874 void addData(SymbolTableSection *SymTab);
875 void initSections();
876
877public:
878 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
879 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
880
881 std::unique_ptr<Object> build();
882};
883
Jake Ehrlich76e91102018-01-25 22:46:17 +0000884template <class ELFT> class ELFBuilder {
885private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000886 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000887 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000888 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000889
890 const ELFFile<ELFT> &ElfFile;
891 Object &Obj;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000892 uint64_t EhdrOffset = 0;
893 Optional<StringRef> ExtractPartition;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000894
Jake Ehrlich6452b112018-02-14 23:31:33 +0000895 void setParentSegment(Segment &Child);
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000896 void readProgramHeaders(const ELFFile<ELFT> &HeadersFile);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000897 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000898 void initSymbolTable(SymbolTableSection *SymTab);
899 void readSectionHeaders();
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000900 void readSections();
901 void findEhdrOffset();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000902 SectionBase &makeSection(const Elf_Shdr &Shdr);
903
904public:
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000905 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj,
906 Optional<StringRef> ExtractPartition)
907 : ElfFile(*ElfObj.getELFFile()), Obj(Obj),
908 ExtractPartition(ExtractPartition) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000909
910 void build();
911};
912
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000913class BinaryReader : public Reader {
914 const MachineInfo &MInfo;
915 MemoryBuffer *MemBuf;
916
917public:
918 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
919 : MInfo(MI), MemBuf(MB) {}
920 std::unique_ptr<Object> create() const override;
921};
922
Jake Ehrlich76e91102018-01-25 22:46:17 +0000923class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000924 Binary *Bin;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000925 Optional<StringRef> ExtractPartition;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000926
927public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000928 std::unique_ptr<Object> create() const override;
Peter Collingbourne8d58a982019-06-07 17:57:48 +0000929 explicit ELFReader(Binary *B, Optional<StringRef> ExtractPartition)
930 : Bin(B), ExtractPartition(ExtractPartition) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000931};
932
933class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000934private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000935 using SecPtr = std::unique_ptr<SectionBase>;
936 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000937
Petr Hosekc4df10e2017-08-04 21:09:26 +0000938 std::vector<SecPtr> Sections;
939 std::vector<SegPtr> Segments;
James Henderson1f448142019-03-25 16:36:26 +0000940 std::vector<SecPtr> RemovedSections;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000941
Petr Hosek05a04cb2017-08-01 00:33:58 +0000942public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000943 template <class T>
944 using Range = iterator_range<
945 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
946
947 template <class T>
948 using ConstRange = iterator_range<pointee_iterator<
949 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
950
Jake Ehrlich6452b112018-02-14 23:31:33 +0000951 // It is often the case that the ELF header and the program header table are
952 // not present in any segment. This could be a problem during file layout,
953 // because other segments may get assigned an offset where either of the
954 // two should reside, which will effectively corrupt the resulting binary.
955 // Other than that we use these segments to track program header offsets
956 // when they may not follow the ELF header.
957 Segment ElfHdrSegment;
958 Segment ProgramHdrSegment;
959
George Rimar4ded7732018-12-20 10:51:42 +0000960 uint8_t OSABI;
961 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000962 uint64_t Entry;
963 uint64_t SHOffset;
964 uint32_t Type;
965 uint32_t Machine;
966 uint32_t Version;
967 uint32_t Flags;
968
James Henderson38cb2382019-04-02 14:11:13 +0000969 bool HadShdrs = true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000970 StringTableSection *SectionNames = nullptr;
971 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000972 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000973
Aaron Ballman09f46a72018-01-25 21:03:38 +0000974 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000975 SectionTableRef sections() { return SectionTableRef(Sections); }
976 ConstRange<SectionBase> sections() const {
977 return make_pointee_range(Sections);
978 }
Eugene Leviant51c1f642019-02-25 14:12:41 +0000979 SectionBase *findSection(StringRef Name) {
980 auto SecIt =
981 find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; });
982 return SecIt == Sections.end() ? nullptr : SecIt->get();
983 }
James Henderson1f448142019-03-25 16:36:26 +0000984 SectionTableRef removedSections() { return SectionTableRef(RemovedSections); }
985
Jake Ehrlich76e91102018-01-25 22:46:17 +0000986 Range<Segment> segments() { return make_pointee_range(Segments); }
987 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000988
James Henderson66a9d0f2019-04-18 09:13:30 +0000989 Error removeSections(bool AllowBrokenLinks,
990 std::function<bool(const SectionBase &)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000991 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000992 template <class T, class... Ts> T &addSection(Ts &&... Args) {
993 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
994 auto Ptr = Sec.get();
995 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000996 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000997 return *Ptr;
998 }
James Henderson1f448142019-03-25 16:36:26 +0000999 Segment &addSegment(ArrayRef<uint8_t> Data) {
1000 Segments.emplace_back(llvm::make_unique<Segment>(Data));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001001 return *Segments.back();
1002 }
Petr Hosekc4df10e2017-08-04 21:09:26 +00001003};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +00001004
1005} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001006} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001007} // end namespace llvm
1008
1009#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H