blob: 16e9d14ce607e9d1c9eb4a17add99316f9b80a86 [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//
10// This file defines the ELFObjectFile class.
11//
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"
19#include "llvm/Support/ELF.h"
20#include "llvm/Support/Endian.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000023#include "llvm/Support/raw_ostream.h"
24#include <algorithm>
Michael J. Spencerb84551a2011-01-20 06:38:47 +000025#include <limits>
26#include <utility>
27
28using namespace llvm;
29using namespace object;
30
31// Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
32namespace {
33template<support::endianness target_endianness>
34struct ELFDataTypeTypedefHelperCommon {
35 typedef support::detail::packed_endian_specific_integral
36 <uint16_t, target_endianness, support::aligned> Elf_Half;
37 typedef support::detail::packed_endian_specific_integral
38 <uint32_t, target_endianness, support::aligned> Elf_Word;
39 typedef support::detail::packed_endian_specific_integral
40 <int32_t, target_endianness, support::aligned> Elf_Sword;
41 typedef support::detail::packed_endian_specific_integral
42 <uint64_t, target_endianness, support::aligned> Elf_Xword;
43 typedef support::detail::packed_endian_specific_integral
44 <int64_t, target_endianness, support::aligned> Elf_Sxword;
45};
46}
47
48namespace {
49template<support::endianness target_endianness, bool is64Bits>
50struct ELFDataTypeTypedefHelper;
51
52/// ELF 32bit types.
53template<support::endianness target_endianness>
54struct ELFDataTypeTypedefHelper<target_endianness, false>
55 : ELFDataTypeTypedefHelperCommon<target_endianness> {
56 typedef support::detail::packed_endian_specific_integral
57 <uint32_t, target_endianness, support::aligned> Elf_Addr;
58 typedef support::detail::packed_endian_specific_integral
59 <uint32_t, target_endianness, support::aligned> Elf_Off;
60};
61
62/// ELF 64bit types.
63template<support::endianness target_endianness>
64struct ELFDataTypeTypedefHelper<target_endianness, true>
65 : ELFDataTypeTypedefHelperCommon<target_endianness>{
66 typedef support::detail::packed_endian_specific_integral
67 <uint64_t, target_endianness, support::aligned> Elf_Addr;
68 typedef support::detail::packed_endian_specific_integral
69 <uint64_t, target_endianness, support::aligned> Elf_Off;
70};
71}
72
73// I really don't like doing this, but the alternative is copypasta.
74#define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
75typedef typename \
76 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
77typedef typename \
78 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
79typedef typename \
80 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
81typedef typename \
82 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
83typedef typename \
84 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
85typedef typename \
86 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
87typedef typename \
88 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
89
90 // Section header.
91namespace {
92template<support::endianness target_endianness, bool is64Bits>
93struct Elf_Shdr_Base;
94
95template<support::endianness target_endianness>
96struct Elf_Shdr_Base<target_endianness, false> {
97 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
98 Elf_Word sh_name; // Section name (index into string table)
99 Elf_Word sh_type; // Section type (SHT_*)
100 Elf_Word sh_flags; // Section flags (SHF_*)
101 Elf_Addr sh_addr; // Address where section is to be loaded
102 Elf_Off sh_offset; // File offset of section data, in bytes
103 Elf_Word sh_size; // Size of section, in bytes
104 Elf_Word sh_link; // Section type-specific header table index link
105 Elf_Word sh_info; // Section type-specific extra information
106 Elf_Word sh_addralign;// Section address alignment
107 Elf_Word sh_entsize; // Size of records contained within the section
108};
109
110template<support::endianness target_endianness>
111struct Elf_Shdr_Base<target_endianness, true> {
112 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
113 Elf_Word sh_name; // Section name (index into string table)
114 Elf_Word sh_type; // Section type (SHT_*)
115 Elf_Xword sh_flags; // Section flags (SHF_*)
116 Elf_Addr sh_addr; // Address where section is to be loaded
117 Elf_Off sh_offset; // File offset of section data, in bytes
118 Elf_Xword sh_size; // Size of section, in bytes
119 Elf_Word sh_link; // Section type-specific header table index link
120 Elf_Word sh_info; // Section type-specific extra information
121 Elf_Xword sh_addralign;// Section address alignment
122 Elf_Xword sh_entsize; // Size of records contained within the section
123};
124
125template<support::endianness target_endianness, bool is64Bits>
126struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> {
127 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize;
128 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size;
129
130 /// @brief Get the number of entities this section contains if it has any.
131 unsigned getEntityCount() const {
132 if (sh_entsize == 0)
133 return 0;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000134 return sh_size / sh_entsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000135 }
136};
137}
138
139namespace {
140template<support::endianness target_endianness, bool is64Bits>
141struct Elf_Sym_Base;
142
143template<support::endianness target_endianness>
144struct Elf_Sym_Base<target_endianness, false> {
145 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
146 Elf_Word st_name; // Symbol name (index into string table)
147 Elf_Addr st_value; // Value or address associated with the symbol
148 Elf_Word st_size; // Size of the symbol
149 unsigned char st_info; // Symbol's type and binding attributes
150 unsigned char st_other; // Must be zero; reserved
151 Elf_Half st_shndx; // Which section (header table index) it's defined in
152};
153
154template<support::endianness target_endianness>
155struct Elf_Sym_Base<target_endianness, true> {
156 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
157 Elf_Word st_name; // Symbol name (index into string table)
158 unsigned char st_info; // Symbol's type and binding attributes
159 unsigned char st_other; // Must be zero; reserved
160 Elf_Half st_shndx; // Which section (header table index) it's defined in
161 Elf_Addr st_value; // Value or address associated with the symbol
162 Elf_Xword st_size; // Size of the symbol
163};
164
165template<support::endianness target_endianness, bool is64Bits>
166struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> {
167 using Elf_Sym_Base<target_endianness, is64Bits>::st_info;
168
169 // These accessors and mutators correspond to the ELF32_ST_BIND,
170 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
171 unsigned char getBinding() const { return st_info >> 4; }
172 unsigned char getType() const { return st_info & 0x0f; }
173 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
174 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
175 void setBindingAndType(unsigned char b, unsigned char t) {
176 st_info = (b << 4) + (t & 0x0f);
177 }
178};
179}
180
181namespace {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000182template<support::endianness target_endianness, bool is64Bits, bool isRela>
183struct Elf_Rel_Base;
184
185template<support::endianness target_endianness>
186struct Elf_Rel_Base<target_endianness, false, false> {
187 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
188 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
189 Elf_Word r_info; // Symbol table index and type of relocation to apply
190};
191
192template<support::endianness target_endianness>
193struct Elf_Rel_Base<target_endianness, true, false> {
194 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
195 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
196 Elf_Xword r_info; // Symbol table index and type of relocation to apply
197};
198
199template<support::endianness target_endianness>
200struct Elf_Rel_Base<target_endianness, false, true> {
201 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
202 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
203 Elf_Word r_info; // Symbol table index and type of relocation to apply
204 Elf_Sword r_addend; // Compute value for relocatable field by adding this
205};
206
207template<support::endianness target_endianness>
208struct Elf_Rel_Base<target_endianness, true, true> {
209 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
210 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
211 Elf_Xword r_info; // Symbol table index and type of relocation to apply
212 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
213};
214
215template<support::endianness target_endianness, bool is64Bits, bool isRela>
216struct Elf_Rel_Impl;
217
218template<support::endianness target_endianness, bool isRela>
219struct Elf_Rel_Impl<target_endianness, true, isRela>
220 : Elf_Rel_Base<target_endianness, true, isRela> {
221 using Elf_Rel_Base<target_endianness, true, isRela>::r_info;
222 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
223
224 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
225 // and ELF64_R_INFO macros defined in the ELF specification:
226 uint64_t getSymbol() const { return (r_info >> 32); }
227 unsigned char getType() const {
228 return (unsigned char) (r_info & 0xffffffffL);
229 }
230 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
231 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
232 void setSymbolAndType(uint64_t s, unsigned char t) {
233 r_info = (s << 32) + (t&0xffffffffL);
234 }
235};
236
237template<support::endianness target_endianness, bool isRela>
238struct Elf_Rel_Impl<target_endianness, false, isRela>
239 : Elf_Rel_Base<target_endianness, false, isRela> {
240 using Elf_Rel_Base<target_endianness, false, isRela>::r_info;
241 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
242
243 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
244 // and ELF32_R_INFO macros defined in the ELF specification:
245 uint32_t getSymbol() const { return (r_info >> 8); }
246 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
247 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
248 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
249 void setSymbolAndType(uint32_t s, unsigned char t) {
250 r_info = (s << 8) + t;
251 }
252};
253
254}
255
256namespace {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000257template<support::endianness target_endianness, bool is64Bits>
258class ELFObjectFile : public ObjectFile {
259 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
260
261 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
262 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000263 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
264 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000265
266 struct Elf_Ehdr {
267 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
268 Elf_Half e_type; // Type of file (see ET_*)
269 Elf_Half e_machine; // Required architecture for this file (see EM_*)
270 Elf_Word e_version; // Must be equal to 1
271 Elf_Addr e_entry; // Address to jump to in order to start program
272 Elf_Off e_phoff; // Program header table's file offset, in bytes
273 Elf_Off e_shoff; // Section header table's file offset, in bytes
274 Elf_Word e_flags; // Processor-specific flags
275 Elf_Half e_ehsize; // Size of ELF header, in bytes
276 Elf_Half e_phentsize;// Size of an entry in the program header table
277 Elf_Half e_phnum; // Number of entries in the program header table
278 Elf_Half e_shentsize;// Size of an entry in the section header table
279 Elf_Half e_shnum; // Number of entries in the section header table
280 Elf_Half e_shstrndx; // Section header table index of section name
281 // string table
282 bool checkMagic() const {
283 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
284 }
285 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
286 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
287 };
288
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000289 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
290 typedef DenseMap<unsigned, unsigned> IndexMap_t;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000291 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000292
293 const Elf_Ehdr *Header;
294 const Elf_Shdr *SectionHeaderTable;
295 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
296 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000297 Sections_t SymbolTableSections;
298 IndexMap_t SymbolTableSectionsIndexMap;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000299
300 /// @brief Map sections to an array of relocation sections that reference
301 /// them sorted by section index.
302 RelocMap_t SectionRelocMap;
303
304 /// @brief Get the relocation section that contains \a Rel.
305 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
306 return getSection(Rel.w.b);
307 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000308
309 void validateSymbol(DataRefImpl Symb) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000310 bool isRelocationHasAddend(DataRefImpl Rel) const;
311 template<typename T>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000312 const T *getEntry(uint16_t Section, uint32_t Entry) const;
313 template<typename T>
314 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000315 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
316 const Elf_Shdr *getSection(DataRefImpl index) const;
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +0000317 const Elf_Shdr *getSection(uint16_t index) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000318 const Elf_Rel *getRel(DataRefImpl Rel) const;
319 const Elf_Rela *getRela(DataRefImpl Rela) const;
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +0000320 const char *getString(uint16_t section, uint32_t offset) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000321 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000322 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000323
324protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000325 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
326 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000327 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000328 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
329 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
330 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
331 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000332 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
333 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000334
Michael J. Spencer25b15772011-06-25 17:55:23 +0000335 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
336 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
337 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
338 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
339 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000340 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000341 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000342 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
343 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000344 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
345 bool &Result) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000346 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
347 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000348
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000349 virtual error_code getRelocationNext(DataRefImpl Rel,
350 RelocationRef &Res) const;
351 virtual error_code getRelocationAddress(DataRefImpl Rel,
352 uint64_t &Res) const;
353 virtual error_code getRelocationSymbol(DataRefImpl Rel,
354 SymbolRef &Res) const;
355 virtual error_code getRelocationType(DataRefImpl Rel,
356 uint32_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000357 virtual error_code getRelocationTypeName(DataRefImpl Rel,
358 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000359 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
360 int64_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000361 virtual error_code getRelocationValueString(DataRefImpl Rel,
362 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000363
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000364public:
Michael J. Spencer001c9202011-06-25 17:54:50 +0000365 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000366 virtual symbol_iterator begin_symbols() const;
367 virtual symbol_iterator end_symbols() const;
368 virtual section_iterator begin_sections() const;
369 virtual section_iterator end_sections() const;
370
371 virtual uint8_t getBytesInAddress() const;
372 virtual StringRef getFileFormatName() const;
373 virtual unsigned getArch() const;
374};
375} // end namespace
376
377template<support::endianness target_endianness, bool is64Bits>
378void ELFObjectFile<target_endianness, is64Bits>
379 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000380 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000381 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000382 // FIXME: We really need to do proper error handling in the case of an invalid
383 // input file. Because we don't use exceptions, I think we'll just pass
384 // an error object around.
385 if (!( symb
386 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000387 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000388 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000389 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000390 + SymbolTableSection->sh_offset
391 + SymbolTableSection->sh_size)))
392 // FIXME: Proper error handling.
393 report_fatal_error("Symb must point to a valid symbol!");
394}
395
396template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000397error_code ELFObjectFile<target_endianness, is64Bits>
398 ::getSymbolNext(DataRefImpl Symb,
399 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000400 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000401 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000402
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000403 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000404 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000405 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000406 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000407 ++Symb.d.b;
408 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000409 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000410 if (Symb.d.b >= SymbolTableSections.size()) {
411 Symb.d.a = std::numeric_limits<uint32_t>::max();
412 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000413 }
414 }
415
Michael J. Spencer25b15772011-06-25 17:55:23 +0000416 Result = SymbolRef(Symb, this);
417 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000418}
419
420template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000421error_code ELFObjectFile<target_endianness, is64Bits>
422 ::getSymbolName(DataRefImpl Symb,
423 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000424 validateSymbol(Symb);
425 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000426 return getSymbolName(symb, Result);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000427}
428
429template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000430error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000431 ::getSymbolOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000432 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000433 validateSymbol(Symb);
434 const Elf_Sym *symb = getSymbol(Symb);
435 const Elf_Shdr *Section;
436 switch (symb->st_shndx) {
437 case ELF::SHN_COMMON:
438 // Undefined symbols have no address yet.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000439 case ELF::SHN_UNDEF:
440 Result = UnknownAddressOrSize;
441 return object_error::success;
442 case ELF::SHN_ABS:
443 Result = symb->st_value;
444 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000445 default: Section = getSection(symb->st_shndx);
446 }
447
448 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000449 case ELF::STT_SECTION:
450 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
451 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000452 case ELF::STT_FUNC:
453 case ELF::STT_OBJECT:
454 case ELF::STT_NOTYPE:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000455 Result = symb->st_value;
456 return object_error::success;
457 default:
458 Result = UnknownAddressOrSize;
459 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000460 }
461}
462
463template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000464error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000465 ::getSymbolAddress(DataRefImpl Symb,
466 uint64_t &Result) const {
467 validateSymbol(Symb);
468 const Elf_Sym *symb = getSymbol(Symb);
469 const Elf_Shdr *Section;
470 switch (symb->st_shndx) {
471 case ELF::SHN_COMMON: // Fall through.
472 // Undefined symbols have no address yet.
473 case ELF::SHN_UNDEF:
474 Result = UnknownAddressOrSize;
475 return object_error::success;
476 case ELF::SHN_ABS:
477 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value);
478 return object_error::success;
479 default: Section = getSection(symb->st_shndx);
480 }
481 const uint8_t* addr = base();
482 if (Section)
483 addr += Section->sh_offset;
484 switch (symb->getType()) {
485 case ELF::STT_SECTION:
486 Result = reinterpret_cast<uintptr_t>(addr);
487 return object_error::success;
488 case ELF::STT_FUNC: // Fall through.
489 case ELF::STT_OBJECT: // Fall through.
490 case ELF::STT_NOTYPE:
491 addr += symb->st_value;
492 Result = reinterpret_cast<uintptr_t>(addr);
493 return object_error::success;
494 default:
495 Result = UnknownAddressOrSize;
496 return object_error::success;
497 }
498}
499
500template<support::endianness target_endianness, bool is64Bits>
501error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000502 ::getSymbolSize(DataRefImpl Symb,
503 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000504 validateSymbol(Symb);
505 const Elf_Sym *symb = getSymbol(Symb);
506 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000507 Result = UnknownAddressOrSize;
508 Result = symb->st_size;
509 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000510}
511
512template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000513error_code ELFObjectFile<target_endianness, is64Bits>
514 ::getSymbolNMTypeChar(DataRefImpl Symb,
515 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000516 validateSymbol(Symb);
517 const Elf_Sym *symb = getSymbol(Symb);
518 const Elf_Shdr *Section = getSection(symb->st_shndx);
519
520 char ret = '?';
521
522 if (Section) {
523 switch (Section->sh_type) {
524 case ELF::SHT_PROGBITS:
525 case ELF::SHT_DYNAMIC:
526 switch (Section->sh_flags) {
527 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
528 ret = 't'; break;
529 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
530 ret = 'd'; break;
531 case ELF::SHF_ALLOC:
532 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
533 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
534 ret = 'r'; break;
535 }
536 break;
537 case ELF::SHT_NOBITS: ret = 'b';
538 }
539 }
540
541 switch (symb->st_shndx) {
542 case ELF::SHN_UNDEF:
543 if (ret == '?')
544 ret = 'U';
545 break;
546 case ELF::SHN_ABS: ret = 'a'; break;
547 case ELF::SHN_COMMON: ret = 'c'; break;
548 }
549
550 switch (symb->getBinding()) {
551 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
552 case ELF::STB_WEAK:
553 if (symb->st_shndx == ELF::SHN_UNDEF)
554 ret = 'w';
555 else
556 if (symb->getType() == ELF::STT_OBJECT)
557 ret = 'V';
558 else
559 ret = 'W';
560 }
561
Michael J. Spencer25b15772011-06-25 17:55:23 +0000562 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
563 StringRef name;
564 if (error_code ec = getSymbolName(Symb, name))
565 return ec;
566 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000567 .StartsWith(".debug", 'N')
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000568 .StartsWith(".note", 'n')
569 .Default('?');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000570 return object_error::success;
571 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000572
Michael J. Spencer25b15772011-06-25 17:55:23 +0000573 Result = ret;
574 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000575}
576
577template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000578error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000579 ::getSymbolType(DataRefImpl Symb,
580 SymbolRef::SymbolType &Result) const {
581 validateSymbol(Symb);
582 const Elf_Sym *symb = getSymbol(Symb);
583
584 if (symb->st_shndx == ELF::SHN_UNDEF) {
585 Result = SymbolRef::ST_External;
586 return object_error::success;
587 }
588
589 switch (symb->getType()) {
590 case ELF::STT_FUNC:
591 Result = SymbolRef::ST_Function;
592 break;
593 case ELF::STT_OBJECT:
594 Result = SymbolRef::ST_Data;
595 break;
596 default:
597 Result = SymbolRef::ST_Other;
598 break;
599 }
600 return object_error::success;
601}
602
603template<support::endianness target_endianness, bool is64Bits>
604error_code ELFObjectFile<target_endianness, is64Bits>
605 ::isSymbolGlobal(DataRefImpl Symb,
606 bool &Result) const {
607 validateSymbol(Symb);
608 const Elf_Sym *symb = getSymbol(Symb);
609
610 Result = symb->getBinding() == ELF::STB_GLOBAL;
611 return object_error::success;
612}
613
614template<support::endianness target_endianness, bool is64Bits>
615error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000616 ::isSymbolInternal(DataRefImpl Symb,
617 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000618 validateSymbol(Symb);
619 const Elf_Sym *symb = getSymbol(Symb);
620
621 if ( symb->getType() == ELF::STT_FILE
622 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000623 Result = true;
624 Result = false;
625 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000626}
627
628template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000629error_code ELFObjectFile<target_endianness, is64Bits>
630 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000631 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000632 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000633 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000634 Result = SectionRef(Sec, this);
635 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000636}
637
638template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000639error_code ELFObjectFile<target_endianness, is64Bits>
640 ::getSectionName(DataRefImpl Sec,
641 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000642 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000643 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
644 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000645}
646
647template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000648error_code ELFObjectFile<target_endianness, is64Bits>
649 ::getSectionAddress(DataRefImpl Sec,
650 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000651 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000652 Result = sec->sh_addr;
653 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000654}
655
656template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000657error_code ELFObjectFile<target_endianness, is64Bits>
658 ::getSectionSize(DataRefImpl Sec,
659 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000660 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000661 Result = sec->sh_size;
662 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000663}
664
665template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000666error_code ELFObjectFile<target_endianness, is64Bits>
667 ::getSectionContents(DataRefImpl Sec,
668 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000669 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000670 const char *start = (const char*)base() + sec->sh_offset;
671 Result = StringRef(start, sec->sh_size);
672 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000673}
674
675template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000676error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000677 ::getSectionAlignment(DataRefImpl Sec,
678 uint64_t &Result) const {
679 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
680 Result = sec->sh_addralign;
681 return object_error::success;
682}
683
684template<support::endianness target_endianness, bool is64Bits>
685error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000686 ::isSectionText(DataRefImpl Sec,
687 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000688 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000689 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000690 Result = true;
691 else
692 Result = false;
693 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000694}
695
696template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000697error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000698 ::isSectionData(DataRefImpl Sec,
699 bool &Result) const {
700 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
701 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
702 && sec->sh_type == ELF::SHT_PROGBITS)
703 Result = true;
704 else
705 Result = false;
706 return object_error::success;
707}
708
709template<support::endianness target_endianness, bool is64Bits>
710error_code ELFObjectFile<target_endianness, is64Bits>
711 ::isSectionBSS(DataRefImpl Sec,
712 bool &Result) const {
713 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
714 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
715 && sec->sh_type == ELF::SHT_NOBITS)
716 Result = true;
717 else
718 Result = false;
719 return object_error::success;
720}
721
722template<support::endianness target_endianness, bool is64Bits>
723error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000724 ::sectionContainsSymbol(DataRefImpl Sec,
725 DataRefImpl Symb,
726 bool &Result) const {
727 // FIXME: Unimplemented.
728 Result = false;
729 return object_error::success;
730}
731
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000732template<support::endianness target_endianness, bool is64Bits>
733relocation_iterator ELFObjectFile<target_endianness, is64Bits>
734 ::getSectionRelBegin(DataRefImpl Sec) const {
735 DataRefImpl RelData;
736 memset(&RelData, 0, sizeof(RelData));
737 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
738 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
739 if (sec != 0 && ittr != SectionRelocMap.end()) {
740 RelData.w.a = getSection(ittr->second[0])->sh_link;
741 RelData.w.b = ittr->second[0];
742 RelData.w.c = 0;
743 }
744 return relocation_iterator(RelocationRef(RelData, this));
745}
746
747template<support::endianness target_endianness, bool is64Bits>
748relocation_iterator ELFObjectFile<target_endianness, is64Bits>
749 ::getSectionRelEnd(DataRefImpl Sec) const {
750 DataRefImpl RelData;
751 memset(&RelData, 0, sizeof(RelData));
752 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
753 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
754 if (sec != 0 && ittr != SectionRelocMap.end()) {
755 // Get the index of the last relocation section for this section.
756 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
757 const Elf_Shdr *relocsec = getSection(relocsecindex);
758 RelData.w.a = relocsec->sh_link;
759 RelData.w.b = relocsecindex;
760 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
761 }
762 return relocation_iterator(RelocationRef(RelData, this));
763}
764
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000765// Relocations
766template<support::endianness target_endianness, bool is64Bits>
767error_code ELFObjectFile<target_endianness, is64Bits>
768 ::getRelocationNext(DataRefImpl Rel,
769 RelocationRef &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000770 ++Rel.w.c;
771 const Elf_Shdr *relocsec = getSection(Rel.w.b);
772 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
773 // We have reached the end of the relocations for this section. See if there
774 // is another relocation section.
Michael J. Spencer01a4db32011-10-07 19:46:12 +0000775 typename RelocMap_t::mapped_type relocseclist =
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000776 SectionRelocMap.lookup(getSection(Rel.w.a));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000777
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000778 // Do a binary search for the current reloc section index (which must be
779 // present). Then get the next one.
780 typename RelocMap_t::mapped_type::const_iterator loc =
781 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
782 ++loc;
783
784 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
785 // to the end iterator.
786 if (loc != relocseclist.end()) {
787 Rel.w.b = *loc;
788 Rel.w.a = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000789 }
790 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000791 Result = RelocationRef(Rel, this);
792 return object_error::success;
793}
794
795template<support::endianness target_endianness, bool is64Bits>
796error_code ELFObjectFile<target_endianness, is64Bits>
797 ::getRelocationSymbol(DataRefImpl Rel,
798 SymbolRef &Result) const {
799 uint32_t symbolIdx;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000800 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000801 switch (sec->sh_type) {
802 default :
803 report_fatal_error("Invalid section type in Rel!");
804 case ELF::SHT_REL : {
805 symbolIdx = getRel(Rel)->getSymbol();
806 break;
807 }
808 case ELF::SHT_RELA : {
809 symbolIdx = getRela(Rel)->getSymbol();
810 break;
811 }
812 }
813 DataRefImpl SymbolData;
814 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
815 if (it == SymbolTableSectionsIndexMap.end())
816 report_fatal_error("Relocation symbol table not found!");
817 SymbolData.d.a = symbolIdx;
818 SymbolData.d.b = it->second;
819 Result = SymbolRef(SymbolData, this);
820 return object_error::success;
821}
822
823template<support::endianness target_endianness, bool is64Bits>
824error_code ELFObjectFile<target_endianness, is64Bits>
825 ::getRelocationAddress(DataRefImpl Rel,
826 uint64_t &Result) const {
827 uint64_t offset;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000828 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000829 switch (sec->sh_type) {
830 default :
831 report_fatal_error("Invalid section type in Rel!");
832 case ELF::SHT_REL : {
833 offset = getRel(Rel)->r_offset;
834 break;
835 }
836 case ELF::SHT_RELA : {
837 offset = getRela(Rel)->r_offset;
838 break;
839 }
840 }
841
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000842 Result = offset;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000843 return object_error::success;
844}
845
846template<support::endianness target_endianness, bool is64Bits>
847error_code ELFObjectFile<target_endianness, is64Bits>
848 ::getRelocationType(DataRefImpl Rel,
849 uint32_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000850 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000851 switch (sec->sh_type) {
852 default :
853 report_fatal_error("Invalid section type in Rel!");
854 case ELF::SHT_REL : {
855 Result = getRel(Rel)->getType();
856 break;
857 }
858 case ELF::SHT_RELA : {
859 Result = getRela(Rel)->getType();
860 break;
861 }
862 }
863 return object_error::success;
864}
865
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000866#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
867 case ELF::enum: res = #enum; break;
868
869template<support::endianness target_endianness, bool is64Bits>
870error_code ELFObjectFile<target_endianness, is64Bits>
871 ::getRelocationTypeName(DataRefImpl Rel,
872 SmallVectorImpl<char> &Result) const {
873 const Elf_Shdr *sec = getSection(Rel.w.b);
874 uint8_t type;
875 StringRef res;
876 switch (sec->sh_type) {
877 default :
878 return object_error::parse_failed;
879 case ELF::SHT_REL : {
880 type = getRel(Rel)->getType();
881 break;
882 }
883 case ELF::SHT_RELA : {
884 type = getRela(Rel)->getType();
885 break;
886 }
887 }
888 switch (Header->e_machine) {
889 case ELF::EM_X86_64:
890 switch (type) {
891 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
892 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
893 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
894 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
895 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
896 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
897 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
898 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
899 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
900 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
901 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
902 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
903 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
904 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
905 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
906 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
907 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
908 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
909 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
910 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
911 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
912 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
913 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
914 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
915 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
916 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
917 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
918 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
919 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
920 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
923 default:
924 res = "Unknown";
925 }
926 break;
927 case ELF::EM_386:
928 switch (type) {
929 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
930 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
953 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
954 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
955 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
956 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
957 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
958 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
959 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
960 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
961 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
969 default:
970 res = "Unknown";
971 }
972 break;
973 default:
974 res = "Unknown";
975 }
976 Result.append(res.begin(), res.end());
977 return object_error::success;
978}
979
980#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
981
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000982template<support::endianness target_endianness, bool is64Bits>
983error_code ELFObjectFile<target_endianness, is64Bits>
984 ::getRelocationAdditionalInfo(DataRefImpl Rel,
985 int64_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000986 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000987 switch (sec->sh_type) {
988 default :
989 report_fatal_error("Invalid section type in Rel!");
990 case ELF::SHT_REL : {
991 Result = 0;
992 return object_error::success;
993 }
994 case ELF::SHT_RELA : {
995 Result = getRela(Rel)->r_addend;
996 return object_error::success;
997 }
998 }
999}
1000
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001001template<support::endianness target_endianness, bool is64Bits>
1002error_code ELFObjectFile<target_endianness, is64Bits>
1003 ::getRelocationValueString(DataRefImpl Rel,
1004 SmallVectorImpl<char> &Result) const {
1005 const Elf_Shdr *sec = getSection(Rel.w.b);
1006 uint8_t type;
1007 StringRef res;
1008 int64_t addend = 0;
1009 uint16_t symbol_index = 0;
1010 switch (sec->sh_type) {
1011 default :
1012 return object_error::parse_failed;
1013 case ELF::SHT_REL : {
1014 type = getRel(Rel)->getType();
1015 symbol_index = getRel(Rel)->getSymbol();
1016 // TODO: Read implicit addend from section data.
1017 break;
1018 }
1019 case ELF::SHT_RELA : {
1020 type = getRela(Rel)->getType();
1021 symbol_index = getRela(Rel)->getSymbol();
1022 addend = getRela(Rel)->r_addend;
1023 break;
1024 }
1025 }
1026 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1027 StringRef symname;
1028 if (error_code ec = getSymbolName(symb, symname))
1029 return ec;
1030 switch (Header->e_machine) {
1031 case ELF::EM_X86_64:
1032 switch (type) {
1033 case ELF::R_X86_64_32S:
1034 res = symname;
1035 break;
1036 case ELF::R_X86_64_PC32: {
1037 std::string fmtbuf;
1038 raw_string_ostream fmt(fmtbuf);
1039 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1040 fmt.flush();
1041 Result.append(fmtbuf.begin(), fmtbuf.end());
1042 }
1043 break;
1044 default:
1045 res = "Unknown";
1046 }
1047 break;
1048 default:
1049 res = "Unknown";
1050 }
1051 if (Result.empty())
1052 Result.append(res.begin(), res.end());
1053 return object_error::success;
1054}
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001055
Benjamin Kramer07ea23a2011-07-15 18:39:21 +00001056template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +00001057ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1058 , error_code &ec)
1059 : ObjectFile(Binary::isELF, Object, ec)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001060 , SectionHeaderTable(0)
1061 , dot_shstrtab_sec(0)
1062 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001063 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001064
1065 if (Header->e_shoff == 0)
1066 return;
1067
1068 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +00001069 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001070 uint32_t SectionTableSize = Header->e_shnum * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001071 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize
Michael J. Spencer001c9202011-06-25 17:54:50 +00001072 <= base() + Data->getBufferSize()))
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001073 // FIXME: Proper error handling.
1074 report_fatal_error("Section table goes past end of file!");
1075
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001076
1077 // To find the symbol tables we walk the section table to find SHT_STMTAB.
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001078 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001079 for (unsigned i = 0; i < Header->e_shnum; ++i) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001080 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001081 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001082 SymbolTableSections.push_back(sh);
1083 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001084 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001085 SectionRelocMap[getSection(sh->sh_link)].push_back(i);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001086 }
1087 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001088 }
1089
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001090 // Sort section relocation lists by index.
1091 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1092 e = SectionRelocMap.end(); i != e; ++i) {
1093 std::sort(i->second.begin(), i->second.end());
1094 }
1095
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001096 // Get string table sections.
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001097 dot_shstrtab_sec = getSection(Header->e_shstrndx);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001098 if (dot_shstrtab_sec) {
1099 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001100 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001101 [dot_shstrtab_sec->sh_size - 1] != 0)
1102 // FIXME: Proper error handling.
1103 report_fatal_error("String table must end with a null terminator!");
1104 }
1105
1106 // Merge this into the above loop.
1107 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001108 *e = i + Header->e_shnum * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001109 i != e; i += Header->e_shentsize) {
1110 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1111 if (sh->sh_type == ELF::SHT_STRTAB) {
1112 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1113 if (SectionName == ".strtab") {
1114 if (dot_strtab_sec != 0)
1115 // FIXME: Proper error handling.
1116 report_fatal_error("Already found section named .strtab!");
1117 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +00001118 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001119 if (dot_strtab[sh->sh_size - 1] != 0)
1120 // FIXME: Proper error handling.
1121 report_fatal_error("String table must end with a null terminator!");
1122 }
1123 }
1124 }
1125}
1126
1127template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001128symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1129 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001130 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001131 memset(&SymbolData, 0, sizeof(SymbolData));
1132 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001133 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1134 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001135 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001136 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1137 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001138 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001139 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001140}
1141
1142template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001143symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1144 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001145 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001146 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001147 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1148 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1149 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001150}
1151
1152template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001153section_iterator ELFObjectFile<target_endianness, is64Bits>
1154 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001155 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001156 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001157 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001158 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001159}
1160
1161template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001162section_iterator ELFObjectFile<target_endianness, is64Bits>
1163 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001164 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001165 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001166 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001167 + Header->e_shoff
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001168 + (Header->e_shentsize * Header->e_shnum));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001169 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001170}
1171
1172template<support::endianness target_endianness, bool is64Bits>
1173uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001174 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001175}
1176
1177template<support::endianness target_endianness, bool is64Bits>
1178StringRef ELFObjectFile<target_endianness, is64Bits>
1179 ::getFileFormatName() const {
1180 switch(Header->e_ident[ELF::EI_CLASS]) {
1181 case ELF::ELFCLASS32:
1182 switch(Header->e_machine) {
1183 case ELF::EM_386:
1184 return "ELF32-i386";
1185 case ELF::EM_X86_64:
1186 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001187 case ELF::EM_ARM:
1188 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001189 default:
1190 return "ELF32-unknown";
1191 }
1192 case ELF::ELFCLASS64:
1193 switch(Header->e_machine) {
1194 case ELF::EM_386:
1195 return "ELF64-i386";
1196 case ELF::EM_X86_64:
1197 return "ELF64-x86-64";
1198 default:
1199 return "ELF64-unknown";
1200 }
1201 default:
1202 // FIXME: Proper error handling.
1203 report_fatal_error("Invalid ELFCLASS!");
1204 }
1205}
1206
1207template<support::endianness target_endianness, bool is64Bits>
1208unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1209 switch(Header->e_machine) {
1210 case ELF::EM_386:
1211 return Triple::x86;
1212 case ELF::EM_X86_64:
1213 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001214 case ELF::EM_ARM:
1215 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001216 default:
1217 return Triple::UnknownArch;
1218 }
1219}
1220
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001221
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001222template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001223template<typename T>
1224inline const T *
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001225ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1226 uint32_t Entry) const {
1227 return getEntry<T>(getSection(Section), Entry);
1228}
1229
1230template<support::endianness target_endianness, bool is64Bits>
1231template<typename T>
1232inline const T *
1233ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1234 uint32_t Entry) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001235 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +00001236 base()
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001237 + Section->sh_offset
1238 + (Entry * Section->sh_entsize));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001239}
1240
1241template<support::endianness target_endianness, bool is64Bits>
1242const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1243ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001244 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001245}
1246
1247template<support::endianness target_endianness, bool is64Bits>
1248const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1249ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001250 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001251}
1252
1253template<support::endianness target_endianness, bool is64Bits>
1254const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1255ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001256 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001257}
1258
1259template<support::endianness target_endianness, bool is64Bits>
1260const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1261ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001262 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001263 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001264 // FIXME: Proper error handling.
1265 report_fatal_error("Invalid symbol table section!");
1266 return sec;
1267}
1268
1269template<support::endianness target_endianness, bool is64Bits>
1270const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001271ELFObjectFile<target_endianness, is64Bits>::getSection(uint16_t index) const {
1272 if (index == 0 || index >= ELF::SHN_LORESERVE)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001273 return 0;
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001274 if (!SectionHeaderTable || index >= Header->e_shnum)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001275 // FIXME: Proper error handling.
1276 report_fatal_error("Invalid section index!");
1277
1278 return reinterpret_cast<const Elf_Shdr *>(
1279 reinterpret_cast<const char *>(SectionHeaderTable)
1280 + (index * Header->e_shentsize));
1281}
1282
1283template<support::endianness target_endianness, bool is64Bits>
1284const char *ELFObjectFile<target_endianness, is64Bits>
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001285 ::getString(uint16_t section,
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001286 ELF::Elf32_Word offset) const {
1287 return getString(getSection(section), offset);
1288}
1289
1290template<support::endianness target_endianness, bool is64Bits>
1291const char *ELFObjectFile<target_endianness, is64Bits>
1292 ::getString(const Elf_Shdr *section,
1293 ELF::Elf32_Word offset) const {
1294 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1295 if (offset >= section->sh_size)
1296 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001297 report_fatal_error("Symbol name offset outside of string table!");
1298 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001299}
1300
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001301template<support::endianness target_endianness, bool is64Bits>
1302error_code ELFObjectFile<target_endianness, is64Bits>
1303 ::getSymbolName(const Elf_Sym *symb,
1304 StringRef &Result) const {
1305 if (symb->st_name == 0) {
1306 const Elf_Shdr *section = getSection(symb->st_shndx);
1307 if (!section)
1308 Result = "";
1309 else
1310 Result = getString(dot_shstrtab_sec, section->sh_name);
1311 return object_error::success;
1312 }
1313
1314 // Use the default symbol table name section.
1315 Result = getString(dot_strtab_sec, symb->st_name);
1316 return object_error::success;
1317}
1318
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001319// EI_CLASS, EI_DATA.
1320static std::pair<unsigned char, unsigned char>
1321getElfArchType(MemoryBuffer *Object) {
1322 if (Object->getBufferSize() < ELF::EI_NIDENT)
1323 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1324 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1325 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1326}
1327
1328namespace llvm {
1329
1330 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
1331 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001332 error_code ec;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001333 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001334 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001335 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001336 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001337 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001338 return new ELFObjectFile<support::little, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001339 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001340 return new ELFObjectFile<support::big, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001341 // FIXME: Proper error handling.
1342 report_fatal_error("Not an ELF object file!");
1343 }
1344
1345} // end namespace llvm