blob: 46c8f1ca4bfde3d5358ab9c54d6fdf23db9652b4 [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 Shaposhnikov8d0b74c2018-10-11 22:33:50 +000013#include "CopyConfig.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000014#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000018#include "llvm/MC/StringTableBuilder.h"
19#include "llvm/Object/ELFObjectFile.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000020#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000021#include "llvm/Support/JamCRC.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000022#include <cstddef>
23#include <cstdint>
24#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000025#include <memory>
26#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000027#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000028
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000029namespace llvm {
Puyan Lotfi99124cc2018-09-07 08:10:22 +000030enum class DebugCompressionType;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000031namespace objcopy {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000032
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000033class Buffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000034class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000035class Section;
36class OwnedDataSection;
37class StringTableSection;
38class SymbolTableSection;
39class RelocationSection;
40class DynamicRelocationSection;
41class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000042class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000043class SectionIndexSection;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000044class CompressedSection;
Puyan Lotfiaf048642018-10-01 10:29:41 +000045class DecompressedSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000046class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000047class Object;
Paul Semel4246a462018-05-09 21:36:54 +000048struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000049
50class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000051 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000052
53public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000054 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
55
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000056 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000057 : Sections(Secs) {}
58 SectionTableRef(const SectionTableRef &) = default;
59
Jake Ehrlich76e91102018-01-25 22:46:17 +000060 iterator begin() { return iterator(Sections.data()); }
61 iterator end() { return iterator(Sections.data() + Sections.size()); }
62
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000063 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000064
65 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000066 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000067};
Petr Hosek05a04cb2017-08-01 00:33:58 +000068
Jake Ehrlich76e91102018-01-25 22:46:17 +000069enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
70
71class SectionVisitor {
72public:
73 virtual ~SectionVisitor();
74
75 virtual void visit(const Section &Sec) = 0;
76 virtual void visit(const OwnedDataSection &Sec) = 0;
77 virtual void visit(const StringTableSection &Sec) = 0;
78 virtual void visit(const SymbolTableSection &Sec) = 0;
79 virtual void visit(const RelocationSection &Sec) = 0;
80 virtual void visit(const DynamicRelocationSection &Sec) = 0;
81 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000082 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000083 virtual void visit(const SectionIndexSection &Sec) = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000084 virtual void visit(const CompressedSection &Sec) = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +000085 virtual void visit(const DecompressedSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000086};
87
88class SectionWriter : public SectionVisitor {
89protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000090 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000091
92public:
93 virtual ~SectionWriter(){};
94
95 void visit(const Section &Sec) override;
96 void visit(const OwnedDataSection &Sec) override;
97 void visit(const StringTableSection &Sec) override;
98 void visit(const DynamicRelocationSection &Sec) override;
99 virtual void visit(const SymbolTableSection &Sec) override = 0;
100 virtual void visit(const RelocationSection &Sec) override = 0;
101 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000102 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000103 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000104 virtual void visit(const CompressedSection &Sec) override = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000105 virtual void visit(const DecompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000106
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000107 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000108};
109
110template <class ELFT> class ELFSectionWriter : public SectionWriter {
111private:
112 using Elf_Word = typename ELFT::Word;
113 using Elf_Rel = typename ELFT::Rel;
114 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000115 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000116
117public:
118 virtual ~ELFSectionWriter() {}
119 void visit(const SymbolTableSection &Sec) override;
120 void visit(const RelocationSection &Sec) override;
121 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000122 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000123 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000124 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000125 void visit(const DecompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000126
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000127 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000128};
129
130#define MAKE_SEC_WRITER_FRIEND \
131 friend class SectionWriter; \
132 template <class ELFT> friend class ELFSectionWriter;
133
134class BinarySectionWriter : public SectionWriter {
135public:
136 virtual ~BinarySectionWriter() {}
137
138 void visit(const SymbolTableSection &Sec) override;
139 void visit(const RelocationSection &Sec) override;
140 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000141 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000142 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000143 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000144 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000145
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000146 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
147};
148
149// The class Buffer abstracts out the common interface of FileOutputBuffer and
150// WritableMemoryBuffer so that the hierarchy of Writers depends on this
151// abstract interface and doesn't depend on a particular implementation.
152// TODO: refactor the buffer classes in LLVM to enable us to use them here
153// directly.
154class Buffer {
155 StringRef Name;
156
157public:
158 virtual ~Buffer();
159 virtual void allocate(size_t Size) = 0;
160 virtual uint8_t *getBufferStart() = 0;
161 virtual Error commit() = 0;
162
163 explicit Buffer(StringRef Name) : Name(Name) {}
164 StringRef getName() const { return Name; }
165};
166
167class FileBuffer : public Buffer {
168 std::unique_ptr<FileOutputBuffer> Buf;
169
170public:
171 void allocate(size_t Size) override;
172 uint8_t *getBufferStart() override;
173 Error commit() override;
174
175 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
176};
177
178class MemBuffer : public Buffer {
179 std::unique_ptr<WritableMemoryBuffer> Buf;
180
181public:
182 void allocate(size_t Size) override;
183 uint8_t *getBufferStart() override;
184 Error commit() override;
185
186 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
187
188 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000189};
190
191class Writer {
192protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000193 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000194 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000195
196public:
197 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000198 virtual void finalize() = 0;
199 virtual void write() = 0;
200
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000201 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000202};
203
204template <class ELFT> class ELFWriter : public Writer {
205private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000206 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000207 using Elf_Shdr = typename ELFT::Shdr;
208 using Elf_Phdr = typename ELFT::Phdr;
209 using Elf_Ehdr = typename ELFT::Ehdr;
210
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000211 void initEhdrSegment();
212
Jake Ehrlich76e91102018-01-25 22:46:17 +0000213 void writeEhdr();
214 void writePhdr(const Segment &Seg);
215 void writeShdr(const SectionBase &Sec);
216
217 void writePhdrs();
218 void writeShdrs();
219 void writeSectionData();
220
221 void assignOffsets();
222
223 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
224
225 size_t totalSize() const;
226
227public:
228 virtual ~ELFWriter() {}
229 bool WriteSectionHeaders = true;
230
231 void finalize() override;
232 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000233 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
234 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000235};
236
237class BinaryWriter : public Writer {
238private:
239 std::unique_ptr<BinarySectionWriter> SecWriter;
240
241 uint64_t TotalSize;
242
243public:
244 ~BinaryWriter() {}
245 void finalize() override;
246 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000247 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000248};
249
Petr Hosek05a04cb2017-08-01 00:33:58 +0000250class SectionBase {
251public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000252 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000253 Segment *ParentSegment = nullptr;
254 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000255 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000256 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000257 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000258
259 uint64_t Addr = 0;
260 uint64_t Align = 1;
261 uint32_t EntrySize = 0;
262 uint64_t Flags = 0;
263 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000264 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000265 uint64_t NameIndex = 0;
266 uint64_t Offset = 0;
267 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000268 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000269 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000270
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000271 SectionBase() = default;
272 SectionBase(const SectionBase &) = default;
273
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000274 virtual ~SectionBase() = default;
275
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000276 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000277 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000278 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000279 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000280 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000281 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000282};
283
284class Segment {
285private:
286 struct SectionCompare {
287 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
288 // Some sections might have the same address if one of them is empty. To
289 // fix this we can use the lexicographic ordering on ->Addr and the
290 // address of the actully stored section.
291 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
292 return Lhs < Rhs;
293 return Lhs->OriginalOffset < Rhs->OriginalOffset;
294 }
295 };
296
297 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000298 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000299
300public:
301 uint64_t Align;
302 uint64_t FileSize;
303 uint32_t Flags;
304 uint32_t Index;
305 uint64_t MemSize;
306 uint64_t Offset;
307 uint64_t PAddr;
308 uint64_t Type;
309 uint64_t VAddr;
310
Petr Hosek3f383832017-08-26 01:32:20 +0000311 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000312 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000313
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000314 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000315 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000316
Petr Hosek05a04cb2017-08-01 00:33:58 +0000317 const SectionBase *firstSection() const {
318 if (!Sections.empty())
319 return *Sections.begin();
320 return nullptr;
321 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000322
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000323 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
324 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000325};
326
327class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000328 MAKE_SEC_WRITER_FRIEND
329
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000330 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000331 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000332
333public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000334 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000335
Jake Ehrlich76e91102018-01-25 22:46:17 +0000336 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000337 void removeSectionReferences(const SectionBase *Sec) override;
338 void initialize(SectionTableRef SecTable) override;
339 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000340};
341
Jake Ehrliche8437de2017-12-19 00:47:30 +0000342class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000343 MAKE_SEC_WRITER_FRIEND
344
Jake Ehrliche8437de2017-12-19 00:47:30 +0000345 std::vector<uint8_t> Data;
346
347public:
348 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
349 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000350 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000351 Type = ELF::SHT_PROGBITS;
352 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000353 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000354 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000355
356 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000357};
358
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000359class CompressedSection : public SectionBase {
360 MAKE_SEC_WRITER_FRIEND
361
362 DebugCompressionType CompressionType;
363 uint64_t DecompressedSize;
364 uint64_t DecompressedAlign;
365 SmallVector<char, 128> CompressedData;
366
367public:
368 CompressedSection(const SectionBase &Sec,
369 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000370 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
371 uint64_t DecompressedAlign);
372
373 uint64_t getDecompressedSize() const { return DecompressedSize; }
374 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
375
376 void accept(SectionVisitor &Visitor) const override;
377
378 static bool classof(const SectionBase *S) {
379 return (S->Flags & ELF::SHF_COMPRESSED) ||
380 (StringRef(S->Name).startswith(".zdebug"));
381 }
382};
383
384class DecompressedSection : public SectionBase {
385 MAKE_SEC_WRITER_FRIEND
386
387public:
388 explicit DecompressedSection(const CompressedSection &Sec)
389 : SectionBase(Sec) {
390 Size = Sec.getDecompressedSize();
391 Align = Sec.getDecompressedAlign();
392 Flags = (Flags & ~ELF::SHF_COMPRESSED);
393 if (StringRef(Name).startswith(".zdebug"))
394 Name = "." + Name.substr(2);
395 }
396
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000397 void accept(SectionVisitor &Visitor) const override;
398};
399
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000400// There are two types of string tables that can exist, dynamic and not dynamic.
401// In the dynamic case the string table is allocated. Changing a dynamic string
402// table would mean altering virtual addresses and thus the memory image. So
403// dynamic string tables should not have an interface to modify them or
404// reconstruct them. This type lets us reconstruct a string table. To avoid
405// this class being used for dynamic string tables (which has happened) the
406// classof method checks that the particular instance is not allocated. This
407// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000408class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000409 MAKE_SEC_WRITER_FRIEND
410
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000411 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000412
413public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000414 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
415 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000416 }
417
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000418 void addString(StringRef Name);
419 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000420 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000421 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000422
Petr Hosek05a04cb2017-08-01 00:33:58 +0000423 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000424 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000425 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000426 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000427 }
428};
429
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000430// Symbols have a st_shndx field that normally stores an index but occasionally
431// stores a different special value. This enum keeps track of what the st_shndx
432// field means. Most of the values are just copies of the special SHN_* values.
433// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
434enum SymbolShndxType {
435 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000436 SYMBOL_ABS = ELF::SHN_ABS,
437 SYMBOL_COMMON = ELF::SHN_COMMON,
438 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
439 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
440 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
441 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000442 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000443};
444
Petr Hosek79cee9e2017-08-29 02:12:03 +0000445struct Symbol {
446 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000447 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000448 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000449 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000450 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000451 uint32_t NameIndex;
452 uint64_t Size;
453 uint8_t Type;
454 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000455 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000456 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000457
458 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000459};
460
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000461class SectionIndexSection : public SectionBase {
462 MAKE_SEC_WRITER_FRIEND
463
464private:
465 std::vector<uint32_t> Indexes;
466 SymbolTableSection *Symbols = nullptr;
467
468public:
469 virtual ~SectionIndexSection() {}
470 void addIndex(uint32_t Index) {
471 Indexes.push_back(Index);
472 Size += 4;
473 }
474 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
475 void initialize(SectionTableRef SecTable) override;
476 void finalize() override;
477 void accept(SectionVisitor &Visitor) const override;
478
479 SectionIndexSection() {
480 Name = ".symtab_shndx";
481 Align = 4;
482 EntrySize = 4;
483 Type = ELF::SHT_SYMTAB_SHNDX;
484 }
485};
486
Petr Hosek79cee9e2017-08-29 02:12:03 +0000487class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000488 MAKE_SEC_WRITER_FRIEND
489
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000490 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000491 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000492
Petr Hosek79cee9e2017-08-29 02:12:03 +0000493protected:
494 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000495 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000496 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000497
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000498 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000499
Petr Hosek79cee9e2017-08-29 02:12:03 +0000500public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000501 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
502
503 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
504 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
505 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000506 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000507 // An 'empty' symbol table still contains a null symbol.
508 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000509 void setShndxTable(SectionIndexSection *ShndxTable) {
510 SectionIndexTable = ShndxTable;
511 }
512 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000513 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000514 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000515 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000516 void updateSymbols(function_ref<void(Symbol &)> Callable);
517
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000518 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000519 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000520 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000521 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000522 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000523
Petr Hosek79cee9e2017-08-29 02:12:03 +0000524 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000525 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000526 }
527};
528
Petr Hosekd7df9b22017-09-06 23:41:02 +0000529struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000530 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000531 uint64_t Offset;
532 uint64_t Addend;
533 uint32_t Type;
534};
535
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000536// All relocation sections denote relocations to apply to another section.
537// However, some relocation sections use a dynamic symbol table and others use
538// a regular symbol table. Because the types of the two symbol tables differ in
539// our system (because they should behave differently) we can't uniformly
540// represent all relocations with the same base class if we expose an interface
541// that mentions the symbol table type. So we split the two base types into two
542// different classes, one which handles the section the relocation is applied to
543// and another which handles the symbol table type. The symbol table type is
544// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
545class RelocationSectionBase : public SectionBase {
546protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000547 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000548
549public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000550 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000551 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000552
553 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000554 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000555 }
556};
557
558// Takes the symbol table type to use as a parameter so that we can deduplicate
559// that code between the two symbol table types.
560template <class SymTabType>
561class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000562 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000563 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000564
565protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000566 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000567
568public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000569 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000570 void initialize(SectionTableRef SecTable) override;
571 void finalize() override;
572};
573
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000574class RelocationSection
575 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000576 MAKE_SEC_WRITER_FRIEND
577
Petr Hosekd7df9b22017-09-06 23:41:02 +0000578 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000579
Petr Hosekd7df9b22017-09-06 23:41:02 +0000580public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000581 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000582 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000583 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000584 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000585
Petr Hosekd7df9b22017-09-06 23:41:02 +0000586 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000587 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000588 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000589 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000590 }
591};
592
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000593// TODO: The way stripping and groups interact is complicated
594// and still needs to be worked on.
595
596class GroupSection : public SectionBase {
597 MAKE_SEC_WRITER_FRIEND
598 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000599 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000600 ELF::Elf32_Word FlagWord;
601 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000602
603public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000604 // TODO: Contents is present in several classes of the hierarchy.
605 // This needs to be refactored to avoid duplication.
606 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000607
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000608 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
609
610 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000611 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000612 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
613 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
614
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000615 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000616 void accept(SectionVisitor &) const override;
617 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000618 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000619 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000620
621 static bool classof(const SectionBase *S) {
622 return S->Type == ELF::SHT_GROUP;
623 }
624};
625
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000626class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000627public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000628 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000629
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000630 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000631 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000632 }
633};
634
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000635class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000636public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000637 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000638
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000639 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000640 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000641 }
642};
643
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000644class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000645 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000646 MAKE_SEC_WRITER_FRIEND
647
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000648private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000649 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000650
651public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000652 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000653
Jake Ehrlich76e91102018-01-25 22:46:17 +0000654 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000655
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000656 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000657 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000658 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000659 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000660 }
661};
662
Jake Ehrlich76e91102018-01-25 22:46:17 +0000663class GnuDebugLinkSection : public SectionBase {
664 MAKE_SEC_WRITER_FRIEND
665
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000666private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000667 StringRef FileName;
668 uint32_t CRC32;
669
670 void init(StringRef File, StringRef Data);
671
672public:
673 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000674 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000675 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000676};
677
Jake Ehrlich76e91102018-01-25 22:46:17 +0000678class Reader {
679public:
680 virtual ~Reader();
681 virtual std::unique_ptr<Object> create() const = 0;
682};
683
Jake Ehrlich76e91102018-01-25 22:46:17 +0000684using object::Binary;
685using object::ELFFile;
686using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000687using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000688
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000689template <class ELFT> class BinaryELFBuilder {
690 using Elf_Sym = typename ELFT::Sym;
691
692 uint16_t EMachine;
693 MemoryBuffer *MemBuf;
694 std::unique_ptr<Object> Obj;
695
696 void initFileHeader();
697 void initHeaderSegment();
698 StringTableSection *addStrTab();
699 SymbolTableSection *addSymTab(StringTableSection *StrTab);
700 void addData(SymbolTableSection *SymTab);
701 void initSections();
702
703public:
704 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
705 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
706
707 std::unique_ptr<Object> build();
708};
709
Jake Ehrlich76e91102018-01-25 22:46:17 +0000710template <class ELFT> class ELFBuilder {
711private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000712 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000713 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000714 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000715
716 const ELFFile<ELFT> &ElfFile;
717 Object &Obj;
718
Jake Ehrlich6452b112018-02-14 23:31:33 +0000719 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000720 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000721 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000722 void initSymbolTable(SymbolTableSection *SymTab);
723 void readSectionHeaders();
724 SectionBase &makeSection(const Elf_Shdr &Shdr);
725
726public:
727 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
728 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
729
730 void build();
731};
732
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000733class BinaryReader : public Reader {
734 const MachineInfo &MInfo;
735 MemoryBuffer *MemBuf;
736
737public:
738 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
739 : MInfo(MI), MemBuf(MB) {}
740 std::unique_ptr<Object> create() const override;
741};
742
Jake Ehrlich76e91102018-01-25 22:46:17 +0000743class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000744 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000745
746public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000747 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000748 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000749};
750
751class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000752private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000753 using SecPtr = std::unique_ptr<SectionBase>;
754 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000755
Petr Hosekc4df10e2017-08-04 21:09:26 +0000756 std::vector<SecPtr> Sections;
757 std::vector<SegPtr> Segments;
758
Petr Hosek05a04cb2017-08-01 00:33:58 +0000759public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000760 template <class T>
761 using Range = iterator_range<
762 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
763
764 template <class T>
765 using ConstRange = iterator_range<pointee_iterator<
766 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
767
Jake Ehrlich6452b112018-02-14 23:31:33 +0000768 // It is often the case that the ELF header and the program header table are
769 // not present in any segment. This could be a problem during file layout,
770 // because other segments may get assigned an offset where either of the
771 // two should reside, which will effectively corrupt the resulting binary.
772 // Other than that we use these segments to track program header offsets
773 // when they may not follow the ELF header.
774 Segment ElfHdrSegment;
775 Segment ProgramHdrSegment;
776
Petr Hosek05a04cb2017-08-01 00:33:58 +0000777 uint64_t Entry;
778 uint64_t SHOffset;
779 uint32_t Type;
780 uint32_t Machine;
781 uint32_t Version;
782 uint32_t Flags;
783
Jake Ehrlich76e91102018-01-25 22:46:17 +0000784 StringTableSection *SectionNames = nullptr;
785 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000786 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000787
Aaron Ballman09f46a72018-01-25 21:03:38 +0000788 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000789 SectionTableRef sections() { return SectionTableRef(Sections); }
790 ConstRange<SectionBase> sections() const {
791 return make_pointee_range(Sections);
792 }
793 Range<Segment> segments() { return make_pointee_range(Segments); }
794 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000795
Jake Ehrlich76e91102018-01-25 22:46:17 +0000796 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000797 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000798 template <class T, class... Ts> T &addSection(Ts &&... Args) {
799 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
800 auto Ptr = Sec.get();
801 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000802 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000803 return *Ptr;
804 }
805 Segment &addSegment(ArrayRef<uint8_t> Data) {
806 Segments.emplace_back(llvm::make_unique<Segment>(Data));
807 return *Segments.back();
808 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000809};
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000810} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000811} // end namespace llvm
812
813#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H