blob: a430c2644998d7e480deedbf42fe44cbf7901aaa [file] [log] [blame]
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectFileELF.h"
11
Stephen Wilsonf325ba92010-07-13 23:07:23 +000012#include <cassert>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include <algorithm>
14
Stephen Wilson2ab0a582011-01-15 00:08:44 +000015#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/DataBuffer.h"
17#include "lldb/Core/Error.h"
Stephen Wilsonf325ba92010-07-13 23:07:23 +000018#include "lldb/Core/FileSpecList.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000019#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Section.h"
22#include "lldb/Core/Stream.h"
Jim Ingham672e6f52011-03-07 23:44:08 +000023#include "lldb/Symbol/SymbolContext.h"
Greg Clayton64195a22011-02-23 00:35:02 +000024#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Stephen Wilson499b40e2011-03-30 16:07:05 +000026#include "llvm/ADT/PointerUnion.h"
27
Stephen Wilsonf325ba92010-07-13 23:07:23 +000028#define CASE_AND_STREAM(s, def, width) \
29 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031using namespace lldb;
32using namespace lldb_private;
Stephen Wilsonf325ba92010-07-13 23:07:23 +000033using namespace elf;
34using namespace llvm::ELF;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Stephen Wilson499b40e2011-03-30 16:07:05 +000036namespace {
37//===----------------------------------------------------------------------===//
38/// @class ELFRelocation
39/// @brief Generic wrapper for ELFRel and ELFRela.
40///
41/// This helper class allows us to parse both ELFRel and ELFRela relocation
42/// entries in a generic manner.
43class ELFRelocation
44{
45public:
46
47 /// Constructs an ELFRelocation entry with a personality as given by @p
48 /// type.
49 ///
50 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
51 ELFRelocation(unsigned type);
52
53 ~ELFRelocation();
54
55 bool
Greg Claytonc7bece562013-01-25 18:06:21 +000056 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
Stephen Wilson499b40e2011-03-30 16:07:05 +000057
58 static unsigned
59 RelocType32(const ELFRelocation &rel);
60
61 static unsigned
62 RelocType64(const ELFRelocation &rel);
63
64 static unsigned
65 RelocSymbol32(const ELFRelocation &rel);
66
67 static unsigned
68 RelocSymbol64(const ELFRelocation &rel);
69
70private:
71 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
72
73 RelocUnion reloc;
74};
75
76ELFRelocation::ELFRelocation(unsigned type)
77{
78 if (type == DT_REL)
79 reloc = new ELFRel();
80 else if (type == DT_RELA)
81 reloc = new ELFRela();
82 else {
83 assert(false && "unexpected relocation type");
84 reloc = static_cast<ELFRel*>(NULL);
85 }
86}
87
88ELFRelocation::~ELFRelocation()
89{
90 if (reloc.is<ELFRel*>())
91 delete reloc.get<ELFRel*>();
92 else
93 delete reloc.get<ELFRela*>();
94}
95
96bool
Greg Claytonc7bece562013-01-25 18:06:21 +000097ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
Stephen Wilson499b40e2011-03-30 16:07:05 +000098{
99 if (reloc.is<ELFRel*>())
100 return reloc.get<ELFRel*>()->Parse(data, offset);
101 else
102 return reloc.get<ELFRela*>()->Parse(data, offset);
103}
104
105unsigned
106ELFRelocation::RelocType32(const ELFRelocation &rel)
107{
108 if (rel.reloc.is<ELFRel*>())
109 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
110 else
111 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
112}
113
114unsigned
115ELFRelocation::RelocType64(const ELFRelocation &rel)
116{
117 if (rel.reloc.is<ELFRel*>())
118 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
119 else
120 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
121}
122
123unsigned
124ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
125{
126 if (rel.reloc.is<ELFRel*>())
127 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
128 else
129 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
130}
131
132unsigned
133ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
134{
135 if (rel.reloc.is<ELFRel*>())
136 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
137 else
138 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
139}
140
141} // end anonymous namespace
142
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000143//------------------------------------------------------------------
144// Static methods.
145//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146void
147ObjectFileELF::Initialize()
148{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000149 PluginManager::RegisterPlugin(GetPluginNameStatic(),
150 GetPluginDescriptionStatic(),
Greg Claytonc9660542012-02-05 02:38:54 +0000151 CreateInstance,
152 CreateMemoryInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153}
154
155void
156ObjectFileELF::Terminate()
157{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000158 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159}
160
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161const char *
162ObjectFileELF::GetPluginNameStatic()
163{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000164 return "object-file.elf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
167const char *
168ObjectFileELF::GetPluginDescriptionStatic()
169{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000170 return "ELF object file reader.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171}
172
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173ObjectFile *
Greg Claytone72dfb32012-02-24 01:59:29 +0000174ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
175 DataBufferSP &data_sp,
176 const FileSpec *file,
177 addr_t offset,
178 addr_t length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000180 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000182 const uint8_t *magic = data_sp->GetBytes() + offset;
183 if (ELFHeader::MagicBytesMatch(magic))
184 {
185 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
186 if (address_size == 4 || address_size == 8)
187 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000188 std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, file, offset, length));
Stephen Wilson3f4200fd2011-02-24 19:16:15 +0000189 ArchSpec spec;
190 if (objfile_ap->GetArchitecture(spec) &&
191 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000192 return objfile_ap.release();
193 }
194 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 }
196 return NULL;
197}
198
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000199
Greg Claytonc9660542012-02-05 02:38:54 +0000200ObjectFile*
Greg Claytone72dfb32012-02-24 01:59:29 +0000201ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
202 DataBufferSP& data_sp,
203 const lldb::ProcessSP &process_sp,
204 lldb::addr_t header_addr)
Greg Claytonc9660542012-02-05 02:38:54 +0000205{
206 return NULL;
207}
208
209
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000210//------------------------------------------------------------------
211// PluginInterface protocol
212//------------------------------------------------------------------
213const char *
214ObjectFileELF::GetPluginName()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000216 return "ObjectFileELF";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000219const char *
220ObjectFileELF::GetShortPluginName()
221{
222 return GetPluginNameStatic();
223}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000225uint32_t
226ObjectFileELF::GetPluginVersion()
227{
228 return m_plugin_version;
229}
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000230//------------------------------------------------------------------
231// ObjectFile protocol
232//------------------------------------------------------------------
233
Greg Claytone72dfb32012-02-24 01:59:29 +0000234ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
235 DataBufferSP& dataSP,
236 const FileSpec* file,
237 addr_t offset,
238 addr_t length) :
239 ObjectFile(module_sp, file, offset, length, dataSP),
240 m_header(),
241 m_program_headers(),
242 m_section_headers(),
243 m_sections_ap(),
244 m_symtab_ap(),
245 m_filespec_ap(),
246 m_shstr_data()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247{
248 if (file)
249 m_file = *file;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000250 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253ObjectFileELF::~ObjectFileELF()
254{
255}
256
Jim Ingham5aee1622010-08-09 23:31:02 +0000257bool
258ObjectFileELF::IsExecutable() const
259{
Stephen Wilson7f3b57c2011-01-15 00:09:50 +0000260 return m_header.e_entry != 0;
Jim Ingham5aee1622010-08-09 23:31:02 +0000261}
262
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263ByteOrder
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000264ObjectFileELF::GetByteOrder() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265{
266 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
267 return eByteOrderBig;
268 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
269 return eByteOrderLittle;
270 return eByteOrderInvalid;
271}
272
Greg Claytonc7bece562013-01-25 18:06:21 +0000273uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000274ObjectFileELF::GetAddressByteSize() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275{
276 return m_data.GetAddressByteSize();
277}
278
Greg Claytonc7bece562013-01-25 18:06:21 +0000279size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000280ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281{
Greg Claytonc7bece562013-01-25 18:06:21 +0000282 return std::distance(m_section_headers.begin(), I) + 1u;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000283}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284
Greg Claytonc7bece562013-01-25 18:06:21 +0000285size_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000286ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
287{
Greg Claytonc7bece562013-01-25 18:06:21 +0000288 return std::distance(m_section_headers.begin(), I) + 1u;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289}
290
291bool
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000292ObjectFileELF::ParseHeader()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293{
Greg Claytonc7bece562013-01-25 18:06:21 +0000294 lldb::offset_t offset = GetOffset();
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000295 return m_header.Parse(m_data, &offset);
296}
297
298bool
Greg Clayton60830262011-02-04 18:53:10 +0000299ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000300{
301 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 return false;
303}
304
305uint32_t
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000306ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000308 size_t num_modules = ParseDependentModules();
309 uint32_t num_specs = 0;
310
311 for (unsigned i = 0; i < num_modules; ++i)
312 {
313 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
314 num_specs++;
315 }
316
317 return num_specs;
318}
319
Stephen Wilson499b40e2011-03-30 16:07:05 +0000320user_id_t
321ObjectFileELF::GetSectionIndexByType(unsigned type)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000322{
323 if (!ParseSectionHeaders())
Stephen Wilson499b40e2011-03-30 16:07:05 +0000324 return 0;
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000325
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000326 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
327 sh_pos != m_section_headers.end(); ++sh_pos)
328 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000329 if (sh_pos->sh_type == type)
330 return SectionIndex(sh_pos);
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000331 }
332
Stephen Wilson499b40e2011-03-30 16:07:05 +0000333 return 0;
334}
335
336Address
337ObjectFileELF::GetImageInfoAddress()
338{
339 if (!ParseDynamicSymbols())
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000340 return Address();
341
342 SectionList *section_list = GetSectionList();
343 if (!section_list)
344 return Address();
345
Stephen Wilson499b40e2011-03-30 16:07:05 +0000346 user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC);
347 if (!dynsym_id)
348 return Address();
349
350 const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
351 if (!dynsym_hdr)
352 return Address();
353
Greg Claytone72dfb32012-02-24 01:59:29 +0000354 SectionSP dynsym_section_sp (section_list->FindSectionByID(dynsym_id));
355 if (dynsym_section_sp)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000356 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000357 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000358 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000359 ELFDynamic &symbol = m_dynamic_symbols[i];
360
361 if (symbol.d_tag == DT_DEBUG)
362 {
363 // Compute the offset as the number of previous entries plus the
364 // size of d_tag.
365 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
366 return Address(dynsym_section_sp, offset);
367 }
Stephen Wilson2ab0a582011-01-15 00:08:44 +0000368 }
369 }
370
371 return Address();
372}
373
Jim Ingham672e6f52011-03-07 23:44:08 +0000374lldb_private::Address
375ObjectFileELF::GetEntryPointAddress ()
376{
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000377 SectionList *sections;
378 addr_t offset;
Jim Ingham672e6f52011-03-07 23:44:08 +0000379
Stephen Wilsond126c8c2011-03-08 04:12:15 +0000380 if (m_entry_point_address.IsValid())
381 return m_entry_point_address;
382
383 if (!ParseHeader() || !IsExecutable())
384 return m_entry_point_address;
385
386 sections = GetSectionList();
387 offset = m_header.e_entry;
388
389 if (!sections)
390 {
391 m_entry_point_address.SetOffset(offset);
392 return m_entry_point_address;
393 }
394
395 m_entry_point_address.ResolveAddressUsingFileSections(offset, sections);
396
397 return m_entry_point_address;
Jim Ingham672e6f52011-03-07 23:44:08 +0000398}
399
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000400//----------------------------------------------------------------------
401// ParseDependentModules
402//----------------------------------------------------------------------
403size_t
404ObjectFileELF::ParseDependentModules()
405{
406 if (m_filespec_ap.get())
407 return m_filespec_ap->GetSize();
408
409 m_filespec_ap.reset(new FileSpecList());
410
411 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
412 return 0;
413
414 // Locate the dynamic table.
415 user_id_t dynsym_id = 0;
416 user_id_t dynstr_id = 0;
Greg Clayton450e3f32010-10-12 02:24:53 +0000417 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
418 sh_pos != m_section_headers.end(); ++sh_pos)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000419 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000420 if (sh_pos->sh_type == SHT_DYNAMIC)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000421 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000422 dynsym_id = SectionIndex(sh_pos);
423 dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000424 break;
425 }
426 }
427
428 if (!(dynsym_id && dynstr_id))
429 return 0;
430
431 SectionList *section_list = GetSectionList();
432 if (!section_list)
433 return 0;
434
435 // Resolve and load the dynamic table entries and corresponding string
436 // table.
437 Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
438 Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
439 if (!(dynsym && dynstr))
440 return 0;
441
442 DataExtractor dynsym_data;
443 DataExtractor dynstr_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000444 if (ReadSectionData(dynsym, dynsym_data) &&
445 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000446 {
447 ELFDynamic symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000448 const lldb::offset_t section_size = dynsym_data.GetByteSize();
449 lldb::offset_t offset = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000450
451 // The only type of entries we are concerned with are tagged DT_NEEDED,
452 // yielding the name of a required library.
453 while (offset < section_size)
454 {
455 if (!symbol.Parse(dynsym_data, &offset))
456 break;
457
458 if (symbol.d_tag != DT_NEEDED)
459 continue;
460
461 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
462 const char *lib_name = dynstr_data.PeekCStr(str_index);
Greg Clayton274060b2010-10-20 20:54:39 +0000463 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000464 }
465 }
466
467 return m_filespec_ap->GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
470//----------------------------------------------------------------------
471// ParseProgramHeaders
472//----------------------------------------------------------------------
473size_t
474ObjectFileELF::ParseProgramHeaders()
475{
476 // We have already parsed the program headers
477 if (!m_program_headers.empty())
478 return m_program_headers.size();
479
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000480 // If there are no program headers to read we are done.
481 if (m_header.e_phnum == 0)
482 return 0;
483
484 m_program_headers.resize(m_header.e_phnum);
485 if (m_program_headers.size() != m_header.e_phnum)
486 return 0;
487
488 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
Greg Clayton44435ed2012-01-12 05:25:17 +0000489 const elf_off ph_offset = m_header.e_phoff;
490 DataExtractor data;
491 if (GetData (ph_offset, ph_size, data) != ph_size)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000492 return 0;
493
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000494 uint32_t idx;
Greg Claytonc7bece562013-01-25 18:06:21 +0000495 lldb::offset_t offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000496 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000498 if (m_program_headers[idx].Parse(data, &offset) == false)
499 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 }
501
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000502 if (idx < m_program_headers.size())
503 m_program_headers.resize(idx);
504
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505 return m_program_headers.size();
506}
507
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508//----------------------------------------------------------------------
509// ParseSectionHeaders
510//----------------------------------------------------------------------
511size_t
512ObjectFileELF::ParseSectionHeaders()
513{
514 // We have already parsed the section headers
515 if (!m_section_headers.empty())
516 return m_section_headers.size();
517
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000518 // If there are no section headers we are done.
519 if (m_header.e_shnum == 0)
520 return 0;
521
522 m_section_headers.resize(m_header.e_shnum);
523 if (m_section_headers.size() != m_header.e_shnum)
524 return 0;
525
526 const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
Greg Clayton44435ed2012-01-12 05:25:17 +0000527 const elf_off sh_offset = m_header.e_shoff;
528 DataExtractor data;
529 if (GetData (sh_offset, sh_size, data) != sh_size)
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000530 return 0;
531
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000532 uint32_t idx;
Greg Claytonc7bece562013-01-25 18:06:21 +0000533 lldb::offset_t offset;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000534 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000536 if (m_section_headers[idx].Parse(data, &offset) == false)
537 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000539 if (idx < m_section_headers.size())
540 m_section_headers.resize(idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541
542 return m_section_headers.size();
543}
544
545size_t
546ObjectFileELF::GetSectionHeaderStringTable()
547{
548 if (m_shstr_data.GetByteSize() == 0)
549 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000550 const unsigned strtab_idx = m_header.e_shstrndx;
551
552 if (strtab_idx && strtab_idx < m_section_headers.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000554 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
555 const size_t byte_size = sheader.sh_size;
Greg Clayton44435ed2012-01-12 05:25:17 +0000556 const Elf64_Off offset = sheader.sh_offset;
557 m_shstr_data.SetData (m_data, offset, byte_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558
Greg Clayton44435ed2012-01-12 05:25:17 +0000559 if (m_shstr_data.GetByteSize() != byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 }
562 }
563 return m_shstr_data.GetByteSize();
564}
565
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000566lldb::user_id_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567ObjectFileELF::GetSectionIndexByName(const char *name)
568{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000569 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
570 return 0;
571
572 // Search the collection of section headers for one with a matching name.
573 for (SectionHeaderCollIter I = m_section_headers.begin();
574 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000576 const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
577
578 if (!sectionName)
579 return 0;
580
581 if (strcmp(name, sectionName) != 0)
582 continue;
583
584 return SectionIndex(I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 }
586
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000587 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588}
589
Stephen Wilson499b40e2011-03-30 16:07:05 +0000590const elf::ELFSectionHeader *
591ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
592{
593 if (!ParseSectionHeaders() || !id)
594 return NULL;
595
596 if (--id < m_section_headers.size())
597 return &m_section_headers[id];
598
599 return NULL;
600}
601
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602SectionList *
603ObjectFileELF::GetSectionList()
604{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000605 if (m_sections_ap.get())
606 return m_sections_ap.get();
607
608 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 {
610 m_sections_ap.reset(new SectionList());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000612 for (SectionHeaderCollIter I = m_section_headers.begin();
613 I != m_section_headers.end(); ++I)
614 {
615 const ELFSectionHeader &header = *I;
616
617 ConstString name(m_shstr_data.PeekCStr(header.sh_name));
Greg Clayton47037bc2012-03-27 02:40:46 +0000618 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
619 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000620
Greg Clayton4ceb9982010-07-21 22:54:26 +0000621 static ConstString g_sect_name_text (".text");
622 static ConstString g_sect_name_data (".data");
623 static ConstString g_sect_name_bss (".bss");
Greg Clayton741f3f92012-03-27 21:10:07 +0000624 static ConstString g_sect_name_tdata (".tdata");
625 static ConstString g_sect_name_tbss (".tbss");
Greg Clayton4ceb9982010-07-21 22:54:26 +0000626 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
627 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
628 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
629 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
630 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
631 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
632 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
633 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
634 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
635 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
636 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
637 static ConstString g_sect_name_eh_frame (".eh_frame");
638
639 SectionType sect_type = eSectionTypeOther;
640
Greg Clayton741f3f92012-03-27 21:10:07 +0000641 bool is_thread_specific = false;
642
Greg Clayton4ceb9982010-07-21 22:54:26 +0000643 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
644 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
645 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
Greg Clayton741f3f92012-03-27 21:10:07 +0000646 else if (name == g_sect_name_tdata)
647 {
648 sect_type = eSectionTypeData;
649 is_thread_specific = true;
650 }
651 else if (name == g_sect_name_tbss)
652 {
653 sect_type = eSectionTypeZeroFill;
654 is_thread_specific = true;
655 }
Greg Clayton4ceb9982010-07-21 22:54:26 +0000656 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
657 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
658 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
659 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
660 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
661 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
662 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
663 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
664 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
665 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
666 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
667 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
668
669
Greg Clayton741f3f92012-03-27 21:10:07 +0000670 SectionSP section_sp(new Section(
Greg Clayton4ceb9982010-07-21 22:54:26 +0000671 GetModule(), // Module to which this section belongs.
672 SectionIndex(I), // Section ID.
673 name, // Section name.
674 sect_type, // Section type.
675 header.sh_addr, // VM address.
Greg Clayton47037bc2012-03-27 02:40:46 +0000676 vm_size, // VM size in bytes of this section.
Greg Clayton4ceb9982010-07-21 22:54:26 +0000677 header.sh_offset, // Offset of this section in the file.
Greg Clayton47037bc2012-03-27 02:40:46 +0000678 file_size, // Size of the section as found in the file.
Greg Clayton4ceb9982010-07-21 22:54:26 +0000679 header.sh_flags)); // Flags for this section.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000680
Greg Clayton741f3f92012-03-27 21:10:07 +0000681 if (is_thread_specific)
682 section_sp->SetIsThreadSpecific (is_thread_specific);
683 m_sections_ap->AddSection(section_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 }
Sean Callanan56775362012-06-08 02:16:08 +0000685
686 m_sections_ap->Finalize(); // Now that we're done adding sections, finalize to build fast-lookup caches
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000688
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 return m_sections_ap.get();
690}
691
Stephen Wilson499b40e2011-03-30 16:07:05 +0000692static unsigned
693ParseSymbols(Symtab *symtab,
694 user_id_t start_id,
695 SectionList *section_list,
696 const ELFSectionHeader *symtab_shdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000697 const DataExtractor &symtab_data,
698 const DataExtractor &strtab_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699{
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000700 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000701 lldb::offset_t offset = 0;
702 const size_t num_symbols = symtab_data.GetByteSize() / symtab_shdr->sh_entsize;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000703
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 static ConstString text_section_name(".text");
705 static ConstString init_section_name(".init");
706 static ConstString fini_section_name(".fini");
707 static ConstString ctors_section_name(".ctors");
708 static ConstString dtors_section_name(".dtors");
709
710 static ConstString data_section_name(".data");
711 static ConstString rodata_section_name(".rodata");
712 static ConstString rodata1_section_name(".rodata1");
713 static ConstString data2_section_name(".data1");
714 static ConstString bss_section_name(".bss");
715
Stephen Wilson499b40e2011-03-30 16:07:05 +0000716 unsigned i;
717 for (i = 0; i < num_symbols; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000719 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 break;
721
Greg Claytone72dfb32012-02-24 01:59:29 +0000722 SectionSP symbol_section_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000724 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000726 switch (symbol_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 {
728 case SHN_ABS:
729 symbol_type = eSymbolTypeAbsolute;
730 break;
731 case SHN_UNDEF:
732 symbol_type = eSymbolTypeUndefined;
733 break;
734 default:
Greg Claytone72dfb32012-02-24 01:59:29 +0000735 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 break;
737 }
738
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000739 switch (symbol.getType())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740 {
741 default:
742 case STT_NOTYPE:
743 // The symbol's type is not specified.
744 break;
745
746 case STT_OBJECT:
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000747 // The symbol is associated with a data object, such as a variable,
748 // an array, etc.
749 symbol_type = eSymbolTypeData;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 break;
751
752 case STT_FUNC:
753 // The symbol is associated with a function or other executable code.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000754 symbol_type = eSymbolTypeCode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755 break;
756
757 case STT_SECTION:
758 // The symbol is associated with a section. Symbol table entries of
759 // this type exist primarily for relocation and normally have
760 // STB_LOCAL binding.
761 break;
762
763 case STT_FILE:
764 // Conventionally, the symbol's name gives the name of the source
765 // file associated with the object file. A file symbol has STB_LOCAL
766 // binding, its section index is SHN_ABS, and it precedes the other
767 // STB_LOCAL symbols for the file, if it is present.
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000768 symbol_type = eSymbolTypeObjectFile;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 break;
770 }
771
772 if (symbol_type == eSymbolTypeInvalid)
773 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000774 if (symbol_section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000776 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 if (sect_name == text_section_name ||
778 sect_name == init_section_name ||
779 sect_name == fini_section_name ||
780 sect_name == ctors_section_name ||
781 sect_name == dtors_section_name)
782 {
783 symbol_type = eSymbolTypeCode;
784 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000785 else if (sect_name == data_section_name ||
786 sect_name == data2_section_name ||
787 sect_name == rodata_section_name ||
788 sect_name == rodata1_section_name ||
789 sect_name == bss_section_name)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 {
791 symbol_type = eSymbolTypeData;
792 }
793 }
794 }
795
796 uint64_t symbol_value = symbol.st_value;
Greg Claytone72dfb32012-02-24 01:59:29 +0000797 if (symbol_section_sp)
798 symbol_value -= symbol_section_sp->GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000800 bool is_global = symbol.getBinding() == STB_GLOBAL;
801 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Greg Clayton47037bc2012-03-27 02:40:46 +0000802 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000803 Symbol dc_symbol(
Greg Claytone72dfb32012-02-24 01:59:29 +0000804 i + start_id, // ID is the original symbol table index.
805 symbol_name, // Symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +0000806 is_mangled, // Is the symbol name mangled?
Greg Claytone72dfb32012-02-24 01:59:29 +0000807 symbol_type, // Type of this symbol
808 is_global, // Is this globally visible?
809 false, // Is this symbol debug info?
810 false, // Is this symbol a trampoline?
811 false, // Is this symbol artificial?
812 symbol_section_sp, // Section in which this symbol is defined or null.
813 symbol_value, // Offset in section or symbol value.
814 symbol.st_size, // Size in bytes of this symbol.
815 flags); // Symbol flags.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 symtab->AddSymbol(dc_symbol);
817 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000818
819 return i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820}
821
Stephen Wilson499b40e2011-03-30 16:07:05 +0000822unsigned
823ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
824 const ELFSectionHeader *symtab_hdr,
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000825 user_id_t symtab_id)
826{
Stephen Wilson499b40e2011-03-30 16:07:05 +0000827 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
828 symtab_hdr->sh_type == SHT_DYNSYM);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000829
830 // Parse in the section list if needed.
831 SectionList *section_list = GetSectionList();
832 if (!section_list)
Stephen Wilson499b40e2011-03-30 16:07:05 +0000833 return 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000834
835 // Section ID's are ones based.
Stephen Wilson499b40e2011-03-30 16:07:05 +0000836 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000837
838 Section *symtab = section_list->FindSectionByID(symtab_id).get();
839 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Stephen Wilson499b40e2011-03-30 16:07:05 +0000840 unsigned num_symbols = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000841 if (symtab && strtab)
842 {
843 DataExtractor symtab_data;
844 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000845 if (ReadSectionData(symtab, symtab_data) &&
846 ReadSectionData(strtab, strtab_data))
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000847 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000848 num_symbols = ParseSymbols(symbol_table, start_id,
849 section_list, symtab_hdr,
850 symtab_data, strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +0000851 }
852 }
Stephen Wilson499b40e2011-03-30 16:07:05 +0000853
854 return num_symbols;
855}
856
857size_t
858ObjectFileELF::ParseDynamicSymbols()
859{
860 if (m_dynamic_symbols.size())
861 return m_dynamic_symbols.size();
862
863 user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC);
864 if (!dyn_id)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000865 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000866
867 SectionList *section_list = GetSectionList();
868 if (!section_list)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000869 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000870
871 Section *dynsym = section_list->FindSectionByID(dyn_id).get();
872 if (!dynsym)
Bill Wendlinged24dcc2012-04-03 07:50:11 +0000873 return 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000874
875 ELFDynamic symbol;
876 DataExtractor dynsym_data;
Greg Claytonc9660542012-02-05 02:38:54 +0000877 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +0000878 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000879 const lldb::offset_t section_size = dynsym_data.GetByteSize();
880 lldb::offset_t cursor = 0;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000881
882 while (cursor < section_size)
883 {
Stephen Wilson499b40e2011-03-30 16:07:05 +0000884 if (!symbol.Parse(dynsym_data, &cursor))
885 break;
886
887 m_dynamic_symbols.push_back(symbol);
888 }
889 }
890
891 return m_dynamic_symbols.size();
892}
893
894const ELFDynamic *
895ObjectFileELF::FindDynamicSymbol(unsigned tag)
896{
897 if (!ParseDynamicSymbols())
898 return NULL;
899
900 SectionList *section_list = GetSectionList();
901 if (!section_list)
902 return 0;
903
904 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
905 DynamicSymbolCollIter E = m_dynamic_symbols.end();
906 for ( ; I != E; ++I)
907 {
908 ELFDynamic *symbol = &*I;
909
910 if (symbol->d_tag == tag)
911 return symbol;
912 }
913
914 return NULL;
915}
916
917Section *
918ObjectFileELF::PLTSection()
919{
920 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
921 SectionList *section_list = GetSectionList();
922
923 if (symbol && section_list)
924 {
925 addr_t addr = symbol->d_ptr;
926 return section_list->FindSectionContainingFileAddress(addr).get();
927 }
928
929 return NULL;
930}
931
932unsigned
933ObjectFileELF::PLTRelocationType()
934{
935 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
936
937 if (symbol)
938 return symbol->d_val;
939
940 return 0;
941}
942
943static unsigned
944ParsePLTRelocations(Symtab *symbol_table,
945 user_id_t start_id,
946 unsigned rel_type,
947 const ELFHeader *hdr,
948 const ELFSectionHeader *rel_hdr,
949 const ELFSectionHeader *plt_hdr,
950 const ELFSectionHeader *sym_hdr,
Greg Claytone72dfb32012-02-24 01:59:29 +0000951 const lldb::SectionSP &plt_section_sp,
Stephen Wilson499b40e2011-03-30 16:07:05 +0000952 DataExtractor &rel_data,
953 DataExtractor &symtab_data,
954 DataExtractor &strtab_data)
955{
956 ELFRelocation rel(rel_type);
957 ELFSymbol symbol;
Greg Claytonc7bece562013-01-25 18:06:21 +0000958 lldb::offset_t offset = 0;
959 const elf_xword plt_entsize = plt_hdr->sh_entsize;
960 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000961
962 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
963 reloc_info_fn reloc_type;
964 reloc_info_fn reloc_symbol;
965
Greg Claytond091afe2012-11-12 22:53:16 +0000966 if (hdr->Is32Bit())
Stephen Wilson499b40e2011-03-30 16:07:05 +0000967 {
968 reloc_type = ELFRelocation::RelocType32;
969 reloc_symbol = ELFRelocation::RelocSymbol32;
970 }
971 else
972 {
973 reloc_type = ELFRelocation::RelocType64;
974 reloc_symbol = ELFRelocation::RelocSymbol64;
975 }
976
977 unsigned slot_type = hdr->GetRelocationJumpSlotType();
978 unsigned i;
979 for (i = 0; i < num_relocations; ++i)
980 {
981 if (rel.Parse(rel_data, &offset) == false)
982 break;
983
984 if (reloc_type(rel) != slot_type)
985 continue;
986
Greg Claytonc7bece562013-01-25 18:06:21 +0000987 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000988 uint64_t plt_index = (i + 1) * plt_entsize;
989
990 if (!symbol.Parse(symtab_data, &symbol_offset))
991 break;
992
993 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton47037bc2012-03-27 02:40:46 +0000994 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilson499b40e2011-03-30 16:07:05 +0000995
996 Symbol jump_symbol(
997 i + start_id, // Symbol table index
998 symbol_name, // symbol name.
Greg Clayton47037bc2012-03-27 02:40:46 +0000999 is_mangled, // is the symbol name mangled?
Stephen Wilson499b40e2011-03-30 16:07:05 +00001000 eSymbolTypeTrampoline, // Type of this symbol
1001 false, // Is this globally visible?
1002 false, // Is this symbol debug info?
1003 true, // Is this symbol a trampoline?
1004 true, // Is this symbol artificial?
Greg Claytone72dfb32012-02-24 01:59:29 +00001005 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001006 plt_index, // Offset in section or symbol value.
1007 plt_entsize, // Size in bytes of this symbol.
1008 0); // Symbol flags.
1009
1010 symbol_table->AddSymbol(jump_symbol);
1011 }
1012
1013 return i;
1014}
1015
1016unsigned
1017ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1018 user_id_t start_id,
1019 const ELFSectionHeader *rel_hdr,
1020 user_id_t rel_id)
1021{
1022 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1023
1024 // The link field points to the asscoiated symbol table. The info field
1025 // points to the section holding the plt.
1026 user_id_t symtab_id = rel_hdr->sh_link;
1027 user_id_t plt_id = rel_hdr->sh_info;
1028
1029 if (!symtab_id || !plt_id)
1030 return 0;
1031
1032 // Section ID's are ones based;
1033 symtab_id++;
1034 plt_id++;
1035
1036 const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id);
1037 if (!plt_hdr)
1038 return 0;
1039
1040 const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1041 if (!sym_hdr)
1042 return 0;
1043
1044 SectionList *section_list = GetSectionList();
1045 if (!section_list)
1046 return 0;
1047
1048 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1049 if (!rel_section)
1050 return 0;
1051
Greg Claytone72dfb32012-02-24 01:59:29 +00001052 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1053 if (!plt_section_sp)
Stephen Wilson499b40e2011-03-30 16:07:05 +00001054 return 0;
1055
1056 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1057 if (!symtab)
1058 return 0;
1059
1060 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1061 if (!strtab)
1062 return 0;
1063
1064 DataExtractor rel_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001065 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001066 return 0;
1067
1068 DataExtractor symtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001069 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001070 return 0;
1071
1072 DataExtractor strtab_data;
Greg Claytonc9660542012-02-05 02:38:54 +00001073 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilson499b40e2011-03-30 16:07:05 +00001074 return 0;
1075
1076 unsigned rel_type = PLTRelocationType();
1077 if (!rel_type)
1078 return 0;
1079
Greg Claytone72dfb32012-02-24 01:59:29 +00001080 return ParsePLTRelocations (symbol_table,
1081 start_id,
1082 rel_type,
1083 &m_header,
1084 rel_hdr,
1085 plt_hdr,
1086 sym_hdr,
1087 plt_section_sp,
1088 rel_data,
1089 symtab_data,
1090 strtab_data);
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001091}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092
1093Symtab *
1094ObjectFileELF::GetSymtab()
1095{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001096 if (m_symtab_ap.get())
1097 return m_symtab_ap.get();
1098
1099 Symtab *symbol_table = new Symtab(this);
1100 m_symtab_ap.reset(symbol_table);
1101
Stephen Wilson499b40e2011-03-30 16:07:05 +00001102 Mutex::Locker locker(symbol_table->GetMutex());
Greg Clayton8087ca22010-10-08 04:20:14 +00001103
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001104 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1105 return symbol_table;
1106
1107 // Locate and parse all linker symbol tables.
Stephen Wilson499b40e2011-03-30 16:07:05 +00001108 uint64_t symbol_id = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001109 for (SectionHeaderCollIter I = m_section_headers.begin();
1110 I != m_section_headers.end(); ++I)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 {
Peter Collingbourneb4aabeb2011-06-03 20:39:58 +00001112 if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113 {
Stephen Wilson499b40e2011-03-30 16:07:05 +00001114 const ELFSectionHeader &symtab_header = *I;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001115 user_id_t section_id = SectionIndex(I);
Stephen Wilson499b40e2011-03-30 16:07:05 +00001116 symbol_id += ParseSymbolTable(symbol_table, symbol_id,
1117 &symtab_header, section_id);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 }
1119 }
Stephen Wilson499b40e2011-03-30 16:07:05 +00001120
1121 // Synthesize trampoline symbols to help navigate the PLT.
1122 Section *reloc_section = PLTSection();
1123 if (reloc_section)
1124 {
1125 user_id_t reloc_id = reloc_section->GetID();
1126 const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id);
1127 assert(reloc_header);
1128
1129 ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id);
1130 }
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001131
1132 return symbol_table;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133}
1134
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001135//===----------------------------------------------------------------------===//
1136// Dump
1137//
1138// Dump the specifics of the runtime file container (such as any headers
1139// segments, sections, etc).
1140//----------------------------------------------------------------------
1141void
1142ObjectFileELF::Dump(Stream *s)
1143{
1144 DumpELFHeader(s, m_header);
1145 s->EOL();
1146 DumpELFProgramHeaders(s);
1147 s->EOL();
1148 DumpELFSectionHeaders(s);
1149 s->EOL();
1150 SectionList *section_list = GetSectionList();
1151 if (section_list)
Greg Clayton10177aa2010-12-08 05:08:21 +00001152 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153 Symtab *symtab = GetSymtab();
1154 if (symtab)
Greg Claytone0d378b2011-03-24 21:19:54 +00001155 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 s->EOL();
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001157 DumpDependentModules(s);
1158 s->EOL();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159}
1160
1161//----------------------------------------------------------------------
1162// DumpELFHeader
1163//
1164// Dump the ELF header to the specified output stream
1165//----------------------------------------------------------------------
1166void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001167ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001169 s->PutCString("ELF Header\n");
1170 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1171 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
1172 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1173 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
1174 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1175 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
1176 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001178 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1179 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1181 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1182 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1183
1184 s->Printf("e_type = 0x%4.4x ", header.e_type);
1185 DumpELFHeader_e_type(s, header.e_type);
1186 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
1187 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Daniel Malead01b2952012-11-29 21:49:15 +00001188 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
1189 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
1190 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
1192 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
1193 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1194 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
1195 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1196 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
1197 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
1198}
1199
1200//----------------------------------------------------------------------
1201// DumpELFHeader_e_type
1202//
1203// Dump an token value for the ELF header member e_type
1204//----------------------------------------------------------------------
1205void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001206ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207{
1208 switch (e_type)
1209 {
1210 case ET_NONE: *s << "ET_NONE"; break;
1211 case ET_REL: *s << "ET_REL"; break;
1212 case ET_EXEC: *s << "ET_EXEC"; break;
1213 case ET_DYN: *s << "ET_DYN"; break;
1214 case ET_CORE: *s << "ET_CORE"; break;
1215 default:
1216 break;
1217 }
1218}
1219
1220//----------------------------------------------------------------------
1221// DumpELFHeader_e_ident_EI_DATA
1222//
1223// Dump an token value for the ELF header member e_ident[EI_DATA]
1224//----------------------------------------------------------------------
1225void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001226ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227{
1228 switch (ei_data)
1229 {
1230 case ELFDATANONE: *s << "ELFDATANONE"; break;
1231 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
1232 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
1233 default:
1234 break;
1235 }
1236}
1237
1238
1239//----------------------------------------------------------------------
1240// DumpELFProgramHeader
1241//
1242// Dump a single ELF program header to the specified output stream
1243//----------------------------------------------------------------------
1244void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001245ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246{
1247 DumpELFProgramHeader_p_type(s, ph.p_type);
Daniel Malead01b2952012-11-29 21:49:15 +00001248 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
1249 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 +00001250
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00001252 s->Printf(") %8.8" PRIx64, ph.p_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253}
1254
1255//----------------------------------------------------------------------
1256// DumpELFProgramHeader_p_type
1257//
1258// Dump an token value for the ELF program header member p_type which
1259// describes the type of the program header
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001260// ----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001262ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263{
1264 const int kStrWidth = 10;
1265 switch (p_type)
1266 {
1267 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
1268 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
1269 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
1270 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
1271 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
1272 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
1273 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
1274 default:
1275 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1276 break;
1277 }
1278}
1279
1280
1281//----------------------------------------------------------------------
1282// DumpELFProgramHeader_p_flags
1283//
1284// Dump an token value for the ELF program header member p_flags
1285//----------------------------------------------------------------------
1286void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001287ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288{
1289 *s << ((p_flags & PF_X) ? "PF_X" : " ")
1290 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1291 << ((p_flags & PF_W) ? "PF_W" : " ")
1292 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1293 << ((p_flags & PF_R) ? "PF_R" : " ");
1294}
1295
1296//----------------------------------------------------------------------
1297// DumpELFProgramHeaders
1298//
1299// Dump all of the ELF program header to the specified output stream
1300//----------------------------------------------------------------------
1301void
1302ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1303{
1304 if (ParseProgramHeaders())
1305 {
1306 s->PutCString("Program Headers\n");
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001307 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
1308 "p_filesz p_memsz p_flags p_align\n");
1309 s->PutCString("==== ---------- -------- -------- -------- "
1310 "-------- -------- ------------------------- --------\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311
1312 uint32_t idx = 0;
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001313 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1314 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001316 s->Printf("[%2u] ", idx);
1317 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318 s->EOL();
1319 }
1320 }
1321}
1322
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001323//----------------------------------------------------------------------
1324// DumpELFSectionHeader
1325//
1326// Dump a single ELF section header to the specified output stream
1327//----------------------------------------------------------------------
1328void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001329ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001330{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001331 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Daniel Malead01b2952012-11-29 21:49:15 +00001333 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001334 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Daniel Malead01b2952012-11-29 21:49:15 +00001335 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 +00001336 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Daniel Malead01b2952012-11-29 21:49:15 +00001337 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338}
1339
1340//----------------------------------------------------------------------
1341// DumpELFSectionHeader_sh_type
1342//
1343// Dump an token value for the ELF section header member sh_type which
1344// describes the type of the section
1345//----------------------------------------------------------------------
1346void
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001347ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348{
1349 const int kStrWidth = 12;
1350 switch (sh_type)
1351 {
1352 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
1353 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1354 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
1355 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
1356 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
1357 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
1358 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
1359 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
1360 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
1361 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
1362 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
1363 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
1364 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
1365 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
1366 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
1367 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
1368 default:
1369 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1370 break;
1371 }
1372}
1373
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374//----------------------------------------------------------------------
1375// DumpELFSectionHeader_sh_flags
1376//
1377// Dump an token value for the ELF section header member sh_flags
1378//----------------------------------------------------------------------
1379void
Greg Claytonc7bece562013-01-25 18:06:21 +00001380ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001381{
1382 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
1383 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1384 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
1385 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1386 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
1387}
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001388
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389//----------------------------------------------------------------------
1390// DumpELFSectionHeaders
1391//
1392// Dump all of the ELF section header to the specified output stream
1393//----------------------------------------------------------------------
1394void
1395ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1396{
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001397 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1398 return;
1399
1400 s->PutCString("Section Headers\n");
1401 s->PutCString("IDX name type flags "
1402 "addr offset size link info addralgn "
1403 "entsize Name\n");
1404 s->PutCString("==== -------- ------------ -------------------------------- "
1405 "-------- -------- -------- -------- -------- -------- "
1406 "-------- ====================\n");
1407
1408 uint32_t idx = 0;
1409 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1410 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001412 s->Printf("[%2u] ", idx);
1413 ObjectFileELF::DumpELFSectionHeader(s, *I);
1414 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1415 if (section_name)
1416 *s << ' ' << section_name << "\n";
1417 }
1418}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001420void
1421ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1422{
1423 size_t num_modules = ParseDependentModules();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001424
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001425 if (num_modules > 0)
1426 {
1427 s->PutCString("Dependent Modules:\n");
1428 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429 {
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001430 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1431 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432 }
1433 }
1434}
1435
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436bool
Greg Clayton514487e2011-02-15 21:59:32 +00001437ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438{
Stephen Wilson3f4200fd2011-02-24 19:16:15 +00001439 if (!ParseHeader())
1440 return false;
1441
Greg Claytone0d378b2011-03-24 21:19:54 +00001442 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton64195a22011-02-23 00:35:02 +00001443 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1444 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonf325ba92010-07-13 23:07:23 +00001445 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446}
1447
Greg Clayton9e00b6a652011-07-09 00:41:34 +00001448ObjectFile::Type
1449ObjectFileELF::CalculateType()
1450{
1451 switch (m_header.e_type)
1452 {
1453 case llvm::ELF::ET_NONE:
1454 // 0 - No file type
1455 return eTypeUnknown;
1456
1457 case llvm::ELF::ET_REL:
1458 // 1 - Relocatable file
1459 return eTypeObjectFile;
1460
1461 case llvm::ELF::ET_EXEC:
1462 // 2 - Executable file
1463 return eTypeExecutable;
1464
1465 case llvm::ELF::ET_DYN:
1466 // 3 - Shared object file
1467 return eTypeSharedLibrary;
1468
1469 case ET_CORE:
1470 // 4 - Core file
1471 return eTypeCoreFile;
1472
1473 default:
1474 break;
1475 }
1476 return eTypeUnknown;
1477}
1478
1479ObjectFile::Strata
1480ObjectFileELF::CalculateStrata()
1481{
1482 switch (m_header.e_type)
1483 {
1484 case llvm::ELF::ET_NONE:
1485 // 0 - No file type
1486 return eStrataUnknown;
1487
1488 case llvm::ELF::ET_REL:
1489 // 1 - Relocatable file
1490 return eStrataUnknown;
1491
1492 case llvm::ELF::ET_EXEC:
1493 // 2 - Executable file
1494 // TODO: is there any way to detect that an executable is a kernel
1495 // related executable by inspecting the program headers, section
1496 // headers, symbols, or any other flag bits???
1497 return eStrataUser;
1498
1499 case llvm::ELF::ET_DYN:
1500 // 3 - Shared object file
1501 // TODO: is there any way to detect that an shared library is a kernel
1502 // related executable by inspecting the program headers, section
1503 // headers, symbols, or any other flag bits???
1504 return eStrataUnknown;
1505
1506 case ET_CORE:
1507 // 4 - Core file
1508 // TODO: is there any way to detect that an core file is a kernel
1509 // related executable by inspecting the program headers, section
1510 // headers, symbols, or any other flag bits???
1511 return eStrataUnknown;
1512
1513 default:
1514 break;
1515 }
1516 return eStrataUnknown;
1517}
1518