blob: 42b2733dc1a5c3541d14d1d8d1fe3c2798e9cde9 [file] [log] [blame]
Petr Hosek05a04cb2017-08-01 00:33:58 +00001//===- Object.h -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000010#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
11#define LLVM_TOOLS_OBJCOPY_OBJECT_H
Petr Hosek05a04cb2017-08-01 00:33:58 +000012
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000013#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000017#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELFObjectFile.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000019#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000020#include "llvm/Support/JamCRC.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000021#include <cstddef>
22#include <cstdint>
23#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000024#include <memory>
25#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000026#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000027
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000028namespace llvm {
Puyan Lotfi99124cc2018-09-07 08:10:22 +000029enum class DebugCompressionType;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000030namespace objcopy {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000031
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000032class Buffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000033class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000034class Section;
35class OwnedDataSection;
36class StringTableSection;
37class SymbolTableSection;
38class RelocationSection;
39class DynamicRelocationSection;
40class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000041class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000042class SectionIndexSection;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000043class CompressedSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000044class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000045class Object;
Paul Semel4246a462018-05-09 21:36:54 +000046struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000047
48class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000049 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000050
51public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000052 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
53
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000054 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000055 : Sections(Secs) {}
56 SectionTableRef(const SectionTableRef &) = default;
57
Jake Ehrlich76e91102018-01-25 22:46:17 +000058 iterator begin() { return iterator(Sections.data()); }
59 iterator end() { return iterator(Sections.data() + Sections.size()); }
60
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000061 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000062
63 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000064 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000065};
Petr Hosek05a04cb2017-08-01 00:33:58 +000066
Jake Ehrlich76e91102018-01-25 22:46:17 +000067enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
68
Jordan Rupprechtcf676332018-08-17 18:51:11 +000069// This type keeps track of the machine info for various architectures. This
70// lets us map architecture names to ELF types and the e_machine value of the
71// ELF file.
72struct MachineInfo {
73 uint16_t EMachine;
74 bool Is64Bit;
75 bool IsLittleEndian;
76};
77
Jake Ehrlich76e91102018-01-25 22:46:17 +000078class SectionVisitor {
79public:
80 virtual ~SectionVisitor();
81
82 virtual void visit(const Section &Sec) = 0;
83 virtual void visit(const OwnedDataSection &Sec) = 0;
84 virtual void visit(const StringTableSection &Sec) = 0;
85 virtual void visit(const SymbolTableSection &Sec) = 0;
86 virtual void visit(const RelocationSection &Sec) = 0;
87 virtual void visit(const DynamicRelocationSection &Sec) = 0;
88 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000089 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000090 virtual void visit(const SectionIndexSection &Sec) = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000091 virtual void visit(const CompressedSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000092};
93
94class SectionWriter : public SectionVisitor {
95protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000096 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000097
98public:
99 virtual ~SectionWriter(){};
100
101 void visit(const Section &Sec) override;
102 void visit(const OwnedDataSection &Sec) override;
103 void visit(const StringTableSection &Sec) override;
104 void visit(const DynamicRelocationSection &Sec) override;
105 virtual void visit(const SymbolTableSection &Sec) override = 0;
106 virtual void visit(const RelocationSection &Sec) override = 0;
107 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000108 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000109 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000110 virtual void visit(const CompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000111
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000112 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000113};
114
115template <class ELFT> class ELFSectionWriter : public SectionWriter {
116private:
117 using Elf_Word = typename ELFT::Word;
118 using Elf_Rel = typename ELFT::Rel;
119 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000120 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000121
122public:
123 virtual ~ELFSectionWriter() {}
124 void visit(const SymbolTableSection &Sec) override;
125 void visit(const RelocationSection &Sec) override;
126 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000127 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000128 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000129 void visit(const CompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000130
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000131 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000132};
133
134#define MAKE_SEC_WRITER_FRIEND \
135 friend class SectionWriter; \
136 template <class ELFT> friend class ELFSectionWriter;
137
138class BinarySectionWriter : public SectionWriter {
139public:
140 virtual ~BinarySectionWriter() {}
141
142 void visit(const SymbolTableSection &Sec) override;
143 void visit(const RelocationSection &Sec) override;
144 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000145 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000146 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000147 void visit(const CompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000148
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000149 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
150};
151
152// The class Buffer abstracts out the common interface of FileOutputBuffer and
153// WritableMemoryBuffer so that the hierarchy of Writers depends on this
154// abstract interface and doesn't depend on a particular implementation.
155// TODO: refactor the buffer classes in LLVM to enable us to use them here
156// directly.
157class Buffer {
158 StringRef Name;
159
160public:
161 virtual ~Buffer();
162 virtual void allocate(size_t Size) = 0;
163 virtual uint8_t *getBufferStart() = 0;
164 virtual Error commit() = 0;
165
166 explicit Buffer(StringRef Name) : Name(Name) {}
167 StringRef getName() const { return Name; }
168};
169
170class FileBuffer : public Buffer {
171 std::unique_ptr<FileOutputBuffer> Buf;
172
173public:
174 void allocate(size_t Size) override;
175 uint8_t *getBufferStart() override;
176 Error commit() override;
177
178 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
179};
180
181class MemBuffer : public Buffer {
182 std::unique_ptr<WritableMemoryBuffer> Buf;
183
184public:
185 void allocate(size_t Size) override;
186 uint8_t *getBufferStart() override;
187 Error commit() override;
188
189 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
190
191 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000192};
193
194class Writer {
195protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000196 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000197 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000198
199public:
200 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000201 virtual void finalize() = 0;
202 virtual void write() = 0;
203
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000204 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000205};
206
207template <class ELFT> class ELFWriter : public Writer {
208private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000209 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000210 using Elf_Shdr = typename ELFT::Shdr;
211 using Elf_Phdr = typename ELFT::Phdr;
212 using Elf_Ehdr = typename ELFT::Ehdr;
213
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000214 void initEhdrSegment();
215
Jake Ehrlich76e91102018-01-25 22:46:17 +0000216 void writeEhdr();
217 void writePhdr(const Segment &Seg);
218 void writeShdr(const SectionBase &Sec);
219
220 void writePhdrs();
221 void writeShdrs();
222 void writeSectionData();
223
224 void assignOffsets();
225
226 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
227
228 size_t totalSize() const;
229
230public:
231 virtual ~ELFWriter() {}
232 bool WriteSectionHeaders = true;
233
234 void finalize() override;
235 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000236 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
237 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000238};
239
240class BinaryWriter : public Writer {
241private:
242 std::unique_ptr<BinarySectionWriter> SecWriter;
243
244 uint64_t TotalSize;
245
246public:
247 ~BinaryWriter() {}
248 void finalize() override;
249 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000250 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000251};
252
Petr Hosek05a04cb2017-08-01 00:33:58 +0000253class SectionBase {
254public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000255 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000256 Segment *ParentSegment = nullptr;
257 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000258 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000259 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000260 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000261
262 uint64_t Addr = 0;
263 uint64_t Align = 1;
264 uint32_t EntrySize = 0;
265 uint64_t Flags = 0;
266 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000267 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000268 uint64_t NameIndex = 0;
269 uint64_t Offset = 0;
270 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000271 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000272 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000273
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000274 SectionBase() = default;
275 SectionBase(const SectionBase &) = default;
276
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000277 virtual ~SectionBase() = default;
278
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000279 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000280 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000281 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000282 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000283 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000284 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000285};
286
287class Segment {
288private:
289 struct SectionCompare {
290 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
291 // Some sections might have the same address if one of them is empty. To
292 // fix this we can use the lexicographic ordering on ->Addr and the
293 // address of the actully stored section.
294 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
295 return Lhs < Rhs;
296 return Lhs->OriginalOffset < Rhs->OriginalOffset;
297 }
298 };
299
300 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000301 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000302
303public:
304 uint64_t Align;
305 uint64_t FileSize;
306 uint32_t Flags;
307 uint32_t Index;
308 uint64_t MemSize;
309 uint64_t Offset;
310 uint64_t PAddr;
311 uint64_t Type;
312 uint64_t VAddr;
313
Petr Hosek3f383832017-08-26 01:32:20 +0000314 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000315 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000316
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000317 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000318 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000319
Petr Hosek05a04cb2017-08-01 00:33:58 +0000320 const SectionBase *firstSection() const {
321 if (!Sections.empty())
322 return *Sections.begin();
323 return nullptr;
324 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000325
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000326 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
327 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000328};
329
330class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000331 MAKE_SEC_WRITER_FRIEND
332
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000333 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000334 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000335
336public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000337 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000338
Jake Ehrlich76e91102018-01-25 22:46:17 +0000339 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000340 void removeSectionReferences(const SectionBase *Sec) override;
341 void initialize(SectionTableRef SecTable) override;
342 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000343};
344
Jake Ehrliche8437de2017-12-19 00:47:30 +0000345class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000346 MAKE_SEC_WRITER_FRIEND
347
Jake Ehrliche8437de2017-12-19 00:47:30 +0000348 std::vector<uint8_t> Data;
349
350public:
351 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
352 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000353 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000354 Type = ELF::SHT_PROGBITS;
355 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000356 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000357 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000358
359 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000360};
361
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000362class CompressedSection : public SectionBase {
363 MAKE_SEC_WRITER_FRIEND
364
365 DebugCompressionType CompressionType;
366 uint64_t DecompressedSize;
367 uint64_t DecompressedAlign;
368 SmallVector<char, 128> CompressedData;
369
370public:
371 CompressedSection(const SectionBase &Sec,
372 DebugCompressionType CompressionType);
373 void accept(SectionVisitor &Visitor) const override;
374};
375
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000376// There are two types of string tables that can exist, dynamic and not dynamic.
377// In the dynamic case the string table is allocated. Changing a dynamic string
378// table would mean altering virtual addresses and thus the memory image. So
379// dynamic string tables should not have an interface to modify them or
380// reconstruct them. This type lets us reconstruct a string table. To avoid
381// this class being used for dynamic string tables (which has happened) the
382// classof method checks that the particular instance is not allocated. This
383// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000384class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000385 MAKE_SEC_WRITER_FRIEND
386
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000387 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000388
389public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000390 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
391 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000392 }
393
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000394 void addString(StringRef Name);
395 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000396 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000397 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000398
Petr Hosek05a04cb2017-08-01 00:33:58 +0000399 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000400 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000401 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000402 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000403 }
404};
405
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000406// Symbols have a st_shndx field that normally stores an index but occasionally
407// stores a different special value. This enum keeps track of what the st_shndx
408// field means. Most of the values are just copies of the special SHN_* values.
409// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
410enum SymbolShndxType {
411 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000412 SYMBOL_ABS = ELF::SHN_ABS,
413 SYMBOL_COMMON = ELF::SHN_COMMON,
414 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
415 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
416 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
417 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000418 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000419};
420
Petr Hosek79cee9e2017-08-29 02:12:03 +0000421struct Symbol {
422 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000423 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000424 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000425 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000426 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000427 uint32_t NameIndex;
428 uint64_t Size;
429 uint8_t Type;
430 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000431 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000432 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000433
434 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000435};
436
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000437class SectionIndexSection : public SectionBase {
438 MAKE_SEC_WRITER_FRIEND
439
440private:
441 std::vector<uint32_t> Indexes;
442 SymbolTableSection *Symbols = nullptr;
443
444public:
445 virtual ~SectionIndexSection() {}
446 void addIndex(uint32_t Index) {
447 Indexes.push_back(Index);
448 Size += 4;
449 }
450 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
451 void initialize(SectionTableRef SecTable) override;
452 void finalize() override;
453 void accept(SectionVisitor &Visitor) const override;
454
455 SectionIndexSection() {
456 Name = ".symtab_shndx";
457 Align = 4;
458 EntrySize = 4;
459 Type = ELF::SHT_SYMTAB_SHNDX;
460 }
461};
462
Petr Hosek79cee9e2017-08-29 02:12:03 +0000463class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000464 MAKE_SEC_WRITER_FRIEND
465
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000466 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000467 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000468
Petr Hosek79cee9e2017-08-29 02:12:03 +0000469protected:
470 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000471 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000472 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000473
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000474 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000475
Petr Hosek79cee9e2017-08-29 02:12:03 +0000476public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000477 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
478
479 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
480 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
481 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000482 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000483 // An 'empty' symbol table still contains a null symbol.
484 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000485 void setShndxTable(SectionIndexSection *ShndxTable) {
486 SectionIndexTable = ShndxTable;
487 }
488 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000489 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000490 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000491 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000492 void updateSymbols(function_ref<void(Symbol &)> Callable);
493
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000494 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000495 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000496 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000497 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000498 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000499
Petr Hosek79cee9e2017-08-29 02:12:03 +0000500 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000501 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000502 }
503};
504
Petr Hosekd7df9b22017-09-06 23:41:02 +0000505struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000506 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000507 uint64_t Offset;
508 uint64_t Addend;
509 uint32_t Type;
510};
511
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000512// All relocation sections denote relocations to apply to another section.
513// However, some relocation sections use a dynamic symbol table and others use
514// a regular symbol table. Because the types of the two symbol tables differ in
515// our system (because they should behave differently) we can't uniformly
516// represent all relocations with the same base class if we expose an interface
517// that mentions the symbol table type. So we split the two base types into two
518// different classes, one which handles the section the relocation is applied to
519// and another which handles the symbol table type. The symbol table type is
520// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
521class RelocationSectionBase : public SectionBase {
522protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000523 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000524
525public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000526 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000527 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000528
529 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000530 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000531 }
532};
533
534// Takes the symbol table type to use as a parameter so that we can deduplicate
535// that code between the two symbol table types.
536template <class SymTabType>
537class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000538 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000539 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000540
541protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000542 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000543
544public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000545 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000546 void initialize(SectionTableRef SecTable) override;
547 void finalize() override;
548};
549
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000550class RelocationSection
551 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000552 MAKE_SEC_WRITER_FRIEND
553
Petr Hosekd7df9b22017-09-06 23:41:02 +0000554 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000555
Petr Hosekd7df9b22017-09-06 23:41:02 +0000556public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000557 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000558 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000559 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000560 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000561
Petr Hosekd7df9b22017-09-06 23:41:02 +0000562 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000563 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000564 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000565 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000566 }
567};
568
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000569// TODO: The way stripping and groups interact is complicated
570// and still needs to be worked on.
571
572class GroupSection : public SectionBase {
573 MAKE_SEC_WRITER_FRIEND
574 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000575 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000576 ELF::Elf32_Word FlagWord;
577 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000578
579public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000580 // TODO: Contents is present in several classes of the hierarchy.
581 // This needs to be refactored to avoid duplication.
582 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000583
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000584 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
585
586 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000587 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000588 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
589 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
590
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000591 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000592 void accept(SectionVisitor &) const override;
593 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000594 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000595 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000596
597 static bool classof(const SectionBase *S) {
598 return S->Type == ELF::SHT_GROUP;
599 }
600};
601
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000602class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000603public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000604 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000605
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000606 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000607 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000608 }
609};
610
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000611class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000612public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000613 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000614
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000615 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000616 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000617 }
618};
619
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000620class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000621 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000622 MAKE_SEC_WRITER_FRIEND
623
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000624private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000625 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000626
627public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000628 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000629
Jake Ehrlich76e91102018-01-25 22:46:17 +0000630 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000631
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000632 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000633 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000634 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000635 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000636 }
637};
638
Jake Ehrlich76e91102018-01-25 22:46:17 +0000639class GnuDebugLinkSection : public SectionBase {
640 MAKE_SEC_WRITER_FRIEND
641
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000642private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000643 StringRef FileName;
644 uint32_t CRC32;
645
646 void init(StringRef File, StringRef Data);
647
648public:
649 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000650 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000651 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000652};
653
Jake Ehrlich76e91102018-01-25 22:46:17 +0000654class Reader {
655public:
656 virtual ~Reader();
657 virtual std::unique_ptr<Object> create() const = 0;
658};
659
Jake Ehrlich76e91102018-01-25 22:46:17 +0000660using object::Binary;
661using object::ELFFile;
662using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000663using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000664
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000665template <class ELFT> class BinaryELFBuilder {
666 using Elf_Sym = typename ELFT::Sym;
667
668 uint16_t EMachine;
669 MemoryBuffer *MemBuf;
670 std::unique_ptr<Object> Obj;
671
672 void initFileHeader();
673 void initHeaderSegment();
674 StringTableSection *addStrTab();
675 SymbolTableSection *addSymTab(StringTableSection *StrTab);
676 void addData(SymbolTableSection *SymTab);
677 void initSections();
678
679public:
680 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
681 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
682
683 std::unique_ptr<Object> build();
684};
685
Jake Ehrlich76e91102018-01-25 22:46:17 +0000686template <class ELFT> class ELFBuilder {
687private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000688 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000689 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000690 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000691
692 const ELFFile<ELFT> &ElfFile;
693 Object &Obj;
694
Jake Ehrlich6452b112018-02-14 23:31:33 +0000695 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000696 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000697 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000698 void initSymbolTable(SymbolTableSection *SymTab);
699 void readSectionHeaders();
700 SectionBase &makeSection(const Elf_Shdr &Shdr);
701
702public:
703 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
704 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
705
706 void build();
707};
708
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000709class BinaryReader : public Reader {
710 const MachineInfo &MInfo;
711 MemoryBuffer *MemBuf;
712
713public:
714 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
715 : MInfo(MI), MemBuf(MB) {}
716 std::unique_ptr<Object> create() const override;
717};
718
Jake Ehrlich76e91102018-01-25 22:46:17 +0000719class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000720 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000721
722public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000723 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000724 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000725};
726
727class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000728private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000729 using SecPtr = std::unique_ptr<SectionBase>;
730 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000731
Petr Hosekc4df10e2017-08-04 21:09:26 +0000732 std::vector<SecPtr> Sections;
733 std::vector<SegPtr> Segments;
734
Petr Hosek05a04cb2017-08-01 00:33:58 +0000735public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000736 template <class T>
737 using Range = iterator_range<
738 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
739
740 template <class T>
741 using ConstRange = iterator_range<pointee_iterator<
742 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
743
Jake Ehrlich6452b112018-02-14 23:31:33 +0000744 // It is often the case that the ELF header and the program header table are
745 // not present in any segment. This could be a problem during file layout,
746 // because other segments may get assigned an offset where either of the
747 // two should reside, which will effectively corrupt the resulting binary.
748 // Other than that we use these segments to track program header offsets
749 // when they may not follow the ELF header.
750 Segment ElfHdrSegment;
751 Segment ProgramHdrSegment;
752
Petr Hosek05a04cb2017-08-01 00:33:58 +0000753 uint64_t Entry;
754 uint64_t SHOffset;
755 uint32_t Type;
756 uint32_t Machine;
757 uint32_t Version;
758 uint32_t Flags;
759
Jake Ehrlich76e91102018-01-25 22:46:17 +0000760 StringTableSection *SectionNames = nullptr;
761 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000762 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000763
Aaron Ballman09f46a72018-01-25 21:03:38 +0000764 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000765 SectionTableRef sections() { return SectionTableRef(Sections); }
766 ConstRange<SectionBase> sections() const {
767 return make_pointee_range(Sections);
768 }
769 Range<Segment> segments() { return make_pointee_range(Segments); }
770 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000771
Jake Ehrlich76e91102018-01-25 22:46:17 +0000772 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000773 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000774 template <class T, class... Ts> T &addSection(Ts &&... Args) {
775 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
776 auto Ptr = Sec.get();
777 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000778 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000779 return *Ptr;
780 }
781 Segment &addSegment(ArrayRef<uint8_t> Data) {
782 Segments.emplace_back(llvm::make_unique<Segment>(Data));
783 return *Segments.back();
784 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000785};
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000786} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000787} // end namespace llvm
788
789#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H