blob: 5b99b8b2be2ed9e386b1e540813a194ab6383e40 [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
Jake Ehrlichf5a43772017-09-25 20:37:28 +000030class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000031class Section;
32class OwnedDataSection;
33class StringTableSection;
34class SymbolTableSection;
35class RelocationSection;
36class DynamicRelocationSection;
37class GnuDebugLinkSection;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +000038class SectionIndexSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000039class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000040class Object;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000041
42class SectionTableRef {
43private:
Jake Ehrlich76e91102018-01-25 22:46:17 +000044 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000045
46public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000047 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
48
49 SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000050 : Sections(Secs) {}
51 SectionTableRef(const SectionTableRef &) = default;
52
Jake Ehrlich76e91102018-01-25 22:46:17 +000053 iterator begin() { return iterator(Sections.data()); }
54 iterator end() { return iterator(Sections.data() + Sections.size()); }
55
Jake Ehrlich0a151bd2018-03-07 19:59:15 +000056 SectionBase *getSection(uint32_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000057
58 template <class T>
Jake Ehrlich0a151bd2018-03-07 19:59:15 +000059 T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000060};
Petr Hosek05a04cb2017-08-01 00:33:58 +000061
Jake Ehrlich76e91102018-01-25 22:46:17 +000062enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
63
64class SectionVisitor {
65public:
66 virtual ~SectionVisitor();
67
68 virtual void visit(const Section &Sec) = 0;
69 virtual void visit(const OwnedDataSection &Sec) = 0;
70 virtual void visit(const StringTableSection &Sec) = 0;
71 virtual void visit(const SymbolTableSection &Sec) = 0;
72 virtual void visit(const RelocationSection &Sec) = 0;
73 virtual void visit(const DynamicRelocationSection &Sec) = 0;
74 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +000075 virtual void visit(const SectionIndexSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000076};
77
78class SectionWriter : public SectionVisitor {
79protected:
80 FileOutputBuffer &Out;
81
82public:
83 virtual ~SectionWriter(){};
84
85 void visit(const Section &Sec) override;
86 void visit(const OwnedDataSection &Sec) override;
87 void visit(const StringTableSection &Sec) override;
88 void visit(const DynamicRelocationSection &Sec) override;
89 virtual void visit(const SymbolTableSection &Sec) override = 0;
90 virtual void visit(const RelocationSection &Sec) override = 0;
91 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +000092 virtual void visit(const SectionIndexSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000093
94 SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {}
95};
96
97template <class ELFT> class ELFSectionWriter : public SectionWriter {
98private:
99 using Elf_Word = typename ELFT::Word;
100 using Elf_Rel = typename ELFT::Rel;
101 using Elf_Rela = typename ELFT::Rela;
102
103public:
104 virtual ~ELFSectionWriter() {}
105 void visit(const SymbolTableSection &Sec) override;
106 void visit(const RelocationSection &Sec) override;
107 void visit(const GnuDebugLinkSection &Sec) override;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000108 void visit(const SectionIndexSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109
110 ELFSectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
111};
112
113#define MAKE_SEC_WRITER_FRIEND \
114 friend class SectionWriter; \
115 template <class ELFT> friend class ELFSectionWriter;
116
117class BinarySectionWriter : public SectionWriter {
118public:
119 virtual ~BinarySectionWriter() {}
120
121 void visit(const SymbolTableSection &Sec) override;
122 void visit(const RelocationSection &Sec) override;
123 void visit(const GnuDebugLinkSection &Sec) override;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000124 void visit(const SectionIndexSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000125 BinarySectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
126};
127
128class Writer {
129protected:
130 StringRef File;
131 Object &Obj;
132 std::unique_ptr<FileOutputBuffer> BufPtr;
133
134 void createBuffer(uint64_t Size);
135
136public:
137 virtual ~Writer();
138
139 virtual void finalize() = 0;
140 virtual void write() = 0;
141
142 Writer(StringRef File, Object &Obj) : File(File), Obj(Obj) {}
143};
144
145template <class ELFT> class ELFWriter : public Writer {
146private:
147 using Elf_Shdr = typename ELFT::Shdr;
148 using Elf_Phdr = typename ELFT::Phdr;
149 using Elf_Ehdr = typename ELFT::Ehdr;
150
151 void writeEhdr();
152 void writePhdr(const Segment &Seg);
153 void writeShdr(const SectionBase &Sec);
154
155 void writePhdrs();
156 void writeShdrs();
157 void writeSectionData();
158
159 void assignOffsets();
160
161 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
162
163 size_t totalSize() const;
164
165public:
166 virtual ~ELFWriter() {}
167 bool WriteSectionHeaders = true;
168
169 void finalize() override;
170 void write() override;
171 ELFWriter(StringRef File, Object &Obj, bool WSH)
172 : Writer(File, Obj), WriteSectionHeaders(WSH) {}
173};
174
175class BinaryWriter : public Writer {
176private:
177 std::unique_ptr<BinarySectionWriter> SecWriter;
178
179 uint64_t TotalSize;
180
181public:
182 ~BinaryWriter() {}
183 void finalize() override;
184 void write() override;
185 BinaryWriter(StringRef File, Object &Obj) : Writer(File, Obj) {}
186};
187
Petr Hosek05a04cb2017-08-01 00:33:58 +0000188class SectionBase {
189public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000190 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000191 Segment *ParentSegment = nullptr;
192 uint64_t HeaderOffset;
193 uint64_t OriginalOffset;
194 uint32_t Index;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000195 bool HasSymbol = false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000196
197 uint64_t Addr = 0;
198 uint64_t Align = 1;
199 uint32_t EntrySize = 0;
200 uint64_t Flags = 0;
201 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000202 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000203 uint64_t NameIndex = 0;
204 uint64_t Offset = 0;
205 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000206 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000207
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000208 virtual ~SectionBase() = default;
209
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000210 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000211 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000212 virtual void removeSectionReferences(const SectionBase *Sec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000213 virtual void accept(SectionVisitor &Visitor) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000214};
215
216class Segment {
217private:
218 struct SectionCompare {
219 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
220 // Some sections might have the same address if one of them is empty. To
221 // fix this we can use the lexicographic ordering on ->Addr and the
222 // address of the actully stored section.
223 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
224 return Lhs < Rhs;
225 return Lhs->OriginalOffset < Rhs->OriginalOffset;
226 }
227 };
228
229 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000230 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000231
232public:
233 uint64_t Align;
234 uint64_t FileSize;
235 uint32_t Flags;
236 uint32_t Index;
237 uint64_t MemSize;
238 uint64_t Offset;
239 uint64_t PAddr;
240 uint64_t Type;
241 uint64_t VAddr;
242
Petr Hosek3f383832017-08-26 01:32:20 +0000243 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000244 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000245
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000246 Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000247 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000248
Petr Hosek05a04cb2017-08-01 00:33:58 +0000249 const SectionBase *firstSection() const {
250 if (!Sections.empty())
251 return *Sections.begin();
252 return nullptr;
253 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000254
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000255 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
256 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000257};
258
259class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000260 MAKE_SEC_WRITER_FRIEND
261
Petr Hosek05a04cb2017-08-01 00:33:58 +0000262private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000263 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000264
265public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000266 Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
267
Jake Ehrlich76e91102018-01-25 22:46:17 +0000268 void accept(SectionVisitor &Visitor) const override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000269};
270
Jake Ehrliche8437de2017-12-19 00:47:30 +0000271class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000272 MAKE_SEC_WRITER_FRIEND
273
Jake Ehrliche8437de2017-12-19 00:47:30 +0000274private:
275 std::vector<uint8_t> Data;
276
277public:
278 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
279 : Data(std::begin(Data), std::end(Data)) {
280 Name = SecName;
281 Type = ELF::SHT_PROGBITS;
282 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000283 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000284 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000285
286 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000287};
288
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000289// There are two types of string tables that can exist, dynamic and not dynamic.
290// In the dynamic case the string table is allocated. Changing a dynamic string
291// table would mean altering virtual addresses and thus the memory image. So
292// dynamic string tables should not have an interface to modify them or
293// reconstruct them. This type lets us reconstruct a string table. To avoid
294// this class being used for dynamic string tables (which has happened) the
295// classof method checks that the particular instance is not allocated. This
296// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000297class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000298 MAKE_SEC_WRITER_FRIEND
299
Petr Hosek05a04cb2017-08-01 00:33:58 +0000300private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000301 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000302
303public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000304 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
305 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306 }
307
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000308 void addString(StringRef Name);
309 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000310 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000311 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000312
Petr Hosek05a04cb2017-08-01 00:33:58 +0000313 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000314 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000315 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000316 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000317 }
318};
319
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000320// Symbols have a st_shndx field that normally stores an index but occasionally
321// stores a different special value. This enum keeps track of what the st_shndx
322// field means. Most of the values are just copies of the special SHN_* values.
323// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
324enum SymbolShndxType {
325 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000326 SYMBOL_ABS = ELF::SHN_ABS,
327 SYMBOL_COMMON = ELF::SHN_COMMON,
328 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
329 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
330 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
331 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000332 SYMBOL_XINDEX = ELF::SHN_XINDEX,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000333};
334
Petr Hosek79cee9e2017-08-29 02:12:03 +0000335struct Symbol {
336 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000337 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000338 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000339 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000340 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000341 uint32_t NameIndex;
342 uint64_t Size;
343 uint8_t Type;
344 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000345 uint8_t Visibility;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000346
347 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000348};
349
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000350class SectionIndexSection : public SectionBase {
351 MAKE_SEC_WRITER_FRIEND
352
353private:
354 std::vector<uint32_t> Indexes;
355 SymbolTableSection *Symbols;
356
357public:
358 virtual ~SectionIndexSection() {}
359 void addIndex(uint32_t Index) {
360 Indexes.push_back(Index);
361 Size += 4;
362 }
363 void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
364 void initialize(SectionTableRef SecTable) override;
365 void finalize() override;
366 void accept(SectionVisitor &Visitor) const override;
367
368 SectionIndexSection() {
369 Name = ".symtab_shndx";
370 OriginalOffset = std::numeric_limits<uint64_t>::max();
371 Align = 4;
372 EntrySize = 4;
373 Type = ELF::SHT_SYMTAB_SHNDX;
374 }
375};
376
Petr Hosek79cee9e2017-08-29 02:12:03 +0000377class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000378 MAKE_SEC_WRITER_FRIEND
379
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000380 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
381
Petr Hosek79cee9e2017-08-29 02:12:03 +0000382protected:
383 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000384 StringTableSection *SymbolNames = nullptr;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000385 SectionIndexSection *SectionIndexTable = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000386
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000387 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000388
Petr Hosek79cee9e2017-08-29 02:12:03 +0000389public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000390 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000391 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
392 uint16_t Shndx, uint64_t Sz);
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000393 void prepareForLayout();
394 void setShndxTable(SectionIndexSection *ShndxTable) { SectionIndexTable = ShndxTable; }
395 const SectionBase *getShndxTable() const { return SectionIndexTable; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000396 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000397 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000398 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000399 void localize(std::function<bool(const Symbol &)> ToLocalize);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000400 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000401 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000402 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000403
Petr Hosek79cee9e2017-08-29 02:12:03 +0000404 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000405 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000406 }
407};
408
Petr Hosekd7df9b22017-09-06 23:41:02 +0000409struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000410 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000411 uint64_t Offset;
412 uint64_t Addend;
413 uint32_t Type;
414};
415
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000416// All relocation sections denote relocations to apply to another section.
417// However, some relocation sections use a dynamic symbol table and others use
418// a regular symbol table. Because the types of the two symbol tables differ in
419// our system (because they should behave differently) we can't uniformly
420// represent all relocations with the same base class if we expose an interface
421// that mentions the symbol table type. So we split the two base types into two
422// different classes, one which handles the section the relocation is applied to
423// and another which handles the symbol table type. The symbol table type is
424// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
425class RelocationSectionBase : public SectionBase {
426protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000427 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000428
429public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000430 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000431 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000432
433 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000434 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000435 }
436};
437
438// Takes the symbol table type to use as a parameter so that we can deduplicate
439// that code between the two symbol table types.
440template <class SymTabType>
441class RelocSectionWithSymtabBase : public RelocationSectionBase {
442private:
443 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000444 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000445
446protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000447 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000448
449public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000450 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000451 void initialize(SectionTableRef SecTable) override;
452 void finalize() override;
453};
454
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000455class RelocationSection
456 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000457 MAKE_SEC_WRITER_FRIEND
458
Petr Hosekd7df9b22017-09-06 23:41:02 +0000459private:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000460 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000461
Petr Hosekd7df9b22017-09-06 23:41:02 +0000462public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000463 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000464 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000465
Petr Hosekd7df9b22017-09-06 23:41:02 +0000466 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000467 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000468 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000469 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000470 }
471};
472
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000473class SectionWithStrTab : public Section {
474private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000475 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000476
477public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000478 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
479
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000480 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000481 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000482 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000483 void finalize() override;
484 static bool classof(const SectionBase *S);
485};
486
487class DynamicSymbolTableSection : public SectionWithStrTab {
488public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000489 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
490
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000491 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000492 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000493 }
494};
495
496class DynamicSection : public SectionWithStrTab {
497public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000498 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
499
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000500 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000501 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000502 }
503};
504
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000505class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000506 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000507 MAKE_SEC_WRITER_FRIEND
508
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000509private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000510 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000511
512public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000513 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
514
Jake Ehrlich76e91102018-01-25 22:46:17 +0000515 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000516
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000517 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000518 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000519 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000520 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000521 }
522};
523
Jake Ehrlich76e91102018-01-25 22:46:17 +0000524class GnuDebugLinkSection : public SectionBase {
525 MAKE_SEC_WRITER_FRIEND
526
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000527private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000528
529 StringRef FileName;
530 uint32_t CRC32;
531
532 void init(StringRef File, StringRef Data);
533
534public:
535 // If we add this section from an external source we can use this ctor.
536 GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000537 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000538};
539
Jake Ehrlich76e91102018-01-25 22:46:17 +0000540class Reader {
541public:
542 virtual ~Reader();
543 virtual std::unique_ptr<Object> create() const = 0;
544};
545
546using object::OwningBinary;
547using object::Binary;
548using object::ELFFile;
549using object::ELFObjectFile;
550
551template <class ELFT> class ELFBuilder {
552private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000553 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000554 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000555 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000556 using Elf_Word = typename ELFT::Word;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000557
558 const ELFFile<ELFT> &ElfFile;
559 Object &Obj;
560
Jake Ehrlich6452b112018-02-14 23:31:33 +0000561 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000562 void readProgramHeaders();
563 void initSymbolTable(SymbolTableSection *SymTab);
564 void readSectionHeaders();
565 SectionBase &makeSection(const Elf_Shdr &Shdr);
566
567public:
568 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
569 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
570
571 void build();
572};
573
574class ELFReader : public Reader {
575private:
Jake Ehrlich9634e182018-01-26 02:01:37 +0000576 std::unique_ptr<Binary> Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000577 std::shared_ptr<MemoryBuffer> Data;
578
579public:
580 ElfType getElfType() const;
581 std::unique_ptr<Object> create() const override;
582 ELFReader(StringRef File);
583};
584
585class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000586private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000587 using SecPtr = std::unique_ptr<SectionBase>;
588 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000589
Jake Ehrlich76e91102018-01-25 22:46:17 +0000590 std::shared_ptr<MemoryBuffer> OwnedData;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000591 std::vector<SecPtr> Sections;
592 std::vector<SegPtr> Segments;
593
Petr Hosek05a04cb2017-08-01 00:33:58 +0000594public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000595 template <class T>
596 using Range = iterator_range<
597 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
598
599 template <class T>
600 using ConstRange = iterator_range<pointee_iterator<
601 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
602
Jake Ehrlich6452b112018-02-14 23:31:33 +0000603 // It is often the case that the ELF header and the program header table are
604 // not present in any segment. This could be a problem during file layout,
605 // because other segments may get assigned an offset where either of the
606 // two should reside, which will effectively corrupt the resulting binary.
607 // Other than that we use these segments to track program header offsets
608 // when they may not follow the ELF header.
609 Segment ElfHdrSegment;
610 Segment ProgramHdrSegment;
611
Petr Hosek05a04cb2017-08-01 00:33:58 +0000612 uint8_t Ident[16];
613 uint64_t Entry;
614 uint64_t SHOffset;
615 uint32_t Type;
616 uint32_t Machine;
617 uint32_t Version;
618 uint32_t Flags;
619
Jake Ehrlich76e91102018-01-25 22:46:17 +0000620 StringTableSection *SectionNames = nullptr;
621 SymbolTableSection *SymbolTable = nullptr;
Jake Ehrlich0a151bd2018-03-07 19:59:15 +0000622 SectionIndexSection *SectionIndexTable = nullptr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000623
624 Object(std::shared_ptr<MemoryBuffer> Data) : OwnedData(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000625 virtual ~Object() = default;
626
Aaron Ballman09f46a72018-01-25 21:03:38 +0000627 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000628 SectionTableRef sections() { return SectionTableRef(Sections); }
629 ConstRange<SectionBase> sections() const {
630 return make_pointee_range(Sections);
631 }
632 Range<Segment> segments() { return make_pointee_range(Segments); }
633 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000634
Jake Ehrlich76e91102018-01-25 22:46:17 +0000635 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
636 template <class T, class... Ts> T &addSection(Ts &&... Args) {
637 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
638 auto Ptr = Sec.get();
639 Sections.emplace_back(std::move(Sec));
640 return *Ptr;
641 }
642 Segment &addSegment(ArrayRef<uint8_t> Data) {
643 Segments.emplace_back(llvm::make_unique<Segment>(Data));
644 return *Segments.back();
645 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000646};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000647
648} // end namespace llvm
649
650#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H