1 //===-- ObjectFile.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 "lldb/lldb-private.h" 11 #include "lldb/lldb-private-log.h" 12 #include "lldb/Core/DataBuffer.h" 13 #include "lldb/Core/DataBufferHeap.h" 14 #include "lldb/Core/Log.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/ModuleSpec.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/RegularExpression.h" 19 #include "lldb/Core/Section.h" 20 #include "lldb/Core/Timer.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Symbol/ObjectContainer.h" 23 #include "lldb/Symbol/SymbolFile.h" 24 #include "lldb/Target/Process.h" 25 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h" 26 27 using namespace lldb; 28 using namespace lldb_private; 29 30 ObjectFileSP 31 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp, 32 const FileSpec* file, 33 lldb::offset_t file_offset, 34 lldb::offset_t file_size, 35 DataBufferSP &data_sp, 36 lldb::offset_t &data_offset) 37 { 38 ObjectFileSP object_file_sp; 39 40 if (module_sp) 41 { 42 Timer scoped_timer (__PRETTY_FUNCTION__, 43 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = 0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")", 44 module_sp->GetFileSpec().GetPath().c_str(), 45 static_cast<const void*>(file), 46 static_cast<uint64_t>(file_offset), 47 static_cast<uint64_t>(file_size)); 48 if (file) 49 { 50 FileSpec archive_file; 51 ObjectContainerCreateInstance create_object_container_callback; 52 53 const bool file_exists = file->Exists(); 54 if (!data_sp) 55 { 56 // We have an object name which most likely means we have 57 // a .o file in a static archive (.a file). Try and see if 58 // we have a cached archive first without reading any data 59 // first 60 if (file_exists && module_sp->GetObjectName()) 61 { 62 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx) 63 { 64 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 65 66 if (object_container_ap.get()) 67 object_file_sp = object_container_ap->GetObjectFile(file); 68 69 if (object_file_sp.get()) 70 return object_file_sp; 71 } 72 } 73 // Ok, we didn't find any containers that have a named object, now 74 // lets read the first 512 bytes from the file so the object file 75 // and object container plug-ins can use these bytes to see if they 76 // can parse this file. 77 if (file_size > 0) 78 { 79 data_sp = file->ReadFileContents(file_offset, std::min<size_t>(512, file_size)); 80 data_offset = 0; 81 } 82 } 83 84 if (!data_sp || data_sp->GetByteSize() == 0) 85 { 86 // Check for archive file with format "/path/to/archive.a(object.o)" 87 char path_with_object[PATH_MAX*2]; 88 module_sp->GetFileSpec().GetPath(path_with_object, sizeof(path_with_object)); 89 90 ConstString archive_object; 91 const bool must_exist = true; 92 if (ObjectFile::SplitArchivePathWithObject (path_with_object, archive_file, archive_object, must_exist)) 93 { 94 file_size = archive_file.GetByteSize(); 95 if (file_size > 0) 96 { 97 file = &archive_file; 98 module_sp->SetFileSpecAndObjectName (archive_file, archive_object); 99 // Check if this is a object container by iterating through all object 100 // container plugin instances and then trying to get an object file 101 // from the container plugins since we had a name. Also, don't read 102 // ANY data in case there is data cached in the container plug-ins 103 // (like BSD archives caching the contained objects within an file). 104 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx) 105 { 106 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 107 108 if (object_container_ap.get()) 109 object_file_sp = object_container_ap->GetObjectFile(file); 110 111 if (object_file_sp.get()) 112 return object_file_sp; 113 } 114 // We failed to find any cached object files in the container 115 // plug-ins, so lets read the first 512 bytes and try again below... 116 data_sp = archive_file.ReadFileContents(file_offset, 512); 117 } 118 } 119 } 120 121 if (data_sp && data_sp->GetByteSize() > 0) 122 { 123 // Check if this is a normal object file by iterating through 124 // all object file plugin instances. 125 ObjectFileCreateInstance create_object_file_callback; 126 for (uint32_t idx = 0; (create_object_file_callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != nullptr; ++idx) 127 { 128 object_file_sp.reset (create_object_file_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 129 if (object_file_sp.get()) 130 return object_file_sp; 131 } 132 133 // Check if this is a object container by iterating through 134 // all object container plugin instances and then trying to get 135 // an object file from the container. 136 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx) 137 { 138 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 139 140 if (object_container_ap.get()) 141 object_file_sp = object_container_ap->GetObjectFile(file); 142 143 if (object_file_sp.get()) 144 return object_file_sp; 145 } 146 } 147 } 148 } 149 // We didn't find it, so clear our shared pointer in case it 150 // contains anything and return an empty shared pointer 151 object_file_sp.reset(); 152 return object_file_sp; 153 } 154 155 ObjectFileSP 156 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp, 157 const ProcessSP &process_sp, 158 lldb::addr_t header_addr, 159 DataBufferSP &data_sp) 160 { 161 ObjectFileSP object_file_sp; 162 163 if (module_sp) 164 { 165 Timer scoped_timer (__PRETTY_FUNCTION__, 166 "ObjectFile::FindPlugin (module = %s, process = %p, header_addr = 0x%" PRIx64 ")", 167 module_sp->GetFileSpec().GetPath().c_str(), 168 static_cast<void*>(process_sp.get()), header_addr); 169 uint32_t idx; 170 171 // Check if this is a normal object file by iterating through 172 // all object file plugin instances. 173 ObjectFileCreateMemoryInstance create_callback; 174 for (idx = 0; (create_callback = PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != nullptr; ++idx) 175 { 176 object_file_sp.reset (create_callback(module_sp, data_sp, process_sp, header_addr)); 177 if (object_file_sp.get()) 178 return object_file_sp; 179 } 180 } 181 182 // We didn't find it, so clear our shared pointer in case it 183 // contains anything and return an empty shared pointer 184 object_file_sp.reset(); 185 return object_file_sp; 186 } 187 188 size_t 189 ObjectFile::GetModuleSpecifications (const FileSpec &file, 190 lldb::offset_t file_offset, 191 lldb::offset_t file_size, 192 ModuleSpecList &specs) 193 { 194 DataBufferSP data_sp (file.ReadFileContents(file_offset, 512)); 195 if (data_sp) 196 { 197 if (file_size == 0) 198 { 199 const lldb::offset_t actual_file_size = file.GetByteSize(); 200 if (actual_file_size > file_offset) 201 file_size = actual_file_size - file_offset; 202 } 203 return ObjectFile::GetModuleSpecifications (file, // file spec 204 data_sp, // data bytes 205 0, // data offset 206 file_offset,// file offset 207 file_size, // file length 208 specs); 209 } 210 return 0; 211 } 212 213 size_t 214 ObjectFile::GetModuleSpecifications (const lldb_private::FileSpec& file, 215 lldb::DataBufferSP& data_sp, 216 lldb::offset_t data_offset, 217 lldb::offset_t file_offset, 218 lldb::offset_t file_size, 219 lldb_private::ModuleSpecList &specs) 220 { 221 const size_t initial_count = specs.GetSize(); 222 ObjectFileGetModuleSpecifications callback; 223 uint32_t i; 224 // Try the ObjectFile plug-ins 225 for (i = 0; (callback = PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i) 226 { 227 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0) 228 return specs.GetSize() - initial_count; 229 } 230 231 // Try the ObjectContainer plug-ins 232 for (i = 0; (callback = PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i) 233 { 234 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0) 235 return specs.GetSize() - initial_count; 236 } 237 return 0; 238 } 239 240 ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp, 241 const FileSpec *file_spec_ptr, 242 lldb::offset_t file_offset, 243 lldb::offset_t length, 244 const lldb::DataBufferSP& data_sp, 245 lldb::offset_t data_offset 246 ) : 247 ModuleChild (module_sp), 248 m_file (), // This file could be different from the original module's file 249 m_type (eTypeInvalid), 250 m_strata (eStrataInvalid), 251 m_file_offset (file_offset), 252 m_length (length), 253 m_data (), 254 m_unwind_table (*this), 255 m_process_wp(), 256 m_memory_addr (LLDB_INVALID_ADDRESS), 257 m_sections_ap(), 258 m_symtab_ap () 259 { 260 if (file_spec_ptr) 261 m_file = *file_spec_ptr; 262 if (data_sp) 263 m_data.SetData (data_sp, data_offset, length); 264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 265 if (log) 266 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, 267 static_cast<void*>(this), 268 static_cast<void*>(module_sp.get()), 269 module_sp->GetSpecificationDescription().c_str(), 270 m_file ? m_file.GetPath().c_str() : "<NULL>", 271 m_file_offset, m_length); 272 } 273 274 275 ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp, 276 const ProcessSP &process_sp, 277 lldb::addr_t header_addr, 278 DataBufferSP& header_data_sp) : 279 ModuleChild (module_sp), 280 m_file (), 281 m_type (eTypeInvalid), 282 m_strata (eStrataInvalid), 283 m_file_offset (0), 284 m_length (0), 285 m_data (), 286 m_unwind_table (*this), 287 m_process_wp (process_sp), 288 m_memory_addr (header_addr), 289 m_sections_ap(), 290 m_symtab_ap () 291 { 292 if (header_data_sp) 293 m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize()); 294 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 295 if (log) 296 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, header_addr = 0x%" PRIx64, 297 static_cast<void*>(this), 298 static_cast<void*>(module_sp.get()), 299 module_sp->GetSpecificationDescription().c_str(), 300 static_cast<void*>(process_sp.get()), m_memory_addr); 301 } 302 303 304 ObjectFile::~ObjectFile() 305 { 306 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 307 if (log) 308 log->Printf ("%p ObjectFile::~ObjectFile ()\n", 309 static_cast<void*>(this)); 310 } 311 312 bool 313 ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch) 314 { 315 ModuleSP module_sp (GetModule()); 316 if (module_sp) 317 return module_sp->SetArchitecture (new_arch); 318 return false; 319 } 320 321 AddressClass 322 ObjectFile::GetAddressClass (addr_t file_addr) 323 { 324 Symtab *symtab = GetSymtab(); 325 if (symtab) 326 { 327 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 328 if (symbol) 329 { 330 if (symbol->ValueIsAddress()) 331 { 332 const SectionSP section_sp (symbol->GetAddress().GetSection()); 333 if (section_sp) 334 { 335 const SectionType section_type = section_sp->GetType(); 336 switch (section_type) 337 { 338 case eSectionTypeInvalid: 339 return eAddressClassUnknown; 340 case eSectionTypeCode: 341 return eAddressClassCode; 342 case eSectionTypeContainer: 343 return eAddressClassUnknown; 344 case eSectionTypeData: 345 case eSectionTypeDataCString: 346 case eSectionTypeDataCStringPointers: 347 case eSectionTypeDataSymbolAddress: 348 case eSectionTypeData4: 349 case eSectionTypeData8: 350 case eSectionTypeData16: 351 case eSectionTypeDataPointers: 352 case eSectionTypeZeroFill: 353 case eSectionTypeDataObjCMessageRefs: 354 case eSectionTypeDataObjCCFStrings: 355 return eAddressClassData; 356 case eSectionTypeDebug: 357 case eSectionTypeDWARFDebugAbbrev: 358 case eSectionTypeDWARFDebugAranges: 359 case eSectionTypeDWARFDebugFrame: 360 case eSectionTypeDWARFDebugInfo: 361 case eSectionTypeDWARFDebugLine: 362 case eSectionTypeDWARFDebugLoc: 363 case eSectionTypeDWARFDebugMacInfo: 364 case eSectionTypeDWARFDebugPubNames: 365 case eSectionTypeDWARFDebugPubTypes: 366 case eSectionTypeDWARFDebugRanges: 367 case eSectionTypeDWARFDebugStr: 368 case eSectionTypeDWARFAppleNames: 369 case eSectionTypeDWARFAppleTypes: 370 case eSectionTypeDWARFAppleNamespaces: 371 case eSectionTypeDWARFAppleObjC: 372 return eAddressClassDebug; 373 case eSectionTypeEHFrame: 374 return eAddressClassRuntime; 375 case eSectionTypeELFSymbolTable: 376 case eSectionTypeELFDynamicSymbols: 377 case eSectionTypeELFRelocationEntries: 378 case eSectionTypeELFDynamicLinkInfo: 379 case eSectionTypeOther: 380 return eAddressClassUnknown; 381 } 382 } 383 } 384 385 const SymbolType symbol_type = symbol->GetType(); 386 switch (symbol_type) 387 { 388 case eSymbolTypeAny: return eAddressClassUnknown; 389 case eSymbolTypeAbsolute: return eAddressClassUnknown; 390 case eSymbolTypeCode: return eAddressClassCode; 391 case eSymbolTypeTrampoline: return eAddressClassCode; 392 case eSymbolTypeResolver: return eAddressClassCode; 393 case eSymbolTypeData: return eAddressClassData; 394 case eSymbolTypeRuntime: return eAddressClassRuntime; 395 case eSymbolTypeException: return eAddressClassRuntime; 396 case eSymbolTypeSourceFile: return eAddressClassDebug; 397 case eSymbolTypeHeaderFile: return eAddressClassDebug; 398 case eSymbolTypeObjectFile: return eAddressClassDebug; 399 case eSymbolTypeCommonBlock: return eAddressClassDebug; 400 case eSymbolTypeBlock: return eAddressClassDebug; 401 case eSymbolTypeLocal: return eAddressClassData; 402 case eSymbolTypeParam: return eAddressClassData; 403 case eSymbolTypeVariable: return eAddressClassData; 404 case eSymbolTypeVariableType: return eAddressClassDebug; 405 case eSymbolTypeLineEntry: return eAddressClassDebug; 406 case eSymbolTypeLineHeader: return eAddressClassDebug; 407 case eSymbolTypeScopeBegin: return eAddressClassDebug; 408 case eSymbolTypeScopeEnd: return eAddressClassDebug; 409 case eSymbolTypeAdditional: return eAddressClassUnknown; 410 case eSymbolTypeCompiler: return eAddressClassDebug; 411 case eSymbolTypeInstrumentation:return eAddressClassDebug; 412 case eSymbolTypeUndefined: return eAddressClassUnknown; 413 case eSymbolTypeObjCClass: return eAddressClassRuntime; 414 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime; 415 case eSymbolTypeObjCIVar: return eAddressClassRuntime; 416 case eSymbolTypeReExported: return eAddressClassRuntime; 417 } 418 } 419 } 420 return eAddressClassUnknown; 421 } 422 423 DataBufferSP 424 ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size) 425 { 426 DataBufferSP data_sp; 427 if (process_sp) 428 { 429 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0)); 430 Error error; 431 const size_t bytes_read = process_sp->ReadMemory (addr, 432 data_ap->GetBytes(), 433 data_ap->GetByteSize(), 434 error); 435 if (bytes_read == byte_size) 436 data_sp.reset (data_ap.release()); 437 } 438 return data_sp; 439 } 440 441 size_t 442 ObjectFile::GetData (lldb::offset_t offset, size_t length, DataExtractor &data) const 443 { 444 // The entire file has already been mmap'ed into m_data, so just copy from there 445 // as the back mmap buffer will be shared with shared pointers. 446 return data.SetData (m_data, offset, length); 447 } 448 449 size_t 450 ObjectFile::CopyData (lldb::offset_t offset, size_t length, void *dst) const 451 { 452 // The entire file has already been mmap'ed into m_data, so just copy from there 453 // Note that the data remains in target byte order. 454 return m_data.CopyData (offset, length, dst); 455 } 456 457 458 size_t 459 ObjectFile::ReadSectionData (const Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) const 460 { 461 assert(section); 462 section_offset *= section->GetTargetByteSize(); 463 464 // If some other objectfile owns this data, pass this to them. 465 if (section->GetObjectFile() != this) 466 return section->GetObjectFile()->ReadSectionData (section, section_offset, dst, dst_len); 467 468 if (IsInMemory()) 469 { 470 ProcessSP process_sp (m_process_wp.lock()); 471 if (process_sp) 472 { 473 Error error; 474 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 475 if (base_load_addr != LLDB_INVALID_ADDRESS) 476 return process_sp->ReadMemory (base_load_addr + section_offset, dst, dst_len, error); 477 } 478 } 479 else 480 { 481 const lldb::offset_t section_file_size = section->GetFileSize(); 482 if (section_offset < section_file_size) 483 { 484 const size_t section_bytes_left = section_file_size - section_offset; 485 size_t section_dst_len = dst_len; 486 if (section_dst_len > section_bytes_left) 487 section_dst_len = section_bytes_left; 488 return CopyData (section->GetFileOffset() + section_offset, section_dst_len, dst); 489 } 490 else 491 { 492 if (section->GetType() == eSectionTypeZeroFill) 493 { 494 const uint64_t section_size = section->GetByteSize(); 495 const uint64_t section_bytes_left = section_size - section_offset; 496 uint64_t section_dst_len = dst_len; 497 if (section_dst_len > section_bytes_left) 498 section_dst_len = section_bytes_left; 499 memset(dst, 0, section_dst_len); 500 return section_dst_len; 501 } 502 } 503 } 504 return 0; 505 } 506 507 //---------------------------------------------------------------------- 508 // Get the section data the file on disk 509 //---------------------------------------------------------------------- 510 size_t 511 ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const 512 { 513 // If some other objectfile owns this data, pass this to them. 514 if (section->GetObjectFile() != this) 515 return section->GetObjectFile()->ReadSectionData (section, section_data); 516 517 if (IsInMemory()) 518 { 519 ProcessSP process_sp (m_process_wp.lock()); 520 if (process_sp) 521 { 522 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 523 if (base_load_addr != LLDB_INVALID_ADDRESS) 524 { 525 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize())); 526 if (data_sp) 527 { 528 section_data.SetData (data_sp, 0, data_sp->GetByteSize()); 529 section_data.SetByteOrder (process_sp->GetByteOrder()); 530 section_data.SetAddressByteSize (process_sp->GetAddressByteSize()); 531 return section_data.GetByteSize(); 532 } 533 } 534 } 535 } 536 else 537 { 538 // The object file now contains a full mmap'ed copy of the object file data, so just use this 539 return MemoryMapSectionData (section, section_data); 540 } 541 section_data.Clear(); 542 return 0; 543 } 544 545 size_t 546 ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const 547 { 548 // If some other objectfile owns this data, pass this to them. 549 if (section->GetObjectFile() != this) 550 return section->GetObjectFile()->MemoryMapSectionData (section, section_data); 551 552 if (IsInMemory()) 553 { 554 return ReadSectionData (section, section_data); 555 } 556 else 557 { 558 // The object file now contains a full mmap'ed copy of the object file data, so just use this 559 return GetData(section->GetFileOffset(), section->GetFileSize(), section_data); 560 } 561 section_data.Clear(); 562 return 0; 563 } 564 565 566 bool 567 ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist) 568 { 569 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$"); 570 RegularExpression::Match regex_match(2); 571 if (g_object_regex.Execute (path_with_object, ®ex_match)) 572 { 573 std::string path; 574 std::string obj; 575 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) && 576 regex_match.GetMatchAtIndex (path_with_object, 2, obj)) 577 { 578 archive_file.SetFile (path.c_str(), false); 579 archive_object.SetCString(obj.c_str()); 580 if (must_exist && !archive_file.Exists()) 581 return false; 582 return true; 583 } 584 } 585 return false; 586 } 587 588 void 589 ObjectFile::ClearSymtab () 590 { 591 ModuleSP module_sp(GetModule()); 592 if (module_sp) 593 { 594 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 595 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 596 if (log) 597 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p", 598 static_cast<void*>(this), 599 static_cast<void*>(m_symtab_ap.get())); 600 m_symtab_ap.reset(); 601 } 602 } 603 604 SectionList * 605 ObjectFile::GetSectionList() 606 { 607 if (m_sections_ap.get() == nullptr) 608 { 609 ModuleSP module_sp(GetModule()); 610 if (module_sp) 611 { 612 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 613 CreateSections(*module_sp->GetUnifiedSectionList()); 614 } 615 } 616 return m_sections_ap.get(); 617 } 618