blob: 4aa3125f26c4d3ee7f054b4ad65cd924f54fef25 [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;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000418};
419
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000420class SectionIndexSection : public SectionBase {
421 MAKE_SEC_WRITER_FRIEND
422
423private:
424 std::vector<uint32_t> Indexes;
425 SymbolTableSection *Symbols = nullptr;
426
427public:
428 virtual ~SectionIndexSection() {}
429 void addIndex(uint32_t Index) {
430 Indexes.push_back(Index);
431 Size += 4;
432 }
433 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
434 void initialize(SectionTableRef SecTable) override;
435 void finalize() override;
436 void accept(SectionVisitor &Visitor) const override;
437
438 SectionIndexSection() {
439 Name = ".symtab_shndx";
440 Align = 4;
441 EntrySize = 4;
442 Type = ELF::SHT_SYMTAB_SHNDX;
443 }
444};
445
Petr Hosek79cee9e2017-08-29 02:12:03 +0000446class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000447 MAKE_SEC_WRITER_FRIEND
448
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000449 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000450 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000451
Petr Hosek79cee9e2017-08-29 02:12:03 +0000452protected:
453 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000454 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000455 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000456
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000457 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000458
Petr Hosek79cee9e2017-08-29 02:12:03 +0000459public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000460 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
461
462 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
463 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
464 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000465 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000466 // An 'empty' symbol table still contains a null symbol.
467 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000468 void setShndxTable(SectionIndexSection *ShndxTable) {
469 SectionIndexTable = ShndxTable;
470 }
471 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000472 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000473 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000474 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000475 void updateSymbols(function_ref<void(Symbol &)> Callable);
476
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000477 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000478 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000479 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000480 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000481 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000482
Petr Hosek79cee9e2017-08-29 02:12:03 +0000483 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000484 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000485 }
486};
487
Petr Hosekd7df9b22017-09-06 23:41:02 +0000488struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000489 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000490 uint64_t Offset;
491 uint64_t Addend;
492 uint32_t Type;
493};
494
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000495// All relocation sections denote relocations to apply to another section.
496// However, some relocation sections use a dynamic symbol table and others use
497// a regular symbol table. Because the types of the two symbol tables differ in
498// our system (because they should behave differently) we can't uniformly
499// represent all relocations with the same base class if we expose an interface
500// that mentions the symbol table type. So we split the two base types into two
501// different classes, one which handles the section the relocation is applied to
502// and another which handles the symbol table type. The symbol table type is
503// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
504class RelocationSectionBase : public SectionBase {
505protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000506 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000507
508public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000509 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000510 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000511
512 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000513 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000514 }
515};
516
517// Takes the symbol table type to use as a parameter so that we can deduplicate
518// that code between the two symbol table types.
519template <class SymTabType>
520class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000521 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000522 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000523
524protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000525 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000526
527public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000528 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000529 void initialize(SectionTableRef SecTable) override;
530 void finalize() override;
531};
532
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000533class RelocationSection
534 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000535 MAKE_SEC_WRITER_FRIEND
536
Petr Hosekd7df9b22017-09-06 23:41:02 +0000537 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000538
Petr Hosekd7df9b22017-09-06 23:41:02 +0000539public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000540 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000541 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000542 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000543 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000544
Petr Hosekd7df9b22017-09-06 23:41:02 +0000545 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000546 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000547 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000548 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000549 }
550};
551
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000552// TODO: The way stripping and groups interact is complicated
553// and still needs to be worked on.
554
555class GroupSection : public SectionBase {
556 MAKE_SEC_WRITER_FRIEND
557 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000558 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000559 ELF::Elf32_Word FlagWord;
560 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000561
562public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000563 // TODO: Contents is present in several classes of the hierarchy.
564 // This needs to be refactored to avoid duplication.
565 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000566
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000567 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
568
569 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000570 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000571 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
572 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
573
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000574 void accept(SectionVisitor &) const override;
575 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000576 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000577 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000578
579 static bool classof(const SectionBase *S) {
580 return S->Type == ELF::SHT_GROUP;
581 }
582};
583
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000584class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000585public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000586 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000587
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000588 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000589 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000590 }
591};
592
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000593class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000594public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000595 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000596
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000597 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000598 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000599 }
600};
601
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000602class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000603 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000604 MAKE_SEC_WRITER_FRIEND
605
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000606private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000607 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000608
609public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000610 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000611
Jake Ehrlich76e91102018-01-25 22:46:17 +0000612 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000613
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000614 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000615 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000616 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000617 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000618 }
619};
620
Jake Ehrlich76e91102018-01-25 22:46:17 +0000621class GnuDebugLinkSection : public SectionBase {
622 MAKE_SEC_WRITER_FRIEND
623
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000624private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000625 StringRef FileName;
626 uint32_t CRC32;
627
628 void init(StringRef File, StringRef Data);
629
630public:
631 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000632 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000633 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000634};
635
Jake Ehrlich76e91102018-01-25 22:46:17 +0000636class Reader {
637public:
638 virtual ~Reader();
639 virtual std::unique_ptr<Object> create() const = 0;
640};
641
Jake Ehrlich76e91102018-01-25 22:46:17 +0000642using object::Binary;
643using object::ELFFile;
644using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000645using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000646
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000647template <class ELFT> class BinaryELFBuilder {
648 using Elf_Sym = typename ELFT::Sym;
649
650 uint16_t EMachine;
651 MemoryBuffer *MemBuf;
652 std::unique_ptr<Object> Obj;
653
654 void initFileHeader();
655 void initHeaderSegment();
656 StringTableSection *addStrTab();
657 SymbolTableSection *addSymTab(StringTableSection *StrTab);
658 void addData(SymbolTableSection *SymTab);
659 void initSections();
660
661public:
662 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
663 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
664
665 std::unique_ptr<Object> build();
666};
667
Jake Ehrlich76e91102018-01-25 22:46:17 +0000668template <class ELFT> class ELFBuilder {
669private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000670 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000671 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000672 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000673
674 const ELFFile<ELFT> &ElfFile;
675 Object &Obj;
676
Jake Ehrlich6452b112018-02-14 23:31:33 +0000677 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000678 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000679 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000680 void initSymbolTable(SymbolTableSection *SymTab);
681 void readSectionHeaders();
682 SectionBase &makeSection(const Elf_Shdr &Shdr);
683
684public:
685 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
686 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
687
688 void build();
689};
690
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000691class BinaryReader : public Reader {
692 const MachineInfo &MInfo;
693 MemoryBuffer *MemBuf;
694
695public:
696 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
697 : MInfo(MI), MemBuf(MB) {}
698 std::unique_ptr<Object> create() const override;
699};
700
Jake Ehrlich76e91102018-01-25 22:46:17 +0000701class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000702 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000703
704public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000705 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000706 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000707};
708
709class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000710private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000711 using SecPtr = std::unique_ptr<SectionBase>;
712 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000713
Petr Hosekc4df10e2017-08-04 21:09:26 +0000714 std::vector<SecPtr> Sections;
715 std::vector<SegPtr> Segments;
716
Petr Hosek05a04cb2017-08-01 00:33:58 +0000717public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000718 template <class T>
719 using Range = iterator_range<
720 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
721
722 template <class T>
723 using ConstRange = iterator_range<pointee_iterator<
724 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
725
Jake Ehrlich6452b112018-02-14 23:31:33 +0000726 // It is often the case that the ELF header and the program header table are
727 // not present in any segment. This could be a problem during file layout,
728 // because other segments may get assigned an offset where either of the
729 // two should reside, which will effectively corrupt the resulting binary.
730 // Other than that we use these segments to track program header offsets
731 // when they may not follow the ELF header.
732 Segment ElfHdrSegment;
733 Segment ProgramHdrSegment;
734
Petr Hosek05a04cb2017-08-01 00:33:58 +0000735 uint64_t Entry;
736 uint64_t SHOffset;
737 uint32_t Type;
738 uint32_t Machine;
739 uint32_t Version;
740 uint32_t Flags;
741
Jake Ehrlich76e91102018-01-25 22:46:17 +0000742 StringTableSection *SectionNames = nullptr;
743 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000744 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000745
Aaron Ballman09f46a72018-01-25 21:03:38 +0000746 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000747 SectionTableRef sections() { return SectionTableRef(Sections); }
748 ConstRange<SectionBase> sections() const {
749 return make_pointee_range(Sections);
750 }
751 Range<Segment> segments() { return make_pointee_range(Segments); }
752 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000753
Jake Ehrlich76e91102018-01-25 22:46:17 +0000754 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000755 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000756 template <class T, class... Ts> T &addSection(Ts &&... Args) {
757 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
758 auto Ptr = Sec.get();
759 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000760 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000761 return *Ptr;
762 }
763 Segment &addSegment(ArrayRef<uint8_t> Data) {
764 Segments.emplace_back(llvm::make_unique<Segment>(Data));
765 return *Segments.back();
766 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000767};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000768
769} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000770} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000771} // end namespace llvm
772
773#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H