blob: 2e300a10d890c051d03f1ecb52a52bf04fc555d9 [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"
Jim Ingham672e6f52011-03-07 23:44:08 +000019#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Section.h"
22#include "lldb/Core/Stream.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000023#include "lldb/Symbol/SymbolContext.h"
Greg Clayton64195a22011-02-23 00:35:02 +000024#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Stephen Wilson499b40e2011-03-30 16:07:05 +000026#include "llvm/ADT/PointerUnion.h"
27
Stephen Wilsonf325ba92010-07-13 23:07:23 +000028#define CASE_AND_STREAM(s, def, width) \
29 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031using namespace lldb;
32using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000033using namespace elf;
34using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Stephen Wilson499b40e2011-03-30 16:07:05 +000036namespace {
37//===----------------------------------------------------------------------===//
38/// @class ELFRelocation
39/// @brief Generic wrapper for ELFRel and ELFRela.
40///
41/// This helper class allows us to parse both ELFRel and ELFRela relocation
42/// entries in a generic manner.
43class ELFRelocation
44{
45public:
46
47 /// Constructs an ELFRelocation entry with a personality as given by @p
48 /// type.
49 ///
50 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
51 ELFRelocation(unsigned type);
52
53 ~ELFRelocation();
54
55 bool
56 Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
57
58 static unsigned
59 RelocType32(const ELFRelocation &rel);
60
61 static unsigned
62 RelocType64(const ELFRelocation &rel);
63
64 static unsigned
65 RelocSymbol32(const ELFRelocation &rel);
66
67 static unsigned
68 RelocSymbol64(const ELFRelocation &rel);
69
70private:
71 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
72
73 RelocUnion reloc;
74};
75
76ELFRelocation::ELFRelocation(unsigned type)
77{
78 if (type == DT_REL)
79 reloc = new ELFRel();
80 else if (type == DT_RELA)
81 reloc = new ELFRela();
82 else {
83 assert(false && "unexpected relocation type");
84 reloc = static_cast<ELFRel*>(NULL);
85 }
86}
87
88ELFRelocation::~ELFRelocation()
89{
90 if (reloc.is<ELFRel*>())
91 delete reloc.get<ELFRel*>();
92 else
93 delete reloc.get<ELFRela*>();
94}
95
96bool
97ELFRelocation::Parse(const lldb_private::DataExtractor &data, uint32_t *offset)
98{
99 if (reloc.is<ELFRel*>())
100 return reloc.get<ELFRel*>()->Parse(data, offset);
101 else
102 return reloc.get<ELFRela*>()->Parse(data, offset);
103}
104
105unsigned
106ELFRelocation::RelocType32(const ELFRelocation &rel)
107{
108 if (rel.reloc.is<ELFRel*>())
109 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
110 else
111 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
112}
113
114unsigned
115ELFRelocation::RelocType64(const ELFRelocation &rel)
116{
117 if (rel.reloc.is<ELFRel*>())
118 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
119 else
120 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
121}
122
123unsigned
124ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
125{
126 if (rel.reloc.is<ELFRel*>())
127 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
128 else
129 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
130}
131
132unsigned
133ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
134{
135 if (rel.reloc.is<ELFRel*>())
136 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
137 else
138 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
139}
140
141} // end anonymous namespace
142
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000143//------------------------------------------------------------------
144// Static methods.
145//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146void
147ObjectFileELF::Initialize()
148{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000149 PluginManager::RegisterPlugin(GetPluginNameStatic(),
150 GetPluginDescriptionStatic(),
151 CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152}
153
154void
155ObjectFileELF::Terminate()
156{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000157 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160const char *
161ObjectFileELF::GetPluginNameStatic()
162{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000163 return "object-file.elf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164}
165
166const char *
167ObjectFileELF::GetPluginDescriptionStatic()
168{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000169 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170}
171
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172ObjectFile *
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000173ObjectFileELF::CreateInstance(Module *module,
174 DataBufferSP &data_sp,
175 const FileSpec *file, addr_t offset,
176 addr_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000178 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000180 const uint8_t *magic = data_sp->GetBytes() + offset;
181 if (ELFHeader::MagicBytesMatch(magic))
182 {
183 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
184 if (address_size == 4 || address_size == 8)
185 {
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000186 std::auto_ptr<ObjectFileELF> objfile_ap(
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000187 new ObjectFileELF(module, data_sp, file, offset, length));
Stephen Wilson3f4200fd2011-02-24 19:16:15 +0000188 ArchSpec spec;
189 if (objfile_ap->GetArchitecture(spec) &&
190 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000191 return objfile_ap.release();
192 }
193 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 }
195 return NULL;
196}
197
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000198
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000199//------------------------------------------------------------------
200// PluginInterface protocol
201//------------------------------------------------------------------
202const char *
203ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000205 return "ObjectFileELF";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000208const char *
209ObjectFileELF::GetShortPluginName()
210{
211 return GetPluginNameStatic();
212}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000214uint32_t
215ObjectFileELF::GetPluginVersion()
216{
217 return m_plugin_version;
218}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000219//------------------------------------------------------------------
220// ObjectFile protocol
221//------------------------------------------------------------------
222
223ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
224 const FileSpec* file, addr_t offset,
225 addr_t length)
226 : ObjectFile(module, file, offset, length, dataSP),
227 m_header(),
228 m_program_headers(),
229 m_section_headers(),
230 m_sections_ap(),
231 m_symtab_ap(),
232 m_filespec_ap(),
233 m_shstr_data()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234{
235 if (file)
236 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000237 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238}
239
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240ObjectFileELF::~ObjectFileELF()
241{
242}
243
Jim Ingham5aee1622010-08-09 23:31:02 +0000244bool
245ObjectFileELF::IsExecutable() const
246{
Stephen Wilson7f3b57c2011-01-15 00:09:50 +0000247 return m_header.e_entry != 0;
Jim Ingham5aee1622010-08-09 23:31:02 +0000248}
249
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000251ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252{
253 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
254 return eByteOrderBig;
255 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
256 return eByteOrderLittle;
257 return eByteOrderInvalid;
258}
259
260size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000261ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262{
263 return m_data.GetAddressByteSize();
264}
265
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000266unsigned
267ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000269 return std::distance(m_section_headers.begin(), I) + 1;
270}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000272unsigned
273ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
274{
275 return std::distance(m_section_headers.begin(), I) + 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276}
277
278bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000279ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000281 uint32_t offset = GetOffset();
282 return m_header.Parse(m_data, &offset);
283}
284
285bool
Greg Clayton60830262011-02-04 18:53:10 +0000286ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000287{
288 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 return false;
290}
291
292uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000293ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000295 size_t num_modules = ParseDependentModules();
296 uint32_t num_specs = 0;
297
298 for (unsigned i = 0; i < num_modules; ++i)
299 {
300 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
301 num_specs++;
302 }
303
304 return num_specs;
305}
306
Stephen Wilson499b40e2011-03-30 16:07:05 +0000307user_id_t
308ObjectFileELF::GetSectionIndexByType(unsigned type)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000309{
310 if (!ParseSectionHeaders())
Stephen Wilson499b40e2011-03-30 16:07:05 +0000311 return 0;
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000312
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000313 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
314 sh_pos != m_section_headers.end(); ++sh_pos)
315 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000316 if (sh_pos->sh_type == type)
317 return SectionIndex(sh_pos);
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000318 }
319
Stephen Wilson499b40e2011-03-30 16:07:05 +0000320 return 0;
321}
322
323Address
324ObjectFileELF::GetImageInfoAddress()
325{
326 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000327 return Address();
328
329 SectionList *section_list = GetSectionList();
330 if (!section_list)
331 return Address();
332
Stephen Wilson499b40e2011-03-30 16:07:05 +0000333 user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC);
334 if (!dynsym_id)
335 return Address();
336
337 const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
338 if (!dynsym_hdr)
339 return Address();
340
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000341 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
342 if (!dynsym)
343 return Address();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000344
345 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000346 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000347 ELFDynamic &symbol = m_dynamic_symbols[i];
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000348
Stephen Wilson499b40e2011-03-30 16:07:05 +0000349 if (symbol.d_tag == DT_DEBUG)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000350 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000351 // Compute the offset as the number of previous entries plus the
352 // size of d_tag.
353 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
354 return Address(dynsym, offset);
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000355 }
356 }
357
358 return Address();
359}
360
Jim Ingham672e6f52011-03-07 23:44:08 +0000361lldb_private::Address
362ObjectFileELF::GetEntryPointAddress ()
363{
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000364 SectionList *sections;
365 addr_t offset;
Jim Ingham672e6f52011-03-07 23:44:08 +0000366
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000367 if (m_entry_point_address.IsValid())
368 return m_entry_point_address;
369
370 if (!ParseHeader() || !IsExecutable())
371 return m_entry_point_address;
372
373 sections = GetSectionList();
374 offset = m_header.e_entry;
375
376 if (!sections)
377 {
378 m_entry_point_address.SetOffset(offset);
379 return m_entry_point_address;
380 }
381
382 m_entry_point_address.ResolveAddressUsingFileSections(offset, sections);
383
384 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +0000385}
386
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000387//----------------------------------------------------------------------
388// ParseDependentModules
389//----------------------------------------------------------------------
390size_t
391ObjectFileELF::ParseDependentModules()
392{
393 if (m_filespec_ap.get())
394 return m_filespec_ap->GetSize();
395
396 m_filespec_ap.reset(new FileSpecList());
397
398 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
399 return 0;
400
401 // Locate the dynamic table.
402 user_id_t dynsym_id = 0;
403 user_id_t dynstr_id = 0;
Greg Clayton450e3f32010-10-12 02:24:53 +0000404 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
405 sh_pos != m_section_headers.end(); ++sh_pos)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000406 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000407 if (sh_pos->sh_type == SHT_DYNAMIC)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000408 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000409 dynsym_id = SectionIndex(sh_pos);
410 dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000411 break;
412 }
413 }
414
415 if (!(dynsym_id && dynstr_id))
416 return 0;
417
418 SectionList *section_list = GetSectionList();
419 if (!section_list)
420 return 0;
421
422 // Resolve and load the dynamic table entries and corresponding string
423 // table.
424 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
425 Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
426 if (!(dynsym && dynstr))
427 return 0;
428
429 DataExtractor dynsym_data;
430 DataExtractor dynstr_data;
431 if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) &&
432 dynstr->ReadSectionDataFromObjectFile(this, dynstr_data))
433 {
434 ELFDynamic symbol;
435 const unsigned section_size = dynsym_data.GetByteSize();
436 unsigned offset = 0;
437
438 // The only type of entries we are concerned with are tagged DT_NEEDED,
439 // yielding the name of a required library.
440 while (offset < section_size)
441 {
442 if (!symbol.Parse(dynsym_data, &offset))
443 break;
444
445 if (symbol.d_tag != DT_NEEDED)
446 continue;
447
448 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
449 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +0000450 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000451 }
452 }
453
454 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455}
456
457//----------------------------------------------------------------------
458// ParseProgramHeaders
459//----------------------------------------------------------------------
460size_t
461ObjectFileELF::ParseProgramHeaders()
462{
463 // We have already parsed the program headers
464 if (!m_program_headers.empty())
465 return m_program_headers.size();
466
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000467 // If there are no program headers to read we are done.
468 if (m_header.e_phnum == 0)
469 return 0;
470
471 m_program_headers.resize(m_header.e_phnum);
472 if (m_program_headers.size() != m_header.e_phnum)
473 return 0;
474
475 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
476 const elf_off ph_offset = m_offset + m_header.e_phoff;
477 DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size));
478
479 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size)
480 return 0;
481
482 DataExtractor data(buffer_sp, m_data.GetByteOrder(),
483 m_data.GetAddressByteSize());
484
485 uint32_t idx;
486 uint32_t offset;
487 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000489 if (m_program_headers[idx].Parse(data, &offset) == false)
490 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 }
492
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000493 if (idx < m_program_headers.size())
494 m_program_headers.resize(idx);
495
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496 return m_program_headers.size();
497}
498
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499//----------------------------------------------------------------------
500// ParseSectionHeaders
501//----------------------------------------------------------------------
502size_t
503ObjectFileELF::ParseSectionHeaders()
504{
505 // We have already parsed the section headers
506 if (!m_section_headers.empty())
507 return m_section_headers.size();
508
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000509 // If there are no section headers we are done.
510 if (m_header.e_shnum == 0)
511 return 0;
512
513 m_section_headers.resize(m_header.e_shnum);
514 if (m_section_headers.size() != m_header.e_shnum)
515 return 0;
516
517 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
518 const elf_off sh_offset = m_offset + m_header.e_shoff;
519 DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size));
520
521 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size)
522 return 0;
523
524 DataExtractor data(buffer_sp,
525 m_data.GetByteOrder(),
526 m_data.GetAddressByteSize());
527
528 uint32_t idx;
529 uint32_t offset;
530 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000532 if (m_section_headers[idx].Parse(data, &offset) == false)
533 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000535 if (idx < m_section_headers.size())
536 m_section_headers.resize(idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537
538 return m_section_headers.size();
539}
540
541size_t
542ObjectFileELF::GetSectionHeaderStringTable()
543{
544 if (m_shstr_data.GetByteSize() == 0)
545 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000546 const unsigned strtab_idx = m_header.e_shstrndx;
547
548 if (strtab_idx && strtab_idx < m_section_headers.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000550 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
551 const size_t byte_size = sheader.sh_size;
552 const Elf64_Off offset = m_offset + sheader.sh_offset;
553 DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554
555 if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size)
556 return 0;
557
558 m_shstr_data.SetData(buffer_sp);
559 }
560 }
561 return m_shstr_data.GetByteSize();
562}
563
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000564lldb::user_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565ObjectFileELF::GetSectionIndexByName(const char *name)
566{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000567 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
568 return 0;
569
570 // Search the collection of section headers for one with a matching name.
571 for (SectionHeaderCollIter I = m_section_headers.begin();
572 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000574 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
575
576 if (!sectionName)
577 return 0;
578
579 if (strcmp(name, sectionName) != 0)
580 continue;
581
582 return SectionIndex(I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 }
584
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000585 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586}
587
Stephen Wilson499b40e2011-03-30 16:07:05 +0000588const elf::ELFSectionHeader *
589ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
590{
591 if (!ParseSectionHeaders() || !id)
592 return NULL;
593
594 if (--id < m_section_headers.size())
595 return &m_section_headers[id];
596
597 return NULL;
598}
599
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600SectionList *
601ObjectFileELF::GetSectionList()
602{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000603 if (m_sections_ap.get())
604 return m_sections_ap.get();
605
606 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 {
608 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000610 for (SectionHeaderCollIter I = m_section_headers.begin();
611 I != m_section_headers.end(); ++I)
612 {
613 const ELFSectionHeader &header = *I;
614
615 ConstString name(m_shstr_data.PeekCStr(header.sh_name));
616 uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
617
Greg Clayton4ceb9982010-07-21 22:54:26 +0000618 static ConstString g_sect_name_text (".text");
619 static ConstString g_sect_name_data (".data");
620 static ConstString g_sect_name_bss (".bss");
621 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
622 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
623 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
624 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
625 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
626 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
627 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
628 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
629 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
630 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
631 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
632 static ConstString g_sect_name_eh_frame (".eh_frame");
633
634 SectionType sect_type = eSectionTypeOther;
635
636 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
637 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
638 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
639 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
640 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
641 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
642 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
643 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
644 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
645 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
646 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
647 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
648 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
649 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
650 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
651
652
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000653 SectionSP section(new Section(
Greg Clayton4ceb9982010-07-21 22:54:26 +0000654 0, // Parent section.
655 GetModule(), // Module to which this section belongs.
656 SectionIndex(I), // Section ID.
657 name, // Section name.
658 sect_type, // Section type.
659 header.sh_addr, // VM address.
660 header.sh_size, // VM size in bytes of this section.
661 header.sh_offset, // Offset of this section in the file.
662 size, // Size of the section as found in the file.
663 header.sh_flags)); // Flags for this section.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000664
665 m_sections_ap->AddSection(section);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 }
667 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000668
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 return m_sections_ap.get();
670}
671
Stephen Wilson499b40e2011-03-30 16:07:05 +0000672static unsigned
673ParseSymbols(Symtab *symtab,
674 user_id_t start_id,
675 SectionList *section_list,
676 const ELFSectionHeader *symtab_shdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000677 const DataExtractor &symtab_data,
678 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000680 ELFSymbol symbol;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681 uint32_t offset = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000682 const unsigned num_symbols =
683 symtab_data.GetByteSize() / symtab_shdr->sh_entsize;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000684
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000685 static ConstString text_section_name(".text");
686 static ConstString init_section_name(".init");
687 static ConstString fini_section_name(".fini");
688 static ConstString ctors_section_name(".ctors");
689 static ConstString dtors_section_name(".dtors");
690
691 static ConstString data_section_name(".data");
692 static ConstString rodata_section_name(".rodata");
693 static ConstString rodata1_section_name(".rodata1");
694 static ConstString data2_section_name(".data1");
695 static ConstString bss_section_name(".bss");
696
Stephen Wilson499b40e2011-03-30 16:07:05 +0000697 unsigned i;
698 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000700 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 break;
702
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000703 Section *symbol_section = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000705 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000707 switch (symbol_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 {
709 case SHN_ABS:
710 symbol_type = eSymbolTypeAbsolute;
711 break;
712 case SHN_UNDEF:
713 symbol_type = eSymbolTypeUndefined;
714 break;
715 default:
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000716 symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 break;
718 }
719
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000720 switch (symbol.getType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 {
722 default:
723 case STT_NOTYPE:
724 // The symbol's type is not specified.
725 break;
726
727 case STT_OBJECT:
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000728 // The symbol is associated with a data object, such as a variable,
729 // an array, etc.
730 symbol_type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 break;
732
733 case STT_FUNC:
734 // The symbol is associated with a function or other executable code.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000735 symbol_type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 break;
737
738 case STT_SECTION:
739 // The symbol is associated with a section. Symbol table entries of
740 // this type exist primarily for relocation and normally have
741 // STB_LOCAL binding.
742 break;
743
744 case STT_FILE:
745 // Conventionally, the symbol's name gives the name of the source
746 // file associated with the object file. A file symbol has STB_LOCAL
747 // binding, its section index is SHN_ABS, and it precedes the other
748 // STB_LOCAL symbols for the file, if it is present.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000749 symbol_type = eSymbolTypeObjectFile;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 break;
751 }
752
753 if (symbol_type == eSymbolTypeInvalid)
754 {
755 if (symbol_section)
756 {
757 const ConstString &sect_name = symbol_section->GetName();
758 if (sect_name == text_section_name ||
759 sect_name == init_section_name ||
760 sect_name == fini_section_name ||
761 sect_name == ctors_section_name ||
762 sect_name == dtors_section_name)
763 {
764 symbol_type = eSymbolTypeCode;
765 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000766 else if (sect_name == data_section_name ||
767 sect_name == data2_section_name ||
768 sect_name == rodata_section_name ||
769 sect_name == rodata1_section_name ||
770 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771 {
772 symbol_type = eSymbolTypeData;
773 }
774 }
775 }
776
777 uint64_t symbol_value = symbol.st_value;
778 if (symbol_section != NULL)
779 symbol_value -= symbol_section->GetFileAddress();
780 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000781 bool is_global = symbol.getBinding() == STB_GLOBAL;
782 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000784 Symbol dc_symbol(
Stephen Wilson499b40e2011-03-30 16:07:05 +0000785 i + start_id, // ID is the original symbol table index.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000786 symbol_name, // Symbol name.
787 false, // Is the symbol name mangled?
788 symbol_type, // Type of this symbol
789 is_global, // Is this globally visible?
790 false, // Is this symbol debug info?
791 false, // Is this symbol a trampoline?
792 false, // Is this symbol artificial?
793 symbol_section, // Section in which this symbol is defined or null.
794 symbol_value, // Offset in section or symbol value.
795 symbol.st_size, // Size in bytes of this symbol.
796 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 symtab->AddSymbol(dc_symbol);
798 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000799
800 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801}
802
Stephen Wilson499b40e2011-03-30 16:07:05 +0000803unsigned
804ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
805 const ELFSectionHeader *symtab_hdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000806 user_id_t symtab_id)
807{
Stephen Wilson499b40e2011-03-30 16:07:05 +0000808 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
809 symtab_hdr->sh_type == SHT_DYNSYM);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000810
811 // Parse in the section list if needed.
812 SectionList *section_list = GetSectionList();
813 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000814 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000815
816 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000817 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000818
819 Section *symtab = section_list->FindSectionByID(symtab_id).get();
820 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000821 unsigned num_symbols = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000822 if (symtab && strtab)
823 {
824 DataExtractor symtab_data;
825 DataExtractor strtab_data;
826 if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) &&
827 strtab->ReadSectionDataFromObjectFile(this, strtab_data))
828 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000829 num_symbols = ParseSymbols(symbol_table, start_id,
830 section_list, symtab_hdr,
831 symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000832 }
833 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000834
835 return num_symbols;
836}
837
838size_t
839ObjectFileELF::ParseDynamicSymbols()
840{
841 if (m_dynamic_symbols.size())
842 return m_dynamic_symbols.size();
843
844 user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC);
845 if (!dyn_id)
846 return NULL;
847
848 SectionList *section_list = GetSectionList();
849 if (!section_list)
850 return NULL;
851
852 Section *dynsym = section_list->FindSectionByID(dyn_id).get();
853 if (!dynsym)
854 return NULL;
855
856 ELFDynamic symbol;
857 DataExtractor dynsym_data;
858 if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data))
859 {
860
861 const unsigned section_size = dynsym_data.GetByteSize();
862 unsigned offset = 0;
863 unsigned cursor = 0;
864
865 while (cursor < section_size)
866 {
867 offset = cursor;
868 if (!symbol.Parse(dynsym_data, &cursor))
869 break;
870
871 m_dynamic_symbols.push_back(symbol);
872 }
873 }
874
875 return m_dynamic_symbols.size();
876}
877
878const ELFDynamic *
879ObjectFileELF::FindDynamicSymbol(unsigned tag)
880{
881 if (!ParseDynamicSymbols())
882 return NULL;
883
884 SectionList *section_list = GetSectionList();
885 if (!section_list)
886 return 0;
887
888 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
889 DynamicSymbolCollIter E = m_dynamic_symbols.end();
890 for ( ; I != E; ++I)
891 {
892 ELFDynamic *symbol = &*I;
893
894 if (symbol->d_tag == tag)
895 return symbol;
896 }
897
898 return NULL;
899}
900
901Section *
902ObjectFileELF::PLTSection()
903{
904 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
905 SectionList *section_list = GetSectionList();
906
907 if (symbol && section_list)
908 {
909 addr_t addr = symbol->d_ptr;
910 return section_list->FindSectionContainingFileAddress(addr).get();
911 }
912
913 return NULL;
914}
915
916unsigned
917ObjectFileELF::PLTRelocationType()
918{
919 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
920
921 if (symbol)
922 return symbol->d_val;
923
924 return 0;
925}
926
927static unsigned
928ParsePLTRelocations(Symtab *symbol_table,
929 user_id_t start_id,
930 unsigned rel_type,
931 const ELFHeader *hdr,
932 const ELFSectionHeader *rel_hdr,
933 const ELFSectionHeader *plt_hdr,
934 const ELFSectionHeader *sym_hdr,
935 Section *plt_section,
936 DataExtractor &rel_data,
937 DataExtractor &symtab_data,
938 DataExtractor &strtab_data)
939{
940 ELFRelocation rel(rel_type);
941 ELFSymbol symbol;
942 uint32_t offset = 0;
943 const unsigned plt_entsize = plt_hdr->sh_entsize;
944 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
945
946 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
947 reloc_info_fn reloc_type;
948 reloc_info_fn reloc_symbol;
949
950 if (hdr->Is32Bit() == 4)
951 {
952 reloc_type = ELFRelocation::RelocType32;
953 reloc_symbol = ELFRelocation::RelocSymbol32;
954 }
955 else
956 {
957 reloc_type = ELFRelocation::RelocType64;
958 reloc_symbol = ELFRelocation::RelocSymbol64;
959 }
960
961 unsigned slot_type = hdr->GetRelocationJumpSlotType();
962 unsigned i;
963 for (i = 0; i < num_relocations; ++i)
964 {
965 if (rel.Parse(rel_data, &offset) == false)
966 break;
967
968 if (reloc_type(rel) != slot_type)
969 continue;
970
971 unsigned symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
972 uint64_t plt_index = (i + 1) * plt_entsize;
973
974 if (!symbol.Parse(symtab_data, &symbol_offset))
975 break;
976
977 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
978
979 Symbol jump_symbol(
980 i + start_id, // Symbol table index
981 symbol_name, // symbol name.
982 false, // is the symbol name mangled?
983 eSymbolTypeTrampoline, // Type of this symbol
984 false, // Is this globally visible?
985 false, // Is this symbol debug info?
986 true, // Is this symbol a trampoline?
987 true, // Is this symbol artificial?
988 plt_section, // Section in which this symbol is defined or null.
989 plt_index, // Offset in section or symbol value.
990 plt_entsize, // Size in bytes of this symbol.
991 0); // Symbol flags.
992
993 symbol_table->AddSymbol(jump_symbol);
994 }
995
996 return i;
997}
998
999unsigned
1000ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1001 user_id_t start_id,
1002 const ELFSectionHeader *rel_hdr,
1003 user_id_t rel_id)
1004{
1005 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1006
1007 // The link field points to the asscoiated symbol table. The info field
1008 // points to the section holding the plt.
1009 user_id_t symtab_id = rel_hdr->sh_link;
1010 user_id_t plt_id = rel_hdr->sh_info;
1011
1012 if (!symtab_id || !plt_id)
1013 return 0;
1014
1015 // Section ID's are ones based;
1016 symtab_id++;
1017 plt_id++;
1018
1019 const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id);
1020 if (!plt_hdr)
1021 return 0;
1022
1023 const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1024 if (!sym_hdr)
1025 return 0;
1026
1027 SectionList *section_list = GetSectionList();
1028 if (!section_list)
1029 return 0;
1030
1031 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1032 if (!rel_section)
1033 return 0;
1034
1035 Section *plt_section = section_list->FindSectionByID(plt_id).get();
1036 if (!plt_section)
1037 return 0;
1038
1039 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1040 if (!symtab)
1041 return 0;
1042
1043 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1044 if (!strtab)
1045 return 0;
1046
1047 DataExtractor rel_data;
1048 if (!rel_section->ReadSectionDataFromObjectFile(this, rel_data))
1049 return 0;
1050
1051 DataExtractor symtab_data;
1052 if (!symtab->ReadSectionDataFromObjectFile(this, symtab_data))
1053 return 0;
1054
1055 DataExtractor strtab_data;
1056 if (!strtab->ReadSectionDataFromObjectFile(this, strtab_data))
1057 return 0;
1058
1059 unsigned rel_type = PLTRelocationType();
1060 if (!rel_type)
1061 return 0;
1062
1063 return ParsePLTRelocations(symbol_table, start_id, rel_type,
1064 &m_header, rel_hdr, plt_hdr, sym_hdr,
1065 plt_section,
1066 rel_data, symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001067}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001068
1069Symtab *
1070ObjectFileELF::GetSymtab()
1071{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001072 if (m_symtab_ap.get())
1073 return m_symtab_ap.get();
1074
1075 Symtab *symbol_table = new Symtab(this);
1076 m_symtab_ap.reset(symbol_table);
1077
Stephen Wilson499b40e2011-03-30 16:07:05 +00001078 Mutex::Locker locker(symbol_table->GetMutex());
Greg Clayton8087ca22010-10-08 04:20:14 +00001079
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001080 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1081 return symbol_table;
1082
1083 // Locate and parse all linker symbol tables.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001084 uint64_t symbol_id = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001085 for (SectionHeaderCollIter I = m_section_headers.begin();
1086 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087 {
Peter Collingbourneb4aabeb2011-06-03 20:39:58 +00001088 if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00001090 const ELFSectionHeader &symtab_header = *I;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001091 user_id_t section_id = SectionIndex(I);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001092 symbol_id += ParseSymbolTable(symbol_table, symbol_id,
1093 &symtab_header, section_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094 }
1095 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001096
1097 // Synthesize trampoline symbols to help navigate the PLT.
1098 Section *reloc_section = PLTSection();
1099 if (reloc_section)
1100 {
1101 user_id_t reloc_id = reloc_section->GetID();
1102 const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id);
1103 assert(reloc_header);
1104
1105 ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id);
1106 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001107
1108 return symbol_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109}
1110
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111//===----------------------------------------------------------------------===//
1112// Dump
1113//
1114// Dump the specifics of the runtime file container (such as any headers
1115// segments, sections, etc).
1116//----------------------------------------------------------------------
1117void
1118ObjectFileELF::Dump(Stream *s)
1119{
1120 DumpELFHeader(s, m_header);
1121 s->EOL();
1122 DumpELFProgramHeaders(s);
1123 s->EOL();
1124 DumpELFSectionHeaders(s);
1125 s->EOL();
1126 SectionList *section_list = GetSectionList();
1127 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00001128 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 Symtab *symtab = GetSymtab();
1130 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00001131 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001133 DumpDependentModules(s);
1134 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135}
1136
1137//----------------------------------------------------------------------
1138// DumpELFHeader
1139//
1140// Dump the ELF header to the specified output stream
1141//----------------------------------------------------------------------
1142void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001143ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001145 s->PutCString("ELF Header\n");
1146 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1147 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
1148 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1149 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
1150 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1151 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
1152 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001154 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1155 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1157 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1158 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1159
1160 s->Printf("e_type = 0x%4.4x ", header.e_type);
1161 DumpELFHeader_e_type(s, header.e_type);
1162 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
1163 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001164 s->Printf("e_entry = 0x%8.8lx\n", header.e_entry);
1165 s->Printf("e_phoff = 0x%8.8lx\n", header.e_phoff);
1166 s->Printf("e_shoff = 0x%8.8lx\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
1168 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
1169 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1170 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
1171 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1172 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
1173 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
1174}
1175
1176//----------------------------------------------------------------------
1177// DumpELFHeader_e_type
1178//
1179// Dump an token value for the ELF header member e_type
1180//----------------------------------------------------------------------
1181void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001182ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183{
1184 switch (e_type)
1185 {
1186 case ET_NONE: *s << "ET_NONE"; break;
1187 case ET_REL: *s << "ET_REL"; break;
1188 case ET_EXEC: *s << "ET_EXEC"; break;
1189 case ET_DYN: *s << "ET_DYN"; break;
1190 case ET_CORE: *s << "ET_CORE"; break;
1191 default:
1192 break;
1193 }
1194}
1195
1196//----------------------------------------------------------------------
1197// DumpELFHeader_e_ident_EI_DATA
1198//
1199// Dump an token value for the ELF header member e_ident[EI_DATA]
1200//----------------------------------------------------------------------
1201void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001202ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203{
1204 switch (ei_data)
1205 {
1206 case ELFDATANONE: *s << "ELFDATANONE"; break;
1207 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
1208 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
1209 default:
1210 break;
1211 }
1212}
1213
1214
1215//----------------------------------------------------------------------
1216// DumpELFProgramHeader
1217//
1218// Dump a single ELF program header to the specified output stream
1219//----------------------------------------------------------------------
1220void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001221ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222{
1223 DumpELFProgramHeader_p_type(s, ph.p_type);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001224 s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
1225 s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags);
1226
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 DumpELFProgramHeader_p_flags(s, ph.p_flags);
1228 s->Printf(") %8.8x", ph.p_align);
1229}
1230
1231//----------------------------------------------------------------------
1232// DumpELFProgramHeader_p_type
1233//
1234// Dump an token value for the ELF program header member p_type which
1235// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001236// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001238ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239{
1240 const int kStrWidth = 10;
1241 switch (p_type)
1242 {
1243 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
1244 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
1245 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
1246 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
1247 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
1248 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
1249 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
1250 default:
1251 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1252 break;
1253 }
1254}
1255
1256
1257//----------------------------------------------------------------------
1258// DumpELFProgramHeader_p_flags
1259//
1260// Dump an token value for the ELF program header member p_flags
1261//----------------------------------------------------------------------
1262void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001263ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264{
1265 *s << ((p_flags & PF_X) ? "PF_X" : " ")
1266 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1267 << ((p_flags & PF_W) ? "PF_W" : " ")
1268 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1269 << ((p_flags & PF_R) ? "PF_R" : " ");
1270}
1271
1272//----------------------------------------------------------------------
1273// DumpELFProgramHeaders
1274//
1275// Dump all of the ELF program header to the specified output stream
1276//----------------------------------------------------------------------
1277void
1278ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1279{
1280 if (ParseProgramHeaders())
1281 {
1282 s->PutCString("Program Headers\n");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001283 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
1284 "p_filesz p_memsz p_flags p_align\n");
1285 s->PutCString("==== ---------- -------- -------- -------- "
1286 "-------- -------- ------------------------- --------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287
1288 uint32_t idx = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001289 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1290 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001292 s->Printf("[%2u] ", idx);
1293 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294 s->EOL();
1295 }
1296 }
1297}
1298
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299//----------------------------------------------------------------------
1300// DumpELFSectionHeader
1301//
1302// Dump a single ELF section header to the specified output stream
1303//----------------------------------------------------------------------
1304void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001305ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001307 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001308 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001309 s->Printf(" %8.8lx (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001311 s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size);
1312 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
1313 s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314}
1315
1316//----------------------------------------------------------------------
1317// DumpELFSectionHeader_sh_type
1318//
1319// Dump an token value for the ELF section header member sh_type which
1320// describes the type of the section
1321//----------------------------------------------------------------------
1322void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001323ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324{
1325 const int kStrWidth = 12;
1326 switch (sh_type)
1327 {
1328 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
1329 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1330 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
1331 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
1332 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
1333 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
1334 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
1335 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
1336 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
1337 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
1338 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
1339 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
1340 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
1341 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
1342 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
1343 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
1344 default:
1345 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1346 break;
1347 }
1348}
1349
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350//----------------------------------------------------------------------
1351// DumpELFSectionHeader_sh_flags
1352//
1353// Dump an token value for the ELF section header member sh_flags
1354//----------------------------------------------------------------------
1355void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001356ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357{
1358 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
1359 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1360 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
1361 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1362 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
1363}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001364
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365//----------------------------------------------------------------------
1366// DumpELFSectionHeaders
1367//
1368// Dump all of the ELF section header to the specified output stream
1369//----------------------------------------------------------------------
1370void
1371ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1372{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001373 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1374 return;
1375
1376 s->PutCString("Section Headers\n");
1377 s->PutCString("IDX name type flags "
1378 "addr offset size link info addralgn "
1379 "entsize Name\n");
1380 s->PutCString("==== -------- ------------ -------------------------------- "
1381 "-------- -------- -------- -------- -------- -------- "
1382 "-------- ====================\n");
1383
1384 uint32_t idx = 0;
1385 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1386 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001388 s->Printf("[%2u] ", idx);
1389 ObjectFileELF::DumpELFSectionHeader(s, *I);
1390 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1391 if (section_name)
1392 *s << ' ' << section_name << "\n";
1393 }
1394}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001396void
1397ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1398{
1399 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001401 if (num_modules > 0)
1402 {
1403 s->PutCString("Dependent Modules:\n");
1404 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001406 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1407 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 }
1409 }
1410}
1411
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412bool
Greg Clayton514487e2011-02-15 21:59:32 +00001413ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00001415 if (!ParseHeader())
1416 return false;
1417
Greg Claytone0d378b2011-03-24 21:19:54 +00001418 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton64195a22011-02-23 00:35:02 +00001419 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1420 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001421 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422}
1423