blob: 17ea6c9413b48a32016ce77ea4459bb22a2974b2 [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;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000196 uint8_t Visibility;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000197
198 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000199};
200
201class SymbolTableSection : public SectionBase {
202protected:
203 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000204 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000205
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000206 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000207
Petr Hosek79cee9e2017-08-29 02:12:03 +0000208public:
209 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000210 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000211 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
212 uint16_t Shndx, uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000213 void addSymbolNames();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000214 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000215 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000216 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000217 void localize(std::function<bool(const Symbol &)> ToLocalize);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000218 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000219 void finalize() override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000220
Petr Hosek79cee9e2017-08-29 02:12:03 +0000221 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000222 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000223 }
224};
225
226// Only writeSection depends on the ELF type so we implement it in a subclass.
227template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000228 void writeSection(FileOutputBuffer &Out) const override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000229};
230
Petr Hosekd7df9b22017-09-06 23:41:02 +0000231struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000232 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000233 uint64_t Offset;
234 uint64_t Addend;
235 uint32_t Type;
236};
237
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000238// All relocation sections denote relocations to apply to another section.
239// However, some relocation sections use a dynamic symbol table and others use
240// a regular symbol table. Because the types of the two symbol tables differ in
241// our system (because they should behave differently) we can't uniformly
242// represent all relocations with the same base class if we expose an interface
243// that mentions the symbol table type. So we split the two base types into two
244// different classes, one which handles the section the relocation is applied to
245// and another which handles the symbol table type. The symbol table type is
246// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
247class RelocationSectionBase : public SectionBase {
248protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000249 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000250
251public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000252 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000253 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000254
255 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000256 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000257 }
258};
259
260// Takes the symbol table type to use as a parameter so that we can deduplicate
261// that code between the two symbol table types.
262template <class SymTabType>
263class RelocSectionWithSymtabBase : public RelocationSectionBase {
264private:
265 SymTabType *Symbols = nullptr;
266
267protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000268 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000269
270public:
271 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000272 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000273 void initialize(SectionTableRef SecTable) override;
274 void finalize() override;
275};
276
277template <class ELFT>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000278class RelocationSection
279 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000280private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000281 using Elf_Rel = typename ELFT::Rel;
282 using Elf_Rela = typename ELFT::Rela;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000283
284 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000285
286 template <class T> void writeRel(T *Buf) const;
287
288public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000289 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000290 void writeSection(FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000291
Petr Hosekd7df9b22017-09-06 23:41:02 +0000292 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000293 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000294 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000295 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000296 }
297};
298
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000299class SectionWithStrTab : public Section {
300private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000301 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000302
303public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000304 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
305
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000306 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000307 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000308 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000309 void finalize() override;
310 static bool classof(const SectionBase *S);
311};
312
313class DynamicSymbolTableSection : public SectionWithStrTab {
314public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000315 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
316
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000317 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000318 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000319 }
320};
321
322class DynamicSection : public SectionWithStrTab {
323public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000324 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
325
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000326 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000327 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000328 }
329};
330
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000331class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000332 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000333private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000334 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000335
336public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000337 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
338
339 void writeSection(FileOutputBuffer &Out) const override;
340
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000341 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000342 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000343 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000344 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000345 }
346};
347
Petr Hosek05a04cb2017-08-01 00:33:58 +0000348template <class ELFT> class Object {
349private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000350 using SecPtr = std::unique_ptr<SectionBase>;
351 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000352
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000353 using Elf_Shdr = typename ELFT::Shdr;
354 using Elf_Ehdr = typename ELFT::Ehdr;
355 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000356
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000357 void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000358 SymbolTableSection *SymTab, SectionTableRef SecTable);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000359 SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000360 const Elf_Shdr &Shdr);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000361 void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
362 SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000363
364protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000365 StringTableSection *SectionNames = nullptr;
366 SymbolTableSection *SymbolTable = nullptr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000367 std::vector<SecPtr> Sections;
368 std::vector<SegPtr> Segments;
369
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000370 void writeHeader(FileOutputBuffer &Out) const;
371 void writeProgramHeaders(FileOutputBuffer &Out) const;
372 void writeSectionData(FileOutputBuffer &Out) const;
373 void writeSectionHeaders(FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000374
375public:
376 uint8_t Ident[16];
377 uint64_t Entry;
378 uint64_t SHOffset;
379 uint32_t Type;
380 uint32_t Machine;
381 uint32_t Version;
382 uint32_t Flags;
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000383 bool WriteSectionHeaders = true;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000384
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000385 Object(const object::ELFObjectFile<ELFT> &Obj);
386 virtual ~Object() = default;
387
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000388 SymbolTableSection *getSymTab() const { return SymbolTable; }
Jake Ehrlich5de70d92017-11-03 18:58:41 +0000389 const SectionBase *getSectionHeaderStrTab() const { return SectionNames; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000390 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000391 void addSection(StringRef SecName, ArrayRef<uint8_t> Data);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000392 virtual size_t totalSize() const = 0;
393 virtual void finalize() = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000394 virtual void write(FileOutputBuffer &Out) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000395};
396
Petr Hosekc4df10e2017-08-04 21:09:26 +0000397template <class ELFT> class ELFObject : public Object<ELFT> {
398private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000399 using SecPtr = std::unique_ptr<SectionBase>;
400 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000401
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000402 using Elf_Shdr = typename ELFT::Shdr;
403 using Elf_Ehdr = typename ELFT::Ehdr;
404 using Elf_Phdr = typename ELFT::Phdr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000405
406 void sortSections();
407 void assignOffsets();
408
409public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000410 ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
411
Petr Hosekc4df10e2017-08-04 21:09:26 +0000412 void finalize() override;
413 size_t totalSize() const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000414 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000415};
416
417template <class ELFT> class BinaryObject : public Object<ELFT> {
418private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000419 using SecPtr = std::unique_ptr<SectionBase>;
420 using SegPtr = std::unique_ptr<Segment>;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000421
422 uint64_t TotalSize;
423
424public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000425 BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
426
Petr Hosekc4df10e2017-08-04 21:09:26 +0000427 void finalize() override;
428 size_t totalSize() const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000429 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000430};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000431
432} // end namespace llvm
433
434#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H