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