blob: 9ee8b7377a0abd7a5e17c6d1900d1c3cd4f315d8 [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;
21
22class SectionBase {
23public:
24 llvm::StringRef Name;
25 Segment *ParentSegment = nullptr;
26 uint64_t HeaderOffset;
27 uint64_t OriginalOffset;
28 uint32_t Index;
29
30 uint64_t Addr = 0;
31 uint64_t Align = 1;
32 uint32_t EntrySize = 0;
33 uint64_t Flags = 0;
34 uint64_t Info = 0;
35 uint64_t Link = llvm::ELF::SHN_UNDEF;
36 uint64_t NameIndex = 0;
37 uint64_t Offset = 0;
38 uint64_t Size = 0;
39 uint64_t Type = llvm::ELF::SHT_NULL;
40
41 virtual ~SectionBase() {}
42 virtual void finalize();
43 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
44 virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0;
45};
46
47class Segment {
48private:
49 struct SectionCompare {
50 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
51 // Some sections might have the same address if one of them is empty. To
52 // fix this we can use the lexicographic ordering on ->Addr and the
53 // address of the actully stored section.
54 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
55 return Lhs < Rhs;
56 return Lhs->OriginalOffset < Rhs->OriginalOffset;
57 }
58 };
59
60 std::set<const SectionBase *, SectionCompare> Sections;
Petr Hosekc4df10e2017-08-04 21:09:26 +000061 llvm::ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +000062
63public:
64 uint64_t Align;
65 uint64_t FileSize;
66 uint32_t Flags;
67 uint32_t Index;
68 uint64_t MemSize;
69 uint64_t Offset;
70 uint64_t PAddr;
71 uint64_t Type;
72 uint64_t VAddr;
73
Petr Hosek3f383832017-08-26 01:32:20 +000074 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +000075 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +000076
Petr Hosekc4df10e2017-08-04 21:09:26 +000077 Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000078 void finalize();
79 const SectionBase *firstSection() const {
80 if (!Sections.empty())
81 return *Sections.begin();
82 return nullptr;
83 }
84 void addSection(const SectionBase *sec) { Sections.insert(sec); }
85 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
Petr Hosekc4df10e2017-08-04 21:09:26 +000086 void writeSegment(llvm::FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +000087};
88
89class Section : public SectionBase {
90private:
91 llvm::ArrayRef<uint8_t> Contents;
92
93public:
94 Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
95 void writeSection(llvm::FileOutputBuffer &Out) const override;
96};
97
98// This is just a wraper around a StringTableBuilder that implements SectionBase
99class StringTableSection : public SectionBase {
100private:
101 llvm::StringTableBuilder StrTabBuilder;
102
103public:
104 StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
105 Type = llvm::ELF::SHT_STRTAB;
106 }
107
108 void addString(llvm::StringRef Name);
109 uint32_t findIndex(llvm::StringRef Name) const;
110 void finalize() override;
111 void writeSection(llvm::FileOutputBuffer &Out) const override;
112 static bool classof(const SectionBase *S) {
113 return S->Type == llvm::ELF::SHT_STRTAB;
114 }
115};
116
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000117// Symbols have a st_shndx field that normally stores an index but occasionally
118// stores a different special value. This enum keeps track of what the st_shndx
119// field means. Most of the values are just copies of the special SHN_* values.
120// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
121enum SymbolShndxType {
122 SYMBOL_SIMPLE_INDEX = 0,
123 SYMBOL_ABS = llvm::ELF::SHN_ABS,
124 SYMBOL_COMMON = llvm::ELF::SHN_COMMON,
125 SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON,
126 SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2,
127 SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4,
128 SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8,
129};
130
Petr Hosek79cee9e2017-08-29 02:12:03 +0000131struct Symbol {
132 uint8_t Binding;
133 SectionBase *DefinedIn;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000134 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000135 uint32_t Index;
136 llvm::StringRef Name;
137 uint32_t NameIndex;
138 uint64_t Size;
139 uint8_t Type;
140 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000141
142 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000143};
144
145class SymbolTableSection : public SectionBase {
146protected:
147 std::vector<std::unique_ptr<Symbol>> Symbols;
148 StringTableSection *SymbolNames;
149
150public:
151 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
152 void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000153 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
154 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000155 void addSymbolNames();
156 const Symbol *getSymbolByIndex(uint32_t Index) const;
157 void finalize() override;
158 static bool classof(const SectionBase *S) {
159 return S->Type == llvm::ELF::SHT_SYMTAB;
160 }
161};
162
163// Only writeSection depends on the ELF type so we implement it in a subclass.
164template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
165 void writeSection(llvm::FileOutputBuffer &Out) const override;
166};
167
Petr Hosekd7df9b22017-09-06 23:41:02 +0000168struct Relocation {
169 const Symbol *RelocSymbol;
170 uint64_t Offset;
171 uint64_t Addend;
172 uint32_t Type;
173};
174
175template <class ELFT> class RelocationSection : public SectionBase {
176private:
177 typedef typename ELFT::Rel Elf_Rel;
178 typedef typename ELFT::Rela Elf_Rela;
179
180 std::vector<Relocation> Relocations;
181 SymbolTableSection *Symbols;
182 SectionBase *SecToApplyRel;
183
184 template <class T> void writeRel(T *Buf) const;
185
186public:
187 void setSymTab(SymbolTableSection *StrTab) { Symbols = StrTab; }
188 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
189 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
190 void finalize() override;
191 void writeSection(llvm::FileOutputBuffer &Out) const override;
192 static bool classof(const SectionBase *S) {
193 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
194 }
195};
196
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000197class SectionWithStrTab : public Section {
198private:
199 StringTableSection *StrTab;
200
201public:
202 SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {}
203 void setStrTab(StringTableSection *StringTable) { StrTab = StringTable; }
204 void finalize() override;
205 static bool classof(const SectionBase *S);
206};
207
208class DynamicSymbolTableSection : public SectionWithStrTab {
209public:
210 DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data)
211 : SectionWithStrTab(Data) {}
212 static bool classof(const SectionBase *S) {
213 return S->Type == llvm::ELF::SHT_DYNSYM;
214 }
215};
216
217class DynamicSection : public SectionWithStrTab {
218public:
219 DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
220 static bool classof(const SectionBase *S) {
221 return S->Type == llvm::ELF::SHT_DYNAMIC;
222 }
223};
224
Petr Hosek05a04cb2017-08-01 00:33:58 +0000225template <class ELFT> class Object {
226private:
227 typedef std::unique_ptr<SectionBase> SecPtr;
228 typedef std::unique_ptr<Segment> SegPtr;
229
230 typedef typename ELFT::Shdr Elf_Shdr;
231 typedef typename ELFT::Ehdr Elf_Ehdr;
232 typedef typename ELFT::Phdr Elf_Phdr;
233
Petr Hosek79cee9e2017-08-29 02:12:03 +0000234 void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
235 SymbolTableSection *SymTab);
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000236 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
237 const Elf_Shdr &Shdr);
238 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
239 void readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000240
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000241 SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg);
242
243 template <class T>
244 T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg,
245 llvm::Twine TypeErrMsg);
246
Petr Hosekc4df10e2017-08-04 21:09:26 +0000247protected:
248 StringTableSection *SectionNames;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000249 SymbolTableSection *SymbolTable;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000250 std::vector<SecPtr> Sections;
251 std::vector<SegPtr> Segments;
252
Petr Hosek05a04cb2017-08-01 00:33:58 +0000253 void writeHeader(llvm::FileOutputBuffer &Out) const;
254 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
255 void writeSectionData(llvm::FileOutputBuffer &Out) const;
256 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
257
258public:
259 uint8_t Ident[16];
260 uint64_t Entry;
261 uint64_t SHOffset;
262 uint32_t Type;
263 uint32_t Machine;
264 uint32_t Version;
265 uint32_t Flags;
266
267 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000268 virtual size_t totalSize() const = 0;
269 virtual void finalize() = 0;
270 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
271 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000272};
273
Petr Hosekc4df10e2017-08-04 21:09:26 +0000274template <class ELFT> class ELFObject : public Object<ELFT> {
275private:
276 typedef std::unique_ptr<SectionBase> SecPtr;
277 typedef std::unique_ptr<Segment> SegPtr;
278
279 typedef typename ELFT::Shdr Elf_Shdr;
280 typedef typename ELFT::Ehdr Elf_Ehdr;
281 typedef typename ELFT::Phdr Elf_Phdr;
282
283 void sortSections();
284 void assignOffsets();
285
286public:
287 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
288 void finalize() override;
289 size_t totalSize() const override;
290 void write(llvm::FileOutputBuffer &Out) const override;
291};
292
293template <class ELFT> class BinaryObject : public Object<ELFT> {
294private:
295 typedef std::unique_ptr<SectionBase> SecPtr;
296 typedef std::unique_ptr<Segment> SegPtr;
297
298 uint64_t TotalSize;
299
300public:
301 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
302 : Object<ELFT>(Obj) {}
303 void finalize() override;
304 size_t totalSize() const override;
305 void write(llvm::FileOutputBuffer &Out) const override;
306};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000307#endif