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