blob: dff3fc40893b21d3ce39933d8f612183a3ef5e2b [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;
75
Petr Hosekc4df10e2017-08-04 21:09:26 +000076 Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000077 void finalize();
78 const SectionBase *firstSection() const {
79 if (!Sections.empty())
80 return *Sections.begin();
81 return nullptr;
82 }
83 void addSection(const SectionBase *sec) { Sections.insert(sec); }
84 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
Petr Hosekc4df10e2017-08-04 21:09:26 +000085 void writeSegment(llvm::FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +000086};
87
88class Section : public SectionBase {
89private:
90 llvm::ArrayRef<uint8_t> Contents;
91
92public:
93 Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
94 void writeSection(llvm::FileOutputBuffer &Out) const override;
95};
96
97// This is just a wraper around a StringTableBuilder that implements SectionBase
98class StringTableSection : public SectionBase {
99private:
100 llvm::StringTableBuilder StrTabBuilder;
101
102public:
103 StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
104 Type = llvm::ELF::SHT_STRTAB;
105 }
106
107 void addString(llvm::StringRef Name);
108 uint32_t findIndex(llvm::StringRef Name) const;
109 void finalize() override;
110 void writeSection(llvm::FileOutputBuffer &Out) const override;
111 static bool classof(const SectionBase *S) {
112 return S->Type == llvm::ELF::SHT_STRTAB;
113 }
114};
115
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000116// Symbols have a st_shndx field that normally stores an index but occasionally
117// stores a different special value. This enum keeps track of what the st_shndx
118// field means. Most of the values are just copies of the special SHN_* values.
119// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
120enum SymbolShndxType {
121 SYMBOL_SIMPLE_INDEX = 0,
122 SYMBOL_ABS = llvm::ELF::SHN_ABS,
123 SYMBOL_COMMON = llvm::ELF::SHN_COMMON,
124 SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON,
125 SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2,
126 SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4,
127 SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8,
128};
129
Petr Hosek79cee9e2017-08-29 02:12:03 +0000130struct Symbol {
131 uint8_t Binding;
132 SectionBase *DefinedIn;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000133 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000134 uint32_t Index;
135 llvm::StringRef Name;
136 uint32_t NameIndex;
137 uint64_t Size;
138 uint8_t Type;
139 uint64_t Value;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000140
141 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000142};
143
144class SymbolTableSection : public SectionBase {
145protected:
146 std::vector<std::unique_ptr<Symbol>> Symbols;
147 StringTableSection *SymbolNames;
148
149public:
150 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
151 void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000152 SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
153 uint64_t Sz);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000154 void addSymbolNames();
155 const Symbol *getSymbolByIndex(uint32_t Index) const;
156 void finalize() override;
157 static bool classof(const SectionBase *S) {
158 return S->Type == llvm::ELF::SHT_SYMTAB;
159 }
160};
161
162// Only writeSection depends on the ELF type so we implement it in a subclass.
163template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
164 void writeSection(llvm::FileOutputBuffer &Out) const override;
165};
166
Petr Hosekd7df9b22017-09-06 23:41:02 +0000167struct Relocation {
168 const Symbol *RelocSymbol;
169 uint64_t Offset;
170 uint64_t Addend;
171 uint32_t Type;
172};
173
174template <class ELFT> class RelocationSection : public SectionBase {
175private:
176 typedef typename ELFT::Rel Elf_Rel;
177 typedef typename ELFT::Rela Elf_Rela;
178
179 std::vector<Relocation> Relocations;
180 SymbolTableSection *Symbols;
181 SectionBase *SecToApplyRel;
182
183 template <class T> void writeRel(T *Buf) const;
184
185public:
186 void setSymTab(SymbolTableSection *StrTab) { Symbols = StrTab; }
187 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
188 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
189 void finalize() override;
190 void writeSection(llvm::FileOutputBuffer &Out) const override;
191 static bool classof(const SectionBase *S) {
192 return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
193 }
194};
195
Petr Hosek05a04cb2017-08-01 00:33:58 +0000196template <class ELFT> class Object {
197private:
198 typedef std::unique_ptr<SectionBase> SecPtr;
199 typedef std::unique_ptr<Segment> SegPtr;
200
201 typedef typename ELFT::Shdr Elf_Shdr;
202 typedef typename ELFT::Ehdr Elf_Ehdr;
203 typedef typename ELFT::Phdr Elf_Phdr;
204
Petr Hosek79cee9e2017-08-29 02:12:03 +0000205 void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
206 SymbolTableSection *SymTab);
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000207 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
208 const Elf_Shdr &Shdr);
209 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
210 void readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000211
212protected:
213 StringTableSection *SectionNames;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000214 SymbolTableSection *SymbolTable;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000215 std::vector<SecPtr> Sections;
216 std::vector<SegPtr> Segments;
217
Petr Hosek05a04cb2017-08-01 00:33:58 +0000218 void writeHeader(llvm::FileOutputBuffer &Out) const;
219 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
220 void writeSectionData(llvm::FileOutputBuffer &Out) const;
221 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
222
223public:
224 uint8_t Ident[16];
225 uint64_t Entry;
226 uint64_t SHOffset;
227 uint32_t Type;
228 uint32_t Machine;
229 uint32_t Version;
230 uint32_t Flags;
231
232 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000233 virtual size_t totalSize() const = 0;
234 virtual void finalize() = 0;
235 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
236 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000237};
238
Petr Hosekc4df10e2017-08-04 21:09:26 +0000239template <class ELFT> class ELFObject : public Object<ELFT> {
240private:
241 typedef std::unique_ptr<SectionBase> SecPtr;
242 typedef std::unique_ptr<Segment> SegPtr;
243
244 typedef typename ELFT::Shdr Elf_Shdr;
245 typedef typename ELFT::Ehdr Elf_Ehdr;
246 typedef typename ELFT::Phdr Elf_Phdr;
247
248 void sortSections();
249 void assignOffsets();
250
251public:
252 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
253 void finalize() override;
254 size_t totalSize() const override;
255 void write(llvm::FileOutputBuffer &Out) const override;
256};
257
258template <class ELFT> class BinaryObject : public Object<ELFT> {
259private:
260 typedef std::unique_ptr<SectionBase> SecPtr;
261 typedef std::unique_ptr<Segment> SegPtr;
262
263 uint64_t TotalSize;
264
265public:
266 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
267 : Object<ELFT>(Obj) {}
268 void finalize() override;
269 size_t totalSize() const override;
270 void write(llvm::FileOutputBuffer &Out) const override;
271};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000272#endif