blob: ba6aea935057b1619bf015dca0d3b7f258ac43d1 [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;
Paul Semela42dec72018-08-09 17:05:21 +0000253 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000254
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000255 virtual ~SectionBase() = default;
256
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000257 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000258 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000259 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000260 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000261 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000262 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000263};
264
265class Segment {
266private:
267 struct SectionCompare {
268 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
269 // Some sections might have the same address if one of them is empty. To
270 // fix this we can use the lexicographic ordering on ->Addr and the
271 // address of the actully stored section.
272 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
273 return Lhs < Rhs;
274 return Lhs->OriginalOffset < Rhs->OriginalOffset;
275 }
276 };
277
278 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000279 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000280
281public:
282 uint64_t Align;
283 uint64_t FileSize;
284 uint32_t Flags;
285 uint32_t Index;
286 uint64_t MemSize;
287 uint64_t Offset;
288 uint64_t PAddr;
289 uint64_t Type;
290 uint64_t VAddr;
291
Petr Hosek3f383832017-08-26 01:32:20 +0000292 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000293 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000294
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000295 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000296 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000297
Petr Hosek05a04cb2017-08-01 00:33:58 +0000298 const SectionBase *firstSection() const {
299 if (!Sections.empty())
300 return *Sections.begin();
301 return nullptr;
302 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000303
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000304 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
305 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306};
307
308class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000309 MAKE_SEC_WRITER_FRIEND
310
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000311 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000312 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000313
314public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000315 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000316
Jake Ehrlich76e91102018-01-25 22:46:17 +0000317 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000318 void removeSectionReferences(const SectionBase *Sec) override;
319 void initialize(SectionTableRef SecTable) override;
320 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000321};
322
Jake Ehrliche8437de2017-12-19 00:47:30 +0000323class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000324 MAKE_SEC_WRITER_FRIEND
325
Jake Ehrliche8437de2017-12-19 00:47:30 +0000326 std::vector<uint8_t> Data;
327
328public:
329 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
330 : Data(std::begin(Data), std::end(Data)) {
331 Name = SecName;
332 Type = ELF::SHT_PROGBITS;
333 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000334 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000335 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000336
337 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000338};
339
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000340// There are two types of string tables that can exist, dynamic and not dynamic.
341// In the dynamic case the string table is allocated. Changing a dynamic string
342// table would mean altering virtual addresses and thus the memory image. So
343// dynamic string tables should not have an interface to modify them or
344// reconstruct them. This type lets us reconstruct a string table. To avoid
345// this class being used for dynamic string tables (which has happened) the
346// classof method checks that the particular instance is not allocated. This
347// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000348class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000349 MAKE_SEC_WRITER_FRIEND
350
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000351 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000352
353public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000354 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
355 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000356 }
357
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000358 void addString(StringRef Name);
359 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000360 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000361 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000362
Petr Hosek05a04cb2017-08-01 00:33:58 +0000363 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000364 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000365 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000366 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000367 }
368};
369
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000370// Symbols have a st_shndx field that normally stores an index but occasionally
371// stores a different special value. This enum keeps track of what the st_shndx
372// field means. Most of the values are just copies of the special SHN_* values.
373// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
374enum SymbolShndxType {
375 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000376 SYMBOL_ABS = ELF::SHN_ABS,
377 SYMBOL_COMMON = ELF::SHN_COMMON,
378 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
379 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
380 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
381 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000382 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000383};
384
Petr Hosek79cee9e2017-08-29 02:12:03 +0000385struct Symbol {
386 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000387 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000388 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000389 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000390 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000391 uint32_t NameIndex;
392 uint64_t Size;
393 uint8_t Type;
394 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000395 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000396 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000397
398 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000399};
400
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000401class SectionIndexSection : public SectionBase {
402 MAKE_SEC_WRITER_FRIEND
403
404private:
405 std::vector<uint32_t> Indexes;
406 SymbolTableSection *Symbols = nullptr;
407
408public:
409 virtual ~SectionIndexSection() {}
410 void addIndex(uint32_t Index) {
411 Indexes.push_back(Index);
412 Size += 4;
413 }
414 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
415 void initialize(SectionTableRef SecTable) override;
416 void finalize() override;
417 void accept(SectionVisitor &Visitor) const override;
418
419 SectionIndexSection() {
420 Name = ".symtab_shndx";
421 Align = 4;
422 EntrySize = 4;
423 Type = ELF::SHT_SYMTAB_SHNDX;
424 }
425};
426
Petr Hosek79cee9e2017-08-29 02:12:03 +0000427class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000428 MAKE_SEC_WRITER_FRIEND
429
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000430 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000431 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000432
Petr Hosek79cee9e2017-08-29 02:12:03 +0000433protected:
434 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000435 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000436 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000437
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000438 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000439
Petr Hosek79cee9e2017-08-29 02:12:03 +0000440public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000441 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000442 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
443 uint16_t Shndx, uint64_t Sz);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000444 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000445 // An 'empty' symbol table still contains a null symbol.
446 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000447 void setShndxTable(SectionIndexSection *ShndxTable) {
448 SectionIndexTable = ShndxTable;
449 }
450 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000451 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000452 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000453 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000454 void updateSymbols(function_ref<void(Symbol &)> Callable);
455
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000456 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000457 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000458 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000459 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000460 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000461
Petr Hosek79cee9e2017-08-29 02:12:03 +0000462 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000463 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000464 }
465};
466
Petr Hosekd7df9b22017-09-06 23:41:02 +0000467struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000468 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000469 uint64_t Offset;
470 uint64_t Addend;
471 uint32_t Type;
472};
473
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000474// All relocation sections denote relocations to apply to another section.
475// However, some relocation sections use a dynamic symbol table and others use
476// a regular symbol table. Because the types of the two symbol tables differ in
477// our system (because they should behave differently) we can't uniformly
478// represent all relocations with the same base class if we expose an interface
479// that mentions the symbol table type. So we split the two base types into two
480// different classes, one which handles the section the relocation is applied to
481// and another which handles the symbol table type. The symbol table type is
482// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
483class RelocationSectionBase : public SectionBase {
484protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000485 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000486
487public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000488 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000489 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000490
491 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000492 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000493 }
494};
495
496// Takes the symbol table type to use as a parameter so that we can deduplicate
497// that code between the two symbol table types.
498template <class SymTabType>
499class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000500 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000501 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000502
503protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000504 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000505
506public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000507 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000508 void initialize(SectionTableRef SecTable) override;
509 void finalize() override;
510};
511
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000512class RelocationSection
513 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000514 MAKE_SEC_WRITER_FRIEND
515
Petr Hosekd7df9b22017-09-06 23:41:02 +0000516 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000517
Petr Hosekd7df9b22017-09-06 23:41:02 +0000518public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000519 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000520 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000521 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000522 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000523
Petr Hosekd7df9b22017-09-06 23:41:02 +0000524 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000525 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000526 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000527 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000528 }
529};
530
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000531// TODO: The way stripping and groups interact is complicated
532// and still needs to be worked on.
533
534class GroupSection : public SectionBase {
535 MAKE_SEC_WRITER_FRIEND
536 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000537 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000538 ELF::Elf32_Word FlagWord;
539 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000540
541public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000542 // TODO: Contents is present in several classes of the hierarchy.
543 // This needs to be refactored to avoid duplication.
544 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000545
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000546 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
547
548 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000549 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000550 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
551 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
552
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000553 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000554 void accept(SectionVisitor &) const override;
555 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000556 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000557 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000558
559 static bool classof(const SectionBase *S) {
560 return S->Type == ELF::SHT_GROUP;
561 }
562};
563
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000564class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000565public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000566 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000567
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000568 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000569 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000570 }
571};
572
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000573class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000574public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000575 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000576
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000577 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000578 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000579 }
580};
581
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000582class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000583 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000584 MAKE_SEC_WRITER_FRIEND
585
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000586private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000587 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000588
589public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000590 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000591
Jake Ehrlich76e91102018-01-25 22:46:17 +0000592 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000593
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000594 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000595 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000596 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000597 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000598 }
599};
600
Jake Ehrlich76e91102018-01-25 22:46:17 +0000601class GnuDebugLinkSection : public SectionBase {
602 MAKE_SEC_WRITER_FRIEND
603
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000604private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000605 StringRef FileName;
606 uint32_t CRC32;
607
608 void init(StringRef File, StringRef Data);
609
610public:
611 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000612 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000613 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000614};
615
Jake Ehrlich76e91102018-01-25 22:46:17 +0000616class Reader {
617public:
618 virtual ~Reader();
619 virtual std::unique_ptr<Object> create() const = 0;
620};
621
Jake Ehrlich76e91102018-01-25 22:46:17 +0000622using object::Binary;
623using object::ELFFile;
624using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000625using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000626
627template <class ELFT> class ELFBuilder {
628private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000629 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000630 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000631 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000632 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000633
634 const ELFFile<ELFT> &ElfFile;
635 Object &Obj;
636
Jake Ehrlich6452b112018-02-14 23:31:33 +0000637 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000638 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000639 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000640 void initSymbolTable(SymbolTableSection *SymTab);
641 void readSectionHeaders();
642 SectionBase &makeSection(const Elf_Shdr &Shdr);
643
644public:
645 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
646 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
647
648 void build();
649};
650
651class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000652 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000653
654public:
655 ElfType getElfType() const;
656 std::unique_ptr<Object> create() const override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000657 explicit ELFReader(Binary *B) : Bin(B){};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000658};
659
660class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000661private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000662 using SecPtr = std::unique_ptr<SectionBase>;
663 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000664
Petr Hosekc4df10e2017-08-04 21:09:26 +0000665 std::vector<SecPtr> Sections;
666 std::vector<SegPtr> Segments;
667
Petr Hosek05a04cb2017-08-01 00:33:58 +0000668public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000669 template <class T>
670 using Range = iterator_range<
671 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
672
673 template <class T>
674 using ConstRange = iterator_range<pointee_iterator<
675 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
676
Jake Ehrlich6452b112018-02-14 23:31:33 +0000677 // It is often the case that the ELF header and the program header table are
678 // not present in any segment. This could be a problem during file layout,
679 // because other segments may get assigned an offset where either of the
680 // two should reside, which will effectively corrupt the resulting binary.
681 // Other than that we use these segments to track program header offsets
682 // when they may not follow the ELF header.
683 Segment ElfHdrSegment;
684 Segment ProgramHdrSegment;
685
Petr Hosek05a04cb2017-08-01 00:33:58 +0000686 uint8_t Ident[16];
687 uint64_t Entry;
688 uint64_t SHOffset;
689 uint32_t Type;
690 uint32_t Machine;
691 uint32_t Version;
692 uint32_t Flags;
693
Jake Ehrlich76e91102018-01-25 22:46:17 +0000694 StringTableSection *SectionNames = nullptr;
695 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000696 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000697
Aaron Ballman09f46a72018-01-25 21:03:38 +0000698 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000699 SectionTableRef sections() { return SectionTableRef(Sections); }
700 ConstRange<SectionBase> sections() const {
701 return make_pointee_range(Sections);
702 }
703 Range<Segment> segments() { return make_pointee_range(Segments); }
704 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000705
Jake Ehrlich76e91102018-01-25 22:46:17 +0000706 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000707 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000708 template <class T, class... Ts> T &addSection(Ts &&... Args) {
709 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
710 auto Ptr = Sec.get();
711 Sections.emplace_back(std::move(Sec));
712 return *Ptr;
713 }
714 Segment &addSegment(ArrayRef<uint8_t> Data) {
715 Segments.emplace_back(llvm::make_unique<Segment>(Data));
716 return *Segments.back();
717 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000718};
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000719} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000720} // end namespace llvm
721
722#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H