blob: 726f8043bf38b471f4cc516a8f7ed1e6e80af642 [file] [log] [blame]
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001//===- ELFObjectFile.cpp - ELF object file implementation -------*- 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//
Eli Bendersky24973c12012-01-22 09:01:03 +000010// This file defines the ELFObjectFile and DyldELFObject classes.
Michael J. Spencerb84551a2011-01-20 06:38:47 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringSwitch.h"
16#include "llvm/ADT/Triple.h"
Benjamin Kramer0fcab072011-09-08 20:52:17 +000017#include "llvm/ADT/DenseMap.h"
Michael J. Spencerb84551a2011-01-20 06:38:47 +000018#include "llvm/Object/ObjectFile.h"
Eli Bendersky24973c12012-01-22 09:01:03 +000019#include "llvm/Support/Casting.h"
Michael J. Spencerb84551a2011-01-20 06:38:47 +000020#include "llvm/Support/ELF.h"
21#include "llvm/Support/Endian.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000024#include "llvm/Support/raw_ostream.h"
25#include <algorithm>
Michael J. Spencerb84551a2011-01-20 06:38:47 +000026#include <limits>
27#include <utility>
28
29using namespace llvm;
30using namespace object;
31
32// Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
33namespace {
34template<support::endianness target_endianness>
35struct ELFDataTypeTypedefHelperCommon {
36 typedef support::detail::packed_endian_specific_integral
37 <uint16_t, target_endianness, support::aligned> Elf_Half;
38 typedef support::detail::packed_endian_specific_integral
39 <uint32_t, target_endianness, support::aligned> Elf_Word;
40 typedef support::detail::packed_endian_specific_integral
41 <int32_t, target_endianness, support::aligned> Elf_Sword;
42 typedef support::detail::packed_endian_specific_integral
43 <uint64_t, target_endianness, support::aligned> Elf_Xword;
44 typedef support::detail::packed_endian_specific_integral
45 <int64_t, target_endianness, support::aligned> Elf_Sxword;
46};
47}
48
49namespace {
50template<support::endianness target_endianness, bool is64Bits>
51struct ELFDataTypeTypedefHelper;
52
53/// ELF 32bit types.
54template<support::endianness target_endianness>
55struct ELFDataTypeTypedefHelper<target_endianness, false>
56 : ELFDataTypeTypedefHelperCommon<target_endianness> {
Eli Bendersky24973c12012-01-22 09:01:03 +000057 typedef uint32_t value_type;
Michael J. Spencerb84551a2011-01-20 06:38:47 +000058 typedef support::detail::packed_endian_specific_integral
Eli Bendersky24973c12012-01-22 09:01:03 +000059 <value_type, target_endianness, support::aligned> Elf_Addr;
Michael J. Spencerb84551a2011-01-20 06:38:47 +000060 typedef support::detail::packed_endian_specific_integral
Eli Bendersky24973c12012-01-22 09:01:03 +000061 <value_type, target_endianness, support::aligned> Elf_Off;
Michael J. Spencerb84551a2011-01-20 06:38:47 +000062};
63
64/// ELF 64bit types.
65template<support::endianness target_endianness>
66struct ELFDataTypeTypedefHelper<target_endianness, true>
67 : ELFDataTypeTypedefHelperCommon<target_endianness>{
Eli Bendersky24973c12012-01-22 09:01:03 +000068 typedef uint64_t value_type;
Michael J. Spencerb84551a2011-01-20 06:38:47 +000069 typedef support::detail::packed_endian_specific_integral
Eli Bendersky24973c12012-01-22 09:01:03 +000070 <value_type, target_endianness, support::aligned> Elf_Addr;
Michael J. Spencerb84551a2011-01-20 06:38:47 +000071 typedef support::detail::packed_endian_specific_integral
Eli Bendersky24973c12012-01-22 09:01:03 +000072 <value_type, target_endianness, support::aligned> Elf_Off;
Michael J. Spencerb84551a2011-01-20 06:38:47 +000073};
74}
75
76// I really don't like doing this, but the alternative is copypasta.
77#define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
78typedef typename \
79 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
80typedef typename \
81 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
82typedef typename \
83 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
84typedef typename \
85 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
86typedef typename \
87 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
88typedef typename \
89 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
90typedef typename \
91 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
92
93 // Section header.
94namespace {
95template<support::endianness target_endianness, bool is64Bits>
96struct Elf_Shdr_Base;
97
98template<support::endianness target_endianness>
99struct Elf_Shdr_Base<target_endianness, false> {
100 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
101 Elf_Word sh_name; // Section name (index into string table)
102 Elf_Word sh_type; // Section type (SHT_*)
103 Elf_Word sh_flags; // Section flags (SHF_*)
104 Elf_Addr sh_addr; // Address where section is to be loaded
105 Elf_Off sh_offset; // File offset of section data, in bytes
106 Elf_Word sh_size; // Size of section, in bytes
107 Elf_Word sh_link; // Section type-specific header table index link
108 Elf_Word sh_info; // Section type-specific extra information
109 Elf_Word sh_addralign;// Section address alignment
110 Elf_Word sh_entsize; // Size of records contained within the section
111};
112
113template<support::endianness target_endianness>
114struct Elf_Shdr_Base<target_endianness, true> {
115 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
116 Elf_Word sh_name; // Section name (index into string table)
117 Elf_Word sh_type; // Section type (SHT_*)
118 Elf_Xword sh_flags; // Section flags (SHF_*)
119 Elf_Addr sh_addr; // Address where section is to be loaded
120 Elf_Off sh_offset; // File offset of section data, in bytes
121 Elf_Xword sh_size; // Size of section, in bytes
122 Elf_Word sh_link; // Section type-specific header table index link
123 Elf_Word sh_info; // Section type-specific extra information
124 Elf_Xword sh_addralign;// Section address alignment
125 Elf_Xword sh_entsize; // Size of records contained within the section
126};
127
128template<support::endianness target_endianness, bool is64Bits>
129struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> {
130 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize;
131 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size;
132
133 /// @brief Get the number of entities this section contains if it has any.
134 unsigned getEntityCount() const {
135 if (sh_entsize == 0)
136 return 0;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000137 return sh_size / sh_entsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000138 }
139};
140}
141
142namespace {
143template<support::endianness target_endianness, bool is64Bits>
144struct Elf_Sym_Base;
145
146template<support::endianness target_endianness>
147struct Elf_Sym_Base<target_endianness, false> {
148 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
149 Elf_Word st_name; // Symbol name (index into string table)
150 Elf_Addr st_value; // Value or address associated with the symbol
151 Elf_Word st_size; // Size of the symbol
152 unsigned char st_info; // Symbol's type and binding attributes
153 unsigned char st_other; // Must be zero; reserved
154 Elf_Half st_shndx; // Which section (header table index) it's defined in
155};
156
157template<support::endianness target_endianness>
158struct Elf_Sym_Base<target_endianness, true> {
159 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
160 Elf_Word st_name; // Symbol name (index into string table)
161 unsigned char st_info; // Symbol's type and binding attributes
162 unsigned char st_other; // Must be zero; reserved
163 Elf_Half st_shndx; // Which section (header table index) it's defined in
164 Elf_Addr st_value; // Value or address associated with the symbol
165 Elf_Xword st_size; // Size of the symbol
166};
167
168template<support::endianness target_endianness, bool is64Bits>
169struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> {
170 using Elf_Sym_Base<target_endianness, is64Bits>::st_info;
171
172 // These accessors and mutators correspond to the ELF32_ST_BIND,
173 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
174 unsigned char getBinding() const { return st_info >> 4; }
175 unsigned char getType() const { return st_info & 0x0f; }
176 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
177 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
178 void setBindingAndType(unsigned char b, unsigned char t) {
179 st_info = (b << 4) + (t & 0x0f);
180 }
181};
182}
183
184namespace {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000185template<support::endianness target_endianness, bool is64Bits, bool isRela>
186struct Elf_Rel_Base;
187
188template<support::endianness target_endianness>
189struct Elf_Rel_Base<target_endianness, false, false> {
190 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
191 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
192 Elf_Word r_info; // Symbol table index and type of relocation to apply
193};
194
195template<support::endianness target_endianness>
196struct Elf_Rel_Base<target_endianness, true, false> {
197 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
198 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
199 Elf_Xword r_info; // Symbol table index and type of relocation to apply
200};
201
202template<support::endianness target_endianness>
203struct Elf_Rel_Base<target_endianness, false, true> {
204 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
205 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
206 Elf_Word r_info; // Symbol table index and type of relocation to apply
207 Elf_Sword r_addend; // Compute value for relocatable field by adding this
208};
209
210template<support::endianness target_endianness>
211struct Elf_Rel_Base<target_endianness, true, true> {
212 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
213 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
214 Elf_Xword r_info; // Symbol table index and type of relocation to apply
215 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
216};
217
218template<support::endianness target_endianness, bool is64Bits, bool isRela>
219struct Elf_Rel_Impl;
220
221template<support::endianness target_endianness, bool isRela>
222struct Elf_Rel_Impl<target_endianness, true, isRela>
223 : Elf_Rel_Base<target_endianness, true, isRela> {
224 using Elf_Rel_Base<target_endianness, true, isRela>::r_info;
225 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
226
227 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
228 // and ELF64_R_INFO macros defined in the ELF specification:
229 uint64_t getSymbol() const { return (r_info >> 32); }
230 unsigned char getType() const {
231 return (unsigned char) (r_info & 0xffffffffL);
232 }
233 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
234 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
235 void setSymbolAndType(uint64_t s, unsigned char t) {
236 r_info = (s << 32) + (t&0xffffffffL);
237 }
238};
239
240template<support::endianness target_endianness, bool isRela>
241struct Elf_Rel_Impl<target_endianness, false, isRela>
242 : Elf_Rel_Base<target_endianness, false, isRela> {
243 using Elf_Rel_Base<target_endianness, false, isRela>::r_info;
244 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
245
246 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
247 // and ELF32_R_INFO macros defined in the ELF specification:
248 uint32_t getSymbol() const { return (r_info >> 8); }
249 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
250 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
251 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
252 void setSymbolAndType(uint32_t s, unsigned char t) {
253 r_info = (s << 8) + t;
254 }
255};
256
257}
258
259namespace {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000260template<support::endianness target_endianness, bool is64Bits>
261class ELFObjectFile : public ObjectFile {
262 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
263
264 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
265 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000266 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
267 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000268
Eli Bendersky24973c12012-01-22 09:01:03 +0000269protected:
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000270 struct Elf_Ehdr {
271 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
272 Elf_Half e_type; // Type of file (see ET_*)
273 Elf_Half e_machine; // Required architecture for this file (see EM_*)
274 Elf_Word e_version; // Must be equal to 1
275 Elf_Addr e_entry; // Address to jump to in order to start program
276 Elf_Off e_phoff; // Program header table's file offset, in bytes
277 Elf_Off e_shoff; // Section header table's file offset, in bytes
278 Elf_Word e_flags; // Processor-specific flags
279 Elf_Half e_ehsize; // Size of ELF header, in bytes
280 Elf_Half e_phentsize;// Size of an entry in the program header table
281 Elf_Half e_phnum; // Number of entries in the program header table
282 Elf_Half e_shentsize;// Size of an entry in the section header table
283 Elf_Half e_shnum; // Number of entries in the section header table
284 Elf_Half e_shstrndx; // Section header table index of section name
285 // string table
286 bool checkMagic() const {
287 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
288 }
289 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
290 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
291 };
Eli Bendersky24973c12012-01-22 09:01:03 +0000292 // This flag is used for classof, to distinguish ELFObjectFile from
293 // its subclass. If more subclasses will be created, this flag will
294 // have to become an enum.
295 bool isDyldELFObject;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000296
Eli Bendersky24973c12012-01-22 09:01:03 +0000297private:
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000298 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
299 typedef DenseMap<unsigned, unsigned> IndexMap_t;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000300 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000301
302 const Elf_Ehdr *Header;
303 const Elf_Shdr *SectionHeaderTable;
304 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
305 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000306 Sections_t SymbolTableSections;
307 IndexMap_t SymbolTableSectionsIndexMap;
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000308 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000309
310 /// @brief Map sections to an array of relocation sections that reference
311 /// them sorted by section index.
312 RelocMap_t SectionRelocMap;
313
314 /// @brief Get the relocation section that contains \a Rel.
315 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
316 return getSection(Rel.w.b);
317 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000318
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000319 bool isRelocationHasAddend(DataRefImpl Rel) const;
320 template<typename T>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000321 const T *getEntry(uint16_t Section, uint32_t Entry) const;
322 template<typename T>
323 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000324 const Elf_Shdr *getSection(DataRefImpl index) const;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000325 const Elf_Shdr *getSection(uint32_t index) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000326 const Elf_Rel *getRel(DataRefImpl Rel) const;
327 const Elf_Rela *getRela(DataRefImpl Rela) const;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000328 const char *getString(uint32_t section, uint32_t offset) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000329 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000330 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000331
332protected:
Eli Bendersky24973c12012-01-22 09:01:03 +0000333 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
334 void validateSymbol(DataRefImpl Symb) const;
335
336protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000337 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
338 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000339 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000340 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
341 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
342 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
343 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000344 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000345 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const;
Michael J. Spencer1130a792011-10-17 20:19:29 +0000346 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000347 virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const;
348 virtual error_code getSymbolSection(DataRefImpl Symb,
349 section_iterator &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000350
Michael J. Spencer25b15772011-06-25 17:55:23 +0000351 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
352 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
353 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
354 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
355 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000356 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000357 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000358 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
359 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000360 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
361 bool &Result) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000362 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
363 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000364
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000365 virtual error_code getRelocationNext(DataRefImpl Rel,
366 RelocationRef &Res) const;
367 virtual error_code getRelocationAddress(DataRefImpl Rel,
368 uint64_t &Res) const;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000369 virtual error_code getRelocationOffset(DataRefImpl Rel,
370 uint64_t &Res) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000371 virtual error_code getRelocationSymbol(DataRefImpl Rel,
372 SymbolRef &Res) const;
373 virtual error_code getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000374 uint64_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000375 virtual error_code getRelocationTypeName(DataRefImpl Rel,
376 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000377 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
378 int64_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000379 virtual error_code getRelocationValueString(DataRefImpl Rel,
380 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000381
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000382public:
Michael J. Spencer001c9202011-06-25 17:54:50 +0000383 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000384 virtual symbol_iterator begin_symbols() const;
385 virtual symbol_iterator end_symbols() const;
386 virtual section_iterator begin_sections() const;
387 virtual section_iterator end_sections() const;
388
389 virtual uint8_t getBytesInAddress() const;
390 virtual StringRef getFileFormatName() const;
391 virtual unsigned getArch() const;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000392
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000393 uint64_t getNumSections() const;
394 uint64_t getStringTableIndex() const;
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000395 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000396 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
Michael J. Spencerab6bcf32011-10-17 23:53:37 +0000397
Eli Bendersky24973c12012-01-22 09:01:03 +0000398 // Methods for type inquiry through isa, cast, and dyn_cast
399 bool isDyldType() const { return isDyldELFObject; }
Michael J. Spencerab6bcf32011-10-17 23:53:37 +0000400 static inline bool classof(const Binary *v) {
Eli Bendersky24973c12012-01-22 09:01:03 +0000401 return v->getType() == Binary::isELF;
Michael J. Spencerab6bcf32011-10-17 23:53:37 +0000402 }
403 static inline bool classof(const ELFObjectFile *v) { return true; }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000404};
405} // end namespace
406
407template<support::endianness target_endianness, bool is64Bits>
408void ELFObjectFile<target_endianness, is64Bits>
409 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000410 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000411 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000412 // FIXME: We really need to do proper error handling in the case of an invalid
413 // input file. Because we don't use exceptions, I think we'll just pass
414 // an error object around.
415 if (!( symb
416 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000417 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000418 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000419 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000420 + SymbolTableSection->sh_offset
421 + SymbolTableSection->sh_size)))
422 // FIXME: Proper error handling.
423 report_fatal_error("Symb must point to a valid symbol!");
424}
425
426template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000427error_code ELFObjectFile<target_endianness, is64Bits>
428 ::getSymbolNext(DataRefImpl Symb,
429 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000430 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000431 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000432
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000433 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000434 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000435 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000436 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000437 ++Symb.d.b;
438 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000439 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000440 if (Symb.d.b >= SymbolTableSections.size()) {
441 Symb.d.a = std::numeric_limits<uint32_t>::max();
442 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000443 }
444 }
445
Michael J. Spencer25b15772011-06-25 17:55:23 +0000446 Result = SymbolRef(Symb, this);
447 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000448}
449
450template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000451error_code ELFObjectFile<target_endianness, is64Bits>
452 ::getSymbolName(DataRefImpl Symb,
453 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000454 validateSymbol(Symb);
Nick Lewycky15c3f722011-10-11 02:57:48 +0000455 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000456 return getSymbolName(symb, Result);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000457}
458
459template<support::endianness target_endianness, bool is64Bits>
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000460ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits>
Nick Lewycky15c3f722011-10-11 02:57:48 +0000461 ::getSymbolTableIndex(const Elf_Sym *symb) const {
462 if (symb->st_shndx == ELF::SHN_XINDEX)
463 return ExtendedSymbolTable.lookup(symb);
464 return symb->st_shndx;
465}
466
467template<support::endianness target_endianness, bool is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000468const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
469ELFObjectFile<target_endianness, is64Bits>
470 ::getSection(const Elf_Sym *symb) const {
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000471 if (symb->st_shndx == ELF::SHN_XINDEX)
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000472 return getSection(ExtendedSymbolTable.lookup(symb));
473 if (symb->st_shndx >= ELF::SHN_LORESERVE)
474 return 0;
475 return getSection(symb->st_shndx);
476}
477
478template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000479error_code ELFObjectFile<target_endianness, is64Bits>
Danil Malyshevb0436a72011-11-29 17:40:10 +0000480 ::getSymbolFileOffset(DataRefImpl Symb,
Nick Lewycky15c3f722011-10-11 02:57:48 +0000481 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000482 validateSymbol(Symb);
483 const Elf_Sym *symb = getSymbol(Symb);
484 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000485 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000486 case ELF::SHN_COMMON:
Eli Bendersky24973c12012-01-22 09:01:03 +0000487 // Unintialized symbols have no offset in the object file
Michael J. Spencer25b15772011-06-25 17:55:23 +0000488 case ELF::SHN_UNDEF:
489 Result = UnknownAddressOrSize;
490 return object_error::success;
491 case ELF::SHN_ABS:
492 Result = symb->st_value;
493 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000494 default: Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000495 }
496
497 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000498 case ELF::STT_SECTION:
499 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
500 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000501 case ELF::STT_FUNC:
502 case ELF::STT_OBJECT:
503 case ELF::STT_NOTYPE:
Danil Malyshevb0436a72011-11-29 17:40:10 +0000504 Result = symb->st_value +
Eli Bendersky24973c12012-01-22 09:01:03 +0000505 (Section ? Section->sh_offset : 0);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000506 return object_error::success;
507 default:
508 Result = UnknownAddressOrSize;
509 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000510 }
511}
512
513template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000514error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000515 ::getSymbolAddress(DataRefImpl Symb,
516 uint64_t &Result) const {
517 validateSymbol(Symb);
518 const Elf_Sym *symb = getSymbol(Symb);
519 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000520 switch (getSymbolTableIndex(symb)) {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000521 case ELF::SHN_COMMON:
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000522 case ELF::SHN_UNDEF:
523 Result = UnknownAddressOrSize;
524 return object_error::success;
525 case ELF::SHN_ABS:
Danil Malyshevb0436a72011-11-29 17:40:10 +0000526 Result = symb->st_value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000527 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000528 default: Section = getSection(symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000529 }
Danil Malyshevb0436a72011-11-29 17:40:10 +0000530
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000531 switch (symb->getType()) {
532 case ELF::STT_SECTION:
Danil Malyshevb0436a72011-11-29 17:40:10 +0000533 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000534 return object_error::success;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000535 case ELF::STT_FUNC:
536 case ELF::STT_OBJECT:
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000537 case ELF::STT_NOTYPE:
Eli Bendersky24973c12012-01-22 09:01:03 +0000538 Result = symb->st_value + (Section ? Section->sh_addr : 0);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000539 return object_error::success;
540 default:
541 Result = UnknownAddressOrSize;
542 return object_error::success;
543 }
544}
545
546template<support::endianness target_endianness, bool is64Bits>
547error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000548 ::getSymbolSize(DataRefImpl Symb,
549 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000550 validateSymbol(Symb);
551 const Elf_Sym *symb = getSymbol(Symb);
552 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000553 Result = UnknownAddressOrSize;
554 Result = symb->st_size;
555 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000556}
557
558template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000559error_code ELFObjectFile<target_endianness, is64Bits>
560 ::getSymbolNMTypeChar(DataRefImpl Symb,
561 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000562 validateSymbol(Symb);
563 const Elf_Sym *symb = getSymbol(Symb);
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000564 const Elf_Shdr *Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000565
566 char ret = '?';
567
568 if (Section) {
569 switch (Section->sh_type) {
570 case ELF::SHT_PROGBITS:
571 case ELF::SHT_DYNAMIC:
572 switch (Section->sh_flags) {
573 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
574 ret = 't'; break;
575 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
576 ret = 'd'; break;
577 case ELF::SHF_ALLOC:
578 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
579 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
580 ret = 'r'; break;
581 }
582 break;
583 case ELF::SHT_NOBITS: ret = 'b';
584 }
585 }
586
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000587 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000588 case ELF::SHN_UNDEF:
589 if (ret == '?')
590 ret = 'U';
591 break;
592 case ELF::SHN_ABS: ret = 'a'; break;
593 case ELF::SHN_COMMON: ret = 'c'; break;
594 }
595
596 switch (symb->getBinding()) {
597 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
598 case ELF::STB_WEAK:
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000599 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000600 ret = 'w';
601 else
602 if (symb->getType() == ELF::STT_OBJECT)
603 ret = 'V';
604 else
605 ret = 'W';
606 }
607
Michael J. Spencer25b15772011-06-25 17:55:23 +0000608 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
609 StringRef name;
610 if (error_code ec = getSymbolName(Symb, name))
611 return ec;
612 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000613 .StartsWith(".debug", 'N')
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000614 .StartsWith(".note", 'n')
615 .Default('?');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000616 return object_error::success;
617 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000618
Michael J. Spencer25b15772011-06-25 17:55:23 +0000619 Result = ret;
620 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000621}
622
623template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000624error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000625 ::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000626 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000627 validateSymbol(Symb);
628 const Elf_Sym *symb = getSymbol(Symb);
629
Nick Lewycky15c3f722011-10-11 02:57:48 +0000630 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000631 Result = SymbolRef::ST_External;
632 return object_error::success;
633 }
634
635 switch (symb->getType()) {
Michael J. Spencer206d17c2011-10-17 23:55:06 +0000636 case ELF::STT_SECTION:
637 Result = SymbolRef::ST_Debug;
638 break;
639 case ELF::STT_FILE:
640 Result = SymbolRef::ST_File;
641 break;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000642 case ELF::STT_FUNC:
643 Result = SymbolRef::ST_Function;
644 break;
645 case ELF::STT_OBJECT:
646 Result = SymbolRef::ST_Data;
647 break;
648 default:
649 Result = SymbolRef::ST_Other;
650 break;
651 }
652 return object_error::success;
653}
654
655template<support::endianness target_endianness, bool is64Bits>
656error_code ELFObjectFile<target_endianness, is64Bits>
657 ::isSymbolGlobal(DataRefImpl Symb,
658 bool &Result) const {
659 validateSymbol(Symb);
660 const Elf_Sym *symb = getSymbol(Symb);
661
662 Result = symb->getBinding() == ELF::STB_GLOBAL;
663 return object_error::success;
664}
665
666template<support::endianness target_endianness, bool is64Bits>
667error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000668 ::isSymbolWeak(DataRefImpl Symb,
669 bool &Result) const {
670 validateSymbol(Symb);
671 const Elf_Sym *symb = getSymbol(Symb);
672
673 Result = symb->getBinding() == ELF::STB_WEAK;
674 return object_error::success;
675}
676
677template<support::endianness target_endianness, bool is64Bits>
678error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000679 ::isSymbolAbsolute(DataRefImpl Symb, bool &Res) const {
680 validateSymbol(Symb);
681 const Elf_Sym *symb = getSymbol(Symb);
682 Res = symb->st_shndx == ELF::SHN_ABS;
683 return object_error::success;
684}
685
686template<support::endianness target_endianness, bool is64Bits>
687error_code ELFObjectFile<target_endianness, is64Bits>
688 ::getSymbolSection(DataRefImpl Symb,
689 section_iterator &Res) const {
690 validateSymbol(Symb);
691 const Elf_Sym *symb = getSymbol(Symb);
692 const Elf_Shdr *sec = getSection(symb);
693 if (!sec)
694 Res = end_sections();
695 else {
696 DataRefImpl Sec;
697 Sec.p = reinterpret_cast<intptr_t>(sec);
698 Res = section_iterator(SectionRef(Sec, this));
699 }
700 return object_error::success;
701}
702
703template<support::endianness target_endianness, bool is64Bits>
704error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000705 ::isSymbolInternal(DataRefImpl Symb,
706 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000707 validateSymbol(Symb);
708 const Elf_Sym *symb = getSymbol(Symb);
709
710 if ( symb->getType() == ELF::STT_FILE
711 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000712 Result = true;
713 Result = false;
714 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000715}
716
717template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000718error_code ELFObjectFile<target_endianness, is64Bits>
719 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000720 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000721 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000722 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000723 Result = SectionRef(Sec, this);
724 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000725}
726
727template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000728error_code ELFObjectFile<target_endianness, is64Bits>
729 ::getSectionName(DataRefImpl Sec,
730 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000731 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000732 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
733 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000734}
735
736template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000737error_code ELFObjectFile<target_endianness, is64Bits>
738 ::getSectionAddress(DataRefImpl Sec,
739 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000740 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000741 Result = sec->sh_addr;
742 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000743}
744
745template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000746error_code ELFObjectFile<target_endianness, is64Bits>
747 ::getSectionSize(DataRefImpl Sec,
748 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000749 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000750 Result = sec->sh_size;
751 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000752}
753
754template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000755error_code ELFObjectFile<target_endianness, is64Bits>
756 ::getSectionContents(DataRefImpl Sec,
757 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000758 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000759 const char *start = (const char*)base() + sec->sh_offset;
760 Result = StringRef(start, sec->sh_size);
761 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000762}
763
764template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000765error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000766 ::getSectionAlignment(DataRefImpl Sec,
767 uint64_t &Result) const {
768 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
769 Result = sec->sh_addralign;
770 return object_error::success;
771}
772
773template<support::endianness target_endianness, bool is64Bits>
774error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000775 ::isSectionText(DataRefImpl Sec,
776 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000777 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000778 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000779 Result = true;
780 else
781 Result = false;
782 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000783}
784
785template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000786error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000787 ::isSectionData(DataRefImpl Sec,
788 bool &Result) const {
789 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
790 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
791 && sec->sh_type == ELF::SHT_PROGBITS)
792 Result = true;
793 else
794 Result = false;
795 return object_error::success;
796}
797
798template<support::endianness target_endianness, bool is64Bits>
799error_code ELFObjectFile<target_endianness, is64Bits>
800 ::isSectionBSS(DataRefImpl Sec,
801 bool &Result) const {
802 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
803 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
804 && sec->sh_type == ELF::SHT_NOBITS)
805 Result = true;
806 else
807 Result = false;
808 return object_error::success;
809}
810
811template<support::endianness target_endianness, bool is64Bits>
812error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000813 ::sectionContainsSymbol(DataRefImpl Sec,
814 DataRefImpl Symb,
815 bool &Result) const {
816 // FIXME: Unimplemented.
817 Result = false;
818 return object_error::success;
819}
820
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000821template<support::endianness target_endianness, bool is64Bits>
822relocation_iterator ELFObjectFile<target_endianness, is64Bits>
823 ::getSectionRelBegin(DataRefImpl Sec) const {
824 DataRefImpl RelData;
825 memset(&RelData, 0, sizeof(RelData));
826 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
827 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
828 if (sec != 0 && ittr != SectionRelocMap.end()) {
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +0000829 RelData.w.a = getSection(ittr->second[0])->sh_info;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000830 RelData.w.b = ittr->second[0];
831 RelData.w.c = 0;
832 }
833 return relocation_iterator(RelocationRef(RelData, this));
834}
835
836template<support::endianness target_endianness, bool is64Bits>
837relocation_iterator ELFObjectFile<target_endianness, is64Bits>
838 ::getSectionRelEnd(DataRefImpl Sec) const {
839 DataRefImpl RelData;
840 memset(&RelData, 0, sizeof(RelData));
841 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
842 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
843 if (sec != 0 && ittr != SectionRelocMap.end()) {
844 // Get the index of the last relocation section for this section.
845 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
846 const Elf_Shdr *relocsec = getSection(relocsecindex);
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +0000847 RelData.w.a = relocsec->sh_info;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000848 RelData.w.b = relocsecindex;
849 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
850 }
851 return relocation_iterator(RelocationRef(RelData, this));
852}
853
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000854// Relocations
855template<support::endianness target_endianness, bool is64Bits>
856error_code ELFObjectFile<target_endianness, is64Bits>
857 ::getRelocationNext(DataRefImpl Rel,
858 RelocationRef &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000859 ++Rel.w.c;
860 const Elf_Shdr *relocsec = getSection(Rel.w.b);
861 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
862 // We have reached the end of the relocations for this section. See if there
863 // is another relocation section.
Michael J. Spencer01a4db32011-10-07 19:46:12 +0000864 typename RelocMap_t::mapped_type relocseclist =
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000865 SectionRelocMap.lookup(getSection(Rel.w.a));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000866
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000867 // Do a binary search for the current reloc section index (which must be
868 // present). Then get the next one.
869 typename RelocMap_t::mapped_type::const_iterator loc =
870 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
871 ++loc;
872
873 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
874 // to the end iterator.
875 if (loc != relocseclist.end()) {
876 Rel.w.b = *loc;
877 Rel.w.a = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000878 }
879 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000880 Result = RelocationRef(Rel, this);
881 return object_error::success;
882}
883
884template<support::endianness target_endianness, bool is64Bits>
885error_code ELFObjectFile<target_endianness, is64Bits>
886 ::getRelocationSymbol(DataRefImpl Rel,
887 SymbolRef &Result) const {
888 uint32_t symbolIdx;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000889 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000890 switch (sec->sh_type) {
891 default :
892 report_fatal_error("Invalid section type in Rel!");
893 case ELF::SHT_REL : {
894 symbolIdx = getRel(Rel)->getSymbol();
895 break;
896 }
897 case ELF::SHT_RELA : {
898 symbolIdx = getRela(Rel)->getSymbol();
899 break;
900 }
901 }
902 DataRefImpl SymbolData;
903 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
904 if (it == SymbolTableSectionsIndexMap.end())
905 report_fatal_error("Relocation symbol table not found!");
906 SymbolData.d.a = symbolIdx;
907 SymbolData.d.b = it->second;
908 Result = SymbolRef(SymbolData, this);
909 return object_error::success;
910}
911
912template<support::endianness target_endianness, bool is64Bits>
913error_code ELFObjectFile<target_endianness, is64Bits>
914 ::getRelocationAddress(DataRefImpl Rel,
915 uint64_t &Result) const {
916 uint64_t offset;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000917 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000918 switch (sec->sh_type) {
919 default :
920 report_fatal_error("Invalid section type in Rel!");
921 case ELF::SHT_REL : {
922 offset = getRel(Rel)->r_offset;
923 break;
924 }
925 case ELF::SHT_RELA : {
926 offset = getRela(Rel)->r_offset;
927 break;
928 }
929 }
930
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000931 Result = offset;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000932 return object_error::success;
933}
934
935template<support::endianness target_endianness, bool is64Bits>
936error_code ELFObjectFile<target_endianness, is64Bits>
Danil Malyshevb0436a72011-11-29 17:40:10 +0000937 ::getRelocationOffset(DataRefImpl Rel,
938 uint64_t &Result) const {
939 uint64_t offset;
940 const Elf_Shdr *sec = getSection(Rel.w.b);
941 switch (sec->sh_type) {
942 default :
943 report_fatal_error("Invalid section type in Rel!");
944 case ELF::SHT_REL : {
945 offset = getRel(Rel)->r_offset;
946 break;
947 }
948 case ELF::SHT_RELA : {
949 offset = getRela(Rel)->r_offset;
950 break;
951 }
952 }
953
954 Result = offset - sec->sh_addr;
955 return object_error::success;
956}
957
958template<support::endianness target_endianness, bool is64Bits>
959error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000960 ::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000961 uint64_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000962 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000963 switch (sec->sh_type) {
964 default :
965 report_fatal_error("Invalid section type in Rel!");
966 case ELF::SHT_REL : {
967 Result = getRel(Rel)->getType();
968 break;
969 }
970 case ELF::SHT_RELA : {
971 Result = getRela(Rel)->getType();
972 break;
973 }
974 }
975 return object_error::success;
976}
977
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000978#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
979 case ELF::enum: res = #enum; break;
980
981template<support::endianness target_endianness, bool is64Bits>
982error_code ELFObjectFile<target_endianness, is64Bits>
983 ::getRelocationTypeName(DataRefImpl Rel,
984 SmallVectorImpl<char> &Result) const {
985 const Elf_Shdr *sec = getSection(Rel.w.b);
986 uint8_t type;
987 StringRef res;
988 switch (sec->sh_type) {
989 default :
990 return object_error::parse_failed;
991 case ELF::SHT_REL : {
992 type = getRel(Rel)->getType();
993 break;
994 }
995 case ELF::SHT_RELA : {
996 type = getRela(Rel)->getType();
997 break;
998 }
999 }
1000 switch (Header->e_machine) {
1001 case ELF::EM_X86_64:
1002 switch (type) {
1003 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
1004 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
1005 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
1006 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
1007 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
1008 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
1009 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1010 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1011 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1012 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1013 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1014 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1015 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1016 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1017 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1018 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1019 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1020 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1021 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1022 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1023 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1024 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1025 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1026 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1027 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1028 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1029 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1030 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1031 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1032 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1033 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1034 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1035 default:
1036 res = "Unknown";
1037 }
1038 break;
1039 case ELF::EM_386:
1040 switch (type) {
1041 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1042 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1043 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1044 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1045 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1046 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1047 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1048 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1049 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1050 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1051 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1052 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1053 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1054 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1055 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1056 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1057 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1058 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1059 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1060 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1061 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1062 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1063 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1064 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1065 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1066 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1067 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1068 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1069 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1070 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1071 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1072 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1073 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1074 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1075 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1076 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1077 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1078 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1079 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1080 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1081 default:
1082 res = "Unknown";
1083 }
1084 break;
1085 default:
1086 res = "Unknown";
1087 }
1088 Result.append(res.begin(), res.end());
1089 return object_error::success;
1090}
1091
1092#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1093
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001094template<support::endianness target_endianness, bool is64Bits>
1095error_code ELFObjectFile<target_endianness, is64Bits>
1096 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1097 int64_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001098 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001099 switch (sec->sh_type) {
1100 default :
1101 report_fatal_error("Invalid section type in Rel!");
1102 case ELF::SHT_REL : {
1103 Result = 0;
1104 return object_error::success;
1105 }
1106 case ELF::SHT_RELA : {
1107 Result = getRela(Rel)->r_addend;
1108 return object_error::success;
1109 }
1110 }
1111}
1112
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001113template<support::endianness target_endianness, bool is64Bits>
1114error_code ELFObjectFile<target_endianness, is64Bits>
1115 ::getRelocationValueString(DataRefImpl Rel,
1116 SmallVectorImpl<char> &Result) const {
1117 const Elf_Shdr *sec = getSection(Rel.w.b);
1118 uint8_t type;
1119 StringRef res;
1120 int64_t addend = 0;
1121 uint16_t symbol_index = 0;
1122 switch (sec->sh_type) {
1123 default :
1124 return object_error::parse_failed;
1125 case ELF::SHT_REL : {
1126 type = getRel(Rel)->getType();
1127 symbol_index = getRel(Rel)->getSymbol();
1128 // TODO: Read implicit addend from section data.
1129 break;
1130 }
1131 case ELF::SHT_RELA : {
1132 type = getRela(Rel)->getType();
1133 symbol_index = getRela(Rel)->getSymbol();
1134 addend = getRela(Rel)->r_addend;
1135 break;
1136 }
1137 }
1138 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1139 StringRef symname;
1140 if (error_code ec = getSymbolName(symb, symname))
1141 return ec;
1142 switch (Header->e_machine) {
1143 case ELF::EM_X86_64:
1144 switch (type) {
1145 case ELF::R_X86_64_32S:
1146 res = symname;
1147 break;
1148 case ELF::R_X86_64_PC32: {
1149 std::string fmtbuf;
1150 raw_string_ostream fmt(fmtbuf);
1151 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1152 fmt.flush();
1153 Result.append(fmtbuf.begin(), fmtbuf.end());
1154 }
1155 break;
1156 default:
1157 res = "Unknown";
1158 }
1159 break;
1160 default:
1161 res = "Unknown";
1162 }
1163 if (Result.empty())
1164 Result.append(res.begin(), res.end());
1165 return object_error::success;
1166}
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001167
Benjamin Kramer07ea23a2011-07-15 18:39:21 +00001168template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +00001169ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1170 , error_code &ec)
1171 : ObjectFile(Binary::isELF, Object, ec)
Eli Bendersky24973c12012-01-22 09:01:03 +00001172 , isDyldELFObject(false)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001173 , SectionHeaderTable(0)
1174 , dot_shstrtab_sec(0)
1175 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001176 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001177
1178 if (Header->e_shoff == 0)
1179 return;
1180
1181 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +00001182 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001183 uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
Eli Bendersky24973c12012-01-22 09:01:03 +00001184
1185 if ((const uint8_t *)SectionHeaderTable + SectionTableSize
1186 > base() + Data->getBufferSize()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001187 // FIXME: Proper error handling.
1188 report_fatal_error("Section table goes past end of file!");
Eli Bendersky24973c12012-01-22 09:01:03 +00001189 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001190
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001191
Nick Lewycky15c3f722011-10-11 02:57:48 +00001192 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1193 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001194 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001195 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
Nick Lewycky15c3f722011-10-11 02:57:48 +00001196 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
1197 if (SymbolTableSectionHeaderIndex)
1198 // FIXME: Proper error handling.
1199 report_fatal_error("More than one .symtab_shndx!");
1200 SymbolTableSectionHeaderIndex = sh;
1201 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001202 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001203 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001204 SymbolTableSections.push_back(sh);
1205 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001206 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +00001207 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001208 }
1209 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001210 }
1211
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001212 // Sort section relocation lists by index.
1213 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1214 e = SectionRelocMap.end(); i != e; ++i) {
1215 std::sort(i->second.begin(), i->second.end());
1216 }
1217
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001218 // Get string table sections.
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001219 dot_shstrtab_sec = getSection(getStringTableIndex());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001220 if (dot_shstrtab_sec) {
1221 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001222 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001223 [dot_shstrtab_sec->sh_size - 1] != 0)
1224 // FIXME: Proper error handling.
1225 report_fatal_error("String table must end with a null terminator!");
1226 }
1227
1228 // Merge this into the above loop.
1229 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001230 *e = i + getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001231 i != e; i += Header->e_shentsize) {
1232 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1233 if (sh->sh_type == ELF::SHT_STRTAB) {
1234 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1235 if (SectionName == ".strtab") {
1236 if (dot_strtab_sec != 0)
1237 // FIXME: Proper error handling.
1238 report_fatal_error("Already found section named .strtab!");
1239 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +00001240 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001241 if (dot_strtab[sh->sh_size - 1] != 0)
1242 // FIXME: Proper error handling.
1243 report_fatal_error("String table must end with a null terminator!");
1244 }
1245 }
1246 }
Nick Lewycky15c3f722011-10-11 02:57:48 +00001247
1248 // Build symbol name side-mapping if there is one.
1249 if (SymbolTableSectionHeaderIndex) {
1250 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1251 SymbolTableSectionHeaderIndex->sh_offset);
1252 error_code ec;
1253 for (symbol_iterator si = begin_symbols(),
1254 se = end_symbols(); si != se; si.increment(ec)) {
1255 if (ec)
1256 report_fatal_error("Fewer extended symbol table entries than symbols!");
1257 if (*ShndxTable != ELF::SHN_UNDEF)
1258 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1259 ++ShndxTable;
1260 }
1261 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001262}
1263
1264template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001265symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1266 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001267 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001268 memset(&SymbolData, 0, sizeof(SymbolData));
1269 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001270 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1271 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001272 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001273 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1274 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001275 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001276 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001277}
1278
1279template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001280symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1281 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001282 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001283 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001284 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1285 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1286 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001287}
1288
1289template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001290section_iterator ELFObjectFile<target_endianness, is64Bits>
1291 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001292 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001293 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001294 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001295 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001296}
1297
1298template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001299section_iterator ELFObjectFile<target_endianness, is64Bits>
1300 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001301 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001302 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001303 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001304 + Header->e_shoff
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001305 + (Header->e_shentsize*getNumSections()));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001306 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001307}
1308
1309template<support::endianness target_endianness, bool is64Bits>
1310uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001311 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001312}
1313
1314template<support::endianness target_endianness, bool is64Bits>
1315StringRef ELFObjectFile<target_endianness, is64Bits>
1316 ::getFileFormatName() const {
1317 switch(Header->e_ident[ELF::EI_CLASS]) {
1318 case ELF::ELFCLASS32:
1319 switch(Header->e_machine) {
1320 case ELF::EM_386:
1321 return "ELF32-i386";
1322 case ELF::EM_X86_64:
1323 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001324 case ELF::EM_ARM:
1325 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001326 default:
1327 return "ELF32-unknown";
1328 }
1329 case ELF::ELFCLASS64:
1330 switch(Header->e_machine) {
1331 case ELF::EM_386:
1332 return "ELF64-i386";
1333 case ELF::EM_X86_64:
1334 return "ELF64-x86-64";
1335 default:
1336 return "ELF64-unknown";
1337 }
1338 default:
1339 // FIXME: Proper error handling.
1340 report_fatal_error("Invalid ELFCLASS!");
1341 }
1342}
1343
1344template<support::endianness target_endianness, bool is64Bits>
1345unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1346 switch(Header->e_machine) {
1347 case ELF::EM_386:
1348 return Triple::x86;
1349 case ELF::EM_X86_64:
1350 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001351 case ELF::EM_ARM:
1352 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001353 default:
1354 return Triple::UnknownArch;
1355 }
1356}
1357
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001358template<support::endianness target_endianness, bool is64Bits>
1359uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
1360 if (Header->e_shnum == ELF::SHN_UNDEF)
1361 return SectionHeaderTable->sh_size;
1362 return Header->e_shnum;
1363}
1364
1365template<support::endianness target_endianness, bool is64Bits>
1366uint64_t
1367ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1368 if (Header->e_shnum == ELF::SHN_UNDEF) {
1369 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1370 return SectionHeaderTable->sh_link;
1371 if (Header->e_shstrndx >= getNumSections())
1372 return 0;
1373 }
1374 return Header->e_shstrndx;
1375}
1376
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001377
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001378template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001379template<typename T>
1380inline const T *
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001381ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1382 uint32_t Entry) const {
1383 return getEntry<T>(getSection(Section), Entry);
1384}
1385
1386template<support::endianness target_endianness, bool is64Bits>
1387template<typename T>
1388inline const T *
1389ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1390 uint32_t Entry) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001391 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +00001392 base()
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001393 + Section->sh_offset
1394 + (Entry * Section->sh_entsize));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001395}
1396
1397template<support::endianness target_endianness, bool is64Bits>
1398const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1399ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001400 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001401}
1402
1403template<support::endianness target_endianness, bool is64Bits>
1404const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1405ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001406 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001407}
1408
1409template<support::endianness target_endianness, bool is64Bits>
1410const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1411ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001412 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001413}
1414
1415template<support::endianness target_endianness, bool is64Bits>
1416const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1417ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001418 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001419 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001420 // FIXME: Proper error handling.
1421 report_fatal_error("Invalid symbol table section!");
1422 return sec;
1423}
1424
1425template<support::endianness target_endianness, bool is64Bits>
1426const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001427ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1428 if (index == 0)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001429 return 0;
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001430 if (!SectionHeaderTable || index >= getNumSections())
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001431 // FIXME: Proper error handling.
1432 report_fatal_error("Invalid section index!");
1433
1434 return reinterpret_cast<const Elf_Shdr *>(
1435 reinterpret_cast<const char *>(SectionHeaderTable)
1436 + (index * Header->e_shentsize));
1437}
1438
1439template<support::endianness target_endianness, bool is64Bits>
1440const char *ELFObjectFile<target_endianness, is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001441 ::getString(uint32_t section,
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001442 ELF::Elf32_Word offset) const {
1443 return getString(getSection(section), offset);
1444}
1445
1446template<support::endianness target_endianness, bool is64Bits>
1447const char *ELFObjectFile<target_endianness, is64Bits>
1448 ::getString(const Elf_Shdr *section,
1449 ELF::Elf32_Word offset) const {
1450 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1451 if (offset >= section->sh_size)
1452 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001453 report_fatal_error("Symbol name offset outside of string table!");
1454 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001455}
1456
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001457template<support::endianness target_endianness, bool is64Bits>
1458error_code ELFObjectFile<target_endianness, is64Bits>
1459 ::getSymbolName(const Elf_Sym *symb,
1460 StringRef &Result) const {
1461 if (symb->st_name == 0) {
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001462 const Elf_Shdr *section = getSection(symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001463 if (!section)
1464 Result = "";
1465 else
1466 Result = getString(dot_shstrtab_sec, section->sh_name);
1467 return object_error::success;
1468 }
1469
1470 // Use the default symbol table name section.
1471 Result = getString(dot_strtab_sec, symb->st_name);
1472 return object_error::success;
1473}
1474
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001475// EI_CLASS, EI_DATA.
1476static std::pair<unsigned char, unsigned char>
1477getElfArchType(MemoryBuffer *Object) {
1478 if (Object->getBufferSize() < ELF::EI_NIDENT)
1479 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1480 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1481 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1482}
1483
Eli Bendersky24973c12012-01-22 09:01:03 +00001484
1485namespace {
1486 template<support::endianness target_endianness, bool is64Bits>
1487 class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
1488 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
1489
1490 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
1491 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
1492 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
1493 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
1494
1495 typedef typename ELFObjectFile<target_endianness, is64Bits>::
1496 Elf_Ehdr Elf_Ehdr;
1497 Elf_Ehdr *Header;
1498
1499 // Update section headers according to the current location in memory
1500 virtual void rebaseObject(std::vector<uint8_t*> *MemoryMap);
1501 // Record memory addresses for cleanup
1502 virtual void saveAddress(std::vector<uint8_t*> *MemoryMap, uint8_t *addr);
1503
1504 protected:
1505 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
1506
1507 public:
1508 DyldELFObject(MemoryBuffer *Object, std::vector<uint8_t*> *MemoryMap,
1509 error_code &ec);
1510
1511 // Methods for type inquiry through isa, cast, and dyn_cast
1512 static inline bool classof(const Binary *v) {
1513 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
1514 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
1515 }
1516 static inline bool classof(
1517 const ELFObjectFile<target_endianness, is64Bits> *v) {
1518 return v->isDyldType();
1519 }
1520 static inline bool classof(const DyldELFObject *v) {
1521 return true;
1522 }
1523 };
1524} // end anonymous namespace
1525
1526template<support::endianness target_endianness, bool is64Bits>
1527DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
1528 std::vector<uint8_t*> *MemoryMap, error_code &ec)
1529 : ELFObjectFile<target_endianness, is64Bits>(Object, ec)
1530 , Header(0) {
1531 this->isDyldELFObject = true;
1532 Header = const_cast<Elf_Ehdr *>(
1533 reinterpret_cast<const Elf_Ehdr *>(this->base()));
1534 if (Header->e_shoff == 0)
1535 return;
1536
1537 // Mark the image as a dynamic shared library
1538 Header->e_type = ELF::ET_DYN;
1539
1540 rebaseObject(MemoryMap);
1541}
1542
1543// Walk through the ELF headers, updating virtual addresses to reflect where
1544// the object is currently loaded in memory
1545template<support::endianness target_endianness, bool is64Bits>
1546void DyldELFObject<target_endianness, is64Bits>::rebaseObject(
1547 std::vector<uint8_t*> *MemoryMap) {
1548 typedef typename ELFDataTypeTypedefHelper<
1549 target_endianness, is64Bits>::value_type addr_type;
1550
1551 uint8_t *base_p = const_cast<uint8_t *>(this->base());
1552 Elf_Shdr *sectionTable =
1553 reinterpret_cast<Elf_Shdr *>(base_p + Header->e_shoff);
1554 uint64_t numSections = this->getNumSections();
1555
1556 // Allocate memory space for NOBITS sections (such as .bss), which only exist
1557 // in memory, but don't occupy space in the object file.
1558 // Update the address in the section headers to reflect this allocation.
1559 for (uint64_t index = 0; index < numSections; index++) {
1560 Elf_Shdr *sec = reinterpret_cast<Elf_Shdr *>(
1561 reinterpret_cast<char *>(sectionTable) + index * Header->e_shentsize);
1562
1563 // Only update sections that are meant to be present in program memory
1564 if (sec->sh_flags & ELF::SHF_ALLOC) {
1565 uint8_t *addr = base_p + sec->sh_offset;
1566 if (sec->sh_type == ELF::SHT_NOBITS) {
1567 addr = static_cast<uint8_t *>(calloc(sec->sh_size, 1));
1568 saveAddress(MemoryMap, addr);
1569 }
1570 else {
1571 // FIXME: Currently memory with RWX permissions is allocated. In the
1572 // future, make sure that permissions are as necessary
1573 if (sec->sh_flags & ELF::SHF_WRITE) {
1574 // see FIXME above
1575 }
1576 if (sec->sh_flags & ELF::SHF_EXECINSTR) {
1577 // see FIXME above
1578 }
1579 }
1580 assert(sizeof(addr_type) == sizeof(intptr_t) &&
1581 "Cross-architecture ELF dy-load is not supported!");
1582 sec->sh_addr = static_cast<addr_type>(intptr_t(addr));
1583 }
1584 }
1585
1586 // Now allocate actual space for COMMON symbols, which also don't occupy
1587 // space in the object file.
1588 // We want to allocate space for all COMMON symbols at once, so the flow is:
1589 // 1. Go over all symbols, find those that are in COMMON. For each such
1590 // symbol, record its size and the value field in its symbol header in a
1591 // special vector.
1592 // 2. Allocate memory for all COMMON symbols in one fell swoop.
1593 // 3. Using the recorded information from (1), update the address fields in
1594 // the symbol headers of the COMMON symbols to reflect their allocated
1595 // address.
1596 uint64_t TotalSize = 0;
1597 std::vector<std::pair<Elf_Addr *, uint64_t> > SymbAddrInfo;
1598 error_code ec = object_error::success;
1599 for (symbol_iterator si = this->begin_symbols(),
1600 se = this->end_symbols(); si != se; si.increment(ec)) {
1601 uint64_t Size = 0;
1602 ec = si->getSize(Size);
1603 Elf_Sym* symb = const_cast<Elf_Sym*>(
1604 this->getSymbol(si->getRawDataRefImpl()));
1605 if (ec == object_error::success &&
1606 this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
1607 SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
1608 TotalSize += Size;
1609 }
1610 }
1611
1612 uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
1613 saveAddress(MemoryMap, SectionPtr);
1614
1615 typedef typename std::vector<std::pair<Elf_Addr *, uint64_t> >::iterator
1616 AddrInfoIterator;
1617 AddrInfoIterator EndIter = SymbAddrInfo.end();
1618 for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
1619 AddrIter != EndIter; ++AddrIter) {
1620 assert(sizeof(addr_type) == sizeof(intptr_t) &&
1621 "Cross-architecture ELF dy-load is not supported!");
1622 *(AddrIter->first) = static_cast<addr_type>(intptr_t(SectionPtr));
1623 SectionPtr += AddrIter->second;
1624 }
1625}
1626
1627// Record memory addresses for callers
1628template<support::endianness target_endianness, bool is64Bits>
1629void DyldELFObject<target_endianness, is64Bits>::saveAddress(
1630 std::vector<uint8_t*> *MemoryMap, uint8_t* addr) {
1631 if (MemoryMap)
1632 MemoryMap->push_back(addr);
1633 else
1634 errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
1635}
1636
1637template<support::endianness target_endianness, bool is64Bits>
1638error_code DyldELFObject<target_endianness, is64Bits>::getSymbolAddress(
1639 DataRefImpl Symb, uint64_t &Result) const {
1640 this->validateSymbol(Symb);
1641 const Elf_Sym *symb = this->getSymbol(Symb);
1642 if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
1643 Result = symb->st_value;
1644 return object_error::success;
1645 }
1646 else {
1647 return ELFObjectFile<target_endianness, is64Bits>::getSymbolAddress(
1648 Symb, Result);
1649 }
1650}
1651
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001652namespace llvm {
1653
Eli Bendersky24973c12012-01-22 09:01:03 +00001654 // Creates an in-memory object-file by default: createELFObjectFile(Buffer)
1655 // Set doDyld to true to create a live (executable/debug-worthy) image
1656 // If doDyld is true, any memory allocated for non-resident sections and
1657 // symbols is recorded in MemoryMap.
1658 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object,
1659 bool doDyld, std::vector<uint8_t *> *MemoryMap) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001660 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001661 error_code ec;
Eli Bendersky24973c12012-01-22 09:01:03 +00001662
1663 if (doDyld) {
1664 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
1665 return new DyldELFObject<support::little, false>(Object, MemoryMap, ec);
1666 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
1667 return new DyldELFObject<support::big, false>(Object, MemoryMap, ec);
1668 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
1669 return new DyldELFObject<support::big, true>(Object, MemoryMap, ec);
1670 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
1671 DyldELFObject<support::little, true> *result =
1672 new DyldELFObject<support::little, true>(Object, MemoryMap, ec);
1673
1674 // Unit testing for type inquiry
1675 bool isBinary = isa<Binary>(result);
1676 bool isDyld = isa<DyldELFObject<support::little, true> >(result);
1677 bool isFile = isa<ELFObjectFile<support::little, true> >(result);
1678 assert(isBinary && isDyld && isFile &&
1679 "Type inquiry failed for ELF object!");
1680 return result;
1681 }
1682 }
1683
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001684 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001685 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001686 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001687 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001688 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001689 return new ELFObjectFile<support::big, true>(Object, ec);
Eli Bendersky24973c12012-01-22 09:01:03 +00001690 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
1691 ELFObjectFile<support::little, true> *result =
1692 new ELFObjectFile<support::little, true>(Object, ec);
1693
1694 // Unit testing for type inquiry
1695 bool isBinary = isa<Binary>(result);
1696 bool isDyld = isa<DyldELFObject<support::little, true> >(result);
1697 bool isFile = isa<ELFObjectFile<support::little, true> >(result);
1698 assert(isBinary && isFile && !isDyld &&
1699 "Type inquiry failed for ELF object!");
1700 return result;
1701 }
1702
1703 report_fatal_error("Buffer is not an ELF object file!");
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001704 }
1705
1706} // end namespace llvm