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/Log.h" 20 #include "lldb/Core/Module.h" 21 #include "lldb/Core/ModuleSpec.h" 22 #include "lldb/Core/PluginManager.h" 23 #include "lldb/Core/Section.h" 24 #include "lldb/Core/Stream.h" 25 #include "lldb/Symbol/DWARFCallFrameInfo.h" 26 #include "lldb/Symbol/SymbolContext.h" 27 #include "lldb/Target/Target.h" 28 #include "lldb/Host/Host.h" 29 30 #include "llvm/ADT/PointerUnion.h" 31 32 #define CASE_AND_STREAM(s, def, width) \ 33 case def: s->Printf("%-*s", width, #def); break; 34 35 using namespace lldb; 36 using namespace lldb_private; 37 using namespace elf; 38 using namespace llvm::ELF; 39 40 namespace { 41 //===----------------------------------------------------------------------===// 42 /// @class ELFRelocation 43 /// @brief Generic wrapper for ELFRel and ELFRela. 44 /// 45 /// This helper class allows us to parse both ELFRel and ELFRela relocation 46 /// entries in a generic manner. 47 class ELFRelocation 48 { 49 public: 50 51 /// Constructs an ELFRelocation entry with a personality as given by @p 52 /// type. 53 /// 54 /// @param type Either DT_REL or DT_RELA. Any other value is invalid. 55 ELFRelocation(unsigned type); 56 57 ~ELFRelocation(); 58 59 bool 60 Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 61 62 static unsigned 63 RelocType32(const ELFRelocation &rel); 64 65 static unsigned 66 RelocType64(const ELFRelocation &rel); 67 68 static unsigned 69 RelocSymbol32(const ELFRelocation &rel); 70 71 static unsigned 72 RelocSymbol64(const ELFRelocation &rel); 73 74 private: 75 typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion; 76 77 RelocUnion reloc; 78 }; 79 80 ELFRelocation::ELFRelocation(unsigned type) 81 { 82 if (type == DT_REL) 83 reloc = new ELFRel(); 84 else if (type == DT_RELA) 85 reloc = new ELFRela(); 86 else { 87 assert(false && "unexpected relocation type"); 88 reloc = static_cast<ELFRel*>(NULL); 89 } 90 } 91 92 ELFRelocation::~ELFRelocation() 93 { 94 if (reloc.is<ELFRel*>()) 95 delete reloc.get<ELFRel*>(); 96 else 97 delete reloc.get<ELFRela*>(); 98 } 99 100 bool 101 ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset) 102 { 103 if (reloc.is<ELFRel*>()) 104 return reloc.get<ELFRel*>()->Parse(data, offset); 105 else 106 return reloc.get<ELFRela*>()->Parse(data, offset); 107 } 108 109 unsigned 110 ELFRelocation::RelocType32(const ELFRelocation &rel) 111 { 112 if (rel.reloc.is<ELFRel*>()) 113 return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>()); 114 else 115 return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>()); 116 } 117 118 unsigned 119 ELFRelocation::RelocType64(const ELFRelocation &rel) 120 { 121 if (rel.reloc.is<ELFRel*>()) 122 return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>()); 123 else 124 return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>()); 125 } 126 127 unsigned 128 ELFRelocation::RelocSymbol32(const ELFRelocation &rel) 129 { 130 if (rel.reloc.is<ELFRel*>()) 131 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>()); 132 else 133 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>()); 134 } 135 136 unsigned 137 ELFRelocation::RelocSymbol64(const ELFRelocation &rel) 138 { 139 if (rel.reloc.is<ELFRel*>()) 140 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>()); 141 else 142 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>()); 143 } 144 145 } // end anonymous namespace 146 147 bool 148 ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) 149 { 150 // Read all fields. 151 if (data.GetU32(offset, &n_namesz, 3) == NULL) 152 return false; 153 154 // The name field is required to be nul-terminated, and n_namesz 155 // includes the terminating nul in observed implementations (contrary 156 // to the ELF-64 spec). A special case is needed for cores generated 157 // by some older Linux versions, which write a note named "CORE" 158 // without a nul terminator and n_namesz = 4. 159 if (n_namesz == 4) 160 { 161 char buf[4]; 162 if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4) 163 return false; 164 if (strncmp (buf, "CORE", 4) == 0) 165 { 166 n_name = "CORE"; 167 *offset += 4; 168 return true; 169 } 170 } 171 172 const char *cstr = data.GetCStr(offset, llvm::RoundUpToAlignment (n_namesz, 4)); 173 if (cstr == NULL) 174 { 175 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS)); 176 if (log) 177 log->Printf("Failed to parse note name lacking nul terminator"); 178 179 return false; 180 } 181 n_name = cstr; 182 return true; 183 } 184 185 //------------------------------------------------------------------ 186 // Static methods. 187 //------------------------------------------------------------------ 188 void 189 ObjectFileELF::Initialize() 190 { 191 PluginManager::RegisterPlugin(GetPluginNameStatic(), 192 GetPluginDescriptionStatic(), 193 CreateInstance, 194 CreateMemoryInstance, 195 GetModuleSpecifications); 196 } 197 198 void 199 ObjectFileELF::Terminate() 200 { 201 PluginManager::UnregisterPlugin(CreateInstance); 202 } 203 204 lldb_private::ConstString 205 ObjectFileELF::GetPluginNameStatic() 206 { 207 static ConstString g_name("elf"); 208 return g_name; 209 } 210 211 const char * 212 ObjectFileELF::GetPluginDescriptionStatic() 213 { 214 return "ELF object file reader."; 215 } 216 217 ObjectFile * 218 ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp, 219 DataBufferSP &data_sp, 220 lldb::offset_t data_offset, 221 const lldb_private::FileSpec* file, 222 lldb::offset_t file_offset, 223 lldb::offset_t length) 224 { 225 if (!data_sp) 226 { 227 data_sp = file->MemoryMapFileContents(file_offset, length); 228 data_offset = 0; 229 } 230 231 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) 232 { 233 const uint8_t *magic = data_sp->GetBytes() + data_offset; 234 if (ELFHeader::MagicBytesMatch(magic)) 235 { 236 // Update the data to contain the entire file if it doesn't already 237 if (data_sp->GetByteSize() < length) { 238 data_sp = file->MemoryMapFileContents(file_offset, length); 239 data_offset = 0; 240 magic = data_sp->GetBytes(); 241 } 242 unsigned address_size = ELFHeader::AddressSizeInBytes(magic); 243 if (address_size == 4 || address_size == 8) 244 { 245 std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length)); 246 ArchSpec spec; 247 if (objfile_ap->GetArchitecture(spec) && 248 objfile_ap->SetModulesArchitecture(spec)) 249 return objfile_ap.release(); 250 } 251 } 252 } 253 return NULL; 254 } 255 256 257 ObjectFile* 258 ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, 259 DataBufferSP& data_sp, 260 const lldb::ProcessSP &process_sp, 261 lldb::addr_t header_addr) 262 { 263 return NULL; 264 } 265 266 bool 267 ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp, 268 lldb::addr_t data_offset, 269 lldb::addr_t data_length) 270 { 271 if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) 272 { 273 const uint8_t *magic = data_sp->GetBytes() + data_offset; 274 return ELFHeader::MagicBytesMatch(magic); 275 } 276 return false; 277 } 278 279 /* 280 * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c 281 * 282 * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or 283 * code or tables extracted from it, as desired without restriction. 284 */ 285 static uint32_t 286 calc_gnu_debuglink_crc32(const void *buf, size_t size) 287 { 288 static const uint32_t g_crc32_tab[] = 289 { 290 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 291 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 292 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 293 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 294 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 295 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 296 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 297 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 298 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 299 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 300 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 301 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 302 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 303 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 304 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 305 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 306 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 307 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 308 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 309 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 310 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 311 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 312 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 313 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 314 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 315 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 316 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 317 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 318 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 319 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 320 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 321 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 322 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 323 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 324 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 325 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 326 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 327 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 328 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 329 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 330 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 331 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 332 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 333 }; 334 const uint8_t *p = (const uint8_t *)buf; 335 uint32_t crc; 336 337 crc = ~0U; 338 while (size--) 339 crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); 340 return crc ^ ~0U; 341 } 342 343 size_t 344 ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file, 345 lldb::DataBufferSP& data_sp, 346 lldb::offset_t data_offset, 347 lldb::offset_t file_offset, 348 lldb::offset_t length, 349 lldb_private::ModuleSpecList &specs) 350 { 351 const size_t initial_count = specs.GetSize(); 352 353 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) 354 { 355 DataExtractor data; 356 data.SetData(data_sp); 357 elf::ELFHeader header; 358 if (header.Parse(data, &data_offset)) 359 { 360 if (data_sp) 361 { 362 ModuleSpec spec; 363 spec.GetFileSpec() = file; 364 spec.GetArchitecture().SetArchitecture(eArchTypeELF, 365 header.e_machine, 366 LLDB_INVALID_CPUTYPE); 367 if (spec.GetArchitecture().IsValid()) 368 { 369 // We could parse the ABI tag information (in .note, .notes, or .note.ABI-tag) to get the 370 // machine information. However, this info isn't guaranteed to exist or be correct. Details: 371 // http://refspecs.linuxfoundation.org/LSB_1.2.0/gLSB/noteabitag.html 372 // Instead of passing potentially incorrect information down the pipeline, grab 373 // the host information and use it. 374 spec.GetArchitecture().GetTriple().setOSName (Host::GetOSString().GetCString()); 375 spec.GetArchitecture().GetTriple().setVendorName(Host::GetVendorString().GetCString()); 376 377 // Try to get the UUID from the section list. Usually that's at the end, so 378 // map the file in if we don't have it already. 379 size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize; 380 if (section_header_end > data_sp->GetByteSize()) 381 { 382 data_sp = file.MemoryMapFileContents (file_offset, section_header_end); 383 data.SetData(data_sp); 384 } 385 386 uint32_t gnu_debuglink_crc = 0; 387 std::string gnu_debuglink_file; 388 SectionHeaderColl section_headers; 389 lldb_private::UUID &uuid = spec.GetUUID(); 390 GetSectionHeaderInfo(section_headers, data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc); 391 392 if (!uuid.IsValid()) 393 { 394 if (!gnu_debuglink_crc) 395 { 396 // Need to map entire file into memory to calculate the crc. 397 data_sp = file.MemoryMapFileContents (file_offset, SIZE_MAX); 398 data.SetData(data_sp); 399 gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize()); 400 } 401 if (gnu_debuglink_crc) 402 { 403 // Use 4 bytes of crc from the .gnu_debuglink section. 404 uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 }; 405 uuid.SetBytes (uuidt, sizeof(uuidt)); 406 } 407 } 408 409 specs.Append(spec); 410 } 411 } 412 } 413 } 414 415 return specs.GetSize() - initial_count; 416 } 417 418 //------------------------------------------------------------------ 419 // PluginInterface protocol 420 //------------------------------------------------------------------ 421 lldb_private::ConstString 422 ObjectFileELF::GetPluginName() 423 { 424 return GetPluginNameStatic(); 425 } 426 427 uint32_t 428 ObjectFileELF::GetPluginVersion() 429 { 430 return m_plugin_version; 431 } 432 //------------------------------------------------------------------ 433 // ObjectFile protocol 434 //------------------------------------------------------------------ 435 436 ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, 437 DataBufferSP& data_sp, 438 lldb::offset_t data_offset, 439 const FileSpec* file, 440 lldb::offset_t file_offset, 441 lldb::offset_t length) : 442 ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), 443 m_header(), 444 m_program_headers(), 445 m_section_headers(), 446 m_filespec_ap() 447 { 448 if (file) 449 m_file = *file; 450 ::memset(&m_header, 0, sizeof(m_header)); 451 m_gnu_debuglink_crc = 0; 452 m_gnu_debuglink_file.clear(); 453 } 454 455 ObjectFileELF::~ObjectFileELF() 456 { 457 } 458 459 bool 460 ObjectFileELF::IsExecutable() const 461 { 462 return m_header.e_entry != 0; 463 } 464 465 ByteOrder 466 ObjectFileELF::GetByteOrder() const 467 { 468 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) 469 return eByteOrderBig; 470 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) 471 return eByteOrderLittle; 472 return eByteOrderInvalid; 473 } 474 475 uint32_t 476 ObjectFileELF::GetAddressByteSize() const 477 { 478 return m_data.GetAddressByteSize(); 479 } 480 481 size_t 482 ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) 483 { 484 return std::distance(m_section_headers.begin(), I) + 1u; 485 } 486 487 size_t 488 ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const 489 { 490 return std::distance(m_section_headers.begin(), I) + 1u; 491 } 492 493 bool 494 ObjectFileELF::ParseHeader() 495 { 496 lldb::offset_t offset = 0; 497 return m_header.Parse(m_data, &offset); 498 } 499 500 bool 501 ObjectFileELF::GetUUID(lldb_private::UUID* uuid) 502 { 503 // Need to parse the section list to get the UUIDs, so make sure that's been done. 504 if (!ParseSectionHeaders()) 505 return false; 506 507 if (m_uuid.IsValid()) 508 { 509 // We have the full build id uuid. 510 *uuid = m_uuid; 511 return true; 512 } 513 else 514 { 515 if (!m_gnu_debuglink_crc) 516 m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize()); 517 if (m_gnu_debuglink_crc) 518 { 519 // Use 4 bytes of crc from the .gnu_debuglink section. 520 uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 }; 521 uuid->SetBytes (uuidt, sizeof(uuidt)); 522 return true; 523 } 524 } 525 526 return false; 527 } 528 529 lldb_private::FileSpecList 530 ObjectFileELF::GetDebugSymbolFilePaths() 531 { 532 FileSpecList file_spec_list; 533 534 if (!m_gnu_debuglink_file.empty()) 535 { 536 FileSpec file_spec (m_gnu_debuglink_file.c_str(), false); 537 file_spec_list.Append (file_spec); 538 } 539 return file_spec_list; 540 } 541 542 uint32_t 543 ObjectFileELF::GetDependentModules(FileSpecList &files) 544 { 545 size_t num_modules = ParseDependentModules(); 546 uint32_t num_specs = 0; 547 548 for (unsigned i = 0; i < num_modules; ++i) 549 { 550 if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) 551 num_specs++; 552 } 553 554 return num_specs; 555 } 556 557 Address 558 ObjectFileELF::GetImageInfoAddress(Target *target) 559 { 560 if (!ParseDynamicSymbols()) 561 return Address(); 562 563 SectionList *section_list = GetSectionList(); 564 if (!section_list) 565 return Address(); 566 567 // Find the SHT_DYNAMIC (.dynamic) section. 568 SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true)); 569 if (!dynsym_section_sp) 570 return Address(); 571 assert (dynsym_section_sp->GetObjectFile() == this); 572 573 user_id_t dynsym_id = dynsym_section_sp->GetID(); 574 const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); 575 if (!dynsym_hdr) 576 return Address(); 577 578 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) 579 { 580 ELFDynamic &symbol = m_dynamic_symbols[i]; 581 582 if (symbol.d_tag == DT_DEBUG) 583 { 584 // Compute the offset as the number of previous entries plus the 585 // size of d_tag. 586 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 587 return Address(dynsym_section_sp, offset); 588 } 589 else if (symbol.d_tag == DT_MIPS_RLD_MAP && target) 590 { 591 addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); 592 addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target); 593 if (dyn_base == LLDB_INVALID_ADDRESS) 594 return Address(); 595 Address addr; 596 Error error; 597 if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr)) 598 return addr; 599 } 600 } 601 602 return Address(); 603 } 604 605 lldb_private::Address 606 ObjectFileELF::GetEntryPointAddress () 607 { 608 if (m_entry_point_address.IsValid()) 609 return m_entry_point_address; 610 611 if (!ParseHeader() || !IsExecutable()) 612 return m_entry_point_address; 613 614 SectionList *section_list = GetSectionList(); 615 addr_t offset = m_header.e_entry; 616 617 if (!section_list) 618 m_entry_point_address.SetOffset(offset); 619 else 620 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); 621 return m_entry_point_address; 622 } 623 624 //---------------------------------------------------------------------- 625 // ParseDependentModules 626 //---------------------------------------------------------------------- 627 size_t 628 ObjectFileELF::ParseDependentModules() 629 { 630 if (m_filespec_ap.get()) 631 return m_filespec_ap->GetSize(); 632 633 m_filespec_ap.reset(new FileSpecList()); 634 635 if (!ParseSectionHeaders()) 636 return 0; 637 638 SectionList *section_list = GetSectionList(); 639 if (!section_list) 640 return 0; 641 642 // Find the SHT_DYNAMIC section. 643 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); 644 if (!dynsym) 645 return 0; 646 assert (dynsym->GetObjectFile() == this); 647 648 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID()); 649 if (!header) 650 return 0; 651 // sh_link: section header index of string table used by entries in the section. 652 Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get(); 653 if (!dynstr) 654 return 0; 655 656 DataExtractor dynsym_data; 657 DataExtractor dynstr_data; 658 if (ReadSectionData(dynsym, dynsym_data) && 659 ReadSectionData(dynstr, dynstr_data)) 660 { 661 ELFDynamic symbol; 662 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 663 lldb::offset_t offset = 0; 664 665 // The only type of entries we are concerned with are tagged DT_NEEDED, 666 // yielding the name of a required library. 667 while (offset < section_size) 668 { 669 if (!symbol.Parse(dynsym_data, &offset)) 670 break; 671 672 if (symbol.d_tag != DT_NEEDED) 673 continue; 674 675 uint32_t str_index = static_cast<uint32_t>(symbol.d_val); 676 const char *lib_name = dynstr_data.PeekCStr(str_index); 677 m_filespec_ap->Append(FileSpec(lib_name, true)); 678 } 679 } 680 681 return m_filespec_ap->GetSize(); 682 } 683 684 //---------------------------------------------------------------------- 685 // ParseProgramHeaders 686 //---------------------------------------------------------------------- 687 size_t 688 ObjectFileELF::ParseProgramHeaders() 689 { 690 // We have already parsed the program headers 691 if (!m_program_headers.empty()) 692 return m_program_headers.size(); 693 694 // If there are no program headers to read we are done. 695 if (m_header.e_phnum == 0) 696 return 0; 697 698 m_program_headers.resize(m_header.e_phnum); 699 if (m_program_headers.size() != m_header.e_phnum) 700 return 0; 701 702 const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; 703 const elf_off ph_offset = m_header.e_phoff; 704 DataExtractor data; 705 if (GetData (ph_offset, ph_size, data) != ph_size) 706 return 0; 707 708 uint32_t idx; 709 lldb::offset_t offset; 710 for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) 711 { 712 if (m_program_headers[idx].Parse(data, &offset) == false) 713 break; 714 } 715 716 if (idx < m_program_headers.size()) 717 m_program_headers.resize(idx); 718 719 return m_program_headers.size(); 720 } 721 722 static bool 723 ParseNoteGNUBuildID(DataExtractor &data, lldb_private::UUID &uuid) 724 { 725 // Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id. 726 // BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId 727 lldb::offset_t offset = 0; 728 static const uint32_t g_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h 729 730 while (true) 731 { 732 ELFNote note = ELFNote(); 733 if (!note.Parse(data, &offset)) 734 return false; 735 736 // 16 bytes is UUID|MD5, 20 bytes is SHA1 737 if (note.n_name == "GNU" && (note.n_type == g_gnu_build_id) && 738 (note.n_descsz == 16 || note.n_descsz == 20)) 739 { 740 uint8_t uuidbuf[20]; 741 if (data.GetU8 (&offset, &uuidbuf, note.n_descsz) == NULL) 742 return false; 743 uuid.SetBytes (uuidbuf, note.n_descsz); 744 return true; 745 } 746 offset += llvm::RoundUpToAlignment(note.n_descsz, 4); 747 } 748 return false; 749 } 750 751 //---------------------------------------------------------------------- 752 // GetSectionHeaderInfo 753 //---------------------------------------------------------------------- 754 size_t 755 ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 756 lldb_private::DataExtractor &object_data, 757 const elf::ELFHeader &header, 758 lldb_private::UUID &uuid, 759 std::string &gnu_debuglink_file, 760 uint32_t &gnu_debuglink_crc) 761 { 762 // We have already parsed the section headers 763 if (!section_headers.empty()) 764 return section_headers.size(); 765 766 // If there are no section headers we are done. 767 if (header.e_shnum == 0) 768 return 0; 769 770 section_headers.resize(header.e_shnum); 771 if (section_headers.size() != header.e_shnum) 772 return 0; 773 774 const size_t sh_size = header.e_shnum * header.e_shentsize; 775 const elf_off sh_offset = header.e_shoff; 776 DataExtractor sh_data; 777 if (sh_data.SetData (object_data, sh_offset, sh_size) != sh_size) 778 return 0; 779 780 uint32_t idx; 781 lldb::offset_t offset; 782 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) 783 { 784 if (section_headers[idx].Parse(sh_data, &offset) == false) 785 break; 786 } 787 if (idx < section_headers.size()) 788 section_headers.resize(idx); 789 790 const unsigned strtab_idx = header.e_shstrndx; 791 if (strtab_idx && strtab_idx < section_headers.size()) 792 { 793 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; 794 const size_t byte_size = sheader.sh_size; 795 const Elf64_Off offset = sheader.sh_offset; 796 lldb_private::DataExtractor shstr_data; 797 798 if (shstr_data.SetData (object_data, offset, byte_size) == byte_size) 799 { 800 for (SectionHeaderCollIter I = section_headers.begin(); 801 I != section_headers.end(); ++I) 802 { 803 static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink"); 804 const ELFSectionHeaderInfo &header = *I; 805 const uint64_t section_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 806 ConstString name(shstr_data.PeekCStr(I->sh_name)); 807 808 I->section_name = name; 809 810 if (name == g_sect_name_gnu_debuglink) 811 { 812 DataExtractor data; 813 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size)) 814 { 815 lldb::offset_t gnu_debuglink_offset = 0; 816 gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset); 817 gnu_debuglink_offset = llvm::RoundUpToAlignment (gnu_debuglink_offset, 4); 818 data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 819 } 820 } 821 822 if (header.sh_type == SHT_NOTE && !uuid.IsValid()) 823 { 824 DataExtractor data; 825 if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size)) 826 { 827 ParseNoteGNUBuildID (data, uuid); 828 } 829 } 830 } 831 832 return section_headers.size(); 833 } 834 } 835 836 section_headers.clear(); 837 return 0; 838 } 839 840 size_t 841 ObjectFileELF::GetProgramHeaderCount() 842 { 843 return ParseProgramHeaders(); 844 } 845 846 const elf::ELFProgramHeader * 847 ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) 848 { 849 if (!id || !ParseProgramHeaders()) 850 return NULL; 851 852 if (--id < m_program_headers.size()) 853 return &m_program_headers[id]; 854 855 return NULL; 856 } 857 858 DataExtractor 859 ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) 860 { 861 const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id); 862 if (segment_header == NULL) 863 return DataExtractor(); 864 return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz); 865 } 866 867 //---------------------------------------------------------------------- 868 // ParseSectionHeaders 869 //---------------------------------------------------------------------- 870 size_t 871 ObjectFileELF::ParseSectionHeaders() 872 { 873 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc); 874 } 875 876 const ObjectFileELF::ELFSectionHeaderInfo * 877 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) 878 { 879 if (!id || !ParseSectionHeaders()) 880 return NULL; 881 882 if (--id < m_section_headers.size()) 883 return &m_section_headers[id]; 884 885 return NULL; 886 } 887 888 void 889 ObjectFileELF::CreateSections(SectionList &unified_section_list) 890 { 891 if (!m_sections_ap.get() && ParseSectionHeaders()) 892 { 893 m_sections_ap.reset(new SectionList()); 894 895 for (SectionHeaderCollIter I = m_section_headers.begin(); 896 I != m_section_headers.end(); ++I) 897 { 898 const ELFSectionHeaderInfo &header = *I; 899 900 ConstString& name = I->section_name; 901 const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 902 const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; 903 904 static ConstString g_sect_name_text (".text"); 905 static ConstString g_sect_name_data (".data"); 906 static ConstString g_sect_name_bss (".bss"); 907 static ConstString g_sect_name_tdata (".tdata"); 908 static ConstString g_sect_name_tbss (".tbss"); 909 static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); 910 static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); 911 static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); 912 static ConstString g_sect_name_dwarf_debug_info (".debug_info"); 913 static ConstString g_sect_name_dwarf_debug_line (".debug_line"); 914 static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); 915 static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); 916 static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); 917 static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); 918 static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); 919 static ConstString g_sect_name_dwarf_debug_str (".debug_str"); 920 static ConstString g_sect_name_eh_frame (".eh_frame"); 921 922 SectionType sect_type = eSectionTypeOther; 923 924 bool is_thread_specific = false; 925 926 if (name == g_sect_name_text) sect_type = eSectionTypeCode; 927 else if (name == g_sect_name_data) sect_type = eSectionTypeData; 928 else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; 929 else if (name == g_sect_name_tdata) 930 { 931 sect_type = eSectionTypeData; 932 is_thread_specific = true; 933 } 934 else if (name == g_sect_name_tbss) 935 { 936 sect_type = eSectionTypeZeroFill; 937 is_thread_specific = true; 938 } 939 // .debug_abbrev – Abbreviations used in the .debug_info section 940 // .debug_aranges – Lookup table for mapping addresses to compilation units 941 // .debug_frame – Call frame information 942 // .debug_info – The core DWARF information section 943 // .debug_line – Line number information 944 // .debug_loc – Location lists used in DW_AT_location attributes 945 // .debug_macinfo – Macro information 946 // .debug_pubnames – Lookup table for mapping object and function names to compilation units 947 // .debug_pubtypes – Lookup table for mapping type names to compilation units 948 // .debug_ranges – Address ranges used in DW_AT_ranges attributes 949 // .debug_str – String table used in .debug_info 950 // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html 951 // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644 952 // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo 953 else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; 954 else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; 955 else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; 956 else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; 957 else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; 958 else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; 959 else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; 960 else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; 961 else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; 962 else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; 963 else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; 964 else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; 965 966 switch (header.sh_type) 967 { 968 case SHT_SYMTAB: 969 assert (sect_type == eSectionTypeOther); 970 sect_type = eSectionTypeELFSymbolTable; 971 break; 972 case SHT_DYNSYM: 973 assert (sect_type == eSectionTypeOther); 974 sect_type = eSectionTypeELFDynamicSymbols; 975 break; 976 case SHT_RELA: 977 case SHT_REL: 978 assert (sect_type == eSectionTypeOther); 979 sect_type = eSectionTypeELFRelocationEntries; 980 break; 981 case SHT_DYNAMIC: 982 assert (sect_type == eSectionTypeOther); 983 sect_type = eSectionTypeELFDynamicLinkInfo; 984 break; 985 } 986 987 SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs. 988 this, // ObjectFile to which this section belongs and should read section data from. 989 SectionIndex(I), // Section ID. 990 name, // Section name. 991 sect_type, // Section type. 992 header.sh_addr, // VM address. 993 vm_size, // VM size in bytes of this section. 994 header.sh_offset, // Offset of this section in the file. 995 file_size, // Size of the section as found in the file. 996 header.sh_flags)); // Flags for this section. 997 998 if (is_thread_specific) 999 section_sp->SetIsThreadSpecific (is_thread_specific); 1000 m_sections_ap->AddSection(section_sp); 1001 } 1002 } 1003 1004 if (m_sections_ap.get()) 1005 { 1006 if (GetType() == eTypeDebugInfo) 1007 { 1008 static const SectionType g_sections[] = 1009 { 1010 eSectionTypeDWARFDebugAranges, 1011 eSectionTypeDWARFDebugInfo, 1012 eSectionTypeDWARFDebugAbbrev, 1013 eSectionTypeDWARFDebugFrame, 1014 eSectionTypeDWARFDebugLine, 1015 eSectionTypeDWARFDebugStr, 1016 eSectionTypeDWARFDebugLoc, 1017 eSectionTypeDWARFDebugMacInfo, 1018 eSectionTypeDWARFDebugPubNames, 1019 eSectionTypeDWARFDebugPubTypes, 1020 eSectionTypeDWARFDebugRanges, 1021 eSectionTypeELFSymbolTable, 1022 }; 1023 SectionList *elf_section_list = m_sections_ap.get(); 1024 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx) 1025 { 1026 SectionType section_type = g_sections[idx]; 1027 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true)); 1028 if (section_sp) 1029 { 1030 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true)); 1031 if (module_section_sp) 1032 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp); 1033 else 1034 unified_section_list.AddSection (section_sp); 1035 } 1036 } 1037 } 1038 else 1039 { 1040 unified_section_list = *m_sections_ap; 1041 } 1042 } 1043 } 1044 1045 // private 1046 unsigned 1047 ObjectFileELF::ParseSymbols (Symtab *symtab, 1048 user_id_t start_id, 1049 SectionList *section_list, 1050 const size_t num_symbols, 1051 const DataExtractor &symtab_data, 1052 const DataExtractor &strtab_data) 1053 { 1054 ELFSymbol symbol; 1055 lldb::offset_t offset = 0; 1056 1057 static ConstString text_section_name(".text"); 1058 static ConstString init_section_name(".init"); 1059 static ConstString fini_section_name(".fini"); 1060 static ConstString ctors_section_name(".ctors"); 1061 static ConstString dtors_section_name(".dtors"); 1062 1063 static ConstString data_section_name(".data"); 1064 static ConstString rodata_section_name(".rodata"); 1065 static ConstString rodata1_section_name(".rodata1"); 1066 static ConstString data2_section_name(".data1"); 1067 static ConstString bss_section_name(".bss"); 1068 1069 //StreamFile strm(stdout, false); 1070 unsigned i; 1071 for (i = 0; i < num_symbols; ++i) 1072 { 1073 if (symbol.Parse(symtab_data, &offset) == false) 1074 break; 1075 1076 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 1077 1078 // No need to add symbols that have no names 1079 if (symbol_name == NULL || symbol_name[0] == '\0') 1080 continue; 1081 1082 //symbol.Dump (&strm, i, &strtab_data, section_list); 1083 1084 SectionSP symbol_section_sp; 1085 SymbolType symbol_type = eSymbolTypeInvalid; 1086 Elf64_Half symbol_idx = symbol.st_shndx; 1087 1088 switch (symbol_idx) 1089 { 1090 case SHN_ABS: 1091 symbol_type = eSymbolTypeAbsolute; 1092 break; 1093 case SHN_UNDEF: 1094 symbol_type = eSymbolTypeUndefined; 1095 break; 1096 default: 1097 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); 1098 break; 1099 } 1100 1101 // If a symbol is undefined do not process it further even if it has a STT type 1102 if (symbol_type != eSymbolTypeUndefined) 1103 { 1104 switch (symbol.getType()) 1105 { 1106 default: 1107 case STT_NOTYPE: 1108 // The symbol's type is not specified. 1109 break; 1110 1111 case STT_OBJECT: 1112 // The symbol is associated with a data object, such as a variable, 1113 // an array, etc. 1114 symbol_type = eSymbolTypeData; 1115 break; 1116 1117 case STT_FUNC: 1118 // The symbol is associated with a function or other executable code. 1119 symbol_type = eSymbolTypeCode; 1120 break; 1121 1122 case STT_SECTION: 1123 // The symbol is associated with a section. Symbol table entries of 1124 // this type exist primarily for relocation and normally have 1125 // STB_LOCAL binding. 1126 break; 1127 1128 case STT_FILE: 1129 // Conventionally, the symbol's name gives the name of the source 1130 // file associated with the object file. A file symbol has STB_LOCAL 1131 // binding, its section index is SHN_ABS, and it precedes the other 1132 // STB_LOCAL symbols for the file, if it is present. 1133 symbol_type = eSymbolTypeSourceFile; 1134 break; 1135 1136 case STT_GNU_IFUNC: 1137 // The symbol is associated with an indirect function. The actual 1138 // function will be resolved if it is referenced. 1139 symbol_type = eSymbolTypeResolver; 1140 break; 1141 } 1142 } 1143 1144 if (symbol_type == eSymbolTypeInvalid) 1145 { 1146 if (symbol_section_sp) 1147 { 1148 const ConstString §_name = symbol_section_sp->GetName(); 1149 if (sect_name == text_section_name || 1150 sect_name == init_section_name || 1151 sect_name == fini_section_name || 1152 sect_name == ctors_section_name || 1153 sect_name == dtors_section_name) 1154 { 1155 symbol_type = eSymbolTypeCode; 1156 } 1157 else if (sect_name == data_section_name || 1158 sect_name == data2_section_name || 1159 sect_name == rodata_section_name || 1160 sect_name == rodata1_section_name || 1161 sect_name == bss_section_name) 1162 { 1163 symbol_type = eSymbolTypeData; 1164 } 1165 } 1166 } 1167 1168 // If the symbol section we've found has no data (SHT_NOBITS), then check the module section 1169 // list. This can happen if we're parsing the debug file and it has no .text section, for example. 1170 if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0)) 1171 { 1172 ModuleSP module_sp(GetModule()); 1173 if (module_sp) 1174 { 1175 SectionList *module_section_list = module_sp->GetSectionList(); 1176 if (module_section_list && module_section_list != section_list) 1177 { 1178 const ConstString §_name = symbol_section_sp->GetName(); 1179 lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name)); 1180 if (section_sp && section_sp->GetFileSize()) 1181 { 1182 symbol_section_sp = section_sp; 1183 } 1184 } 1185 } 1186 } 1187 1188 uint64_t symbol_value = symbol.st_value; 1189 if (symbol_section_sp) 1190 symbol_value -= symbol_section_sp->GetFileAddress(); 1191 bool is_global = symbol.getBinding() == STB_GLOBAL; 1192 uint32_t flags = symbol.st_other << 8 | symbol.st_info; 1193 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 1194 Symbol dc_symbol( 1195 i + start_id, // ID is the original symbol table index. 1196 symbol_name, // Symbol name. 1197 is_mangled, // Is the symbol name mangled? 1198 symbol_type, // Type of this symbol 1199 is_global, // Is this globally visible? 1200 false, // Is this symbol debug info? 1201 false, // Is this symbol a trampoline? 1202 false, // Is this symbol artificial? 1203 symbol_section_sp, // Section in which this symbol is defined or null. 1204 symbol_value, // Offset in section or symbol value. 1205 symbol.st_size, // Size in bytes of this symbol. 1206 true, // Size is valid 1207 flags); // Symbol flags. 1208 symtab->AddSymbol(dc_symbol); 1209 } 1210 1211 return i; 1212 } 1213 1214 unsigned 1215 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab) 1216 { 1217 if (symtab->GetObjectFile() != this) 1218 { 1219 // If the symbol table section is owned by a different object file, have it do the 1220 // parsing. 1221 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 1222 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab); 1223 } 1224 1225 // Get section list for this object file. 1226 SectionList *section_list = m_sections_ap.get(); 1227 if (!section_list) 1228 return 0; 1229 1230 user_id_t symtab_id = symtab->GetID(); 1231 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 1232 assert(symtab_hdr->sh_type == SHT_SYMTAB || 1233 symtab_hdr->sh_type == SHT_DYNSYM); 1234 1235 // sh_link: section header index of associated string table. 1236 // Section ID's are ones based. 1237 user_id_t strtab_id = symtab_hdr->sh_link + 1; 1238 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 1239 1240 unsigned num_symbols = 0; 1241 if (symtab && strtab) 1242 { 1243 assert (symtab->GetObjectFile() == this); 1244 assert (strtab->GetObjectFile() == this); 1245 1246 DataExtractor symtab_data; 1247 DataExtractor strtab_data; 1248 if (ReadSectionData(symtab, symtab_data) && 1249 ReadSectionData(strtab, strtab_data)) 1250 { 1251 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 1252 1253 num_symbols = ParseSymbols(symbol_table, start_id, 1254 section_list, num_symbols, 1255 symtab_data, strtab_data); 1256 } 1257 } 1258 1259 return num_symbols; 1260 } 1261 1262 size_t 1263 ObjectFileELF::ParseDynamicSymbols() 1264 { 1265 if (m_dynamic_symbols.size()) 1266 return m_dynamic_symbols.size(); 1267 1268 SectionList *section_list = GetSectionList(); 1269 if (!section_list) 1270 return 0; 1271 1272 // Find the SHT_DYNAMIC section. 1273 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); 1274 if (!dynsym) 1275 return 0; 1276 assert (dynsym->GetObjectFile() == this); 1277 1278 ELFDynamic symbol; 1279 DataExtractor dynsym_data; 1280 if (ReadSectionData(dynsym, dynsym_data)) 1281 { 1282 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 1283 lldb::offset_t cursor = 0; 1284 1285 while (cursor < section_size) 1286 { 1287 if (!symbol.Parse(dynsym_data, &cursor)) 1288 break; 1289 1290 m_dynamic_symbols.push_back(symbol); 1291 } 1292 } 1293 1294 return m_dynamic_symbols.size(); 1295 } 1296 1297 const ELFDynamic * 1298 ObjectFileELF::FindDynamicSymbol(unsigned tag) 1299 { 1300 if (!ParseDynamicSymbols()) 1301 return NULL; 1302 1303 DynamicSymbolCollIter I = m_dynamic_symbols.begin(); 1304 DynamicSymbolCollIter E = m_dynamic_symbols.end(); 1305 for ( ; I != E; ++I) 1306 { 1307 ELFDynamic *symbol = &*I; 1308 1309 if (symbol->d_tag == tag) 1310 return symbol; 1311 } 1312 1313 return NULL; 1314 } 1315 1316 unsigned 1317 ObjectFileELF::PLTRelocationType() 1318 { 1319 // DT_PLTREL 1320 // This member specifies the type of relocation entry to which the 1321 // procedure linkage table refers. The d_val member holds DT_REL or 1322 // DT_RELA, as appropriate. All relocations in a procedure linkage table 1323 // must use the same relocation. 1324 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 1325 1326 if (symbol) 1327 return symbol->d_val; 1328 1329 return 0; 1330 } 1331 1332 static unsigned 1333 ParsePLTRelocations(Symtab *symbol_table, 1334 user_id_t start_id, 1335 unsigned rel_type, 1336 const ELFHeader *hdr, 1337 const ELFSectionHeader *rel_hdr, 1338 const ELFSectionHeader *plt_hdr, 1339 const ELFSectionHeader *sym_hdr, 1340 const lldb::SectionSP &plt_section_sp, 1341 DataExtractor &rel_data, 1342 DataExtractor &symtab_data, 1343 DataExtractor &strtab_data) 1344 { 1345 ELFRelocation rel(rel_type); 1346 ELFSymbol symbol; 1347 lldb::offset_t offset = 0; 1348 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes. 1349 // So round the entsize up by the alignment if addralign is set. 1350 const elf_xword plt_entsize = plt_hdr->sh_addralign ? 1351 llvm::RoundUpToAlignment (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize; 1352 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 1353 1354 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 1355 reloc_info_fn reloc_type; 1356 reloc_info_fn reloc_symbol; 1357 1358 if (hdr->Is32Bit()) 1359 { 1360 reloc_type = ELFRelocation::RelocType32; 1361 reloc_symbol = ELFRelocation::RelocSymbol32; 1362 } 1363 else 1364 { 1365 reloc_type = ELFRelocation::RelocType64; 1366 reloc_symbol = ELFRelocation::RelocSymbol64; 1367 } 1368 1369 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 1370 unsigned i; 1371 for (i = 0; i < num_relocations; ++i) 1372 { 1373 if (rel.Parse(rel_data, &offset) == false) 1374 break; 1375 1376 if (reloc_type(rel) != slot_type) 1377 continue; 1378 1379 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 1380 uint64_t plt_index = (i + 1) * plt_entsize; 1381 1382 if (!symbol.Parse(symtab_data, &symbol_offset)) 1383 break; 1384 1385 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 1386 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 1387 1388 Symbol jump_symbol( 1389 i + start_id, // Symbol table index 1390 symbol_name, // symbol name. 1391 is_mangled, // is the symbol name mangled? 1392 eSymbolTypeTrampoline, // Type of this symbol 1393 false, // Is this globally visible? 1394 false, // Is this symbol debug info? 1395 true, // Is this symbol a trampoline? 1396 true, // Is this symbol artificial? 1397 plt_section_sp, // Section in which this symbol is defined or null. 1398 plt_index, // Offset in section or symbol value. 1399 plt_entsize, // Size in bytes of this symbol. 1400 true, // Size is valid 1401 0); // Symbol flags. 1402 1403 symbol_table->AddSymbol(jump_symbol); 1404 } 1405 1406 return i; 1407 } 1408 1409 unsigned 1410 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, 1411 user_id_t start_id, 1412 const ELFSectionHeaderInfo *rel_hdr, 1413 user_id_t rel_id) 1414 { 1415 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 1416 1417 // The link field points to the associated symbol table. The info field 1418 // points to the section holding the plt. 1419 user_id_t symtab_id = rel_hdr->sh_link; 1420 user_id_t plt_id = rel_hdr->sh_info; 1421 1422 if (!symtab_id || !plt_id) 1423 return 0; 1424 1425 // Section ID's are ones based; 1426 symtab_id++; 1427 plt_id++; 1428 1429 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 1430 if (!plt_hdr) 1431 return 0; 1432 1433 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 1434 if (!sym_hdr) 1435 return 0; 1436 1437 SectionList *section_list = m_sections_ap.get(); 1438 if (!section_list) 1439 return 0; 1440 1441 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 1442 if (!rel_section) 1443 return 0; 1444 1445 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id)); 1446 if (!plt_section_sp) 1447 return 0; 1448 1449 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 1450 if (!symtab) 1451 return 0; 1452 1453 // sh_link points to associated string table. 1454 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); 1455 if (!strtab) 1456 return 0; 1457 1458 DataExtractor rel_data; 1459 if (!ReadSectionData(rel_section, rel_data)) 1460 return 0; 1461 1462 DataExtractor symtab_data; 1463 if (!ReadSectionData(symtab, symtab_data)) 1464 return 0; 1465 1466 DataExtractor strtab_data; 1467 if (!ReadSectionData(strtab, strtab_data)) 1468 return 0; 1469 1470 unsigned rel_type = PLTRelocationType(); 1471 if (!rel_type) 1472 return 0; 1473 1474 return ParsePLTRelocations (symbol_table, 1475 start_id, 1476 rel_type, 1477 &m_header, 1478 rel_hdr, 1479 plt_hdr, 1480 sym_hdr, 1481 plt_section_sp, 1482 rel_data, 1483 symtab_data, 1484 strtab_data); 1485 } 1486 1487 Symtab * 1488 ObjectFileELF::GetSymtab() 1489 { 1490 ModuleSP module_sp(GetModule()); 1491 if (!module_sp) 1492 return NULL; 1493 1494 // We always want to use the main object file so we (hopefully) only have one cached copy 1495 // of our symtab, dynamic sections, etc. 1496 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 1497 if (module_obj_file && module_obj_file != this) 1498 return module_obj_file->GetSymtab(); 1499 1500 if (m_symtab_ap.get() == NULL) 1501 { 1502 SectionList *section_list = GetSectionList(); 1503 if (!section_list) 1504 return NULL; 1505 1506 uint64_t symbol_id = 0; 1507 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 1508 1509 m_symtab_ap.reset(new Symtab(this)); 1510 1511 // Sharable objects and dynamic executables usually have 2 distinct symbol 1512 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller 1513 // version of the symtab that only contains global symbols. The information found 1514 // in the dynsym is therefore also found in the symtab, while the reverse is not 1515 // necessarily true. 1516 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get(); 1517 if (!symtab) 1518 { 1519 // The symtab section is non-allocable and can be stripped, so if it doesn't exist 1520 // then use the dynsym section which should always be there. 1521 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get(); 1522 } 1523 if (symtab) 1524 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab); 1525 1526 // DT_JMPREL 1527 // If present, this entry's d_ptr member holds the address of relocation 1528 // entries associated solely with the procedure linkage table. Separating 1529 // these relocation entries lets the dynamic linker ignore them during 1530 // process initialization, if lazy binding is enabled. If this entry is 1531 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 1532 // also be present. 1533 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 1534 if (symbol) 1535 { 1536 // Synthesize trampoline symbols to help navigate the PLT. 1537 addr_t addr = symbol->d_ptr; 1538 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get(); 1539 if (reloc_section) 1540 { 1541 user_id_t reloc_id = reloc_section->GetID(); 1542 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id); 1543 assert(reloc_header); 1544 1545 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id); 1546 } 1547 } 1548 } 1549 return m_symtab_ap.get(); 1550 } 1551 1552 Symbol * 1553 ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique) 1554 { 1555 if (!m_symtab_ap.get()) 1556 return nullptr; // GetSymtab() should be called first. 1557 1558 const SectionList *section_list = GetSectionList(); 1559 if (!section_list) 1560 return nullptr; 1561 1562 if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo()) 1563 { 1564 AddressRange range; 1565 if (eh_frame->GetAddressRange (so_addr, range)) 1566 { 1567 const addr_t file_addr = range.GetBaseAddress().GetFileAddress(); 1568 Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr; 1569 if (symbol) 1570 return symbol; 1571 1572 // Note that a (stripped) symbol won't be found by GetSymtab()... 1573 lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr); 1574 if (eh_sym_section_sp.get()) 1575 { 1576 addr_t section_base = eh_sym_section_sp->GetFileAddress(); 1577 addr_t offset = file_addr - section_base; 1578 uint64_t symbol_id = m_symtab_ap->GetNumSymbols(); 1579 1580 Symbol eh_symbol( 1581 symbol_id, // Symbol table index. 1582 "???", // Symbol name. 1583 false, // Is the symbol name mangled? 1584 eSymbolTypeCode, // Type of this symbol. 1585 true, // Is this globally visible? 1586 false, // Is this symbol debug info? 1587 false, // Is this symbol a trampoline? 1588 true, // Is this symbol artificial? 1589 eh_sym_section_sp, // Section in which this symbol is defined or null. 1590 offset, // Offset in section or symbol value. 1591 range.GetByteSize(), // Size in bytes of this symbol. 1592 true, // Size is valid. 1593 0); // Symbol flags. 1594 if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol)) 1595 return m_symtab_ap->SymbolAtIndex(symbol_id); 1596 } 1597 } 1598 } 1599 return nullptr; 1600 } 1601 1602 1603 bool 1604 ObjectFileELF::IsStripped () 1605 { 1606 // TODO: determine this for ELF 1607 return false; 1608 } 1609 1610 //===----------------------------------------------------------------------===// 1611 // Dump 1612 // 1613 // Dump the specifics of the runtime file container (such as any headers 1614 // segments, sections, etc). 1615 //---------------------------------------------------------------------- 1616 void 1617 ObjectFileELF::Dump(Stream *s) 1618 { 1619 DumpELFHeader(s, m_header); 1620 s->EOL(); 1621 DumpELFProgramHeaders(s); 1622 s->EOL(); 1623 DumpELFSectionHeaders(s); 1624 s->EOL(); 1625 SectionList *section_list = GetSectionList(); 1626 if (section_list) 1627 section_list->Dump(s, NULL, true, UINT32_MAX); 1628 Symtab *symtab = GetSymtab(); 1629 if (symtab) 1630 symtab->Dump(s, NULL, eSortOrderNone); 1631 s->EOL(); 1632 DumpDependentModules(s); 1633 s->EOL(); 1634 } 1635 1636 //---------------------------------------------------------------------- 1637 // DumpELFHeader 1638 // 1639 // Dump the ELF header to the specified output stream 1640 //---------------------------------------------------------------------- 1641 void 1642 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) 1643 { 1644 s->PutCString("ELF Header\n"); 1645 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 1646 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", 1647 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); 1648 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", 1649 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); 1650 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", 1651 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); 1652 1653 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 1654 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 1655 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 1656 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 1657 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 1658 1659 s->Printf("e_type = 0x%4.4x ", header.e_type); 1660 DumpELFHeader_e_type(s, header.e_type); 1661 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 1662 s->Printf("e_version = 0x%8.8x\n", header.e_version); 1663 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 1664 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 1665 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 1666 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 1667 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 1668 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 1669 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); 1670 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 1671 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); 1672 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); 1673 } 1674 1675 //---------------------------------------------------------------------- 1676 // DumpELFHeader_e_type 1677 // 1678 // Dump an token value for the ELF header member e_type 1679 //---------------------------------------------------------------------- 1680 void 1681 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) 1682 { 1683 switch (e_type) 1684 { 1685 case ET_NONE: *s << "ET_NONE"; break; 1686 case ET_REL: *s << "ET_REL"; break; 1687 case ET_EXEC: *s << "ET_EXEC"; break; 1688 case ET_DYN: *s << "ET_DYN"; break; 1689 case ET_CORE: *s << "ET_CORE"; break; 1690 default: 1691 break; 1692 } 1693 } 1694 1695 //---------------------------------------------------------------------- 1696 // DumpELFHeader_e_ident_EI_DATA 1697 // 1698 // Dump an token value for the ELF header member e_ident[EI_DATA] 1699 //---------------------------------------------------------------------- 1700 void 1701 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) 1702 { 1703 switch (ei_data) 1704 { 1705 case ELFDATANONE: *s << "ELFDATANONE"; break; 1706 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; 1707 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; 1708 default: 1709 break; 1710 } 1711 } 1712 1713 1714 //---------------------------------------------------------------------- 1715 // DumpELFProgramHeader 1716 // 1717 // Dump a single ELF program header to the specified output stream 1718 //---------------------------------------------------------------------- 1719 void 1720 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) 1721 { 1722 DumpELFProgramHeader_p_type(s, ph.p_type); 1723 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr); 1724 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags); 1725 1726 DumpELFProgramHeader_p_flags(s, ph.p_flags); 1727 s->Printf(") %8.8" PRIx64, ph.p_align); 1728 } 1729 1730 //---------------------------------------------------------------------- 1731 // DumpELFProgramHeader_p_type 1732 // 1733 // Dump an token value for the ELF program header member p_type which 1734 // describes the type of the program header 1735 // ---------------------------------------------------------------------- 1736 void 1737 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) 1738 { 1739 const int kStrWidth = 15; 1740 switch (p_type) 1741 { 1742 CASE_AND_STREAM(s, PT_NULL , kStrWidth); 1743 CASE_AND_STREAM(s, PT_LOAD , kStrWidth); 1744 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); 1745 CASE_AND_STREAM(s, PT_INTERP , kStrWidth); 1746 CASE_AND_STREAM(s, PT_NOTE , kStrWidth); 1747 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); 1748 CASE_AND_STREAM(s, PT_PHDR , kStrWidth); 1749 CASE_AND_STREAM(s, PT_TLS , kStrWidth); 1750 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 1751 default: 1752 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 1753 break; 1754 } 1755 } 1756 1757 1758 //---------------------------------------------------------------------- 1759 // DumpELFProgramHeader_p_flags 1760 // 1761 // Dump an token value for the ELF program header member p_flags 1762 //---------------------------------------------------------------------- 1763 void 1764 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) 1765 { 1766 *s << ((p_flags & PF_X) ? "PF_X" : " ") 1767 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 1768 << ((p_flags & PF_W) ? "PF_W" : " ") 1769 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 1770 << ((p_flags & PF_R) ? "PF_R" : " "); 1771 } 1772 1773 //---------------------------------------------------------------------- 1774 // DumpELFProgramHeaders 1775 // 1776 // Dump all of the ELF program header to the specified output stream 1777 //---------------------------------------------------------------------- 1778 void 1779 ObjectFileELF::DumpELFProgramHeaders(Stream *s) 1780 { 1781 if (ParseProgramHeaders()) 1782 { 1783 s->PutCString("Program Headers\n"); 1784 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 1785 "p_filesz p_memsz p_flags p_align\n"); 1786 s->PutCString("==== --------------- -------- -------- -------- " 1787 "-------- -------- ------------------------- --------\n"); 1788 1789 uint32_t idx = 0; 1790 for (ProgramHeaderCollConstIter I = m_program_headers.begin(); 1791 I != m_program_headers.end(); ++I, ++idx) 1792 { 1793 s->Printf("[%2u] ", idx); 1794 ObjectFileELF::DumpELFProgramHeader(s, *I); 1795 s->EOL(); 1796 } 1797 } 1798 } 1799 1800 //---------------------------------------------------------------------- 1801 // DumpELFSectionHeader 1802 // 1803 // Dump a single ELF section header to the specified output stream 1804 //---------------------------------------------------------------------- 1805 void 1806 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh) 1807 { 1808 s->Printf("%8.8x ", sh.sh_name); 1809 DumpELFSectionHeader_sh_type(s, sh.sh_type); 1810 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 1811 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 1812 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size); 1813 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 1814 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 1815 } 1816 1817 //---------------------------------------------------------------------- 1818 // DumpELFSectionHeader_sh_type 1819 // 1820 // Dump an token value for the ELF section header member sh_type which 1821 // describes the type of the section 1822 //---------------------------------------------------------------------- 1823 void 1824 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) 1825 { 1826 const int kStrWidth = 12; 1827 switch (sh_type) 1828 { 1829 CASE_AND_STREAM(s, SHT_NULL , kStrWidth); 1830 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); 1831 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); 1832 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); 1833 CASE_AND_STREAM(s, SHT_RELA , kStrWidth); 1834 CASE_AND_STREAM(s, SHT_HASH , kStrWidth); 1835 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); 1836 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); 1837 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); 1838 CASE_AND_STREAM(s, SHT_REL , kStrWidth); 1839 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); 1840 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); 1841 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); 1842 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); 1843 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); 1844 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); 1845 default: 1846 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 1847 break; 1848 } 1849 } 1850 1851 //---------------------------------------------------------------------- 1852 // DumpELFSectionHeader_sh_flags 1853 // 1854 // Dump an token value for the ELF section header member sh_flags 1855 //---------------------------------------------------------------------- 1856 void 1857 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags) 1858 { 1859 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 1860 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 1861 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 1862 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 1863 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 1864 } 1865 1866 //---------------------------------------------------------------------- 1867 // DumpELFSectionHeaders 1868 // 1869 // Dump all of the ELF section header to the specified output stream 1870 //---------------------------------------------------------------------- 1871 void 1872 ObjectFileELF::DumpELFSectionHeaders(Stream *s) 1873 { 1874 if (!ParseSectionHeaders()) 1875 return; 1876 1877 s->PutCString("Section Headers\n"); 1878 s->PutCString("IDX name type flags " 1879 "addr offset size link info addralgn " 1880 "entsize Name\n"); 1881 s->PutCString("==== -------- ------------ -------------------------------- " 1882 "-------- -------- -------- -------- -------- -------- " 1883 "-------- ====================\n"); 1884 1885 uint32_t idx = 0; 1886 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 1887 I != m_section_headers.end(); ++I, ++idx) 1888 { 1889 s->Printf("[%2u] ", idx); 1890 ObjectFileELF::DumpELFSectionHeader(s, *I); 1891 const char* section_name = I->section_name.AsCString(""); 1892 if (section_name) 1893 *s << ' ' << section_name << "\n"; 1894 } 1895 } 1896 1897 void 1898 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) 1899 { 1900 size_t num_modules = ParseDependentModules(); 1901 1902 if (num_modules > 0) 1903 { 1904 s->PutCString("Dependent Modules:\n"); 1905 for (unsigned i = 0; i < num_modules; ++i) 1906 { 1907 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); 1908 s->Printf(" %s\n", spec.GetFilename().GetCString()); 1909 } 1910 } 1911 } 1912 1913 bool 1914 ObjectFileELF::GetArchitecture (ArchSpec &arch) 1915 { 1916 if (!ParseHeader()) 1917 return false; 1918 1919 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE); 1920 arch.GetTriple().setOSName (Host::GetOSString().GetCString()); 1921 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString()); 1922 return true; 1923 } 1924 1925 ObjectFile::Type 1926 ObjectFileELF::CalculateType() 1927 { 1928 switch (m_header.e_type) 1929 { 1930 case llvm::ELF::ET_NONE: 1931 // 0 - No file type 1932 return eTypeUnknown; 1933 1934 case llvm::ELF::ET_REL: 1935 // 1 - Relocatable file 1936 return eTypeObjectFile; 1937 1938 case llvm::ELF::ET_EXEC: 1939 // 2 - Executable file 1940 return eTypeExecutable; 1941 1942 case llvm::ELF::ET_DYN: 1943 // 3 - Shared object file 1944 return eTypeSharedLibrary; 1945 1946 case ET_CORE: 1947 // 4 - Core file 1948 return eTypeCoreFile; 1949 1950 default: 1951 break; 1952 } 1953 return eTypeUnknown; 1954 } 1955 1956 ObjectFile::Strata 1957 ObjectFileELF::CalculateStrata() 1958 { 1959 switch (m_header.e_type) 1960 { 1961 case llvm::ELF::ET_NONE: 1962 // 0 - No file type 1963 return eStrataUnknown; 1964 1965 case llvm::ELF::ET_REL: 1966 // 1 - Relocatable file 1967 return eStrataUnknown; 1968 1969 case llvm::ELF::ET_EXEC: 1970 // 2 - Executable file 1971 // TODO: is there any way to detect that an executable is a kernel 1972 // related executable by inspecting the program headers, section 1973 // headers, symbols, or any other flag bits??? 1974 return eStrataUser; 1975 1976 case llvm::ELF::ET_DYN: 1977 // 3 - Shared object file 1978 // TODO: is there any way to detect that an shared library is a kernel 1979 // related executable by inspecting the program headers, section 1980 // headers, symbols, or any other flag bits??? 1981 return eStrataUnknown; 1982 1983 case ET_CORE: 1984 // 4 - Core file 1985 // TODO: is there any way to detect that an core file is a kernel 1986 // related executable by inspecting the program headers, section 1987 // headers, symbols, or any other flag bits??? 1988 return eStrataUnknown; 1989 1990 default: 1991 break; 1992 } 1993 return eStrataUnknown; 1994 } 1995 1996