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