blob: e9a4c35d398c34d8cf1f9051b0456a7453f7f554 [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
Jordan Rupprechtcf676332018-08-17 18:51:11 +000067// This type keeps track of the machine info for various architectures. This
68// lets us map architecture names to ELF types and the e_machine value of the
69// ELF file.
70struct MachineInfo {
71 uint16_t EMachine;
72 bool Is64Bit;
73 bool IsLittleEndian;
74};
75
Jake Ehrlich76e91102018-01-25 22:46:17 +000076class SectionVisitor {
77public:
78 virtual ~SectionVisitor();
79
80 virtual void visit(const Section &Sec) = 0;
81 virtual void visit(const OwnedDataSection &Sec) = 0;
82 virtual void visit(const StringTableSection &Sec) = 0;
83 virtual void visit(const SymbolTableSection &Sec) = 0;
84 virtual void visit(const RelocationSection &Sec) = 0;
85 virtual void visit(const DynamicRelocationSection &Sec) = 0;
86 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000087 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +000088 virtual void visit(const SectionIndexSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000089};
90
91class SectionWriter : public SectionVisitor {
92protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000093 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000094
95public:
96 virtual ~SectionWriter(){};
97
98 void visit(const Section &Sec) override;
99 void visit(const OwnedDataSection &Sec) override;
100 void visit(const StringTableSection &Sec) override;
101 void visit(const DynamicRelocationSection &Sec) override;
102 virtual void visit(const SymbolTableSection &Sec) override = 0;
103 virtual void visit(const RelocationSection &Sec) override = 0;
104 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000105 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000106 virtual void visit(const SectionIndexSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000107
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000108 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109};
110
111template <class ELFT> class ELFSectionWriter : public SectionWriter {
112private:
113 using Elf_Word = typename ELFT::Word;
114 using Elf_Rel = typename ELFT::Rel;
115 using Elf_Rela = typename ELFT::Rela;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000116 using Elf_Sym = typename ELFT::Sym;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000117
118public:
119 virtual ~ELFSectionWriter() {}
120 void visit(const SymbolTableSection &Sec) override;
121 void visit(const RelocationSection &Sec) override;
122 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000123 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000124 void visit(const SectionIndexSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000125
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000126 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000127};
128
129#define MAKE_SEC_WRITER_FRIEND \
130 friend class SectionWriter; \
131 template <class ELFT> friend class ELFSectionWriter;
132
133class BinarySectionWriter : public SectionWriter {
134public:
135 virtual ~BinarySectionWriter() {}
136
137 void visit(const SymbolTableSection &Sec) override;
138 void visit(const RelocationSection &Sec) override;
139 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000140 void visit(const GroupSection &Sec) override;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000141 void visit(const SectionIndexSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000142
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000143 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
144};
145
146// The class Buffer abstracts out the common interface of FileOutputBuffer and
147// WritableMemoryBuffer so that the hierarchy of Writers depends on this
148// abstract interface and doesn't depend on a particular implementation.
149// TODO: refactor the buffer classes in LLVM to enable us to use them here
150// directly.
151class Buffer {
152 StringRef Name;
153
154public:
155 virtual ~Buffer();
156 virtual void allocate(size_t Size) = 0;
157 virtual uint8_t *getBufferStart() = 0;
158 virtual Error commit() = 0;
159
160 explicit Buffer(StringRef Name) : Name(Name) {}
161 StringRef getName() const { return Name; }
162};
163
164class FileBuffer : public Buffer {
165 std::unique_ptr<FileOutputBuffer> Buf;
166
167public:
168 void allocate(size_t Size) override;
169 uint8_t *getBufferStart() override;
170 Error commit() override;
171
172 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
173};
174
175class MemBuffer : public Buffer {
176 std::unique_ptr<WritableMemoryBuffer> Buf;
177
178public:
179 void allocate(size_t Size) override;
180 uint8_t *getBufferStart() override;
181 Error commit() override;
182
183 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
184
185 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000186};
187
188class Writer {
189protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000190 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000191 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000192
193public:
194 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000195 virtual void finalize() = 0;
196 virtual void write() = 0;
197
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000198 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000199};
200
201template <class ELFT> class ELFWriter : public Writer {
202private:
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000203 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000204 using Elf_Shdr = typename ELFT::Shdr;
205 using Elf_Phdr = typename ELFT::Phdr;
206 using Elf_Ehdr = typename ELFT::Ehdr;
207
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000208 void initEhdrSegment();
209
Jake Ehrlich76e91102018-01-25 22:46:17 +0000210 void writeEhdr();
211 void writePhdr(const Segment &Seg);
212 void writeShdr(const SectionBase &Sec);
213
214 void writePhdrs();
215 void writeShdrs();
216 void writeSectionData();
217
218 void assignOffsets();
219
220 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
221
222 size_t totalSize() const;
223
224public:
225 virtual ~ELFWriter() {}
226 bool WriteSectionHeaders = true;
227
228 void finalize() override;
229 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000230 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
231 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000232};
233
234class BinaryWriter : public Writer {
235private:
236 std::unique_ptr<BinarySectionWriter> SecWriter;
237
238 uint64_t TotalSize;
239
240public:
241 ~BinaryWriter() {}
242 void finalize() override;
243 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000244 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000245};
246
Petr Hosek05a04cb2017-08-01 00:33:58 +0000247class SectionBase {
248public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000249 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000250 Segment *ParentSegment = nullptr;
251 uint64_t HeaderOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000252 uint64_t OriginalOffset = std::numeric_limits<uint64_t>::max();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000253 uint32_t Index;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000254 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000255
256 uint64_t Addr = 0;
257 uint64_t Align = 1;
258 uint32_t EntrySize = 0;
259 uint64_t Flags = 0;
260 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000261 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000262 uint64_t NameIndex = 0;
263 uint64_t Offset = 0;
264 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000265 uint64_t Type = ELF::SHT_NULL;
Paul Semela42dec72018-08-09 17:05:21 +0000266 ArrayRef<uint8_t> OriginalData;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000267
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000268 virtual ~SectionBase() = default;
269
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000270 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000271 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000272 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000273 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000274 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000275 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000276};
277
278class Segment {
279private:
280 struct SectionCompare {
281 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
282 // Some sections might have the same address if one of them is empty. To
283 // fix this we can use the lexicographic ordering on ->Addr and the
284 // address of the actully stored section.
285 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
286 return Lhs < Rhs;
287 return Lhs->OriginalOffset < Rhs->OriginalOffset;
288 }
289 };
290
291 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000292 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000293
294public:
295 uint64_t Align;
296 uint64_t FileSize;
297 uint32_t Flags;
298 uint32_t Index;
299 uint64_t MemSize;
300 uint64_t Offset;
301 uint64_t PAddr;
302 uint64_t Type;
303 uint64_t VAddr;
304
Petr Hosek3f383832017-08-26 01:32:20 +0000305 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000306 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000307
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000308 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000309 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000310
Petr Hosek05a04cb2017-08-01 00:33:58 +0000311 const SectionBase *firstSection() const {
312 if (!Sections.empty())
313 return *Sections.begin();
314 return nullptr;
315 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000316
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000317 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
318 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000319};
320
321class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000322 MAKE_SEC_WRITER_FRIEND
323
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000324 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000325 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000326
327public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000328 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000329
Jake Ehrlich76e91102018-01-25 22:46:17 +0000330 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000331 void removeSectionReferences(const SectionBase *Sec) override;
332 void initialize(SectionTableRef SecTable) override;
333 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000334};
335
Jake Ehrliche8437de2017-12-19 00:47:30 +0000336class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000337 MAKE_SEC_WRITER_FRIEND
338
Jake Ehrliche8437de2017-12-19 00:47:30 +0000339 std::vector<uint8_t> Data;
340
341public:
342 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
343 : Data(std::begin(Data), std::end(Data)) {
344 Name = SecName;
345 Type = ELF::SHT_PROGBITS;
346 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000347 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000348 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000349
350 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000351};
352
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000353// There are two types of string tables that can exist, dynamic and not dynamic.
354// In the dynamic case the string table is allocated. Changing a dynamic string
355// table would mean altering virtual addresses and thus the memory image. So
356// dynamic string tables should not have an interface to modify them or
357// reconstruct them. This type lets us reconstruct a string table. To avoid
358// this class being used for dynamic string tables (which has happened) the
359// classof method checks that the particular instance is not allocated. This
360// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000361class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000362 MAKE_SEC_WRITER_FRIEND
363
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000364 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000365
366public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000367 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
368 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000369 }
370
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000371 void addString(StringRef Name);
372 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000373 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000374 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000375
Petr Hosek05a04cb2017-08-01 00:33:58 +0000376 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000377 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000378 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000379 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000380 }
381};
382
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000383// Symbols have a st_shndx field that normally stores an index but occasionally
384// stores a different special value. This enum keeps track of what the st_shndx
385// field means. Most of the values are just copies of the special SHN_* values.
386// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
387enum SymbolShndxType {
388 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000389 SYMBOL_ABS = ELF::SHN_ABS,
390 SYMBOL_COMMON = ELF::SHN_COMMON,
391 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
392 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
393 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
394 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000395 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000396};
397
Petr Hosek79cee9e2017-08-29 02:12:03 +0000398struct Symbol {
399 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000400 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000401 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000402 uint32_t Index;
Paul Semel7a3dc2c2018-08-09 17:49:04 +0000403 std::string Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000404 uint32_t NameIndex;
405 uint64_t Size;
406 uint8_t Type;
407 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000408 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000409 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000410
411 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000412};
413
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000414class SectionIndexSection : public SectionBase {
415 MAKE_SEC_WRITER_FRIEND
416
417private:
418 std::vector<uint32_t> Indexes;
419 SymbolTableSection *Symbols = nullptr;
420
421public:
422 virtual ~SectionIndexSection() {}
423 void addIndex(uint32_t Index) {
424 Indexes.push_back(Index);
425 Size += 4;
426 }
427 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
428 void initialize(SectionTableRef SecTable) override;
429 void finalize() override;
430 void accept(SectionVisitor &Visitor) const override;
431
432 SectionIndexSection() {
433 Name = ".symtab_shndx";
434 Align = 4;
435 EntrySize = 4;
436 Type = ELF::SHT_SYMTAB_SHNDX;
437 }
438};
439
Petr Hosek79cee9e2017-08-29 02:12:03 +0000440class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000441 MAKE_SEC_WRITER_FRIEND
442
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000443 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000444 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000445
Petr Hosek79cee9e2017-08-29 02:12:03 +0000446protected:
447 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000448 StringTableSection *SymbolNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000449 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000450
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000451 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000452
Petr Hosek79cee9e2017-08-29 02:12:03 +0000453public:
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000454 SymbolTableSection() { Type = ELF::SHT_SYMTAB; }
455
456 void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn,
457 uint64_t Value, uint8_t Visibility, uint16_t Shndx,
458 uint64_t Size);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000459 void prepareForLayout();
Paul Semel46201fb2018-06-01 16:19:46 +0000460 // An 'empty' symbol table still contains a null symbol.
461 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000462 void setShndxTable(SectionIndexSection *ShndxTable) {
463 SectionIndexTable = ShndxTable;
464 }
465 const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000466 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000467 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000468 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000469 void updateSymbols(function_ref<void(Symbol &)> Callable);
470
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000471 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000472 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000473 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000474 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000475 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000476
Petr Hosek79cee9e2017-08-29 02:12:03 +0000477 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000478 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000479 }
480};
481
Petr Hosekd7df9b22017-09-06 23:41:02 +0000482struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000483 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000484 uint64_t Offset;
485 uint64_t Addend;
486 uint32_t Type;
487};
488
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000489// All relocation sections denote relocations to apply to another section.
490// However, some relocation sections use a dynamic symbol table and others use
491// a regular symbol table. Because the types of the two symbol tables differ in
492// our system (because they should behave differently) we can't uniformly
493// represent all relocations with the same base class if we expose an interface
494// that mentions the symbol table type. So we split the two base types into two
495// different classes, one which handles the section the relocation is applied to
496// and another which handles the symbol table type. The symbol table type is
497// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
498class RelocationSectionBase : public SectionBase {
499protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000500 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000501
502public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000503 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000504 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000505
506 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000507 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000508 }
509};
510
511// Takes the symbol table type to use as a parameter so that we can deduplicate
512// that code between the two symbol table types.
513template <class SymTabType>
514class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000515 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000516 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000517
518protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000519 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000520
521public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000522 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000523 void initialize(SectionTableRef SecTable) override;
524 void finalize() override;
525};
526
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000527class RelocationSection
528 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000529 MAKE_SEC_WRITER_FRIEND
530
Petr Hosekd7df9b22017-09-06 23:41:02 +0000531 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000532
Petr Hosekd7df9b22017-09-06 23:41:02 +0000533public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000534 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000535 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000536 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000537 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000538
Petr Hosekd7df9b22017-09-06 23:41:02 +0000539 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000540 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000541 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000542 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000543 }
544};
545
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000546// TODO: The way stripping and groups interact is complicated
547// and still needs to be worked on.
548
549class GroupSection : public SectionBase {
550 MAKE_SEC_WRITER_FRIEND
551 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000552 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000553 ELF::Elf32_Word FlagWord;
554 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000555
556public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000557 // TODO: Contents is present in several classes of the hierarchy.
558 // This needs to be refactored to avoid duplication.
559 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000560
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000561 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
562
563 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000564 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000565 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
566 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
567
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000568 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000569 void accept(SectionVisitor &) const override;
570 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000571 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000572 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000573
574 static bool classof(const SectionBase *S) {
575 return S->Type == ELF::SHT_GROUP;
576 }
577};
578
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000579class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000580public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000581 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000582
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000583 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000584 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000585 }
586};
587
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000588class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000589public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000590 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000591
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000592 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000593 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000594 }
595};
596
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000597class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000598 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000599 MAKE_SEC_WRITER_FRIEND
600
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000601private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000602 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000603
604public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000605 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000606
Jake Ehrlich76e91102018-01-25 22:46:17 +0000607 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000608
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000609 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000610 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000611 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000612 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000613 }
614};
615
Jake Ehrlich76e91102018-01-25 22:46:17 +0000616class GnuDebugLinkSection : public SectionBase {
617 MAKE_SEC_WRITER_FRIEND
618
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000619private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000620 StringRef FileName;
621 uint32_t CRC32;
622
623 void init(StringRef File, StringRef Data);
624
625public:
626 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000627 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000628 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000629};
630
Jake Ehrlich76e91102018-01-25 22:46:17 +0000631class Reader {
632public:
633 virtual ~Reader();
634 virtual std::unique_ptr<Object> create() const = 0;
635};
636
Jake Ehrlich76e91102018-01-25 22:46:17 +0000637using object::Binary;
638using object::ELFFile;
639using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000640using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000641
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000642template <class ELFT> class BinaryELFBuilder {
643 using Elf_Sym = typename ELFT::Sym;
644
645 uint16_t EMachine;
646 MemoryBuffer *MemBuf;
647 std::unique_ptr<Object> Obj;
648
649 void initFileHeader();
650 void initHeaderSegment();
651 StringTableSection *addStrTab();
652 SymbolTableSection *addSymTab(StringTableSection *StrTab);
653 void addData(SymbolTableSection *SymTab);
654 void initSections();
655
656public:
657 BinaryELFBuilder(uint16_t EM, MemoryBuffer *MB)
658 : EMachine(EM), MemBuf(MB), Obj(llvm::make_unique<Object>()) {}
659
660 std::unique_ptr<Object> build();
661};
662
Jake Ehrlich76e91102018-01-25 22:46:17 +0000663template <class ELFT> class ELFBuilder {
664private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000665 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000666 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000667 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000668
669 const ELFFile<ELFT> &ElfFile;
670 Object &Obj;
671
Jake Ehrlich6452b112018-02-14 23:31:33 +0000672 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000673 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000674 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000675 void initSymbolTable(SymbolTableSection *SymTab);
676 void readSectionHeaders();
677 SectionBase &makeSection(const Elf_Shdr &Shdr);
678
679public:
680 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
681 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
682
683 void build();
684};
685
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000686class BinaryReader : public Reader {
687 const MachineInfo &MInfo;
688 MemoryBuffer *MemBuf;
689
690public:
691 BinaryReader(const MachineInfo &MI, MemoryBuffer *MB)
692 : MInfo(MI), MemBuf(MB) {}
693 std::unique_ptr<Object> create() const override;
694};
695
Jake Ehrlich76e91102018-01-25 22:46:17 +0000696class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000697 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000698
699public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000700 std::unique_ptr<Object> create() const override;
Jordan Rupprecht6b575392018-08-13 21:30:27 +0000701 explicit ELFReader(Binary *B) : Bin(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000702};
703
704class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000705private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000706 using SecPtr = std::unique_ptr<SectionBase>;
707 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000708
Petr Hosekc4df10e2017-08-04 21:09:26 +0000709 std::vector<SecPtr> Sections;
710 std::vector<SegPtr> Segments;
711
Petr Hosek05a04cb2017-08-01 00:33:58 +0000712public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000713 template <class T>
714 using Range = iterator_range<
715 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
716
717 template <class T>
718 using ConstRange = iterator_range<pointee_iterator<
719 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
720
Jake Ehrlich6452b112018-02-14 23:31:33 +0000721 // It is often the case that the ELF header and the program header table are
722 // not present in any segment. This could be a problem during file layout,
723 // because other segments may get assigned an offset where either of the
724 // two should reside, which will effectively corrupt the resulting binary.
725 // Other than that we use these segments to track program header offsets
726 // when they may not follow the ELF header.
727 Segment ElfHdrSegment;
728 Segment ProgramHdrSegment;
729
Petr Hosek05a04cb2017-08-01 00:33:58 +0000730 uint64_t Entry;
731 uint64_t SHOffset;
732 uint32_t Type;
733 uint32_t Machine;
734 uint32_t Version;
735 uint32_t Flags;
736
Jake Ehrlich76e91102018-01-25 22:46:17 +0000737 StringTableSection *SectionNames = nullptr;
738 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000739 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000740
Aaron Ballman09f46a72018-01-25 21:03:38 +0000741 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000742 SectionTableRef sections() { return SectionTableRef(Sections); }
743 ConstRange<SectionBase> sections() const {
744 return make_pointee_range(Sections);
745 }
746 Range<Segment> segments() { return make_pointee_range(Segments); }
747 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000748
Jake Ehrlich76e91102018-01-25 22:46:17 +0000749 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000750 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000751 template <class T, class... Ts> T &addSection(Ts &&... Args) {
752 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
753 auto Ptr = Sec.get();
754 Sections.emplace_back(std::move(Sec));
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000755 Ptr->Index = Sections.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000756 return *Ptr;
757 }
758 Segment &addSegment(ArrayRef<uint8_t> Data) {
759 Segments.emplace_back(llvm::make_unique<Segment>(Data));
760 return *Segments.back();
761 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000762};
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +0000763} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000764} // end namespace llvm
765
766#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H