1 //===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ExecutionEngine/ExecutionEngine.h"
11 #include "llvm/ExecutionEngine/ObjectCache.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/raw_ostream.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/Disassembler.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/Section.h"
22 #include "lldb/Expression/IRExecutionUnit.h"
23 #include "lldb/Symbol/CompileUnit.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Symbol/SymbolFile.h"
26 #include "lldb/Symbol/SymbolVendor.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/ObjCLanguageRuntime.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Utility/DataBufferHeap.h"
31 #include "lldb/Utility/DataExtractor.h"
32 #include "lldb/Utility/LLDBAssert.h"
33 #include "lldb/Utility/Log.h"
35 #include "lldb/../../source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
36 #include "lldb/../../source/Plugins/ObjectFile/JIT/ObjectFileJIT.h"
38 using namespace lldb_private;
40 IRExecutionUnit::IRExecutionUnit(std::unique_ptr<llvm::LLVMContext> &context_ap,
41 std::unique_ptr<llvm::Module> &module_ap,
43 const lldb::TargetSP &target_sp,
44 const SymbolContext &sym_ctx,
45 std::vector<std::string> &cpu_features)
46 : IRMemoryMap(target_sp), m_context_ap(context_ap.release()),
47 m_module_ap(module_ap.release()), m_module(m_module_ap.get()),
48 m_cpu_features(cpu_features), m_name(name), m_sym_ctx(sym_ctx),
49 m_did_jit(false), m_function_load_addr(LLDB_INVALID_ADDRESS),
50 m_function_end_load_addr(LLDB_INVALID_ADDRESS),
51 m_reported_allocations(false) {}
53 lldb::addr_t IRExecutionUnit::WriteNow(const uint8_t *bytes, size_t size,
55 const bool zero_memory = false;
56 lldb::addr_t allocation_process_addr =
57 Malloc(size, 8, lldb::ePermissionsWritable | lldb::ePermissionsReadable,
58 eAllocationPolicyMirror, zero_memory, error);
61 return LLDB_INVALID_ADDRESS;
63 WriteMemory(allocation_process_addr, bytes, size, error);
65 if (!error.Success()) {
67 Free(allocation_process_addr, err);
69 return LLDB_INVALID_ADDRESS;
73 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
74 DataBufferHeap my_buffer(size, 0);
76 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
79 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(),
80 lldb::eByteOrderBig, 8);
81 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
82 allocation_process_addr, 16,
83 DataExtractor::TypeUInt8);
87 return allocation_process_addr;
90 void IRExecutionUnit::FreeNow(lldb::addr_t allocation) {
91 if (allocation == LLDB_INVALID_ADDRESS)
96 Free(allocation, err);
99 Status IRExecutionUnit::DisassembleFunction(Stream &stream,
100 lldb::ProcessSP &process_wp) {
101 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
103 ExecutionContext exe_ctx(process_wp);
109 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
110 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
112 for (JittedFunction &function : m_jitted_functions) {
113 if (function.m_name == m_name) {
114 func_local_addr = function.m_local_addr;
115 func_remote_addr = function.m_remote_addr;
119 if (func_local_addr == LLDB_INVALID_ADDRESS) {
120 ret.SetErrorToGenericError();
121 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly",
127 log->Printf("Found function, has local address 0x%" PRIx64
128 " and remote address 0x%" PRIx64,
129 (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
131 std::pair<lldb::addr_t, lldb::addr_t> func_range;
133 func_range = GetRemoteRangeForLocal(func_local_addr);
135 if (func_range.first == 0 && func_range.second == 0) {
136 ret.SetErrorToGenericError();
137 ret.SetErrorStringWithFormat("Couldn't find code range for function %s",
143 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]",
144 func_range.first, func_range.second);
146 Target *target = exe_ctx.GetTargetPtr();
148 ret.SetErrorToGenericError();
149 ret.SetErrorString("Couldn't find the target");
153 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
155 Process *process = exe_ctx.GetProcessPtr();
157 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(),
158 buffer_sp->GetByteSize(), err);
160 if (!err.Success()) {
161 ret.SetErrorToGenericError();
162 ret.SetErrorStringWithFormat("Couldn't read from process: %s",
163 err.AsCString("unknown error"));
167 ArchSpec arch(target->GetArchitecture());
169 const char *plugin_name = NULL;
170 const char *flavor_string = NULL;
171 lldb::DisassemblerSP disassembler_sp =
172 Disassembler::FindPlugin(arch, flavor_string, plugin_name);
174 if (!disassembler_sp) {
175 ret.SetErrorToGenericError();
176 ret.SetErrorStringWithFormat(
177 "Unable to find disassembler plug-in for %s architecture.",
178 arch.GetArchitectureName());
183 ret.SetErrorToGenericError();
184 ret.SetErrorString("Couldn't find the process");
188 DataExtractor extractor(buffer_sp, process->GetByteOrder(),
189 target->GetArchitecture().GetAddressByteSize());
192 log->Printf("Function data has contents:");
193 extractor.PutToLog(log, 0, extractor.GetByteSize(), func_remote_addr, 16,
194 DataExtractor::TypeUInt8);
197 disassembler_sp->DecodeInstructions(Address(func_remote_addr), extractor, 0,
198 UINT32_MAX, false, false);
200 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
201 instruction_list.Dump(&stream, true, true, &exe_ctx);
205 static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic,
206 void *Context, unsigned LocCookie) {
207 Status *err = static_cast<Status *>(Context);
209 if (err && err->Success()) {
210 err->SetErrorToGenericError();
211 err->SetErrorStringWithFormat("Inline assembly error: %s",
212 diagnostic.getMessage().str().c_str());
216 void IRExecutionUnit::ReportSymbolLookupError(const ConstString &name) {
217 m_failed_lookups.push_back(name);
220 void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
221 lldb::addr_t &func_end) {
222 lldb::ProcessSP process_sp(GetProcessWP().lock());
224 static std::recursive_mutex s_runnable_info_mutex;
226 func_addr = LLDB_INVALID_ADDRESS;
227 func_end = LLDB_INVALID_ADDRESS;
230 error.SetErrorToGenericError();
231 error.SetErrorString("Couldn't write the JIT compiled code into the "
232 "process because the process is invalid");
237 func_addr = m_function_load_addr;
238 func_end = m_function_end_load_addr;
243 std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
247 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
249 std::string error_string;
253 llvm::raw_string_ostream oss(s);
255 m_module->print(oss, NULL);
259 log->Printf("Module being sent to JIT: \n%s", s.c_str());
262 llvm::Triple triple(m_module->getTargetTriple());
263 llvm::Reloc::Model relocModel;
265 if (triple.isOSBinFormatELF()) {
266 relocModel = llvm::Reloc::Static;
268 relocModel = llvm::Reloc::PIC_;
271 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError,
274 llvm::EngineBuilder builder(std::move(m_module_ap));
276 builder.setEngineKind(llvm::EngineKind::JIT)
277 .setErrorStr(&error_string)
278 .setRelocationModel(relocModel)
279 .setMCJITMemoryManager(
280 std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
281 .setOptLevel(llvm::CodeGenOpt::Less);
283 llvm::StringRef mArch;
284 llvm::StringRef mCPU;
285 llvm::SmallVector<std::string, 0> mAttrs;
287 for (std::string &feature : m_cpu_features)
288 mAttrs.push_back(feature);
290 llvm::TargetMachine *target_machine =
291 builder.selectTarget(triple, mArch, mCPU, mAttrs);
293 m_execution_engine_ap.reset(builder.create(target_machine));
296 (m_execution_engine_ap->getDataLayout().getGlobalPrefix() == '_');
298 if (!m_execution_engine_ap.get()) {
299 error.SetErrorToGenericError();
300 error.SetErrorStringWithFormat("Couldn't JIT the function: %s",
301 error_string.c_str());
305 class ObjectDumper : public llvm::ObjectCache {
307 void notifyObjectCompiled(const llvm::Module *module,
308 llvm::MemoryBufferRef object) override {
310 llvm::SmallVector<char, 256> result_path;
311 std::string object_name_model =
312 "jit-object-" + module->getModuleIdentifier() + "-%%%.o";
313 (void)llvm::sys::fs::createUniqueFile(object_name_model, fd, result_path);
314 llvm::raw_fd_ostream fds(fd, true);
315 fds.write(object.getBufferStart(), object.getBufferSize());
318 std::unique_ptr<llvm::MemoryBuffer>
319 getObject(const llvm::Module *module) override {
320 // Return nothing - we're just abusing the object-cache mechanism to dump
326 if (process_sp->GetTarget().GetEnableSaveObjects()) {
327 m_object_cache_ap = llvm::make_unique<ObjectDumper>();
328 m_execution_engine_ap->setObjectCache(m_object_cache_ap.get());
331 // Make sure we see all sections, including ones that don't have
333 m_execution_engine_ap->setProcessAllSections(true);
335 m_execution_engine_ap->DisableLazyCompilation();
337 for (llvm::Function &function : *m_module) {
338 if (function.isDeclaration() || function.hasPrivateLinkage())
341 const bool external =
342 function.hasExternalLinkage() || function.hasLinkOnceODRLinkage();
344 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(&function);
346 if (!error.Success()) {
347 // We got an error through our callback!
352 error.SetErrorToGenericError();
353 error.SetErrorStringWithFormat(
354 "'%s' was in the JITted module but wasn't lowered",
355 function.getName().str().c_str());
358 m_jitted_functions.push_back(JittedFunction(
359 function.getName().str().c_str(), external, (lldb::addr_t)fun_ptr));
362 CommitAllocations(process_sp);
363 ReportAllocations(*m_execution_engine_ap);
365 // We have to do this after calling ReportAllocations because for the MCJIT,
366 // getGlobalValueAddress will cause the JIT to perform all relocations. That
367 // can only be done once, and has to happen after we do the remapping from
368 // local -> remote. That means we don't know the local address of the
369 // Variables, but we don't need that for anything, so that's okay.
371 std::function<void(llvm::GlobalValue &)> RegisterOneValue = [this](
372 llvm::GlobalValue &val) {
373 if (val.hasExternalLinkage() && !val.isDeclaration()) {
374 uint64_t var_ptr_addr =
375 m_execution_engine_ap->getGlobalValueAddress(val.getName().str());
377 lldb::addr_t remote_addr = GetRemoteAddressForLocal(var_ptr_addr);
379 // This is a really unfortunae API that sometimes returns local addresses
380 // and sometimes returns remote addresses, based on whether the variable
381 // was relocated during ReportAllocations or not.
383 if (remote_addr == LLDB_INVALID_ADDRESS) {
384 remote_addr = var_ptr_addr;
387 if (var_ptr_addr != 0)
388 m_jitted_global_variables.push_back(JittedGlobalVariable(
389 val.getName().str().c_str(), LLDB_INVALID_ADDRESS, remote_addr));
393 for (llvm::GlobalVariable &global_var : m_module->getGlobalList()) {
394 RegisterOneValue(global_var);
397 for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) {
398 RegisterOneValue(global_alias);
401 WriteData(process_sp);
403 if (m_failed_lookups.size()) {
406 ss.PutCString("Couldn't lookup symbols:\n");
408 bool emitNewLine = false;
410 for (const ConstString &failed_lookup : m_failed_lookups) {
415 ss.PutCString(Mangled(failed_lookup)
416 .GetDemangledName(lldb::eLanguageTypeObjC_plus_plus)
420 m_failed_lookups.clear();
422 error.SetErrorString(ss.GetString());
427 m_function_load_addr = LLDB_INVALID_ADDRESS;
428 m_function_end_load_addr = LLDB_INVALID_ADDRESS;
430 for (JittedFunction &jitted_function : m_jitted_functions) {
431 jitted_function.m_remote_addr =
432 GetRemoteAddressForLocal(jitted_function.m_local_addr);
434 if (!m_name.IsEmpty() && jitted_function.m_name == m_name) {
435 AddrRange func_range =
436 GetRemoteRangeForLocal(jitted_function.m_local_addr);
437 m_function_end_load_addr = func_range.first + func_range.second;
438 m_function_load_addr = jitted_function.m_remote_addr;
443 log->Printf("Code can be run in the target.");
445 StreamString disassembly_stream;
447 Status err = DisassembleFunction(disassembly_stream, process_sp);
449 if (!err.Success()) {
450 log->Printf("Couldn't disassemble function : %s",
451 err.AsCString("unknown error"));
453 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
456 log->Printf("Sections: ");
457 for (AllocationRecord &record : m_records) {
458 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
461 DataBufferHeap my_buffer(record.m_size, 0);
463 ReadMemory(my_buffer.GetBytes(), record.m_process_address,
467 DataExtractor my_extractor(my_buffer.GetBytes(),
468 my_buffer.GetByteSize(),
469 lldb::eByteOrderBig, 8);
470 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(),
471 record.m_process_address, 16,
472 DataExtractor::TypeUInt8);
477 DataExtractor my_extractor((const void *)record.m_host_address,
478 record.m_size, lldb::eByteOrderBig, 8);
479 my_extractor.PutToLog(log, 0, record.m_size, record.m_host_address, 16,
480 DataExtractor::TypeUInt8);
485 func_addr = m_function_load_addr;
486 func_end = m_function_end_load_addr;
491 IRExecutionUnit::~IRExecutionUnit() {
493 m_execution_engine_ap.reset();
494 m_context_ap.reset();
497 IRExecutionUnit::MemoryManager::MemoryManager(IRExecutionUnit &parent)
498 : m_default_mm_ap(new llvm::SectionMemoryManager()), m_parent(parent) {}
500 IRExecutionUnit::MemoryManager::~MemoryManager() {}
502 lldb::SectionType IRExecutionUnit::GetSectionTypeFromSectionName(
503 const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind) {
504 lldb::SectionType sect_type = lldb::eSectionTypeCode;
505 switch (alloc_kind) {
506 case AllocationKind::Stub:
507 sect_type = lldb::eSectionTypeCode;
509 case AllocationKind::Code:
510 sect_type = lldb::eSectionTypeCode;
512 case AllocationKind::Data:
513 sect_type = lldb::eSectionTypeData;
515 case AllocationKind::Global:
516 sect_type = lldb::eSectionTypeData;
518 case AllocationKind::Bytes:
519 sect_type = lldb::eSectionTypeOther;
524 if (name.equals("__text") || name.equals(".text"))
525 sect_type = lldb::eSectionTypeCode;
526 else if (name.equals("__data") || name.equals(".data"))
527 sect_type = lldb::eSectionTypeCode;
528 else if (name.startswith("__debug_") || name.startswith(".debug_")) {
529 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
530 llvm::StringRef dwarf_name(name.substr(name_idx));
531 switch (dwarf_name[0]) {
533 if (dwarf_name.equals("abbrev"))
534 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
535 else if (dwarf_name.equals("aranges"))
536 sect_type = lldb::eSectionTypeDWARFDebugAranges;
537 else if (dwarf_name.equals("addr"))
538 sect_type = lldb::eSectionTypeDWARFDebugAddr;
542 if (dwarf_name.equals("frame"))
543 sect_type = lldb::eSectionTypeDWARFDebugFrame;
547 if (dwarf_name.equals("info"))
548 sect_type = lldb::eSectionTypeDWARFDebugInfo;
552 if (dwarf_name.equals("line"))
553 sect_type = lldb::eSectionTypeDWARFDebugLine;
554 else if (dwarf_name.equals("loc"))
555 sect_type = lldb::eSectionTypeDWARFDebugLoc;
556 else if (dwarf_name.equals("loclists"))
557 sect_type = lldb::eSectionTypeDWARFDebugLocLists;
561 if (dwarf_name.equals("macinfo"))
562 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
566 if (dwarf_name.equals("pubnames"))
567 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
568 else if (dwarf_name.equals("pubtypes"))
569 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
573 if (dwarf_name.equals("str"))
574 sect_type = lldb::eSectionTypeDWARFDebugStr;
575 else if (dwarf_name.equals("str_offsets"))
576 sect_type = lldb::eSectionTypeDWARFDebugStrOffsets;
580 if (dwarf_name.equals("ranges"))
581 sect_type = lldb::eSectionTypeDWARFDebugRanges;
587 } else if (name.startswith("__apple_") || name.startswith(".apple_"))
588 sect_type = lldb::eSectionTypeInvalid;
589 else if (name.equals("__objc_imageinfo"))
590 sect_type = lldb::eSectionTypeOther;
595 uint8_t *IRExecutionUnit::MemoryManager::allocateCodeSection(
596 uintptr_t Size, unsigned Alignment, unsigned SectionID,
597 llvm::StringRef SectionName) {
598 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
600 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(
601 Size, Alignment, SectionID, SectionName);
603 m_parent.m_records.push_back(AllocationRecord(
604 (uintptr_t)return_value,
605 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
606 GetSectionTypeFromSectionName(SectionName, AllocationKind::Code), Size,
607 Alignment, SectionID, SectionName.str().c_str()));
610 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
611 ", Alignment=%u, SectionID=%u) = %p",
612 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
615 if (m_parent.m_reported_allocations) {
617 lldb::ProcessSP process_sp =
618 m_parent.GetBestExecutionContextScope()->CalculateProcess();
620 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
626 uint8_t *IRExecutionUnit::MemoryManager::allocateDataSection(
627 uintptr_t Size, unsigned Alignment, unsigned SectionID,
628 llvm::StringRef SectionName, bool IsReadOnly) {
629 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
631 uint8_t *return_value = m_default_mm_ap->allocateDataSection(
632 Size, Alignment, SectionID, SectionName, IsReadOnly);
634 uint32_t permissions = lldb::ePermissionsReadable;
636 permissions |= lldb::ePermissionsWritable;
637 m_parent.m_records.push_back(AllocationRecord(
638 (uintptr_t)return_value, permissions,
639 GetSectionTypeFromSectionName(SectionName, AllocationKind::Data), Size,
640 Alignment, SectionID, SectionName.str().c_str()));
642 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
643 ", Alignment=%u, SectionID=%u) = %p",
644 (uint64_t)Size, Alignment, SectionID, (void *)return_value);
647 if (m_parent.m_reported_allocations) {
649 lldb::ProcessSP process_sp =
650 m_parent.GetBestExecutionContextScope()->CalculateProcess();
652 m_parent.CommitOneAllocation(process_sp, err, m_parent.m_records.back());
659 FindBestAlternateMangledName(const ConstString &demangled,
660 const lldb::LanguageType &lang_type,
661 const SymbolContext &sym_ctx) {
662 CPlusPlusLanguage::MethodName cpp_name(demangled);
663 std::string scope_qualified_name = cpp_name.GetScopeQualifiedName();
665 if (!scope_qualified_name.size())
666 return ConstString();
668 if (!sym_ctx.module_sp)
669 return ConstString();
671 SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor();
673 return ConstString();
675 lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile();
677 return ConstString();
679 std::vector<ConstString> alternates;
680 sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates);
682 std::vector<ConstString> param_and_qual_matches;
683 std::vector<ConstString> param_matches;
684 for (size_t i = 0; i < alternates.size(); i++) {
685 ConstString alternate_mangled_name = alternates[i];
686 Mangled mangled(alternate_mangled_name, true);
687 ConstString demangled = mangled.GetDemangledName(lang_type);
689 CPlusPlusLanguage::MethodName alternate_cpp_name(demangled);
690 if (!cpp_name.IsValid())
693 if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) {
694 if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers())
695 param_and_qual_matches.push_back(alternate_mangled_name);
697 param_matches.push_back(alternate_mangled_name);
701 if (param_and_qual_matches.size())
702 return param_and_qual_matches[0]; // It is assumed that there will be only
704 else if (param_matches.size())
705 return param_matches[0]; // Return one of them as a best match
707 return ConstString();
710 struct IRExecutionUnit::SearchSpec {
712 lldb::FunctionNameType mask;
714 SearchSpec(ConstString n,
715 lldb::FunctionNameType m = lldb::eFunctionNameTypeFull)
716 : name(n), mask(m) {}
719 void IRExecutionUnit::CollectCandidateCNames(
720 std::vector<IRExecutionUnit::SearchSpec> &C_specs,
721 const ConstString &name) {
722 if (m_strip_underscore && name.AsCString()[0] == '_')
723 C_specs.insert(C_specs.begin(), ConstString(&name.AsCString()[1]));
724 C_specs.push_back(SearchSpec(name));
727 void IRExecutionUnit::CollectCandidateCPlusPlusNames(
728 std::vector<IRExecutionUnit::SearchSpec> &CPP_specs,
729 const std::vector<SearchSpec> &C_specs, const SymbolContext &sc) {
730 for (const SearchSpec &C_spec : C_specs) {
731 const ConstString &name = C_spec.name;
733 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
734 Mangled mangled(name, true);
735 ConstString demangled =
736 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
739 ConstString best_alternate_mangled_name = FindBestAlternateMangledName(
740 demangled, lldb::eLanguageTypeC_plus_plus, sc);
742 if (best_alternate_mangled_name) {
743 CPP_specs.push_back(best_alternate_mangled_name);
746 CPP_specs.push_back(SearchSpec(demangled, lldb::eFunctionNameTypeFull));
750 std::set<ConstString> alternates;
751 CPlusPlusLanguage::FindAlternateFunctionManglings(name, alternates);
752 CPP_specs.insert(CPP_specs.end(), alternates.begin(), alternates.end());
756 void IRExecutionUnit::CollectFallbackNames(
757 std::vector<SearchSpec> &fallback_specs,
758 const std::vector<SearchSpec> &C_specs) {
759 // As a last-ditch fallback, try the base name for C++ names. It's terrible,
760 // but the DWARF doesn't always encode "extern C" correctly.
762 for (const SearchSpec &C_spec : C_specs) {
763 const ConstString &name = C_spec.name;
765 if (CPlusPlusLanguage::IsCPPMangledName(name.GetCString())) {
766 Mangled mangled_name(name);
767 ConstString demangled_name =
768 mangled_name.GetDemangledName(lldb::eLanguageTypeC_plus_plus);
769 if (!demangled_name.IsEmpty()) {
770 const char *demangled_cstr = demangled_name.AsCString();
771 const char *lparen_loc = strchr(demangled_cstr, '(');
773 llvm::StringRef base_name(demangled_cstr,
774 lparen_loc - demangled_cstr);
775 fallback_specs.push_back(ConstString(base_name));
782 lldb::addr_t IRExecutionUnit::FindInSymbols(
783 const std::vector<IRExecutionUnit::SearchSpec> &specs,
784 const lldb_private::SymbolContext &sc) {
785 Target *target = sc.target_sp.get();
788 // we shouldn't be doing any symbol lookup at all without a target
789 return LLDB_INVALID_ADDRESS;
792 for (const SearchSpec &spec : specs) {
793 SymbolContextList sc_list;
795 lldb::addr_t best_internal_load_address = LLDB_INVALID_ADDRESS;
797 std::function<bool(lldb::addr_t &, SymbolContextList &,
798 const lldb_private::SymbolContext &)>
799 get_external_load_address = [&best_internal_load_address, target](
800 lldb::addr_t &load_address, SymbolContextList &sc_list,
801 const lldb_private::SymbolContext &sc) -> lldb::addr_t {
802 load_address = LLDB_INVALID_ADDRESS;
804 for (size_t si = 0, se = sc_list.GetSize(); si < se; ++si) {
805 SymbolContext candidate_sc;
807 sc_list.GetContextAtIndex(si, candidate_sc);
809 const bool is_external =
810 (candidate_sc.function) ||
811 (candidate_sc.symbol && candidate_sc.symbol->IsExternal());
812 if (candidate_sc.symbol) {
813 load_address = candidate_sc.symbol->ResolveCallableAddress(*target);
815 if (load_address == LLDB_INVALID_ADDRESS) {
816 if (target->GetProcessSP())
818 candidate_sc.symbol->GetAddress().GetLoadAddress(target);
820 load_address = candidate_sc.symbol->GetAddress().GetFileAddress();
824 if (load_address == LLDB_INVALID_ADDRESS && candidate_sc.function) {
825 if (target->GetProcessSP())
826 load_address = candidate_sc.function->GetAddressRange()
828 .GetLoadAddress(target);
830 load_address = candidate_sc.function->GetAddressRange()
835 if (load_address != LLDB_INVALID_ADDRESS) {
838 } else if (best_internal_load_address == LLDB_INVALID_ADDRESS) {
839 best_internal_load_address = load_address;
840 load_address = LLDB_INVALID_ADDRESS;
849 sc.module_sp->FindFunctions(spec.name, NULL, spec.mask,
850 true, // include_symbols
851 false, // include_inlines
856 lldb::addr_t load_address = LLDB_INVALID_ADDRESS;
858 if (get_external_load_address(load_address, sc_list, sc)) {
864 if (sc_list.GetSize() == 0 && sc.target_sp) {
865 sc.target_sp->GetImages().FindFunctions(spec.name, spec.mask,
866 true, // include_symbols
867 false, // include_inlines
872 if (get_external_load_address(load_address, sc_list, sc)) {
878 if (sc_list.GetSize() == 0 && sc.target_sp) {
879 sc.target_sp->GetImages().FindSymbolsWithNameAndType(
880 spec.name, lldb::eSymbolTypeAny, sc_list);
883 if (get_external_load_address(load_address, sc_list, sc)) {
886 // if there are any searches we try after this, add an sc_list.Clear() in
887 // an "else" clause here
889 if (best_internal_load_address != LLDB_INVALID_ADDRESS) {
890 return best_internal_load_address;
894 return LLDB_INVALID_ADDRESS;
898 IRExecutionUnit::FindInRuntimes(const std::vector<SearchSpec> &specs,
899 const lldb_private::SymbolContext &sc) {
900 lldb::TargetSP target_sp = sc.target_sp;
903 return LLDB_INVALID_ADDRESS;
906 lldb::ProcessSP process_sp = sc.target_sp->GetProcessSP();
909 return LLDB_INVALID_ADDRESS;
912 ObjCLanguageRuntime *runtime = process_sp->GetObjCLanguageRuntime();
915 for (const SearchSpec &spec : specs) {
916 lldb::addr_t symbol_load_addr = runtime->LookupRuntimeSymbol(spec.name);
918 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
919 return symbol_load_addr;
923 return LLDB_INVALID_ADDRESS;
926 lldb::addr_t IRExecutionUnit::FindInUserDefinedSymbols(
927 const std::vector<SearchSpec> &specs,
928 const lldb_private::SymbolContext &sc) {
929 lldb::TargetSP target_sp = sc.target_sp;
931 for (const SearchSpec &spec : specs) {
932 lldb::addr_t symbol_load_addr = target_sp->GetPersistentSymbol(spec.name);
934 if (symbol_load_addr != LLDB_INVALID_ADDRESS)
935 return symbol_load_addr;
938 return LLDB_INVALID_ADDRESS;
942 IRExecutionUnit::FindSymbol(const lldb_private::ConstString &name) {
943 std::vector<SearchSpec> candidate_C_names;
944 std::vector<SearchSpec> candidate_CPlusPlus_names;
946 CollectCandidateCNames(candidate_C_names, name);
948 lldb::addr_t ret = FindInSymbols(candidate_C_names, m_sym_ctx);
949 if (ret == LLDB_INVALID_ADDRESS)
950 ret = FindInRuntimes(candidate_C_names, m_sym_ctx);
952 if (ret == LLDB_INVALID_ADDRESS)
953 ret = FindInUserDefinedSymbols(candidate_C_names, m_sym_ctx);
955 if (ret == LLDB_INVALID_ADDRESS) {
956 CollectCandidateCPlusPlusNames(candidate_CPlusPlus_names, candidate_C_names,
958 ret = FindInSymbols(candidate_CPlusPlus_names, m_sym_ctx);
961 if (ret == LLDB_INVALID_ADDRESS) {
962 std::vector<SearchSpec> candidate_fallback_names;
964 CollectFallbackNames(candidate_fallback_names, candidate_C_names);
965 ret = FindInSymbols(candidate_fallback_names, m_sym_ctx);
971 void IRExecutionUnit::GetStaticInitializers(
972 std::vector<lldb::addr_t> &static_initializers) {
973 if (llvm::GlobalVariable *global_ctors =
974 m_module->getNamedGlobal("llvm.global_ctors")) {
975 if (llvm::ConstantArray *ctor_array = llvm::dyn_cast<llvm::ConstantArray>(
976 global_ctors->getInitializer())) {
977 for (llvm::Use &ctor_use : ctor_array->operands()) {
978 if (llvm::ConstantStruct *ctor_struct =
979 llvm::dyn_cast<llvm::ConstantStruct>(ctor_use)) {
980 lldbassert(ctor_struct->getNumOperands() ==
981 3); // this is standardized
982 if (llvm::Function *ctor_function =
983 llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1))) {
984 ConstString ctor_function_name_cs(ctor_function->getName().str());
986 for (JittedFunction &jitted_function : m_jitted_functions) {
987 if (ctor_function_name_cs == jitted_function.m_name) {
988 if (jitted_function.m_remote_addr != LLDB_INVALID_ADDRESS) {
989 static_initializers.push_back(jitted_function.m_remote_addr);
1002 IRExecutionUnit::MemoryManager::getSymbolAddress(const std::string &Name) {
1003 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1005 ConstString name_cs(Name.c_str());
1007 lldb::addr_t ret = m_parent.FindSymbol(name_cs);
1009 if (ret == LLDB_INVALID_ADDRESS) {
1012 "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
1015 m_parent.ReportSymbolLookupError(name_cs);
1019 log->Printf("IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
1025 void *IRExecutionUnit::MemoryManager::getPointerToNamedFunction(
1026 const std::string &Name, bool AbortOnFailure) {
1027 assert(sizeof(void *) == 8);
1029 return (void *)getSymbolAddress(Name);
1033 IRExecutionUnit::GetRemoteAddressForLocal(lldb::addr_t local_address) {
1034 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1036 for (AllocationRecord &record : m_records) {
1037 if (local_address >= record.m_host_address &&
1038 local_address < record.m_host_address + record.m_size) {
1039 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1040 return LLDB_INVALID_ADDRESS;
1043 record.m_process_address + (local_address - record.m_host_address);
1047 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
1048 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64
1049 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
1050 local_address, (uint64_t)record.m_host_address,
1051 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
1052 record.m_process_address, record.m_process_address + record.m_size);
1059 return LLDB_INVALID_ADDRESS;
1062 IRExecutionUnit::AddrRange
1063 IRExecutionUnit::GetRemoteRangeForLocal(lldb::addr_t local_address) {
1064 for (AllocationRecord &record : m_records) {
1065 if (local_address >= record.m_host_address &&
1066 local_address < record.m_host_address + record.m_size) {
1067 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1068 return AddrRange(0, 0);
1070 return AddrRange(record.m_process_address, record.m_size);
1074 return AddrRange(0, 0);
1077 bool IRExecutionUnit::CommitOneAllocation(lldb::ProcessSP &process_sp,
1079 AllocationRecord &record) {
1080 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1084 switch (record.m_sect_type) {
1085 case lldb::eSectionTypeInvalid:
1086 case lldb::eSectionTypeDWARFDebugAbbrev:
1087 case lldb::eSectionTypeDWARFDebugAddr:
1088 case lldb::eSectionTypeDWARFDebugAranges:
1089 case lldb::eSectionTypeDWARFDebugCuIndex:
1090 case lldb::eSectionTypeDWARFDebugFrame:
1091 case lldb::eSectionTypeDWARFDebugInfo:
1092 case lldb::eSectionTypeDWARFDebugLine:
1093 case lldb::eSectionTypeDWARFDebugLoc:
1094 case lldb::eSectionTypeDWARFDebugLocLists:
1095 case lldb::eSectionTypeDWARFDebugMacInfo:
1096 case lldb::eSectionTypeDWARFDebugPubNames:
1097 case lldb::eSectionTypeDWARFDebugPubTypes:
1098 case lldb::eSectionTypeDWARFDebugRanges:
1099 case lldb::eSectionTypeDWARFDebugStr:
1100 case lldb::eSectionTypeDWARFDebugStrOffsets:
1101 case lldb::eSectionTypeDWARFAppleNames:
1102 case lldb::eSectionTypeDWARFAppleTypes:
1103 case lldb::eSectionTypeDWARFAppleNamespaces:
1104 case lldb::eSectionTypeDWARFAppleObjC:
1105 case lldb::eSectionTypeDWARFGNUDebugAltLink:
1109 const bool zero_memory = false;
1110 record.m_process_address =
1111 Malloc(record.m_size, record.m_alignment, record.m_permissions,
1112 eAllocationPolicyProcessOnly, zero_memory, error);
1116 return error.Success();
1119 bool IRExecutionUnit::CommitAllocations(lldb::ProcessSP &process_sp) {
1122 lldb_private::Status err;
1124 for (AllocationRecord &record : m_records) {
1125 ret = CommitOneAllocation(process_sp, err, record);
1133 for (AllocationRecord &record : m_records) {
1134 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1135 Free(record.m_process_address, err);
1136 record.m_process_address = LLDB_INVALID_ADDRESS;
1144 void IRExecutionUnit::ReportAllocations(llvm::ExecutionEngine &engine) {
1145 m_reported_allocations = true;
1147 for (AllocationRecord &record : m_records) {
1148 if (record.m_process_address == LLDB_INVALID_ADDRESS)
1151 if (record.m_section_id == eSectionIDInvalid)
1154 engine.mapSectionAddress((void *)record.m_host_address,
1155 record.m_process_address);
1158 // Trigger re-application of relocations.
1159 engine.finalizeObject();
1162 bool IRExecutionUnit::WriteData(lldb::ProcessSP &process_sp) {
1163 bool wrote_something = false;
1164 for (AllocationRecord &record : m_records) {
1165 if (record.m_process_address != LLDB_INVALID_ADDRESS) {
1166 lldb_private::Status err;
1167 WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
1168 record.m_size, err);
1170 wrote_something = true;
1173 return wrote_something;
1176 void IRExecutionUnit::AllocationRecord::dump(Log *log) {
1180 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
1181 (unsigned long long)m_host_address, (unsigned long long)m_size,
1182 (unsigned long long)m_process_address, (unsigned)m_alignment,
1183 (unsigned)m_section_id, m_name.c_str());
1186 lldb::ByteOrder IRExecutionUnit::GetByteOrder() const {
1187 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1188 return exe_ctx.GetByteOrder();
1191 uint32_t IRExecutionUnit::GetAddressByteSize() const {
1192 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1193 return exe_ctx.GetAddressByteSize();
1196 void IRExecutionUnit::PopulateSymtab(lldb_private::ObjectFile *obj_file,
1197 lldb_private::Symtab &symtab) {
1198 // No symbols yet...
1201 void IRExecutionUnit::PopulateSectionList(
1202 lldb_private::ObjectFile *obj_file,
1203 lldb_private::SectionList §ion_list) {
1204 for (AllocationRecord &record : m_records) {
1205 if (record.m_size > 0) {
1206 lldb::SectionSP section_sp(new lldb_private::Section(
1207 obj_file->GetModule(), obj_file, record.m_section_id,
1208 ConstString(record.m_name), record.m_sect_type,
1209 record.m_process_address, record.m_size,
1210 record.m_host_address, // file_offset (which is the host address for
1212 record.m_size, // file_size
1214 record.m_permissions)); // flags
1215 section_list.AddSection(section_sp);
1220 ArchSpec IRExecutionUnit::GetArchitecture() {
1221 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1222 if(Target *target = exe_ctx.GetTargetPtr())
1223 return target->GetArchitecture();
1227 lldb::ModuleSP IRExecutionUnit::GetJITModule() {
1228 ExecutionContext exe_ctx(GetBestExecutionContextScope());
1229 Target *target = exe_ctx.GetTargetPtr();
1233 auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
1234 shared_from_this());
1236 lldb::ModuleSP jit_module_sp =
1237 lldb_private::Module::CreateModuleFromObjectFile<ObjectFileJIT>(Delegate);
1241 bool changed = false;
1242 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
1243 return jit_module_sp;