blob: 9e2b64be9dc2b0235a376820aa8bbb3352905b84 [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();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000276 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000277 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000278 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000279 virtual void accept(MutableSectionVisitor &Visitor) = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000280 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000281};
282
283class Segment {
284private:
285 struct SectionCompare {
286 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
287 // Some sections might have the same address if one of them is empty. To
288 // fix this we can use the lexicographic ordering on ->Addr and the
289 // address of the actully stored section.
290 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
291 return Lhs < Rhs;
292 return Lhs->OriginalOffset < Rhs->OriginalOffset;
293 }
294 };
295
296 std::set<const SectionBase *, SectionCompare> Sections;
297
298public:
Fangrui Song967ce402018-12-12 22:46:37 +0000299 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000300 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000301 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000302 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000303 uint64_t PAddr;
304 uint64_t FileSize;
305 uint64_t MemSize;
306 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000307
Fangrui Song967ce402018-12-12 22:46:37 +0000308 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000309 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000310 Segment *ParentSegment = nullptr;
Jake Ehrlich8ad77792018-12-03 19:49:23 +0000311 ArrayRef<uint8_t> Contents;
Petr Hosek3f383832017-08-26 01:32:20 +0000312
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000313 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000314 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000315
Petr Hosek05a04cb2017-08-01 00:33:58 +0000316 const SectionBase *firstSection() const {
317 if (!Sections.empty())
318 return *Sections.begin();
319 return nullptr;
320 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000321
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000322 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
323 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000324};
325
326class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000327 MAKE_SEC_WRITER_FRIEND
328
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000329 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000330 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000331
332public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000333 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000334
Jake Ehrlich76e91102018-01-25 22:46:17 +0000335 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000336 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000337 void removeSectionReferences(const SectionBase *Sec) override;
338 void initialize(SectionTableRef SecTable) override;
339 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000340};
341
Jake Ehrliche8437de2017-12-19 00:47:30 +0000342class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000343 MAKE_SEC_WRITER_FRIEND
344
Jake Ehrliche8437de2017-12-19 00:47:30 +0000345 std::vector<uint8_t> Data;
346
347public:
348 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
349 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000350 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000351 Type = ELF::SHT_PROGBITS;
352 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000353 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000354 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000355
356 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000357 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000358};
359
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000360class CompressedSection : public SectionBase {
361 MAKE_SEC_WRITER_FRIEND
362
363 DebugCompressionType CompressionType;
364 uint64_t DecompressedSize;
365 uint64_t DecompressedAlign;
366 SmallVector<char, 128> CompressedData;
367
368public:
369 CompressedSection(const SectionBase &Sec,
370 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000371 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
372 uint64_t DecompressedAlign);
373
374 uint64_t getDecompressedSize() const { return DecompressedSize; }
375 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
376
377 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000378 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000379
380 static bool classof(const SectionBase *S) {
381 return (S->Flags & ELF::SHF_COMPRESSED) ||
382 (StringRef(S->Name).startswith(".zdebug"));
383 }
384};
385
386class DecompressedSection : public SectionBase {
387 MAKE_SEC_WRITER_FRIEND
388
389public:
390 explicit DecompressedSection(const CompressedSection &Sec)
391 : SectionBase(Sec) {
392 Size = Sec.getDecompressedSize();
393 Align = Sec.getDecompressedAlign();
394 Flags = (Flags & ~ELF::SHF_COMPRESSED);
395 if (StringRef(Name).startswith(".zdebug"))
396 Name = "." + Name.substr(2);
397 }
398
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000399 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000400 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000401};
402
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000403// There are two types of string tables that can exist, dynamic and not dynamic.
404// In the dynamic case the string table is allocated. Changing a dynamic string
405// table would mean altering virtual addresses and thus the memory image. So
406// dynamic string tables should not have an interface to modify them or
407// reconstruct them. This type lets us reconstruct a string table. To avoid
408// this class being used for dynamic string tables (which has happened) the
409// classof method checks that the particular instance is not allocated. This
410// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000411class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000412 MAKE_SEC_WRITER_FRIEND
413
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000414 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000415
416public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000417 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
418 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000419 }
420
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000421 void addString(StringRef Name);
422 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000423 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000424 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000425 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000426
Petr Hosek05a04cb2017-08-01 00:33:58 +0000427 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000428 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000429 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000430 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000431 }
432};
433
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000434// Symbols have a st_shndx field that normally stores an index but occasionally
435// stores a different special value. This enum keeps track of what the st_shndx
436// field means. Most of the values are just copies of the special SHN_* values.
437// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
438enum SymbolShndxType {
439 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000440 SYMBOL_ABS = ELF::SHN_ABS,
441 SYMBOL_COMMON = ELF::SHN_COMMON,
442 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
443 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
444 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
445 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000446 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000447};
448
Petr Hosek79cee9e2017-08-29 02:12:03 +0000449struct Symbol {
450 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000451 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000452 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000453 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000454 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000455 uint32_t NameIndex;
456 uint64_t Size;
457 uint8_t Type;
458 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000459 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000460 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000461
462 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000463 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000464};
465
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000466class SectionIndexSection : public SectionBase {
467 MAKE_SEC_WRITER_FRIEND
468
469private:
470 std::vector<uint32_t> Indexes;
471 SymbolTableSection *Symbols = nullptr;
472
473public:
474 virtual ~SectionIndexSection() {}
475 void addIndex(uint32_t Index) {
476 Indexes.push_back(Index);
477 Size += 4;
478 }
479 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
480 void initialize(SectionTableRef SecTable) override;
481 void finalize() override;
482 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000483 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000484
485 SectionIndexSection() {
486 Name = ".symtab_shndx";
487 Align = 4;
488 EntrySize = 4;
489 Type = ELF::SHT_SYMTAB_SHNDX;
490 }
491};
492
Petr Hosek79cee9e2017-08-29 02:12:03 +0000493class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000494 MAKE_SEC_WRITER_FRIEND
495
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000496 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000497 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000498
Petr Hosek79cee9e2017-08-29 02:12:03 +0000499protected:
500 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000501 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000502 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000503
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000504 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000505
Petr Hosek79cee9e2017-08-29 02:12:03 +0000506public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000507 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
508
509 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
510 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
511 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000512 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000513 // An 'empty' symbol table still contains a null symbol.
514 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000515 void setShndxTable(SectionIndexSection *ShndxTable) {
516 SectionIndexTable = ShndxTable;
517 }
518 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000519 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000520 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000521 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000522 void updateSymbols(function_ref<void(Symbol &)> Callable);
523
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000524 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000525 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000526 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000527 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000528 void accept(MutableSectionVisitor &Visitor) override;
Paul Semel4246a462018-05-09 21:36:54 +0000529 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000530
Petr Hosek79cee9e2017-08-29 02:12:03 +0000531 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000532 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000533 }
534};
535
Petr Hosekd7df9b22017-09-06 23:41:02 +0000536struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000537 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000538 uint64_t Offset;
539 uint64_t Addend;
540 uint32_t Type;
541};
542
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000543// All relocation sections denote relocations to apply to another section.
544// However, some relocation sections use a dynamic symbol table and others use
545// a regular symbol table. Because the types of the two symbol tables differ in
546// our system (because they should behave differently) we can't uniformly
547// represent all relocations with the same base class if we expose an interface
548// that mentions the symbol table type. So we split the two base types into two
549// different classes, one which handles the section the relocation is applied to
550// and another which handles the symbol table type. The symbol table type is
551// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
552class RelocationSectionBase : public SectionBase {
553protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000554 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000555
556public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000557 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000558 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000559
560 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000561 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000562 }
563};
564
565// Takes the symbol table type to use as a parameter so that we can deduplicate
566// that code between the two symbol table types.
567template <class SymTabType>
568class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000569 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000570 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000571
572protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000573 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000574
575public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000576 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000577 void initialize(SectionTableRef SecTable) override;
578 void finalize() override;
579};
580
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000581class RelocationSection
582 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000583 MAKE_SEC_WRITER_FRIEND
584
Petr Hosekd7df9b22017-09-06 23:41:02 +0000585 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000586
Petr Hosekd7df9b22017-09-06 23:41:02 +0000587public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000588 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000589 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000590 void accept(MutableSectionVisitor &Visitor) override;
Paul Semel4246a462018-05-09 21:36:54 +0000591 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000592 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000593
Petr Hosekd7df9b22017-09-06 23:41:02 +0000594 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000595 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000596 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000597 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000598 }
599};
600
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000601// TODO: The way stripping and groups interact is complicated
602// and still needs to be worked on.
603
604class GroupSection : public SectionBase {
605 MAKE_SEC_WRITER_FRIEND
606 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000607 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000608 ELF::Elf32_Word FlagWord;
609 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000610
611public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000612 // TODO: Contents is present in several classes of the hierarchy.
613 // This needs to be refactored to avoid duplication.
614 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000615
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000616 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
617
618 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000619 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000620 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
621 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
622
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000623 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000624 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000625 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000626 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000627 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000628
629 static bool classof(const SectionBase *S) {
630 return S->Type == ELF::SHT_GROUP;
631 }
632};
633
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000634class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000635public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000636 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000637
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000638 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000639 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000640 }
641};
642
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000643class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000644public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000645 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000646
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000647 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000648 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000649 }
650};
651
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000652class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000653 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000654 MAKE_SEC_WRITER_FRIEND
655
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000656private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000657 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000658
659public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000660 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000661
Jake Ehrlich76e91102018-01-25 22:46:17 +0000662 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000663 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000664
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000665 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000666 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000667 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000668 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000669 }
670};
671
Jake Ehrlich76e91102018-01-25 22:46:17 +0000672class GnuDebugLinkSection : public SectionBase {
673 MAKE_SEC_WRITER_FRIEND
674
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000675private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000676 StringRef FileName;
677 uint32_t CRC32;
678
679 void init(StringRef File, StringRef Data);
680
681public:
682 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000683 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000684 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000685 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000686};
687
Jake Ehrlich76e91102018-01-25 22:46:17 +0000688class Reader {
689public:
690 virtual ~Reader();
691 virtual std::unique_ptr<Object> create() const = 0;
692};
693
Jake Ehrlich76e91102018-01-25 22:46:17 +0000694using object::Binary;
695using object::ELFFile;
696using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000697using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000698
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000699class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000700 uint16_t EMachine;
701 MemoryBuffer *MemBuf;
702 std::unique_ptr<Object> Obj;
703
704 void initFileHeader();
705 void initHeaderSegment();
706 StringTableSection *addStrTab();
707 SymbolTableSection *addSymTab(StringTableSection *StrTab);
708 void addData(SymbolTableSection *SymTab);
709 void initSections();
710
711public:
712 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
713 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
714
715 std::unique_ptr<Object> build();
716};
717
Jake Ehrlich76e91102018-01-25 22:46:17 +0000718template <class ELFT> class ELFBuilder {
719private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000720 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000721 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000722 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000723
724 const ELFFile<ELFT> &ElfFile;
725 Object &Obj;
726
Jake Ehrlich6452b112018-02-14 23:31:33 +0000727 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000728 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000729 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000730 void initSymbolTable(SymbolTableSection *SymTab);
731 void readSectionHeaders();
732 SectionBase &makeSection(const Elf_Shdr &Shdr);
733
734public:
735 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
736 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
737
738 void build();
739};
740
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000741class BinaryReader : public Reader {
742 const MachineInfo &MInfo;
743 MemoryBuffer *MemBuf;
744
745public:
746 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
747 : MInfo(MI), MemBuf(MB) {}
748 std::unique_ptr<Object> create() const override;
749};
750
Jake Ehrlich76e91102018-01-25 22:46:17 +0000751class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000752 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000753
754public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000755 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000756 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000757};
758
759class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000760private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000761 using SecPtr = std::unique_ptr<SectionBase>;
762 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000763
Petr Hosekc4df10e2017-08-04 21:09:26 +0000764 std::vector<SecPtr> Sections;
765 std::vector<SegPtr> Segments;
766
Petr Hosek05a04cb2017-08-01 00:33:58 +0000767public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000768 template <class T>
769 using Range = iterator_range<
770 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
771
772 template <class T>
773 using ConstRange = iterator_range<pointee_iterator<
774 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
775
Jake Ehrlich6452b112018-02-14 23:31:33 +0000776 // It is often the case that the ELF header and the program header table are
777 // not present in any segment. This could be a problem during file layout,
778 // because other segments may get assigned an offset where either of the
779 // two should reside, which will effectively corrupt the resulting binary.
780 // Other than that we use these segments to track program header offsets
781 // when they may not follow the ELF header.
782 Segment ElfHdrSegment;
783 Segment ProgramHdrSegment;
784
George Rimar4ded7732018-12-20 10:51:42 +0000785 uint8_t OSABI;
786 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000787 uint64_t Entry;
788 uint64_t SHOffset;
789 uint32_t Type;
790 uint32_t Machine;
791 uint32_t Version;
792 uint32_t Flags;
793
Jake Ehrlich76e91102018-01-25 22:46:17 +0000794 StringTableSection *SectionNames = nullptr;
795 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000796 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000797
Aaron Ballman09f46a72018-01-25 21:03:38 +0000798 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000799 SectionTableRef sections() { return SectionTableRef(Sections); }
800 ConstRange<SectionBase> sections() const {
801 return make_pointee_range(Sections);
802 }
803 Range<Segment> segments() { return make_pointee_range(Segments); }
804 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000805
Jake Ehrlich76e91102018-01-25 22:46:17 +0000806 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000807 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000808 template <class T, class... Ts> T &addSection(Ts &&... Args) {
809 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
810 auto Ptr = Sec.get();
811 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000812 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000813 return *Ptr;
814 }
815 Segment &addSegment(ArrayRef<uint8_t> Data) {
816 Segments.emplace_back(llvm::make_unique<Segment>(Data));
817 return *Segments.back();
818 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000819};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000820
821} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000822} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000823} // end namespace llvm
824
825#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H