blob: 15a286e71c449133dc8c9fec0963777b4b1401b3 [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 Ehrlichdf355942018-01-25 20:24:17 +000019#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlich99482fd2018-01-09 23:00:25 +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 Ehrlichdf355942018-01-25 20:24: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 Ehrlichdf355942018-01-25 20:24:17 +000039class Object;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000040
41class SectionTableRef {
42private:
Jake Ehrlichdf355942018-01-25 20:24:17 +000043 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000044
45public:
Jake Ehrlichdf355942018-01-25 20:24: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 Ehrlichdf355942018-01-25 20:24: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 Ehrlichdf355942018-01-25 20:24: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 template 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 Ehrlichdf355942018-01-25 20:24: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) {}
241
Petr Hosek05a04cb2017-08-01 00:33:58 +0000242 const SectionBase *firstSection() const {
243 if (!Sections.empty())
244 return *Sections.begin();
245 return nullptr;
246 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000247
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000248 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
249 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000250};
251
252class Section : public SectionBase {
Jake Ehrlichdf355942018-01-25 20:24:17 +0000253 MAKE_SEC_WRITER_FRIEND
254
Petr Hosek05a04cb2017-08-01 00:33:58 +0000255private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000256 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000257
258public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000259 Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
260
Jake Ehrlichdf355942018-01-25 20:24:17 +0000261 void accept(SectionVisitor &Visitor) const override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000262};
263
Jake Ehrliche8437de2017-12-19 00:47:30 +0000264class OwnedDataSection : public SectionBase {
Jake Ehrlichdf355942018-01-25 20:24:17 +0000265 MAKE_SEC_WRITER_FRIEND
266
Jake Ehrliche8437de2017-12-19 00:47:30 +0000267private:
268 std::vector<uint8_t> Data;
269
270public:
271 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
272 : Data(std::begin(Data), std::end(Data)) {
273 Name = SecName;
274 Type = ELF::SHT_PROGBITS;
275 Size = Data.size();
Jake Ehrlichdf355942018-01-25 20:24:17 +0000276 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000277 }
Jake Ehrlichdf355942018-01-25 20:24:17 +0000278
279 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000280};
281
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000282// There are two types of string tables that can exist, dynamic and not dynamic.
283// In the dynamic case the string table is allocated. Changing a dynamic string
284// table would mean altering virtual addresses and thus the memory image. So
285// dynamic string tables should not have an interface to modify them or
286// reconstruct them. This type lets us reconstruct a string table. To avoid
287// this class being used for dynamic string tables (which has happened) the
288// classof method checks that the particular instance is not allocated. This
289// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000290class StringTableSection : public SectionBase {
Jake Ehrlichdf355942018-01-25 20:24:17 +0000291 MAKE_SEC_WRITER_FRIEND
292
Petr Hosek05a04cb2017-08-01 00:33:58 +0000293private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000294 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000295
296public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000297 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
298 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000299 }
300
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000301 void addString(StringRef Name);
302 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000303 void finalize() override;
Jake Ehrlichdf355942018-01-25 20:24:17 +0000304 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000305
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000307 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000308 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000309 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000310 }
311};
312
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000313// Symbols have a st_shndx field that normally stores an index but occasionally
314// stores a different special value. This enum keeps track of what the st_shndx
315// field means. Most of the values are just copies of the special SHN_* values.
316// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
317enum SymbolShndxType {
318 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000319 SYMBOL_ABS = ELF::SHN_ABS,
320 SYMBOL_COMMON = ELF::SHN_COMMON,
321 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
322 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
323 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
324 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000325};
326
Petr Hosek79cee9e2017-08-29 02:12:03 +0000327struct Symbol {
328 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000329 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000330 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000331 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000332 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000333 uint32_t NameIndex;
334 uint64_t Size;
335 uint8_t Type;
336 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000337 uint8_t Visibility;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000338
339 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000340};
341
342class SymbolTableSection : public SectionBase {
Jake Ehrlichdf355942018-01-25 20:24:17 +0000343 MAKE_SEC_WRITER_FRIEND
344
Petr Hosek79cee9e2017-08-29 02:12:03 +0000345protected:
346 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000347 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000348
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000349 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000350
Petr Hosek79cee9e2017-08-29 02:12:03 +0000351public:
352 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000353 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000354 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
355 uint16_t Shndx, uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000356 void addSymbolNames();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000357 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000358 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000359 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000360 void localize(std::function<bool(const Symbol &)> ToLocalize);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000361 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000362 void finalize() override;
Jake Ehrlichdf355942018-01-25 20:24:17 +0000363 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000364
Petr Hosek79cee9e2017-08-29 02:12:03 +0000365 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000366 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000367 }
368};
369
Petr Hosekd7df9b22017-09-06 23:41:02 +0000370struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000371 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000372 uint64_t Offset;
373 uint64_t Addend;
374 uint32_t Type;
375};
376
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000377// All relocation sections denote relocations to apply to another section.
378// However, some relocation sections use a dynamic symbol table and others use
379// a regular symbol table. Because the types of the two symbol tables differ in
380// our system (because they should behave differently) we can't uniformly
381// represent all relocations with the same base class if we expose an interface
382// that mentions the symbol table type. So we split the two base types into two
383// different classes, one which handles the section the relocation is applied to
384// and another which handles the symbol table type. The symbol table type is
385// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
386class RelocationSectionBase : public SectionBase {
387protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000388 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000389
390public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000391 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000392 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000393
394 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000395 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000396 }
397};
398
399// Takes the symbol table type to use as a parameter so that we can deduplicate
400// that code between the two symbol table types.
401template <class SymTabType>
402class RelocSectionWithSymtabBase : public RelocationSectionBase {
403private:
404 SymTabType *Symbols = nullptr;
405
406protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000407 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000408
409public:
410 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000411 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000412 void initialize(SectionTableRef SecTable) override;
413 void finalize() override;
414};
415
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000416class RelocationSection
417 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlichdf355942018-01-25 20:24:17 +0000418 MAKE_SEC_WRITER_FRIEND
419
Petr Hosekd7df9b22017-09-06 23:41:02 +0000420private:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000421 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000422
Petr Hosekd7df9b22017-09-06 23:41:02 +0000423public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000424 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlichdf355942018-01-25 20:24:17 +0000425 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000426
Petr Hosekd7df9b22017-09-06 23:41:02 +0000427 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000428 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000429 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000430 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000431 }
432};
433
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000434class SectionWithStrTab : public Section {
435private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000436 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000437
438public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000439 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
440
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000441 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000442 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000443 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000444 void finalize() override;
445 static bool classof(const SectionBase *S);
446};
447
448class DynamicSymbolTableSection : public SectionWithStrTab {
449public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000450 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
451
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000452 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000453 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000454 }
455};
456
457class DynamicSection : public SectionWithStrTab {
458public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000459 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
460
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000461 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000462 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000463 }
464};
465
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000466class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000467 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlichdf355942018-01-25 20:24:17 +0000468 MAKE_SEC_WRITER_FRIEND
469
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000470private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000471 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000472
473public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000474 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
475
Jake Ehrlichdf355942018-01-25 20:24:17 +0000476 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000477
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000478 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000479 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000480 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000481 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000482 }
483};
484
Jake Ehrlichdf355942018-01-25 20:24:17 +0000485class GnuDebugLinkSection : public SectionBase {
486 MAKE_SEC_WRITER_FRIEND
487
Jake Ehrlich99482fd2018-01-09 23:00:25 +0000488private:
Jake Ehrlich99482fd2018-01-09 23:00:25 +0000489
490 StringRef FileName;
491 uint32_t CRC32;
492
493 void init(StringRef File, StringRef Data);
494
495public:
496 // If we add this section from an external source we can use this ctor.
497 GnuDebugLinkSection(StringRef File);
Jake Ehrlichdf355942018-01-25 20:24:17 +0000498 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlich99482fd2018-01-09 23:00:25 +0000499};
500
Jake Ehrlichdf355942018-01-25 20:24:17 +0000501class Reader {
502public:
503 virtual ~Reader();
504 virtual std::unique_ptr<Object> create() const = 0;
505};
506
507using object::OwningBinary;
508using object::Binary;
509using object::ELFFile;
510using object::ELFObjectFile;
511
512template <class ELFT> class ELFBuilder {
513private:
514 using Elf_Shdr = typename ELFT::Shdr;
515
516 const ELFFile<ELFT> &ElfFile;
517 Object &Obj;
518
519 void readProgramHeaders();
520 void initSymbolTable(SymbolTableSection *SymTab);
521 void readSectionHeaders();
522 SectionBase &makeSection(const Elf_Shdr &Shdr);
523
524public:
525 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
526 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
527
528 void build();
529};
530
531class ELFReader : public Reader {
532private:
533 std::unique_ptr<Binary> Binary;
534 std::shared_ptr<MemoryBuffer> Data;
535
536public:
537 ElfType getElfType() const;
538 std::unique_ptr<Object> create() const override;
539 ELFReader(StringRef File);
540};
541
542class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000543private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000544 using SecPtr = std::unique_ptr<SectionBase>;
545 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000546
Jake Ehrlichdf355942018-01-25 20:24:17 +0000547 std::shared_ptr<MemoryBuffer> OwnedData;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000548 std::vector<SecPtr> Sections;
549 std::vector<SegPtr> Segments;
550
Petr Hosek05a04cb2017-08-01 00:33:58 +0000551public:
Jake Ehrlichdf355942018-01-25 20:24:17 +0000552 template <class T>
553 using Range = iterator_range<
554 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
555
556 template <class T>
557 using ConstRange = iterator_range<pointee_iterator<
558 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
559
Petr Hosek05a04cb2017-08-01 00:33:58 +0000560 uint8_t Ident[16];
561 uint64_t Entry;
562 uint64_t SHOffset;
563 uint32_t Type;
564 uint32_t Machine;
565 uint32_t Version;
566 uint32_t Flags;
567
Jake Ehrlichdf355942018-01-25 20:24:17 +0000568 StringTableSection *SectionNames = nullptr;
569 SymbolTableSection *SymbolTable = nullptr;
570
571 Object(std::shared_ptr<MemoryBuffer> Data) : OwnedData(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000572 virtual ~Object() = default;
573
Petr Hosekc4df10e2017-08-04 21:09:26 +0000574 void sortSections();
Jake Ehrlichdf355942018-01-25 20:24:17 +0000575 SectionTableRef sections() { return SectionTableRef(Sections); }
576 ConstRange<SectionBase> sections() const {
577 return make_pointee_range(Sections);
578 }
579 Range<Segment> segments() { return make_pointee_range(Segments); }
580 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000581
Jake Ehrlichdf355942018-01-25 20:24:17 +0000582 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
583 template <class T, class... Ts> T &addSection(Ts &&... Args) {
584 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
585 auto Ptr = Sec.get();
586 Sections.emplace_back(std::move(Sec));
587 return *Ptr;
588 }
589 Segment &addSegment(ArrayRef<uint8_t> Data) {
590 Segments.emplace_back(llvm::make_unique<Segment>(Data));
591 return *Segments.back();
592 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000593};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000594
595} // end namespace llvm
596
597#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H