blob: e892d066a6cd19e7b44ea5ff63ba10432ea58b5c [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:
Fangrui Songa85bf872019-03-15 10:20:51 +0000110 virtual ~SectionWriter() = default;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000111
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();
George Rimard8a5c6c2019-03-11 11:01:24 +0000283 virtual void
284 replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000285};
286
287class Segment {
288private:
289 struct SectionCompare {
290 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
291 // Some sections might have the same address if one of them is empty. To
292 // fix this we can use the lexicographic ordering on ->Addr and the
293 // address of the actully stored section.
294 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
295 return Lhs < Rhs;
296 return Lhs->OriginalOffset < Rhs->OriginalOffset;
297 }
298 };
299
300 std::set<const SectionBase *, SectionCompare> Sections;
301
302public:
Fangrui Song967ce402018-12-12 22:46:37 +0000303 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000304 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000305 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000307 uint64_t PAddr;
308 uint64_t FileSize;
309 uint64_t MemSize;
310 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000311
Fangrui Song967ce402018-12-12 22:46:37 +0000312 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000313 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000314 Segment *ParentSegment = nullptr;
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;
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000337 Error removeSectionReferences(
338 function_ref<bool(const SectionBase *)> ToRemove) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000339 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;
George Rimarfaf308b2019-03-18 14:27:41 +0000424 void prepareForLayout();
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
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000525 Error removeSectionReferences(
526 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000527 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000528 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000529 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000530 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000531 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
George Rimar0373bed2019-03-20 13:57:47 +0000532 void replaceSectionReferences(
533 const DenseMap<SectionBase *, SectionBase *> &FromTo) 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 {
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000573 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000574
575protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000576 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000577
George Rimar79fb8582019-02-27 11:18:27 +0000578 SymTabType *Symbols = nullptr;
579
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000580public:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000581 void initialize(SectionTableRef SecTable) override;
582 void finalize() override;
583};
584
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000585class RelocationSection
586 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000587 MAKE_SEC_WRITER_FRIEND
588
Petr Hosekd7df9b22017-09-06 23:41:02 +0000589 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000590
Petr Hosekd7df9b22017-09-06 23:41:02 +0000591public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000592 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000593 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000594 void accept(MutableSectionVisitor &Visitor) override;
George Rimar79fb8582019-02-27 11:18:27 +0000595 Error removeSectionReferences(
596 function_ref<bool(const SectionBase *)> ToRemove) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000597 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000598 void markSymbols() override;
George Rimard8a5c6c2019-03-11 11:01:24 +0000599 void replaceSectionReferences(
600 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000601
Petr Hosekd7df9b22017-09-06 23:41:02 +0000602 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000603 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000604 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000605 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000606 }
607};
608
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000609// TODO: The way stripping and groups interact is complicated
610// and still needs to be worked on.
611
612class GroupSection : public SectionBase {
613 MAKE_SEC_WRITER_FRIEND
614 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000615 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000616 ELF::Elf32_Word FlagWord;
617 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000618
619public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000620 // TODO: Contents is present in several classes of the hierarchy.
621 // This needs to be refactored to avoid duplication.
622 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000623
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000624 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
625
626 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000627 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000628 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
629 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
630
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000631 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000632 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000633 void finalize() override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000634 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000635 void markSymbols() override;
George Rimar27257172019-03-24 14:41:45 +0000636 void replaceSectionReferences(
637 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000638
639 static bool classof(const SectionBase *S) {
640 return S->Type == ELF::SHT_GROUP;
641 }
642};
643
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000644class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000645public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000646 explicit DynamicSymbolTableSection(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_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000650 }
651};
652
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000653class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000654public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000655 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000656
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000657 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000658 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000659 }
660};
661
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000662class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000663 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000664 MAKE_SEC_WRITER_FRIEND
665
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000666private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000667 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000668
669public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000670 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000671
Jake Ehrlich76e91102018-01-25 22:46:17 +0000672 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000673 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000674
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000675 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000676 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000677 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000678 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000679 }
680};
681
Jake Ehrlich76e91102018-01-25 22:46:17 +0000682class GnuDebugLinkSection : public SectionBase {
683 MAKE_SEC_WRITER_FRIEND
684
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000685private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000686 StringRef FileName;
687 uint32_t CRC32;
688
689 void init(StringRef File, StringRef Data);
690
691public:
692 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000693 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000694 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000695 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000696};
697
Jake Ehrlich76e91102018-01-25 22:46:17 +0000698class Reader {
699public:
700 virtual ~Reader();
701 virtual std::unique_ptr<Object> create() const = 0;
702};
703
Jake Ehrlich76e91102018-01-25 22:46:17 +0000704using object::Binary;
705using object::ELFFile;
706using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000707using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000708
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000709class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000710 uint16_t EMachine;
711 MemoryBuffer *MemBuf;
712 std::unique_ptr<Object> Obj;
713
714 void initFileHeader();
715 void initHeaderSegment();
716 StringTableSection *addStrTab();
717 SymbolTableSection *addSymTab(StringTableSection *StrTab);
718 void addData(SymbolTableSection *SymTab);
719 void initSections();
720
721public:
722 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
723 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
724
725 std::unique_ptr<Object> build();
726};
727
Jake Ehrlich76e91102018-01-25 22:46:17 +0000728template <class ELFT> class ELFBuilder {
729private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000730 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000731 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000732 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000733
734 const ELFFile<ELFT> &ElfFile;
735 Object &Obj;
736
Jake Ehrlich6452b112018-02-14 23:31:33 +0000737 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000738 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000739 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000740 void initSymbolTable(SymbolTableSection *SymTab);
741 void readSectionHeaders();
742 SectionBase &makeSection(const Elf_Shdr &Shdr);
743
744public:
745 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
746 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
747
748 void build();
749};
750
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000751class BinaryReader : public Reader {
752 const MachineInfo &MInfo;
753 MemoryBuffer *MemBuf;
754
755public:
756 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
757 : MInfo(MI), MemBuf(MB) {}
758 std::unique_ptr<Object> create() const override;
759};
760
Jake Ehrlich76e91102018-01-25 22:46:17 +0000761class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000762 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000763
764public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000765 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000766 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000767};
768
769class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000770private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000771 using SecPtr = std::unique_ptr<SectionBase>;
772 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000773
Petr Hosekc4df10e2017-08-04 21:09:26 +0000774 std::vector<SecPtr> Sections;
775 std::vector<SegPtr> Segments;
776
Petr Hosek05a04cb2017-08-01 00:33:58 +0000777public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000778 template <class T>
779 using Range = iterator_range<
780 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
781
782 template <class T>
783 using ConstRange = iterator_range<pointee_iterator<
784 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
785
Jake Ehrlich6452b112018-02-14 23:31:33 +0000786 // It is often the case that the ELF header and the program header table are
787 // not present in any segment. This could be a problem during file layout,
788 // because other segments may get assigned an offset where either of the
789 // two should reside, which will effectively corrupt the resulting binary.
790 // Other than that we use these segments to track program header offsets
791 // when they may not follow the ELF header.
792 Segment ElfHdrSegment;
793 Segment ProgramHdrSegment;
794
George Rimar4ded7732018-12-20 10:51:42 +0000795 uint8_t OSABI;
796 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000797 uint64_t Entry;
798 uint64_t SHOffset;
799 uint32_t Type;
800 uint32_t Machine;
801 uint32_t Version;
802 uint32_t Flags;
803
Jake Ehrlich76e91102018-01-25 22:46:17 +0000804 StringTableSection *SectionNames = nullptr;
805 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000806 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000807
Aaron Ballman09f46a72018-01-25 21:03:38 +0000808 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000809 SectionTableRef sections() { return SectionTableRef(Sections); }
810 ConstRange<SectionBase> sections() const {
811 return make_pointee_range(Sections);
812 }
Eugene Leviant51c1f642019-02-25 14:12:41 +0000813 SectionBase *findSection(StringRef Name) {
814 auto SecIt =
815 find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; });
816 return SecIt == Sections.end() ? nullptr : SecIt->get();
817 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000818 Range<Segment> segments() { return make_pointee_range(Segments); }
819 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000820
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000821 Error removeSections(std::function<bool(const SectionBase &)> ToRemove);
822 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000823 template <class T, class... Ts> T &addSection(Ts &&... Args) {
824 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
825 auto Ptr = Sec.get();
826 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000827 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000828 return *Ptr;
829 }
Peter Collingbournebf92b3f2019-03-12 02:17:01 +0000830 Segment &addSegment() {
831 Segments.emplace_back(llvm::make_unique<Segment>());
Jake Ehrlich76e91102018-01-25 22:46:17 +0000832 return *Segments.back();
833 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000834};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000835
836} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000837} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000838} // end namespace llvm
839
840#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H