blob: edf9824d3dfd26b4d440c976a3fb0939ce378905 [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"
17#include "llvm/Object/ObjectFile.h"
18#include "llvm/Support/ELF.h"
19#include "llvm/Support/Endian.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include <limits>
23#include <utility>
24
25using namespace llvm;
26using namespace object;
27
28// Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
29namespace {
30template<support::endianness target_endianness>
31struct ELFDataTypeTypedefHelperCommon {
32 typedef support::detail::packed_endian_specific_integral
33 <uint16_t, target_endianness, support::aligned> Elf_Half;
34 typedef support::detail::packed_endian_specific_integral
35 <uint32_t, target_endianness, support::aligned> Elf_Word;
36 typedef support::detail::packed_endian_specific_integral
37 <int32_t, target_endianness, support::aligned> Elf_Sword;
38 typedef support::detail::packed_endian_specific_integral
39 <uint64_t, target_endianness, support::aligned> Elf_Xword;
40 typedef support::detail::packed_endian_specific_integral
41 <int64_t, target_endianness, support::aligned> Elf_Sxword;
42};
43}
44
45namespace {
46template<support::endianness target_endianness, bool is64Bits>
47struct ELFDataTypeTypedefHelper;
48
49/// ELF 32bit types.
50template<support::endianness target_endianness>
51struct ELFDataTypeTypedefHelper<target_endianness, false>
52 : ELFDataTypeTypedefHelperCommon<target_endianness> {
53 typedef support::detail::packed_endian_specific_integral
54 <uint32_t, target_endianness, support::aligned> Elf_Addr;
55 typedef support::detail::packed_endian_specific_integral
56 <uint32_t, target_endianness, support::aligned> Elf_Off;
57};
58
59/// ELF 64bit types.
60template<support::endianness target_endianness>
61struct ELFDataTypeTypedefHelper<target_endianness, true>
62 : ELFDataTypeTypedefHelperCommon<target_endianness>{
63 typedef support::detail::packed_endian_specific_integral
64 <uint64_t, target_endianness, support::aligned> Elf_Addr;
65 typedef support::detail::packed_endian_specific_integral
66 <uint64_t, target_endianness, support::aligned> Elf_Off;
67};
68}
69
70// I really don't like doing this, but the alternative is copypasta.
71#define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
72typedef typename \
73 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
74typedef typename \
75 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
76typedef typename \
77 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
78typedef typename \
79 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
80typedef typename \
81 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
82typedef typename \
83 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
84typedef typename \
85 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
86
87 // Section header.
88namespace {
89template<support::endianness target_endianness, bool is64Bits>
90struct Elf_Shdr_Base;
91
92template<support::endianness target_endianness>
93struct Elf_Shdr_Base<target_endianness, false> {
94 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
95 Elf_Word sh_name; // Section name (index into string table)
96 Elf_Word sh_type; // Section type (SHT_*)
97 Elf_Word sh_flags; // Section flags (SHF_*)
98 Elf_Addr sh_addr; // Address where section is to be loaded
99 Elf_Off sh_offset; // File offset of section data, in bytes
100 Elf_Word sh_size; // Size of section, in bytes
101 Elf_Word sh_link; // Section type-specific header table index link
102 Elf_Word sh_info; // Section type-specific extra information
103 Elf_Word sh_addralign;// Section address alignment
104 Elf_Word sh_entsize; // Size of records contained within the section
105};
106
107template<support::endianness target_endianness>
108struct Elf_Shdr_Base<target_endianness, true> {
109 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
110 Elf_Word sh_name; // Section name (index into string table)
111 Elf_Word sh_type; // Section type (SHT_*)
112 Elf_Xword sh_flags; // Section flags (SHF_*)
113 Elf_Addr sh_addr; // Address where section is to be loaded
114 Elf_Off sh_offset; // File offset of section data, in bytes
115 Elf_Xword sh_size; // Size of section, in bytes
116 Elf_Word sh_link; // Section type-specific header table index link
117 Elf_Word sh_info; // Section type-specific extra information
118 Elf_Xword sh_addralign;// Section address alignment
119 Elf_Xword sh_entsize; // Size of records contained within the section
120};
121
122template<support::endianness target_endianness, bool is64Bits>
123struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> {
124 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize;
125 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size;
126
127 /// @brief Get the number of entities this section contains if it has any.
128 unsigned getEntityCount() const {
129 if (sh_entsize == 0)
130 return 0;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000131 return sh_size / sh_entsize;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000132 }
133};
134}
135
136namespace {
137template<support::endianness target_endianness, bool is64Bits>
138struct Elf_Sym_Base;
139
140template<support::endianness target_endianness>
141struct Elf_Sym_Base<target_endianness, false> {
142 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
143 Elf_Word st_name; // Symbol name (index into string table)
144 Elf_Addr st_value; // Value or address associated with the symbol
145 Elf_Word st_size; // Size of the symbol
146 unsigned char st_info; // Symbol's type and binding attributes
147 unsigned char st_other; // Must be zero; reserved
148 Elf_Half st_shndx; // Which section (header table index) it's defined in
149};
150
151template<support::endianness target_endianness>
152struct Elf_Sym_Base<target_endianness, true> {
153 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
154 Elf_Word st_name; // Symbol name (index into string table)
155 unsigned char st_info; // Symbol's type and binding attributes
156 unsigned char st_other; // Must be zero; reserved
157 Elf_Half st_shndx; // Which section (header table index) it's defined in
158 Elf_Addr st_value; // Value or address associated with the symbol
159 Elf_Xword st_size; // Size of the symbol
160};
161
162template<support::endianness target_endianness, bool is64Bits>
163struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> {
164 using Elf_Sym_Base<target_endianness, is64Bits>::st_info;
165
166 // These accessors and mutators correspond to the ELF32_ST_BIND,
167 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
168 unsigned char getBinding() const { return st_info >> 4; }
169 unsigned char getType() const { return st_info & 0x0f; }
170 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
171 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
172 void setBindingAndType(unsigned char b, unsigned char t) {
173 st_info = (b << 4) + (t & 0x0f);
174 }
175};
176}
177
178namespace {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000179template<support::endianness target_endianness, bool is64Bits>
180class ELFObjectFile : public ObjectFile {
181 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
182
183 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
184 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
185
186 struct Elf_Ehdr {
187 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
188 Elf_Half e_type; // Type of file (see ET_*)
189 Elf_Half e_machine; // Required architecture for this file (see EM_*)
190 Elf_Word e_version; // Must be equal to 1
191 Elf_Addr e_entry; // Address to jump to in order to start program
192 Elf_Off e_phoff; // Program header table's file offset, in bytes
193 Elf_Off e_shoff; // Section header table's file offset, in bytes
194 Elf_Word e_flags; // Processor-specific flags
195 Elf_Half e_ehsize; // Size of ELF header, in bytes
196 Elf_Half e_phentsize;// Size of an entry in the program header table
197 Elf_Half e_phnum; // Number of entries in the program header table
198 Elf_Half e_shentsize;// Size of an entry in the section header table
199 Elf_Half e_shnum; // Number of entries in the section header table
200 Elf_Half e_shstrndx; // Section header table index of section name
201 // string table
202 bool checkMagic() const {
203 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
204 }
205 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
206 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
207 };
208
209 typedef SmallVector<const Elf_Shdr*, 1> SymbolTableSections_t;
210
211 const Elf_Ehdr *Header;
212 const Elf_Shdr *SectionHeaderTable;
213 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
214 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
215 SymbolTableSections_t SymbolTableSections;
216
217 void validateSymbol(DataRefImpl Symb) const;
218 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
219 const Elf_Shdr *getSection(DataRefImpl index) const;
220 const Elf_Shdr *getSection(uint16_t index) const;
221 const char *getString(uint16_t section, uint32_t offset) const;
222 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
223
224protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000225 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
226 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
227 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
228 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
229 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
230 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000231
Michael J. Spencer25b15772011-06-25 17:55:23 +0000232 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
233 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
234 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
235 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
236 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
237 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000238
239public:
Michael J. Spencer001c9202011-06-25 17:54:50 +0000240 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000241 virtual symbol_iterator begin_symbols() const;
242 virtual symbol_iterator end_symbols() const;
243 virtual section_iterator begin_sections() const;
244 virtual section_iterator end_sections() const;
245
246 virtual uint8_t getBytesInAddress() const;
247 virtual StringRef getFileFormatName() const;
248 virtual unsigned getArch() const;
249};
250} // end namespace
251
252template<support::endianness target_endianness, bool is64Bits>
253void ELFObjectFile<target_endianness, is64Bits>
254 ::validateSymbol(DataRefImpl Symb) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000255 const Elf_Sym *symb = getSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000256 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000257 // FIXME: We really need to do proper error handling in the case of an invalid
258 // input file. Because we don't use exceptions, I think we'll just pass
259 // an error object around.
260 if (!( symb
261 && SymbolTableSection
Michael J. Spencer001c9202011-06-25 17:54:50 +0000262 && symb >= (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000263 + SymbolTableSection->sh_offset)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000264 && symb < (const Elf_Sym*)(base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000265 + SymbolTableSection->sh_offset
266 + SymbolTableSection->sh_size)))
267 // FIXME: Proper error handling.
268 report_fatal_error("Symb must point to a valid symbol!");
269}
270
271template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000272error_code ELFObjectFile<target_endianness, is64Bits>
273 ::getSymbolNext(DataRefImpl Symb,
274 SymbolRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000275 validateSymbol(Symb);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000276 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000277
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000278 ++Symb.d.a;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000279 // Check to see if we are at the end of this symbol table.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000280 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000281 // We are at the end. If there are other symbol tables, jump to them.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000282 ++Symb.d.b;
283 Symb.d.a = 1; // The 0th symbol in ELF is fake.
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000284 // Otherwise return the terminator.
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000285 if (Symb.d.b >= SymbolTableSections.size()) {
286 Symb.d.a = std::numeric_limits<uint32_t>::max();
287 Symb.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000288 }
289 }
290
Michael J. Spencer25b15772011-06-25 17:55:23 +0000291 Result = SymbolRef(Symb, this);
292 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000293}
294
295template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000296error_code ELFObjectFile<target_endianness, is64Bits>
297 ::getSymbolName(DataRefImpl Symb,
298 StringRef &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000299 validateSymbol(Symb);
300 const Elf_Sym *symb = getSymbol(Symb);
301 if (symb->st_name == 0) {
302 const Elf_Shdr *section = getSection(symb->st_shndx);
303 if (!section)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000304 Result = "";
305 else
306 Result = getString(dot_shstrtab_sec, section->sh_name);
307 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000308 }
309
310 // Use the default symbol table name section.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000311 Result = getString(dot_strtab_sec, symb->st_name);
312 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000313}
314
315template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000316error_code ELFObjectFile<target_endianness, is64Bits>
317 ::getSymbolAddress(DataRefImpl Symb,
318 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000319 validateSymbol(Symb);
320 const Elf_Sym *symb = getSymbol(Symb);
321 const Elf_Shdr *Section;
322 switch (symb->st_shndx) {
323 case ELF::SHN_COMMON:
324 // Undefined symbols have no address yet.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000325 case ELF::SHN_UNDEF:
326 Result = UnknownAddressOrSize;
327 return object_error::success;
328 case ELF::SHN_ABS:
329 Result = symb->st_value;
330 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000331 default: Section = getSection(symb->st_shndx);
332 }
333
334 switch (symb->getType()) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000335 case ELF::STT_SECTION:
336 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
337 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000338 case ELF::STT_FUNC:
339 case ELF::STT_OBJECT:
340 case ELF::STT_NOTYPE:
Michael J. Spencer25b15772011-06-25 17:55:23 +0000341 Result = symb->st_value;
342 return object_error::success;
343 default:
344 Result = UnknownAddressOrSize;
345 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000346 }
347}
348
349template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000350error_code ELFObjectFile<target_endianness, is64Bits>
351 ::getSymbolSize(DataRefImpl Symb,
352 uint64_t &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000353 validateSymbol(Symb);
354 const Elf_Sym *symb = getSymbol(Symb);
355 if (symb->st_size == 0)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000356 Result = UnknownAddressOrSize;
357 Result = symb->st_size;
358 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000359}
360
361template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000362error_code ELFObjectFile<target_endianness, is64Bits>
363 ::getSymbolNMTypeChar(DataRefImpl Symb,
364 char &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000365 validateSymbol(Symb);
366 const Elf_Sym *symb = getSymbol(Symb);
367 const Elf_Shdr *Section = getSection(symb->st_shndx);
368
369 char ret = '?';
370
371 if (Section) {
372 switch (Section->sh_type) {
373 case ELF::SHT_PROGBITS:
374 case ELF::SHT_DYNAMIC:
375 switch (Section->sh_flags) {
376 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
377 ret = 't'; break;
378 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
379 ret = 'd'; break;
380 case ELF::SHF_ALLOC:
381 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
382 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
383 ret = 'r'; break;
384 }
385 break;
386 case ELF::SHT_NOBITS: ret = 'b';
387 }
388 }
389
390 switch (symb->st_shndx) {
391 case ELF::SHN_UNDEF:
392 if (ret == '?')
393 ret = 'U';
394 break;
395 case ELF::SHN_ABS: ret = 'a'; break;
396 case ELF::SHN_COMMON: ret = 'c'; break;
397 }
398
399 switch (symb->getBinding()) {
400 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
401 case ELF::STB_WEAK:
402 if (symb->st_shndx == ELF::SHN_UNDEF)
403 ret = 'w';
404 else
405 if (symb->getType() == ELF::STT_OBJECT)
406 ret = 'V';
407 else
408 ret = 'W';
409 }
410
Michael J. Spencer25b15772011-06-25 17:55:23 +0000411 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
412 StringRef name;
413 if (error_code ec = getSymbolName(Symb, name))
414 return ec;
415 Result = StringSwitch<char>(name)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000416 .StartsWith(".debug", 'N')
417 .StartsWith(".note", 'n');
Michael J. Spencer25b15772011-06-25 17:55:23 +0000418 return object_error::success;
419 }
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000420
Michael J. Spencer25b15772011-06-25 17:55:23 +0000421 Result = ret;
422 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000423}
424
425template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000426error_code ELFObjectFile<target_endianness, is64Bits>
427 ::isSymbolInternal(DataRefImpl Symb,
428 bool &Result) const {
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000429 validateSymbol(Symb);
430 const Elf_Sym *symb = getSymbol(Symb);
431
432 if ( symb->getType() == ELF::STT_FILE
433 || symb->getType() == ELF::STT_SECTION)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000434 Result = true;
435 Result = false;
436 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000437}
438
439template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000440error_code ELFObjectFile<target_endianness, is64Bits>
441 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000442 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000443 sec += Header->e_shentsize;
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000444 Sec.p = reinterpret_cast<intptr_t>(sec);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000445 Result = SectionRef(Sec, this);
446 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000447}
448
449template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000450error_code ELFObjectFile<target_endianness, is64Bits>
451 ::getSectionName(DataRefImpl Sec,
452 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000453 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000454 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
455 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000456}
457
458template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000459error_code ELFObjectFile<target_endianness, is64Bits>
460 ::getSectionAddress(DataRefImpl Sec,
461 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000462 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000463 Result = sec->sh_addr;
464 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000465}
466
467template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000468error_code ELFObjectFile<target_endianness, is64Bits>
469 ::getSectionSize(DataRefImpl Sec,
470 uint64_t &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000471 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000472 Result = sec->sh_size;
473 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000474}
475
476template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000477error_code ELFObjectFile<target_endianness, is64Bits>
478 ::getSectionContents(DataRefImpl Sec,
479 StringRef &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000480 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000481 const char *start = (const char*)base() + sec->sh_offset;
482 Result = StringRef(start, sec->sh_size);
483 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000484}
485
486template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer25b15772011-06-25 17:55:23 +0000487error_code ELFObjectFile<target_endianness, is64Bits>
488 ::isSectionText(DataRefImpl Sec,
489 bool &Result) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000490 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000491 if (sec->sh_flags & ELF::SHF_EXECINSTR)
Michael J. Spencer25b15772011-06-25 17:55:23 +0000492 Result = true;
493 else
494 Result = false;
495 return object_error::success;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000496}
497
498template<support::endianness target_endianness, bool is64Bits>
Michael J. Spencer001c9202011-06-25 17:54:50 +0000499ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
500 , error_code &ec)
501 : ObjectFile(Binary::isELF, Object, ec)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000502 , SectionHeaderTable(0)
503 , dot_shstrtab_sec(0)
504 , dot_strtab_sec(0) {
Michael J. Spencer001c9202011-06-25 17:54:50 +0000505 Header = reinterpret_cast<const Elf_Ehdr *>(base());
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000506
507 if (Header->e_shoff == 0)
508 return;
509
510 SectionHeaderTable =
Michael J. Spencer001c9202011-06-25 17:54:50 +0000511 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000512 uint32_t SectionTableSize = Header->e_shnum * Header->e_shentsize;
513 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize
Michael J. Spencer001c9202011-06-25 17:54:50 +0000514 <= base() + Data->getBufferSize()))
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000515 // FIXME: Proper error handling.
516 report_fatal_error("Section table goes past end of file!");
517
518
519 // To find the symbol tables we walk the section table to find SHT_STMTAB.
520 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
521 *e = i + Header->e_shnum * Header->e_shentsize;
522 i != e; i += Header->e_shentsize) {
523 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
524 if (sh->sh_type == ELF::SHT_SYMTAB) {
525 SymbolTableSections.push_back(sh);
526 }
527 }
528
529 // Get string table sections.
530 dot_shstrtab_sec = getSection(Header->e_shstrndx);
531 if (dot_shstrtab_sec) {
532 // Verify that the last byte in the string table in a null.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000533 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000534 [dot_shstrtab_sec->sh_size - 1] != 0)
535 // FIXME: Proper error handling.
536 report_fatal_error("String table must end with a null terminator!");
537 }
538
539 // Merge this into the above loop.
540 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
541 *e = i + Header->e_shnum * Header->e_shentsize;
542 i != e; i += Header->e_shentsize) {
543 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
544 if (sh->sh_type == ELF::SHT_STRTAB) {
545 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
546 if (SectionName == ".strtab") {
547 if (dot_strtab_sec != 0)
548 // FIXME: Proper error handling.
549 report_fatal_error("Already found section named .strtab!");
550 dot_strtab_sec = sh;
Michael J. Spencer001c9202011-06-25 17:54:50 +0000551 const char *dot_strtab = (const char*)base() + sh->sh_offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000552 if (dot_strtab[sh->sh_size - 1] != 0)
553 // FIXME: Proper error handling.
554 report_fatal_error("String table must end with a null terminator!");
555 }
556 }
557 }
558}
559
560template<support::endianness target_endianness, bool is64Bits>
561ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
562 ::begin_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000563 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000564 memset(&SymbolData, 0, sizeof(SymbolData));
565 if (SymbolTableSections.size() == 0) {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000566 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
567 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000568 } else {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000569 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
570 SymbolData.d.b = 0;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000571 }
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000572 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000573}
574
575template<support::endianness target_endianness, bool is64Bits>
576ObjectFile::symbol_iterator ELFObjectFile<target_endianness, is64Bits>
577 ::end_symbols() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000578 DataRefImpl SymbolData;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000579 memset(&SymbolData, 0, sizeof(SymbolData));
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000580 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
581 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
582 return symbol_iterator(SymbolRef(SymbolData, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000583}
584
585template<support::endianness target_endianness, bool is64Bits>
586ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
587 ::begin_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000588 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000589 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +0000590 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000591 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000592}
593
594template<support::endianness target_endianness, bool is64Bits>
595ObjectFile::section_iterator ELFObjectFile<target_endianness, is64Bits>
596 ::end_sections() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000597 DataRefImpl ret;
Eric Christopher539d8d82011-04-03 22:53:19 +0000598 memset(&ret, 0, sizeof(DataRefImpl));
Michael J. Spencer001c9202011-06-25 17:54:50 +0000599 ret.p = reinterpret_cast<intptr_t>(base()
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000600 + Header->e_shoff
601 + (Header->e_shentsize * Header->e_shnum));
602 return section_iterator(SectionRef(ret, this));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000603}
604
605template<support::endianness target_endianness, bool is64Bits>
606uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000607 return is64Bits ? 8 : 4;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000608}
609
610template<support::endianness target_endianness, bool is64Bits>
611StringRef ELFObjectFile<target_endianness, is64Bits>
612 ::getFileFormatName() const {
613 switch(Header->e_ident[ELF::EI_CLASS]) {
614 case ELF::ELFCLASS32:
615 switch(Header->e_machine) {
616 case ELF::EM_386:
617 return "ELF32-i386";
618 case ELF::EM_X86_64:
619 return "ELF32-x86-64";
620 default:
621 return "ELF32-unknown";
622 }
623 case ELF::ELFCLASS64:
624 switch(Header->e_machine) {
625 case ELF::EM_386:
626 return "ELF64-i386";
627 case ELF::EM_X86_64:
628 return "ELF64-x86-64";
629 default:
630 return "ELF64-unknown";
631 }
632 default:
633 // FIXME: Proper error handling.
634 report_fatal_error("Invalid ELFCLASS!");
635 }
636}
637
638template<support::endianness target_endianness, bool is64Bits>
639unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
640 switch(Header->e_machine) {
641 case ELF::EM_386:
642 return Triple::x86;
643 case ELF::EM_X86_64:
644 return Triple::x86_64;
645 default:
646 return Triple::UnknownArch;
647 }
648}
649
650template<support::endianness target_endianness, bool is64Bits>
651const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
652ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000653 const Elf_Shdr *sec = SymbolTableSections[Symb.d.b];
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000654 return reinterpret_cast<const Elf_Sym *>(
Michael J. Spencer001c9202011-06-25 17:54:50 +0000655 base()
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000656 + sec->sh_offset
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000657 + (Symb.d.a * sec->sh_entsize));
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000658}
659
660template<support::endianness target_endianness, bool is64Bits>
661const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
662ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
Michael J. Spencer7acdb4d2011-01-21 02:27:02 +0000663 const Elf_Shdr *sec = getSection(Symb.d.b);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000664 if (sec->sh_type != ELF::SHT_SYMTAB)
665 // FIXME: Proper error handling.
666 report_fatal_error("Invalid symbol table section!");
667 return sec;
668}
669
670template<support::endianness target_endianness, bool is64Bits>
671const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
672ELFObjectFile<target_endianness, is64Bits>::getSection(uint16_t index) const {
673 if (index == 0 || index >= ELF::SHN_LORESERVE)
674 return 0;
675 if (!SectionHeaderTable || index >= Header->e_shnum)
676 // FIXME: Proper error handling.
677 report_fatal_error("Invalid section index!");
678
679 return reinterpret_cast<const Elf_Shdr *>(
680 reinterpret_cast<const char *>(SectionHeaderTable)
681 + (index * Header->e_shentsize));
682}
683
684template<support::endianness target_endianness, bool is64Bits>
685const char *ELFObjectFile<target_endianness, is64Bits>
686 ::getString(uint16_t section,
687 ELF::Elf32_Word offset) const {
688 return getString(getSection(section), offset);
689}
690
691template<support::endianness target_endianness, bool is64Bits>
692const char *ELFObjectFile<target_endianness, is64Bits>
693 ::getString(const Elf_Shdr *section,
694 ELF::Elf32_Word offset) const {
695 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
696 if (offset >= section->sh_size)
697 // FIXME: Proper error handling.
Michael J. Spencer001c9202011-06-25 17:54:50 +0000698 report_fatal_error("Symbol name offset outside of string table!");
699 return (const char *)base() + section->sh_offset + offset;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000700}
701
702// EI_CLASS, EI_DATA.
703static std::pair<unsigned char, unsigned char>
704getElfArchType(MemoryBuffer *Object) {
705 if (Object->getBufferSize() < ELF::EI_NIDENT)
706 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
707 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
708 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
709}
710
711namespace llvm {
712
713 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
714 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
Michael J. Spencer001c9202011-06-25 17:54:50 +0000715 error_code ec;
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000716 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000717 return new ELFObjectFile<support::little, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000718 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000719 return new ELFObjectFile<support::big, false>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000720 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000721 return new ELFObjectFile<support::little, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000722 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
Michael J. Spencer001c9202011-06-25 17:54:50 +0000723 return new ELFObjectFile<support::big, true>(Object, ec);
Michael J. Spencerb84551a2011-01-20 06:38:47 +0000724 // FIXME: Proper error handling.
725 report_fatal_error("Not an ELF object file!");
726 }
727
728} // end namespace llvm