blob: 41c9d7b4a3b9aea5dfcf4ec17036559e9974b7ba [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 void finalize();
97 const SectionBase *firstSection() const {
98 if (!Sections.empty())
99 return *Sections.begin();
100 return nullptr;
101 }
102 void addSection(const SectionBase *sec) { Sections.insert(sec); }
103 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000104 void writeSegment(llvm::FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000105};
106
107class Section : public SectionBase {
108private:
109 llvm::ArrayRef<uint8_t> Contents;
110
111public:
112 Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
113 void writeSection(llvm::FileOutputBuffer &Out) const override;
114};
115
116// This is just a wraper around a StringTableBuilder that implements SectionBase
117class StringTableSection : public SectionBase {
118private:
119 llvm::StringTableBuilder StrTabBuilder;
120
121public:
122 StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
123 Type = llvm::ELF::SHT_STRTAB;
124 }
125
126 void addString(llvm::StringRef Name);
127 uint32_t findIndex(llvm::StringRef Name) const;
128 void finalize() override;
129 void writeSection(llvm::FileOutputBuffer &Out) const override;
130 static bool classof(const SectionBase *S) {
131 return S->Type == llvm::ELF::SHT_STRTAB;
132 }
133};
134
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000135// Symbols have a st_shndx field that normally stores an index but occasionally
136// stores a different special value. This enum keeps track of what the st_shndx
137// field means. Most of the values are just copies of the special SHN_* values.
138// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
139enum SymbolShndxType {
140 SYMBOL_SIMPLE_INDEX = 0,
141 SYMBOL_ABS = llvm::ELF::SHN_ABS,
142 SYMBOL_COMMON = llvm::ELF::SHN_COMMON,
143 SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON,
144 SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2,
145 SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4,
146 SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8,
147};
148
Petr Hosek79cee9e2017-08-29 02:12:03 +0000149struct Symbol {
150 uint8_t Binding;
151 SectionBase *DefinedIn;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000152 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000153 uint32_t Index;
154 llvm::StringRef Name;
155 uint32_t NameIndex;
156 uint64_t Size;
157 uint8_t Type;
158 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000159
160 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000161};
162
163class SymbolTableSection : public SectionBase {
164protected:
165 std::vector<std::unique_ptr<Symbol>> Symbols;
166 StringTableSection *SymbolNames;
167
168public:
169 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
170 void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000171 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
172 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000173 void addSymbolNames();
174 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000175 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000176 void finalize() override;
177 static bool classof(const SectionBase *S) {
178 return S->Type == llvm::ELF::SHT_SYMTAB;
179 }
180};
181
182// Only writeSection depends on the ELF type so we implement it in a subclass.
183template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
184 void writeSection(llvm::FileOutputBuffer &Out) const override;
185};
186
Petr Hosekd7df9b22017-09-06 23:41:02 +0000187struct Relocation {
188 const Symbol *RelocSymbol;
189 uint64_t Offset;
190 uint64_t Addend;
191 uint32_t Type;
192};
193
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000194template <class SymTabType> class RelocationSectionBase : public SectionBase {
195private:
196 SymTabType *Symbols;
197 SectionBase *SecToApplyRel;
198
199public:
200 void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
201 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
202 void initialize(SectionTableRef SecTable) override;
203 void finalize() override;
204};
205
206template <class ELFT>
207class RelocationSection : public RelocationSectionBase<SymbolTableSection> {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000208private:
209 typedef typename ELFT::Rel Elf_Rel;
210 typedef typename ELFT::Rela Elf_Rela;
211
212 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000213
214 template <class T> void writeRel(T *Buf) const;
215
216public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000217 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Petr Hosekd7df9b22017-09-06 23:41:02 +0000218 void writeSection(llvm::FileOutputBuffer &Out) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000219
Petr Hosekd7df9b22017-09-06 23:41:02 +0000220 static bool classof(const SectionBase *S) {
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000221 if (S->Flags & llvm::ELF::SHF_ALLOC)
222 return false;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000223 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
224 }
225};
226
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000227class SectionWithStrTab : public Section {
228private:
229 StringTableSection *StrTab;
230
231public:
232 SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {}
233 void setStrTab(StringTableSection *StringTable) { StrTab = StringTable; }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000234 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000235 void finalize() override;
236 static bool classof(const SectionBase *S);
237};
238
239class DynamicSymbolTableSection : public SectionWithStrTab {
240public:
241 DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data)
242 : SectionWithStrTab(Data) {}
243 static bool classof(const SectionBase *S) {
244 return S->Type == llvm::ELF::SHT_DYNSYM;
245 }
246};
247
248class DynamicSection : public SectionWithStrTab {
249public:
250 DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
251 static bool classof(const SectionBase *S) {
252 return S->Type == llvm::ELF::SHT_DYNAMIC;
253 }
254};
255
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000256class DynamicRelocationSection
257 : public RelocationSectionBase<DynamicSymbolTableSection> {
258private:
259 llvm::ArrayRef<uint8_t> Contents;
260
261public:
262 DynamicRelocationSection(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
263 void writeSection(llvm::FileOutputBuffer &Out) const override;
264 static bool classof(const SectionBase *S) {
265 if (!(S->Flags & llvm::ELF::SHF_ALLOC))
266 return false;
267 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
268 }
269};
270
Petr Hosek05a04cb2017-08-01 00:33:58 +0000271template <class ELFT> class Object {
272private:
273 typedef std::unique_ptr<SectionBase> SecPtr;
274 typedef std::unique_ptr<Segment> SegPtr;
275
276 typedef typename ELFT::Shdr Elf_Shdr;
277 typedef typename ELFT::Ehdr Elf_Ehdr;
278 typedef typename ELFT::Phdr Elf_Phdr;
279
Petr Hosek79cee9e2017-08-29 02:12:03 +0000280 void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000281 SymbolTableSection *SymTab, SectionTableRef SecTable);
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000282 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
283 const Elf_Shdr &Shdr);
284 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000285 SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000286
287protected:
288 StringTableSection *SectionNames;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000289 SymbolTableSection *SymbolTable;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000290 std::vector<SecPtr> Sections;
291 std::vector<SegPtr> Segments;
292
Petr Hosek05a04cb2017-08-01 00:33:58 +0000293 void writeHeader(llvm::FileOutputBuffer &Out) const;
294 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
295 void writeSectionData(llvm::FileOutputBuffer &Out) const;
296 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
297
298public:
299 uint8_t Ident[16];
300 uint64_t Entry;
301 uint64_t SHOffset;
302 uint32_t Type;
303 uint32_t Machine;
304 uint32_t Version;
305 uint32_t Flags;
306
307 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000308 virtual size_t totalSize() const = 0;
309 virtual void finalize() = 0;
310 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
311 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000312};
313
Petr Hosekc4df10e2017-08-04 21:09:26 +0000314template <class ELFT> class ELFObject : public Object<ELFT> {
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
323 void sortSections();
324 void assignOffsets();
325
326public:
327 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
328 void finalize() override;
329 size_t totalSize() const override;
330 void write(llvm::FileOutputBuffer &Out) const override;
331};
332
333template <class ELFT> class BinaryObject : public Object<ELFT> {
334private:
335 typedef std::unique_ptr<SectionBase> SecPtr;
336 typedef std::unique_ptr<Segment> SegPtr;
337
338 uint64_t TotalSize;
339
340public:
341 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
342 : Object<ELFT>(Obj) {}
343 void finalize() override;
344 size_t totalSize() const override;
345 void write(llvm::FileOutputBuffer &Out) const override;
346};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000347#endif