blob: b0e83d2f56c34ca6831b76d7991bd64ad7f159ed [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:
Jordan Rupprecht1f821762019-01-03 17:45:30 +000074 virtual ~SectionVisitor() = default;
Jake Ehrlich76e91102018-01-25 22:46:17 +000075
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
Jordan Rupprecht1f821762019-01-03 17:45:30 +000089class MutableSectionVisitor {
90public:
91 virtual ~MutableSectionVisitor() = default;
92
93 virtual void visit(Section &Sec) = 0;
94 virtual void visit(OwnedDataSection &Sec) = 0;
95 virtual void visit(StringTableSection &Sec) = 0;
96 virtual void visit(SymbolTableSection &Sec) = 0;
97 virtual void visit(RelocationSection &Sec) = 0;
98 virtual void visit(DynamicRelocationSection &Sec) = 0;
99 virtual void visit(GnuDebugLinkSection &Sec) = 0;
100 virtual void visit(GroupSection &Sec) = 0;
101 virtual void visit(SectionIndexSection &Sec) = 0;
102 virtual void visit(CompressedSection &Sec) = 0;
103 virtual void visit(DecompressedSection &Sec) = 0;
104};
105
Jake Ehrlich76e91102018-01-25 22:46:17 +0000106class SectionWriter : public SectionVisitor {
107protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000108 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109
110public:
111 virtual ~SectionWriter(){};
112
113 void visit(const Section &Sec) override;
114 void visit(const OwnedDataSection &Sec) override;
115 void visit(const StringTableSection &Sec) override;
116 void visit(const DynamicRelocationSection &Sec) override;
117 virtual void visit(const SymbolTableSection &Sec) override = 0;
118 virtual void visit(const RelocationSection &Sec) override = 0;
119 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000120 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000121 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000122 virtual void visit(const CompressedSection &Sec) override = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000123 virtual void visit(const DecompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000124
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000125 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000126};
127
128template <class ELFT> class ELFSectionWriter : public SectionWriter {
129private:
130 using Elf_Word = typename ELFT::Word;
131 using Elf_Rel = typename ELFT::Rel;
132 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000133 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000134
135public:
136 virtual ~ELFSectionWriter() {}
137 void visit(const SymbolTableSection &Sec) override;
138 void visit(const RelocationSection &Sec) override;
139 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000140 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000141 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000142 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000143 void visit(const DecompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000144
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000145 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000146};
147
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000148template <class ELFT> class ELFSectionSizer : public MutableSectionVisitor {
149private:
150 using Elf_Rel = typename ELFT::Rel;
151 using Elf_Rela = typename ELFT::Rela;
152 using Elf_Sym = typename ELFT::Sym;
153
154public:
155 void visit(Section &Sec) override;
156 void visit(OwnedDataSection &Sec) override;
157 void visit(StringTableSection &Sec) override;
158 void visit(DynamicRelocationSection &Sec) override;
159 void visit(SymbolTableSection &Sec) override;
160 void visit(RelocationSection &Sec) override;
161 void visit(GnuDebugLinkSection &Sec) override;
162 void visit(GroupSection &Sec) override;
163 void visit(SectionIndexSection &Sec) override;
164 void visit(CompressedSection &Sec) override;
165 void visit(DecompressedSection &Sec) override;
166};
167
Jake Ehrlich76e91102018-01-25 22:46:17 +0000168#define MAKE_SEC_WRITER_FRIEND \
169 friend class SectionWriter; \
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000170 template <class ELFT> friend class ELFSectionWriter; \
171 template <class ELFT> friend class ELFSectionSizer;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000172
173class BinarySectionWriter : public SectionWriter {
174public:
175 virtual ~BinarySectionWriter() {}
176
177 void visit(const SymbolTableSection &Sec) override;
178 void visit(const RelocationSection &Sec) override;
179 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000180 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000181 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000182 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000183 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000184
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000185 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
186};
187
Jake Ehrlich76e91102018-01-25 22:46:17 +0000188class Writer {
189protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000190 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000191 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000192
193public:
194 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000195 virtual void finalize() = 0;
196 virtual void write() = 0;
197
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000198 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000199};
200
201template <class ELFT> class ELFWriter : public Writer {
202private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000203 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000204 using Elf_Shdr = typename ELFT::Shdr;
205 using Elf_Phdr = typename ELFT::Phdr;
206 using Elf_Ehdr = typename ELFT::Ehdr;
207
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000208 void initEhdrSegment();
209
Jake Ehrlich76e91102018-01-25 22:46:17 +0000210 void writeEhdr();
211 void writePhdr(const Segment &Seg);
212 void writeShdr(const SectionBase &Sec);
213
214 void writePhdrs();
215 void writeShdrs();
216 void writeSectionData();
217
218 void assignOffsets();
219
220 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
221
222 size_t totalSize() const;
223
224public:
225 virtual ~ELFWriter() {}
226 bool WriteSectionHeaders = true;
227
228 void finalize() override;
229 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000230 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
231 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000232};
233
234class BinaryWriter : public Writer {
235private:
236 std::unique_ptr<BinarySectionWriter> SecWriter;
237
238 uint64_t TotalSize;
239
240public:
241 ~BinaryWriter() {}
242 void finalize() override;
243 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000244 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000245};
246
Petr Hosek05a04cb2017-08-01 00:33:58 +0000247class SectionBase {
248public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000249 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000250 Segment *ParentSegment = nullptr;
251 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000252 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000253 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000254 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000255
256 uint64_t Addr = 0;
257 uint64_t Align = 1;
258 uint32_t EntrySize = 0;
259 uint64_t Flags = 0;
260 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000261 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000262 uint64_t NameIndex = 0;
263 uint64_t Offset = 0;
264 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000265 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000266 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000267
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000268 SectionBase() = default;
269 SectionBase(const SectionBase &) = default;
270
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000271 virtual ~SectionBase() = default;
272
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000273 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000274 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000275 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000276 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000277 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000278 virtual void accept(MutableSectionVisitor &Visitor) = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000279 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000280};
281
282class Segment {
283private:
284 struct SectionCompare {
285 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
286 // Some sections might have the same address if one of them is empty. To
287 // fix this we can use the lexicographic ordering on ->Addr and the
288 // address of the actully stored section.
289 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
290 return Lhs < Rhs;
291 return Lhs->OriginalOffset < Rhs->OriginalOffset;
292 }
293 };
294
295 std::set<const SectionBase *, SectionCompare> Sections;
296
297public:
Fangrui Song967ce402018-12-12 22:46:37 +0000298 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000299 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000300 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000301 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000302 uint64_t PAddr;
303 uint64_t FileSize;
304 uint64_t MemSize;
305 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306
Fangrui Song967ce402018-12-12 22:46:37 +0000307 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000308 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000309 Segment *ParentSegment = nullptr;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000310 ArrayRef<uint8_t> Contents;
Petr Hosek3f383832017-08-26 01:32:20 +0000311
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000312 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000313 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000314
Petr Hosek05a04cb2017-08-01 00:33:58 +0000315 const SectionBase *firstSection() const {
316 if (!Sections.empty())
317 return *Sections.begin();
318 return nullptr;
319 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000320
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000321 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
322 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000323};
324
325class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000326 MAKE_SEC_WRITER_FRIEND
327
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000328 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000329 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000330
331public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000332 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000333
Jake Ehrlich76e91102018-01-25 22:46:17 +0000334 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000335 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000336 void removeSectionReferences(const SectionBase *Sec) override;
337 void initialize(SectionTableRef SecTable) override;
338 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000339};
340
Jake Ehrliche8437de2017-12-19 00:47:30 +0000341class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000342 MAKE_SEC_WRITER_FRIEND
343
Jake Ehrliche8437de2017-12-19 00:47:30 +0000344 std::vector<uint8_t> Data;
345
346public:
347 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
348 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000349 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000350 Type = ELF::SHT_PROGBITS;
351 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000352 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000353 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000354
355 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000356 void accept(MutableSectionVisitor &Visitor) 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;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000377 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000378
379 static bool classof(const SectionBase *S) {
380 return (S->Flags & ELF::SHF_COMPRESSED) ||
381 (StringRef(S->Name).startswith(".zdebug"));
382 }
383};
384
385class DecompressedSection : public SectionBase {
386 MAKE_SEC_WRITER_FRIEND
387
388public:
389 explicit DecompressedSection(const CompressedSection &Sec)
390 : SectionBase(Sec) {
391 Size = Sec.getDecompressedSize();
392 Align = Sec.getDecompressedAlign();
393 Flags = (Flags & ~ELF::SHF_COMPRESSED);
394 if (StringRef(Name).startswith(".zdebug"))
395 Name = "." + Name.substr(2);
396 }
397
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000398 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000399 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000400};
401
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000402// There are two types of string tables that can exist, dynamic and not dynamic.
403// In the dynamic case the string table is allocated. Changing a dynamic string
404// table would mean altering virtual addresses and thus the memory image. So
405// dynamic string tables should not have an interface to modify them or
406// reconstruct them. This type lets us reconstruct a string table. To avoid
407// this class being used for dynamic string tables (which has happened) the
408// classof method checks that the particular instance is not allocated. This
409// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000410class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000411 MAKE_SEC_WRITER_FRIEND
412
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000413 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000414
415public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000416 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
417 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000418 }
419
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000420 void addString(StringRef Name);
421 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000422 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000423 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000424 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000425
Petr Hosek05a04cb2017-08-01 00:33:58 +0000426 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000427 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000428 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000429 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000430 }
431};
432
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000433// Symbols have a st_shndx field that normally stores an index but occasionally
434// stores a different special value. This enum keeps track of what the st_shndx
435// field means. Most of the values are just copies of the special SHN_* values.
436// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
437enum SymbolShndxType {
438 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000439 SYMBOL_ABS = ELF::SHN_ABS,
440 SYMBOL_COMMON = ELF::SHN_COMMON,
441 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
442 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
443 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
444 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000445 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000446};
447
Petr Hosek79cee9e2017-08-29 02:12:03 +0000448struct Symbol {
449 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000450 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000451 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000452 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000453 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000454 uint32_t NameIndex;
455 uint64_t Size;
456 uint8_t Type;
457 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000458 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000459 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000460
461 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000462 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000463};
464
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000465class SectionIndexSection : public SectionBase {
466 MAKE_SEC_WRITER_FRIEND
467
468private:
469 std::vector<uint32_t> Indexes;
470 SymbolTableSection *Symbols = nullptr;
471
472public:
473 virtual ~SectionIndexSection() {}
474 void addIndex(uint32_t Index) {
475 Indexes.push_back(Index);
476 Size += 4;
477 }
478 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
479 void initialize(SectionTableRef SecTable) override;
480 void finalize() override;
481 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000482 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000483
484 SectionIndexSection() {
485 Name = ".symtab_shndx";
486 Align = 4;
487 EntrySize = 4;
488 Type = ELF::SHT_SYMTAB_SHNDX;
489 }
490};
491
Petr Hosek79cee9e2017-08-29 02:12:03 +0000492class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000493 MAKE_SEC_WRITER_FRIEND
494
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000495 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000496 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000497
Petr Hosek79cee9e2017-08-29 02:12:03 +0000498protected:
499 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000500 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000501 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000502
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000503 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000504
Petr Hosek79cee9e2017-08-29 02:12:03 +0000505public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000506 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
507
508 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
509 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
510 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000511 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000512 // An 'empty' symbol table still contains a null symbol.
513 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000514 void setShndxTable(SectionIndexSection *ShndxTable) {
515 SectionIndexTable = ShndxTable;
516 }
517 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000518 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000519 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000520 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000521 void updateSymbols(function_ref<void(Symbol &)> Callable);
522
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000523 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000524 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000525 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000526 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000527 void accept(MutableSectionVisitor &Visitor) override;
Paul Semel4246a462018-05-09 21:36:54 +0000528 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000529
Petr Hosek79cee9e2017-08-29 02:12:03 +0000530 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000531 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000532 }
533};
534
Petr Hosekd7df9b22017-09-06 23:41:02 +0000535struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000536 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000537 uint64_t Offset;
538 uint64_t Addend;
539 uint32_t Type;
540};
541
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000542// All relocation sections denote relocations to apply to another section.
543// However, some relocation sections use a dynamic symbol table and others use
544// a regular symbol table. Because the types of the two symbol tables differ in
545// our system (because they should behave differently) we can't uniformly
546// represent all relocations with the same base class if we expose an interface
547// that mentions the symbol table type. So we split the two base types into two
548// different classes, one which handles the section the relocation is applied to
549// and another which handles the symbol table type. The symbol table type is
550// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
551class RelocationSectionBase : public SectionBase {
552protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000553 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000554
555public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000556 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000557 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000558
559 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000560 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000561 }
562};
563
564// Takes the symbol table type to use as a parameter so that we can deduplicate
565// that code between the two symbol table types.
566template <class SymTabType>
567class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000568 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000569 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000570
571protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000572 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000573
574public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000575 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000576 void initialize(SectionTableRef SecTable) override;
577 void finalize() override;
578};
579
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000580class RelocationSection
581 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000582 MAKE_SEC_WRITER_FRIEND
583
Petr Hosekd7df9b22017-09-06 23:41:02 +0000584 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000585
Petr Hosekd7df9b22017-09-06 23:41:02 +0000586public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000587 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000588 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000589 void accept(MutableSectionVisitor &Visitor) override;
Paul Semel4246a462018-05-09 21:36:54 +0000590 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000591 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000592
Petr Hosekd7df9b22017-09-06 23:41:02 +0000593 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000594 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000595 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000596 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000597 }
598};
599
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000600// TODO: The way stripping and groups interact is complicated
601// and still needs to be worked on.
602
603class GroupSection : public SectionBase {
604 MAKE_SEC_WRITER_FRIEND
605 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000606 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000607 ELF::Elf32_Word FlagWord;
608 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000609
610public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000611 // TODO: Contents is present in several classes of the hierarchy.
612 // This needs to be refactored to avoid duplication.
613 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000614
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000615 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
616
617 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000618 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000619 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
620 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
621
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000622 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000623 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000624 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000625 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000626 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000627
628 static bool classof(const SectionBase *S) {
629 return S->Type == ELF::SHT_GROUP;
630 }
631};
632
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000633class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000634public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000635 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000636
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000637 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000638 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000639 }
640};
641
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000642class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000643public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000644 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000645
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000646 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000647 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000648 }
649};
650
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000651class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000652 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000653 MAKE_SEC_WRITER_FRIEND
654
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000655private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000656 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000657
658public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000659 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000660
Jake Ehrlich76e91102018-01-25 22:46:17 +0000661 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000662 void accept(MutableSectionVisitor &Visitor) 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;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000684 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000685};
686
Jake Ehrlich76e91102018-01-25 22:46:17 +0000687class Reader {
688public:
689 virtual ~Reader();
690 virtual std::unique_ptr<Object> create() const = 0;
691};
692
Jake Ehrlich76e91102018-01-25 22:46:17 +0000693using object::Binary;
694using object::ELFFile;
695using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000696using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000697
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000698class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000699 uint16_t EMachine;
700 MemoryBuffer *MemBuf;
701 std::unique_ptr<Object> Obj;
702
703 void initFileHeader();
704 void initHeaderSegment();
705 StringTableSection *addStrTab();
706 SymbolTableSection *addSymTab(StringTableSection *StrTab);
707 void addData(SymbolTableSection *SymTab);
708 void initSections();
709
710public:
711 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
712 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
713
714 std::unique_ptr<Object> build();
715};
716
Jake Ehrlich76e91102018-01-25 22:46:17 +0000717template <class ELFT> class ELFBuilder {
718private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000719 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000720 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000721 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000722
723 const ELFFile<ELFT> &ElfFile;
724 Object &Obj;
725
Jake Ehrlich6452b112018-02-14 23:31:33 +0000726 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000727 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000728 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000729 void initSymbolTable(SymbolTableSection *SymTab);
730 void readSectionHeaders();
731 SectionBase &makeSection(const Elf_Shdr &Shdr);
732
733public:
734 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
735 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
736
737 void build();
738};
739
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000740class BinaryReader : public Reader {
741 const MachineInfo &MInfo;
742 MemoryBuffer *MemBuf;
743
744public:
745 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
746 : MInfo(MI), MemBuf(MB) {}
747 std::unique_ptr<Object> create() const override;
748};
749
Jake Ehrlich76e91102018-01-25 22:46:17 +0000750class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000751 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000752
753public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000754 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000755 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000756};
757
758class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000759private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000760 using SecPtr = std::unique_ptr<SectionBase>;
761 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000762
Petr Hosekc4df10e2017-08-04 21:09:26 +0000763 std::vector<SecPtr> Sections;
764 std::vector<SegPtr> Segments;
765
Petr Hosek05a04cb2017-08-01 00:33:58 +0000766public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000767 template <class T>
768 using Range = iterator_range<
769 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
770
771 template <class T>
772 using ConstRange = iterator_range<pointee_iterator<
773 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
774
Jake Ehrlich6452b112018-02-14 23:31:33 +0000775 // It is often the case that the ELF header and the program header table are
776 // not present in any segment. This could be a problem during file layout,
777 // because other segments may get assigned an offset where either of the
778 // two should reside, which will effectively corrupt the resulting binary.
779 // Other than that we use these segments to track program header offsets
780 // when they may not follow the ELF header.
781 Segment ElfHdrSegment;
782 Segment ProgramHdrSegment;
783
George Rimar4ded7732018-12-20 10:51:42 +0000784 uint8_t OSABI;
785 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000786 uint64_t Entry;
787 uint64_t SHOffset;
788 uint32_t Type;
789 uint32_t Machine;
790 uint32_t Version;
791 uint32_t Flags;
792
Jake Ehrlich76e91102018-01-25 22:46:17 +0000793 StringTableSection *SectionNames = nullptr;
794 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000795 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000796
Aaron Ballman09f46a72018-01-25 21:03:38 +0000797 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000798 SectionTableRef sections() { return SectionTableRef(Sections); }
799 ConstRange<SectionBase> sections() const {
800 return make_pointee_range(Sections);
801 }
802 Range<Segment> segments() { return make_pointee_range(Segments); }
803 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000804
Jake Ehrlich76e91102018-01-25 22:46:17 +0000805 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000806 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000807 template <class T, class... Ts> T &addSection(Ts &&... Args) {
808 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
809 auto Ptr = Sec.get();
810 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000811 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000812 return *Ptr;
813 }
814 Segment &addSegment(ArrayRef<uint8_t> Data) {
815 Segments.emplace_back(llvm::make_unique<Segment>(Data));
816 return *Segments.back();
817 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000818};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000819
820} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000821} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000822} // end namespace llvm
823
824#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H