1 //===-- ObjectFile.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
28 using namespace lldb_private;
31 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp,
33 lldb::offset_t file_offset,
34 lldb::offset_t file_size,
35 DataBufferSP &data_sp,
36 lldb::offset_t &data_offset)
38 ObjectFileSP object_file_sp;
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);
48 FileSpec archive_file;
49 ObjectContainerCreateInstance create_object_container_callback;
51 const bool file_exists = file->Exists();
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
58 if (file_exists && module_sp->GetObjectName())
60 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != NULL; ++idx)
62 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
64 if (object_container_ap.get())
65 object_file_sp = object_container_ap->GetObjectFile(file);
67 if (object_file_sp.get())
68 return object_file_sp;
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.
77 data_sp = file->ReadFileContents(file_offset, std::min<size_t>(512, file_size));
82 if (!data_sp || data_sp->GetByteSize() == 0)
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));
88 ConstString archive_object;
89 const bool must_exist = true;
90 if (ObjectFile::SplitArchivePathWithObject (path_with_object, archive_file, archive_object, must_exist))
92 file_size = archive_file.GetByteSize();
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)
104 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
106 if (object_container_ap.get())
107 object_file_sp = object_container_ap->GetObjectFile(file);
109 if (object_file_sp.get())
110 return object_file_sp;
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);
119 if (data_sp && data_sp->GetByteSize() > 0)
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)
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;
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)
136 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
138 if (object_container_ap.get())
139 object_file_sp = object_container_ap->GetObjectFile(file);
141 if (object_file_sp.get())
142 return object_file_sp;
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;
154 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp,
155 const ProcessSP &process_sp,
156 lldb::addr_t header_addr,
157 DataBufferSP &data_sp)
159 ObjectFileSP object_file_sp;
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);
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)
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;
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;
187 ObjectFile::GetModuleSpecifications (const FileSpec &file,
188 lldb::offset_t file_offset,
189 lldb::offset_t file_size,
190 ModuleSpecList &specs)
192 DataBufferSP data_sp (file.ReadFileContents(file_offset, 512));
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;
201 return ObjectFile::GetModuleSpecifications (file, // file spec
202 data_sp, // data bytes
204 file_offset,// file offset
205 file_size, // file length
212 ObjectFile::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)
219 const size_t initial_count = specs.GetSize();
220 ObjectFileGetModuleSpecifications callback;
222 // Try the ObjectFile plug-ins
223 for (i = 0; (callback = PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(i)) != NULL; ++i)
225 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
226 return specs.GetSize() - initial_count;
229 // Try the ObjectContainer plug-ins
230 for (i = 0; (callback = PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != NULL; ++i)
232 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
233 return specs.GetSize() - initial_count;
238 ObjectFile::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
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),
252 m_unwind_table (*this),
254 m_memory_addr (LLDB_INVALID_ADDRESS),
259 m_file = *file_spec_ptr;
261 m_data.SetData (data_sp, data_offset, length);
262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
267 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
270 module_sp->GetSpecificationDescription().c_str(),
271 m_file.GetPath().c_str(),
277 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), file = <NULL>, file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
280 module_sp->GetSpecificationDescription().c_str(),
288 ObjectFile::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),
294 m_type (eTypeInvalid),
295 m_strata (eStrataInvalid),
299 m_unwind_table (*this),
300 m_process_wp (process_sp),
301 m_memory_addr (header_addr),
306 m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize());
307 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
310 log->Printf ("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, header_addr = 0x%" PRIx64,
313 module_sp->GetSpecificationDescription().c_str(),
320 ObjectFile::~ObjectFile()
322 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
324 log->Printf ("%p ObjectFile::~ObjectFile ()\n", this);
328 ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch)
330 ModuleSP module_sp (GetModule());
332 return module_sp->SetArchitecture (new_arch);
337 ObjectFile::GetAddressClass (addr_t file_addr)
339 Symtab *symtab = GetSymtab();
342 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
345 if (symbol->ValueIsAddress())
347 const SectionSP section_sp (symbol->GetAddress().GetSection());
350 const SectionType section_type = section_sp->GetType();
351 switch (section_type)
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;
395 const SymbolType symbol_type = symbol->GetType();
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;
429 return eAddressClassUnknown;
433 ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size)
435 DataBufferSP data_sp;
438 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0));
440 const size_t bytes_read = process_sp->ReadMemory (addr,
442 data_ap->GetByteSize(),
444 if (bytes_read == byte_size)
445 data_sp.reset (data_ap.release());
451 ObjectFile::GetData (off_t offset, size_t length, DataExtractor &data) const
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);
459 ObjectFile::CopyData (off_t offset, size_t length, void *dst) const
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());
467 ObjectFile::ReadSectionData (const Section *section, off_t section_offset, void *dst, size_t dst_len) const
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);
475 ProcessSP process_sp (m_process_wp.lock());
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);
486 const uint64_t section_file_size = section->GetFileSize();
487 if (section_offset < section_file_size)
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);
497 if (section->GetType() == eSectionTypeZeroFill)
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;
512 //----------------------------------------------------------------------
513 // Get the section data the file on disk
514 //----------------------------------------------------------------------
516 ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const
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);
524 ProcessSP process_sp (m_process_wp.lock());
527 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget());
528 if (base_load_addr != LLDB_INVALID_ADDRESS)
530 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize()));
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();
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);
546 section_data.Clear();
551 ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const
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);
559 return ReadSectionData (section, section_data);
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);
566 section_data.Clear();
572 ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist)
574 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$");
575 RegularExpression::Match regex_match(2);
576 if (g_object_regex.Execute (path_with_object, ®ex_match))
580 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) &&
581 regex_match.GetMatchAtIndex (path_with_object, 2, obj))
583 archive_file.SetFile (path.c_str(), false);
584 archive_object.SetCString(obj.c_str());
585 if (must_exist && !archive_file.Exists())
594 ObjectFile::ClearSymtab ()
596 ModuleSP module_sp(GetModule());
599 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
600 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
603 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p",
612 ObjectFile::GetSectionList()
614 if (m_sections_ap.get() == NULL)
616 ModuleSP module_sp(GetModule());
618 CreateSections(*module_sp->GetUnifiedSectionList());
620 return m_sections_ap.get();