blob: 9298518bcce9bbc98555ac56daf63f699a2b9775 [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"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000021#include <cstddef>
22#include <cstdint>
23#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000024#include <memory>
25#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000026#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000027
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000028namespace llvm {
Puyan Lotfi99124cc2018-09-07 08:10:22 +000029enum class DebugCompressionType;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000030namespace objcopy {
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +000031namespace elf {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000032
Jake Ehrlichf5a43772017-09-25 20:37:28 +000033class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000034class Section;
35class OwnedDataSection;
36class StringTableSection;
37class SymbolTableSection;
38class RelocationSection;
39class DynamicRelocationSection;
40class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000041class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000042class SectionIndexSection;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000043class CompressedSection;
Puyan Lotfiaf048642018-10-01 10:29:41 +000044class DecompressedSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000045class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000046class Object;
Paul Semel4246a462018-05-09 21:36:54 +000047struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000048
49class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000050 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000051
52public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000053 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
54
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000055 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000056 : Sections(Secs) {}
57 SectionTableRef(const SectionTableRef &) = default;
58
Jake Ehrlich76e91102018-01-25 22:46:17 +000059 iterator begin() { return iterator(Sections.data()); }
60 iterator end() { return iterator(Sections.data() + Sections.size()); }
Fangrui Song82b01e02019-03-30 14:08:59 +000061 size_t size() const { return Sections.size(); }
Jake Ehrlich76e91102018-01-25 22:46:17 +000062
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();
James Henderson1f448142019-03-25 16:36:26 +0000218 void writeSegmentData();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000219
220 void assignOffsets();
221
222 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
223
224 size_t totalSize() const;
225
226public:
227 virtual ~ELFWriter() {}
James Henderson38cb2382019-04-02 14:11:13 +0000228 bool WriteSectionHeaders;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000229
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000230 Error finalize() override;
231 Error write() override;
James Henderson38cb2382019-04-02 14:11:13 +0000232 ELFWriter(Object &Obj, Buffer &Buf, bool 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
James Henderson66a9d0f2019-04-18 09:13:30 +0000278 removeSectionReferences(bool AllowBrokenLinks,
279 function_ref<bool(const SectionBase *)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000280 virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000281 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000282 virtual void accept(MutableSectionVisitor &Visitor) = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000283 virtual void markSymbols();
George Rimard8a5c6c2019-03-11 11:01:24 +0000284 virtual void
285 replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000286};
287
288class Segment {
289private:
290 struct SectionCompare {
291 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
292 // Some sections might have the same address if one of them is empty. To
293 // fix this we can use the lexicographic ordering on ->Addr and the
294 // address of the actully stored section.
295 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
296 return Lhs < Rhs;
297 return Lhs->OriginalOffset < Rhs->OriginalOffset;
298 }
299 };
300
301 std::set<const SectionBase *, SectionCompare> Sections;
302
303public:
Fangrui Song967ce402018-12-12 22:46:37 +0000304 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000305 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000307 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000308 uint64_t PAddr;
309 uint64_t FileSize;
310 uint64_t MemSize;
311 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000312
Fangrui Song967ce402018-12-12 22:46:37 +0000313 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000314 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000315 Segment *ParentSegment = nullptr;
James Henderson1f448142019-03-25 16:36:26 +0000316 ArrayRef<uint8_t> Contents;
317
318 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
319 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000320
Petr Hosek05a04cb2017-08-01 00:33:58 +0000321 const SectionBase *firstSection() const {
322 if (!Sections.empty())
323 return *Sections.begin();
324 return nullptr;
325 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000326
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000327 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
328 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
James Henderson1f448142019-03-25 16:36:26 +0000329
330 ArrayRef<uint8_t> getContents() const { return Contents; }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000331};
332
333class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000334 MAKE_SEC_WRITER_FRIEND
335
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000336 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000337 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000338
339public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000340 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000341
Jake Ehrlich76e91102018-01-25 22:46:17 +0000342 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000343 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000344 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000345 function_ref<bool(const SectionBase *)> ToRemove) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000346 void initialize(SectionTableRef SecTable) override;
347 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000348};
349
Jake Ehrliche8437de2017-12-19 00:47:30 +0000350class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000351 MAKE_SEC_WRITER_FRIEND
352
Jake Ehrliche8437de2017-12-19 00:47:30 +0000353 std::vector<uint8_t> Data;
354
355public:
356 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
357 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000358 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000359 Type = ELF::SHT_PROGBITS;
360 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000361 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000362 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000363
364 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000365 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000366};
367
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000368class CompressedSection : public SectionBase {
369 MAKE_SEC_WRITER_FRIEND
370
371 DebugCompressionType CompressionType;
372 uint64_t DecompressedSize;
373 uint64_t DecompressedAlign;
374 SmallVector<char, 128> CompressedData;
375
376public:
377 CompressedSection(const SectionBase &Sec,
378 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000379 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
380 uint64_t DecompressedAlign);
381
382 uint64_t getDecompressedSize() const { return DecompressedSize; }
383 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
384
385 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000386 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000387
388 static bool classof(const SectionBase *S) {
389 return (S->Flags & ELF::SHF_COMPRESSED) ||
390 (StringRef(S->Name).startswith(".zdebug"));
391 }
392};
393
394class DecompressedSection : public SectionBase {
395 MAKE_SEC_WRITER_FRIEND
396
397public:
398 explicit DecompressedSection(const CompressedSection &Sec)
399 : SectionBase(Sec) {
400 Size = Sec.getDecompressedSize();
401 Align = Sec.getDecompressedAlign();
402 Flags = (Flags & ~ELF::SHF_COMPRESSED);
403 if (StringRef(Name).startswith(".zdebug"))
404 Name = "." + Name.substr(2);
405 }
406
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000407 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000408 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000409};
410
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000411// There are two types of string tables that can exist, dynamic and not dynamic.
412// In the dynamic case the string table is allocated. Changing a dynamic string
413// table would mean altering virtual addresses and thus the memory image. So
414// dynamic string tables should not have an interface to modify them or
415// reconstruct them. This type lets us reconstruct a string table. To avoid
416// this class being used for dynamic string tables (which has happened) the
417// classof method checks that the particular instance is not allocated. This
418// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000419class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000420 MAKE_SEC_WRITER_FRIEND
421
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000422 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000423
424public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000425 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
426 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000427 }
428
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000429 void addString(StringRef Name);
430 uint32_t findIndex(StringRef Name) const;
George Rimarfaf308b2019-03-18 14:27:41 +0000431 void prepareForLayout();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000432 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000433 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000434
Petr Hosek05a04cb2017-08-01 00:33:58 +0000435 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000436 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000437 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000438 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000439 }
440};
441
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000442// Symbols have a st_shndx field that normally stores an index but occasionally
443// stores a different special value. This enum keeps track of what the st_shndx
444// field means. Most of the values are just copies of the special SHN_* values.
445// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
446enum SymbolShndxType {
447 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000448 SYMBOL_ABS = ELF::SHN_ABS,
449 SYMBOL_COMMON = ELF::SHN_COMMON,
450 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
451 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
452 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
453 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000454 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000455};
456
Petr Hosek79cee9e2017-08-29 02:12:03 +0000457struct Symbol {
458 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000459 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000460 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000461 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000462 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000463 uint32_t NameIndex;
464 uint64_t Size;
465 uint8_t Type;
466 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000467 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000468 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000469
470 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000471 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000472};
473
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000474class SectionIndexSection : public SectionBase {
475 MAKE_SEC_WRITER_FRIEND
476
477private:
478 std::vector<uint32_t> Indexes;
479 SymbolTableSection *Symbols = nullptr;
480
481public:
482 virtual ~SectionIndexSection() {}
483 void addIndex(uint32_t Index) {
Eugene Leviant88089fe2019-04-12 11:59:30 +0000484 assert(Size > 0);
485 Indexes.push_back(Index);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000486 }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000487
488 void reserve(size_t NumSymbols) {
489 Indexes.reserve(NumSymbols);
490 Size = NumSymbols * 4;
491 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000492 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
493 void initialize(SectionTableRef SecTable) override;
494 void finalize() override;
495 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000496 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000497
498 SectionIndexSection() {
499 Name = ".symtab_shndx";
500 Align = 4;
501 EntrySize = 4;
502 Type = ELF::SHT_SYMTAB_SHNDX;
503 }
504};
505
Petr Hosek79cee9e2017-08-29 02:12:03 +0000506class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000507 MAKE_SEC_WRITER_FRIEND
508
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000509 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000510 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000511
Petr Hosek79cee9e2017-08-29 02:12:03 +0000512protected:
513 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000514 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000515 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000516
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000517 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000518
Petr Hosek79cee9e2017-08-29 02:12:03 +0000519public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000520 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
521
522 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
523 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
George Rimar17dbb192019-05-08 07:31:05 +0000524 uint64_t SymbolSize);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000525 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000526 // An 'empty' symbol table still contains a null symbol.
527 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000528 void setShndxTable(SectionIndexSection *ShndxTable) {
529 SectionIndexTable = ShndxTable;
530 }
531 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000532 void fillShndxTable();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000533 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000534 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000535 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000536 void updateSymbols(function_ref<void(Symbol &)> Callable);
537
James Henderson66a9d0f2019-04-18 09:13:30 +0000538 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000539 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000540 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000541 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000542 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000543 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000544 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
George Rimar0373bed2019-03-20 13:57:47 +0000545 void replaceSectionReferences(
546 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000547
Petr Hosek79cee9e2017-08-29 02:12:03 +0000548 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000549 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000550 }
551};
552
Petr Hosekd7df9b22017-09-06 23:41:02 +0000553struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000554 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000555 uint64_t Offset;
556 uint64_t Addend;
557 uint32_t Type;
558};
559
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000560// All relocation sections denote relocations to apply to another section.
561// However, some relocation sections use a dynamic symbol table and others use
562// a regular symbol table. Because the types of the two symbol tables differ in
563// our system (because they should behave differently) we can't uniformly
564// represent all relocations with the same base class if we expose an interface
565// that mentions the symbol table type. So we split the two base types into two
566// different classes, one which handles the section the relocation is applied to
567// and another which handles the symbol table type. The symbol table type is
568// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
569class RelocationSectionBase : public SectionBase {
570protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000571 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000572
573public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000574 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000575 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000576
577 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000578 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000579 }
580};
581
582// Takes the symbol table type to use as a parameter so that we can deduplicate
583// that code between the two symbol table types.
584template <class SymTabType>
585class RelocSectionWithSymtabBase : public RelocationSectionBase {
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000586 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000587
588protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000589 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000590
George Rimar79fb8582019-02-27 11:18:27 +0000591 SymTabType *Symbols = nullptr;
592
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000593public:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000594 void initialize(SectionTableRef SecTable) override;
595 void finalize() override;
596};
597
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000598class RelocationSection
599 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000600 MAKE_SEC_WRITER_FRIEND
601
Petr Hosekd7df9b22017-09-06 23:41:02 +0000602 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000603
Petr Hosekd7df9b22017-09-06 23:41:02 +0000604public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000605 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000606 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000607 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000608 Error removeSectionReferences(bool AllowBrokenLinks,
George Rimar79fb8582019-02-27 11:18:27 +0000609 function_ref<bool(const SectionBase *)> ToRemove) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000610 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000611 void markSymbols() override;
George Rimard8a5c6c2019-03-11 11:01:24 +0000612 void replaceSectionReferences(
613 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000614
Petr Hosekd7df9b22017-09-06 23:41:02 +0000615 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000616 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000617 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000618 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000619 }
620};
621
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000622// TODO: The way stripping and groups interact is complicated
623// and still needs to be worked on.
624
625class GroupSection : public SectionBase {
626 MAKE_SEC_WRITER_FRIEND
627 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000628 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000629 ELF::Elf32_Word FlagWord;
630 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000631
632public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000633 // TODO: Contents is present in several classes of the hierarchy.
634 // This needs to be refactored to avoid duplication.
635 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000636
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000637 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
638
639 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000640 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000641 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
642 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
643
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000644 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000645 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000646 void finalize() override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000647 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000648 void markSymbols() override;
George Rimar27257172019-03-24 14:41:45 +0000649 void replaceSectionReferences(
650 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000651
652 static bool classof(const SectionBase *S) {
653 return S->Type == ELF::SHT_GROUP;
654 }
655};
656
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000657class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000658public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000659 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000660
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000661 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000662 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000663 }
664};
665
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000666class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000667public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000668 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000669
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000670 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000671 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000672 }
673};
674
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000675class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000676 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000677 MAKE_SEC_WRITER_FRIEND
678
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000679private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000680 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000681
682public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000683 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
George Rimar67f590e2019-04-30 11:02:09 +0000684
685 void accept(SectionVisitor &) const override;
686 void accept(MutableSectionVisitor &Visitor) override;
687 Error removeSectionReferences(
688 bool AllowBrokenLinks,
689 function_ref<bool(const SectionBase *)> ToRemove) override;
690
691 static bool classof(const SectionBase *S) {
692 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000693 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000694 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000695 }
696};
697
Jake Ehrlich76e91102018-01-25 22:46:17 +0000698class GnuDebugLinkSection : public SectionBase {
699 MAKE_SEC_WRITER_FRIEND
700
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000701private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000702 StringRef FileName;
703 uint32_t CRC32;
704
James Henderson9df38832019-05-14 10:59:04 +0000705 void init(StringRef File);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000706
707public:
708 // If we add this section from an external source we can use this ctor.
James Henderson9df38832019-05-14 10:59:04 +0000709 explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000710 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000711 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000712};
713
Jake Ehrlich76e91102018-01-25 22:46:17 +0000714class Reader {
715public:
716 virtual ~Reader();
717 virtual std::unique_ptr<Object> create() const = 0;
718};
719
Jake Ehrlich76e91102018-01-25 22:46:17 +0000720using object::Binary;
721using object::ELFFile;
722using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000723using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000724
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000725class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000726 uint16_t EMachine;
727 MemoryBuffer *MemBuf;
728 std::unique_ptr<Object> Obj;
729
730 void initFileHeader();
731 void initHeaderSegment();
732 StringTableSection *addStrTab();
733 SymbolTableSection *addSymTab(StringTableSection *StrTab);
734 void addData(SymbolTableSection *SymTab);
735 void initSections();
736
737public:
738 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
739 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
740
741 std::unique_ptr<Object> build();
742};
743
Jake Ehrlich76e91102018-01-25 22:46:17 +0000744template <class ELFT> class ELFBuilder {
745private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000746 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000747 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000748 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000749
750 const ELFFile<ELFT> &ElfFile;
751 Object &Obj;
752
Jake Ehrlich6452b112018-02-14 23:31:33 +0000753 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000754 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000755 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000756 void initSymbolTable(SymbolTableSection *SymTab);
757 void readSectionHeaders();
758 SectionBase &makeSection(const Elf_Shdr &Shdr);
759
760public:
761 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
762 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
763
764 void build();
765};
766
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000767class BinaryReader : public Reader {
768 const MachineInfo &MInfo;
769 MemoryBuffer *MemBuf;
770
771public:
772 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
773 : MInfo(MI), MemBuf(MB) {}
774 std::unique_ptr<Object> create() const override;
775};
776
Jake Ehrlich76e91102018-01-25 22:46:17 +0000777class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000778 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000779
780public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000781 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000782 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000783};
784
785class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000786private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000787 using SecPtr = std::unique_ptr<SectionBase>;
788 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000789
Petr Hosekc4df10e2017-08-04 21:09:26 +0000790 std::vector<SecPtr> Sections;
791 std::vector<SegPtr> Segments;
James Henderson1f448142019-03-25 16:36:26 +0000792 std::vector<SecPtr> RemovedSections;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000793
Petr Hosek05a04cb2017-08-01 00:33:58 +0000794public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000795 template <class T>
796 using Range = iterator_range<
797 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
798
799 template <class T>
800 using ConstRange = iterator_range<pointee_iterator<
801 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
802
Jake Ehrlich6452b112018-02-14 23:31:33 +0000803 // It is often the case that the ELF header and the program header table are
804 // not present in any segment. This could be a problem during file layout,
805 // because other segments may get assigned an offset where either of the
806 // two should reside, which will effectively corrupt the resulting binary.
807 // Other than that we use these segments to track program header offsets
808 // when they may not follow the ELF header.
809 Segment ElfHdrSegment;
810 Segment ProgramHdrSegment;
811
George Rimar4ded7732018-12-20 10:51:42 +0000812 uint8_t OSABI;
813 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000814 uint64_t Entry;
815 uint64_t SHOffset;
816 uint32_t Type;
817 uint32_t Machine;
818 uint32_t Version;
819 uint32_t Flags;
820
James Henderson38cb2382019-04-02 14:11:13 +0000821 bool HadShdrs = true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000822 StringTableSection *SectionNames = nullptr;
823 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000824 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000825
Aaron Ballman09f46a72018-01-25 21:03:38 +0000826 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000827 SectionTableRef sections() { return SectionTableRef(Sections); }
828 ConstRange<SectionBase> sections() const {
829 return make_pointee_range(Sections);
830 }
Eugene Leviant51c1f642019-02-25 14:12:41 +0000831 SectionBase *findSection(StringRef Name) {
832 auto SecIt =
833 find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; });
834 return SecIt == Sections.end() ? nullptr : SecIt->get();
835 }
James Henderson1f448142019-03-25 16:36:26 +0000836 SectionTableRef removedSections() { return SectionTableRef(RemovedSections); }
837
Jake Ehrlich76e91102018-01-25 22:46:17 +0000838 Range<Segment> segments() { return make_pointee_range(Segments); }
839 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000840
James Henderson66a9d0f2019-04-18 09:13:30 +0000841 Error removeSections(bool AllowBrokenLinks,
842 std::function<bool(const SectionBase &)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000843 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000844 template <class T, class... Ts> T &addSection(Ts &&... Args) {
845 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
846 auto Ptr = Sec.get();
847 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000848 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000849 return *Ptr;
850 }
James Henderson1f448142019-03-25 16:36:26 +0000851 Segment &addSegment(ArrayRef<uint8_t> Data) {
852 Segments.emplace_back(llvm::make_unique<Segment>(Data));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000853 return *Segments.back();
854 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000855};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000856
857} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000858} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000859} // end namespace llvm
860
861#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H