xref: /llvm-project/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp (revision b4aabeb8d72692d85292f435881476cc42ae8b6c)
1 //===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
2 //
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 
12 #include <cassert>
13 #include <algorithm>
14 
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/DataBuffer.h"
17 #include "lldb/Core/Error.h"
18 #include "lldb/Core/FileSpecList.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/Section.h"
22 #include "lldb/Core/Stream.h"
23 #include "lldb/Symbol/SymbolContext.h"
24 #include "lldb/Host/Host.h"
25 
26 #include "llvm/ADT/PointerUnion.h"
27 
28 #define CASE_AND_STREAM(s, def, width)                  \
29     case def: s->Printf("%-*s", width, #def); break;
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 using namespace elf;
34 using namespace llvm::ELF;
35 
36 namespace {
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.
43 class ELFRelocation
44 {
45 public:
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 
70 private:
71     typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
72 
73     RelocUnion reloc;
74 };
75 
76 ELFRelocation::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 
88 ELFRelocation::~ELFRelocation()
89 {
90     if (reloc.is<ELFRel*>())
91         delete reloc.get<ELFRel*>();
92     else
93         delete reloc.get<ELFRela*>();
94 }
95 
96 bool
97 ELFRelocation::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 
105 unsigned
106 ELFRelocation::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 
114 unsigned
115 ELFRelocation::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 
123 unsigned
124 ELFRelocation::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 
132 unsigned
133 ELFRelocation::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 
143 //------------------------------------------------------------------
144 // Static methods.
145 //------------------------------------------------------------------
146 void
147 ObjectFileELF::Initialize()
148 {
149     PluginManager::RegisterPlugin(GetPluginNameStatic(),
150                                   GetPluginDescriptionStatic(),
151                                   CreateInstance);
152 }
153 
154 void
155 ObjectFileELF::Terminate()
156 {
157     PluginManager::UnregisterPlugin(CreateInstance);
158 }
159 
160 const char *
161 ObjectFileELF::GetPluginNameStatic()
162 {
163     return "object-file.elf";
164 }
165 
166 const char *
167 ObjectFileELF::GetPluginDescriptionStatic()
168 {
169     return "ELF object file reader.";
170 }
171 
172 ObjectFile *
173 ObjectFileELF::CreateInstance(Module *module,
174                               DataBufferSP &data_sp,
175                               const FileSpec *file, addr_t offset,
176                               addr_t length)
177 {
178     if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
179     {
180         const uint8_t *magic = data_sp->GetBytes() + offset;
181         if (ELFHeader::MagicBytesMatch(magic))
182         {
183             unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
184             if (address_size == 4 || address_size == 8)
185             {
186                 std::auto_ptr<ObjectFileELF> objfile_ap(
187                     new ObjectFileELF(module, data_sp, file, offset, length));
188                 ArchSpec spec;
189                 if (objfile_ap->GetArchitecture(spec) &&
190                     objfile_ap->SetModulesArchitecture(spec))
191                     return objfile_ap.release();
192             }
193         }
194     }
195     return NULL;
196 }
197 
198 
199 //------------------------------------------------------------------
200 // PluginInterface protocol
201 //------------------------------------------------------------------
202 const char *
203 ObjectFileELF::GetPluginName()
204 {
205     return "ObjectFileELF";
206 }
207 
208 const char *
209 ObjectFileELF::GetShortPluginName()
210 {
211     return GetPluginNameStatic();
212 }
213 
214 uint32_t
215 ObjectFileELF::GetPluginVersion()
216 {
217     return m_plugin_version;
218 }
219 //------------------------------------------------------------------
220 // ObjectFile protocol
221 //------------------------------------------------------------------
222 
223 ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
224                              const FileSpec* file, addr_t offset,
225                              addr_t length)
226     : ObjectFile(module, file, offset, length, dataSP),
227       m_header(),
228       m_program_headers(),
229       m_section_headers(),
230       m_sections_ap(),
231       m_symtab_ap(),
232       m_filespec_ap(),
233       m_shstr_data()
234 {
235     if (file)
236         m_file = *file;
237     ::memset(&m_header, 0, sizeof(m_header));
238 }
239 
240 ObjectFileELF::~ObjectFileELF()
241 {
242 }
243 
244 bool
245 ObjectFileELF::IsExecutable() const
246 {
247     return m_header.e_entry != 0;
248 }
249 
250 ByteOrder
251 ObjectFileELF::GetByteOrder() const
252 {
253     if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
254         return eByteOrderBig;
255     if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
256         return eByteOrderLittle;
257     return eByteOrderInvalid;
258 }
259 
260 size_t
261 ObjectFileELF::GetAddressByteSize() const
262 {
263     return m_data.GetAddressByteSize();
264 }
265 
266 unsigned
267 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
268 {
269     return std::distance(m_section_headers.begin(), I) + 1;
270 }
271 
272 unsigned
273 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
274 {
275     return std::distance(m_section_headers.begin(), I) + 1;
276 }
277 
278 bool
279 ObjectFileELF::ParseHeader()
280 {
281     uint32_t offset = GetOffset();
282     return m_header.Parse(m_data, &offset);
283 }
284 
285 bool
286 ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
287 {
288     // FIXME: Return MD5 sum here.  See comment in ObjectFile.h.
289     return false;
290 }
291 
292 uint32_t
293 ObjectFileELF::GetDependentModules(FileSpecList &files)
294 {
295     size_t num_modules = ParseDependentModules();
296     uint32_t num_specs = 0;
297 
298     for (unsigned i = 0; i < num_modules; ++i)
299     {
300         if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
301             num_specs++;
302     }
303 
304     return num_specs;
305 }
306 
307 user_id_t
308 ObjectFileELF::GetSectionIndexByType(unsigned type)
309 {
310     if (!ParseSectionHeaders())
311         return 0;
312 
313     for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
314          sh_pos != m_section_headers.end(); ++sh_pos)
315     {
316         if (sh_pos->sh_type == type)
317             return SectionIndex(sh_pos);
318     }
319 
320     return 0;
321 }
322 
323 Address
324 ObjectFileELF::GetImageInfoAddress()
325 {
326     if (!ParseDynamicSymbols())
327         return Address();
328 
329     SectionList *section_list = GetSectionList();
330     if (!section_list)
331         return Address();
332 
333     user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC);
334     if (!dynsym_id)
335         return Address();
336 
337     const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
338     if (!dynsym_hdr)
339         return Address();
340 
341     Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
342     if (!dynsym)
343         return Address();
344 
345     for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
346     {
347         ELFDynamic &symbol = m_dynamic_symbols[i];
348 
349         if (symbol.d_tag == DT_DEBUG)
350         {
351             // Compute the offset as the number of previous entries plus the
352             // size of d_tag.
353             addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
354             return Address(dynsym, offset);
355         }
356     }
357 
358     return Address();
359 }
360 
361 lldb_private::Address
362 ObjectFileELF::GetEntryPointAddress ()
363 {
364     SectionList *sections;
365     addr_t offset;
366 
367     if (m_entry_point_address.IsValid())
368         return m_entry_point_address;
369 
370     if (!ParseHeader() || !IsExecutable())
371         return m_entry_point_address;
372 
373     sections = GetSectionList();
374     offset = m_header.e_entry;
375 
376     if (!sections)
377     {
378         m_entry_point_address.SetOffset(offset);
379         return m_entry_point_address;
380     }
381 
382     m_entry_point_address.ResolveAddressUsingFileSections(offset, sections);
383 
384     return m_entry_point_address;
385 }
386 
387 //----------------------------------------------------------------------
388 // ParseDependentModules
389 //----------------------------------------------------------------------
390 size_t
391 ObjectFileELF::ParseDependentModules()
392 {
393     if (m_filespec_ap.get())
394         return m_filespec_ap->GetSize();
395 
396     m_filespec_ap.reset(new FileSpecList());
397 
398     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
399         return 0;
400 
401     // Locate the dynamic table.
402     user_id_t dynsym_id = 0;
403     user_id_t dynstr_id = 0;
404     for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
405          sh_pos != m_section_headers.end(); ++sh_pos)
406     {
407         if (sh_pos->sh_type == SHT_DYNAMIC)
408         {
409             dynsym_id = SectionIndex(sh_pos);
410             dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
411             break;
412         }
413     }
414 
415     if (!(dynsym_id && dynstr_id))
416         return 0;
417 
418     SectionList *section_list = GetSectionList();
419     if (!section_list)
420         return 0;
421 
422     // Resolve and load the dynamic table entries and corresponding string
423     // table.
424     Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
425     Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
426     if (!(dynsym && dynstr))
427         return 0;
428 
429     DataExtractor dynsym_data;
430     DataExtractor dynstr_data;
431     if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) &&
432         dynstr->ReadSectionDataFromObjectFile(this, dynstr_data))
433     {
434         ELFDynamic symbol;
435         const unsigned section_size = dynsym_data.GetByteSize();
436         unsigned offset = 0;
437 
438         // The only type of entries we are concerned with are tagged DT_NEEDED,
439         // yielding the name of a required library.
440         while (offset < section_size)
441         {
442             if (!symbol.Parse(dynsym_data, &offset))
443                 break;
444 
445             if (symbol.d_tag != DT_NEEDED)
446                 continue;
447 
448             uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
449             const char *lib_name = dynstr_data.PeekCStr(str_index);
450             m_filespec_ap->Append(FileSpec(lib_name, true));
451         }
452     }
453 
454     return m_filespec_ap->GetSize();
455 }
456 
457 //----------------------------------------------------------------------
458 // ParseProgramHeaders
459 //----------------------------------------------------------------------
460 size_t
461 ObjectFileELF::ParseProgramHeaders()
462 {
463     // We have already parsed the program headers
464     if (!m_program_headers.empty())
465         return m_program_headers.size();
466 
467     // If there are no program headers to read we are done.
468     if (m_header.e_phnum == 0)
469         return 0;
470 
471     m_program_headers.resize(m_header.e_phnum);
472     if (m_program_headers.size() != m_header.e_phnum)
473         return 0;
474 
475     const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
476     const elf_off ph_offset = m_offset + m_header.e_phoff;
477     DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size));
478 
479     if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size)
480         return 0;
481 
482     DataExtractor data(buffer_sp, m_data.GetByteOrder(),
483                        m_data.GetAddressByteSize());
484 
485     uint32_t idx;
486     uint32_t offset;
487     for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
488     {
489         if (m_program_headers[idx].Parse(data, &offset) == false)
490             break;
491     }
492 
493     if (idx < m_program_headers.size())
494         m_program_headers.resize(idx);
495 
496     return m_program_headers.size();
497 }
498 
499 //----------------------------------------------------------------------
500 // ParseSectionHeaders
501 //----------------------------------------------------------------------
502 size_t
503 ObjectFileELF::ParseSectionHeaders()
504 {
505     // We have already parsed the section headers
506     if (!m_section_headers.empty())
507         return m_section_headers.size();
508 
509     // If there are no section headers we are done.
510     if (m_header.e_shnum == 0)
511         return 0;
512 
513     m_section_headers.resize(m_header.e_shnum);
514     if (m_section_headers.size() != m_header.e_shnum)
515         return 0;
516 
517     const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
518     const elf_off sh_offset = m_offset + m_header.e_shoff;
519     DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size));
520 
521     if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size)
522         return 0;
523 
524     DataExtractor data(buffer_sp,
525                        m_data.GetByteOrder(),
526                        m_data.GetAddressByteSize());
527 
528     uint32_t idx;
529     uint32_t offset;
530     for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
531     {
532         if (m_section_headers[idx].Parse(data, &offset) == false)
533             break;
534     }
535     if (idx < m_section_headers.size())
536         m_section_headers.resize(idx);
537 
538     return m_section_headers.size();
539 }
540 
541 size_t
542 ObjectFileELF::GetSectionHeaderStringTable()
543 {
544     if (m_shstr_data.GetByteSize() == 0)
545     {
546         const unsigned strtab_idx = m_header.e_shstrndx;
547 
548         if (strtab_idx && strtab_idx < m_section_headers.size())
549         {
550             const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
551             const size_t byte_size = sheader.sh_size;
552             const Elf64_Off offset = m_offset + sheader.sh_offset;
553             DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size));
554 
555             if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size)
556                 return 0;
557 
558             m_shstr_data.SetData(buffer_sp);
559         }
560     }
561     return m_shstr_data.GetByteSize();
562 }
563 
564 lldb::user_id_t
565 ObjectFileELF::GetSectionIndexByName(const char *name)
566 {
567     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
568         return 0;
569 
570     // Search the collection of section headers for one with a matching name.
571     for (SectionHeaderCollIter I = m_section_headers.begin();
572          I != m_section_headers.end(); ++I)
573     {
574         const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
575 
576         if (!sectionName)
577             return 0;
578 
579         if (strcmp(name, sectionName) != 0)
580             continue;
581 
582         return SectionIndex(I);
583     }
584 
585     return 0;
586 }
587 
588 const elf::ELFSectionHeader *
589 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
590 {
591     if (!ParseSectionHeaders() || !id)
592         return NULL;
593 
594     if (--id < m_section_headers.size())
595         return &m_section_headers[id];
596 
597     return NULL;
598 }
599 
600 SectionList *
601 ObjectFileELF::GetSectionList()
602 {
603     if (m_sections_ap.get())
604         return m_sections_ap.get();
605 
606     if (ParseSectionHeaders() && GetSectionHeaderStringTable())
607     {
608         m_sections_ap.reset(new SectionList());
609 
610         for (SectionHeaderCollIter I = m_section_headers.begin();
611              I != m_section_headers.end(); ++I)
612         {
613             const ELFSectionHeader &header = *I;
614 
615             ConstString name(m_shstr_data.PeekCStr(header.sh_name));
616             uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
617 
618             static ConstString g_sect_name_text (".text");
619             static ConstString g_sect_name_data (".data");
620             static ConstString g_sect_name_bss (".bss");
621             static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
622             static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
623             static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
624             static ConstString g_sect_name_dwarf_debug_info (".debug_info");
625             static ConstString g_sect_name_dwarf_debug_line (".debug_line");
626             static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
627             static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
628             static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
629             static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
630             static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
631             static ConstString g_sect_name_dwarf_debug_str (".debug_str");
632             static ConstString g_sect_name_eh_frame (".eh_frame");
633 
634             SectionType sect_type = eSectionTypeOther;
635 
636             if      (name == g_sect_name_text)                  sect_type = eSectionTypeCode;
637             else if (name == g_sect_name_data)                  sect_type = eSectionTypeData;
638             else if (name == g_sect_name_bss)                   sect_type = eSectionTypeZeroFill;
639             else if (name == g_sect_name_dwarf_debug_abbrev)    sect_type = eSectionTypeDWARFDebugAbbrev;
640             else if (name == g_sect_name_dwarf_debug_aranges)   sect_type = eSectionTypeDWARFDebugAranges;
641             else if (name == g_sect_name_dwarf_debug_frame)     sect_type = eSectionTypeDWARFDebugFrame;
642             else if (name == g_sect_name_dwarf_debug_info)      sect_type = eSectionTypeDWARFDebugInfo;
643             else if (name == g_sect_name_dwarf_debug_line)      sect_type = eSectionTypeDWARFDebugLine;
644             else if (name == g_sect_name_dwarf_debug_loc)       sect_type = eSectionTypeDWARFDebugLoc;
645             else if (name == g_sect_name_dwarf_debug_macinfo)   sect_type = eSectionTypeDWARFDebugMacInfo;
646             else if (name == g_sect_name_dwarf_debug_pubnames)  sect_type = eSectionTypeDWARFDebugPubNames;
647             else if (name == g_sect_name_dwarf_debug_pubtypes)  sect_type = eSectionTypeDWARFDebugPubTypes;
648             else if (name == g_sect_name_dwarf_debug_ranges)    sect_type = eSectionTypeDWARFDebugRanges;
649             else if (name == g_sect_name_dwarf_debug_str)       sect_type = eSectionTypeDWARFDebugStr;
650             else if (name == g_sect_name_eh_frame)              sect_type = eSectionTypeEHFrame;
651 
652 
653             SectionSP section(new Section(
654                 0,                  // Parent section.
655                 GetModule(),        // Module to which this section belongs.
656                 SectionIndex(I),    // Section ID.
657                 name,               // Section name.
658                 sect_type,          // Section type.
659                 header.sh_addr,     // VM address.
660                 header.sh_size,     // VM size in bytes of this section.
661                 header.sh_offset,   // Offset of this section in the file.
662                 size,               // Size of the section as found in the file.
663                 header.sh_flags));  // Flags for this section.
664 
665             m_sections_ap->AddSection(section);
666         }
667     }
668 
669     return m_sections_ap.get();
670 }
671 
672 static unsigned
673 ParseSymbols(Symtab *symtab,
674              user_id_t start_id,
675              SectionList *section_list,
676              const ELFSectionHeader *symtab_shdr,
677              const DataExtractor &symtab_data,
678              const DataExtractor &strtab_data)
679 {
680     ELFSymbol symbol;
681     uint32_t offset = 0;
682     const unsigned num_symbols =
683         symtab_data.GetByteSize() / symtab_shdr->sh_entsize;
684 
685     static ConstString text_section_name(".text");
686     static ConstString init_section_name(".init");
687     static ConstString fini_section_name(".fini");
688     static ConstString ctors_section_name(".ctors");
689     static ConstString dtors_section_name(".dtors");
690 
691     static ConstString data_section_name(".data");
692     static ConstString rodata_section_name(".rodata");
693     static ConstString rodata1_section_name(".rodata1");
694     static ConstString data2_section_name(".data1");
695     static ConstString bss_section_name(".bss");
696 
697     unsigned i;
698     for (i = 0; i < num_symbols; ++i)
699     {
700         if (symbol.Parse(symtab_data, &offset) == false)
701             break;
702 
703         Section *symbol_section = NULL;
704         SymbolType symbol_type = eSymbolTypeInvalid;
705         Elf64_Half symbol_idx = symbol.st_shndx;
706 
707         switch (symbol_idx)
708         {
709         case SHN_ABS:
710             symbol_type = eSymbolTypeAbsolute;
711             break;
712         case SHN_UNDEF:
713             symbol_type = eSymbolTypeUndefined;
714             break;
715         default:
716             symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
717             break;
718         }
719 
720         switch (symbol.getType())
721         {
722         default:
723         case STT_NOTYPE:
724             // The symbol's type is not specified.
725             break;
726 
727         case STT_OBJECT:
728             // The symbol is associated with a data object, such as a variable,
729             // an array, etc.
730             symbol_type = eSymbolTypeData;
731             break;
732 
733         case STT_FUNC:
734             // The symbol is associated with a function or other executable code.
735             symbol_type = eSymbolTypeCode;
736             break;
737 
738         case STT_SECTION:
739             // The symbol is associated with a section. Symbol table entries of
740             // this type exist primarily for relocation and normally have
741             // STB_LOCAL binding.
742             break;
743 
744         case STT_FILE:
745             // Conventionally, the symbol's name gives the name of the source
746             // file associated with the object file. A file symbol has STB_LOCAL
747             // binding, its section index is SHN_ABS, and it precedes the other
748             // STB_LOCAL symbols for the file, if it is present.
749             symbol_type = eSymbolTypeObjectFile;
750             break;
751         }
752 
753         if (symbol_type == eSymbolTypeInvalid)
754         {
755             if (symbol_section)
756             {
757                 const ConstString &sect_name = symbol_section->GetName();
758                 if (sect_name == text_section_name ||
759                     sect_name == init_section_name ||
760                     sect_name == fini_section_name ||
761                     sect_name == ctors_section_name ||
762                     sect_name == dtors_section_name)
763                 {
764                     symbol_type = eSymbolTypeCode;
765                 }
766                 else if (sect_name == data_section_name ||
767                          sect_name == data2_section_name ||
768                          sect_name == rodata_section_name ||
769                          sect_name == rodata1_section_name ||
770                          sect_name == bss_section_name)
771                 {
772                     symbol_type = eSymbolTypeData;
773                 }
774             }
775         }
776 
777         uint64_t symbol_value = symbol.st_value;
778         if (symbol_section != NULL)
779             symbol_value -= symbol_section->GetFileAddress();
780         const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
781         bool is_global = symbol.getBinding() == STB_GLOBAL;
782         uint32_t flags = symbol.st_other << 8 | symbol.st_info;
783 
784         Symbol dc_symbol(
785             i + start_id,    // ID is the original symbol table index.
786             symbol_name,     // Symbol name.
787             false,           // Is the symbol name mangled?
788             symbol_type,     // Type of this symbol
789             is_global,       // Is this globally visible?
790             false,           // Is this symbol debug info?
791             false,           // Is this symbol a trampoline?
792             false,           // Is this symbol artificial?
793             symbol_section,  // Section in which this symbol is defined or null.
794             symbol_value,    // Offset in section or symbol value.
795             symbol.st_size,  // Size in bytes of this symbol.
796             flags);          // Symbol flags.
797         symtab->AddSymbol(dc_symbol);
798     }
799 
800     return i;
801 }
802 
803 unsigned
804 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,
805                                 const ELFSectionHeader *symtab_hdr,
806                                 user_id_t symtab_id)
807 {
808     assert(symtab_hdr->sh_type == SHT_SYMTAB ||
809            symtab_hdr->sh_type == SHT_DYNSYM);
810 
811     // Parse in the section list if needed.
812     SectionList *section_list = GetSectionList();
813     if (!section_list)
814         return 0;
815 
816     // Section ID's are ones based.
817     user_id_t strtab_id = symtab_hdr->sh_link + 1;
818 
819     Section *symtab = section_list->FindSectionByID(symtab_id).get();
820     Section *strtab = section_list->FindSectionByID(strtab_id).get();
821     unsigned num_symbols = 0;
822     if (symtab && strtab)
823     {
824         DataExtractor symtab_data;
825         DataExtractor strtab_data;
826         if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) &&
827             strtab->ReadSectionDataFromObjectFile(this, strtab_data))
828         {
829             num_symbols = ParseSymbols(symbol_table, start_id,
830                                        section_list, symtab_hdr,
831                                        symtab_data, strtab_data);
832         }
833     }
834 
835     return num_symbols;
836 }
837 
838 size_t
839 ObjectFileELF::ParseDynamicSymbols()
840 {
841     if (m_dynamic_symbols.size())
842         return m_dynamic_symbols.size();
843 
844     user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC);
845     if (!dyn_id)
846         return NULL;
847 
848     SectionList *section_list = GetSectionList();
849     if (!section_list)
850         return NULL;
851 
852     Section *dynsym = section_list->FindSectionByID(dyn_id).get();
853     if (!dynsym)
854         return NULL;
855 
856     ELFDynamic symbol;
857     DataExtractor dynsym_data;
858     if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data))
859     {
860 
861         const unsigned section_size = dynsym_data.GetByteSize();
862         unsigned offset = 0;
863         unsigned cursor = 0;
864 
865         while (cursor < section_size)
866         {
867             offset = cursor;
868             if (!symbol.Parse(dynsym_data, &cursor))
869                 break;
870 
871             m_dynamic_symbols.push_back(symbol);
872         }
873     }
874 
875     return m_dynamic_symbols.size();
876 }
877 
878 const ELFDynamic *
879 ObjectFileELF::FindDynamicSymbol(unsigned tag)
880 {
881     if (!ParseDynamicSymbols())
882         return NULL;
883 
884     SectionList *section_list = GetSectionList();
885     if (!section_list)
886         return 0;
887 
888     DynamicSymbolCollIter I = m_dynamic_symbols.begin();
889     DynamicSymbolCollIter E = m_dynamic_symbols.end();
890     for ( ; I != E; ++I)
891     {
892         ELFDynamic *symbol = &*I;
893 
894         if (symbol->d_tag == tag)
895             return symbol;
896     }
897 
898     return NULL;
899 }
900 
901 Section *
902 ObjectFileELF::PLTSection()
903 {
904     const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
905     SectionList *section_list = GetSectionList();
906 
907     if (symbol && section_list)
908     {
909         addr_t addr = symbol->d_ptr;
910         return section_list->FindSectionContainingFileAddress(addr).get();
911     }
912 
913     return NULL;
914 }
915 
916 unsigned
917 ObjectFileELF::PLTRelocationType()
918 {
919     const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
920 
921     if (symbol)
922         return symbol->d_val;
923 
924     return 0;
925 }
926 
927 static unsigned
928 ParsePLTRelocations(Symtab *symbol_table,
929                     user_id_t start_id,
930                     unsigned rel_type,
931                     const ELFHeader *hdr,
932                     const ELFSectionHeader *rel_hdr,
933                     const ELFSectionHeader *plt_hdr,
934                     const ELFSectionHeader *sym_hdr,
935                     Section *plt_section,
936                     DataExtractor &rel_data,
937                     DataExtractor &symtab_data,
938                     DataExtractor &strtab_data)
939 {
940     ELFRelocation rel(rel_type);
941     ELFSymbol symbol;
942     uint32_t offset = 0;
943     const unsigned plt_entsize = plt_hdr->sh_entsize;
944     const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
945 
946     typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
947     reloc_info_fn reloc_type;
948     reloc_info_fn reloc_symbol;
949 
950     if (hdr->Is32Bit() == 4)
951     {
952         reloc_type = ELFRelocation::RelocType32;
953         reloc_symbol = ELFRelocation::RelocSymbol32;
954     }
955     else
956     {
957         reloc_type = ELFRelocation::RelocType64;
958         reloc_symbol = ELFRelocation::RelocSymbol64;
959     }
960 
961     unsigned slot_type = hdr->GetRelocationJumpSlotType();
962     unsigned i;
963     for (i = 0; i < num_relocations; ++i)
964     {
965         if (rel.Parse(rel_data, &offset) == false)
966             break;
967 
968         if (reloc_type(rel) != slot_type)
969             continue;
970 
971         unsigned symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
972         uint64_t plt_index = (i + 1) * plt_entsize;
973 
974         if (!symbol.Parse(symtab_data, &symbol_offset))
975             break;
976 
977         const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
978 
979         Symbol jump_symbol(
980             i + start_id,    // Symbol table index
981             symbol_name,     // symbol name.
982             false,           // is the symbol name mangled?
983             eSymbolTypeTrampoline, // Type of this symbol
984             false,           // Is this globally visible?
985             false,           // Is this symbol debug info?
986             true,            // Is this symbol a trampoline?
987             true,            // Is this symbol artificial?
988             plt_section,     // Section in which this symbol is defined or null.
989             plt_index,       // Offset in section or symbol value.
990             plt_entsize,     // Size in bytes of this symbol.
991             0);              // Symbol flags.
992 
993         symbol_table->AddSymbol(jump_symbol);
994     }
995 
996     return i;
997 }
998 
999 unsigned
1000 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1001                                       user_id_t start_id,
1002                                       const ELFSectionHeader *rel_hdr,
1003                                       user_id_t rel_id)
1004 {
1005     assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1006 
1007     // The link field points to the asscoiated symbol table.  The info field
1008     // points to the section holding the plt.
1009     user_id_t symtab_id = rel_hdr->sh_link;
1010     user_id_t plt_id = rel_hdr->sh_info;
1011 
1012     if (!symtab_id || !plt_id)
1013         return 0;
1014 
1015     // Section ID's are ones based;
1016     symtab_id++;
1017     plt_id++;
1018 
1019     const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id);
1020     if (!plt_hdr)
1021         return 0;
1022 
1023     const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1024     if (!sym_hdr)
1025         return 0;
1026 
1027     SectionList *section_list = GetSectionList();
1028     if (!section_list)
1029         return 0;
1030 
1031     Section *rel_section = section_list->FindSectionByID(rel_id).get();
1032     if (!rel_section)
1033         return 0;
1034 
1035     Section *plt_section = section_list->FindSectionByID(plt_id).get();
1036     if (!plt_section)
1037         return 0;
1038 
1039     Section *symtab = section_list->FindSectionByID(symtab_id).get();
1040     if (!symtab)
1041         return 0;
1042 
1043     Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1044     if (!strtab)
1045         return 0;
1046 
1047     DataExtractor rel_data;
1048     if (!rel_section->ReadSectionDataFromObjectFile(this, rel_data))
1049         return 0;
1050 
1051     DataExtractor symtab_data;
1052     if (!symtab->ReadSectionDataFromObjectFile(this, symtab_data))
1053         return 0;
1054 
1055     DataExtractor strtab_data;
1056     if (!strtab->ReadSectionDataFromObjectFile(this, strtab_data))
1057         return 0;
1058 
1059     unsigned rel_type = PLTRelocationType();
1060     if (!rel_type)
1061         return 0;
1062 
1063     return ParsePLTRelocations(symbol_table, start_id, rel_type,
1064                                &m_header, rel_hdr, plt_hdr, sym_hdr,
1065                                plt_section,
1066                                rel_data, symtab_data, strtab_data);
1067 }
1068 
1069 Symtab *
1070 ObjectFileELF::GetSymtab()
1071 {
1072     if (m_symtab_ap.get())
1073         return m_symtab_ap.get();
1074 
1075     Symtab *symbol_table = new Symtab(this);
1076     m_symtab_ap.reset(symbol_table);
1077 
1078     Mutex::Locker locker(symbol_table->GetMutex());
1079 
1080     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1081         return symbol_table;
1082 
1083     // Locate and parse all linker symbol tables.
1084     uint64_t symbol_id = 0;
1085     for (SectionHeaderCollIter I = m_section_headers.begin();
1086          I != m_section_headers.end(); ++I)
1087     {
1088         if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM)
1089         {
1090             const ELFSectionHeader &symtab_header = *I;
1091             user_id_t section_id = SectionIndex(I);
1092             symbol_id += ParseSymbolTable(symbol_table, symbol_id,
1093                                           &symtab_header, section_id);
1094         }
1095     }
1096 
1097     // Synthesize trampoline symbols to help navigate the PLT.
1098     Section *reloc_section = PLTSection();
1099     if (reloc_section)
1100     {
1101         user_id_t reloc_id = reloc_section->GetID();
1102         const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id);
1103         assert(reloc_header);
1104 
1105         ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id);
1106     }
1107 
1108     return symbol_table;
1109 }
1110 
1111 //===----------------------------------------------------------------------===//
1112 // Dump
1113 //
1114 // Dump the specifics of the runtime file container (such as any headers
1115 // segments, sections, etc).
1116 //----------------------------------------------------------------------
1117 void
1118 ObjectFileELF::Dump(Stream *s)
1119 {
1120     DumpELFHeader(s, m_header);
1121     s->EOL();
1122     DumpELFProgramHeaders(s);
1123     s->EOL();
1124     DumpELFSectionHeaders(s);
1125     s->EOL();
1126     SectionList *section_list = GetSectionList();
1127     if (section_list)
1128         section_list->Dump(s, NULL, true, UINT32_MAX);
1129     Symtab *symtab = GetSymtab();
1130     if (symtab)
1131         symtab->Dump(s, NULL, eSortOrderNone);
1132     s->EOL();
1133     DumpDependentModules(s);
1134     s->EOL();
1135 }
1136 
1137 //----------------------------------------------------------------------
1138 // DumpELFHeader
1139 //
1140 // Dump the ELF header to the specified output stream
1141 //----------------------------------------------------------------------
1142 void
1143 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
1144 {
1145     s->PutCString("ELF Header\n");
1146     s->Printf("e_ident[EI_MAG0   ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1147     s->Printf("e_ident[EI_MAG1   ] = 0x%2.2x '%c'\n",
1148               header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1149     s->Printf("e_ident[EI_MAG2   ] = 0x%2.2x '%c'\n",
1150               header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1151     s->Printf("e_ident[EI_MAG3   ] = 0x%2.2x '%c'\n",
1152               header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
1153 
1154     s->Printf("e_ident[EI_CLASS  ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1155     s->Printf("e_ident[EI_DATA   ] = 0x%2.2x ", header.e_ident[EI_DATA]);
1156     DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1157     s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1158     s->Printf ("e_ident[EI_PAD    ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1159 
1160     s->Printf("e_type      = 0x%4.4x ", header.e_type);
1161     DumpELFHeader_e_type(s, header.e_type);
1162     s->Printf("\ne_machine   = 0x%4.4x\n", header.e_machine);
1163     s->Printf("e_version   = 0x%8.8x\n", header.e_version);
1164     s->Printf("e_entry     = 0x%8.8lx\n", header.e_entry);
1165     s->Printf("e_phoff     = 0x%8.8lx\n", header.e_phoff);
1166     s->Printf("e_shoff     = 0x%8.8lx\n", header.e_shoff);
1167     s->Printf("e_flags     = 0x%8.8x\n", header.e_flags);
1168     s->Printf("e_ehsize    = 0x%4.4x\n", header.e_ehsize);
1169     s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1170     s->Printf("e_phnum     = 0x%4.4x\n", header.e_phnum);
1171     s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1172     s->Printf("e_shnum     = 0x%4.4x\n", header.e_shnum);
1173     s->Printf("e_shstrndx  = 0x%4.4x\n", header.e_shstrndx);
1174 }
1175 
1176 //----------------------------------------------------------------------
1177 // DumpELFHeader_e_type
1178 //
1179 // Dump an token value for the ELF header member e_type
1180 //----------------------------------------------------------------------
1181 void
1182 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
1183 {
1184     switch (e_type)
1185     {
1186     case ET_NONE:   *s << "ET_NONE"; break;
1187     case ET_REL:    *s << "ET_REL"; break;
1188     case ET_EXEC:   *s << "ET_EXEC"; break;
1189     case ET_DYN:    *s << "ET_DYN"; break;
1190     case ET_CORE:   *s << "ET_CORE"; break;
1191     default:
1192         break;
1193     }
1194 }
1195 
1196 //----------------------------------------------------------------------
1197 // DumpELFHeader_e_ident_EI_DATA
1198 //
1199 // Dump an token value for the ELF header member e_ident[EI_DATA]
1200 //----------------------------------------------------------------------
1201 void
1202 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
1203 {
1204     switch (ei_data)
1205     {
1206     case ELFDATANONE:   *s << "ELFDATANONE"; break;
1207     case ELFDATA2LSB:   *s << "ELFDATA2LSB - Little Endian"; break;
1208     case ELFDATA2MSB:   *s << "ELFDATA2MSB - Big Endian"; break;
1209     default:
1210         break;
1211     }
1212 }
1213 
1214 
1215 //----------------------------------------------------------------------
1216 // DumpELFProgramHeader
1217 //
1218 // Dump a single ELF program header to the specified output stream
1219 //----------------------------------------------------------------------
1220 void
1221 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
1222 {
1223     DumpELFProgramHeader_p_type(s, ph.p_type);
1224     s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
1225     s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags);
1226 
1227     DumpELFProgramHeader_p_flags(s, ph.p_flags);
1228     s->Printf(") %8.8x", ph.p_align);
1229 }
1230 
1231 //----------------------------------------------------------------------
1232 // DumpELFProgramHeader_p_type
1233 //
1234 // Dump an token value for the ELF program header member p_type which
1235 // describes the type of the program header
1236 // ----------------------------------------------------------------------
1237 void
1238 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
1239 {
1240     const int kStrWidth = 10;
1241     switch (p_type)
1242     {
1243     CASE_AND_STREAM(s, PT_NULL      , kStrWidth);
1244     CASE_AND_STREAM(s, PT_LOAD      , kStrWidth);
1245     CASE_AND_STREAM(s, PT_DYNAMIC   , kStrWidth);
1246     CASE_AND_STREAM(s, PT_INTERP    , kStrWidth);
1247     CASE_AND_STREAM(s, PT_NOTE      , kStrWidth);
1248     CASE_AND_STREAM(s, PT_SHLIB     , kStrWidth);
1249     CASE_AND_STREAM(s, PT_PHDR      , kStrWidth);
1250     default:
1251         s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1252         break;
1253     }
1254 }
1255 
1256 
1257 //----------------------------------------------------------------------
1258 // DumpELFProgramHeader_p_flags
1259 //
1260 // Dump an token value for the ELF program header member p_flags
1261 //----------------------------------------------------------------------
1262 void
1263 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
1264 {
1265     *s  << ((p_flags & PF_X) ? "PF_X" : "    ")
1266         << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1267         << ((p_flags & PF_W) ? "PF_W" : "    ")
1268         << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1269         << ((p_flags & PF_R) ? "PF_R" : "    ");
1270 }
1271 
1272 //----------------------------------------------------------------------
1273 // DumpELFProgramHeaders
1274 //
1275 // Dump all of the ELF program header to the specified output stream
1276 //----------------------------------------------------------------------
1277 void
1278 ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1279 {
1280     if (ParseProgramHeaders())
1281     {
1282         s->PutCString("Program Headers\n");
1283         s->PutCString("IDX  p_type     p_offset p_vaddr  p_paddr  "
1284                       "p_filesz p_memsz  p_flags                   p_align\n");
1285         s->PutCString("==== ---------- -------- -------- -------- "
1286                       "-------- -------- ------------------------- --------\n");
1287 
1288         uint32_t idx = 0;
1289         for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1290              I != m_program_headers.end(); ++I, ++idx)
1291         {
1292             s->Printf("[%2u] ", idx);
1293             ObjectFileELF::DumpELFProgramHeader(s, *I);
1294             s->EOL();
1295         }
1296     }
1297 }
1298 
1299 //----------------------------------------------------------------------
1300 // DumpELFSectionHeader
1301 //
1302 // Dump a single ELF section header to the specified output stream
1303 //----------------------------------------------------------------------
1304 void
1305 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
1306 {
1307     s->Printf("%8.8x ", sh.sh_name);
1308     DumpELFSectionHeader_sh_type(s, sh.sh_type);
1309     s->Printf(" %8.8lx (", sh.sh_flags);
1310     DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
1311     s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size);
1312     s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
1313     s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize);
1314 }
1315 
1316 //----------------------------------------------------------------------
1317 // DumpELFSectionHeader_sh_type
1318 //
1319 // Dump an token value for the ELF section header member sh_type which
1320 // describes the type of the section
1321 //----------------------------------------------------------------------
1322 void
1323 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
1324 {
1325     const int kStrWidth = 12;
1326     switch (sh_type)
1327     {
1328     CASE_AND_STREAM(s, SHT_NULL     , kStrWidth);
1329     CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1330     CASE_AND_STREAM(s, SHT_SYMTAB   , kStrWidth);
1331     CASE_AND_STREAM(s, SHT_STRTAB   , kStrWidth);
1332     CASE_AND_STREAM(s, SHT_RELA     , kStrWidth);
1333     CASE_AND_STREAM(s, SHT_HASH     , kStrWidth);
1334     CASE_AND_STREAM(s, SHT_DYNAMIC  , kStrWidth);
1335     CASE_AND_STREAM(s, SHT_NOTE     , kStrWidth);
1336     CASE_AND_STREAM(s, SHT_NOBITS   , kStrWidth);
1337     CASE_AND_STREAM(s, SHT_REL      , kStrWidth);
1338     CASE_AND_STREAM(s, SHT_SHLIB    , kStrWidth);
1339     CASE_AND_STREAM(s, SHT_DYNSYM   , kStrWidth);
1340     CASE_AND_STREAM(s, SHT_LOPROC   , kStrWidth);
1341     CASE_AND_STREAM(s, SHT_HIPROC   , kStrWidth);
1342     CASE_AND_STREAM(s, SHT_LOUSER   , kStrWidth);
1343     CASE_AND_STREAM(s, SHT_HIUSER   , kStrWidth);
1344     default:
1345         s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1346         break;
1347     }
1348 }
1349 
1350 //----------------------------------------------------------------------
1351 // DumpELFSectionHeader_sh_flags
1352 //
1353 // Dump an token value for the ELF section header member sh_flags
1354 //----------------------------------------------------------------------
1355 void
1356 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
1357 {
1358     *s  << ((sh_flags & SHF_WRITE) ? "WRITE" : "     ")
1359         << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1360         << ((sh_flags & SHF_ALLOC) ? "ALLOC" : "     ")
1361         << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1362         << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : "         ");
1363 }
1364 
1365 //----------------------------------------------------------------------
1366 // DumpELFSectionHeaders
1367 //
1368 // Dump all of the ELF section header to the specified output stream
1369 //----------------------------------------------------------------------
1370 void
1371 ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1372 {
1373     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1374         return;
1375 
1376     s->PutCString("Section Headers\n");
1377     s->PutCString("IDX  name     type         flags                            "
1378                   "addr     offset   size     link     info     addralgn "
1379                   "entsize  Name\n");
1380     s->PutCString("==== -------- ------------ -------------------------------- "
1381                   "-------- -------- -------- -------- -------- -------- "
1382                   "-------- ====================\n");
1383 
1384     uint32_t idx = 0;
1385     for (SectionHeaderCollConstIter I = m_section_headers.begin();
1386          I != m_section_headers.end(); ++I, ++idx)
1387     {
1388         s->Printf("[%2u] ", idx);
1389         ObjectFileELF::DumpELFSectionHeader(s, *I);
1390         const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1391         if (section_name)
1392             *s << ' ' << section_name << "\n";
1393     }
1394 }
1395 
1396 void
1397 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1398 {
1399     size_t num_modules = ParseDependentModules();
1400 
1401     if (num_modules > 0)
1402     {
1403         s->PutCString("Dependent Modules:\n");
1404         for (unsigned i = 0; i < num_modules; ++i)
1405         {
1406             const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1407             s->Printf("   %s\n", spec.GetFilename().GetCString());
1408         }
1409     }
1410 }
1411 
1412 bool
1413 ObjectFileELF::GetArchitecture (ArchSpec &arch)
1414 {
1415     if (!ParseHeader())
1416         return false;
1417 
1418     arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
1419     arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1420     arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
1421     return true;
1422 }
1423 
1424