blob: 0458c0713c1500ec5b9d1bcb6cf0127d05138d29 [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"
23#include <limits>
24#include <utility>
25
26using namespace llvm;
27using namespace object;
28
29// Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
30namespace {
31template<support::endianness target_endianness>
32struct ELFDataTypeTypedefHelperCommon {
33 typedef support::detail::packed_endian_specific_integral
34 <uint16_t, target_endianness, support::aligned> Elf_Half;
35 typedef support::detail::packed_endian_specific_integral
36 <uint32_t, target_endianness, support::aligned> Elf_Word;
37 typedef support::detail::packed_endian_specific_integral
38 <int32_t, target_endianness, support::aligned> Elf_Sword;
39 typedef support::detail::packed_endian_specific_integral
40 <uint64_t, target_endianness, support::aligned> Elf_Xword;
41 typedef support::detail::packed_endian_specific_integral
42 <int64_t, target_endianness, support::aligned> Elf_Sxword;
43};
44}
45
46namespace {
47template<support::endianness target_endianness, bool is64Bits>
48struct ELFDataTypeTypedefHelper;
49
50/// ELF 32bit types.
51template<support::endianness target_endianness>
52struct ELFDataTypeTypedefHelper<target_endianness, false>
53 : ELFDataTypeTypedefHelperCommon<target_endianness> {
54 typedef support::detail::packed_endian_specific_integral
55 <uint32_t, target_endianness, support::aligned> Elf_Addr;
56 typedef support::detail::packed_endian_specific_integral
57 <uint32_t, target_endianness, support::aligned> Elf_Off;
58};
59
60/// ELF 64bit types.
61template<support::endianness target_endianness>
62struct ELFDataTypeTypedefHelper<target_endianness, true>
63 : ELFDataTypeTypedefHelperCommon<target_endianness>{
64 typedef support::detail::packed_endian_specific_integral
65 <uint64_t, target_endianness, support::aligned> Elf_Addr;
66 typedef support::detail::packed_endian_specific_integral
67 <uint64_t, target_endianness, support::aligned> Elf_Off;
68};
69}
70
71// I really don't like doing this, but the alternative is copypasta.
72#define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
73typedef typename \
74 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
75typedef typename \
76 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
77typedef typename \
78 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
79typedef typename \
80 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
81typedef typename \
82 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
83typedef typename \
84 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
85typedef typename \
86 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
87
88 // Section header.
89namespace {
90template<support::endianness target_endianness, bool is64Bits>
91struct Elf_Shdr_Base;
92
93template<support::endianness target_endianness>
94struct Elf_Shdr_Base<target_endianness, false> {
95 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
96 Elf_Word sh_name; // Section name (index into string table)
97 Elf_Word sh_type; // Section type (SHT_*)
98 Elf_Word sh_flags; // Section flags (SHF_*)
99 Elf_Addr sh_addr; // Address where section is to be loaded
100 Elf_Off sh_offset; // File offset of section data, in bytes
101 Elf_Word sh_size; // Size of section, in bytes
102 Elf_Word sh_link; // Section type-specific header table index link
103 Elf_Word sh_info; // Section type-specific extra information
104 Elf_Word sh_addralign;// Section address alignment
105 Elf_Word sh_entsize; // Size of records contained within the section
106};
107
108template<support::endianness target_endianness>
109struct Elf_Shdr_Base<target_endianness, true> {
110 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
111 Elf_Word sh_name; // Section name (index into string table)
112 Elf_Word sh_type; // Section type (SHT_*)
113 Elf_Xword sh_flags; // Section flags (SHF_*)
114 Elf_Addr sh_addr; // Address where section is to be loaded
115 Elf_Off sh_offset; // File offset of section data, in bytes
116 Elf_Xword sh_size; // Size of section, in bytes
117 Elf_Word sh_link; // Section type-specific header table index link
118 Elf_Word sh_info; // Section type-specific extra information
119 Elf_Xword sh_addralign;// Section address alignment
120 Elf_Xword sh_entsize; // Size of records contained within the section
121};
122
123template<support::endianness target_endianness, bool is64Bits>
124struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> {
125 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize;
126 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size;
127
128 /// @brief Get the number of entities this section contains if it has any.
129 unsigned getEntityCount() const {
130 if (sh_entsize == 0)
131 return 0;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000132 return sh_size / sh_entsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000133 }
134};
135}
136
137namespace {
138template<support::endianness target_endianness, bool is64Bits>
139struct Elf_Sym_Base;
140
141template<support::endianness target_endianness>
142struct Elf_Sym_Base<target_endianness, false> {
143 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
144 Elf_Word st_name; // Symbol name (index into string table)
145 Elf_Addr st_value; // Value or address associated with the symbol
146 Elf_Word st_size; // Size of the symbol
147 unsigned char st_info; // Symbol's type and binding attributes
148 unsigned char st_other; // Must be zero; reserved
149 Elf_Half st_shndx; // Which section (header table index) it's defined in
150};
151
152template<support::endianness target_endianness>
153struct Elf_Sym_Base<target_endianness, true> {
154 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
155 Elf_Word st_name; // Symbol name (index into string table)
156 unsigned char st_info; // Symbol's type and binding attributes
157 unsigned char st_other; // Must be zero; reserved
158 Elf_Half st_shndx; // Which section (header table index) it's defined in
159 Elf_Addr st_value; // Value or address associated with the symbol
160 Elf_Xword st_size; // Size of the symbol
161};
162
163template<support::endianness target_endianness, bool is64Bits>
164struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> {
165 using Elf_Sym_Base<target_endianness, is64Bits>::st_info;
166
167 // These accessors and mutators correspond to the ELF32_ST_BIND,
168 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
169 unsigned char getBinding() const { return st_info >> 4; }
170 unsigned char getType() const { return st_info & 0x0f; }
171 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
172 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
173 void setBindingAndType(unsigned char b, unsigned char t) {
174 st_info = (b << 4) + (t & 0x0f);
175 }
176};
177}
178
179namespace {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000180template<support::endianness target_endianness, bool is64Bits, bool isRela>
181struct Elf_Rel_Base;
182
183template<support::endianness target_endianness>
184struct Elf_Rel_Base<target_endianness, false, false> {
185 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
186 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
187 Elf_Word r_info; // Symbol table index and type of relocation to apply
188};
189
190template<support::endianness target_endianness>
191struct Elf_Rel_Base<target_endianness, true, false> {
192 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
193 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
194 Elf_Xword r_info; // Symbol table index and type of relocation to apply
195};
196
197template<support::endianness target_endianness>
198struct Elf_Rel_Base<target_endianness, false, true> {
199 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
200 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
201 Elf_Word r_info; // Symbol table index and type of relocation to apply
202 Elf_Sword r_addend; // Compute value for relocatable field by adding this
203};
204
205template<support::endianness target_endianness>
206struct Elf_Rel_Base<target_endianness, true, true> {
207 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
208 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
209 Elf_Xword r_info; // Symbol table index and type of relocation to apply
210 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
211};
212
213template<support::endianness target_endianness, bool is64Bits, bool isRela>
214struct Elf_Rel_Impl;
215
216template<support::endianness target_endianness, bool isRela>
217struct Elf_Rel_Impl<target_endianness, true, isRela>
218 : Elf_Rel_Base<target_endianness, true, isRela> {
219 using Elf_Rel_Base<target_endianness, true, isRela>::r_info;
220 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
221
222 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
223 // and ELF64_R_INFO macros defined in the ELF specification:
224 uint64_t getSymbol() const { return (r_info >> 32); }
225 unsigned char getType() const {
226 return (unsigned char) (r_info & 0xffffffffL);
227 }
228 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
229 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
230 void setSymbolAndType(uint64_t s, unsigned char t) {
231 r_info = (s << 32) + (t&0xffffffffL);
232 }
233};
234
235template<support::endianness target_endianness, bool isRela>
236struct Elf_Rel_Impl<target_endianness, false, isRela>
237 : Elf_Rel_Base<target_endianness, false, isRela> {
238 using Elf_Rel_Base<target_endianness, false, isRela>::r_info;
239 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
240
241 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
242 // and ELF32_R_INFO macros defined in the ELF specification:
243 uint32_t getSymbol() const { return (r_info >> 8); }
244 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
245 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
246 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
247 void setSymbolAndType(uint32_t s, unsigned char t) {
248 r_info = (s << 8) + t;
249 }
250};
251
252}
253
254namespace {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000255template<support::endianness target_endianness, bool is64Bits>
256class ELFObjectFile : public ObjectFile {
257 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
258
259 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
260 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000261 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
262 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000263
264 struct Elf_Ehdr {
265 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
266 Elf_Half e_type; // Type of file (see ET_*)
267 Elf_Half e_machine; // Required architecture for this file (see EM_*)
268 Elf_Word e_version; // Must be equal to 1
269 Elf_Addr e_entry; // Address to jump to in order to start program
270 Elf_Off e_phoff; // Program header table's file offset, in bytes
271 Elf_Off e_shoff; // Section header table's file offset, in bytes
272 Elf_Word e_flags; // Processor-specific flags
273 Elf_Half e_ehsize; // Size of ELF header, in bytes
274 Elf_Half e_phentsize;// Size of an entry in the program header table
275 Elf_Half e_phnum; // Number of entries in the program header table
276 Elf_Half e_shentsize;// Size of an entry in the section header table
277 Elf_Half e_shnum; // Number of entries in the section header table
278 Elf_Half e_shstrndx; // Section header table index of section name
279 // string table
280 bool checkMagic() const {
281 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
282 }
283 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
284 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
285 };
286
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000287 typedef SmallVector<const Elf_Shdr*, 1> Sections_t;
288 typedef DenseMap<unsigned, unsigned> IndexMap_t;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000289
290 const Elf_Ehdr *Header;
291 const Elf_Shdr *SectionHeaderTable;
292 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
293 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000294 Sections_t SymbolTableSections;
295 IndexMap_t SymbolTableSectionsIndexMap;
296 Sections_t RelocationTableSections;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000297
298 void validateSymbol(DataRefImpl Symb) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000299 bool isRelocationHasAddend(DataRefImpl Rel) const;
300 template<typename T>
301 const T *getEntry(DataRefImpl Entry, Sections_t Sections) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000302 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
303 const Elf_Shdr *getSection(DataRefImpl index) const;
304 const Elf_Shdr *getSection(uint16_t index) const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000305 const Elf_Rel *getRel(DataRefImpl Rel) const;
306 const Elf_Rela *getRela(DataRefImpl Rela) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000307 const char *getString(uint16_t section, uint32_t offset) const;
308 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
309
310protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000311 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
312 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000313 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000314 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
315 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
316 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
317 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000318 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
319 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000320
Michael J. Spencer25b15772011-06-25 17:55:23 +0000321 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
322 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
323 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
324 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
325 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
326 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000327 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
328 bool &Result) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000329
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000330 virtual error_code getRelocationNext(DataRefImpl Rel,
331 RelocationRef &Res) const;
332 virtual error_code getRelocationAddress(DataRefImpl Rel,
333 uint64_t &Res) const;
334 virtual error_code getRelocationSymbol(DataRefImpl Rel,
335 SymbolRef &Res) const;
336 virtual error_code getRelocationType(DataRefImpl Rel,
337 uint32_t &Res) const;
338 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
339 int64_t &Res) const;
340
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000341public:
Michael J. Spencer001c9202011-06-25 17:54:50 +0000342 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000343 virtual symbol_iterator begin_symbols() const;
344 virtual symbol_iterator end_symbols() const;
345 virtual section_iterator begin_sections() const;
346 virtual section_iterator end_sections() const;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000347 virtual relocation_iterator begin_relocations() const;
348 virtual relocation_iterator end_relocations() const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000349
350 virtual uint8_t getBytesInAddress() const;
351 virtual StringRef getFileFormatName() const;
352 virtual unsigned getArch() const;
353};
354} // end namespace
355
356template<support::endianness target_endianness, bool is64Bits>
357void ELFObjectFile<target_endianness, is64Bits>
358 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000359 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000360 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000361 // FIXME: We really need to do proper error handling in the case of an invalid
362 // input file. Because we don't use exceptions, I think we'll just pass
363 // an error object around.
364 if (!( symb
365 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000366 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000367 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000368 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000369 + SymbolTableSection->sh_offset
370 + SymbolTableSection->sh_size)))
371 // FIXME: Proper error handling.
372 report_fatal_error("Symb must point to a valid symbol!");
373}
374
375template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000376error_code ELFObjectFile<target_endianness, is64Bits>
377 ::getSymbolNext(DataRefImpl Symb,
378 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000379 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000380 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000381
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000382 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000383 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000384 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000385 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000386 ++Symb.d.b;
387 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000388 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000389 if (Symb.d.b >= SymbolTableSections.size()) {
390 Symb.d.a = std::numeric_limits<uint32_t>::max();
391 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000392 }
393 }
394
Michael J. Spencer25b15772011-06-25 17:55:23 +0000395 Result = SymbolRef(Symb, this);
396 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000397}
398
399template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000400error_code ELFObjectFile<target_endianness, is64Bits>
401 ::getSymbolName(DataRefImpl Symb,
402 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000403 validateSymbol(Symb);
404 const Elf_Sym *symb = getSymbol(Symb);
405 if (symb->st_name == 0) {
406 const Elf_Shdr *section = getSection(symb->st_shndx);
407 if (!section)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000408 Result = "";
409 else
410 Result = getString(dot_shstrtab_sec, section->sh_name);
411 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000412 }
413
414 // Use the default symbol table name section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000415 Result = getString(dot_strtab_sec, symb->st_name);
416 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000417}
418
419template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000420error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000421 ::getSymbolOffset(DataRefImpl Symb,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000422 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000423 validateSymbol(Symb);
424 const Elf_Sym *symb = getSymbol(Symb);
425 const Elf_Shdr *Section;
426 switch (symb->st_shndx) {
427 case ELF::SHN_COMMON:
428 // Undefined symbols have no address yet.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000429 case ELF::SHN_UNDEF:
430 Result = UnknownAddressOrSize;
431 return object_error::success;
432 case ELF::SHN_ABS:
433 Result = symb->st_value;
434 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000435 default: Section = getSection(symb->st_shndx);
436 }
437
438 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000439 case ELF::STT_SECTION:
440 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
441 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000442 case ELF::STT_FUNC:
443 case ELF::STT_OBJECT:
444 case ELF::STT_NOTYPE:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000445 Result = symb->st_value;
446 return object_error::success;
447 default:
448 Result = UnknownAddressOrSize;
449 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000450 }
451}
452
453template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000454error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000455 ::getSymbolAddress(DataRefImpl Symb,
456 uint64_t &Result) const {
457 validateSymbol(Symb);
458 const Elf_Sym *symb = getSymbol(Symb);
459 const Elf_Shdr *Section;
460 switch (symb->st_shndx) {
461 case ELF::SHN_COMMON: // Fall through.
462 // Undefined symbols have no address yet.
463 case ELF::SHN_UNDEF:
464 Result = UnknownAddressOrSize;
465 return object_error::success;
466 case ELF::SHN_ABS:
467 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value);
468 return object_error::success;
469 default: Section = getSection(symb->st_shndx);
470 }
471 const uint8_t* addr = base();
472 if (Section)
473 addr += Section->sh_offset;
474 switch (symb->getType()) {
475 case ELF::STT_SECTION:
476 Result = reinterpret_cast<uintptr_t>(addr);
477 return object_error::success;
478 case ELF::STT_FUNC: // Fall through.
479 case ELF::STT_OBJECT: // Fall through.
480 case ELF::STT_NOTYPE:
481 addr += symb->st_value;
482 Result = reinterpret_cast<uintptr_t>(addr);
483 return object_error::success;
484 default:
485 Result = UnknownAddressOrSize;
486 return object_error::success;
487 }
488}
489
490template<support::endianness target_endianness, bool is64Bits>
491error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000492 ::getSymbolSize(DataRefImpl Symb,
493 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000494 validateSymbol(Symb);
495 const Elf_Sym *symb = getSymbol(Symb);
496 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000497 Result = UnknownAddressOrSize;
498 Result = symb->st_size;
499 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000500}
501
502template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000503error_code ELFObjectFile<target_endianness, is64Bits>
504 ::getSymbolNMTypeChar(DataRefImpl Symb,
505 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000506 validateSymbol(Symb);
507 const Elf_Sym *symb = getSymbol(Symb);
508 const Elf_Shdr *Section = getSection(symb->st_shndx);
509
510 char ret = '?';
511
512 if (Section) {
513 switch (Section->sh_type) {
514 case ELF::SHT_PROGBITS:
515 case ELF::SHT_DYNAMIC:
516 switch (Section->sh_flags) {
517 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
518 ret = 't'; break;
519 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
520 ret = 'd'; break;
521 case ELF::SHF_ALLOC:
522 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
523 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
524 ret = 'r'; break;
525 }
526 break;
527 case ELF::SHT_NOBITS: ret = 'b';
528 }
529 }
530
531 switch (symb->st_shndx) {
532 case ELF::SHN_UNDEF:
533 if (ret == '?')
534 ret = 'U';
535 break;
536 case ELF::SHN_ABS: ret = 'a'; break;
537 case ELF::SHN_COMMON: ret = 'c'; break;
538 }
539
540 switch (symb->getBinding()) {
541 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
542 case ELF::STB_WEAK:
543 if (symb->st_shndx == ELF::SHN_UNDEF)
544 ret = 'w';
545 else
546 if (symb->getType() == ELF::STT_OBJECT)
547 ret = 'V';
548 else
549 ret = 'W';
550 }
551
Michael J. Spencer25b15772011-06-25 17:55:23 +0000552 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
553 StringRef name;
554 if (error_code ec = getSymbolName(Symb, name))
555 return ec;
556 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000557 .StartsWith(".debug", 'N')
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000558 .StartsWith(".note", 'n')
559 .Default('?');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000560 return object_error::success;
561 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000562
Michael J. Spencer25b15772011-06-25 17:55:23 +0000563 Result = ret;
564 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000565}
566
567template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000568error_code ELFObjectFile<target_endianness, is64Bits>
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000569 ::getSymbolType(DataRefImpl Symb,
570 SymbolRef::SymbolType &Result) const {
571 validateSymbol(Symb);
572 const Elf_Sym *symb = getSymbol(Symb);
573
574 if (symb->st_shndx == ELF::SHN_UNDEF) {
575 Result = SymbolRef::ST_External;
576 return object_error::success;
577 }
578
579 switch (symb->getType()) {
580 case ELF::STT_FUNC:
581 Result = SymbolRef::ST_Function;
582 break;
583 case ELF::STT_OBJECT:
584 Result = SymbolRef::ST_Data;
585 break;
586 default:
587 Result = SymbolRef::ST_Other;
588 break;
589 }
590 return object_error::success;
591}
592
593template<support::endianness target_endianness, bool is64Bits>
594error_code ELFObjectFile<target_endianness, is64Bits>
595 ::isSymbolGlobal(DataRefImpl Symb,
596 bool &Result) const {
597 validateSymbol(Symb);
598 const Elf_Sym *symb = getSymbol(Symb);
599
600 Result = symb->getBinding() == ELF::STB_GLOBAL;
601 return object_error::success;
602}
603
604template<support::endianness target_endianness, bool is64Bits>
605error_code ELFObjectFile<target_endianness, is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000606 ::isSymbolInternal(DataRefImpl Symb,
607 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000608 validateSymbol(Symb);
609 const Elf_Sym *symb = getSymbol(Symb);
610
611 if ( symb->getType() == ELF::STT_FILE
612 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000613 Result = true;
614 Result = false;
615 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000616}
617
618template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000619error_code ELFObjectFile<target_endianness, is64Bits>
620 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000621 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000622 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000623 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000624 Result = SectionRef(Sec, this);
625 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000626}
627
628template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000629error_code ELFObjectFile<target_endianness, is64Bits>
630 ::getSectionName(DataRefImpl Sec,
631 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000632 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000633 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
634 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000635}
636
637template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000638error_code ELFObjectFile<target_endianness, is64Bits>
639 ::getSectionAddress(DataRefImpl Sec,
640 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000641 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000642 Result = sec->sh_addr;
643 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000644}
645
646template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000647error_code ELFObjectFile<target_endianness, is64Bits>
648 ::getSectionSize(DataRefImpl Sec,
649 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000650 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000651 Result = sec->sh_size;
652 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000653}
654
655template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000656error_code ELFObjectFile<target_endianness, is64Bits>
657 ::getSectionContents(DataRefImpl Sec,
658 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000659 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000660 const char *start = (const char*)base() + sec->sh_offset;
661 Result = StringRef(start, sec->sh_size);
662 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000663}
664
665template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000666error_code ELFObjectFile<target_endianness, is64Bits>
667 ::isSectionText(DataRefImpl Sec,
668 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000669 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000670 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000671 Result = true;
672 else
673 Result = false;
674 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000675}
676
677template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000678error_code ELFObjectFile<target_endianness, is64Bits>
679 ::sectionContainsSymbol(DataRefImpl Sec,
680 DataRefImpl Symb,
681 bool &Result) const {
682 // FIXME: Unimplemented.
683 Result = false;
684 return object_error::success;
685}
686
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000687// Relocations
688template<support::endianness target_endianness, bool is64Bits>
689error_code ELFObjectFile<target_endianness, is64Bits>
690 ::getRelocationNext(DataRefImpl Rel,
691 RelocationRef &Result) const {
692 const Elf_Shdr *RelocationTableSection = RelocationTableSections[Rel.d.b];
693
694 // Check to see if we are at the end of this relocation table.
695 if (++Rel.d.a >= RelocationTableSection->getEntityCount()) {
696 // We are at the end. If there are other relocation tables, jump to them.
697 Rel.d.a = 0;
698 // Otherwise return the terminator.
699 if (++Rel.d.b >= SymbolTableSections.size()) {
700 Rel.d.a = std::numeric_limits<uint32_t>::max();
701 Rel.d.b = std::numeric_limits<uint32_t>::max();
702 }
703 }
704
705 Result = RelocationRef(Rel, this);
706 return object_error::success;
707}
708
709template<support::endianness target_endianness, bool is64Bits>
710error_code ELFObjectFile<target_endianness, is64Bits>
711 ::getRelocationSymbol(DataRefImpl Rel,
712 SymbolRef &Result) const {
713 uint32_t symbolIdx;
714 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
715 switch (sec->sh_type) {
716 default :
717 report_fatal_error("Invalid section type in Rel!");
718 case ELF::SHT_REL : {
719 symbolIdx = getRel(Rel)->getSymbol();
720 break;
721 }
722 case ELF::SHT_RELA : {
723 symbolIdx = getRela(Rel)->getSymbol();
724 break;
725 }
726 }
727 DataRefImpl SymbolData;
728 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
729 if (it == SymbolTableSectionsIndexMap.end())
730 report_fatal_error("Relocation symbol table not found!");
731 SymbolData.d.a = symbolIdx;
732 SymbolData.d.b = it->second;
733 Result = SymbolRef(SymbolData, this);
734 return object_error::success;
735}
736
737template<support::endianness target_endianness, bool is64Bits>
738error_code ELFObjectFile<target_endianness, is64Bits>
739 ::getRelocationAddress(DataRefImpl Rel,
740 uint64_t &Result) const {
741 uint64_t offset;
742 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
743 switch (sec->sh_type) {
744 default :
745 report_fatal_error("Invalid section type in Rel!");
746 case ELF::SHT_REL : {
747 offset = getRel(Rel)->r_offset;
748 break;
749 }
750 case ELF::SHT_RELA : {
751 offset = getRela(Rel)->r_offset;
752 break;
753 }
754 }
755
756 const Elf_Shdr *secAddr = getSection(sec->sh_info);
757 Result = offset + reinterpret_cast<uintptr_t>(base() + secAddr->sh_offset);
758 return object_error::success;
759}
760
761template<support::endianness target_endianness, bool is64Bits>
762error_code ELFObjectFile<target_endianness, is64Bits>
763 ::getRelocationType(DataRefImpl Rel,
764 uint32_t &Result) const {
765 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
766 switch (sec->sh_type) {
767 default :
768 report_fatal_error("Invalid section type in Rel!");
769 case ELF::SHT_REL : {
770 Result = getRel(Rel)->getType();
771 break;
772 }
773 case ELF::SHT_RELA : {
774 Result = getRela(Rel)->getType();
775 break;
776 }
777 }
778 return object_error::success;
779}
780
781template<support::endianness target_endianness, bool is64Bits>
782error_code ELFObjectFile<target_endianness, is64Bits>
783 ::getRelocationAdditionalInfo(DataRefImpl Rel,
784 int64_t &Result) const {
785 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
786 switch (sec->sh_type) {
787 default :
788 report_fatal_error("Invalid section type in Rel!");
789 case ELF::SHT_REL : {
790 Result = 0;
791 return object_error::success;
792 }
793 case ELF::SHT_RELA : {
794 Result = getRela(Rel)->r_addend;
795 return object_error::success;
796 }
797 }
798}
799
800
801
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000802template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +0000803ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
804 , error_code &ec)
805 : ObjectFile(Binary::isELF, Object, ec)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000806 , SectionHeaderTable(0)
807 , dot_shstrtab_sec(0)
808 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000809 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000810
811 if (Header->e_shoff == 0)
812 return;
813
814 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000815 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000816 uint32_t SectionTableSize = Header->e_shnum * Header->e_shentsize;
817 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize
Michael J. Spencer001c9202011-06-25 17:54:50 +0000818 <= base() + Data->getBufferSize()))
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000819 // FIXME: Proper error handling.
820 report_fatal_error("Section table goes past end of file!");
821
822
823 // To find the symbol tables we walk the section table to find SHT_STMTAB.
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000824 const Elf_Shdr* sh =
825 reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
826 for (unsigned i = 0; i < Header->e_shnum; ++i) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000827 if (sh->sh_type == ELF::SHT_SYMTAB) {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000828 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000829 SymbolTableSections.push_back(sh);
830 }
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000831 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
832 RelocationTableSections.push_back(sh);
833 }
834 ++sh;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000835 }
836
837 // Get string table sections.
838 dot_shstrtab_sec = getSection(Header->e_shstrndx);
839 if (dot_shstrtab_sec) {
840 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000841 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000842 [dot_shstrtab_sec->sh_size - 1] != 0)
843 // FIXME: Proper error handling.
844 report_fatal_error("String table must end with a null terminator!");
845 }
846
847 // Merge this into the above loop.
848 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
849 *e = i + Header->e_shnum * Header->e_shentsize;
850 i != e; i += Header->e_shentsize) {
851 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
852 if (sh->sh_type == ELF::SHT_STRTAB) {
853 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
854 if (SectionName == ".strtab") {
855 if (dot_strtab_sec != 0)
856 // FIXME: Proper error handling.
857 report_fatal_error("Already found section named .strtab!");
858 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +0000859 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000860 if (dot_strtab[sh->sh_size - 1] != 0)
861 // FIXME: Proper error handling.
862 report_fatal_error("String table must end with a null terminator!");
863 }
864 }
865 }
866}
867
868template<support::endianness target_endianness, bool is64Bits>
869ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
870 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000871 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000872 memset(&SymbolData, 0, sizeof(SymbolData));
873 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000874 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
875 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000876 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000877 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
878 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000879 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000880 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000881}
882
883template<support::endianness target_endianness, bool is64Bits>
884ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
885 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000886 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000887 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000888 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
889 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
890 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000891}
892
893template<support::endianness target_endianness, bool is64Bits>
894ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
895 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000896 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000897 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +0000898 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000899 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000900}
901
902template<support::endianness target_endianness, bool is64Bits>
903ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
904 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000905 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000906 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +0000907 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000908 + Header->e_shoff
909 + (Header->e_shentsize * Header->e_shnum));
910 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000911}
912
913template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000914ObjectFile::relocation_iterator ELFObjectFile<target_endianness, is64Bits>
915 ::begin_relocations() const {
916 DataRefImpl RelData;
917 memset(&RelData, 0, sizeof(RelData));
918 if (RelocationTableSections.size() == 0) {
919 RelData.d.a = std::numeric_limits<uint32_t>::max();
920 RelData.d.b = std::numeric_limits<uint32_t>::max();
921 } else {
922 RelData.d.a = 0;
923 RelData.d.b = 0;
924 }
925 return relocation_iterator(RelocationRef(RelData, this));
926}
927
928template<support::endianness target_endianness, bool is64Bits>
929ObjectFile::relocation_iterator ELFObjectFile<target_endianness, is64Bits>
930 ::end_relocations() const {
931 DataRefImpl RelData;
932 memset(&RelData, 0, sizeof(RelData));
933 RelData.d.a = std::numeric_limits<uint32_t>::max();
934 RelData.d.b = std::numeric_limits<uint32_t>::max();
935 return relocation_iterator(RelocationRef(RelData, this));
936}
937
938template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000939uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000940 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000941}
942
943template<support::endianness target_endianness, bool is64Bits>
944StringRef ELFObjectFile<target_endianness, is64Bits>
945 ::getFileFormatName() const {
946 switch(Header->e_ident[ELF::EI_CLASS]) {
947 case ELF::ELFCLASS32:
948 switch(Header->e_machine) {
949 case ELF::EM_386:
950 return "ELF32-i386";
951 case ELF::EM_X86_64:
952 return "ELF32-x86-64";
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000953 case ELF::EM_ARM:
954 return "ELF32-arm";
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000955 default:
956 return "ELF32-unknown";
957 }
958 case ELF::ELFCLASS64:
959 switch(Header->e_machine) {
960 case ELF::EM_386:
961 return "ELF64-i386";
962 case ELF::EM_X86_64:
963 return "ELF64-x86-64";
964 default:
965 return "ELF64-unknown";
966 }
967 default:
968 // FIXME: Proper error handling.
969 report_fatal_error("Invalid ELFCLASS!");
970 }
971}
972
973template<support::endianness target_endianness, bool is64Bits>
974unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
975 switch(Header->e_machine) {
976 case ELF::EM_386:
977 return Triple::x86;
978 case ELF::EM_X86_64:
979 return Triple::x86_64;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000980 case ELF::EM_ARM:
981 return Triple::arm;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000982 default:
983 return Triple::UnknownArch;
984 }
985}
986
987template<support::endianness target_endianness, bool is64Bits>
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000988template<typename T>
989inline const T *
990ELFObjectFile<target_endianness, is64Bits>::getEntry(DataRefImpl Entry,
991 Sections_t Sections) const {
992 const Elf_Shdr *sec = Sections[Entry.d.b];
993 return reinterpret_cast<const T *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +0000994 base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000995 + sec->sh_offset
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000996 + (Entry.d.a * sec->sh_entsize));
997}
998
999template<support::endianness target_endianness, bool is64Bits>
1000const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1001ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
1002 return getEntry<Elf_Sym>(Symb, SymbolTableSections);
1003}
1004
1005template<support::endianness target_endianness, bool is64Bits>
1006const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1007ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
1008 return getEntry<Elf_Rel>(Rel, RelocationTableSections);
1009}
1010
1011template<support::endianness target_endianness, bool is64Bits>
1012const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1013ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
1014 return getEntry<Elf_Rela>(Rela, RelocationTableSections);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001015}
1016
1017template<support::endianness target_endianness, bool is64Bits>
1018const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1019ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +00001020 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001021 if (sec->sh_type != ELF::SHT_SYMTAB)
1022 // FIXME: Proper error handling.
1023 report_fatal_error("Invalid symbol table section!");
1024 return sec;
1025}
1026
1027template<support::endianness target_endianness, bool is64Bits>
1028const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1029ELFObjectFile<target_endianness, is64Bits>::getSection(uint16_t index) const {
1030 if (index == 0 || index >= ELF::SHN_LORESERVE)
1031 return 0;
1032 if (!SectionHeaderTable || index >= Header->e_shnum)
1033 // FIXME: Proper error handling.
1034 report_fatal_error("Invalid section index!");
1035
1036 return reinterpret_cast<const Elf_Shdr *>(
1037 reinterpret_cast<const char *>(SectionHeaderTable)
1038 + (index * Header->e_shentsize));
1039}
1040
1041template<support::endianness target_endianness, bool is64Bits>
1042const char *ELFObjectFile<target_endianness, is64Bits>
1043 ::getString(uint16_t section,
1044 ELF::Elf32_Word offset) const {
1045 return getString(getSection(section), offset);
1046}
1047
1048template<support::endianness target_endianness, bool is64Bits>
1049const char *ELFObjectFile<target_endianness, is64Bits>
1050 ::getString(const Elf_Shdr *section,
1051 ELF::Elf32_Word offset) const {
1052 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1053 if (offset >= section->sh_size)
1054 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +00001055 report_fatal_error("Symbol name offset outside of string table!");
1056 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001057}
1058
1059// EI_CLASS, EI_DATA.
1060static std::pair<unsigned char, unsigned char>
1061getElfArchType(MemoryBuffer *Object) {
1062 if (Object->getBufferSize() < ELF::EI_NIDENT)
1063 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1064 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1065 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1066}
1067
1068namespace llvm {
1069
1070 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
1071 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +00001072 error_code ec;
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001073 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001074 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001075 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001076 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001077 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001078 return new ELFObjectFile<support::little, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001079 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +00001080 return new ELFObjectFile<support::big, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +00001081 // FIXME: Proper error handling.
1082 report_fatal_error("Not an ELF object file!");
1083 }
1084
1085} // end namespace llvm