blob: a5e3910d16c80da78117132963b4c4cbd972bb8b [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. Spencerc38c36a2011-10-17 23:54:22 +0000334 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const;
Michael J. Spencer1130a792011-10-17 20:19:29 +0000335 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000336 virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const;
337 virtual error_code getSymbolSection(DataRefImpl Symb,
338 section_iterator &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000339
Michael J. Spencer25b15772011-06-25 17:55:23 +0000340 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
341 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
342 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
343 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
344 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000345 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000346 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000347 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
348 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000349 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
350 bool &Result) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000351 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
352 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000353
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000354 virtual error_code getRelocationNext(DataRefImpl Rel,
355 RelocationRef &Res) const;
356 virtual error_code getRelocationAddress(DataRefImpl Rel,
357 uint64_t &Res) const;
358 virtual error_code getRelocationSymbol(DataRefImpl Rel,
359 SymbolRef &Res) const;
360 virtual error_code getRelocationType(DataRefImpl Rel,
361 uint32_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000362 virtual error_code getRelocationTypeName(DataRefImpl Rel,
363 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000364 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
365 int64_t &Res) const;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000366 virtual error_code getRelocationValueString(DataRefImpl Rel,
367 SmallVectorImpl<char> &Result) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000368
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000369public:
Michael J. Spencer001c9202011-06-25 17:54:50 +0000370 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000371 virtual symbol_iterator begin_symbols() const;
372 virtual symbol_iterator end_symbols() const;
373 virtual section_iterator begin_sections() const;
374 virtual section_iterator end_sections() const;
375
376 virtual uint8_t getBytesInAddress() const;
377 virtual StringRef getFileFormatName() const;
378 virtual unsigned getArch() const;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000379
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000380 uint64_t getNumSections() const;
381 uint64_t getStringTableIndex() const;
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000382 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000383 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
Michael J. Spencerab6bcf32011-10-17 23:53:37 +0000384
385 static inline bool classof(const Binary *v) {
386 return v->getType() == isELF;
387 }
388 static inline bool classof(const ELFObjectFile *v) { return true; }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000389};
390} // end namespace
391
392template<support::endianness target_endianness, bool is64Bits>
393void ELFObjectFile<target_endianness, is64Bits>
394 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000395 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000396 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000397 // FIXME: We really need to do proper error handling in the case of an invalid
398 // input file. Because we don't use exceptions, I think we'll just pass
399 // an error object around.
400 if (!( symb
401 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000402 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000403 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000404 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000405 + SymbolTableSection->sh_offset
406 + SymbolTableSection->sh_size)))
407 // FIXME: Proper error handling.
408 report_fatal_error("Symb must point to a valid symbol!");
409}
410
411template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000412error_code ELFObjectFile<target_endianness, is64Bits>
413 ::getSymbolNext(DataRefImpl Symb,
414 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000415 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000416 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000417
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000418 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000419 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000420 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000421 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000422 ++Symb.d.b;
423 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000424 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000425 if (Symb.d.b >= SymbolTableSections.size()) {
426 Symb.d.a = std::numeric_limits<uint32_t>::max();
427 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000428 }
429 }
430
Michael J. Spencer25b15772011-06-25 17:55:23 +0000431 Result = SymbolRef(Symb, this);
432 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000433}
434
435template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000436error_code ELFObjectFile<target_endianness, is64Bits>
437 ::getSymbolName(DataRefImpl Symb,
438 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000439 validateSymbol(Symb);
Nick Lewycky15c3f722011-10-11 02:57:48 +0000440 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000441 return getSymbolName(symb, Result);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000442}
443
444template<support::endianness target_endianness, bool is64Bits>
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000445ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits>
Nick Lewycky15c3f722011-10-11 02:57:48 +0000446 ::getSymbolTableIndex(const Elf_Sym *symb) const {
447 if (symb->st_shndx == ELF::SHN_XINDEX)
448 return ExtendedSymbolTable.lookup(symb);
449 return symb->st_shndx;
450}
451
452template<support::endianness target_endianness, bool is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000453const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
454ELFObjectFile<target_endianness, is64Bits>
455 ::getSection(const Elf_Sym *symb) const {
Nick Lewyckyf77dea12011-10-13 03:30:21 +0000456 if (symb->st_shndx == ELF::SHN_XINDEX)
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000457 return getSection(ExtendedSymbolTable.lookup(symb));
458 if (symb->st_shndx >= ELF::SHN_LORESERVE)
459 return 0;
460 return getSection(symb->st_shndx);
461}
462
463template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000464error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000465 ::getSymbolOffset(DataRefImpl Symb,
Nick Lewycky15c3f722011-10-11 02:57:48 +0000466 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000467 validateSymbol(Symb);
468 const Elf_Sym *symb = getSymbol(Symb);
469 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000470 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000471 case ELF::SHN_COMMON:
472 // Undefined symbols have no address yet.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000473 case ELF::SHN_UNDEF:
474 Result = UnknownAddressOrSize;
475 return object_error::success;
476 case ELF::SHN_ABS:
477 Result = symb->st_value;
478 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000479 default: Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000480 }
481
482 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000483 case ELF::STT_SECTION:
484 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
485 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000486 case ELF::STT_FUNC:
487 case ELF::STT_OBJECT:
488 case ELF::STT_NOTYPE:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000489 Result = symb->st_value;
490 return object_error::success;
491 default:
492 Result = UnknownAddressOrSize;
493 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000494 }
495}
496
497template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000498error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000499 ::getSymbolAddress(DataRefImpl Symb,
500 uint64_t &Result) const {
501 validateSymbol(Symb);
502 const Elf_Sym *symb = getSymbol(Symb);
503 const Elf_Shdr *Section;
Nick Lewycky15c3f722011-10-11 02:57:48 +0000504 switch (getSymbolTableIndex(symb)) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000505 case ELF::SHN_COMMON: // Fall through.
506 // Undefined symbols have no address yet.
507 case ELF::SHN_UNDEF:
508 Result = UnknownAddressOrSize;
509 return object_error::success;
510 case ELF::SHN_ABS:
511 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value);
512 return object_error::success;
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000513 default: Section = getSection(symb);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000514 }
515 const uint8_t* addr = base();
516 if (Section)
517 addr += Section->sh_offset;
518 switch (symb->getType()) {
519 case ELF::STT_SECTION:
520 Result = reinterpret_cast<uintptr_t>(addr);
521 return object_error::success;
522 case ELF::STT_FUNC: // Fall through.
523 case ELF::STT_OBJECT: // Fall through.
524 case ELF::STT_NOTYPE:
525 addr += symb->st_value;
526 Result = reinterpret_cast<uintptr_t>(addr);
527 return object_error::success;
528 default:
529 Result = UnknownAddressOrSize;
530 return object_error::success;
531 }
532}
533
534template<support::endianness target_endianness, bool is64Bits>
535error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000536 ::getSymbolSize(DataRefImpl Symb,
537 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000538 validateSymbol(Symb);
539 const Elf_Sym *symb = getSymbol(Symb);
540 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000541 Result = UnknownAddressOrSize;
542 Result = symb->st_size;
543 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000544}
545
546template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000547error_code ELFObjectFile<target_endianness, is64Bits>
548 ::getSymbolNMTypeChar(DataRefImpl Symb,
549 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000550 validateSymbol(Symb);
551 const Elf_Sym *symb = getSymbol(Symb);
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000552 const Elf_Shdr *Section = getSection(symb);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000553
554 char ret = '?';
555
556 if (Section) {
557 switch (Section->sh_type) {
558 case ELF::SHT_PROGBITS:
559 case ELF::SHT_DYNAMIC:
560 switch (Section->sh_flags) {
561 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
562 ret = 't'; break;
563 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
564 ret = 'd'; break;
565 case ELF::SHF_ALLOC:
566 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
567 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
568 ret = 'r'; break;
569 }
570 break;
571 case ELF::SHT_NOBITS: ret = 'b';
572 }
573 }
574
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000575 switch (getSymbolTableIndex(symb)) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000576 case ELF::SHN_UNDEF:
577 if (ret == '?')
578 ret = 'U';
579 break;
580 case ELF::SHN_ABS: ret = 'a'; break;
581 case ELF::SHN_COMMON: ret = 'c'; break;
582 }
583
584 switch (symb->getBinding()) {
585 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
586 case ELF::STB_WEAK:
Nick Lewyckybfbbe322011-10-11 03:18:58 +0000587 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000588 ret = 'w';
589 else
590 if (symb->getType() == ELF::STT_OBJECT)
591 ret = 'V';
592 else
593 ret = 'W';
594 }
595
Michael J. Spencer25b15772011-06-25 17:55:23 +0000596 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
597 StringRef name;
598 if (error_code ec = getSymbolName(Symb, name))
599 return ec;
600 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000601 .StartsWith(".debug", 'N')
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000602 .StartsWith(".note", 'n')
603 .Default('?');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000604 return object_error::success;
605 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000606
Michael J. Spencer25b15772011-06-25 17:55:23 +0000607 Result = ret;
608 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000609}
610
611template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000612error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000613 ::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000614 SymbolRef::Type &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000615 validateSymbol(Symb);
616 const Elf_Sym *symb = getSymbol(Symb);
617
Nick Lewycky15c3f722011-10-11 02:57:48 +0000618 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000619 Result = SymbolRef::ST_External;
620 return object_error::success;
621 }
622
623 switch (symb->getType()) {
624 case ELF::STT_FUNC:
625 Result = SymbolRef::ST_Function;
626 break;
627 case ELF::STT_OBJECT:
628 Result = SymbolRef::ST_Data;
629 break;
630 default:
631 Result = SymbolRef::ST_Other;
632 break;
633 }
634 return object_error::success;
635}
636
637template<support::endianness target_endianness, bool is64Bits>
638error_code ELFObjectFile<target_endianness, is64Bits>
639 ::isSymbolGlobal(DataRefImpl Symb,
640 bool &Result) const {
641 validateSymbol(Symb);
642 const Elf_Sym *symb = getSymbol(Symb);
643
644 Result = symb->getBinding() == ELF::STB_GLOBAL;
645 return object_error::success;
646}
647
648template<support::endianness target_endianness, bool is64Bits>
649error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencerc38c36a2011-10-17 23:54:22 +0000650 ::isSymbolWeak(DataRefImpl Symb,
651 bool &Result) const {
652 validateSymbol(Symb);
653 const Elf_Sym *symb = getSymbol(Symb);
654
655 Result = symb->getBinding() == ELF::STB_WEAK;
656 return object_error::success;
657}
658
659template<support::endianness target_endianness, bool is64Bits>
660error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000661 ::isSymbolAbsolute(DataRefImpl Symb, bool &Res) const {
662 validateSymbol(Symb);
663 const Elf_Sym *symb = getSymbol(Symb);
664 Res = symb->st_shndx == ELF::SHN_ABS;
665 return object_error::success;
666}
667
668template<support::endianness target_endianness, bool is64Bits>
669error_code ELFObjectFile<target_endianness, is64Bits>
670 ::getSymbolSection(DataRefImpl Symb,
671 section_iterator &Res) const {
672 validateSymbol(Symb);
673 const Elf_Sym *symb = getSymbol(Symb);
674 const Elf_Shdr *sec = getSection(symb);
675 if (!sec)
676 Res = end_sections();
677 else {
678 DataRefImpl Sec;
679 Sec.p = reinterpret_cast<intptr_t>(sec);
680 Res = section_iterator(SectionRef(Sec, this));
681 }
682 return object_error::success;
683}
684
685template<support::endianness target_endianness, bool is64Bits>
686error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000687 ::isSymbolInternal(DataRefImpl Symb,
688 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000689 validateSymbol(Symb);
690 const Elf_Sym *symb = getSymbol(Symb);
691
692 if ( symb->getType() == ELF::STT_FILE
693 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000694 Result = true;
695 Result = false;
696 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000697}
698
699template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000700error_code ELFObjectFile<target_endianness, is64Bits>
701 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000702 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000703 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000704 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000705 Result = SectionRef(Sec, this);
706 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000707}
708
709template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000710error_code ELFObjectFile<target_endianness, is64Bits>
711 ::getSectionName(DataRefImpl Sec,
712 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000713 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000714 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
715 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000716}
717
718template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000719error_code ELFObjectFile<target_endianness, is64Bits>
720 ::getSectionAddress(DataRefImpl Sec,
721 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000722 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000723 Result = sec->sh_addr;
724 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000725}
726
727template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000728error_code ELFObjectFile<target_endianness, is64Bits>
729 ::getSectionSize(DataRefImpl Sec,
730 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000731 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000732 Result = sec->sh_size;
733 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000734}
735
736template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000737error_code ELFObjectFile<target_endianness, is64Bits>
738 ::getSectionContents(DataRefImpl Sec,
739 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000740 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000741 const char *start = (const char*)base() + sec->sh_offset;
742 Result = StringRef(start, sec->sh_size);
743 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000744}
745
746template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000747error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000748 ::getSectionAlignment(DataRefImpl Sec,
749 uint64_t &Result) const {
750 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
751 Result = sec->sh_addralign;
752 return object_error::success;
753}
754
755template<support::endianness target_endianness, bool is64Bits>
756error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000757 ::isSectionText(DataRefImpl Sec,
758 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000759 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000760 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000761 Result = true;
762 else
763 Result = false;
764 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000765}
766
767template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000768error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000769 ::isSectionData(DataRefImpl Sec,
770 bool &Result) const {
771 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
772 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
773 && sec->sh_type == ELF::SHT_PROGBITS)
774 Result = true;
775 else
776 Result = false;
777 return object_error::success;
778}
779
780template<support::endianness target_endianness, bool is64Bits>
781error_code ELFObjectFile<target_endianness, is64Bits>
782 ::isSectionBSS(DataRefImpl Sec,
783 bool &Result) const {
784 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
785 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
786 && sec->sh_type == ELF::SHT_NOBITS)
787 Result = true;
788 else
789 Result = false;
790 return object_error::success;
791}
792
793template<support::endianness target_endianness, bool is64Bits>
794error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000795 ::sectionContainsSymbol(DataRefImpl Sec,
796 DataRefImpl Symb,
797 bool &Result) const {
798 // FIXME: Unimplemented.
799 Result = false;
800 return object_error::success;
801}
802
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000803template<support::endianness target_endianness, bool is64Bits>
804relocation_iterator ELFObjectFile<target_endianness, is64Bits>
805 ::getSectionRelBegin(DataRefImpl Sec) const {
806 DataRefImpl RelData;
807 memset(&RelData, 0, sizeof(RelData));
808 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
809 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
810 if (sec != 0 && ittr != SectionRelocMap.end()) {
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +0000811 RelData.w.a = getSection(ittr->second[0])->sh_info;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000812 RelData.w.b = ittr->second[0];
813 RelData.w.c = 0;
814 }
815 return relocation_iterator(RelocationRef(RelData, this));
816}
817
818template<support::endianness target_endianness, bool is64Bits>
819relocation_iterator ELFObjectFile<target_endianness, is64Bits>
820 ::getSectionRelEnd(DataRefImpl Sec) const {
821 DataRefImpl RelData;
822 memset(&RelData, 0, sizeof(RelData));
823 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
824 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
825 if (sec != 0 && ittr != SectionRelocMap.end()) {
826 // Get the index of the last relocation section for this section.
827 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
828 const Elf_Shdr *relocsec = getSection(relocsecindex);
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +0000829 RelData.w.a = relocsec->sh_info;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000830 RelData.w.b = relocsecindex;
831 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
832 }
833 return relocation_iterator(RelocationRef(RelData, this));
834}
835
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000836// Relocations
837template<support::endianness target_endianness, bool is64Bits>
838error_code ELFObjectFile<target_endianness, is64Bits>
839 ::getRelocationNext(DataRefImpl Rel,
840 RelocationRef &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000841 ++Rel.w.c;
842 const Elf_Shdr *relocsec = getSection(Rel.w.b);
843 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
844 // We have reached the end of the relocations for this section. See if there
845 // is another relocation section.
Michael J. Spencer01a4db32011-10-07 19:46:12 +0000846 typename RelocMap_t::mapped_type relocseclist =
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000847 SectionRelocMap.lookup(getSection(Rel.w.a));
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000848
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000849 // Do a binary search for the current reloc section index (which must be
850 // present). Then get the next one.
851 typename RelocMap_t::mapped_type::const_iterator loc =
852 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
853 ++loc;
854
855 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
856 // to the end iterator.
857 if (loc != relocseclist.end()) {
858 Rel.w.b = *loc;
859 Rel.w.a = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000860 }
861 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000862 Result = RelocationRef(Rel, this);
863 return object_error::success;
864}
865
866template<support::endianness target_endianness, bool is64Bits>
867error_code ELFObjectFile<target_endianness, is64Bits>
868 ::getRelocationSymbol(DataRefImpl Rel,
869 SymbolRef &Result) const {
870 uint32_t symbolIdx;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000871 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000872 switch (sec->sh_type) {
873 default :
874 report_fatal_error("Invalid section type in Rel!");
875 case ELF::SHT_REL : {
876 symbolIdx = getRel(Rel)->getSymbol();
877 break;
878 }
879 case ELF::SHT_RELA : {
880 symbolIdx = getRela(Rel)->getSymbol();
881 break;
882 }
883 }
884 DataRefImpl SymbolData;
885 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
886 if (it == SymbolTableSectionsIndexMap.end())
887 report_fatal_error("Relocation symbol table not found!");
888 SymbolData.d.a = symbolIdx;
889 SymbolData.d.b = it->second;
890 Result = SymbolRef(SymbolData, this);
891 return object_error::success;
892}
893
894template<support::endianness target_endianness, bool is64Bits>
895error_code ELFObjectFile<target_endianness, is64Bits>
896 ::getRelocationAddress(DataRefImpl Rel,
897 uint64_t &Result) const {
898 uint64_t offset;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000899 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000900 switch (sec->sh_type) {
901 default :
902 report_fatal_error("Invalid section type in Rel!");
903 case ELF::SHT_REL : {
904 offset = getRel(Rel)->r_offset;
905 break;
906 }
907 case ELF::SHT_RELA : {
908 offset = getRela(Rel)->r_offset;
909 break;
910 }
911 }
912
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000913 Result = offset;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000914 return object_error::success;
915}
916
917template<support::endianness target_endianness, bool is64Bits>
918error_code ELFObjectFile<target_endianness, is64Bits>
919 ::getRelocationType(DataRefImpl Rel,
920 uint32_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000921 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000922 switch (sec->sh_type) {
923 default :
924 report_fatal_error("Invalid section type in Rel!");
925 case ELF::SHT_REL : {
926 Result = getRel(Rel)->getType();
927 break;
928 }
929 case ELF::SHT_RELA : {
930 Result = getRela(Rel)->getType();
931 break;
932 }
933 }
934 return object_error::success;
935}
936
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000937#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
938 case ELF::enum: res = #enum; break;
939
940template<support::endianness target_endianness, bool is64Bits>
941error_code ELFObjectFile<target_endianness, is64Bits>
942 ::getRelocationTypeName(DataRefImpl Rel,
943 SmallVectorImpl<char> &Result) const {
944 const Elf_Shdr *sec = getSection(Rel.w.b);
945 uint8_t type;
946 StringRef res;
947 switch (sec->sh_type) {
948 default :
949 return object_error::parse_failed;
950 case ELF::SHT_REL : {
951 type = getRel(Rel)->getType();
952 break;
953 }
954 case ELF::SHT_RELA : {
955 type = getRela(Rel)->getType();
956 break;
957 }
958 }
959 switch (Header->e_machine) {
960 case ELF::EM_X86_64:
961 switch (type) {
962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
969 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
970 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
971 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
972 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
973 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
974 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
975 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
976 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
977 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
978 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
979 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
980 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
981 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
982 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
983 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
984 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
985 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
986 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
987 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
988 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
989 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
990 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
991 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
992 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
993 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
994 default:
995 res = "Unknown";
996 }
997 break;
998 case ELF::EM_386:
999 switch (type) {
1000 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1001 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1002 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1003 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1004 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1005 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1006 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1007 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1008 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1009 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1010 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1011 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1012 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1013 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1014 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1015 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1016 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1017 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1018 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1019 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1020 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1021 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1022 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1023 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1024 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1025 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1026 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1027 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1028 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1029 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1030 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1031 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1032 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1033 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1034 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1035 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1036 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1037 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1038 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1039 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1040 default:
1041 res = "Unknown";
1042 }
1043 break;
1044 default:
1045 res = "Unknown";
1046 }
1047 Result.append(res.begin(), res.end());
1048 return object_error::success;
1049}
1050
1051#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1052
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001053template<support::endianness target_endianness, bool is64Bits>
1054error_code ELFObjectFile<target_endianness, is64Bits>
1055 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1056 int64_t &Result) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001057 const Elf_Shdr *sec = getSection(Rel.w.b);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001058 switch (sec->sh_type) {
1059 default :
1060 report_fatal_error("Invalid section type in Rel!");
1061 case ELF::SHT_REL : {
1062 Result = 0;
1063 return object_error::success;
1064 }
1065 case ELF::SHT_RELA : {
1066 Result = getRela(Rel)->r_addend;
1067 return object_error::success;
1068 }
1069 }
1070}
1071
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001072template<support::endianness target_endianness, bool is64Bits>
1073error_code ELFObjectFile<target_endianness, is64Bits>
1074 ::getRelocationValueString(DataRefImpl Rel,
1075 SmallVectorImpl<char> &Result) const {
1076 const Elf_Shdr *sec = getSection(Rel.w.b);
1077 uint8_t type;
1078 StringRef res;
1079 int64_t addend = 0;
1080 uint16_t symbol_index = 0;
1081 switch (sec->sh_type) {
1082 default :
1083 return object_error::parse_failed;
1084 case ELF::SHT_REL : {
1085 type = getRel(Rel)->getType();
1086 symbol_index = getRel(Rel)->getSymbol();
1087 // TODO: Read implicit addend from section data.
1088 break;
1089 }
1090 case ELF::SHT_RELA : {
1091 type = getRela(Rel)->getType();
1092 symbol_index = getRela(Rel)->getSymbol();
1093 addend = getRela(Rel)->r_addend;
1094 break;
1095 }
1096 }
1097 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1098 StringRef symname;
1099 if (error_code ec = getSymbolName(symb, symname))
1100 return ec;
1101 switch (Header->e_machine) {
1102 case ELF::EM_X86_64:
1103 switch (type) {
1104 case ELF::R_X86_64_32S:
1105 res = symname;
1106 break;
1107 case ELF::R_X86_64_PC32: {
1108 std::string fmtbuf;
1109 raw_string_ostream fmt(fmtbuf);
1110 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1111 fmt.flush();
1112 Result.append(fmtbuf.begin(), fmtbuf.end());
1113 }
1114 break;
1115 default:
1116 res = "Unknown";
1117 }
1118 break;
1119 default:
1120 res = "Unknown";
1121 }
1122 if (Result.empty())
1123 Result.append(res.begin(), res.end());
1124 return object_error::success;
1125}
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001126
Benjamin Kramer07ea23a2011-07-15 18:39:21 +00001127template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +00001128ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1129 , error_code &ec)
1130 : ObjectFile(Binary::isELF, Object, ec)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001131 , SectionHeaderTable(0)
1132 , dot_shstrtab_sec(0)
1133 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +00001134 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001135
1136 if (Header->e_shoff == 0)
1137 return;
1138
1139 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +00001140 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001141 uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001142 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize
Michael J. Spencer001c9202011-06-25 17:54:50 +00001143 <= base() + Data->getBufferSize()))
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001144 // FIXME: Proper error handling.
1145 report_fatal_error("Section table goes past end of file!");
1146
Nick Lewyckyfb05d3d2011-10-11 00:38:56 +00001147
Nick Lewycky15c3f722011-10-11 02:57:48 +00001148 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1149 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001150 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001151 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
Nick Lewycky15c3f722011-10-11 02:57:48 +00001152 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
1153 if (SymbolTableSectionHeaderIndex)
1154 // FIXME: Proper error handling.
1155 report_fatal_error("More than one .symtab_shndx!");
1156 SymbolTableSectionHeaderIndex = sh;
1157 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001158 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001159 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001160 SymbolTableSections.push_back(sh);
1161 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001162 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
Michael J. Spencer63b2f8c2011-10-13 22:30:10 +00001163 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001164 }
1165 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001166 }
1167
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001168 // Sort section relocation lists by index.
1169 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1170 e = SectionRelocMap.end(); i != e; ++i) {
1171 std::sort(i->second.begin(), i->second.end());
1172 }
1173
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001174 // Get string table sections.
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001175 dot_shstrtab_sec = getSection(getStringTableIndex());
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001176 if (dot_shstrtab_sec) {
1177 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001178 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001179 [dot_shstrtab_sec->sh_size - 1] != 0)
1180 // FIXME: Proper error handling.
1181 report_fatal_error("String table must end with a null terminator!");
1182 }
1183
1184 // Merge this into the above loop.
1185 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001186 *e = i + getNumSections() * Header->e_shentsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001187 i != e; i += Header->e_shentsize) {
1188 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1189 if (sh->sh_type == ELF::SHT_STRTAB) {
1190 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1191 if (SectionName == ".strtab") {
1192 if (dot_strtab_sec != 0)
1193 // FIXME: Proper error handling.
1194 report_fatal_error("Already found section named .strtab!");
1195 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +00001196 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001197 if (dot_strtab[sh->sh_size - 1] != 0)
1198 // FIXME: Proper error handling.
1199 report_fatal_error("String table must end with a null terminator!");
1200 }
1201 }
1202 }
Nick Lewycky15c3f722011-10-11 02:57:48 +00001203
1204 // Build symbol name side-mapping if there is one.
1205 if (SymbolTableSectionHeaderIndex) {
1206 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1207 SymbolTableSectionHeaderIndex->sh_offset);
1208 error_code ec;
1209 for (symbol_iterator si = begin_symbols(),
1210 se = end_symbols(); si != se; si.increment(ec)) {
1211 if (ec)
1212 report_fatal_error("Fewer extended symbol table entries than symbols!");
1213 if (*ShndxTable != ELF::SHN_UNDEF)
1214 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1215 ++ShndxTable;
1216 }
1217 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001218}
1219
1220template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001221symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1222 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001223 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001224 memset(&SymbolData, 0, sizeof(SymbolData));
1225 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001226 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1227 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001228 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001229 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1230 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001231 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001232 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001233}
1234
1235template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001236symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1237 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001238 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001239 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001240 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1241 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1242 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001243}
1244
1245template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001246section_iterator ELFObjectFile<target_endianness, is64Bits>
1247 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001248 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001249 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001250 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001251 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001252}
1253
1254template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001255section_iterator ELFObjectFile<target_endianness, is64Bits>
1256 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001257 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +00001258 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +00001259 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001260 + Header->e_shoff
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001261 + (Header->e_shentsize*getNumSections()));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001262 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001263}
1264
1265template<support::endianness target_endianness, bool is64Bits>
1266uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001267 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001268}
1269
1270template<support::endianness target_endianness, bool is64Bits>
1271StringRef ELFObjectFile<target_endianness, is64Bits>
1272 ::getFileFormatName() const {
1273 switch(Header->e_ident[ELF::EI_CLASS]) {
1274 case ELF::ELFCLASS32:
1275 switch(Header->e_machine) {
1276 case ELF::EM_386:
1277 return "ELF32-i386";
1278 case ELF::EM_X86_64:
1279 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001280 case ELF::EM_ARM:
1281 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001282 default:
1283 return "ELF32-unknown";
1284 }
1285 case ELF::ELFCLASS64:
1286 switch(Header->e_machine) {
1287 case ELF::EM_386:
1288 return "ELF64-i386";
1289 case ELF::EM_X86_64:
1290 return "ELF64-x86-64";
1291 default:
1292 return "ELF64-unknown";
1293 }
1294 default:
1295 // FIXME: Proper error handling.
1296 report_fatal_error("Invalid ELFCLASS!");
1297 }
1298}
1299
1300template<support::endianness target_endianness, bool is64Bits>
1301unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1302 switch(Header->e_machine) {
1303 case ELF::EM_386:
1304 return Triple::x86;
1305 case ELF::EM_X86_64:
1306 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001307 case ELF::EM_ARM:
1308 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001309 default:
1310 return Triple::UnknownArch;
1311 }
1312}
1313
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001314template<support::endianness target_endianness, bool is64Bits>
1315uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
1316 if (Header->e_shnum == ELF::SHN_UNDEF)
1317 return SectionHeaderTable->sh_size;
1318 return Header->e_shnum;
1319}
1320
1321template<support::endianness target_endianness, bool is64Bits>
1322uint64_t
1323ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1324 if (Header->e_shnum == ELF::SHN_UNDEF) {
1325 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1326 return SectionHeaderTable->sh_link;
1327 if (Header->e_shstrndx >= getNumSections())
1328 return 0;
1329 }
1330 return Header->e_shstrndx;
1331}
1332
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001333
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001334template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001335template<typename T>
1336inline const T *
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001337ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1338 uint32_t Entry) const {
1339 return getEntry<T>(getSection(Section), Entry);
1340}
1341
1342template<support::endianness target_endianness, bool is64Bits>
1343template<typename T>
1344inline const T *
1345ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1346 uint32_t Entry) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001347 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +00001348 base()
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001349 + Section->sh_offset
1350 + (Entry * Section->sh_entsize));
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001351}
1352
1353template<support::endianness target_endianness, bool is64Bits>
1354const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1355ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001356 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001357}
1358
1359template<support::endianness target_endianness, bool is64Bits>
1360const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1361ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001362 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001363}
1364
1365template<support::endianness target_endianness, bool is64Bits>
1366const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1367ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001368 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001369}
1370
1371template<support::endianness target_endianness, bool is64Bits>
1372const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1373ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001374 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001375 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001376 // FIXME: Proper error handling.
1377 report_fatal_error("Invalid symbol table section!");
1378 return sec;
1379}
1380
1381template<support::endianness target_endianness, bool is64Bits>
1382const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001383ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1384 if (index == 0)
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001385 return 0;
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001386 if (!SectionHeaderTable || index >= getNumSections())
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001387 // FIXME: Proper error handling.
1388 report_fatal_error("Invalid section index!");
1389
1390 return reinterpret_cast<const Elf_Shdr *>(
1391 reinterpret_cast<const char *>(SectionHeaderTable)
1392 + (index * Header->e_shentsize));
1393}
1394
1395template<support::endianness target_endianness, bool is64Bits>
1396const char *ELFObjectFile<target_endianness, is64Bits>
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001397 ::getString(uint32_t section,
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001398 ELF::Elf32_Word offset) const {
1399 return getString(getSection(section), offset);
1400}
1401
1402template<support::endianness target_endianness, bool is64Bits>
1403const char *ELFObjectFile<target_endianness, is64Bits>
1404 ::getString(const Elf_Shdr *section,
1405 ELF::Elf32_Word offset) const {
1406 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1407 if (offset >= section->sh_size)
1408 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001409 report_fatal_error("Symbol name offset outside of string table!");
1410 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001411}
1412
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001413template<support::endianness target_endianness, bool is64Bits>
1414error_code ELFObjectFile<target_endianness, is64Bits>
1415 ::getSymbolName(const Elf_Sym *symb,
1416 StringRef &Result) const {
1417 if (symb->st_name == 0) {
Nick Lewyckybfbbe322011-10-11 03:18:58 +00001418 const Elf_Shdr *section = getSection(symb);
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001419 if (!section)
1420 Result = "";
1421 else
1422 Result = getString(dot_shstrtab_sec, section->sh_name);
1423 return object_error::success;
1424 }
1425
1426 // Use the default symbol table name section.
1427 Result = getString(dot_strtab_sec, symb->st_name);
1428 return object_error::success;
1429}
1430
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001431// EI_CLASS, EI_DATA.
1432static std::pair<unsigned char, unsigned char>
1433getElfArchType(MemoryBuffer *Object) {
1434 if (Object->getBufferSize() < ELF::EI_NIDENT)
1435 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1436 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1437 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1438}
1439
1440namespace llvm {
1441
1442 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
1443 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001444 error_code ec;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001445 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001446 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001447 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001448 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001449 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001450 return new ELFObjectFile<support::little, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001451 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001452 return new ELFObjectFile<support::big, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001453 // FIXME: Proper error handling.
1454 report_fatal_error("Not an ELF object file!");
1455 }
1456
1457} // end namespace llvm