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 __builtin_ffs(header.sh_addralign), // Alignment of the section 1240 header.sh_flags)); // Flags for this section. 1241 1242 if (is_thread_specific) 1243 section_sp->SetIsThreadSpecific (is_thread_specific); 1244 m_sections_ap->AddSection(section_sp); 1245 } 1246 } 1247 1248 if (m_sections_ap.get()) 1249 { 1250 if (GetType() == eTypeDebugInfo) 1251 { 1252 static const SectionType g_sections[] = 1253 { 1254 eSectionTypeDWARFDebugAranges, 1255 eSectionTypeDWARFDebugInfo, 1256 eSectionTypeDWARFDebugAbbrev, 1257 eSectionTypeDWARFDebugFrame, 1258 eSectionTypeDWARFDebugLine, 1259 eSectionTypeDWARFDebugStr, 1260 eSectionTypeDWARFDebugLoc, 1261 eSectionTypeDWARFDebugMacInfo, 1262 eSectionTypeDWARFDebugPubNames, 1263 eSectionTypeDWARFDebugPubTypes, 1264 eSectionTypeDWARFDebugRanges, 1265 eSectionTypeELFSymbolTable, 1266 }; 1267 SectionList *elf_section_list = m_sections_ap.get(); 1268 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx) 1269 { 1270 SectionType section_type = g_sections[idx]; 1271 SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true)); 1272 if (section_sp) 1273 { 1274 SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true)); 1275 if (module_section_sp) 1276 unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp); 1277 else 1278 unified_section_list.AddSection (section_sp); 1279 } 1280 } 1281 } 1282 else 1283 { 1284 unified_section_list = *m_sections_ap; 1285 } 1286 } 1287 } 1288 1289 // private 1290 unsigned 1291 ObjectFileELF::ParseSymbols (Symtab *symtab, 1292 user_id_t start_id, 1293 SectionList *section_list, 1294 const size_t num_symbols, 1295 const DataExtractor &symtab_data, 1296 const DataExtractor &strtab_data) 1297 { 1298 ELFSymbol symbol; 1299 lldb::offset_t offset = 0; 1300 1301 static ConstString text_section_name(".text"); 1302 static ConstString init_section_name(".init"); 1303 static ConstString fini_section_name(".fini"); 1304 static ConstString ctors_section_name(".ctors"); 1305 static ConstString dtors_section_name(".dtors"); 1306 1307 static ConstString data_section_name(".data"); 1308 static ConstString rodata_section_name(".rodata"); 1309 static ConstString rodata1_section_name(".rodata1"); 1310 static ConstString data2_section_name(".data1"); 1311 static ConstString bss_section_name(".bss"); 1312 1313 //StreamFile strm(stdout, false); 1314 unsigned i; 1315 for (i = 0; i < num_symbols; ++i) 1316 { 1317 if (symbol.Parse(symtab_data, &offset) == false) 1318 break; 1319 1320 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 1321 1322 // No need to add non-section symbols that have no names 1323 if (symbol.getType() != STT_SECTION && 1324 (symbol_name == NULL || symbol_name[0] == '\0')) 1325 continue; 1326 1327 //symbol.Dump (&strm, i, &strtab_data, section_list); 1328 1329 SectionSP symbol_section_sp; 1330 SymbolType symbol_type = eSymbolTypeInvalid; 1331 Elf64_Half symbol_idx = symbol.st_shndx; 1332 1333 switch (symbol_idx) 1334 { 1335 case SHN_ABS: 1336 symbol_type = eSymbolTypeAbsolute; 1337 break; 1338 case SHN_UNDEF: 1339 symbol_type = eSymbolTypeUndefined; 1340 break; 1341 default: 1342 symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); 1343 break; 1344 } 1345 1346 // If a symbol is undefined do not process it further even if it has a STT type 1347 if (symbol_type != eSymbolTypeUndefined) 1348 { 1349 switch (symbol.getType()) 1350 { 1351 default: 1352 case STT_NOTYPE: 1353 // The symbol's type is not specified. 1354 break; 1355 1356 case STT_OBJECT: 1357 // The symbol is associated with a data object, such as a variable, 1358 // an array, etc. 1359 symbol_type = eSymbolTypeData; 1360 break; 1361 1362 case STT_FUNC: 1363 // The symbol is associated with a function or other executable code. 1364 symbol_type = eSymbolTypeCode; 1365 break; 1366 1367 case STT_SECTION: 1368 // The symbol is associated with a section. Symbol table entries of 1369 // this type exist primarily for relocation and normally have 1370 // STB_LOCAL binding. 1371 break; 1372 1373 case STT_FILE: 1374 // Conventionally, the symbol's name gives the name of the source 1375 // file associated with the object file. A file symbol has STB_LOCAL 1376 // binding, its section index is SHN_ABS, and it precedes the other 1377 // STB_LOCAL symbols for the file, if it is present. 1378 symbol_type = eSymbolTypeSourceFile; 1379 break; 1380 1381 case STT_GNU_IFUNC: 1382 // The symbol is associated with an indirect function. The actual 1383 // function will be resolved if it is referenced. 1384 symbol_type = eSymbolTypeResolver; 1385 break; 1386 } 1387 } 1388 1389 if (symbol_type == eSymbolTypeInvalid) 1390 { 1391 if (symbol_section_sp) 1392 { 1393 const ConstString §_name = symbol_section_sp->GetName(); 1394 if (sect_name == text_section_name || 1395 sect_name == init_section_name || 1396 sect_name == fini_section_name || 1397 sect_name == ctors_section_name || 1398 sect_name == dtors_section_name) 1399 { 1400 symbol_type = eSymbolTypeCode; 1401 } 1402 else if (sect_name == data_section_name || 1403 sect_name == data2_section_name || 1404 sect_name == rodata_section_name || 1405 sect_name == rodata1_section_name || 1406 sect_name == bss_section_name) 1407 { 1408 symbol_type = eSymbolTypeData; 1409 } 1410 } 1411 } 1412 1413 // If the symbol section we've found has no data (SHT_NOBITS), then check the module section 1414 // list. This can happen if we're parsing the debug file and it has no .text section, for example. 1415 if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0)) 1416 { 1417 ModuleSP module_sp(GetModule()); 1418 if (module_sp) 1419 { 1420 SectionList *module_section_list = module_sp->GetSectionList(); 1421 if (module_section_list && module_section_list != section_list) 1422 { 1423 const ConstString §_name = symbol_section_sp->GetName(); 1424 lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name)); 1425 if (section_sp && section_sp->GetFileSize()) 1426 { 1427 symbol_section_sp = section_sp; 1428 } 1429 } 1430 } 1431 } 1432 1433 uint64_t symbol_value = symbol.st_value; 1434 if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile) 1435 symbol_value -= symbol_section_sp->GetFileAddress(); 1436 bool is_global = symbol.getBinding() == STB_GLOBAL; 1437 uint32_t flags = symbol.st_other << 8 | symbol.st_info; 1438 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 1439 Symbol dc_symbol( 1440 i + start_id, // ID is the original symbol table index. 1441 symbol_name, // Symbol name. 1442 is_mangled, // Is the symbol name mangled? 1443 symbol_type, // Type of this symbol 1444 is_global, // Is this globally visible? 1445 false, // Is this symbol debug info? 1446 false, // Is this symbol a trampoline? 1447 false, // Is this symbol artificial? 1448 symbol_section_sp, // Section in which this symbol is defined or null. 1449 symbol_value, // Offset in section or symbol value. 1450 symbol.st_size, // Size in bytes of this symbol. 1451 true, // Size is valid 1452 flags); // Symbol flags. 1453 symtab->AddSymbol(dc_symbol); 1454 } 1455 1456 return i; 1457 } 1458 1459 unsigned 1460 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab) 1461 { 1462 if (symtab->GetObjectFile() != this) 1463 { 1464 // If the symbol table section is owned by a different object file, have it do the 1465 // parsing. 1466 ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 1467 return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab); 1468 } 1469 1470 // Get section list for this object file. 1471 SectionList *section_list = m_sections_ap.get(); 1472 if (!section_list) 1473 return 0; 1474 1475 user_id_t symtab_id = symtab->GetID(); 1476 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 1477 assert(symtab_hdr->sh_type == SHT_SYMTAB || 1478 symtab_hdr->sh_type == SHT_DYNSYM); 1479 1480 // sh_link: section header index of associated string table. 1481 // Section ID's are ones based. 1482 user_id_t strtab_id = symtab_hdr->sh_link + 1; 1483 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 1484 1485 if (symtab && strtab) 1486 { 1487 assert (symtab->GetObjectFile() == this); 1488 assert (strtab->GetObjectFile() == this); 1489 1490 DataExtractor symtab_data; 1491 DataExtractor strtab_data; 1492 if (ReadSectionData(symtab, symtab_data) && 1493 ReadSectionData(strtab, strtab_data)) 1494 { 1495 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 1496 1497 return ParseSymbols(symbol_table, start_id, section_list, 1498 num_symbols, symtab_data, strtab_data); 1499 } 1500 } 1501 1502 return 0; 1503 } 1504 1505 size_t 1506 ObjectFileELF::ParseDynamicSymbols() 1507 { 1508 if (m_dynamic_symbols.size()) 1509 return m_dynamic_symbols.size(); 1510 1511 SectionList *section_list = GetSectionList(); 1512 if (!section_list) 1513 return 0; 1514 1515 // Find the SHT_DYNAMIC section. 1516 Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); 1517 if (!dynsym) 1518 return 0; 1519 assert (dynsym->GetObjectFile() == this); 1520 1521 ELFDynamic symbol; 1522 DataExtractor dynsym_data; 1523 if (ReadSectionData(dynsym, dynsym_data)) 1524 { 1525 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 1526 lldb::offset_t cursor = 0; 1527 1528 while (cursor < section_size) 1529 { 1530 if (!symbol.Parse(dynsym_data, &cursor)) 1531 break; 1532 1533 m_dynamic_symbols.push_back(symbol); 1534 } 1535 } 1536 1537 return m_dynamic_symbols.size(); 1538 } 1539 1540 const ELFDynamic * 1541 ObjectFileELF::FindDynamicSymbol(unsigned tag) 1542 { 1543 if (!ParseDynamicSymbols()) 1544 return NULL; 1545 1546 DynamicSymbolCollIter I = m_dynamic_symbols.begin(); 1547 DynamicSymbolCollIter E = m_dynamic_symbols.end(); 1548 for ( ; I != E; ++I) 1549 { 1550 ELFDynamic *symbol = &*I; 1551 1552 if (symbol->d_tag == tag) 1553 return symbol; 1554 } 1555 1556 return NULL; 1557 } 1558 1559 unsigned 1560 ObjectFileELF::PLTRelocationType() 1561 { 1562 // DT_PLTREL 1563 // This member specifies the type of relocation entry to which the 1564 // procedure linkage table refers. The d_val member holds DT_REL or 1565 // DT_RELA, as appropriate. All relocations in a procedure linkage table 1566 // must use the same relocation. 1567 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 1568 1569 if (symbol) 1570 return symbol->d_val; 1571 1572 return 0; 1573 } 1574 1575 static unsigned 1576 ParsePLTRelocations(Symtab *symbol_table, 1577 user_id_t start_id, 1578 unsigned rel_type, 1579 const ELFHeader *hdr, 1580 const ELFSectionHeader *rel_hdr, 1581 const ELFSectionHeader *plt_hdr, 1582 const ELFSectionHeader *sym_hdr, 1583 const lldb::SectionSP &plt_section_sp, 1584 DataExtractor &rel_data, 1585 DataExtractor &symtab_data, 1586 DataExtractor &strtab_data) 1587 { 1588 ELFRelocation rel(rel_type); 1589 ELFSymbol symbol; 1590 lldb::offset_t offset = 0; 1591 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes. 1592 // So round the entsize up by the alignment if addralign is set. 1593 const elf_xword plt_entsize = plt_hdr->sh_addralign ? 1594 llvm::RoundUpToAlignment (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize; 1595 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 1596 1597 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 1598 reloc_info_fn reloc_type; 1599 reloc_info_fn reloc_symbol; 1600 1601 if (hdr->Is32Bit()) 1602 { 1603 reloc_type = ELFRelocation::RelocType32; 1604 reloc_symbol = ELFRelocation::RelocSymbol32; 1605 } 1606 else 1607 { 1608 reloc_type = ELFRelocation::RelocType64; 1609 reloc_symbol = ELFRelocation::RelocSymbol64; 1610 } 1611 1612 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 1613 unsigned i; 1614 for (i = 0; i < num_relocations; ++i) 1615 { 1616 if (rel.Parse(rel_data, &offset) == false) 1617 break; 1618 1619 if (reloc_type(rel) != slot_type) 1620 continue; 1621 1622 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 1623 uint64_t plt_index = (i + 1) * plt_entsize; 1624 1625 if (!symbol.Parse(symtab_data, &symbol_offset)) 1626 break; 1627 1628 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 1629 bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; 1630 1631 Symbol jump_symbol( 1632 i + start_id, // Symbol table index 1633 symbol_name, // symbol name. 1634 is_mangled, // is the symbol name mangled? 1635 eSymbolTypeTrampoline, // Type of this symbol 1636 false, // Is this globally visible? 1637 false, // Is this symbol debug info? 1638 true, // Is this symbol a trampoline? 1639 true, // Is this symbol artificial? 1640 plt_section_sp, // Section in which this symbol is defined or null. 1641 plt_index, // Offset in section or symbol value. 1642 plt_entsize, // Size in bytes of this symbol. 1643 true, // Size is valid 1644 0); // Symbol flags. 1645 1646 symbol_table->AddSymbol(jump_symbol); 1647 } 1648 1649 return i; 1650 } 1651 1652 unsigned 1653 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, 1654 user_id_t start_id, 1655 const ELFSectionHeaderInfo *rel_hdr, 1656 user_id_t rel_id) 1657 { 1658 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 1659 1660 // The link field points to the associated symbol table. The info field 1661 // points to the section holding the plt. 1662 user_id_t symtab_id = rel_hdr->sh_link; 1663 user_id_t plt_id = rel_hdr->sh_info; 1664 1665 if (!symtab_id || !plt_id) 1666 return 0; 1667 1668 // Section ID's are ones based; 1669 symtab_id++; 1670 plt_id++; 1671 1672 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 1673 if (!plt_hdr) 1674 return 0; 1675 1676 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 1677 if (!sym_hdr) 1678 return 0; 1679 1680 SectionList *section_list = m_sections_ap.get(); 1681 if (!section_list) 1682 return 0; 1683 1684 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 1685 if (!rel_section) 1686 return 0; 1687 1688 SectionSP plt_section_sp (section_list->FindSectionByID(plt_id)); 1689 if (!plt_section_sp) 1690 return 0; 1691 1692 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 1693 if (!symtab) 1694 return 0; 1695 1696 // sh_link points to associated string table. 1697 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); 1698 if (!strtab) 1699 return 0; 1700 1701 DataExtractor rel_data; 1702 if (!ReadSectionData(rel_section, rel_data)) 1703 return 0; 1704 1705 DataExtractor symtab_data; 1706 if (!ReadSectionData(symtab, symtab_data)) 1707 return 0; 1708 1709 DataExtractor strtab_data; 1710 if (!ReadSectionData(strtab, strtab_data)) 1711 return 0; 1712 1713 unsigned rel_type = PLTRelocationType(); 1714 if (!rel_type) 1715 return 0; 1716 1717 return ParsePLTRelocations (symbol_table, 1718 start_id, 1719 rel_type, 1720 &m_header, 1721 rel_hdr, 1722 plt_hdr, 1723 sym_hdr, 1724 plt_section_sp, 1725 rel_data, 1726 symtab_data, 1727 strtab_data); 1728 } 1729 1730 unsigned 1731 ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 1732 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, 1733 DataExtractor &rel_data, DataExtractor &symtab_data, 1734 DataExtractor &debug_data, Section* rel_section) 1735 { 1736 ELFRelocation rel(rel_hdr->sh_type); 1737 lldb::addr_t offset = 0; 1738 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 1739 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 1740 reloc_info_fn reloc_type; 1741 reloc_info_fn reloc_symbol; 1742 1743 if (hdr->Is32Bit()) 1744 { 1745 reloc_type = ELFRelocation::RelocType32; 1746 reloc_symbol = ELFRelocation::RelocSymbol32; 1747 } 1748 else 1749 { 1750 reloc_type = ELFRelocation::RelocType64; 1751 reloc_symbol = ELFRelocation::RelocSymbol64; 1752 } 1753 1754 for (unsigned i = 0; i < num_relocations; ++i) 1755 { 1756 if (rel.Parse(rel_data, &offset) == false) 1757 break; 1758 1759 Symbol* symbol = NULL; 1760 1761 if (hdr->Is32Bit()) 1762 { 1763 switch (reloc_type(rel)) { 1764 case R_386_32: 1765 case R_386_PC32: 1766 default: 1767 assert(false && "unexpected relocation type"); 1768 } 1769 } else { 1770 switch (reloc_type(rel)) { 1771 case R_X86_64_64: 1772 { 1773 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 1774 if (symbol) 1775 { 1776 addr_t value = symbol->GetAddress().GetFileAddress(); 1777 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer(); 1778 uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel)); 1779 *dst = value + ELFRelocation::RelocAddend64(rel); 1780 } 1781 break; 1782 } 1783 case R_X86_64_32: 1784 case R_X86_64_32S: 1785 { 1786 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 1787 if (symbol) 1788 { 1789 addr_t value = symbol->GetAddress().GetFileAddress(); 1790 value += ELFRelocation::RelocAddend32(rel); 1791 assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) || 1792 (reloc_type(rel) == R_X86_64_32S && 1793 ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN))); 1794 uint32_t truncated_addr = (value & 0xFFFFFFFF); 1795 DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer(); 1796 uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel)); 1797 *dst = truncated_addr; 1798 } 1799 break; 1800 } 1801 case R_X86_64_PC32: 1802 default: 1803 assert(false && "unexpected relocation type"); 1804 } 1805 } 1806 } 1807 1808 return 0; 1809 } 1810 1811 unsigned 1812 ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id) 1813 { 1814 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 1815 1816 // Parse in the section list if needed. 1817 SectionList *section_list = GetSectionList(); 1818 if (!section_list) 1819 return 0; 1820 1821 // Section ID's are ones based. 1822 user_id_t symtab_id = rel_hdr->sh_link + 1; 1823 user_id_t debug_id = rel_hdr->sh_info + 1; 1824 1825 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 1826 if (!symtab_hdr) 1827 return 0; 1828 1829 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); 1830 if (!debug_hdr) 1831 return 0; 1832 1833 Section *rel = section_list->FindSectionByID(rel_id).get(); 1834 if (!rel) 1835 return 0; 1836 1837 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 1838 if (!symtab) 1839 return 0; 1840 1841 Section *debug = section_list->FindSectionByID(debug_id).get(); 1842 if (!debug) 1843 return 0; 1844 1845 DataExtractor rel_data; 1846 DataExtractor symtab_data; 1847 DataExtractor debug_data; 1848 1849 if (ReadSectionData(rel, rel_data) && 1850 ReadSectionData(symtab, symtab_data) && 1851 ReadSectionData(debug, debug_data)) 1852 { 1853 RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr, 1854 rel_data, symtab_data, debug_data, debug); 1855 } 1856 1857 return 0; 1858 } 1859 1860 Symtab * 1861 ObjectFileELF::GetSymtab() 1862 { 1863 ModuleSP module_sp(GetModule()); 1864 if (!module_sp) 1865 return NULL; 1866 1867 // We always want to use the main object file so we (hopefully) only have one cached copy 1868 // of our symtab, dynamic sections, etc. 1869 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 1870 if (module_obj_file && module_obj_file != this) 1871 return module_obj_file->GetSymtab(); 1872 1873 if (m_symtab_ap.get() == NULL) 1874 { 1875 SectionList *section_list = GetSectionList(); 1876 if (!section_list) 1877 return NULL; 1878 1879 uint64_t symbol_id = 0; 1880 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 1881 1882 m_symtab_ap.reset(new Symtab(this)); 1883 1884 // Sharable objects and dynamic executables usually have 2 distinct symbol 1885 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller 1886 // version of the symtab that only contains global symbols. The information found 1887 // in the dynsym is therefore also found in the symtab, while the reverse is not 1888 // necessarily true. 1889 Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get(); 1890 if (!symtab) 1891 { 1892 // The symtab section is non-allocable and can be stripped, so if it doesn't exist 1893 // then use the dynsym section which should always be there. 1894 symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get(); 1895 } 1896 if (symtab) 1897 symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab); 1898 1899 // DT_JMPREL 1900 // If present, this entry's d_ptr member holds the address of relocation 1901 // entries associated solely with the procedure linkage table. Separating 1902 // these relocation entries lets the dynamic linker ignore them during 1903 // process initialization, if lazy binding is enabled. If this entry is 1904 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 1905 // also be present. 1906 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 1907 if (symbol) 1908 { 1909 // Synthesize trampoline symbols to help navigate the PLT. 1910 addr_t addr = symbol->d_ptr; 1911 Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get(); 1912 if (reloc_section) 1913 { 1914 user_id_t reloc_id = reloc_section->GetID(); 1915 const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id); 1916 assert(reloc_header); 1917 1918 ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id); 1919 } 1920 } 1921 } 1922 1923 for (SectionHeaderCollIter I = m_section_headers.begin(); 1924 I != m_section_headers.end(); ++I) 1925 { 1926 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) 1927 { 1928 if (CalculateType() == eTypeObjectFile) 1929 { 1930 const char *section_name = I->section_name.AsCString(""); 1931 if (strstr(section_name, ".rela.debug") || 1932 strstr(section_name, ".rel.debug")) 1933 { 1934 const ELFSectionHeader &reloc_header = *I; 1935 user_id_t reloc_id = SectionIndex(I); 1936 RelocateDebugSections(&reloc_header, reloc_id); 1937 } 1938 } 1939 } 1940 } 1941 return m_symtab_ap.get(); 1942 } 1943 1944 Symbol * 1945 ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique) 1946 { 1947 if (!m_symtab_ap.get()) 1948 return nullptr; // GetSymtab() should be called first. 1949 1950 const SectionList *section_list = GetSectionList(); 1951 if (!section_list) 1952 return nullptr; 1953 1954 if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo()) 1955 { 1956 AddressRange range; 1957 if (eh_frame->GetAddressRange (so_addr, range)) 1958 { 1959 const addr_t file_addr = range.GetBaseAddress().GetFileAddress(); 1960 Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr; 1961 if (symbol) 1962 return symbol; 1963 1964 // Note that a (stripped) symbol won't be found by GetSymtab()... 1965 lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr); 1966 if (eh_sym_section_sp.get()) 1967 { 1968 addr_t section_base = eh_sym_section_sp->GetFileAddress(); 1969 addr_t offset = file_addr - section_base; 1970 uint64_t symbol_id = m_symtab_ap->GetNumSymbols(); 1971 1972 Symbol eh_symbol( 1973 symbol_id, // Symbol table index. 1974 "???", // Symbol name. 1975 false, // Is the symbol name mangled? 1976 eSymbolTypeCode, // Type of this symbol. 1977 true, // Is this globally visible? 1978 false, // Is this symbol debug info? 1979 false, // Is this symbol a trampoline? 1980 true, // Is this symbol artificial? 1981 eh_sym_section_sp, // Section in which this symbol is defined or null. 1982 offset, // Offset in section or symbol value. 1983 range.GetByteSize(), // Size in bytes of this symbol. 1984 true, // Size is valid. 1985 0); // Symbol flags. 1986 if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol)) 1987 return m_symtab_ap->SymbolAtIndex(symbol_id); 1988 } 1989 } 1990 } 1991 return nullptr; 1992 } 1993 1994 1995 bool 1996 ObjectFileELF::IsStripped () 1997 { 1998 // TODO: determine this for ELF 1999 return false; 2000 } 2001 2002 //===----------------------------------------------------------------------===// 2003 // Dump 2004 // 2005 // Dump the specifics of the runtime file container (such as any headers 2006 // segments, sections, etc). 2007 //---------------------------------------------------------------------- 2008 void 2009 ObjectFileELF::Dump(Stream *s) 2010 { 2011 DumpELFHeader(s, m_header); 2012 s->EOL(); 2013 DumpELFProgramHeaders(s); 2014 s->EOL(); 2015 DumpELFSectionHeaders(s); 2016 s->EOL(); 2017 SectionList *section_list = GetSectionList(); 2018 if (section_list) 2019 section_list->Dump(s, NULL, true, UINT32_MAX); 2020 Symtab *symtab = GetSymtab(); 2021 if (symtab) 2022 symtab->Dump(s, NULL, eSortOrderNone); 2023 s->EOL(); 2024 DumpDependentModules(s); 2025 s->EOL(); 2026 } 2027 2028 //---------------------------------------------------------------------- 2029 // DumpELFHeader 2030 // 2031 // Dump the ELF header to the specified output stream 2032 //---------------------------------------------------------------------- 2033 void 2034 ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) 2035 { 2036 s->PutCString("ELF Header\n"); 2037 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 2038 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", 2039 header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); 2040 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", 2041 header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); 2042 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", 2043 header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); 2044 2045 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 2046 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 2047 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 2048 s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 2049 s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 2050 2051 s->Printf("e_type = 0x%4.4x ", header.e_type); 2052 DumpELFHeader_e_type(s, header.e_type); 2053 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 2054 s->Printf("e_version = 0x%8.8x\n", header.e_version); 2055 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 2056 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 2057 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 2058 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 2059 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 2060 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 2061 s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); 2062 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 2063 s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); 2064 s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); 2065 } 2066 2067 //---------------------------------------------------------------------- 2068 // DumpELFHeader_e_type 2069 // 2070 // Dump an token value for the ELF header member e_type 2071 //---------------------------------------------------------------------- 2072 void 2073 ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) 2074 { 2075 switch (e_type) 2076 { 2077 case ET_NONE: *s << "ET_NONE"; break; 2078 case ET_REL: *s << "ET_REL"; break; 2079 case ET_EXEC: *s << "ET_EXEC"; break; 2080 case ET_DYN: *s << "ET_DYN"; break; 2081 case ET_CORE: *s << "ET_CORE"; break; 2082 default: 2083 break; 2084 } 2085 } 2086 2087 //---------------------------------------------------------------------- 2088 // DumpELFHeader_e_ident_EI_DATA 2089 // 2090 // Dump an token value for the ELF header member e_ident[EI_DATA] 2091 //---------------------------------------------------------------------- 2092 void 2093 ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) 2094 { 2095 switch (ei_data) 2096 { 2097 case ELFDATANONE: *s << "ELFDATANONE"; break; 2098 case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; 2099 case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; 2100 default: 2101 break; 2102 } 2103 } 2104 2105 2106 //---------------------------------------------------------------------- 2107 // DumpELFProgramHeader 2108 // 2109 // Dump a single ELF program header to the specified output stream 2110 //---------------------------------------------------------------------- 2111 void 2112 ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) 2113 { 2114 DumpELFProgramHeader_p_type(s, ph.p_type); 2115 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr); 2116 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags); 2117 2118 DumpELFProgramHeader_p_flags(s, ph.p_flags); 2119 s->Printf(") %8.8" PRIx64, ph.p_align); 2120 } 2121 2122 //---------------------------------------------------------------------- 2123 // DumpELFProgramHeader_p_type 2124 // 2125 // Dump an token value for the ELF program header member p_type which 2126 // describes the type of the program header 2127 // ---------------------------------------------------------------------- 2128 void 2129 ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) 2130 { 2131 const int kStrWidth = 15; 2132 switch (p_type) 2133 { 2134 CASE_AND_STREAM(s, PT_NULL , kStrWidth); 2135 CASE_AND_STREAM(s, PT_LOAD , kStrWidth); 2136 CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); 2137 CASE_AND_STREAM(s, PT_INTERP , kStrWidth); 2138 CASE_AND_STREAM(s, PT_NOTE , kStrWidth); 2139 CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); 2140 CASE_AND_STREAM(s, PT_PHDR , kStrWidth); 2141 CASE_AND_STREAM(s, PT_TLS , kStrWidth); 2142 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 2143 default: 2144 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 2145 break; 2146 } 2147 } 2148 2149 2150 //---------------------------------------------------------------------- 2151 // DumpELFProgramHeader_p_flags 2152 // 2153 // Dump an token value for the ELF program header member p_flags 2154 //---------------------------------------------------------------------- 2155 void 2156 ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) 2157 { 2158 *s << ((p_flags & PF_X) ? "PF_X" : " ") 2159 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 2160 << ((p_flags & PF_W) ? "PF_W" : " ") 2161 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 2162 << ((p_flags & PF_R) ? "PF_R" : " "); 2163 } 2164 2165 //---------------------------------------------------------------------- 2166 // DumpELFProgramHeaders 2167 // 2168 // Dump all of the ELF program header to the specified output stream 2169 //---------------------------------------------------------------------- 2170 void 2171 ObjectFileELF::DumpELFProgramHeaders(Stream *s) 2172 { 2173 if (ParseProgramHeaders()) 2174 { 2175 s->PutCString("Program Headers\n"); 2176 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 2177 "p_filesz p_memsz p_flags p_align\n"); 2178 s->PutCString("==== --------------- -------- -------- -------- " 2179 "-------- -------- ------------------------- --------\n"); 2180 2181 uint32_t idx = 0; 2182 for (ProgramHeaderCollConstIter I = m_program_headers.begin(); 2183 I != m_program_headers.end(); ++I, ++idx) 2184 { 2185 s->Printf("[%2u] ", idx); 2186 ObjectFileELF::DumpELFProgramHeader(s, *I); 2187 s->EOL(); 2188 } 2189 } 2190 } 2191 2192 //---------------------------------------------------------------------- 2193 // DumpELFSectionHeader 2194 // 2195 // Dump a single ELF section header to the specified output stream 2196 //---------------------------------------------------------------------- 2197 void 2198 ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh) 2199 { 2200 s->Printf("%8.8x ", sh.sh_name); 2201 DumpELFSectionHeader_sh_type(s, sh.sh_type); 2202 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 2203 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 2204 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size); 2205 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 2206 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 2207 } 2208 2209 //---------------------------------------------------------------------- 2210 // DumpELFSectionHeader_sh_type 2211 // 2212 // Dump an token value for the ELF section header member sh_type which 2213 // describes the type of the section 2214 //---------------------------------------------------------------------- 2215 void 2216 ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) 2217 { 2218 const int kStrWidth = 12; 2219 switch (sh_type) 2220 { 2221 CASE_AND_STREAM(s, SHT_NULL , kStrWidth); 2222 CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); 2223 CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); 2224 CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); 2225 CASE_AND_STREAM(s, SHT_RELA , kStrWidth); 2226 CASE_AND_STREAM(s, SHT_HASH , kStrWidth); 2227 CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); 2228 CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); 2229 CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); 2230 CASE_AND_STREAM(s, SHT_REL , kStrWidth); 2231 CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); 2232 CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); 2233 CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); 2234 CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); 2235 CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); 2236 CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); 2237 default: 2238 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 2239 break; 2240 } 2241 } 2242 2243 //---------------------------------------------------------------------- 2244 // DumpELFSectionHeader_sh_flags 2245 // 2246 // Dump an token value for the ELF section header member sh_flags 2247 //---------------------------------------------------------------------- 2248 void 2249 ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags) 2250 { 2251 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 2252 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 2253 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 2254 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 2255 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 2256 } 2257 2258 //---------------------------------------------------------------------- 2259 // DumpELFSectionHeaders 2260 // 2261 // Dump all of the ELF section header to the specified output stream 2262 //---------------------------------------------------------------------- 2263 void 2264 ObjectFileELF::DumpELFSectionHeaders(Stream *s) 2265 { 2266 if (!ParseSectionHeaders()) 2267 return; 2268 2269 s->PutCString("Section Headers\n"); 2270 s->PutCString("IDX name type flags " 2271 "addr offset size link info addralgn " 2272 "entsize Name\n"); 2273 s->PutCString("==== -------- ------------ -------------------------------- " 2274 "-------- -------- -------- -------- -------- -------- " 2275 "-------- ====================\n"); 2276 2277 uint32_t idx = 0; 2278 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 2279 I != m_section_headers.end(); ++I, ++idx) 2280 { 2281 s->Printf("[%2u] ", idx); 2282 ObjectFileELF::DumpELFSectionHeader(s, *I); 2283 const char* section_name = I->section_name.AsCString(""); 2284 if (section_name) 2285 *s << ' ' << section_name << "\n"; 2286 } 2287 } 2288 2289 void 2290 ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) 2291 { 2292 size_t num_modules = ParseDependentModules(); 2293 2294 if (num_modules > 0) 2295 { 2296 s->PutCString("Dependent Modules:\n"); 2297 for (unsigned i = 0; i < num_modules; ++i) 2298 { 2299 const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); 2300 s->Printf(" %s\n", spec.GetFilename().GetCString()); 2301 } 2302 } 2303 } 2304 2305 bool 2306 ObjectFileELF::GetArchitecture (ArchSpec &arch) 2307 { 2308 if (!ParseHeader()) 2309 return false; 2310 2311 arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE); 2312 arch.GetTriple().setOSName (Host::GetOSString().GetCString()); 2313 arch.GetTriple().setVendorName(Host::GetVendorString().GetCString()); 2314 return true; 2315 } 2316 2317 ObjectFile::Type 2318 ObjectFileELF::CalculateType() 2319 { 2320 switch (m_header.e_type) 2321 { 2322 case llvm::ELF::ET_NONE: 2323 // 0 - No file type 2324 return eTypeUnknown; 2325 2326 case llvm::ELF::ET_REL: 2327 // 1 - Relocatable file 2328 return eTypeObjectFile; 2329 2330 case llvm::ELF::ET_EXEC: 2331 // 2 - Executable file 2332 return eTypeExecutable; 2333 2334 case llvm::ELF::ET_DYN: 2335 // 3 - Shared object file 2336 return eTypeSharedLibrary; 2337 2338 case ET_CORE: 2339 // 4 - Core file 2340 return eTypeCoreFile; 2341 2342 default: 2343 break; 2344 } 2345 return eTypeUnknown; 2346 } 2347 2348 ObjectFile::Strata 2349 ObjectFileELF::CalculateStrata() 2350 { 2351 switch (m_header.e_type) 2352 { 2353 case llvm::ELF::ET_NONE: 2354 // 0 - No file type 2355 return eStrataUnknown; 2356 2357 case llvm::ELF::ET_REL: 2358 // 1 - Relocatable file 2359 return eStrataUnknown; 2360 2361 case llvm::ELF::ET_EXEC: 2362 // 2 - Executable file 2363 // TODO: is there any way to detect that an executable is a kernel 2364 // related executable by inspecting the program headers, section 2365 // headers, symbols, or any other flag bits??? 2366 return eStrataUser; 2367 2368 case llvm::ELF::ET_DYN: 2369 // 3 - Shared object file 2370 // TODO: is there any way to detect that an shared library is a kernel 2371 // related executable by inspecting the program headers, section 2372 // headers, symbols, or any other flag bits??? 2373 return eStrataUnknown; 2374 2375 case ET_CORE: 2376 // 4 - Core file 2377 // TODO: is there any way to detect that an core file is a kernel 2378 // related executable by inspecting the program headers, section 2379 // headers, symbols, or any other flag bits??? 2380 return eStrataUnknown; 2381 2382 default: 2383 break; 2384 } 2385 return eStrataUnknown; 2386 } 2387 2388