blob: e427bedb982da71b77a2f88f149e247805e467c8 [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;
317 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;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000320 const char *getString(uint16_t section, uint32_t offset) const;
321 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;
340 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000341 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
342 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000343 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
344 bool &Result) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000345 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
346 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000347
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000348 virtual error_code getRelocationNext(DataRefImpl Rel,
349 RelocationRef &Res) const;
350 virtual error_code getRelocationAddress(DataRefImpl Rel,
351 uint64_t &Res) const;
352 virtual error_code getRelocationSymbol(DataRefImpl Rel,
353 SymbolRef &Res) const;
354 virtual error_code getRelocationType(DataRefImpl Rel,
355 uint32_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000356 virtual error_code getRelocationTypeName(DataRefImpl Rel,
357 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000358 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
359 int64_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000360 virtual error_code getRelocationValueString(DataRefImpl Rel,
361 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000362
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000363public:
Michael J. Spencer001c9202011-06-25 17:54:50 +0000364 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000365 virtual symbol_iterator begin_symbols() const;
366 virtual symbol_iterator end_symbols() const;
367 virtual section_iterator begin_sections() const;
368 virtual section_iterator end_sections() const;
369
370 virtual uint8_t getBytesInAddress() const;
371 virtual StringRef getFileFormatName() const;
372 virtual unsigned getArch() const;
373};
374} // end namespace
375
376template<support::endianness target_endianness, bool is64Bits>
377void ELFObjectFile<target_endianness, is64Bits>
378 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000379 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000380 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000381 // FIXME: We really need to do proper error handling in the case of an invalid
382 // input file. Because we don't use exceptions, I think we'll just pass
383 // an error object around.
384 if (!( symb
385 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000386 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000387 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000388 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000389 + SymbolTableSection->sh_offset
390 + SymbolTableSection->sh_size)))
391 // FIXME: Proper error handling.
392 report_fatal_error("Symb must point to a valid symbol!");
393}
394
395template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000396error_code ELFObjectFile<target_endianness, is64Bits>
397 ::getSymbolNext(DataRefImpl Symb,
398 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000399 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000400 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000401
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000402 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000403 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000404 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000405 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000406 ++Symb.d.b;
407 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000408 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000409 if (Symb.d.b >= SymbolTableSections.size()) {
410 Symb.d.a = std::numeric_limits<uint32_t>::max();
411 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000412 }
413 }
414
Michael J. Spencer25b15772011-06-25 17:55:23 +0000415 Result = SymbolRef(Symb, this);
416 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000417}
418
419template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000420error_code ELFObjectFile<target_endianness, is64Bits>
421 ::getSymbolName(DataRefImpl Symb,
422 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000423 validateSymbol(Symb);
424 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000425 return getSymbolName(symb, Result);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000426}
427
428template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000429error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000430 ::getSymbolOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000431 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000432 validateSymbol(Symb);
433 const Elf_Sym *symb = getSymbol(Symb);
434 const Elf_Shdr *Section;
435 switch (symb->st_shndx) {
436 case ELF::SHN_COMMON:
437 // Undefined symbols have no address yet.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000438 case ELF::SHN_UNDEF:
439 Result = UnknownAddressOrSize;
440 return object_error::success;
441 case ELF::SHN_ABS:
442 Result = symb->st_value;
443 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000444 default: Section = getSection(symb->st_shndx);
445 }
446
447 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000448 case ELF::STT_SECTION:
449 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
450 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000451 case ELF::STT_FUNC:
452 case ELF::STT_OBJECT:
453 case ELF::STT_NOTYPE:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000454 Result = symb->st_value;
455 return object_error::success;
456 default:
457 Result = UnknownAddressOrSize;
458 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000459 }
460}
461
462template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000463error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000464 ::getSymbolAddress(DataRefImpl Symb,
465 uint64_t &Result) const {
466 validateSymbol(Symb);
467 const Elf_Sym *symb = getSymbol(Symb);
468 const Elf_Shdr *Section;
469 switch (symb->st_shndx) {
470 case ELF::SHN_COMMON: // Fall through.
471 // Undefined symbols have no address yet.
472 case ELF::SHN_UNDEF:
473 Result = UnknownAddressOrSize;
474 return object_error::success;
475 case ELF::SHN_ABS:
476 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value);
477 return object_error::success;
478 default: Section = getSection(symb->st_shndx);
479 }
480 const uint8_t* addr = base();
481 if (Section)
482 addr += Section->sh_offset;
483 switch (symb->getType()) {
484 case ELF::STT_SECTION:
485 Result = reinterpret_cast<uintptr_t>(addr);
486 return object_error::success;
487 case ELF::STT_FUNC: // Fall through.
488 case ELF::STT_OBJECT: // Fall through.
489 case ELF::STT_NOTYPE:
490 addr += symb->st_value;
491 Result = reinterpret_cast<uintptr_t>(addr);
492 return object_error::success;
493 default:
494 Result = UnknownAddressOrSize;
495 return object_error::success;
496 }
497}
498
499template<support::endianness target_endianness, bool is64Bits>
500error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000501 ::getSymbolSize(DataRefImpl Symb,
502 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000503 validateSymbol(Symb);
504 const Elf_Sym *symb = getSymbol(Symb);
505 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000506 Result = UnknownAddressOrSize;
507 Result = symb->st_size;
508 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000509}
510
511template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000512error_code ELFObjectFile<target_endianness, is64Bits>
513 ::getSymbolNMTypeChar(DataRefImpl Symb,
514 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000515 validateSymbol(Symb);
516 const Elf_Sym *symb = getSymbol(Symb);
517 const Elf_Shdr *Section = getSection(symb->st_shndx);
518
519 char ret = '?';
520
521 if (Section) {
522 switch (Section->sh_type) {
523 case ELF::SHT_PROGBITS:
524 case ELF::SHT_DYNAMIC:
525 switch (Section->sh_flags) {
526 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
527 ret = 't'; break;
528 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
529 ret = 'd'; break;
530 case ELF::SHF_ALLOC:
531 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
532 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
533 ret = 'r'; break;
534 }
535 break;
536 case ELF::SHT_NOBITS: ret = 'b';
537 }
538 }
539
540 switch (symb->st_shndx) {
541 case ELF::SHN_UNDEF:
542 if (ret == '?')
543 ret = 'U';
544 break;
545 case ELF::SHN_ABS: ret = 'a'; break;
546 case ELF::SHN_COMMON: ret = 'c'; break;
547 }
548
549 switch (symb->getBinding()) {
550 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
551 case ELF::STB_WEAK:
552 if (symb->st_shndx == ELF::SHN_UNDEF)
553 ret = 'w';
554 else
555 if (symb->getType() == ELF::STT_OBJECT)
556 ret = 'V';
557 else
558 ret = 'W';
559 }
560
Michael J. Spencer25b15772011-06-25 17:55:23 +0000561 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
562 StringRef name;
563 if (error_code ec = getSymbolName(Symb, name))
564 return ec;
565 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000566 .StartsWith(".debug", 'N')
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000567 .StartsWith(".note", 'n')
568 .Default('?');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000569 return object_error::success;
570 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000571
Michael J. Spencer25b15772011-06-25 17:55:23 +0000572 Result = ret;
573 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000574}
575
576template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000577error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000578 ::getSymbolType(DataRefImpl Symb,
579 SymbolRef::SymbolType &Result) const {
580 validateSymbol(Symb);
581 const Elf_Sym *symb = getSymbol(Symb);
582
583 if (symb->st_shndx == ELF::SHN_UNDEF) {
584 Result = SymbolRef::ST_External;
585 return object_error::success;
586 }
587
588 switch (symb->getType()) {
589 case ELF::STT_FUNC:
590 Result = SymbolRef::ST_Function;
591 break;
592 case ELF::STT_OBJECT:
593 Result = SymbolRef::ST_Data;
594 break;
595 default:
596 Result = SymbolRef::ST_Other;
597 break;
598 }
599 return object_error::success;
600}
601
602template<support::endianness target_endianness, bool is64Bits>
603error_code ELFObjectFile<target_endianness, is64Bits>
604 ::isSymbolGlobal(DataRefImpl Symb,
605 bool &Result) const {
606 validateSymbol(Symb);
607 const Elf_Sym *symb = getSymbol(Symb);
608
609 Result = symb->getBinding() == ELF::STB_GLOBAL;
610 return object_error::success;
611}
612
613template<support::endianness target_endianness, bool is64Bits>
614error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000615 ::isSymbolInternal(DataRefImpl Symb,
616 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000617 validateSymbol(Symb);
618 const Elf_Sym *symb = getSymbol(Symb);
619
620 if ( symb->getType() == ELF::STT_FILE
621 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000622 Result = true;
623 Result = false;
624 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000625}
626
627template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000628error_code ELFObjectFile<target_endianness, is64Bits>
629 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000630 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000631 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000632 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000633 Result = SectionRef(Sec, this);
634 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000635}
636
637template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000638error_code ELFObjectFile<target_endianness, is64Bits>
639 ::getSectionName(DataRefImpl Sec,
640 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000641 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000642 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
643 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000644}
645
646template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000647error_code ELFObjectFile<target_endianness, is64Bits>
648 ::getSectionAddress(DataRefImpl Sec,
649 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000650 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000651 Result = sec->sh_addr;
652 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000653}
654
655template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000656error_code ELFObjectFile<target_endianness, is64Bits>
657 ::getSectionSize(DataRefImpl Sec,
658 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000659 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000660 Result = sec->sh_size;
661 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000662}
663
664template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000665error_code ELFObjectFile<target_endianness, is64Bits>
666 ::getSectionContents(DataRefImpl Sec,
667 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000668 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000669 const char *start = (const char*)base() + sec->sh_offset;
670 Result = StringRef(start, sec->sh_size);
671 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000672}
673
674template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000675error_code ELFObjectFile<target_endianness, is64Bits>
676 ::isSectionText(DataRefImpl Sec,
677 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000678 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000679 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000680 Result = true;
681 else
682 Result = false;
683 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000684}
685
686template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000687error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000688 ::isSectionData(DataRefImpl Sec,
689 bool &Result) const {
690 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
691 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
692 && sec->sh_type == ELF::SHT_PROGBITS)
693 Result = true;
694 else
695 Result = false;
696 return object_error::success;
697}
698
699template<support::endianness target_endianness, bool is64Bits>
700error_code ELFObjectFile<target_endianness, is64Bits>
701 ::isSectionBSS(DataRefImpl Sec,
702 bool &Result) const {
703 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
704 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
705 && sec->sh_type == ELF::SHT_NOBITS)
706 Result = true;
707 else
708 Result = false;
709 return object_error::success;
710}
711
712template<support::endianness target_endianness, bool is64Bits>
713error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000714 ::sectionContainsSymbol(DataRefImpl Sec,
715 DataRefImpl Symb,
716 bool &Result) const {
717 // FIXME: Unimplemented.
718 Result = false;
719 return object_error::success;
720}
721
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000722template<support::endianness target_endianness, bool is64Bits>
723relocation_iterator ELFObjectFile<target_endianness, is64Bits>
724 ::getSectionRelBegin(DataRefImpl Sec) const {
725 DataRefImpl RelData;
726 memset(&RelData, 0, sizeof(RelData));
727 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
728 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
729 if (sec != 0 && ittr != SectionRelocMap.end()) {
730 RelData.w.a = getSection(ittr->second[0])->sh_link;
731 RelData.w.b = ittr->second[0];
732 RelData.w.c = 0;
733 }
734 return relocation_iterator(RelocationRef(RelData, this));
735}
736
737template<support::endianness target_endianness, bool is64Bits>
738relocation_iterator ELFObjectFile<target_endianness, is64Bits>
739 ::getSectionRelEnd(DataRefImpl Sec) const {
740 DataRefImpl RelData;
741 memset(&RelData, 0, sizeof(RelData));
742 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
743 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
744 if (sec != 0 && ittr != SectionRelocMap.end()) {
745 // Get the index of the last relocation section for this section.
746 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
747 const Elf_Shdr *relocsec = getSection(relocsecindex);
748 RelData.w.a = relocsec->sh_link;
749 RelData.w.b = relocsecindex;
750 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
751 }
752 return relocation_iterator(RelocationRef(RelData, this));
753}
754
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000755// Relocations
756template<support::endianness target_endianness, bool is64Bits>
757error_code ELFObjectFile<target_endianness, is64Bits>
758 ::getRelocationNext(DataRefImpl Rel,
759 RelocationRef &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000760 ++Rel.w.c;
761 const Elf_Shdr *relocsec = getSection(Rel.w.b);
762 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
763 // We have reached the end of the relocations for this section. See if there
764 // is another relocation section.
765 typename RelocMap_t::mapped_type &relocseclist =
766 SectionRelocMap.lookup(getSection(Rel.w.a));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000767
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000768 // Do a binary search for the current reloc section index (which must be
769 // present). Then get the next one.
770 typename RelocMap_t::mapped_type::const_iterator loc =
771 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
772 ++loc;
773
774 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
775 // to the end iterator.
776 if (loc != relocseclist.end()) {
777 Rel.w.b = *loc;
778 Rel.w.a = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000779 }
780 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000781 Result = RelocationRef(Rel, this);
782 return object_error::success;
783}
784
785template<support::endianness target_endianness, bool is64Bits>
786error_code ELFObjectFile<target_endianness, is64Bits>
787 ::getRelocationSymbol(DataRefImpl Rel,
788 SymbolRef &Result) const {
789 uint32_t symbolIdx;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000790 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000791 switch (sec->sh_type) {
792 default :
793 report_fatal_error("Invalid section type in Rel!");
794 case ELF::SHT_REL : {
795 symbolIdx = getRel(Rel)->getSymbol();
796 break;
797 }
798 case ELF::SHT_RELA : {
799 symbolIdx = getRela(Rel)->getSymbol();
800 break;
801 }
802 }
803 DataRefImpl SymbolData;
804 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
805 if (it == SymbolTableSectionsIndexMap.end())
806 report_fatal_error("Relocation symbol table not found!");
807 SymbolData.d.a = symbolIdx;
808 SymbolData.d.b = it->second;
809 Result = SymbolRef(SymbolData, this);
810 return object_error::success;
811}
812
813template<support::endianness target_endianness, bool is64Bits>
814error_code ELFObjectFile<target_endianness, is64Bits>
815 ::getRelocationAddress(DataRefImpl Rel,
816 uint64_t &Result) const {
817 uint64_t offset;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000818 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000819 switch (sec->sh_type) {
820 default :
821 report_fatal_error("Invalid section type in Rel!");
822 case ELF::SHT_REL : {
823 offset = getRel(Rel)->r_offset;
824 break;
825 }
826 case ELF::SHT_RELA : {
827 offset = getRela(Rel)->r_offset;
828 break;
829 }
830 }
831
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000832 Result = offset;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000833 return object_error::success;
834}
835
836template<support::endianness target_endianness, bool is64Bits>
837error_code ELFObjectFile<target_endianness, is64Bits>
838 ::getRelocationType(DataRefImpl Rel,
839 uint32_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000840 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000841 switch (sec->sh_type) {
842 default :
843 report_fatal_error("Invalid section type in Rel!");
844 case ELF::SHT_REL : {
845 Result = getRel(Rel)->getType();
846 break;
847 }
848 case ELF::SHT_RELA : {
849 Result = getRela(Rel)->getType();
850 break;
851 }
852 }
853 return object_error::success;
854}
855
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000856#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
857 case ELF::enum: res = #enum; break;
858
859template<support::endianness target_endianness, bool is64Bits>
860error_code ELFObjectFile<target_endianness, is64Bits>
861 ::getRelocationTypeName(DataRefImpl Rel,
862 SmallVectorImpl<char> &Result) const {
863 const Elf_Shdr *sec = getSection(Rel.w.b);
864 uint8_t type;
865 StringRef res;
866 switch (sec->sh_type) {
867 default :
868 return object_error::parse_failed;
869 case ELF::SHT_REL : {
870 type = getRel(Rel)->getType();
871 break;
872 }
873 case ELF::SHT_RELA : {
874 type = getRela(Rel)->getType();
875 break;
876 }
877 }
878 switch (Header->e_machine) {
879 case ELF::EM_X86_64:
880 switch (type) {
881 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
882 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
883 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
884 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
885 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
886 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
887 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
888 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
889 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
890 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
891 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
892 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
893 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
894 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
895 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
896 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
897 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
898 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
899 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
900 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
901 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
902 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
903 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
904 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
905 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
906 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
907 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
908 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
909 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
910 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
911 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
912 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
913 default:
914 res = "Unknown";
915 }
916 break;
917 case ELF::EM_386:
918 switch (type) {
919 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
920 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
923 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
924 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
925 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
926 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
927 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
928 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
929 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
930 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
953 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
954 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
955 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
956 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
957 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
958 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
959 default:
960 res = "Unknown";
961 }
962 break;
963 default:
964 res = "Unknown";
965 }
966 Result.append(res.begin(), res.end());
967 return object_error::success;
968}
969
970#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
971
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000972template<support::endianness target_endianness, bool is64Bits>
973error_code ELFObjectFile<target_endianness, is64Bits>
974 ::getRelocationAdditionalInfo(DataRefImpl Rel,
975 int64_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000976 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000977 switch (sec->sh_type) {
978 default :
979 report_fatal_error("Invalid section type in Rel!");
980 case ELF::SHT_REL : {
981 Result = 0;
982 return object_error::success;
983 }
984 case ELF::SHT_RELA : {
985 Result = getRela(Rel)->r_addend;
986 return object_error::success;
987 }
988 }
989}
990
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000991template<support::endianness target_endianness, bool is64Bits>
992error_code ELFObjectFile<target_endianness, is64Bits>
993 ::getRelocationValueString(DataRefImpl Rel,
994 SmallVectorImpl<char> &Result) const {
995 const Elf_Shdr *sec = getSection(Rel.w.b);
996 uint8_t type;
997 StringRef res;
998 int64_t addend = 0;
999 uint16_t symbol_index = 0;
1000 switch (sec->sh_type) {
1001 default :
1002 return object_error::parse_failed;
1003 case ELF::SHT_REL : {
1004 type = getRel(Rel)->getType();
1005 symbol_index = getRel(Rel)->getSymbol();
1006 // TODO: Read implicit addend from section data.
1007 break;
1008 }
1009 case ELF::SHT_RELA : {
1010 type = getRela(Rel)->getType();
1011 symbol_index = getRela(Rel)->getSymbol();
1012 addend = getRela(Rel)->r_addend;
1013 break;
1014 }
1015 }
1016 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1017 StringRef symname;
1018 if (error_code ec = getSymbolName(symb, symname))
1019 return ec;
1020 switch (Header->e_machine) {
1021 case ELF::EM_X86_64:
1022 switch (type) {
1023 case ELF::R_X86_64_32S:
1024 res = symname;
1025 break;
1026 case ELF::R_X86_64_PC32: {
1027 std::string fmtbuf;
1028 raw_string_ostream fmt(fmtbuf);
1029 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1030 fmt.flush();
1031 Result.append(fmtbuf.begin(), fmtbuf.end());
1032 }
1033 break;
1034 default:
1035 res = "Unknown";
1036 }
1037 break;
1038 default:
1039 res = "Unknown";
1040 }
1041 if (Result.empty())
1042 Result.append(res.begin(), res.end());
1043 return object_error::success;
1044}
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001045
Benjamin Kramer07ea23a2011-07-15 18:39:21 +00001046template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +00001047ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1048 , error_code &ec)
1049 : ObjectFile(Binary::isELF, Object, ec)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001050 , SectionHeaderTable(0)
1051 , dot_shstrtab_sec(0)
1052 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001053 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001054
1055 if (Header->e_shoff == 0)
1056 return;
1057
1058 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +00001059 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001060 uint32_t SectionTableSize = Header->e_shnum * Header->e_shentsize;
1061 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize
Michael J. Spencer001c9202011-06-25 17:54:50 +00001062 <= base() + Data->getBufferSize()))
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001063 // FIXME: Proper error handling.
1064 report_fatal_error("Section table goes past end of file!");
1065
1066
1067 // To find the symbol tables we walk the section table to find SHT_STMTAB.
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001068 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001069 for (unsigned i = 0; i < Header->e_shnum; ++i) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001070 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001071 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001072 SymbolTableSections.push_back(sh);
1073 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001074 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001075 SectionRelocMap[getSection(sh->sh_link)].push_back(i);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001076 }
1077 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001078 }
1079
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001080 // Sort section relocation lists by index.
1081 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1082 e = SectionRelocMap.end(); i != e; ++i) {
1083 std::sort(i->second.begin(), i->second.end());
1084 }
1085
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001086 // Get string table sections.
1087 dot_shstrtab_sec = getSection(Header->e_shstrndx);
1088 if (dot_shstrtab_sec) {
1089 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001090 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001091 [dot_shstrtab_sec->sh_size - 1] != 0)
1092 // FIXME: Proper error handling.
1093 report_fatal_error("String table must end with a null terminator!");
1094 }
1095
1096 // Merge this into the above loop.
1097 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
1098 *e = i + Header->e_shnum * Header->e_shentsize;
1099 i != e; i += Header->e_shentsize) {
1100 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1101 if (sh->sh_type == ELF::SHT_STRTAB) {
1102 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1103 if (SectionName == ".strtab") {
1104 if (dot_strtab_sec != 0)
1105 // FIXME: Proper error handling.
1106 report_fatal_error("Already found section named .strtab!");
1107 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +00001108 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001109 if (dot_strtab[sh->sh_size - 1] != 0)
1110 // FIXME: Proper error handling.
1111 report_fatal_error("String table must end with a null terminator!");
1112 }
1113 }
1114 }
1115}
1116
1117template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001118symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1119 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001120 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001121 memset(&SymbolData, 0, sizeof(SymbolData));
1122 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001123 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1124 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001125 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001126 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1127 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001128 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001129 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001130}
1131
1132template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001133symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1134 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001135 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001136 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001137 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1138 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1139 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 +00001143section_iterator ELFObjectFile<target_endianness, is64Bits>
1144 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001145 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001146 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001147 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001148 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001149}
1150
1151template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001152section_iterator ELFObjectFile<target_endianness, is64Bits>
1153 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001154 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001155 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001156 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001157 + Header->e_shoff
1158 + (Header->e_shentsize * Header->e_shnum));
1159 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001160}
1161
1162template<support::endianness target_endianness, bool is64Bits>
1163uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001164 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001165}
1166
1167template<support::endianness target_endianness, bool is64Bits>
1168StringRef ELFObjectFile<target_endianness, is64Bits>
1169 ::getFileFormatName() const {
1170 switch(Header->e_ident[ELF::EI_CLASS]) {
1171 case ELF::ELFCLASS32:
1172 switch(Header->e_machine) {
1173 case ELF::EM_386:
1174 return "ELF32-i386";
1175 case ELF::EM_X86_64:
1176 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001177 case ELF::EM_ARM:
1178 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001179 default:
1180 return "ELF32-unknown";
1181 }
1182 case ELF::ELFCLASS64:
1183 switch(Header->e_machine) {
1184 case ELF::EM_386:
1185 return "ELF64-i386";
1186 case ELF::EM_X86_64:
1187 return "ELF64-x86-64";
1188 default:
1189 return "ELF64-unknown";
1190 }
1191 default:
1192 // FIXME: Proper error handling.
1193 report_fatal_error("Invalid ELFCLASS!");
1194 }
1195}
1196
1197template<support::endianness target_endianness, bool is64Bits>
1198unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1199 switch(Header->e_machine) {
1200 case ELF::EM_386:
1201 return Triple::x86;
1202 case ELF::EM_X86_64:
1203 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001204 case ELF::EM_ARM:
1205 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001206 default:
1207 return Triple::UnknownArch;
1208 }
1209}
1210
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001211
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001212template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001213template<typename T>
1214inline const T *
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001215ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1216 uint32_t Entry) const {
1217 return getEntry<T>(getSection(Section), Entry);
1218}
1219
1220template<support::endianness target_endianness, bool is64Bits>
1221template<typename T>
1222inline const T *
1223ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1224 uint32_t Entry) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001225 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +00001226 base()
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001227 + Section->sh_offset
1228 + (Entry * Section->sh_entsize));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001229}
1230
1231template<support::endianness target_endianness, bool is64Bits>
1232const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1233ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001234 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001235}
1236
1237template<support::endianness target_endianness, bool is64Bits>
1238const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1239ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001240 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001241}
1242
1243template<support::endianness target_endianness, bool is64Bits>
1244const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1245ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001246 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001247}
1248
1249template<support::endianness target_endianness, bool is64Bits>
1250const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1251ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001252 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001253 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001254 // FIXME: Proper error handling.
1255 report_fatal_error("Invalid symbol table section!");
1256 return sec;
1257}
1258
1259template<support::endianness target_endianness, bool is64Bits>
1260const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1261ELFObjectFile<target_endianness, is64Bits>::getSection(uint16_t index) const {
1262 if (index == 0 || index >= ELF::SHN_LORESERVE)
1263 return 0;
1264 if (!SectionHeaderTable || index >= Header->e_shnum)
1265 // FIXME: Proper error handling.
1266 report_fatal_error("Invalid section index!");
1267
1268 return reinterpret_cast<const Elf_Shdr *>(
1269 reinterpret_cast<const char *>(SectionHeaderTable)
1270 + (index * Header->e_shentsize));
1271}
1272
1273template<support::endianness target_endianness, bool is64Bits>
1274const char *ELFObjectFile<target_endianness, is64Bits>
1275 ::getString(uint16_t section,
1276 ELF::Elf32_Word offset) const {
1277 return getString(getSection(section), offset);
1278}
1279
1280template<support::endianness target_endianness, bool is64Bits>
1281const char *ELFObjectFile<target_endianness, is64Bits>
1282 ::getString(const Elf_Shdr *section,
1283 ELF::Elf32_Word offset) const {
1284 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1285 if (offset >= section->sh_size)
1286 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001287 report_fatal_error("Symbol name offset outside of string table!");
1288 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001289}
1290
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001291template<support::endianness target_endianness, bool is64Bits>
1292error_code ELFObjectFile<target_endianness, is64Bits>
1293 ::getSymbolName(const Elf_Sym *symb,
1294 StringRef &Result) const {
1295 if (symb->st_name == 0) {
1296 const Elf_Shdr *section = getSection(symb->st_shndx);
1297 if (!section)
1298 Result = "";
1299 else
1300 Result = getString(dot_shstrtab_sec, section->sh_name);
1301 return object_error::success;
1302 }
1303
1304 // Use the default symbol table name section.
1305 Result = getString(dot_strtab_sec, symb->st_name);
1306 return object_error::success;
1307}
1308
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001309// EI_CLASS, EI_DATA.
1310static std::pair<unsigned char, unsigned char>
1311getElfArchType(MemoryBuffer *Object) {
1312 if (Object->getBufferSize() < ELF::EI_NIDENT)
1313 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1314 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1315 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1316}
1317
1318namespace llvm {
1319
1320 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
1321 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001322 error_code ec;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001323 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001324 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001325 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001326 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001327 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001328 return new ELFObjectFile<support::little, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001329 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001330 return new ELFObjectFile<support::big, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001331 // FIXME: Proper error handling.
1332 report_fatal_error("Not an ELF object file!");
1333 }
1334
1335} // end namespace llvm