blob: 7add2d8a52eaf71380d321040469a0b8c8b16412 [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;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000299 DenseMap<const Elf_Sym*, Elf_Word> ExtendedSymbolTable;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000300
301 /// @brief Map sections to an array of relocation sections that reference
302 /// them sorted by section index.
303 RelocMap_t SectionRelocMap;
304
305 /// @brief Get the relocation section that contains \a Rel.
306 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
307 return getSection(Rel.w.b);
308 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000309
310 void validateSymbol(DataRefImpl Symb) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000311 bool isRelocationHasAddend(DataRefImpl Rel) const;
312 template<typename T>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000313 const T *getEntry(uint16_t Section, uint32_t Entry) const;
314 template<typename T>
315 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000316 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
317 const Elf_Shdr *getSection(DataRefImpl index) const;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000318 const Elf_Shdr *getSection(uint32_t index) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000319 const Elf_Rel *getRel(DataRefImpl Rel) const;
320 const Elf_Rela *getRela(DataRefImpl Rela) const;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000321 const char *getString(uint32_t section, uint32_t offset) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000322 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000323 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000324
325protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000326 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
327 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000328 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000329 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
330 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
331 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
332 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000333 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
334 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000335
Michael J. Spencer25b15772011-06-25 17:55:23 +0000336 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
337 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
338 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
339 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
340 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000341 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000342 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000343 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
344 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000345 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
346 bool &Result) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000347 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
348 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000349
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000350 virtual error_code getRelocationNext(DataRefImpl Rel,
351 RelocationRef &Res) const;
352 virtual error_code getRelocationAddress(DataRefImpl Rel,
353 uint64_t &Res) const;
354 virtual error_code getRelocationSymbol(DataRefImpl Rel,
355 SymbolRef &Res) const;
356 virtual error_code getRelocationType(DataRefImpl Rel,
357 uint32_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000358 virtual error_code getRelocationTypeName(DataRefImpl Rel,
359 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000360 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
361 int64_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000362 virtual error_code getRelocationValueString(DataRefImpl Rel,
363 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000364
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000365public:
Michael J. Spencer001c9202011-06-25 17:54:50 +0000366 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000367 virtual symbol_iterator begin_symbols() const;
368 virtual symbol_iterator end_symbols() const;
369 virtual section_iterator begin_sections() const;
370 virtual section_iterator end_sections() const;
371
372 virtual uint8_t getBytesInAddress() const;
373 virtual StringRef getFileFormatName() const;
374 virtual unsigned getArch() const;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000375
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000376 uint64_t getNumSections() const;
377 uint64_t getStringTableIndex() const;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000378 uint64_t getSymbolTableIndex(const Elf_Sym *symb) const;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000379 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000380};
381} // end namespace
382
383template<support::endianness target_endianness, bool is64Bits>
384void ELFObjectFile<target_endianness, is64Bits>
385 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000386 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000387 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000388 // FIXME: We really need to do proper error handling in the case of an invalid
389 // input file. Because we don't use exceptions, I think we'll just pass
390 // an error object around.
391 if (!( symb
392 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000393 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000394 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000395 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000396 + SymbolTableSection->sh_offset
397 + SymbolTableSection->sh_size)))
398 // FIXME: Proper error handling.
399 report_fatal_error("Symb must point to a valid symbol!");
400}
401
402template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000403error_code ELFObjectFile<target_endianness, is64Bits>
404 ::getSymbolNext(DataRefImpl Symb,
405 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000406 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000407 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000408
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000409 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000410 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000411 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000412 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000413 ++Symb.d.b;
414 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000415 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000416 if (Symb.d.b >= SymbolTableSections.size()) {
417 Symb.d.a = std::numeric_limits<uint32_t>::max();
418 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000419 }
420 }
421
Michael J. Spencer25b15772011-06-25 17:55:23 +0000422 Result = SymbolRef(Symb, this);
423 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000424}
425
426template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000427error_code ELFObjectFile<target_endianness, is64Bits>
428 ::getSymbolName(DataRefImpl Symb,
429 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000430 validateSymbol(Symb);
Nick Lewycky15c3f722011-10-11 02:57:48 +0000431 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000432 return getSymbolName(symb, Result);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000433}
434
435template<support::endianness target_endianness, bool is64Bits>
Nick Lewycky15c3f722011-10-11 02:57:48 +0000436uint64_t ELFObjectFile<target_endianness, is64Bits>
437 ::getSymbolTableIndex(const Elf_Sym *symb) const {
438 if (symb->st_shndx == ELF::SHN_XINDEX)
439 return ExtendedSymbolTable.lookup(symb);
440 return symb->st_shndx;
441}
442
443template<support::endianness target_endianness, bool is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000444const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
445ELFObjectFile<target_endianness, is64Bits>
446 ::getSection(const Elf_Sym *symb) const {
NAKAMURA Takumi18dcb872011-10-12 10:28:55 +0000447 if (symb->st_shndx == ELF::SHN_XINDEX) {
448 if (!ExtendedSymbolTable.count(symb))
449 return 0;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000450 return getSection(ExtendedSymbolTable.lookup(symb));
NAKAMURA Takumi18dcb872011-10-12 10:28:55 +0000451 }
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000452 if (symb->st_shndx >= ELF::SHN_LORESERVE)
453 return 0;
454 return getSection(symb->st_shndx);
455}
456
457template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000458error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000459 ::getSymbolOffset(DataRefImpl Symb,
Nick Lewycky15c3f722011-10-11 02:57:48 +0000460 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000461 validateSymbol(Symb);
462 const Elf_Sym *symb = getSymbol(Symb);
463 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000464 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000465 case ELF::SHN_COMMON:
466 // Undefined symbols have no address yet.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000467 case ELF::SHN_UNDEF:
468 Result = UnknownAddressOrSize;
469 return object_error::success;
470 case ELF::SHN_ABS:
471 Result = symb->st_value;
472 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000473 default: Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000474 }
475
476 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000477 case ELF::STT_SECTION:
478 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
479 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000480 case ELF::STT_FUNC:
481 case ELF::STT_OBJECT:
482 case ELF::STT_NOTYPE:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000483 Result = symb->st_value;
484 return object_error::success;
485 default:
486 Result = UnknownAddressOrSize;
487 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000488 }
489}
490
491template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000492error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000493 ::getSymbolAddress(DataRefImpl Symb,
494 uint64_t &Result) const {
495 validateSymbol(Symb);
496 const Elf_Sym *symb = getSymbol(Symb);
497 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000498 switch (getSymbolTableIndex(symb)) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000499 case ELF::SHN_COMMON: // Fall through.
500 // Undefined symbols have no address yet.
501 case ELF::SHN_UNDEF:
502 Result = UnknownAddressOrSize;
503 return object_error::success;
504 case ELF::SHN_ABS:
505 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value);
506 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000507 default: Section = getSection(symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000508 }
509 const uint8_t* addr = base();
510 if (Section)
511 addr += Section->sh_offset;
512 switch (symb->getType()) {
513 case ELF::STT_SECTION:
514 Result = reinterpret_cast<uintptr_t>(addr);
515 return object_error::success;
516 case ELF::STT_FUNC: // Fall through.
517 case ELF::STT_OBJECT: // Fall through.
518 case ELF::STT_NOTYPE:
519 addr += symb->st_value;
520 Result = reinterpret_cast<uintptr_t>(addr);
521 return object_error::success;
522 default:
523 Result = UnknownAddressOrSize;
524 return object_error::success;
525 }
526}
527
528template<support::endianness target_endianness, bool is64Bits>
529error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000530 ::getSymbolSize(DataRefImpl Symb,
531 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000532 validateSymbol(Symb);
533 const Elf_Sym *symb = getSymbol(Symb);
534 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000535 Result = UnknownAddressOrSize;
536 Result = symb->st_size;
537 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000538}
539
540template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000541error_code ELFObjectFile<target_endianness, is64Bits>
542 ::getSymbolNMTypeChar(DataRefImpl Symb,
543 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000544 validateSymbol(Symb);
545 const Elf_Sym *symb = getSymbol(Symb);
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000546 const Elf_Shdr *Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000547
548 char ret = '?';
549
550 if (Section) {
551 switch (Section->sh_type) {
552 case ELF::SHT_PROGBITS:
553 case ELF::SHT_DYNAMIC:
554 switch (Section->sh_flags) {
555 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
556 ret = 't'; break;
557 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
558 ret = 'd'; break;
559 case ELF::SHF_ALLOC:
560 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
561 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
562 ret = 'r'; break;
563 }
564 break;
565 case ELF::SHT_NOBITS: ret = 'b';
566 }
567 }
568
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000569 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000570 case ELF::SHN_UNDEF:
571 if (ret == '?')
572 ret = 'U';
573 break;
574 case ELF::SHN_ABS: ret = 'a'; break;
575 case ELF::SHN_COMMON: ret = 'c'; break;
576 }
577
578 switch (symb->getBinding()) {
579 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
580 case ELF::STB_WEAK:
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000581 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000582 ret = 'w';
583 else
584 if (symb->getType() == ELF::STT_OBJECT)
585 ret = 'V';
586 else
587 ret = 'W';
588 }
589
Michael J. Spencer25b15772011-06-25 17:55:23 +0000590 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
591 StringRef name;
592 if (error_code ec = getSymbolName(Symb, name))
593 return ec;
594 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000595 .StartsWith(".debug", 'N')
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000596 .StartsWith(".note", 'n')
597 .Default('?');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000598 return object_error::success;
599 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000600
Michael J. Spencer25b15772011-06-25 17:55:23 +0000601 Result = ret;
602 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000603}
604
605template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000606error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000607 ::getSymbolType(DataRefImpl Symb,
608 SymbolRef::SymbolType &Result) const {
609 validateSymbol(Symb);
610 const Elf_Sym *symb = getSymbol(Symb);
611
Nick Lewycky15c3f722011-10-11 02:57:48 +0000612 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000613 Result = SymbolRef::ST_External;
614 return object_error::success;
615 }
616
617 switch (symb->getType()) {
618 case ELF::STT_FUNC:
619 Result = SymbolRef::ST_Function;
620 break;
621 case ELF::STT_OBJECT:
622 Result = SymbolRef::ST_Data;
623 break;
624 default:
625 Result = SymbolRef::ST_Other;
626 break;
627 }
628 return object_error::success;
629}
630
631template<support::endianness target_endianness, bool is64Bits>
632error_code ELFObjectFile<target_endianness, is64Bits>
633 ::isSymbolGlobal(DataRefImpl Symb,
634 bool &Result) const {
635 validateSymbol(Symb);
636 const Elf_Sym *symb = getSymbol(Symb);
637
638 Result = symb->getBinding() == ELF::STB_GLOBAL;
639 return object_error::success;
640}
641
642template<support::endianness target_endianness, bool is64Bits>
643error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000644 ::isSymbolInternal(DataRefImpl Symb,
645 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000646 validateSymbol(Symb);
647 const Elf_Sym *symb = getSymbol(Symb);
648
649 if ( symb->getType() == ELF::STT_FILE
650 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000651 Result = true;
652 Result = false;
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 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000659 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000660 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000661 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000662 Result = SectionRef(Sec, this);
663 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000664}
665
666template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000667error_code ELFObjectFile<target_endianness, is64Bits>
668 ::getSectionName(DataRefImpl Sec,
669 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000670 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000671 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
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>
677 ::getSectionAddress(DataRefImpl Sec,
678 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000679 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000680 Result = sec->sh_addr;
681 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000682}
683
684template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000685error_code ELFObjectFile<target_endianness, is64Bits>
686 ::getSectionSize(DataRefImpl Sec,
687 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000688 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000689 Result = sec->sh_size;
690 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000691}
692
693template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000694error_code ELFObjectFile<target_endianness, is64Bits>
695 ::getSectionContents(DataRefImpl Sec,
696 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000697 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000698 const char *start = (const char*)base() + sec->sh_offset;
699 Result = StringRef(start, sec->sh_size);
700 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000701}
702
703template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000704error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000705 ::getSectionAlignment(DataRefImpl Sec,
706 uint64_t &Result) const {
707 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
708 Result = sec->sh_addralign;
709 return object_error::success;
710}
711
712template<support::endianness target_endianness, bool is64Bits>
713error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000714 ::isSectionText(DataRefImpl Sec,
715 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000716 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000717 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000718 Result = true;
719 else
720 Result = false;
721 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000722}
723
724template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000725error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000726 ::isSectionData(DataRefImpl Sec,
727 bool &Result) const {
728 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
729 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
730 && sec->sh_type == ELF::SHT_PROGBITS)
731 Result = true;
732 else
733 Result = false;
734 return object_error::success;
735}
736
737template<support::endianness target_endianness, bool is64Bits>
738error_code ELFObjectFile<target_endianness, is64Bits>
739 ::isSectionBSS(DataRefImpl Sec,
740 bool &Result) const {
741 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
742 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
743 && sec->sh_type == ELF::SHT_NOBITS)
744 Result = true;
745 else
746 Result = false;
747 return object_error::success;
748}
749
750template<support::endianness target_endianness, bool is64Bits>
751error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000752 ::sectionContainsSymbol(DataRefImpl Sec,
753 DataRefImpl Symb,
754 bool &Result) const {
755 // FIXME: Unimplemented.
756 Result = false;
757 return object_error::success;
758}
759
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000760template<support::endianness target_endianness, bool is64Bits>
761relocation_iterator ELFObjectFile<target_endianness, is64Bits>
762 ::getSectionRelBegin(DataRefImpl Sec) const {
763 DataRefImpl RelData;
764 memset(&RelData, 0, sizeof(RelData));
765 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
766 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
767 if (sec != 0 && ittr != SectionRelocMap.end()) {
768 RelData.w.a = getSection(ittr->second[0])->sh_link;
769 RelData.w.b = ittr->second[0];
770 RelData.w.c = 0;
771 }
772 return relocation_iterator(RelocationRef(RelData, this));
773}
774
775template<support::endianness target_endianness, bool is64Bits>
776relocation_iterator ELFObjectFile<target_endianness, is64Bits>
777 ::getSectionRelEnd(DataRefImpl Sec) const {
778 DataRefImpl RelData;
779 memset(&RelData, 0, sizeof(RelData));
780 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
781 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
782 if (sec != 0 && ittr != SectionRelocMap.end()) {
783 // Get the index of the last relocation section for this section.
784 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
785 const Elf_Shdr *relocsec = getSection(relocsecindex);
786 RelData.w.a = relocsec->sh_link;
787 RelData.w.b = relocsecindex;
788 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
789 }
790 return relocation_iterator(RelocationRef(RelData, this));
791}
792
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000793// Relocations
794template<support::endianness target_endianness, bool is64Bits>
795error_code ELFObjectFile<target_endianness, is64Bits>
796 ::getRelocationNext(DataRefImpl Rel,
797 RelocationRef &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000798 ++Rel.w.c;
799 const Elf_Shdr *relocsec = getSection(Rel.w.b);
800 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
801 // We have reached the end of the relocations for this section. See if there
802 // is another relocation section.
Michael J. Spencer01a4db32011-10-07 19:46:12 +0000803 typename RelocMap_t::mapped_type relocseclist =
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000804 SectionRelocMap.lookup(getSection(Rel.w.a));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000805
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000806 // Do a binary search for the current reloc section index (which must be
807 // present). Then get the next one.
808 typename RelocMap_t::mapped_type::const_iterator loc =
809 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
810 ++loc;
811
812 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
813 // to the end iterator.
814 if (loc != relocseclist.end()) {
815 Rel.w.b = *loc;
816 Rel.w.a = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000817 }
818 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000819 Result = RelocationRef(Rel, this);
820 return object_error::success;
821}
822
823template<support::endianness target_endianness, bool is64Bits>
824error_code ELFObjectFile<target_endianness, is64Bits>
825 ::getRelocationSymbol(DataRefImpl Rel,
826 SymbolRef &Result) const {
827 uint32_t symbolIdx;
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 symbolIdx = getRel(Rel)->getSymbol();
834 break;
835 }
836 case ELF::SHT_RELA : {
837 symbolIdx = getRela(Rel)->getSymbol();
838 break;
839 }
840 }
841 DataRefImpl SymbolData;
842 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
843 if (it == SymbolTableSectionsIndexMap.end())
844 report_fatal_error("Relocation symbol table not found!");
845 SymbolData.d.a = symbolIdx;
846 SymbolData.d.b = it->second;
847 Result = SymbolRef(SymbolData, this);
848 return object_error::success;
849}
850
851template<support::endianness target_endianness, bool is64Bits>
852error_code ELFObjectFile<target_endianness, is64Bits>
853 ::getRelocationAddress(DataRefImpl Rel,
854 uint64_t &Result) const {
855 uint64_t offset;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000856 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000857 switch (sec->sh_type) {
858 default :
859 report_fatal_error("Invalid section type in Rel!");
860 case ELF::SHT_REL : {
861 offset = getRel(Rel)->r_offset;
862 break;
863 }
864 case ELF::SHT_RELA : {
865 offset = getRela(Rel)->r_offset;
866 break;
867 }
868 }
869
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000870 Result = offset;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000871 return object_error::success;
872}
873
874template<support::endianness target_endianness, bool is64Bits>
875error_code ELFObjectFile<target_endianness, is64Bits>
876 ::getRelocationType(DataRefImpl Rel,
877 uint32_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000878 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000879 switch (sec->sh_type) {
880 default :
881 report_fatal_error("Invalid section type in Rel!");
882 case ELF::SHT_REL : {
883 Result = getRel(Rel)->getType();
884 break;
885 }
886 case ELF::SHT_RELA : {
887 Result = getRela(Rel)->getType();
888 break;
889 }
890 }
891 return object_error::success;
892}
893
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000894#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
895 case ELF::enum: res = #enum; break;
896
897template<support::endianness target_endianness, bool is64Bits>
898error_code ELFObjectFile<target_endianness, is64Bits>
899 ::getRelocationTypeName(DataRefImpl Rel,
900 SmallVectorImpl<char> &Result) const {
901 const Elf_Shdr *sec = getSection(Rel.w.b);
902 uint8_t type;
903 StringRef res;
904 switch (sec->sh_type) {
905 default :
906 return object_error::parse_failed;
907 case ELF::SHT_REL : {
908 type = getRel(Rel)->getType();
909 break;
910 }
911 case ELF::SHT_RELA : {
912 type = getRela(Rel)->getType();
913 break;
914 }
915 }
916 switch (Header->e_machine) {
917 case ELF::EM_X86_64:
918 switch (type) {
919 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
920 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
923 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
924 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
925 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
926 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
927 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
928 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
929 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
930 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
951 default:
952 res = "Unknown";
953 }
954 break;
955 case ELF::EM_386:
956 switch (type) {
957 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
958 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
959 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
960 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
961 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
969 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
970 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
971 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
972 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
973 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
974 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
975 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
976 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
977 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
978 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
979 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
980 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
981 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
982 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
983 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
984 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
985 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
986 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
987 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
988 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
989 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
990 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
991 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
992 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
993 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
994 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
995 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
996 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
997 default:
998 res = "Unknown";
999 }
1000 break;
1001 default:
1002 res = "Unknown";
1003 }
1004 Result.append(res.begin(), res.end());
1005 return object_error::success;
1006}
1007
1008#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1009
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001010template<support::endianness target_endianness, bool is64Bits>
1011error_code ELFObjectFile<target_endianness, is64Bits>
1012 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1013 int64_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001014 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001015 switch (sec->sh_type) {
1016 default :
1017 report_fatal_error("Invalid section type in Rel!");
1018 case ELF::SHT_REL : {
1019 Result = 0;
1020 return object_error::success;
1021 }
1022 case ELF::SHT_RELA : {
1023 Result = getRela(Rel)->r_addend;
1024 return object_error::success;
1025 }
1026 }
1027}
1028
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001029template<support::endianness target_endianness, bool is64Bits>
1030error_code ELFObjectFile<target_endianness, is64Bits>
1031 ::getRelocationValueString(DataRefImpl Rel,
1032 SmallVectorImpl<char> &Result) const {
1033 const Elf_Shdr *sec = getSection(Rel.w.b);
1034 uint8_t type;
1035 StringRef res;
1036 int64_t addend = 0;
1037 uint16_t symbol_index = 0;
1038 switch (sec->sh_type) {
1039 default :
1040 return object_error::parse_failed;
1041 case ELF::SHT_REL : {
1042 type = getRel(Rel)->getType();
1043 symbol_index = getRel(Rel)->getSymbol();
1044 // TODO: Read implicit addend from section data.
1045 break;
1046 }
1047 case ELF::SHT_RELA : {
1048 type = getRela(Rel)->getType();
1049 symbol_index = getRela(Rel)->getSymbol();
1050 addend = getRela(Rel)->r_addend;
1051 break;
1052 }
1053 }
1054 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1055 StringRef symname;
1056 if (error_code ec = getSymbolName(symb, symname))
1057 return ec;
1058 switch (Header->e_machine) {
1059 case ELF::EM_X86_64:
1060 switch (type) {
1061 case ELF::R_X86_64_32S:
1062 res = symname;
1063 break;
1064 case ELF::R_X86_64_PC32: {
1065 std::string fmtbuf;
1066 raw_string_ostream fmt(fmtbuf);
1067 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1068 fmt.flush();
1069 Result.append(fmtbuf.begin(), fmtbuf.end());
1070 }
1071 break;
1072 default:
1073 res = "Unknown";
1074 }
1075 break;
1076 default:
1077 res = "Unknown";
1078 }
1079 if (Result.empty())
1080 Result.append(res.begin(), res.end());
1081 return object_error::success;
1082}
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001083
Benjamin Kramer07ea23a2011-07-15 18:39:21 +00001084template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +00001085ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1086 , error_code &ec)
1087 : ObjectFile(Binary::isELF, Object, ec)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001088 , SectionHeaderTable(0)
1089 , dot_shstrtab_sec(0)
1090 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001091 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001092
1093 if (Header->e_shoff == 0)
1094 return;
1095
1096 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +00001097 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001098 uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001099 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize
Michael J. Spencer001c9202011-06-25 17:54:50 +00001100 <= base() + Data->getBufferSize()))
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001101 // FIXME: Proper error handling.
1102 report_fatal_error("Section table goes past end of file!");
1103
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001104
Nick Lewycky15c3f722011-10-11 02:57:48 +00001105 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1106 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001107 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001108 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
Nick Lewycky15c3f722011-10-11 02:57:48 +00001109 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
1110 if (SymbolTableSectionHeaderIndex)
1111 // FIXME: Proper error handling.
1112 report_fatal_error("More than one .symtab_shndx!");
1113 SymbolTableSectionHeaderIndex = sh;
1114 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001115 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001116 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001117 SymbolTableSections.push_back(sh);
1118 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001119 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001120 SectionRelocMap[getSection(sh->sh_link)].push_back(i);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001121 }
1122 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001123 }
1124
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001125 // Sort section relocation lists by index.
1126 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1127 e = SectionRelocMap.end(); i != e; ++i) {
1128 std::sort(i->second.begin(), i->second.end());
1129 }
1130
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001131 // Get string table sections.
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001132 dot_shstrtab_sec = getSection(getStringTableIndex());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001133 if (dot_shstrtab_sec) {
1134 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001135 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001136 [dot_shstrtab_sec->sh_size - 1] != 0)
1137 // FIXME: Proper error handling.
1138 report_fatal_error("String table must end with a null terminator!");
1139 }
1140
1141 // Merge this into the above loop.
1142 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001143 *e = i + getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001144 i != e; i += Header->e_shentsize) {
1145 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1146 if (sh->sh_type == ELF::SHT_STRTAB) {
1147 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1148 if (SectionName == ".strtab") {
1149 if (dot_strtab_sec != 0)
1150 // FIXME: Proper error handling.
1151 report_fatal_error("Already found section named .strtab!");
1152 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +00001153 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001154 if (dot_strtab[sh->sh_size - 1] != 0)
1155 // FIXME: Proper error handling.
1156 report_fatal_error("String table must end with a null terminator!");
1157 }
1158 }
1159 }
Nick Lewycky15c3f722011-10-11 02:57:48 +00001160
1161 // Build symbol name side-mapping if there is one.
1162 if (SymbolTableSectionHeaderIndex) {
1163 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1164 SymbolTableSectionHeaderIndex->sh_offset);
1165 error_code ec;
1166 for (symbol_iterator si = begin_symbols(),
1167 se = end_symbols(); si != se; si.increment(ec)) {
1168 if (ec)
1169 report_fatal_error("Fewer extended symbol table entries than symbols!");
1170 if (*ShndxTable != ELF::SHN_UNDEF)
1171 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1172 ++ShndxTable;
1173 }
1174 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001175}
1176
1177template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001178symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1179 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001180 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001181 memset(&SymbolData, 0, sizeof(SymbolData));
1182 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001183 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1184 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001185 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001186 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1187 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001188 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001189 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001190}
1191
1192template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001193symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1194 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001195 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001196 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001197 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1198 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1199 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001200}
1201
1202template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001203section_iterator ELFObjectFile<target_endianness, is64Bits>
1204 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001205 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001206 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001207 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001208 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001209}
1210
1211template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001212section_iterator ELFObjectFile<target_endianness, is64Bits>
1213 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001214 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001215 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001216 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001217 + Header->e_shoff
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001218 + (Header->e_shentsize*getNumSections()));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001219 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001220}
1221
1222template<support::endianness target_endianness, bool is64Bits>
1223uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001224 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001225}
1226
1227template<support::endianness target_endianness, bool is64Bits>
1228StringRef ELFObjectFile<target_endianness, is64Bits>
1229 ::getFileFormatName() const {
1230 switch(Header->e_ident[ELF::EI_CLASS]) {
1231 case ELF::ELFCLASS32:
1232 switch(Header->e_machine) {
1233 case ELF::EM_386:
1234 return "ELF32-i386";
1235 case ELF::EM_X86_64:
1236 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001237 case ELF::EM_ARM:
1238 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001239 default:
1240 return "ELF32-unknown";
1241 }
1242 case ELF::ELFCLASS64:
1243 switch(Header->e_machine) {
1244 case ELF::EM_386:
1245 return "ELF64-i386";
1246 case ELF::EM_X86_64:
1247 return "ELF64-x86-64";
1248 default:
1249 return "ELF64-unknown";
1250 }
1251 default:
1252 // FIXME: Proper error handling.
1253 report_fatal_error("Invalid ELFCLASS!");
1254 }
1255}
1256
1257template<support::endianness target_endianness, bool is64Bits>
1258unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1259 switch(Header->e_machine) {
1260 case ELF::EM_386:
1261 return Triple::x86;
1262 case ELF::EM_X86_64:
1263 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001264 case ELF::EM_ARM:
1265 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001266 default:
1267 return Triple::UnknownArch;
1268 }
1269}
1270
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001271template<support::endianness target_endianness, bool is64Bits>
1272uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
1273 if (Header->e_shnum == ELF::SHN_UNDEF)
1274 return SectionHeaderTable->sh_size;
1275 return Header->e_shnum;
1276}
1277
1278template<support::endianness target_endianness, bool is64Bits>
1279uint64_t
1280ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1281 if (Header->e_shnum == ELF::SHN_UNDEF) {
1282 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1283 return SectionHeaderTable->sh_link;
1284 if (Header->e_shstrndx >= getNumSections())
1285 return 0;
1286 }
1287 return Header->e_shstrndx;
1288}
1289
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001290
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001291template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001292template<typename T>
1293inline const T *
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001294ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1295 uint32_t Entry) const {
1296 return getEntry<T>(getSection(Section), Entry);
1297}
1298
1299template<support::endianness target_endianness, bool is64Bits>
1300template<typename T>
1301inline const T *
1302ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1303 uint32_t Entry) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001304 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +00001305 base()
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001306 + Section->sh_offset
1307 + (Entry * Section->sh_entsize));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001308}
1309
1310template<support::endianness target_endianness, bool is64Bits>
1311const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1312ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001313 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001314}
1315
1316template<support::endianness target_endianness, bool is64Bits>
1317const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1318ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001319 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001320}
1321
1322template<support::endianness target_endianness, bool is64Bits>
1323const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1324ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001325 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001326}
1327
1328template<support::endianness target_endianness, bool is64Bits>
1329const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1330ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001331 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001332 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001333 // FIXME: Proper error handling.
1334 report_fatal_error("Invalid symbol table section!");
1335 return sec;
1336}
1337
1338template<support::endianness target_endianness, bool is64Bits>
1339const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001340ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1341 if (index == 0)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001342 return 0;
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001343 if (!SectionHeaderTable || index >= getNumSections())
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001344 // FIXME: Proper error handling.
1345 report_fatal_error("Invalid section index!");
1346
1347 return reinterpret_cast<const Elf_Shdr *>(
1348 reinterpret_cast<const char *>(SectionHeaderTable)
1349 + (index * Header->e_shentsize));
1350}
1351
1352template<support::endianness target_endianness, bool is64Bits>
1353const char *ELFObjectFile<target_endianness, is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001354 ::getString(uint32_t section,
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001355 ELF::Elf32_Word offset) const {
1356 return getString(getSection(section), offset);
1357}
1358
1359template<support::endianness target_endianness, bool is64Bits>
1360const char *ELFObjectFile<target_endianness, is64Bits>
1361 ::getString(const Elf_Shdr *section,
1362 ELF::Elf32_Word offset) const {
1363 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1364 if (offset >= section->sh_size)
1365 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001366 report_fatal_error("Symbol name offset outside of string table!");
1367 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001368}
1369
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001370template<support::endianness target_endianness, bool is64Bits>
1371error_code ELFObjectFile<target_endianness, is64Bits>
1372 ::getSymbolName(const Elf_Sym *symb,
1373 StringRef &Result) const {
1374 if (symb->st_name == 0) {
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001375 const Elf_Shdr *section = getSection(symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001376 if (!section)
1377 Result = "";
1378 else
1379 Result = getString(dot_shstrtab_sec, section->sh_name);
1380 return object_error::success;
1381 }
1382
1383 // Use the default symbol table name section.
1384 Result = getString(dot_strtab_sec, symb->st_name);
1385 return object_error::success;
1386}
1387
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001388// EI_CLASS, EI_DATA.
1389static std::pair<unsigned char, unsigned char>
1390getElfArchType(MemoryBuffer *Object) {
1391 if (Object->getBufferSize() < ELF::EI_NIDENT)
1392 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1393 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1394 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1395}
1396
1397namespace llvm {
1398
1399 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
1400 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001401 error_code ec;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001402 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001403 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001404 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001405 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001406 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001407 return new ELFObjectFile<support::little, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001408 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001409 return new ELFObjectFile<support::big, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001410 // FIXME: Proper error handling.
1411 report_fatal_error("Not an ELF object file!");
1412 }
1413
1414} // end namespace llvm