1 //===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ObjectFileELF.h"
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/ModuleSpec.h"
21 #include "lldb/Core/PluginManager.h"
22 #include "lldb/Core/Section.h"
23 #include "lldb/Core/Stream.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Host/Host.h"
27 #include "llvm/ADT/PointerUnion.h"
29 #define CASE_AND_STREAM(s, def, width) \
30 case def: s->Printf("%-*s", width, #def); break;
33 using namespace lldb_private;
35 using namespace llvm::ELF;
38 //===----------------------------------------------------------------------===//
39 /// @class ELFRelocation
40 /// @brief Generic wrapper for ELFRel and ELFRela.
42 /// This helper class allows us to parse both ELFRel and ELFRela relocation
43 /// entries in a generic manner.
48 /// Constructs an ELFRelocation entry with a personality as given by @p
51 /// @param type Either DT_REL or DT_RELA. Any other value is invalid.
52 ELFRelocation(unsigned type);
57 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
60 RelocType32(const ELFRelocation &rel);
63 RelocType64(const ELFRelocation &rel);
66 RelocSymbol32(const ELFRelocation &rel);
69 RelocSymbol64(const ELFRelocation &rel);
72 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion;
77 ELFRelocation::ELFRelocation(unsigned type)
81 else if (type == DT_RELA)
82 reloc = new ELFRela();
84 assert(false && "unexpected relocation type");
85 reloc = static_cast<ELFRel*>(NULL);
89 ELFRelocation::~ELFRelocation()
91 if (reloc.is<ELFRel*>())
92 delete reloc.get<ELFRel*>();
94 delete reloc.get<ELFRela*>();
98 ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset)
100 if (reloc.is<ELFRel*>())
101 return reloc.get<ELFRel*>()->Parse(data, offset);
103 return reloc.get<ELFRela*>()->Parse(data, offset);
107 ELFRelocation::RelocType32(const ELFRelocation &rel)
109 if (rel.reloc.is<ELFRel*>())
110 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>());
112 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>());
116 ELFRelocation::RelocType64(const ELFRelocation &rel)
118 if (rel.reloc.is<ELFRel*>())
119 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>());
121 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>());
125 ELFRelocation::RelocSymbol32(const ELFRelocation &rel)
127 if (rel.reloc.is<ELFRel*>())
128 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>());
130 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>());
134 ELFRelocation::RelocSymbol64(const ELFRelocation &rel)
136 if (rel.reloc.is<ELFRel*>())
137 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>());
139 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>());
142 } // end anonymous namespace
144 //------------------------------------------------------------------
146 //------------------------------------------------------------------
148 ObjectFileELF::Initialize()
150 PluginManager::RegisterPlugin(GetPluginNameStatic(),
151 GetPluginDescriptionStatic(),
153 CreateMemoryInstance,
154 GetModuleSpecifications);
158 ObjectFileELF::Terminate()
160 PluginManager::UnregisterPlugin(CreateInstance);
163 lldb_private::ConstString
164 ObjectFileELF::GetPluginNameStatic()
166 static ConstString g_name("elf");
171 ObjectFileELF::GetPluginDescriptionStatic()
173 return "ELF object file reader.";
177 ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp,
178 DataBufferSP &data_sp,
179 lldb::offset_t data_offset,
180 const lldb_private::FileSpec* file,
181 lldb::offset_t file_offset,
182 lldb::offset_t length)
186 data_sp = file->MemoryMapFileContents(file_offset, length);
190 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
192 const uint8_t *magic = data_sp->GetBytes() + data_offset;
193 if (ELFHeader::MagicBytesMatch(magic))
195 // Update the data to contain the entire file if it doesn't already
196 if (data_sp->GetByteSize() < length) {
197 data_sp = file->MemoryMapFileContents(file_offset, length);
199 magic = data_sp->GetBytes();
201 unsigned address_size = ELFHeader::AddressSizeInBytes(magic);
202 if (address_size == 4 || address_size == 8)
204 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length));
206 if (objfile_ap->GetArchitecture(spec) &&
207 objfile_ap->SetModulesArchitecture(spec))
208 return objfile_ap.release();
217 ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp,
218 DataBufferSP& data_sp,
219 const lldb::ProcessSP &process_sp,
220 lldb::addr_t header_addr)
226 ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp,
227 lldb::addr_t data_offset,
228 lldb::addr_t data_length)
230 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset))
232 const uint8_t *magic = data_sp->GetBytes() + data_offset;
233 return ELFHeader::MagicBytesMatch(magic);
239 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c
241 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or
242 * code or tables extracted from it, as desired without restriction.
245 calc_gnu_debuglink_crc32(const void *buf, size_t size)
247 static const uint32_t g_crc32_tab[] =
249 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
250 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
251 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
252 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
253 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
254 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
255 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
256 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
257 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
258 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
259 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
260 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
261 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
262 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
263 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
264 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
265 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
266 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
267 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
268 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
269 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
270 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
271 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
272 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
273 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
274 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
275 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
276 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
277 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
278 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
279 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
280 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
281 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
282 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
283 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
284 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
285 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
286 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
287 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
288 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
289 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
290 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
291 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
293 const uint8_t *p = (const uint8_t *)buf;
298 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
303 ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file,
304 lldb::DataBufferSP& data_sp,
305 lldb::offset_t data_offset,
306 lldb::offset_t file_offset,
307 lldb::offset_t length,
308 lldb_private::ModuleSpecList &specs)
310 const size_t initial_count = specs.GetSize();
312 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize()))
315 data.SetData(data_sp);
316 elf::ELFHeader header;
317 if (header.Parse(data, &data_offset))
322 spec.GetFileSpec() = file;
323 spec.GetArchitecture().SetArchitecture(eArchTypeELF,
325 LLDB_INVALID_CPUTYPE);
326 if (spec.GetArchitecture().IsValid())
328 // We could parse the ABI tag information (in .note, .notes, or .note.ABI-tag) to get the
329 // machine information. However, this info isn't guaranteed to exist or be correct. Details:
330 // http://refspecs.linuxfoundation.org/LSB_1.2.0/gLSB/noteabitag.html
331 // Instead of passing potentially incorrect information down the pipeline, grab
332 // the host information and use it.
333 spec.GetArchitecture().GetTriple().setOSName (Host::GetOSString().GetCString());
334 spec.GetArchitecture().GetTriple().setVendorName(Host::GetVendorString().GetCString());
336 // Try to get the UUID from the section list. Usually that's at the end, so
337 // map the file in if we don't have it already.
338 size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize;
339 if (section_header_end > data_sp->GetByteSize())
341 data_sp = file.MemoryMapFileContents (file_offset, section_header_end);
342 data.SetData(data_sp);
345 uint32_t gnu_debuglink_crc = 0;
346 std::string gnu_debuglink_file;
347 SectionHeaderColl section_headers;
348 lldb_private::UUID &uuid = spec.GetUUID();
349 GetSectionHeaderInfo(section_headers, data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc);
353 if (!gnu_debuglink_crc)
355 // Need to map entire file into memory to calculate the crc.
356 data_sp = file.MemoryMapFileContents (file_offset, SIZE_MAX);
357 data.SetData(data_sp);
358 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize());
360 if (gnu_debuglink_crc)
362 // Use 4 bytes of crc from the .gnu_debuglink section.
363 uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 };
364 uuid.SetBytes (uuidt, sizeof(uuidt));
374 return specs.GetSize() - initial_count;
377 //------------------------------------------------------------------
378 // PluginInterface protocol
379 //------------------------------------------------------------------
380 lldb_private::ConstString
381 ObjectFileELF::GetPluginName()
383 return GetPluginNameStatic();
387 ObjectFileELF::GetPluginVersion()
389 return m_plugin_version;
391 //------------------------------------------------------------------
392 // ObjectFile protocol
393 //------------------------------------------------------------------
395 ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp,
396 DataBufferSP& data_sp,
397 lldb::offset_t data_offset,
398 const FileSpec* file,
399 lldb::offset_t file_offset,
400 lldb::offset_t length) :
401 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
409 ::memset(&m_header, 0, sizeof(m_header));
410 m_gnu_debuglink_crc = 0;
411 m_gnu_debuglink_file.clear();
414 ObjectFileELF::~ObjectFileELF()
419 ObjectFileELF::IsExecutable() const
421 return m_header.e_entry != 0;
425 ObjectFileELF::GetByteOrder() const
427 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)
428 return eByteOrderBig;
429 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)
430 return eByteOrderLittle;
431 return eByteOrderInvalid;
435 ObjectFileELF::GetAddressByteSize() const
437 return m_data.GetAddressByteSize();
441 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I)
443 return std::distance(m_section_headers.begin(), I) + 1u;
447 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const
449 return std::distance(m_section_headers.begin(), I) + 1u;
453 ObjectFileELF::ParseHeader()
455 lldb::offset_t offset = 0;
456 return m_header.Parse(m_data, &offset);
460 ObjectFileELF::GetUUID(lldb_private::UUID* uuid)
462 // Need to parse the section list to get the UUIDs, so make sure that's been done.
463 if (!ParseSectionHeaders())
466 if (m_uuid.IsValid())
468 // We have the full build id uuid.
474 if (!m_gnu_debuglink_crc)
475 m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize());
476 if (m_gnu_debuglink_crc)
478 // Use 4 bytes of crc from the .gnu_debuglink section.
479 uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 };
480 uuid->SetBytes (uuidt, sizeof(uuidt));
488 lldb_private::FileSpecList
489 ObjectFileELF::GetDebugSymbolFilePaths()
491 FileSpecList file_spec_list;
493 if (!m_gnu_debuglink_file.empty())
495 FileSpec file_spec (m_gnu_debuglink_file.c_str(), false);
496 file_spec_list.Append (file_spec);
498 return file_spec_list;
502 ObjectFileELF::GetDependentModules(FileSpecList &files)
504 size_t num_modules = ParseDependentModules();
505 uint32_t num_specs = 0;
507 for (unsigned i = 0; i < num_modules; ++i)
509 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i)))
517 ObjectFileELF::GetImageInfoAddress()
519 if (!ParseDynamicSymbols())
522 SectionList *section_list = GetSectionList();
526 // Find the SHT_DYNAMIC (.dynamic) section.
527 SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true));
528 if (!dynsym_section_sp)
530 assert (dynsym_section_sp->GetObjectFile() == this);
532 user_id_t dynsym_id = dynsym_section_sp->GetID();
533 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);
537 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i)
539 ELFDynamic &symbol = m_dynamic_symbols[i];
541 if (symbol.d_tag == DT_DEBUG)
543 // Compute the offset as the number of previous entries plus the
545 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();
546 return Address(dynsym_section_sp, offset);
553 lldb_private::Address
554 ObjectFileELF::GetEntryPointAddress ()
556 if (m_entry_point_address.IsValid())
557 return m_entry_point_address;
559 if (!ParseHeader() || !IsExecutable())
560 return m_entry_point_address;
562 SectionList *section_list = GetSectionList();
563 addr_t offset = m_header.e_entry;
566 m_entry_point_address.SetOffset(offset);
568 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);
569 return m_entry_point_address;
572 //----------------------------------------------------------------------
573 // ParseDependentModules
574 //----------------------------------------------------------------------
576 ObjectFileELF::ParseDependentModules()
578 if (m_filespec_ap.get())
579 return m_filespec_ap->GetSize();
581 m_filespec_ap.reset(new FileSpecList());
583 if (!ParseSectionHeaders())
586 SectionList *section_list = GetSectionList();
590 // Find the SHT_DYNAMIC section.
591 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
594 assert (dynsym->GetObjectFile() == this);
596 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID());
599 // sh_link: section header index of string table used by entries in the section.
600 Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get();
604 DataExtractor dynsym_data;
605 DataExtractor dynstr_data;
606 if (ReadSectionData(dynsym, dynsym_data) &&
607 ReadSectionData(dynstr, dynstr_data))
610 const lldb::offset_t section_size = dynsym_data.GetByteSize();
611 lldb::offset_t offset = 0;
613 // The only type of entries we are concerned with are tagged DT_NEEDED,
614 // yielding the name of a required library.
615 while (offset < section_size)
617 if (!symbol.Parse(dynsym_data, &offset))
620 if (symbol.d_tag != DT_NEEDED)
623 uint32_t str_index = static_cast<uint32_t>(symbol.d_val);
624 const char *lib_name = dynstr_data.PeekCStr(str_index);
625 m_filespec_ap->Append(FileSpec(lib_name, true));
629 return m_filespec_ap->GetSize();
632 //----------------------------------------------------------------------
633 // ParseProgramHeaders
634 //----------------------------------------------------------------------
636 ObjectFileELF::ParseProgramHeaders()
638 // We have already parsed the program headers
639 if (!m_program_headers.empty())
640 return m_program_headers.size();
642 // If there are no program headers to read we are done.
643 if (m_header.e_phnum == 0)
646 m_program_headers.resize(m_header.e_phnum);
647 if (m_program_headers.size() != m_header.e_phnum)
650 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize;
651 const elf_off ph_offset = m_header.e_phoff;
653 if (GetData (ph_offset, ph_size, data) != ph_size)
657 lldb::offset_t offset;
658 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx)
660 if (m_program_headers[idx].Parse(data, &offset) == false)
664 if (idx < m_program_headers.size())
665 m_program_headers.resize(idx);
667 return m_program_headers.size();
671 ParseNoteGNUBuildID(DataExtractor &data, lldb_private::UUID &uuid)
673 // Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id.
674 // BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId
677 uint32_t name_len; // Length of note name
678 uint32_t desc_len; // Length of note descriptor
679 uint32_t type; // Type of note (1 is ABI_TAG, 3 is BUILD_ID)
681 lldb::offset_t offset = 0;
682 static const uint32_t g_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h
686 if (data.GetU32 (&offset, ¬ehdr, 3) == NULL)
689 notehdr.name_len = llvm::RoundUpToAlignment (notehdr.name_len, 4);
690 notehdr.desc_len = llvm::RoundUpToAlignment (notehdr.desc_len, 4);
692 lldb::offset_t offset_next_note = offset + notehdr.name_len + notehdr.desc_len;
694 // 16 bytes is UUID|MD5, 20 bytes is SHA1
695 if ((notehdr.type == g_gnu_build_id) && (notehdr.name_len == 4) &&
696 (notehdr.desc_len == 16 || notehdr.desc_len == 20))
699 if (data.GetU8 (&offset, name, 4) == NULL)
701 if (!strcmp(name, "GNU"))
704 if (data.GetU8 (&offset, &uuidbuf, notehdr.desc_len) == NULL)
706 uuid.SetBytes (uuidbuf, notehdr.desc_len);
710 offset = offset_next_note;
715 //----------------------------------------------------------------------
716 // GetSectionHeaderInfo
717 //----------------------------------------------------------------------
719 ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers,
720 lldb_private::DataExtractor &object_data,
721 const elf::ELFHeader &header,
722 lldb_private::UUID &uuid,
723 std::string &gnu_debuglink_file,
724 uint32_t &gnu_debuglink_crc)
726 // We have already parsed the section headers
727 if (!section_headers.empty())
728 return section_headers.size();
730 // If there are no section headers we are done.
731 if (header.e_shnum == 0)
734 section_headers.resize(header.e_shnum);
735 if (section_headers.size() != header.e_shnum)
738 const size_t sh_size = header.e_shnum * header.e_shentsize;
739 const elf_off sh_offset = header.e_shoff;
740 DataExtractor sh_data;
741 if (sh_data.SetData (object_data, sh_offset, sh_size) != sh_size)
745 lldb::offset_t offset;
746 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx)
748 if (section_headers[idx].Parse(sh_data, &offset) == false)
751 if (idx < section_headers.size())
752 section_headers.resize(idx);
754 const unsigned strtab_idx = header.e_shstrndx;
755 if (strtab_idx && strtab_idx < section_headers.size())
757 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];
758 const size_t byte_size = sheader.sh_size;
759 const Elf64_Off offset = sheader.sh_offset;
760 lldb_private::DataExtractor shstr_data;
762 if (shstr_data.SetData (object_data, offset, byte_size) == byte_size)
764 for (SectionHeaderCollIter I = section_headers.begin();
765 I != section_headers.end(); ++I)
767 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink");
768 const ELFSectionHeaderInfo &header = *I;
769 const uint64_t section_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
770 ConstString name(shstr_data.PeekCStr(I->sh_name));
772 I->section_name = name;
774 if (name == g_sect_name_gnu_debuglink)
777 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size))
779 lldb::offset_t gnu_debuglink_offset = 0;
780 gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset);
781 gnu_debuglink_offset = llvm::RoundUpToAlignment (gnu_debuglink_offset, 4);
782 data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1);
786 if (header.sh_type == SHT_NOTE && !uuid.IsValid())
789 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size))
791 ParseNoteGNUBuildID (data, uuid);
796 return section_headers.size();
800 section_headers.clear();
805 ObjectFileELF::GetProgramHeaderCount()
807 return ParseProgramHeaders();
810 const elf::ELFProgramHeader *
811 ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id)
813 if (!id || !ParseProgramHeaders())
816 if (--id < m_program_headers.size())
817 return &m_program_headers[id];
823 ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id)
825 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id);
826 if (segment_header == NULL)
827 return DataExtractor();
828 return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz);
831 //----------------------------------------------------------------------
832 // ParseSectionHeaders
833 //----------------------------------------------------------------------
835 ObjectFileELF::ParseSectionHeaders()
837 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc);
840 const ObjectFileELF::ELFSectionHeaderInfo *
841 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id)
843 if (!id || !ParseSectionHeaders())
846 if (--id < m_section_headers.size())
847 return &m_section_headers[id];
853 ObjectFileELF::CreateSections(SectionList &unified_section_list)
855 if (!m_sections_ap.get() && ParseSectionHeaders())
857 m_sections_ap.reset(new SectionList());
859 for (SectionHeaderCollIter I = m_section_headers.begin();
860 I != m_section_headers.end(); ++I)
862 const ELFSectionHeaderInfo &header = *I;
864 ConstString& name = I->section_name;
865 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size;
866 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0;
868 static ConstString g_sect_name_text (".text");
869 static ConstString g_sect_name_data (".data");
870 static ConstString g_sect_name_bss (".bss");
871 static ConstString g_sect_name_tdata (".tdata");
872 static ConstString g_sect_name_tbss (".tbss");
873 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev");
874 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges");
875 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame");
876 static ConstString g_sect_name_dwarf_debug_info (".debug_info");
877 static ConstString g_sect_name_dwarf_debug_line (".debug_line");
878 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc");
879 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo");
880 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames");
881 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes");
882 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges");
883 static ConstString g_sect_name_dwarf_debug_str (".debug_str");
884 static ConstString g_sect_name_eh_frame (".eh_frame");
886 SectionType sect_type = eSectionTypeOther;
888 bool is_thread_specific = false;
890 if (name == g_sect_name_text) sect_type = eSectionTypeCode;
891 else if (name == g_sect_name_data) sect_type = eSectionTypeData;
892 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill;
893 else if (name == g_sect_name_tdata)
895 sect_type = eSectionTypeData;
896 is_thread_specific = true;
898 else if (name == g_sect_name_tbss)
900 sect_type = eSectionTypeZeroFill;
901 is_thread_specific = true;
903 // .debug_abbrev – Abbreviations used in the .debug_info section
904 // .debug_aranges – Lookup table for mapping addresses to compilation units
905 // .debug_frame – Call frame information
906 // .debug_info – The core DWARF information section
907 // .debug_line – Line number information
908 // .debug_loc – Location lists used in DW_AT_location attributes
909 // .debug_macinfo – Macro information
910 // .debug_pubnames – Lookup table for mapping object and function names to compilation units
911 // .debug_pubtypes – Lookup table for mapping type names to compilation units
912 // .debug_ranges – Address ranges used in DW_AT_ranges attributes
913 // .debug_str – String table used in .debug_info
914 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html
915 // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644
916 // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo
917 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev;
918 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges;
919 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame;
920 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo;
921 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine;
922 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc;
923 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo;
924 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames;
925 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes;
926 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges;
927 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr;
928 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame;
930 switch (header.sh_type)
933 assert (sect_type == eSectionTypeOther);
934 sect_type = eSectionTypeELFSymbolTable;
937 assert (sect_type == eSectionTypeOther);
938 sect_type = eSectionTypeELFDynamicSymbols;
942 assert (sect_type == eSectionTypeOther);
943 sect_type = eSectionTypeELFRelocationEntries;
946 assert (sect_type == eSectionTypeOther);
947 sect_type = eSectionTypeELFDynamicLinkInfo;
951 SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs.
952 this, // ObjectFile to which this section belongs and should read section data from.
953 SectionIndex(I), // Section ID.
954 name, // Section name.
955 sect_type, // Section type.
956 header.sh_addr, // VM address.
957 vm_size, // VM size in bytes of this section.
958 header.sh_offset, // Offset of this section in the file.
959 file_size, // Size of the section as found in the file.
960 header.sh_flags)); // Flags for this section.
962 if (is_thread_specific)
963 section_sp->SetIsThreadSpecific (is_thread_specific);
964 m_sections_ap->AddSection(section_sp);
968 if (m_sections_ap.get())
970 if (GetType() == eTypeDebugInfo)
972 static const SectionType g_sections[] =
974 eSectionTypeDWARFDebugAranges,
975 eSectionTypeDWARFDebugInfo,
976 eSectionTypeDWARFDebugAbbrev,
977 eSectionTypeDWARFDebugFrame,
978 eSectionTypeDWARFDebugLine,
979 eSectionTypeDWARFDebugStr,
980 eSectionTypeDWARFDebugLoc,
981 eSectionTypeDWARFDebugMacInfo,
982 eSectionTypeDWARFDebugPubNames,
983 eSectionTypeDWARFDebugPubTypes,
984 eSectionTypeDWARFDebugRanges,
985 eSectionTypeELFSymbolTable,
987 SectionList *elf_section_list = m_sections_ap.get();
988 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx)
990 SectionType section_type = g_sections[idx];
991 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true));
994 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true));
995 if (module_section_sp)
996 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp);
998 unified_section_list.AddSection (section_sp);
1004 unified_section_list = *m_sections_ap;
1011 ObjectFileELF::ParseSymbols (Symtab *symtab,
1013 SectionList *section_list,
1014 const size_t num_symbols,
1015 const DataExtractor &symtab_data,
1016 const DataExtractor &strtab_data)
1019 lldb::offset_t offset = 0;
1021 static ConstString text_section_name(".text");
1022 static ConstString init_section_name(".init");
1023 static ConstString fini_section_name(".fini");
1024 static ConstString ctors_section_name(".ctors");
1025 static ConstString dtors_section_name(".dtors");
1027 static ConstString data_section_name(".data");
1028 static ConstString rodata_section_name(".rodata");
1029 static ConstString rodata1_section_name(".rodata1");
1030 static ConstString data2_section_name(".data1");
1031 static ConstString bss_section_name(".bss");
1033 //StreamFile strm(stdout, false);
1035 for (i = 0; i < num_symbols; ++i)
1037 if (symbol.Parse(symtab_data, &offset) == false)
1040 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
1042 // No need to add symbols that have no names
1043 if (symbol_name == NULL || symbol_name[0] == '\0')
1046 //symbol.Dump (&strm, i, &strtab_data, section_list);
1048 SectionSP symbol_section_sp;
1049 SymbolType symbol_type = eSymbolTypeInvalid;
1050 Elf64_Half symbol_idx = symbol.st_shndx;
1055 symbol_type = eSymbolTypeAbsolute;
1058 symbol_type = eSymbolTypeUndefined;
1061 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx);
1065 // If a symbol is undefined do not process it further even if it has a STT type
1066 if (symbol_type != eSymbolTypeUndefined)
1068 switch (symbol.getType())
1072 // The symbol's type is not specified.
1076 // The symbol is associated with a data object, such as a variable,
1078 symbol_type = eSymbolTypeData;
1082 // The symbol is associated with a function or other executable code.
1083 symbol_type = eSymbolTypeCode;
1087 // The symbol is associated with a section. Symbol table entries of
1088 // this type exist primarily for relocation and normally have
1089 // STB_LOCAL binding.
1093 // Conventionally, the symbol's name gives the name of the source
1094 // file associated with the object file. A file symbol has STB_LOCAL
1095 // binding, its section index is SHN_ABS, and it precedes the other
1096 // STB_LOCAL symbols for the file, if it is present.
1097 symbol_type = eSymbolTypeSourceFile;
1101 // The symbol is associated with an indirect function. The actual
1102 // function will be resolved if it is referenced.
1103 symbol_type = eSymbolTypeResolver;
1108 if (symbol_type == eSymbolTypeInvalid)
1110 if (symbol_section_sp)
1112 const ConstString §_name = symbol_section_sp->GetName();
1113 if (sect_name == text_section_name ||
1114 sect_name == init_section_name ||
1115 sect_name == fini_section_name ||
1116 sect_name == ctors_section_name ||
1117 sect_name == dtors_section_name)
1119 symbol_type = eSymbolTypeCode;
1121 else if (sect_name == data_section_name ||
1122 sect_name == data2_section_name ||
1123 sect_name == rodata_section_name ||
1124 sect_name == rodata1_section_name ||
1125 sect_name == bss_section_name)
1127 symbol_type = eSymbolTypeData;
1132 // If the symbol section we've found has no data (SHT_NOBITS), then check the module section
1133 // list. This can happen if we're parsing the debug file and it has no .text section, for example.
1134 if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0))
1136 ModuleSP module_sp(GetModule());
1139 SectionList *module_section_list = module_sp->GetSectionList();
1140 if (module_section_list && module_section_list != section_list)
1142 const ConstString §_name = symbol_section_sp->GetName();
1143 lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name));
1144 if (section_sp && section_sp->GetFileSize())
1146 symbol_section_sp = section_sp;
1152 uint64_t symbol_value = symbol.st_value;
1153 if (symbol_section_sp)
1154 symbol_value -= symbol_section_sp->GetFileAddress();
1155 bool is_global = symbol.getBinding() == STB_GLOBAL;
1156 uint32_t flags = symbol.st_other << 8 | symbol.st_info;
1157 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
1159 i + start_id, // ID is the original symbol table index.
1160 symbol_name, // Symbol name.
1161 is_mangled, // Is the symbol name mangled?
1162 symbol_type, // Type of this symbol
1163 is_global, // Is this globally visible?
1164 false, // Is this symbol debug info?
1165 false, // Is this symbol a trampoline?
1166 false, // Is this symbol artificial?
1167 symbol_section_sp, // Section in which this symbol is defined or null.
1168 symbol_value, // Offset in section or symbol value.
1169 symbol.st_size, // Size in bytes of this symbol.
1170 true, // Size is valid
1171 flags); // Symbol flags.
1172 symtab->AddSymbol(dc_symbol);
1179 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab)
1181 if (symtab->GetObjectFile() != this)
1183 // If the symbol table section is owned by a different object file, have it do the
1185 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile());
1186 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab);
1189 // Get section list for this object file.
1190 SectionList *section_list = m_sections_ap.get();
1194 user_id_t symtab_id = symtab->GetID();
1195 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);
1196 assert(symtab_hdr->sh_type == SHT_SYMTAB ||
1197 symtab_hdr->sh_type == SHT_DYNSYM);
1199 // sh_link: section header index of associated string table.
1200 // Section ID's are ones based.
1201 user_id_t strtab_id = symtab_hdr->sh_link + 1;
1202 Section *strtab = section_list->FindSectionByID(strtab_id).get();
1204 unsigned num_symbols = 0;
1205 if (symtab && strtab)
1207 assert (symtab->GetObjectFile() == this);
1208 assert (strtab->GetObjectFile() == this);
1210 DataExtractor symtab_data;
1211 DataExtractor strtab_data;
1212 if (ReadSectionData(symtab, symtab_data) &&
1213 ReadSectionData(strtab, strtab_data))
1215 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;
1217 num_symbols = ParseSymbols(symbol_table, start_id,
1218 section_list, num_symbols,
1219 symtab_data, strtab_data);
1227 ObjectFileELF::ParseDynamicSymbols()
1229 if (m_dynamic_symbols.size())
1230 return m_dynamic_symbols.size();
1232 SectionList *section_list = GetSectionList();
1236 // Find the SHT_DYNAMIC section.
1237 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get();
1240 assert (dynsym->GetObjectFile() == this);
1243 DataExtractor dynsym_data;
1244 if (ReadSectionData(dynsym, dynsym_data))
1246 const lldb::offset_t section_size = dynsym_data.GetByteSize();
1247 lldb::offset_t cursor = 0;
1249 while (cursor < section_size)
1251 if (!symbol.Parse(dynsym_data, &cursor))
1254 m_dynamic_symbols.push_back(symbol);
1258 return m_dynamic_symbols.size();
1262 ObjectFileELF::FindDynamicSymbol(unsigned tag)
1264 if (!ParseDynamicSymbols())
1267 DynamicSymbolCollIter I = m_dynamic_symbols.begin();
1268 DynamicSymbolCollIter E = m_dynamic_symbols.end();
1269 for ( ; I != E; ++I)
1271 ELFDynamic *symbol = &*I;
1273 if (symbol->d_tag == tag)
1281 ObjectFileELF::PLTRelocationType()
1283 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);
1286 return symbol->d_val;
1292 ParsePLTRelocations(Symtab *symbol_table,
1295 const ELFHeader *hdr,
1296 const ELFSectionHeader *rel_hdr,
1297 const ELFSectionHeader *plt_hdr,
1298 const ELFSectionHeader *sym_hdr,
1299 const lldb::SectionSP &plt_section_sp,
1300 DataExtractor &rel_data,
1301 DataExtractor &symtab_data,
1302 DataExtractor &strtab_data)
1304 ELFRelocation rel(rel_type);
1306 lldb::offset_t offset = 0;
1307 const elf_xword plt_entsize = plt_hdr->sh_entsize;
1308 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;
1310 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);
1311 reloc_info_fn reloc_type;
1312 reloc_info_fn reloc_symbol;
1316 reloc_type = ELFRelocation::RelocType32;
1317 reloc_symbol = ELFRelocation::RelocSymbol32;
1321 reloc_type = ELFRelocation::RelocType64;
1322 reloc_symbol = ELFRelocation::RelocSymbol64;
1325 unsigned slot_type = hdr->GetRelocationJumpSlotType();
1327 for (i = 0; i < num_relocations; ++i)
1329 if (rel.Parse(rel_data, &offset) == false)
1332 if (reloc_type(rel) != slot_type)
1335 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;
1336 uint64_t plt_index = (i + 1) * plt_entsize;
1338 if (!symbol.Parse(symtab_data, &symbol_offset))
1341 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);
1342 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false;
1345 i + start_id, // Symbol table index
1346 symbol_name, // symbol name.
1347 is_mangled, // is the symbol name mangled?
1348 eSymbolTypeTrampoline, // Type of this symbol
1349 false, // Is this globally visible?
1350 false, // Is this symbol debug info?
1351 true, // Is this symbol a trampoline?
1352 true, // Is this symbol artificial?
1353 plt_section_sp, // Section in which this symbol is defined or null.
1354 plt_index, // Offset in section or symbol value.
1355 plt_entsize, // Size in bytes of this symbol.
1356 true, // Size is valid
1357 0); // Symbol flags.
1359 symbol_table->AddSymbol(jump_symbol);
1366 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table,
1368 const ELFSectionHeaderInfo *rel_hdr,
1371 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);
1373 // The link field points to the associated symbol table. The info field
1374 // points to the section holding the plt.
1375 user_id_t symtab_id = rel_hdr->sh_link;
1376 user_id_t plt_id = rel_hdr->sh_info;
1378 if (!symtab_id || !plt_id)
1381 // Section ID's are ones based;
1385 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);
1389 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);
1393 SectionList *section_list = m_sections_ap.get();
1397 Section *rel_section = section_list->FindSectionByID(rel_id).get();
1401 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id));
1402 if (!plt_section_sp)
1405 Section *symtab = section_list->FindSectionByID(symtab_id).get();
1409 // sh_link points to associated string table.
1410 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get();
1414 DataExtractor rel_data;
1415 if (!ReadSectionData(rel_section, rel_data))
1418 DataExtractor symtab_data;
1419 if (!ReadSectionData(symtab, symtab_data))
1422 DataExtractor strtab_data;
1423 if (!ReadSectionData(strtab, strtab_data))
1426 unsigned rel_type = PLTRelocationType();
1430 return ParsePLTRelocations (symbol_table,
1444 ObjectFileELF::GetSymtab()
1446 ModuleSP module_sp(GetModule());
1450 // We always want to use the main object file so we (hopefully) only have one cached copy
1451 // of our symtab, dynamic sections, etc.
1452 ObjectFile *module_obj_file = module_sp->GetObjectFile();
1453 if (module_obj_file && module_obj_file != this)
1454 return module_obj_file->GetSymtab();
1456 if (m_symtab_ap.get() == NULL)
1458 SectionList *section_list = GetSectionList();
1462 uint64_t symbol_id = 0;
1463 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
1465 m_symtab_ap.reset(new Symtab(this));
1467 // Sharable objects and dynamic executables usually have 2 distinct symbol
1468 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller
1469 // version of the symtab that only contains global symbols. The information found
1470 // in the dynsym is therefore also found in the symtab, while the reverse is not
1471 // necessarily true.
1472 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get();
1475 // The symtab section is non-allocable and can be stripped, so if it doesn't exist
1476 // then use the dynsym section which should always be there.
1477 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get();
1480 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab);
1482 // Synthesize trampoline symbols to help navigate the PLT.
1483 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);
1486 addr_t addr = symbol->d_ptr;
1487 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get();
1490 user_id_t reloc_id = reloc_section->GetID();
1491 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id);
1492 assert(reloc_header);
1494 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id);
1498 return m_symtab_ap.get();
1502 ObjectFileELF::IsStripped ()
1504 // TODO: determine this for ELF
1508 //===----------------------------------------------------------------------===//
1511 // Dump the specifics of the runtime file container (such as any headers
1512 // segments, sections, etc).
1513 //----------------------------------------------------------------------
1515 ObjectFileELF::Dump(Stream *s)
1517 DumpELFHeader(s, m_header);
1519 DumpELFProgramHeaders(s);
1521 DumpELFSectionHeaders(s);
1523 SectionList *section_list = GetSectionList();
1525 section_list->Dump(s, NULL, true, UINT32_MAX);
1526 Symtab *symtab = GetSymtab();
1528 symtab->Dump(s, NULL, eSortOrderNone);
1530 DumpDependentModules(s);
1534 //----------------------------------------------------------------------
1537 // Dump the ELF header to the specified output stream
1538 //----------------------------------------------------------------------
1540 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header)
1542 s->PutCString("ELF Header\n");
1543 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);
1544 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n",
1545 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]);
1546 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n",
1547 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]);
1548 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n",
1549 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]);
1551 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);
1552 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);
1553 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);
1554 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);
1555 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);
1557 s->Printf("e_type = 0x%4.4x ", header.e_type);
1558 DumpELFHeader_e_type(s, header.e_type);
1559 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);
1560 s->Printf("e_version = 0x%8.8x\n", header.e_version);
1561 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);
1562 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);
1563 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);
1564 s->Printf("e_flags = 0x%8.8x\n", header.e_flags);
1565 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);
1566 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);
1567 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum);
1568 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);
1569 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum);
1570 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx);
1573 //----------------------------------------------------------------------
1574 // DumpELFHeader_e_type
1576 // Dump an token value for the ELF header member e_type
1577 //----------------------------------------------------------------------
1579 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type)
1583 case ET_NONE: *s << "ET_NONE"; break;
1584 case ET_REL: *s << "ET_REL"; break;
1585 case ET_EXEC: *s << "ET_EXEC"; break;
1586 case ET_DYN: *s << "ET_DYN"; break;
1587 case ET_CORE: *s << "ET_CORE"; break;
1593 //----------------------------------------------------------------------
1594 // DumpELFHeader_e_ident_EI_DATA
1596 // Dump an token value for the ELF header member e_ident[EI_DATA]
1597 //----------------------------------------------------------------------
1599 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data)
1603 case ELFDATANONE: *s << "ELFDATANONE"; break;
1604 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break;
1605 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break;
1612 //----------------------------------------------------------------------
1613 // DumpELFProgramHeader
1615 // Dump a single ELF program header to the specified output stream
1616 //----------------------------------------------------------------------
1618 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph)
1620 DumpELFProgramHeader_p_type(s, ph.p_type);
1621 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr);
1622 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags);
1624 DumpELFProgramHeader_p_flags(s, ph.p_flags);
1625 s->Printf(") %8.8" PRIx64, ph.p_align);
1628 //----------------------------------------------------------------------
1629 // DumpELFProgramHeader_p_type
1631 // Dump an token value for the ELF program header member p_type which
1632 // describes the type of the program header
1633 // ----------------------------------------------------------------------
1635 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type)
1637 const int kStrWidth = 15;
1640 CASE_AND_STREAM(s, PT_NULL , kStrWidth);
1641 CASE_AND_STREAM(s, PT_LOAD , kStrWidth);
1642 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth);
1643 CASE_AND_STREAM(s, PT_INTERP , kStrWidth);
1644 CASE_AND_STREAM(s, PT_NOTE , kStrWidth);
1645 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth);
1646 CASE_AND_STREAM(s, PT_PHDR , kStrWidth);
1647 CASE_AND_STREAM(s, PT_TLS , kStrWidth);
1648 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);
1650 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");
1656 //----------------------------------------------------------------------
1657 // DumpELFProgramHeader_p_flags
1659 // Dump an token value for the ELF program header member p_flags
1660 //----------------------------------------------------------------------
1662 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags)
1664 *s << ((p_flags & PF_X) ? "PF_X" : " ")
1665 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')
1666 << ((p_flags & PF_W) ? "PF_W" : " ")
1667 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')
1668 << ((p_flags & PF_R) ? "PF_R" : " ");
1671 //----------------------------------------------------------------------
1672 // DumpELFProgramHeaders
1674 // Dump all of the ELF program header to the specified output stream
1675 //----------------------------------------------------------------------
1677 ObjectFileELF::DumpELFProgramHeaders(Stream *s)
1679 if (ParseProgramHeaders())
1681 s->PutCString("Program Headers\n");
1682 s->PutCString("IDX p_type p_offset p_vaddr p_paddr "
1683 "p_filesz p_memsz p_flags p_align\n");
1684 s->PutCString("==== --------------- -------- -------- -------- "
1685 "-------- -------- ------------------------- --------\n");
1688 for (ProgramHeaderCollConstIter I = m_program_headers.begin();
1689 I != m_program_headers.end(); ++I, ++idx)
1691 s->Printf("[%2u] ", idx);
1692 ObjectFileELF::DumpELFProgramHeader(s, *I);
1698 //----------------------------------------------------------------------
1699 // DumpELFSectionHeader
1701 // Dump a single ELF section header to the specified output stream
1702 //----------------------------------------------------------------------
1704 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh)
1706 s->Printf("%8.8x ", sh.sh_name);
1707 DumpELFSectionHeader_sh_type(s, sh.sh_type);
1708 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);
1709 DumpELFSectionHeader_sh_flags(s, sh.sh_flags);
1710 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size);
1711 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);
1712 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);
1715 //----------------------------------------------------------------------
1716 // DumpELFSectionHeader_sh_type
1718 // Dump an token value for the ELF section header member sh_type which
1719 // describes the type of the section
1720 //----------------------------------------------------------------------
1722 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type)
1724 const int kStrWidth = 12;
1727 CASE_AND_STREAM(s, SHT_NULL , kStrWidth);
1728 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth);
1729 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth);
1730 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth);
1731 CASE_AND_STREAM(s, SHT_RELA , kStrWidth);
1732 CASE_AND_STREAM(s, SHT_HASH , kStrWidth);
1733 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth);
1734 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth);
1735 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth);
1736 CASE_AND_STREAM(s, SHT_REL , kStrWidth);
1737 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth);
1738 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth);
1739 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth);
1740 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth);
1741 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth);
1742 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth);
1744 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");
1749 //----------------------------------------------------------------------
1750 // DumpELFSectionHeader_sh_flags
1752 // Dump an token value for the ELF section header member sh_flags
1753 //----------------------------------------------------------------------
1755 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags)
1757 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")
1758 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')
1759 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")
1760 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')
1761 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");
1764 //----------------------------------------------------------------------
1765 // DumpELFSectionHeaders
1767 // Dump all of the ELF section header to the specified output stream
1768 //----------------------------------------------------------------------
1770 ObjectFileELF::DumpELFSectionHeaders(Stream *s)
1772 if (!ParseSectionHeaders())
1775 s->PutCString("Section Headers\n");
1776 s->PutCString("IDX name type flags "
1777 "addr offset size link info addralgn "
1779 s->PutCString("==== -------- ------------ -------------------------------- "
1780 "-------- -------- -------- -------- -------- -------- "
1781 "-------- ====================\n");
1784 for (SectionHeaderCollConstIter I = m_section_headers.begin();
1785 I != m_section_headers.end(); ++I, ++idx)
1787 s->Printf("[%2u] ", idx);
1788 ObjectFileELF::DumpELFSectionHeader(s, *I);
1789 const char* section_name = I->section_name.AsCString("");
1791 *s << ' ' << section_name << "\n";
1796 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s)
1798 size_t num_modules = ParseDependentModules();
1800 if (num_modules > 0)
1802 s->PutCString("Dependent Modules:\n");
1803 for (unsigned i = 0; i < num_modules; ++i)
1805 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i);
1806 s->Printf(" %s\n", spec.GetFilename().GetCString());
1812 ObjectFileELF::GetArchitecture (ArchSpec &arch)
1817 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE);
1818 arch.GetTriple().setOSName (Host::GetOSString().GetCString());
1819 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString());
1824 ObjectFileELF::CalculateType()
1826 switch (m_header.e_type)
1828 case llvm::ELF::ET_NONE:
1830 return eTypeUnknown;
1832 case llvm::ELF::ET_REL:
1833 // 1 - Relocatable file
1834 return eTypeObjectFile;
1836 case llvm::ELF::ET_EXEC:
1837 // 2 - Executable file
1838 return eTypeExecutable;
1840 case llvm::ELF::ET_DYN:
1841 // 3 - Shared object file
1842 return eTypeSharedLibrary;
1846 return eTypeCoreFile;
1851 return eTypeUnknown;
1855 ObjectFileELF::CalculateStrata()
1857 switch (m_header.e_type)
1859 case llvm::ELF::ET_NONE:
1861 return eStrataUnknown;
1863 case llvm::ELF::ET_REL:
1864 // 1 - Relocatable file
1865 return eStrataUnknown;
1867 case llvm::ELF::ET_EXEC:
1868 // 2 - Executable file
1869 // TODO: is there any way to detect that an executable is a kernel
1870 // related executable by inspecting the program headers, section
1871 // headers, symbols, or any other flag bits???
1874 case llvm::ELF::ET_DYN:
1875 // 3 - Shared object file
1876 // TODO: is there any way to detect that an shared library is a kernel
1877 // related executable by inspecting the program headers, section
1878 // headers, symbols, or any other flag bits???
1879 return eStrataUnknown;
1883 // TODO: is there any way to detect that an core file is a kernel
1884 // related executable by inspecting the program headers, section
1885 // headers, symbols, or any other flag bits???
1886 return eStrataUnknown;
1891 return eStrataUnknown;