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