blob: 6ac7edd32ae9e22c43c20a65a88d0fe464bcbe93 [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 Ehrlich99482fd2018-01-09 23:00:25 +000019#include "llvm/Support/JamCRC.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000020#include <cstddef>
21#include <cstdint>
22#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000023#include <memory>
24#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000025#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000026
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000027namespace llvm {
28
Aaron Ballman09f46a72018-01-25 21:03:38 +000029class FileOutputBuffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000030class SectionBase;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000031class Segment;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000032
33class SectionTableRef {
34private:
Aaron Ballman09f46a72018-01-25 21:03:38 +000035 ArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000036
37public:
Aaron Ballman09f46a72018-01-25 21:03:38 +000038 SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000039 : Sections(Secs) {}
40 SectionTableRef(const SectionTableRef &) = default;
41
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000042 SectionBase *getSection(uint16_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000043
44 template <class T>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000045 T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000046};
Petr Hosek05a04cb2017-08-01 00:33:58 +000047
48class SectionBase {
49public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000050 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +000051 Segment *ParentSegment = nullptr;
52 uint64_t HeaderOffset;
53 uint64_t OriginalOffset;
54 uint32_t Index;
55
56 uint64_t Addr = 0;
57 uint64_t Align = 1;
58 uint32_t EntrySize = 0;
59 uint64_t Flags = 0;
60 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000061 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +000062 uint64_t NameIndex = 0;
63 uint64_t Offset = 0;
64 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000065 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +000066
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000067 virtual ~SectionBase() = default;
68
Jake Ehrlichf5a43772017-09-25 20:37:28 +000069 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +000070 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +000071 virtual void removeSectionReferences(const SectionBase *Sec);
Aaron Ballman09f46a72018-01-25 21:03:38 +000072 template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
73 virtual void writeSection(FileOutputBuffer &Out) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +000074};
75
76class Segment {
77private:
78 struct SectionCompare {
79 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
80 // Some sections might have the same address if one of them is empty. To
81 // fix this we can use the lexicographic ordering on ->Addr and the
82 // address of the actully stored section.
83 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
84 return Lhs < Rhs;
85 return Lhs->OriginalOffset < Rhs->OriginalOffset;
86 }
87 };
88
89 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000090 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +000091
92public:
93 uint64_t Align;
94 uint64_t FileSize;
95 uint32_t Flags;
96 uint32_t Index;
97 uint64_t MemSize;
98 uint64_t Offset;
99 uint64_t PAddr;
100 uint64_t Type;
101 uint64_t VAddr;
102
Petr Hosek3f383832017-08-26 01:32:20 +0000103 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000104 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000105
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000106 Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
107
Petr Hosek05a04cb2017-08-01 00:33:58 +0000108 const SectionBase *firstSection() const {
109 if (!Sections.empty())
110 return *Sections.begin();
111 return nullptr;
112 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000113
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000114 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
115 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000116 template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
117 void writeSegment(FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000118};
119
120class Section : public SectionBase {
121private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000122 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000123
124public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000125 Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
126
Aaron Ballman09f46a72018-01-25 21:03:38 +0000127 void writeSection(FileOutputBuffer &Out) const override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000128};
129
Jake Ehrliche8437de2017-12-19 00:47:30 +0000130class OwnedDataSection : public SectionBase {
131private:
132 std::vector<uint8_t> Data;
133
134public:
135 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
136 : Data(std::begin(Data), std::end(Data)) {
137 Name = SecName;
138 Type = ELF::SHT_PROGBITS;
139 Size = Data.size();
140 }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000141 void writeSection(FileOutputBuffer &Out) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000142};
143
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000144// There are two types of string tables that can exist, dynamic and not dynamic.
145// In the dynamic case the string table is allocated. Changing a dynamic string
146// table would mean altering virtual addresses and thus the memory image. So
147// dynamic string tables should not have an interface to modify them or
148// reconstruct them. This type lets us reconstruct a string table. To avoid
149// this class being used for dynamic string tables (which has happened) the
150// classof method checks that the particular instance is not allocated. This
151// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000152class StringTableSection : public SectionBase {
153private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000154 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000155
156public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000157 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
158 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000159 }
160
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000161 void addString(StringRef Name);
162 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000163 void finalize() override;
Aaron Ballman09f46a72018-01-25 21:03:38 +0000164 void writeSection(FileOutputBuffer &Out) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000165
Petr Hosek05a04cb2017-08-01 00:33:58 +0000166 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000167 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000168 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000169 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000170 }
171};
172
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000173// Symbols have a st_shndx field that normally stores an index but occasionally
174// stores a different special value. This enum keeps track of what the st_shndx
175// field means. Most of the values are just copies of the special SHN_* values.
176// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
177enum SymbolShndxType {
178 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000179 SYMBOL_ABS = ELF::SHN_ABS,
180 SYMBOL_COMMON = ELF::SHN_COMMON,
181 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
182 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
183 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
184 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000185};
186
Petr Hosek79cee9e2017-08-29 02:12:03 +0000187struct Symbol {
188 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000189 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000190 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000191 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000192 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000193 uint32_t NameIndex;
194 uint64_t Size;
195 uint8_t Type;
196 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000197 uint8_t Visibility;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000198
199 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000200};
201
202class SymbolTableSection : public SectionBase {
203protected:
204 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000205 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000206
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000207 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000208
Petr Hosek79cee9e2017-08-29 02:12:03 +0000209public:
210 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000211 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000212 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
213 uint16_t Shndx, uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000214 void addSymbolNames();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000215 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000216 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000217 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000218 void localize(std::function<bool(const Symbol &)> ToLocalize);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000219 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000220 void finalize() override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000221
Petr Hosek79cee9e2017-08-29 02:12:03 +0000222 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000223 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000224 }
225};
226
Aaron Ballman09f46a72018-01-25 21:03:38 +0000227// Only writeSection depends on the ELF type so we implement it in a subclass.
228template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
229 void writeSection(FileOutputBuffer &Out) const override;
230};
231
Petr Hosekd7df9b22017-09-06 23:41:02 +0000232struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000233 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000234 uint64_t Offset;
235 uint64_t Addend;
236 uint32_t Type;
237};
238
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000239// All relocation sections denote relocations to apply to another section.
240// However, some relocation sections use a dynamic symbol table and others use
241// a regular symbol table. Because the types of the two symbol tables differ in
242// our system (because they should behave differently) we can't uniformly
243// represent all relocations with the same base class if we expose an interface
244// that mentions the symbol table type. So we split the two base types into two
245// different classes, one which handles the section the relocation is applied to
246// and another which handles the symbol table type. The symbol table type is
247// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
248class RelocationSectionBase : public SectionBase {
249protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000250 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000251
252public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000253 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000254 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000255
256 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000257 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000258 }
259};
260
261// Takes the symbol table type to use as a parameter so that we can deduplicate
262// that code between the two symbol table types.
263template <class SymTabType>
264class RelocSectionWithSymtabBase : public RelocationSectionBase {
265private:
266 SymTabType *Symbols = nullptr;
267
268protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000269 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000270
271public:
272 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000273 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000274 void initialize(SectionTableRef SecTable) override;
275 void finalize() override;
276};
277
Aaron Ballman09f46a72018-01-25 21:03:38 +0000278template <class ELFT>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000279class RelocationSection
280 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000281private:
Aaron Ballman09f46a72018-01-25 21:03:38 +0000282 using Elf_Rel = typename ELFT::Rel;
283 using Elf_Rela = typename ELFT::Rela;
284
Petr Hosekd7df9b22017-09-06 23:41:02 +0000285 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000286
Aaron Ballman09f46a72018-01-25 21:03:38 +0000287 template <class T> void writeRel(T *Buf) const;
288
Petr Hosekd7df9b22017-09-06 23:41:02 +0000289public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000290 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000291 void writeSection(FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000292
Petr Hosekd7df9b22017-09-06 23:41:02 +0000293 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000294 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000295 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000296 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000297 }
298};
299
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000300class SectionWithStrTab : public Section {
301private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000302 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000303
304public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000305 SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
306
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000307 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000308 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000309 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000310 void finalize() override;
311 static bool classof(const SectionBase *S);
312};
313
314class DynamicSymbolTableSection : public SectionWithStrTab {
315public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000316 DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
317
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000318 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000319 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000320 }
321};
322
323class DynamicSection : public SectionWithStrTab {
324public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000325 DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
326
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000327 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000328 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000329 }
330};
331
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000332class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000333 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000334private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000335 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000336
337public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000338 DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
339
Aaron Ballman09f46a72018-01-25 21:03:38 +0000340 void writeSection(FileOutputBuffer &Out) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000341
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000342 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000343 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000344 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000345 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000346 }
347};
348
Aaron Ballman09f46a72018-01-25 21:03:38 +0000349template <class ELFT> class GnuDebugLinkSection : public SectionBase {
Jake Ehrlich99482fd2018-01-09 23:00:25 +0000350private:
Aaron Ballman09f46a72018-01-25 21:03:38 +0000351 // Elf_Word is 4-bytes on every format but has the same endianess as the elf
352 // type ELFT. We'll need to write the CRC32 out in the proper endianess so
353 // we'll make sure to use this type.
354 using Elf_Word = typename ELFT::Word;
Jake Ehrlich99482fd2018-01-09 23:00:25 +0000355
356 StringRef FileName;
357 uint32_t CRC32;
358
359 void init(StringRef File, StringRef Data);
360
361public:
362 // If we add this section from an external source we can use this ctor.
363 GnuDebugLinkSection(StringRef File);
Aaron Ballman09f46a72018-01-25 21:03:38 +0000364 void writeSection(FileOutputBuffer &Out) const override;
Jake Ehrlich99482fd2018-01-09 23:00:25 +0000365};
366
Aaron Ballman09f46a72018-01-25 21:03:38 +0000367template <class ELFT> class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000368private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000369 using SecPtr = std::unique_ptr<SectionBase>;
370 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000371
Aaron Ballman09f46a72018-01-25 21:03:38 +0000372 using Elf_Shdr = typename ELFT::Shdr;
373 using Elf_Ehdr = typename ELFT::Ehdr;
374 using Elf_Phdr = typename ELFT::Phdr;
375
376 void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
377 SymbolTableSection *SymTab, SectionTableRef SecTable);
378 SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
379 const Elf_Shdr &Shdr);
380 void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
381 SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
382
383protected:
384 StringTableSection *SectionNames = nullptr;
385 SymbolTableSection *SymbolTable = nullptr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000386 std::vector<SecPtr> Sections;
387 std::vector<SegPtr> Segments;
388
Aaron Ballman09f46a72018-01-25 21:03:38 +0000389 void writeHeader(FileOutputBuffer &Out) const;
390 void writeProgramHeaders(FileOutputBuffer &Out) const;
391 void writeSectionData(FileOutputBuffer &Out) const;
392 void writeSectionHeaders(FileOutputBuffer &Out) const;
393
Petr Hosek05a04cb2017-08-01 00:33:58 +0000394public:
395 uint8_t Ident[16];
396 uint64_t Entry;
397 uint64_t SHOffset;
398 uint32_t Type;
399 uint32_t Machine;
400 uint32_t Version;
401 uint32_t Flags;
Aaron Ballman09f46a72018-01-25 21:03:38 +0000402 bool WriteSectionHeaders = true;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000403
Aaron Ballman09f46a72018-01-25 21:03:38 +0000404 Object(const object::ELFObjectFile<ELFT> &Obj);
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000405 virtual ~Object() = default;
406
Aaron Ballman09f46a72018-01-25 21:03:38 +0000407 SymbolTableSection *getSymTab() const { return SymbolTable; }
408 const SectionBase *getSectionHeaderStrTab() const { return SectionNames; }
Jake Ehrlichdf355942018-01-25 20:24:17 +0000409 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Aaron Ballman09f46a72018-01-25 21:03:38 +0000410 void addSection(StringRef SecName, ArrayRef<uint8_t> Data);
411 void addGnuDebugLink(StringRef File);
412 virtual size_t totalSize() const = 0;
413 virtual void finalize() = 0;
414 virtual void write(FileOutputBuffer &Out) const = 0;
415};
416
417template <class ELFT> class ELFObject : public Object<ELFT> {
418private:
419 using SecPtr = std::unique_ptr<SectionBase>;
420 using SegPtr = std::unique_ptr<Segment>;
421
422 using Elf_Shdr = typename ELFT::Shdr;
423 using Elf_Ehdr = typename ELFT::Ehdr;
424 using Elf_Phdr = typename ELFT::Phdr;
425
426 void sortSections();
427 void assignOffsets();
428
429public:
430 ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
431
432 void finalize() override;
433 size_t totalSize() const override;
434 void write(FileOutputBuffer &Out) const override;
435};
436
437template <class ELFT> class BinaryObject : public Object<ELFT> {
438private:
439 using SecPtr = std::unique_ptr<SectionBase>;
440 using SegPtr = std::unique_ptr<Segment>;
441
442 uint64_t TotalSize;
443
444public:
445 BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
446
447 void finalize() override;
448 size_t totalSize() const override;
449 void write(FileOutputBuffer &Out) const override;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000450};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000451
452} // end namespace llvm
453
454#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H