blob: 8b745284fb0316d0023da7484b9f965b6fa09355 [file] [log] [blame]
Petr Hosek05a04cb2017-08-01 00:33:58 +00001//===- Object.h -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000010#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
11#define LLVM_TOOLS_OBJCOPY_OBJECT_H
Petr Hosek05a04cb2017-08-01 00:33:58 +000012
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000013#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000017#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELFObjectFile.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000019#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000020#include "llvm/Support/JamCRC.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 Lotfi0f5d5fa2018-07-18 00:10:51 +000029namespace objcopy {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000030
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000031class Buffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000032class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000033class Section;
34class OwnedDataSection;
35class StringTableSection;
36class SymbolTableSection;
37class RelocationSection;
38class DynamicRelocationSection;
39class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000040class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000041class SectionIndexSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000042class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000043class Object;
Paul Semel4246a462018-05-09 21:36:54 +000044struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000045
46class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000047 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000048
49public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000050 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
51
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000052 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000053 : Sections(Secs) {}
54 SectionTableRef(const SectionTableRef &) = default;
55
Jake Ehrlich76e91102018-01-25 22:46:17 +000056 iterator begin() { return iterator(Sections.data()); }
57 iterator end() { return iterator(Sections.data() + Sections.size()); }
58
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000059 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000060
61 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000062 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000063};
Petr Hosek05a04cb2017-08-01 00:33:58 +000064
Jake Ehrlich76e91102018-01-25 22:46:17 +000065enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
66
67class SectionVisitor {
68public:
69 virtual ~SectionVisitor();
70
71 virtual void visit(const Section &Sec) = 0;
72 virtual void visit(const OwnedDataSection &Sec) = 0;
73 virtual void visit(const StringTableSection &Sec) = 0;
74 virtual void visit(const SymbolTableSection &Sec) = 0;
75 virtual void visit(const RelocationSection &Sec) = 0;
76 virtual void visit(const DynamicRelocationSection &Sec) = 0;
77 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000078 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000079 virtual void visit(const SectionIndexSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000080};
81
82class SectionWriter : public SectionVisitor {
83protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000084 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000085
86public:
87 virtual ~SectionWriter(){};
88
89 void visit(const Section &Sec) override;
90 void visit(const OwnedDataSection &Sec) override;
91 void visit(const StringTableSection &Sec) override;
92 void visit(const DynamicRelocationSection &Sec) override;
93 virtual void visit(const SymbolTableSection &Sec) override = 0;
94 virtual void visit(const RelocationSection &Sec) override = 0;
95 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000096 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000097 virtual void visit(const SectionIndexSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000098
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000099 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000100};
101
102template <class ELFT> class ELFSectionWriter : public SectionWriter {
103private:
104 using Elf_Word = typename ELFT::Word;
105 using Elf_Rel = typename ELFT::Rel;
106 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000107 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000108
109public:
110 virtual ~ELFSectionWriter() {}
111 void visit(const SymbolTableSection &Sec) override;
112 void visit(const RelocationSection &Sec) override;
113 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000114 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000115 void visit(const SectionIndexSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000116
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000117 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000118};
119
120#define MAKE_SEC_WRITER_FRIEND \
121 friend class SectionWriter; \
122 template <class ELFT> friend class ELFSectionWriter;
123
124class BinarySectionWriter : public SectionWriter {
125public:
126 virtual ~BinarySectionWriter() {}
127
128 void visit(const SymbolTableSection &Sec) override;
129 void visit(const RelocationSection &Sec) override;
130 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000131 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000132 void visit(const SectionIndexSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000133
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000134 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
135};
136
137// The class Buffer abstracts out the common interface of FileOutputBuffer and
138// WritableMemoryBuffer so that the hierarchy of Writers depends on this
139// abstract interface and doesn't depend on a particular implementation.
140// TODO: refactor the buffer classes in LLVM to enable us to use them here
141// directly.
142class Buffer {
143 StringRef Name;
144
145public:
146 virtual ~Buffer();
147 virtual void allocate(size_t Size) = 0;
148 virtual uint8_t *getBufferStart() = 0;
149 virtual Error commit() = 0;
150
151 explicit Buffer(StringRef Name) : Name(Name) {}
152 StringRef getName() const { return Name; }
153};
154
155class FileBuffer : public Buffer {
156 std::unique_ptr<FileOutputBuffer> Buf;
157
158public:
159 void allocate(size_t Size) override;
160 uint8_t *getBufferStart() override;
161 Error commit() override;
162
163 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
164};
165
166class MemBuffer : public Buffer {
167 std::unique_ptr<WritableMemoryBuffer> Buf;
168
169public:
170 void allocate(size_t Size) override;
171 uint8_t *getBufferStart() override;
172 Error commit() override;
173
174 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
175
176 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000177};
178
179class Writer {
180protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000181 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000182 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000183
184public:
185 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000186 virtual void finalize() = 0;
187 virtual void write() = 0;
188
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000189 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000190};
191
192template <class ELFT> class ELFWriter : public Writer {
193private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000194 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000195 using Elf_Shdr = typename ELFT::Shdr;
196 using Elf_Phdr = typename ELFT::Phdr;
197 using Elf_Ehdr = typename ELFT::Ehdr;
198
199 void writeEhdr();
200 void writePhdr(const Segment &Seg);
201 void writeShdr(const SectionBase &Sec);
202
203 void writePhdrs();
204 void writeShdrs();
205 void writeSectionData();
206
207 void assignOffsets();
208
209 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
210
211 size_t totalSize() const;
212
213public:
214 virtual ~ELFWriter() {}
215 bool WriteSectionHeaders = true;
216
217 void finalize() override;
218 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000219 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
220 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000221};
222
223class BinaryWriter : public Writer {
224private:
225 std::unique_ptr<BinarySectionWriter> SecWriter;
226
227 uint64_t TotalSize;
228
229public:
230 ~BinaryWriter() {}
231 void finalize() override;
232 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000233 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000234};
235
Petr Hosek05a04cb2017-08-01 00:33:58 +0000236class SectionBase {
237public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000238 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000239 Segment *ParentSegment = nullptr;
240 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000241 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000242 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000243 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000244
245 uint64_t Addr = 0;
246 uint64_t Align = 1;
247 uint32_t EntrySize = 0;
248 uint64_t Flags = 0;
249 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000250 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000251 uint64_t NameIndex = 0;
252 uint64_t Offset = 0;
253 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000254 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000255 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000256
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000257 virtual ~SectionBase() = default;
258
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000259 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000260 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000261 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000262 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000263 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000264 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000265};
266
267class Segment {
268private:
269 struct SectionCompare {
270 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
271 // Some sections might have the same address if one of them is empty. To
272 // fix this we can use the lexicographic ordering on ->Addr and the
273 // address of the actully stored section.
274 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
275 return Lhs < Rhs;
276 return Lhs->OriginalOffset < Rhs->OriginalOffset;
277 }
278 };
279
280 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000281 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000282
283public:
284 uint64_t Align;
285 uint64_t FileSize;
286 uint32_t Flags;
287 uint32_t Index;
288 uint64_t MemSize;
289 uint64_t Offset;
290 uint64_t PAddr;
291 uint64_t Type;
292 uint64_t VAddr;
293
Petr Hosek3f383832017-08-26 01:32:20 +0000294 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000295 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000296
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000297 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000298 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000299
Petr Hosek05a04cb2017-08-01 00:33:58 +0000300 const SectionBase *firstSection() const {
301 if (!Sections.empty())
302 return *Sections.begin();
303 return nullptr;
304 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000305
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000306 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
307 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000308};
309
310class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000311 MAKE_SEC_WRITER_FRIEND
312
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000313 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000314 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000315
316public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000317 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000318
Jake Ehrlich76e91102018-01-25 22:46:17 +0000319 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000320 void removeSectionReferences(const SectionBase *Sec) override;
321 void initialize(SectionTableRef SecTable) override;
322 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000323};
324
Jake Ehrliche8437de2017-12-19 00:47:30 +0000325class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000326 MAKE_SEC_WRITER_FRIEND
327
Jake Ehrliche8437de2017-12-19 00:47:30 +0000328 std::vector<uint8_t> Data;
329
330public:
331 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
332 : Data(std::begin(Data), std::end(Data)) {
333 Name = SecName;
334 Type = ELF::SHT_PROGBITS;
335 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000336 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000337 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000338
339 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000340};
341
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000342// There are two types of string tables that can exist, dynamic and not dynamic.
343// In the dynamic case the string table is allocated. Changing a dynamic string
344// table would mean altering virtual addresses and thus the memory image. So
345// dynamic string tables should not have an interface to modify them or
346// reconstruct them. This type lets us reconstruct a string table. To avoid
347// this class being used for dynamic string tables (which has happened) the
348// classof method checks that the particular instance is not allocated. This
349// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000350class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000351 MAKE_SEC_WRITER_FRIEND
352
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000353 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000354
355public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000356 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
357 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000358 }
359
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000360 void addString(StringRef Name);
361 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000362 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000363 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000364
Petr Hosek05a04cb2017-08-01 00:33:58 +0000365 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000366 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000367 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000368 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000369 }
370};
371
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000372// Symbols have a st_shndx field that normally stores an index but occasionally
373// stores a different special value. This enum keeps track of what the st_shndx
374// field means. Most of the values are just copies of the special SHN_* values.
375// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
376enum SymbolShndxType {
377 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000378 SYMBOL_ABS = ELF::SHN_ABS,
379 SYMBOL_COMMON = ELF::SHN_COMMON,
380 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
381 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
382 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
383 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000384 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000385};
386
Petr Hosek79cee9e2017-08-29 02:12:03 +0000387struct Symbol {
388 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000389 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000390 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000391 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000392 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000393 uint32_t NameIndex;
394 uint64_t Size;
395 uint8_t Type;
396 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000397 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000398 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000399
400 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000401};
402
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000403class SectionIndexSection : public SectionBase {
404 MAKE_SEC_WRITER_FRIEND
405
406private:
407 std::vector<uint32_t> Indexes;
408 SymbolTableSection *Symbols = nullptr;
409
410public:
411 virtual ~SectionIndexSection() {}
412 void addIndex(uint32_t Index) {
413 Indexes.push_back(Index);
414 Size += 4;
415 }
416 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
417 void initialize(SectionTableRef SecTable) override;
418 void finalize() override;
419 void accept(SectionVisitor &Visitor) const override;
420
421 SectionIndexSection() {
422 Name = ".symtab_shndx";
423 Align = 4;
424 EntrySize = 4;
425 Type = ELF::SHT_SYMTAB_SHNDX;
426 }
427};
428
Petr Hosek79cee9e2017-08-29 02:12:03 +0000429class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000430 MAKE_SEC_WRITER_FRIEND
431
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000432 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000433 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000434
Petr Hosek79cee9e2017-08-29 02:12:03 +0000435protected:
436 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000437 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000438 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000439
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000440 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000441
Petr Hosek79cee9e2017-08-29 02:12:03 +0000442public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000443 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000444 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
445 uint16_t Shndx, uint64_t Sz);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000446 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000447 // An 'empty' symbol table still contains a null symbol.
448 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000449 void setShndxTable(SectionIndexSection *ShndxTable) {
450 SectionIndexTable = ShndxTable;
451 }
452 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000453 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000454 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000455 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000456 void updateSymbols(function_ref<void(Symbol &)> Callable);
457
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000458 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000459 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000460 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000461 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000462 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000463
Petr Hosek79cee9e2017-08-29 02:12:03 +0000464 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000465 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000466 }
467};
468
Petr Hosekd7df9b22017-09-06 23:41:02 +0000469struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000470 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000471 uint64_t Offset;
472 uint64_t Addend;
473 uint32_t Type;
474};
475
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000476// All relocation sections denote relocations to apply to another section.
477// However, some relocation sections use a dynamic symbol table and others use
478// a regular symbol table. Because the types of the two symbol tables differ in
479// our system (because they should behave differently) we can't uniformly
480// represent all relocations with the same base class if we expose an interface
481// that mentions the symbol table type. So we split the two base types into two
482// different classes, one which handles the section the relocation is applied to
483// and another which handles the symbol table type. The symbol table type is
484// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
485class RelocationSectionBase : public SectionBase {
486protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000487 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000488
489public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000490 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000491 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000492
493 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000494 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000495 }
496};
497
498// Takes the symbol table type to use as a parameter so that we can deduplicate
499// that code between the two symbol table types.
500template <class SymTabType>
501class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000502 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000503 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000504
505protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000506 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000507
508public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000509 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000510 void initialize(SectionTableRef SecTable) override;
511 void finalize() override;
512};
513
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000514class RelocationSection
515 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000516 MAKE_SEC_WRITER_FRIEND
517
Petr Hosekd7df9b22017-09-06 23:41:02 +0000518 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000519
Petr Hosekd7df9b22017-09-06 23:41:02 +0000520public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000521 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000522 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000523 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000524 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000525
Petr Hosekd7df9b22017-09-06 23:41:02 +0000526 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000527 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000528 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000529 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000530 }
531};
532
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000533// TODO: The way stripping and groups interact is complicated
534// and still needs to be worked on.
535
536class GroupSection : public SectionBase {
537 MAKE_SEC_WRITER_FRIEND
538 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000539 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000540 ELF::Elf32_Word FlagWord;
541 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000542
543public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000544 // TODO: Contents is present in several classes of the hierarchy.
545 // This needs to be refactored to avoid duplication.
546 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000547
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000548 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
549
550 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000551 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000552 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
553 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
554
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000555 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000556 void accept(SectionVisitor &) const override;
557 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000558 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000559 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000560
561 static bool classof(const SectionBase *S) {
562 return S->Type == ELF::SHT_GROUP;
563 }
564};
565
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000566class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000567public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000568 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000569
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000570 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000571 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000572 }
573};
574
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000575class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000576public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000577 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000578
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000579 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000580 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000581 }
582};
583
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000584class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000585 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000586 MAKE_SEC_WRITER_FRIEND
587
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000588private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000589 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000590
591public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000592 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000593
Jake Ehrlich76e91102018-01-25 22:46:17 +0000594 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000595
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000596 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000597 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000598 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000599 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000600 }
601};
602
Jake Ehrlich76e91102018-01-25 22:46:17 +0000603class GnuDebugLinkSection : public SectionBase {
604 MAKE_SEC_WRITER_FRIEND
605
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000606private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000607 StringRef FileName;
608 uint32_t CRC32;
609
610 void init(StringRef File, StringRef Data);
611
612public:
613 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000614 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000615 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000616};
617
Jake Ehrlich76e91102018-01-25 22:46:17 +0000618class Reader {
619public:
620 virtual ~Reader();
621 virtual std::unique_ptr<Object> create() const = 0;
622};
623
Jake Ehrlich76e91102018-01-25 22:46:17 +0000624using object::Binary;
625using object::ELFFile;
626using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000627using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000628
629template <class ELFT> class ELFBuilder {
630private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000631 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000632 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000633 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000634 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000635
636 const ELFFile<ELFT> &ElfFile;
637 Object &Obj;
638
Jake Ehrlich6452b112018-02-14 23:31:33 +0000639 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000640 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000641 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000642 void initSymbolTable(SymbolTableSection *SymTab);
643 void readSectionHeaders();
644 SectionBase &makeSection(const Elf_Shdr &Shdr);
645
646public:
647 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
648 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
649
650 void build();
651};
652
653class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000654 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000655
656public:
657 ElfType getElfType() const;
658 std::unique_ptr<Object> create() const override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000659 explicit ELFReader(Binary *B) : Bin(B){};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000660};
661
662class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000663private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000664 using SecPtr = std::unique_ptr<SectionBase>;
665 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000666
Petr Hosekc4df10e2017-08-04 21:09:26 +0000667 std::vector<SecPtr> Sections;
668 std::vector<SegPtr> Segments;
669
Petr Hosek05a04cb2017-08-01 00:33:58 +0000670public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000671 template <class T>
672 using Range = iterator_range<
673 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
674
675 template <class T>
676 using ConstRange = iterator_range<pointee_iterator<
677 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
678
Jake Ehrlich6452b112018-02-14 23:31:33 +0000679 // It is often the case that the ELF header and the program header table are
680 // not present in any segment. This could be a problem during file layout,
681 // because other segments may get assigned an offset where either of the
682 // two should reside, which will effectively corrupt the resulting binary.
683 // Other than that we use these segments to track program header offsets
684 // when they may not follow the ELF header.
685 Segment ElfHdrSegment;
686 Segment ProgramHdrSegment;
687
Petr Hosek05a04cb2017-08-01 00:33:58 +0000688 uint8_t Ident[16];
689 uint64_t Entry;
690 uint64_t SHOffset;
691 uint32_t Type;
692 uint32_t Machine;
693 uint32_t Version;
694 uint32_t Flags;
695
Jake Ehrlich76e91102018-01-25 22:46:17 +0000696 StringTableSection *SectionNames = nullptr;
697 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000698 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000699
Aaron Ballman09f46a72018-01-25 21:03:38 +0000700 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000701 SectionTableRef sections() { return SectionTableRef(Sections); }
702 ConstRange<SectionBase> sections() const {
703 return make_pointee_range(Sections);
704 }
705 Range<Segment> segments() { return make_pointee_range(Segments); }
706 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000707
Jake Ehrlich76e91102018-01-25 22:46:17 +0000708 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000709 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000710 template <class T, class... Ts> T &addSection(Ts &&... Args) {
711 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
712 auto Ptr = Sec.get();
713 Sections.emplace_back(std::move(Sec));
714 return *Ptr;
715 }
716 Segment &addSegment(ArrayRef<uint8_t> Data) {
717 Segments.emplace_back(llvm::make_unique<Segment>(Data));
718 return *Segments.back();
719 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000720};
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000721} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000722} // end namespace llvm
723
724#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H