blob: b04b0c1a6415ba904a8c50c9f549471524840a94 [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"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000019#include <cstddef>
20#include <cstdint>
21#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000022#include <memory>
23#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000024#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000025
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000026namespace llvm {
27
28class FileOutputBuffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000029class SectionBase;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000030class Segment;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000031
32class SectionTableRef {
33private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000034 ArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000035
36public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000037 SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000038 : Sections(Secs) {}
39 SectionTableRef(const SectionTableRef &) = default;
40
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000041 SectionBase *getSection(uint16_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000042
43 template <class T>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000044 T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000045};
Petr Hosek05a04cb2017-08-01 00:33:58 +000046
47class SectionBase {
48public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000049 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +000050 Segment *ParentSegment = nullptr;
51 uint64_t HeaderOffset;
52 uint64_t OriginalOffset;
53 uint32_t Index;
54
55 uint64_t Addr = 0;
56 uint64_t Align = 1;
57 uint32_t EntrySize = 0;
58 uint64_t Flags = 0;
59 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000060 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +000061 uint64_t NameIndex = 0;
62 uint64_t Offset = 0;
63 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000064 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +000065
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000066 virtual ~SectionBase() = default;
67
Jake Ehrlichf5a43772017-09-25 20:37:28 +000068 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +000069 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +000070 virtual void removeSectionReferences(const SectionBase *Sec);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000071 template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
72 virtual void writeSection(FileOutputBuffer &Out) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +000073};
74
75class Segment {
76private:
77 struct SectionCompare {
78 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
79 // Some sections might have the same address if one of them is empty. To
80 // fix this we can use the lexicographic ordering on ->Addr and the
81 // address of the actully stored section.
82 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
83 return Lhs < Rhs;
84 return Lhs->OriginalOffset < Rhs->OriginalOffset;
85 }
86 };
87
88 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000089 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +000090
91public:
92 uint64_t Align;
93 uint64_t FileSize;
94 uint32_t Flags;
95 uint32_t Index;
96 uint64_t MemSize;
97 uint64_t Offset;
98 uint64_t PAddr;
99 uint64_t Type;
100 uint64_t VAddr;
101
Petr Hosek3f383832017-08-26 01:32:20 +0000102 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000103 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000104
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000105 Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
106
Petr Hosek05a04cb2017-08-01 00:33:58 +0000107 const SectionBase *firstSection() const {
108 if (!Sections.empty())
109 return *Sections.begin();
110 return nullptr;
111 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000112
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000113 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
114 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000115 template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
116 void writeSegment(FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000117};
118
119class Section : public SectionBase {
120private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000121 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000122
123public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000124 Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
125
126 void writeSection(FileOutputBuffer &Out) const override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000127};
128
Jake Ehrliche8437de2017-12-19 00:47:30 +0000129class OwnedDataSection : public SectionBase {
130private:
131 std::vector<uint8_t> Data;
132
133public:
134 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
135 : Data(std::begin(Data), std::end(Data)) {
136 Name = SecName;
137 Type = ELF::SHT_PROGBITS;
138 Size = Data.size();
139 }
140 void writeSection(FileOutputBuffer &Out) const override;
141};
142
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000143// There are two types of string tables that can exist, dynamic and not dynamic.
144// In the dynamic case the string table is allocated. Changing a dynamic string
145// table would mean altering virtual addresses and thus the memory image. So
146// dynamic string tables should not have an interface to modify them or
147// reconstruct them. This type lets us reconstruct a string table. To avoid
148// this class being used for dynamic string tables (which has happened) the
149// classof method checks that the particular instance is not allocated. This
150// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000151class StringTableSection : public SectionBase {
152private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000153 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000154
155public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000156 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
157 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000158 }
159
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000160 void addString(StringRef Name);
161 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000162 void finalize() override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000163 void writeSection(FileOutputBuffer &Out) const override;
164
Petr Hosek05a04cb2017-08-01 00:33:58 +0000165 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000166 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000167 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000168 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000169 }
170};
171
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000172// Symbols have a st_shndx field that normally stores an index but occasionally
173// stores a different special value. This enum keeps track of what the st_shndx
174// field means. Most of the values are just copies of the special SHN_* values.
175// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
176enum SymbolShndxType {
177 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000178 SYMBOL_ABS = ELF::SHN_ABS,
179 SYMBOL_COMMON = ELF::SHN_COMMON,
180 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
181 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
182 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
183 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000184};
185
Petr Hosek79cee9e2017-08-29 02:12:03 +0000186struct Symbol {
187 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000188 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000189 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000190 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000191 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000192 uint32_t NameIndex;
193 uint64_t Size;
194 uint8_t Type;
195 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000196
197 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000198};
199
200class SymbolTableSection : public SectionBase {
201protected:
202 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000203 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000204
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000205 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000206
Petr Hosek79cee9e2017-08-29 02:12:03 +0000207public:
208 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000209 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000210 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
211 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000212 void addSymbolNames();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000213 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000214 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000215 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000216 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000217 void finalize() override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000218
Petr Hosek79cee9e2017-08-29 02:12:03 +0000219 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000220 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000221 }
222};
223
224// Only writeSection depends on the ELF type so we implement it in a subclass.
225template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000226 void writeSection(FileOutputBuffer &Out) const override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000227};
228
Petr Hosekd7df9b22017-09-06 23:41:02 +0000229struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000230 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000231 uint64_t Offset;
232 uint64_t Addend;
233 uint32_t Type;
234};
235
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000236// All relocation sections denote relocations to apply to another section.
237// However, some relocation sections use a dynamic symbol table and others use
238// a regular symbol table. Because the types of the two symbol tables differ in
239// our system (because they should behave differently) we can't uniformly
240// represent all relocations with the same base class if we expose an interface
241// that mentions the symbol table type. So we split the two base types into two
242// different classes, one which handles the section the relocation is applied to
243// and another which handles the symbol table type. The symbol table type is
244// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
245class RelocationSectionBase : public SectionBase {
246protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000247 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000248
249public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000250 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000251 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000252
253 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000254 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000255 }
256};
257
258// Takes the symbol table type to use as a parameter so that we can deduplicate
259// that code between the two symbol table types.
260template <class SymTabType>
261class RelocSectionWithSymtabBase : public RelocationSectionBase {
262private:
263 SymTabType *Symbols = nullptr;
264
265protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000266 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000267
268public:
269 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000270 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000271 void initialize(SectionTableRef SecTable) override;
272 void finalize() override;
273};
274
275template <class ELFT>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000276class RelocationSection
277 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000278private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000279 using Elf_Rel = typename ELFT::Rel;
280 using Elf_Rela = typename ELFT::Rela;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000281
282 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000283
284 template <class T> void writeRel(T *Buf) const;
285
286public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000287 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000288 void writeSection(FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000289
Petr Hosekd7df9b22017-09-06 23:41:02 +0000290 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000291 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000292 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000293 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000294 }
295};
296
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000297class SectionWithStrTab : public Section {
298private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000299 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000300
301public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000302 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
303
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000304 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000305 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000306 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000307 void finalize() override;
308 static bool classof(const SectionBase *S);
309};
310
311class DynamicSymbolTableSection : public SectionWithStrTab {
312public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000313 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
314
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000315 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000316 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000317 }
318};
319
320class DynamicSection : public SectionWithStrTab {
321public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000322 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
323
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000324 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000325 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000326 }
327};
328
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000329class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000330 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000331private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000332 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000333
334public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000335 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
336
337 void writeSection(FileOutputBuffer &Out) const override;
338
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000339 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000340 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000341 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000342 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000343 }
344};
345
Petr Hosek05a04cb2017-08-01 00:33:58 +0000346template <class ELFT> class Object {
347private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000348 using SecPtr = std::unique_ptr<SectionBase>;
349 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000350
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000351 using Elf_Shdr = typename ELFT::Shdr;
352 using Elf_Ehdr = typename ELFT::Ehdr;
353 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000354
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000355 void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000356 SymbolTableSection *SymTab, SectionTableRef SecTable);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000357 SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000358 const Elf_Shdr &Shdr);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000359 void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
360 SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000361
362protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000363 StringTableSection *SectionNames = nullptr;
364 SymbolTableSection *SymbolTable = nullptr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000365 std::vector<SecPtr> Sections;
366 std::vector<SegPtr> Segments;
367
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000368 void writeHeader(FileOutputBuffer &Out) const;
369 void writeProgramHeaders(FileOutputBuffer &Out) const;
370 void writeSectionData(FileOutputBuffer &Out) const;
371 void writeSectionHeaders(FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000372
373public:
374 uint8_t Ident[16];
375 uint64_t Entry;
376 uint64_t SHOffset;
377 uint32_t Type;
378 uint32_t Machine;
379 uint32_t Version;
380 uint32_t Flags;
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000381 bool WriteSectionHeaders = true;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000382
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000383 Object(const object::ELFObjectFile<ELFT> &Obj);
384 virtual ~Object() = default;
385
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000386 const SymbolTableSection *getSymTab() const { return SymbolTable; }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000387 const SectionBase *getSectionHeaderStrTab() const { return SectionNames; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000388 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000389 void addSection(StringRef SecName, ArrayRef<uint8_t> Data);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000390 virtual size_t totalSize() const = 0;
391 virtual void finalize() = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000392 virtual void write(FileOutputBuffer &Out) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000393};
394
Petr Hosekc4df10e2017-08-04 21:09:26 +0000395template <class ELFT> class ELFObject : public Object<ELFT> {
396private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000397 using SecPtr = std::unique_ptr<SectionBase>;
398 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000399
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000400 using Elf_Shdr = typename ELFT::Shdr;
401 using Elf_Ehdr = typename ELFT::Ehdr;
402 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000403
404 void sortSections();
405 void assignOffsets();
406
407public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000408 ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
409
Petr Hosekc4df10e2017-08-04 21:09:26 +0000410 void finalize() override;
411 size_t totalSize() const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000412 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000413};
414
415template <class ELFT> class BinaryObject : public Object<ELFT> {
416private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000417 using SecPtr = std::unique_ptr<SectionBase>;
418 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000419
420 uint64_t TotalSize;
421
422public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000423 BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
424
Petr Hosekc4df10e2017-08-04 21:09:26 +0000425 void finalize() override;
426 size_t totalSize() const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000427 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000428};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000429
430} // end namespace llvm
431
432#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H