blob: f4af192e38b98391dc51fc34f00f3349c7d45314 [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"
Greg Claytonf4d6de62013-04-24 22:29:28 +000020#include "lldb/Core/ModuleSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/PluginManager.h"
22#include "lldb/Core/Section.h"
23#include "lldb/Core/Stream.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000024#include "lldb/Symbol/SymbolContext.h"
Greg Clayton64195a22011-02-23 00:35:02 +000025#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Stephen Wilson499b40e2011-03-30 16:07:05 +000027#include "llvm/ADT/PointerUnion.h"
28
Stephen Wilsonf325ba92010-07-13 23:07:23 +000029#define CASE_AND_STREAM(s, def, width) \
30 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032using namespace lldb;
33using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000034using namespace elf;
35using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Stephen Wilson499b40e2011-03-30 16:07:05 +000037namespace {
38//===----------------------------------------------------------------------===//
39/// @class ELFRelocation
40/// @brief Generic wrapper for ELFRel and ELFRela.
41///
42/// This helper class allows us to parse both ELFRel and ELFRela relocation
43/// entries in a generic manner.
44class ELFRelocation
45{
46public:
47
48 /// Constructs an ELFRelocation entry with a personality as given by @p
49 /// type.
50 ///
51 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
52 ELFRelocation(unsigned type);
53
54 ~ELFRelocation();
55
56 bool
Greg Claytonc7bece562013-01-25 18:06:21 +000057 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson499b40e2011-03-30 16:07:05 +000058
59 static unsigned
60 RelocType32(const ELFRelocation &rel);
61
62 static unsigned
63 RelocType64(const ELFRelocation &rel);
64
65 static unsigned
66 RelocSymbol32(const ELFRelocation &rel);
67
68 static unsigned
69 RelocSymbol64(const ELFRelocation &rel);
70
71private:
72 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
73
74 RelocUnion reloc;
75};
76
77ELFRelocation::ELFRelocation(unsigned type)
78{
79 if (type == DT_REL)
80 reloc = new ELFRel();
81 else if (type == DT_RELA)
82 reloc = new ELFRela();
83 else {
84 assert(false && "unexpected relocation type");
85 reloc = static_cast<ELFRel*>(NULL);
86 }
87}
88
89ELFRelocation::~ELFRelocation()
90{
91 if (reloc.is<ELFRel*>())
92 delete reloc.get<ELFRel*>();
93 else
94 delete reloc.get<ELFRela*>();
95}
96
97bool
Greg Claytonc7bece562013-01-25 18:06:21 +000098ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
Stephen Wilson499b40e2011-03-30 16:07:05 +000099{
100 if (reloc.is<ELFRel*>())
101 return reloc.get<ELFRel*>()->Parse(data, offset);
102 else
103 return reloc.get<ELFRela*>()->Parse(data, offset);
104}
105
106unsigned
107ELFRelocation::RelocType32(const ELFRelocation &rel)
108{
109 if (rel.reloc.is<ELFRel*>())
110 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
111 else
112 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
113}
114
115unsigned
116ELFRelocation::RelocType64(const ELFRelocation &rel)
117{
118 if (rel.reloc.is<ELFRel*>())
119 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
120 else
121 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
122}
123
124unsigned
125ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
126{
127 if (rel.reloc.is<ELFRel*>())
128 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
129 else
130 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
131}
132
133unsigned
134ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
135{
136 if (rel.reloc.is<ELFRel*>())
137 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
138 else
139 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
140}
141
142} // end anonymous namespace
143
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000144//------------------------------------------------------------------
145// Static methods.
146//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147void
148ObjectFileELF::Initialize()
149{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000150 PluginManager::RegisterPlugin(GetPluginNameStatic(),
151 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000152 CreateInstance,
Greg Claytonf4d6de62013-04-24 22:29:28 +0000153 CreateMemoryInstance,
154 GetModuleSpecifications);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155}
156
157void
158ObjectFileELF::Terminate()
159{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000160 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161}
162
Greg Clayton57abc5d2013-05-10 21:47:16 +0000163lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164ObjectFileELF::GetPluginNameStatic()
165{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000166 static ConstString g_name("elf");
167 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168}
169
170const char *
171ObjectFileELF::GetPluginDescriptionStatic()
172{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000173 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174}
175
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +0000177ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
178 DataBufferSP &data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000179 lldb::offset_t data_offset,
180 const lldb_private::FileSpec* file,
181 lldb::offset_t file_offset,
182 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000184 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000186 data_sp = file->MemoryMapFileContents(file_offset, length);
187 data_offset = 0;
188 }
189
190 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
191 {
192 const uint8_t *magic = data_sp->GetBytes() + data_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000193 if (ELFHeader::MagicBytesMatch(magic))
194 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000195 // Update the data to contain the entire file if it doesn't already
Andrew Kaylor213f6722013-02-07 21:30:54 +0000196 if (data_sp->GetByteSize() < length) {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000197 data_sp = file->MemoryMapFileContents(file_offset, length);
Greg Clayton64ff6c72013-02-07 21:49:54 +0000198 data_offset = 0;
199 magic = data_sp->GetBytes();
Andrew Kaylor213f6722013-02-07 21:30:54 +0000200 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000201 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
202 if (address_size == 4 || address_size == 8)
203 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000204 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length));
Stephen Wilson3f4200fd2011-02-24 19:16:15 +0000205 ArchSpec spec;
206 if (objfile_ap->GetArchitecture(spec) &&
207 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000208 return objfile_ap.release();
209 }
210 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 }
212 return NULL;
213}
214
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000215
Greg Claytonc9660542012-02-05 02:38:54 +0000216ObjectFile*
Greg Claytone72dfb32012-02-24 01:59:29 +0000217ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
218 DataBufferSP& data_sp,
219 const lldb::ProcessSP &process_sp,
220 lldb::addr_t header_addr)
Greg Claytonc9660542012-02-05 02:38:54 +0000221{
222 return NULL;
223}
224
225
Greg Claytonf4d6de62013-04-24 22:29:28 +0000226size_t
227ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file,
228 lldb::DataBufferSP& data_sp,
229 lldb::offset_t data_offset,
230 lldb::offset_t file_offset,
231 lldb::offset_t length,
232 lldb_private::ModuleSpecList &specs)
233{
234 return 0;
235}
236
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000237//------------------------------------------------------------------
238// PluginInterface protocol
239//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000240lldb_private::ConstString
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000241ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000243 return GetPluginNameStatic();
244}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000246uint32_t
247ObjectFileELF::GetPluginVersion()
248{
249 return m_plugin_version;
250}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000251//------------------------------------------------------------------
252// ObjectFile protocol
253//------------------------------------------------------------------
254
Greg Claytone72dfb32012-02-24 01:59:29 +0000255ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000256 DataBufferSP& data_sp,
257 lldb::offset_t data_offset,
Greg Claytone72dfb32012-02-24 01:59:29 +0000258 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000259 lldb::offset_t file_offset,
260 lldb::offset_t length) :
261 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytone72dfb32012-02-24 01:59:29 +0000262 m_header(),
263 m_program_headers(),
264 m_section_headers(),
Greg Claytone72dfb32012-02-24 01:59:29 +0000265 m_filespec_ap(),
266 m_shstr_data()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267{
268 if (file)
269 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000270 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271}
272
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273ObjectFileELF::~ObjectFileELF()
274{
275}
276
Jim Ingham5aee1622010-08-09 23:31:02 +0000277bool
278ObjectFileELF::IsExecutable() const
279{
Stephen Wilson7f3b57c2011-01-15 00:09:50 +0000280 return m_header.e_entry != 0;
Jim Ingham5aee1622010-08-09 23:31:02 +0000281}
282
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000284ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285{
286 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
287 return eByteOrderBig;
288 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
289 return eByteOrderLittle;
290 return eByteOrderInvalid;
291}
292
Greg Claytonc7bece562013-01-25 18:06:21 +0000293uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000294ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295{
296 return m_data.GetAddressByteSize();
297}
298
Greg Claytonc7bece562013-01-25 18:06:21 +0000299size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000300ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301{
Greg Claytonc7bece562013-01-25 18:06:21 +0000302 return std::distance(m_section_headers.begin(), I) + 1u;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000303}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
Greg Claytonc7bece562013-01-25 18:06:21 +0000305size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000306ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
307{
Greg Claytonc7bece562013-01-25 18:06:21 +0000308 return std::distance(m_section_headers.begin(), I) + 1u;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309}
310
311bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000312ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000314 lldb::offset_t offset = GetFileOffset();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000315 return m_header.Parse(m_data, &offset);
316}
317
318bool
Greg Clayton60830262011-02-04 18:53:10 +0000319ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000320{
321 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 return false;
323}
324
325uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000326ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000328 size_t num_modules = ParseDependentModules();
329 uint32_t num_specs = 0;
330
331 for (unsigned i = 0; i < num_modules; ++i)
332 {
333 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
334 num_specs++;
335 }
336
337 return num_specs;
338}
339
Stephen Wilson499b40e2011-03-30 16:07:05 +0000340user_id_t
341ObjectFileELF::GetSectionIndexByType(unsigned type)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000342{
343 if (!ParseSectionHeaders())
Stephen Wilson499b40e2011-03-30 16:07:05 +0000344 return 0;
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000345
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000346 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
347 sh_pos != m_section_headers.end(); ++sh_pos)
348 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000349 if (sh_pos->sh_type == type)
350 return SectionIndex(sh_pos);
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000351 }
352
Stephen Wilson499b40e2011-03-30 16:07:05 +0000353 return 0;
354}
355
356Address
357ObjectFileELF::GetImageInfoAddress()
358{
359 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000360 return Address();
361
362 SectionList *section_list = GetSectionList();
363 if (!section_list)
364 return Address();
365
Stephen Wilson499b40e2011-03-30 16:07:05 +0000366 user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC);
367 if (!dynsym_id)
368 return Address();
369
370 const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
371 if (!dynsym_hdr)
372 return Address();
373
Greg Claytone72dfb32012-02-24 01:59:29 +0000374 SectionSP dynsym_section_sp (section_list->FindSectionByID(dynsym_id));
375 if (dynsym_section_sp)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000376 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000377 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000378 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000379 ELFDynamic &symbol = m_dynamic_symbols[i];
380
381 if (symbol.d_tag == DT_DEBUG)
382 {
383 // Compute the offset as the number of previous entries plus the
384 // size of d_tag.
385 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
386 return Address(dynsym_section_sp, offset);
387 }
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000388 }
389 }
390
391 return Address();
392}
393
Jim Ingham672e6f52011-03-07 23:44:08 +0000394lldb_private::Address
395ObjectFileELF::GetEntryPointAddress ()
396{
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000397 SectionList *sections;
398 addr_t offset;
Jim Ingham672e6f52011-03-07 23:44:08 +0000399
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000400 if (m_entry_point_address.IsValid())
401 return m_entry_point_address;
402
403 if (!ParseHeader() || !IsExecutable())
404 return m_entry_point_address;
405
406 sections = GetSectionList();
407 offset = m_header.e_entry;
408
409 if (!sections)
410 {
411 m_entry_point_address.SetOffset(offset);
412 return m_entry_point_address;
413 }
414
415 m_entry_point_address.ResolveAddressUsingFileSections(offset, sections);
416
417 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +0000418}
419
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000420//----------------------------------------------------------------------
421// ParseDependentModules
422//----------------------------------------------------------------------
423size_t
424ObjectFileELF::ParseDependentModules()
425{
426 if (m_filespec_ap.get())
427 return m_filespec_ap->GetSize();
428
429 m_filespec_ap.reset(new FileSpecList());
430
431 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
432 return 0;
433
434 // Locate the dynamic table.
435 user_id_t dynsym_id = 0;
436 user_id_t dynstr_id = 0;
Greg Clayton450e3f32010-10-12 02:24:53 +0000437 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
438 sh_pos != m_section_headers.end(); ++sh_pos)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000439 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000440 if (sh_pos->sh_type == SHT_DYNAMIC)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000441 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000442 dynsym_id = SectionIndex(sh_pos);
443 dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000444 break;
445 }
446 }
447
448 if (!(dynsym_id && dynstr_id))
449 return 0;
450
451 SectionList *section_list = GetSectionList();
452 if (!section_list)
453 return 0;
454
455 // Resolve and load the dynamic table entries and corresponding string
456 // table.
457 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
458 Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
459 if (!(dynsym && dynstr))
460 return 0;
461
462 DataExtractor dynsym_data;
463 DataExtractor dynstr_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000464 if (ReadSectionData(dynsym, dynsym_data) &&
465 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000466 {
467 ELFDynamic symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000468 const lldb::offset_t section_size = dynsym_data.GetByteSize();
469 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000470
471 // The only type of entries we are concerned with are tagged DT_NEEDED,
472 // yielding the name of a required library.
473 while (offset < section_size)
474 {
475 if (!symbol.Parse(dynsym_data, &offset))
476 break;
477
478 if (symbol.d_tag != DT_NEEDED)
479 continue;
480
481 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
482 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +0000483 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000484 }
485 }
486
487 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
490//----------------------------------------------------------------------
491// ParseProgramHeaders
492//----------------------------------------------------------------------
493size_t
494ObjectFileELF::ParseProgramHeaders()
495{
496 // We have already parsed the program headers
497 if (!m_program_headers.empty())
498 return m_program_headers.size();
499
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000500 // If there are no program headers to read we are done.
501 if (m_header.e_phnum == 0)
502 return 0;
503
504 m_program_headers.resize(m_header.e_phnum);
505 if (m_program_headers.size() != m_header.e_phnum)
506 return 0;
507
508 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
Greg Clayton44435ed2012-01-12 05:25:17 +0000509 const elf_off ph_offset = m_header.e_phoff;
510 DataExtractor data;
511 if (GetData (ph_offset, ph_size, data) != ph_size)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000512 return 0;
513
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000514 uint32_t idx;
Greg Claytonc7bece562013-01-25 18:06:21 +0000515 lldb::offset_t offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000516 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000518 if (m_program_headers[idx].Parse(data, &offset) == false)
519 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520 }
521
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000522 if (idx < m_program_headers.size())
523 m_program_headers.resize(idx);
524
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 return m_program_headers.size();
526}
527
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528//----------------------------------------------------------------------
529// ParseSectionHeaders
530//----------------------------------------------------------------------
531size_t
532ObjectFileELF::ParseSectionHeaders()
533{
534 // We have already parsed the section headers
535 if (!m_section_headers.empty())
536 return m_section_headers.size();
537
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000538 // If there are no section headers we are done.
539 if (m_header.e_shnum == 0)
540 return 0;
541
542 m_section_headers.resize(m_header.e_shnum);
543 if (m_section_headers.size() != m_header.e_shnum)
544 return 0;
545
546 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
Greg Clayton44435ed2012-01-12 05:25:17 +0000547 const elf_off sh_offset = m_header.e_shoff;
548 DataExtractor data;
549 if (GetData (sh_offset, sh_size, data) != sh_size)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000550 return 0;
551
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000552 uint32_t idx;
Greg Claytonc7bece562013-01-25 18:06:21 +0000553 lldb::offset_t offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000554 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000556 if (m_section_headers[idx].Parse(data, &offset) == false)
557 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000559 if (idx < m_section_headers.size())
560 m_section_headers.resize(idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561
562 return m_section_headers.size();
563}
564
565size_t
566ObjectFileELF::GetSectionHeaderStringTable()
567{
568 if (m_shstr_data.GetByteSize() == 0)
569 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000570 const unsigned strtab_idx = m_header.e_shstrndx;
571
572 if (strtab_idx && strtab_idx < m_section_headers.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000574 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
575 const size_t byte_size = sheader.sh_size;
Greg Clayton44435ed2012-01-12 05:25:17 +0000576 const Elf64_Off offset = sheader.sh_offset;
577 m_shstr_data.SetData (m_data, offset, byte_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578
Greg Clayton44435ed2012-01-12 05:25:17 +0000579 if (m_shstr_data.GetByteSize() != byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 }
582 }
583 return m_shstr_data.GetByteSize();
584}
585
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000586lldb::user_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587ObjectFileELF::GetSectionIndexByName(const char *name)
588{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000589 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
590 return 0;
591
592 // Search the collection of section headers for one with a matching name.
593 for (SectionHeaderCollIter I = m_section_headers.begin();
594 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000596 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
597
598 if (!sectionName)
599 return 0;
600
601 if (strcmp(name, sectionName) != 0)
602 continue;
603
604 return SectionIndex(I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 }
606
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000607 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608}
609
Stephen Wilson499b40e2011-03-30 16:07:05 +0000610const elf::ELFSectionHeader *
611ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
612{
613 if (!ParseSectionHeaders() || !id)
614 return NULL;
615
616 if (--id < m_section_headers.size())
617 return &m_section_headers[id];
618
619 return NULL;
620}
621
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622SectionList *
623ObjectFileELF::GetSectionList()
624{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000625 if (m_sections_ap.get())
626 return m_sections_ap.get();
627
628 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 {
630 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000632 for (SectionHeaderCollIter I = m_section_headers.begin();
633 I != m_section_headers.end(); ++I)
634 {
635 const ELFSectionHeader &header = *I;
636
637 ConstString name(m_shstr_data.PeekCStr(header.sh_name));
Greg Clayton47037bc2012-03-27 02:40:46 +0000638 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
639 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000640
Greg Clayton4ceb9982010-07-21 22:54:26 +0000641 static ConstString g_sect_name_text (".text");
642 static ConstString g_sect_name_data (".data");
643 static ConstString g_sect_name_bss (".bss");
Greg Clayton741f3f92012-03-27 21:10:07 +0000644 static ConstString g_sect_name_tdata (".tdata");
645 static ConstString g_sect_name_tbss (".tbss");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000646 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
647 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
648 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
649 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
650 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
651 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
652 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
653 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
654 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
655 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
656 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
657 static ConstString g_sect_name_eh_frame (".eh_frame");
658
659 SectionType sect_type = eSectionTypeOther;
660
Greg Clayton741f3f92012-03-27 21:10:07 +0000661 bool is_thread_specific = false;
662
Greg Clayton4ceb9982010-07-21 22:54:26 +0000663 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
664 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
665 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
Greg Clayton741f3f92012-03-27 21:10:07 +0000666 else if (name == g_sect_name_tdata)
667 {
668 sect_type = eSectionTypeData;
669 is_thread_specific = true;
670 }
671 else if (name == g_sect_name_tbss)
672 {
673 sect_type = eSectionTypeZeroFill;
674 is_thread_specific = true;
675 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000676 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
677 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
678 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
679 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
680 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
681 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
682 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
683 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
684 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
685 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
686 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
687 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
688
689
Greg Clayton741f3f92012-03-27 21:10:07 +0000690 SectionSP section_sp(new Section(
Greg Clayton4ceb9982010-07-21 22:54:26 +0000691 GetModule(), // Module to which this section belongs.
692 SectionIndex(I), // Section ID.
693 name, // Section name.
694 sect_type, // Section type.
695 header.sh_addr, // VM address.
Greg Clayton47037bc2012-03-27 02:40:46 +0000696 vm_size, // VM size in bytes of this section.
Greg Clayton4ceb9982010-07-21 22:54:26 +0000697 header.sh_offset, // Offset of this section in the file.
Greg Clayton47037bc2012-03-27 02:40:46 +0000698 file_size, // Size of the section as found in the file.
Greg Clayton4ceb9982010-07-21 22:54:26 +0000699 header.sh_flags)); // Flags for this section.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000700
Greg Clayton741f3f92012-03-27 21:10:07 +0000701 if (is_thread_specific)
702 section_sp->SetIsThreadSpecific (is_thread_specific);
703 m_sections_ap->AddSection(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 }
Sean Callanan56775362012-06-08 02:16:08 +0000705
706 m_sections_ap->Finalize(); // Now that we're done adding sections, finalize to build fast-lookup caches
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000708
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 return m_sections_ap.get();
710}
711
Stephen Wilson499b40e2011-03-30 16:07:05 +0000712static unsigned
713ParseSymbols(Symtab *symtab,
714 user_id_t start_id,
715 SectionList *section_list,
716 const ELFSectionHeader *symtab_shdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000717 const DataExtractor &symtab_data,
718 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000720 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000721 lldb::offset_t offset = 0;
722 const size_t num_symbols = symtab_data.GetByteSize() / symtab_shdr->sh_entsize;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000723
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 static ConstString text_section_name(".text");
725 static ConstString init_section_name(".init");
726 static ConstString fini_section_name(".fini");
727 static ConstString ctors_section_name(".ctors");
728 static ConstString dtors_section_name(".dtors");
729
730 static ConstString data_section_name(".data");
731 static ConstString rodata_section_name(".rodata");
732 static ConstString rodata1_section_name(".rodata1");
733 static ConstString data2_section_name(".data1");
734 static ConstString bss_section_name(".bss");
735
Greg Clayton9594f4c2013-04-13 23:17:23 +0000736 //StreamFile strm(stdout, false);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000737 unsigned i;
738 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000740 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 break;
Greg Clayton9594f4c2013-04-13 23:17:23 +0000742
743 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
744
745 // No need to add symbols that have no names
746 if (symbol_name == NULL || symbol_name[0] == '\0')
747 continue;
748
749 //symbol.Dump (&strm, i, &strtab_data, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750
Greg Claytone72dfb32012-02-24 01:59:29 +0000751 SectionSP symbol_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000753 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000755 switch (symbol_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 {
757 case SHN_ABS:
758 symbol_type = eSymbolTypeAbsolute;
759 break;
760 case SHN_UNDEF:
761 symbol_type = eSymbolTypeUndefined;
762 break;
763 default:
Greg Claytone72dfb32012-02-24 01:59:29 +0000764 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 break;
766 }
767
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000768 // If a symbol is undefined do not process it further even if it has a STT type
769 if (symbol_type != eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 {
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000771 switch (symbol.getType())
772 {
773 default:
774 case STT_NOTYPE:
775 // The symbol's type is not specified.
776 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000778 case STT_OBJECT:
779 // The symbol is associated with a data object, such as a variable,
780 // an array, etc.
781 symbol_type = eSymbolTypeData;
782 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000784 case STT_FUNC:
785 // The symbol is associated with a function or other executable code.
786 symbol_type = eSymbolTypeCode;
787 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000789 case STT_SECTION:
790 // The symbol is associated with a section. Symbol table entries of
791 // this type exist primarily for relocation and normally have
792 // STB_LOCAL binding.
793 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000795 case STT_FILE:
796 // Conventionally, the symbol's name gives the name of the source
797 // file associated with the object file. A file symbol has STB_LOCAL
798 // binding, its section index is SHN_ABS, and it precedes the other
799 // STB_LOCAL symbols for the file, if it is present.
Greg Clayton9594f4c2013-04-13 23:17:23 +0000800 symbol_type = eSymbolTypeSourceFile;
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000801 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000802
803 case STT_GNU_IFUNC:
804 // The symbol is associated with an indirect function. The actual
805 // function will be resolved if it is referenced.
806 symbol_type = eSymbolTypeResolver;
807 break;
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000808 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809 }
810
811 if (symbol_type == eSymbolTypeInvalid)
812 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000813 if (symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000815 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 if (sect_name == text_section_name ||
817 sect_name == init_section_name ||
818 sect_name == fini_section_name ||
819 sect_name == ctors_section_name ||
820 sect_name == dtors_section_name)
821 {
822 symbol_type = eSymbolTypeCode;
823 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000824 else if (sect_name == data_section_name ||
825 sect_name == data2_section_name ||
826 sect_name == rodata_section_name ||
827 sect_name == rodata1_section_name ||
828 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 {
830 symbol_type = eSymbolTypeData;
831 }
832 }
833 }
834
835 uint64_t symbol_value = symbol.st_value;
Greg Claytone72dfb32012-02-24 01:59:29 +0000836 if (symbol_section_sp)
837 symbol_value -= symbol_section_sp->GetFileAddress();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000838 bool is_global = symbol.getBinding() == STB_GLOBAL;
839 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Greg Clayton47037bc2012-03-27 02:40:46 +0000840 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000841 Symbol dc_symbol(
Greg Claytone72dfb32012-02-24 01:59:29 +0000842 i + start_id, // ID is the original symbol table index.
843 symbol_name, // Symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +0000844 is_mangled, // Is the symbol name mangled?
Greg Claytone72dfb32012-02-24 01:59:29 +0000845 symbol_type, // Type of this symbol
846 is_global, // Is this globally visible?
847 false, // Is this symbol debug info?
848 false, // Is this symbol a trampoline?
849 false, // Is this symbol artificial?
850 symbol_section_sp, // Section in which this symbol is defined or null.
851 symbol_value, // Offset in section or symbol value.
852 symbol.st_size, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +0000853 true, // Size is valid
Greg Claytone72dfb32012-02-24 01:59:29 +0000854 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 symtab->AddSymbol(dc_symbol);
856 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000857
858 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859}
860
Stephen Wilson499b40e2011-03-30 16:07:05 +0000861unsigned
862ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
863 const ELFSectionHeader *symtab_hdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000864 user_id_t symtab_id)
865{
Stephen Wilson499b40e2011-03-30 16:07:05 +0000866 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
867 symtab_hdr->sh_type == SHT_DYNSYM);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000868
869 // Parse in the section list if needed.
870 SectionList *section_list = GetSectionList();
871 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000872 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000873
874 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000875 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000876
877 Section *symtab = section_list->FindSectionByID(symtab_id).get();
878 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000879 unsigned num_symbols = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000880 if (symtab && strtab)
881 {
882 DataExtractor symtab_data;
883 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000884 if (ReadSectionData(symtab, symtab_data) &&
885 ReadSectionData(strtab, strtab_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000886 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000887 num_symbols = ParseSymbols(symbol_table, start_id,
888 section_list, symtab_hdr,
889 symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000890 }
891 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000892
893 return num_symbols;
894}
895
896size_t
897ObjectFileELF::ParseDynamicSymbols()
898{
899 if (m_dynamic_symbols.size())
900 return m_dynamic_symbols.size();
901
902 user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC);
903 if (!dyn_id)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000904 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000905
906 SectionList *section_list = GetSectionList();
907 if (!section_list)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000908 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000909
910 Section *dynsym = section_list->FindSectionByID(dyn_id).get();
911 if (!dynsym)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000912 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000913
914 ELFDynamic symbol;
915 DataExtractor dynsym_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000916 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +0000917 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000918 const lldb::offset_t section_size = dynsym_data.GetByteSize();
919 lldb::offset_t cursor = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000920
921 while (cursor < section_size)
922 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000923 if (!symbol.Parse(dynsym_data, &cursor))
924 break;
925
926 m_dynamic_symbols.push_back(symbol);
927 }
928 }
929
930 return m_dynamic_symbols.size();
931}
932
933const ELFDynamic *
934ObjectFileELF::FindDynamicSymbol(unsigned tag)
935{
936 if (!ParseDynamicSymbols())
937 return NULL;
938
939 SectionList *section_list = GetSectionList();
940 if (!section_list)
941 return 0;
942
943 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
944 DynamicSymbolCollIter E = m_dynamic_symbols.end();
945 for ( ; I != E; ++I)
946 {
947 ELFDynamic *symbol = &*I;
948
949 if (symbol->d_tag == tag)
950 return symbol;
951 }
952
953 return NULL;
954}
955
956Section *
957ObjectFileELF::PLTSection()
958{
959 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
960 SectionList *section_list = GetSectionList();
961
962 if (symbol && section_list)
963 {
964 addr_t addr = symbol->d_ptr;
965 return section_list->FindSectionContainingFileAddress(addr).get();
966 }
967
968 return NULL;
969}
970
971unsigned
972ObjectFileELF::PLTRelocationType()
973{
974 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
975
976 if (symbol)
977 return symbol->d_val;
978
979 return 0;
980}
981
982static unsigned
983ParsePLTRelocations(Symtab *symbol_table,
984 user_id_t start_id,
985 unsigned rel_type,
986 const ELFHeader *hdr,
987 const ELFSectionHeader *rel_hdr,
988 const ELFSectionHeader *plt_hdr,
989 const ELFSectionHeader *sym_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +0000990 const lldb::SectionSP &plt_section_sp,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000991 DataExtractor &rel_data,
992 DataExtractor &symtab_data,
993 DataExtractor &strtab_data)
994{
995 ELFRelocation rel(rel_type);
996 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000997 lldb::offset_t offset = 0;
998 const elf_xword plt_entsize = plt_hdr->sh_entsize;
999 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001000
1001 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1002 reloc_info_fn reloc_type;
1003 reloc_info_fn reloc_symbol;
1004
Greg Claytond091afe2012-11-12 22:53:16 +00001005 if (hdr->Is32Bit())
Stephen Wilson499b40e2011-03-30 16:07:05 +00001006 {
1007 reloc_type = ELFRelocation::RelocType32;
1008 reloc_symbol = ELFRelocation::RelocSymbol32;
1009 }
1010 else
1011 {
1012 reloc_type = ELFRelocation::RelocType64;
1013 reloc_symbol = ELFRelocation::RelocSymbol64;
1014 }
1015
1016 unsigned slot_type = hdr->GetRelocationJumpSlotType();
1017 unsigned i;
1018 for (i = 0; i < num_relocations; ++i)
1019 {
1020 if (rel.Parse(rel_data, &offset) == false)
1021 break;
1022
1023 if (reloc_type(rel) != slot_type)
1024 continue;
1025
Greg Claytonc7bece562013-01-25 18:06:21 +00001026 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001027 uint64_t plt_index = (i + 1) * plt_entsize;
1028
1029 if (!symbol.Parse(symtab_data, &symbol_offset))
1030 break;
1031
1032 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton47037bc2012-03-27 02:40:46 +00001033 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001034
1035 Symbol jump_symbol(
1036 i + start_id, // Symbol table index
1037 symbol_name, // symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00001038 is_mangled, // is the symbol name mangled?
Stephen Wilson499b40e2011-03-30 16:07:05 +00001039 eSymbolTypeTrampoline, // Type of this symbol
1040 false, // Is this globally visible?
1041 false, // Is this symbol debug info?
1042 true, // Is this symbol a trampoline?
1043 true, // Is this symbol artificial?
Greg Claytone72dfb32012-02-24 01:59:29 +00001044 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001045 plt_index, // Offset in section or symbol value.
1046 plt_entsize, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001047 true, // Size is valid
Stephen Wilson499b40e2011-03-30 16:07:05 +00001048 0); // Symbol flags.
1049
1050 symbol_table->AddSymbol(jump_symbol);
1051 }
1052
1053 return i;
1054}
1055
1056unsigned
1057ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1058 user_id_t start_id,
1059 const ELFSectionHeader *rel_hdr,
1060 user_id_t rel_id)
1061{
1062 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1063
1064 // The link field points to the asscoiated symbol table. The info field
1065 // points to the section holding the plt.
1066 user_id_t symtab_id = rel_hdr->sh_link;
1067 user_id_t plt_id = rel_hdr->sh_info;
1068
1069 if (!symtab_id || !plt_id)
1070 return 0;
1071
1072 // Section ID's are ones based;
1073 symtab_id++;
1074 plt_id++;
1075
1076 const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id);
1077 if (!plt_hdr)
1078 return 0;
1079
1080 const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1081 if (!sym_hdr)
1082 return 0;
1083
1084 SectionList *section_list = GetSectionList();
1085 if (!section_list)
1086 return 0;
1087
1088 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1089 if (!rel_section)
1090 return 0;
1091
Greg Claytone72dfb32012-02-24 01:59:29 +00001092 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1093 if (!plt_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001094 return 0;
1095
1096 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1097 if (!symtab)
1098 return 0;
1099
1100 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1101 if (!strtab)
1102 return 0;
1103
1104 DataExtractor rel_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001105 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001106 return 0;
1107
1108 DataExtractor symtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001109 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001110 return 0;
1111
1112 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001113 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001114 return 0;
1115
1116 unsigned rel_type = PLTRelocationType();
1117 if (!rel_type)
1118 return 0;
1119
Greg Claytone72dfb32012-02-24 01:59:29 +00001120 return ParsePLTRelocations (symbol_table,
1121 start_id,
1122 rel_type,
1123 &m_header,
1124 rel_hdr,
1125 plt_hdr,
1126 sym_hdr,
1127 plt_section_sp,
1128 rel_data,
1129 symtab_data,
1130 strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001131}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132
1133Symtab *
1134ObjectFileELF::GetSymtab()
1135{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001136 if (m_symtab_ap.get())
1137 return m_symtab_ap.get();
1138
1139 Symtab *symbol_table = new Symtab(this);
1140 m_symtab_ap.reset(symbol_table);
1141
Stephen Wilson499b40e2011-03-30 16:07:05 +00001142 Mutex::Locker locker(symbol_table->GetMutex());
Greg Clayton8087ca22010-10-08 04:20:14 +00001143
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001144 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1145 return symbol_table;
1146
1147 // Locate and parse all linker symbol tables.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001148 uint64_t symbol_id = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001149 for (SectionHeaderCollIter I = m_section_headers.begin();
1150 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001151 {
Peter Collingbourneb4aabeb2011-06-03 20:39:58 +00001152 if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00001154 const ELFSectionHeader &symtab_header = *I;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001155 user_id_t section_id = SectionIndex(I);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001156 symbol_id += ParseSymbolTable(symbol_table, symbol_id,
1157 &symtab_header, section_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 }
1159 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001160
1161 // Synthesize trampoline symbols to help navigate the PLT.
1162 Section *reloc_section = PLTSection();
1163 if (reloc_section)
1164 {
1165 user_id_t reloc_id = reloc_section->GetID();
1166 const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id);
1167 assert(reloc_header);
1168
1169 ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id);
1170 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001171
1172 return symbol_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173}
1174
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175//===----------------------------------------------------------------------===//
1176// Dump
1177//
1178// Dump the specifics of the runtime file container (such as any headers
1179// segments, sections, etc).
1180//----------------------------------------------------------------------
1181void
1182ObjectFileELF::Dump(Stream *s)
1183{
1184 DumpELFHeader(s, m_header);
1185 s->EOL();
1186 DumpELFProgramHeaders(s);
1187 s->EOL();
1188 DumpELFSectionHeaders(s);
1189 s->EOL();
1190 SectionList *section_list = GetSectionList();
1191 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00001192 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 Symtab *symtab = GetSymtab();
1194 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00001195 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001197 DumpDependentModules(s);
1198 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199}
1200
1201//----------------------------------------------------------------------
1202// DumpELFHeader
1203//
1204// Dump the ELF header to the specified output stream
1205//----------------------------------------------------------------------
1206void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001207ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001209 s->PutCString("ELF Header\n");
1210 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1211 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
1212 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1213 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
1214 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1215 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
1216 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001218 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1219 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1221 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1222 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1223
1224 s->Printf("e_type = 0x%4.4x ", header.e_type);
1225 DumpELFHeader_e_type(s, header.e_type);
1226 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
1227 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Daniel Malead01b2952012-11-29 21:49:15 +00001228 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
1229 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
1230 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
1232 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
1233 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1234 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
1235 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1236 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
1237 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
1238}
1239
1240//----------------------------------------------------------------------
1241// DumpELFHeader_e_type
1242//
1243// Dump an token value for the ELF header member e_type
1244//----------------------------------------------------------------------
1245void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001246ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247{
1248 switch (e_type)
1249 {
1250 case ET_NONE: *s << "ET_NONE"; break;
1251 case ET_REL: *s << "ET_REL"; break;
1252 case ET_EXEC: *s << "ET_EXEC"; break;
1253 case ET_DYN: *s << "ET_DYN"; break;
1254 case ET_CORE: *s << "ET_CORE"; break;
1255 default:
1256 break;
1257 }
1258}
1259
1260//----------------------------------------------------------------------
1261// DumpELFHeader_e_ident_EI_DATA
1262//
1263// Dump an token value for the ELF header member e_ident[EI_DATA]
1264//----------------------------------------------------------------------
1265void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001266ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267{
1268 switch (ei_data)
1269 {
1270 case ELFDATANONE: *s << "ELFDATANONE"; break;
1271 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
1272 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
1273 default:
1274 break;
1275 }
1276}
1277
1278
1279//----------------------------------------------------------------------
1280// DumpELFProgramHeader
1281//
1282// Dump a single ELF program header to the specified output stream
1283//----------------------------------------------------------------------
1284void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001285ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286{
1287 DumpELFProgramHeader_p_type(s, ph.p_type);
Daniel Malead01b2952012-11-29 21:49:15 +00001288 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
1289 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001290
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00001292 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293}
1294
1295//----------------------------------------------------------------------
1296// DumpELFProgramHeader_p_type
1297//
1298// Dump an token value for the ELF program header member p_type which
1299// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001300// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001301void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001302ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303{
1304 const int kStrWidth = 10;
1305 switch (p_type)
1306 {
1307 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
1308 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
1309 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
1310 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
1311 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
1312 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
1313 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
1314 default:
1315 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1316 break;
1317 }
1318}
1319
1320
1321//----------------------------------------------------------------------
1322// DumpELFProgramHeader_p_flags
1323//
1324// Dump an token value for the ELF program header member p_flags
1325//----------------------------------------------------------------------
1326void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001327ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328{
1329 *s << ((p_flags & PF_X) ? "PF_X" : " ")
1330 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1331 << ((p_flags & PF_W) ? "PF_W" : " ")
1332 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1333 << ((p_flags & PF_R) ? "PF_R" : " ");
1334}
1335
1336//----------------------------------------------------------------------
1337// DumpELFProgramHeaders
1338//
1339// Dump all of the ELF program header to the specified output stream
1340//----------------------------------------------------------------------
1341void
1342ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1343{
1344 if (ParseProgramHeaders())
1345 {
1346 s->PutCString("Program Headers\n");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001347 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
1348 "p_filesz p_memsz p_flags p_align\n");
1349 s->PutCString("==== ---------- -------- -------- -------- "
1350 "-------- -------- ------------------------- --------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351
1352 uint32_t idx = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001353 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1354 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001356 s->Printf("[%2u] ", idx);
1357 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 s->EOL();
1359 }
1360 }
1361}
1362
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363//----------------------------------------------------------------------
1364// DumpELFSectionHeader
1365//
1366// Dump a single ELF section header to the specified output stream
1367//----------------------------------------------------------------------
1368void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001369ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001371 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001372 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Daniel Malead01b2952012-11-29 21:49:15 +00001373 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00001375 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001376 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Daniel Malead01b2952012-11-29 21:49:15 +00001377 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378}
1379
1380//----------------------------------------------------------------------
1381// DumpELFSectionHeader_sh_type
1382//
1383// Dump an token value for the ELF section header member sh_type which
1384// describes the type of the section
1385//----------------------------------------------------------------------
1386void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001387ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388{
1389 const int kStrWidth = 12;
1390 switch (sh_type)
1391 {
1392 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
1393 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1394 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
1395 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
1396 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
1397 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
1398 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
1399 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
1400 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
1401 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
1402 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
1403 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
1404 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
1405 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
1406 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
1407 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
1408 default:
1409 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1410 break;
1411 }
1412}
1413
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414//----------------------------------------------------------------------
1415// DumpELFSectionHeader_sh_flags
1416//
1417// Dump an token value for the ELF section header member sh_flags
1418//----------------------------------------------------------------------
1419void
Greg Claytonc7bece562013-01-25 18:06:21 +00001420ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421{
1422 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
1423 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1424 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
1425 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1426 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
1427}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001428
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429//----------------------------------------------------------------------
1430// DumpELFSectionHeaders
1431//
1432// Dump all of the ELF section header to the specified output stream
1433//----------------------------------------------------------------------
1434void
1435ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1436{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001437 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1438 return;
1439
1440 s->PutCString("Section Headers\n");
1441 s->PutCString("IDX name type flags "
1442 "addr offset size link info addralgn "
1443 "entsize Name\n");
1444 s->PutCString("==== -------- ------------ -------------------------------- "
1445 "-------- -------- -------- -------- -------- -------- "
1446 "-------- ====================\n");
1447
1448 uint32_t idx = 0;
1449 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1450 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001452 s->Printf("[%2u] ", idx);
1453 ObjectFileELF::DumpELFSectionHeader(s, *I);
1454 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1455 if (section_name)
1456 *s << ' ' << section_name << "\n";
1457 }
1458}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001460void
1461ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1462{
1463 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001465 if (num_modules > 0)
1466 {
1467 s->PutCString("Dependent Modules:\n");
1468 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001470 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1471 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 }
1473 }
1474}
1475
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476bool
Greg Clayton514487e2011-02-15 21:59:32 +00001477ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00001479 if (!ParseHeader())
1480 return false;
1481
Greg Claytone0d378b2011-03-24 21:19:54 +00001482 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton64195a22011-02-23 00:35:02 +00001483 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1484 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001485 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001486}
1487
Greg Clayton9e00b6a652011-07-09 00:41:34 +00001488ObjectFile::Type
1489ObjectFileELF::CalculateType()
1490{
1491 switch (m_header.e_type)
1492 {
1493 case llvm::ELF::ET_NONE:
1494 // 0 - No file type
1495 return eTypeUnknown;
1496
1497 case llvm::ELF::ET_REL:
1498 // 1 - Relocatable file
1499 return eTypeObjectFile;
1500
1501 case llvm::ELF::ET_EXEC:
1502 // 2 - Executable file
1503 return eTypeExecutable;
1504
1505 case llvm::ELF::ET_DYN:
1506 // 3 - Shared object file
1507 return eTypeSharedLibrary;
1508
1509 case ET_CORE:
1510 // 4 - Core file
1511 return eTypeCoreFile;
1512
1513 default:
1514 break;
1515 }
1516 return eTypeUnknown;
1517}
1518
1519ObjectFile::Strata
1520ObjectFileELF::CalculateStrata()
1521{
1522 switch (m_header.e_type)
1523 {
1524 case llvm::ELF::ET_NONE:
1525 // 0 - No file type
1526 return eStrataUnknown;
1527
1528 case llvm::ELF::ET_REL:
1529 // 1 - Relocatable file
1530 return eStrataUnknown;
1531
1532 case llvm::ELF::ET_EXEC:
1533 // 2 - Executable file
1534 // TODO: is there any way to detect that an executable is a kernel
1535 // related executable by inspecting the program headers, section
1536 // headers, symbols, or any other flag bits???
1537 return eStrataUser;
1538
1539 case llvm::ELF::ET_DYN:
1540 // 3 - Shared object file
1541 // TODO: is there any way to detect that an shared library is a kernel
1542 // related executable by inspecting the program headers, section
1543 // headers, symbols, or any other flag bits???
1544 return eStrataUnknown;
1545
1546 case ET_CORE:
1547 // 4 - Core file
1548 // TODO: is there any way to detect that an core file is a kernel
1549 // related executable by inspecting the program headers, section
1550 // headers, symbols, or any other flag bits???
1551 return eStrataUnknown;
1552
1553 default:
1554 break;
1555 }
1556 return eStrataUnknown;
1557}
1558