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/Symbol/ObjectFile.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Symbol/ObjectContainer.h"
16 #include "lldb/Symbol/SymbolFile.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/SectionLoadList.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/DataBuffer.h"
21 #include "lldb/Utility/DataBufferHeap.h"
22 #include "lldb/Utility/Log.h"
23 #include "lldb/Utility/RegularExpression.h"
24 #include "lldb/Utility/Timer.h"
25 #include "lldb/lldb-private.h"
26
27 using namespace lldb;
28 using namespace lldb_private;
29
30 ObjectFileSP
FindPlugin(const lldb::ModuleSP & module_sp,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t file_size,DataBufferSP & data_sp,lldb::offset_t & data_offset)31 ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file,
32 lldb::offset_t file_offset, lldb::offset_t file_size,
33 DataBufferSP &data_sp, lldb::offset_t &data_offset) {
34 ObjectFileSP object_file_sp;
35
36 if (module_sp) {
37 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
38 Timer scoped_timer(
39 func_cat,
40 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "
41 "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
42 module_sp->GetFileSpec().GetPath().c_str(),
43 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
44 static_cast<uint64_t>(file_size));
45 if (file) {
46 FileSpec archive_file;
47 ObjectContainerCreateInstance create_object_container_callback;
48
49 const bool file_exists = FileSystem::Instance().Exists(*file);
50 if (!data_sp) {
51 // We have an object name which most likely means we have a .o file in
52 // a static archive (.a file). Try and see if we have a cached archive
53 // first without reading any data first
54 if (file_exists && module_sp->GetObjectName()) {
55 for (uint32_t idx = 0;
56 (create_object_container_callback =
57 PluginManager::GetObjectContainerCreateCallbackAtIndex(
58 idx)) != nullptr;
59 ++idx) {
60 std::unique_ptr<ObjectContainer> object_container_ap(
61 create_object_container_callback(module_sp, data_sp,
62 data_offset, file, file_offset,
63 file_size));
64
65 if (object_container_ap.get())
66 object_file_sp = object_container_ap->GetObjectFile(file);
67
68 if (object_file_sp.get())
69 return object_file_sp;
70 }
71 }
72 // Ok, we didn't find any containers that have a named object, now lets
73 // read the first 512 bytes from the file so the object file and object
74 // container plug-ins can use these bytes to see if they can parse this
75 // file.
76 if (file_size > 0) {
77 data_sp = FileSystem::Instance().CreateDataBuffer(file->GetPath(),
78 512, file_offset);
79 data_offset = 0;
80 }
81 }
82
83 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,
87 sizeof(path_with_object));
88
89 ConstString archive_object;
90 const bool must_exist = true;
91 if (ObjectFile::SplitArchivePathWithObject(
92 path_with_object, archive_file, archive_object, must_exist)) {
93 file_size = FileSystem::Instance().GetByteSize(archive_file);
94 if (file_size > 0) {
95 file = &archive_file;
96 module_sp->SetFileSpecAndObjectName(archive_file, archive_object);
97 // Check if this is a object container by iterating through all
98 // object container plugin instances and then trying to get an
99 // object file from the container plugins since we had a name.
100 // Also, don't read
101 // ANY data in case there is data cached in the container plug-ins
102 // (like BSD archives caching the contained objects within an
103 // file).
104 for (uint32_t idx = 0;
105 (create_object_container_callback =
106 PluginManager::GetObjectContainerCreateCallbackAtIndex(
107 idx)) != nullptr;
108 ++idx) {
109 std::unique_ptr<ObjectContainer> object_container_ap(
110 create_object_container_callback(module_sp, data_sp,
111 data_offset, file,
112 file_offset, file_size));
113
114 if (object_container_ap.get())
115 object_file_sp = object_container_ap->GetObjectFile(file);
116
117 if (object_file_sp.get())
118 return object_file_sp;
119 }
120 // We failed to find any cached object files in the container plug-
121 // ins, so lets read the first 512 bytes and try again below...
122 data_sp = FileSystem::Instance().CreateDataBuffer(
123 archive_file.GetPath(), 512, file_offset);
124 }
125 }
126 }
127
128 if (data_sp && data_sp->GetByteSize() > 0) {
129 // Check if this is a normal object file by iterating through all
130 // object file plugin instances.
131 ObjectFileCreateInstance create_object_file_callback;
132 for (uint32_t idx = 0;
133 (create_object_file_callback =
134 PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) !=
135 nullptr;
136 ++idx) {
137 object_file_sp.reset(create_object_file_callback(
138 module_sp, data_sp, data_offset, file, file_offset, file_size));
139 if (object_file_sp.get())
140 return object_file_sp;
141 }
142
143 // Check if this is a object container by iterating through all object
144 // container plugin instances and then trying to get an object file
145 // from the container.
146 for (uint32_t idx = 0;
147 (create_object_container_callback =
148 PluginManager::GetObjectContainerCreateCallbackAtIndex(
149 idx)) != nullptr;
150 ++idx) {
151 std::unique_ptr<ObjectContainer> object_container_ap(
152 create_object_container_callback(module_sp, data_sp, data_offset,
153 file, file_offset, file_size));
154
155 if (object_container_ap.get())
156 object_file_sp = object_container_ap->GetObjectFile(file);
157
158 if (object_file_sp.get())
159 return object_file_sp;
160 }
161 }
162 }
163 }
164 // We didn't find it, so clear our shared pointer in case it contains
165 // anything and return an empty shared pointer
166 object_file_sp.reset();
167 return object_file_sp;
168 }
169
FindPlugin(const lldb::ModuleSP & module_sp,const ProcessSP & process_sp,lldb::addr_t header_addr,DataBufferSP & data_sp)170 ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp,
171 const ProcessSP &process_sp,
172 lldb::addr_t header_addr,
173 DataBufferSP &data_sp) {
174 ObjectFileSP object_file_sp;
175
176 if (module_sp) {
177 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
178 Timer scoped_timer(func_cat,
179 "ObjectFile::FindPlugin (module = "
180 "%s, process = %p, header_addr = "
181 "0x%" PRIx64 ")",
182 module_sp->GetFileSpec().GetPath().c_str(),
183 static_cast<void *>(process_sp.get()), header_addr);
184 uint32_t idx;
185
186 // Check if this is a normal object file by iterating through all object
187 // file plugin instances.
188 ObjectFileCreateMemoryInstance create_callback;
189 for (idx = 0;
190 (create_callback =
191 PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) !=
192 nullptr;
193 ++idx) {
194 object_file_sp.reset(
195 create_callback(module_sp, data_sp, process_sp, header_addr));
196 if (object_file_sp.get())
197 return object_file_sp;
198 }
199 }
200
201 // We didn't find it, so clear our shared pointer in case it contains
202 // anything and return an empty shared pointer
203 object_file_sp.reset();
204 return object_file_sp;
205 }
206
GetModuleSpecifications(const FileSpec & file,lldb::offset_t file_offset,lldb::offset_t file_size,ModuleSpecList & specs)207 size_t ObjectFile::GetModuleSpecifications(const FileSpec &file,
208 lldb::offset_t file_offset,
209 lldb::offset_t file_size,
210 ModuleSpecList &specs) {
211 DataBufferSP data_sp =
212 FileSystem::Instance().CreateDataBuffer(file.GetPath(), 512, file_offset);
213 if (data_sp) {
214 if (file_size == 0) {
215 const lldb::offset_t actual_file_size =
216 FileSystem::Instance().GetByteSize(file);
217 if (actual_file_size > file_offset)
218 file_size = actual_file_size - file_offset;
219 }
220 return ObjectFile::GetModuleSpecifications(file, // file spec
221 data_sp, // data bytes
222 0, // data offset
223 file_offset, // file offset
224 file_size, // file length
225 specs);
226 }
227 return 0;
228 }
229
GetModuleSpecifications(const lldb_private::FileSpec & file,lldb::DataBufferSP & data_sp,lldb::offset_t data_offset,lldb::offset_t file_offset,lldb::offset_t file_size,lldb_private::ModuleSpecList & specs)230 size_t ObjectFile::GetModuleSpecifications(
231 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
232 lldb::offset_t data_offset, lldb::offset_t file_offset,
233 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
234 const size_t initial_count = specs.GetSize();
235 ObjectFileGetModuleSpecifications callback;
236 uint32_t i;
237 // Try the ObjectFile plug-ins
238 for (i = 0;
239 (callback =
240 PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(
241 i)) != nullptr;
242 ++i) {
243 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
244 return specs.GetSize() - initial_count;
245 }
246
247 // Try the ObjectContainer plug-ins
248 for (i = 0;
249 (callback = PluginManager::
250 GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) !=
251 nullptr;
252 ++i) {
253 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
254 return specs.GetSize() - initial_count;
255 }
256 return 0;
257 }
258
ObjectFile(const lldb::ModuleSP & module_sp,const FileSpec * file_spec_ptr,lldb::offset_t file_offset,lldb::offset_t length,const lldb::DataBufferSP & data_sp,lldb::offset_t data_offset)259 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
260 const FileSpec *file_spec_ptr,
261 lldb::offset_t file_offset, lldb::offset_t length,
262 const lldb::DataBufferSP &data_sp,
263 lldb::offset_t data_offset)
264 : ModuleChild(module_sp),
265 m_file(), // This file could be different from the original module's file
266 m_type(eTypeInvalid), m_strata(eStrataInvalid),
267 m_file_offset(file_offset), m_length(length), m_data(),
268 m_unwind_table(*this), m_process_wp(),
269 m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_ap(), m_symtab_ap(),
270 m_synthetic_symbol_idx(0) {
271 if (file_spec_ptr)
272 m_file = *file_spec_ptr;
273 if (data_sp)
274 m_data.SetData(data_sp, data_offset, length);
275 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
276 if (log)
277 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
278 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
279 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
280 module_sp->GetSpecificationDescription().c_str(),
281 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
282 m_length);
283 }
284
ObjectFile(const lldb::ModuleSP & module_sp,const ProcessSP & process_sp,lldb::addr_t header_addr,DataBufferSP & header_data_sp)285 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
286 const ProcessSP &process_sp, lldb::addr_t header_addr,
287 DataBufferSP &header_data_sp)
288 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
289 m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(),
290 m_unwind_table(*this), m_process_wp(process_sp),
291 m_memory_addr(header_addr), m_sections_ap(), m_symtab_ap(),
292 m_synthetic_symbol_idx(0) {
293 if (header_data_sp)
294 m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize());
295 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
296 if (log)
297 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
298 "header_addr = 0x%" PRIx64,
299 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
300 module_sp->GetSpecificationDescription().c_str(),
301 static_cast<void *>(process_sp.get()), m_memory_addr);
302 }
303
~ObjectFile()304 ObjectFile::~ObjectFile() {
305 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
306 if (log)
307 log->Printf("%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
308 }
309
SetModulesArchitecture(const ArchSpec & new_arch)310 bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) {
311 ModuleSP module_sp(GetModule());
312 if (module_sp)
313 return module_sp->SetArchitecture(new_arch);
314 return false;
315 }
316
GetAddressClass(addr_t file_addr)317 AddressClass ObjectFile::GetAddressClass(addr_t file_addr) {
318 Symtab *symtab = GetSymtab();
319 if (symtab) {
320 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
321 if (symbol) {
322 if (symbol->ValueIsAddress()) {
323 const SectionSP section_sp(symbol->GetAddressRef().GetSection());
324 if (section_sp) {
325 const SectionType section_type = section_sp->GetType();
326 switch (section_type) {
327 case eSectionTypeInvalid:
328 return AddressClass::eUnknown;
329 case eSectionTypeCode:
330 return AddressClass::eCode;
331 case eSectionTypeContainer:
332 return AddressClass::eUnknown;
333 case eSectionTypeData:
334 case eSectionTypeDataCString:
335 case eSectionTypeDataCStringPointers:
336 case eSectionTypeDataSymbolAddress:
337 case eSectionTypeData4:
338 case eSectionTypeData8:
339 case eSectionTypeData16:
340 case eSectionTypeDataPointers:
341 case eSectionTypeZeroFill:
342 case eSectionTypeDataObjCMessageRefs:
343 case eSectionTypeDataObjCCFStrings:
344 case eSectionTypeGoSymtab:
345 return AddressClass::eData;
346 case eSectionTypeDebug:
347 case eSectionTypeDWARFDebugAbbrev:
348 case eSectionTypeDWARFDebugAbbrevDwo:
349 case eSectionTypeDWARFDebugAddr:
350 case eSectionTypeDWARFDebugAranges:
351 case eSectionTypeDWARFDebugCuIndex:
352 case eSectionTypeDWARFDebugFrame:
353 case eSectionTypeDWARFDebugInfo:
354 case eSectionTypeDWARFDebugInfoDwo:
355 case eSectionTypeDWARFDebugLine:
356 case eSectionTypeDWARFDebugLineStr:
357 case eSectionTypeDWARFDebugLoc:
358 case eSectionTypeDWARFDebugLocLists:
359 case eSectionTypeDWARFDebugMacInfo:
360 case eSectionTypeDWARFDebugMacro:
361 case eSectionTypeDWARFDebugNames:
362 case eSectionTypeDWARFDebugPubNames:
363 case eSectionTypeDWARFDebugPubTypes:
364 case eSectionTypeDWARFDebugRanges:
365 case eSectionTypeDWARFDebugRngLists:
366 case eSectionTypeDWARFDebugStr:
367 case eSectionTypeDWARFDebugStrDwo:
368 case eSectionTypeDWARFDebugStrOffsets:
369 case eSectionTypeDWARFDebugStrOffsetsDwo:
370 case eSectionTypeDWARFDebugTypes:
371 case eSectionTypeDWARFAppleNames:
372 case eSectionTypeDWARFAppleTypes:
373 case eSectionTypeDWARFAppleNamespaces:
374 case eSectionTypeDWARFAppleObjC:
375 case eSectionTypeDWARFGNUDebugAltLink:
376 return AddressClass::eDebug;
377 case eSectionTypeEHFrame:
378 case eSectionTypeARMexidx:
379 case eSectionTypeARMextab:
380 case eSectionTypeCompactUnwind:
381 return AddressClass::eRuntime;
382 case eSectionTypeELFSymbolTable:
383 case eSectionTypeELFDynamicSymbols:
384 case eSectionTypeELFRelocationEntries:
385 case eSectionTypeELFDynamicLinkInfo:
386 case eSectionTypeOther:
387 return AddressClass::eUnknown;
388 case eSectionTypeAbsoluteAddress:
389 // In case of absolute sections decide the address class based on
390 // the symbol type because the section type isn't specify if it is
391 // a code or a data section.
392 break;
393 }
394 }
395 }
396
397 const SymbolType symbol_type = symbol->GetType();
398 switch (symbol_type) {
399 case eSymbolTypeAny:
400 return AddressClass::eUnknown;
401 case eSymbolTypeAbsolute:
402 return AddressClass::eUnknown;
403 case eSymbolTypeCode:
404 return AddressClass::eCode;
405 case eSymbolTypeTrampoline:
406 return AddressClass::eCode;
407 case eSymbolTypeResolver:
408 return AddressClass::eCode;
409 case eSymbolTypeData:
410 return AddressClass::eData;
411 case eSymbolTypeRuntime:
412 return AddressClass::eRuntime;
413 case eSymbolTypeException:
414 return AddressClass::eRuntime;
415 case eSymbolTypeSourceFile:
416 return AddressClass::eDebug;
417 case eSymbolTypeHeaderFile:
418 return AddressClass::eDebug;
419 case eSymbolTypeObjectFile:
420 return AddressClass::eDebug;
421 case eSymbolTypeCommonBlock:
422 return AddressClass::eDebug;
423 case eSymbolTypeBlock:
424 return AddressClass::eDebug;
425 case eSymbolTypeLocal:
426 return AddressClass::eData;
427 case eSymbolTypeParam:
428 return AddressClass::eData;
429 case eSymbolTypeVariable:
430 return AddressClass::eData;
431 case eSymbolTypeVariableType:
432 return AddressClass::eDebug;
433 case eSymbolTypeLineEntry:
434 return AddressClass::eDebug;
435 case eSymbolTypeLineHeader:
436 return AddressClass::eDebug;
437 case eSymbolTypeScopeBegin:
438 return AddressClass::eDebug;
439 case eSymbolTypeScopeEnd:
440 return AddressClass::eDebug;
441 case eSymbolTypeAdditional:
442 return AddressClass::eUnknown;
443 case eSymbolTypeCompiler:
444 return AddressClass::eDebug;
445 case eSymbolTypeInstrumentation:
446 return AddressClass::eDebug;
447 case eSymbolTypeUndefined:
448 return AddressClass::eUnknown;
449 case eSymbolTypeObjCClass:
450 return AddressClass::eRuntime;
451 case eSymbolTypeObjCMetaClass:
452 return AddressClass::eRuntime;
453 case eSymbolTypeObjCIVar:
454 return AddressClass::eRuntime;
455 case eSymbolTypeReExported:
456 return AddressClass::eRuntime;
457 }
458 }
459 }
460 return AddressClass::eUnknown;
461 }
462
ReadMemory(const ProcessSP & process_sp,lldb::addr_t addr,size_t byte_size)463 DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp,
464 lldb::addr_t addr, size_t byte_size) {
465 DataBufferSP data_sp;
466 if (process_sp) {
467 std::unique_ptr<DataBufferHeap> data_ap(new DataBufferHeap(byte_size, 0));
468 Status error;
469 const size_t bytes_read = process_sp->ReadMemory(
470 addr, data_ap->GetBytes(), data_ap->GetByteSize(), error);
471 if (bytes_read == byte_size)
472 data_sp.reset(data_ap.release());
473 }
474 return data_sp;
475 }
476
GetData(lldb::offset_t offset,size_t length,DataExtractor & data) const477 size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
478 DataExtractor &data) const {
479 // The entire file has already been mmap'ed into m_data, so just copy from
480 // there as the back mmap buffer will be shared with shared pointers.
481 return data.SetData(m_data, offset, length);
482 }
483
CopyData(lldb::offset_t offset,size_t length,void * dst) const484 size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
485 void *dst) const {
486 // The entire file has already been mmap'ed into m_data, so just copy from
487 // there Note that the data remains in target byte order.
488 return m_data.CopyData(offset, length, dst);
489 }
490
ReadSectionData(Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)491 size_t ObjectFile::ReadSectionData(Section *section,
492 lldb::offset_t section_offset, void *dst,
493 size_t dst_len) {
494 assert(section);
495 section_offset *= section->GetTargetByteSize();
496
497 // If some other objectfile owns this data, pass this to them.
498 if (section->GetObjectFile() != this)
499 return section->GetObjectFile()->ReadSectionData(section, section_offset,
500 dst, dst_len);
501
502 if (IsInMemory()) {
503 ProcessSP process_sp(m_process_wp.lock());
504 if (process_sp) {
505 Status error;
506 const addr_t base_load_addr =
507 section->GetLoadBaseAddress(&process_sp->GetTarget());
508 if (base_load_addr != LLDB_INVALID_ADDRESS)
509 return process_sp->ReadMemory(base_load_addr + section_offset, dst,
510 dst_len, error);
511 }
512 } else {
513 if (!section->IsRelocated())
514 RelocateSection(section);
515
516 const lldb::offset_t section_file_size = section->GetFileSize();
517 if (section_offset < section_file_size) {
518 const size_t section_bytes_left = section_file_size - section_offset;
519 size_t section_dst_len = dst_len;
520 if (section_dst_len > section_bytes_left)
521 section_dst_len = section_bytes_left;
522 return CopyData(section->GetFileOffset() + section_offset,
523 section_dst_len, dst);
524 } else {
525 if (section->GetType() == eSectionTypeZeroFill) {
526 const uint64_t section_size = section->GetByteSize();
527 const uint64_t section_bytes_left = section_size - section_offset;
528 uint64_t section_dst_len = dst_len;
529 if (section_dst_len > section_bytes_left)
530 section_dst_len = section_bytes_left;
531 memset(dst, 0, section_dst_len);
532 return section_dst_len;
533 }
534 }
535 }
536 return 0;
537 }
538
539 //----------------------------------------------------------------------
540 // Get the section data the file on disk
541 //----------------------------------------------------------------------
ReadSectionData(Section * section,DataExtractor & section_data)542 size_t ObjectFile::ReadSectionData(Section *section,
543 DataExtractor §ion_data) {
544 // If some other objectfile owns this data, pass this to them.
545 if (section->GetObjectFile() != this)
546 return section->GetObjectFile()->ReadSectionData(section, section_data);
547
548 if (IsInMemory()) {
549 ProcessSP process_sp(m_process_wp.lock());
550 if (process_sp) {
551 const addr_t base_load_addr =
552 section->GetLoadBaseAddress(&process_sp->GetTarget());
553 if (base_load_addr != LLDB_INVALID_ADDRESS) {
554 DataBufferSP data_sp(
555 ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
556 if (data_sp) {
557 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
558 section_data.SetByteOrder(process_sp->GetByteOrder());
559 section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
560 return section_data.GetByteSize();
561 }
562 }
563 }
564 return GetData(section->GetFileOffset(), section->GetFileSize(),
565 section_data);
566 } else {
567 // The object file now contains a full mmap'ed copy of the object file
568 // data, so just use this
569 if (!section->IsRelocated())
570 RelocateSection(section);
571
572 return GetData(section->GetFileOffset(), section->GetFileSize(),
573 section_data);
574 }
575 }
576
SplitArchivePathWithObject(const char * path_with_object,FileSpec & archive_file,ConstString & archive_object,bool must_exist)577 bool ObjectFile::SplitArchivePathWithObject(const char *path_with_object,
578 FileSpec &archive_file,
579 ConstString &archive_object,
580 bool must_exist) {
581 RegularExpression g_object_regex(llvm::StringRef("(.*)\\(([^\\)]+)\\)$"));
582 RegularExpression::Match regex_match(2);
583 if (g_object_regex.Execute(llvm::StringRef::withNullAsEmpty(path_with_object),
584 ®ex_match)) {
585 std::string path;
586 std::string obj;
587 if (regex_match.GetMatchAtIndex(path_with_object, 1, path) &&
588 regex_match.GetMatchAtIndex(path_with_object, 2, obj)) {
589 archive_file.SetFile(path, FileSpec::Style::native);
590 archive_object.SetCString(obj.c_str());
591 return !(must_exist && !FileSystem::Instance().Exists(archive_file));
592 }
593 }
594 return false;
595 }
596
ClearSymtab()597 void ObjectFile::ClearSymtab() {
598 ModuleSP module_sp(GetModule());
599 if (module_sp) {
600 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
601 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
602 if (log)
603 log->Printf("%p ObjectFile::ClearSymtab () symtab = %p",
604 static_cast<void *>(this),
605 static_cast<void *>(m_symtab_ap.get()));
606 m_symtab_ap.reset();
607 }
608 }
609
GetSectionList(bool update_module_section_list)610 SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
611 if (m_sections_ap.get() == nullptr) {
612 if (update_module_section_list) {
613 ModuleSP module_sp(GetModule());
614 if (module_sp) {
615 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
616 CreateSections(*module_sp->GetUnifiedSectionList());
617 }
618 } else {
619 SectionList unified_section_list;
620 CreateSections(unified_section_list);
621 }
622 }
623 return m_sections_ap.get();
624 }
625
626 lldb::SymbolType
GetSymbolTypeFromName(llvm::StringRef name,lldb::SymbolType symbol_type_hint)627 ObjectFile::GetSymbolTypeFromName(llvm::StringRef name,
628 lldb::SymbolType symbol_type_hint) {
629 if (!name.empty()) {
630 if (name.startswith("_OBJC_")) {
631 // ObjC
632 if (name.startswith("_OBJC_CLASS_$_"))
633 return lldb::eSymbolTypeObjCClass;
634 if (name.startswith("_OBJC_METACLASS_$_"))
635 return lldb::eSymbolTypeObjCMetaClass;
636 if (name.startswith("_OBJC_IVAR_$_"))
637 return lldb::eSymbolTypeObjCIVar;
638 } else if (name.startswith(".objc_class_name_")) {
639 // ObjC v1
640 return lldb::eSymbolTypeObjCClass;
641 }
642 }
643 return symbol_type_hint;
644 }
645
GetNextSyntheticSymbolName()646 ConstString ObjectFile::GetNextSyntheticSymbolName() {
647 StreamString ss;
648 ConstString file_name = GetModule()->GetFileSpec().GetFilename();
649 ss.Printf("___lldb_unnamed_symbol%u$$%s", ++m_synthetic_symbol_idx,
650 file_name.GetCString());
651 return ConstString(ss.GetString());
652 }
653
654 std::vector<ObjectFile::LoadableData>
GetLoadableData(Target & target)655 ObjectFile::GetLoadableData(Target &target) {
656 std::vector<LoadableData> loadables;
657 SectionList *section_list = GetSectionList();
658 if (!section_list)
659 return loadables;
660 // Create a list of loadable data from loadable sections
661 size_t section_count = section_list->GetNumSections(0);
662 for (size_t i = 0; i < section_count; ++i) {
663 LoadableData loadable;
664 SectionSP section_sp = section_list->GetSectionAtIndex(i);
665 loadable.Dest =
666 target.GetSectionLoadList().GetSectionLoadAddress(section_sp);
667 if (loadable.Dest == LLDB_INVALID_ADDRESS)
668 continue;
669 // We can skip sections like bss
670 if (section_sp->GetFileSize() == 0)
671 continue;
672 DataExtractor section_data;
673 section_sp->GetSectionData(section_data);
674 loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(),
675 section_data.GetByteSize());
676 loadables.push_back(loadable);
677 }
678 return loadables;
679 }
680
RelocateSection(lldb_private::Section * section)681 void ObjectFile::RelocateSection(lldb_private::Section *section)
682 {
683 }
684
MapFileData(const FileSpec & file,uint64_t Size,uint64_t Offset)685 DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size,
686 uint64_t Offset) {
687 return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
688 }
689
format(const ObjectFile::Type & type,raw_ostream & OS,StringRef Style)690 void llvm::format_provider<ObjectFile::Type>::format(
691 const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {
692 switch (type) {
693 case ObjectFile::eTypeInvalid:
694 OS << "invalid";
695 break;
696 case ObjectFile::eTypeCoreFile:
697 OS << "core file";
698 break;
699 case ObjectFile::eTypeExecutable:
700 OS << "executable";
701 break;
702 case ObjectFile::eTypeDebugInfo:
703 OS << "debug info";
704 break;
705 case ObjectFile::eTypeDynamicLinker:
706 OS << "dynamic linker";
707 break;
708 case ObjectFile::eTypeObjectFile:
709 OS << "object file";
710 break;
711 case ObjectFile::eTypeSharedLibrary:
712 OS << "shared library";
713 break;
714 case ObjectFile::eTypeStubLibrary:
715 OS << "stub library";
716 break;
717 case ObjectFile::eTypeJIT:
718 OS << "jit";
719 break;
720 case ObjectFile::eTypeUnknown:
721 OS << "unknown";
722 break;
723 }
724 }
725
format(const ObjectFile::Strata & strata,raw_ostream & OS,StringRef Style)726 void llvm::format_provider<ObjectFile::Strata>::format(
727 const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {
728 switch (strata) {
729 case ObjectFile::eStrataInvalid:
730 OS << "invalid";
731 break;
732 case ObjectFile::eStrataUnknown:
733 OS << "unknown";
734 break;
735 case ObjectFile::eStrataUser:
736 OS << "user";
737 break;
738 case ObjectFile::eStrataKernel:
739 OS << "kernel";
740 break;
741 case ObjectFile::eStrataRawImage:
742 OS << "raw image";
743 break;
744 case ObjectFile::eStrataJIT:
745 OS << "jit";
746 break;
747 }
748 }
749