blob: ca17d183076dc6b7299c59eecfa079205d03c954 [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
Petr Hosek05a04cb2017-08-01 00:33:58 +0000197template <class ELFT> class Object {
198private:
199 typedef std::unique_ptr<SectionBase> SecPtr;
200 typedef std::unique_ptr<Segment> SegPtr;
201
202 typedef typename ELFT::Shdr Elf_Shdr;
203 typedef typename ELFT::Ehdr Elf_Ehdr;
204 typedef typename ELFT::Phdr Elf_Phdr;
205
Petr Hosek79cee9e2017-08-29 02:12:03 +0000206 void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
207 SymbolTableSection *SymTab);
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000208 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
209 const Elf_Shdr &Shdr);
210 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
211 void readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000212
213protected:
214 StringTableSection *SectionNames;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000215 SymbolTableSection *SymbolTable;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000216 std::vector<SecPtr> Sections;
217 std::vector<SegPtr> Segments;
218
Petr Hosek05a04cb2017-08-01 00:33:58 +0000219 void writeHeader(llvm::FileOutputBuffer &Out) const;
220 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
221 void writeSectionData(llvm::FileOutputBuffer &Out) const;
222 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
223
224public:
225 uint8_t Ident[16];
226 uint64_t Entry;
227 uint64_t SHOffset;
228 uint32_t Type;
229 uint32_t Machine;
230 uint32_t Version;
231 uint32_t Flags;
232
233 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000234 virtual size_t totalSize() const = 0;
235 virtual void finalize() = 0;
236 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
237 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000238};
239
Petr Hosekc4df10e2017-08-04 21:09:26 +0000240template <class ELFT> class ELFObject : public Object<ELFT> {
241private:
242 typedef std::unique_ptr<SectionBase> SecPtr;
243 typedef std::unique_ptr<Segment> SegPtr;
244
245 typedef typename ELFT::Shdr Elf_Shdr;
246 typedef typename ELFT::Ehdr Elf_Ehdr;
247 typedef typename ELFT::Phdr Elf_Phdr;
248
249 void sortSections();
250 void assignOffsets();
251
252public:
253 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
254 void finalize() override;
255 size_t totalSize() const override;
256 void write(llvm::FileOutputBuffer &Out) const override;
257};
258
259template <class ELFT> class BinaryObject : public Object<ELFT> {
260private:
261 typedef std::unique_ptr<SectionBase> SecPtr;
262 typedef std::unique_ptr<Segment> SegPtr;
263
264 uint64_t TotalSize;
265
266public:
267 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
268 : Object<ELFT>(Obj) {}
269 void finalize() override;
270 size_t totalSize() const override;
271 void write(llvm::FileOutputBuffer &Out) const override;
272};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000273#endif