1 //===-- ObjectFile.cpp ------------------------------------------*- C++ -*-===//
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 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)32 ObjectFile::FindPlugin(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 ObjectFileSP object_file_sp;
36
37 if (module_sp) {
38 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
39 Timer scoped_timer(
40 func_cat,
41 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "
42 "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
43 module_sp->GetFileSpec().GetPath().c_str(),
44 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
45 static_cast<uint64_t>(file_size));
46 if (file) {
47 FileSpec archive_file;
48 ObjectContainerCreateInstance create_object_container_callback;
49
50 const bool file_exists = FileSystem::Instance().Exists(*file);
51 if (!data_sp) {
52 // We have an object name which most likely means we have a .o file in
53 // a static archive (.a file). Try and see if we have a cached archive
54 // first without reading any data first
55 if (file_exists && module_sp->GetObjectName()) {
56 for (uint32_t idx = 0;
57 (create_object_container_callback =
58 PluginManager::GetObjectContainerCreateCallbackAtIndex(
59 idx)) != nullptr;
60 ++idx) {
61 std::unique_ptr<ObjectContainer> object_container_up(
62 create_object_container_callback(module_sp, data_sp,
63 data_offset, file, file_offset,
64 file_size));
65
66 if (object_container_up)
67 object_file_sp = object_container_up->GetObjectFile(file);
68
69 if (object_file_sp.get())
70 return object_file_sp;
71 }
72 }
73 // Ok, we didn't find any containers that have a named object, now lets
74 // read the first 512 bytes from the file so the object file and object
75 // container plug-ins can use these bytes to see if they can parse this
76 // file.
77 if (file_size > 0) {
78 data_sp = FileSystem::Instance().CreateDataBuffer(file->GetPath(),
79 512, file_offset);
80 data_offset = 0;
81 }
82 }
83
84 if (!data_sp || data_sp->GetByteSize() == 0) {
85 // Check for archive file with format "/path/to/archive.a(object.o)"
86 llvm::SmallString<256> path_with_object;
87 module_sp->GetFileSpec().GetPath(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_up(
110 create_object_container_callback(module_sp, data_sp,
111 data_offset, file,
112 file_offset, file_size));
113
114 if (object_container_up)
115 object_file_sp = object_container_up->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_up(
152 create_object_container_callback(module_sp, data_sp, data_offset,
153 file, file_offset, file_size));
154
155 if (object_container_up)
156 object_file_sp = object_container_up->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(), m_process_wp(),
268 m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_up(), m_symtab_up(),
269 m_synthetic_symbol_idx(0) {
270 if (file_spec_ptr)
271 m_file = *file_spec_ptr;
272 if (data_sp)
273 m_data.SetData(data_sp, data_offset, length);
274 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
275 LLDB_LOGF(log,
276 "%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
277 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
278 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
279 module_sp->GetSpecificationDescription().c_str(),
280 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
281 m_length);
282 }
283
ObjectFile(const lldb::ModuleSP & module_sp,const ProcessSP & process_sp,lldb::addr_t header_addr,DataBufferSP & header_data_sp)284 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
285 const ProcessSP &process_sp, lldb::addr_t header_addr,
286 DataBufferSP &header_data_sp)
287 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
288 m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(),
289 m_process_wp(process_sp), m_memory_addr(header_addr), m_sections_up(),
290 m_symtab_up(), m_synthetic_symbol_idx(0) {
291 if (header_data_sp)
292 m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize());
293 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
294 LLDB_LOGF(log,
295 "%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
296 "header_addr = 0x%" PRIx64,
297 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
298 module_sp->GetSpecificationDescription().c_str(),
299 static_cast<void *>(process_sp.get()), m_memory_addr);
300 }
301
~ObjectFile()302 ObjectFile::~ObjectFile() {
303 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
304 LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
305 }
306
SetModulesArchitecture(const ArchSpec & new_arch)307 bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) {
308 ModuleSP module_sp(GetModule());
309 if (module_sp)
310 return module_sp->SetArchitecture(new_arch);
311 return false;
312 }
313
GetAddressClass(addr_t file_addr)314 AddressClass ObjectFile::GetAddressClass(addr_t file_addr) {
315 Symtab *symtab = GetSymtab();
316 if (symtab) {
317 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
318 if (symbol) {
319 if (symbol->ValueIsAddress()) {
320 const SectionSP section_sp(symbol->GetAddressRef().GetSection());
321 if (section_sp) {
322 const SectionType section_type = section_sp->GetType();
323 switch (section_type) {
324 case eSectionTypeInvalid:
325 return AddressClass::eUnknown;
326 case eSectionTypeCode:
327 return AddressClass::eCode;
328 case eSectionTypeContainer:
329 return AddressClass::eUnknown;
330 case eSectionTypeData:
331 case eSectionTypeDataCString:
332 case eSectionTypeDataCStringPointers:
333 case eSectionTypeDataSymbolAddress:
334 case eSectionTypeData4:
335 case eSectionTypeData8:
336 case eSectionTypeData16:
337 case eSectionTypeDataPointers:
338 case eSectionTypeZeroFill:
339 case eSectionTypeDataObjCMessageRefs:
340 case eSectionTypeDataObjCCFStrings:
341 case eSectionTypeGoSymtab:
342 return AddressClass::eData;
343 case eSectionTypeDebug:
344 case eSectionTypeDWARFDebugAbbrev:
345 case eSectionTypeDWARFDebugAbbrevDwo:
346 case eSectionTypeDWARFDebugAddr:
347 case eSectionTypeDWARFDebugAranges:
348 case eSectionTypeDWARFDebugCuIndex:
349 case eSectionTypeDWARFDebugFrame:
350 case eSectionTypeDWARFDebugInfo:
351 case eSectionTypeDWARFDebugInfoDwo:
352 case eSectionTypeDWARFDebugLine:
353 case eSectionTypeDWARFDebugLineStr:
354 case eSectionTypeDWARFDebugLoc:
355 case eSectionTypeDWARFDebugLocDwo:
356 case eSectionTypeDWARFDebugLocLists:
357 case eSectionTypeDWARFDebugLocListsDwo:
358 case eSectionTypeDWARFDebugMacInfo:
359 case eSectionTypeDWARFDebugMacro:
360 case eSectionTypeDWARFDebugNames:
361 case eSectionTypeDWARFDebugPubNames:
362 case eSectionTypeDWARFDebugPubTypes:
363 case eSectionTypeDWARFDebugRanges:
364 case eSectionTypeDWARFDebugRngLists:
365 case eSectionTypeDWARFDebugRngListsDwo:
366 case eSectionTypeDWARFDebugStr:
367 case eSectionTypeDWARFDebugStrDwo:
368 case eSectionTypeDWARFDebugStrOffsets:
369 case eSectionTypeDWARFDebugStrOffsetsDwo:
370 case eSectionTypeDWARFDebugTypes:
371 case eSectionTypeDWARFDebugTypesDwo:
372 case eSectionTypeDWARFAppleNames:
373 case eSectionTypeDWARFAppleTypes:
374 case eSectionTypeDWARFAppleNamespaces:
375 case eSectionTypeDWARFAppleObjC:
376 case eSectionTypeDWARFGNUDebugAltLink:
377 return AddressClass::eDebug;
378 case eSectionTypeEHFrame:
379 case eSectionTypeARMexidx:
380 case eSectionTypeARMextab:
381 case eSectionTypeCompactUnwind:
382 return AddressClass::eRuntime;
383 case eSectionTypeELFSymbolTable:
384 case eSectionTypeELFDynamicSymbols:
385 case eSectionTypeELFRelocationEntries:
386 case eSectionTypeELFDynamicLinkInfo:
387 case eSectionTypeOther:
388 return AddressClass::eUnknown;
389 case eSectionTypeAbsoluteAddress:
390 // In case of absolute sections decide the address class based on
391 // the symbol type because the section type isn't specify if it is
392 // a code or a data section.
393 break;
394 }
395 }
396 }
397
398 const SymbolType symbol_type = symbol->GetType();
399 switch (symbol_type) {
400 case eSymbolTypeAny:
401 return AddressClass::eUnknown;
402 case eSymbolTypeAbsolute:
403 return AddressClass::eUnknown;
404 case eSymbolTypeCode:
405 return AddressClass::eCode;
406 case eSymbolTypeTrampoline:
407 return AddressClass::eCode;
408 case eSymbolTypeResolver:
409 return AddressClass::eCode;
410 case eSymbolTypeData:
411 return AddressClass::eData;
412 case eSymbolTypeRuntime:
413 return AddressClass::eRuntime;
414 case eSymbolTypeException:
415 return AddressClass::eRuntime;
416 case eSymbolTypeSourceFile:
417 return AddressClass::eDebug;
418 case eSymbolTypeHeaderFile:
419 return AddressClass::eDebug;
420 case eSymbolTypeObjectFile:
421 return AddressClass::eDebug;
422 case eSymbolTypeCommonBlock:
423 return AddressClass::eDebug;
424 case eSymbolTypeBlock:
425 return AddressClass::eDebug;
426 case eSymbolTypeLocal:
427 return AddressClass::eData;
428 case eSymbolTypeParam:
429 return AddressClass::eData;
430 case eSymbolTypeVariable:
431 return AddressClass::eData;
432 case eSymbolTypeVariableType:
433 return AddressClass::eDebug;
434 case eSymbolTypeLineEntry:
435 return AddressClass::eDebug;
436 case eSymbolTypeLineHeader:
437 return AddressClass::eDebug;
438 case eSymbolTypeScopeBegin:
439 return AddressClass::eDebug;
440 case eSymbolTypeScopeEnd:
441 return AddressClass::eDebug;
442 case eSymbolTypeAdditional:
443 return AddressClass::eUnknown;
444 case eSymbolTypeCompiler:
445 return AddressClass::eDebug;
446 case eSymbolTypeInstrumentation:
447 return AddressClass::eDebug;
448 case eSymbolTypeUndefined:
449 return AddressClass::eUnknown;
450 case eSymbolTypeObjCClass:
451 return AddressClass::eRuntime;
452 case eSymbolTypeObjCMetaClass:
453 return AddressClass::eRuntime;
454 case eSymbolTypeObjCIVar:
455 return AddressClass::eRuntime;
456 case eSymbolTypeReExported:
457 return AddressClass::eRuntime;
458 }
459 }
460 }
461 return AddressClass::eUnknown;
462 }
463
ReadMemory(const ProcessSP & process_sp,lldb::addr_t addr,size_t byte_size)464 DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp,
465 lldb::addr_t addr, size_t byte_size) {
466 DataBufferSP data_sp;
467 if (process_sp) {
468 std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0));
469 Status error;
470 const size_t bytes_read = process_sp->ReadMemory(
471 addr, data_up->GetBytes(), data_up->GetByteSize(), error);
472 if (bytes_read == byte_size)
473 data_sp.reset(data_up.release());
474 }
475 return data_sp;
476 }
477
GetData(lldb::offset_t offset,size_t length,DataExtractor & data) const478 size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
479 DataExtractor &data) const {
480 // The entire file has already been mmap'ed into m_data, so just copy from
481 // there as the back mmap buffer will be shared with shared pointers.
482 return data.SetData(m_data, offset, length);
483 }
484
CopyData(lldb::offset_t offset,size_t length,void * dst) const485 size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
486 void *dst) const {
487 // The entire file has already been mmap'ed into m_data, so just copy from
488 // there Note that the data remains in target byte order.
489 return m_data.CopyData(offset, length, dst);
490 }
491
ReadSectionData(Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)492 size_t ObjectFile::ReadSectionData(Section *section,
493 lldb::offset_t section_offset, void *dst,
494 size_t dst_len) {
495 assert(section);
496 section_offset *= section->GetTargetByteSize();
497
498 // If some other objectfile owns this data, pass this to them.
499 if (section->GetObjectFile() != this)
500 return section->GetObjectFile()->ReadSectionData(section, section_offset,
501 dst, dst_len);
502
503 if (IsInMemory()) {
504 ProcessSP process_sp(m_process_wp.lock());
505 if (process_sp) {
506 Status error;
507 const addr_t base_load_addr =
508 section->GetLoadBaseAddress(&process_sp->GetTarget());
509 if (base_load_addr != LLDB_INVALID_ADDRESS)
510 return process_sp->ReadMemory(base_load_addr + section_offset, dst,
511 dst_len, error);
512 }
513 } else {
514 if (!section->IsRelocated())
515 RelocateSection(section);
516
517 const lldb::offset_t section_file_size = section->GetFileSize();
518 if (section_offset < section_file_size) {
519 const size_t section_bytes_left = section_file_size - section_offset;
520 size_t section_dst_len = dst_len;
521 if (section_dst_len > section_bytes_left)
522 section_dst_len = section_bytes_left;
523 return CopyData(section->GetFileOffset() + section_offset,
524 section_dst_len, dst);
525 } else {
526 if (section->GetType() == eSectionTypeZeroFill) {
527 const uint64_t section_size = section->GetByteSize();
528 const uint64_t section_bytes_left = section_size - section_offset;
529 uint64_t section_dst_len = dst_len;
530 if (section_dst_len > section_bytes_left)
531 section_dst_len = section_bytes_left;
532 memset(dst, 0, section_dst_len);
533 return section_dst_len;
534 }
535 }
536 }
537 return 0;
538 }
539
540 // Get the section data the file on disk
ReadSectionData(Section * section,DataExtractor & section_data)541 size_t ObjectFile::ReadSectionData(Section *section,
542 DataExtractor §ion_data) {
543 // If some other objectfile owns this data, pass this to them.
544 if (section->GetObjectFile() != this)
545 return section->GetObjectFile()->ReadSectionData(section, section_data);
546
547 if (IsInMemory()) {
548 ProcessSP process_sp(m_process_wp.lock());
549 if (process_sp) {
550 const addr_t base_load_addr =
551 section->GetLoadBaseAddress(&process_sp->GetTarget());
552 if (base_load_addr != LLDB_INVALID_ADDRESS) {
553 DataBufferSP data_sp(
554 ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
555 if (data_sp) {
556 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
557 section_data.SetByteOrder(process_sp->GetByteOrder());
558 section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
559 return section_data.GetByteSize();
560 }
561 }
562 }
563 return GetData(section->GetFileOffset(), section->GetFileSize(),
564 section_data);
565 } else {
566 // The object file now contains a full mmap'ed copy of the object file
567 // data, so just use this
568 if (!section->IsRelocated())
569 RelocateSection(section);
570
571 return GetData(section->GetFileOffset(), section->GetFileSize(),
572 section_data);
573 }
574 }
575
SplitArchivePathWithObject(llvm::StringRef path_with_object,FileSpec & archive_file,ConstString & archive_object,bool must_exist)576 bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,
577 FileSpec &archive_file,
578 ConstString &archive_object,
579 bool must_exist) {
580 size_t len = path_with_object.size();
581 if (len < 2 || path_with_object.back() != ')')
582 return false;
583 llvm::StringRef archive = path_with_object.substr(0, path_with_object.rfind('('));
584 if (archive.empty())
585 return false;
586 llvm::StringRef object = path_with_object.substr(archive.size() + 1).drop_back();
587 archive_file.SetFile(archive, FileSpec::Style::native);
588 if (must_exist && !FileSystem::Instance().Exists(archive_file))
589 return false;
590 archive_object.SetString(object);
591 return true;
592 }
593
ClearSymtab()594 void ObjectFile::ClearSymtab() {
595 ModuleSP module_sp(GetModule());
596 if (module_sp) {
597 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
598 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
599 LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",
600 static_cast<void *>(this),
601 static_cast<void *>(m_symtab_up.get()));
602 m_symtab_up.reset();
603 }
604 }
605
GetSectionList(bool update_module_section_list)606 SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
607 if (m_sections_up == nullptr) {
608 if (update_module_section_list) {
609 ModuleSP module_sp(GetModule());
610 if (module_sp) {
611 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
612 CreateSections(*module_sp->GetUnifiedSectionList());
613 }
614 } else {
615 SectionList unified_section_list;
616 CreateSections(unified_section_list);
617 }
618 }
619 return m_sections_up.get();
620 }
621
622 lldb::SymbolType
GetSymbolTypeFromName(llvm::StringRef name,lldb::SymbolType symbol_type_hint)623 ObjectFile::GetSymbolTypeFromName(llvm::StringRef name,
624 lldb::SymbolType symbol_type_hint) {
625 if (!name.empty()) {
626 if (name.startswith("_OBJC_")) {
627 // ObjC
628 if (name.startswith("_OBJC_CLASS_$_"))
629 return lldb::eSymbolTypeObjCClass;
630 if (name.startswith("_OBJC_METACLASS_$_"))
631 return lldb::eSymbolTypeObjCMetaClass;
632 if (name.startswith("_OBJC_IVAR_$_"))
633 return lldb::eSymbolTypeObjCIVar;
634 } else if (name.startswith(".objc_class_name_")) {
635 // ObjC v1
636 return lldb::eSymbolTypeObjCClass;
637 }
638 }
639 return symbol_type_hint;
640 }
641
GetNextSyntheticSymbolName()642 ConstString ObjectFile::GetNextSyntheticSymbolName() {
643 StreamString ss;
644 ConstString file_name = GetModule()->GetFileSpec().GetFilename();
645 ss.Printf("___lldb_unnamed_symbol%u$$%s", ++m_synthetic_symbol_idx,
646 file_name.GetCString());
647 return ConstString(ss.GetString());
648 }
649
650 std::vector<ObjectFile::LoadableData>
GetLoadableData(Target & target)651 ObjectFile::GetLoadableData(Target &target) {
652 std::vector<LoadableData> loadables;
653 SectionList *section_list = GetSectionList();
654 if (!section_list)
655 return loadables;
656 // Create a list of loadable data from loadable sections
657 size_t section_count = section_list->GetNumSections(0);
658 for (size_t i = 0; i < section_count; ++i) {
659 LoadableData loadable;
660 SectionSP section_sp = section_list->GetSectionAtIndex(i);
661 loadable.Dest =
662 target.GetSectionLoadList().GetSectionLoadAddress(section_sp);
663 if (loadable.Dest == LLDB_INVALID_ADDRESS)
664 continue;
665 // We can skip sections like bss
666 if (section_sp->GetFileSize() == 0)
667 continue;
668 DataExtractor section_data;
669 section_sp->GetSectionData(section_data);
670 loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(),
671 section_data.GetByteSize());
672 loadables.push_back(loadable);
673 }
674 return loadables;
675 }
676
CreateCallFrameInfo()677 std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {
678 return {};
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