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 file, (uint64_t) file_offset, (uint64_t) file_size); 46 if (file) 47 { 48 FileSpec archive_file; 49 ObjectContainerCreateInstance create_object_container_callback; 50 51 const bool file_exists = file->Exists(); 52 if (!data_sp) 53 { 54 // We have an object name which most likely means we have 55 // a .o file in a static archive (.a file). Try and see if 56 // we have a cached archive first without reading any data 57 // first 58 if (file_exists && module_sp->GetObjectName()) 59 { 60 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != NULL; ++idx) 61 { 62 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 63 64 if (object_container_ap.get()) 65 object_file_sp = object_container_ap->GetObjectFile(file); 66 67 if (object_file_sp.get()) 68 return object_file_sp; 69 } 70 } 71 // Ok, we didn't find any containers that have a named object, now 72 // lets read the first 512 bytes from the file so the object file 73 // and object container plug-ins can use these bytes to see if they 74 // can parse this file. 75 if (file_size > 0) 76 { 77 data_sp = file->ReadFileContents(file_offset, std::min<size_t>(512, file_size)); 78 data_offset = 0; 79 } 80 } 81 82 if (!data_sp || data_sp->GetByteSize() == 0) 83 { 84 // Check for archive file with format "/path/to/archive.a(object.o)" 85 char path_with_object[PATH_MAX*2]; 86 module_sp->GetFileSpec().GetPath(path_with_object, sizeof(path_with_object)); 87 88 ConstString archive_object; 89 const bool must_exist = true; 90 if (ObjectFile::SplitArchivePathWithObject (path_with_object, archive_file, archive_object, must_exist)) 91 { 92 file_size = archive_file.GetByteSize(); 93 if (file_size > 0) 94 { 95 file = &archive_file; 96 module_sp->SetFileSpecAndObjectName (archive_file, archive_object); 97 // Check if this is a object container by iterating through all object 98 // container plugin instances and then trying to get an object file 99 // from the container plugins since we had a name. Also, don't read 100 // ANY data in case there is data cached in the container plug-ins 101 // (like BSD archives caching the contained objects within an file). 102 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != NULL; ++idx) 103 { 104 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 105 106 if (object_container_ap.get()) 107 object_file_sp = object_container_ap->GetObjectFile(file); 108 109 if (object_file_sp.get()) 110 return object_file_sp; 111 } 112 // We failed to find any cached object files in the container 113 // plug-ins, so lets read the first 512 bytes and try again below... 114 data_sp = archive_file.ReadFileContents(file_offset, 512); 115 } 116 } 117 } 118 119 if (data_sp && data_sp->GetByteSize() > 0) 120 { 121 // Check if this is a normal object file by iterating through 122 // all object file plugin instances. 123 ObjectFileCreateInstance create_object_file_callback; 124 for (uint32_t idx = 0; (create_object_file_callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != NULL; ++idx) 125 { 126 object_file_sp.reset (create_object_file_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 127 if (object_file_sp.get()) 128 return object_file_sp; 129 } 130 131 // Check if this is a object container by iterating through 132 // all object container plugin instances and then trying to get 133 // an object file from the container. 134 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != NULL; ++idx) 135 { 136 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size)); 137 138 if (object_container_ap.get()) 139 object_file_sp = object_container_ap->GetObjectFile(file); 140 141 if (object_file_sp.get()) 142 return object_file_sp; 143 } 144 } 145 } 146 } 147 // We didn't find it, so clear our shared pointer in case it 148 // contains anything and return an empty shared pointer 149 object_file_sp.reset(); 150 return object_file_sp; 151 } 152 153 ObjectFileSP 154 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp, 155 const ProcessSP &process_sp, 156 lldb::addr_t header_addr, 157 DataBufferSP &data_sp) 158 { 159 ObjectFileSP object_file_sp; 160 161 if (module_sp) 162 { 163 Timer scoped_timer (__PRETTY_FUNCTION__, 164 "ObjectFile::FindPlugin (module = %s, process = %p, header_addr = 0x%" PRIx64 ")", 165 module_sp->GetFileSpec().GetPath().c_str(), 166 process_sp.get(), header_addr); 167 uint32_t idx; 168 169 // Check if this is a normal object file by iterating through 170 // all object file plugin instances. 171 ObjectFileCreateMemoryInstance create_callback; 172 for (idx = 0; (create_callback = PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != NULL; ++idx) 173 { 174 object_file_sp.reset (create_callback(module_sp, data_sp, process_sp, header_addr)); 175 if (object_file_sp.get()) 176 return object_file_sp; 177 } 178 179 } 180 // We didn't find it, so clear our shared pointer in case it 181 // contains anything and return an empty shared pointer 182 object_file_sp.reset(); 183 return object_file_sp; 184 } 185 186 size_t 187 ObjectFile::GetModuleSpecifications (const FileSpec &file, 188 lldb::offset_t file_offset, 189 ModuleSpecList &specs) 190 { 191 DataBufferSP data_sp (file.ReadFileContents(file_offset, 512)); 192 if (data_sp) 193 return ObjectFile::GetModuleSpecifications (file, // file spec 194 data_sp, // data bytes 195 0, // data offset 196 file_offset, // file offset 197 data_sp->GetByteSize(), // data length 198 specs); 199 return 0; 200 } 201 202 size_t 203 ObjectFile::GetModuleSpecifications (const lldb_private::FileSpec& file, 204 lldb::DataBufferSP& data_sp, 205 lldb::offset_t data_offset, 206 lldb::offset_t file_offset, 207 lldb::offset_t length, 208 lldb_private::ModuleSpecList &specs) 209 { 210 const size_t initial_count = specs.GetSize(); 211 ObjectFileGetModuleSpecifications callback; 212 uint32_t i; 213 // Try the ObjectFile plug-ins 214 for (i = 0; (callback = PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(i)) != NULL; ++i) 215 { 216 if (callback (file, data_sp, data_offset, file_offset, length, specs) > 0) 217 return specs.GetSize() - initial_count; 218 } 219 220 // Try the ObjectContainer plug-ins 221 for (i = 0; (callback = PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != NULL; ++i) 222 { 223 if (callback (file, data_sp, data_offset, file_offset, length, specs) > 0) 224 return specs.GetSize() - initial_count; 225 } 226 return 0; 227 } 228 229 ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp, 230 const FileSpec *file_spec_ptr, 231 lldb::offset_t file_offset, 232 lldb::offset_t length, 233 lldb::DataBufferSP& data_sp, 234 lldb::offset_t data_offset 235 ) : 236 ModuleChild (module_sp), 237 m_file (), // This file could be different from the original module's file 238 m_type (eTypeInvalid), 239 m_strata (eStrataInvalid), 240 m_file_offset (file_offset), 241 m_length (length), 242 m_data (), 243 m_unwind_table (*this), 244 m_process_wp(), 245 m_memory_addr (LLDB_INVALID_ADDRESS), 246 m_sections_ap (), 247 m_symtab_ap (), 248 m_symtab_unified_ap (), 249 m_symtab_unified_revisionid (0) 250 { 251 if (file_spec_ptr) 252 m_file = *file_spec_ptr; 253 if (data_sp) 254 m_data.SetData (data_sp, data_offset, length); 255 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 256 if (log) 257 { 258 if (m_file) 259 { 260 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, 261 this, 262 module_sp.get(), 263 module_sp->GetSpecificationDescription().c_str(), 264 m_file.GetPath().c_str(), 265 m_file_offset, 266 m_length); 267 } 268 else 269 { 270 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = <NULL>, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, 271 this, 272 module_sp.get(), 273 module_sp->GetSpecificationDescription().c_str(), 274 m_file_offset, 275 m_length); 276 } 277 } 278 } 279 280 281 ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp, 282 const ProcessSP &process_sp, 283 lldb::addr_t header_addr, 284 DataBufferSP& header_data_sp) : 285 ModuleChild (module_sp), 286 m_file (), 287 m_type (eTypeInvalid), 288 m_strata (eStrataInvalid), 289 m_file_offset (0), 290 m_length (0), 291 m_data (), 292 m_unwind_table (*this), 293 m_process_wp (process_sp), 294 m_memory_addr (header_addr), 295 m_sections_ap (), 296 m_symtab_ap (), 297 m_symtab_unified_ap (), 298 m_symtab_unified_revisionid (0) 299 { 300 if (header_data_sp) 301 m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize()); 302 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 303 if (log) 304 { 305 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, header_addr = 0x%" PRIx64, 306 this, 307 module_sp.get(), 308 module_sp->GetSpecificationDescription().c_str(), 309 process_sp.get(), 310 m_memory_addr); 311 } 312 } 313 314 315 ObjectFile::~ObjectFile() 316 { 317 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 318 if (log) 319 log->Printf ("%p ObjectFile::~ObjectFile ()\n", this); 320 } 321 322 bool 323 ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch) 324 { 325 ModuleSP module_sp (GetModule()); 326 if (module_sp) 327 return module_sp->SetArchitecture (new_arch); 328 return false; 329 } 330 331 AddressClass 332 ObjectFile::GetAddressClass (addr_t file_addr) 333 { 334 Symtab *symtab = GetSymtab(ObjectFile::eSymtabFromUnifiedSectionList); 335 if (symtab) 336 { 337 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 338 if (symbol) 339 { 340 if (symbol->ValueIsAddress()) 341 { 342 const SectionSP section_sp (symbol->GetAddress().GetSection()); 343 if (section_sp) 344 { 345 const SectionType section_type = section_sp->GetType(); 346 switch (section_type) 347 { 348 case eSectionTypeInvalid: return eAddressClassUnknown; 349 case eSectionTypeCode: return eAddressClassCode; 350 case eSectionTypeContainer: return eAddressClassUnknown; 351 case eSectionTypeData: 352 case eSectionTypeDataCString: 353 case eSectionTypeDataCStringPointers: 354 case eSectionTypeDataSymbolAddress: 355 case eSectionTypeData4: 356 case eSectionTypeData8: 357 case eSectionTypeData16: 358 case eSectionTypeDataPointers: 359 case eSectionTypeZeroFill: 360 case eSectionTypeDataObjCMessageRefs: 361 case eSectionTypeDataObjCCFStrings: 362 return eAddressClassData; 363 case eSectionTypeDebug: 364 case eSectionTypeDWARFDebugAbbrev: 365 case eSectionTypeDWARFDebugAranges: 366 case eSectionTypeDWARFDebugFrame: 367 case eSectionTypeDWARFDebugInfo: 368 case eSectionTypeDWARFDebugLine: 369 case eSectionTypeDWARFDebugLoc: 370 case eSectionTypeDWARFDebugMacInfo: 371 case eSectionTypeDWARFDebugPubNames: 372 case eSectionTypeDWARFDebugPubTypes: 373 case eSectionTypeDWARFDebugRanges: 374 case eSectionTypeDWARFDebugStr: 375 case eSectionTypeDWARFAppleNames: 376 case eSectionTypeDWARFAppleTypes: 377 case eSectionTypeDWARFAppleNamespaces: 378 case eSectionTypeDWARFAppleObjC: 379 return eAddressClassDebug; 380 case eSectionTypeEHFrame: return eAddressClassRuntime; 381 case eSectionTypeELFSymbolTable: 382 case eSectionTypeELFDynamicSymbols: 383 case eSectionTypeELFRelocationEntries: 384 case eSectionTypeELFDynamicLinkInfo: 385 case eSectionTypeOther: return eAddressClassUnknown; 386 } 387 } 388 } 389 390 const SymbolType symbol_type = symbol->GetType(); 391 switch (symbol_type) 392 { 393 case eSymbolTypeAny: return eAddressClassUnknown; 394 case eSymbolTypeAbsolute: return eAddressClassUnknown; 395 case eSymbolTypeCode: return eAddressClassCode; 396 case eSymbolTypeTrampoline: return eAddressClassCode; 397 case eSymbolTypeResolver: return eAddressClassCode; 398 case eSymbolTypeData: return eAddressClassData; 399 case eSymbolTypeRuntime: return eAddressClassRuntime; 400 case eSymbolTypeException: return eAddressClassRuntime; 401 case eSymbolTypeSourceFile: return eAddressClassDebug; 402 case eSymbolTypeHeaderFile: return eAddressClassDebug; 403 case eSymbolTypeObjectFile: return eAddressClassDebug; 404 case eSymbolTypeCommonBlock: return eAddressClassDebug; 405 case eSymbolTypeBlock: return eAddressClassDebug; 406 case eSymbolTypeLocal: return eAddressClassData; 407 case eSymbolTypeParam: return eAddressClassData; 408 case eSymbolTypeVariable: return eAddressClassData; 409 case eSymbolTypeVariableType: return eAddressClassDebug; 410 case eSymbolTypeLineEntry: return eAddressClassDebug; 411 case eSymbolTypeLineHeader: return eAddressClassDebug; 412 case eSymbolTypeScopeBegin: return eAddressClassDebug; 413 case eSymbolTypeScopeEnd: return eAddressClassDebug; 414 case eSymbolTypeAdditional: return eAddressClassUnknown; 415 case eSymbolTypeCompiler: return eAddressClassDebug; 416 case eSymbolTypeInstrumentation:return eAddressClassDebug; 417 case eSymbolTypeUndefined: return eAddressClassUnknown; 418 case eSymbolTypeObjCClass: return eAddressClassRuntime; 419 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime; 420 case eSymbolTypeObjCIVar: return eAddressClassRuntime; 421 } 422 } 423 } 424 return eAddressClassUnknown; 425 } 426 427 DataBufferSP 428 ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size) 429 { 430 DataBufferSP data_sp; 431 if (process_sp) 432 { 433 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0)); 434 Error error; 435 const size_t bytes_read = process_sp->ReadMemory (addr, 436 data_ap->GetBytes(), 437 data_ap->GetByteSize(), 438 error); 439 if (bytes_read == byte_size) 440 data_sp.reset (data_ap.release()); 441 } 442 return data_sp; 443 } 444 445 size_t 446 ObjectFile::GetData (off_t offset, size_t length, DataExtractor &data) const 447 { 448 // The entire file has already been mmap'ed into m_data, so just copy from there 449 // as the back mmap buffer will be shared with shared pointers. 450 return data.SetData (m_data, offset, length); 451 } 452 453 size_t 454 ObjectFile::CopyData (off_t offset, size_t length, void *dst) const 455 { 456 // The entire file has already been mmap'ed into m_data, so just copy from there 457 return m_data.CopyByteOrderedData (offset, length, dst, length, lldb::endian::InlHostByteOrder()); 458 } 459 460 461 size_t 462 ObjectFile::ReadSectionData (const Section *section, off_t section_offset, void *dst, size_t dst_len) const 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 uint64_t section_file_size = section->GetFileSize(); 482 if (section_offset < section_file_size) 483 { 484 const uint64_t section_bytes_left = section_file_size - section_offset; 485 uint64_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 bzero(dst, 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 (uint32_t flags) 590 { 591 ModuleSP module_sp(GetModule()); 592 if (module_sp) 593 { 594 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 595 bool unified_section_list = !!(flags & ObjectFile::eSymtabFromUnifiedSectionList); 596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 597 if (log) 598 { 599 log->Printf ("%p ObjectFile::ClearSymtab (%s) symtab = %p", 600 this, 601 unified_section_list ? "unified" : "", 602 unified_section_list ? m_symtab_unified_ap.get() : m_symtab_ap.get()); 603 } 604 if (unified_section_list) 605 { 606 m_symtab_unified_ap.reset(); 607 m_symtab_unified_revisionid = 0; 608 } 609 else 610 { 611 m_symtab_ap.reset(); 612 } 613 } 614 } 615