blob: f6088434805d62588d7bf1386f682cacc573f7b0 [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
10#ifndef LLVM_OBJCOPY_OBJECT_H
11#define LLVM_OBJCOPY_OBJECT_H
12
13#include "llvm/MC/StringTableBuilder.h"
14#include "llvm/Object/ELFObjectFile.h"
15#include "llvm/Support/FileOutputBuffer.h"
16
17#include <memory>
18#include <set>
19
20class Segment;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000021class SectionBase;
22
23class SectionTableRef {
24private:
25 llvm::ArrayRef<std::unique_ptr<SectionBase>> Sections;
26
27public:
28 SectionTableRef(llvm::ArrayRef<std::unique_ptr<SectionBase>> Secs)
29 : Sections(Secs) {}
30 SectionTableRef(const SectionTableRef &) = default;
31
32 SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg);
33
34 template <class T>
Jake Ehrlichf5a43772017-09-25 20:37:28 +000035 T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg,
36 llvm::Twine TypeErrMsg);
37};
Petr Hosek05a04cb2017-08-01 00:33:58 +000038
39class SectionBase {
40public:
41 llvm::StringRef Name;
42 Segment *ParentSegment = nullptr;
43 uint64_t HeaderOffset;
44 uint64_t OriginalOffset;
45 uint32_t Index;
46
47 uint64_t Addr = 0;
48 uint64_t Align = 1;
49 uint32_t EntrySize = 0;
50 uint64_t Flags = 0;
51 uint64_t Info = 0;
52 uint64_t Link = llvm::ELF::SHN_UNDEF;
53 uint64_t NameIndex = 0;
54 uint64_t Offset = 0;
55 uint64_t Size = 0;
56 uint64_t Type = llvm::ELF::SHT_NULL;
57
58 virtual ~SectionBase() {}
Jake Ehrlichf5a43772017-09-25 20:37:28 +000059 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +000060 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +000061 virtual void removeSectionReferences(const SectionBase *Sec);
Petr Hosek05a04cb2017-08-01 00:33:58 +000062 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
63 virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0;
64};
65
66class Segment {
67private:
68 struct SectionCompare {
69 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
70 // Some sections might have the same address if one of them is empty. To
71 // fix this we can use the lexicographic ordering on ->Addr and the
72 // address of the actully stored section.
73 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
74 return Lhs < Rhs;
75 return Lhs->OriginalOffset < Rhs->OriginalOffset;
76 }
77 };
78
79 std::set<const SectionBase *, SectionCompare> Sections;
Petr Hosekc4df10e2017-08-04 21:09:26 +000080 llvm::ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +000081
82public:
83 uint64_t Align;
84 uint64_t FileSize;
85 uint32_t Flags;
86 uint32_t Index;
87 uint64_t MemSize;
88 uint64_t Offset;
89 uint64_t PAddr;
90 uint64_t Type;
91 uint64_t VAddr;
92
Petr Hosek3f383832017-08-26 01:32:20 +000093 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +000094 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +000095
Petr Hosekc4df10e2017-08-04 21:09:26 +000096 Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000097 const SectionBase *firstSection() const {
98 if (!Sections.empty())
99 return *Sections.begin();
100 return nullptr;
101 }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000102 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
103 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000104 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000105 void writeSegment(llvm::FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000106};
107
108class Section : public SectionBase {
109private:
110 llvm::ArrayRef<uint8_t> Contents;
111
112public:
113 Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
114 void writeSection(llvm::FileOutputBuffer &Out) const override;
115};
116
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000117// There are two types of string tables that can exist, dynamic and not dynamic.
118// In the dynamic case the string table is allocated. Changing a dynamic string
119// table would mean altering virtual addresses and thus the memory image. So
120// dynamic string tables should not have an interface to modify them or
121// reconstruct them. This type lets us reconstruct a string table. To avoid
122// this class being used for dynamic string tables (which has happened) the
123// classof method checks that the particular instance is not allocated. This
124// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000125class StringTableSection : public SectionBase {
126private:
127 llvm::StringTableBuilder StrTabBuilder;
128
129public:
130 StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
131 Type = llvm::ELF::SHT_STRTAB;
132 }
133
134 void addString(llvm::StringRef Name);
135 uint32_t findIndex(llvm::StringRef Name) const;
136 void finalize() override;
137 void writeSection(llvm::FileOutputBuffer &Out) const override;
138 static bool classof(const SectionBase *S) {
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000139 if (S->Flags & llvm::ELF::SHF_ALLOC)
140 return false;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000141 return S->Type == llvm::ELF::SHT_STRTAB;
142 }
143};
144
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000145// Symbols have a st_shndx field that normally stores an index but occasionally
146// stores a different special value. This enum keeps track of what the st_shndx
147// field means. Most of the values are just copies of the special SHN_* values.
148// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
149enum SymbolShndxType {
150 SYMBOL_SIMPLE_INDEX = 0,
151 SYMBOL_ABS = llvm::ELF::SHN_ABS,
152 SYMBOL_COMMON = llvm::ELF::SHN_COMMON,
153 SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON,
154 SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2,
155 SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4,
156 SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8,
157};
158
Petr Hosek79cee9e2017-08-29 02:12:03 +0000159struct Symbol {
160 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000161 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000162 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000163 uint32_t Index;
164 llvm::StringRef Name;
165 uint32_t NameIndex;
166 uint64_t Size;
167 uint8_t Type;
168 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000169
170 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000171};
172
173class SymbolTableSection : public SectionBase {
174protected:
175 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000176 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000177
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000178 typedef std::unique_ptr<Symbol> SymPtr;
179
Petr Hosek79cee9e2017-08-29 02:12:03 +0000180public:
181 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
182 void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000183 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
184 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000185 void addSymbolNames();
186 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000187 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000188 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000189 void finalize() override;
190 static bool classof(const SectionBase *S) {
191 return S->Type == llvm::ELF::SHT_SYMTAB;
192 }
193};
194
195// Only writeSection depends on the ELF type so we implement it in a subclass.
196template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
197 void writeSection(llvm::FileOutputBuffer &Out) const override;
198};
199
Petr Hosekd7df9b22017-09-06 23:41:02 +0000200struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000201 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000202 uint64_t Offset;
203 uint64_t Addend;
204 uint32_t Type;
205};
206
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000207// All relocation sections denote relocations to apply to another section.
208// However, some relocation sections use a dynamic symbol table and others use
209// a regular symbol table. Because the types of the two symbol tables differ in
210// our system (because they should behave differently) we can't uniformly
211// represent all relocations with the same base class if we expose an interface
212// that mentions the symbol table type. So we split the two base types into two
213// different classes, one which handles the section the relocation is applied to
214// and another which handles the symbol table type. The symbol table type is
215// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
216class RelocationSectionBase : public SectionBase {
217protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000218 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000219
220public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000221 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000222 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000223
224 static bool classof(const SectionBase *S) {
225 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
226 }
227};
228
229// Takes the symbol table type to use as a parameter so that we can deduplicate
230// that code between the two symbol table types.
231template <class SymTabType>
232class RelocSectionWithSymtabBase : public RelocationSectionBase {
233private:
234 SymTabType *Symbols = nullptr;
235
236protected:
237 RelocSectionWithSymtabBase() {}
238
239public:
240 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
241
242 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000243 void initialize(SectionTableRef SecTable) override;
244 void finalize() override;
245};
246
247template <class ELFT>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000248class RelocationSection
249 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000250private:
251 typedef typename ELFT::Rel Elf_Rel;
252 typedef typename ELFT::Rela Elf_Rela;
253
254 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000255
256 template <class T> void writeRel(T *Buf) const;
257
258public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000259 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Petr Hosekd7df9b22017-09-06 23:41:02 +0000260 void writeSection(llvm::FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000261
Petr Hosekd7df9b22017-09-06 23:41:02 +0000262 static bool classof(const SectionBase *S) {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000263 if (S->Flags & llvm::ELF::SHF_ALLOC)
264 return false;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000265 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
266 }
267};
268
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000269class SectionWithStrTab : public Section {
270private:
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000271 const SectionBase *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000272
273public:
274 SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {}
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000275 void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000276 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000277 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000278 void finalize() override;
279 static bool classof(const SectionBase *S);
280};
281
282class DynamicSymbolTableSection : public SectionWithStrTab {
283public:
284 DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data)
285 : SectionWithStrTab(Data) {}
286 static bool classof(const SectionBase *S) {
287 return S->Type == llvm::ELF::SHT_DYNSYM;
288 }
289};
290
291class DynamicSection : public SectionWithStrTab {
292public:
293 DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
294 static bool classof(const SectionBase *S) {
295 return S->Type == llvm::ELF::SHT_DYNAMIC;
296 }
297};
298
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000299class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000300 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000301private:
302 llvm::ArrayRef<uint8_t> Contents;
303
304public:
305 DynamicRelocationSection(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
306 void writeSection(llvm::FileOutputBuffer &Out) const override;
307 static bool classof(const SectionBase *S) {
308 if (!(S->Flags & llvm::ELF::SHF_ALLOC))
309 return false;
310 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
311 }
312};
313
Petr Hosek05a04cb2017-08-01 00:33:58 +0000314template <class ELFT> class Object {
315private:
316 typedef std::unique_ptr<SectionBase> SecPtr;
317 typedef std::unique_ptr<Segment> SegPtr;
318
319 typedef typename ELFT::Shdr Elf_Shdr;
320 typedef typename ELFT::Ehdr Elf_Ehdr;
321 typedef typename ELFT::Phdr Elf_Phdr;
322
Petr Hosek79cee9e2017-08-29 02:12:03 +0000323 void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000324 SymbolTableSection *SymTab, SectionTableRef SecTable);
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000325 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
326 const Elf_Shdr &Shdr);
327 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000328 SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000329
330protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000331 StringTableSection *SectionNames = nullptr;
332 SymbolTableSection *SymbolTable = nullptr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000333 std::vector<SecPtr> Sections;
334 std::vector<SegPtr> Segments;
335
Petr Hosek05a04cb2017-08-01 00:33:58 +0000336 void writeHeader(llvm::FileOutputBuffer &Out) const;
337 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
338 void writeSectionData(llvm::FileOutputBuffer &Out) const;
339 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
340
341public:
342 uint8_t Ident[16];
343 uint64_t Entry;
344 uint64_t SHOffset;
345 uint32_t Type;
346 uint32_t Machine;
347 uint32_t Version;
348 uint32_t Flags;
Jake Ehrlichf03384d2017-10-11 18:09:18 +0000349 bool WriteSectionHeaders = true;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000350
351 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000352 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000353 virtual size_t totalSize() const = 0;
354 virtual void finalize() = 0;
355 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
356 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000357};
358
Petr Hosekc4df10e2017-08-04 21:09:26 +0000359template <class ELFT> class ELFObject : public Object<ELFT> {
360private:
361 typedef std::unique_ptr<SectionBase> SecPtr;
362 typedef std::unique_ptr<Segment> SegPtr;
363
364 typedef typename ELFT::Shdr Elf_Shdr;
365 typedef typename ELFT::Ehdr Elf_Ehdr;
366 typedef typename ELFT::Phdr Elf_Phdr;
367
368 void sortSections();
369 void assignOffsets();
370
371public:
372 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
373 void finalize() override;
374 size_t totalSize() const override;
375 void write(llvm::FileOutputBuffer &Out) const override;
376};
377
378template <class ELFT> class BinaryObject : public Object<ELFT> {
379private:
380 typedef std::unique_ptr<SectionBase> SecPtr;
381 typedef std::unique_ptr<Segment> SegPtr;
382
383 uint64_t TotalSize;
384
385public:
386 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
387 : Object<ELFT>(Obj) {}
388 void finalize() override;
389 size_t totalSize() const override;
390 void write(llvm::FileOutputBuffer &Out) const override;
391};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000392#endif