blob: e5730cd543ee6f2baa3a09c9ed50cb7aa0cbf5de [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;
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +0000153 using Elf_Word = typename ELFT::Word;
154 using Elf_Xword = typename ELFT::Xword;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000155
156public:
157 void visit(Section &Sec) override;
158 void visit(OwnedDataSection &Sec) override;
159 void visit(StringTableSection &Sec) override;
160 void visit(DynamicRelocationSection &Sec) override;
161 void visit(SymbolTableSection &Sec) override;
162 void visit(RelocationSection &Sec) override;
163 void visit(GnuDebugLinkSection &Sec) override;
164 void visit(GroupSection &Sec) override;
165 void visit(SectionIndexSection &Sec) override;
166 void visit(CompressedSection &Sec) override;
167 void visit(DecompressedSection &Sec) override;
168};
169
Jake Ehrlich76e91102018-01-25 22:46:17 +0000170#define MAKE_SEC_WRITER_FRIEND \
171 friend class SectionWriter; \
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000172 template <class ELFT> friend class ELFSectionWriter; \
173 template <class ELFT> friend class ELFSectionSizer;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000174
175class BinarySectionWriter : public SectionWriter {
176public:
177 virtual ~BinarySectionWriter() {}
178
179 void visit(const SymbolTableSection &Sec) override;
180 void visit(const RelocationSection &Sec) override;
181 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000182 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000183 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000184 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000185 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000186
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000187 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
188};
189
Jake Ehrlich76e91102018-01-25 22:46:17 +0000190class Writer {
191protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000192 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000193 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000194
195public:
196 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000197 virtual void finalize() = 0;
198 virtual void write() = 0;
199
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000200 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000201};
202
203template <class ELFT> class ELFWriter : public Writer {
204private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000205 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000206 using Elf_Shdr = typename ELFT::Shdr;
207 using Elf_Phdr = typename ELFT::Phdr;
208 using Elf_Ehdr = typename ELFT::Ehdr;
209
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000210 void initEhdrSegment();
211
Jake Ehrlich76e91102018-01-25 22:46:17 +0000212 void writeEhdr();
213 void writePhdr(const Segment &Seg);
214 void writeShdr(const SectionBase &Sec);
215
216 void writePhdrs();
217 void writeShdrs();
218 void writeSectionData();
219
220 void assignOffsets();
221
222 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
223
224 size_t totalSize() const;
225
226public:
227 virtual ~ELFWriter() {}
228 bool WriteSectionHeaders = true;
229
230 void finalize() override;
231 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000232 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
233 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000234};
235
236class BinaryWriter : public Writer {
237private:
238 std::unique_ptr<BinarySectionWriter> SecWriter;
239
240 uint64_t TotalSize;
241
242public:
243 ~BinaryWriter() {}
244 void finalize() override;
245 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000246 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000247};
248
Petr Hosek05a04cb2017-08-01 00:33:58 +0000249class SectionBase {
250public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000251 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000252 Segment *ParentSegment = nullptr;
253 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000254 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000255 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000256 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000257
258 uint64_t Addr = 0;
259 uint64_t Align = 1;
260 uint32_t EntrySize = 0;
261 uint64_t Flags = 0;
262 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000263 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000264 uint64_t NameIndex = 0;
265 uint64_t Offset = 0;
266 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000267 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000268 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000269
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000270 SectionBase() = default;
271 SectionBase(const SectionBase &) = default;
272
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000273 virtual ~SectionBase() = default;
274
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000275 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000276 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000277 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000278 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000279 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000280 virtual void accept(MutableSectionVisitor &Visitor) = 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;
298
299public:
Fangrui Song967ce402018-12-12 22:46:37 +0000300 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000301 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000302 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000303 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000304 uint64_t PAddr;
305 uint64_t FileSize;
306 uint64_t MemSize;
307 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000308
Fangrui Song967ce402018-12-12 22:46:37 +0000309 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000310 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000311 Segment *ParentSegment = nullptr;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000312 ArrayRef<uint8_t> Contents;
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;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000337 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000338 void removeSectionReferences(const SectionBase *Sec) override;
339 void initialize(SectionTableRef SecTable) override;
340 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000341};
342
Jake Ehrliche8437de2017-12-19 00:47:30 +0000343class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000344 MAKE_SEC_WRITER_FRIEND
345
Jake Ehrliche8437de2017-12-19 00:47:30 +0000346 std::vector<uint8_t> Data;
347
348public:
349 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
350 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000351 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000352 Type = ELF::SHT_PROGBITS;
353 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000354 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000355 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000356
357 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000358 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000359};
360
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000361class CompressedSection : public SectionBase {
362 MAKE_SEC_WRITER_FRIEND
363
364 DebugCompressionType CompressionType;
365 uint64_t DecompressedSize;
366 uint64_t DecompressedAlign;
367 SmallVector<char, 128> CompressedData;
368
369public:
370 CompressedSection(const SectionBase &Sec,
371 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000372 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
373 uint64_t DecompressedAlign);
374
375 uint64_t getDecompressedSize() const { return DecompressedSize; }
376 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
377
378 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000379 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000380
381 static bool classof(const SectionBase *S) {
382 return (S->Flags & ELF::SHF_COMPRESSED) ||
383 (StringRef(S->Name).startswith(".zdebug"));
384 }
385};
386
387class DecompressedSection : public SectionBase {
388 MAKE_SEC_WRITER_FRIEND
389
390public:
391 explicit DecompressedSection(const CompressedSection &Sec)
392 : SectionBase(Sec) {
393 Size = Sec.getDecompressedSize();
394 Align = Sec.getDecompressedAlign();
395 Flags = (Flags & ~ELF::SHF_COMPRESSED);
396 if (StringRef(Name).startswith(".zdebug"))
397 Name = "." + Name.substr(2);
398 }
399
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000400 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000401 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000402};
403
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000404// There are two types of string tables that can exist, dynamic and not dynamic.
405// In the dynamic case the string table is allocated. Changing a dynamic string
406// table would mean altering virtual addresses and thus the memory image. So
407// dynamic string tables should not have an interface to modify them or
408// reconstruct them. This type lets us reconstruct a string table. To avoid
409// this class being used for dynamic string tables (which has happened) the
410// classof method checks that the particular instance is not allocated. This
411// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000412class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000413 MAKE_SEC_WRITER_FRIEND
414
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000415 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000416
417public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000418 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
419 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000420 }
421
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000422 void addString(StringRef Name);
423 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000424 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000425 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000426 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000427
Petr Hosek05a04cb2017-08-01 00:33:58 +0000428 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000429 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000430 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000431 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000432 }
433};
434
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000435// Symbols have a st_shndx field that normally stores an index but occasionally
436// stores a different special value. This enum keeps track of what the st_shndx
437// field means. Most of the values are just copies of the special SHN_* values.
438// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
439enum SymbolShndxType {
440 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000441 SYMBOL_ABS = ELF::SHN_ABS,
442 SYMBOL_COMMON = ELF::SHN_COMMON,
443 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
444 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
445 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
446 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000447 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000448};
449
Petr Hosek79cee9e2017-08-29 02:12:03 +0000450struct Symbol {
451 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000452 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000453 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000454 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000455 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000456 uint32_t NameIndex;
457 uint64_t Size;
458 uint8_t Type;
459 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000460 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000461 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000462
463 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000464 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000465};
466
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000467class SectionIndexSection : public SectionBase {
468 MAKE_SEC_WRITER_FRIEND
469
470private:
471 std::vector<uint32_t> Indexes;
472 SymbolTableSection *Symbols = nullptr;
473
474public:
475 virtual ~SectionIndexSection() {}
476 void addIndex(uint32_t Index) {
477 Indexes.push_back(Index);
478 Size += 4;
479 }
480 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
481 void initialize(SectionTableRef SecTable) override;
482 void finalize() override;
483 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000484 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000485
486 SectionIndexSection() {
487 Name = ".symtab_shndx";
488 Align = 4;
489 EntrySize = 4;
490 Type = ELF::SHT_SYMTAB_SHNDX;
491 }
492};
493
Petr Hosek79cee9e2017-08-29 02:12:03 +0000494class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000495 MAKE_SEC_WRITER_FRIEND
496
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000497 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000498 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000499
Petr Hosek79cee9e2017-08-29 02:12:03 +0000500protected:
501 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000502 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000503 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000504
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000505 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000506
Petr Hosek79cee9e2017-08-29 02:12:03 +0000507public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000508 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
509
510 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
511 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
512 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000513 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000514 // An 'empty' symbol table still contains a null symbol.
515 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000516 void setShndxTable(SectionIndexSection *ShndxTable) {
517 SectionIndexTable = ShndxTable;
518 }
519 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000520 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000521 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000522 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000523 void updateSymbols(function_ref<void(Symbol &)> Callable);
524
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000525 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000526 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000527 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000528 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000529 void accept(MutableSectionVisitor &Visitor) 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;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000591 void accept(MutableSectionVisitor &Visitor) override;
Paul Semel4246a462018-05-09 21:36:54 +0000592 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000593 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000594
Petr Hosekd7df9b22017-09-06 23:41:02 +0000595 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000596 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000597 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000598 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000599 }
600};
601
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000602// TODO: The way stripping and groups interact is complicated
603// and still needs to be worked on.
604
605class GroupSection : public SectionBase {
606 MAKE_SEC_WRITER_FRIEND
607 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000608 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000609 ELF::Elf32_Word FlagWord;
610 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000611
612public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000613 // TODO: Contents is present in several classes of the hierarchy.
614 // This needs to be refactored to avoid duplication.
615 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000616
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000617 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
618
619 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000620 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000621 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
622 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
623
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000624 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000625 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000626 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000627 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000628 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000629
630 static bool classof(const SectionBase *S) {
631 return S->Type == ELF::SHT_GROUP;
632 }
633};
634
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000635class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000636public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000637 explicit DynamicSymbolTableSection(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_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000641 }
642};
643
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000644class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000645public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000646 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000647
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000648 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000649 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000650 }
651};
652
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000653class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000654 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000655 MAKE_SEC_WRITER_FRIEND
656
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000657private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000658 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000659
660public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000661 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000662
Jake Ehrlich76e91102018-01-25 22:46:17 +0000663 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000664 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000665
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000666 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000667 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000668 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000669 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000670 }
671};
672
Jake Ehrlich76e91102018-01-25 22:46:17 +0000673class GnuDebugLinkSection : public SectionBase {
674 MAKE_SEC_WRITER_FRIEND
675
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000676private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000677 StringRef FileName;
678 uint32_t CRC32;
679
680 void init(StringRef File, StringRef Data);
681
682public:
683 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000684 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000685 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000686 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000687};
688
Jake Ehrlich76e91102018-01-25 22:46:17 +0000689class Reader {
690public:
691 virtual ~Reader();
692 virtual std::unique_ptr<Object> create() const = 0;
693};
694
Jake Ehrlich76e91102018-01-25 22:46:17 +0000695using object::Binary;
696using object::ELFFile;
697using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000698using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000699
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000700class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000701 uint16_t EMachine;
702 MemoryBuffer *MemBuf;
703 std::unique_ptr<Object> Obj;
704
705 void initFileHeader();
706 void initHeaderSegment();
707 StringTableSection *addStrTab();
708 SymbolTableSection *addSymTab(StringTableSection *StrTab);
709 void addData(SymbolTableSection *SymTab);
710 void initSections();
711
712public:
713 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
714 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
715
716 std::unique_ptr<Object> build();
717};
718
Jake Ehrlich76e91102018-01-25 22:46:17 +0000719template <class ELFT> class ELFBuilder {
720private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000721 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000722 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000723 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000724
725 const ELFFile<ELFT> &ElfFile;
726 Object &Obj;
727
Jake Ehrlich6452b112018-02-14 23:31:33 +0000728 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000729 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000730 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000731 void initSymbolTable(SymbolTableSection *SymTab);
732 void readSectionHeaders();
733 SectionBase &makeSection(const Elf_Shdr &Shdr);
734
735public:
736 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
737 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
738
739 void build();
740};
741
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000742class BinaryReader : public Reader {
743 const MachineInfo &MInfo;
744 MemoryBuffer *MemBuf;
745
746public:
747 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
748 : MInfo(MI), MemBuf(MB) {}
749 std::unique_ptr<Object> create() const override;
750};
751
Jake Ehrlich76e91102018-01-25 22:46:17 +0000752class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000753 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000754
755public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000756 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000757 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000758};
759
760class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000761private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000762 using SecPtr = std::unique_ptr<SectionBase>;
763 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000764
Petr Hosekc4df10e2017-08-04 21:09:26 +0000765 std::vector<SecPtr> Sections;
766 std::vector<SegPtr> Segments;
767
Petr Hosek05a04cb2017-08-01 00:33:58 +0000768public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000769 template <class T>
770 using Range = iterator_range<
771 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
772
773 template <class T>
774 using ConstRange = iterator_range<pointee_iterator<
775 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
776
Jake Ehrlich6452b112018-02-14 23:31:33 +0000777 // It is often the case that the ELF header and the program header table are
778 // not present in any segment. This could be a problem during file layout,
779 // because other segments may get assigned an offset where either of the
780 // two should reside, which will effectively corrupt the resulting binary.
781 // Other than that we use these segments to track program header offsets
782 // when they may not follow the ELF header.
783 Segment ElfHdrSegment;
784 Segment ProgramHdrSegment;
785
George Rimar4ded7732018-12-20 10:51:42 +0000786 uint8_t OSABI;
787 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000788 uint64_t Entry;
789 uint64_t SHOffset;
790 uint32_t Type;
791 uint32_t Machine;
792 uint32_t Version;
793 uint32_t Flags;
794
Jake Ehrlich76e91102018-01-25 22:46:17 +0000795 StringTableSection *SectionNames = nullptr;
796 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000797 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000798
Aaron Ballman09f46a72018-01-25 21:03:38 +0000799 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000800 SectionTableRef sections() { return SectionTableRef(Sections); }
801 ConstRange<SectionBase> sections() const {
802 return make_pointee_range(Sections);
803 }
804 Range<Segment> segments() { return make_pointee_range(Segments); }
805 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000806
Jake Ehrlich76e91102018-01-25 22:46:17 +0000807 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000808 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000809 template <class T, class... Ts> T &addSection(Ts &&... Args) {
810 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
811 auto Ptr = Sec.get();
812 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000813 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000814 return *Ptr;
815 }
816 Segment &addSegment(ArrayRef<uint8_t> Data) {
817 Segments.emplace_back(llvm::make_unique<Segment>(Data));
818 return *Segments.back();
819 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000820};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000821
822} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000823} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000824} // end namespace llvm
825
826#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H