blob: 91ff1cddac17c6f3bccab3e0801f576ff7bbd9ee [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
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000013#include "Buffer.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000014#include "CopyConfig.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000015#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000019#include "llvm/MC/StringTableBuilder.h"
20#include "llvm/Object/ELFObjectFile.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000021#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000022#include "llvm/Support/JamCRC.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000023#include <cstddef>
24#include <cstdint>
25#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000026#include <memory>
27#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000028#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000029
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000030namespace llvm {
Puyan Lotfi99124cc2018-09-07 08:10:22 +000031enum class DebugCompressionType;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000032namespace objcopy {
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +000033namespace elf {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000034
Jake Ehrlichf5a43772017-09-25 20:37:28 +000035class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000036class Section;
37class OwnedDataSection;
38class StringTableSection;
39class SymbolTableSection;
40class RelocationSection;
41class DynamicRelocationSection;
42class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000043class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000044class SectionIndexSection;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000045class CompressedSection;
Puyan Lotfiaf048642018-10-01 10:29:41 +000046class DecompressedSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000047class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000048class Object;
Paul Semel4246a462018-05-09 21:36:54 +000049struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000050
51class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000052 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000053
54public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000055 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
56
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000057 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000058 : Sections(Secs) {}
59 SectionTableRef(const SectionTableRef &) = default;
60
Jake Ehrlich76e91102018-01-25 22:46:17 +000061 iterator begin() { return iterator(Sections.data()); }
62 iterator end() { return iterator(Sections.data() + Sections.size()); }
63
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:
74 virtual ~SectionVisitor();
75
76 virtual void visit(const Section &Sec) = 0;
77 virtual void visit(const OwnedDataSection &Sec) = 0;
78 virtual void visit(const StringTableSection &Sec) = 0;
79 virtual void visit(const SymbolTableSection &Sec) = 0;
80 virtual void visit(const RelocationSection &Sec) = 0;
81 virtual void visit(const DynamicRelocationSection &Sec) = 0;
82 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander 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
89class SectionWriter : public SectionVisitor {
90protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000091 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000092
93public:
94 virtual ~SectionWriter(){};
95
96 void visit(const Section &Sec) override;
97 void visit(const OwnedDataSection &Sec) override;
98 void visit(const StringTableSection &Sec) override;
99 void visit(const DynamicRelocationSection &Sec) override;
100 virtual void visit(const SymbolTableSection &Sec) override = 0;
101 virtual void visit(const RelocationSection &Sec) override = 0;
102 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000103 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000104 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000105 virtual void visit(const CompressedSection &Sec) override = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000106 virtual void visit(const DecompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000107
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000108 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109};
110
111template <class ELFT> class ELFSectionWriter : public SectionWriter {
112private:
113 using Elf_Word = typename ELFT::Word;
114 using Elf_Rel = typename ELFT::Rel;
115 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000116 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000117
118public:
119 virtual ~ELFSectionWriter() {}
120 void visit(const SymbolTableSection &Sec) override;
121 void visit(const RelocationSection &Sec) override;
122 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000123 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000124 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000125 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000126 void visit(const DecompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000127
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000128 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000129};
130
131#define MAKE_SEC_WRITER_FRIEND \
132 friend class SectionWriter; \
133 template <class ELFT> friend class ELFSectionWriter;
134
135class BinarySectionWriter : public SectionWriter {
136public:
137 virtual ~BinarySectionWriter() {}
138
139 void visit(const SymbolTableSection &Sec) override;
140 void visit(const RelocationSection &Sec) override;
141 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000142 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000143 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000144 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000145 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000146
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000147 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
148};
149
Jake Ehrlich76e91102018-01-25 22:46:17 +0000150class Writer {
151protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000152 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000153 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000154
155public:
156 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000157 virtual void finalize() = 0;
158 virtual void write() = 0;
159
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000160 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000161};
162
163template <class ELFT> class ELFWriter : public Writer {
164private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000165 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000166 using Elf_Shdr = typename ELFT::Shdr;
167 using Elf_Phdr = typename ELFT::Phdr;
168 using Elf_Ehdr = typename ELFT::Ehdr;
169
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000170 void initEhdrSegment();
171
Jake Ehrlich76e91102018-01-25 22:46:17 +0000172 void writeEhdr();
173 void writePhdr(const Segment &Seg);
174 void writeShdr(const SectionBase &Sec);
175
176 void writePhdrs();
177 void writeShdrs();
178 void writeSectionData();
179
180 void assignOffsets();
181
182 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
183
184 size_t totalSize() const;
185
186public:
187 virtual ~ELFWriter() {}
188 bool WriteSectionHeaders = true;
189
190 void finalize() override;
191 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000192 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
193 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000194};
195
196class BinaryWriter : public Writer {
197private:
198 std::unique_ptr<BinarySectionWriter> SecWriter;
199
200 uint64_t TotalSize;
201
202public:
203 ~BinaryWriter() {}
204 void finalize() override;
205 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000206 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000207};
208
Petr Hosek05a04cb2017-08-01 00:33:58 +0000209class SectionBase {
210public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000211 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000212 Segment *ParentSegment = nullptr;
213 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000214 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000215 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000216 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000217
218 uint64_t Addr = 0;
219 uint64_t Align = 1;
220 uint32_t EntrySize = 0;
221 uint64_t Flags = 0;
222 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000223 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000224 uint64_t NameIndex = 0;
225 uint64_t Offset = 0;
226 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000227 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000228 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000229
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000230 SectionBase() = default;
231 SectionBase(const SectionBase &) = default;
232
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000233 virtual ~SectionBase() = default;
234
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000235 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000236 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000237 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000238 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000239 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000240 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000241};
242
243class Segment {
244private:
245 struct SectionCompare {
246 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
247 // Some sections might have the same address if one of them is empty. To
248 // fix this we can use the lexicographic ordering on ->Addr and the
249 // address of the actully stored section.
250 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
251 return Lhs < Rhs;
252 return Lhs->OriginalOffset < Rhs->OriginalOffset;
253 }
254 };
255
256 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000257 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000258
259public:
260 uint64_t Align;
261 uint64_t FileSize;
262 uint32_t Flags;
263 uint32_t Index;
264 uint64_t MemSize;
265 uint64_t Offset;
266 uint64_t PAddr;
267 uint64_t Type;
268 uint64_t VAddr;
269
Petr Hosek3f383832017-08-26 01:32:20 +0000270 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000271 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000272
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000273 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000274 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000275
Petr Hosek05a04cb2017-08-01 00:33:58 +0000276 const SectionBase *firstSection() const {
277 if (!Sections.empty())
278 return *Sections.begin();
279 return nullptr;
280 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000281
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000282 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
283 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000284};
285
286class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000287 MAKE_SEC_WRITER_FRIEND
288
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000289 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000290 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000291
292public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000293 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000294
Jake Ehrlich76e91102018-01-25 22:46:17 +0000295 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000296 void removeSectionReferences(const SectionBase *Sec) override;
297 void initialize(SectionTableRef SecTable) override;
298 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000299};
300
Jake Ehrliche8437de2017-12-19 00:47:30 +0000301class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000302 MAKE_SEC_WRITER_FRIEND
303
Jake Ehrliche8437de2017-12-19 00:47:30 +0000304 std::vector<uint8_t> Data;
305
306public:
307 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
308 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000309 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000310 Type = ELF::SHT_PROGBITS;
311 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000312 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000313 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000314
315 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000316};
317
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000318class CompressedSection : public SectionBase {
319 MAKE_SEC_WRITER_FRIEND
320
321 DebugCompressionType CompressionType;
322 uint64_t DecompressedSize;
323 uint64_t DecompressedAlign;
324 SmallVector<char, 128> CompressedData;
325
326public:
327 CompressedSection(const SectionBase &Sec,
328 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000329 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
330 uint64_t DecompressedAlign);
331
332 uint64_t getDecompressedSize() const { return DecompressedSize; }
333 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
334
335 void accept(SectionVisitor &Visitor) const override;
336
337 static bool classof(const SectionBase *S) {
338 return (S->Flags & ELF::SHF_COMPRESSED) ||
339 (StringRef(S->Name).startswith(".zdebug"));
340 }
341};
342
343class DecompressedSection : public SectionBase {
344 MAKE_SEC_WRITER_FRIEND
345
346public:
347 explicit DecompressedSection(const CompressedSection &Sec)
348 : SectionBase(Sec) {
349 Size = Sec.getDecompressedSize();
350 Align = Sec.getDecompressedAlign();
351 Flags = (Flags & ~ELF::SHF_COMPRESSED);
352 if (StringRef(Name).startswith(".zdebug"))
353 Name = "." + Name.substr(2);
354 }
355
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000356 void accept(SectionVisitor &Visitor) const override;
357};
358
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000359// There are two types of string tables that can exist, dynamic and not dynamic.
360// In the dynamic case the string table is allocated. Changing a dynamic string
361// table would mean altering virtual addresses and thus the memory image. So
362// dynamic string tables should not have an interface to modify them or
363// reconstruct them. This type lets us reconstruct a string table. To avoid
364// this class being used for dynamic string tables (which has happened) the
365// classof method checks that the particular instance is not allocated. This
366// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000367class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000368 MAKE_SEC_WRITER_FRIEND
369
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000370 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000371
372public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000373 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
374 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000375 }
376
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000377 void addString(StringRef Name);
378 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000379 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000380 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000381
Petr Hosek05a04cb2017-08-01 00:33:58 +0000382 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000383 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000384 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000385 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000386 }
387};
388
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000389// Symbols have a st_shndx field that normally stores an index but occasionally
390// stores a different special value. This enum keeps track of what the st_shndx
391// field means. Most of the values are just copies of the special SHN_* values.
392// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
393enum SymbolShndxType {
394 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000395 SYMBOL_ABS = ELF::SHN_ABS,
396 SYMBOL_COMMON = ELF::SHN_COMMON,
397 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
398 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
399 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
400 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000401 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000402};
403
Petr Hosek79cee9e2017-08-29 02:12:03 +0000404struct Symbol {
405 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000406 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000407 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000408 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000409 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000410 uint32_t NameIndex;
411 uint64_t Size;
412 uint8_t Type;
413 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000414 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000415 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000416
417 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000418 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000419};
420
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000421class SectionIndexSection : public SectionBase {
422 MAKE_SEC_WRITER_FRIEND
423
424private:
425 std::vector<uint32_t> Indexes;
426 SymbolTableSection *Symbols = nullptr;
427
428public:
429 virtual ~SectionIndexSection() {}
430 void addIndex(uint32_t Index) {
431 Indexes.push_back(Index);
432 Size += 4;
433 }
434 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
435 void initialize(SectionTableRef SecTable) override;
436 void finalize() override;
437 void accept(SectionVisitor &Visitor) const override;
438
439 SectionIndexSection() {
440 Name = ".symtab_shndx";
441 Align = 4;
442 EntrySize = 4;
443 Type = ELF::SHT_SYMTAB_SHNDX;
444 }
445};
446
Petr Hosek79cee9e2017-08-29 02:12:03 +0000447class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000448 MAKE_SEC_WRITER_FRIEND
449
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000450 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000451 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000452
Petr Hosek79cee9e2017-08-29 02:12:03 +0000453protected:
454 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000455 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000456 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000457
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000458 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000459
Petr Hosek79cee9e2017-08-29 02:12:03 +0000460public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000461 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
462
463 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
464 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
465 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000466 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000467 // An 'empty' symbol table still contains a null symbol.
468 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000469 void setShndxTable(SectionIndexSection *ShndxTable) {
470 SectionIndexTable = ShndxTable;
471 }
472 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000473 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000474 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000475 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000476 void updateSymbols(function_ref<void(Symbol &)> Callable);
477
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000478 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000479 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000480 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000481 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000482 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000483
Petr Hosek79cee9e2017-08-29 02:12:03 +0000484 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000485 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000486 }
487};
488
Petr Hosekd7df9b22017-09-06 23:41:02 +0000489struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000490 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000491 uint64_t Offset;
492 uint64_t Addend;
493 uint32_t Type;
494};
495
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000496// All relocation sections denote relocations to apply to another section.
497// However, some relocation sections use a dynamic symbol table and others use
498// a regular symbol table. Because the types of the two symbol tables differ in
499// our system (because they should behave differently) we can't uniformly
500// represent all relocations with the same base class if we expose an interface
501// that mentions the symbol table type. So we split the two base types into two
502// different classes, one which handles the section the relocation is applied to
503// and another which handles the symbol table type. The symbol table type is
504// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
505class RelocationSectionBase : public SectionBase {
506protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000507 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000508
509public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000510 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000511 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000512
513 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000514 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000515 }
516};
517
518// Takes the symbol table type to use as a parameter so that we can deduplicate
519// that code between the two symbol table types.
520template <class SymTabType>
521class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000522 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000523 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000524
525protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000526 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000527
528public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000529 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000530 void initialize(SectionTableRef SecTable) override;
531 void finalize() override;
532};
533
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000534class RelocationSection
535 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000536 MAKE_SEC_WRITER_FRIEND
537
Petr Hosekd7df9b22017-09-06 23:41:02 +0000538 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000539
Petr Hosekd7df9b22017-09-06 23:41:02 +0000540public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000541 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000542 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000543 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000544 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000545
Petr Hosekd7df9b22017-09-06 23:41:02 +0000546 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000547 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000548 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000549 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000550 }
551};
552
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000553// TODO: The way stripping and groups interact is complicated
554// and still needs to be worked on.
555
556class GroupSection : public SectionBase {
557 MAKE_SEC_WRITER_FRIEND
558 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000559 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000560 ELF::Elf32_Word FlagWord;
561 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000562
563public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000564 // TODO: Contents is present in several classes of the hierarchy.
565 // This needs to be refactored to avoid duplication.
566 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000567
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000568 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
569
570 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000571 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000572 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
573 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
574
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000575 void accept(SectionVisitor &) const override;
576 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000577 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000578 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000579
580 static bool classof(const SectionBase *S) {
581 return S->Type == ELF::SHT_GROUP;
582 }
583};
584
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000585class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000586public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000587 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000588
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000589 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000590 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000591 }
592};
593
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000594class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000595public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000596 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000597
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000598 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000599 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000600 }
601};
602
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000603class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000604 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000605 MAKE_SEC_WRITER_FRIEND
606
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000607private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000608 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000609
610public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000611 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000612
Jake Ehrlich76e91102018-01-25 22:46:17 +0000613 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000614
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000615 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000616 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000617 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000618 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000619 }
620};
621
Jake Ehrlich76e91102018-01-25 22:46:17 +0000622class GnuDebugLinkSection : public SectionBase {
623 MAKE_SEC_WRITER_FRIEND
624
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000625private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000626 StringRef FileName;
627 uint32_t CRC32;
628
629 void init(StringRef File, StringRef Data);
630
631public:
632 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000633 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000634 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000635};
636
Jake Ehrlich76e91102018-01-25 22:46:17 +0000637class Reader {
638public:
639 virtual ~Reader();
640 virtual std::unique_ptr<Object> create() const = 0;
641};
642
Jake Ehrlich76e91102018-01-25 22:46:17 +0000643using object::Binary;
644using object::ELFFile;
645using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000646using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000647
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000648template <class ELFT> class BinaryELFBuilder {
649 using Elf_Sym = typename ELFT::Sym;
650
651 uint16_t EMachine;
652 MemoryBuffer *MemBuf;
653 std::unique_ptr<Object> Obj;
654
655 void initFileHeader();
656 void initHeaderSegment();
657 StringTableSection *addStrTab();
658 SymbolTableSection *addSymTab(StringTableSection *StrTab);
659 void addData(SymbolTableSection *SymTab);
660 void initSections();
661
662public:
663 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
664 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
665
666 std::unique_ptr<Object> build();
667};
668
Jake Ehrlich76e91102018-01-25 22:46:17 +0000669template <class ELFT> class ELFBuilder {
670private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000671 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000672 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000673 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000674
675 const ELFFile<ELFT> &ElfFile;
676 Object &Obj;
677
Jake Ehrlich6452b112018-02-14 23:31:33 +0000678 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000679 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000680 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000681 void initSymbolTable(SymbolTableSection *SymTab);
682 void readSectionHeaders();
683 SectionBase &makeSection(const Elf_Shdr &Shdr);
684
685public:
686 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
687 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
688
689 void build();
690};
691
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000692class BinaryReader : public Reader {
693 const MachineInfo &MInfo;
694 MemoryBuffer *MemBuf;
695
696public:
697 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
698 : MInfo(MI), MemBuf(MB) {}
699 std::unique_ptr<Object> create() const override;
700};
701
Jake Ehrlich76e91102018-01-25 22:46:17 +0000702class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000703 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000704
705public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000706 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000707 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000708};
709
710class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000711private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000712 using SecPtr = std::unique_ptr<SectionBase>;
713 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000714
Petr Hosekc4df10e2017-08-04 21:09:26 +0000715 std::vector<SecPtr> Sections;
716 std::vector<SegPtr> Segments;
717
Petr Hosek05a04cb2017-08-01 00:33:58 +0000718public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000719 template <class T>
720 using Range = iterator_range<
721 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
722
723 template <class T>
724 using ConstRange = iterator_range<pointee_iterator<
725 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
726
Jake Ehrlich6452b112018-02-14 23:31:33 +0000727 // It is often the case that the ELF header and the program header table are
728 // not present in any segment. This could be a problem during file layout,
729 // because other segments may get assigned an offset where either of the
730 // two should reside, which will effectively corrupt the resulting binary.
731 // Other than that we use these segments to track program header offsets
732 // when they may not follow the ELF header.
733 Segment ElfHdrSegment;
734 Segment ProgramHdrSegment;
735
Petr Hosek05a04cb2017-08-01 00:33:58 +0000736 uint64_t Entry;
737 uint64_t SHOffset;
738 uint32_t Type;
739 uint32_t Machine;
740 uint32_t Version;
741 uint32_t Flags;
742
Jake Ehrlich76e91102018-01-25 22:46:17 +0000743 StringTableSection *SectionNames = nullptr;
744 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000745 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000746
Aaron Ballman09f46a72018-01-25 21:03:38 +0000747 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000748 SectionTableRef sections() { return SectionTableRef(Sections); }
749 ConstRange<SectionBase> sections() const {
750 return make_pointee_range(Sections);
751 }
752 Range<Segment> segments() { return make_pointee_range(Segments); }
753 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000754
Jake Ehrlich76e91102018-01-25 22:46:17 +0000755 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000756 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000757 template <class T, class... Ts> T &addSection(Ts &&... Args) {
758 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
759 auto Ptr = Sec.get();
760 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000761 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000762 return *Ptr;
763 }
764 Segment &addSegment(ArrayRef<uint8_t> Data) {
765 Segments.emplace_back(llvm::make_unique<Segment>(Data));
766 return *Segments.back();
767 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000768};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000769
770} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000771} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000772} // end namespace llvm
773
774#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H