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 case eSectionTypeCompactUnwind: 375 return eAddressClassRuntime; 376 case eSectionTypeELFSymbolTable: 377 case eSectionTypeELFDynamicSymbols: 378 case eSectionTypeELFRelocationEntries: 379 case eSectionTypeELFDynamicLinkInfo: 380 case eSectionTypeOther: 381 return eAddressClassUnknown; 382 } 383 } 384 } 385 386 const SymbolType symbol_type = symbol->GetType(); 387 switch (symbol_type) 388 { 389 case eSymbolTypeAny: return eAddressClassUnknown; 390 case eSymbolTypeAbsolute: return eAddressClassUnknown; 391 case eSymbolTypeCode: return eAddressClassCode; 392 case eSymbolTypeTrampoline: return eAddressClassCode; 393 case eSymbolTypeResolver: return eAddressClassCode; 394 case eSymbolTypeData: return eAddressClassData; 395 case eSymbolTypeRuntime: return eAddressClassRuntime; 396 case eSymbolTypeException: return eAddressClassRuntime; 397 case eSymbolTypeSourceFile: return eAddressClassDebug; 398 case eSymbolTypeHeaderFile: return eAddressClassDebug; 399 case eSymbolTypeObjectFile: return eAddressClassDebug; 400 case eSymbolTypeCommonBlock: return eAddressClassDebug; 401 case eSymbolTypeBlock: return eAddressClassDebug; 402 case eSymbolTypeLocal: return eAddressClassData; 403 case eSymbolTypeParam: return eAddressClassData; 404 case eSymbolTypeVariable: return eAddressClassData; 405 case eSymbolTypeVariableType: return eAddressClassDebug; 406 case eSymbolTypeLineEntry: return eAddressClassDebug; 407 case eSymbolTypeLineHeader: return eAddressClassDebug; 408 case eSymbolTypeScopeBegin: return eAddressClassDebug; 409 case eSymbolTypeScopeEnd: return eAddressClassDebug; 410 case eSymbolTypeAdditional: return eAddressClassUnknown; 411 case eSymbolTypeCompiler: return eAddressClassDebug; 412 case eSymbolTypeInstrumentation:return eAddressClassDebug; 413 case eSymbolTypeUndefined: return eAddressClassUnknown; 414 case eSymbolTypeObjCClass: return eAddressClassRuntime; 415 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime; 416 case eSymbolTypeObjCIVar: return eAddressClassRuntime; 417 case eSymbolTypeReExported: return eAddressClassRuntime; 418 } 419 } 420 } 421 return eAddressClassUnknown; 422 } 423 424 DataBufferSP 425 ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size) 426 { 427 DataBufferSP data_sp; 428 if (process_sp) 429 { 430 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0)); 431 Error error; 432 const size_t bytes_read = process_sp->ReadMemory (addr, 433 data_ap->GetBytes(), 434 data_ap->GetByteSize(), 435 error); 436 if (bytes_read == byte_size) 437 data_sp.reset (data_ap.release()); 438 } 439 return data_sp; 440 } 441 442 size_t 443 ObjectFile::GetData (lldb::offset_t offset, size_t length, DataExtractor &data) const 444 { 445 // The entire file has already been mmap'ed into m_data, so just copy from there 446 // as the back mmap buffer will be shared with shared pointers. 447 return data.SetData (m_data, offset, length); 448 } 449 450 size_t 451 ObjectFile::CopyData (lldb::offset_t offset, size_t length, void *dst) const 452 { 453 // The entire file has already been mmap'ed into m_data, so just copy from there 454 // Note that the data remains in target byte order. 455 return m_data.CopyData (offset, length, dst); 456 } 457 458 459 size_t 460 ObjectFile::ReadSectionData (const Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) const 461 { 462 assert(section); 463 section_offset *= section->GetTargetByteSize(); 464 465 // If some other objectfile owns this data, pass this to them. 466 if (section->GetObjectFile() != this) 467 return section->GetObjectFile()->ReadSectionData (section, section_offset, dst, dst_len); 468 469 if (IsInMemory()) 470 { 471 ProcessSP process_sp (m_process_wp.lock()); 472 if (process_sp) 473 { 474 Error error; 475 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 476 if (base_load_addr != LLDB_INVALID_ADDRESS) 477 return process_sp->ReadMemory (base_load_addr + section_offset, dst, dst_len, error); 478 } 479 } 480 else 481 { 482 const lldb::offset_t section_file_size = section->GetFileSize(); 483 if (section_offset < section_file_size) 484 { 485 const size_t section_bytes_left = section_file_size - section_offset; 486 size_t section_dst_len = dst_len; 487 if (section_dst_len > section_bytes_left) 488 section_dst_len = section_bytes_left; 489 return CopyData (section->GetFileOffset() + section_offset, section_dst_len, dst); 490 } 491 else 492 { 493 if (section->GetType() == eSectionTypeZeroFill) 494 { 495 const uint64_t section_size = section->GetByteSize(); 496 const uint64_t section_bytes_left = section_size - section_offset; 497 uint64_t section_dst_len = dst_len; 498 if (section_dst_len > section_bytes_left) 499 section_dst_len = section_bytes_left; 500 memset(dst, 0, section_dst_len); 501 return section_dst_len; 502 } 503 } 504 } 505 return 0; 506 } 507 508 //---------------------------------------------------------------------- 509 // Get the section data the file on disk 510 //---------------------------------------------------------------------- 511 size_t 512 ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const 513 { 514 // If some other objectfile owns this data, pass this to them. 515 if (section->GetObjectFile() != this) 516 return section->GetObjectFile()->ReadSectionData (section, section_data); 517 518 if (IsInMemory()) 519 { 520 ProcessSP process_sp (m_process_wp.lock()); 521 if (process_sp) 522 { 523 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 524 if (base_load_addr != LLDB_INVALID_ADDRESS) 525 { 526 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize())); 527 if (data_sp) 528 { 529 section_data.SetData (data_sp, 0, data_sp->GetByteSize()); 530 section_data.SetByteOrder (process_sp->GetByteOrder()); 531 section_data.SetAddressByteSize (process_sp->GetAddressByteSize()); 532 return section_data.GetByteSize(); 533 } 534 } 535 } 536 } 537 else 538 { 539 // The object file now contains a full mmap'ed copy of the object file data, so just use this 540 return MemoryMapSectionData (section, section_data); 541 } 542 section_data.Clear(); 543 return 0; 544 } 545 546 size_t 547 ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const 548 { 549 // If some other objectfile owns this data, pass this to them. 550 if (section->GetObjectFile() != this) 551 return section->GetObjectFile()->MemoryMapSectionData (section, section_data); 552 553 if (IsInMemory()) 554 { 555 return ReadSectionData (section, section_data); 556 } 557 else 558 { 559 // The object file now contains a full mmap'ed copy of the object file data, so just use this 560 return GetData(section->GetFileOffset(), section->GetFileSize(), section_data); 561 } 562 } 563 564 565 bool 566 ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist) 567 { 568 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$"); 569 RegularExpression::Match regex_match(2); 570 if (g_object_regex.Execute (path_with_object, ®ex_match)) 571 { 572 std::string path; 573 std::string obj; 574 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) && 575 regex_match.GetMatchAtIndex (path_with_object, 2, obj)) 576 { 577 archive_file.SetFile (path.c_str(), false); 578 archive_object.SetCString(obj.c_str()); 579 if (must_exist && !archive_file.Exists()) 580 return false; 581 return true; 582 } 583 } 584 return false; 585 } 586 587 void 588 ObjectFile::ClearSymtab () 589 { 590 ModuleSP module_sp(GetModule()); 591 if (module_sp) 592 { 593 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 594 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 595 if (log) 596 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p", 597 static_cast<void*>(this), 598 static_cast<void*>(m_symtab_ap.get())); 599 m_symtab_ap.reset(); 600 } 601 } 602 603 SectionList * 604 ObjectFile::GetSectionList() 605 { 606 if (m_sections_ap.get() == nullptr) 607 { 608 ModuleSP module_sp(GetModule()); 609 if (module_sp) 610 { 611 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 612 CreateSections(*module_sp->GetUnifiedSectionList()); 613 } 614 } 615 return m_sections_ap.get(); 616 } 617