blob: ec400b5ba6010732d7d1f521f5cc82337f3db061 [file] [log] [blame]
Petr Hosek05a04cb2017-08-01 00:33:58 +00001//===- Object.h -------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Petr Hosek05a04cb2017-08-01 00:33:58 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00009#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
10#define LLVM_TOOLS_OBJCOPY_OBJECT_H
Petr Hosek05a04cb2017-08-01 00:33:58 +000011
Alexander Shaposhnikov3d4c4ac2018-10-16 05:40:18 +000012#include "Buffer.h"
Alexander Shaposhnikov8d0b74c2018-10-11 22:33:50 +000013#include "CopyConfig.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000014#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000018#include "llvm/MC/StringTableBuilder.h"
19#include "llvm/Object/ELFObjectFile.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000020#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000021#include "llvm/Support/JamCRC.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000022#include <cstddef>
23#include <cstdint>
24#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000025#include <memory>
26#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000027#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000028
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000029namespace llvm {
Puyan Lotfi99124cc2018-09-07 08:10:22 +000030enum class DebugCompressionType;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000031namespace objcopy {
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +000032namespace elf {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000033
Jake Ehrlichf5a43772017-09-25 20:37:28 +000034class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000035class Section;
36class OwnedDataSection;
37class StringTableSection;
38class SymbolTableSection;
39class RelocationSection;
40class DynamicRelocationSection;
41class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000042class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000043class SectionIndexSection;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000044class CompressedSection;
Puyan Lotfiaf048642018-10-01 10:29:41 +000045class DecompressedSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000046class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000047class Object;
Paul Semel4246a462018-05-09 21:36:54 +000048struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000049
50class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000051 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000052
53public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000054 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
55
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000056 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000057 : Sections(Secs) {}
58 SectionTableRef(const SectionTableRef &) = default;
59
Jake Ehrlich76e91102018-01-25 22:46:17 +000060 iterator begin() { return iterator(Sections.data()); }
61 iterator end() { return iterator(Sections.data() + Sections.size()); }
62
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000063 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000064
65 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000066 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000067};
Petr Hosek05a04cb2017-08-01 00:33:58 +000068
Jake Ehrlich76e91102018-01-25 22:46:17 +000069enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
70
71class SectionVisitor {
72public:
Jordan Rupprecht1f821762019-01-03 17:45:30 +000073 virtual ~SectionVisitor() = default;
Jake Ehrlich76e91102018-01-25 22:46:17 +000074
75 virtual void visit(const Section &Sec) = 0;
76 virtual void visit(const OwnedDataSection &Sec) = 0;
77 virtual void visit(const StringTableSection &Sec) = 0;
78 virtual void visit(const SymbolTableSection &Sec) = 0;
79 virtual void visit(const RelocationSection &Sec) = 0;
80 virtual void visit(const DynamicRelocationSection &Sec) = 0;
81 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000082 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000083 virtual void visit(const SectionIndexSection &Sec) = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000084 virtual void visit(const CompressedSection &Sec) = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +000085 virtual void visit(const DecompressedSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000086};
87
Jordan Rupprecht1f821762019-01-03 17:45:30 +000088class MutableSectionVisitor {
89public:
90 virtual ~MutableSectionVisitor() = default;
91
92 virtual void visit(Section &Sec) = 0;
93 virtual void visit(OwnedDataSection &Sec) = 0;
94 virtual void visit(StringTableSection &Sec) = 0;
95 virtual void visit(SymbolTableSection &Sec) = 0;
96 virtual void visit(RelocationSection &Sec) = 0;
97 virtual void visit(DynamicRelocationSection &Sec) = 0;
98 virtual void visit(GnuDebugLinkSection &Sec) = 0;
99 virtual void visit(GroupSection &Sec) = 0;
100 virtual void visit(SectionIndexSection &Sec) = 0;
101 virtual void visit(CompressedSection &Sec) = 0;
102 virtual void visit(DecompressedSection &Sec) = 0;
103};
104
Jake Ehrlich76e91102018-01-25 22:46:17 +0000105class SectionWriter : public SectionVisitor {
106protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000107 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000108
109public:
110 virtual ~SectionWriter(){};
111
112 void visit(const Section &Sec) override;
113 void visit(const OwnedDataSection &Sec) override;
114 void visit(const StringTableSection &Sec) override;
115 void visit(const DynamicRelocationSection &Sec) override;
116 virtual void visit(const SymbolTableSection &Sec) override = 0;
117 virtual void visit(const RelocationSection &Sec) override = 0;
118 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000119 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000120 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000121 virtual void visit(const CompressedSection &Sec) override = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000122 virtual void visit(const DecompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000123
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000124 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000125};
126
127template <class ELFT> class ELFSectionWriter : public SectionWriter {
128private:
129 using Elf_Word = typename ELFT::Word;
130 using Elf_Rel = typename ELFT::Rel;
131 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000132 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000133
134public:
135 virtual ~ELFSectionWriter() {}
136 void visit(const SymbolTableSection &Sec) override;
137 void visit(const RelocationSection &Sec) override;
138 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000139 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000140 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000141 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000142 void visit(const DecompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000143
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000144 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000145};
146
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000147template <class ELFT> class ELFSectionSizer : public MutableSectionVisitor {
148private:
149 using Elf_Rel = typename ELFT::Rel;
150 using Elf_Rela = typename ELFT::Rela;
151 using Elf_Sym = typename ELFT::Sym;
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +0000152 using Elf_Word = typename ELFT::Word;
153 using Elf_Xword = typename ELFT::Xword;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000154
155public:
156 void visit(Section &Sec) override;
157 void visit(OwnedDataSection &Sec) override;
158 void visit(StringTableSection &Sec) override;
159 void visit(DynamicRelocationSection &Sec) override;
160 void visit(SymbolTableSection &Sec) override;
161 void visit(RelocationSection &Sec) override;
162 void visit(GnuDebugLinkSection &Sec) override;
163 void visit(GroupSection &Sec) override;
164 void visit(SectionIndexSection &Sec) override;
165 void visit(CompressedSection &Sec) override;
166 void visit(DecompressedSection &Sec) override;
167};
168
Jake Ehrlich76e91102018-01-25 22:46:17 +0000169#define MAKE_SEC_WRITER_FRIEND \
170 friend class SectionWriter; \
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000171 template <class ELFT> friend class ELFSectionWriter; \
172 template <class ELFT> friend class ELFSectionSizer;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000173
174class BinarySectionWriter : public SectionWriter {
175public:
176 virtual ~BinarySectionWriter() {}
177
178 void visit(const SymbolTableSection &Sec) override;
179 void visit(const RelocationSection &Sec) override;
180 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000181 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000182 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000183 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000184 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000185
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000186 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
187};
188
Jake Ehrlich76e91102018-01-25 22:46:17 +0000189class Writer {
190protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000191 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000192 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000193
194public:
195 virtual ~Writer();
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000196 virtual Error finalize() = 0;
197 virtual Error write() = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000198
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000199 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000200};
201
202template <class ELFT> class ELFWriter : public Writer {
203private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000204 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000205 using Elf_Shdr = typename ELFT::Shdr;
206 using Elf_Phdr = typename ELFT::Phdr;
207 using Elf_Ehdr = typename ELFT::Ehdr;
208
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000209 void initEhdrSegment();
210
Jake Ehrlich76e91102018-01-25 22:46:17 +0000211 void writeEhdr();
212 void writePhdr(const Segment &Seg);
213 void writeShdr(const SectionBase &Sec);
214
215 void writePhdrs();
216 void writeShdrs();
217 void writeSectionData();
218
219 void assignOffsets();
220
221 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
222
223 size_t totalSize() const;
224
225public:
226 virtual ~ELFWriter() {}
227 bool WriteSectionHeaders = true;
228
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000229 Error finalize() override;
230 Error write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000231 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
232 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000233};
234
235class BinaryWriter : public Writer {
236private:
237 std::unique_ptr<BinarySectionWriter> SecWriter;
238
239 uint64_t TotalSize;
240
241public:
242 ~BinaryWriter() {}
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000243 Error finalize() override;
244 Error write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000245 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000246};
247
Petr Hosek05a04cb2017-08-01 00:33:58 +0000248class SectionBase {
249public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000250 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000251 Segment *ParentSegment = nullptr;
252 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000253 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000254 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000255 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000256
257 uint64_t Addr = 0;
258 uint64_t Align = 1;
259 uint32_t EntrySize = 0;
260 uint64_t Flags = 0;
261 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000262 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000263 uint64_t NameIndex = 0;
264 uint64_t Offset = 0;
265 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000266 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000267 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000268
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000269 SectionBase() = default;
270 SectionBase(const SectionBase &) = default;
271
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000272 virtual ~SectionBase() = default;
273
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000274 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000275 virtual void finalize();
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000276 // Remove references to these sections. The list of sections must be sorted.
277 virtual Error
278 removeSectionReferences(function_ref<bool(const SectionBase *)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000279 virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000280 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000281 virtual void accept(MutableSectionVisitor &Visitor) = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000282 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000283};
284
285class Segment {
286private:
287 struct SectionCompare {
288 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
289 // Some sections might have the same address if one of them is empty. To
290 // fix this we can use the lexicographic ordering on ->Addr and the
291 // address of the actully stored section.
292 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
293 return Lhs < Rhs;
294 return Lhs->OriginalOffset < Rhs->OriginalOffset;
295 }
296 };
297
298 std::set<const SectionBase *, SectionCompare> Sections;
299
300public:
Fangrui Song967ce402018-12-12 22:46:37 +0000301 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000302 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000303 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000304 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000305 uint64_t PAddr;
306 uint64_t FileSize;
307 uint64_t MemSize;
308 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000309
Fangrui Song967ce402018-12-12 22:46:37 +0000310 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000311 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000312 Segment *ParentSegment = nullptr;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000313 ArrayRef<uint8_t> Contents;
Petr Hosek3f383832017-08-26 01:32:20 +0000314
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000315 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000316 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000317
Petr Hosek05a04cb2017-08-01 00:33:58 +0000318 const SectionBase *firstSection() const {
319 if (!Sections.empty())
320 return *Sections.begin();
321 return nullptr;
322 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000323
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000324 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
325 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000326};
327
328class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000329 MAKE_SEC_WRITER_FRIEND
330
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000331 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000332 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000333
334public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000335 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000336
Jake Ehrlich76e91102018-01-25 22:46:17 +0000337 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000338 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000339 Error removeSectionReferences(
340 function_ref<bool(const SectionBase *)> ToRemove) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000341 void initialize(SectionTableRef SecTable) override;
342 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000343};
344
Jake Ehrliche8437de2017-12-19 00:47:30 +0000345class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000346 MAKE_SEC_WRITER_FRIEND
347
Jake Ehrliche8437de2017-12-19 00:47:30 +0000348 std::vector<uint8_t> Data;
349
350public:
351 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
352 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000353 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000354 Type = ELF::SHT_PROGBITS;
355 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000356 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000357 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000358
359 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000360 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000361};
362
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000363class CompressedSection : public SectionBase {
364 MAKE_SEC_WRITER_FRIEND
365
366 DebugCompressionType CompressionType;
367 uint64_t DecompressedSize;
368 uint64_t DecompressedAlign;
369 SmallVector<char, 128> CompressedData;
370
371public:
372 CompressedSection(const SectionBase &Sec,
373 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000374 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
375 uint64_t DecompressedAlign);
376
377 uint64_t getDecompressedSize() const { return DecompressedSize; }
378 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
379
380 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000381 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000382
383 static bool classof(const SectionBase *S) {
384 return (S->Flags & ELF::SHF_COMPRESSED) ||
385 (StringRef(S->Name).startswith(".zdebug"));
386 }
387};
388
389class DecompressedSection : public SectionBase {
390 MAKE_SEC_WRITER_FRIEND
391
392public:
393 explicit DecompressedSection(const CompressedSection &Sec)
394 : SectionBase(Sec) {
395 Size = Sec.getDecompressedSize();
396 Align = Sec.getDecompressedAlign();
397 Flags = (Flags & ~ELF::SHF_COMPRESSED);
398 if (StringRef(Name).startswith(".zdebug"))
399 Name = "." + Name.substr(2);
400 }
401
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000402 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000403 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000404};
405
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000406// There are two types of string tables that can exist, dynamic and not dynamic.
407// In the dynamic case the string table is allocated. Changing a dynamic string
408// table would mean altering virtual addresses and thus the memory image. So
409// dynamic string tables should not have an interface to modify them or
410// reconstruct them. This type lets us reconstruct a string table. To avoid
411// this class being used for dynamic string tables (which has happened) the
412// classof method checks that the particular instance is not allocated. This
413// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000414class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000415 MAKE_SEC_WRITER_FRIEND
416
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000417 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000418
419public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000420 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
421 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000422 }
423
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000424 void addString(StringRef Name);
425 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000426 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000427 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000428 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000429
Petr Hosek05a04cb2017-08-01 00:33:58 +0000430 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000431 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000432 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000433 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000434 }
435};
436
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000437// Symbols have a st_shndx field that normally stores an index but occasionally
438// stores a different special value. This enum keeps track of what the st_shndx
439// field means. Most of the values are just copies of the special SHN_* values.
440// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
441enum SymbolShndxType {
442 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000443 SYMBOL_ABS = ELF::SHN_ABS,
444 SYMBOL_COMMON = ELF::SHN_COMMON,
445 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
446 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
447 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
448 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000449 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000450};
451
Petr Hosek79cee9e2017-08-29 02:12:03 +0000452struct Symbol {
453 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000454 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000455 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000456 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000457 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000458 uint32_t NameIndex;
459 uint64_t Size;
460 uint8_t Type;
461 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000462 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000463 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000464
465 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000466 bool isCommon() 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;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000486 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000487
488 SectionIndexSection() {
489 Name = ".symtab_shndx";
490 Align = 4;
491 EntrySize = 4;
492 Type = ELF::SHT_SYMTAB_SHNDX;
493 }
494};
495
Petr Hosek79cee9e2017-08-29 02:12:03 +0000496class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000497 MAKE_SEC_WRITER_FRIEND
498
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000499 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000500 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000501
Petr Hosek79cee9e2017-08-29 02:12:03 +0000502protected:
503 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000504 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000505 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000506
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000507 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000508
Petr Hosek79cee9e2017-08-29 02:12:03 +0000509public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000510 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
511
512 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
513 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
514 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000515 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000516 // An 'empty' symbol table still contains a null symbol.
517 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000518 void setShndxTable(SectionIndexSection *ShndxTable) {
519 SectionIndexTable = ShndxTable;
520 }
521 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000522 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000523 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000524 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000525 void updateSymbols(function_ref<void(Symbol &)> Callable);
526
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000527 Error removeSectionReferences(
528 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000529 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000530 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000531 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000532 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000533 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000534
Petr Hosek79cee9e2017-08-29 02:12:03 +0000535 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000536 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000537 }
538};
539
Petr Hosekd7df9b22017-09-06 23:41:02 +0000540struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000541 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000542 uint64_t Offset;
543 uint64_t Addend;
544 uint32_t Type;
545};
546
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000547// All relocation sections denote relocations to apply to another section.
548// However, some relocation sections use a dynamic symbol table and others use
549// a regular symbol table. Because the types of the two symbol tables differ in
550// our system (because they should behave differently) we can't uniformly
551// represent all relocations with the same base class if we expose an interface
552// that mentions the symbol table type. So we split the two base types into two
553// different classes, one which handles the section the relocation is applied to
554// and another which handles the symbol table type. The symbol table type is
555// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
556class RelocationSectionBase : public SectionBase {
557protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000558 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000559
560public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000561 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000562 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000563
564 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000565 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000566 }
567};
568
569// Takes the symbol table type to use as a parameter so that we can deduplicate
570// that code between the two symbol table types.
571template <class SymTabType>
572class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000573 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000574 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000575
576protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000577 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000578
579public:
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000580 Error removeSectionReferences(
581 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000582 void initialize(SectionTableRef SecTable) override;
583 void finalize() override;
584};
585
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000586class RelocationSection
587 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000588 MAKE_SEC_WRITER_FRIEND
589
Petr Hosekd7df9b22017-09-06 23:41:02 +0000590 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000591
Petr Hosekd7df9b22017-09-06 23:41:02 +0000592public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000593 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000594 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000595 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000596 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000597 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000598
Petr Hosekd7df9b22017-09-06 23:41:02 +0000599 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000600 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000601 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000602 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000603 }
604};
605
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000606// TODO: The way stripping and groups interact is complicated
607// and still needs to be worked on.
608
609class GroupSection : public SectionBase {
610 MAKE_SEC_WRITER_FRIEND
611 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000612 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000613 ELF::Elf32_Word FlagWord;
614 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000615
616public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000617 // TODO: Contents is present in several classes of the hierarchy.
618 // This needs to be refactored to avoid duplication.
619 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000620
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000621 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
622
623 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000624 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000625 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
626 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
627
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000628 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000629 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000630 void finalize() override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000631 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000632 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000633
634 static bool classof(const SectionBase *S) {
635 return S->Type == ELF::SHT_GROUP;
636 }
637};
638
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000639class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000640public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000641 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000642
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000643 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000644 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000645 }
646};
647
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000648class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000649public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000650 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000651
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000652 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000653 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000654 }
655};
656
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000657class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000658 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000659 MAKE_SEC_WRITER_FRIEND
660
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000661private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000662 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000663
664public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000665 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000666
Jake Ehrlich76e91102018-01-25 22:46:17 +0000667 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000668 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000669
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000670 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000671 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000672 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000673 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000674 }
675};
676
Jake Ehrlich76e91102018-01-25 22:46:17 +0000677class GnuDebugLinkSection : public SectionBase {
678 MAKE_SEC_WRITER_FRIEND
679
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000680private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000681 StringRef FileName;
682 uint32_t CRC32;
683
684 void init(StringRef File, StringRef Data);
685
686public:
687 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000688 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000689 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000690 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000691};
692
Jake Ehrlich76e91102018-01-25 22:46:17 +0000693class Reader {
694public:
695 virtual ~Reader();
696 virtual std::unique_ptr<Object> create() const = 0;
697};
698
Jake Ehrlich76e91102018-01-25 22:46:17 +0000699using object::Binary;
700using object::ELFFile;
701using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000702using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000703
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000704class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000705 uint16_t EMachine;
706 MemoryBuffer *MemBuf;
707 std::unique_ptr<Object> Obj;
708
709 void initFileHeader();
710 void initHeaderSegment();
711 StringTableSection *addStrTab();
712 SymbolTableSection *addSymTab(StringTableSection *StrTab);
713 void addData(SymbolTableSection *SymTab);
714 void initSections();
715
716public:
717 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
718 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
719
720 std::unique_ptr<Object> build();
721};
722
Jake Ehrlich76e91102018-01-25 22:46:17 +0000723template <class ELFT> class ELFBuilder {
724private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000725 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000726 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000727 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000728
729 const ELFFile<ELFT> &ElfFile;
730 Object &Obj;
731
Jake Ehrlich6452b112018-02-14 23:31:33 +0000732 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000733 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000734 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000735 void initSymbolTable(SymbolTableSection *SymTab);
736 void readSectionHeaders();
737 SectionBase &makeSection(const Elf_Shdr &Shdr);
738
739public:
740 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
741 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
742
743 void build();
744};
745
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000746class BinaryReader : public Reader {
747 const MachineInfo &MInfo;
748 MemoryBuffer *MemBuf;
749
750public:
751 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
752 : MInfo(MI), MemBuf(MB) {}
753 std::unique_ptr<Object> create() const override;
754};
755
Jake Ehrlich76e91102018-01-25 22:46:17 +0000756class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000757 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000758
759public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000760 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000761 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000762};
763
764class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000765private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000766 using SecPtr = std::unique_ptr<SectionBase>;
767 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000768
Petr Hosekc4df10e2017-08-04 21:09:26 +0000769 std::vector<SecPtr> Sections;
770 std::vector<SegPtr> Segments;
771
Petr Hosek05a04cb2017-08-01 00:33:58 +0000772public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000773 template <class T>
774 using Range = iterator_range<
775 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
776
777 template <class T>
778 using ConstRange = iterator_range<pointee_iterator<
779 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
780
Jake Ehrlich6452b112018-02-14 23:31:33 +0000781 // It is often the case that the ELF header and the program header table are
782 // not present in any segment. This could be a problem during file layout,
783 // because other segments may get assigned an offset where either of the
784 // two should reside, which will effectively corrupt the resulting binary.
785 // Other than that we use these segments to track program header offsets
786 // when they may not follow the ELF header.
787 Segment ElfHdrSegment;
788 Segment ProgramHdrSegment;
789
George Rimar4ded7732018-12-20 10:51:42 +0000790 uint8_t OSABI;
791 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000792 uint64_t Entry;
793 uint64_t SHOffset;
794 uint32_t Type;
795 uint32_t Machine;
796 uint32_t Version;
797 uint32_t Flags;
798
Jake Ehrlich76e91102018-01-25 22:46:17 +0000799 StringTableSection *SectionNames = nullptr;
800 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000801 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000802
Aaron Ballman09f46a72018-01-25 21:03:38 +0000803 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000804 SectionTableRef sections() { return SectionTableRef(Sections); }
805 ConstRange<SectionBase> sections() const {
806 return make_pointee_range(Sections);
807 }
808 Range<Segment> segments() { return make_pointee_range(Segments); }
809 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000810
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000811 Error removeSections(std::function<bool(const SectionBase &)> ToRemove);
812 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000813 template <class T, class... Ts> T &addSection(Ts &&... Args) {
814 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
815 auto Ptr = Sec.get();
816 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000817 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000818 return *Ptr;
819 }
820 Segment &addSegment(ArrayRef<uint8_t> Data) {
821 Segments.emplace_back(llvm::make_unique<Segment>(Data));
822 return *Segments.back();
823 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000824};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000825
826} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000827} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000828} // end namespace llvm
829
830#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H