blob: 9f98c04ad9bb92b166ee02833741e5cf9e44476b [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 Ehrlich70bd75f2017-10-10 21:28:22 +0000129// There are two types of string tables that can exist, dynamic and not dynamic.
130// In the dynamic case the string table is allocated. Changing a dynamic string
131// table would mean altering virtual addresses and thus the memory image. So
132// dynamic string tables should not have an interface to modify them or
133// reconstruct them. This type lets us reconstruct a string table. To avoid
134// this class being used for dynamic string tables (which has happened) the
135// classof method checks that the particular instance is not allocated. This
136// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000137class StringTableSection : public SectionBase {
138private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000139 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000140
141public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000142 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
143 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000144 }
145
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000146 void addString(StringRef Name);
147 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000148 void finalize() override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000149 void writeSection(FileOutputBuffer &Out) const override;
150
Petr Hosek05a04cb2017-08-01 00:33:58 +0000151 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000152 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000153 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000154 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000155 }
156};
157
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000158// Symbols have a st_shndx field that normally stores an index but occasionally
159// stores a different special value. This enum keeps track of what the st_shndx
160// field means. Most of the values are just copies of the special SHN_* values.
161// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
162enum SymbolShndxType {
163 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000164 SYMBOL_ABS = ELF::SHN_ABS,
165 SYMBOL_COMMON = ELF::SHN_COMMON,
166 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
167 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
168 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
169 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000170};
171
Petr Hosek79cee9e2017-08-29 02:12:03 +0000172struct Symbol {
173 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000174 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000175 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000176 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000177 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000178 uint32_t NameIndex;
179 uint64_t Size;
180 uint8_t Type;
181 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000182
183 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000184};
185
186class SymbolTableSection : public SectionBase {
187protected:
188 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000189 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000190
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000191 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000192
Petr Hosek79cee9e2017-08-29 02:12:03 +0000193public:
194 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000195 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000196 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
197 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000198 void addSymbolNames();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000199 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000200 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000201 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000202 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000203 void finalize() override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000204
Petr Hosek79cee9e2017-08-29 02:12:03 +0000205 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000206 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000207 }
208};
209
210// Only writeSection depends on the ELF type so we implement it in a subclass.
211template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000212 void writeSection(FileOutputBuffer &Out) const override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000213};
214
Petr Hosekd7df9b22017-09-06 23:41:02 +0000215struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000216 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000217 uint64_t Offset;
218 uint64_t Addend;
219 uint32_t Type;
220};
221
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000222// All relocation sections denote relocations to apply to another section.
223// However, some relocation sections use a dynamic symbol table and others use
224// a regular symbol table. Because the types of the two symbol tables differ in
225// our system (because they should behave differently) we can't uniformly
226// represent all relocations with the same base class if we expose an interface
227// that mentions the symbol table type. So we split the two base types into two
228// different classes, one which handles the section the relocation is applied to
229// and another which handles the symbol table type. The symbol table type is
230// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
231class RelocationSectionBase : public SectionBase {
232protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000233 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000234
235public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000236 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000237 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000238
239 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000240 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000241 }
242};
243
244// Takes the symbol table type to use as a parameter so that we can deduplicate
245// that code between the two symbol table types.
246template <class SymTabType>
247class RelocSectionWithSymtabBase : public RelocationSectionBase {
248private:
249 SymTabType *Symbols = nullptr;
250
251protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000252 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000253
254public:
255 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000256 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000257 void initialize(SectionTableRef SecTable) override;
258 void finalize() override;
259};
260
261template <class ELFT>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000262class RelocationSection
263 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000264private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000265 using Elf_Rel = typename ELFT::Rel;
266 using Elf_Rela = typename ELFT::Rela;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000267
268 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000269
270 template <class T> void writeRel(T *Buf) const;
271
272public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000273 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000274 void writeSection(FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000275
Petr Hosekd7df9b22017-09-06 23:41:02 +0000276 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000277 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000278 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000279 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000280 }
281};
282
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000283class SectionWithStrTab : public Section {
284private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000285 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000286
287public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000288 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
289
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000290 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000291 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000292 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000293 void finalize() override;
294 static bool classof(const SectionBase *S);
295};
296
297class DynamicSymbolTableSection : public SectionWithStrTab {
298public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000299 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
300
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000301 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000302 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000303 }
304};
305
306class DynamicSection : public SectionWithStrTab {
307public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000308 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
309
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000310 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000311 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000312 }
313};
314
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000315class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000316 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000317private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000318 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000319
320public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000321 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
322
323 void writeSection(FileOutputBuffer &Out) const override;
324
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000325 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000326 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000327 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000328 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000329 }
330};
331
Petr Hosek05a04cb2017-08-01 00:33:58 +0000332template <class ELFT> class Object {
333private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000334 using SecPtr = std::unique_ptr<SectionBase>;
335 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000336
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000337 using Elf_Shdr = typename ELFT::Shdr;
338 using Elf_Ehdr = typename ELFT::Ehdr;
339 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000340
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000341 void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000342 SymbolTableSection *SymTab, SectionTableRef SecTable);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000343 SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000344 const Elf_Shdr &Shdr);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000345 void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
346 SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000347
348protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000349 StringTableSection *SectionNames = nullptr;
350 SymbolTableSection *SymbolTable = nullptr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000351 std::vector<SecPtr> Sections;
352 std::vector<SegPtr> Segments;
353
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000354 void writeHeader(FileOutputBuffer &Out) const;
355 void writeProgramHeaders(FileOutputBuffer &Out) const;
356 void writeSectionData(FileOutputBuffer &Out) const;
357 void writeSectionHeaders(FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000358
359public:
360 uint8_t Ident[16];
361 uint64_t Entry;
362 uint64_t SHOffset;
363 uint32_t Type;
364 uint32_t Machine;
365 uint32_t Version;
366 uint32_t Flags;
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000367 bool WriteSectionHeaders = true;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000368
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000369 Object(const object::ELFObjectFile<ELFT> &Obj);
370 virtual ~Object() = default;
371
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000372 const SymbolTableSection *getSymTab() const { return SymbolTable; }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000373 const SectionBase *getSectionHeaderStrTab() const { return SectionNames; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000374 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000375 virtual size_t totalSize() const = 0;
376 virtual void finalize() = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000377 virtual void write(FileOutputBuffer &Out) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000378};
379
Petr Hosekc4df10e2017-08-04 21:09:26 +0000380template <class ELFT> class ELFObject : public Object<ELFT> {
381private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000382 using SecPtr = std::unique_ptr<SectionBase>;
383 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000384
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000385 using Elf_Shdr = typename ELFT::Shdr;
386 using Elf_Ehdr = typename ELFT::Ehdr;
387 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000388
389 void sortSections();
390 void assignOffsets();
391
392public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000393 ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
394
Petr Hosekc4df10e2017-08-04 21:09:26 +0000395 void finalize() override;
396 size_t totalSize() const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000397 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000398};
399
400template <class ELFT> class BinaryObject : public Object<ELFT> {
401private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000402 using SecPtr = std::unique_ptr<SectionBase>;
403 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000404
405 uint64_t TotalSize;
406
407public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000408 BinaryObject(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};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000414
415} // end namespace llvm
416
417#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H