blob: 4ba081a7b7ca582bb8262e0a9f62e2ebd801001d [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163const char *
164ObjectFileELF::GetPluginNameStatic()
165{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000166 return "object-file.elf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
169const char *
170ObjectFileELF::GetPluginDescriptionStatic()
171{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000172 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173}
174
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +0000176ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
177 DataBufferSP &data_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000178 lldb::offset_t data_offset,
179 const lldb_private::FileSpec* file,
180 lldb::offset_t file_offset,
181 lldb::offset_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000183 if (!data_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000185 data_sp = file->MemoryMapFileContents(file_offset, length);
186 data_offset = 0;
187 }
188
189 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
190 {
191 const uint8_t *magic = data_sp->GetBytes() + data_offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000192 if (ELFHeader::MagicBytesMatch(magic))
193 {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000194 // Update the data to contain the entire file if it doesn't already
Andrew Kaylor213f6722013-02-07 21:30:54 +0000195 if (data_sp->GetByteSize() < length) {
Greg Clayton5ce9c562013-02-06 17:22:03 +0000196 data_sp = file->MemoryMapFileContents(file_offset, length);
Greg Clayton64ff6c72013-02-07 21:49:54 +0000197 data_offset = 0;
198 magic = data_sp->GetBytes();
Andrew Kaylor213f6722013-02-07 21:30:54 +0000199 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000200 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
201 if (address_size == 4 || address_size == 8)
202 {
Greg Clayton7b0992d2013-04-18 22:45:39 +0000203 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 +0000204 ArchSpec spec;
205 if (objfile_ap->GetArchitecture(spec) &&
206 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000207 return objfile_ap.release();
208 }
209 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 }
211 return NULL;
212}
213
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000214
Greg Claytonc9660542012-02-05 02:38:54 +0000215ObjectFile*
Greg Claytone72dfb32012-02-24 01:59:29 +0000216ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
217 DataBufferSP& data_sp,
218 const lldb::ProcessSP &process_sp,
219 lldb::addr_t header_addr)
Greg Claytonc9660542012-02-05 02:38:54 +0000220{
221 return NULL;
222}
223
224
Greg Claytonf4d6de62013-04-24 22:29:28 +0000225size_t
226ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file,
227 lldb::DataBufferSP& data_sp,
228 lldb::offset_t data_offset,
229 lldb::offset_t file_offset,
230 lldb::offset_t length,
231 lldb_private::ModuleSpecList &specs)
232{
233 return 0;
234}
235
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000236//------------------------------------------------------------------
237// PluginInterface protocol
238//------------------------------------------------------------------
239const char *
240ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000242 return "ObjectFileELF";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243}
244
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000245const char *
246ObjectFileELF::GetShortPluginName()
247{
248 return GetPluginNameStatic();
249}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000251uint32_t
252ObjectFileELF::GetPluginVersion()
253{
254 return m_plugin_version;
255}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000256//------------------------------------------------------------------
257// ObjectFile protocol
258//------------------------------------------------------------------
259
Greg Claytone72dfb32012-02-24 01:59:29 +0000260ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000261 DataBufferSP& data_sp,
262 lldb::offset_t data_offset,
Greg Claytone72dfb32012-02-24 01:59:29 +0000263 const FileSpec* file,
Greg Clayton5ce9c562013-02-06 17:22:03 +0000264 lldb::offset_t file_offset,
265 lldb::offset_t length) :
266 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
Greg Claytone72dfb32012-02-24 01:59:29 +0000267 m_header(),
268 m_program_headers(),
269 m_section_headers(),
Greg Claytone72dfb32012-02-24 01:59:29 +0000270 m_filespec_ap(),
271 m_shstr_data()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272{
273 if (file)
274 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000275 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276}
277
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278ObjectFileELF::~ObjectFileELF()
279{
280}
281
Jim Ingham5aee1622010-08-09 23:31:02 +0000282bool
283ObjectFileELF::IsExecutable() const
284{
Stephen Wilson7f3b57c2011-01-15 00:09:50 +0000285 return m_header.e_entry != 0;
Jim Ingham5aee1622010-08-09 23:31:02 +0000286}
287
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000289ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290{
291 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
292 return eByteOrderBig;
293 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
294 return eByteOrderLittle;
295 return eByteOrderInvalid;
296}
297
Greg Claytonc7bece562013-01-25 18:06:21 +0000298uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000299ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300{
301 return m_data.GetAddressByteSize();
302}
303
Greg Claytonc7bece562013-01-25 18:06:21 +0000304size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000305ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306{
Greg Claytonc7bece562013-01-25 18:06:21 +0000307 return std::distance(m_section_headers.begin(), I) + 1u;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000308}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309
Greg Claytonc7bece562013-01-25 18:06:21 +0000310size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000311ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
312{
Greg Claytonc7bece562013-01-25 18:06:21 +0000313 return std::distance(m_section_headers.begin(), I) + 1u;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314}
315
316bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000317ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318{
Greg Clayton5ce9c562013-02-06 17:22:03 +0000319 lldb::offset_t offset = GetFileOffset();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000320 return m_header.Parse(m_data, &offset);
321}
322
323bool
Greg Clayton60830262011-02-04 18:53:10 +0000324ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000325{
326 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 return false;
328}
329
330uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000331ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000333 size_t num_modules = ParseDependentModules();
334 uint32_t num_specs = 0;
335
336 for (unsigned i = 0; i < num_modules; ++i)
337 {
338 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
339 num_specs++;
340 }
341
342 return num_specs;
343}
344
Stephen Wilson499b40e2011-03-30 16:07:05 +0000345user_id_t
346ObjectFileELF::GetSectionIndexByType(unsigned type)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000347{
348 if (!ParseSectionHeaders())
Stephen Wilson499b40e2011-03-30 16:07:05 +0000349 return 0;
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000350
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000351 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
352 sh_pos != m_section_headers.end(); ++sh_pos)
353 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000354 if (sh_pos->sh_type == type)
355 return SectionIndex(sh_pos);
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000356 }
357
Stephen Wilson499b40e2011-03-30 16:07:05 +0000358 return 0;
359}
360
361Address
362ObjectFileELF::GetImageInfoAddress()
363{
364 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000365 return Address();
366
367 SectionList *section_list = GetSectionList();
368 if (!section_list)
369 return Address();
370
Stephen Wilson499b40e2011-03-30 16:07:05 +0000371 user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC);
372 if (!dynsym_id)
373 return Address();
374
375 const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
376 if (!dynsym_hdr)
377 return Address();
378
Greg Claytone72dfb32012-02-24 01:59:29 +0000379 SectionSP dynsym_section_sp (section_list->FindSectionByID(dynsym_id));
380 if (dynsym_section_sp)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000381 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000382 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000383 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000384 ELFDynamic &symbol = m_dynamic_symbols[i];
385
386 if (symbol.d_tag == DT_DEBUG)
387 {
388 // Compute the offset as the number of previous entries plus the
389 // size of d_tag.
390 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
391 return Address(dynsym_section_sp, offset);
392 }
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000393 }
394 }
395
396 return Address();
397}
398
Jim Ingham672e6f52011-03-07 23:44:08 +0000399lldb_private::Address
400ObjectFileELF::GetEntryPointAddress ()
401{
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000402 SectionList *sections;
403 addr_t offset;
Jim Ingham672e6f52011-03-07 23:44:08 +0000404
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000405 if (m_entry_point_address.IsValid())
406 return m_entry_point_address;
407
408 if (!ParseHeader() || !IsExecutable())
409 return m_entry_point_address;
410
411 sections = GetSectionList();
412 offset = m_header.e_entry;
413
414 if (!sections)
415 {
416 m_entry_point_address.SetOffset(offset);
417 return m_entry_point_address;
418 }
419
420 m_entry_point_address.ResolveAddressUsingFileSections(offset, sections);
421
422 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +0000423}
424
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000425//----------------------------------------------------------------------
426// ParseDependentModules
427//----------------------------------------------------------------------
428size_t
429ObjectFileELF::ParseDependentModules()
430{
431 if (m_filespec_ap.get())
432 return m_filespec_ap->GetSize();
433
434 m_filespec_ap.reset(new FileSpecList());
435
436 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
437 return 0;
438
439 // Locate the dynamic table.
440 user_id_t dynsym_id = 0;
441 user_id_t dynstr_id = 0;
Greg Clayton450e3f32010-10-12 02:24:53 +0000442 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
443 sh_pos != m_section_headers.end(); ++sh_pos)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000444 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000445 if (sh_pos->sh_type == SHT_DYNAMIC)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000446 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000447 dynsym_id = SectionIndex(sh_pos);
448 dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000449 break;
450 }
451 }
452
453 if (!(dynsym_id && dynstr_id))
454 return 0;
455
456 SectionList *section_list = GetSectionList();
457 if (!section_list)
458 return 0;
459
460 // Resolve and load the dynamic table entries and corresponding string
461 // table.
462 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
463 Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
464 if (!(dynsym && dynstr))
465 return 0;
466
467 DataExtractor dynsym_data;
468 DataExtractor dynstr_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000469 if (ReadSectionData(dynsym, dynsym_data) &&
470 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000471 {
472 ELFDynamic symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000473 const lldb::offset_t section_size = dynsym_data.GetByteSize();
474 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000475
476 // The only type of entries we are concerned with are tagged DT_NEEDED,
477 // yielding the name of a required library.
478 while (offset < section_size)
479 {
480 if (!symbol.Parse(dynsym_data, &offset))
481 break;
482
483 if (symbol.d_tag != DT_NEEDED)
484 continue;
485
486 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
487 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +0000488 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000489 }
490 }
491
492 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493}
494
495//----------------------------------------------------------------------
496// ParseProgramHeaders
497//----------------------------------------------------------------------
498size_t
499ObjectFileELF::ParseProgramHeaders()
500{
501 // We have already parsed the program headers
502 if (!m_program_headers.empty())
503 return m_program_headers.size();
504
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000505 // If there are no program headers to read we are done.
506 if (m_header.e_phnum == 0)
507 return 0;
508
509 m_program_headers.resize(m_header.e_phnum);
510 if (m_program_headers.size() != m_header.e_phnum)
511 return 0;
512
513 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
Greg Clayton44435ed2012-01-12 05:25:17 +0000514 const elf_off ph_offset = m_header.e_phoff;
515 DataExtractor data;
516 if (GetData (ph_offset, ph_size, data) != ph_size)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000517 return 0;
518
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000519 uint32_t idx;
Greg Claytonc7bece562013-01-25 18:06:21 +0000520 lldb::offset_t offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000521 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000523 if (m_program_headers[idx].Parse(data, &offset) == false)
524 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 }
526
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000527 if (idx < m_program_headers.size())
528 m_program_headers.resize(idx);
529
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 return m_program_headers.size();
531}
532
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533//----------------------------------------------------------------------
534// ParseSectionHeaders
535//----------------------------------------------------------------------
536size_t
537ObjectFileELF::ParseSectionHeaders()
538{
539 // We have already parsed the section headers
540 if (!m_section_headers.empty())
541 return m_section_headers.size();
542
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000543 // If there are no section headers we are done.
544 if (m_header.e_shnum == 0)
545 return 0;
546
547 m_section_headers.resize(m_header.e_shnum);
548 if (m_section_headers.size() != m_header.e_shnum)
549 return 0;
550
551 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
Greg Clayton44435ed2012-01-12 05:25:17 +0000552 const elf_off sh_offset = m_header.e_shoff;
553 DataExtractor data;
554 if (GetData (sh_offset, sh_size, data) != sh_size)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000555 return 0;
556
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000557 uint32_t idx;
Greg Claytonc7bece562013-01-25 18:06:21 +0000558 lldb::offset_t offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000559 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000561 if (m_section_headers[idx].Parse(data, &offset) == false)
562 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000564 if (idx < m_section_headers.size())
565 m_section_headers.resize(idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566
567 return m_section_headers.size();
568}
569
570size_t
571ObjectFileELF::GetSectionHeaderStringTable()
572{
573 if (m_shstr_data.GetByteSize() == 0)
574 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000575 const unsigned strtab_idx = m_header.e_shstrndx;
576
577 if (strtab_idx && strtab_idx < m_section_headers.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000579 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
580 const size_t byte_size = sheader.sh_size;
Greg Clayton44435ed2012-01-12 05:25:17 +0000581 const Elf64_Off offset = sheader.sh_offset;
582 m_shstr_data.SetData (m_data, offset, byte_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583
Greg Clayton44435ed2012-01-12 05:25:17 +0000584 if (m_shstr_data.GetByteSize() != byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586 }
587 }
588 return m_shstr_data.GetByteSize();
589}
590
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000591lldb::user_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592ObjectFileELF::GetSectionIndexByName(const char *name)
593{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000594 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
595 return 0;
596
597 // Search the collection of section headers for one with a matching name.
598 for (SectionHeaderCollIter I = m_section_headers.begin();
599 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000601 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
602
603 if (!sectionName)
604 return 0;
605
606 if (strcmp(name, sectionName) != 0)
607 continue;
608
609 return SectionIndex(I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 }
611
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000612 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613}
614
Stephen Wilson499b40e2011-03-30 16:07:05 +0000615const elf::ELFSectionHeader *
616ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
617{
618 if (!ParseSectionHeaders() || !id)
619 return NULL;
620
621 if (--id < m_section_headers.size())
622 return &m_section_headers[id];
623
624 return NULL;
625}
626
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627SectionList *
628ObjectFileELF::GetSectionList()
629{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000630 if (m_sections_ap.get())
631 return m_sections_ap.get();
632
633 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 {
635 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000637 for (SectionHeaderCollIter I = m_section_headers.begin();
638 I != m_section_headers.end(); ++I)
639 {
640 const ELFSectionHeader &header = *I;
641
642 ConstString name(m_shstr_data.PeekCStr(header.sh_name));
Greg Clayton47037bc2012-03-27 02:40:46 +0000643 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
644 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000645
Greg Clayton4ceb9982010-07-21 22:54:26 +0000646 static ConstString g_sect_name_text (".text");
647 static ConstString g_sect_name_data (".data");
648 static ConstString g_sect_name_bss (".bss");
Greg Clayton741f3f92012-03-27 21:10:07 +0000649 static ConstString g_sect_name_tdata (".tdata");
650 static ConstString g_sect_name_tbss (".tbss");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000651 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
652 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
653 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
654 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
655 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
656 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
657 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
658 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
659 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
660 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
661 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
662 static ConstString g_sect_name_eh_frame (".eh_frame");
663
664 SectionType sect_type = eSectionTypeOther;
665
Greg Clayton741f3f92012-03-27 21:10:07 +0000666 bool is_thread_specific = false;
667
Greg Clayton4ceb9982010-07-21 22:54:26 +0000668 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
669 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
670 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
Greg Clayton741f3f92012-03-27 21:10:07 +0000671 else if (name == g_sect_name_tdata)
672 {
673 sect_type = eSectionTypeData;
674 is_thread_specific = true;
675 }
676 else if (name == g_sect_name_tbss)
677 {
678 sect_type = eSectionTypeZeroFill;
679 is_thread_specific = true;
680 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000681 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
682 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
683 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
684 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
685 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
686 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
687 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
688 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
689 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
690 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
691 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
692 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
693
694
Greg Clayton741f3f92012-03-27 21:10:07 +0000695 SectionSP section_sp(new Section(
Greg Clayton4ceb9982010-07-21 22:54:26 +0000696 GetModule(), // Module to which this section belongs.
697 SectionIndex(I), // Section ID.
698 name, // Section name.
699 sect_type, // Section type.
700 header.sh_addr, // VM address.
Greg Clayton47037bc2012-03-27 02:40:46 +0000701 vm_size, // VM size in bytes of this section.
Greg Clayton4ceb9982010-07-21 22:54:26 +0000702 header.sh_offset, // Offset of this section in the file.
Greg Clayton47037bc2012-03-27 02:40:46 +0000703 file_size, // Size of the section as found in the file.
Greg Clayton4ceb9982010-07-21 22:54:26 +0000704 header.sh_flags)); // Flags for this section.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000705
Greg Clayton741f3f92012-03-27 21:10:07 +0000706 if (is_thread_specific)
707 section_sp->SetIsThreadSpecific (is_thread_specific);
708 m_sections_ap->AddSection(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 }
Sean Callanan56775362012-06-08 02:16:08 +0000710
711 m_sections_ap->Finalize(); // Now that we're done adding sections, finalize to build fast-lookup caches
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000713
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 return m_sections_ap.get();
715}
716
Stephen Wilson499b40e2011-03-30 16:07:05 +0000717static unsigned
718ParseSymbols(Symtab *symtab,
719 user_id_t start_id,
720 SectionList *section_list,
721 const ELFSectionHeader *symtab_shdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000722 const DataExtractor &symtab_data,
723 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000725 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000726 lldb::offset_t offset = 0;
727 const size_t num_symbols = symtab_data.GetByteSize() / symtab_shdr->sh_entsize;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000728
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 static ConstString text_section_name(".text");
730 static ConstString init_section_name(".init");
731 static ConstString fini_section_name(".fini");
732 static ConstString ctors_section_name(".ctors");
733 static ConstString dtors_section_name(".dtors");
734
735 static ConstString data_section_name(".data");
736 static ConstString rodata_section_name(".rodata");
737 static ConstString rodata1_section_name(".rodata1");
738 static ConstString data2_section_name(".data1");
739 static ConstString bss_section_name(".bss");
740
Greg Clayton9594f4c2013-04-13 23:17:23 +0000741 //StreamFile strm(stdout, false);
Stephen Wilson499b40e2011-03-30 16:07:05 +0000742 unsigned i;
743 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000745 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746 break;
Greg Clayton9594f4c2013-04-13 23:17:23 +0000747
748 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
749
750 // No need to add symbols that have no names
751 if (symbol_name == NULL || symbol_name[0] == '\0')
752 continue;
753
754 //symbol.Dump (&strm, i, &strtab_data, section_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755
Greg Claytone72dfb32012-02-24 01:59:29 +0000756 SectionSP symbol_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000758 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000760 switch (symbol_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 {
762 case SHN_ABS:
763 symbol_type = eSymbolTypeAbsolute;
764 break;
765 case SHN_UNDEF:
766 symbol_type = eSymbolTypeUndefined;
767 break;
768 default:
Greg Claytone72dfb32012-02-24 01:59:29 +0000769 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 break;
771 }
772
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000773 // If a symbol is undefined do not process it further even if it has a STT type
774 if (symbol_type != eSymbolTypeUndefined)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775 {
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000776 switch (symbol.getType())
777 {
778 default:
779 case STT_NOTYPE:
780 // The symbol's type is not specified.
781 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000783 case STT_OBJECT:
784 // The symbol is associated with a data object, such as a variable,
785 // an array, etc.
786 symbol_type = eSymbolTypeData;
787 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000789 case STT_FUNC:
790 // The symbol is associated with a function or other executable code.
791 symbol_type = eSymbolTypeCode;
792 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000794 case STT_SECTION:
795 // The symbol is associated with a section. Symbol table entries of
796 // this type exist primarily for relocation and normally have
797 // STB_LOCAL binding.
798 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000800 case STT_FILE:
801 // Conventionally, the symbol's name gives the name of the source
802 // file associated with the object file. A file symbol has STB_LOCAL
803 // binding, its section index is SHN_ABS, and it precedes the other
804 // STB_LOCAL symbols for the file, if it is present.
Greg Clayton9594f4c2013-04-13 23:17:23 +0000805 symbol_type = eSymbolTypeSourceFile;
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000806 break;
Matt Kopec00049b82013-02-27 20:13:38 +0000807
808 case STT_GNU_IFUNC:
809 // The symbol is associated with an indirect function. The actual
810 // function will be resolved if it is referenced.
811 symbol_type = eSymbolTypeResolver;
812 break;
Matt Kopec92dd5cf2013-02-12 18:30:30 +0000813 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 }
815
816 if (symbol_type == eSymbolTypeInvalid)
817 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000818 if (symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000820 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 if (sect_name == text_section_name ||
822 sect_name == init_section_name ||
823 sect_name == fini_section_name ||
824 sect_name == ctors_section_name ||
825 sect_name == dtors_section_name)
826 {
827 symbol_type = eSymbolTypeCode;
828 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000829 else if (sect_name == data_section_name ||
830 sect_name == data2_section_name ||
831 sect_name == rodata_section_name ||
832 sect_name == rodata1_section_name ||
833 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834 {
835 symbol_type = eSymbolTypeData;
836 }
837 }
838 }
839
840 uint64_t symbol_value = symbol.st_value;
Greg Claytone72dfb32012-02-24 01:59:29 +0000841 if (symbol_section_sp)
842 symbol_value -= symbol_section_sp->GetFileAddress();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000843 bool is_global = symbol.getBinding() == STB_GLOBAL;
844 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Greg Clayton47037bc2012-03-27 02:40:46 +0000845 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000846 Symbol dc_symbol(
Greg Claytone72dfb32012-02-24 01:59:29 +0000847 i + start_id, // ID is the original symbol table index.
848 symbol_name, // Symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +0000849 is_mangled, // Is the symbol name mangled?
Greg Claytone72dfb32012-02-24 01:59:29 +0000850 symbol_type, // Type of this symbol
851 is_global, // Is this globally visible?
852 false, // Is this symbol debug info?
853 false, // Is this symbol a trampoline?
854 false, // Is this symbol artificial?
855 symbol_section_sp, // Section in which this symbol is defined or null.
856 symbol_value, // Offset in section or symbol value.
857 symbol.st_size, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +0000858 true, // Size is valid
Greg Claytone72dfb32012-02-24 01:59:29 +0000859 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 symtab->AddSymbol(dc_symbol);
861 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000862
863 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864}
865
Stephen Wilson499b40e2011-03-30 16:07:05 +0000866unsigned
867ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
868 const ELFSectionHeader *symtab_hdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000869 user_id_t symtab_id)
870{
Stephen Wilson499b40e2011-03-30 16:07:05 +0000871 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
872 symtab_hdr->sh_type == SHT_DYNSYM);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000873
874 // Parse in the section list if needed.
875 SectionList *section_list = GetSectionList();
876 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000877 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000878
879 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000880 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000881
882 Section *symtab = section_list->FindSectionByID(symtab_id).get();
883 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000884 unsigned num_symbols = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000885 if (symtab && strtab)
886 {
887 DataExtractor symtab_data;
888 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000889 if (ReadSectionData(symtab, symtab_data) &&
890 ReadSectionData(strtab, strtab_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000891 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000892 num_symbols = ParseSymbols(symbol_table, start_id,
893 section_list, symtab_hdr,
894 symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000895 }
896 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000897
898 return num_symbols;
899}
900
901size_t
902ObjectFileELF::ParseDynamicSymbols()
903{
904 if (m_dynamic_symbols.size())
905 return m_dynamic_symbols.size();
906
907 user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC);
908 if (!dyn_id)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000909 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000910
911 SectionList *section_list = GetSectionList();
912 if (!section_list)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000913 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000914
915 Section *dynsym = section_list->FindSectionByID(dyn_id).get();
916 if (!dynsym)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000917 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000918
919 ELFDynamic symbol;
920 DataExtractor dynsym_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000921 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +0000922 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000923 const lldb::offset_t section_size = dynsym_data.GetByteSize();
924 lldb::offset_t cursor = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000925
926 while (cursor < section_size)
927 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000928 if (!symbol.Parse(dynsym_data, &cursor))
929 break;
930
931 m_dynamic_symbols.push_back(symbol);
932 }
933 }
934
935 return m_dynamic_symbols.size();
936}
937
938const ELFDynamic *
939ObjectFileELF::FindDynamicSymbol(unsigned tag)
940{
941 if (!ParseDynamicSymbols())
942 return NULL;
943
944 SectionList *section_list = GetSectionList();
945 if (!section_list)
946 return 0;
947
948 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
949 DynamicSymbolCollIter E = m_dynamic_symbols.end();
950 for ( ; I != E; ++I)
951 {
952 ELFDynamic *symbol = &*I;
953
954 if (symbol->d_tag == tag)
955 return symbol;
956 }
957
958 return NULL;
959}
960
961Section *
962ObjectFileELF::PLTSection()
963{
964 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
965 SectionList *section_list = GetSectionList();
966
967 if (symbol && section_list)
968 {
969 addr_t addr = symbol->d_ptr;
970 return section_list->FindSectionContainingFileAddress(addr).get();
971 }
972
973 return NULL;
974}
975
976unsigned
977ObjectFileELF::PLTRelocationType()
978{
979 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
980
981 if (symbol)
982 return symbol->d_val;
983
984 return 0;
985}
986
987static unsigned
988ParsePLTRelocations(Symtab *symbol_table,
989 user_id_t start_id,
990 unsigned rel_type,
991 const ELFHeader *hdr,
992 const ELFSectionHeader *rel_hdr,
993 const ELFSectionHeader *plt_hdr,
994 const ELFSectionHeader *sym_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +0000995 const lldb::SectionSP &plt_section_sp,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000996 DataExtractor &rel_data,
997 DataExtractor &symtab_data,
998 DataExtractor &strtab_data)
999{
1000 ELFRelocation rel(rel_type);
1001 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +00001002 lldb::offset_t offset = 0;
1003 const elf_xword plt_entsize = plt_hdr->sh_entsize;
1004 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001005
1006 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1007 reloc_info_fn reloc_type;
1008 reloc_info_fn reloc_symbol;
1009
Greg Claytond091afe2012-11-12 22:53:16 +00001010 if (hdr->Is32Bit())
Stephen Wilson499b40e2011-03-30 16:07:05 +00001011 {
1012 reloc_type = ELFRelocation::RelocType32;
1013 reloc_symbol = ELFRelocation::RelocSymbol32;
1014 }
1015 else
1016 {
1017 reloc_type = ELFRelocation::RelocType64;
1018 reloc_symbol = ELFRelocation::RelocSymbol64;
1019 }
1020
1021 unsigned slot_type = hdr->GetRelocationJumpSlotType();
1022 unsigned i;
1023 for (i = 0; i < num_relocations; ++i)
1024 {
1025 if (rel.Parse(rel_data, &offset) == false)
1026 break;
1027
1028 if (reloc_type(rel) != slot_type)
1029 continue;
1030
Greg Claytonc7bece562013-01-25 18:06:21 +00001031 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001032 uint64_t plt_index = (i + 1) * plt_entsize;
1033
1034 if (!symbol.Parse(symtab_data, &symbol_offset))
1035 break;
1036
1037 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton47037bc2012-03-27 02:40:46 +00001038 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilson499b40e2011-03-30 16:07:05 +00001039
1040 Symbol jump_symbol(
1041 i + start_id, // Symbol table index
1042 symbol_name, // symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +00001043 is_mangled, // is the symbol name mangled?
Stephen Wilson499b40e2011-03-30 16:07:05 +00001044 eSymbolTypeTrampoline, // Type of this symbol
1045 false, // Is this globally visible?
1046 false, // Is this symbol debug info?
1047 true, // Is this symbol a trampoline?
1048 true, // Is this symbol artificial?
Greg Claytone72dfb32012-02-24 01:59:29 +00001049 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001050 plt_index, // Offset in section or symbol value.
1051 plt_entsize, // Size in bytes of this symbol.
Greg Clayton9594f4c2013-04-13 23:17:23 +00001052 true, // Size is valid
Stephen Wilson499b40e2011-03-30 16:07:05 +00001053 0); // Symbol flags.
1054
1055 symbol_table->AddSymbol(jump_symbol);
1056 }
1057
1058 return i;
1059}
1060
1061unsigned
1062ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1063 user_id_t start_id,
1064 const ELFSectionHeader *rel_hdr,
1065 user_id_t rel_id)
1066{
1067 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1068
1069 // The link field points to the asscoiated symbol table. The info field
1070 // points to the section holding the plt.
1071 user_id_t symtab_id = rel_hdr->sh_link;
1072 user_id_t plt_id = rel_hdr->sh_info;
1073
1074 if (!symtab_id || !plt_id)
1075 return 0;
1076
1077 // Section ID's are ones based;
1078 symtab_id++;
1079 plt_id++;
1080
1081 const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id);
1082 if (!plt_hdr)
1083 return 0;
1084
1085 const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1086 if (!sym_hdr)
1087 return 0;
1088
1089 SectionList *section_list = GetSectionList();
1090 if (!section_list)
1091 return 0;
1092
1093 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1094 if (!rel_section)
1095 return 0;
1096
Greg Claytone72dfb32012-02-24 01:59:29 +00001097 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1098 if (!plt_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001099 return 0;
1100
1101 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1102 if (!symtab)
1103 return 0;
1104
1105 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1106 if (!strtab)
1107 return 0;
1108
1109 DataExtractor rel_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001110 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001111 return 0;
1112
1113 DataExtractor symtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001114 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001115 return 0;
1116
1117 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001118 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001119 return 0;
1120
1121 unsigned rel_type = PLTRelocationType();
1122 if (!rel_type)
1123 return 0;
1124
Greg Claytone72dfb32012-02-24 01:59:29 +00001125 return ParsePLTRelocations (symbol_table,
1126 start_id,
1127 rel_type,
1128 &m_header,
1129 rel_hdr,
1130 plt_hdr,
1131 sym_hdr,
1132 plt_section_sp,
1133 rel_data,
1134 symtab_data,
1135 strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001136}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137
1138Symtab *
1139ObjectFileELF::GetSymtab()
1140{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001141 if (m_symtab_ap.get())
1142 return m_symtab_ap.get();
1143
1144 Symtab *symbol_table = new Symtab(this);
1145 m_symtab_ap.reset(symbol_table);
1146
Stephen Wilson499b40e2011-03-30 16:07:05 +00001147 Mutex::Locker locker(symbol_table->GetMutex());
Greg Clayton8087ca22010-10-08 04:20:14 +00001148
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001149 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1150 return symbol_table;
1151
1152 // Locate and parse all linker symbol tables.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001153 uint64_t symbol_id = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001154 for (SectionHeaderCollIter I = m_section_headers.begin();
1155 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 {
Peter Collingbourneb4aabeb2011-06-03 20:39:58 +00001157 if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00001159 const ELFSectionHeader &symtab_header = *I;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001160 user_id_t section_id = SectionIndex(I);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001161 symbol_id += ParseSymbolTable(symbol_table, symbol_id,
1162 &symtab_header, section_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 }
1164 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001165
1166 // Synthesize trampoline symbols to help navigate the PLT.
1167 Section *reloc_section = PLTSection();
1168 if (reloc_section)
1169 {
1170 user_id_t reloc_id = reloc_section->GetID();
1171 const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id);
1172 assert(reloc_header);
1173
1174 ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id);
1175 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001176
1177 return symbol_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178}
1179
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180//===----------------------------------------------------------------------===//
1181// Dump
1182//
1183// Dump the specifics of the runtime file container (such as any headers
1184// segments, sections, etc).
1185//----------------------------------------------------------------------
1186void
1187ObjectFileELF::Dump(Stream *s)
1188{
1189 DumpELFHeader(s, m_header);
1190 s->EOL();
1191 DumpELFProgramHeaders(s);
1192 s->EOL();
1193 DumpELFSectionHeaders(s);
1194 s->EOL();
1195 SectionList *section_list = GetSectionList();
1196 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00001197 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 Symtab *symtab = GetSymtab();
1199 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00001200 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001202 DumpDependentModules(s);
1203 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204}
1205
1206//----------------------------------------------------------------------
1207// DumpELFHeader
1208//
1209// Dump the ELF header to the specified output stream
1210//----------------------------------------------------------------------
1211void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001212ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001214 s->PutCString("ELF Header\n");
1215 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1216 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
1217 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1218 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
1219 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1220 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
1221 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001223 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1224 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1226 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1227 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1228
1229 s->Printf("e_type = 0x%4.4x ", header.e_type);
1230 DumpELFHeader_e_type(s, header.e_type);
1231 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
1232 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Daniel Malead01b2952012-11-29 21:49:15 +00001233 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
1234 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
1235 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
1237 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
1238 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1239 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
1240 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1241 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
1242 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
1243}
1244
1245//----------------------------------------------------------------------
1246// DumpELFHeader_e_type
1247//
1248// Dump an token value for the ELF header member e_type
1249//----------------------------------------------------------------------
1250void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001251ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252{
1253 switch (e_type)
1254 {
1255 case ET_NONE: *s << "ET_NONE"; break;
1256 case ET_REL: *s << "ET_REL"; break;
1257 case ET_EXEC: *s << "ET_EXEC"; break;
1258 case ET_DYN: *s << "ET_DYN"; break;
1259 case ET_CORE: *s << "ET_CORE"; break;
1260 default:
1261 break;
1262 }
1263}
1264
1265//----------------------------------------------------------------------
1266// DumpELFHeader_e_ident_EI_DATA
1267//
1268// Dump an token value for the ELF header member e_ident[EI_DATA]
1269//----------------------------------------------------------------------
1270void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001271ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272{
1273 switch (ei_data)
1274 {
1275 case ELFDATANONE: *s << "ELFDATANONE"; break;
1276 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
1277 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
1278 default:
1279 break;
1280 }
1281}
1282
1283
1284//----------------------------------------------------------------------
1285// DumpELFProgramHeader
1286//
1287// Dump a single ELF program header to the specified output stream
1288//----------------------------------------------------------------------
1289void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001290ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291{
1292 DumpELFProgramHeader_p_type(s, ph.p_type);
Daniel Malead01b2952012-11-29 21:49:15 +00001293 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
1294 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 +00001295
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001296 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00001297 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001298}
1299
1300//----------------------------------------------------------------------
1301// DumpELFProgramHeader_p_type
1302//
1303// Dump an token value for the ELF program header member p_type which
1304// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001305// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001307ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001308{
1309 const int kStrWidth = 10;
1310 switch (p_type)
1311 {
1312 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
1313 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
1314 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
1315 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
1316 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
1317 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
1318 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
1319 default:
1320 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1321 break;
1322 }
1323}
1324
1325
1326//----------------------------------------------------------------------
1327// DumpELFProgramHeader_p_flags
1328//
1329// Dump an token value for the ELF program header member p_flags
1330//----------------------------------------------------------------------
1331void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001332ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333{
1334 *s << ((p_flags & PF_X) ? "PF_X" : " ")
1335 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1336 << ((p_flags & PF_W) ? "PF_W" : " ")
1337 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1338 << ((p_flags & PF_R) ? "PF_R" : " ");
1339}
1340
1341//----------------------------------------------------------------------
1342// DumpELFProgramHeaders
1343//
1344// Dump all of the ELF program header to the specified output stream
1345//----------------------------------------------------------------------
1346void
1347ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1348{
1349 if (ParseProgramHeaders())
1350 {
1351 s->PutCString("Program Headers\n");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001352 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
1353 "p_filesz p_memsz p_flags p_align\n");
1354 s->PutCString("==== ---------- -------- -------- -------- "
1355 "-------- -------- ------------------------- --------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356
1357 uint32_t idx = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001358 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1359 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001361 s->Printf("[%2u] ", idx);
1362 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363 s->EOL();
1364 }
1365 }
1366}
1367
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368//----------------------------------------------------------------------
1369// DumpELFSectionHeader
1370//
1371// Dump a single ELF section header to the specified output stream
1372//----------------------------------------------------------------------
1373void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001374ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001376 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001377 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Daniel Malead01b2952012-11-29 21:49:15 +00001378 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00001380 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 +00001381 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Daniel Malead01b2952012-11-29 21:49:15 +00001382 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383}
1384
1385//----------------------------------------------------------------------
1386// DumpELFSectionHeader_sh_type
1387//
1388// Dump an token value for the ELF section header member sh_type which
1389// describes the type of the section
1390//----------------------------------------------------------------------
1391void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001392ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393{
1394 const int kStrWidth = 12;
1395 switch (sh_type)
1396 {
1397 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
1398 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1399 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
1400 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
1401 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
1402 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
1403 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
1404 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
1405 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
1406 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
1407 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
1408 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
1409 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
1410 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
1411 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
1412 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
1413 default:
1414 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1415 break;
1416 }
1417}
1418
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419//----------------------------------------------------------------------
1420// DumpELFSectionHeader_sh_flags
1421//
1422// Dump an token value for the ELF section header member sh_flags
1423//----------------------------------------------------------------------
1424void
Greg Claytonc7bece562013-01-25 18:06:21 +00001425ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426{
1427 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
1428 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1429 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
1430 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1431 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
1432}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001433
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434//----------------------------------------------------------------------
1435// DumpELFSectionHeaders
1436//
1437// Dump all of the ELF section header to the specified output stream
1438//----------------------------------------------------------------------
1439void
1440ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1441{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001442 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1443 return;
1444
1445 s->PutCString("Section Headers\n");
1446 s->PutCString("IDX name type flags "
1447 "addr offset size link info addralgn "
1448 "entsize Name\n");
1449 s->PutCString("==== -------- ------------ -------------------------------- "
1450 "-------- -------- -------- -------- -------- -------- "
1451 "-------- ====================\n");
1452
1453 uint32_t idx = 0;
1454 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1455 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001457 s->Printf("[%2u] ", idx);
1458 ObjectFileELF::DumpELFSectionHeader(s, *I);
1459 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1460 if (section_name)
1461 *s << ' ' << section_name << "\n";
1462 }
1463}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001465void
1466ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1467{
1468 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001470 if (num_modules > 0)
1471 {
1472 s->PutCString("Dependent Modules:\n");
1473 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001475 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1476 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001477 }
1478 }
1479}
1480
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481bool
Greg Clayton514487e2011-02-15 21:59:32 +00001482ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00001484 if (!ParseHeader())
1485 return false;
1486
Greg Claytone0d378b2011-03-24 21:19:54 +00001487 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton64195a22011-02-23 00:35:02 +00001488 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1489 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001490 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491}
1492
Greg Clayton9e00b6a652011-07-09 00:41:34 +00001493ObjectFile::Type
1494ObjectFileELF::CalculateType()
1495{
1496 switch (m_header.e_type)
1497 {
1498 case llvm::ELF::ET_NONE:
1499 // 0 - No file type
1500 return eTypeUnknown;
1501
1502 case llvm::ELF::ET_REL:
1503 // 1 - Relocatable file
1504 return eTypeObjectFile;
1505
1506 case llvm::ELF::ET_EXEC:
1507 // 2 - Executable file
1508 return eTypeExecutable;
1509
1510 case llvm::ELF::ET_DYN:
1511 // 3 - Shared object file
1512 return eTypeSharedLibrary;
1513
1514 case ET_CORE:
1515 // 4 - Core file
1516 return eTypeCoreFile;
1517
1518 default:
1519 break;
1520 }
1521 return eTypeUnknown;
1522}
1523
1524ObjectFile::Strata
1525ObjectFileELF::CalculateStrata()
1526{
1527 switch (m_header.e_type)
1528 {
1529 case llvm::ELF::ET_NONE:
1530 // 0 - No file type
1531 return eStrataUnknown;
1532
1533 case llvm::ELF::ET_REL:
1534 // 1 - Relocatable file
1535 return eStrataUnknown;
1536
1537 case llvm::ELF::ET_EXEC:
1538 // 2 - Executable file
1539 // TODO: is there any way to detect that an executable is a kernel
1540 // related executable by inspecting the program headers, section
1541 // headers, symbols, or any other flag bits???
1542 return eStrataUser;
1543
1544 case llvm::ELF::ET_DYN:
1545 // 3 - Shared object file
1546 // TODO: is there any way to detect that an shared library is a kernel
1547 // related executable by inspecting the program headers, section
1548 // headers, symbols, or any other flag bits???
1549 return eStrataUnknown;
1550
1551 case ET_CORE:
1552 // 4 - Core file
1553 // TODO: is there any way to detect that an core file is a kernel
1554 // related executable by inspecting the program headers, section
1555 // headers, symbols, or any other flag bits???
1556 return eStrataUnknown;
1557
1558 default:
1559 break;
1560 }
1561 return eStrataUnknown;
1562}
1563