blob: 5fb03a5501e76f6576651dcc5d8b36463917449e [file] [log] [blame]
Petr Hosek05a04cb2017-08-01 00:33:58 +00001//===- Object.h -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000010#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
11#define LLVM_TOOLS_OBJCOPY_OBJECT_H
Petr Hosek05a04cb2017-08-01 00:33:58 +000012
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000013#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000017#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELFObjectFile.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000019#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000020#include "llvm/Support/JamCRC.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000021#include <cstddef>
22#include <cstdint>
23#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000024#include <memory>
25#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000026#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000027
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000028namespace llvm {
Puyan Lotfi99124cc2018-09-07 08:10:22 +000029enum class DebugCompressionType;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000030namespace objcopy {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000031
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000032class Buffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000033class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000034class Section;
35class OwnedDataSection;
36class StringTableSection;
37class SymbolTableSection;
38class RelocationSection;
39class DynamicRelocationSection;
40class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000041class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000042class SectionIndexSection;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000043class CompressedSection;
Puyan Lotfiaf048642018-10-01 10:29:41 +000044class DecompressedSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000045class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000046class Object;
Paul Semel4246a462018-05-09 21:36:54 +000047struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000048
49class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000050 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000051
52public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000053 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
54
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000055 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000056 : Sections(Secs) {}
57 SectionTableRef(const SectionTableRef &) = default;
58
Jake Ehrlich76e91102018-01-25 22:46:17 +000059 iterator begin() { return iterator(Sections.data()); }
60 iterator end() { return iterator(Sections.data() + Sections.size()); }
61
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000062 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000063
64 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000065 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000066};
Petr Hosek05a04cb2017-08-01 00:33:58 +000067
Jake Ehrlich76e91102018-01-25 22:46:17 +000068enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
69
Jordan Rupprechtcf676332018-08-17 18:51:11 +000070// This type keeps track of the machine info for various architectures. This
71// lets us map architecture names to ELF types and the e_machine value of the
72// ELF file.
73struct MachineInfo {
74 uint16_t EMachine;
75 bool Is64Bit;
76 bool IsLittleEndian;
77};
78
Jake Ehrlich76e91102018-01-25 22:46:17 +000079class SectionVisitor {
80public:
81 virtual ~SectionVisitor();
82
83 virtual void visit(const Section &Sec) = 0;
84 virtual void visit(const OwnedDataSection &Sec) = 0;
85 virtual void visit(const StringTableSection &Sec) = 0;
86 virtual void visit(const SymbolTableSection &Sec) = 0;
87 virtual void visit(const RelocationSection &Sec) = 0;
88 virtual void visit(const DynamicRelocationSection &Sec) = 0;
89 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000090 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000091 virtual void visit(const SectionIndexSection &Sec) = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000092 virtual void visit(const CompressedSection &Sec) = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +000093 virtual void visit(const DecompressedSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000094};
95
96class SectionWriter : public SectionVisitor {
97protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000098 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000099
100public:
101 virtual ~SectionWriter(){};
102
103 void visit(const Section &Sec) override;
104 void visit(const OwnedDataSection &Sec) override;
105 void visit(const StringTableSection &Sec) override;
106 void visit(const DynamicRelocationSection &Sec) override;
107 virtual void visit(const SymbolTableSection &Sec) override = 0;
108 virtual void visit(const RelocationSection &Sec) override = 0;
109 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000110 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000111 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000112 virtual void visit(const CompressedSection &Sec) override = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000113 virtual void visit(const DecompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000114
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000115 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000116};
117
118template <class ELFT> class ELFSectionWriter : public SectionWriter {
119private:
120 using Elf_Word = typename ELFT::Word;
121 using Elf_Rel = typename ELFT::Rel;
122 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000123 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000124
125public:
126 virtual ~ELFSectionWriter() {}
127 void visit(const SymbolTableSection &Sec) override;
128 void visit(const RelocationSection &Sec) override;
129 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000130 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000131 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000132 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000133 void visit(const DecompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000134
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000135 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000136};
137
138#define MAKE_SEC_WRITER_FRIEND \
139 friend class SectionWriter; \
140 template <class ELFT> friend class ELFSectionWriter;
141
142class BinarySectionWriter : public SectionWriter {
143public:
144 virtual ~BinarySectionWriter() {}
145
146 void visit(const SymbolTableSection &Sec) override;
147 void visit(const RelocationSection &Sec) override;
148 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000149 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000150 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000151 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000152 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000153
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000154 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
155};
156
157// The class Buffer abstracts out the common interface of FileOutputBuffer and
158// WritableMemoryBuffer so that the hierarchy of Writers depends on this
159// abstract interface and doesn't depend on a particular implementation.
160// TODO: refactor the buffer classes in LLVM to enable us to use them here
161// directly.
162class Buffer {
163 StringRef Name;
164
165public:
166 virtual ~Buffer();
167 virtual void allocate(size_t Size) = 0;
168 virtual uint8_t *getBufferStart() = 0;
169 virtual Error commit() = 0;
170
171 explicit Buffer(StringRef Name) : Name(Name) {}
172 StringRef getName() const { return Name; }
173};
174
175class FileBuffer : public Buffer {
176 std::unique_ptr<FileOutputBuffer> Buf;
177
178public:
179 void allocate(size_t Size) override;
180 uint8_t *getBufferStart() override;
181 Error commit() override;
182
183 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
184};
185
186class MemBuffer : public Buffer {
187 std::unique_ptr<WritableMemoryBuffer> Buf;
188
189public:
190 void allocate(size_t Size) override;
191 uint8_t *getBufferStart() override;
192 Error commit() override;
193
194 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
195
196 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000197};
198
199class Writer {
200protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000201 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000202 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000203
204public:
205 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000206 virtual void finalize() = 0;
207 virtual void write() = 0;
208
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000209 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000210};
211
212template <class ELFT> class ELFWriter : public Writer {
213private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000214 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000215 using Elf_Shdr = typename ELFT::Shdr;
216 using Elf_Phdr = typename ELFT::Phdr;
217 using Elf_Ehdr = typename ELFT::Ehdr;
218
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000219 void initEhdrSegment();
220
Jake Ehrlich76e91102018-01-25 22:46:17 +0000221 void writeEhdr();
222 void writePhdr(const Segment &Seg);
223 void writeShdr(const SectionBase &Sec);
224
225 void writePhdrs();
226 void writeShdrs();
227 void writeSectionData();
228
229 void assignOffsets();
230
231 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
232
233 size_t totalSize() const;
234
235public:
236 virtual ~ELFWriter() {}
237 bool WriteSectionHeaders = true;
238
239 void finalize() override;
240 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000241 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
242 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000243};
244
245class BinaryWriter : public Writer {
246private:
247 std::unique_ptr<BinarySectionWriter> SecWriter;
248
249 uint64_t TotalSize;
250
251public:
252 ~BinaryWriter() {}
253 void finalize() override;
254 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000255 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000256};
257
Petr Hosek05a04cb2017-08-01 00:33:58 +0000258class SectionBase {
259public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000260 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000261 Segment *ParentSegment = nullptr;
262 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000263 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000264 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000265 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000266
267 uint64_t Addr = 0;
268 uint64_t Align = 1;
269 uint32_t EntrySize = 0;
270 uint64_t Flags = 0;
271 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000272 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000273 uint64_t NameIndex = 0;
274 uint64_t Offset = 0;
275 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000276 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000277 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000278
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000279 SectionBase() = default;
280 SectionBase(const SectionBase &) = default;
281
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000282 virtual ~SectionBase() = default;
283
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000284 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000285 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000286 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000287 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000288 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000289 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000290};
291
292class Segment {
293private:
294 struct SectionCompare {
295 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
296 // Some sections might have the same address if one of them is empty. To
297 // fix this we can use the lexicographic ordering on ->Addr and the
298 // address of the actully stored section.
299 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
300 return Lhs < Rhs;
301 return Lhs->OriginalOffset < Rhs->OriginalOffset;
302 }
303 };
304
305 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000306 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000307
308public:
309 uint64_t Align;
310 uint64_t FileSize;
311 uint32_t Flags;
312 uint32_t Index;
313 uint64_t MemSize;
314 uint64_t Offset;
315 uint64_t PAddr;
316 uint64_t Type;
317 uint64_t VAddr;
318
Petr Hosek3f383832017-08-26 01:32:20 +0000319 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000320 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000321
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000322 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000323 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000324
Petr Hosek05a04cb2017-08-01 00:33:58 +0000325 const SectionBase *firstSection() const {
326 if (!Sections.empty())
327 return *Sections.begin();
328 return nullptr;
329 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000330
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000331 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
332 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000333};
334
335class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000336 MAKE_SEC_WRITER_FRIEND
337
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000338 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000339 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000340
341public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000342 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000343
Jake Ehrlich76e91102018-01-25 22:46:17 +0000344 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000345 void removeSectionReferences(const SectionBase *Sec) override;
346 void initialize(SectionTableRef SecTable) override;
347 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000348};
349
Jake Ehrliche8437de2017-12-19 00:47:30 +0000350class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000351 MAKE_SEC_WRITER_FRIEND
352
Jake Ehrliche8437de2017-12-19 00:47:30 +0000353 std::vector<uint8_t> Data;
354
355public:
356 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
357 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000358 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000359 Type = ELF::SHT_PROGBITS;
360 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000361 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000362 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000363
364 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000365};
366
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000367class CompressedSection : public SectionBase {
368 MAKE_SEC_WRITER_FRIEND
369
370 DebugCompressionType CompressionType;
371 uint64_t DecompressedSize;
372 uint64_t DecompressedAlign;
373 SmallVector<char, 128> CompressedData;
374
375public:
376 CompressedSection(const SectionBase &Sec,
377 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000378 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
379 uint64_t DecompressedAlign);
380
381 uint64_t getDecompressedSize() const { return DecompressedSize; }
382 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
383
384 void accept(SectionVisitor &Visitor) const override;
385
386 static bool classof(const SectionBase *S) {
387 return (S->Flags & ELF::SHF_COMPRESSED) ||
388 (StringRef(S->Name).startswith(".zdebug"));
389 }
390};
391
392class DecompressedSection : public SectionBase {
393 MAKE_SEC_WRITER_FRIEND
394
395public:
396 explicit DecompressedSection(const CompressedSection &Sec)
397 : SectionBase(Sec) {
398 Size = Sec.getDecompressedSize();
399 Align = Sec.getDecompressedAlign();
400 Flags = (Flags & ~ELF::SHF_COMPRESSED);
401 if (StringRef(Name).startswith(".zdebug"))
402 Name = "." + Name.substr(2);
403 }
404
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000405 void accept(SectionVisitor &Visitor) const override;
406};
407
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000408// There are two types of string tables that can exist, dynamic and not dynamic.
409// In the dynamic case the string table is allocated. Changing a dynamic string
410// table would mean altering virtual addresses and thus the memory image. So
411// dynamic string tables should not have an interface to modify them or
412// reconstruct them. This type lets us reconstruct a string table. To avoid
413// this class being used for dynamic string tables (which has happened) the
414// classof method checks that the particular instance is not allocated. This
415// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000416class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000417 MAKE_SEC_WRITER_FRIEND
418
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000419 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000420
421public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000422 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
423 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000424 }
425
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000426 void addString(StringRef Name);
427 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000428 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000429 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000430
Petr Hosek05a04cb2017-08-01 00:33:58 +0000431 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000432 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000433 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000434 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000435 }
436};
437
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000438// Symbols have a st_shndx field that normally stores an index but occasionally
439// stores a different special value. This enum keeps track of what the st_shndx
440// field means. Most of the values are just copies of the special SHN_* values.
441// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
442enum SymbolShndxType {
443 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000444 SYMBOL_ABS = ELF::SHN_ABS,
445 SYMBOL_COMMON = ELF::SHN_COMMON,
446 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
447 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
448 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
449 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000450 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000451};
452
Petr Hosek79cee9e2017-08-29 02:12:03 +0000453struct Symbol {
454 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000455 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000456 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000457 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000458 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000459 uint32_t NameIndex;
460 uint64_t Size;
461 uint8_t Type;
462 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000463 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000464 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000465
466 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000467};
468
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000469class SectionIndexSection : public SectionBase {
470 MAKE_SEC_WRITER_FRIEND
471
472private:
473 std::vector<uint32_t> Indexes;
474 SymbolTableSection *Symbols = nullptr;
475
476public:
477 virtual ~SectionIndexSection() {}
478 void addIndex(uint32_t Index) {
479 Indexes.push_back(Index);
480 Size += 4;
481 }
482 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
483 void initialize(SectionTableRef SecTable) override;
484 void finalize() override;
485 void accept(SectionVisitor &Visitor) const override;
486
487 SectionIndexSection() {
488 Name = ".symtab_shndx";
489 Align = 4;
490 EntrySize = 4;
491 Type = ELF::SHT_SYMTAB_SHNDX;
492 }
493};
494
Petr Hosek79cee9e2017-08-29 02:12:03 +0000495class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000496 MAKE_SEC_WRITER_FRIEND
497
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000498 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000499 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000500
Petr Hosek79cee9e2017-08-29 02:12:03 +0000501protected:
502 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000503 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000504 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000505
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000506 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000507
Petr Hosek79cee9e2017-08-29 02:12:03 +0000508public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000509 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
510
511 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
512 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
513 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000514 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000515 // An 'empty' symbol table still contains a null symbol.
516 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000517 void setShndxTable(SectionIndexSection *ShndxTable) {
518 SectionIndexTable = ShndxTable;
519 }
520 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000521 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000522 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000523 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000524 void updateSymbols(function_ref<void(Symbol &)> Callable);
525
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000526 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000527 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000528 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000529 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000530 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000531
Petr Hosek79cee9e2017-08-29 02:12:03 +0000532 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000533 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000534 }
535};
536
Petr Hosekd7df9b22017-09-06 23:41:02 +0000537struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000538 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000539 uint64_t Offset;
540 uint64_t Addend;
541 uint32_t Type;
542};
543
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000544// All relocation sections denote relocations to apply to another section.
545// However, some relocation sections use a dynamic symbol table and others use
546// a regular symbol table. Because the types of the two symbol tables differ in
547// our system (because they should behave differently) we can't uniformly
548// represent all relocations with the same base class if we expose an interface
549// that mentions the symbol table type. So we split the two base types into two
550// different classes, one which handles the section the relocation is applied to
551// and another which handles the symbol table type. The symbol table type is
552// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
553class RelocationSectionBase : public SectionBase {
554protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000555 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000556
557public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000558 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000559 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000560
561 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000562 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000563 }
564};
565
566// Takes the symbol table type to use as a parameter so that we can deduplicate
567// that code between the two symbol table types.
568template <class SymTabType>
569class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000570 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000571 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000572
573protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000574 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000575
576public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000577 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000578 void initialize(SectionTableRef SecTable) override;
579 void finalize() override;
580};
581
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000582class RelocationSection
583 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000584 MAKE_SEC_WRITER_FRIEND
585
Petr Hosekd7df9b22017-09-06 23:41:02 +0000586 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000587
Petr Hosekd7df9b22017-09-06 23:41:02 +0000588public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000589 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000590 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000591 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000592 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000593
Petr Hosekd7df9b22017-09-06 23:41:02 +0000594 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000595 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000596 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000597 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000598 }
599};
600
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000601// TODO: The way stripping and groups interact is complicated
602// and still needs to be worked on.
603
604class GroupSection : public SectionBase {
605 MAKE_SEC_WRITER_FRIEND
606 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000607 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000608 ELF::Elf32_Word FlagWord;
609 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000610
611public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000612 // TODO: Contents is present in several classes of the hierarchy.
613 // This needs to be refactored to avoid duplication.
614 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000615
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000616 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
617
618 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000619 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000620 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
621 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
622
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000623 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000624 void accept(SectionVisitor &) const override;
625 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000626 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000627 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000628
629 static bool classof(const SectionBase *S) {
630 return S->Type == ELF::SHT_GROUP;
631 }
632};
633
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000634class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000635public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000636 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000637
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000638 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000639 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000640 }
641};
642
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000643class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000644public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000645 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000646
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000647 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000648 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000649 }
650};
651
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000652class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000653 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000654 MAKE_SEC_WRITER_FRIEND
655
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000656private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000657 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000658
659public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000660 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000661
Jake Ehrlich76e91102018-01-25 22:46:17 +0000662 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000663
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000664 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000665 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000666 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000667 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000668 }
669};
670
Jake Ehrlich76e91102018-01-25 22:46:17 +0000671class GnuDebugLinkSection : public SectionBase {
672 MAKE_SEC_WRITER_FRIEND
673
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000674private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000675 StringRef FileName;
676 uint32_t CRC32;
677
678 void init(StringRef File, StringRef Data);
679
680public:
681 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000682 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000683 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000684};
685
Jake Ehrlich76e91102018-01-25 22:46:17 +0000686class Reader {
687public:
688 virtual ~Reader();
689 virtual std::unique_ptr<Object> create() const = 0;
690};
691
Jake Ehrlich76e91102018-01-25 22:46:17 +0000692using object::Binary;
693using object::ELFFile;
694using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000695using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000696
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000697template <class ELFT> class BinaryELFBuilder {
698 using Elf_Sym = typename ELFT::Sym;
699
700 uint16_t EMachine;
701 MemoryBuffer *MemBuf;
702 std::unique_ptr<Object> Obj;
703
704 void initFileHeader();
705 void initHeaderSegment();
706 StringTableSection *addStrTab();
707 SymbolTableSection *addSymTab(StringTableSection *StrTab);
708 void addData(SymbolTableSection *SymTab);
709 void initSections();
710
711public:
712 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
713 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
714
715 std::unique_ptr<Object> build();
716};
717
Jake Ehrlich76e91102018-01-25 22:46:17 +0000718template <class ELFT> class ELFBuilder {
719private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000720 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000721 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000722 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000723
724 const ELFFile<ELFT> &ElfFile;
725 Object &Obj;
726
Jake Ehrlich6452b112018-02-14 23:31:33 +0000727 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000728 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000729 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000730 void initSymbolTable(SymbolTableSection *SymTab);
731 void readSectionHeaders();
732 SectionBase &makeSection(const Elf_Shdr &Shdr);
733
734public:
735 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
736 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
737
738 void build();
739};
740
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000741class BinaryReader : public Reader {
742 const MachineInfo &MInfo;
743 MemoryBuffer *MemBuf;
744
745public:
746 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
747 : MInfo(MI), MemBuf(MB) {}
748 std::unique_ptr<Object> create() const override;
749};
750
Jake Ehrlich76e91102018-01-25 22:46:17 +0000751class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000752 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000753
754public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000755 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000756 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000757};
758
759class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000760private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000761 using SecPtr = std::unique_ptr<SectionBase>;
762 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000763
Petr Hosekc4df10e2017-08-04 21:09:26 +0000764 std::vector<SecPtr> Sections;
765 std::vector<SegPtr> Segments;
766
Petr Hosek05a04cb2017-08-01 00:33:58 +0000767public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000768 template <class T>
769 using Range = iterator_range<
770 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
771
772 template <class T>
773 using ConstRange = iterator_range<pointee_iterator<
774 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
775
Jake Ehrlich6452b112018-02-14 23:31:33 +0000776 // It is often the case that the ELF header and the program header table are
777 // not present in any segment. This could be a problem during file layout,
778 // because other segments may get assigned an offset where either of the
779 // two should reside, which will effectively corrupt the resulting binary.
780 // Other than that we use these segments to track program header offsets
781 // when they may not follow the ELF header.
782 Segment ElfHdrSegment;
783 Segment ProgramHdrSegment;
784
Petr Hosek05a04cb2017-08-01 00:33:58 +0000785 uint64_t Entry;
786 uint64_t SHOffset;
787 uint32_t Type;
788 uint32_t Machine;
789 uint32_t Version;
790 uint32_t Flags;
791
Jake Ehrlich76e91102018-01-25 22:46:17 +0000792 StringTableSection *SectionNames = nullptr;
793 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000794 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000795
Aaron Ballman09f46a72018-01-25 21:03:38 +0000796 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000797 SectionTableRef sections() { return SectionTableRef(Sections); }
798 ConstRange<SectionBase> sections() const {
799 return make_pointee_range(Sections);
800 }
801 Range<Segment> segments() { return make_pointee_range(Segments); }
802 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000803
Jake Ehrlich76e91102018-01-25 22:46:17 +0000804 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000805 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000806 template <class T, class... Ts> T &addSection(Ts &&... Args) {
807 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
808 auto Ptr = Sec.get();
809 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000810 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000811 return *Ptr;
812 }
813 Segment &addSegment(ArrayRef<uint8_t> Data) {
814 Segments.emplace_back(llvm::make_unique<Segment>(Data));
815 return *Segments.back();
816 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000817};
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000818} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000819} // end namespace llvm
820
821#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H