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/PluginManager.h"
20 #include "lldb/Core/Section.h"
21 #include "lldb/Core/Stream.h"
22 #include "lldb/Host/Host.h"
23 
24 #define CASE_AND_STREAM(s, def, width)                  \
25     case def: s->Printf("%-*s", width, #def); break;
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 using namespace elf;
30 using namespace llvm::ELF;
31 
32 //------------------------------------------------------------------
33 // Static methods.
34 //------------------------------------------------------------------
35 void
36 ObjectFileELF::Initialize()
37 {
38     PluginManager::RegisterPlugin(GetPluginNameStatic(),
39                                   GetPluginDescriptionStatic(),
40                                   CreateInstance);
41 }
42 
43 void
44 ObjectFileELF::Terminate()
45 {
46     PluginManager::UnregisterPlugin(CreateInstance);
47 }
48 
49 const char *
50 ObjectFileELF::GetPluginNameStatic()
51 {
52     return "object-file.elf";
53 }
54 
55 const char *
56 ObjectFileELF::GetPluginDescriptionStatic()
57 {
58     return "ELF object file reader.";
59 }
60 
61 ObjectFile *
62 ObjectFileELF::CreateInstance(Module *module,
63                               DataBufferSP &data_sp,
64                               const FileSpec *file, addr_t offset,
65                               addr_t length)
66 {
67     if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset))
68     {
69         const uint8_t *magic = data_sp->GetBytes() + offset;
70         if (ELFHeader::MagicBytesMatch(magic))
71         {
72             unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
73             if (address_size == 4 || address_size == 8)
74             {
75                 std::auto_ptr<ObjectFileELF> objfile_ap(
76                     new ObjectFileELF(module, data_sp, file, offset, length));
77                 ArchSpec spec;
78                 if (objfile_ap->GetArchitecture(spec) &&
79                     objfile_ap->SetModulesArchitecture(spec))
80                     return objfile_ap.release();
81             }
82         }
83     }
84     return NULL;
85 }
86 
87 
88 //------------------------------------------------------------------
89 // PluginInterface protocol
90 //------------------------------------------------------------------
91 const char *
92 ObjectFileELF::GetPluginName()
93 {
94     return "ObjectFileELF";
95 }
96 
97 const char *
98 ObjectFileELF::GetShortPluginName()
99 {
100     return GetPluginNameStatic();
101 }
102 
103 uint32_t
104 ObjectFileELF::GetPluginVersion()
105 {
106     return m_plugin_version;
107 }
108 
109 void
110 ObjectFileELF::GetPluginCommandHelp(const char *command, Stream *strm)
111 {
112 }
113 
114 Error
115 ObjectFileELF::ExecutePluginCommand(Args &command, Stream *strm)
116 {
117     Error error;
118     error.SetErrorString("No plug-in commands are currently supported.");
119     return error;
120 }
121 
122 Log *
123 ObjectFileELF::EnablePluginLogging(Stream *strm, Args &command)
124 {
125     return NULL;
126 }
127 
128 //------------------------------------------------------------------
129 // ObjectFile protocol
130 //------------------------------------------------------------------
131 
132 ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP,
133                              const FileSpec* file, addr_t offset,
134                              addr_t length)
135     : ObjectFile(module, file, offset, length, dataSP),
136       m_header(),
137       m_program_headers(),
138       m_section_headers(),
139       m_sections_ap(),
140       m_symtab_ap(),
141       m_filespec_ap(),
142       m_shstr_data()
143 {
144     if (file)
145         m_file = *file;
146     ::memset(&m_header, 0, sizeof(m_header));
147 }
148 
149 ObjectFileELF::~ObjectFileELF()
150 {
151 }
152 
153 bool
154 ObjectFileELF::IsExecutable() const
155 {
156     return m_header.e_entry != 0;
157 }
158 
159 Address
160 ObjectFileELF::GetEntryPoint() const
161 {
162     if (m_header.e_entry)
163         return Address(NULL, m_header.e_entry);
164     else
165         return Address();
166 }
167 
168 ByteOrder
169 ObjectFileELF::GetByteOrder() const
170 {
171     if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
172         return eByteOrderBig;
173     if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
174         return eByteOrderLittle;
175     return eByteOrderInvalid;
176 }
177 
178 size_t
179 ObjectFileELF::GetAddressByteSize() const
180 {
181     return m_data.GetAddressByteSize();
182 }
183 
184 unsigned
185 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
186 {
187     return std::distance(m_section_headers.begin(), I) + 1;
188 }
189 
190 unsigned
191 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
192 {
193     return std::distance(m_section_headers.begin(), I) + 1;
194 }
195 
196 bool
197 ObjectFileELF::ParseHeader()
198 {
199     uint32_t offset = GetOffset();
200     return m_header.Parse(m_data, &offset);
201 }
202 
203 bool
204 ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
205 {
206     // FIXME: Return MD5 sum here.  See comment in ObjectFile.h.
207     return false;
208 }
209 
210 uint32_t
211 ObjectFileELF::GetDependentModules(FileSpecList &files)
212 {
213     size_t num_modules = ParseDependentModules();
214     uint32_t num_specs = 0;
215 
216     for (unsigned i = 0; i < num_modules; ++i)
217     {
218         if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
219             num_specs++;
220     }
221 
222     return num_specs;
223 }
224 
225 Address
226 ObjectFileELF::GetImageInfoAddress()
227 {
228     if (!ParseSectionHeaders())
229         return Address();
230 
231     user_id_t dynsym_id = 0;
232     for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
233          sh_pos != m_section_headers.end(); ++sh_pos)
234     {
235         if (sh_pos->sh_type == SHT_DYNAMIC)
236         {
237             dynsym_id = SectionIndex(sh_pos);
238             break;
239         }
240     }
241 
242     if (!dynsym_id)
243         return Address();
244 
245     SectionList *section_list = GetSectionList();
246     if (!section_list)
247         return Address();
248 
249     // Resolve the dynamic table entries.
250     Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
251     if (!dynsym)
252         return Address();
253 
254     DataExtractor dynsym_data;
255     if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data))
256     {
257         ELFDynamic symbol;
258         const unsigned section_size = dynsym_data.GetByteSize();
259         unsigned offset = 0;
260         unsigned cursor = 0;
261 
262         // Look for a DT_DEBUG entry.
263         while (cursor < section_size)
264         {
265             offset = cursor;
266             if (!symbol.Parse(dynsym_data, &cursor))
267                 break;
268 
269             if (symbol.d_tag != DT_DEBUG)
270                 continue;
271 
272             return Address(dynsym, offset + sizeof(symbol.d_tag));
273         }
274     }
275 
276     return Address();
277 }
278 
279 //----------------------------------------------------------------------
280 // ParseDependentModules
281 //----------------------------------------------------------------------
282 size_t
283 ObjectFileELF::ParseDependentModules()
284 {
285     if (m_filespec_ap.get())
286         return m_filespec_ap->GetSize();
287 
288     m_filespec_ap.reset(new FileSpecList());
289 
290     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
291         return 0;
292 
293     // Locate the dynamic table.
294     user_id_t dynsym_id = 0;
295     user_id_t dynstr_id = 0;
296     for (SectionHeaderCollIter sh_pos = m_section_headers.begin();
297          sh_pos != m_section_headers.end(); ++sh_pos)
298     {
299         if (sh_pos->sh_type == SHT_DYNAMIC)
300         {
301             dynsym_id = SectionIndex(sh_pos);
302             dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based.
303             break;
304         }
305     }
306 
307     if (!(dynsym_id && dynstr_id))
308         return 0;
309 
310     SectionList *section_list = GetSectionList();
311     if (!section_list)
312         return 0;
313 
314     // Resolve and load the dynamic table entries and corresponding string
315     // table.
316     Section *dynsym = section_list->FindSectionByID(dynsym_id).get();
317     Section *dynstr = section_list->FindSectionByID(dynstr_id).get();
318     if (!(dynsym && dynstr))
319         return 0;
320 
321     DataExtractor dynsym_data;
322     DataExtractor dynstr_data;
323     if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) &&
324         dynstr->ReadSectionDataFromObjectFile(this, dynstr_data))
325     {
326         ELFDynamic symbol;
327         const unsigned section_size = dynsym_data.GetByteSize();
328         unsigned offset = 0;
329 
330         // The only type of entries we are concerned with are tagged DT_NEEDED,
331         // yielding the name of a required library.
332         while (offset < section_size)
333         {
334             if (!symbol.Parse(dynsym_data, &offset))
335                 break;
336 
337             if (symbol.d_tag != DT_NEEDED)
338                 continue;
339 
340             uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
341             const char *lib_name = dynstr_data.PeekCStr(str_index);
342             m_filespec_ap->Append(FileSpec(lib_name, true));
343         }
344     }
345 
346     return m_filespec_ap->GetSize();
347 }
348 
349 //----------------------------------------------------------------------
350 // ParseProgramHeaders
351 //----------------------------------------------------------------------
352 size_t
353 ObjectFileELF::ParseProgramHeaders()
354 {
355     // We have already parsed the program headers
356     if (!m_program_headers.empty())
357         return m_program_headers.size();
358 
359     // If there are no program headers to read we are done.
360     if (m_header.e_phnum == 0)
361         return 0;
362 
363     m_program_headers.resize(m_header.e_phnum);
364     if (m_program_headers.size() != m_header.e_phnum)
365         return 0;
366 
367     const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
368     const elf_off ph_offset = m_offset + m_header.e_phoff;
369     DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size));
370 
371     if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size)
372         return 0;
373 
374     DataExtractor data(buffer_sp, m_data.GetByteOrder(),
375                        m_data.GetAddressByteSize());
376 
377     uint32_t idx;
378     uint32_t offset;
379     for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
380     {
381         if (m_program_headers[idx].Parse(data, &offset) == false)
382             break;
383     }
384 
385     if (idx < m_program_headers.size())
386         m_program_headers.resize(idx);
387 
388     return m_program_headers.size();
389 }
390 
391 //----------------------------------------------------------------------
392 // ParseSectionHeaders
393 //----------------------------------------------------------------------
394 size_t
395 ObjectFileELF::ParseSectionHeaders()
396 {
397     // We have already parsed the section headers
398     if (!m_section_headers.empty())
399         return m_section_headers.size();
400 
401     // If there are no section headers we are done.
402     if (m_header.e_shnum == 0)
403         return 0;
404 
405     m_section_headers.resize(m_header.e_shnum);
406     if (m_section_headers.size() != m_header.e_shnum)
407         return 0;
408 
409     const size_t sh_size = m_header.e_shnum * m_header.e_shentsize;
410     const elf_off sh_offset = m_offset + m_header.e_shoff;
411     DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size));
412 
413     if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size)
414         return 0;
415 
416     DataExtractor data(buffer_sp,
417                        m_data.GetByteOrder(),
418                        m_data.GetAddressByteSize());
419 
420     uint32_t idx;
421     uint32_t offset;
422     for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx)
423     {
424         if (m_section_headers[idx].Parse(data, &offset) == false)
425             break;
426     }
427     if (idx < m_section_headers.size())
428         m_section_headers.resize(idx);
429 
430     return m_section_headers.size();
431 }
432 
433 size_t
434 ObjectFileELF::GetSectionHeaderStringTable()
435 {
436     if (m_shstr_data.GetByteSize() == 0)
437     {
438         const unsigned strtab_idx = m_header.e_shstrndx;
439 
440         if (strtab_idx && strtab_idx < m_section_headers.size())
441         {
442             const ELFSectionHeader &sheader = m_section_headers[strtab_idx];
443             const size_t byte_size = sheader.sh_size;
444             const Elf64_Off offset = m_offset + sheader.sh_offset;
445             DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size));
446 
447             if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size)
448                 return 0;
449 
450             m_shstr_data.SetData(buffer_sp);
451         }
452     }
453     return m_shstr_data.GetByteSize();
454 }
455 
456 lldb::user_id_t
457 ObjectFileELF::GetSectionIndexByName(const char *name)
458 {
459     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
460         return 0;
461 
462     // Search the collection of section headers for one with a matching name.
463     for (SectionHeaderCollIter I = m_section_headers.begin();
464          I != m_section_headers.end(); ++I)
465     {
466         const char *sectionName = m_shstr_data.PeekCStr(I->sh_name);
467 
468         if (!sectionName)
469             return 0;
470 
471         if (strcmp(name, sectionName) != 0)
472             continue;
473 
474         return SectionIndex(I);
475     }
476 
477     return 0;
478 }
479 
480 SectionList *
481 ObjectFileELF::GetSectionList()
482 {
483     if (m_sections_ap.get())
484         return m_sections_ap.get();
485 
486     if (ParseSectionHeaders() && GetSectionHeaderStringTable())
487     {
488         m_sections_ap.reset(new SectionList());
489 
490         for (SectionHeaderCollIter I = m_section_headers.begin();
491              I != m_section_headers.end(); ++I)
492         {
493             const ELFSectionHeader &header = *I;
494 
495             ConstString name(m_shstr_data.PeekCStr(header.sh_name));
496             uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
497 
498             static ConstString g_sect_name_text (".text");
499             static ConstString g_sect_name_data (".data");
500             static ConstString g_sect_name_bss (".bss");
501             static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
502             static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
503             static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
504             static ConstString g_sect_name_dwarf_debug_info (".debug_info");
505             static ConstString g_sect_name_dwarf_debug_line (".debug_line");
506             static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
507             static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
508             static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
509             static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
510             static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
511             static ConstString g_sect_name_dwarf_debug_str (".debug_str");
512             static ConstString g_sect_name_eh_frame (".eh_frame");
513 
514             SectionType sect_type = eSectionTypeOther;
515 
516             if      (name == g_sect_name_text)                  sect_type = eSectionTypeCode;
517             else if (name == g_sect_name_data)                  sect_type = eSectionTypeData;
518             else if (name == g_sect_name_bss)                   sect_type = eSectionTypeZeroFill;
519             else if (name == g_sect_name_dwarf_debug_abbrev)    sect_type = eSectionTypeDWARFDebugAbbrev;
520             else if (name == g_sect_name_dwarf_debug_aranges)   sect_type = eSectionTypeDWARFDebugAranges;
521             else if (name == g_sect_name_dwarf_debug_frame)     sect_type = eSectionTypeDWARFDebugFrame;
522             else if (name == g_sect_name_dwarf_debug_info)      sect_type = eSectionTypeDWARFDebugInfo;
523             else if (name == g_sect_name_dwarf_debug_line)      sect_type = eSectionTypeDWARFDebugLine;
524             else if (name == g_sect_name_dwarf_debug_loc)       sect_type = eSectionTypeDWARFDebugLoc;
525             else if (name == g_sect_name_dwarf_debug_macinfo)   sect_type = eSectionTypeDWARFDebugMacInfo;
526             else if (name == g_sect_name_dwarf_debug_pubnames)  sect_type = eSectionTypeDWARFDebugPubNames;
527             else if (name == g_sect_name_dwarf_debug_pubtypes)  sect_type = eSectionTypeDWARFDebugPubTypes;
528             else if (name == g_sect_name_dwarf_debug_ranges)    sect_type = eSectionTypeDWARFDebugRanges;
529             else if (name == g_sect_name_dwarf_debug_str)       sect_type = eSectionTypeDWARFDebugStr;
530             else if (name == g_sect_name_eh_frame)              sect_type = eSectionTypeEHFrame;
531 
532 
533             SectionSP section(new Section(
534                 0,                  // Parent section.
535                 GetModule(),        // Module to which this section belongs.
536                 SectionIndex(I),    // Section ID.
537                 name,               // Section name.
538                 sect_type,          // Section type.
539                 header.sh_addr,     // VM address.
540                 header.sh_size,     // VM size in bytes of this section.
541                 header.sh_offset,   // Offset of this section in the file.
542                 size,               // Size of the section as found in the file.
543                 header.sh_flags));  // Flags for this section.
544 
545             m_sections_ap->AddSection(section);
546         }
547     }
548 
549     return m_sections_ap.get();
550 }
551 
552 static void
553 ParseSymbols(Symtab *symtab, SectionList *section_list,
554              const ELFSectionHeader &symtab_shdr,
555              const DataExtractor &symtab_data,
556              const DataExtractor &strtab_data)
557 {
558     ELFSymbol symbol;
559     uint32_t offset = 0;
560     const unsigned numSymbols =
561         symtab_data.GetByteSize() / symtab_shdr.sh_entsize;
562 
563     static ConstString text_section_name(".text");
564     static ConstString init_section_name(".init");
565     static ConstString fini_section_name(".fini");
566     static ConstString ctors_section_name(".ctors");
567     static ConstString dtors_section_name(".dtors");
568 
569     static ConstString data_section_name(".data");
570     static ConstString rodata_section_name(".rodata");
571     static ConstString rodata1_section_name(".rodata1");
572     static ConstString data2_section_name(".data1");
573     static ConstString bss_section_name(".bss");
574 
575     for (unsigned i = 0; i < numSymbols; ++i)
576     {
577         if (symbol.Parse(symtab_data, &offset) == false)
578             break;
579 
580         Section *symbol_section = NULL;
581         SymbolType symbol_type = eSymbolTypeInvalid;
582         Elf64_Half symbol_idx = symbol.st_shndx;
583 
584         switch (symbol_idx)
585         {
586         case SHN_ABS:
587             symbol_type = eSymbolTypeAbsolute;
588             break;
589         case SHN_UNDEF:
590             symbol_type = eSymbolTypeUndefined;
591             break;
592         default:
593             symbol_section = section_list->GetSectionAtIndex(symbol_idx).get();
594             break;
595         }
596 
597         switch (symbol.getType())
598         {
599         default:
600         case STT_NOTYPE:
601             // The symbol's type is not specified.
602             break;
603 
604         case STT_OBJECT:
605             // The symbol is associated with a data object, such as a variable,
606             // an array, etc.
607             symbol_type = eSymbolTypeData;
608             break;
609 
610         case STT_FUNC:
611             // The symbol is associated with a function or other executable code.
612             symbol_type = eSymbolTypeCode;
613             break;
614 
615         case STT_SECTION:
616             // The symbol is associated with a section. Symbol table entries of
617             // this type exist primarily for relocation and normally have
618             // STB_LOCAL binding.
619             break;
620 
621         case STT_FILE:
622             // Conventionally, the symbol's name gives the name of the source
623             // file associated with the object file. A file symbol has STB_LOCAL
624             // binding, its section index is SHN_ABS, and it precedes the other
625             // STB_LOCAL symbols for the file, if it is present.
626             symbol_type = eSymbolTypeObjectFile;
627             break;
628         }
629 
630         if (symbol_type == eSymbolTypeInvalid)
631         {
632             if (symbol_section)
633             {
634                 const ConstString &sect_name = symbol_section->GetName();
635                 if (sect_name == text_section_name ||
636                     sect_name == init_section_name ||
637                     sect_name == fini_section_name ||
638                     sect_name == ctors_section_name ||
639                     sect_name == dtors_section_name)
640                 {
641                     symbol_type = eSymbolTypeCode;
642                 }
643                 else if (sect_name == data_section_name ||
644                          sect_name == data2_section_name ||
645                          sect_name == rodata_section_name ||
646                          sect_name == rodata1_section_name ||
647                          sect_name == bss_section_name)
648                 {
649                     symbol_type = eSymbolTypeData;
650                 }
651             }
652         }
653 
654         uint64_t symbol_value = symbol.st_value;
655         if (symbol_section != NULL)
656             symbol_value -= symbol_section->GetFileAddress();
657         const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
658         bool is_global = symbol.getBinding() == STB_GLOBAL;
659         uint32_t flags = symbol.st_other << 8 | symbol.st_info;
660 
661         Symbol dc_symbol(
662             i,               // ID is the original symbol table index.
663             symbol_name,     // Symbol name.
664             false,           // Is the symbol name mangled?
665             symbol_type,     // Type of this symbol
666             is_global,       // Is this globally visible?
667             false,           // Is this symbol debug info?
668             false,           // Is this symbol a trampoline?
669             false,           // Is this symbol artificial?
670             symbol_section,  // Section in which this symbol is defined or null.
671             symbol_value,    // Offset in section or symbol value.
672             symbol.st_size,  // Size in bytes of this symbol.
673             flags);          // Symbol flags.
674         symtab->AddSymbol(dc_symbol);
675     }
676 }
677 
678 void
679 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table,
680                                 const ELFSectionHeader &symtab_hdr,
681                                 user_id_t symtab_id)
682 {
683     assert(symtab_hdr.sh_type == SHT_SYMTAB ||
684            symtab_hdr.sh_type == SHT_DYNSYM);
685 
686     // Parse in the section list if needed.
687     SectionList *section_list = GetSectionList();
688     if (!section_list)
689         return;
690 
691     // Section ID's are ones based.
692     user_id_t strtab_id = symtab_hdr.sh_link + 1;
693 
694     Section *symtab = section_list->FindSectionByID(symtab_id).get();
695     Section *strtab = section_list->FindSectionByID(strtab_id).get();
696     if (symtab && strtab)
697     {
698         DataExtractor symtab_data;
699         DataExtractor strtab_data;
700         if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) &&
701             strtab->ReadSectionDataFromObjectFile(this, strtab_data))
702         {
703             ParseSymbols(symbol_table, section_list, symtab_hdr,
704                          symtab_data, strtab_data);
705         }
706     }
707 }
708 
709 Symtab *
710 ObjectFileELF::GetSymtab()
711 {
712     if (m_symtab_ap.get())
713         return m_symtab_ap.get();
714 
715     Symtab *symbol_table = new Symtab(this);
716     m_symtab_ap.reset(symbol_table);
717 
718     Mutex::Locker locker (symbol_table->GetMutex ());
719 
720     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
721         return symbol_table;
722 
723     // Locate and parse all linker symbol tables.
724     for (SectionHeaderCollIter I = m_section_headers.begin();
725          I != m_section_headers.end(); ++I)
726     {
727         if (I->sh_type == SHT_SYMTAB)
728         {
729             const ELFSectionHeader &symtab_section = *I;
730             user_id_t section_id = SectionIndex(I);
731             ParseSymbolTable (symbol_table, symtab_section, section_id);
732         }
733     }
734 
735     return symbol_table;
736 }
737 
738 //===----------------------------------------------------------------------===//
739 // Dump
740 //
741 // Dump the specifics of the runtime file container (such as any headers
742 // segments, sections, etc).
743 //----------------------------------------------------------------------
744 void
745 ObjectFileELF::Dump(Stream *s)
746 {
747     DumpELFHeader(s, m_header);
748     s->EOL();
749     DumpELFProgramHeaders(s);
750     s->EOL();
751     DumpELFSectionHeaders(s);
752     s->EOL();
753     SectionList *section_list = GetSectionList();
754     if (section_list)
755         section_list->Dump(s, NULL, true, UINT32_MAX);
756     Symtab *symtab = GetSymtab();
757     if (symtab)
758         symtab->Dump(s, NULL, lldb::eSortOrderNone);
759     s->EOL();
760     DumpDependentModules(s);
761     s->EOL();
762 }
763 
764 //----------------------------------------------------------------------
765 // DumpELFHeader
766 //
767 // Dump the ELF header to the specified output stream
768 //----------------------------------------------------------------------
769 void
770 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
771 {
772     s->PutCString("ELF Header\n");
773     s->Printf("e_ident[EI_MAG0   ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
774     s->Printf("e_ident[EI_MAG1   ] = 0x%2.2x '%c'\n",
775               header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
776     s->Printf("e_ident[EI_MAG2   ] = 0x%2.2x '%c'\n",
777               header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
778     s->Printf("e_ident[EI_MAG3   ] = 0x%2.2x '%c'\n",
779               header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
780 
781     s->Printf("e_ident[EI_CLASS  ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
782     s->Printf("e_ident[EI_DATA   ] = 0x%2.2x ", header.e_ident[EI_DATA]);
783     DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
784     s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
785     s->Printf ("e_ident[EI_PAD    ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
786 
787     s->Printf("e_type      = 0x%4.4x ", header.e_type);
788     DumpELFHeader_e_type(s, header.e_type);
789     s->Printf("\ne_machine   = 0x%4.4x\n", header.e_machine);
790     s->Printf("e_version   = 0x%8.8x\n", header.e_version);
791     s->Printf("e_entry     = 0x%8.8lx\n", header.e_entry);
792     s->Printf("e_phoff     = 0x%8.8lx\n", header.e_phoff);
793     s->Printf("e_shoff     = 0x%8.8lx\n", header.e_shoff);
794     s->Printf("e_flags     = 0x%8.8x\n", header.e_flags);
795     s->Printf("e_ehsize    = 0x%4.4x\n", header.e_ehsize);
796     s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
797     s->Printf("e_phnum     = 0x%4.4x\n", header.e_phnum);
798     s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
799     s->Printf("e_shnum     = 0x%4.4x\n", header.e_shnum);
800     s->Printf("e_shstrndx  = 0x%4.4x\n", header.e_shstrndx);
801 }
802 
803 //----------------------------------------------------------------------
804 // DumpELFHeader_e_type
805 //
806 // Dump an token value for the ELF header member e_type
807 //----------------------------------------------------------------------
808 void
809 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
810 {
811     switch (e_type)
812     {
813     case ET_NONE:   *s << "ET_NONE"; break;
814     case ET_REL:    *s << "ET_REL"; break;
815     case ET_EXEC:   *s << "ET_EXEC"; break;
816     case ET_DYN:    *s << "ET_DYN"; break;
817     case ET_CORE:   *s << "ET_CORE"; break;
818     default:
819         break;
820     }
821 }
822 
823 //----------------------------------------------------------------------
824 // DumpELFHeader_e_ident_EI_DATA
825 //
826 // Dump an token value for the ELF header member e_ident[EI_DATA]
827 //----------------------------------------------------------------------
828 void
829 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
830 {
831     switch (ei_data)
832     {
833     case ELFDATANONE:   *s << "ELFDATANONE"; break;
834     case ELFDATA2LSB:   *s << "ELFDATA2LSB - Little Endian"; break;
835     case ELFDATA2MSB:   *s << "ELFDATA2MSB - Big Endian"; break;
836     default:
837         break;
838     }
839 }
840 
841 
842 //----------------------------------------------------------------------
843 // DumpELFProgramHeader
844 //
845 // Dump a single ELF program header to the specified output stream
846 //----------------------------------------------------------------------
847 void
848 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
849 {
850     DumpELFProgramHeader_p_type(s, ph.p_type);
851     s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr);
852     s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags);
853 
854     DumpELFProgramHeader_p_flags(s, ph.p_flags);
855     s->Printf(") %8.8x", ph.p_align);
856 }
857 
858 //----------------------------------------------------------------------
859 // DumpELFProgramHeader_p_type
860 //
861 // Dump an token value for the ELF program header member p_type which
862 // describes the type of the program header
863 // ----------------------------------------------------------------------
864 void
865 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
866 {
867     const int kStrWidth = 10;
868     switch (p_type)
869     {
870     CASE_AND_STREAM(s, PT_NULL      , kStrWidth);
871     CASE_AND_STREAM(s, PT_LOAD      , kStrWidth);
872     CASE_AND_STREAM(s, PT_DYNAMIC   , kStrWidth);
873     CASE_AND_STREAM(s, PT_INTERP    , kStrWidth);
874     CASE_AND_STREAM(s, PT_NOTE      , kStrWidth);
875     CASE_AND_STREAM(s, PT_SHLIB     , kStrWidth);
876     CASE_AND_STREAM(s, PT_PHDR      , kStrWidth);
877     default:
878         s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
879         break;
880     }
881 }
882 
883 
884 //----------------------------------------------------------------------
885 // DumpELFProgramHeader_p_flags
886 //
887 // Dump an token value for the ELF program header member p_flags
888 //----------------------------------------------------------------------
889 void
890 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
891 {
892     *s  << ((p_flags & PF_X) ? "PF_X" : "    ")
893         << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
894         << ((p_flags & PF_W) ? "PF_W" : "    ")
895         << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
896         << ((p_flags & PF_R) ? "PF_R" : "    ");
897 }
898 
899 //----------------------------------------------------------------------
900 // DumpELFProgramHeaders
901 //
902 // Dump all of the ELF program header to the specified output stream
903 //----------------------------------------------------------------------
904 void
905 ObjectFileELF::DumpELFProgramHeaders(Stream *s)
906 {
907     if (ParseProgramHeaders())
908     {
909         s->PutCString("Program Headers\n");
910         s->PutCString("IDX  p_type     p_offset p_vaddr  p_paddr  "
911                       "p_filesz p_memsz  p_flags                   p_align\n");
912         s->PutCString("==== ---------- -------- -------- -------- "
913                       "-------- -------- ------------------------- --------\n");
914 
915         uint32_t idx = 0;
916         for (ProgramHeaderCollConstIter I = m_program_headers.begin();
917              I != m_program_headers.end(); ++I, ++idx)
918         {
919             s->Printf("[%2u] ", idx);
920             ObjectFileELF::DumpELFProgramHeader(s, *I);
921             s->EOL();
922         }
923     }
924 }
925 
926 //----------------------------------------------------------------------
927 // DumpELFSectionHeader
928 //
929 // Dump a single ELF section header to the specified output stream
930 //----------------------------------------------------------------------
931 void
932 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh)
933 {
934     s->Printf("%8.8x ", sh.sh_name);
935     DumpELFSectionHeader_sh_type(s, sh.sh_type);
936     s->Printf(" %8.8lx (", sh.sh_flags);
937     DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
938     s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size);
939     s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
940     s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize);
941 }
942 
943 //----------------------------------------------------------------------
944 // DumpELFSectionHeader_sh_type
945 //
946 // Dump an token value for the ELF section header member sh_type which
947 // describes the type of the section
948 //----------------------------------------------------------------------
949 void
950 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
951 {
952     const int kStrWidth = 12;
953     switch (sh_type)
954     {
955     CASE_AND_STREAM(s, SHT_NULL     , kStrWidth);
956     CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
957     CASE_AND_STREAM(s, SHT_SYMTAB   , kStrWidth);
958     CASE_AND_STREAM(s, SHT_STRTAB   , kStrWidth);
959     CASE_AND_STREAM(s, SHT_RELA     , kStrWidth);
960     CASE_AND_STREAM(s, SHT_HASH     , kStrWidth);
961     CASE_AND_STREAM(s, SHT_DYNAMIC  , kStrWidth);
962     CASE_AND_STREAM(s, SHT_NOTE     , kStrWidth);
963     CASE_AND_STREAM(s, SHT_NOBITS   , kStrWidth);
964     CASE_AND_STREAM(s, SHT_REL      , kStrWidth);
965     CASE_AND_STREAM(s, SHT_SHLIB    , kStrWidth);
966     CASE_AND_STREAM(s, SHT_DYNSYM   , kStrWidth);
967     CASE_AND_STREAM(s, SHT_LOPROC   , kStrWidth);
968     CASE_AND_STREAM(s, SHT_HIPROC   , kStrWidth);
969     CASE_AND_STREAM(s, SHT_LOUSER   , kStrWidth);
970     CASE_AND_STREAM(s, SHT_HIUSER   , kStrWidth);
971     default:
972         s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
973         break;
974     }
975 }
976 
977 //----------------------------------------------------------------------
978 // DumpELFSectionHeader_sh_flags
979 //
980 // Dump an token value for the ELF section header member sh_flags
981 //----------------------------------------------------------------------
982 void
983 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags)
984 {
985     *s  << ((sh_flags & SHF_WRITE) ? "WRITE" : "     ")
986         << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
987         << ((sh_flags & SHF_ALLOC) ? "ALLOC" : "     ")
988         << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
989         << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : "         ");
990 }
991 
992 //----------------------------------------------------------------------
993 // DumpELFSectionHeaders
994 //
995 // Dump all of the ELF section header to the specified output stream
996 //----------------------------------------------------------------------
997 void
998 ObjectFileELF::DumpELFSectionHeaders(Stream *s)
999 {
1000     if (!(ParseSectionHeaders() && GetSectionHeaderStringTable()))
1001         return;
1002 
1003     s->PutCString("Section Headers\n");
1004     s->PutCString("IDX  name     type         flags                            "
1005                   "addr     offset   size     link     info     addralgn "
1006                   "entsize  Name\n");
1007     s->PutCString("==== -------- ------------ -------------------------------- "
1008                   "-------- -------- -------- -------- -------- -------- "
1009                   "-------- ====================\n");
1010 
1011     uint32_t idx = 0;
1012     for (SectionHeaderCollConstIter I = m_section_headers.begin();
1013          I != m_section_headers.end(); ++I, ++idx)
1014     {
1015         s->Printf("[%2u] ", idx);
1016         ObjectFileELF::DumpELFSectionHeader(s, *I);
1017         const char* section_name = m_shstr_data.PeekCStr(I->sh_name);
1018         if (section_name)
1019             *s << ' ' << section_name << "\n";
1020     }
1021 }
1022 
1023 void
1024 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1025 {
1026     size_t num_modules = ParseDependentModules();
1027 
1028     if (num_modules > 0)
1029     {
1030         s->PutCString("Dependent Modules:\n");
1031         for (unsigned i = 0; i < num_modules; ++i)
1032         {
1033             const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1034             s->Printf("   %s\n", spec.GetFilename().GetCString());
1035         }
1036     }
1037 }
1038 
1039 bool
1040 ObjectFileELF::GetArchitecture (ArchSpec &arch)
1041 {
1042     if (!ParseHeader())
1043         return false;
1044 
1045     arch.SetArchitecture (lldb::eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
1046     arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1047     arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
1048     return true;
1049 }
1050 
1051