blob: 180c68f5bcd41f665ee021038ad92a1de6755911 [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"
16#include "lldb/Core/Disassembler.h"
17#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/Section.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000020#include "lldb/Expression/IRExecutionUnit.h"
21#include "lldb/Target/ExecutionContext.h"
22#include "lldb/Target/Target.h"
23
24using namespace lldb_private;
25
Greg Clayton7b0992d2013-04-18 22:45:39 +000026IRExecutionUnit::IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
27 std::unique_ptr<llvm::Module> &module_ap,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000028 ConstString &name,
Sean Callananb024d872013-04-15 17:12:47 +000029 const lldb::TargetSP &target_sp,
Sean Callanan8dfb68e2013-03-19 00:10:07 +000030 std::vector<std::string> &cpu_features) :
Sean Callananb024d872013-04-15 17:12:47 +000031 IRMemoryMap(target_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +000032 m_context_ap(context_ap.release()),
33 m_module_ap(module_ap.release()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000034 m_module(m_module_ap.get()),
Sean Callanan2c047352013-03-19 23:03:21 +000035 m_cpu_features(cpu_features),
Sean Callanan8dfb68e2013-03-19 00:10:07 +000036 m_name(name),
37 m_did_jit(false),
38 m_function_load_addr(LLDB_INVALID_ADDRESS),
39 m_function_end_load_addr(LLDB_INVALID_ADDRESS)
40{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000041}
42
43lldb::addr_t
44IRExecutionUnit::WriteNow (const uint8_t *bytes,
45 size_t size,
46 Error &error)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000047{
Sean Callanan5a1af4e2013-04-05 02:22:57 +000048 lldb::addr_t allocation_process_addr = Malloc (size,
49 8,
50 lldb::ePermissionsWritable | lldb::ePermissionsReadable,
51 eAllocationPolicyMirror,
52 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000053
Sean Callanan5a1af4e2013-04-05 02:22:57 +000054 if (!error.Success())
55 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000056
Sean Callanan5a1af4e2013-04-05 02:22:57 +000057 WriteMemory(allocation_process_addr, bytes, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000058
Sean Callanan5a1af4e2013-04-05 02:22:57 +000059 if (!error.Success())
60 {
61 Error err;
62 Free (allocation_process_addr, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000063
Sean Callanan5a1af4e2013-04-05 02:22:57 +000064 return LLDB_INVALID_ADDRESS;
65 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000066
Sean Callanan5a1af4e2013-04-05 02:22:57 +000067 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
68 {
69 DataBufferHeap my_buffer(size, 0);
70 Error err;
71 ReadMemory(my_buffer.GetBytes(), allocation_process_addr, size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000072
Sean Callanan5a1af4e2013-04-05 02:22:57 +000073 if (err.Success())
74 {
75 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
Sean Callanan4f2c1982014-01-21 00:54:48 +000076 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), allocation_process_addr, 16, DataExtractor::TypeUInt8);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000077 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +000078 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000079
Sean Callanan5a1af4e2013-04-05 02:22:57 +000080 return allocation_process_addr;
Sean Callanan8dfb68e2013-03-19 00:10:07 +000081}
82
83void
84IRExecutionUnit::FreeNow (lldb::addr_t allocation)
85{
Sean Callanan8dfb68e2013-03-19 00:10:07 +000086 if (allocation == LLDB_INVALID_ADDRESS)
87 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000088
Sean Callanan5a1af4e2013-04-05 02:22:57 +000089 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000090
Sean Callanan5a1af4e2013-04-05 02:22:57 +000091 Free(allocation, err);
Sean Callanan8dfb68e2013-03-19 00:10:07 +000092}
93
94Error
95IRExecutionUnit::DisassembleFunction (Stream &stream,
96 lldb::ProcessSP &process_wp)
97{
Greg Clayton5160ce52013-03-27 23:08:40 +000098 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000099
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000100 ExecutionContext exe_ctx(process_wp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000101
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000102 Error ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000103
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000104 ret.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000105
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000106 lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS;
107 lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000108
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000109 for (JittedFunction &function : m_jitted_functions)
110 {
111 if (strstr(function.m_name.c_str(), m_name.AsCString()))
112 {
113 func_local_addr = function.m_local_addr;
114 func_remote_addr = function.m_remote_addr;
115 }
116 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000117
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000118 if (func_local_addr == LLDB_INVALID_ADDRESS)
119 {
120 ret.SetErrorToGenericError();
121 ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", m_name.AsCString());
122 return ret;
123 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000124
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000125 if (log)
126 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 +0000127
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000128 std::pair <lldb::addr_t, lldb::addr_t> func_range;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000129
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000130 func_range = GetRemoteRangeForLocal(func_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000131
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000132 if (func_range.first == 0 && func_range.second == 0)
133 {
134 ret.SetErrorToGenericError();
135 ret.SetErrorStringWithFormat("Couldn't find code range for function %s", m_name.AsCString());
136 return ret;
137 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000138
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000139 if (log)
140 log->Printf("Function's code range is [0x%" PRIx64 "+0x%" PRIx64 "]", func_range.first, func_range.second);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000141
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000142 Target *target = exe_ctx.GetTargetPtr();
143 if (!target)
144 {
145 ret.SetErrorToGenericError();
146 ret.SetErrorString("Couldn't find the target");
147 return ret;
148 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000149
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000150 lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second, 0));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000151
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000152 Process *process = exe_ctx.GetProcessPtr();
153 Error err;
154 process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000155
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000156 if (!err.Success())
157 {
158 ret.SetErrorToGenericError();
159 ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error"));
160 return ret;
161 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000162
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000163 ArchSpec arch(target->GetArchitecture());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000164
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000165 const char *plugin_name = NULL;
166 const char *flavor_string = NULL;
Jim Ingham56d40422013-07-31 02:19:15 +0000167 lldb::DisassemblerSP disassembler_sp = Disassembler::FindPlugin(arch, flavor_string, plugin_name);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000168
Jim Ingham56d40422013-07-31 02:19:15 +0000169 if (!disassembler_sp)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000170 {
171 ret.SetErrorToGenericError();
172 ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.GetArchitectureName());
173 return ret;
174 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000175
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000176 if (!process)
177 {
178 ret.SetErrorToGenericError();
179 ret.SetErrorString("Couldn't find the process");
180 return ret;
181 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000182
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000183 DataExtractor extractor(buffer_sp,
184 process->GetByteOrder(),
185 target->GetArchitecture().GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000186
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000187 if (log)
188 {
189 log->Printf("Function data has contents:");
Greg Clayton5160ce52013-03-27 23:08:40 +0000190 extractor.PutToLog (log,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000191 0,
192 extractor.GetByteSize(),
193 func_remote_addr,
194 16,
195 DataExtractor::TypeUInt8);
196 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000197
Jim Ingham56d40422013-07-31 02:19:15 +0000198 disassembler_sp->DecodeInstructions (Address (func_remote_addr), extractor, 0, UINT32_MAX, false, false);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000199
Jim Ingham56d40422013-07-31 02:19:15 +0000200 InstructionList &instruction_list = disassembler_sp->GetInstructionList();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000201 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000203 for (size_t instruction_index = 0, num_instructions = instruction_list.GetSize();
204 instruction_index < num_instructions;
205 ++instruction_index)
206 {
207 Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index).get();
208 instruction->Dump (&stream,
209 max_opcode_byte_size,
210 true,
211 true,
212 &exe_ctx);
213 stream.PutChar('\n');
214 }
Jim Ingham56d40422013-07-31 02:19:15 +0000215 // FIXME: The DisassemblerLLVMC has a reference cycle and won't go away if it has any active instructions.
216 // I'll fix that but for now, just clear the list and it will go away nicely.
217 disassembler_sp->GetInstructionList().Clear();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000218 return ret;
219}
220
Sean Callanan44bc6572013-03-27 03:09:55 +0000221static void ReportInlineAsmError(const llvm::SMDiagnostic &diagnostic, void *Context, unsigned LocCookie)
222{
223 Error *err = static_cast<Error*>(Context);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000224
Sean Callanan44bc6572013-03-27 03:09:55 +0000225 if (err && err->Success())
226 {
227 err->SetErrorToGenericError();
228 err->SetErrorStringWithFormat("Inline assembly error: %s", diagnostic.getMessage().str().c_str());
229 }
230}
231
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000232void
233IRExecutionUnit::GetRunnableInfo(Error &error,
234 lldb::addr_t &func_addr,
235 lldb::addr_t &func_end)
236{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000237 lldb::ProcessSP process_sp(GetProcessWP().lock());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Sean Callanan7c435a52014-02-06 22:25:20 +0000239 static Mutex s_runnable_info_mutex(Mutex::Type::eMutexTypeRecursive);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000240
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000241 func_addr = LLDB_INVALID_ADDRESS;
242 func_end = LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000243
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000244 if (!process_sp)
245 {
246 error.SetErrorToGenericError();
247 error.SetErrorString("Couldn't write the JIT compiled code into the process because the process is invalid");
248 return;
249 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000250
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000251 if (m_did_jit)
252 {
253 func_addr = m_function_load_addr;
254 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000255
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000256 return;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000257 };
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000258
Sean Callanan7c435a52014-02-06 22:25:20 +0000259 Mutex::Locker runnable_info_mutex_locker(s_runnable_info_mutex);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000260
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000261 m_did_jit = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000262
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000263 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000264
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000265 std::string error_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000266
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000267 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000268 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000269 std::string s;
270 llvm::raw_string_ostream oss(s);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000271
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000272 m_module->print(oss, NULL);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000273
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000274 oss.flush();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000276 log->Printf ("Module being sent to JIT: \n%s", s.c_str());
277 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000278
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000279 llvm::Triple triple(m_module->getTargetTriple());
280 llvm::Function *function = m_module->getFunction (m_name.AsCString());
281 llvm::Reloc::Model relocModel;
282 llvm::CodeModel::Model codeModel;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000283
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000284 if (triple.isOSBinFormatELF())
285 {
286 relocModel = llvm::Reloc::Static;
287 // This will be small for 32-bit and large for 64-bit.
288 codeModel = llvm::CodeModel::JITDefault;
289 }
290 else
291 {
292 relocModel = llvm::Reloc::PIC_;
293 codeModel = llvm::CodeModel::Small;
294 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000295
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000296 m_module_ap->getContext().setInlineAsmDiagnosticHandler(ReportInlineAsmError, &error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000297
Rafael Espindola7c03d952014-08-19 04:27:03 +0000298 llvm::EngineBuilder builder(std::move(m_module_ap));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000299
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000300 builder.setEngineKind(llvm::EngineKind::JIT)
301 .setErrorStr(&error_string)
302 .setRelocationModel(relocModel)
Todd Fialad5635cd2014-09-24 15:55:47 +0000303 .setMCJITMemoryManager(new MemoryManager(*this))
David Majnemer8faf9372014-09-16 06:34:29 +0000304 .setCodeModel(codeModel)
Reid Kleckner54209532014-09-03 00:40:36 +0000305 .setOptLevel(llvm::CodeGenOpt::Less);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000306
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000307 llvm::StringRef mArch;
308 llvm::StringRef mCPU;
309 llvm::SmallVector<std::string, 0> mAttrs;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000310
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000311 for (std::string &feature : m_cpu_features)
312 mAttrs.push_back(feature);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000313
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000314 llvm::TargetMachine *target_machine = builder.selectTarget(triple,
315 mArch,
316 mCPU,
317 mAttrs);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000318
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000319 m_execution_engine_ap.reset(builder.create(target_machine));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000320
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000321 if (!m_execution_engine_ap.get())
322 {
323 error.SetErrorToGenericError();
324 error.SetErrorStringWithFormat("Couldn't JIT the function: %s", error_string.c_str());
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000325 return;
326 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000327
Greg Clayton23f8c952014-03-24 23:10:19 +0000328 // Make sure we see all sections, including ones that don't have relocations...
329 m_execution_engine_ap->setProcessAllSections(true);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000330
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000331 m_execution_engine_ap->DisableLazyCompilation();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000332
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000333 // We don't actually need the function pointer here, this just forces it to get resolved.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000334
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000335 void *fun_ptr = m_execution_engine_ap->getPointerToFunction(function);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000337 if (!error.Success())
338 {
339 // We got an error through our callback!
340 return;
341 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000342
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000343 if (!function)
344 {
345 error.SetErrorToGenericError();
346 error.SetErrorStringWithFormat("Couldn't find '%s' in the JITted module", m_name.AsCString());
347 return;
348 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000349
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000350 if (!fun_ptr)
351 {
352 error.SetErrorToGenericError();
353 error.SetErrorStringWithFormat("'%s' was in the JITted module but wasn't lowered", m_name.AsCString());
354 return;
355 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000356
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000357 m_jitted_functions.push_back (JittedFunction(m_name.AsCString(), (lldb::addr_t)fun_ptr));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000358
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000359 CommitAllocations(process_sp);
360 ReportAllocations(*m_execution_engine_ap);
361 WriteData(process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000362
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000363 for (JittedFunction &jitted_function : m_jitted_functions)
364 {
365 jitted_function.m_remote_addr = GetRemoteAddressForLocal (jitted_function.m_local_addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000366
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000367 if (!jitted_function.m_name.compare(m_name.AsCString()))
368 {
369 AddrRange func_range = GetRemoteRangeForLocal(jitted_function.m_local_addr);
370 m_function_end_load_addr = func_range.first + func_range.second;
371 m_function_load_addr = jitted_function.m_remote_addr;
372 }
373 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000374
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000375 if (log)
376 {
377 log->Printf("Code can be run in the target.");
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000378
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000379 StreamString disassembly_stream;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000380
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000381 Error err = DisassembleFunction(disassembly_stream, process_sp);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000382
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000383 if (!err.Success())
384 {
385 log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error"));
386 }
387 else
388 {
389 log->Printf("Function disassembly:\n%s", disassembly_stream.GetData());
390 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000391
Sean Callanan4f2c1982014-01-21 00:54:48 +0000392 log->Printf("Sections: ");
393 for (AllocationRecord &record : m_records)
394 {
395 if (record.m_process_address != LLDB_INVALID_ADDRESS)
396 {
397 record.dump(log);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000398
Sean Callanan4f2c1982014-01-21 00:54:48 +0000399 DataBufferHeap my_buffer(record.m_size, 0);
400 Error err;
401 ReadMemory(my_buffer.GetBytes(), record.m_process_address, record.m_size, err);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000402
Sean Callanan4f2c1982014-01-21 00:54:48 +0000403 if (err.Success())
404 {
405 DataExtractor my_extractor(my_buffer.GetBytes(), my_buffer.GetByteSize(), lldb::eByteOrderBig, 8);
406 my_extractor.PutToLog(log, 0, my_buffer.GetByteSize(), record.m_process_address, 16, DataExtractor::TypeUInt8);
407 }
408 }
409 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000410 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000411
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000412 func_addr = m_function_load_addr;
413 func_end = m_function_end_load_addr;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000414
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000415 return;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000416}
417
418IRExecutionUnit::~IRExecutionUnit ()
419{
Sean Callanan14b1bae2013-04-16 23:25:35 +0000420 m_module_ap.reset();
421 m_execution_engine_ap.reset();
422 m_context_ap.reset();
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000423}
424
425IRExecutionUnit::MemoryManager::MemoryManager (IRExecutionUnit &parent) :
Todd Fialad5635cd2014-09-24 15:55:47 +0000426 m_default_mm_ap (new llvm::SectionMemoryManager()),
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000427 m_parent (parent)
428{
429}
430
Greg Clayton23f8c952014-03-24 23:10:19 +0000431IRExecutionUnit::MemoryManager::~MemoryManager ()
432{
433}
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000434
Greg Clayton23f8c952014-03-24 23:10:19 +0000435lldb::SectionType
436IRExecutionUnit::GetSectionTypeFromSectionName (const llvm::StringRef &name, IRExecutionUnit::AllocationKind alloc_kind)
437{
438 lldb::SectionType sect_type = lldb::eSectionTypeCode;
439 switch (alloc_kind)
440 {
441 case AllocationKind::Stub: sect_type = lldb::eSectionTypeCode; break;
442 case AllocationKind::Code: sect_type = lldb::eSectionTypeCode; break;
443 case AllocationKind::Data: sect_type = lldb::eSectionTypeData; break;
444 case AllocationKind::Global:sect_type = lldb::eSectionTypeData; break;
445 case AllocationKind::Bytes: sect_type = lldb::eSectionTypeOther; break;
446 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000447
Greg Clayton23f8c952014-03-24 23:10:19 +0000448 if (!name.empty())
449 {
450 if (name.equals("__text") || name.equals(".text"))
451 sect_type = lldb::eSectionTypeCode;
452 else if (name.equals("__data") || name.equals(".data"))
453 sect_type = lldb::eSectionTypeCode;
454 else if (name.startswith("__debug_") || name.startswith(".debug_"))
455 {
456 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
457 llvm::StringRef dwarf_name(name.substr(name_idx));
458 switch (dwarf_name[0])
459 {
460 case 'a':
461 if (dwarf_name.equals("abbrev"))
462 sect_type = lldb::eSectionTypeDWARFDebugAbbrev;
463 else if (dwarf_name.equals("aranges"))
464 sect_type = lldb::eSectionTypeDWARFDebugAranges;
465 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000466
Greg Clayton23f8c952014-03-24 23:10:19 +0000467 case 'f':
468 if (dwarf_name.equals("frame"))
469 sect_type = lldb::eSectionTypeDWARFDebugFrame;
470 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000471
Greg Clayton23f8c952014-03-24 23:10:19 +0000472 case 'i':
473 if (dwarf_name.equals("info"))
474 sect_type = lldb::eSectionTypeDWARFDebugInfo;
475 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000476
Greg Clayton23f8c952014-03-24 23:10:19 +0000477 case 'l':
478 if (dwarf_name.equals("line"))
479 sect_type = lldb::eSectionTypeDWARFDebugLine;
480 else if (dwarf_name.equals("loc"))
481 sect_type = lldb::eSectionTypeDWARFDebugLoc;
482 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000483
Greg Clayton23f8c952014-03-24 23:10:19 +0000484 case 'm':
485 if (dwarf_name.equals("macinfo"))
486 sect_type = lldb::eSectionTypeDWARFDebugMacInfo;
487 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000488
Greg Clayton23f8c952014-03-24 23:10:19 +0000489 case 'p':
490 if (dwarf_name.equals("pubnames"))
491 sect_type = lldb::eSectionTypeDWARFDebugPubNames;
492 else if (dwarf_name.equals("pubtypes"))
493 sect_type = lldb::eSectionTypeDWARFDebugPubTypes;
494 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000495
Greg Clayton23f8c952014-03-24 23:10:19 +0000496 case 's':
497 if (dwarf_name.equals("str"))
498 sect_type = lldb::eSectionTypeDWARFDebugStr;
499 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000500
Greg Clayton23f8c952014-03-24 23:10:19 +0000501 case 'r':
502 if (dwarf_name.equals("ranges"))
503 sect_type = lldb::eSectionTypeDWARFDebugRanges;
504 break;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000505
Greg Clayton23f8c952014-03-24 23:10:19 +0000506 default:
507 break;
508 }
509 }
510 else if (name.startswith("__apple_") || name.startswith(".apple_"))
511 {
512#if 0
513 const uint32_t name_idx = name[0] == '_' ? 8 : 7;
514 llvm::StringRef apple_name(name.substr(name_idx));
515 switch (apple_name[0])
516 {
517 case 'n':
518 if (apple_name.equals("names"))
519 sect_type = lldb::eSectionTypeDWARFAppleNames;
520 else if (apple_name.equals("namespac") || apple_name.equals("namespaces"))
521 sect_type = lldb::eSectionTypeDWARFAppleNamespaces;
522 break;
523 case 't':
524 if (apple_name.equals("types"))
525 sect_type = lldb::eSectionTypeDWARFAppleTypes;
526 break;
527 case 'o':
528 if (apple_name.equals("objc"))
529 sect_type = lldb::eSectionTypeDWARFAppleObjC;
530 break;
531 default:
532 break;
533 }
534#else
535 sect_type = lldb::eSectionTypeInvalid;
536#endif
537 }
538 else if (name.equals("__objc_imageinfo"))
539 sect_type = lldb::eSectionTypeOther;
540 }
541 return sect_type;
542}
543
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000544uint8_t *
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000545IRExecutionUnit::MemoryManager::allocateCodeSection(uintptr_t Size,
546 unsigned Alignment,
Filip Pizlobfcff682013-10-02 01:43:46 +0000547 unsigned SectionID,
548 llvm::StringRef SectionName)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000549{
Greg Clayton5160ce52013-03-27 23:08:40 +0000550 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000551
Filip Pizlobfcff682013-10-02 01:43:46 +0000552 uint8_t *return_value = m_default_mm_ap->allocateCodeSection(Size, Alignment, SectionID, SectionName);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000553
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000554 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Sean Callananec1c0b32013-04-09 01:13:08 +0000555 lldb::ePermissionsReadable | lldb::ePermissionsExecutable,
Greg Clayton23f8c952014-03-24 23:10:19 +0000556 GetSectionTypeFromSectionName (SectionName, AllocationKind::Code),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000557 Size,
558 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000559 SectionID,
560 SectionName.str().c_str()));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000561
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000562 if (log)
563 {
564 log->Printf("IRExecutionUnit::allocateCodeSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
565 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000566 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000567
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000568 return return_value;
569}
570
571uint8_t *
572IRExecutionUnit::MemoryManager::allocateDataSection(uintptr_t Size,
573 unsigned Alignment,
574 unsigned SectionID,
Filip Pizlobfcff682013-10-02 01:43:46 +0000575 llvm::StringRef SectionName,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000576 bool IsReadOnly)
577{
Greg Clayton5160ce52013-03-27 23:08:40 +0000578 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000579
Filip Pizlobfcff682013-10-02 01:43:46 +0000580 uint8_t *return_value = m_default_mm_ap->allocateDataSection(Size, Alignment, SectionID, SectionName, IsReadOnly);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000581
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000582 m_parent.m_records.push_back(AllocationRecord((uintptr_t)return_value,
Greg Clayton23f8c952014-03-24 23:10:19 +0000583 lldb::ePermissionsReadable | (IsReadOnly ? 0 : lldb::ePermissionsWritable),
584 GetSectionTypeFromSectionName (SectionName, AllocationKind::Data),
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000585 Size,
586 Alignment,
Greg Clayton23f8c952014-03-24 23:10:19 +0000587 SectionID,
588 SectionName.str().c_str()));
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000589 if (log)
590 {
591 log->Printf("IRExecutionUnit::allocateDataSection(Size=0x%" PRIx64 ", Alignment=%u, SectionID=%u) = %p",
592 (uint64_t)Size, Alignment, SectionID, return_value);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000593 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000594
595 return return_value;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000596}
597
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000598lldb::addr_t
599IRExecutionUnit::GetRemoteAddressForLocal (lldb::addr_t local_address)
600{
Sean Callananffae9442013-06-27 01:42:47 +0000601 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
602
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000603 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000604 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000605 if (local_address >= record.m_host_address &&
606 local_address < record.m_host_address + record.m_size)
607 {
608 if (record.m_process_address == LLDB_INVALID_ADDRESS)
609 return LLDB_INVALID_ADDRESS;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000610
Sean Callananffae9442013-06-27 01:42:47 +0000611 lldb::addr_t ret = record.m_process_address + (local_address - record.m_host_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000612
Sean Callananffae9442013-06-27 01:42:47 +0000613 if (log)
614 {
615 log->Printf("IRExecutionUnit::GetRemoteAddressForLocal() found 0x%" PRIx64 " in [0x%" PRIx64 "..0x%" PRIx64 "], and returned 0x%" PRIx64 " from [0x%" PRIx64 "..0x%" PRIx64 "].",
616 local_address,
Michael Sartain89c862f2013-08-07 19:05:15 +0000617 (uint64_t)record.m_host_address,
618 (uint64_t)record.m_host_address + (uint64_t)record.m_size,
Sean Callananffae9442013-06-27 01:42:47 +0000619 ret,
620 record.m_process_address,
621 record.m_process_address + record.m_size);
622 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000623
Sean Callananffae9442013-06-27 01:42:47 +0000624 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000625 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000626 }
627
628 return LLDB_INVALID_ADDRESS;
629}
630
631IRExecutionUnit::AddrRange
632IRExecutionUnit::GetRemoteRangeForLocal (lldb::addr_t local_address)
633{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000634 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000635 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000636 if (local_address >= record.m_host_address &&
637 local_address < record.m_host_address + record.m_size)
638 {
639 if (record.m_process_address == LLDB_INVALID_ADDRESS)
640 return AddrRange(0, 0);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000641
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000642 return AddrRange(record.m_process_address, record.m_size);
643 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000644 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000645
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000646 return AddrRange (0, 0);
647}
648
649bool
650IRExecutionUnit::CommitAllocations (lldb::ProcessSP &process_sp)
651{
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000652 bool ret = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000653
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000654 lldb_private::Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000655
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000656 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000657 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000658 if (record.m_process_address != LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000659 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000660
Greg Clayton23f8c952014-03-24 23:10:19 +0000661 switch (record.m_sect_type)
662 {
663 case lldb::eSectionTypeInvalid:
664 case lldb::eSectionTypeDWARFDebugAbbrev:
665 case lldb::eSectionTypeDWARFDebugAranges:
666 case lldb::eSectionTypeDWARFDebugFrame:
667 case lldb::eSectionTypeDWARFDebugInfo:
668 case lldb::eSectionTypeDWARFDebugLine:
669 case lldb::eSectionTypeDWARFDebugLoc:
670 case lldb::eSectionTypeDWARFDebugMacInfo:
671 case lldb::eSectionTypeDWARFDebugPubNames:
672 case lldb::eSectionTypeDWARFDebugPubTypes:
673 case lldb::eSectionTypeDWARFDebugRanges:
674 case lldb::eSectionTypeDWARFDebugStr:
675 case lldb::eSectionTypeDWARFAppleNames:
676 case lldb::eSectionTypeDWARFAppleTypes:
677 case lldb::eSectionTypeDWARFAppleNamespaces:
678 case lldb::eSectionTypeDWARFAppleObjC:
679 err.Clear();
680 break;
681 default:
682 record.m_process_address = Malloc (record.m_size,
683 record.m_alignment,
684 record.m_permissions,
685 eAllocationPolicyProcessOnly,
686 err);
687 break;
688 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000689
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000690 if (!err.Success())
691 {
692 ret = false;
693 break;
694 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000695 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000696
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000697 if (!ret)
698 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000699 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000700 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000701 if (record.m_process_address != LLDB_INVALID_ADDRESS)
702 {
703 Free(record.m_process_address, err);
704 record.m_process_address = LLDB_INVALID_ADDRESS;
705 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000706 }
707 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000708
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000709 return ret;
710}
711
712void
713IRExecutionUnit::ReportAllocations (llvm::ExecutionEngine &engine)
714{
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000715 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000716 {
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000717 if (record.m_process_address == LLDB_INVALID_ADDRESS)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000718 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000719
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000720 if (record.m_section_id == eSectionIDInvalid)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000721 continue;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000722
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000723 engine.mapSectionAddress((void*)record.m_host_address, record.m_process_address);
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000724 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000725
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000726 // Trigger re-application of relocations.
727 engine.finalizeObject();
728}
729
730bool
731IRExecutionUnit::WriteData (lldb::ProcessSP &process_sp)
732{
Greg Clayton23f8c952014-03-24 23:10:19 +0000733 bool wrote_something = false;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000734 for (AllocationRecord &record : m_records)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000735 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000736 if (record.m_process_address != LLDB_INVALID_ADDRESS)
737 {
738 lldb_private::Error err;
739 WriteMemory (record.m_process_address, (uint8_t*)record.m_host_address, record.m_size, err);
740 if (err.Success())
741 wrote_something = true;
742 }
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000743 }
Greg Clayton23f8c952014-03-24 23:10:19 +0000744 return wrote_something;
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000745}
746
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000747void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000748IRExecutionUnit::AllocationRecord::dump (Log *log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000749{
750 if (!log)
751 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000752
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000753 log->Printf("[0x%llx+0x%llx]->0x%llx (alignment %d, section ID %d)",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000754 (unsigned long long)m_host_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000755 (unsigned long long)m_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000756 (unsigned long long)m_process_address,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000757 (unsigned)m_alignment,
758 (unsigned)m_section_id);
759}
Greg Clayton23f8c952014-03-24 23:10:19 +0000760
761
762lldb::ByteOrder
763IRExecutionUnit::GetByteOrder () const
764{
765 ExecutionContext exe_ctx (GetBestExecutionContextScope());
766 return exe_ctx.GetByteOrder();
767}
768
769uint32_t
770IRExecutionUnit::GetAddressByteSize () const
771{
772 ExecutionContext exe_ctx (GetBestExecutionContextScope());
773 return exe_ctx.GetAddressByteSize();
774}
775
776void
777IRExecutionUnit::PopulateSymtab (lldb_private::ObjectFile *obj_file,
778 lldb_private::Symtab &symtab)
779{
780 // No symbols yet...
781}
782
783
784void
785IRExecutionUnit::PopulateSectionList (lldb_private::ObjectFile *obj_file,
786 lldb_private::SectionList &section_list)
787{
788 for (AllocationRecord &record : m_records)
789 {
790 if (record.m_size > 0)
791 {
792 lldb::SectionSP section_sp (new lldb_private::Section (obj_file->GetModule(),
793 obj_file,
794 record.m_section_id,
795 ConstString(record.m_name),
796 record.m_sect_type,
797 record.m_process_address,
798 record.m_size,
799 record.m_host_address, // file_offset (which is the host address for the data)
800 record.m_size, // file_size
Greg Clayton48672af2014-06-24 22:22:43 +0000801 0,
Greg Clayton23f8c952014-03-24 23:10:19 +0000802 record.m_permissions)); // flags
803 section_list.AddSection (section_sp);
804 }
805 }
806}
807
808bool
809IRExecutionUnit::GetArchitecture (lldb_private::ArchSpec &arch)
810{
811 ExecutionContext exe_ctx (GetBestExecutionContextScope());
812 Target *target = exe_ctx.GetTargetPtr();
813 if (target)
814 arch = target->GetArchitecture();
815 else
816 arch.Clear();
817 return arch.IsValid();
818}
819
820lldb::ModuleSP
821IRExecutionUnit::GetJITModule ()
822{
823 ExecutionContext exe_ctx (GetBestExecutionContextScope());
824 Target *target = exe_ctx.GetTargetPtr();
825 if (target)
826 {
827 lldb::ModuleSP jit_module_sp = lldb_private::Module::CreateJITModule (std::static_pointer_cast<lldb_private::ObjectFileJITDelegate>(shared_from_this()));
828 if (jit_module_sp)
829 {
830 bool changed = false;
831 jit_module_sp->SetLoadAddress(*target, 0, true, changed);
832 }
833 return jit_module_sp;
834 }
835 return lldb::ModuleSP();
836}