blob: f12e6da7d21cdc6d8e07c3e633c10b48ad5726f5 [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();
199 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000200 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000201 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000202 void finalize() override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000203
Petr Hosek79cee9e2017-08-29 02:12:03 +0000204 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000205 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000206 }
207};
208
209// Only writeSection depends on the ELF type so we implement it in a subclass.
210template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000211 void writeSection(FileOutputBuffer &Out) const override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000212};
213
Petr Hosekd7df9b22017-09-06 23:41:02 +0000214struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000215 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000216 uint64_t Offset;
217 uint64_t Addend;
218 uint32_t Type;
219};
220
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000221// All relocation sections denote relocations to apply to another section.
222// However, some relocation sections use a dynamic symbol table and others use
223// a regular symbol table. Because the types of the two symbol tables differ in
224// our system (because they should behave differently) we can't uniformly
225// represent all relocations with the same base class if we expose an interface
226// that mentions the symbol table type. So we split the two base types into two
227// different classes, one which handles the section the relocation is applied to
228// and another which handles the symbol table type. The symbol table type is
229// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
230class RelocationSectionBase : public SectionBase {
231protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000232 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000233
234public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000235 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000236 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000237
238 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000239 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000240 }
241};
242
243// Takes the symbol table type to use as a parameter so that we can deduplicate
244// that code between the two symbol table types.
245template <class SymTabType>
246class RelocSectionWithSymtabBase : public RelocationSectionBase {
247private:
248 SymTabType *Symbols = nullptr;
249
250protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000251 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000252
253public:
254 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000255 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000256 void initialize(SectionTableRef SecTable) override;
257 void finalize() override;
258};
259
260template <class ELFT>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000261class RelocationSection
262 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000263private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000264 using Elf_Rel = typename ELFT::Rel;
265 using Elf_Rela = typename ELFT::Rela;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000266
267 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000268
269 template <class T> void writeRel(T *Buf) const;
270
271public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000272 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000273 void writeSection(FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000274
Petr Hosekd7df9b22017-09-06 23:41:02 +0000275 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000276 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000277 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000278 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000279 }
280};
281
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000282class SectionWithStrTab : public Section {
283private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000284 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000285
286public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000287 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
288
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000289 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000290 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000291 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000292 void finalize() override;
293 static bool classof(const SectionBase *S);
294};
295
296class DynamicSymbolTableSection : public SectionWithStrTab {
297public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000298 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
299
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000300 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000301 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000302 }
303};
304
305class DynamicSection : public SectionWithStrTab {
306public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000307 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
308
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000309 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000310 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000311 }
312};
313
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000314class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000315 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000316private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000317 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000318
319public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000320 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
321
322 void writeSection(FileOutputBuffer &Out) const override;
323
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000324 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000325 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000326 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000327 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000328 }
329};
330
Petr Hosek05a04cb2017-08-01 00:33:58 +0000331template <class ELFT> class Object {
332private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000333 using SecPtr = std::unique_ptr<SectionBase>;
334 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000335
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000336 using Elf_Shdr = typename ELFT::Shdr;
337 using Elf_Ehdr = typename ELFT::Ehdr;
338 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000339
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000340 void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000341 SymbolTableSection *SymTab, SectionTableRef SecTable);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000342 SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000343 const Elf_Shdr &Shdr);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000344 void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
345 SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000346
347protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000348 StringTableSection *SectionNames = nullptr;
349 SymbolTableSection *SymbolTable = nullptr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000350 std::vector<SecPtr> Sections;
351 std::vector<SegPtr> Segments;
352
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000353 void writeHeader(FileOutputBuffer &Out) const;
354 void writeProgramHeaders(FileOutputBuffer &Out) const;
355 void writeSectionData(FileOutputBuffer &Out) const;
356 void writeSectionHeaders(FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000357
358public:
359 uint8_t Ident[16];
360 uint64_t Entry;
361 uint64_t SHOffset;
362 uint32_t Type;
363 uint32_t Machine;
364 uint32_t Version;
365 uint32_t Flags;
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000366 bool WriteSectionHeaders = true;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000367
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000368 Object(const object::ELFObjectFile<ELFT> &Obj);
369 virtual ~Object() = default;
370
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000371 const SectionBase *getSectionHeaderStrTab() const { return SectionNames; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000372 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000373 virtual size_t totalSize() const = 0;
374 virtual void finalize() = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000375 virtual void write(FileOutputBuffer &Out) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000376};
377
Petr Hosekc4df10e2017-08-04 21:09:26 +0000378template <class ELFT> class ELFObject : public Object<ELFT> {
379private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000380 using SecPtr = std::unique_ptr<SectionBase>;
381 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000382
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000383 using Elf_Shdr = typename ELFT::Shdr;
384 using Elf_Ehdr = typename ELFT::Ehdr;
385 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000386
387 void sortSections();
388 void assignOffsets();
389
390public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000391 ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
392
Petr Hosekc4df10e2017-08-04 21:09:26 +0000393 void finalize() override;
394 size_t totalSize() const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000395 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000396};
397
398template <class ELFT> class BinaryObject : public Object<ELFT> {
399private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000400 using SecPtr = std::unique_ptr<SectionBase>;
401 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000402
403 uint64_t TotalSize;
404
405public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000406 BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
407
Petr Hosekc4df10e2017-08-04 21:09:26 +0000408 void finalize() override;
409 size_t totalSize() const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000410 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000411};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000412
413} // end namespace llvm
414
415#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H