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