blob: 54e72d29aba866999ba6366221f9746a7ae10a55 [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()); }
Fangrui Song82b01e02019-03-30 14:08:59 +000062 size_t size() const { return Sections.size(); }
Jake Ehrlich76e91102018-01-25 22:46:17 +000063
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000064 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000065
66 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000067 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000068};
Petr Hosek05a04cb2017-08-01 00:33:58 +000069
Jake Ehrlich76e91102018-01-25 22:46:17 +000070enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
71
72class SectionVisitor {
73public:
Jordan Rupprecht1f821762019-01-03 17:45:30 +000074 virtual ~SectionVisitor() = default;
Jake Ehrlich76e91102018-01-25 22:46:17 +000075
76 virtual void visit(const Section &Sec) = 0;
77 virtual void visit(const OwnedDataSection &Sec) = 0;
78 virtual void visit(const StringTableSection &Sec) = 0;
79 virtual void visit(const SymbolTableSection &Sec) = 0;
80 virtual void visit(const RelocationSection &Sec) = 0;
81 virtual void visit(const DynamicRelocationSection &Sec) = 0;
82 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000083 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000084 virtual void visit(const SectionIndexSection &Sec) = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +000085 virtual void visit(const CompressedSection &Sec) = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +000086 virtual void visit(const DecompressedSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000087};
88
Jordan Rupprecht1f821762019-01-03 17:45:30 +000089class MutableSectionVisitor {
90public:
91 virtual ~MutableSectionVisitor() = default;
92
93 virtual void visit(Section &Sec) = 0;
94 virtual void visit(OwnedDataSection &Sec) = 0;
95 virtual void visit(StringTableSection &Sec) = 0;
96 virtual void visit(SymbolTableSection &Sec) = 0;
97 virtual void visit(RelocationSection &Sec) = 0;
98 virtual void visit(DynamicRelocationSection &Sec) = 0;
99 virtual void visit(GnuDebugLinkSection &Sec) = 0;
100 virtual void visit(GroupSection &Sec) = 0;
101 virtual void visit(SectionIndexSection &Sec) = 0;
102 virtual void visit(CompressedSection &Sec) = 0;
103 virtual void visit(DecompressedSection &Sec) = 0;
104};
105
Jake Ehrlich76e91102018-01-25 22:46:17 +0000106class SectionWriter : public SectionVisitor {
107protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000108 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109
110public:
Fangrui Songa85bf872019-03-15 10:20:51 +0000111 virtual ~SectionWriter() = default;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000112
113 void visit(const Section &Sec) override;
114 void visit(const OwnedDataSection &Sec) override;
115 void visit(const StringTableSection &Sec) override;
116 void visit(const DynamicRelocationSection &Sec) override;
117 virtual void visit(const SymbolTableSection &Sec) override = 0;
118 virtual void visit(const RelocationSection &Sec) override = 0;
119 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000120 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000121 virtual void visit(const SectionIndexSection &Sec) override = 0;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000122 virtual void visit(const CompressedSection &Sec) override = 0;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000123 virtual void visit(const DecompressedSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000124
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000125 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000126};
127
128template <class ELFT> class ELFSectionWriter : public SectionWriter {
129private:
130 using Elf_Word = typename ELFT::Word;
131 using Elf_Rel = typename ELFT::Rel;
132 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000133 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000134
135public:
136 virtual ~ELFSectionWriter() {}
137 void visit(const SymbolTableSection &Sec) override;
138 void visit(const RelocationSection &Sec) override;
139 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000140 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000141 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000142 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000143 void visit(const DecompressedSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000144
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000145 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000146};
147
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000148template <class ELFT> class ELFSectionSizer : public MutableSectionVisitor {
149private:
150 using Elf_Rel = typename ELFT::Rel;
151 using Elf_Rela = typename ELFT::Rela;
152 using Elf_Sym = typename ELFT::Sym;
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +0000153 using Elf_Word = typename ELFT::Word;
154 using Elf_Xword = typename ELFT::Xword;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000155
156public:
157 void visit(Section &Sec) override;
158 void visit(OwnedDataSection &Sec) override;
159 void visit(StringTableSection &Sec) override;
160 void visit(DynamicRelocationSection &Sec) override;
161 void visit(SymbolTableSection &Sec) override;
162 void visit(RelocationSection &Sec) override;
163 void visit(GnuDebugLinkSection &Sec) override;
164 void visit(GroupSection &Sec) override;
165 void visit(SectionIndexSection &Sec) override;
166 void visit(CompressedSection &Sec) override;
167 void visit(DecompressedSection &Sec) override;
168};
169
Jake Ehrlich76e91102018-01-25 22:46:17 +0000170#define MAKE_SEC_WRITER_FRIEND \
171 friend class SectionWriter; \
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000172 template <class ELFT> friend class ELFSectionWriter; \
173 template <class ELFT> friend class ELFSectionSizer;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000174
175class BinarySectionWriter : public SectionWriter {
176public:
177 virtual ~BinarySectionWriter() {}
178
179 void visit(const SymbolTableSection &Sec) override;
180 void visit(const RelocationSection &Sec) override;
181 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000182 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000183 void visit(const SectionIndexSection &Sec) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000184 void visit(const CompressedSection &Sec) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000185 void visit(const DecompressedSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000186
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000187 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
188};
189
Jake Ehrlich76e91102018-01-25 22:46:17 +0000190class Writer {
191protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000192 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000193 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000194
195public:
196 virtual ~Writer();
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000197 virtual Error finalize() = 0;
198 virtual Error write() = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000199
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000200 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000201};
202
203template <class ELFT> class ELFWriter : public Writer {
204private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000205 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000206 using Elf_Shdr = typename ELFT::Shdr;
207 using Elf_Phdr = typename ELFT::Phdr;
208 using Elf_Ehdr = typename ELFT::Ehdr;
209
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000210 void initEhdrSegment();
211
Jake Ehrlich76e91102018-01-25 22:46:17 +0000212 void writeEhdr();
213 void writePhdr(const Segment &Seg);
214 void writeShdr(const SectionBase &Sec);
215
216 void writePhdrs();
217 void writeShdrs();
218 void writeSectionData();
James Henderson1f448142019-03-25 16:36:26 +0000219 void writeSegmentData();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000220
221 void assignOffsets();
222
223 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
224
225 size_t totalSize() const;
226
227public:
228 virtual ~ELFWriter() {}
James Henderson38cb2382019-04-02 14:11:13 +0000229 bool WriteSectionHeaders;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000230
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000231 Error finalize() override;
232 Error write() override;
James Henderson38cb2382019-04-02 14:11:13 +0000233 ELFWriter(Object &Obj, Buffer &Buf, bool WSH);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000234};
235
236class BinaryWriter : public Writer {
237private:
238 std::unique_ptr<BinarySectionWriter> SecWriter;
239
240 uint64_t TotalSize;
241
242public:
243 ~BinaryWriter() {}
Jordan Rupprecht881cae72019-01-22 23:49:16 +0000244 Error finalize() override;
245 Error write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000246 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000247};
248
Petr Hosek05a04cb2017-08-01 00:33:58 +0000249class SectionBase {
250public:
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000251 std::string Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000252 Segment *ParentSegment = nullptr;
253 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000254 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000255 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000256 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000257
258 uint64_t Addr = 0;
259 uint64_t Align = 1;
260 uint32_t EntrySize = 0;
261 uint64_t Flags = 0;
262 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000263 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000264 uint64_t NameIndex = 0;
265 uint64_t Offset = 0;
266 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000267 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000268 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000269
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000270 SectionBase() = default;
271 SectionBase(const SectionBase &) = default;
272
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000273 virtual ~SectionBase() = default;
274
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000275 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000276 virtual void finalize();
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000277 // Remove references to these sections. The list of sections must be sorted.
278 virtual Error
James Henderson66a9d0f2019-04-18 09:13:30 +0000279 removeSectionReferences(bool AllowBrokenLinks,
280 function_ref<bool(const SectionBase *)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000281 virtual Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000282 virtual void accept(SectionVisitor &Visitor) const = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000283 virtual void accept(MutableSectionVisitor &Visitor) = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000284 virtual void markSymbols();
George Rimard8a5c6c2019-03-11 11:01:24 +0000285 virtual void
286 replaceSectionReferences(const DenseMap<SectionBase *, SectionBase *> &);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000287};
288
289class Segment {
290private:
291 struct SectionCompare {
292 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
293 // Some sections might have the same address if one of them is empty. To
294 // fix this we can use the lexicographic ordering on ->Addr and the
295 // address of the actully stored section.
296 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
297 return Lhs < Rhs;
298 return Lhs->OriginalOffset < Rhs->OriginalOffset;
299 }
300 };
301
302 std::set<const SectionBase *, SectionCompare> Sections;
303
304public:
Fangrui Song967ce402018-12-12 22:46:37 +0000305 uint32_t Type;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306 uint32_t Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000307 uint64_t Offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000308 uint64_t VAddr;
Fangrui Song967ce402018-12-12 22:46:37 +0000309 uint64_t PAddr;
310 uint64_t FileSize;
311 uint64_t MemSize;
312 uint64_t Align;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000313
Fangrui Song967ce402018-12-12 22:46:37 +0000314 uint32_t Index;
Petr Hosek3f383832017-08-26 01:32:20 +0000315 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000316 Segment *ParentSegment = nullptr;
James Henderson1f448142019-03-25 16:36:26 +0000317 ArrayRef<uint8_t> Contents;
318
319 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
320 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000321
Petr Hosek05a04cb2017-08-01 00:33:58 +0000322 const SectionBase *firstSection() const {
323 if (!Sections.empty())
324 return *Sections.begin();
325 return nullptr;
326 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000327
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000328 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
329 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
James Henderson1f448142019-03-25 16:36:26 +0000330
331 ArrayRef<uint8_t> getContents() const { return Contents; }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000332};
333
334class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000335 MAKE_SEC_WRITER_FRIEND
336
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000337 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000338 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000339
340public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000341 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000342
Jake Ehrlich76e91102018-01-25 22:46:17 +0000343 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000344 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000345 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000346 function_ref<bool(const SectionBase *)> ToRemove) override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000347 void initialize(SectionTableRef SecTable) override;
348 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000349};
350
Jake Ehrliche8437de2017-12-19 00:47:30 +0000351class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000352 MAKE_SEC_WRITER_FRIEND
353
Jake Ehrliche8437de2017-12-19 00:47:30 +0000354 std::vector<uint8_t> Data;
355
356public:
357 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
358 : Data(std::begin(Data), std::end(Data)) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000359 Name = SecName.str();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000360 Type = ELF::SHT_PROGBITS;
361 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000362 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000363 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000364
365 void accept(SectionVisitor &Sec) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000366 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000367};
368
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000369class CompressedSection : public SectionBase {
370 MAKE_SEC_WRITER_FRIEND
371
372 DebugCompressionType CompressionType;
373 uint64_t DecompressedSize;
374 uint64_t DecompressedAlign;
375 SmallVector<char, 128> CompressedData;
376
377public:
378 CompressedSection(const SectionBase &Sec,
379 DebugCompressionType CompressionType);
Puyan Lotfiaf048642018-10-01 10:29:41 +0000380 CompressedSection(ArrayRef<uint8_t> CompressedData, uint64_t DecompressedSize,
381 uint64_t DecompressedAlign);
382
383 uint64_t getDecompressedSize() const { return DecompressedSize; }
384 uint64_t getDecompressedAlign() const { return DecompressedAlign; }
385
386 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000387 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000388
389 static bool classof(const SectionBase *S) {
390 return (S->Flags & ELF::SHF_COMPRESSED) ||
391 (StringRef(S->Name).startswith(".zdebug"));
392 }
393};
394
395class DecompressedSection : public SectionBase {
396 MAKE_SEC_WRITER_FRIEND
397
398public:
399 explicit DecompressedSection(const CompressedSection &Sec)
400 : SectionBase(Sec) {
401 Size = Sec.getDecompressedSize();
402 Align = Sec.getDecompressedAlign();
403 Flags = (Flags & ~ELF::SHF_COMPRESSED);
404 if (StringRef(Name).startswith(".zdebug"))
405 Name = "." + Name.substr(2);
406 }
407
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000408 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000409 void accept(MutableSectionVisitor &Visitor) override;
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000410};
411
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000412// There are two types of string tables that can exist, dynamic and not dynamic.
413// In the dynamic case the string table is allocated. Changing a dynamic string
414// table would mean altering virtual addresses and thus the memory image. So
415// dynamic string tables should not have an interface to modify them or
416// reconstruct them. This type lets us reconstruct a string table. To avoid
417// this class being used for dynamic string tables (which has happened) the
418// classof method checks that the particular instance is not allocated. This
419// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000420class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000421 MAKE_SEC_WRITER_FRIEND
422
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000423 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000424
425public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000426 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
427 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000428 }
429
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000430 void addString(StringRef Name);
431 uint32_t findIndex(StringRef Name) const;
George Rimarfaf308b2019-03-18 14:27:41 +0000432 void prepareForLayout();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000433 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000434 void accept(MutableSectionVisitor &Visitor) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000435
Petr Hosek05a04cb2017-08-01 00:33:58 +0000436 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000437 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000438 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000439 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000440 }
441};
442
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000443// Symbols have a st_shndx field that normally stores an index but occasionally
444// stores a different special value. This enum keeps track of what the st_shndx
445// field means. Most of the values are just copies of the special SHN_* values.
446// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
447enum SymbolShndxType {
448 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000449 SYMBOL_ABS = ELF::SHN_ABS,
450 SYMBOL_COMMON = ELF::SHN_COMMON,
451 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
452 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
453 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
454 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000455 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000456};
457
Petr Hosek79cee9e2017-08-29 02:12:03 +0000458struct Symbol {
459 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000460 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000461 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000462 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000463 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000464 uint32_t NameIndex;
465 uint64_t Size;
466 uint8_t Type;
467 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000468 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000469 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000470
471 uint16_t getShndx() const;
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000472 bool isCommon() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000473};
474
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000475class SectionIndexSection : public SectionBase {
476 MAKE_SEC_WRITER_FRIEND
477
478private:
479 std::vector<uint32_t> Indexes;
480 SymbolTableSection *Symbols = nullptr;
481
482public:
483 virtual ~SectionIndexSection() {}
484 void addIndex(uint32_t Index) {
Eugene Leviant88089fe2019-04-12 11:59:30 +0000485 assert(Size > 0);
486 Indexes.push_back(Index);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000487 }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000488
489 void reserve(size_t NumSymbols) {
490 Indexes.reserve(NumSymbols);
491 Size = NumSymbols * 4;
492 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000493 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
494 void initialize(SectionTableRef SecTable) override;
495 void finalize() override;
496 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000497 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000498
499 SectionIndexSection() {
500 Name = ".symtab_shndx";
501 Align = 4;
502 EntrySize = 4;
503 Type = ELF::SHT_SYMTAB_SHNDX;
504 }
505};
506
Petr Hosek79cee9e2017-08-29 02:12:03 +0000507class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000508 MAKE_SEC_WRITER_FRIEND
509
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000510 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000511 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000512
Petr Hosek79cee9e2017-08-29 02:12:03 +0000513protected:
514 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000515 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000516 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000517
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000518 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000519
Petr Hosek79cee9e2017-08-29 02:12:03 +0000520public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000521 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
522
523 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
524 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
George Rimar17dbb192019-05-08 07:31:05 +0000525 uint64_t SymbolSize);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000526 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000527 // An 'empty' symbol table still contains a null symbol.
528 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000529 void setShndxTable(SectionIndexSection *ShndxTable) {
530 SectionIndexTable = ShndxTable;
531 }
532 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Eugene Leviant88089fe2019-04-12 11:59:30 +0000533 void fillShndxTable();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000534 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000535 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000536 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000537 void updateSymbols(function_ref<void(Symbol &)> Callable);
538
James Henderson66a9d0f2019-04-18 09:13:30 +0000539 Error removeSectionReferences(bool AllowBrokenLinks,
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000540 function_ref<bool(const SectionBase *)> ToRemove) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000541 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000542 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000543 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000544 void accept(MutableSectionVisitor &Visitor) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000545 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
George Rimar0373bed2019-03-20 13:57:47 +0000546 void replaceSectionReferences(
547 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000548
Petr Hosek79cee9e2017-08-29 02:12:03 +0000549 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000550 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000551 }
552};
553
Petr Hosekd7df9b22017-09-06 23:41:02 +0000554struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000555 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000556 uint64_t Offset;
557 uint64_t Addend;
558 uint32_t Type;
559};
560
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000561// All relocation sections denote relocations to apply to another section.
562// However, some relocation sections use a dynamic symbol table and others use
563// a regular symbol table. Because the types of the two symbol tables differ in
564// our system (because they should behave differently) we can't uniformly
565// represent all relocations with the same base class if we expose an interface
566// that mentions the symbol table type. So we split the two base types into two
567// different classes, one which handles the section the relocation is applied to
568// and another which handles the symbol table type. The symbol table type is
569// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
570class RelocationSectionBase : public SectionBase {
571protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000572 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000573
574public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000575 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000576 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000577
578 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000579 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000580 }
581};
582
583// Takes the symbol table type to use as a parameter so that we can deduplicate
584// that code between the two symbol table types.
585template <class SymTabType>
586class RelocSectionWithSymtabBase : public RelocationSectionBase {
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000587 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000588
589protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000590 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000591
George Rimar79fb8582019-02-27 11:18:27 +0000592 SymTabType *Symbols = nullptr;
593
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000594public:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000595 void initialize(SectionTableRef SecTable) override;
596 void finalize() override;
597};
598
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000599class RelocationSection
600 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000601 MAKE_SEC_WRITER_FRIEND
602
Petr Hosekd7df9b22017-09-06 23:41:02 +0000603 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000604
Petr Hosekd7df9b22017-09-06 23:41:02 +0000605public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000606 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000607 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000608 void accept(MutableSectionVisitor &Visitor) override;
James Henderson66a9d0f2019-04-18 09:13:30 +0000609 Error removeSectionReferences(bool AllowBrokenLinks,
George Rimar79fb8582019-02-27 11:18:27 +0000610 function_ref<bool(const SectionBase *)> ToRemove) override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000611 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000612 void markSymbols() override;
George Rimard8a5c6c2019-03-11 11:01:24 +0000613 void replaceSectionReferences(
614 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000615
Petr Hosekd7df9b22017-09-06 23:41:02 +0000616 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000617 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000618 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000619 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000620 }
621};
622
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000623// TODO: The way stripping and groups interact is complicated
624// and still needs to be worked on.
625
626class GroupSection : public SectionBase {
627 MAKE_SEC_WRITER_FRIEND
628 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000629 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000630 ELF::Elf32_Word FlagWord;
631 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000632
633public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000634 // TODO: Contents is present in several classes of the hierarchy.
635 // This needs to be refactored to avoid duplication.
636 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000637
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000638 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
639
640 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000641 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000642 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
643 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
644
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000645 void accept(SectionVisitor &) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000646 void accept(MutableSectionVisitor &Visitor) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000647 void finalize() override;
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000648 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000649 void markSymbols() override;
George Rimar27257172019-03-24 14:41:45 +0000650 void replaceSectionReferences(
651 const DenseMap<SectionBase *, SectionBase *> &FromTo) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000652
653 static bool classof(const SectionBase *S) {
654 return S->Type == ELF::SHT_GROUP;
655 }
656};
657
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000658class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000659public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000660 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000661
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000662 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000663 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000664 }
665};
666
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000667class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000668public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000669 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000670
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000671 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000672 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000673 }
674};
675
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000676class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000677 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000678 MAKE_SEC_WRITER_FRIEND
679
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000680private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000681 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000682
683public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000684 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
George Rimar67f590e2019-04-30 11:02:09 +0000685
686 void accept(SectionVisitor &) const override;
687 void accept(MutableSectionVisitor &Visitor) override;
688 Error removeSectionReferences(
689 bool AllowBrokenLinks,
690 function_ref<bool(const SectionBase *)> ToRemove) override;
691
692 static bool classof(const SectionBase *S) {
693 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000694 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000695 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000696 }
697};
698
Jake Ehrlich76e91102018-01-25 22:46:17 +0000699class GnuDebugLinkSection : public SectionBase {
700 MAKE_SEC_WRITER_FRIEND
701
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000702private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000703 StringRef FileName;
704 uint32_t CRC32;
705
706 void init(StringRef File, StringRef Data);
707
708public:
709 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000710 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000711 void accept(SectionVisitor &Visitor) const override;
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000712 void accept(MutableSectionVisitor &Visitor) override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000713};
714
Jake Ehrlich76e91102018-01-25 22:46:17 +0000715class Reader {
716public:
717 virtual ~Reader();
718 virtual std::unique_ptr<Object> create() const = 0;
719};
720
Jake Ehrlich76e91102018-01-25 22:46:17 +0000721using object::Binary;
722using object::ELFFile;
723using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000724using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000725
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000726class BinaryELFBuilder {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000727 uint16_t EMachine;
728 MemoryBuffer *MemBuf;
729 std::unique_ptr<Object> Obj;
730
731 void initFileHeader();
732 void initHeaderSegment();
733 StringTableSection *addStrTab();
734 SymbolTableSection *addSymTab(StringTableSection *StrTab);
735 void addData(SymbolTableSection *SymTab);
736 void initSections();
737
738public:
739 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
740 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
741
742 std::unique_ptr<Object> build();
743};
744
Jake Ehrlich76e91102018-01-25 22:46:17 +0000745template <class ELFT> class ELFBuilder {
746private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000747 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000748 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000749 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000750
751 const ELFFile<ELFT> &ElfFile;
752 Object &Obj;
753
Jake Ehrlich6452b112018-02-14 23:31:33 +0000754 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000755 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000756 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000757 void initSymbolTable(SymbolTableSection *SymTab);
758 void readSectionHeaders();
759 SectionBase &makeSection(const Elf_Shdr &Shdr);
760
761public:
762 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
763 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
764
765 void build();
766};
767
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000768class BinaryReader : public Reader {
769 const MachineInfo &MInfo;
770 MemoryBuffer *MemBuf;
771
772public:
773 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
774 : MInfo(MI), MemBuf(MB) {}
775 std::unique_ptr<Object> create() const override;
776};
777
Jake Ehrlich76e91102018-01-25 22:46:17 +0000778class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000779 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000780
781public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000782 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000783 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000784};
785
786class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000787private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000788 using SecPtr = std::unique_ptr<SectionBase>;
789 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000790
Petr Hosekc4df10e2017-08-04 21:09:26 +0000791 std::vector<SecPtr> Sections;
792 std::vector<SegPtr> Segments;
James Henderson1f448142019-03-25 16:36:26 +0000793 std::vector<SecPtr> RemovedSections;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000794
Petr Hosek05a04cb2017-08-01 00:33:58 +0000795public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000796 template <class T>
797 using Range = iterator_range<
798 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
799
800 template <class T>
801 using ConstRange = iterator_range<pointee_iterator<
802 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
803
Jake Ehrlich6452b112018-02-14 23:31:33 +0000804 // It is often the case that the ELF header and the program header table are
805 // not present in any segment. This could be a problem during file layout,
806 // because other segments may get assigned an offset where either of the
807 // two should reside, which will effectively corrupt the resulting binary.
808 // Other than that we use these segments to track program header offsets
809 // when they may not follow the ELF header.
810 Segment ElfHdrSegment;
811 Segment ProgramHdrSegment;
812
George Rimar4ded7732018-12-20 10:51:42 +0000813 uint8_t OSABI;
814 uint8_t ABIVersion;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000815 uint64_t Entry;
816 uint64_t SHOffset;
817 uint32_t Type;
818 uint32_t Machine;
819 uint32_t Version;
820 uint32_t Flags;
821
James Henderson38cb2382019-04-02 14:11:13 +0000822 bool HadShdrs = true;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000823 StringTableSection *SectionNames = nullptr;
824 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000825 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000826
Aaron Ballman09f46a72018-01-25 21:03:38 +0000827 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000828 SectionTableRef sections() { return SectionTableRef(Sections); }
829 ConstRange<SectionBase> sections() const {
830 return make_pointee_range(Sections);
831 }
Eugene Leviant51c1f642019-02-25 14:12:41 +0000832 SectionBase *findSection(StringRef Name) {
833 auto SecIt =
834 find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; });
835 return SecIt == Sections.end() ? nullptr : SecIt->get();
836 }
James Henderson1f448142019-03-25 16:36:26 +0000837 SectionTableRef removedSections() { return SectionTableRef(RemovedSections); }
838
Jake Ehrlich76e91102018-01-25 22:46:17 +0000839 Range<Segment> segments() { return make_pointee_range(Segments); }
840 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000841
James Henderson66a9d0f2019-04-18 09:13:30 +0000842 Error removeSections(bool AllowBrokenLinks,
843 std::function<bool(const SectionBase &)> ToRemove);
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000844 Error removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000845 template <class T, class... Ts> T &addSection(Ts &&... Args) {
846 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
847 auto Ptr = Sec.get();
848 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000849 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000850 return *Ptr;
851 }
James Henderson1f448142019-03-25 16:36:26 +0000852 Segment &addSegment(ArrayRef<uint8_t> Data) {
853 Segments.emplace_back(llvm::make_unique<Segment>(Data));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000854 return *Segments.back();
855 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000856};
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +0000857
858} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000859} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000860} // end namespace llvm
861
862#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H