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 static_cast<const void*>(file),
46 static_cast<uint64_t>(file_offset),
47 static_cast<uint64_t>(file_size));
50 FileSpec archive_file;
51 ObjectContainerCreateInstance create_object_container_callback;
53 const bool file_exists = file->Exists();
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
60 if (file_exists && module_sp->GetObjectName())
62 for (uint32_t idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != nullptr; ++idx)
64 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
66 if (object_container_ap.get())
67 object_file_sp = object_container_ap->GetObjectFile(file);
69 if (object_file_sp.get())
70 return object_file_sp;
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.
79 data_sp = file->ReadFileContents(file_offset, std::min<size_t>(512, file_size));
84 if (!data_sp || data_sp->GetByteSize() == 0)
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));
90 ConstString archive_object;
91 const bool must_exist = true;
92 if (ObjectFile::SplitArchivePathWithObject (path_with_object, archive_file, archive_object, must_exist))
94 file_size = archive_file.GetByteSize();
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)
106 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
108 if (object_container_ap.get())
109 object_file_sp = object_container_ap->GetObjectFile(file);
111 if (object_file_sp.get())
112 return object_file_sp;
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);
121 if (data_sp && data_sp->GetByteSize() > 0)
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)
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;
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)
138 std::unique_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module_sp, data_sp, data_offset, file, file_offset, file_size));
140 if (object_container_ap.get())
141 object_file_sp = object_container_ap->GetObjectFile(file);
143 if (object_file_sp.get())
144 return object_file_sp;
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;
156 ObjectFile::FindPlugin (const lldb::ModuleSP &module_sp,
157 const ProcessSP &process_sp,
158 lldb::addr_t header_addr,
159 DataBufferSP &data_sp)
161 ObjectFileSP object_file_sp;
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);
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)
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;
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;
189 ObjectFile::GetModuleSpecifications (const FileSpec &file,
190 lldb::offset_t file_offset,
191 lldb::offset_t file_size,
192 ModuleSpecList &specs)
194 DataBufferSP data_sp (file.ReadFileContents(file_offset, 512));
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;
203 return ObjectFile::GetModuleSpecifications (file, // file spec
204 data_sp, // data bytes
206 file_offset,// file offset
207 file_size, // file length
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)
221 const size_t initial_count = specs.GetSize();
222 ObjectFileGetModuleSpecifications callback;
224 // Try the ObjectFile plug-ins
225 for (i = 0; (callback = PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i)
227 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
228 return specs.GetSize() - initial_count;
231 // Try the ObjectContainer plug-ins
232 for (i = 0; (callback = PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != nullptr; ++i)
234 if (callback (file, data_sp, data_offset, file_offset, file_size, specs) > 0)
235 return specs.GetSize() - initial_count;
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
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),
254 m_unwind_table (*this),
256 m_memory_addr (LLDB_INVALID_ADDRESS),
261 m_file = *file_spec_ptr;
263 m_data.SetData (data_sp, data_offset, length);
264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
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);
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),
281 m_type (eTypeInvalid),
282 m_strata (eStrataInvalid),
286 m_unwind_table (*this),
287 m_process_wp (process_sp),
288 m_memory_addr (header_addr),
293 m_data.SetData (header_data_sp, 0, header_data_sp->GetByteSize());
294 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
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);
304 ObjectFile::~ObjectFile()
306 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
308 log->Printf ("%p ObjectFile::~ObjectFile ()\n",
309 static_cast<void*>(this));
313 ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch)
315 ModuleSP module_sp (GetModule());
317 return module_sp->SetArchitecture (new_arch);
322 ObjectFile::GetAddressClass (addr_t file_addr)
324 Symtab *symtab = GetSymtab();
327 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
330 if (symbol->ValueIsAddress())
332 const SectionSP section_sp (symbol->GetAddress().GetSection());
335 const SectionType section_type = section_sp->GetType();
336 switch (section_type)
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;
385 const SymbolType symbol_type = symbol->GetType();
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;
420 return eAddressClassUnknown;
424 ObjectFile::ReadMemory (const ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size)
426 DataBufferSP data_sp;
429 std::unique_ptr<DataBufferHeap> data_ap (new DataBufferHeap (byte_size, 0));
431 const size_t bytes_read = process_sp->ReadMemory (addr,
433 data_ap->GetByteSize(),
435 if (bytes_read == byte_size)
436 data_sp.reset (data_ap.release());
442 ObjectFile::GetData (lldb::offset_t offset, size_t length, DataExtractor &data) const
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);
450 ObjectFile::CopyData (lldb::offset_t offset, size_t length, void *dst) const
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);
459 ObjectFile::ReadSectionData (const Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) const
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);
467 ProcessSP process_sp (m_process_wp.lock());
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);
478 const lldb::offset_t section_file_size = section->GetFileSize();
479 if (section_offset < section_file_size)
481 const size_t section_bytes_left = section_file_size - section_offset;
482 size_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);
489 if (section->GetType() == eSectionTypeZeroFill)
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;
504 //----------------------------------------------------------------------
505 // Get the section data the file on disk
506 //----------------------------------------------------------------------
508 ObjectFile::ReadSectionData (const Section *section, DataExtractor& section_data) const
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);
516 ProcessSP process_sp (m_process_wp.lock());
519 const addr_t base_load_addr = section->GetLoadBaseAddress (&process_sp->GetTarget());
520 if (base_load_addr != LLDB_INVALID_ADDRESS)
522 DataBufferSP data_sp (ReadMemory (process_sp, base_load_addr, section->GetByteSize()));
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();
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);
538 section_data.Clear();
543 ObjectFile::MemoryMapSectionData (const Section *section, DataExtractor& section_data) const
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);
551 return ReadSectionData (section, section_data);
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);
558 section_data.Clear();
564 ObjectFile::SplitArchivePathWithObject (const char *path_with_object, FileSpec &archive_file, ConstString &archive_object, bool must_exist)
566 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$");
567 RegularExpression::Match regex_match(2);
568 if (g_object_regex.Execute (path_with_object, ®ex_match))
572 if (regex_match.GetMatchAtIndex (path_with_object, 1, path) &&
573 regex_match.GetMatchAtIndex (path_with_object, 2, obj))
575 archive_file.SetFile (path.c_str(), false);
576 archive_object.SetCString(obj.c_str());
577 if (must_exist && !archive_file.Exists())
586 ObjectFile::ClearSymtab ()
588 ModuleSP module_sp(GetModule());
591 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
592 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
594 log->Printf ("%p ObjectFile::ClearSymtab () symtab = %p",
595 static_cast<void*>(this),
596 static_cast<void*>(m_symtab_ap.get()));
602 ObjectFile::GetSectionList()
604 if (m_sections_ap.get() == nullptr)
606 ModuleSP module_sp(GetModule());
609 lldb_private::Mutex::Locker locker(module_sp->GetMutex());
610 CreateSections(*module_sp->GetUnifiedSectionList());
613 return m_sections_ap.get();