blob: 6e849e47006ee7c5ed514a4d751944365475186c [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>
35
36 T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg,
37 llvm::Twine TypeErrMsg);
38};
Petr Hosek05a04cb2017-08-01 00:33:58 +000039
40class SectionBase {
41public:
42 llvm::StringRef Name;
43 Segment *ParentSegment = nullptr;
44 uint64_t HeaderOffset;
45 uint64_t OriginalOffset;
46 uint32_t Index;
47
48 uint64_t Addr = 0;
49 uint64_t Align = 1;
50 uint32_t EntrySize = 0;
51 uint64_t Flags = 0;
52 uint64_t Info = 0;
53 uint64_t Link = llvm::ELF::SHN_UNDEF;
54 uint64_t NameIndex = 0;
55 uint64_t Offset = 0;
56 uint64_t Size = 0;
57 uint64_t Type = llvm::ELF::SHT_NULL;
58
59 virtual ~SectionBase() {}
Jake Ehrlichf5a43772017-09-25 20:37:28 +000060 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +000061 virtual void finalize();
62 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 void finalize();
98 const SectionBase *firstSection() const {
99 if (!Sections.empty())
100 return *Sections.begin();
101 return nullptr;
102 }
103 void addSection(const SectionBase *sec) { Sections.insert(sec); }
104 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
117// This is just a wraper around a StringTableBuilder that implements SectionBase
118class StringTableSection : public SectionBase {
119private:
120 llvm::StringTableBuilder StrTabBuilder;
121
122public:
123 StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
124 Type = llvm::ELF::SHT_STRTAB;
125 }
126
127 void addString(llvm::StringRef Name);
128 uint32_t findIndex(llvm::StringRef Name) const;
129 void finalize() override;
130 void writeSection(llvm::FileOutputBuffer &Out) const override;
131 static bool classof(const SectionBase *S) {
132 return S->Type == llvm::ELF::SHT_STRTAB;
133 }
134};
135
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000136// Symbols have a st_shndx field that normally stores an index but occasionally
137// stores a different special value. This enum keeps track of what the st_shndx
138// field means. Most of the values are just copies of the special SHN_* values.
139// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
140enum SymbolShndxType {
141 SYMBOL_SIMPLE_INDEX = 0,
142 SYMBOL_ABS = llvm::ELF::SHN_ABS,
143 SYMBOL_COMMON = llvm::ELF::SHN_COMMON,
144 SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON,
145 SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2,
146 SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4,
147 SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8,
148};
149
Petr Hosek79cee9e2017-08-29 02:12:03 +0000150struct Symbol {
151 uint8_t Binding;
152 SectionBase *DefinedIn;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000153 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000154 uint32_t Index;
155 llvm::StringRef Name;
156 uint32_t NameIndex;
157 uint64_t Size;
158 uint8_t Type;
159 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000160
161 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000162};
163
164class SymbolTableSection : public SectionBase {
165protected:
166 std::vector<std::unique_ptr<Symbol>> Symbols;
167 StringTableSection *SymbolNames;
168
169public:
170 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
171 void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000172 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
173 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000174 void addSymbolNames();
175 const Symbol *getSymbolByIndex(uint32_t Index) const;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000176 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000177 void finalize() override;
178 static bool classof(const SectionBase *S) {
179 return S->Type == llvm::ELF::SHT_SYMTAB;
180 }
181};
182
183// Only writeSection depends on the ELF type so we implement it in a subclass.
184template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
185 void writeSection(llvm::FileOutputBuffer &Out) const override;
186};
187
Petr Hosekd7df9b22017-09-06 23:41:02 +0000188struct Relocation {
189 const Symbol *RelocSymbol;
190 uint64_t Offset;
191 uint64_t Addend;
192 uint32_t Type;
193};
194
195template <class ELFT> class RelocationSection : public SectionBase {
196private:
197 typedef typename ELFT::Rel Elf_Rel;
198 typedef typename ELFT::Rela Elf_Rela;
199
200 std::vector<Relocation> Relocations;
201 SymbolTableSection *Symbols;
202 SectionBase *SecToApplyRel;
203
204 template <class T> void writeRel(T *Buf) const;
205
206public:
207 void setSymTab(SymbolTableSection *StrTab) { Symbols = StrTab; }
208 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
209 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000210 void initialize(SectionTableRef SecTable) override;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000211 void finalize() override;
212 void writeSection(llvm::FileOutputBuffer &Out) const override;
213 static bool classof(const SectionBase *S) {
214 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
215 }
216};
217
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000218class SectionWithStrTab : public Section {
219private:
220 StringTableSection *StrTab;
221
222public:
223 SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {}
224 void setStrTab(StringTableSection *StringTable) { StrTab = StringTable; }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000225 void initialize(SectionTableRef SecTable) override;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000226 void finalize() override;
227 static bool classof(const SectionBase *S);
228};
229
230class DynamicSymbolTableSection : public SectionWithStrTab {
231public:
232 DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data)
233 : SectionWithStrTab(Data) {}
234 static bool classof(const SectionBase *S) {
235 return S->Type == llvm::ELF::SHT_DYNSYM;
236 }
237};
238
239class DynamicSection : public SectionWithStrTab {
240public:
241 DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
242 static bool classof(const SectionBase *S) {
243 return S->Type == llvm::ELF::SHT_DYNAMIC;
244 }
245};
246
Petr Hosek05a04cb2017-08-01 00:33:58 +0000247template <class ELFT> class Object {
248private:
249 typedef std::unique_ptr<SectionBase> SecPtr;
250 typedef std::unique_ptr<Segment> SegPtr;
251
252 typedef typename ELFT::Shdr Elf_Shdr;
253 typedef typename ELFT::Ehdr Elf_Ehdr;
254 typedef typename ELFT::Phdr Elf_Phdr;
255
Petr Hosek79cee9e2017-08-29 02:12:03 +0000256 void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000257 SymbolTableSection *SymTab, SectionTableRef SecTable);
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000258 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
259 const Elf_Shdr &Shdr);
260 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000261 SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000262
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000263 SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg);
264
265 template <class T>
266 T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg,
267 llvm::Twine TypeErrMsg);
268
Petr Hosekc4df10e2017-08-04 21:09:26 +0000269protected:
270 StringTableSection *SectionNames;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000271 SymbolTableSection *SymbolTable;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000272 std::vector<SecPtr> Sections;
273 std::vector<SegPtr> Segments;
274
Petr Hosek05a04cb2017-08-01 00:33:58 +0000275 void writeHeader(llvm::FileOutputBuffer &Out) const;
276 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
277 void writeSectionData(llvm::FileOutputBuffer &Out) const;
278 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
279
280public:
281 uint8_t Ident[16];
282 uint64_t Entry;
283 uint64_t SHOffset;
284 uint32_t Type;
285 uint32_t Machine;
286 uint32_t Version;
287 uint32_t Flags;
288
289 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000290 virtual size_t totalSize() const = 0;
291 virtual void finalize() = 0;
292 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
293 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000294};
295
Petr Hosekc4df10e2017-08-04 21:09:26 +0000296template <class ELFT> class ELFObject : public Object<ELFT> {
297private:
298 typedef std::unique_ptr<SectionBase> SecPtr;
299 typedef std::unique_ptr<Segment> SegPtr;
300
301 typedef typename ELFT::Shdr Elf_Shdr;
302 typedef typename ELFT::Ehdr Elf_Ehdr;
303 typedef typename ELFT::Phdr Elf_Phdr;
304
305 void sortSections();
306 void assignOffsets();
307
308public:
309 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
310 void finalize() override;
311 size_t totalSize() const override;
312 void write(llvm::FileOutputBuffer &Out) const override;
313};
314
315template <class ELFT> class BinaryObject : public Object<ELFT> {
316private:
317 typedef std::unique_ptr<SectionBase> SecPtr;
318 typedef std::unique_ptr<Segment> SegPtr;
319
320 uint64_t TotalSize;
321
322public:
323 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
324 : Object<ELFT>(Obj) {}
325 void finalize() override;
326 size_t totalSize() const override;
327 void write(llvm::FileOutputBuffer &Out) const override;
328};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000329#endif