blob: e02f49c80e2dc7785a0e9b5069a5ba2498e74136 [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
Chris Lattner30fdc8d2010-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 Wilsonf325ba92010-07-13 23:07:23 +000012#include <cassert>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include <algorithm>
14
Stephen Wilson2ab0a582011-01-15 00:08:44 +000015#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/DataBuffer.h"
17#include "lldb/Core/Error.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000018#include "lldb/Core/FileSpecList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/Section.h"
21#include "lldb/Core/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
Stephen Wilsonf325ba92010-07-13 23:07:23 +000023#define CASE_AND_STREAM(s, def, width) \
24 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026using namespace lldb;
27using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000028using namespace elf;
29using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Stephen Wilsonf325ba92010-07-13 23:07:23 +000031//------------------------------------------------------------------
32// Static methods.
33//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034void
35ObjectFileELF::Initialize()
36{
Stephen Wilsonf325ba92010-07-13 23:07:23 +000037 PluginManager::RegisterPlugin(GetPluginNameStatic(),
38 GetPluginDescriptionStatic(),
39 CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040}
41
42void
43ObjectFileELF::Terminate()
44{
Stephen Wilsonf325ba92010-07-13 23:07:23 +000045 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046}
47
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048const char *
49ObjectFileELF::GetPluginNameStatic()
50{
Stephen Wilsonf325ba92010-07-13 23:07:23 +000051 return "object-file.elf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052}
53
54const char *
55ObjectFileELF::GetPluginDescriptionStatic()
56{
Stephen Wilsonf325ba92010-07-13 23:07:23 +000057 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058}
59
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060ObjectFile *
Stephen Wilsonf325ba92010-07-13 23:07:23 +000061ObjectFileELF::CreateInstance(Module *module,
62 DataBufferSP &data_sp,
63 const FileSpec *file, addr_t offset,
64 addr_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065{
Stephen Wilsonf325ba92010-07-13 23:07:23 +000066 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +000068 const uint8_t *magic = data_sp->GetBytes() + offset;
69 if (ELFHeader::MagicBytesMatch(magic))
70 {
71 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
72 if (address_size == 4 || address_size == 8)
73 {
Stephen Wilson2ab0a582011-01-15 00:08:44 +000074 std::auto_ptr<ObjectFileELF> objfile_ap(
Stephen Wilsonf325ba92010-07-13 23:07:23 +000075 new ObjectFileELF(module, data_sp, file, offset, length));
Stephen Wilson2ab0a582011-01-15 00:08:44 +000076 ArchSpec spec = objfile_ap->GetArchitecture();
77 if (spec.IsValid() && objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +000078 return objfile_ap.release();
79 }
80 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 }
82 return NULL;
83}
84
Stephen Wilson2ab0a582011-01-15 00:08:44 +000085ArchSpec
86ObjectFileELF::GetArchitecture()
87{
88 if (!ParseHeader())
89 return ArchSpec();
90
91 return ArchSpec(eArchTypeELF, m_header.e_machine, m_header.e_flags);
92}
93
Stephen Wilsonf325ba92010-07-13 23:07:23 +000094//------------------------------------------------------------------
95// PluginInterface protocol
96//------------------------------------------------------------------
97const char *
98ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000100 return "ObjectFileELF";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101}
102
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000103const char *
104ObjectFileELF::GetShortPluginName()
105{
106 return GetPluginNameStatic();
107}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000109uint32_t
110ObjectFileELF::GetPluginVersion()
111{
112 return m_plugin_version;
113}
114
115void
116ObjectFileELF::GetPluginCommandHelp(const char *command, Stream *strm)
117{
118}
119
120Error
121ObjectFileELF::ExecutePluginCommand(Args &command, Stream *strm)
122{
123 Error error;
124 error.SetErrorString("No plug-in commands are currently supported.");
125 return error;
126}
127
128Log *
129ObjectFileELF::EnablePluginLogging(Stream *strm, Args &command)
130{
131 return NULL;
132}
133
134//------------------------------------------------------------------
135// ObjectFile protocol
136//------------------------------------------------------------------
137
138ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
139 const FileSpec* file, addr_t offset,
140 addr_t length)
141 : ObjectFile(module, file, offset, length, dataSP),
142 m_header(),
143 m_program_headers(),
144 m_section_headers(),
145 m_sections_ap(),
146 m_symtab_ap(),
147 m_filespec_ap(),
148 m_shstr_data()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149{
150 if (file)
151 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000152 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153}
154
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155ObjectFileELF::~ObjectFileELF()
156{
157}
158
Jim Ingham5aee1622010-08-09 23:31:02 +0000159bool
160ObjectFileELF::IsExecutable() const
161{
Stephen Wilson7f3b57c2011-01-15 00:09:50 +0000162 return m_header.e_entry != 0;
Jim Ingham5aee1622010-08-09 23:31:02 +0000163}
164
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000165Address
166ObjectFileELF::GetEntryPoint() const
167{
168 if (m_header.e_entry)
169 return Address(NULL, m_header.e_entry);
170 else
171 return Address();
172}
173
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000175ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176{
177 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
178 return eByteOrderBig;
179 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
180 return eByteOrderLittle;
181 return eByteOrderInvalid;
182}
183
184size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000185ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186{
187 return m_data.GetAddressByteSize();
188}
189
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000190unsigned
191ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000193 return std::distance(m_section_headers.begin(), I) + 1;
194}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000196unsigned
197ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
198{
199 return std::distance(m_section_headers.begin(), I) + 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200}
201
202bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000203ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000205 uint32_t offset = GetOffset();
206 return m_header.Parse(m_data, &offset);
207}
208
209bool
Greg Clayton60830262011-02-04 18:53:10 +0000210ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000211{
212 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 return false;
214}
215
216uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000217ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000219 size_t num_modules = ParseDependentModules();
220 uint32_t num_specs = 0;
221
222 for (unsigned i = 0; i < num_modules; ++i)
223 {
224 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
225 num_specs++;
226 }
227
228 return num_specs;
229}
230
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000231Address
232ObjectFileELF::GetImageInfoAddress()
233{
234 if (!ParseSectionHeaders())
235 return Address();
236
237 user_id_t dynsym_id = 0;
238 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
239 sh_pos != m_section_headers.end(); ++sh_pos)
240 {
241 if (sh_pos->sh_type == SHT_DYNAMIC)
242 {
243 dynsym_id = SectionIndex(sh_pos);
244 break;
245 }
246 }
247
248 if (!dynsym_id)
249 return Address();
250
251 SectionList *section_list = GetSectionList();
252 if (!section_list)
253 return Address();
254
255 // Resolve the dynamic table entries.
256 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
257 if (!dynsym)
258 return Address();
259
260 DataExtractor dynsym_data;
261 if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data))
262 {
263 ELFDynamic symbol;
264 const unsigned section_size = dynsym_data.GetByteSize();
265 unsigned offset = 0;
266 unsigned cursor = 0;
267
268 // Look for a DT_DEBUG entry.
269 while (cursor < section_size)
270 {
271 offset = cursor;
272 if (!symbol.Parse(dynsym_data, &cursor))
273 break;
274
275 if (symbol.d_tag != DT_DEBUG)
276 continue;
277
278 return Address(dynsym, offset + sizeof(symbol.d_tag));
279 }
280 }
281
282 return Address();
283}
284
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000285//----------------------------------------------------------------------
286// ParseDependentModules
287//----------------------------------------------------------------------
288size_t
289ObjectFileELF::ParseDependentModules()
290{
291 if (m_filespec_ap.get())
292 return m_filespec_ap->GetSize();
293
294 m_filespec_ap.reset(new FileSpecList());
295
296 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
297 return 0;
298
299 // Locate the dynamic table.
300 user_id_t dynsym_id = 0;
301 user_id_t dynstr_id = 0;
Greg Clayton450e3f32010-10-12 02:24:53 +0000302 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
303 sh_pos != m_section_headers.end(); ++sh_pos)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000304 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000305 if (sh_pos->sh_type == SHT_DYNAMIC)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000306 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000307 dynsym_id = SectionIndex(sh_pos);
308 dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000309 break;
310 }
311 }
312
313 if (!(dynsym_id && dynstr_id))
314 return 0;
315
316 SectionList *section_list = GetSectionList();
317 if (!section_list)
318 return 0;
319
320 // Resolve and load the dynamic table entries and corresponding string
321 // table.
322 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
323 Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
324 if (!(dynsym && dynstr))
325 return 0;
326
327 DataExtractor dynsym_data;
328 DataExtractor dynstr_data;
329 if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) &&
330 dynstr->ReadSectionDataFromObjectFile(this, dynstr_data))
331 {
332 ELFDynamic symbol;
333 const unsigned section_size = dynsym_data.GetByteSize();
334 unsigned offset = 0;
335
336 // The only type of entries we are concerned with are tagged DT_NEEDED,
337 // yielding the name of a required library.
338 while (offset < section_size)
339 {
340 if (!symbol.Parse(dynsym_data, &offset))
341 break;
342
343 if (symbol.d_tag != DT_NEEDED)
344 continue;
345
346 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
347 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +0000348 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000349 }
350 }
351
352 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353}
354
355//----------------------------------------------------------------------
356// ParseProgramHeaders
357//----------------------------------------------------------------------
358size_t
359ObjectFileELF::ParseProgramHeaders()
360{
361 // We have already parsed the program headers
362 if (!m_program_headers.empty())
363 return m_program_headers.size();
364
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000365 // If there are no program headers to read we are done.
366 if (m_header.e_phnum == 0)
367 return 0;
368
369 m_program_headers.resize(m_header.e_phnum);
370 if (m_program_headers.size() != m_header.e_phnum)
371 return 0;
372
373 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
374 const elf_off ph_offset = m_offset + m_header.e_phoff;
375 DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size));
376
377 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size)
378 return 0;
379
380 DataExtractor data(buffer_sp, m_data.GetByteOrder(),
381 m_data.GetAddressByteSize());
382
383 uint32_t idx;
384 uint32_t offset;
385 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000387 if (m_program_headers[idx].Parse(data, &offset) == false)
388 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 }
390
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000391 if (idx < m_program_headers.size())
392 m_program_headers.resize(idx);
393
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 return m_program_headers.size();
395}
396
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397//----------------------------------------------------------------------
398// ParseSectionHeaders
399//----------------------------------------------------------------------
400size_t
401ObjectFileELF::ParseSectionHeaders()
402{
403 // We have already parsed the section headers
404 if (!m_section_headers.empty())
405 return m_section_headers.size();
406
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000407 // If there are no section headers we are done.
408 if (m_header.e_shnum == 0)
409 return 0;
410
411 m_section_headers.resize(m_header.e_shnum);
412 if (m_section_headers.size() != m_header.e_shnum)
413 return 0;
414
415 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
416 const elf_off sh_offset = m_offset + m_header.e_shoff;
417 DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size));
418
419 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size)
420 return 0;
421
422 DataExtractor data(buffer_sp,
423 m_data.GetByteOrder(),
424 m_data.GetAddressByteSize());
425
426 uint32_t idx;
427 uint32_t offset;
428 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000430 if (m_section_headers[idx].Parse(data, &offset) == false)
431 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000433 if (idx < m_section_headers.size())
434 m_section_headers.resize(idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
436 return m_section_headers.size();
437}
438
439size_t
440ObjectFileELF::GetSectionHeaderStringTable()
441{
442 if (m_shstr_data.GetByteSize() == 0)
443 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000444 const unsigned strtab_idx = m_header.e_shstrndx;
445
446 if (strtab_idx && strtab_idx < m_section_headers.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000448 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
449 const size_t byte_size = sheader.sh_size;
450 const Elf64_Off offset = m_offset + sheader.sh_offset;
451 DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
453 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size)
454 return 0;
455
456 m_shstr_data.SetData(buffer_sp);
457 }
458 }
459 return m_shstr_data.GetByteSize();
460}
461
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000462lldb::user_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463ObjectFileELF::GetSectionIndexByName(const char *name)
464{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000465 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
466 return 0;
467
468 // Search the collection of section headers for one with a matching name.
469 for (SectionHeaderCollIter I = m_section_headers.begin();
470 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000472 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
473
474 if (!sectionName)
475 return 0;
476
477 if (strcmp(name, sectionName) != 0)
478 continue;
479
480 return SectionIndex(I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 }
482
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000483 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
485
486SectionList *
487ObjectFileELF::GetSectionList()
488{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000489 if (m_sections_ap.get())
490 return m_sections_ap.get();
491
492 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 {
494 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000496 for (SectionHeaderCollIter I = m_section_headers.begin();
497 I != m_section_headers.end(); ++I)
498 {
499 const ELFSectionHeader &header = *I;
500
501 ConstString name(m_shstr_data.PeekCStr(header.sh_name));
502 uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
503
Greg Clayton4ceb9982010-07-21 22:54:26 +0000504 static ConstString g_sect_name_text (".text");
505 static ConstString g_sect_name_data (".data");
506 static ConstString g_sect_name_bss (".bss");
507 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
508 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
509 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
510 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
511 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
512 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
513 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
514 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
515 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
516 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
517 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
518 static ConstString g_sect_name_eh_frame (".eh_frame");
519
520 SectionType sect_type = eSectionTypeOther;
521
522 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
523 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
524 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
525 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
526 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
527 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
528 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
529 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
530 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
531 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
532 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
533 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
534 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
535 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
536 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
537
538
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000539 SectionSP section(new Section(
Greg Clayton4ceb9982010-07-21 22:54:26 +0000540 0, // Parent section.
541 GetModule(), // Module to which this section belongs.
542 SectionIndex(I), // Section ID.
543 name, // Section name.
544 sect_type, // Section type.
545 header.sh_addr, // VM address.
546 header.sh_size, // VM size in bytes of this section.
547 header.sh_offset, // Offset of this section in the file.
548 size, // Size of the section as found in the file.
549 header.sh_flags)); // Flags for this section.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000550
551 m_sections_ap->AddSection(section);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 }
553 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000554
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555 return m_sections_ap.get();
556}
557
558static void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000559ParseSymbols(Symtab *symtab, SectionList *section_list,
560 const ELFSectionHeader &symtab_shdr,
561 const DataExtractor &symtab_data,
562 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000564 ELFSymbol symbol;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 uint32_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000566 const unsigned numSymbols =
567 symtab_data.GetByteSize() / symtab_shdr.sh_entsize;
568
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 static ConstString text_section_name(".text");
570 static ConstString init_section_name(".init");
571 static ConstString fini_section_name(".fini");
572 static ConstString ctors_section_name(".ctors");
573 static ConstString dtors_section_name(".dtors");
574
575 static ConstString data_section_name(".data");
576 static ConstString rodata_section_name(".rodata");
577 static ConstString rodata1_section_name(".rodata1");
578 static ConstString data2_section_name(".data1");
579 static ConstString bss_section_name(".bss");
580
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000581 for (unsigned i = 0; i < numSymbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000583 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 break;
585
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000586 Section *symbol_section = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000588 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000590 switch (symbol_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591 {
592 case SHN_ABS:
593 symbol_type = eSymbolTypeAbsolute;
594 break;
595 case SHN_UNDEF:
596 symbol_type = eSymbolTypeUndefined;
597 break;
598 default:
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000599 symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 break;
601 }
602
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000603 switch (symbol.getType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 {
605 default:
606 case STT_NOTYPE:
607 // The symbol's type is not specified.
608 break;
609
610 case STT_OBJECT:
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000611 // The symbol is associated with a data object, such as a variable,
612 // an array, etc.
613 symbol_type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 break;
615
616 case STT_FUNC:
617 // The symbol is associated with a function or other executable code.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000618 symbol_type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 break;
620
621 case STT_SECTION:
622 // The symbol is associated with a section. Symbol table entries of
623 // this type exist primarily for relocation and normally have
624 // STB_LOCAL binding.
625 break;
626
627 case STT_FILE:
628 // Conventionally, the symbol's name gives the name of the source
629 // file associated with the object file. A file symbol has STB_LOCAL
630 // binding, its section index is SHN_ABS, and it precedes the other
631 // STB_LOCAL symbols for the file, if it is present.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000632 symbol_type = eSymbolTypeObjectFile;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 break;
634 }
635
636 if (symbol_type == eSymbolTypeInvalid)
637 {
638 if (symbol_section)
639 {
640 const ConstString &sect_name = symbol_section->GetName();
641 if (sect_name == text_section_name ||
642 sect_name == init_section_name ||
643 sect_name == fini_section_name ||
644 sect_name == ctors_section_name ||
645 sect_name == dtors_section_name)
646 {
647 symbol_type = eSymbolTypeCode;
648 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000649 else if (sect_name == data_section_name ||
650 sect_name == data2_section_name ||
651 sect_name == rodata_section_name ||
652 sect_name == rodata1_section_name ||
653 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 {
655 symbol_type = eSymbolTypeData;
656 }
657 }
658 }
659
660 uint64_t symbol_value = symbol.st_value;
661 if (symbol_section != NULL)
662 symbol_value -= symbol_section->GetFileAddress();
663 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000664 bool is_global = symbol.getBinding() == STB_GLOBAL;
665 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000667 Symbol dc_symbol(
668 i, // ID is the original symbol table index.
669 symbol_name, // Symbol name.
670 false, // Is the symbol name mangled?
671 symbol_type, // Type of this symbol
672 is_global, // Is this globally visible?
673 false, // Is this symbol debug info?
674 false, // Is this symbol a trampoline?
675 false, // Is this symbol artificial?
676 symbol_section, // Section in which this symbol is defined or null.
677 symbol_value, // Offset in section or symbol value.
678 symbol.st_size, // Size in bytes of this symbol.
679 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 symtab->AddSymbol(dc_symbol);
681 }
682}
683
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000684void
685ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
686 const ELFSectionHeader &symtab_hdr,
687 user_id_t symtab_id)
688{
689 assert(symtab_hdr.sh_type == SHT_SYMTAB ||
690 symtab_hdr.sh_type == SHT_DYNSYM);
691
692 // Parse in the section list if needed.
693 SectionList *section_list = GetSectionList();
694 if (!section_list)
695 return;
696
697 // Section ID's are ones based.
698 user_id_t strtab_id = symtab_hdr.sh_link + 1;
699
700 Section *symtab = section_list->FindSectionByID(symtab_id).get();
701 Section *strtab = section_list->FindSectionByID(strtab_id).get();
702 if (symtab && strtab)
703 {
704 DataExtractor symtab_data;
705 DataExtractor strtab_data;
706 if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) &&
707 strtab->ReadSectionDataFromObjectFile(this, strtab_data))
708 {
709 ParseSymbols(symbol_table, section_list, symtab_hdr,
710 symtab_data, strtab_data);
711 }
712 }
713}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714
715Symtab *
716ObjectFileELF::GetSymtab()
717{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000718 if (m_symtab_ap.get())
719 return m_symtab_ap.get();
720
721 Symtab *symbol_table = new Symtab(this);
722 m_symtab_ap.reset(symbol_table);
723
Greg Clayton8087ca22010-10-08 04:20:14 +0000724 Mutex::Locker locker (symbol_table->GetMutex ());
725
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000726 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
727 return symbol_table;
728
729 // Locate and parse all linker symbol tables.
730 for (SectionHeaderCollIter I = m_section_headers.begin();
731 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000733 if (I->sh_type == SHT_SYMTAB)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000735 const ELFSectionHeader &symtab_section = *I;
736 user_id_t section_id = SectionIndex(I);
Greg Clayton8087ca22010-10-08 04:20:14 +0000737 ParseSymbolTable (symbol_table, symtab_section, section_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738 }
739 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000740
741 return symbol_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742}
743
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744//===----------------------------------------------------------------------===//
745// Dump
746//
747// Dump the specifics of the runtime file container (such as any headers
748// segments, sections, etc).
749//----------------------------------------------------------------------
750void
751ObjectFileELF::Dump(Stream *s)
752{
753 DumpELFHeader(s, m_header);
754 s->EOL();
755 DumpELFProgramHeaders(s);
756 s->EOL();
757 DumpELFSectionHeaders(s);
758 s->EOL();
759 SectionList *section_list = GetSectionList();
760 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +0000761 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762 Symtab *symtab = GetSymtab();
763 if (symtab)
Greg Clayton8087ca22010-10-08 04:20:14 +0000764 symtab->Dump(s, NULL, lldb::eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000766 DumpDependentModules(s);
767 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768}
769
770//----------------------------------------------------------------------
771// DumpELFHeader
772//
773// Dump the ELF header to the specified output stream
774//----------------------------------------------------------------------
775void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000776ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000778 s->PutCString("ELF Header\n");
779 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
780 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
781 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
782 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
783 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
784 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
785 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000787 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
788 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
790 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
791 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
792
793 s->Printf("e_type = 0x%4.4x ", header.e_type);
794 DumpELFHeader_e_type(s, header.e_type);
795 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
796 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000797 s->Printf("e_entry = 0x%8.8lx\n", header.e_entry);
798 s->Printf("e_phoff = 0x%8.8lx\n", header.e_phoff);
799 s->Printf("e_shoff = 0x%8.8lx\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
801 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
802 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
803 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
804 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
805 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
806 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
807}
808
809//----------------------------------------------------------------------
810// DumpELFHeader_e_type
811//
812// Dump an token value for the ELF header member e_type
813//----------------------------------------------------------------------
814void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000815ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816{
817 switch (e_type)
818 {
819 case ET_NONE: *s << "ET_NONE"; break;
820 case ET_REL: *s << "ET_REL"; break;
821 case ET_EXEC: *s << "ET_EXEC"; break;
822 case ET_DYN: *s << "ET_DYN"; break;
823 case ET_CORE: *s << "ET_CORE"; break;
824 default:
825 break;
826 }
827}
828
829//----------------------------------------------------------------------
830// DumpELFHeader_e_ident_EI_DATA
831//
832// Dump an token value for the ELF header member e_ident[EI_DATA]
833//----------------------------------------------------------------------
834void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000835ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836{
837 switch (ei_data)
838 {
839 case ELFDATANONE: *s << "ELFDATANONE"; break;
840 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
841 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
842 default:
843 break;
844 }
845}
846
847
848//----------------------------------------------------------------------
849// DumpELFProgramHeader
850//
851// Dump a single ELF program header to the specified output stream
852//----------------------------------------------------------------------
853void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000854ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855{
856 DumpELFProgramHeader_p_type(s, ph.p_type);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000857 s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
858 s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags);
859
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 DumpELFProgramHeader_p_flags(s, ph.p_flags);
861 s->Printf(") %8.8x", ph.p_align);
862}
863
864//----------------------------------------------------------------------
865// DumpELFProgramHeader_p_type
866//
867// Dump an token value for the ELF program header member p_type which
868// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000869// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000871ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872{
873 const int kStrWidth = 10;
874 switch (p_type)
875 {
876 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
877 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
878 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
879 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
880 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
881 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
882 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
883 default:
884 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
885 break;
886 }
887}
888
889
890//----------------------------------------------------------------------
891// DumpELFProgramHeader_p_flags
892//
893// Dump an token value for the ELF program header member p_flags
894//----------------------------------------------------------------------
895void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000896ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897{
898 *s << ((p_flags & PF_X) ? "PF_X" : " ")
899 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
900 << ((p_flags & PF_W) ? "PF_W" : " ")
901 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
902 << ((p_flags & PF_R) ? "PF_R" : " ");
903}
904
905//----------------------------------------------------------------------
906// DumpELFProgramHeaders
907//
908// Dump all of the ELF program header to the specified output stream
909//----------------------------------------------------------------------
910void
911ObjectFileELF::DumpELFProgramHeaders(Stream *s)
912{
913 if (ParseProgramHeaders())
914 {
915 s->PutCString("Program Headers\n");
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000916 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
917 "p_filesz p_memsz p_flags p_align\n");
918 s->PutCString("==== ---------- -------- -------- -------- "
919 "-------- -------- ------------------------- --------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000920
921 uint32_t idx = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000922 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
923 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000925 s->Printf("[%2u] ", idx);
926 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 s->EOL();
928 }
929 }
930}
931
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932//----------------------------------------------------------------------
933// DumpELFSectionHeader
934//
935// Dump a single ELF section header to the specified output stream
936//----------------------------------------------------------------------
937void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000938ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000940 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000942 s->Printf(" %8.8lx (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000944 s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size);
945 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
946 s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947}
948
949//----------------------------------------------------------------------
950// DumpELFSectionHeader_sh_type
951//
952// Dump an token value for the ELF section header member sh_type which
953// describes the type of the section
954//----------------------------------------------------------------------
955void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000956ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957{
958 const int kStrWidth = 12;
959 switch (sh_type)
960 {
961 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
962 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
963 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
964 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
965 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
966 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
967 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
968 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
969 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
970 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
971 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
972 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
973 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
974 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
975 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
976 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
977 default:
978 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
979 break;
980 }
981}
982
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983//----------------------------------------------------------------------
984// DumpELFSectionHeader_sh_flags
985//
986// Dump an token value for the ELF section header member sh_flags
987//----------------------------------------------------------------------
988void
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000989ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990{
991 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
992 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
993 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
994 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
995 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
996}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000997
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998//----------------------------------------------------------------------
999// DumpELFSectionHeaders
1000//
1001// Dump all of the ELF section header to the specified output stream
1002//----------------------------------------------------------------------
1003void
1004ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1005{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001006 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1007 return;
1008
1009 s->PutCString("Section Headers\n");
1010 s->PutCString("IDX name type flags "
1011 "addr offset size link info addralgn "
1012 "entsize Name\n");
1013 s->PutCString("==== -------- ------------ -------------------------------- "
1014 "-------- -------- -------- -------- -------- -------- "
1015 "-------- ====================\n");
1016
1017 uint32_t idx = 0;
1018 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1019 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001020 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001021 s->Printf("[%2u] ", idx);
1022 ObjectFileELF::DumpELFSectionHeader(s, *I);
1023 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1024 if (section_name)
1025 *s << ' ' << section_name << "\n";
1026 }
1027}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001029void
1030ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1031{
1032 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001033
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001034 if (num_modules > 0)
1035 {
1036 s->PutCString("Dependent Modules:\n");
1037 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001039 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1040 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 }
1042 }
1043}
1044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001045bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001046ObjectFileELF::GetTargetTriple(ConstString &target_triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047{
1048 static ConstString g_target_triple;
1049
1050 if (g_target_triple)
1051 {
1052 target_triple = g_target_triple;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001053 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001055
1056 std::string triple;
1057 switch (m_header.e_machine)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001059 default:
1060 assert(false && "Unexpected machine type.");
1061 break;
1062 case EM_SPARC: triple.assign("sparc-"); break;
1063 case EM_386: triple.assign("i386-"); break;
1064 case EM_68K: triple.assign("68k-"); break;
1065 case EM_88K: triple.assign("88k-"); break;
1066 case EM_860: triple.assign("i860-"); break;
1067 case EM_MIPS: triple.assign("mips-"); break;
1068 case EM_PPC: triple.assign("powerpc-"); break;
1069 case EM_PPC64: triple.assign("powerpc64-"); break;
1070 case EM_ARM: triple.assign("arm-"); break;
1071 case EM_X86_64: triple.assign("x86_64-"); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001073 // TODO: determine if there is a vendor in the ELF? Default to "linux" for now
1074 triple += "linux-";
1075 // TODO: determine if there is an OS in the ELF? Default to "gnu" for now
1076 triple += "gnu";
1077 g_target_triple.SetCString(triple.c_str());
1078 target_triple = g_target_triple;
1079
1080 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081}
1082