blob: b8f45a431e1fd11377b0360d392fccd1a2d7f242 [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 {
29
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000030class Buffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000031class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000032class Section;
33class OwnedDataSection;
34class StringTableSection;
35class SymbolTableSection;
36class RelocationSection;
37class DynamicRelocationSection;
38class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000039class GroupSection;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000040class SectionIndexSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000041class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000042class Object;
Paul Semel4246a462018-05-09 21:36:54 +000043struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000044
45class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000046 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000047
48public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000049 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
50
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000051 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000052 : Sections(Secs) {}
53 SectionTableRef(const SectionTableRef &) = default;
54
Jake Ehrlich76e91102018-01-25 22:46:17 +000055 iterator begin() { return iterator(Sections.data()); }
56 iterator end() { return iterator(Sections.data() + Sections.size()); }
57
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000058 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000059
60 template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000061 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000062};
Petr Hosek05a04cb2017-08-01 00:33:58 +000063
Jake Ehrlich76e91102018-01-25 22:46:17 +000064enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
65
66class SectionVisitor {
67public:
68 virtual ~SectionVisitor();
69
70 virtual void visit(const Section &Sec) = 0;
71 virtual void visit(const OwnedDataSection &Sec) = 0;
72 virtual void visit(const StringTableSection &Sec) = 0;
73 virtual void visit(const SymbolTableSection &Sec) = 0;
74 virtual void visit(const RelocationSection &Sec) = 0;
75 virtual void visit(const DynamicRelocationSection &Sec) = 0;
76 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000077 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000078 virtual void visit(const SectionIndexSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000079};
80
81class SectionWriter : public SectionVisitor {
82protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000083 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000084
85public:
86 virtual ~SectionWriter(){};
87
88 void visit(const Section &Sec) override;
89 void visit(const OwnedDataSection &Sec) override;
90 void visit(const StringTableSection &Sec) override;
91 void visit(const DynamicRelocationSection &Sec) override;
92 virtual void visit(const SymbolTableSection &Sec) override = 0;
93 virtual void visit(const RelocationSection &Sec) override = 0;
94 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000095 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000096 virtual void visit(const SectionIndexSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000097
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000098 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +000099};
100
101template <class ELFT> class ELFSectionWriter : public SectionWriter {
102private:
103 using Elf_Word = typename ELFT::Word;
104 using Elf_Rel = typename ELFT::Rel;
105 using Elf_Rela = typename ELFT::Rela;
106
107public:
108 virtual ~ELFSectionWriter() {}
109 void visit(const SymbolTableSection &Sec) override;
110 void visit(const RelocationSection &Sec) override;
111 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000112 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000113 void visit(const SectionIndexSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000114
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000115 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000116};
117
118#define MAKE_SEC_WRITER_FRIEND \
119 friend class SectionWriter; \
120 template <class ELFT> friend class ELFSectionWriter;
121
122class BinarySectionWriter : public SectionWriter {
123public:
124 virtual ~BinarySectionWriter() {}
125
126 void visit(const SymbolTableSection &Sec) override;
127 void visit(const RelocationSection &Sec) override;
128 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000129 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000130 void visit(const SectionIndexSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000131
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000132 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
133};
134
135// The class Buffer abstracts out the common interface of FileOutputBuffer and
136// WritableMemoryBuffer so that the hierarchy of Writers depends on this
137// abstract interface and doesn't depend on a particular implementation.
138// TODO: refactor the buffer classes in LLVM to enable us to use them here
139// directly.
140class Buffer {
141 StringRef Name;
142
143public:
144 virtual ~Buffer();
145 virtual void allocate(size_t Size) = 0;
146 virtual uint8_t *getBufferStart() = 0;
147 virtual Error commit() = 0;
148
149 explicit Buffer(StringRef Name) : Name(Name) {}
150 StringRef getName() const { return Name; }
151};
152
153class FileBuffer : public Buffer {
154 std::unique_ptr<FileOutputBuffer> Buf;
155
156public:
157 void allocate(size_t Size) override;
158 uint8_t *getBufferStart() override;
159 Error commit() override;
160
161 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
162};
163
164class MemBuffer : public Buffer {
165 std::unique_ptr<WritableMemoryBuffer> Buf;
166
167public:
168 void allocate(size_t Size) override;
169 uint8_t *getBufferStart() override;
170 Error commit() override;
171
172 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
173
174 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000175};
176
177class Writer {
178protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000179 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000180 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000181
182public:
183 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000184 virtual void finalize() = 0;
185 virtual void write() = 0;
186
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000187 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000188};
189
190template <class ELFT> class ELFWriter : public Writer {
191private:
192 using Elf_Shdr = typename ELFT::Shdr;
193 using Elf_Phdr = typename ELFT::Phdr;
194 using Elf_Ehdr = typename ELFT::Ehdr;
195
196 void writeEhdr();
197 void writePhdr(const Segment &Seg);
198 void writeShdr(const SectionBase &Sec);
199
200 void writePhdrs();
201 void writeShdrs();
202 void writeSectionData();
203
204 void assignOffsets();
205
206 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
207
208 size_t totalSize() const;
209
210public:
211 virtual ~ELFWriter() {}
212 bool WriteSectionHeaders = true;
213
214 void finalize() override;
215 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000216 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
217 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000218};
219
220class BinaryWriter : public Writer {
221private:
222 std::unique_ptr<BinarySectionWriter> SecWriter;
223
224 uint64_t TotalSize;
225
226public:
227 ~BinaryWriter() {}
228 void finalize() override;
229 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000230 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000231};
232
Petr Hosek05a04cb2017-08-01 00:33:58 +0000233class SectionBase {
234public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000235 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000236 Segment *ParentSegment = nullptr;
237 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000238 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000239 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000240 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000241
242 uint64_t Addr = 0;
243 uint64_t Align = 1;
244 uint32_t EntrySize = 0;
245 uint64_t Flags = 0;
246 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000247 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000248 uint64_t NameIndex = 0;
249 uint64_t Offset = 0;
250 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000251 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000252
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000253 virtual ~SectionBase() = default;
254
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000255 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000256 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000257 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000258 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000259 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000260 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000261};
262
263class Segment {
264private:
265 struct SectionCompare {
266 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
267 // Some sections might have the same address if one of them is empty. To
268 // fix this we can use the lexicographic ordering on ->Addr and the
269 // address of the actully stored section.
270 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
271 return Lhs < Rhs;
272 return Lhs->OriginalOffset < Rhs->OriginalOffset;
273 }
274 };
275
276 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000277 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000278
279public:
280 uint64_t Align;
281 uint64_t FileSize;
282 uint32_t Flags;
283 uint32_t Index;
284 uint64_t MemSize;
285 uint64_t Offset;
286 uint64_t PAddr;
287 uint64_t Type;
288 uint64_t VAddr;
289
Petr Hosek3f383832017-08-26 01:32:20 +0000290 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000291 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000292
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000293 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000294 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000295
Petr Hosek05a04cb2017-08-01 00:33:58 +0000296 const SectionBase *firstSection() const {
297 if (!Sections.empty())
298 return *Sections.begin();
299 return nullptr;
300 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000301
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000302 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
303 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000304};
305
306class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000307 MAKE_SEC_WRITER_FRIEND
308
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000309 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000310 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000311
312public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000313 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000314
Jake Ehrlich76e91102018-01-25 22:46:17 +0000315 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000316 void removeSectionReferences(const SectionBase *Sec) override;
317 void initialize(SectionTableRef SecTable) override;
318 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000319};
320
Jake Ehrliche8437de2017-12-19 00:47:30 +0000321class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000322 MAKE_SEC_WRITER_FRIEND
323
Jake Ehrliche8437de2017-12-19 00:47:30 +0000324 std::vector<uint8_t> Data;
325
326public:
327 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
328 : Data(std::begin(Data), std::end(Data)) {
329 Name = SecName;
330 Type = ELF::SHT_PROGBITS;
331 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000332 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000333 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000334
335 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000336};
337
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000338// There are two types of string tables that can exist, dynamic and not dynamic.
339// In the dynamic case the string table is allocated. Changing a dynamic string
340// table would mean altering virtual addresses and thus the memory image. So
341// dynamic string tables should not have an interface to modify them or
342// reconstruct them. This type lets us reconstruct a string table. To avoid
343// this class being used for dynamic string tables (which has happened) the
344// classof method checks that the particular instance is not allocated. This
345// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000346class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000347 MAKE_SEC_WRITER_FRIEND
348
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000349 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000350
351public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000352 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
353 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000354 }
355
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000356 void addString(StringRef Name);
357 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000358 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000359 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000360
Petr Hosek05a04cb2017-08-01 00:33:58 +0000361 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000362 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000363 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000364 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000365 }
366};
367
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000368// Symbols have a st_shndx field that normally stores an index but occasionally
369// stores a different special value. This enum keeps track of what the st_shndx
370// field means. Most of the values are just copies of the special SHN_* values.
371// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
372enum SymbolShndxType {
373 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000374 SYMBOL_ABS = ELF::SHN_ABS,
375 SYMBOL_COMMON = ELF::SHN_COMMON,
376 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
377 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
378 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
379 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000380 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000381};
382
Petr Hosek79cee9e2017-08-29 02:12:03 +0000383struct Symbol {
384 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000385 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000386 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000387 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000388 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000389 uint32_t NameIndex;
390 uint64_t Size;
391 uint8_t Type;
392 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000393 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000394 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000395
396 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000397};
398
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000399class SectionIndexSection : public SectionBase {
400 MAKE_SEC_WRITER_FRIEND
401
402private:
403 std::vector<uint32_t> Indexes;
404 SymbolTableSection *Symbols = nullptr;
405
406public:
407 virtual ~SectionIndexSection() {}
408 void addIndex(uint32_t Index) {
409 Indexes.push_back(Index);
410 Size += 4;
411 }
412 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
413 void initialize(SectionTableRef SecTable) override;
414 void finalize() override;
415 void accept(SectionVisitor &Visitor) const override;
416
417 SectionIndexSection() {
418 Name = ".symtab_shndx";
419 Align = 4;
420 EntrySize = 4;
421 Type = ELF::SHT_SYMTAB_SHNDX;
422 }
423};
424
Petr Hosek79cee9e2017-08-29 02:12:03 +0000425class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000426 MAKE_SEC_WRITER_FRIEND
427
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000428 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000429 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000430
Petr Hosek79cee9e2017-08-29 02:12:03 +0000431protected:
432 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000433 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000434 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000435
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000436 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000437
Petr Hosek79cee9e2017-08-29 02:12:03 +0000438public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000439 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000440 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
441 uint16_t Shndx, uint64_t Sz);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000442 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000443 // An 'empty' symbol table still contains a null symbol.
444 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000445 void setShndxTable(SectionIndexSection *ShndxTable) {
446 SectionIndexTable = ShndxTable;
447 }
448 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000449 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000450 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000451 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000452 void updateSymbols(function_ref<void(Symbol &)> Callable);
453
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000454 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000455 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000456 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000457 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000458 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000459
Petr Hosek79cee9e2017-08-29 02:12:03 +0000460 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000461 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000462 }
463};
464
Petr Hosekd7df9b22017-09-06 23:41:02 +0000465struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000466 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000467 uint64_t Offset;
468 uint64_t Addend;
469 uint32_t Type;
470};
471
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000472// All relocation sections denote relocations to apply to another section.
473// However, some relocation sections use a dynamic symbol table and others use
474// a regular symbol table. Because the types of the two symbol tables differ in
475// our system (because they should behave differently) we can't uniformly
476// represent all relocations with the same base class if we expose an interface
477// that mentions the symbol table type. So we split the two base types into two
478// different classes, one which handles the section the relocation is applied to
479// and another which handles the symbol table type. The symbol table type is
480// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
481class RelocationSectionBase : public SectionBase {
482protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000483 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000484
485public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000486 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000487 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000488
489 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000490 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000491 }
492};
493
494// Takes the symbol table type to use as a parameter so that we can deduplicate
495// that code between the two symbol table types.
496template <class SymTabType>
497class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000498 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000499 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000500
501protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000502 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000503
504public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000505 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000506 void initialize(SectionTableRef SecTable) override;
507 void finalize() override;
508};
509
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000510class RelocationSection
511 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000512 MAKE_SEC_WRITER_FRIEND
513
Petr Hosekd7df9b22017-09-06 23:41:02 +0000514 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000515
Petr Hosekd7df9b22017-09-06 23:41:02 +0000516public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000517 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000518 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000519 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000520 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000521
Petr Hosekd7df9b22017-09-06 23:41:02 +0000522 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000523 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000524 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000525 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000526 }
527};
528
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000529// TODO: The way stripping and groups interact is complicated
530// and still needs to be worked on.
531
532class GroupSection : public SectionBase {
533 MAKE_SEC_WRITER_FRIEND
534 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000535 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000536 ELF::Elf32_Word FlagWord;
537 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000538
539public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000540 // TODO: Contents is present in several classes of the hierarchy.
541 // This needs to be refactored to avoid duplication.
542 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000543
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000544 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
545
546 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000547 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000548 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
549 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
550
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000551 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000552 void accept(SectionVisitor &) const override;
553 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000554 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000555 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000556
557 static bool classof(const SectionBase *S) {
558 return S->Type == ELF::SHT_GROUP;
559 }
560};
561
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000562class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000563public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000564 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000565
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000566 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000567 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000568 }
569};
570
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000571class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000572public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000573 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000574
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000575 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000576 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000577 }
578};
579
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000580class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000581 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000582 MAKE_SEC_WRITER_FRIEND
583
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000584private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000585 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000586
587public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000588 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000589
Jake Ehrlich76e91102018-01-25 22:46:17 +0000590 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000591
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000592 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000593 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000594 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000595 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000596 }
597};
598
Jake Ehrlich76e91102018-01-25 22:46:17 +0000599class GnuDebugLinkSection : public SectionBase {
600 MAKE_SEC_WRITER_FRIEND
601
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000602private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000603 StringRef FileName;
604 uint32_t CRC32;
605
606 void init(StringRef File, StringRef Data);
607
608public:
609 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000610 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000611 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000612};
613
Jake Ehrlich76e91102018-01-25 22:46:17 +0000614class Reader {
615public:
616 virtual ~Reader();
617 virtual std::unique_ptr<Object> create() const = 0;
618};
619
Jake Ehrlich76e91102018-01-25 22:46:17 +0000620using object::Binary;
621using object::ELFFile;
622using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000623using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000624
625template <class ELFT> class ELFBuilder {
626private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000627 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000628 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000629 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000630 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000631
632 const ELFFile<ELFT> &ElfFile;
633 Object &Obj;
634
Jake Ehrlich6452b112018-02-14 23:31:33 +0000635 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000636 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000637 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000638 void initSymbolTable(SymbolTableSection *SymTab);
639 void readSectionHeaders();
640 SectionBase &makeSection(const Elf_Shdr &Shdr);
641
642public:
643 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
644 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
645
646 void build();
647};
648
649class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000650 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000651
652public:
653 ElfType getElfType() const;
654 std::unique_ptr<Object> create() const override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000655 explicit ELFReader(Binary *B) : Bin(B){};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000656};
657
658class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000659private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000660 using SecPtr = std::unique_ptr<SectionBase>;
661 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000662
Petr Hosekc4df10e2017-08-04 21:09:26 +0000663 std::vector<SecPtr> Sections;
664 std::vector<SegPtr> Segments;
665
Petr Hosek05a04cb2017-08-01 00:33:58 +0000666public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000667 template <class T>
668 using Range = iterator_range<
669 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
670
671 template <class T>
672 using ConstRange = iterator_range<pointee_iterator<
673 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
674
Jake Ehrlich6452b112018-02-14 23:31:33 +0000675 // It is often the case that the ELF header and the program header table are
676 // not present in any segment. This could be a problem during file layout,
677 // because other segments may get assigned an offset where either of the
678 // two should reside, which will effectively corrupt the resulting binary.
679 // Other than that we use these segments to track program header offsets
680 // when they may not follow the ELF header.
681 Segment ElfHdrSegment;
682 Segment ProgramHdrSegment;
683
Petr Hosek05a04cb2017-08-01 00:33:58 +0000684 uint8_t Ident[16];
685 uint64_t Entry;
686 uint64_t SHOffset;
687 uint32_t Type;
688 uint32_t Machine;
689 uint32_t Version;
690 uint32_t Flags;
691
Jake Ehrlich76e91102018-01-25 22:46:17 +0000692 StringTableSection *SectionNames = nullptr;
693 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000694 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000695
Aaron Ballman09f46a72018-01-25 21:03:38 +0000696 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000697 SectionTableRef sections() { return SectionTableRef(Sections); }
698 ConstRange<SectionBase> sections() const {
699 return make_pointee_range(Sections);
700 }
701 Range<Segment> segments() { return make_pointee_range(Segments); }
702 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000703
Jake Ehrlich76e91102018-01-25 22:46:17 +0000704 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000705 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000706 template <class T, class... Ts> T &addSection(Ts &&... Args) {
707 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
708 auto Ptr = Sec.get();
709 Sections.emplace_back(std::move(Sec));
710 return *Ptr;
711 }
712 Segment &addSegment(ArrayRef<uint8_t> Data) {
713 Segments.emplace_back(llvm::make_unique<Segment>(Data));
714 return *Segments.back();
715 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000716};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000717} // end namespace llvm
718
719#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H