blob: 76748d5fc64115bc57fedb5dbd0d37a4c3a44d51 [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;
107
108public:
109 virtual ~ELFSectionWriter() {}
110 void visit(const SymbolTableSection &Sec) override;
111 void visit(const RelocationSection &Sec) override;
112 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000113 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000114 void visit(const SectionIndexSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000115
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000116 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000117};
118
119#define MAKE_SEC_WRITER_FRIEND \
120 friend class SectionWriter; \
121 template <class ELFT> friend class ELFSectionWriter;
122
123class BinarySectionWriter : public SectionWriter {
124public:
125 virtual ~BinarySectionWriter() {}
126
127 void visit(const SymbolTableSection &Sec) override;
128 void visit(const RelocationSection &Sec) override;
129 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000130 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000131 void visit(const SectionIndexSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000132
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000133 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
134};
135
136// The class Buffer abstracts out the common interface of FileOutputBuffer and
137// WritableMemoryBuffer so that the hierarchy of Writers depends on this
138// abstract interface and doesn't depend on a particular implementation.
139// TODO: refactor the buffer classes in LLVM to enable us to use them here
140// directly.
141class Buffer {
142 StringRef Name;
143
144public:
145 virtual ~Buffer();
146 virtual void allocate(size_t Size) = 0;
147 virtual uint8_t *getBufferStart() = 0;
148 virtual Error commit() = 0;
149
150 explicit Buffer(StringRef Name) : Name(Name) {}
151 StringRef getName() const { return Name; }
152};
153
154class FileBuffer : public Buffer {
155 std::unique_ptr<FileOutputBuffer> Buf;
156
157public:
158 void allocate(size_t Size) override;
159 uint8_t *getBufferStart() override;
160 Error commit() override;
161
162 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
163};
164
165class MemBuffer : public Buffer {
166 std::unique_ptr<WritableMemoryBuffer> Buf;
167
168public:
169 void allocate(size_t Size) override;
170 uint8_t *getBufferStart() override;
171 Error commit() override;
172
173 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
174
175 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000176};
177
178class Writer {
179protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000180 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000181 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000182
183public:
184 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000185 virtual void finalize() = 0;
186 virtual void write() = 0;
187
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000188 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000189};
190
191template <class ELFT> class ELFWriter : public Writer {
192private:
193 using Elf_Shdr = typename ELFT::Shdr;
194 using Elf_Phdr = typename ELFT::Phdr;
195 using Elf_Ehdr = typename ELFT::Ehdr;
196
197 void writeEhdr();
198 void writePhdr(const Segment &Seg);
199 void writeShdr(const SectionBase &Sec);
200
201 void writePhdrs();
202 void writeShdrs();
203 void writeSectionData();
204
205 void assignOffsets();
206
207 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
208
209 size_t totalSize() const;
210
211public:
212 virtual ~ELFWriter() {}
213 bool WriteSectionHeaders = true;
214
215 void finalize() override;
216 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000217 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
218 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000219};
220
221class BinaryWriter : public Writer {
222private:
223 std::unique_ptr<BinarySectionWriter> SecWriter;
224
225 uint64_t TotalSize;
226
227public:
228 ~BinaryWriter() {}
229 void finalize() override;
230 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000231 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000232};
233
Petr Hosek05a04cb2017-08-01 00:33:58 +0000234class SectionBase {
235public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000236 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000237 Segment *ParentSegment = nullptr;
238 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000239 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000240 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000241 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000242
243 uint64_t Addr = 0;
244 uint64_t Align = 1;
245 uint32_t EntrySize = 0;
246 uint64_t Flags = 0;
247 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000248 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000249 uint64_t NameIndex = 0;
250 uint64_t Offset = 0;
251 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000252 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000253
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000254 virtual ~SectionBase() = default;
255
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000256 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000257 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000258 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000259 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000260 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000261 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000262};
263
264class Segment {
265private:
266 struct SectionCompare {
267 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
268 // Some sections might have the same address if one of them is empty. To
269 // fix this we can use the lexicographic ordering on ->Addr and the
270 // address of the actully stored section.
271 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
272 return Lhs < Rhs;
273 return Lhs->OriginalOffset < Rhs->OriginalOffset;
274 }
275 };
276
277 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000278 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000279
280public:
281 uint64_t Align;
282 uint64_t FileSize;
283 uint32_t Flags;
284 uint32_t Index;
285 uint64_t MemSize;
286 uint64_t Offset;
287 uint64_t PAddr;
288 uint64_t Type;
289 uint64_t VAddr;
290
Petr Hosek3f383832017-08-26 01:32:20 +0000291 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000292 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000293
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000294 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000295 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000296
Petr Hosek05a04cb2017-08-01 00:33:58 +0000297 const SectionBase *firstSection() const {
298 if (!Sections.empty())
299 return *Sections.begin();
300 return nullptr;
301 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000302
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000303 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
304 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000305};
306
307class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000308 MAKE_SEC_WRITER_FRIEND
309
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000310 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000311 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000312
313public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000314 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000315
Jake Ehrlich76e91102018-01-25 22:46:17 +0000316 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000317 void removeSectionReferences(const SectionBase *Sec) override;
318 void initialize(SectionTableRef SecTable) override;
319 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000320};
321
Jake Ehrliche8437de2017-12-19 00:47:30 +0000322class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000323 MAKE_SEC_WRITER_FRIEND
324
Jake Ehrliche8437de2017-12-19 00:47:30 +0000325 std::vector<uint8_t> Data;
326
327public:
328 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
329 : Data(std::begin(Data), std::end(Data)) {
330 Name = SecName;
331 Type = ELF::SHT_PROGBITS;
332 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000333 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000334 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000335
336 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000337};
338
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000339// There are two types of string tables that can exist, dynamic and not dynamic.
340// In the dynamic case the string table is allocated. Changing a dynamic string
341// table would mean altering virtual addresses and thus the memory image. So
342// dynamic string tables should not have an interface to modify them or
343// reconstruct them. This type lets us reconstruct a string table. To avoid
344// this class being used for dynamic string tables (which has happened) the
345// classof method checks that the particular instance is not allocated. This
346// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000347class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000348 MAKE_SEC_WRITER_FRIEND
349
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000350 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000351
352public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000353 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
354 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000355 }
356
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000357 void addString(StringRef Name);
358 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000359 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000360 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000361
Petr Hosek05a04cb2017-08-01 00:33:58 +0000362 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000363 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000364 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000365 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000366 }
367};
368
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000369// Symbols have a st_shndx field that normally stores an index but occasionally
370// stores a different special value. This enum keeps track of what the st_shndx
371// field means. Most of the values are just copies of the special SHN_* values.
372// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
373enum SymbolShndxType {
374 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000375 SYMBOL_ABS = ELF::SHN_ABS,
376 SYMBOL_COMMON = ELF::SHN_COMMON,
377 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
378 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
379 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
380 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000381 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000382};
383
Petr Hosek79cee9e2017-08-29 02:12:03 +0000384struct Symbol {
385 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000386 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000387 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000388 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000389 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000390 uint32_t NameIndex;
391 uint64_t Size;
392 uint8_t Type;
393 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000394 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000395 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000396
397 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000398};
399
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000400class SectionIndexSection : public SectionBase {
401 MAKE_SEC_WRITER_FRIEND
402
403private:
404 std::vector<uint32_t> Indexes;
405 SymbolTableSection *Symbols = nullptr;
406
407public:
408 virtual ~SectionIndexSection() {}
409 void addIndex(uint32_t Index) {
410 Indexes.push_back(Index);
411 Size += 4;
412 }
413 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
414 void initialize(SectionTableRef SecTable) override;
415 void finalize() override;
416 void accept(SectionVisitor &Visitor) const override;
417
418 SectionIndexSection() {
419 Name = ".symtab_shndx";
420 Align = 4;
421 EntrySize = 4;
422 Type = ELF::SHT_SYMTAB_SHNDX;
423 }
424};
425
Petr Hosek79cee9e2017-08-29 02:12:03 +0000426class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000427 MAKE_SEC_WRITER_FRIEND
428
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000429 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000430 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000431
Petr Hosek79cee9e2017-08-29 02:12:03 +0000432protected:
433 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000434 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000435 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000436
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000437 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000438
Petr Hosek79cee9e2017-08-29 02:12:03 +0000439public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000440 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000441 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
442 uint16_t Shndx, uint64_t Sz);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000443 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000444 // An 'empty' symbol table still contains a null symbol.
445 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000446 void setShndxTable(SectionIndexSection *ShndxTable) {
447 SectionIndexTable = ShndxTable;
448 }
449 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000450 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000451 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000452 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000453 void updateSymbols(function_ref<void(Symbol &)> Callable);
454
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000455 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000456 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000457 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000458 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000459 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000460
Petr Hosek79cee9e2017-08-29 02:12:03 +0000461 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000462 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000463 }
464};
465
Petr Hosekd7df9b22017-09-06 23:41:02 +0000466struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000467 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000468 uint64_t Offset;
469 uint64_t Addend;
470 uint32_t Type;
471};
472
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000473// All relocation sections denote relocations to apply to another section.
474// However, some relocation sections use a dynamic symbol table and others use
475// a regular symbol table. Because the types of the two symbol tables differ in
476// our system (because they should behave differently) we can't uniformly
477// represent all relocations with the same base class if we expose an interface
478// that mentions the symbol table type. So we split the two base types into two
479// different classes, one which handles the section the relocation is applied to
480// and another which handles the symbol table type. The symbol table type is
481// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
482class RelocationSectionBase : public SectionBase {
483protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000484 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000485
486public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000487 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000488 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000489
490 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000491 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000492 }
493};
494
495// Takes the symbol table type to use as a parameter so that we can deduplicate
496// that code between the two symbol table types.
497template <class SymTabType>
498class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000499 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000500 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000501
502protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000503 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000504
505public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000506 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000507 void initialize(SectionTableRef SecTable) override;
508 void finalize() override;
509};
510
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000511class RelocationSection
512 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000513 MAKE_SEC_WRITER_FRIEND
514
Petr Hosekd7df9b22017-09-06 23:41:02 +0000515 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000516
Petr Hosekd7df9b22017-09-06 23:41:02 +0000517public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000518 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000519 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000520 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000521 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000522
Petr Hosekd7df9b22017-09-06 23:41:02 +0000523 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000524 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000525 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000526 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000527 }
528};
529
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000530// TODO: The way stripping and groups interact is complicated
531// and still needs to be worked on.
532
533class GroupSection : public SectionBase {
534 MAKE_SEC_WRITER_FRIEND
535 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000536 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000537 ELF::Elf32_Word FlagWord;
538 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000539
540public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000541 // TODO: Contents is present in several classes of the hierarchy.
542 // This needs to be refactored to avoid duplication.
543 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000544
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000545 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
546
547 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000548 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000549 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
550 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
551
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000552 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000553 void accept(SectionVisitor &) const override;
554 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000555 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000556 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000557
558 static bool classof(const SectionBase *S) {
559 return S->Type == ELF::SHT_GROUP;
560 }
561};
562
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000563class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000564public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000565 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000566
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000567 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000568 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000569 }
570};
571
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000572class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000573public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000574 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000575
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000576 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000577 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000578 }
579};
580
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000581class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000582 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000583 MAKE_SEC_WRITER_FRIEND
584
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000585private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000586 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000587
588public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000589 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000590
Jake Ehrlich76e91102018-01-25 22:46:17 +0000591 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000592
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000593 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000594 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000595 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000596 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000597 }
598};
599
Jake Ehrlich76e91102018-01-25 22:46:17 +0000600class GnuDebugLinkSection : public SectionBase {
601 MAKE_SEC_WRITER_FRIEND
602
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000603private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000604 StringRef FileName;
605 uint32_t CRC32;
606
607 void init(StringRef File, StringRef Data);
608
609public:
610 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000611 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000612 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000613};
614
Jake Ehrlich76e91102018-01-25 22:46:17 +0000615class Reader {
616public:
617 virtual ~Reader();
618 virtual std::unique_ptr<Object> create() const = 0;
619};
620
Jake Ehrlich76e91102018-01-25 22:46:17 +0000621using object::Binary;
622using object::ELFFile;
623using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000624using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000625
626template <class ELFT> class ELFBuilder {
627private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000628 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000629 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000630 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000631 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000632
633 const ELFFile<ELFT> &ElfFile;
634 Object &Obj;
635
Jake Ehrlich6452b112018-02-14 23:31:33 +0000636 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000637 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000638 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000639 void initSymbolTable(SymbolTableSection *SymTab);
640 void readSectionHeaders();
641 SectionBase &makeSection(const Elf_Shdr &Shdr);
642
643public:
644 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
645 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
646
647 void build();
648};
649
650class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000651 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000652
653public:
654 ElfType getElfType() const;
655 std::unique_ptr<Object> create() const override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000656 explicit ELFReader(Binary *B) : Bin(B){};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000657};
658
659class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000660private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000661 using SecPtr = std::unique_ptr<SectionBase>;
662 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000663
Petr Hosekc4df10e2017-08-04 21:09:26 +0000664 std::vector<SecPtr> Sections;
665 std::vector<SegPtr> Segments;
666
Petr Hosek05a04cb2017-08-01 00:33:58 +0000667public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000668 template <class T>
669 using Range = iterator_range<
670 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
671
672 template <class T>
673 using ConstRange = iterator_range<pointee_iterator<
674 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
675
Jake Ehrlich6452b112018-02-14 23:31:33 +0000676 // It is often the case that the ELF header and the program header table are
677 // not present in any segment. This could be a problem during file layout,
678 // because other segments may get assigned an offset where either of the
679 // two should reside, which will effectively corrupt the resulting binary.
680 // Other than that we use these segments to track program header offsets
681 // when they may not follow the ELF header.
682 Segment ElfHdrSegment;
683 Segment ProgramHdrSegment;
684
Petr Hosek05a04cb2017-08-01 00:33:58 +0000685 uint8_t Ident[16];
686 uint64_t Entry;
687 uint64_t SHOffset;
688 uint32_t Type;
689 uint32_t Machine;
690 uint32_t Version;
691 uint32_t Flags;
692
Jake Ehrlich76e91102018-01-25 22:46:17 +0000693 StringTableSection *SectionNames = nullptr;
694 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000695 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000696
Aaron Ballman09f46a72018-01-25 21:03:38 +0000697 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000698 SectionTableRef sections() { return SectionTableRef(Sections); }
699 ConstRange<SectionBase> sections() const {
700 return make_pointee_range(Sections);
701 }
702 Range<Segment> segments() { return make_pointee_range(Segments); }
703 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000704
Jake Ehrlich76e91102018-01-25 22:46:17 +0000705 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000706 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000707 template <class T, class... Ts> T &addSection(Ts &&... Args) {
708 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
709 auto Ptr = Sec.get();
710 Sections.emplace_back(std::move(Sec));
711 return *Ptr;
712 }
713 Segment &addSegment(ArrayRef<uint8_t> Data) {
714 Segments.emplace_back(llvm::make_unique<Segment>(Data));
715 return *Segments.back();
716 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000717};
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000718} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000719} // end namespace llvm
720
721#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H