blob: e7cb728778e6a2292a2b4853f869be156b67a0f3 [file] [log] [blame]
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001//===-- IRExecutionUnit.cpp -------------------------------------*- C++ -*-===//
Sean Callanan8dfb68e2013-03-19 00:10:07 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Sean Callanan8dfb68e2013-03-19 00:10:07 +000010#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000011#include "llvm/IR/LLVMContext.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000012#include "llvm/IR/Module.h"
Sean Callanan44bc6572013-03-27 03:09:55 +000013#include "llvm/Support/SourceMgr.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000014#include "lldb/Core/DataBufferHeap.h"
15#include "lldb/Core/DataExtractor.h"
Jason Molendaaff1b352014-10-10 23:07:36 +000016#include "lldb/Core/Debugger.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000017#include "lldb/Core/Disassembler.h"
18#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000019#include "lldb/Core/Module.h"
20#include "lldb/Core/Section.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000021#include "lldb/Expression/IRExecutionUnit.h"
22#include "lldb/Target/ExecutionContext.h"
23#include "lldb/Target/Target.h"
24
25using namespace lldb_private;
26
Greg Clayton7b0992d2013-04-18 22:45:39 +000027IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
28 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000029 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000030 const lldb::TargetSP &target_sp,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000031 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000032 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000033 m_context_ap(context_ap.release()),
34 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000035 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000036 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000037 m_name(name),
38 m_did_jit(false),
39 m_function_load_addr(LLDB_INVALID_ADDRESS),
40 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
41{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000042}
43
44lldb::addr_t
45IRExecutionUnit::WriteNow (const uint8_t *bytes,
46 size_t size,
47 Error &error)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000048{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000049 lldb::addr_t allocation_process_addr = Malloc (size,
50 8,
51 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
52 eAllocationPolicyMirror,
53 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000054
Sean Callanan5a1af4e2013-04-05 02:22:57 +000055 if (!error.Success())
56 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000057
Sean Callanan5a1af4e2013-04-05 02:22:57 +000058 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000059
Sean Callanan5a1af4e2013-04-05 02:22:57 +000060 if (!error.Success())
61 {
62 Error err;
63 Free (allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000064
Sean Callanan5a1af4e2013-04-05 02:22:57 +000065 return LLDB_INVALID_ADDRESS;
66 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000067
Sean Callanan5a1af4e2013-04-05 02:22:57 +000068 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
69 {
70 DataBufferHeap my_buffer(size, 0);
71 Error err;
72 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000073
Sean Callanan5a1af4e2013-04-05 02:22:57 +000074 if (err.Success())
75 {
76 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000077 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000078 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000079 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000080
Sean Callanan5a1af4e2013-04-05 02:22:57 +000081 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000082}
83
84void
85IRExecutionUnit::FreeNow (lldb::addr_t allocation)
86{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000087 if (allocation == LLDB_INVALID_ADDRESS)
88 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000089
Sean Callanan5a1af4e2013-04-05 02:22:57 +000090 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000091
Sean Callanan5a1af4e2013-04-05 02:22:57 +000092 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000093}
94
95Error
96IRExecutionUnit::DisassembleFunction (Stream &stream,
97 lldb::ProcessSP &process_wp)
98{
Greg Clayton5160ce52013-03-27 23:08:40 +000099 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000100
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000101 ExecutionContext exe_ctx(process_wp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000102
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000103 Error ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000104
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000105 ret.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000106
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000107 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
108 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000109
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000110 for (JittedFunction &function : m_jitted_functions)
111 {
112 if (strstr(function.m_name.c_str(), m_name.AsCString()))
113 {
114 func_local_addr = function.m_local_addr;
115 func_remote_addr = function.m_remote_addr;
116 }
117 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000118
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000119 if (func_local_addr == LLDB_INVALID_ADDRESS)
120 {
121 ret.SetErrorToGenericError();
122 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
123 return ret;
124 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000125
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000126 if (log)
127 log->Printf("Found function, has local address 0x%" PRIx64 " and remote address 0x%" PRIx64, (uint64_t)func_local_addr, (uint64_t)func_remote_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000128
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000129 std::pair <lldb::addr_t, lldb::addr_t> func_range;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000130
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000131 func_range = GetRemoteRangeForLocal(func_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000132
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000133 if (func_range.first == 0 && func_range.second == 0)
134 {
135 ret.SetErrorToGenericError();
136 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
137 return ret;
138 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000139
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000140 if (log)
141 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000142
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000143 Target *target = exe_ctx.GetTargetPtr();
144 if (!target)
145 {
146 ret.SetErrorToGenericError();
147 ret.SetErrorString("Couldn't find the target");
148 return ret;
149 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000150
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000151 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000152
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000153 Process *process = exe_ctx.GetProcessPtr();
154 Error err;
155 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000156
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000157 if (!err.Success())
158 {
159 ret.SetErrorToGenericError();
160 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
161 return ret;
162 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000163
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000164 ArchSpec arch(target->GetArchitecture());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000165
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000166 const char *plugin_name = NULL;
167 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000168 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000169
Jim Ingham56d40422013-07-31 02:19:15 +0000170 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000171 {
172 ret.SetErrorToGenericError();
173 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
174 return ret;
175 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000176
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000177 if (!process)
178 {
179 ret.SetErrorToGenericError();
180 ret.SetErrorString("Couldn't find the process");
181 return ret;
182 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000183
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000184 DataExtractor extractor(buffer_sp,
185 process->GetByteOrder(),
186 target->GetArchitecture().GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000187
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000188 if (log)
189 {
190 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000191 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000192 0,
193 extractor.GetByteSize(),
194 func_remote_addr,
195 16,
196 DataExtractor::TypeUInt8);
197 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000198
Jim Ingham56d40422013-07-31 02:19:15 +0000199 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000200
Jim Ingham56d40422013-07-31 02:19:15 +0000201 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000202 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
Jason Molendaaff1b352014-10-10 23:07:36 +0000203 const char *disassemble_format = "${addr-file-or-load}: ";
204 if (exe_ctx.HasTargetScope())
205 {
206 disassemble_format = exe_ctx.GetTargetRef().GetDebugger().GetDisassemblyFormat();
207 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000208
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000209 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
210 instruction_index < num_instructions;
211 ++instruction_index)
212 {
213 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
214 instruction->Dump (&stream,
215 max_opcode_byte_size,
216 true,
217 true,
Jason Molendaaff1b352014-10-10 23:07:36 +0000218 &exe_ctx,
219 NULL,
220 NULL,
221 disassemble_format);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000222 stream.PutChar('\n');
223 }
Jim Ingham56d40422013-07-31 02:19:15 +0000224 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
225 // I'll fix that but for now, just clear the list and it will go away nicely.
226 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000227 return ret;
228}
229
Sean Callanan44bc6572013-03-27 03:09:55 +0000230static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
231{
232 Error *err = static_cast<Error*>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000233
Sean Callanan44bc6572013-03-27 03:09:55 +0000234 if (err && err->Success())
235 {
236 err->SetErrorToGenericError();
237 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
238 }
239}
240
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000241void
242IRExecutionUnit::GetRunnableInfo(Error &error,
243 lldb::addr_t &func_addr,
244 lldb::addr_t &func_end)
245{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000246 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000247
Sean Callanan7c435a52014-02-06 22:25:20 +0000248 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000250 func_addr = LLDB_INVALID_ADDRESS;
251 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000252
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000253 if (!process_sp)
254 {
255 error.SetErrorToGenericError();
256 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
257 return;
258 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000259
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000260 if (m_did_jit)
261 {
262 func_addr = m_function_load_addr;
263 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000264
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000265 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000266 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000267
Sean Callanan7c435a52014-02-06 22:25:20 +0000268 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000269
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000270 m_did_jit = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000271
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000272 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000273
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000274 std::string error_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000276 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000277 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000278 std::string s;
279 llvm::raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000280
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000281 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000282
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000283 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000284
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000285 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
286 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000287
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000288 llvm::Triple triple(m_module->getTargetTriple());
289 llvm::Function *function = m_module->getFunction (m_name.AsCString());
290 llvm::Reloc::Model relocModel;
291 llvm::CodeModel::Model codeModel;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000292
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000293 if (triple.isOSBinFormatELF())
294 {
295 relocModel = llvm::Reloc::Static;
296 // This will be small for 32-bit and large for 64-bit.
297 codeModel = llvm::CodeModel::JITDefault;
298 }
299 else
300 {
301 relocModel = llvm::Reloc::PIC_;
302 codeModel = llvm::CodeModel::Small;
303 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000304
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000305 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000306
Rafael Espindola7c03d952014-08-19 04:27:03 +0000307 llvm::EngineBuilder builder(std::move(m_module_ap));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000308
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000309 builder.setEngineKind(llvm::EngineKind::JIT)
310 .setErrorStr(&error_string)
311 .setRelocationModel(relocModel)
David Majnemerb313e462014-12-04 21:26:25 +0000312 .setMCJITMemoryManager(std::unique_ptr<MemoryManager>(new MemoryManager(*this)))
David Majnemer8faf9372014-09-16 06:34:29 +0000313 .setCodeModel(codeModel)
Reid Kleckner54209532014-09-03 00:40:36 +0000314 .setOptLevel(llvm::CodeGenOpt::Less);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000315
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000316 llvm::StringRef mArch;
317 llvm::StringRef mCPU;
318 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000320 for (std::string &feature : m_cpu_features)
321 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000322
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000323 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
324 mArch,
325 mCPU,
326 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000327
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000328 m_execution_engine_ap.reset(builder.create(target_machine));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000330 if (!m_execution_engine_ap.get())
331 {
332 error.SetErrorToGenericError();
333 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000334 return;
335 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Greg Clayton23f8c952014-03-24 23:10:19 +0000337 // Make sure we see all sections, including ones that don't have relocations...
338 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000339
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000340 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000341
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000342 // We don't actually need the function pointer here, this just forces it to get resolved.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000343
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000344 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000345
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000346 if (!error.Success())
347 {
348 // We got an error through our callback!
349 return;
350 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000351
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000352 if (!function)
353 {
354 error.SetErrorToGenericError();
355 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
356 return;
357 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000358
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000359 if (!fun_ptr)
360 {
361 error.SetErrorToGenericError();
362 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
363 return;
364 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000365
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000366 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000367
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000368 CommitAllocations(process_sp);
369 ReportAllocations(*m_execution_engine_ap);
370 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000371
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000372 for (JittedFunction &jitted_function : m_jitted_functions)
373 {
374 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000375
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000376 if (!jitted_function.m_name.compare(m_name.AsCString()))
377 {
378 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
379 m_function_end_load_addr = func_range.first + func_range.second;
380 m_function_load_addr = jitted_function.m_remote_addr;
381 }
382 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000383
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000384 if (log)
385 {
386 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000387
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000388 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000389
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000390 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000391
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000392 if (!err.Success())
393 {
394 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
395 }
396 else
397 {
398 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
399 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000400
Sean Callanan4f2c1982014-01-21 00:54:48 +0000401 log->Printf("Sections: ");
402 for (AllocationRecord &record : m_records)
403 {
404 if (record.m_process_address != LLDB_INVALID_ADDRESS)
405 {
406 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000407
Sean Callanan4f2c1982014-01-21 00:54:48 +0000408 DataBufferHeap my_buffer(record.m_size, 0);
409 Error err;
410 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000411
Sean Callanan4f2c1982014-01-21 00:54:48 +0000412 if (err.Success())
413 {
414 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
415 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
416 }
417 }
418 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000419 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000420
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000421 func_addr = m_function_load_addr;
422 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000423
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000424 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000425}
426
427IRExecutionUnit::~IRExecutionUnit ()
428{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000429 m_module_ap.reset();
430 m_execution_engine_ap.reset();
431 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000432}
433
434IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
Todd Fialad5635cd2014-09-24 15:55:47 +0000435 m_default_mm_ap (new llvm::SectionMemoryManager()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000436 m_parent (parent)
437{
438}
439
Greg Clayton23f8c952014-03-24 23:10:19 +0000440IRExecutionUnit::MemoryManager::~MemoryManager ()
441{
442}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000443
Greg Clayton23f8c952014-03-24 23:10:19 +0000444lldb::SectionType
445IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
446{
447 lldb::SectionType sect_type = lldb::eSectionTypeCode;
448 switch (alloc_kind)
449 {
450 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
451 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
452 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
453 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
454 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
455 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000456
Greg Clayton23f8c952014-03-24 23:10:19 +0000457 if (!name.empty())
458 {
459 if (name.equals("__text") || name.equals(".text"))
460 sect_type = lldb::eSectionTypeCode;
461 else if (name.equals("__data") || name.equals(".data"))
462 sect_type = lldb::eSectionTypeCode;
463 else if (name.startswith("__debug_") || name.startswith(".debug_"))
464 {
465 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
466 llvm::StringRef dwarf_name(name.substr(name_idx));
467 switch (dwarf_name[0])
468 {
469 case 'a':
470 if (dwarf_name.equals("abbrev"))
471 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
472 else if (dwarf_name.equals("aranges"))
473 sect_type = lldb::eSectionTypeDWARFDebugAranges;
474 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000475
Greg Clayton23f8c952014-03-24 23:10:19 +0000476 case 'f':
477 if (dwarf_name.equals("frame"))
478 sect_type = lldb::eSectionTypeDWARFDebugFrame;
479 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000480
Greg Clayton23f8c952014-03-24 23:10:19 +0000481 case 'i':
482 if (dwarf_name.equals("info"))
483 sect_type = lldb::eSectionTypeDWARFDebugInfo;
484 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000485
Greg Clayton23f8c952014-03-24 23:10:19 +0000486 case 'l':
487 if (dwarf_name.equals("line"))
488 sect_type = lldb::eSectionTypeDWARFDebugLine;
489 else if (dwarf_name.equals("loc"))
490 sect_type = lldb::eSectionTypeDWARFDebugLoc;
491 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000492
Greg Clayton23f8c952014-03-24 23:10:19 +0000493 case 'm':
494 if (dwarf_name.equals("macinfo"))
495 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
496 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000497
Greg Clayton23f8c952014-03-24 23:10:19 +0000498 case 'p':
499 if (dwarf_name.equals("pubnames"))
500 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
501 else if (dwarf_name.equals("pubtypes"))
502 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
503 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000504
Greg Clayton23f8c952014-03-24 23:10:19 +0000505 case 's':
506 if (dwarf_name.equals("str"))
507 sect_type = lldb::eSectionTypeDWARFDebugStr;
508 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000509
Greg Clayton23f8c952014-03-24 23:10:19 +0000510 case 'r':
511 if (dwarf_name.equals("ranges"))
512 sect_type = lldb::eSectionTypeDWARFDebugRanges;
513 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000514
Greg Clayton23f8c952014-03-24 23:10:19 +0000515 default:
516 break;
517 }
518 }
519 else if (name.startswith("__apple_") || name.startswith(".apple_"))
520 {
521#if 0
522 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
523 llvm::StringRef apple_name(name.substr(name_idx));
524 switch (apple_name[0])
525 {
526 case 'n':
527 if (apple_name.equals("names"))
528 sect_type = lldb::eSectionTypeDWARFAppleNames;
529 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
530 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
531 break;
532 case 't':
533 if (apple_name.equals("types"))
534 sect_type = lldb::eSectionTypeDWARFAppleTypes;
535 break;
536 case 'o':
537 if (apple_name.equals("objc"))
538 sect_type = lldb::eSectionTypeDWARFAppleObjC;
539 break;
540 default:
541 break;
542 }
543#else
544 sect_type = lldb::eSectionTypeInvalid;
545#endif
546 }
547 else if (name.equals("__objc_imageinfo"))
548 sect_type = lldb::eSectionTypeOther;
549 }
550 return sect_type;
551}
552
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000553uint8_t *
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000554IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
555 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000556 unsigned SectionID,
557 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000558{
Greg Clayton5160ce52013-03-27 23:08:40 +0000559 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000560
Filip Pizlobfcff682013-10-02 01:43:46 +0000561 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000562
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000563 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000564 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000565 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000566 Size,
567 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000568 SectionID,
569 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000570
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000571 if (log)
572 {
573 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
574 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000575 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000576
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000577 return return_value;
578}
579
580uint8_t *
581IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
582 unsigned Alignment,
583 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000584 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000585 bool IsReadOnly)
586{
Greg Clayton5160ce52013-03-27 23:08:40 +0000587 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000588
Filip Pizlobfcff682013-10-02 01:43:46 +0000589 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000590
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000591 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Greg Clayton23f8c952014-03-24 23:10:19 +0000592 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable),
593 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000594 Size,
595 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000596 SectionID,
597 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000598 if (log)
599 {
600 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
601 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000602 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000603
604 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000605}
606
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000607lldb::addr_t
608IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
609{
Sean Callananffae9442013-06-27 01:42:47 +0000610 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
611
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000612 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000613 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000614 if (local_address >= record.m_host_address &&
615 local_address < record.m_host_address + record.m_size)
616 {
617 if (record.m_process_address == LLDB_INVALID_ADDRESS)
618 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000619
Sean Callananffae9442013-06-27 01:42:47 +0000620 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000621
Sean Callananffae9442013-06-27 01:42:47 +0000622 if (log)
623 {
624 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
625 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000626 (uint64_t)record.m_host_address,
627 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000628 ret,
629 record.m_process_address,
630 record.m_process_address + record.m_size);
631 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000632
Sean Callananffae9442013-06-27 01:42:47 +0000633 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000634 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000635 }
636
637 return LLDB_INVALID_ADDRESS;
638}
639
640IRExecutionUnit::AddrRange
641IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
642{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000643 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000644 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000645 if (local_address >= record.m_host_address &&
646 local_address < record.m_host_address + record.m_size)
647 {
648 if (record.m_process_address == LLDB_INVALID_ADDRESS)
649 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000650
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000651 return AddrRange(record.m_process_address, record.m_size);
652 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000653 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000654
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000655 return AddrRange (0, 0);
656}
657
658bool
659IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
660{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000661 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000662
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000663 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000664
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000665 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000666 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000667 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000668 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000669
Greg Clayton23f8c952014-03-24 23:10:19 +0000670 switch (record.m_sect_type)
671 {
672 case lldb::eSectionTypeInvalid:
673 case lldb::eSectionTypeDWARFDebugAbbrev:
674 case lldb::eSectionTypeDWARFDebugAranges:
675 case lldb::eSectionTypeDWARFDebugFrame:
676 case lldb::eSectionTypeDWARFDebugInfo:
677 case lldb::eSectionTypeDWARFDebugLine:
678 case lldb::eSectionTypeDWARFDebugLoc:
679 case lldb::eSectionTypeDWARFDebugMacInfo:
680 case lldb::eSectionTypeDWARFDebugPubNames:
681 case lldb::eSectionTypeDWARFDebugPubTypes:
682 case lldb::eSectionTypeDWARFDebugRanges:
683 case lldb::eSectionTypeDWARFDebugStr:
684 case lldb::eSectionTypeDWARFAppleNames:
685 case lldb::eSectionTypeDWARFAppleTypes:
686 case lldb::eSectionTypeDWARFAppleNamespaces:
687 case lldb::eSectionTypeDWARFAppleObjC:
688 err.Clear();
689 break;
690 default:
691 record.m_process_address = Malloc (record.m_size,
692 record.m_alignment,
693 record.m_permissions,
694 eAllocationPolicyProcessOnly,
695 err);
696 break;
697 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000698
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000699 if (!err.Success())
700 {
701 ret = false;
702 break;
703 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000704 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000705
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000706 if (!ret)
707 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000708 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000709 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000710 if (record.m_process_address != LLDB_INVALID_ADDRESS)
711 {
712 Free(record.m_process_address, err);
713 record.m_process_address = LLDB_INVALID_ADDRESS;
714 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000715 }
716 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000717
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000718 return ret;
719}
720
721void
722IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
723{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000724 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000725 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000726 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000727 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000728
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000729 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000730 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000731
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000732 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000733 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000734
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000735 // Trigger re-application of relocations.
736 engine.finalizeObject();
737}
738
739bool
740IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
741{
Greg Clayton23f8c952014-03-24 23:10:19 +0000742 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000743 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000744 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000745 if (record.m_process_address != LLDB_INVALID_ADDRESS)
746 {
747 lldb_private::Error err;
748 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
749 if (err.Success())
750 wrote_something = true;
751 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000752 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000753 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000754}
755
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000756void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000757IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000758{
759 if (!log)
760 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000761
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000762 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000763 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000764 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000765 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000766 (unsigned)m_alignment,
767 (unsigned)m_section_id);
768}
Greg Clayton23f8c952014-03-24 23:10:19 +0000769
770
771lldb::ByteOrder
772IRExecutionUnit::GetByteOrder () const
773{
774 ExecutionContext exe_ctx (GetBestExecutionContextScope());
775 return exe_ctx.GetByteOrder();
776}
777
778uint32_t
779IRExecutionUnit::GetAddressByteSize () const
780{
781 ExecutionContext exe_ctx (GetBestExecutionContextScope());
782 return exe_ctx.GetAddressByteSize();
783}
784
785void
786IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
787 lldb_private::Symtab &symtab)
788{
789 // No symbols yet...
790}
791
792
793void
794IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
795 lldb_private::SectionList &section_list)
796{
797 for (AllocationRecord &record : m_records)
798 {
799 if (record.m_size > 0)
800 {
801 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
802 obj_file,
803 record.m_section_id,
804 ConstString(record.m_name),
805 record.m_sect_type,
806 record.m_process_address,
807 record.m_size,
808 record.m_host_address, // file_offset (which is the host address for the data)
809 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000810 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000811 record.m_permissions)); // flags
812 section_list.AddSection (section_sp);
813 }
814 }
815}
816
817bool
818IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
819{
820 ExecutionContext exe_ctx (GetBestExecutionContextScope());
821 Target *target = exe_ctx.GetTargetPtr();
822 if (target)
823 arch = target->GetArchitecture();
824 else
825 arch.Clear();
826 return arch.IsValid();
827}
828
829lldb::ModuleSP
830IRExecutionUnit::GetJITModule ()
831{
832 ExecutionContext exe_ctx (GetBestExecutionContextScope());
833 Target *target = exe_ctx.GetTargetPtr();
834 if (target)
835 {
836 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
837 if (jit_module_sp)
838 {
839 bool changed = false;
840 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
841 }
842 return jit_module_sp;
843 }
844 return lldb::ModuleSP();
845}