blob: 1568639c8110cf4eed6d2f0b8b53c220ed76802c [file] [log] [blame]
Stephen Wilsonddd29622010-07-13 23:07:23 +00001//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
Chris Lattner24943d22010-06-08 16:52:24 +00002//
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#include "ObjectFileELF.h"
11
Stephen Wilsonddd29622010-07-13 23:07:23 +000012#include <cassert>
Chris Lattner24943d22010-06-08 16:52:24 +000013#include <algorithm>
14
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/DataBuffer.h"
16#include "lldb/Core/Error.h"
Stephen Wilsonddd29622010-07-13 23:07:23 +000017#include "lldb/Core/FileSpecList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/PluginManager.h"
19#include "lldb/Core/Section.h"
20#include "lldb/Core/Stream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
Stephen Wilsonddd29622010-07-13 23:07:23 +000022#define CASE_AND_STREAM(s, def, width) \
23 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner24943d22010-06-08 16:52:24 +000024
Chris Lattner24943d22010-06-08 16:52:24 +000025using namespace lldb;
26using namespace lldb_private;
Stephen Wilsonddd29622010-07-13 23:07:23 +000027using namespace elf;
28using namespace llvm::ELF;
Chris Lattner24943d22010-06-08 16:52:24 +000029
Stephen Wilsonddd29622010-07-13 23:07:23 +000030//------------------------------------------------------------------
31// Static methods.
32//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +000033void
34ObjectFileELF::Initialize()
35{
Stephen Wilsonddd29622010-07-13 23:07:23 +000036 PluginManager::RegisterPlugin(GetPluginNameStatic(),
37 GetPluginDescriptionStatic(),
38 CreateInstance);
Chris Lattner24943d22010-06-08 16:52:24 +000039}
40
41void
42ObjectFileELF::Terminate()
43{
Stephen Wilsonddd29622010-07-13 23:07:23 +000044 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner24943d22010-06-08 16:52:24 +000045}
46
Chris Lattner24943d22010-06-08 16:52:24 +000047const char *
48ObjectFileELF::GetPluginNameStatic()
49{
Stephen Wilsonddd29622010-07-13 23:07:23 +000050 return "object-file.elf";
Chris Lattner24943d22010-06-08 16:52:24 +000051}
52
53const char *
54ObjectFileELF::GetPluginDescriptionStatic()
55{
Stephen Wilsonddd29622010-07-13 23:07:23 +000056 return "ELF object file reader.";
Chris Lattner24943d22010-06-08 16:52:24 +000057}
58
Chris Lattner24943d22010-06-08 16:52:24 +000059ObjectFile *
Stephen Wilsonddd29622010-07-13 23:07:23 +000060ObjectFileELF::CreateInstance(Module *module,
61 DataBufferSP &data_sp,
62 const FileSpec *file, addr_t offset,
63 addr_t length)
Chris Lattner24943d22010-06-08 16:52:24 +000064{
Stephen Wilsonddd29622010-07-13 23:07:23 +000065 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
Chris Lattner24943d22010-06-08 16:52:24 +000066 {
Stephen Wilsonddd29622010-07-13 23:07:23 +000067 const uint8_t *magic = data_sp->GetBytes() + offset;
68 if (ELFHeader::MagicBytesMatch(magic))
69 {
70 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
71 if (address_size == 4 || address_size == 8)
72 {
73 std::auto_ptr<ObjectFile> objfile_ap(
74 new ObjectFileELF(module, data_sp, file, offset, length));
75 if (objfile_ap->ParseHeader())
76 return objfile_ap.release();
77 }
78 }
Chris Lattner24943d22010-06-08 16:52:24 +000079 }
80 return NULL;
81}
82
Stephen Wilsonddd29622010-07-13 23:07:23 +000083//------------------------------------------------------------------
84// PluginInterface protocol
85//------------------------------------------------------------------
86const char *
87ObjectFileELF::GetPluginName()
Chris Lattner24943d22010-06-08 16:52:24 +000088{
Stephen Wilsonddd29622010-07-13 23:07:23 +000089 return "ObjectFileELF";
Chris Lattner24943d22010-06-08 16:52:24 +000090}
91
Stephen Wilsonddd29622010-07-13 23:07:23 +000092const char *
93ObjectFileELF::GetShortPluginName()
94{
95 return GetPluginNameStatic();
96}
Chris Lattner24943d22010-06-08 16:52:24 +000097
Stephen Wilsonddd29622010-07-13 23:07:23 +000098uint32_t
99ObjectFileELF::GetPluginVersion()
100{
101 return m_plugin_version;
102}
103
104void
105ObjectFileELF::GetPluginCommandHelp(const char *command, Stream *strm)
106{
107}
108
109Error
110ObjectFileELF::ExecutePluginCommand(Args &command, Stream *strm)
111{
112 Error error;
113 error.SetErrorString("No plug-in commands are currently supported.");
114 return error;
115}
116
117Log *
118ObjectFileELF::EnablePluginLogging(Stream *strm, Args &command)
119{
120 return NULL;
121}
122
123//------------------------------------------------------------------
124// ObjectFile protocol
125//------------------------------------------------------------------
126
127ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
128 const FileSpec* file, addr_t offset,
129 addr_t length)
130 : ObjectFile(module, file, offset, length, dataSP),
131 m_header(),
132 m_program_headers(),
133 m_section_headers(),
134 m_sections_ap(),
135 m_symtab_ap(),
136 m_filespec_ap(),
137 m_shstr_data()
Chris Lattner24943d22010-06-08 16:52:24 +0000138{
139 if (file)
140 m_file = *file;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000141 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner24943d22010-06-08 16:52:24 +0000142}
143
Chris Lattner24943d22010-06-08 16:52:24 +0000144ObjectFileELF::~ObjectFileELF()
145{
146}
147
Jim Ingham7508e732010-08-09 23:31:02 +0000148bool
149ObjectFileELF::IsExecutable() const
150{
151 // FIXME: How is this marked in ELF?
152 return false;
153}
154
Chris Lattner24943d22010-06-08 16:52:24 +0000155ByteOrder
Stephen Wilsonddd29622010-07-13 23:07:23 +0000156ObjectFileELF::GetByteOrder() const
Chris Lattner24943d22010-06-08 16:52:24 +0000157{
158 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
159 return eByteOrderBig;
160 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
161 return eByteOrderLittle;
162 return eByteOrderInvalid;
163}
164
165size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000166ObjectFileELF::GetAddressByteSize() const
Chris Lattner24943d22010-06-08 16:52:24 +0000167{
168 return m_data.GetAddressByteSize();
169}
170
Stephen Wilsonddd29622010-07-13 23:07:23 +0000171unsigned
172ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner24943d22010-06-08 16:52:24 +0000173{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000174 return std::distance(m_section_headers.begin(), I) + 1;
175}
Chris Lattner24943d22010-06-08 16:52:24 +0000176
Stephen Wilsonddd29622010-07-13 23:07:23 +0000177unsigned
178ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
179{
180 return std::distance(m_section_headers.begin(), I) + 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000181}
182
183bool
Stephen Wilsonddd29622010-07-13 23:07:23 +0000184ObjectFileELF::ParseHeader()
Chris Lattner24943d22010-06-08 16:52:24 +0000185{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000186 uint32_t offset = GetOffset();
187 return m_header.Parse(m_data, &offset);
188}
189
190bool
191ObjectFileELF::GetUUID(UUID* uuid)
192{
193 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner24943d22010-06-08 16:52:24 +0000194 return false;
195}
196
197uint32_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000198ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner24943d22010-06-08 16:52:24 +0000199{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000200 size_t num_modules = ParseDependentModules();
201 uint32_t num_specs = 0;
202
203 for (unsigned i = 0; i < num_modules; ++i)
204 {
205 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
206 num_specs++;
207 }
208
209 return num_specs;
210}
211
212//----------------------------------------------------------------------
213// ParseDependentModules
214//----------------------------------------------------------------------
215size_t
216ObjectFileELF::ParseDependentModules()
217{
218 if (m_filespec_ap.get())
219 return m_filespec_ap->GetSize();
220
221 m_filespec_ap.reset(new FileSpecList());
222
223 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
224 return 0;
225
226 // Locate the dynamic table.
227 user_id_t dynsym_id = 0;
228 user_id_t dynstr_id = 0;
229 for (SectionHeaderCollIter I = m_section_headers.begin();
230 I != m_section_headers.end(); ++I)
231 {
232 if (I->sh_type == SHT_DYNAMIC)
233 {
234 dynsym_id = SectionIndex(I);
235 dynstr_id = I->sh_link + 1; // Section ID's are 1 based.
236 break;
237 }
238 }
239
240 if (!(dynsym_id && dynstr_id))
241 return 0;
242
243 SectionList *section_list = GetSectionList();
244 if (!section_list)
245 return 0;
246
247 // Resolve and load the dynamic table entries and corresponding string
248 // table.
249 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
250 Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
251 if (!(dynsym && dynstr))
252 return 0;
253
254 DataExtractor dynsym_data;
255 DataExtractor dynstr_data;
256 if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) &&
257 dynstr->ReadSectionDataFromObjectFile(this, dynstr_data))
258 {
259 ELFDynamic symbol;
260 const unsigned section_size = dynsym_data.GetByteSize();
261 unsigned offset = 0;
262
263 // The only type of entries we are concerned with are tagged DT_NEEDED,
264 // yielding the name of a required library.
265 while (offset < section_size)
266 {
267 if (!symbol.Parse(dynsym_data, &offset))
268 break;
269
270 if (symbol.d_tag != DT_NEEDED)
271 continue;
272
273 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
274 const char *lib_name = dynstr_data.PeekCStr(str_index);
275 m_filespec_ap->Append(FileSpec(lib_name));
276 }
277 }
278
279 return m_filespec_ap->GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000280}
281
282//----------------------------------------------------------------------
283// ParseProgramHeaders
284//----------------------------------------------------------------------
285size_t
286ObjectFileELF::ParseProgramHeaders()
287{
288 // We have already parsed the program headers
289 if (!m_program_headers.empty())
290 return m_program_headers.size();
291
Stephen Wilsonddd29622010-07-13 23:07:23 +0000292 // If there are no program headers to read we are done.
293 if (m_header.e_phnum == 0)
294 return 0;
295
296 m_program_headers.resize(m_header.e_phnum);
297 if (m_program_headers.size() != m_header.e_phnum)
298 return 0;
299
300 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
301 const elf_off ph_offset = m_offset + m_header.e_phoff;
302 DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size));
303
304 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size)
305 return 0;
306
307 DataExtractor data(buffer_sp, m_data.GetByteOrder(),
308 m_data.GetAddressByteSize());
309
310 uint32_t idx;
311 uint32_t offset;
312 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000313 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000314 if (m_program_headers[idx].Parse(data, &offset) == false)
315 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000316 }
317
Stephen Wilsonddd29622010-07-13 23:07:23 +0000318 if (idx < m_program_headers.size())
319 m_program_headers.resize(idx);
320
Chris Lattner24943d22010-06-08 16:52:24 +0000321 return m_program_headers.size();
322}
323
Chris Lattner24943d22010-06-08 16:52:24 +0000324//----------------------------------------------------------------------
325// ParseSectionHeaders
326//----------------------------------------------------------------------
327size_t
328ObjectFileELF::ParseSectionHeaders()
329{
330 // We have already parsed the section headers
331 if (!m_section_headers.empty())
332 return m_section_headers.size();
333
Stephen Wilsonddd29622010-07-13 23:07:23 +0000334 // If there are no section headers we are done.
335 if (m_header.e_shnum == 0)
336 return 0;
337
338 m_section_headers.resize(m_header.e_shnum);
339 if (m_section_headers.size() != m_header.e_shnum)
340 return 0;
341
342 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
343 const elf_off sh_offset = m_offset + m_header.e_shoff;
344 DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size));
345
346 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size)
347 return 0;
348
349 DataExtractor data(buffer_sp,
350 m_data.GetByteOrder(),
351 m_data.GetAddressByteSize());
352
353 uint32_t idx;
354 uint32_t offset;
355 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000356 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000357 if (m_section_headers[idx].Parse(data, &offset) == false)
358 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000359 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000360 if (idx < m_section_headers.size())
361 m_section_headers.resize(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000362
363 return m_section_headers.size();
364}
365
366size_t
367ObjectFileELF::GetSectionHeaderStringTable()
368{
369 if (m_shstr_data.GetByteSize() == 0)
370 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000371 const unsigned strtab_idx = m_header.e_shstrndx;
372
373 if (strtab_idx && strtab_idx < m_section_headers.size())
Chris Lattner24943d22010-06-08 16:52:24 +0000374 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000375 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
376 const size_t byte_size = sheader.sh_size;
377 const Elf64_Off offset = m_offset + sheader.sh_offset;
378 DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size));
Chris Lattner24943d22010-06-08 16:52:24 +0000379
380 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size)
381 return 0;
382
383 m_shstr_data.SetData(buffer_sp);
384 }
385 }
386 return m_shstr_data.GetByteSize();
387}
388
Stephen Wilsonddd29622010-07-13 23:07:23 +0000389lldb::user_id_t
Chris Lattner24943d22010-06-08 16:52:24 +0000390ObjectFileELF::GetSectionIndexByName(const char *name)
391{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000392 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
393 return 0;
394
395 // Search the collection of section headers for one with a matching name.
396 for (SectionHeaderCollIter I = m_section_headers.begin();
397 I != m_section_headers.end(); ++I)
Chris Lattner24943d22010-06-08 16:52:24 +0000398 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000399 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
400
401 if (!sectionName)
402 return 0;
403
404 if (strcmp(name, sectionName) != 0)
405 continue;
406
407 return SectionIndex(I);
Chris Lattner24943d22010-06-08 16:52:24 +0000408 }
409
Stephen Wilsonddd29622010-07-13 23:07:23 +0000410 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000411}
412
413SectionList *
414ObjectFileELF::GetSectionList()
415{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000416 if (m_sections_ap.get())
417 return m_sections_ap.get();
418
419 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner24943d22010-06-08 16:52:24 +0000420 {
421 m_sections_ap.reset(new SectionList());
Chris Lattner24943d22010-06-08 16:52:24 +0000422
Stephen Wilsonddd29622010-07-13 23:07:23 +0000423 for (SectionHeaderCollIter I = m_section_headers.begin();
424 I != m_section_headers.end(); ++I)
425 {
426 const ELFSectionHeader &header = *I;
427
428 ConstString name(m_shstr_data.PeekCStr(header.sh_name));
429 uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
430
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000431 static ConstString g_sect_name_text (".text");
432 static ConstString g_sect_name_data (".data");
433 static ConstString g_sect_name_bss (".bss");
434 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
435 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
436 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
437 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
438 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
439 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
440 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
441 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
442 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
443 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
444 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
445 static ConstString g_sect_name_eh_frame (".eh_frame");
446
447 SectionType sect_type = eSectionTypeOther;
448
449 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
450 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
451 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
452 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
453 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
454 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
455 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
456 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
457 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
458 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
459 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
460 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
461 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
462 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
463 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
464
465
Stephen Wilsonddd29622010-07-13 23:07:23 +0000466 SectionSP section(new Section(
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000467 0, // Parent section.
468 GetModule(), // Module to which this section belongs.
469 SectionIndex(I), // Section ID.
470 name, // Section name.
471 sect_type, // Section type.
472 header.sh_addr, // VM address.
473 header.sh_size, // VM size in bytes of this section.
474 header.sh_offset, // Offset of this section in the file.
475 size, // Size of the section as found in the file.
476 header.sh_flags)); // Flags for this section.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000477
478 m_sections_ap->AddSection(section);
Chris Lattner24943d22010-06-08 16:52:24 +0000479 }
480 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000481
Chris Lattner24943d22010-06-08 16:52:24 +0000482 return m_sections_ap.get();
483}
484
485static void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000486ParseSymbols(Symtab *symtab, SectionList *section_list,
487 const ELFSectionHeader &symtab_shdr,
488 const DataExtractor &symtab_data,
489 const DataExtractor &strtab_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000490{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000491 ELFSymbol symbol;
Chris Lattner24943d22010-06-08 16:52:24 +0000492 uint32_t offset = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000493 const unsigned numSymbols =
494 symtab_data.GetByteSize() / symtab_shdr.sh_entsize;
495
Chris Lattner24943d22010-06-08 16:52:24 +0000496 static ConstString text_section_name(".text");
497 static ConstString init_section_name(".init");
498 static ConstString fini_section_name(".fini");
499 static ConstString ctors_section_name(".ctors");
500 static ConstString dtors_section_name(".dtors");
501
502 static ConstString data_section_name(".data");
503 static ConstString rodata_section_name(".rodata");
504 static ConstString rodata1_section_name(".rodata1");
505 static ConstString data2_section_name(".data1");
506 static ConstString bss_section_name(".bss");
507
Stephen Wilsonddd29622010-07-13 23:07:23 +0000508 for (unsigned i = 0; i < numSymbols; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000509 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000510 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000511 break;
512
Stephen Wilsonddd29622010-07-13 23:07:23 +0000513 Section *symbol_section = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000514 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000515 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner24943d22010-06-08 16:52:24 +0000516
Stephen Wilsonddd29622010-07-13 23:07:23 +0000517 switch (symbol_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000518 {
519 case SHN_ABS:
520 symbol_type = eSymbolTypeAbsolute;
521 break;
522 case SHN_UNDEF:
523 symbol_type = eSymbolTypeUndefined;
524 break;
525 default:
Stephen Wilsonddd29622010-07-13 23:07:23 +0000526 symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000527 break;
528 }
529
Stephen Wilsonddd29622010-07-13 23:07:23 +0000530 switch (symbol.getType())
Chris Lattner24943d22010-06-08 16:52:24 +0000531 {
532 default:
533 case STT_NOTYPE:
534 // The symbol's type is not specified.
535 break;
536
537 case STT_OBJECT:
Stephen Wilsonddd29622010-07-13 23:07:23 +0000538 // The symbol is associated with a data object, such as a variable,
539 // an array, etc.
540 symbol_type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +0000541 break;
542
543 case STT_FUNC:
544 // The symbol is associated with a function or other executable code.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000545 symbol_type = eSymbolTypeCode;
Chris Lattner24943d22010-06-08 16:52:24 +0000546 break;
547
548 case STT_SECTION:
549 // The symbol is associated with a section. Symbol table entries of
550 // this type exist primarily for relocation and normally have
551 // STB_LOCAL binding.
552 break;
553
554 case STT_FILE:
555 // Conventionally, the symbol's name gives the name of the source
556 // file associated with the object file. A file symbol has STB_LOCAL
557 // binding, its section index is SHN_ABS, and it precedes the other
558 // STB_LOCAL symbols for the file, if it is present.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000559 symbol_type = eSymbolTypeObjectFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000560 break;
561 }
562
563 if (symbol_type == eSymbolTypeInvalid)
564 {
565 if (symbol_section)
566 {
567 const ConstString &sect_name = symbol_section->GetName();
568 if (sect_name == text_section_name ||
569 sect_name == init_section_name ||
570 sect_name == fini_section_name ||
571 sect_name == ctors_section_name ||
572 sect_name == dtors_section_name)
573 {
574 symbol_type = eSymbolTypeCode;
575 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000576 else if (sect_name == data_section_name ||
577 sect_name == data2_section_name ||
578 sect_name == rodata_section_name ||
579 sect_name == rodata1_section_name ||
580 sect_name == bss_section_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000581 {
582 symbol_type = eSymbolTypeData;
583 }
584 }
585 }
586
587 uint64_t symbol_value = symbol.st_value;
588 if (symbol_section != NULL)
589 symbol_value -= symbol_section->GetFileAddress();
590 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000591 bool is_global = symbol.getBinding() == STB_GLOBAL;
592 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000593
Stephen Wilsonddd29622010-07-13 23:07:23 +0000594 Symbol dc_symbol(
595 i, // ID is the original symbol table index.
596 symbol_name, // Symbol name.
597 false, // Is the symbol name mangled?
598 symbol_type, // Type of this symbol
599 is_global, // Is this globally visible?
600 false, // Is this symbol debug info?
601 false, // Is this symbol a trampoline?
602 false, // Is this symbol artificial?
603 symbol_section, // Section in which this symbol is defined or null.
604 symbol_value, // Offset in section or symbol value.
605 symbol.st_size, // Size in bytes of this symbol.
606 flags); // Symbol flags.
Chris Lattner24943d22010-06-08 16:52:24 +0000607 symtab->AddSymbol(dc_symbol);
608 }
609}
610
Stephen Wilsonddd29622010-07-13 23:07:23 +0000611void
612ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
613 const ELFSectionHeader &symtab_hdr,
614 user_id_t symtab_id)
615{
616 assert(symtab_hdr.sh_type == SHT_SYMTAB ||
617 symtab_hdr.sh_type == SHT_DYNSYM);
618
619 // Parse in the section list if needed.
620 SectionList *section_list = GetSectionList();
621 if (!section_list)
622 return;
623
624 // Section ID's are ones based.
625 user_id_t strtab_id = symtab_hdr.sh_link + 1;
626
627 Section *symtab = section_list->FindSectionByID(symtab_id).get();
628 Section *strtab = section_list->FindSectionByID(strtab_id).get();
629 if (symtab && strtab)
630 {
631 DataExtractor symtab_data;
632 DataExtractor strtab_data;
633 if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) &&
634 strtab->ReadSectionDataFromObjectFile(this, strtab_data))
635 {
636 ParseSymbols(symbol_table, section_list, symtab_hdr,
637 symtab_data, strtab_data);
638 }
639 }
640}
Chris Lattner24943d22010-06-08 16:52:24 +0000641
642Symtab *
643ObjectFileELF::GetSymtab()
644{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000645 if (m_symtab_ap.get())
646 return m_symtab_ap.get();
647
648 Symtab *symbol_table = new Symtab(this);
649 m_symtab_ap.reset(symbol_table);
650
651 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
652 return symbol_table;
653
654 // Locate and parse all linker symbol tables.
655 for (SectionHeaderCollIter I = m_section_headers.begin();
656 I != m_section_headers.end(); ++I)
Chris Lattner24943d22010-06-08 16:52:24 +0000657 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000658 if (I->sh_type == SHT_SYMTAB)
Chris Lattner24943d22010-06-08 16:52:24 +0000659 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000660 const ELFSectionHeader &symtab_section = *I;
661 user_id_t section_id = SectionIndex(I);
662 ParseSymbolTable(symbol_table, symtab_section, section_id);
Chris Lattner24943d22010-06-08 16:52:24 +0000663 }
664 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000665
666 return symbol_table;
Chris Lattner24943d22010-06-08 16:52:24 +0000667}
668
Chris Lattner24943d22010-06-08 16:52:24 +0000669//===----------------------------------------------------------------------===//
670// Dump
671//
672// Dump the specifics of the runtime file container (such as any headers
673// segments, sections, etc).
674//----------------------------------------------------------------------
675void
676ObjectFileELF::Dump(Stream *s)
677{
678 DumpELFHeader(s, m_header);
679 s->EOL();
680 DumpELFProgramHeaders(s);
681 s->EOL();
682 DumpELFSectionHeaders(s);
683 s->EOL();
684 SectionList *section_list = GetSectionList();
685 if (section_list)
686 section_list->Dump(s, NULL, true);
687 Symtab *symtab = GetSymtab();
688 if (symtab)
689 symtab->Dump(s, NULL);
690 s->EOL();
Stephen Wilsonddd29622010-07-13 23:07:23 +0000691 DumpDependentModules(s);
692 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +0000693}
694
695//----------------------------------------------------------------------
696// DumpELFHeader
697//
698// Dump the ELF header to the specified output stream
699//----------------------------------------------------------------------
700void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000701ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner24943d22010-06-08 16:52:24 +0000702{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000703 s->PutCString("ELF Header\n");
704 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
705 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
706 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
707 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
708 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
709 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
710 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner24943d22010-06-08 16:52:24 +0000711
Stephen Wilsonddd29622010-07-13 23:07:23 +0000712 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
713 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner24943d22010-06-08 16:52:24 +0000714 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
715 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
716 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
717
718 s->Printf("e_type = 0x%4.4x ", header.e_type);
719 DumpELFHeader_e_type(s, header.e_type);
720 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
721 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000722 s->Printf("e_entry = 0x%8.8lx\n", header.e_entry);
723 s->Printf("e_phoff = 0x%8.8lx\n", header.e_phoff);
724 s->Printf("e_shoff = 0x%8.8lx\n", header.e_shoff);
Chris Lattner24943d22010-06-08 16:52:24 +0000725 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
726 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
727 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
728 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
729 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
730 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
731 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
732}
733
734//----------------------------------------------------------------------
735// DumpELFHeader_e_type
736//
737// Dump an token value for the ELF header member e_type
738//----------------------------------------------------------------------
739void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000740ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000741{
742 switch (e_type)
743 {
744 case ET_NONE: *s << "ET_NONE"; break;
745 case ET_REL: *s << "ET_REL"; break;
746 case ET_EXEC: *s << "ET_EXEC"; break;
747 case ET_DYN: *s << "ET_DYN"; break;
748 case ET_CORE: *s << "ET_CORE"; break;
749 default:
750 break;
751 }
752}
753
754//----------------------------------------------------------------------
755// DumpELFHeader_e_ident_EI_DATA
756//
757// Dump an token value for the ELF header member e_ident[EI_DATA]
758//----------------------------------------------------------------------
759void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000760ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000761{
762 switch (ei_data)
763 {
764 case ELFDATANONE: *s << "ELFDATANONE"; break;
765 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
766 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
767 default:
768 break;
769 }
770}
771
772
773//----------------------------------------------------------------------
774// DumpELFProgramHeader
775//
776// Dump a single ELF program header to the specified output stream
777//----------------------------------------------------------------------
778void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000779ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner24943d22010-06-08 16:52:24 +0000780{
781 DumpELFProgramHeader_p_type(s, ph.p_type);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000782 s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
783 s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags);
784
Chris Lattner24943d22010-06-08 16:52:24 +0000785 DumpELFProgramHeader_p_flags(s, ph.p_flags);
786 s->Printf(") %8.8x", ph.p_align);
787}
788
789//----------------------------------------------------------------------
790// DumpELFProgramHeader_p_type
791//
792// Dump an token value for the ELF program header member p_type which
793// describes the type of the program header
Stephen Wilsonddd29622010-07-13 23:07:23 +0000794// ----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000795void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000796ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000797{
798 const int kStrWidth = 10;
799 switch (p_type)
800 {
801 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
802 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
803 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
804 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
805 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
806 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
807 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
808 default:
809 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
810 break;
811 }
812}
813
814
815//----------------------------------------------------------------------
816// DumpELFProgramHeader_p_flags
817//
818// Dump an token value for the ELF program header member p_flags
819//----------------------------------------------------------------------
820void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000821ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner24943d22010-06-08 16:52:24 +0000822{
823 *s << ((p_flags & PF_X) ? "PF_X" : " ")
824 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
825 << ((p_flags & PF_W) ? "PF_W" : " ")
826 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
827 << ((p_flags & PF_R) ? "PF_R" : " ");
828}
829
830//----------------------------------------------------------------------
831// DumpELFProgramHeaders
832//
833// Dump all of the ELF program header to the specified output stream
834//----------------------------------------------------------------------
835void
836ObjectFileELF::DumpELFProgramHeaders(Stream *s)
837{
838 if (ParseProgramHeaders())
839 {
840 s->PutCString("Program Headers\n");
Stephen Wilsonddd29622010-07-13 23:07:23 +0000841 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
842 "p_filesz p_memsz p_flags p_align\n");
843 s->PutCString("==== ---------- -------- -------- -------- "
844 "-------- -------- ------------------------- --------\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000845
846 uint32_t idx = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000847 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
848 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000849 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000850 s->Printf("[%2u] ", idx);
851 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner24943d22010-06-08 16:52:24 +0000852 s->EOL();
853 }
854 }
855}
856
Chris Lattner24943d22010-06-08 16:52:24 +0000857//----------------------------------------------------------------------
858// DumpELFSectionHeader
859//
860// Dump a single ELF section header to the specified output stream
861//----------------------------------------------------------------------
862void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000863ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner24943d22010-06-08 16:52:24 +0000864{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000865 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000866 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000867 s->Printf(" %8.8lx (", sh.sh_flags);
Chris Lattner24943d22010-06-08 16:52:24 +0000868 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000869 s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size);
870 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
871 s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize);
Chris Lattner24943d22010-06-08 16:52:24 +0000872}
873
874//----------------------------------------------------------------------
875// DumpELFSectionHeader_sh_type
876//
877// Dump an token value for the ELF section header member sh_type which
878// describes the type of the section
879//----------------------------------------------------------------------
880void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000881ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner24943d22010-06-08 16:52:24 +0000882{
883 const int kStrWidth = 12;
884 switch (sh_type)
885 {
886 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
887 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
888 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
889 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
890 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
891 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
892 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
893 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
894 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
895 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
896 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
897 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
898 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
899 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
900 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
901 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
902 default:
903 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
904 break;
905 }
906}
907
Chris Lattner24943d22010-06-08 16:52:24 +0000908//----------------------------------------------------------------------
909// DumpELFSectionHeader_sh_flags
910//
911// Dump an token value for the ELF section header member sh_flags
912//----------------------------------------------------------------------
913void
Stephen Wilsonddd29622010-07-13 23:07:23 +0000914ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
Chris Lattner24943d22010-06-08 16:52:24 +0000915{
916 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
917 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
918 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
919 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
920 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
921}
Stephen Wilsonddd29622010-07-13 23:07:23 +0000922
Chris Lattner24943d22010-06-08 16:52:24 +0000923//----------------------------------------------------------------------
924// DumpELFSectionHeaders
925//
926// Dump all of the ELF section header to the specified output stream
927//----------------------------------------------------------------------
928void
929ObjectFileELF::DumpELFSectionHeaders(Stream *s)
930{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000931 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
932 return;
933
934 s->PutCString("Section Headers\n");
935 s->PutCString("IDX name type flags "
936 "addr offset size link info addralgn "
937 "entsize Name\n");
938 s->PutCString("==== -------- ------------ -------------------------------- "
939 "-------- -------- -------- -------- -------- -------- "
940 "-------- ====================\n");
941
942 uint32_t idx = 0;
943 for (SectionHeaderCollConstIter I = m_section_headers.begin();
944 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000945 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000946 s->Printf("[%2u] ", idx);
947 ObjectFileELF::DumpELFSectionHeader(s, *I);
948 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
949 if (section_name)
950 *s << ' ' << section_name << "\n";
951 }
952}
Chris Lattner24943d22010-06-08 16:52:24 +0000953
Stephen Wilsonddd29622010-07-13 23:07:23 +0000954void
955ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
956{
957 size_t num_modules = ParseDependentModules();
Chris Lattner24943d22010-06-08 16:52:24 +0000958
Stephen Wilsonddd29622010-07-13 23:07:23 +0000959 if (num_modules > 0)
960 {
961 s->PutCString("Dependent Modules:\n");
962 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000963 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000964 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
965 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000966 }
967 }
968}
969
Chris Lattner24943d22010-06-08 16:52:24 +0000970bool
Stephen Wilsonddd29622010-07-13 23:07:23 +0000971ObjectFileELF::GetTargetTriple(ConstString &target_triple)
Chris Lattner24943d22010-06-08 16:52:24 +0000972{
973 static ConstString g_target_triple;
974
975 if (g_target_triple)
976 {
977 target_triple = g_target_triple;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000978 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000979 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000980
981 std::string triple;
982 switch (m_header.e_machine)
Chris Lattner24943d22010-06-08 16:52:24 +0000983 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000984 default:
985 assert(false && "Unexpected machine type.");
986 break;
987 case EM_SPARC: triple.assign("sparc-"); break;
988 case EM_386: triple.assign("i386-"); break;
989 case EM_68K: triple.assign("68k-"); break;
990 case EM_88K: triple.assign("88k-"); break;
991 case EM_860: triple.assign("i860-"); break;
992 case EM_MIPS: triple.assign("mips-"); break;
993 case EM_PPC: triple.assign("powerpc-"); break;
994 case EM_PPC64: triple.assign("powerpc64-"); break;
995 case EM_ARM: triple.assign("arm-"); break;
996 case EM_X86_64: triple.assign("x86_64-"); break;
Chris Lattner24943d22010-06-08 16:52:24 +0000997 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000998 // TODO: determine if there is a vendor in the ELF? Default to "linux" for now
999 triple += "linux-";
1000 // TODO: determine if there is an OS in the ELF? Default to "gnu" for now
1001 triple += "gnu";
1002 g_target_triple.SetCString(triple.c_str());
1003 target_triple = g_target_triple;
1004
1005 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00001006}
1007