blob: 398eed7e16c05c911d609b1b084ee9b284b12bcb [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();
61 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
62 virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0;
63};
64
65class Segment {
66private:
67 struct SectionCompare {
68 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
69 // Some sections might have the same address if one of them is empty. To
70 // fix this we can use the lexicographic ordering on ->Addr and the
71 // address of the actully stored section.
72 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
73 return Lhs < Rhs;
74 return Lhs->OriginalOffset < Rhs->OriginalOffset;
75 }
76 };
77
78 std::set<const SectionBase *, SectionCompare> Sections;
Petr Hosekc4df10e2017-08-04 21:09:26 +000079 llvm::ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +000080
81public:
82 uint64_t Align;
83 uint64_t FileSize;
84 uint32_t Flags;
85 uint32_t Index;
86 uint64_t MemSize;
87 uint64_t Offset;
88 uint64_t PAddr;
89 uint64_t Type;
90 uint64_t VAddr;
91
Petr Hosek3f383832017-08-26 01:32:20 +000092 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +000093 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +000094
Petr Hosekc4df10e2017-08-04 21:09:26 +000095 Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000096 const SectionBase *firstSection() const {
97 if (!Sections.empty())
98 return *Sections.begin();
99 return nullptr;
100 }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000101 void addSection(const SectionBase *sec) { Sections.insert(sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000102 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000103 void writeSegment(llvm::FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000104};
105
106class Section : public SectionBase {
107private:
108 llvm::ArrayRef<uint8_t> Contents;
109
110public:
111 Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
112 void writeSection(llvm::FileOutputBuffer &Out) const override;
113};
114
115// This is just a wraper around a StringTableBuilder that implements SectionBase
116class StringTableSection : public SectionBase {
117private:
118 llvm::StringTableBuilder StrTabBuilder;
119
120public:
121 StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
122 Type = llvm::ELF::SHT_STRTAB;
123 }
124
125 void addString(llvm::StringRef Name);
126 uint32_t findIndex(llvm::StringRef Name) const;
127 void finalize() override;
128 void writeSection(llvm::FileOutputBuffer &Out) const override;
129 static bool classof(const SectionBase *S) {
130 return S->Type == llvm::ELF::SHT_STRTAB;
131 }
132};
133
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000134// Symbols have a st_shndx field that normally stores an index but occasionally
135// stores a different special value. This enum keeps track of what the st_shndx
136// field means. Most of the values are just copies of the special SHN_* values.
137// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
138enum SymbolShndxType {
139 SYMBOL_SIMPLE_INDEX = 0,
140 SYMBOL_ABS = llvm::ELF::SHN_ABS,
141 SYMBOL_COMMON = llvm::ELF::SHN_COMMON,
142 SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON,
143 SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2,
144 SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4,
145 SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8,
146};
147
Petr Hosek79cee9e2017-08-29 02:12:03 +0000148struct Symbol {
149 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000150 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000151 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000152 uint32_t Index;
153 llvm::StringRef Name;
154 uint32_t NameIndex;
155 uint64_t Size;
156 uint8_t Type;
157 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000158
159 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000160};
161
162class SymbolTableSection : public SectionBase {
163protected:
164 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000165 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000166
167public:
168 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
169 void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000170 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
171 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000172 void addSymbolNames();
173 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000174 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000175 void finalize() override;
176 static bool classof(const SectionBase *S) {
177 return S->Type == llvm::ELF::SHT_SYMTAB;
178 }
179};
180
181// Only writeSection depends on the ELF type so we implement it in a subclass.
182template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
183 void writeSection(llvm::FileOutputBuffer &Out) const override;
184};
185
Petr Hosekd7df9b22017-09-06 23:41:02 +0000186struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000187 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000188 uint64_t Offset;
189 uint64_t Addend;
190 uint32_t Type;
191};
192
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000193template <class SymTabType> class RelocationSectionBase : public SectionBase {
194private:
195 SymTabType *Symbols = nullptr;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000196 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000197
198public:
Jake Ehrlich77ec1ff2017-10-10 18:28:15 +0000199 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000200 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000201 void initialize(SectionTableRef SecTable) override;
202 void finalize() override;
203};
204
205template <class ELFT>
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000206class RelocationSection : public RelocationSectionBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000207private:
208 typedef typename ELFT::Rel Elf_Rel;
209 typedef typename ELFT::Rela Elf_Rela;
210
211 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000212
213 template <class T> void writeRel(T *Buf) const;
214
215public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000216 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Petr Hosekd7df9b22017-09-06 23:41:02 +0000217 void writeSection(llvm::FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000218
Petr Hosekd7df9b22017-09-06 23:41:02 +0000219 static bool classof(const SectionBase *S) {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000220 if (S->Flags & llvm::ELF::SHF_ALLOC)
221 return false;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000222 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
223 }
224};
225
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000226class SectionWithStrTab : public Section {
227private:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000228 StringTableSection *StrTab = nullptr;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000229
230public:
231 SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {}
232 void setStrTab(StringTableSection *StringTable) { StrTab = StringTable; }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000233 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000234 void finalize() override;
235 static bool classof(const SectionBase *S);
236};
237
238class DynamicSymbolTableSection : public SectionWithStrTab {
239public:
240 DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data)
241 : SectionWithStrTab(Data) {}
242 static bool classof(const SectionBase *S) {
243 return S->Type == llvm::ELF::SHT_DYNSYM;
244 }
245};
246
247class DynamicSection : public SectionWithStrTab {
248public:
249 DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
250 static bool classof(const SectionBase *S) {
251 return S->Type == llvm::ELF::SHT_DYNAMIC;
252 }
253};
254
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000255class DynamicRelocationSection
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000256 : public RelocationSectionBase<DynamicSymbolTableSection> {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000257private:
258 llvm::ArrayRef<uint8_t> Contents;
259
260public:
261 DynamicRelocationSection(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
262 void writeSection(llvm::FileOutputBuffer &Out) const override;
263 static bool classof(const SectionBase *S) {
264 if (!(S->Flags & llvm::ELF::SHF_ALLOC))
265 return false;
266 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
267 }
268};
269
Petr Hosek05a04cb2017-08-01 00:33:58 +0000270template <class ELFT> class Object {
271private:
272 typedef std::unique_ptr<SectionBase> SecPtr;
273 typedef std::unique_ptr<Segment> SegPtr;
274
275 typedef typename ELFT::Shdr Elf_Shdr;
276 typedef typename ELFT::Ehdr Elf_Ehdr;
277 typedef typename ELFT::Phdr Elf_Phdr;
278
Petr Hosek79cee9e2017-08-29 02:12:03 +0000279 void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000280 SymbolTableSection *SymTab, SectionTableRef SecTable);
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000281 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
282 const Elf_Shdr &Shdr);
283 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000284 SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000285
286protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000287 StringTableSection *SectionNames = nullptr;
288 SymbolTableSection *SymbolTable = nullptr;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000289 std::vector<SecPtr> Sections;
290 std::vector<SegPtr> Segments;
291
Petr Hosek05a04cb2017-08-01 00:33:58 +0000292 void writeHeader(llvm::FileOutputBuffer &Out) const;
293 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
294 void writeSectionData(llvm::FileOutputBuffer &Out) const;
295 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
296
297public:
298 uint8_t Ident[16];
299 uint64_t Entry;
300 uint64_t SHOffset;
301 uint32_t Type;
302 uint32_t Machine;
303 uint32_t Version;
304 uint32_t Flags;
305
306 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000307 virtual size_t totalSize() const = 0;
308 virtual void finalize() = 0;
309 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
310 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000311};
312
Petr Hosekc4df10e2017-08-04 21:09:26 +0000313template <class ELFT> class ELFObject : public Object<ELFT> {
314private:
315 typedef std::unique_ptr<SectionBase> SecPtr;
316 typedef std::unique_ptr<Segment> SegPtr;
317
318 typedef typename ELFT::Shdr Elf_Shdr;
319 typedef typename ELFT::Ehdr Elf_Ehdr;
320 typedef typename ELFT::Phdr Elf_Phdr;
321
322 void sortSections();
323 void assignOffsets();
324
325public:
326 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
327 void finalize() override;
328 size_t totalSize() const override;
329 void write(llvm::FileOutputBuffer &Out) const override;
330};
331
332template <class ELFT> class BinaryObject : public Object<ELFT> {
333private:
334 typedef std::unique_ptr<SectionBase> SecPtr;
335 typedef std::unique_ptr<Segment> SegPtr;
336
337 uint64_t TotalSize;
338
339public:
340 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
341 : Object<ELFT>(Obj) {}
342 void finalize() override;
343 size_t totalSize() const override;
344 void write(llvm::FileOutputBuffer &Out) const override;
345};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000346#endif