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 (off_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 (off_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, off_t section_offset, void *dst, size_t dst_len) const 460 { 461 // If some other objectfile owns this data, pass this to them. 462 if (section->GetObjectFile() != this) 463 return section->GetObjectFile()->ReadSectionData (section, section_offset, dst, dst_len); 464 465 if (IsInMemory()) 466 { 467 ProcessSP process_sp (m_process_wp.lock()); 468 if (process_sp) 469 { 470 Error error; 471 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 472 if (base_load_addr != LLDB_INVALID_ADDRESS) 473 return process_sp->ReadMemory (base_load_addr + section_offset, dst, dst_len, error); 474 } 475 } 476 else 477 { 478 const uint64_t section_file_size = section->GetFileSize(); 479 if (section_offset < static_cast<off_t>(section_file_size)) 480 { 481 const uint64_t section_bytes_left = section_file_size - section_offset; 482 uint64_t section_dst_len = dst_len; 483 if (section_dst_len > section_bytes_left) 484 section_dst_len = section_bytes_left; 485 return CopyData (section->GetFileOffset() + section_offset, section_dst_len, dst); 486 } 487 else 488 { 489 if (section->GetType() == eSectionTypeZeroFill) 490 { 491 const uint64_t section_size = section->GetByteSize(); 492 const uint64_t section_bytes_left = section_size - section_offset; 493 uint64_t section_dst_len = dst_len; 494 if (section_dst_len > section_bytes_left) 495 section_dst_len = section_bytes_left; 496 memset(dst, 0, section_dst_len); 497 return section_dst_len; 498 } 499 } 500 } 501 return 0; 502 } 503 504 //---------------------------------------------------------------------- 505 // Get the section data the file on disk 506 //---------------------------------------------------------------------- 507 size_t 508 ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const 509 { 510 // If some other objectfile owns this data, pass this to them. 511 if (section->GetObjectFile() != this) 512 return section->GetObjectFile()->ReadSectionData (section, section_data); 513 514 if (IsInMemory()) 515 { 516 ProcessSP process_sp (m_process_wp.lock()); 517 if (process_sp) 518 { 519 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 520 if (base_load_addr != LLDB_INVALID_ADDRESS) 521 { 522 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize())); 523 if (data_sp) 524 { 525 section_data.SetData (data_sp, 0, data_sp->GetByteSize()); 526 section_data.SetByteOrder (process_sp->GetByteOrder()); 527 section_data.SetAddressByteSize (process_sp->GetAddressByteSize()); 528 return section_data.GetByteSize(); 529 } 530 } 531 } 532 } 533 else 534 { 535 // The object file now contains a full mmap'ed copy of the object file data, so just use this 536 return MemoryMapSectionData (section, section_data); 537 } 538 section_data.Clear(); 539 return 0; 540 } 541 542 size_t 543 ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const 544 { 545 // If some other objectfile owns this data, pass this to them. 546 if (section->GetObjectFile() != this) 547 return section->GetObjectFile()->MemoryMapSectionData (section, section_data); 548 549 if (IsInMemory()) 550 { 551 return ReadSectionData (section, section_data); 552 } 553 else 554 { 555 // The object file now contains a full mmap'ed copy of the object file data, so just use this 556 return GetData(section->GetFileOffset(), section->GetFileSize(), section_data); 557 } 558 section_data.Clear(); 559 return 0; 560 } 561 562 563 bool 564 ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist) 565 { 566 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$"); 567 RegularExpression::Match regex_match(2); 568 if (g_object_regex.Execute (path_with_object, ®ex_match)) 569 { 570 std::string path; 571 std::string obj; 572 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) && 573 regex_match.GetMatchAtIndex (path_with_object, 2, obj)) 574 { 575 archive_file.SetFile (path.c_str(), false); 576 archive_object.SetCString(obj.c_str()); 577 if (must_exist && !archive_file.Exists()) 578 return false; 579 return true; 580 } 581 } 582 return false; 583 } 584 585 void 586 ObjectFile::ClearSymtab () 587 { 588 ModuleSP module_sp(GetModule()); 589 if (module_sp) 590 { 591 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 592 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 593 if (log) 594 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p", 595 static_cast<void*>(this), 596 static_cast<void*>(m_symtab_ap.get())); 597 m_symtab_ap.reset(); 598 } 599 } 600 601 SectionList * 602 ObjectFile::GetSectionList() 603 { 604 if (m_sections_ap.get() == nullptr) 605 { 606 ModuleSP module_sp(GetModule()); 607 if (module_sp) 608 CreateSections(*module_sp->GetUnifiedSectionList()); 609 } 610 return m_sections_ap.get(); 611 } 612