blob: 88357ad82c286510c3b583b29509f875d149da75 [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;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000038class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000039class Object;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000040
41class SectionTableRef {
42private:
Jake Ehrlich76e91102018-01-25 22:46:17 +000043 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000044
45public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000046 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
47
48 SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000049 : Sections(Secs) {}
50 SectionTableRef(const SectionTableRef &) = default;
51
Jake Ehrlich76e91102018-01-25 22:46:17 +000052 iterator begin() { return iterator(Sections.data()); }
53 iterator end() { return iterator(Sections.data() + Sections.size()); }
54
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000055 SectionBase *getSection(uint16_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000056
57 template <class T>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000058 T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000059};
Petr Hosek05a04cb2017-08-01 00:33:58 +000060
Jake Ehrlich76e91102018-01-25 22:46:17 +000061enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
62
63class SectionVisitor {
64public:
65 virtual ~SectionVisitor();
66
67 virtual void visit(const Section &Sec) = 0;
68 virtual void visit(const OwnedDataSection &Sec) = 0;
69 virtual void visit(const StringTableSection &Sec) = 0;
70 virtual void visit(const SymbolTableSection &Sec) = 0;
71 virtual void visit(const RelocationSection &Sec) = 0;
72 virtual void visit(const DynamicRelocationSection &Sec) = 0;
73 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
74};
75
76class SectionWriter : public SectionVisitor {
77protected:
78 FileOutputBuffer &Out;
79
80public:
81 virtual ~SectionWriter(){};
82
83 void visit(const Section &Sec) override;
84 void visit(const OwnedDataSection &Sec) override;
85 void visit(const StringTableSection &Sec) override;
86 void visit(const DynamicRelocationSection &Sec) override;
87 virtual void visit(const SymbolTableSection &Sec) override = 0;
88 virtual void visit(const RelocationSection &Sec) override = 0;
89 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
90
91 SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {}
92};
93
94template <class ELFT> class ELFSectionWriter : public SectionWriter {
95private:
96 using Elf_Word = typename ELFT::Word;
97 using Elf_Rel = typename ELFT::Rel;
98 using Elf_Rela = typename ELFT::Rela;
99
100public:
101 virtual ~ELFSectionWriter() {}
102 void visit(const SymbolTableSection &Sec) override;
103 void visit(const RelocationSection &Sec) override;
104 void visit(const GnuDebugLinkSection &Sec) override;
105
106 ELFSectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
107};
108
109#define MAKE_SEC_WRITER_FRIEND \
110 friend class SectionWriter; \
111 template <class ELFT> friend class ELFSectionWriter;
112
113class BinarySectionWriter : public SectionWriter {
114public:
115 virtual ~BinarySectionWriter() {}
116
117 void visit(const SymbolTableSection &Sec) override;
118 void visit(const RelocationSection &Sec) override;
119 void visit(const GnuDebugLinkSection &Sec) override;
120 BinarySectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
121};
122
123class Writer {
124protected:
125 StringRef File;
126 Object &Obj;
127 std::unique_ptr<FileOutputBuffer> BufPtr;
128
129 void createBuffer(uint64_t Size);
130
131public:
132 virtual ~Writer();
133
134 virtual void finalize() = 0;
135 virtual void write() = 0;
136
137 Writer(StringRef File, Object &Obj) : File(File), Obj(Obj) {}
138};
139
140template <class ELFT> class ELFWriter : public Writer {
141private:
142 using Elf_Shdr = typename ELFT::Shdr;
143 using Elf_Phdr = typename ELFT::Phdr;
144 using Elf_Ehdr = typename ELFT::Ehdr;
145
146 void writeEhdr();
147 void writePhdr(const Segment &Seg);
148 void writeShdr(const SectionBase &Sec);
149
150 void writePhdrs();
151 void writeShdrs();
152 void writeSectionData();
153
154 void assignOffsets();
155
156 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
157
158 size_t totalSize() const;
159
160public:
161 virtual ~ELFWriter() {}
162 bool WriteSectionHeaders = true;
163
164 void finalize() override;
165 void write() override;
166 ELFWriter(StringRef File, Object &Obj, bool WSH)
167 : Writer(File, Obj), WriteSectionHeaders(WSH) {}
168};
169
170class BinaryWriter : public Writer {
171private:
172 std::unique_ptr<BinarySectionWriter> SecWriter;
173
174 uint64_t TotalSize;
175
176public:
177 ~BinaryWriter() {}
178 void finalize() override;
179 void write() override;
180 BinaryWriter(StringRef File, Object &Obj) : Writer(File, Obj) {}
181};
182
Petr Hosek05a04cb2017-08-01 00:33:58 +0000183class SectionBase {
184public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000185 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000186 Segment *ParentSegment = nullptr;
187 uint64_t HeaderOffset;
188 uint64_t OriginalOffset;
189 uint32_t Index;
190
191 uint64_t Addr = 0;
192 uint64_t Align = 1;
193 uint32_t EntrySize = 0;
194 uint64_t Flags = 0;
195 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000196 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000197 uint64_t NameIndex = 0;
198 uint64_t Offset = 0;
199 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000200 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000201
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000202 virtual ~SectionBase() = default;
203
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000204 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000205 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000206 virtual void removeSectionReferences(const SectionBase *Sec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000207 virtual void accept(SectionVisitor &Visitor) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000208};
209
210class Segment {
211private:
212 struct SectionCompare {
213 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
214 // Some sections might have the same address if one of them is empty. To
215 // fix this we can use the lexicographic ordering on ->Addr and the
216 // address of the actully stored section.
217 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
218 return Lhs < Rhs;
219 return Lhs->OriginalOffset < Rhs->OriginalOffset;
220 }
221 };
222
223 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000224 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000225
226public:
227 uint64_t Align;
228 uint64_t FileSize;
229 uint32_t Flags;
230 uint32_t Index;
231 uint64_t MemSize;
232 uint64_t Offset;
233 uint64_t PAddr;
234 uint64_t Type;
235 uint64_t VAddr;
236
Petr Hosek3f383832017-08-26 01:32:20 +0000237 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000238 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000239
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000240 Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000241 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000242
Petr Hosek05a04cb2017-08-01 00:33:58 +0000243 const SectionBase *firstSection() const {
244 if (!Sections.empty())
245 return *Sections.begin();
246 return nullptr;
247 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000248
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000249 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
250 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000251};
252
253class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000254 MAKE_SEC_WRITER_FRIEND
255
Petr Hosek05a04cb2017-08-01 00:33:58 +0000256private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000257 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000258
259public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000260 Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
261
Jake Ehrlich76e91102018-01-25 22:46:17 +0000262 void accept(SectionVisitor &Visitor) const override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000263};
264
Jake Ehrliche8437de2017-12-19 00:47:30 +0000265class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000266 MAKE_SEC_WRITER_FRIEND
267
Jake Ehrliche8437de2017-12-19 00:47:30 +0000268private:
269 std::vector<uint8_t> Data;
270
271public:
272 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
273 : Data(std::begin(Data), std::end(Data)) {
274 Name = SecName;
275 Type = ELF::SHT_PROGBITS;
276 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000277 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000278 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000279
280 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000281};
282
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000283// There are two types of string tables that can exist, dynamic and not dynamic.
284// In the dynamic case the string table is allocated. Changing a dynamic string
285// table would mean altering virtual addresses and thus the memory image. So
286// dynamic string tables should not have an interface to modify them or
287// reconstruct them. This type lets us reconstruct a string table. To avoid
288// this class being used for dynamic string tables (which has happened) the
289// classof method checks that the particular instance is not allocated. This
290// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000291class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000292 MAKE_SEC_WRITER_FRIEND
293
Petr Hosek05a04cb2017-08-01 00:33:58 +0000294private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000295 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000296
297public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000298 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
299 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000300 }
301
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000302 void addString(StringRef Name);
303 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000304 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000305 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000306
Petr Hosek05a04cb2017-08-01 00:33:58 +0000307 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000308 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000309 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000310 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000311 }
312};
313
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000314// Symbols have a st_shndx field that normally stores an index but occasionally
315// stores a different special value. This enum keeps track of what the st_shndx
316// field means. Most of the values are just copies of the special SHN_* values.
317// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
318enum SymbolShndxType {
319 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000320 SYMBOL_ABS = ELF::SHN_ABS,
321 SYMBOL_COMMON = ELF::SHN_COMMON,
322 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
323 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
324 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
325 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000326};
327
Petr Hosek79cee9e2017-08-29 02:12:03 +0000328struct Symbol {
329 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000330 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000331 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000332 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000333 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000334 uint32_t NameIndex;
335 uint64_t Size;
336 uint8_t Type;
337 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000338 uint8_t Visibility;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000339
340 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000341};
342
343class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000344 MAKE_SEC_WRITER_FRIEND
345
Petr Hosek79cee9e2017-08-29 02:12:03 +0000346protected:
347 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000348 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000349
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000350 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000351
Petr Hosek79cee9e2017-08-29 02:12:03 +0000352public:
353 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000354 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000355 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
356 uint16_t Shndx, uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000357 void addSymbolNames();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000358 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000359 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000360 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000361 void localize(std::function<bool(const Symbol &)> ToLocalize);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000362 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000363 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000364 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000365
Petr Hosek79cee9e2017-08-29 02:12:03 +0000366 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000367 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000368 }
369};
370
Petr Hosekd7df9b22017-09-06 23:41:02 +0000371struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000372 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000373 uint64_t Offset;
374 uint64_t Addend;
375 uint32_t Type;
376};
377
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000378// All relocation sections denote relocations to apply to another section.
379// However, some relocation sections use a dynamic symbol table and others use
380// a regular symbol table. Because the types of the two symbol tables differ in
381// our system (because they should behave differently) we can't uniformly
382// represent all relocations with the same base class if we expose an interface
383// that mentions the symbol table type. So we split the two base types into two
384// different classes, one which handles the section the relocation is applied to
385// and another which handles the symbol table type. The symbol table type is
386// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
387class RelocationSectionBase : public SectionBase {
388protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000389 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000390
391public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000392 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000393 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000394
395 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000396 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000397 }
398};
399
400// Takes the symbol table type to use as a parameter so that we can deduplicate
401// that code between the two symbol table types.
402template <class SymTabType>
403class RelocSectionWithSymtabBase : public RelocationSectionBase {
404private:
405 SymTabType *Symbols = nullptr;
406
407protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000408 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000409
410public:
411 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000412 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000413 void initialize(SectionTableRef SecTable) override;
414 void finalize() override;
415};
416
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000417class RelocationSection
418 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000419 MAKE_SEC_WRITER_FRIEND
420
Petr Hosekd7df9b22017-09-06 23:41:02 +0000421private:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000422 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000423
Petr Hosekd7df9b22017-09-06 23:41:02 +0000424public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000425 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000426 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000427
Petr Hosekd7df9b22017-09-06 23:41:02 +0000428 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000429 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000430 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000431 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000432 }
433};
434
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000435class SectionWithStrTab : public Section {
436private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000437 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000438
439public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000440 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
441
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000442 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000443 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000444 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000445 void finalize() override;
446 static bool classof(const SectionBase *S);
447};
448
449class DynamicSymbolTableSection : public SectionWithStrTab {
450public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000451 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
452
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000453 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000454 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000455 }
456};
457
458class DynamicSection : public SectionWithStrTab {
459public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000460 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
461
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000462 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000463 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000464 }
465};
466
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000467class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000468 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000469 MAKE_SEC_WRITER_FRIEND
470
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000471private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000472 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000473
474public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000475 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
476
Jake Ehrlich76e91102018-01-25 22:46:17 +0000477 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000478
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000479 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000480 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000481 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000482 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000483 }
484};
485
Jake Ehrlich76e91102018-01-25 22:46:17 +0000486class GnuDebugLinkSection : public SectionBase {
487 MAKE_SEC_WRITER_FRIEND
488
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000489private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000490
491 StringRef FileName;
492 uint32_t CRC32;
493
494 void init(StringRef File, StringRef Data);
495
496public:
497 // If we add this section from an external source we can use this ctor.
498 GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000499 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000500};
501
Jake Ehrlich76e91102018-01-25 22:46:17 +0000502class Reader {
503public:
504 virtual ~Reader();
505 virtual std::unique_ptr<Object> create() const = 0;
506};
507
508using object::OwningBinary;
509using object::Binary;
510using object::ELFFile;
511using object::ELFObjectFile;
512
513template <class ELFT> class ELFBuilder {
514private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000515 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000516 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000517 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000518
519 const ELFFile<ELFT> &ElfFile;
520 Object &Obj;
521
Jake Ehrlich6452b112018-02-14 23:31:33 +0000522 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000523 void readProgramHeaders();
524 void initSymbolTable(SymbolTableSection *SymTab);
525 void readSectionHeaders();
526 SectionBase &makeSection(const Elf_Shdr &Shdr);
527
528public:
529 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
530 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
531
532 void build();
533};
534
535class ELFReader : public Reader {
536private:
Jake Ehrlich9634e182018-01-26 02:01:37 +0000537 std::unique_ptr<Binary> Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000538 std::shared_ptr<MemoryBuffer> Data;
539
540public:
541 ElfType getElfType() const;
542 std::unique_ptr<Object> create() const override;
543 ELFReader(StringRef File);
544};
545
546class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000547private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000548 using SecPtr = std::unique_ptr<SectionBase>;
549 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000550
Jake Ehrlich76e91102018-01-25 22:46:17 +0000551 std::shared_ptr<MemoryBuffer> OwnedData;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000552 std::vector<SecPtr> Sections;
553 std::vector<SegPtr> Segments;
554
Petr Hosek05a04cb2017-08-01 00:33:58 +0000555public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000556 template <class T>
557 using Range = iterator_range<
558 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
559
560 template <class T>
561 using ConstRange = iterator_range<pointee_iterator<
562 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
563
Jake Ehrlich6452b112018-02-14 23:31:33 +0000564 // It is often the case that the ELF header and the program header table are
565 // not present in any segment. This could be a problem during file layout,
566 // because other segments may get assigned an offset where either of the
567 // two should reside, which will effectively corrupt the resulting binary.
568 // Other than that we use these segments to track program header offsets
569 // when they may not follow the ELF header.
570 Segment ElfHdrSegment;
571 Segment ProgramHdrSegment;
572
Petr Hosek05a04cb2017-08-01 00:33:58 +0000573 uint8_t Ident[16];
574 uint64_t Entry;
575 uint64_t SHOffset;
576 uint32_t Type;
577 uint32_t Machine;
578 uint32_t Version;
579 uint32_t Flags;
580
Jake Ehrlich76e91102018-01-25 22:46:17 +0000581 StringTableSection *SectionNames = nullptr;
582 SymbolTableSection *SymbolTable = nullptr;
583
584 Object(std::shared_ptr<MemoryBuffer> Data) : OwnedData(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000585 virtual ~Object() = default;
586
Aaron Ballman09f46a72018-01-25 21:03:38 +0000587 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000588 SectionTableRef sections() { return SectionTableRef(Sections); }
589 ConstRange<SectionBase> sections() const {
590 return make_pointee_range(Sections);
591 }
592 Range<Segment> segments() { return make_pointee_range(Segments); }
593 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000594
Jake Ehrlich76e91102018-01-25 22:46:17 +0000595 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
596 template <class T, class... Ts> T &addSection(Ts &&... Args) {
597 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
598 auto Ptr = Sec.get();
599 Sections.emplace_back(std::move(Sec));
600 return *Ptr;
601 }
602 Segment &addSegment(ArrayRef<uint8_t> Data) {
603 Segments.emplace_back(llvm::make_unique<Segment>(Data));
604 return *Segments.back();
605 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000606};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000607
608} // end namespace llvm
609
610#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H