blob: 1b29cf682f03ec94d69e0fe722f48dc6739d2852 [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) {
Dylan Noblesmith069df952012-02-04 02:41:39 +00001176
1177 const uint64_t FileSize = Data->getBufferSize();
1178
1179 if (sizeof(Elf_Ehdr) > FileSize)
1180 // FIXME: Proper error handling.
1181 report_fatal_error("File too short!");
1182
Michael J. Spencer001c9202011-06-25 17:54:50 +00001183 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001184
1185 if (Header->e_shoff == 0)
1186 return;
1187
Dylan Noblesmith069df952012-02-04 02:41:39 +00001188 const uint64_t SectionTableOffset = Header->e_shoff;
Eli Bendersky24973c12012-01-22 09:01:03 +00001189
Dylan Noblesmith069df952012-02-04 02:41:39 +00001190 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1191 // FIXME: Proper error handling.
1192 report_fatal_error("Section header table goes past end of file!");
1193
1194 // The getNumSections() call below depends on SectionHeaderTable being set.
1195 SectionHeaderTable =
1196 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset);
1197 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1198
1199 if (SectionTableOffset + SectionTableSize > FileSize)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001200 // FIXME: Proper error handling.
1201 report_fatal_error("Section table goes past end of file!");
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001202
Nick Lewycky15c3f722011-10-11 02:57:48 +00001203 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1204 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
Dylan Noblesmith069df952012-02-04 02:41:39 +00001205 const Elf_Shdr* sh = SectionHeaderTable;
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001206 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
Nick Lewycky15c3f722011-10-11 02:57:48 +00001207 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
1208 if (SymbolTableSectionHeaderIndex)
1209 // FIXME: Proper error handling.
1210 report_fatal_error("More than one .symtab_shndx!");
1211 SymbolTableSectionHeaderIndex = sh;
1212 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001213 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001214 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001215 SymbolTableSections.push_back(sh);
1216 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001217 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +00001218 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001219 }
1220 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001221 }
1222
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001223 // Sort section relocation lists by index.
1224 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1225 e = SectionRelocMap.end(); i != e; ++i) {
1226 std::sort(i->second.begin(), i->second.end());
1227 }
1228
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001229 // Get string table sections.
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001230 dot_shstrtab_sec = getSection(getStringTableIndex());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001231 if (dot_shstrtab_sec) {
1232 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001233 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001234 [dot_shstrtab_sec->sh_size - 1] != 0)
1235 // FIXME: Proper error handling.
1236 report_fatal_error("String table must end with a null terminator!");
1237 }
1238
1239 // Merge this into the above loop.
1240 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001241 *e = i + getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001242 i != e; i += Header->e_shentsize) {
1243 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1244 if (sh->sh_type == ELF::SHT_STRTAB) {
1245 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1246 if (SectionName == ".strtab") {
1247 if (dot_strtab_sec != 0)
1248 // FIXME: Proper error handling.
1249 report_fatal_error("Already found section named .strtab!");
1250 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +00001251 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001252 if (dot_strtab[sh->sh_size - 1] != 0)
1253 // FIXME: Proper error handling.
1254 report_fatal_error("String table must end with a null terminator!");
1255 }
1256 }
1257 }
Nick Lewycky15c3f722011-10-11 02:57:48 +00001258
1259 // Build symbol name side-mapping if there is one.
1260 if (SymbolTableSectionHeaderIndex) {
1261 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1262 SymbolTableSectionHeaderIndex->sh_offset);
1263 error_code ec;
1264 for (symbol_iterator si = begin_symbols(),
1265 se = end_symbols(); si != se; si.increment(ec)) {
1266 if (ec)
1267 report_fatal_error("Fewer extended symbol table entries than symbols!");
1268 if (*ShndxTable != ELF::SHN_UNDEF)
1269 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1270 ++ShndxTable;
1271 }
1272 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001273}
1274
1275template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001276symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1277 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001278 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001279 memset(&SymbolData, 0, sizeof(SymbolData));
1280 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001281 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1282 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001283 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001284 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1285 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001286 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001287 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001288}
1289
1290template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001291symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1292 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001293 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001294 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001295 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1296 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1297 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001298}
1299
1300template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001301section_iterator ELFObjectFile<target_endianness, is64Bits>
1302 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001303 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001304 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001305 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
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>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001310section_iterator ELFObjectFile<target_endianness, is64Bits>
1311 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001312 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001313 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001314 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001315 + Header->e_shoff
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001316 + (Header->e_shentsize*getNumSections()));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001317 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001318}
1319
1320template<support::endianness target_endianness, bool is64Bits>
1321uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001322 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001323}
1324
1325template<support::endianness target_endianness, bool is64Bits>
1326StringRef ELFObjectFile<target_endianness, is64Bits>
1327 ::getFileFormatName() const {
1328 switch(Header->e_ident[ELF::EI_CLASS]) {
1329 case ELF::ELFCLASS32:
1330 switch(Header->e_machine) {
1331 case ELF::EM_386:
1332 return "ELF32-i386";
1333 case ELF::EM_X86_64:
1334 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001335 case ELF::EM_ARM:
1336 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001337 default:
1338 return "ELF32-unknown";
1339 }
1340 case ELF::ELFCLASS64:
1341 switch(Header->e_machine) {
1342 case ELF::EM_386:
1343 return "ELF64-i386";
1344 case ELF::EM_X86_64:
1345 return "ELF64-x86-64";
1346 default:
1347 return "ELF64-unknown";
1348 }
1349 default:
1350 // FIXME: Proper error handling.
1351 report_fatal_error("Invalid ELFCLASS!");
1352 }
1353}
1354
1355template<support::endianness target_endianness, bool is64Bits>
1356unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1357 switch(Header->e_machine) {
1358 case ELF::EM_386:
1359 return Triple::x86;
1360 case ELF::EM_X86_64:
1361 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001362 case ELF::EM_ARM:
1363 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001364 default:
1365 return Triple::UnknownArch;
1366 }
1367}
1368
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001369template<support::endianness target_endianness, bool is64Bits>
1370uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
Dylan Noblesmith069df952012-02-04 02:41:39 +00001371 assert(Header && "Header not initialized!");
1372 if (Header->e_shnum == ELF::SHN_UNDEF) {
1373 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001374 return SectionHeaderTable->sh_size;
Dylan Noblesmith069df952012-02-04 02:41:39 +00001375 }
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001376 return Header->e_shnum;
1377}
1378
1379template<support::endianness target_endianness, bool is64Bits>
1380uint64_t
1381ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1382 if (Header->e_shnum == ELF::SHN_UNDEF) {
1383 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1384 return SectionHeaderTable->sh_link;
1385 if (Header->e_shstrndx >= getNumSections())
1386 return 0;
1387 }
1388 return Header->e_shstrndx;
1389}
1390
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001391
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001392template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001393template<typename T>
1394inline const T *
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001395ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1396 uint32_t Entry) const {
1397 return getEntry<T>(getSection(Section), Entry);
1398}
1399
1400template<support::endianness target_endianness, bool is64Bits>
1401template<typename T>
1402inline const T *
1403ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1404 uint32_t Entry) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001405 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +00001406 base()
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001407 + Section->sh_offset
1408 + (Entry * Section->sh_entsize));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001409}
1410
1411template<support::endianness target_endianness, bool is64Bits>
1412const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1413ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001414 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001415}
1416
1417template<support::endianness target_endianness, bool is64Bits>
1418const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1419ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001420 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001421}
1422
1423template<support::endianness target_endianness, bool is64Bits>
1424const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1425ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001426 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001427}
1428
1429template<support::endianness target_endianness, bool is64Bits>
1430const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1431ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001432 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001433 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001434 // FIXME: Proper error handling.
1435 report_fatal_error("Invalid symbol table section!");
1436 return sec;
1437}
1438
1439template<support::endianness target_endianness, bool is64Bits>
1440const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001441ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1442 if (index == 0)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001443 return 0;
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001444 if (!SectionHeaderTable || index >= getNumSections())
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001445 // FIXME: Proper error handling.
1446 report_fatal_error("Invalid section index!");
1447
1448 return reinterpret_cast<const Elf_Shdr *>(
1449 reinterpret_cast<const char *>(SectionHeaderTable)
1450 + (index * Header->e_shentsize));
1451}
1452
1453template<support::endianness target_endianness, bool is64Bits>
1454const char *ELFObjectFile<target_endianness, is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001455 ::getString(uint32_t section,
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001456 ELF::Elf32_Word offset) const {
1457 return getString(getSection(section), offset);
1458}
1459
1460template<support::endianness target_endianness, bool is64Bits>
1461const char *ELFObjectFile<target_endianness, is64Bits>
1462 ::getString(const Elf_Shdr *section,
1463 ELF::Elf32_Word offset) const {
1464 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1465 if (offset >= section->sh_size)
1466 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001467 report_fatal_error("Symbol name offset outside of string table!");
1468 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001469}
1470
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001471template<support::endianness target_endianness, bool is64Bits>
1472error_code ELFObjectFile<target_endianness, is64Bits>
1473 ::getSymbolName(const Elf_Sym *symb,
1474 StringRef &Result) const {
1475 if (symb->st_name == 0) {
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001476 const Elf_Shdr *section = getSection(symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001477 if (!section)
1478 Result = "";
1479 else
1480 Result = getString(dot_shstrtab_sec, section->sh_name);
1481 return object_error::success;
1482 }
1483
1484 // Use the default symbol table name section.
1485 Result = getString(dot_strtab_sec, symb->st_name);
1486 return object_error::success;
1487}
1488
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001489// EI_CLASS, EI_DATA.
1490static std::pair<unsigned char, unsigned char>
1491getElfArchType(MemoryBuffer *Object) {
1492 if (Object->getBufferSize() < ELF::EI_NIDENT)
1493 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1494 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1495 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1496}
1497
Eli Bendersky24973c12012-01-22 09:01:03 +00001498
1499namespace {
1500 template<support::endianness target_endianness, bool is64Bits>
1501 class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
1502 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
1503
1504 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
1505 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
1506 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
1507 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
1508
1509 typedef typename ELFObjectFile<target_endianness, is64Bits>::
1510 Elf_Ehdr Elf_Ehdr;
1511 Elf_Ehdr *Header;
1512
1513 // Update section headers according to the current location in memory
1514 virtual void rebaseObject(std::vector<uint8_t*> *MemoryMap);
1515 // Record memory addresses for cleanup
1516 virtual void saveAddress(std::vector<uint8_t*> *MemoryMap, uint8_t *addr);
1517
1518 protected:
1519 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
1520
1521 public:
1522 DyldELFObject(MemoryBuffer *Object, std::vector<uint8_t*> *MemoryMap,
1523 error_code &ec);
1524
1525 // Methods for type inquiry through isa, cast, and dyn_cast
1526 static inline bool classof(const Binary *v) {
1527 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
1528 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
1529 }
1530 static inline bool classof(
1531 const ELFObjectFile<target_endianness, is64Bits> *v) {
1532 return v->isDyldType();
1533 }
1534 static inline bool classof(const DyldELFObject *v) {
1535 return true;
1536 }
1537 };
1538} // end anonymous namespace
1539
1540template<support::endianness target_endianness, bool is64Bits>
1541DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
1542 std::vector<uint8_t*> *MemoryMap, error_code &ec)
1543 : ELFObjectFile<target_endianness, is64Bits>(Object, ec)
1544 , Header(0) {
1545 this->isDyldELFObject = true;
1546 Header = const_cast<Elf_Ehdr *>(
1547 reinterpret_cast<const Elf_Ehdr *>(this->base()));
1548 if (Header->e_shoff == 0)
1549 return;
1550
1551 // Mark the image as a dynamic shared library
1552 Header->e_type = ELF::ET_DYN;
1553
1554 rebaseObject(MemoryMap);
1555}
1556
1557// Walk through the ELF headers, updating virtual addresses to reflect where
1558// the object is currently loaded in memory
1559template<support::endianness target_endianness, bool is64Bits>
1560void DyldELFObject<target_endianness, is64Bits>::rebaseObject(
1561 std::vector<uint8_t*> *MemoryMap) {
1562 typedef typename ELFDataTypeTypedefHelper<
1563 target_endianness, is64Bits>::value_type addr_type;
1564
1565 uint8_t *base_p = const_cast<uint8_t *>(this->base());
1566 Elf_Shdr *sectionTable =
1567 reinterpret_cast<Elf_Shdr *>(base_p + Header->e_shoff);
1568 uint64_t numSections = this->getNumSections();
1569
1570 // Allocate memory space for NOBITS sections (such as .bss), which only exist
1571 // in memory, but don't occupy space in the object file.
1572 // Update the address in the section headers to reflect this allocation.
1573 for (uint64_t index = 0; index < numSections; index++) {
1574 Elf_Shdr *sec = reinterpret_cast<Elf_Shdr *>(
1575 reinterpret_cast<char *>(sectionTable) + index * Header->e_shentsize);
1576
1577 // Only update sections that are meant to be present in program memory
1578 if (sec->sh_flags & ELF::SHF_ALLOC) {
1579 uint8_t *addr = base_p + sec->sh_offset;
1580 if (sec->sh_type == ELF::SHT_NOBITS) {
1581 addr = static_cast<uint8_t *>(calloc(sec->sh_size, 1));
1582 saveAddress(MemoryMap, addr);
1583 }
1584 else {
1585 // FIXME: Currently memory with RWX permissions is allocated. In the
1586 // future, make sure that permissions are as necessary
1587 if (sec->sh_flags & ELF::SHF_WRITE) {
1588 // see FIXME above
1589 }
1590 if (sec->sh_flags & ELF::SHF_EXECINSTR) {
1591 // see FIXME above
1592 }
1593 }
1594 assert(sizeof(addr_type) == sizeof(intptr_t) &&
1595 "Cross-architecture ELF dy-load is not supported!");
1596 sec->sh_addr = static_cast<addr_type>(intptr_t(addr));
1597 }
1598 }
1599
1600 // Now allocate actual space for COMMON symbols, which also don't occupy
1601 // space in the object file.
1602 // We want to allocate space for all COMMON symbols at once, so the flow is:
1603 // 1. Go over all symbols, find those that are in COMMON. For each such
1604 // symbol, record its size and the value field in its symbol header in a
1605 // special vector.
1606 // 2. Allocate memory for all COMMON symbols in one fell swoop.
1607 // 3. Using the recorded information from (1), update the address fields in
1608 // the symbol headers of the COMMON symbols to reflect their allocated
1609 // address.
1610 uint64_t TotalSize = 0;
1611 std::vector<std::pair<Elf_Addr *, uint64_t> > SymbAddrInfo;
1612 error_code ec = object_error::success;
1613 for (symbol_iterator si = this->begin_symbols(),
1614 se = this->end_symbols(); si != se; si.increment(ec)) {
1615 uint64_t Size = 0;
1616 ec = si->getSize(Size);
1617 Elf_Sym* symb = const_cast<Elf_Sym*>(
1618 this->getSymbol(si->getRawDataRefImpl()));
1619 if (ec == object_error::success &&
1620 this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
1621 SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
1622 TotalSize += Size;
1623 }
1624 }
1625
1626 uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
1627 saveAddress(MemoryMap, SectionPtr);
1628
1629 typedef typename std::vector<std::pair<Elf_Addr *, uint64_t> >::iterator
1630 AddrInfoIterator;
1631 AddrInfoIterator EndIter = SymbAddrInfo.end();
1632 for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
1633 AddrIter != EndIter; ++AddrIter) {
1634 assert(sizeof(addr_type) == sizeof(intptr_t) &&
1635 "Cross-architecture ELF dy-load is not supported!");
1636 *(AddrIter->first) = static_cast<addr_type>(intptr_t(SectionPtr));
1637 SectionPtr += AddrIter->second;
1638 }
1639}
1640
1641// Record memory addresses for callers
1642template<support::endianness target_endianness, bool is64Bits>
1643void DyldELFObject<target_endianness, is64Bits>::saveAddress(
1644 std::vector<uint8_t*> *MemoryMap, uint8_t* addr) {
1645 if (MemoryMap)
1646 MemoryMap->push_back(addr);
1647 else
1648 errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
1649}
1650
1651template<support::endianness target_endianness, bool is64Bits>
1652error_code DyldELFObject<target_endianness, is64Bits>::getSymbolAddress(
1653 DataRefImpl Symb, uint64_t &Result) const {
1654 this->validateSymbol(Symb);
1655 const Elf_Sym *symb = this->getSymbol(Symb);
1656 if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
1657 Result = symb->st_value;
1658 return object_error::success;
1659 }
1660 else {
1661 return ELFObjectFile<target_endianness, is64Bits>::getSymbolAddress(
1662 Symb, Result);
1663 }
1664}
1665
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001666namespace llvm {
1667
Eli Bendersky24973c12012-01-22 09:01:03 +00001668 // Creates an in-memory object-file by default: createELFObjectFile(Buffer)
1669 // Set doDyld to true to create a live (executable/debug-worthy) image
1670 // If doDyld is true, any memory allocated for non-resident sections and
1671 // symbols is recorded in MemoryMap.
1672 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object,
1673 bool doDyld, std::vector<uint8_t *> *MemoryMap) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001674 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001675 error_code ec;
Eli Bendersky24973c12012-01-22 09:01:03 +00001676
1677 if (doDyld) {
1678 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
1679 return new DyldELFObject<support::little, false>(Object, MemoryMap, ec);
1680 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
1681 return new DyldELFObject<support::big, false>(Object, MemoryMap, ec);
1682 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
1683 return new DyldELFObject<support::big, true>(Object, MemoryMap, ec);
1684 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
1685 DyldELFObject<support::little, true> *result =
1686 new DyldELFObject<support::little, true>(Object, MemoryMap, ec);
1687
1688 // Unit testing for type inquiry
Matt Beaumont-Gay7f648232012-01-24 19:43:30 +00001689 assert(isa<Binary>(result) && "Type inquiry failed for ELF object!");
1690 assert((isa<DyldELFObject<support::little, true> >(result)) &&
1691 "Type inquiry failed for ELF object!");
1692 assert((isa<ELFObjectFile<support::little, true> >(result)) &&
1693 "Type inquiry failed for ELF object!");
Eli Bendersky24973c12012-01-22 09:01:03 +00001694 return result;
1695 }
1696 }
1697
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001698 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001699 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001700 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001701 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001702 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001703 return new ELFObjectFile<support::big, true>(Object, ec);
Eli Bendersky24973c12012-01-22 09:01:03 +00001704 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
1705 ELFObjectFile<support::little, true> *result =
1706 new ELFObjectFile<support::little, true>(Object, ec);
1707
1708 // Unit testing for type inquiry
Matt Beaumont-Gay7f648232012-01-24 19:43:30 +00001709 assert(isa<Binary>(result) && "Type inquiry failed for ELF object!");
1710 assert((!isa<DyldELFObject<support::little, true> >(result)) &&
1711 "Type inquiry failed for ELF object!");
1712 assert((isa<ELFObjectFile<support::little, true> >(result)) &&
1713 "Type inquiry failed for ELF object!");
Eli Bendersky24973c12012-01-22 09:01:03 +00001714 return result;
1715 }
1716
1717 report_fatal_error("Buffer is not an ELF object file!");
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001718 }
1719
1720} // end namespace llvm