blob: a024d1b31486e6daec9b58427660a45860ba8a61 [file] [log] [blame]
Stephen Wilsonddd29622010-07-13 23:07:23 +00001//===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
Chris Lattner24943d22010-06-08 16:52:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ObjectFileELF.h"
11
Stephen Wilsonddd29622010-07-13 23:07:23 +000012#include <cassert>
Chris Lattner24943d22010-06-08 16:52:24 +000013#include <algorithm>
14
Stephen Wilsonf2ad3252011-01-15 00:08:44 +000015#include "lldb/Core/ArchSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Core/DataBuffer.h"
17#include "lldb/Core/Error.h"
Stephen Wilsonddd29622010-07-13 23:07:23 +000018#include "lldb/Core/FileSpecList.h"
Jim Ingham28775942011-03-07 23:44:08 +000019#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/Section.h"
22#include "lldb/Core/Stream.h"
Jim Ingham28775942011-03-07 23:44:08 +000023#include "lldb/Symbol/SymbolContext.h"
Greg Clayton940b1032011-02-23 00:35:02 +000024#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025
Stephen Wilsonce477322011-03-30 16:07:05 +000026#include "llvm/ADT/PointerUnion.h"
27
Stephen Wilsonddd29622010-07-13 23:07:23 +000028#define CASE_AND_STREAM(s, def, width) \
29 case def: s->Printf("%-*s", width, #def); break;
Chris Lattner24943d22010-06-08 16:52:24 +000030
Chris Lattner24943d22010-06-08 16:52:24 +000031using namespace lldb;
32using namespace lldb_private;
Stephen Wilsonddd29622010-07-13 23:07:23 +000033using namespace elf;
34using namespace llvm::ELF;
Chris Lattner24943d22010-06-08 16:52:24 +000035
Stephen Wilsonce477322011-03-30 16:07:05 +000036namespace {
37//===----------------------------------------------------------------------===//
38/// @class ELFRelocation
39/// @brief Generic wrapper for ELFRel and ELFRela.
40///
41/// This helper class allows us to parse both ELFRel and ELFRela relocation
42/// entries in a generic manner.
43class ELFRelocation
44{
45public:
46
47 /// Constructs an ELFRelocation entry with a personality as given by @p
48 /// type.
49 ///
50 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
51 ELFRelocation(unsigned type);
52
53 ~ELFRelocation();
54
55 bool
56 Parse(const lldb_private::DataExtractor &data, uint32_t *offset);
57
58 static unsigned
59 RelocType32(const ELFRelocation &rel);
60
61 static unsigned
62 RelocType64(const ELFRelocation &rel);
63
64 static unsigned
65 RelocSymbol32(const ELFRelocation &rel);
66
67 static unsigned
68 RelocSymbol64(const ELFRelocation &rel);
69
70private:
71 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
72
73 RelocUnion reloc;
74};
75
76ELFRelocation::ELFRelocation(unsigned type)
77{
78 if (type == DT_REL)
79 reloc = new ELFRel();
80 else if (type == DT_RELA)
81 reloc = new ELFRela();
82 else {
83 assert(false && "unexpected relocation type");
84 reloc = static_cast<ELFRel*>(NULL);
85 }
86}
87
88ELFRelocation::~ELFRelocation()
89{
90 if (reloc.is<ELFRel*>())
91 delete reloc.get<ELFRel*>();
92 else
93 delete reloc.get<ELFRela*>();
94}
95
96bool
97ELFRelocation::Parse(const lldb_private::DataExtractor &data, uint32_t *offset)
98{
99 if (reloc.is<ELFRel*>())
100 return reloc.get<ELFRel*>()->Parse(data, offset);
101 else
102 return reloc.get<ELFRela*>()->Parse(data, offset);
103}
104
105unsigned
106ELFRelocation::RelocType32(const ELFRelocation &rel)
107{
108 if (rel.reloc.is<ELFRel*>())
109 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
110 else
111 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
112}
113
114unsigned
115ELFRelocation::RelocType64(const ELFRelocation &rel)
116{
117 if (rel.reloc.is<ELFRel*>())
118 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
119 else
120 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
121}
122
123unsigned
124ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
125{
126 if (rel.reloc.is<ELFRel*>())
127 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
128 else
129 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
130}
131
132unsigned
133ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
134{
135 if (rel.reloc.is<ELFRel*>())
136 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
137 else
138 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
139}
140
141} // end anonymous namespace
142
Stephen Wilsonddd29622010-07-13 23:07:23 +0000143//------------------------------------------------------------------
144// Static methods.
145//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000146void
147ObjectFileELF::Initialize()
148{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000149 PluginManager::RegisterPlugin(GetPluginNameStatic(),
150 GetPluginDescriptionStatic(),
Greg Claytonb5a8f142012-02-05 02:38:54 +0000151 CreateInstance,
152 CreateMemoryInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000153}
154
155void
156ObjectFileELF::Terminate()
157{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000158 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner24943d22010-06-08 16:52:24 +0000159}
160
Chris Lattner24943d22010-06-08 16:52:24 +0000161const char *
162ObjectFileELF::GetPluginNameStatic()
163{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000164 return "object-file.elf";
Chris Lattner24943d22010-06-08 16:52:24 +0000165}
166
167const char *
168ObjectFileELF::GetPluginDescriptionStatic()
169{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000170 return "ELF object file reader.";
Chris Lattner24943d22010-06-08 16:52:24 +0000171}
172
Chris Lattner24943d22010-06-08 16:52:24 +0000173ObjectFile *
Greg Clayton3508c382012-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 Lattner24943d22010-06-08 16:52:24 +0000179{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000180 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
Chris Lattner24943d22010-06-08 16:52:24 +0000181 {
Stephen Wilsonddd29622010-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 Clayton3508c382012-02-24 01:59:29 +0000188 std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, file, offset, length));
Stephen Wilsonf7cc64d2011-02-24 19:16:15 +0000189 ArchSpec spec;
190 if (objfile_ap->GetArchitecture(spec) &&
191 objfile_ap->SetModulesArchitecture(spec))
Stephen Wilsonddd29622010-07-13 23:07:23 +0000192 return objfile_ap.release();
193 }
194 }
Chris Lattner24943d22010-06-08 16:52:24 +0000195 }
196 return NULL;
197}
198
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000199
Greg Claytonb5a8f142012-02-05 02:38:54 +0000200ObjectFile*
Greg Clayton3508c382012-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 Claytonb5a8f142012-02-05 02:38:54 +0000205{
206 return NULL;
207}
208
209
Stephen Wilsonddd29622010-07-13 23:07:23 +0000210//------------------------------------------------------------------
211// PluginInterface protocol
212//------------------------------------------------------------------
213const char *
214ObjectFileELF::GetPluginName()
Chris Lattner24943d22010-06-08 16:52:24 +0000215{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000216 return "ObjectFileELF";
Chris Lattner24943d22010-06-08 16:52:24 +0000217}
218
Stephen Wilsonddd29622010-07-13 23:07:23 +0000219const char *
220ObjectFileELF::GetShortPluginName()
221{
222 return GetPluginNameStatic();
223}
Chris Lattner24943d22010-06-08 16:52:24 +0000224
Stephen Wilsonddd29622010-07-13 23:07:23 +0000225uint32_t
226ObjectFileELF::GetPluginVersion()
227{
228 return m_plugin_version;
229}
Stephen Wilsonddd29622010-07-13 23:07:23 +0000230//------------------------------------------------------------------
231// ObjectFile protocol
232//------------------------------------------------------------------
233
Greg Clayton3508c382012-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 Lattner24943d22010-06-08 16:52:24 +0000247{
248 if (file)
249 m_file = *file;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000250 ::memset(&m_header, 0, sizeof(m_header));
Chris Lattner24943d22010-06-08 16:52:24 +0000251}
252
Chris Lattner24943d22010-06-08 16:52:24 +0000253ObjectFileELF::~ObjectFileELF()
254{
255}
256
Jim Ingham7508e732010-08-09 23:31:02 +0000257bool
258ObjectFileELF::IsExecutable() const
259{
Stephen Wilson90987392011-01-15 00:09:50 +0000260 return m_header.e_entry != 0;
Jim Ingham7508e732010-08-09 23:31:02 +0000261}
262
Chris Lattner24943d22010-06-08 16:52:24 +0000263ByteOrder
Stephen Wilsonddd29622010-07-13 23:07:23 +0000264ObjectFileELF::GetByteOrder() const
Chris Lattner24943d22010-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
273size_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000274ObjectFileELF::GetAddressByteSize() const
Chris Lattner24943d22010-06-08 16:52:24 +0000275{
276 return m_data.GetAddressByteSize();
277}
278
Stephen Wilsonddd29622010-07-13 23:07:23 +0000279unsigned
280ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
Chris Lattner24943d22010-06-08 16:52:24 +0000281{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000282 return std::distance(m_section_headers.begin(), I) + 1;
283}
Chris Lattner24943d22010-06-08 16:52:24 +0000284
Stephen Wilsonddd29622010-07-13 23:07:23 +0000285unsigned
286ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
287{
288 return std::distance(m_section_headers.begin(), I) + 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000289}
290
291bool
Stephen Wilsonddd29622010-07-13 23:07:23 +0000292ObjectFileELF::ParseHeader()
Chris Lattner24943d22010-06-08 16:52:24 +0000293{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000294 uint32_t offset = GetOffset();
295 return m_header.Parse(m_data, &offset);
296}
297
298bool
Greg Clayton0467c782011-02-04 18:53:10 +0000299ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000300{
301 // FIXME: Return MD5 sum here. See comment in ObjectFile.h.
Chris Lattner24943d22010-06-08 16:52:24 +0000302 return false;
303}
304
305uint32_t
Stephen Wilsonddd29622010-07-13 23:07:23 +0000306ObjectFileELF::GetDependentModules(FileSpecList &files)
Chris Lattner24943d22010-06-08 16:52:24 +0000307{
Stephen Wilsonddd29622010-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 Wilsonce477322011-03-30 16:07:05 +0000320user_id_t
321ObjectFileELF::GetSectionIndexByType(unsigned type)
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000322{
323 if (!ParseSectionHeaders())
Stephen Wilsonce477322011-03-30 16:07:05 +0000324 return 0;
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000325
Stephen Wilsonf2ad3252011-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 Wilsonce477322011-03-30 16:07:05 +0000329 if (sh_pos->sh_type == type)
330 return SectionIndex(sh_pos);
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000331 }
332
Stephen Wilsonce477322011-03-30 16:07:05 +0000333 return 0;
334}
335
336Address
337ObjectFileELF::GetImageInfoAddress()
338{
339 if (!ParseDynamicSymbols())
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000340 return Address();
341
342 SectionList *section_list = GetSectionList();
343 if (!section_list)
344 return Address();
345
Stephen Wilsonce477322011-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 Clayton3508c382012-02-24 01:59:29 +0000354 SectionSP dynsym_section_sp (section_list->FindSectionByID(dynsym_id));
355 if (dynsym_section_sp)
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000356 {
Greg Clayton3508c382012-02-24 01:59:29 +0000357 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
Stephen Wilsonf2ad3252011-01-15 00:08:44 +0000358 {
Greg Clayton3508c382012-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 Wilsonf2ad3252011-01-15 00:08:44 +0000368 }
369 }
370
371 return Address();
372}
373
Jim Ingham28775942011-03-07 23:44:08 +0000374lldb_private::Address
375ObjectFileELF::GetEntryPointAddress ()
376{
Stephen Wilson791af3b2011-03-08 04:12:15 +0000377 SectionList *sections;
378 addr_t offset;
Jim Ingham28775942011-03-07 23:44:08 +0000379
Stephen Wilson791af3b2011-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 Ingham28775942011-03-07 23:44:08 +0000398}
399
Stephen Wilsonddd29622010-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 Claytonad60bf42010-10-12 02:24:53 +0000417 for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
418 sh_pos != m_section_headers.end(); ++sh_pos)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000419 {
Greg Claytonad60bf42010-10-12 02:24:53 +0000420 if (sh_pos->sh_type == SHT_DYNAMIC)
Stephen Wilsonddd29622010-07-13 23:07:23 +0000421 {
Greg Claytonad60bf42010-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 Wilsonddd29622010-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 Claytonb5a8f142012-02-05 02:38:54 +0000444 if (ReadSectionData(dynsym, dynsym_data) &&
445 ReadSectionData(dynstr, dynstr_data))
Stephen Wilsonddd29622010-07-13 23:07:23 +0000446 {
447 ELFDynamic symbol;
448 const unsigned section_size = dynsym_data.GetByteSize();
449 unsigned offset = 0;
450
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 Clayton537a7a82010-10-20 20:54:39 +0000463 m_filespec_ap->Append(FileSpec(lib_name, true));
Stephen Wilsonddd29622010-07-13 23:07:23 +0000464 }
465 }
466
467 return m_filespec_ap->GetSize();
Chris Lattner24943d22010-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 Wilsonddd29622010-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 Claytondb2dc2b2012-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 Wilsonddd29622010-07-13 23:07:23 +0000492 return 0;
493
Stephen Wilsonddd29622010-07-13 23:07:23 +0000494 uint32_t idx;
495 uint32_t offset;
496 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000497 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000498 if (m_program_headers[idx].Parse(data, &offset) == false)
499 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000500 }
501
Stephen Wilsonddd29622010-07-13 23:07:23 +0000502 if (idx < m_program_headers.size())
503 m_program_headers.resize(idx);
504
Chris Lattner24943d22010-06-08 16:52:24 +0000505 return m_program_headers.size();
506}
507
Chris Lattner24943d22010-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 Wilsonddd29622010-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 Claytondb2dc2b2012-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 Wilsonddd29622010-07-13 23:07:23 +0000530 return 0;
531
Stephen Wilsonddd29622010-07-13 23:07:23 +0000532 uint32_t idx;
533 uint32_t offset;
534 for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000535 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000536 if (m_section_headers[idx].Parse(data, &offset) == false)
537 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000538 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000539 if (idx < m_section_headers.size())
540 m_section_headers.resize(idx);
Chris Lattner24943d22010-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 Wilsonddd29622010-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 Lattner24943d22010-06-08 16:52:24 +0000553 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000554 const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
555 const size_t byte_size = sheader.sh_size;
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000556 const Elf64_Off offset = sheader.sh_offset;
557 m_shstr_data.SetData (m_data, offset, byte_size);
Chris Lattner24943d22010-06-08 16:52:24 +0000558
Greg Claytondb2dc2b2012-01-12 05:25:17 +0000559 if (m_shstr_data.GetByteSize() != byte_size)
Chris Lattner24943d22010-06-08 16:52:24 +0000560 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000561 }
562 }
563 return m_shstr_data.GetByteSize();
564}
565
Stephen Wilsonddd29622010-07-13 23:07:23 +0000566lldb::user_id_t
Chris Lattner24943d22010-06-08 16:52:24 +0000567ObjectFileELF::GetSectionIndexByName(const char *name)
568{
Stephen Wilsonddd29622010-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 Lattner24943d22010-06-08 16:52:24 +0000575 {
Stephen Wilsonddd29622010-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 Lattner24943d22010-06-08 16:52:24 +0000585 }
586
Stephen Wilsonddd29622010-07-13 23:07:23 +0000587 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000588}
589
Stephen Wilsonce477322011-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 Lattner24943d22010-06-08 16:52:24 +0000602SectionList *
603ObjectFileELF::GetSectionList()
604{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000605 if (m_sections_ap.get())
606 return m_sections_ap.get();
607
608 if (ParseSectionHeaders() && GetSectionHeaderStringTable())
Chris Lattner24943d22010-06-08 16:52:24 +0000609 {
610 m_sections_ap.reset(new SectionList());
Chris Lattner24943d22010-06-08 16:52:24 +0000611
Stephen Wilsonddd29622010-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 Clayton23d90ae2012-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 Wilsonddd29622010-07-13 23:07:23 +0000620
Greg Clayton32a8c7e2010-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");
624 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
625 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
626 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
627 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
628 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
629 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
630 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
631 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
632 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
633 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
634 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
635 static ConstString g_sect_name_eh_frame (".eh_frame");
636
637 SectionType sect_type = eSectionTypeOther;
638
639 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
640 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
641 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
642 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
643 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
644 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
645 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
646 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
647 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
648 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
649 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
650 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
651 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
652 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
653 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
654
655
Stephen Wilsonddd29622010-07-13 23:07:23 +0000656 SectionSP section(new Section(
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000657 GetModule(), // Module to which this section belongs.
658 SectionIndex(I), // Section ID.
659 name, // Section name.
660 sect_type, // Section type.
661 header.sh_addr, // VM address.
Greg Clayton23d90ae2012-03-27 02:40:46 +0000662 vm_size, // VM size in bytes of this section.
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000663 header.sh_offset, // Offset of this section in the file.
Greg Clayton23d90ae2012-03-27 02:40:46 +0000664 file_size, // Size of the section as found in the file.
Greg Clayton32a8c7e2010-07-21 22:54:26 +0000665 header.sh_flags)); // Flags for this section.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000666
667 m_sections_ap->AddSection(section);
Chris Lattner24943d22010-06-08 16:52:24 +0000668 }
669 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000670
Chris Lattner24943d22010-06-08 16:52:24 +0000671 return m_sections_ap.get();
672}
673
Stephen Wilsonce477322011-03-30 16:07:05 +0000674static unsigned
675ParseSymbols(Symtab *symtab,
676 user_id_t start_id,
677 SectionList *section_list,
678 const ELFSectionHeader *symtab_shdr,
Stephen Wilsonddd29622010-07-13 23:07:23 +0000679 const DataExtractor &symtab_data,
680 const DataExtractor &strtab_data)
Chris Lattner24943d22010-06-08 16:52:24 +0000681{
Stephen Wilsonddd29622010-07-13 23:07:23 +0000682 ELFSymbol symbol;
Chris Lattner24943d22010-06-08 16:52:24 +0000683 uint32_t offset = 0;
Stephen Wilsonce477322011-03-30 16:07:05 +0000684 const unsigned num_symbols =
685 symtab_data.GetByteSize() / symtab_shdr->sh_entsize;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000686
Chris Lattner24943d22010-06-08 16:52:24 +0000687 static ConstString text_section_name(".text");
688 static ConstString init_section_name(".init");
689 static ConstString fini_section_name(".fini");
690 static ConstString ctors_section_name(".ctors");
691 static ConstString dtors_section_name(".dtors");
692
693 static ConstString data_section_name(".data");
694 static ConstString rodata_section_name(".rodata");
695 static ConstString rodata1_section_name(".rodata1");
696 static ConstString data2_section_name(".data1");
697 static ConstString bss_section_name(".bss");
698
Stephen Wilsonce477322011-03-30 16:07:05 +0000699 unsigned i;
700 for (i = 0; i < num_symbols; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000701 {
Stephen Wilsonddd29622010-07-13 23:07:23 +0000702 if (symbol.Parse(symtab_data, &offset) == false)
Chris Lattner24943d22010-06-08 16:52:24 +0000703 break;
704
Greg Clayton3508c382012-02-24 01:59:29 +0000705 SectionSP symbol_section_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000706 SymbolType symbol_type = eSymbolTypeInvalid;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000707 Elf64_Half symbol_idx = symbol.st_shndx;
Chris Lattner24943d22010-06-08 16:52:24 +0000708
Stephen Wilsonddd29622010-07-13 23:07:23 +0000709 switch (symbol_idx)
Chris Lattner24943d22010-06-08 16:52:24 +0000710 {
711 case SHN_ABS:
712 symbol_type = eSymbolTypeAbsolute;
713 break;
714 case SHN_UNDEF:
715 symbol_type = eSymbolTypeUndefined;
716 break;
717 default:
Greg Clayton3508c382012-02-24 01:59:29 +0000718 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
Chris Lattner24943d22010-06-08 16:52:24 +0000719 break;
720 }
721
Stephen Wilsonddd29622010-07-13 23:07:23 +0000722 switch (symbol.getType())
Chris Lattner24943d22010-06-08 16:52:24 +0000723 {
724 default:
725 case STT_NOTYPE:
726 // The symbol's type is not specified.
727 break;
728
729 case STT_OBJECT:
Stephen Wilsonddd29622010-07-13 23:07:23 +0000730 // The symbol is associated with a data object, such as a variable,
731 // an array, etc.
732 symbol_type = eSymbolTypeData;
Chris Lattner24943d22010-06-08 16:52:24 +0000733 break;
734
735 case STT_FUNC:
736 // The symbol is associated with a function or other executable code.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000737 symbol_type = eSymbolTypeCode;
Chris Lattner24943d22010-06-08 16:52:24 +0000738 break;
739
740 case STT_SECTION:
741 // The symbol is associated with a section. Symbol table entries of
742 // this type exist primarily for relocation and normally have
743 // STB_LOCAL binding.
744 break;
745
746 case STT_FILE:
747 // Conventionally, the symbol's name gives the name of the source
748 // file associated with the object file. A file symbol has STB_LOCAL
749 // binding, its section index is SHN_ABS, and it precedes the other
750 // STB_LOCAL symbols for the file, if it is present.
Stephen Wilsonddd29622010-07-13 23:07:23 +0000751 symbol_type = eSymbolTypeObjectFile;
Chris Lattner24943d22010-06-08 16:52:24 +0000752 break;
753 }
754
755 if (symbol_type == eSymbolTypeInvalid)
756 {
Greg Clayton3508c382012-02-24 01:59:29 +0000757 if (symbol_section_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000758 {
Greg Clayton3508c382012-02-24 01:59:29 +0000759 const ConstString &sect_name = symbol_section_sp->GetName();
Chris Lattner24943d22010-06-08 16:52:24 +0000760 if (sect_name == text_section_name ||
761 sect_name == init_section_name ||
762 sect_name == fini_section_name ||
763 sect_name == ctors_section_name ||
764 sect_name == dtors_section_name)
765 {
766 symbol_type = eSymbolTypeCode;
767 }
Stephen Wilsonddd29622010-07-13 23:07:23 +0000768 else if (sect_name == data_section_name ||
769 sect_name == data2_section_name ||
770 sect_name == rodata_section_name ||
771 sect_name == rodata1_section_name ||
772 sect_name == bss_section_name)
Chris Lattner24943d22010-06-08 16:52:24 +0000773 {
774 symbol_type = eSymbolTypeData;
775 }
776 }
777 }
778
779 uint64_t symbol_value = symbol.st_value;
Greg Clayton3508c382012-02-24 01:59:29 +0000780 if (symbol_section_sp)
781 symbol_value -= symbol_section_sp->GetFileAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000782 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000783 bool is_global = symbol.getBinding() == STB_GLOBAL;
784 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
Greg Clayton23d90ae2012-03-27 02:40:46 +0000785 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000786 Symbol dc_symbol(
Greg Clayton3508c382012-02-24 01:59:29 +0000787 i + start_id, // ID is the original symbol table index.
788 symbol_name, // Symbol name.
Greg Clayton23d90ae2012-03-27 02:40:46 +0000789 is_mangled, // Is the symbol name mangled?
Greg Clayton3508c382012-02-24 01:59:29 +0000790 symbol_type, // Type of this symbol
791 is_global, // Is this globally visible?
792 false, // Is this symbol debug info?
793 false, // Is this symbol a trampoline?
794 false, // Is this symbol artificial?
795 symbol_section_sp, // Section in which this symbol is defined or null.
796 symbol_value, // Offset in section or symbol value.
797 symbol.st_size, // Size in bytes of this symbol.
798 flags); // Symbol flags.
Chris Lattner24943d22010-06-08 16:52:24 +0000799 symtab->AddSymbol(dc_symbol);
800 }
Stephen Wilsonce477322011-03-30 16:07:05 +0000801
802 return i;
Chris Lattner24943d22010-06-08 16:52:24 +0000803}
804
Stephen Wilsonce477322011-03-30 16:07:05 +0000805unsigned
806ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
807 const ELFSectionHeader *symtab_hdr,
Stephen Wilsonddd29622010-07-13 23:07:23 +0000808 user_id_t symtab_id)
809{
Stephen Wilsonce477322011-03-30 16:07:05 +0000810 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
811 symtab_hdr->sh_type == SHT_DYNSYM);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000812
813 // Parse in the section list if needed.
814 SectionList *section_list = GetSectionList();
815 if (!section_list)
Stephen Wilsonce477322011-03-30 16:07:05 +0000816 return 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000817
818 // Section ID's are ones based.
Stephen Wilsonce477322011-03-30 16:07:05 +0000819 user_id_t strtab_id = symtab_hdr->sh_link + 1;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000820
821 Section *symtab = section_list->FindSectionByID(symtab_id).get();
822 Section *strtab = section_list->FindSectionByID(strtab_id).get();
Stephen Wilsonce477322011-03-30 16:07:05 +0000823 unsigned num_symbols = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +0000824 if (symtab && strtab)
825 {
826 DataExtractor symtab_data;
827 DataExtractor strtab_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000828 if (ReadSectionData(symtab, symtab_data) &&
829 ReadSectionData(strtab, strtab_data))
Stephen Wilsonddd29622010-07-13 23:07:23 +0000830 {
Stephen Wilsonce477322011-03-30 16:07:05 +0000831 num_symbols = ParseSymbols(symbol_table, start_id,
832 section_list, symtab_hdr,
833 symtab_data, strtab_data);
Stephen Wilsonddd29622010-07-13 23:07:23 +0000834 }
835 }
Stephen Wilsonce477322011-03-30 16:07:05 +0000836
837 return num_symbols;
838}
839
840size_t
841ObjectFileELF::ParseDynamicSymbols()
842{
843 if (m_dynamic_symbols.size())
844 return m_dynamic_symbols.size();
845
846 user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC);
847 if (!dyn_id)
848 return NULL;
849
850 SectionList *section_list = GetSectionList();
851 if (!section_list)
852 return NULL;
853
854 Section *dynsym = section_list->FindSectionByID(dyn_id).get();
855 if (!dynsym)
856 return NULL;
857
858 ELFDynamic symbol;
859 DataExtractor dynsym_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +0000860 if (ReadSectionData(dynsym, dynsym_data))
Stephen Wilsonce477322011-03-30 16:07:05 +0000861 {
862
863 const unsigned section_size = dynsym_data.GetByteSize();
864 unsigned offset = 0;
865 unsigned cursor = 0;
866
867 while (cursor < section_size)
868 {
869 offset = cursor;
870 if (!symbol.Parse(dynsym_data, &cursor))
871 break;
872
873 m_dynamic_symbols.push_back(symbol);
874 }
875 }
876
877 return m_dynamic_symbols.size();
878}
879
880const ELFDynamic *
881ObjectFileELF::FindDynamicSymbol(unsigned tag)
882{
883 if (!ParseDynamicSymbols())
884 return NULL;
885
886 SectionList *section_list = GetSectionList();
887 if (!section_list)
888 return 0;
889
890 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
891 DynamicSymbolCollIter E = m_dynamic_symbols.end();
892 for ( ; I != E; ++I)
893 {
894 ELFDynamic *symbol = &*I;
895
896 if (symbol->d_tag == tag)
897 return symbol;
898 }
899
900 return NULL;
901}
902
903Section *
904ObjectFileELF::PLTSection()
905{
906 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
907 SectionList *section_list = GetSectionList();
908
909 if (symbol && section_list)
910 {
911 addr_t addr = symbol->d_ptr;
912 return section_list->FindSectionContainingFileAddress(addr).get();
913 }
914
915 return NULL;
916}
917
918unsigned
919ObjectFileELF::PLTRelocationType()
920{
921 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
922
923 if (symbol)
924 return symbol->d_val;
925
926 return 0;
927}
928
929static unsigned
930ParsePLTRelocations(Symtab *symbol_table,
931 user_id_t start_id,
932 unsigned rel_type,
933 const ELFHeader *hdr,
934 const ELFSectionHeader *rel_hdr,
935 const ELFSectionHeader *plt_hdr,
936 const ELFSectionHeader *sym_hdr,
Greg Clayton3508c382012-02-24 01:59:29 +0000937 const lldb::SectionSP &plt_section_sp,
Stephen Wilsonce477322011-03-30 16:07:05 +0000938 DataExtractor &rel_data,
939 DataExtractor &symtab_data,
940 DataExtractor &strtab_data)
941{
942 ELFRelocation rel(rel_type);
943 ELFSymbol symbol;
944 uint32_t offset = 0;
945 const unsigned plt_entsize = plt_hdr->sh_entsize;
946 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
947
948 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
949 reloc_info_fn reloc_type;
950 reloc_info_fn reloc_symbol;
951
952 if (hdr->Is32Bit() == 4)
953 {
954 reloc_type = ELFRelocation::RelocType32;
955 reloc_symbol = ELFRelocation::RelocSymbol32;
956 }
957 else
958 {
959 reloc_type = ELFRelocation::RelocType64;
960 reloc_symbol = ELFRelocation::RelocSymbol64;
961 }
962
963 unsigned slot_type = hdr->GetRelocationJumpSlotType();
964 unsigned i;
965 for (i = 0; i < num_relocations; ++i)
966 {
967 if (rel.Parse(rel_data, &offset) == false)
968 break;
969
970 if (reloc_type(rel) != slot_type)
971 continue;
972
973 unsigned symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
974 uint64_t plt_index = (i + 1) * plt_entsize;
975
976 if (!symbol.Parse(symtab_data, &symbol_offset))
977 break;
978
979 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
Greg Clayton23d90ae2012-03-27 02:40:46 +0000980 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
Stephen Wilsonce477322011-03-30 16:07:05 +0000981
982 Symbol jump_symbol(
983 i + start_id, // Symbol table index
984 symbol_name, // symbol name.
Greg Clayton23d90ae2012-03-27 02:40:46 +0000985 is_mangled, // is the symbol name mangled?
Stephen Wilsonce477322011-03-30 16:07:05 +0000986 eSymbolTypeTrampoline, // Type of this symbol
987 false, // Is this globally visible?
988 false, // Is this symbol debug info?
989 true, // Is this symbol a trampoline?
990 true, // Is this symbol artificial?
Greg Clayton3508c382012-02-24 01:59:29 +0000991 plt_section_sp, // Section in which this symbol is defined or null.
Stephen Wilsonce477322011-03-30 16:07:05 +0000992 plt_index, // Offset in section or symbol value.
993 plt_entsize, // Size in bytes of this symbol.
994 0); // Symbol flags.
995
996 symbol_table->AddSymbol(jump_symbol);
997 }
998
999 return i;
1000}
1001
1002unsigned
1003ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1004 user_id_t start_id,
1005 const ELFSectionHeader *rel_hdr,
1006 user_id_t rel_id)
1007{
1008 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1009
1010 // The link field points to the asscoiated symbol table. The info field
1011 // points to the section holding the plt.
1012 user_id_t symtab_id = rel_hdr->sh_link;
1013 user_id_t plt_id = rel_hdr->sh_info;
1014
1015 if (!symtab_id || !plt_id)
1016 return 0;
1017
1018 // Section ID's are ones based;
1019 symtab_id++;
1020 plt_id++;
1021
1022 const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id);
1023 if (!plt_hdr)
1024 return 0;
1025
1026 const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1027 if (!sym_hdr)
1028 return 0;
1029
1030 SectionList *section_list = GetSectionList();
1031 if (!section_list)
1032 return 0;
1033
1034 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1035 if (!rel_section)
1036 return 0;
1037
Greg Clayton3508c382012-02-24 01:59:29 +00001038 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1039 if (!plt_section_sp)
Stephen Wilsonce477322011-03-30 16:07:05 +00001040 return 0;
1041
1042 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1043 if (!symtab)
1044 return 0;
1045
1046 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1047 if (!strtab)
1048 return 0;
1049
1050 DataExtractor rel_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001051 if (!ReadSectionData(rel_section, rel_data))
Stephen Wilsonce477322011-03-30 16:07:05 +00001052 return 0;
1053
1054 DataExtractor symtab_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001055 if (!ReadSectionData(symtab, symtab_data))
Stephen Wilsonce477322011-03-30 16:07:05 +00001056 return 0;
1057
1058 DataExtractor strtab_data;
Greg Claytonb5a8f142012-02-05 02:38:54 +00001059 if (!ReadSectionData(strtab, strtab_data))
Stephen Wilsonce477322011-03-30 16:07:05 +00001060 return 0;
1061
1062 unsigned rel_type = PLTRelocationType();
1063 if (!rel_type)
1064 return 0;
1065
Greg Clayton3508c382012-02-24 01:59:29 +00001066 return ParsePLTRelocations (symbol_table,
1067 start_id,
1068 rel_type,
1069 &m_header,
1070 rel_hdr,
1071 plt_hdr,
1072 sym_hdr,
1073 plt_section_sp,
1074 rel_data,
1075 symtab_data,
1076 strtab_data);
Stephen Wilsonddd29622010-07-13 23:07:23 +00001077}
Chris Lattner24943d22010-06-08 16:52:24 +00001078
1079Symtab *
1080ObjectFileELF::GetSymtab()
1081{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001082 if (m_symtab_ap.get())
1083 return m_symtab_ap.get();
1084
1085 Symtab *symbol_table = new Symtab(this);
1086 m_symtab_ap.reset(symbol_table);
1087
Stephen Wilsonce477322011-03-30 16:07:05 +00001088 Mutex::Locker locker(symbol_table->GetMutex());
Greg Clayton8d3802d2010-10-08 04:20:14 +00001089
Stephen Wilsonddd29622010-07-13 23:07:23 +00001090 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1091 return symbol_table;
1092
1093 // Locate and parse all linker symbol tables.
Stephen Wilsonce477322011-03-30 16:07:05 +00001094 uint64_t symbol_id = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +00001095 for (SectionHeaderCollIter I = m_section_headers.begin();
1096 I != m_section_headers.end(); ++I)
Chris Lattner24943d22010-06-08 16:52:24 +00001097 {
Peter Collingbourne182c2862011-06-03 20:39:58 +00001098 if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM)
Chris Lattner24943d22010-06-08 16:52:24 +00001099 {
Stephen Wilsonce477322011-03-30 16:07:05 +00001100 const ELFSectionHeader &symtab_header = *I;
Stephen Wilsonddd29622010-07-13 23:07:23 +00001101 user_id_t section_id = SectionIndex(I);
Stephen Wilsonce477322011-03-30 16:07:05 +00001102 symbol_id += ParseSymbolTable(symbol_table, symbol_id,
1103 &symtab_header, section_id);
Chris Lattner24943d22010-06-08 16:52:24 +00001104 }
1105 }
Stephen Wilsonce477322011-03-30 16:07:05 +00001106
1107 // Synthesize trampoline symbols to help navigate the PLT.
1108 Section *reloc_section = PLTSection();
1109 if (reloc_section)
1110 {
1111 user_id_t reloc_id = reloc_section->GetID();
1112 const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id);
1113 assert(reloc_header);
1114
1115 ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id);
1116 }
Stephen Wilsonddd29622010-07-13 23:07:23 +00001117
1118 return symbol_table;
Chris Lattner24943d22010-06-08 16:52:24 +00001119}
1120
Chris Lattner24943d22010-06-08 16:52:24 +00001121//===----------------------------------------------------------------------===//
1122// Dump
1123//
1124// Dump the specifics of the runtime file container (such as any headers
1125// segments, sections, etc).
1126//----------------------------------------------------------------------
1127void
1128ObjectFileELF::Dump(Stream *s)
1129{
1130 DumpELFHeader(s, m_header);
1131 s->EOL();
1132 DumpELFProgramHeaders(s);
1133 s->EOL();
1134 DumpELFSectionHeaders(s);
1135 s->EOL();
1136 SectionList *section_list = GetSectionList();
1137 if (section_list)
Greg Clayton58e844b2010-12-08 05:08:21 +00001138 section_list->Dump(s, NULL, true, UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00001139 Symtab *symtab = GetSymtab();
1140 if (symtab)
Greg Claytonb3448432011-03-24 21:19:54 +00001141 symtab->Dump(s, NULL, eSortOrderNone);
Chris Lattner24943d22010-06-08 16:52:24 +00001142 s->EOL();
Stephen Wilsonddd29622010-07-13 23:07:23 +00001143 DumpDependentModules(s);
1144 s->EOL();
Chris Lattner24943d22010-06-08 16:52:24 +00001145}
1146
1147//----------------------------------------------------------------------
1148// DumpELFHeader
1149//
1150// Dump the ELF header to the specified output stream
1151//----------------------------------------------------------------------
1152void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001153ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
Chris Lattner24943d22010-06-08 16:52:24 +00001154{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001155 s->PutCString("ELF Header\n");
1156 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1157 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
1158 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1159 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
1160 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1161 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
1162 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
Chris Lattner24943d22010-06-08 16:52:24 +00001163
Stephen Wilsonddd29622010-07-13 23:07:23 +00001164 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1165 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
Chris Lattner24943d22010-06-08 16:52:24 +00001166 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1167 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1168 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1169
1170 s->Printf("e_type = 0x%4.4x ", header.e_type);
1171 DumpELFHeader_e_type(s, header.e_type);
1172 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
1173 s->Printf("e_version = 0x%8.8x\n", header.e_version);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001174 s->Printf("e_entry = 0x%8.8llx\n", header.e_entry);
1175 s->Printf("e_phoff = 0x%8.8llx\n", header.e_phoff);
1176 s->Printf("e_shoff = 0x%8.8llx\n", header.e_shoff);
Chris Lattner24943d22010-06-08 16:52:24 +00001177 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
1178 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
1179 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1180 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
1181 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1182 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
1183 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
1184}
1185
1186//----------------------------------------------------------------------
1187// DumpELFHeader_e_type
1188//
1189// Dump an token value for the ELF header member e_type
1190//----------------------------------------------------------------------
1191void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001192ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001193{
1194 switch (e_type)
1195 {
1196 case ET_NONE: *s << "ET_NONE"; break;
1197 case ET_REL: *s << "ET_REL"; break;
1198 case ET_EXEC: *s << "ET_EXEC"; break;
1199 case ET_DYN: *s << "ET_DYN"; break;
1200 case ET_CORE: *s << "ET_CORE"; break;
1201 default:
1202 break;
1203 }
1204}
1205
1206//----------------------------------------------------------------------
1207// DumpELFHeader_e_ident_EI_DATA
1208//
1209// Dump an token value for the ELF header member e_ident[EI_DATA]
1210//----------------------------------------------------------------------
1211void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001212ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
Chris Lattner24943d22010-06-08 16:52:24 +00001213{
1214 switch (ei_data)
1215 {
1216 case ELFDATANONE: *s << "ELFDATANONE"; break;
1217 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
1218 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
1219 default:
1220 break;
1221 }
1222}
1223
1224
1225//----------------------------------------------------------------------
1226// DumpELFProgramHeader
1227//
1228// Dump a single ELF program header to the specified output stream
1229//----------------------------------------------------------------------
1230void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001231ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
Chris Lattner24943d22010-06-08 16:52:24 +00001232{
1233 DumpELFProgramHeader_p_type(s, ph.p_type);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001234 s->Printf(" %8.8llx %8.8llx %8.8llx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
1235 s->Printf(" %8.8llx %8.8llx %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags);
Stephen Wilsonddd29622010-07-13 23:07:23 +00001236
Chris Lattner24943d22010-06-08 16:52:24 +00001237 DumpELFProgramHeader_p_flags(s, ph.p_flags);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001238 s->Printf(") %8.8llx", ph.p_align);
Chris Lattner24943d22010-06-08 16:52:24 +00001239}
1240
1241//----------------------------------------------------------------------
1242// DumpELFProgramHeader_p_type
1243//
1244// Dump an token value for the ELF program header member p_type which
1245// describes the type of the program header
Stephen Wilsonddd29622010-07-13 23:07:23 +00001246// ----------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +00001247void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001248ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001249{
1250 const int kStrWidth = 10;
1251 switch (p_type)
1252 {
1253 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
1254 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
1255 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
1256 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
1257 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
1258 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
1259 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
1260 default:
1261 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1262 break;
1263 }
1264}
1265
1266
1267//----------------------------------------------------------------------
1268// DumpELFProgramHeader_p_flags
1269//
1270// Dump an token value for the ELF program header member p_flags
1271//----------------------------------------------------------------------
1272void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001273ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
Chris Lattner24943d22010-06-08 16:52:24 +00001274{
1275 *s << ((p_flags & PF_X) ? "PF_X" : " ")
1276 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1277 << ((p_flags & PF_W) ? "PF_W" : " ")
1278 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1279 << ((p_flags & PF_R) ? "PF_R" : " ");
1280}
1281
1282//----------------------------------------------------------------------
1283// DumpELFProgramHeaders
1284//
1285// Dump all of the ELF program header to the specified output stream
1286//----------------------------------------------------------------------
1287void
1288ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1289{
1290 if (ParseProgramHeaders())
1291 {
1292 s->PutCString("Program Headers\n");
Stephen Wilsonddd29622010-07-13 23:07:23 +00001293 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
1294 "p_filesz p_memsz p_flags p_align\n");
1295 s->PutCString("==== ---------- -------- -------- -------- "
1296 "-------- -------- ------------------------- --------\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001297
1298 uint32_t idx = 0;
Stephen Wilsonddd29622010-07-13 23:07:23 +00001299 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1300 I != m_program_headers.end(); ++I, ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001301 {
Stephen Wilsonddd29622010-07-13 23:07:23 +00001302 s->Printf("[%2u] ", idx);
1303 ObjectFileELF::DumpELFProgramHeader(s, *I);
Chris Lattner24943d22010-06-08 16:52:24 +00001304 s->EOL();
1305 }
1306 }
1307}
1308
Chris Lattner24943d22010-06-08 16:52:24 +00001309//----------------------------------------------------------------------
1310// DumpELFSectionHeader
1311//
1312// Dump a single ELF section header to the specified output stream
1313//----------------------------------------------------------------------
1314void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001315ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
Chris Lattner24943d22010-06-08 16:52:24 +00001316{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001317 s->Printf("%8.8x ", sh.sh_name);
Chris Lattner24943d22010-06-08 16:52:24 +00001318 DumpELFSectionHeader_sh_type(s, sh.sh_type);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001319 s->Printf(" %8.8llx (", sh.sh_flags);
Chris Lattner24943d22010-06-08 16:52:24 +00001320 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001321 s->Printf(") %8.8llx %8.8llx %8.8llx", sh.sh_addr, sh.sh_offset, sh.sh_size);
Stephen Wilsonddd29622010-07-13 23:07:23 +00001322 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +00001323 s->Printf(" %8.8llx %8.8llx", sh.sh_addralign, sh.sh_entsize);
Chris Lattner24943d22010-06-08 16:52:24 +00001324}
1325
1326//----------------------------------------------------------------------
1327// DumpELFSectionHeader_sh_type
1328//
1329// Dump an token value for the ELF section header member sh_type which
1330// describes the type of the section
1331//----------------------------------------------------------------------
1332void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001333ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
Chris Lattner24943d22010-06-08 16:52:24 +00001334{
1335 const int kStrWidth = 12;
1336 switch (sh_type)
1337 {
1338 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
1339 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1340 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
1341 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
1342 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
1343 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
1344 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
1345 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
1346 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
1347 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
1348 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
1349 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
1350 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
1351 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
1352 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
1353 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
1354 default:
1355 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1356 break;
1357 }
1358}
1359
Chris Lattner24943d22010-06-08 16:52:24 +00001360//----------------------------------------------------------------------
1361// DumpELFSectionHeader_sh_flags
1362//
1363// Dump an token value for the ELF section header member sh_flags
1364//----------------------------------------------------------------------
1365void
Stephen Wilsonddd29622010-07-13 23:07:23 +00001366ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
Chris Lattner24943d22010-06-08 16:52:24 +00001367{
1368 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
1369 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1370 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
1371 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1372 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
1373}
Stephen Wilsonddd29622010-07-13 23:07:23 +00001374
Chris Lattner24943d22010-06-08 16:52:24 +00001375//----------------------------------------------------------------------
1376// DumpELFSectionHeaders
1377//
1378// Dump all of the ELF section header to the specified output stream
1379//----------------------------------------------------------------------
1380void
1381ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1382{
Stephen Wilsonddd29622010-07-13 23:07:23 +00001383 if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1384 return;
1385
1386 s->PutCString("Section Headers\n");
1387 s->PutCString("IDX name type flags "
1388 "addr offset size link info addralgn "
1389 "entsize Name\n");
1390 s->PutCString("==== -------- ------------ -------------------------------- "
1391 "-------- -------- -------- -------- -------- -------- "
1392 "-------- ====================\n");
1393
1394 uint32_t idx = 0;
1395 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1396 I != m_section_headers.end(); ++I, ++idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001397 {
Stephen Wilsonddd29622010-07-13 23:07:23 +00001398 s->Printf("[%2u] ", idx);
1399 ObjectFileELF::DumpELFSectionHeader(s, *I);
1400 const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1401 if (section_name)
1402 *s << ' ' << section_name << "\n";
1403 }
1404}
Chris Lattner24943d22010-06-08 16:52:24 +00001405
Stephen Wilsonddd29622010-07-13 23:07:23 +00001406void
1407ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1408{
1409 size_t num_modules = ParseDependentModules();
Chris Lattner24943d22010-06-08 16:52:24 +00001410
Stephen Wilsonddd29622010-07-13 23:07:23 +00001411 if (num_modules > 0)
1412 {
1413 s->PutCString("Dependent Modules:\n");
1414 for (unsigned i = 0; i < num_modules; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +00001415 {
Stephen Wilsonddd29622010-07-13 23:07:23 +00001416 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1417 s->Printf(" %s\n", spec.GetFilename().GetCString());
Chris Lattner24943d22010-06-08 16:52:24 +00001418 }
1419 }
1420}
1421
Chris Lattner24943d22010-06-08 16:52:24 +00001422bool
Greg Clayton395fc332011-02-15 21:59:32 +00001423ObjectFileELF::GetArchitecture (ArchSpec &arch)
Chris Lattner24943d22010-06-08 16:52:24 +00001424{
Stephen Wilsonf7cc64d2011-02-24 19:16:15 +00001425 if (!ParseHeader())
1426 return false;
1427
Greg Claytonb3448432011-03-24 21:19:54 +00001428 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
Greg Clayton940b1032011-02-23 00:35:02 +00001429 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1430 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
Stephen Wilsonddd29622010-07-13 23:07:23 +00001431 return true;
Chris Lattner24943d22010-06-08 16:52:24 +00001432}
1433
Greg Claytonca319972011-07-09 00:41:34 +00001434ObjectFile::Type
1435ObjectFileELF::CalculateType()
1436{
1437 switch (m_header.e_type)
1438 {
1439 case llvm::ELF::ET_NONE:
1440 // 0 - No file type
1441 return eTypeUnknown;
1442
1443 case llvm::ELF::ET_REL:
1444 // 1 - Relocatable file
1445 return eTypeObjectFile;
1446
1447 case llvm::ELF::ET_EXEC:
1448 // 2 - Executable file
1449 return eTypeExecutable;
1450
1451 case llvm::ELF::ET_DYN:
1452 // 3 - Shared object file
1453 return eTypeSharedLibrary;
1454
1455 case ET_CORE:
1456 // 4 - Core file
1457 return eTypeCoreFile;
1458
1459 default:
1460 break;
1461 }
1462 return eTypeUnknown;
1463}
1464
1465ObjectFile::Strata
1466ObjectFileELF::CalculateStrata()
1467{
1468 switch (m_header.e_type)
1469 {
1470 case llvm::ELF::ET_NONE:
1471 // 0 - No file type
1472 return eStrataUnknown;
1473
1474 case llvm::ELF::ET_REL:
1475 // 1 - Relocatable file
1476 return eStrataUnknown;
1477
1478 case llvm::ELF::ET_EXEC:
1479 // 2 - Executable file
1480 // TODO: is there any way to detect that an executable is a kernel
1481 // related executable by inspecting the program headers, section
1482 // headers, symbols, or any other flag bits???
1483 return eStrataUser;
1484
1485 case llvm::ELF::ET_DYN:
1486 // 3 - Shared object file
1487 // TODO: is there any way to detect that an shared library is a kernel
1488 // related executable by inspecting the program headers, section
1489 // headers, symbols, or any other flag bits???
1490 return eStrataUnknown;
1491
1492 case ET_CORE:
1493 // 4 - Core file
1494 // TODO: is there any way to detect that an core file is a kernel
1495 // related executable by inspecting the program headers, section
1496 // headers, symbols, or any other flag bits???
1497 return eStrataUnknown;
1498
1499 default:
1500 break;
1501 }
1502 return eStrataUnknown;
1503}
1504