9#include "llvm/ExecutionEngine/ExecutionEngine.h"
10#include "llvm/ExecutionEngine/ObjectCache.h"
11#include "llvm/IR/Constants.h"
12#include "llvm/IR/DiagnosticHandler.h"
13#include "llvm/IR/DiagnosticInfo.h"
14#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/SourceMgr.h"
18#include "llvm/Support/raw_ostream.h"
42 std::unique_ptr<llvm::Module> &module_up,
46 std::vector<std::string> &cpu_features)
52 ?
m_module_up->getDataLayout().getGlobalPrefix() ==
'_'
72 if (function.m_name ==
m_name) {
73 func_local_addr = function.m_local_addr;
74 func_remote_addr = function.m_remote_addr;
80 "Couldn't find function {0} for disassembly",
m_name);
85 "Found function, has local address 0x%" PRIx64
86 " and remote address 0x%" PRIx64,
87 (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
89 std::pair<lldb::addr_t, lldb::addr_t> func_range;
93 if (func_range.first == 0 && func_range.second == 0) {
95 "Couldn't find code range for function {0}",
m_name);
99 LLDB_LOGF(log,
"Function's code range is [0x%" PRIx64
"+0x%" PRIx64
"]",
100 func_range.first, func_range.second);
113 process->
ReadMemory(func_remote_addr, buffer_sp->GetBytes(),
114 buffer_sp->GetByteSize(), err);
124 const char *plugin_name =
nullptr;
125 const char *flavor_string =
nullptr;
126 const char *cpu_string =
nullptr;
127 const char *features_string =
nullptr;
129 arch, flavor_string, cpu_string, features_string, plugin_name);
131 if (!disassembler_sp) {
133 "Unable to find disassembler plug-in for %s architecture.",
147 LLDB_LOGF(log,
"Function data has contents:");
152 disassembler_sp->DecodeInstructions(
Address(func_remote_addr), extractor, 0,
155 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
156 instruction_list.
Dump(&stream,
true,
true,
false,
163struct IRExecDiagnosticHandler :
public llvm::DiagnosticHandler {
165 IRExecDiagnosticHandler(
Status *err) : err(err) {}
166 bool handleDiagnostics(
const llvm::DiagnosticInfo &DI)
override {
167 if (DI.getSeverity() == llvm::DS_Error) {
168 const auto &DISM = llvm::cast<llvm::DiagnosticInfoSrcMgr>(DI);
171 "IRExecution error: %s",
172 DISM.getSMDiag().getMessage().str().c_str());
189 static std::recursive_mutex s_runnable_info_mutex;
197 "process because the process is invalid");
208 std::lock_guard<std::recursive_mutex> guard(s_runnable_info_mutex);
214 std::string error_string;
218 llvm::raw_string_ostream oss(s);
222 LLDB_LOGF(log,
"Module being sent to JIT: \n%s", s.c_str());
226 std::make_unique<IRExecDiagnosticHandler>(&
error));
228 llvm::EngineBuilder builder(std::move(
m_module_up));
229 llvm::Triple triple(
m_module->getTargetTriple());
231 builder.setEngineKind(llvm::EngineKind::JIT)
232 .setErrorStr(&error_string)
233 .setRelocationModel(triple.isOSBinFormatMachO() ? llvm::Reloc::PIC_
234 : llvm::Reloc::Static)
235 .setMCJITMemoryManager(std::make_unique<MemoryManager>(*
this))
236 .setOptLevel(llvm::CodeGenOptLevel::Less);
241 if (triple.isRISCV64())
242 builder.setCodeModel(llvm::CodeModel::Large);
244 llvm::StringRef mArch;
245 llvm::StringRef mCPU;
246 llvm::SmallVector<std::string, 0> mAttrs;
249 mAttrs.push_back(feature);
251 llvm::TargetMachine *target_machine =
252 builder.selectTarget(triple, mArch, mCPU, mAttrs);
258 error_string.c_str());
262 class ObjectDumper :
public llvm::ObjectCache {
264 ObjectDumper(
FileSpec output_dir) : m_out_dir(output_dir) {}
265 void notifyObjectCompiled(
const llvm::Module *module,
266 llvm::MemoryBufferRef
object)
override {
268 llvm::SmallVector<char, 256> result_path;
269 std::string object_name_model =
270 "jit-object-" +
module->getModuleIdentifier() + "-%%%.o";
273 std::string model_path = model_spec.
GetPath();
275 std::error_code result
276 = llvm::sys::fs::createUniqueFile(model_path, fd, result_path);
278 llvm::raw_fd_ostream fds(fd,
true);
279 fds.write(
object.getBufferStart(),
object.getBufferSize());
282 std::unique_ptr<llvm::MemoryBuffer>
283 getObject(
const llvm::Module *module)
override {
292 FileSpec save_objects_dir = process_sp->GetTarget().GetSaveJITObjectsDir();
293 if (save_objects_dir) {
304 for (llvm::Function &function : *
m_module) {
305 if (function.isDeclaration() || function.hasPrivateLinkage())
308 const bool external = !function.hasLocalLinkage();
312 if (!
error.Success()) {
319 "'%s' was in the JITted module but wasn't lowered",
320 function.getName().str().c_str());
324 function.getName().str().c_str(), external,
reinterpret_cast<uintptr_t
>(fun_ptr)));
336 std::function<void(llvm::GlobalValue &)> RegisterOneValue = [
this](
337 llvm::GlobalValue &val) {
338 if (val.hasExternalLinkage() && !val.isDeclaration()) {
339 uint64_t var_ptr_addr =
349 remote_addr = var_ptr_addr;
352 if (var_ptr_addr != 0)
358 for (llvm::GlobalVariable &global_var :
m_module->globals()) {
359 RegisterOneValue(global_var);
362 for (llvm::GlobalAlias &global_alias :
m_module->aliases()) {
363 RegisterOneValue(global_alias);
373 bool emitNewLine =
false;
385 "\nHint: The expression tried to call a function that is not present "
386 "in the target, perhaps because it was optimized out by the compiler.");
396 jitted_function.m_remote_addr =
399 if (!
m_name.IsEmpty() && jitted_function.m_name ==
m_name) {
408 LLDB_LOGF(log,
"Code can be run in the target.");
415 LLDB_LOGF(log,
"Couldn't disassemble function : %s",
436 record.m_process_address, 16,
442 DataExtractor my_extractor((
const void *)record.m_host_address,
444 my_extractor.
PutToLog(log, 0, record.m_size, record.m_host_address, 16,
468 switch (alloc_kind) {
487 if (name ==
"__text" || name ==
".text")
489 else if (name ==
"__data" || name ==
".data")
491 else if (name.starts_with(
"__debug_") || name.starts_with(
".debug_")) {
492 const uint32_t name_idx = name[0] ==
'_' ? 8 : 7;
493 llvm::StringRef dwarf_name(name.substr(name_idx));
495 }
else if (name.starts_with(
"__apple_") || name.starts_with(
".apple_"))
497 else if (name ==
"__objc_imageinfo")
504 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
505 llvm::StringRef SectionName) {
509 Size, Alignment, SectionID, SectionName);
512 (uintptr_t)return_value,
513 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
515 Alignment, SectionID, SectionName.str().c_str()));
518 "IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64
519 ", Alignment=%u, SectionID=%u) = %p",
520 (uint64_t)Size, Alignment, SectionID, (
void *)return_value);
522 if (
m_parent.m_reported_allocations) {
525 m_parent.GetBestExecutionContextScope()->CalculateProcess();
534 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
535 llvm::StringRef SectionName,
bool IsReadOnly) {
539 Size, Alignment, SectionID, SectionName, IsReadOnly);
541 uint32_t permissions = lldb::ePermissionsReadable;
543 permissions |= lldb::ePermissionsWritable;
545 (uintptr_t)return_value, permissions,
547 Alignment, SectionID, SectionName.str().c_str()));
549 "IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64
550 ", Alignment=%u, SectionID=%u) = %p",
551 (uint64_t)Size, Alignment, SectionID, (
void *)return_value);
553 if (
m_parent.m_reported_allocations) {
556 m_parent.GetBestExecutionContextScope()->CalculateProcess();
565 std::vector<lldb::addr_t> &static_initializers) {
568 llvm::GlobalVariable *global_ctors =
569 m_module->getNamedGlobal(
"llvm.global_ctors");
571 LLDB_LOG(log,
"Couldn't find llvm.global_ctors.");
575 llvm::dyn_cast<llvm::ConstantArray>(global_ctors->getInitializer());
577 LLDB_LOG(log,
"llvm.global_ctors not a ConstantArray.");
581 for (llvm::Use &ctor_use : ctor_array->operands()) {
582 auto *ctor_struct = llvm::dyn_cast<llvm::ConstantStruct>(ctor_use);
586 lldbassert(ctor_struct->getNumOperands() == 3);
587 auto *ctor_function =
588 llvm::dyn_cast<llvm::Function>(ctor_struct->getOperand(1));
589 if (!ctor_function) {
590 LLDB_LOG(log,
"global_ctor doesn't contain an llvm::Function");
594 ConstString ctor_function_name(ctor_function->getName().str());
595 LLDB_LOG(log,
"Looking for callable jitted function with name {0}.",
599 if (ctor_function_name != jitted_function.m_name)
602 LLDB_LOG(log,
"Found jitted function with invalid address.");
605 static_initializers.push_back(jitted_function.m_remote_addr);
606 LLDB_LOG(log,
"Calling function at address {0:x}.",
607 jitted_function.m_remote_addr);
615 bool missing_weak =
false;
619 return llvm::JITSymbol(addr,
620 llvm::JITSymbolFlags::Exported | llvm::JITSymbolFlags::Weak);
622 return llvm::JITSymbol(addr, llvm::JITSymbolFlags::Exported);
627 bool missing_weak =
false;
633 const std::string &Name,
bool &missing_weak) {
642 "IRExecutionUnit::getSymbolAddress(Name=\"%s\") = <not found>",
645 m_parent.ReportSymbolLookupError(name_cs);
648 LLDB_LOGF(log,
"IRExecutionUnit::getSymbolAddress(Name=\"%s\") = %" PRIx64,
655 const std::string &Name,
bool AbortOnFailure) {
664 if (local_address >= record.m_host_address &&
665 local_address < record.m_host_address + record.m_size) {
670 record.m_process_address + (local_address - record.m_host_address);
673 "IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64
674 " in [0x%" PRIx64
"..0x%" PRIx64
"], and returned 0x%" PRIx64
675 " from [0x%" PRIx64
"..0x%" PRIx64
"].",
676 local_address, (uint64_t)record.m_host_address,
677 (uint64_t)record.m_host_address + (uint64_t)record.m_size, ret,
678 record.m_process_address,
679 record.m_process_address + record.m_size);
691 if (local_address >= record.m_host_address &&
692 local_address < record.m_host_address + record.m_size) {
696 return AddrRange(record.m_process_address, record.m_size);
735 const bool zero_memory =
false;
736 if (
auto address_or_error =
745 return error.Success();
764 Free(record.m_process_address, err);
783 engine.mapSectionAddress((
void *)record.m_host_address,
784 record.m_process_address);
788 engine.finalizeObject();
792 bool wrote_something =
false;
796 WriteMemory(record.m_process_address, (uint8_t *)record.m_host_address,
799 wrote_something =
true;
802 return wrote_something;
810 "[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d, name %s)",
835 if (record.m_size > 0) {
837 obj_file->
GetModule(), obj_file, record.m_section_id, record.m_name,
838 record.m_sect_type, record.m_process_address, record.m_size,
839 record.m_host_address,
843 record.m_permissions);
852 return target->GetArchitecture();
862 auto Delegate = std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(
870 bool changed =
false;
871 jit_module_sp->SetLoadAddress(*target, 0,
true, changed);
872 return jit_module_sp;
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOGF(log,...)
A section + offset based address class.
An architecture specification class.
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
A uniqued constant string class.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
static lldb::DisassemblerSP FindPlugin(const ArchSpec &arch, const char *flavor, const char *cpu, const char *features, const char *plugin_name)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
uint32_t GetAddressByteSize() const
lldb::ByteOrder GetByteOrder() const
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true) override
std::unique_ptr< SectionMemoryManager > m_default_mm_up
The memory allocator to use in actually creating space.
uint64_t getSymbolAddress(const std::string &Name) override
uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, llvm::StringRef SectionName, bool IsReadOnly) override
Allocate space for data, and add it to the m_spaceBlocks map.
IRExecutionUnit & m_parent
The execution unit this is a proxy for.
~MemoryManager() override
uint64_t GetSymbolAddressAndPresence(const std::string &Name, bool &missing_weak)
llvm::JITSymbol findSymbol(const std::string &Name) override
MemoryManager(IRExecutionUnit &parent)
uint8_t * allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, llvm::StringRef SectionName) override
Allocate space for executable code, and add it to the m_spaceBlocks map.
std::vector< std::string > m_cpu_features
static const unsigned eSectionIDInvalid
void PopulateSymtab(lldb_private::ObjectFile *obj_file, lldb_private::Symtab &symtab) override
void GetRunnableInfo(Status &error, lldb::addr_t &func_addr, lldb::addr_t &func_end)
lldb::ByteOrder GetByteOrder() const override
ObjectFileJITDelegate overrides.
std::vector< JittedFunction > m_jitted_functions
A vector of all functions that have been JITted into machine code.
Status DisassembleFunction(Stream &stream, lldb::ProcessSP &process_sp)
std::vector< ConstString > m_failed_lookups
std::unique_ptr< llvm::Module > m_module_up
Holder for the module until it's been handed off.
IRExecutionUnit(std::unique_ptr< llvm::LLVMContext > &context_up, std::unique_ptr< llvm::Module > &module_up, ConstString &name, const lldb::TargetSP &target_sp, const SymbolContext &sym_ctx, std::vector< std::string > &cpu_features)
Constructor.
static lldb::SectionType GetSectionTypeFromSectionName(const llvm::StringRef &name, AllocationKind alloc_kind)
uint32_t GetAddressByteSize() const override
bool CommitAllocations(lldb::ProcessSP &process_sp)
Commit all allocations to the process and record where they were stored.
bool CommitOneAllocation(lldb::ProcessSP &process_sp, Status &error, AllocationRecord &record)
void GetStaticInitializers(std::vector< lldb::addr_t > &static_initializers)
std::unique_ptr< llvm::ExecutionEngine > m_execution_engine_up
llvm::Module * m_module
Owned by the execution engine.
std::unique_ptr< llvm::LLVMContext > m_context_up
ArchSpec GetArchitecture() override
lldb::addr_t m_function_load_addr
AddrRange GetRemoteRangeForLocal(lldb::addr_t local_address)
Look up the object in m_address_map that contains a given address, find where it was copied to,...
void ReportSymbolLookupError(ConstString name)
lldb::addr_t m_function_end_load_addr
void PopulateSectionList(lldb_private::ObjectFile *obj_file, lldb_private::SectionList §ion_list) override
std::pair< lldb::addr_t, uintptr_t > AddrRange
std::atomic< bool > m_did_jit
std::vector< JittedGlobalVariable > m_jitted_global_variables
A vector of all functions that have been JITted into machine code.
bool WriteData(lldb::ProcessSP &process_sp)
Write the contents of all allocations to the process.
std::unique_ptr< llvm::ObjectCache > m_object_cache_up
ExternalSymbolResolver m_external_symbol_resolver
~IRExecutionUnit() override
Destructor.
lldb::ModuleSP GetJITModule()
void ReportAllocations(llvm::ExecutionEngine &engine)
Report all committed allocations to the execution engine.
lldb::addr_t GetRemoteAddressForLocal(lldb::addr_t local_address)
Look up the object in m_address_map that contains a given address, find where it was copied to,...
bool m_reported_allocations
True after allocations have been reported.
void Free(lldb::addr_t process_address, Status &error)
llvm::Expected< lldb::addr_t > Malloc(size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, AllocationPolicy *used_policy=nullptr)
ExecutionContextScope * GetBestExecutionContextScope() const
lldb::ProcessWP & GetProcessWP()
IRMemoryMap(lldb::TargetSP target_sp)
void WriteMemory(lldb::addr_t process_address, const uint8_t *bytes, size_t size, Status &error)
void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size, Status &error)
@ eAllocationPolicyProcessOnly
The intent is that this allocation exist only in the process.
void Dump(Stream *s, bool show_address, bool show_bytes, bool show_control_flow_kind, const ExecutionContext *exe_ctx)
A class that handles mangled names.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
static lldb::ModuleSP CreateModuleFromObjectFile(Args &&...args)
A plug-in interface definition class for object file parsers.
static lldb::SectionType GetDWARFSectionTypeFromName(llvm::StringRef name)
Parses the section type from a section name for DWARF sections.
A plug-in interface definition class for debugging a process.
size_t ReadMemory(const ProcessAddress &process_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
lldb::ByteOrder GetByteOrder() const
size_t AddSection(const lldb::SectionSP §ion_sp)
void Clear()
Clear the object state.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
bool Success() const
Test for success condition.
const char * GetData() const
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Defines a symbol context baton that can be handed other debug core functions.
const ArchSpec & GetArchitecture() const
uint8_t * GetBytes()
Get a pointer to the data.
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
std::shared_ptr< lldb_private::Target > TargetSP
@ eSectionTypeDWARFDebugStrOffsets
@ eSectionTypeDWARFDebugPubNames
@ eSectionTypeDWARFDebugFrame
@ eSectionTypeDWARFDebugLocLists
DWARF v5 .debug_loclists.
@ eSectionTypeDWARFDebugMacInfo
@ eSectionTypeDWARFAppleNamespaces
@ eSectionTypeDWARFAppleTypes
@ eSectionTypeDWARFDebugInfo
@ eSectionTypeDWARFDebugRanges
@ eSectionTypeDWARFDebugLine
@ eSectionTypeDWARFDebugPubTypes
@ eSectionTypeDWARFDebugStr
@ eSectionTypeDWARFDebugLoc
@ eSectionTypeDWARFAppleNames
@ eSectionTypeDWARFAppleObjC
@ eSectionTypeDWARFDebugCuIndex
@ eSectionTypeDWARFDebugAranges
@ eSectionTypeDWARFGNUDebugAltLink
@ eSectionTypeDWARFDebugAbbrev
@ eSectionTypeDWARFDebugAddr
std::shared_ptr< lldb_private::Module > ModuleSP
Encapsulates a single allocation request made by the JIT.
lldb::SectionType m_sect_type
lldb::addr_t m_process_address
"lldb/Expression/IRExecutionUnit.h" Encapsulates a single function that has been generated by the JIT...