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 27using namespace lldb; 28using namespace lldb_private; 29 30ObjectFileSP 31ObjectFile::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 153ObjectFileSP 154ObjectFile::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 186size_t 187ObjectFile::GetModuleSpecifications (const FileSpec &file, 188 lldb::offset_t file_offset, 189 lldb::offset_t file_size, 190 ModuleSpecList &specs) 191{ 192 DataBufferSP data_sp (file.ReadFileContents(file_offset, 512)); 193 if (data_sp) 194 { 195 if (file_size == 0) 196 { 197 const lldb::offset_t actual_file_size = file.GetByteSize(); 198 if (actual_file_size > file_offset) 199 file_size = actual_file_size - file_offset; 200 } 201 return ObjectFile::GetModuleSpecifications (file, // file spec 202 data_sp, // data bytes 203 0, // data offset 204 file_offset,// file offset 205 file_size, // file length 206 specs); 207 } 208 return 0; 209} 210 211size_t 212ObjectFile::GetModuleSpecifications (const lldb_private::FileSpec& file, 213 lldb::DataBufferSP& data_sp, 214 lldb::offset_t data_offset, 215 lldb::offset_t file_offset, 216 lldb::offset_t file_size, 217 lldb_private::ModuleSpecList &specs) 218{ 219 const size_t initial_count = specs.GetSize(); 220 ObjectFileGetModuleSpecifications callback; 221 uint32_t i; 222 // Try the ObjectFile plug-ins 223 for (i = 0; (callback = PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(i)) != NULL; ++i) 224 { 225 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0) 226 return specs.GetSize() - initial_count; 227 } 228 229 // Try the ObjectContainer plug-ins 230 for (i = 0; (callback = PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != NULL; ++i) 231 { 232 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0) 233 return specs.GetSize() - initial_count; 234 } 235 return 0; 236} 237 238ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp, 239 const FileSpec *file_spec_ptr, 240 lldb::offset_t file_offset, 241 lldb::offset_t length, 242 lldb::DataBufferSP& data_sp, 243 lldb::offset_t data_offset 244) : 245 ModuleChild (module_sp), 246 m_file (), // This file could be different from the original module's file 247 m_type (eTypeInvalid), 248 m_strata (eStrataInvalid), 249 m_file_offset (file_offset), 250 m_length (length), 251 m_data (), 252 m_unwind_table (*this), 253 m_process_wp(), 254 m_memory_addr (LLDB_INVALID_ADDRESS), 255 m_sections_ap(), 256 m_symtab_ap () 257{ 258 if (file_spec_ptr) 259 m_file = *file_spec_ptr; 260 if (data_sp) 261 m_data.SetData (data_sp, data_offset, length); 262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 263 if (log) 264 { 265 if (m_file) 266 { 267 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, 268 this, 269 module_sp.get(), 270 module_sp->GetSpecificationDescription().c_str(), 271 m_file.GetPath().c_str(), 272 m_file_offset, 273 m_length); 274 } 275 else 276 { 277 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = <NULL>, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, 278 this, 279 module_sp.get(), 280 module_sp->GetSpecificationDescription().c_str(), 281 m_file_offset, 282 m_length); 283 } 284 } 285} 286 287 288ObjectFile::ObjectFile (const lldb::ModuleSP &module_sp, 289 const ProcessSP &process_sp, 290 lldb::addr_t header_addr, 291 DataBufferSP& header_data_sp) : 292 ModuleChild (module_sp), 293 m_file (), 294 m_type (eTypeInvalid), 295 m_strata (eStrataInvalid), 296 m_file_offset (0), 297 m_length (0), 298 m_data (), 299 m_unwind_table (*this), 300 m_process_wp (process_sp), 301 m_memory_addr (header_addr), 302 m_sections_ap(), 303 m_symtab_ap () 304{ 305 if (header_data_sp) 306 m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize()); 307 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 308 if (log) 309 { 310 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, header_addr = 0x%" PRIx64, 311 this, 312 module_sp.get(), 313 module_sp->GetSpecificationDescription().c_str(), 314 process_sp.get(), 315 m_memory_addr); 316 } 317} 318 319 320ObjectFile::~ObjectFile() 321{ 322 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 323 if (log) 324 log->Printf ("%p ObjectFile::~ObjectFile ()\n", this); 325} 326 327bool 328ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch) 329{ 330 ModuleSP module_sp (GetModule()); 331 if (module_sp) 332 return module_sp->SetArchitecture (new_arch); 333 return false; 334} 335 336AddressClass 337ObjectFile::GetAddressClass (addr_t file_addr) 338{ 339 Symtab *symtab = GetSymtab(); 340 if (symtab) 341 { 342 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 343 if (symbol) 344 { 345 if (symbol->ValueIsAddress()) 346 { 347 const SectionSP section_sp (symbol->GetAddress().GetSection()); 348 if (section_sp) 349 { 350 const SectionType section_type = section_sp->GetType(); 351 switch (section_type) 352 { 353 case eSectionTypeInvalid: return eAddressClassUnknown; 354 case eSectionTypeCode: return eAddressClassCode; 355 case eSectionTypeContainer: return eAddressClassUnknown; 356 case eSectionTypeData: 357 case eSectionTypeDataCString: 358 case eSectionTypeDataCStringPointers: 359 case eSectionTypeDataSymbolAddress: 360 case eSectionTypeData4: 361 case eSectionTypeData8: 362 case eSectionTypeData16: 363 case eSectionTypeDataPointers: 364 case eSectionTypeZeroFill: 365 case eSectionTypeDataObjCMessageRefs: 366 case eSectionTypeDataObjCCFStrings: 367 return eAddressClassData; 368 case eSectionTypeDebug: 369 case eSectionTypeDWARFDebugAbbrev: 370 case eSectionTypeDWARFDebugAranges: 371 case eSectionTypeDWARFDebugFrame: 372 case eSectionTypeDWARFDebugInfo: 373 case eSectionTypeDWARFDebugLine: 374 case eSectionTypeDWARFDebugLoc: 375 case eSectionTypeDWARFDebugMacInfo: 376 case eSectionTypeDWARFDebugPubNames: 377 case eSectionTypeDWARFDebugPubTypes: 378 case eSectionTypeDWARFDebugRanges: 379 case eSectionTypeDWARFDebugStr: 380 case eSectionTypeDWARFAppleNames: 381 case eSectionTypeDWARFAppleTypes: 382 case eSectionTypeDWARFAppleNamespaces: 383 case eSectionTypeDWARFAppleObjC: 384 return eAddressClassDebug; 385 case eSectionTypeEHFrame: return eAddressClassRuntime; 386 case eSectionTypeELFSymbolTable: 387 case eSectionTypeELFDynamicSymbols: 388 case eSectionTypeELFRelocationEntries: 389 case eSectionTypeELFDynamicLinkInfo: 390 case eSectionTypeOther: return eAddressClassUnknown; 391 } 392 } 393 } 394 395 const SymbolType symbol_type = symbol->GetType(); 396 switch (symbol_type) 397 { 398 case eSymbolTypeAny: return eAddressClassUnknown; 399 case eSymbolTypeAbsolute: return eAddressClassUnknown; 400 case eSymbolTypeCode: return eAddressClassCode; 401 case eSymbolTypeTrampoline: return eAddressClassCode; 402 case eSymbolTypeResolver: return eAddressClassCode; 403 case eSymbolTypeData: return eAddressClassData; 404 case eSymbolTypeRuntime: return eAddressClassRuntime; 405 case eSymbolTypeException: return eAddressClassRuntime; 406 case eSymbolTypeSourceFile: return eAddressClassDebug; 407 case eSymbolTypeHeaderFile: return eAddressClassDebug; 408 case eSymbolTypeObjectFile: return eAddressClassDebug; 409 case eSymbolTypeCommonBlock: return eAddressClassDebug; 410 case eSymbolTypeBlock: return eAddressClassDebug; 411 case eSymbolTypeLocal: return eAddressClassData; 412 case eSymbolTypeParam: return eAddressClassData; 413 case eSymbolTypeVariable: return eAddressClassData; 414 case eSymbolTypeVariableType: return eAddressClassDebug; 415 case eSymbolTypeLineEntry: return eAddressClassDebug; 416 case eSymbolTypeLineHeader: return eAddressClassDebug; 417 case eSymbolTypeScopeBegin: return eAddressClassDebug; 418 case eSymbolTypeScopeEnd: return eAddressClassDebug; 419 case eSymbolTypeAdditional: return eAddressClassUnknown; 420 case eSymbolTypeCompiler: return eAddressClassDebug; 421 case eSymbolTypeInstrumentation:return eAddressClassDebug; 422 case eSymbolTypeUndefined: return eAddressClassUnknown; 423 case eSymbolTypeObjCClass: return eAddressClassRuntime; 424 case eSymbolTypeObjCMetaClass: return eAddressClassRuntime; 425 case eSymbolTypeObjCIVar: return eAddressClassRuntime; 426 } 427 } 428 } 429 return eAddressClassUnknown; 430} 431 432DataBufferSP 433ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size) 434{ 435 DataBufferSP data_sp; 436 if (process_sp) 437 { 438 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0)); 439 Error error; 440 const size_t bytes_read = process_sp->ReadMemory (addr, 441 data_ap->GetBytes(), 442 data_ap->GetByteSize(), 443 error); 444 if (bytes_read == byte_size) 445 data_sp.reset (data_ap.release()); 446 } 447 return data_sp; 448} 449 450size_t 451ObjectFile::GetData (off_t offset, size_t length, DataExtractor &data) const 452{ 453 // The entire file has already been mmap'ed into m_data, so just copy from there 454 // as the back mmap buffer will be shared with shared pointers. 455 return data.SetData (m_data, offset, length); 456} 457 458size_t 459ObjectFile::CopyData (off_t offset, size_t length, void *dst) const 460{ 461 // The entire file has already been mmap'ed into m_data, so just copy from there 462 return m_data.CopyByteOrderedData (offset, length, dst, length, lldb::endian::InlHostByteOrder()); 463} 464 465 466size_t 467ObjectFile::ReadSectionData (const Section *section, off_t section_offset, void *dst, size_t dst_len) const 468{ 469 // If some other objectfile owns this data, pass this to them. 470 if (section->GetObjectFile() != this) 471 return section->GetObjectFile()->ReadSectionData (section, section_offset, dst, dst_len); 472 473 if (IsInMemory()) 474 { 475 ProcessSP process_sp (m_process_wp.lock()); 476 if (process_sp) 477 { 478 Error error; 479 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 480 if (base_load_addr != LLDB_INVALID_ADDRESS) 481 return process_sp->ReadMemory (base_load_addr + section_offset, dst, dst_len, error); 482 } 483 } 484 else 485 { 486 const uint64_t section_file_size = section->GetFileSize(); 487 if (section_offset < section_file_size) 488 { 489 const uint64_t section_bytes_left = section_file_size - section_offset; 490 uint64_t section_dst_len = dst_len; 491 if (section_dst_len > section_bytes_left) 492 section_dst_len = section_bytes_left; 493 return CopyData (section->GetFileOffset() + section_offset, section_dst_len, dst); 494 } 495 else 496 { 497 if (section->GetType() == eSectionTypeZeroFill) 498 { 499 const uint64_t section_size = section->GetByteSize(); 500 const uint64_t section_bytes_left = section_size - section_offset; 501 uint64_t section_dst_len = dst_len; 502 if (section_dst_len > section_bytes_left) 503 section_dst_len = section_bytes_left; 504 bzero(dst, section_dst_len); 505 return section_dst_len; 506 } 507 } 508 } 509 return 0; 510} 511 512//---------------------------------------------------------------------- 513// Get the section data the file on disk 514//---------------------------------------------------------------------- 515size_t 516ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const 517{ 518 // If some other objectfile owns this data, pass this to them. 519 if (section->GetObjectFile() != this) 520 return section->GetObjectFile()->ReadSectionData (section, section_data); 521 522 if (IsInMemory()) 523 { 524 ProcessSP process_sp (m_process_wp.lock()); 525 if (process_sp) 526 { 527 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget()); 528 if (base_load_addr != LLDB_INVALID_ADDRESS) 529 { 530 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize())); 531 if (data_sp) 532 { 533 section_data.SetData (data_sp, 0, data_sp->GetByteSize()); 534 section_data.SetByteOrder (process_sp->GetByteOrder()); 535 section_data.SetAddressByteSize (process_sp->GetAddressByteSize()); 536 return section_data.GetByteSize(); 537 } 538 } 539 } 540 } 541 else 542 { 543 // The object file now contains a full mmap'ed copy of the object file data, so just use this 544 return MemoryMapSectionData (section, section_data); 545 } 546 section_data.Clear(); 547 return 0; 548} 549 550size_t 551ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const 552{ 553 // If some other objectfile owns this data, pass this to them. 554 if (section->GetObjectFile() != this) 555 return section->GetObjectFile()->MemoryMapSectionData (section, section_data); 556 557 if (IsInMemory()) 558 { 559 return ReadSectionData (section, section_data); 560 } 561 else 562 { 563 // The object file now contains a full mmap'ed copy of the object file data, so just use this 564 return GetData(section->GetFileOffset(), section->GetFileSize(), section_data); 565 } 566 section_data.Clear(); 567 return 0; 568} 569 570 571bool 572ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist) 573{ 574 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$"); 575 RegularExpression::Match regex_match(2); 576 if (g_object_regex.Execute (path_with_object, ®ex_match)) 577 { 578 std::string path; 579 std::string obj; 580 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) && 581 regex_match.GetMatchAtIndex (path_with_object, 2, obj)) 582 { 583 archive_file.SetFile (path.c_str(), false); 584 archive_object.SetCString(obj.c_str()); 585 if (must_exist && !archive_file.Exists()) 586 return false; 587 return true; 588 } 589 } 590 return false; 591} 592 593void 594ObjectFile::ClearSymtab () 595{ 596 ModuleSP module_sp(GetModule()); 597 if (module_sp) 598 { 599 lldb_private::Mutex::Locker locker(module_sp->GetMutex()); 600 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 601 if (log) 602 { 603 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p", 604 this, 605 m_symtab_ap.get()); 606 } 607 m_symtab_ap.reset(); 608 } 609} 610 611SectionList * 612ObjectFile::GetSectionList() 613{ 614 if (m_sections_ap.get() == NULL) 615 { 616 ModuleSP module_sp(GetModule()); 617 if (module_sp) 618 CreateSections(*module_sp->GetUnifiedSectionList()); 619 } 620 return m_sections_ap.get(); 621} 622