blob: 09bdff889c8ddb9468ffdd0aa0334dee77ba3eb4 [file] [log] [blame]
Stephen Wilsonddd29622010-07-13 23:07:23 +00001//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
Chris Lattner24943d22010-06-08 16:52:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectFileELF.h"
11
Stephen Wilsonddd29622010-07-13 23:07:23 +000012#include <cassert>
Chris Lattner24943d22010-06-08 16:52:24 +000013#include <algorithm>
14
Stephen Wilsonf2ad3252011-01-15 00:08:44 +000015#include "lldb/Core/ArchSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/DataBuffer.h"
17#include "lldb/Core/Error.h"
Stephen Wilsonddd29622010-07-13 23:07:23 +000018#include "lldb/Core/FileSpecList.h"
Jim Ingham28775942011-03-07 23:44:08 +000019#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Section.h"
22#include "lldb/Core/Stream.h"
Jim Ingham28775942011-03-07 23:44:08 +000023#include "lldb/Symbol/SymbolContext.h"
Greg Clayton940b1032011-02-23 00:35:02 +000024#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025
Stephen Wilsonce477322011-03-30 16:07:05 +000026#include "llvm/ADT/PointerUnion.h"
27
Stephen Wilsonddd29622010-07-13 23:07:23 +000028#define CASE_AND_STREAM(s, def, width) \
29 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner24943d22010-06-08 16:52:24 +000030
Chris Lattner24943d22010-06-08 16:52:24 +000031using namespace lldb;
32using namespace lldb_private;
Stephen Wilsonddd29622010-07-13 23:07:23 +000033using namespace elf;
34using namespace llvm::ELF;
Chris Lattner24943d22010-06-08 16:52:24 +000035
Stephen Wilsonce477322011-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 Wilsonddd29622010-07-13 23:07:23 +0000143//------------------------------------------------------------------
144// Static methods.
145//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000146void
147ObjectFileELF::Initialize()
148{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000149 PluginManager::RegisterPlugin(GetPluginNameStatic(),
150 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000151 CreateInstance,
152 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000153}
154
155void
156ObjectFileELF::Terminate()
157{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000158 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000159}
160
Chris Lattner24943d22010-06-08 16:52:24 +0000161const char *
162ObjectFileELF::GetPluginNameStatic()
163{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000164 return "object-file.elf";
Chris Lattner24943d22010-06-08 16:52:24 +0000165}
166
167const char *
168ObjectFileELF::GetPluginDescriptionStatic()
169{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000170 return "ELF object file reader.";
Chris Lattner24943d22010-06-08 16:52:24 +0000171}
172
Chris Lattner24943d22010-06-08 16:52:24 +0000173ObjectFile *
Stephen Wilsonddd29622010-07-13 23:07:23 +0000174ObjectFileELF::CreateInstance(Module *module,
175 DataBufferSP &data_sp,
176 const FileSpec *file, addr_t offset,
177 addr_t length)
Chris Lattner24943d22010-06-08 16:52:24 +0000178{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000179 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
Chris Lattner24943d22010-06-08 16:52:24 +0000180 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000181 const uint8_t *magic = data_sp->GetBytes() + offset;
182 if (ELFHeader::MagicBytesMatch(magic))
183 {
184 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
185 if (address_size == 4 || address_size == 8)
186 {
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000187 std::auto_ptr<ObjectFileELF> objfile_ap(
Stephen Wilsonddd29622010-07-13 23:07:23 +0000188 new ObjectFileELF(module, data_sp, file, offset, length));
Stephen Wilsonf7cc64d2011-02-24 19:16:15 +0000189 ArchSpec spec;
190 if (objfile_ap->GetArchitecture(spec) &&
191 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonddd29622010-07-13 23:07:23 +0000192 return objfile_ap.release();
193 }
194 }
Chris Lattner24943d22010-06-08 16:52:24 +0000195 }
196 return NULL;
197}
198
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000199
Greg Claytonb5a8f142012-02-05 02:38:54 +0000200ObjectFile*
201ObjectFileELF::CreateMemoryInstance(Module* module, DataBufferSP& data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
202{
203 return NULL;
204}
205
206
Stephen Wilsonddd29622010-07-13 23:07:23 +0000207//------------------------------------------------------------------
208// PluginInterface protocol
209//------------------------------------------------------------------
210const char *
211ObjectFileELF::GetPluginName()
Chris Lattner24943d22010-06-08 16:52:24 +0000212{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000213 return "ObjectFileELF";
Chris Lattner24943d22010-06-08 16:52:24 +0000214}
215
Stephen Wilsonddd29622010-07-13 23:07:23 +0000216const char *
217ObjectFileELF::GetShortPluginName()
218{
219 return GetPluginNameStatic();
220}
Chris Lattner24943d22010-06-08 16:52:24 +0000221
Stephen Wilsonddd29622010-07-13 23:07:23 +0000222uint32_t
223ObjectFileELF::GetPluginVersion()
224{
225 return m_plugin_version;
226}
Stephen Wilsonddd29622010-07-13 23:07:23 +0000227//------------------------------------------------------------------
228// ObjectFile protocol
229//------------------------------------------------------------------
230
231ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
232 const FileSpec* file, addr_t offset,
233 addr_t length)
234 : ObjectFile(module, file, offset, length, dataSP),
235 m_header(),
236 m_program_headers(),
237 m_section_headers(),
238 m_sections_ap(),
239 m_symtab_ap(),
240 m_filespec_ap(),
241 m_shstr_data()
Chris Lattner24943d22010-06-08 16:52:24 +0000242{
243 if (file)
244 m_file = *file;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000245 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner24943d22010-06-08 16:52:24 +0000246}
247
Chris Lattner24943d22010-06-08 16:52:24 +0000248ObjectFileELF::~ObjectFileELF()
249{
250}
251
Jim Ingham7508e732010-08-09 23:31:02 +0000252bool
253ObjectFileELF::IsExecutable() const
254{
Stephen Wilson90987392011-01-15 00:09:50 +0000255 return m_header.e_entry != 0;
Jim Ingham7508e732010-08-09 23:31:02 +0000256}
257
Chris Lattner24943d22010-06-08 16:52:24 +0000258ByteOrder
Stephen Wilsonddd29622010-07-13 23:07:23 +0000259ObjectFileELF::GetByteOrder() const
Chris Lattner24943d22010-06-08 16:52:24 +0000260{
261 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
262 return eByteOrderBig;
263 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
264 return eByteOrderLittle;
265 return eByteOrderInvalid;
266}
267
268size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000269ObjectFileELF::GetAddressByteSize() const
Chris Lattner24943d22010-06-08 16:52:24 +0000270{
271 return m_data.GetAddressByteSize();
272}
273
Stephen Wilsonddd29622010-07-13 23:07:23 +0000274unsigned
275ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner24943d22010-06-08 16:52:24 +0000276{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000277 return std::distance(m_section_headers.begin(), I) + 1;
278}
Chris Lattner24943d22010-06-08 16:52:24 +0000279
Stephen Wilsonddd29622010-07-13 23:07:23 +0000280unsigned
281ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
282{
283 return std::distance(m_section_headers.begin(), I) + 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000284}
285
286bool
Stephen Wilsonddd29622010-07-13 23:07:23 +0000287ObjectFileELF::ParseHeader()
Chris Lattner24943d22010-06-08 16:52:24 +0000288{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000289 uint32_t offset = GetOffset();
290 return m_header.Parse(m_data, &offset);
291}
292
293bool
Greg Clayton0467c782011-02-04 18:53:10 +0000294ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000295{
296 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner24943d22010-06-08 16:52:24 +0000297 return false;
298}
299
300uint32_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000301ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner24943d22010-06-08 16:52:24 +0000302{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000303 size_t num_modules = ParseDependentModules();
304 uint32_t num_specs = 0;
305
306 for (unsigned i = 0; i < num_modules; ++i)
307 {
308 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
309 num_specs++;
310 }
311
312 return num_specs;
313}
314
Stephen Wilsonce477322011-03-30 16:07:05 +0000315user_id_t
316ObjectFileELF::GetSectionIndexByType(unsigned type)
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000317{
318 if (!ParseSectionHeaders())
Stephen Wilsonce477322011-03-30 16:07:05 +0000319 return 0;
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000320
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000321 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
322 sh_pos != m_section_headers.end(); ++sh_pos)
323 {
Stephen Wilsonce477322011-03-30 16:07:05 +0000324 if (sh_pos->sh_type == type)
325 return SectionIndex(sh_pos);
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000326 }
327
Stephen Wilsonce477322011-03-30 16:07:05 +0000328 return 0;
329}
330
331Address
332ObjectFileELF::GetImageInfoAddress()
333{
334 if (!ParseDynamicSymbols())
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000335 return Address();
336
337 SectionList *section_list = GetSectionList();
338 if (!section_list)
339 return Address();
340
Stephen Wilsonce477322011-03-30 16:07:05 +0000341 user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC);
342 if (!dynsym_id)
343 return Address();
344
345 const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
346 if (!dynsym_hdr)
347 return Address();
348
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000349 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
350 if (!dynsym)
351 return Address();
Stephen Wilsonce477322011-03-30 16:07:05 +0000352
353 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000354 {
Stephen Wilsonce477322011-03-30 16:07:05 +0000355 ELFDynamic &symbol = m_dynamic_symbols[i];
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000356
Stephen Wilsonce477322011-03-30 16:07:05 +0000357 if (symbol.d_tag == DT_DEBUG)
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000358 {
Stephen Wilsonce477322011-03-30 16:07:05 +0000359 // Compute the offset as the number of previous entries plus the
360 // size of d_tag.
361 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
362 return Address(dynsym, offset);
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000363 }
364 }
365
366 return Address();
367}
368
Jim Ingham28775942011-03-07 23:44:08 +0000369lldb_private::Address
370ObjectFileELF::GetEntryPointAddress ()
371{
Stephen Wilson791af3b2011-03-08 04:12:15 +0000372 SectionList *sections;
373 addr_t offset;
Jim Ingham28775942011-03-07 23:44:08 +0000374
Stephen Wilson791af3b2011-03-08 04:12:15 +0000375 if (m_entry_point_address.IsValid())
376 return m_entry_point_address;
377
378 if (!ParseHeader() || !IsExecutable())
379 return m_entry_point_address;
380
381 sections = GetSectionList();
382 offset = m_header.e_entry;
383
384 if (!sections)
385 {
386 m_entry_point_address.SetOffset(offset);
387 return m_entry_point_address;
388 }
389
390 m_entry_point_address.ResolveAddressUsingFileSections(offset, sections);
391
392 return m_entry_point_address;
Jim Ingham28775942011-03-07 23:44:08 +0000393}
394
Stephen Wilsonddd29622010-07-13 23:07:23 +0000395//----------------------------------------------------------------------
396// ParseDependentModules
397//----------------------------------------------------------------------
398size_t
399ObjectFileELF::ParseDependentModules()
400{
401 if (m_filespec_ap.get())
402 return m_filespec_ap->GetSize();
403
404 m_filespec_ap.reset(new FileSpecList());
405
406 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
407 return 0;
408
409 // Locate the dynamic table.
410 user_id_t dynsym_id = 0;
411 user_id_t dynstr_id = 0;
Greg Claytonad60bf42010-10-12 02:24:53 +0000412 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
413 sh_pos != m_section_headers.end(); ++sh_pos)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000414 {
Greg Claytonad60bf42010-10-12 02:24:53 +0000415 if (sh_pos->sh_type == SHT_DYNAMIC)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000416 {
Greg Claytonad60bf42010-10-12 02:24:53 +0000417 dynsym_id = SectionIndex(sh_pos);
418 dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000419 break;
420 }
421 }
422
423 if (!(dynsym_id && dynstr_id))
424 return 0;
425
426 SectionList *section_list = GetSectionList();
427 if (!section_list)
428 return 0;
429
430 // Resolve and load the dynamic table entries and corresponding string
431 // table.
432 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
433 Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
434 if (!(dynsym && dynstr))
435 return 0;
436
437 DataExtractor dynsym_data;
438 DataExtractor dynstr_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000439 if (ReadSectionData(dynsym, dynsym_data) &&
440 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonddd29622010-07-13 23:07:23 +0000441 {
442 ELFDynamic symbol;
443 const unsigned section_size = dynsym_data.GetByteSize();
444 unsigned offset = 0;
445
446 // The only type of entries we are concerned with are tagged DT_NEEDED,
447 // yielding the name of a required library.
448 while (offset < section_size)
449 {
450 if (!symbol.Parse(dynsym_data, &offset))
451 break;
452
453 if (symbol.d_tag != DT_NEEDED)
454 continue;
455
456 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
457 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton537a7a82010-10-20 20:54:39 +0000458 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonddd29622010-07-13 23:07:23 +0000459 }
460 }
461
462 return m_filespec_ap->GetSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000463}
464
465//----------------------------------------------------------------------
466// ParseProgramHeaders
467//----------------------------------------------------------------------
468size_t
469ObjectFileELF::ParseProgramHeaders()
470{
471 // We have already parsed the program headers
472 if (!m_program_headers.empty())
473 return m_program_headers.size();
474
Stephen Wilsonddd29622010-07-13 23:07:23 +0000475 // If there are no program headers to read we are done.
476 if (m_header.e_phnum == 0)
477 return 0;
478
479 m_program_headers.resize(m_header.e_phnum);
480 if (m_program_headers.size() != m_header.e_phnum)
481 return 0;
482
483 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000484 const elf_off ph_offset = m_header.e_phoff;
485 DataExtractor data;
486 if (GetData (ph_offset, ph_size, data) != ph_size)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000487 return 0;
488
Stephen Wilsonddd29622010-07-13 23:07:23 +0000489 uint32_t idx;
490 uint32_t offset;
491 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000492 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000493 if (m_program_headers[idx].Parse(data, &offset) == false)
494 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000495 }
496
Stephen Wilsonddd29622010-07-13 23:07:23 +0000497 if (idx < m_program_headers.size())
498 m_program_headers.resize(idx);
499
Chris Lattner24943d22010-06-08 16:52:24 +0000500 return m_program_headers.size();
501}
502
Chris Lattner24943d22010-06-08 16:52:24 +0000503//----------------------------------------------------------------------
504// ParseSectionHeaders
505//----------------------------------------------------------------------
506size_t
507ObjectFileELF::ParseSectionHeaders()
508{
509 // We have already parsed the section headers
510 if (!m_section_headers.empty())
511 return m_section_headers.size();
512
Stephen Wilsonddd29622010-07-13 23:07:23 +0000513 // If there are no section headers we are done.
514 if (m_header.e_shnum == 0)
515 return 0;
516
517 m_section_headers.resize(m_header.e_shnum);
518 if (m_section_headers.size() != m_header.e_shnum)
519 return 0;
520
521 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000522 const elf_off sh_offset = m_header.e_shoff;
523 DataExtractor data;
524 if (GetData (sh_offset, sh_size, data) != sh_size)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000525 return 0;
526
Stephen Wilsonddd29622010-07-13 23:07:23 +0000527 uint32_t idx;
528 uint32_t offset;
529 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000530 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000531 if (m_section_headers[idx].Parse(data, &offset) == false)
532 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000533 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000534 if (idx < m_section_headers.size())
535 m_section_headers.resize(idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000536
537 return m_section_headers.size();
538}
539
540size_t
541ObjectFileELF::GetSectionHeaderStringTable()
542{
543 if (m_shstr_data.GetByteSize() == 0)
544 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000545 const unsigned strtab_idx = m_header.e_shstrndx;
546
547 if (strtab_idx && strtab_idx < m_section_headers.size())
Chris Lattner24943d22010-06-08 16:52:24 +0000548 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000549 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
550 const size_t byte_size = sheader.sh_size;
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000551 const Elf64_Off offset = sheader.sh_offset;
552 m_shstr_data.SetData (m_data, offset, byte_size);
Chris Lattner24943d22010-06-08 16:52:24 +0000553
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000554 if (m_shstr_data.GetByteSize() != byte_size)
Chris Lattner24943d22010-06-08 16:52:24 +0000555 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000556 }
557 }
558 return m_shstr_data.GetByteSize();
559}
560
Stephen Wilsonddd29622010-07-13 23:07:23 +0000561lldb::user_id_t
Chris Lattner24943d22010-06-08 16:52:24 +0000562ObjectFileELF::GetSectionIndexByName(const char *name)
563{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000564 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
565 return 0;
566
567 // Search the collection of section headers for one with a matching name.
568 for (SectionHeaderCollIter I = m_section_headers.begin();
569 I != m_section_headers.end(); ++I)
Chris Lattner24943d22010-06-08 16:52:24 +0000570 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000571 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
572
573 if (!sectionName)
574 return 0;
575
576 if (strcmp(name, sectionName) != 0)
577 continue;
578
579 return SectionIndex(I);
Chris Lattner24943d22010-06-08 16:52:24 +0000580 }
581
Stephen Wilsonddd29622010-07-13 23:07:23 +0000582 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000583}
584
Stephen Wilsonce477322011-03-30 16:07:05 +0000585const elf::ELFSectionHeader *
586ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
587{
588 if (!ParseSectionHeaders() || !id)
589 return NULL;
590
591 if (--id < m_section_headers.size())
592 return &m_section_headers[id];
593
594 return NULL;
595}
596
Chris Lattner24943d22010-06-08 16:52:24 +0000597SectionList *
598ObjectFileELF::GetSectionList()
599{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000600 if (m_sections_ap.get())
601 return m_sections_ap.get();
602
603 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner24943d22010-06-08 16:52:24 +0000604 {
605 m_sections_ap.reset(new SectionList());
Chris Lattner24943d22010-06-08 16:52:24 +0000606
Stephen Wilsonddd29622010-07-13 23:07:23 +0000607 for (SectionHeaderCollIter I = m_section_headers.begin();
608 I != m_section_headers.end(); ++I)
609 {
610 const ELFSectionHeader &header = *I;
611
612 ConstString name(m_shstr_data.PeekCStr(header.sh_name));
613 uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
614
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000615 static ConstString g_sect_name_text (".text");
616 static ConstString g_sect_name_data (".data");
617 static ConstString g_sect_name_bss (".bss");
618 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
619 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
620 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
621 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
622 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
623 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
624 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
625 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
626 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
627 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
628 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
629 static ConstString g_sect_name_eh_frame (".eh_frame");
630
631 SectionType sect_type = eSectionTypeOther;
632
633 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
634 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
635 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
636 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
637 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
638 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
639 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
640 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
641 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
642 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
643 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
644 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
645 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
646 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
647 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
648
649
Stephen Wilsonddd29622010-07-13 23:07:23 +0000650 SectionSP section(new Section(
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000651 0, // Parent section.
652 GetModule(), // Module to which this section belongs.
653 SectionIndex(I), // Section ID.
654 name, // Section name.
655 sect_type, // Section type.
656 header.sh_addr, // VM address.
657 header.sh_size, // VM size in bytes of this section.
658 header.sh_offset, // Offset of this section in the file.
659 size, // Size of the section as found in the file.
660 header.sh_flags)); // Flags for this section.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000661
662 m_sections_ap->AddSection(section);
Chris Lattner24943d22010-06-08 16:52:24 +0000663 }
664 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000665
Chris Lattner24943d22010-06-08 16:52:24 +0000666 return m_sections_ap.get();
667}
668
Stephen Wilsonce477322011-03-30 16:07:05 +0000669static unsigned
670ParseSymbols(Symtab *symtab,
671 user_id_t start_id,
672 SectionList *section_list,
673 const ELFSectionHeader *symtab_shdr,
Stephen Wilsonddd29622010-07-13 23:07:23 +0000674 const DataExtractor &symtab_data,
675 const DataExtractor &strtab_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000676{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000677 ELFSymbol symbol;
Chris Lattner24943d22010-06-08 16:52:24 +0000678 uint32_t offset = 0;
Stephen Wilsonce477322011-03-30 16:07:05 +0000679 const unsigned num_symbols =
680 symtab_data.GetByteSize() / symtab_shdr->sh_entsize;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000681
Chris Lattner24943d22010-06-08 16:52:24 +0000682 static ConstString text_section_name(".text");
683 static ConstString init_section_name(".init");
684 static ConstString fini_section_name(".fini");
685 static ConstString ctors_section_name(".ctors");
686 static ConstString dtors_section_name(".dtors");
687
688 static ConstString data_section_name(".data");
689 static ConstString rodata_section_name(".rodata");
690 static ConstString rodata1_section_name(".rodata1");
691 static ConstString data2_section_name(".data1");
692 static ConstString bss_section_name(".bss");
693
Stephen Wilsonce477322011-03-30 16:07:05 +0000694 unsigned i;
695 for (i = 0; i < num_symbols; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000696 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000697 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000698 break;
699
Stephen Wilsonddd29622010-07-13 23:07:23 +0000700 Section *symbol_section = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000701 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000702 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner24943d22010-06-08 16:52:24 +0000703
Stephen Wilsonddd29622010-07-13 23:07:23 +0000704 switch (symbol_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000705 {
706 case SHN_ABS:
707 symbol_type = eSymbolTypeAbsolute;
708 break;
709 case SHN_UNDEF:
710 symbol_type = eSymbolTypeUndefined;
711 break;
712 default:
Stephen Wilsonddd29622010-07-13 23:07:23 +0000713 symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
Chris Lattner24943d22010-06-08 16:52:24 +0000714 break;
715 }
716
Stephen Wilsonddd29622010-07-13 23:07:23 +0000717 switch (symbol.getType())
Chris Lattner24943d22010-06-08 16:52:24 +0000718 {
719 default:
720 case STT_NOTYPE:
721 // The symbol's type is not specified.
722 break;
723
724 case STT_OBJECT:
Stephen Wilsonddd29622010-07-13 23:07:23 +0000725 // The symbol is associated with a data object, such as a variable,
726 // an array, etc.
727 symbol_type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +0000728 break;
729
730 case STT_FUNC:
731 // The symbol is associated with a function or other executable code.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000732 symbol_type = eSymbolTypeCode;
Chris Lattner24943d22010-06-08 16:52:24 +0000733 break;
734
735 case STT_SECTION:
736 // The symbol is associated with a section. Symbol table entries of
737 // this type exist primarily for relocation and normally have
738 // STB_LOCAL binding.
739 break;
740
741 case STT_FILE:
742 // Conventionally, the symbol's name gives the name of the source
743 // file associated with the object file. A file symbol has STB_LOCAL
744 // binding, its section index is SHN_ABS, and it precedes the other
745 // STB_LOCAL symbols for the file, if it is present.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000746 symbol_type = eSymbolTypeObjectFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000747 break;
748 }
749
750 if (symbol_type == eSymbolTypeInvalid)
751 {
752 if (symbol_section)
753 {
754 const ConstString &sect_name = symbol_section->GetName();
755 if (sect_name == text_section_name ||
756 sect_name == init_section_name ||
757 sect_name == fini_section_name ||
758 sect_name == ctors_section_name ||
759 sect_name == dtors_section_name)
760 {
761 symbol_type = eSymbolTypeCode;
762 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000763 else if (sect_name == data_section_name ||
764 sect_name == data2_section_name ||
765 sect_name == rodata_section_name ||
766 sect_name == rodata1_section_name ||
767 sect_name == bss_section_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000768 {
769 symbol_type = eSymbolTypeData;
770 }
771 }
772 }
773
774 uint64_t symbol_value = symbol.st_value;
775 if (symbol_section != NULL)
776 symbol_value -= symbol_section->GetFileAddress();
777 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000778 bool is_global = symbol.getBinding() == STB_GLOBAL;
779 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Chris Lattner24943d22010-06-08 16:52:24 +0000780
Stephen Wilsonddd29622010-07-13 23:07:23 +0000781 Symbol dc_symbol(
Stephen Wilsonce477322011-03-30 16:07:05 +0000782 i + start_id, // ID is the original symbol table index.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000783 symbol_name, // Symbol name.
784 false, // Is the symbol name mangled?
785 symbol_type, // Type of this symbol
786 is_global, // Is this globally visible?
787 false, // Is this symbol debug info?
788 false, // Is this symbol a trampoline?
789 false, // Is this symbol artificial?
790 symbol_section, // Section in which this symbol is defined or null.
791 symbol_value, // Offset in section or symbol value.
792 symbol.st_size, // Size in bytes of this symbol.
793 flags); // Symbol flags.
Chris Lattner24943d22010-06-08 16:52:24 +0000794 symtab->AddSymbol(dc_symbol);
795 }
Stephen Wilsonce477322011-03-30 16:07:05 +0000796
797 return i;
Chris Lattner24943d22010-06-08 16:52:24 +0000798}
799
Stephen Wilsonce477322011-03-30 16:07:05 +0000800unsigned
801ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
802 const ELFSectionHeader *symtab_hdr,
Stephen Wilsonddd29622010-07-13 23:07:23 +0000803 user_id_t symtab_id)
804{
Stephen Wilsonce477322011-03-30 16:07:05 +0000805 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
806 symtab_hdr->sh_type == SHT_DYNSYM);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000807
808 // Parse in the section list if needed.
809 SectionList *section_list = GetSectionList();
810 if (!section_list)
Stephen Wilsonce477322011-03-30 16:07:05 +0000811 return 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000812
813 // Section ID's are ones based.
Stephen Wilsonce477322011-03-30 16:07:05 +0000814 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000815
816 Section *symtab = section_list->FindSectionByID(symtab_id).get();
817 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Stephen Wilsonce477322011-03-30 16:07:05 +0000818 unsigned num_symbols = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000819 if (symtab && strtab)
820 {
821 DataExtractor symtab_data;
822 DataExtractor strtab_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000823 if (ReadSectionData(symtab, symtab_data) &&
824 ReadSectionData(strtab, strtab_data))
Stephen Wilsonddd29622010-07-13 23:07:23 +0000825 {
Stephen Wilsonce477322011-03-30 16:07:05 +0000826 num_symbols = ParseSymbols(symbol_table, start_id,
827 section_list, symtab_hdr,
828 symtab_data, strtab_data);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000829 }
830 }
Stephen Wilsonce477322011-03-30 16:07:05 +0000831
832 return num_symbols;
833}
834
835size_t
836ObjectFileELF::ParseDynamicSymbols()
837{
838 if (m_dynamic_symbols.size())
839 return m_dynamic_symbols.size();
840
841 user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC);
842 if (!dyn_id)
843 return NULL;
844
845 SectionList *section_list = GetSectionList();
846 if (!section_list)
847 return NULL;
848
849 Section *dynsym = section_list->FindSectionByID(dyn_id).get();
850 if (!dynsym)
851 return NULL;
852
853 ELFDynamic symbol;
854 DataExtractor dynsym_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000855 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilsonce477322011-03-30 16:07:05 +0000856 {
857
858 const unsigned section_size = dynsym_data.GetByteSize();
859 unsigned offset = 0;
860 unsigned cursor = 0;
861
862 while (cursor < section_size)
863 {
864 offset = cursor;
865 if (!symbol.Parse(dynsym_data, &cursor))
866 break;
867
868 m_dynamic_symbols.push_back(symbol);
869 }
870 }
871
872 return m_dynamic_symbols.size();
873}
874
875const ELFDynamic *
876ObjectFileELF::FindDynamicSymbol(unsigned tag)
877{
878 if (!ParseDynamicSymbols())
879 return NULL;
880
881 SectionList *section_list = GetSectionList();
882 if (!section_list)
883 return 0;
884
885 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
886 DynamicSymbolCollIter E = m_dynamic_symbols.end();
887 for ( ; I != E; ++I)
888 {
889 ELFDynamic *symbol = &*I;
890
891 if (symbol->d_tag == tag)
892 return symbol;
893 }
894
895 return NULL;
896}
897
898Section *
899ObjectFileELF::PLTSection()
900{
901 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
902 SectionList *section_list = GetSectionList();
903
904 if (symbol && section_list)
905 {
906 addr_t addr = symbol->d_ptr;
907 return section_list->FindSectionContainingFileAddress(addr).get();
908 }
909
910 return NULL;
911}
912
913unsigned
914ObjectFileELF::PLTRelocationType()
915{
916 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
917
918 if (symbol)
919 return symbol->d_val;
920
921 return 0;
922}
923
924static unsigned
925ParsePLTRelocations(Symtab *symbol_table,
926 user_id_t start_id,
927 unsigned rel_type,
928 const ELFHeader *hdr,
929 const ELFSectionHeader *rel_hdr,
930 const ELFSectionHeader *plt_hdr,
931 const ELFSectionHeader *sym_hdr,
932 Section *plt_section,
933 DataExtractor &rel_data,
934 DataExtractor &symtab_data,
935 DataExtractor &strtab_data)
936{
937 ELFRelocation rel(rel_type);
938 ELFSymbol symbol;
939 uint32_t offset = 0;
940 const unsigned plt_entsize = plt_hdr->sh_entsize;
941 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
942
943 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
944 reloc_info_fn reloc_type;
945 reloc_info_fn reloc_symbol;
946
947 if (hdr->Is32Bit() == 4)
948 {
949 reloc_type = ELFRelocation::RelocType32;
950 reloc_symbol = ELFRelocation::RelocSymbol32;
951 }
952 else
953 {
954 reloc_type = ELFRelocation::RelocType64;
955 reloc_symbol = ELFRelocation::RelocSymbol64;
956 }
957
958 unsigned slot_type = hdr->GetRelocationJumpSlotType();
959 unsigned i;
960 for (i = 0; i < num_relocations; ++i)
961 {
962 if (rel.Parse(rel_data, &offset) == false)
963 break;
964
965 if (reloc_type(rel) != slot_type)
966 continue;
967
968 unsigned symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
969 uint64_t plt_index = (i + 1) * plt_entsize;
970
971 if (!symbol.Parse(symtab_data, &symbol_offset))
972 break;
973
974 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
975
976 Symbol jump_symbol(
977 i + start_id, // Symbol table index
978 symbol_name, // symbol name.
979 false, // is the symbol name mangled?
980 eSymbolTypeTrampoline, // Type of this symbol
981 false, // Is this globally visible?
982 false, // Is this symbol debug info?
983 true, // Is this symbol a trampoline?
984 true, // Is this symbol artificial?
985 plt_section, // Section in which this symbol is defined or null.
986 plt_index, // Offset in section or symbol value.
987 plt_entsize, // Size in bytes of this symbol.
988 0); // Symbol flags.
989
990 symbol_table->AddSymbol(jump_symbol);
991 }
992
993 return i;
994}
995
996unsigned
997ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
998 user_id_t start_id,
999 const ELFSectionHeader *rel_hdr,
1000 user_id_t rel_id)
1001{
1002 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1003
1004 // The link field points to the asscoiated symbol table. The info field
1005 // points to the section holding the plt.
1006 user_id_t symtab_id = rel_hdr->sh_link;
1007 user_id_t plt_id = rel_hdr->sh_info;
1008
1009 if (!symtab_id || !plt_id)
1010 return 0;
1011
1012 // Section ID's are ones based;
1013 symtab_id++;
1014 plt_id++;
1015
1016 const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id);
1017 if (!plt_hdr)
1018 return 0;
1019
1020 const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1021 if (!sym_hdr)
1022 return 0;
1023
1024 SectionList *section_list = GetSectionList();
1025 if (!section_list)
1026 return 0;
1027
1028 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1029 if (!rel_section)
1030 return 0;
1031
1032 Section *plt_section = section_list->FindSectionByID(plt_id).get();
1033 if (!plt_section)
1034 return 0;
1035
1036 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1037 if (!symtab)
1038 return 0;
1039
1040 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1041 if (!strtab)
1042 return 0;
1043
1044 DataExtractor rel_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001045 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilsonce477322011-03-30 16:07:05 +00001046 return 0;
1047
1048 DataExtractor symtab_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001049 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilsonce477322011-03-30 16:07:05 +00001050 return 0;
1051
1052 DataExtractor strtab_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001053 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilsonce477322011-03-30 16:07:05 +00001054 return 0;
1055
1056 unsigned rel_type = PLTRelocationType();
1057 if (!rel_type)
1058 return 0;
1059
1060 return ParsePLTRelocations(symbol_table, start_id, rel_type,
1061 &m_header, rel_hdr, plt_hdr, sym_hdr,
1062 plt_section,
1063 rel_data, symtab_data, strtab_data);
Stephen Wilsonddd29622010-07-13 23:07:23 +00001064}
Chris Lattner24943d22010-06-08 16:52:24 +00001065
1066Symtab *
1067ObjectFileELF::GetSymtab()
1068{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001069 if (m_symtab_ap.get())
1070 return m_symtab_ap.get();
1071
1072 Symtab *symbol_table = new Symtab(this);
1073 m_symtab_ap.reset(symbol_table);
1074
Stephen Wilsonce477322011-03-30 16:07:05 +00001075 Mutex::Locker locker(symbol_table->GetMutex());
Greg Clayton8d3802d2010-10-08 04:20:14 +00001076
Stephen Wilsonddd29622010-07-13 23:07:23 +00001077 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1078 return symbol_table;
1079
1080 // Locate and parse all linker symbol tables.
Stephen Wilsonce477322011-03-30 16:07:05 +00001081 uint64_t symbol_id = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +00001082 for (SectionHeaderCollIter I = m_section_headers.begin();
1083 I != m_section_headers.end(); ++I)
Chris Lattner24943d22010-06-08 16:52:24 +00001084 {
Peter Collingbourne182c2862011-06-03 20:39:58 +00001085 if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001086 {
Stephen Wilsonce477322011-03-30 16:07:05 +00001087 const ELFSectionHeader &symtab_header = *I;
Stephen Wilsonddd29622010-07-13 23:07:23 +00001088 user_id_t section_id = SectionIndex(I);
Stephen Wilsonce477322011-03-30 16:07:05 +00001089 symbol_id += ParseSymbolTable(symbol_table, symbol_id,
1090 &symtab_header, section_id);
Chris Lattner24943d22010-06-08 16:52:24 +00001091 }
1092 }
Stephen Wilsonce477322011-03-30 16:07:05 +00001093
1094 // Synthesize trampoline symbols to help navigate the PLT.
1095 Section *reloc_section = PLTSection();
1096 if (reloc_section)
1097 {
1098 user_id_t reloc_id = reloc_section->GetID();
1099 const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id);
1100 assert(reloc_header);
1101
1102 ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id);
1103 }
Stephen Wilsonddd29622010-07-13 23:07:23 +00001104
1105 return symbol_table;
Chris Lattner24943d22010-06-08 16:52:24 +00001106}
1107
Chris Lattner24943d22010-06-08 16:52:24 +00001108//===----------------------------------------------------------------------===//
1109// Dump
1110//
1111// Dump the specifics of the runtime file container (such as any headers
1112// segments, sections, etc).
1113//----------------------------------------------------------------------
1114void
1115ObjectFileELF::Dump(Stream *s)
1116{
1117 DumpELFHeader(s, m_header);
1118 s->EOL();
1119 DumpELFProgramHeaders(s);
1120 s->EOL();
1121 DumpELFSectionHeaders(s);
1122 s->EOL();
1123 SectionList *section_list = GetSectionList();
1124 if (section_list)
Greg Clayton58e844b2010-12-08 05:08:21 +00001125 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00001126 Symtab *symtab = GetSymtab();
1127 if (symtab)
Greg Claytonb3448432011-03-24 21:19:54 +00001128 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner24943d22010-06-08 16:52:24 +00001129 s->EOL();
Stephen Wilsonddd29622010-07-13 23:07:23 +00001130 DumpDependentModules(s);
1131 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +00001132}
1133
1134//----------------------------------------------------------------------
1135// DumpELFHeader
1136//
1137// Dump the ELF header to the specified output stream
1138//----------------------------------------------------------------------
1139void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001140ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner24943d22010-06-08 16:52:24 +00001141{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001142 s->PutCString("ELF Header\n");
1143 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1144 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
1145 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1146 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
1147 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1148 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
1149 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner24943d22010-06-08 16:52:24 +00001150
Stephen Wilsonddd29622010-07-13 23:07:23 +00001151 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1152 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner24943d22010-06-08 16:52:24 +00001153 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1154 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1155 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1156
1157 s->Printf("e_type = 0x%4.4x ", header.e_type);
1158 DumpELFHeader_e_type(s, header.e_type);
1159 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
1160 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001161 s->Printf("e_entry = 0x%8.8llx\n", header.e_entry);
1162 s->Printf("e_phoff = 0x%8.8llx\n", header.e_phoff);
1163 s->Printf("e_shoff = 0x%8.8llx\n", header.e_shoff);
Chris Lattner24943d22010-06-08 16:52:24 +00001164 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
1165 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
1166 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1167 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
1168 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1169 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
1170 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
1171}
1172
1173//----------------------------------------------------------------------
1174// DumpELFHeader_e_type
1175//
1176// Dump an token value for the ELF header member e_type
1177//----------------------------------------------------------------------
1178void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001179ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001180{
1181 switch (e_type)
1182 {
1183 case ET_NONE: *s << "ET_NONE"; break;
1184 case ET_REL: *s << "ET_REL"; break;
1185 case ET_EXEC: *s << "ET_EXEC"; break;
1186 case ET_DYN: *s << "ET_DYN"; break;
1187 case ET_CORE: *s << "ET_CORE"; break;
1188 default:
1189 break;
1190 }
1191}
1192
1193//----------------------------------------------------------------------
1194// DumpELFHeader_e_ident_EI_DATA
1195//
1196// Dump an token value for the ELF header member e_ident[EI_DATA]
1197//----------------------------------------------------------------------
1198void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001199ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner24943d22010-06-08 16:52:24 +00001200{
1201 switch (ei_data)
1202 {
1203 case ELFDATANONE: *s << "ELFDATANONE"; break;
1204 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
1205 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
1206 default:
1207 break;
1208 }
1209}
1210
1211
1212//----------------------------------------------------------------------
1213// DumpELFProgramHeader
1214//
1215// Dump a single ELF program header to the specified output stream
1216//----------------------------------------------------------------------
1217void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001218ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner24943d22010-06-08 16:52:24 +00001219{
1220 DumpELFProgramHeader_p_type(s, ph.p_type);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001221 s->Printf(" %8.8llx %8.8llx %8.8llx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
1222 s->Printf(" %8.8llx %8.8llx %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags);
Stephen Wilsonddd29622010-07-13 23:07:23 +00001223
Chris Lattner24943d22010-06-08 16:52:24 +00001224 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001225 s->Printf(") %8.8llx", ph.p_align);
Chris Lattner24943d22010-06-08 16:52:24 +00001226}
1227
1228//----------------------------------------------------------------------
1229// DumpELFProgramHeader_p_type
1230//
1231// Dump an token value for the ELF program header member p_type which
1232// describes the type of the program header
Stephen Wilsonddd29622010-07-13 23:07:23 +00001233// ----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001234void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001235ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001236{
1237 const int kStrWidth = 10;
1238 switch (p_type)
1239 {
1240 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
1241 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
1242 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
1243 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
1244 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
1245 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
1246 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
1247 default:
1248 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1249 break;
1250 }
1251}
1252
1253
1254//----------------------------------------------------------------------
1255// DumpELFProgramHeader_p_flags
1256//
1257// Dump an token value for the ELF program header member p_flags
1258//----------------------------------------------------------------------
1259void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001260ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner24943d22010-06-08 16:52:24 +00001261{
1262 *s << ((p_flags & PF_X) ? "PF_X" : " ")
1263 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1264 << ((p_flags & PF_W) ? "PF_W" : " ")
1265 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1266 << ((p_flags & PF_R) ? "PF_R" : " ");
1267}
1268
1269//----------------------------------------------------------------------
1270// DumpELFProgramHeaders
1271//
1272// Dump all of the ELF program header to the specified output stream
1273//----------------------------------------------------------------------
1274void
1275ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1276{
1277 if (ParseProgramHeaders())
1278 {
1279 s->PutCString("Program Headers\n");
Stephen Wilsonddd29622010-07-13 23:07:23 +00001280 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
1281 "p_filesz p_memsz p_flags p_align\n");
1282 s->PutCString("==== ---------- -------- -------- -------- "
1283 "-------- -------- ------------------------- --------\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001284
1285 uint32_t idx = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +00001286 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1287 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001288 {
Stephen Wilsonddd29622010-07-13 23:07:23 +00001289 s->Printf("[%2u] ", idx);
1290 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner24943d22010-06-08 16:52:24 +00001291 s->EOL();
1292 }
1293 }
1294}
1295
Chris Lattner24943d22010-06-08 16:52:24 +00001296//----------------------------------------------------------------------
1297// DumpELFSectionHeader
1298//
1299// Dump a single ELF section header to the specified output stream
1300//----------------------------------------------------------------------
1301void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001302ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner24943d22010-06-08 16:52:24 +00001303{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001304 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner24943d22010-06-08 16:52:24 +00001305 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001306 s->Printf(" %8.8llx (", sh.sh_flags);
Chris Lattner24943d22010-06-08 16:52:24 +00001307 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001308 s->Printf(") %8.8llx %8.8llx %8.8llx", sh.sh_addr, sh.sh_offset, sh.sh_size);
Stephen Wilsonddd29622010-07-13 23:07:23 +00001309 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001310 s->Printf(" %8.8llx %8.8llx", sh.sh_addralign, sh.sh_entsize);
Chris Lattner24943d22010-06-08 16:52:24 +00001311}
1312
1313//----------------------------------------------------------------------
1314// DumpELFSectionHeader_sh_type
1315//
1316// Dump an token value for the ELF section header member sh_type which
1317// describes the type of the section
1318//----------------------------------------------------------------------
1319void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001320ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001321{
1322 const int kStrWidth = 12;
1323 switch (sh_type)
1324 {
1325 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
1326 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1327 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
1328 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
1329 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
1330 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
1331 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
1332 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
1333 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
1334 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
1335 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
1336 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
1337 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
1338 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
1339 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
1340 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
1341 default:
1342 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1343 break;
1344 }
1345}
1346
Chris Lattner24943d22010-06-08 16:52:24 +00001347//----------------------------------------------------------------------
1348// DumpELFSectionHeader_sh_flags
1349//
1350// Dump an token value for the ELF section header member sh_flags
1351//----------------------------------------------------------------------
1352void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001353ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
Chris Lattner24943d22010-06-08 16:52:24 +00001354{
1355 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
1356 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1357 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
1358 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1359 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
1360}
Stephen Wilsonddd29622010-07-13 23:07:23 +00001361
Chris Lattner24943d22010-06-08 16:52:24 +00001362//----------------------------------------------------------------------
1363// DumpELFSectionHeaders
1364//
1365// Dump all of the ELF section header to the specified output stream
1366//----------------------------------------------------------------------
1367void
1368ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1369{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001370 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1371 return;
1372
1373 s->PutCString("Section Headers\n");
1374 s->PutCString("IDX name type flags "
1375 "addr offset size link info addralgn "
1376 "entsize Name\n");
1377 s->PutCString("==== -------- ------------ -------------------------------- "
1378 "-------- -------- -------- -------- -------- -------- "
1379 "-------- ====================\n");
1380
1381 uint32_t idx = 0;
1382 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1383 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001384 {
Stephen Wilsonddd29622010-07-13 23:07:23 +00001385 s->Printf("[%2u] ", idx);
1386 ObjectFileELF::DumpELFSectionHeader(s, *I);
1387 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1388 if (section_name)
1389 *s << ' ' << section_name << "\n";
1390 }
1391}
Chris Lattner24943d22010-06-08 16:52:24 +00001392
Stephen Wilsonddd29622010-07-13 23:07:23 +00001393void
1394ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1395{
1396 size_t num_modules = ParseDependentModules();
Chris Lattner24943d22010-06-08 16:52:24 +00001397
Stephen Wilsonddd29622010-07-13 23:07:23 +00001398 if (num_modules > 0)
1399 {
1400 s->PutCString("Dependent Modules:\n");
1401 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001402 {
Stephen Wilsonddd29622010-07-13 23:07:23 +00001403 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1404 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner24943d22010-06-08 16:52:24 +00001405 }
1406 }
1407}
1408
Chris Lattner24943d22010-06-08 16:52:24 +00001409bool
Greg Clayton395fc332011-02-15 21:59:32 +00001410ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00001411{
Stephen Wilsonf7cc64d2011-02-24 19:16:15 +00001412 if (!ParseHeader())
1413 return false;
1414
Greg Claytonb3448432011-03-24 21:19:54 +00001415 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton940b1032011-02-23 00:35:02 +00001416 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1417 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonddd29622010-07-13 23:07:23 +00001418 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00001419}
1420
Greg Claytonca319972011-07-09 00:41:34 +00001421ObjectFile::Type
1422ObjectFileELF::CalculateType()
1423{
1424 switch (m_header.e_type)
1425 {
1426 case llvm::ELF::ET_NONE:
1427 // 0 - No file type
1428 return eTypeUnknown;
1429
1430 case llvm::ELF::ET_REL:
1431 // 1 - Relocatable file
1432 return eTypeObjectFile;
1433
1434 case llvm::ELF::ET_EXEC:
1435 // 2 - Executable file
1436 return eTypeExecutable;
1437
1438 case llvm::ELF::ET_DYN:
1439 // 3 - Shared object file
1440 return eTypeSharedLibrary;
1441
1442 case ET_CORE:
1443 // 4 - Core file
1444 return eTypeCoreFile;
1445
1446 default:
1447 break;
1448 }
1449 return eTypeUnknown;
1450}
1451
1452ObjectFile::Strata
1453ObjectFileELF::CalculateStrata()
1454{
1455 switch (m_header.e_type)
1456 {
1457 case llvm::ELF::ET_NONE:
1458 // 0 - No file type
1459 return eStrataUnknown;
1460
1461 case llvm::ELF::ET_REL:
1462 // 1 - Relocatable file
1463 return eStrataUnknown;
1464
1465 case llvm::ELF::ET_EXEC:
1466 // 2 - Executable file
1467 // TODO: is there any way to detect that an executable is a kernel
1468 // related executable by inspecting the program headers, section
1469 // headers, symbols, or any other flag bits???
1470 return eStrataUser;
1471
1472 case llvm::ELF::ET_DYN:
1473 // 3 - Shared object file
1474 // TODO: is there any way to detect that an shared library is a kernel
1475 // related executable by inspecting the program headers, section
1476 // headers, symbols, or any other flag bits???
1477 return eStrataUnknown;
1478
1479 case ET_CORE:
1480 // 4 - Core file
1481 // TODO: is there any way to detect that an core file is a kernel
1482 // related executable by inspecting the program headers, section
1483 // headers, symbols, or any other flag bits???
1484 return eStrataUnknown;
1485
1486 default:
1487 break;
1488 }
1489 return eStrataUnknown;
1490}
1491