blob: 7a992e2fecf9769161c920305cfcdc8f444500d1 [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 Lewyckyf77dea12011-10-13 03:30:21 +0000299 DenseMap<const Elf_Sym*, ELF::Elf64_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;
Michael J. Spencer1130a792011-10-17 20:19:29 +0000334 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &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 Lewyckyf77dea12011-10-13 03:30:21 +0000378 ELF::Elf64_Word 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. Spencerab6bcf32011-10-17 23:53:37 +0000380
381 static inline bool classof(const Binary *v) {
382 return v->getType() == isELF;
383 }
384 static inline bool classof(const ELFObjectFile *v) { return true; }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000385};
386} // end namespace
387
388template<support::endianness target_endianness, bool is64Bits>
389void ELFObjectFile<target_endianness, is64Bits>
390 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000391 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000392 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000393 // FIXME: We really need to do proper error handling in the case of an invalid
394 // input file. Because we don't use exceptions, I think we'll just pass
395 // an error object around.
396 if (!( symb
397 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000398 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000399 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000400 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000401 + SymbolTableSection->sh_offset
402 + SymbolTableSection->sh_size)))
403 // FIXME: Proper error handling.
404 report_fatal_error("Symb must point to a valid symbol!");
405}
406
407template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000408error_code ELFObjectFile<target_endianness, is64Bits>
409 ::getSymbolNext(DataRefImpl Symb,
410 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000411 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000412 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000413
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000414 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000415 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000416 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000417 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000418 ++Symb.d.b;
419 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000420 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000421 if (Symb.d.b >= SymbolTableSections.size()) {
422 Symb.d.a = std::numeric_limits<uint32_t>::max();
423 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000424 }
425 }
426
Michael J. Spencer25b15772011-06-25 17:55:23 +0000427 Result = SymbolRef(Symb, this);
428 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000429}
430
431template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000432error_code ELFObjectFile<target_endianness, is64Bits>
433 ::getSymbolName(DataRefImpl Symb,
434 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000435 validateSymbol(Symb);
Nick Lewycky15c3f722011-10-11 02:57:48 +0000436 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000437 return getSymbolName(symb, Result);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000438}
439
440template<support::endianness target_endianness, bool is64Bits>
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000441ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits>
Nick Lewycky15c3f722011-10-11 02:57:48 +0000442 ::getSymbolTableIndex(const Elf_Sym *symb) const {
443 if (symb->st_shndx == ELF::SHN_XINDEX)
444 return ExtendedSymbolTable.lookup(symb);
445 return symb->st_shndx;
446}
447
448template<support::endianness target_endianness, bool is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000449const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
450ELFObjectFile<target_endianness, is64Bits>
451 ::getSection(const Elf_Sym *symb) const {
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000452 if (symb->st_shndx == ELF::SHN_XINDEX)
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000453 return getSection(ExtendedSymbolTable.lookup(symb));
454 if (symb->st_shndx >= ELF::SHN_LORESERVE)
455 return 0;
456 return getSection(symb->st_shndx);
457}
458
459template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000460error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000461 ::getSymbolOffset(DataRefImpl Symb,
Nick Lewycky15c3f722011-10-11 02:57:48 +0000462 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000463 validateSymbol(Symb);
464 const Elf_Sym *symb = getSymbol(Symb);
465 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000466 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000467 case ELF::SHN_COMMON:
468 // Undefined symbols have no address yet.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000469 case ELF::SHN_UNDEF:
470 Result = UnknownAddressOrSize;
471 return object_error::success;
472 case ELF::SHN_ABS:
473 Result = symb->st_value;
474 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000475 default: Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000476 }
477
478 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000479 case ELF::STT_SECTION:
480 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
481 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000482 case ELF::STT_FUNC:
483 case ELF::STT_OBJECT:
484 case ELF::STT_NOTYPE:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000485 Result = symb->st_value;
486 return object_error::success;
487 default:
488 Result = UnknownAddressOrSize;
489 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000490 }
491}
492
493template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000494error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000495 ::getSymbolAddress(DataRefImpl Symb,
496 uint64_t &Result) const {
497 validateSymbol(Symb);
498 const Elf_Sym *symb = getSymbol(Symb);
499 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000500 switch (getSymbolTableIndex(symb)) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000501 case ELF::SHN_COMMON: // Fall through.
502 // Undefined symbols have no address yet.
503 case ELF::SHN_UNDEF:
504 Result = UnknownAddressOrSize;
505 return object_error::success;
506 case ELF::SHN_ABS:
507 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value);
508 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000509 default: Section = getSection(symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000510 }
511 const uint8_t* addr = base();
512 if (Section)
513 addr += Section->sh_offset;
514 switch (symb->getType()) {
515 case ELF::STT_SECTION:
516 Result = reinterpret_cast<uintptr_t>(addr);
517 return object_error::success;
518 case ELF::STT_FUNC: // Fall through.
519 case ELF::STT_OBJECT: // Fall through.
520 case ELF::STT_NOTYPE:
521 addr += symb->st_value;
522 Result = reinterpret_cast<uintptr_t>(addr);
523 return object_error::success;
524 default:
525 Result = UnknownAddressOrSize;
526 return object_error::success;
527 }
528}
529
530template<support::endianness target_endianness, bool is64Bits>
531error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000532 ::getSymbolSize(DataRefImpl Symb,
533 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000534 validateSymbol(Symb);
535 const Elf_Sym *symb = getSymbol(Symb);
536 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000537 Result = UnknownAddressOrSize;
538 Result = symb->st_size;
539 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000540}
541
542template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000543error_code ELFObjectFile<target_endianness, is64Bits>
544 ::getSymbolNMTypeChar(DataRefImpl Symb,
545 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000546 validateSymbol(Symb);
547 const Elf_Sym *symb = getSymbol(Symb);
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000548 const Elf_Shdr *Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000549
550 char ret = '?';
551
552 if (Section) {
553 switch (Section->sh_type) {
554 case ELF::SHT_PROGBITS:
555 case ELF::SHT_DYNAMIC:
556 switch (Section->sh_flags) {
557 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
558 ret = 't'; break;
559 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
560 ret = 'd'; break;
561 case ELF::SHF_ALLOC:
562 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
563 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
564 ret = 'r'; break;
565 }
566 break;
567 case ELF::SHT_NOBITS: ret = 'b';
568 }
569 }
570
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000571 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000572 case ELF::SHN_UNDEF:
573 if (ret == '?')
574 ret = 'U';
575 break;
576 case ELF::SHN_ABS: ret = 'a'; break;
577 case ELF::SHN_COMMON: ret = 'c'; break;
578 }
579
580 switch (symb->getBinding()) {
581 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
582 case ELF::STB_WEAK:
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000583 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000584 ret = 'w';
585 else
586 if (symb->getType() == ELF::STT_OBJECT)
587 ret = 'V';
588 else
589 ret = 'W';
590 }
591
Michael J. Spencer25b15772011-06-25 17:55:23 +0000592 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
593 StringRef name;
594 if (error_code ec = getSymbolName(Symb, name))
595 return ec;
596 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000597 .StartsWith(".debug", 'N')
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000598 .StartsWith(".note", 'n')
599 .Default('?');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000600 return object_error::success;
601 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000602
Michael J. Spencer25b15772011-06-25 17:55:23 +0000603 Result = ret;
604 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000605}
606
607template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000608error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000609 ::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000610 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000611 validateSymbol(Symb);
612 const Elf_Sym *symb = getSymbol(Symb);
613
Nick Lewycky15c3f722011-10-11 02:57:48 +0000614 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000615 Result = SymbolRef::ST_External;
616 return object_error::success;
617 }
618
619 switch (symb->getType()) {
620 case ELF::STT_FUNC:
621 Result = SymbolRef::ST_Function;
622 break;
623 case ELF::STT_OBJECT:
624 Result = SymbolRef::ST_Data;
625 break;
626 default:
627 Result = SymbolRef::ST_Other;
628 break;
629 }
630 return object_error::success;
631}
632
633template<support::endianness target_endianness, bool is64Bits>
634error_code ELFObjectFile<target_endianness, is64Bits>
635 ::isSymbolGlobal(DataRefImpl Symb,
636 bool &Result) const {
637 validateSymbol(Symb);
638 const Elf_Sym *symb = getSymbol(Symb);
639
640 Result = symb->getBinding() == ELF::STB_GLOBAL;
641 return object_error::success;
642}
643
644template<support::endianness target_endianness, bool is64Bits>
645error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000646 ::isSymbolInternal(DataRefImpl Symb,
647 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000648 validateSymbol(Symb);
649 const Elf_Sym *symb = getSymbol(Symb);
650
651 if ( symb->getType() == ELF::STT_FILE
652 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000653 Result = true;
654 Result = false;
655 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000656}
657
658template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000659error_code ELFObjectFile<target_endianness, is64Bits>
660 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000661 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000662 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000663 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000664 Result = SectionRef(Sec, this);
665 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000666}
667
668template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000669error_code ELFObjectFile<target_endianness, is64Bits>
670 ::getSectionName(DataRefImpl Sec,
671 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000672 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000673 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
674 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000675}
676
677template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000678error_code ELFObjectFile<target_endianness, is64Bits>
679 ::getSectionAddress(DataRefImpl Sec,
680 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000681 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000682 Result = sec->sh_addr;
683 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000684}
685
686template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000687error_code ELFObjectFile<target_endianness, is64Bits>
688 ::getSectionSize(DataRefImpl Sec,
689 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000690 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000691 Result = sec->sh_size;
692 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000693}
694
695template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000696error_code ELFObjectFile<target_endianness, is64Bits>
697 ::getSectionContents(DataRefImpl Sec,
698 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000699 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000700 const char *start = (const char*)base() + sec->sh_offset;
701 Result = StringRef(start, sec->sh_size);
702 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000703}
704
705template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000706error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000707 ::getSectionAlignment(DataRefImpl Sec,
708 uint64_t &Result) const {
709 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
710 Result = sec->sh_addralign;
711 return object_error::success;
712}
713
714template<support::endianness target_endianness, bool is64Bits>
715error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000716 ::isSectionText(DataRefImpl Sec,
717 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000718 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000719 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000720 Result = true;
721 else
722 Result = false;
723 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000724}
725
726template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000727error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000728 ::isSectionData(DataRefImpl Sec,
729 bool &Result) const {
730 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
731 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
732 && sec->sh_type == ELF::SHT_PROGBITS)
733 Result = true;
734 else
735 Result = false;
736 return object_error::success;
737}
738
739template<support::endianness target_endianness, bool is64Bits>
740error_code ELFObjectFile<target_endianness, is64Bits>
741 ::isSectionBSS(DataRefImpl Sec,
742 bool &Result) const {
743 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
744 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
745 && sec->sh_type == ELF::SHT_NOBITS)
746 Result = true;
747 else
748 Result = false;
749 return object_error::success;
750}
751
752template<support::endianness target_endianness, bool is64Bits>
753error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000754 ::sectionContainsSymbol(DataRefImpl Sec,
755 DataRefImpl Symb,
756 bool &Result) const {
757 // FIXME: Unimplemented.
758 Result = false;
759 return object_error::success;
760}
761
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000762template<support::endianness target_endianness, bool is64Bits>
763relocation_iterator ELFObjectFile<target_endianness, is64Bits>
764 ::getSectionRelBegin(DataRefImpl Sec) const {
765 DataRefImpl RelData;
766 memset(&RelData, 0, sizeof(RelData));
767 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
768 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
769 if (sec != 0 && ittr != SectionRelocMap.end()) {
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +0000770 RelData.w.a = getSection(ittr->second[0])->sh_info;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000771 RelData.w.b = ittr->second[0];
772 RelData.w.c = 0;
773 }
774 return relocation_iterator(RelocationRef(RelData, this));
775}
776
777template<support::endianness target_endianness, bool is64Bits>
778relocation_iterator ELFObjectFile<target_endianness, is64Bits>
779 ::getSectionRelEnd(DataRefImpl Sec) const {
780 DataRefImpl RelData;
781 memset(&RelData, 0, sizeof(RelData));
782 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
783 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
784 if (sec != 0 && ittr != SectionRelocMap.end()) {
785 // Get the index of the last relocation section for this section.
786 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
787 const Elf_Shdr *relocsec = getSection(relocsecindex);
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +0000788 RelData.w.a = relocsec->sh_info;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000789 RelData.w.b = relocsecindex;
790 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
791 }
792 return relocation_iterator(RelocationRef(RelData, this));
793}
794
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000795// Relocations
796template<support::endianness target_endianness, bool is64Bits>
797error_code ELFObjectFile<target_endianness, is64Bits>
798 ::getRelocationNext(DataRefImpl Rel,
799 RelocationRef &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000800 ++Rel.w.c;
801 const Elf_Shdr *relocsec = getSection(Rel.w.b);
802 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
803 // We have reached the end of the relocations for this section. See if there
804 // is another relocation section.
Michael J. Spencer01a4db32011-10-07 19:46:12 +0000805 typename RelocMap_t::mapped_type relocseclist =
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000806 SectionRelocMap.lookup(getSection(Rel.w.a));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000807
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000808 // Do a binary search for the current reloc section index (which must be
809 // present). Then get the next one.
810 typename RelocMap_t::mapped_type::const_iterator loc =
811 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
812 ++loc;
813
814 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
815 // to the end iterator.
816 if (loc != relocseclist.end()) {
817 Rel.w.b = *loc;
818 Rel.w.a = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000819 }
820 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000821 Result = RelocationRef(Rel, this);
822 return object_error::success;
823}
824
825template<support::endianness target_endianness, bool is64Bits>
826error_code ELFObjectFile<target_endianness, is64Bits>
827 ::getRelocationSymbol(DataRefImpl Rel,
828 SymbolRef &Result) const {
829 uint32_t symbolIdx;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000830 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000831 switch (sec->sh_type) {
832 default :
833 report_fatal_error("Invalid section type in Rel!");
834 case ELF::SHT_REL : {
835 symbolIdx = getRel(Rel)->getSymbol();
836 break;
837 }
838 case ELF::SHT_RELA : {
839 symbolIdx = getRela(Rel)->getSymbol();
840 break;
841 }
842 }
843 DataRefImpl SymbolData;
844 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
845 if (it == SymbolTableSectionsIndexMap.end())
846 report_fatal_error("Relocation symbol table not found!");
847 SymbolData.d.a = symbolIdx;
848 SymbolData.d.b = it->second;
849 Result = SymbolRef(SymbolData, this);
850 return object_error::success;
851}
852
853template<support::endianness target_endianness, bool is64Bits>
854error_code ELFObjectFile<target_endianness, is64Bits>
855 ::getRelocationAddress(DataRefImpl Rel,
856 uint64_t &Result) const {
857 uint64_t offset;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000858 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000859 switch (sec->sh_type) {
860 default :
861 report_fatal_error("Invalid section type in Rel!");
862 case ELF::SHT_REL : {
863 offset = getRel(Rel)->r_offset;
864 break;
865 }
866 case ELF::SHT_RELA : {
867 offset = getRela(Rel)->r_offset;
868 break;
869 }
870 }
871
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000872 Result = offset;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000873 return object_error::success;
874}
875
876template<support::endianness target_endianness, bool is64Bits>
877error_code ELFObjectFile<target_endianness, is64Bits>
878 ::getRelocationType(DataRefImpl Rel,
879 uint32_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000880 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000881 switch (sec->sh_type) {
882 default :
883 report_fatal_error("Invalid section type in Rel!");
884 case ELF::SHT_REL : {
885 Result = getRel(Rel)->getType();
886 break;
887 }
888 case ELF::SHT_RELA : {
889 Result = getRela(Rel)->getType();
890 break;
891 }
892 }
893 return object_error::success;
894}
895
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000896#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
897 case ELF::enum: res = #enum; break;
898
899template<support::endianness target_endianness, bool is64Bits>
900error_code ELFObjectFile<target_endianness, is64Bits>
901 ::getRelocationTypeName(DataRefImpl Rel,
902 SmallVectorImpl<char> &Result) const {
903 const Elf_Shdr *sec = getSection(Rel.w.b);
904 uint8_t type;
905 StringRef res;
906 switch (sec->sh_type) {
907 default :
908 return object_error::parse_failed;
909 case ELF::SHT_REL : {
910 type = getRel(Rel)->getType();
911 break;
912 }
913 case ELF::SHT_RELA : {
914 type = getRela(Rel)->getType();
915 break;
916 }
917 }
918 switch (Header->e_machine) {
919 case ELF::EM_X86_64:
920 switch (type) {
921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
923 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
924 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
925 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
926 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
927 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
928 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
929 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
930 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
953 default:
954 res = "Unknown";
955 }
956 break;
957 case ELF::EM_386:
958 switch (type) {
959 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
960 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
961 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
969 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
970 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
971 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
972 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
973 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
974 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
975 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
976 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
977 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
978 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
979 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
980 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
981 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
982 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
983 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
984 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
985 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
986 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
987 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
988 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
989 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
990 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
991 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
992 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
993 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
994 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
995 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
996 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
997 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
998 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
999 default:
1000 res = "Unknown";
1001 }
1002 break;
1003 default:
1004 res = "Unknown";
1005 }
1006 Result.append(res.begin(), res.end());
1007 return object_error::success;
1008}
1009
1010#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1011
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001012template<support::endianness target_endianness, bool is64Bits>
1013error_code ELFObjectFile<target_endianness, is64Bits>
1014 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1015 int64_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001016 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001017 switch (sec->sh_type) {
1018 default :
1019 report_fatal_error("Invalid section type in Rel!");
1020 case ELF::SHT_REL : {
1021 Result = 0;
1022 return object_error::success;
1023 }
1024 case ELF::SHT_RELA : {
1025 Result = getRela(Rel)->r_addend;
1026 return object_error::success;
1027 }
1028 }
1029}
1030
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001031template<support::endianness target_endianness, bool is64Bits>
1032error_code ELFObjectFile<target_endianness, is64Bits>
1033 ::getRelocationValueString(DataRefImpl Rel,
1034 SmallVectorImpl<char> &Result) const {
1035 const Elf_Shdr *sec = getSection(Rel.w.b);
1036 uint8_t type;
1037 StringRef res;
1038 int64_t addend = 0;
1039 uint16_t symbol_index = 0;
1040 switch (sec->sh_type) {
1041 default :
1042 return object_error::parse_failed;
1043 case ELF::SHT_REL : {
1044 type = getRel(Rel)->getType();
1045 symbol_index = getRel(Rel)->getSymbol();
1046 // TODO: Read implicit addend from section data.
1047 break;
1048 }
1049 case ELF::SHT_RELA : {
1050 type = getRela(Rel)->getType();
1051 symbol_index = getRela(Rel)->getSymbol();
1052 addend = getRela(Rel)->r_addend;
1053 break;
1054 }
1055 }
1056 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1057 StringRef symname;
1058 if (error_code ec = getSymbolName(symb, symname))
1059 return ec;
1060 switch (Header->e_machine) {
1061 case ELF::EM_X86_64:
1062 switch (type) {
1063 case ELF::R_X86_64_32S:
1064 res = symname;
1065 break;
1066 case ELF::R_X86_64_PC32: {
1067 std::string fmtbuf;
1068 raw_string_ostream fmt(fmtbuf);
1069 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1070 fmt.flush();
1071 Result.append(fmtbuf.begin(), fmtbuf.end());
1072 }
1073 break;
1074 default:
1075 res = "Unknown";
1076 }
1077 break;
1078 default:
1079 res = "Unknown";
1080 }
1081 if (Result.empty())
1082 Result.append(res.begin(), res.end());
1083 return object_error::success;
1084}
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001085
Benjamin Kramer07ea23a2011-07-15 18:39:21 +00001086template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +00001087ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1088 , error_code &ec)
1089 : ObjectFile(Binary::isELF, Object, ec)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001090 , SectionHeaderTable(0)
1091 , dot_shstrtab_sec(0)
1092 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001093 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001094
1095 if (Header->e_shoff == 0)
1096 return;
1097
1098 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +00001099 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001100 uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001101 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize
Michael J. Spencer001c9202011-06-25 17:54:50 +00001102 <= base() + Data->getBufferSize()))
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001103 // FIXME: Proper error handling.
1104 report_fatal_error("Section table goes past end of file!");
1105
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001106
Nick Lewycky15c3f722011-10-11 02:57:48 +00001107 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1108 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001109 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001110 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
Nick Lewycky15c3f722011-10-11 02:57:48 +00001111 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
1112 if (SymbolTableSectionHeaderIndex)
1113 // FIXME: Proper error handling.
1114 report_fatal_error("More than one .symtab_shndx!");
1115 SymbolTableSectionHeaderIndex = sh;
1116 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001117 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001118 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001119 SymbolTableSections.push_back(sh);
1120 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001121 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +00001122 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001123 }
1124 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001125 }
1126
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001127 // Sort section relocation lists by index.
1128 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1129 e = SectionRelocMap.end(); i != e; ++i) {
1130 std::sort(i->second.begin(), i->second.end());
1131 }
1132
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001133 // Get string table sections.
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001134 dot_shstrtab_sec = getSection(getStringTableIndex());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001135 if (dot_shstrtab_sec) {
1136 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001137 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001138 [dot_shstrtab_sec->sh_size - 1] != 0)
1139 // FIXME: Proper error handling.
1140 report_fatal_error("String table must end with a null terminator!");
1141 }
1142
1143 // Merge this into the above loop.
1144 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001145 *e = i + getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001146 i != e; i += Header->e_shentsize) {
1147 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1148 if (sh->sh_type == ELF::SHT_STRTAB) {
1149 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1150 if (SectionName == ".strtab") {
1151 if (dot_strtab_sec != 0)
1152 // FIXME: Proper error handling.
1153 report_fatal_error("Already found section named .strtab!");
1154 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +00001155 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001156 if (dot_strtab[sh->sh_size - 1] != 0)
1157 // FIXME: Proper error handling.
1158 report_fatal_error("String table must end with a null terminator!");
1159 }
1160 }
1161 }
Nick Lewycky15c3f722011-10-11 02:57:48 +00001162
1163 // Build symbol name side-mapping if there is one.
1164 if (SymbolTableSectionHeaderIndex) {
1165 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1166 SymbolTableSectionHeaderIndex->sh_offset);
1167 error_code ec;
1168 for (symbol_iterator si = begin_symbols(),
1169 se = end_symbols(); si != se; si.increment(ec)) {
1170 if (ec)
1171 report_fatal_error("Fewer extended symbol table entries than symbols!");
1172 if (*ShndxTable != ELF::SHN_UNDEF)
1173 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1174 ++ShndxTable;
1175 }
1176 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001177}
1178
1179template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001180symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1181 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001182 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001183 memset(&SymbolData, 0, sizeof(SymbolData));
1184 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001185 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1186 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001187 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001188 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1189 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001190 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001191 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001192}
1193
1194template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001195symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1196 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001197 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001198 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001199 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1200 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1201 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001202}
1203
1204template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001205section_iterator ELFObjectFile<target_endianness, is64Bits>
1206 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001207 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001208 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001209 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001210 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001211}
1212
1213template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001214section_iterator ELFObjectFile<target_endianness, is64Bits>
1215 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001216 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001217 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001218 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001219 + Header->e_shoff
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001220 + (Header->e_shentsize*getNumSections()));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001221 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001222}
1223
1224template<support::endianness target_endianness, bool is64Bits>
1225uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001226 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001227}
1228
1229template<support::endianness target_endianness, bool is64Bits>
1230StringRef ELFObjectFile<target_endianness, is64Bits>
1231 ::getFileFormatName() const {
1232 switch(Header->e_ident[ELF::EI_CLASS]) {
1233 case ELF::ELFCLASS32:
1234 switch(Header->e_machine) {
1235 case ELF::EM_386:
1236 return "ELF32-i386";
1237 case ELF::EM_X86_64:
1238 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001239 case ELF::EM_ARM:
1240 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001241 default:
1242 return "ELF32-unknown";
1243 }
1244 case ELF::ELFCLASS64:
1245 switch(Header->e_machine) {
1246 case ELF::EM_386:
1247 return "ELF64-i386";
1248 case ELF::EM_X86_64:
1249 return "ELF64-x86-64";
1250 default:
1251 return "ELF64-unknown";
1252 }
1253 default:
1254 // FIXME: Proper error handling.
1255 report_fatal_error("Invalid ELFCLASS!");
1256 }
1257}
1258
1259template<support::endianness target_endianness, bool is64Bits>
1260unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1261 switch(Header->e_machine) {
1262 case ELF::EM_386:
1263 return Triple::x86;
1264 case ELF::EM_X86_64:
1265 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001266 case ELF::EM_ARM:
1267 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001268 default:
1269 return Triple::UnknownArch;
1270 }
1271}
1272
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001273template<support::endianness target_endianness, bool is64Bits>
1274uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
1275 if (Header->e_shnum == ELF::SHN_UNDEF)
1276 return SectionHeaderTable->sh_size;
1277 return Header->e_shnum;
1278}
1279
1280template<support::endianness target_endianness, bool is64Bits>
1281uint64_t
1282ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1283 if (Header->e_shnum == ELF::SHN_UNDEF) {
1284 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1285 return SectionHeaderTable->sh_link;
1286 if (Header->e_shstrndx >= getNumSections())
1287 return 0;
1288 }
1289 return Header->e_shstrndx;
1290}
1291
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001292
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001293template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001294template<typename T>
1295inline const T *
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001296ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1297 uint32_t Entry) const {
1298 return getEntry<T>(getSection(Section), Entry);
1299}
1300
1301template<support::endianness target_endianness, bool is64Bits>
1302template<typename T>
1303inline const T *
1304ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1305 uint32_t Entry) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001306 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +00001307 base()
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001308 + Section->sh_offset
1309 + (Entry * Section->sh_entsize));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001310}
1311
1312template<support::endianness target_endianness, bool is64Bits>
1313const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1314ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001315 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001316}
1317
1318template<support::endianness target_endianness, bool is64Bits>
1319const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1320ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001321 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001322}
1323
1324template<support::endianness target_endianness, bool is64Bits>
1325const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1326ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001327 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001328}
1329
1330template<support::endianness target_endianness, bool is64Bits>
1331const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1332ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001333 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001334 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001335 // FIXME: Proper error handling.
1336 report_fatal_error("Invalid symbol table section!");
1337 return sec;
1338}
1339
1340template<support::endianness target_endianness, bool is64Bits>
1341const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001342ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1343 if (index == 0)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001344 return 0;
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001345 if (!SectionHeaderTable || index >= getNumSections())
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001346 // FIXME: Proper error handling.
1347 report_fatal_error("Invalid section index!");
1348
1349 return reinterpret_cast<const Elf_Shdr *>(
1350 reinterpret_cast<const char *>(SectionHeaderTable)
1351 + (index * Header->e_shentsize));
1352}
1353
1354template<support::endianness target_endianness, bool is64Bits>
1355const char *ELFObjectFile<target_endianness, is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001356 ::getString(uint32_t section,
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001357 ELF::Elf32_Word offset) const {
1358 return getString(getSection(section), offset);
1359}
1360
1361template<support::endianness target_endianness, bool is64Bits>
1362const char *ELFObjectFile<target_endianness, is64Bits>
1363 ::getString(const Elf_Shdr *section,
1364 ELF::Elf32_Word offset) const {
1365 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1366 if (offset >= section->sh_size)
1367 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001368 report_fatal_error("Symbol name offset outside of string table!");
1369 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001370}
1371
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001372template<support::endianness target_endianness, bool is64Bits>
1373error_code ELFObjectFile<target_endianness, is64Bits>
1374 ::getSymbolName(const Elf_Sym *symb,
1375 StringRef &Result) const {
1376 if (symb->st_name == 0) {
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001377 const Elf_Shdr *section = getSection(symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001378 if (!section)
1379 Result = "";
1380 else
1381 Result = getString(dot_shstrtab_sec, section->sh_name);
1382 return object_error::success;
1383 }
1384
1385 // Use the default symbol table name section.
1386 Result = getString(dot_strtab_sec, symb->st_name);
1387 return object_error::success;
1388}
1389
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001390// EI_CLASS, EI_DATA.
1391static std::pair<unsigned char, unsigned char>
1392getElfArchType(MemoryBuffer *Object) {
1393 if (Object->getBufferSize() < ELF::EI_NIDENT)
1394 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1395 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1396 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1397}
1398
1399namespace llvm {
1400
1401 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
1402 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001403 error_code ec;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001404 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001405 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001406 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001407 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001408 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001409 return new ELFObjectFile<support::little, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001410 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001411 return new ELFObjectFile<support::big, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001412 // FIXME: Proper error handling.
1413 report_fatal_error("Not an ELF object file!");
1414 }
1415
1416} // end namespace llvm