1 //===-- IRExecutionUnit.h ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_IRExecutionUnit_h_
11 #define lldb_IRExecutionUnit_h_
20 // Other libraries and framework includes
21 #include "llvm/IR/Module.h"
24 #include "lldb/lldb-forward.h"
25 #include "lldb/lldb-private.h"
26 #include "lldb/Core/ClangForward.h"
27 #include "lldb/Core/DataBufferHeap.h"
28 #include "llvm/ExecutionEngine/JITMemoryManager.h"
29 #include "lldb/Expression/ClangExpression.h"
30 #include "lldb/Expression/ClangExpressionParser.h"
31 #include "lldb/Expression/IRMemoryMap.h"
32 #include "lldb/Host/Mutex.h"
37 class ExecutionEngine;
41 namespace lldb_private {
45 //----------------------------------------------------------------------
46 /// @class IRExecutionUnit IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h"
47 /// @brief Contains the IR and, optionally, JIT-compiled code for a module.
49 /// This class encapsulates the compiled version of an expression, in IR
50 /// form (for interpretation purposes) and in raw machine code form (for
51 /// execution in the target).
53 /// This object wraps an IR module that comes from the expression parser,
54 /// and knows how to use the JIT to make it into executable code. It can
55 /// then be used as input to the IR interpreter, or the address of the
56 /// executable code can be passed to a thread plan to run in the target.
58 /// This class creates a subclass of LLVM's JITMemoryManager, because that is
59 /// how the JIT emits code. Because LLDB needs to move JIT-compiled code
60 /// into the target process, the IRExecutionUnit knows how to copy the
61 /// emitted code into the target process.
62 //----------------------------------------------------------------------
63 class IRExecutionUnit : public IRMemoryMap
66 //------------------------------------------------------------------
68 //------------------------------------------------------------------
69 IRExecutionUnit (std::unique_ptr<llvm::LLVMContext> &context_ap,
70 std::unique_ptr<llvm::Module> &module_ap,
72 const lldb::TargetSP &target_sp,
73 std::vector<std::string> &cpu_features);
75 //------------------------------------------------------------------
77 //------------------------------------------------------------------
80 llvm::Module *GetModule()
85 llvm::Function *GetFunction()
88 return m_module->getFunction (m_name.AsCString());
93 void GetRunnableInfo(Error &error,
94 lldb::addr_t &func_addr,
95 lldb::addr_t &func_end);
97 //------------------------------------------------------------------
98 /// Accessors for IRForTarget and other clients that may want binary
99 /// data placed on their behalf. The binary data is owned by the
100 /// IRExecutionUnit unless the client explicitly chooses to free it.
101 //------------------------------------------------------------------
103 lldb::addr_t WriteNow(const uint8_t *bytes,
107 void FreeNow(lldb::addr_t allocation);
110 //------------------------------------------------------------------
111 /// Look up the object in m_address_map that contains a given address,
112 /// find where it was copied to, and return the remote address at the
113 /// same offset into the copied entity
115 /// @param[in] local_address
116 /// The address in the debugger.
119 /// The address in the target process.
120 //------------------------------------------------------------------
122 GetRemoteAddressForLocal (lldb::addr_t local_address);
124 //------------------------------------------------------------------
125 /// Look up the object in m_address_map that contains a given address,
126 /// find where it was copied to, and return its address range in the
129 /// @param[in] local_address
130 /// The address in the debugger.
133 /// The range of the containing object in the target process.
134 //------------------------------------------------------------------
135 typedef std::pair <lldb::addr_t, uintptr_t> AddrRange;
137 GetRemoteRangeForLocal (lldb::addr_t local_address);
139 //------------------------------------------------------------------
140 /// Commit all allocations to the process and record where they were stored.
142 /// @param[in] process
143 /// The process to allocate memory in.
146 /// True <=> all allocations were performed successfully.
147 /// This method will attempt to free allocated memory if the
149 //------------------------------------------------------------------
151 CommitAllocations (lldb::ProcessSP &process_sp);
153 //------------------------------------------------------------------
154 /// Report all committed allocations to the execution engine.
156 /// @param[in] engine
157 /// The execution engine to notify.
158 //------------------------------------------------------------------
160 ReportAllocations (llvm::ExecutionEngine &engine);
162 //------------------------------------------------------------------
163 /// Write the contents of all allocations to the process.
165 /// @param[in] local_address
166 /// The process containing the allocations.
169 /// True <=> all allocations were performed successfully.
170 //------------------------------------------------------------------
172 WriteData (lldb::ProcessSP &process_sp);
175 DisassembleFunction (Stream &stream,
176 lldb::ProcessSP &process_sp);
178 class MemoryManager : public llvm::JITMemoryManager
181 MemoryManager (IRExecutionUnit &parent);
183 //------------------------------------------------------------------
184 /// Passthrough interface stub
185 //------------------------------------------------------------------
186 virtual void setMemoryWritable ();
188 //------------------------------------------------------------------
189 /// Passthrough interface stub
190 //------------------------------------------------------------------
191 virtual void setMemoryExecutable ();
193 //------------------------------------------------------------------
194 /// Passthrough interface stub
195 //------------------------------------------------------------------
196 virtual void setPoisonMemory (bool poison)
198 m_default_mm_ap->setPoisonMemory (poison);
201 //------------------------------------------------------------------
202 /// Passthrough interface stub
203 //------------------------------------------------------------------
204 virtual void AllocateGOT()
206 m_default_mm_ap->AllocateGOT();
209 //------------------------------------------------------------------
210 /// Passthrough interface stub
211 //------------------------------------------------------------------
212 virtual uint8_t *getGOTBase() const
214 return m_default_mm_ap->getGOTBase();
217 //------------------------------------------------------------------
218 /// Passthrough interface stub
219 //------------------------------------------------------------------
220 virtual uint8_t *startFunctionBody(const llvm::Function *F,
221 uintptr_t &ActualSize);
223 //------------------------------------------------------------------
224 /// Allocate room for a dyld stub for a lazy-referenced function,
225 /// and add it to the m_stubs map
228 /// The function being referenced.
230 /// @param[in] StubSize
231 /// The size of the stub.
233 /// @param[in] Alignment
234 /// The required alignment of the stub.
237 /// Allocated space for the stub.
238 //------------------------------------------------------------------
239 virtual uint8_t *allocateStub(const llvm::GlobalValue* F,
243 //------------------------------------------------------------------
244 /// Complete the body of a function, and add it to the m_functions map
247 /// The function being completed.
249 /// @param[in] FunctionStart
250 /// The first instruction of the function.
252 /// @param[in] FunctionEnd
253 /// The last byte of the last instruction of the function.
254 //------------------------------------------------------------------
255 virtual void endFunctionBody(const llvm::Function *F,
256 uint8_t *FunctionStart,
257 uint8_t *FunctionEnd);
258 //------------------------------------------------------------------
259 /// Allocate space for an unspecified purpose, and add it to the
260 /// m_spaceBlocks map
263 /// The size of the area.
265 /// @param[in] Alignment
266 /// The required alignment of the area.
270 //------------------------------------------------------------------
271 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment);
273 //------------------------------------------------------------------
274 /// Allocate space for executable code, and add it to the
275 /// m_spaceBlocks map
278 /// The size of the area.
280 /// @param[in] Alignment
281 /// The required alignment of the area.
283 /// @param[in] SectionID
284 /// A unique identifier for the section.
288 //------------------------------------------------------------------
289 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
291 llvm::StringRef SectionName);
293 //------------------------------------------------------------------
294 /// Allocate space for data, and add it to the m_spaceBlocks map
297 /// The size of the area.
299 /// @param[in] Alignment
300 /// The required alignment of the area.
302 /// @param[in] SectionID
303 /// A unique identifier for the section.
305 /// @param[in] IsReadOnly
306 /// Flag indicating the section is read-only.
310 //------------------------------------------------------------------
311 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
313 llvm::StringRef SectionName,
316 //------------------------------------------------------------------
317 /// Allocate space for a global variable, and add it to the
318 /// m_spaceBlocks map
321 /// The size of the variable.
323 /// @param[in] Alignment
324 /// The required alignment of the variable.
327 /// Allocated space for the global.
328 //------------------------------------------------------------------
329 virtual uint8_t *allocateGlobal(uintptr_t Size,
332 //------------------------------------------------------------------
333 /// Called when object loading is complete and section page
334 /// permissions can be applied. Currently unimplemented for LLDB.
336 /// @param[out] ErrMsg
337 /// The error that prevented the page protection from succeeding.
340 /// True in case of failure, false in case of success.
341 //------------------------------------------------------------------
342 virtual bool finalizeMemory(std::string *ErrMsg) {
343 // TODO: Ensure that the instruction cache is flushed because
344 // relocations are updated by dy-load. See:
345 // sys::Memory::InvalidateInstructionCache
346 // llvm::SectionMemoryManager
350 //------------------------------------------------------------------
351 /// Passthrough interface stub
352 //------------------------------------------------------------------
353 virtual void deallocateFunctionBody(void *Body);
355 //------------------------------------------------------------------
356 /// Passthrough interface stub
357 //------------------------------------------------------------------
358 virtual size_t GetDefaultCodeSlabSize() {
359 return m_default_mm_ap->GetDefaultCodeSlabSize();
362 //------------------------------------------------------------------
363 /// Passthrough interface stub
364 //------------------------------------------------------------------
365 virtual size_t GetDefaultDataSlabSize() {
366 return m_default_mm_ap->GetDefaultDataSlabSize();
369 virtual size_t GetDefaultStubSlabSize() {
370 return m_default_mm_ap->GetDefaultStubSlabSize();
373 //------------------------------------------------------------------
374 /// Passthrough interface stub
375 //------------------------------------------------------------------
376 virtual unsigned GetNumCodeSlabs() {
377 return m_default_mm_ap->GetNumCodeSlabs();
380 //------------------------------------------------------------------
381 /// Passthrough interface stub
382 //------------------------------------------------------------------
383 virtual unsigned GetNumDataSlabs() {
384 return m_default_mm_ap->GetNumDataSlabs();
387 //------------------------------------------------------------------
388 /// Passthrough interface stub
389 //------------------------------------------------------------------
390 virtual unsigned GetNumStubSlabs() {
391 return m_default_mm_ap->GetNumStubSlabs();
394 virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
395 return m_default_mm_ap->registerEHFrames(Addr, LoadAddr, Size);
398 //------------------------------------------------------------------
399 /// Passthrough interface stub
400 //------------------------------------------------------------------
401 virtual void *getPointerToNamedFunction(const std::string &Name,
402 bool AbortOnFailure = true) {
403 return m_default_mm_ap->getPointerToNamedFunction(Name, AbortOnFailure);
406 std::unique_ptr<JITMemoryManager> m_default_mm_ap; ///< The memory allocator to use in actually creating space. All calls are passed through to it.
407 IRExecutionUnit &m_parent; ///< The execution unit this is a proxy for.
410 //----------------------------------------------------------------------
411 /// @class JittedFunction IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h"
412 /// @brief Encapsulates a single function that has been generated by the JIT.
414 /// Functions that have been generated by the JIT are first resident in the
415 /// local process, and then placed in the target process. JittedFunction
416 /// represents a function possibly resident in both.
417 //----------------------------------------------------------------------
418 struct JittedFunction {
419 std::string m_name; ///< The function's name
420 lldb::addr_t m_local_addr; ///< The address of the function in LLDB's memory
421 lldb::addr_t m_remote_addr; ///< The address of the function in the target's memory
423 //------------------------------------------------------------------
426 /// Initializes class variabes.
429 /// The name of the function.
431 /// @param[in] local_addr
432 /// The address of the function in LLDB, or LLDB_INVALID_ADDRESS if
433 /// it is not present in LLDB's memory.
435 /// @param[in] remote_addr
436 /// The address of the function in the target, or LLDB_INVALID_ADDRESS
437 /// if it is not present in the target's memory.
438 //------------------------------------------------------------------
439 JittedFunction (const char *name,
440 lldb::addr_t local_addr = LLDB_INVALID_ADDRESS,
441 lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS) :
443 m_local_addr (local_addr),
444 m_remote_addr (remote_addr)
449 static const unsigned eSectionIDInvalid = (unsigned)-1;
451 //----------------------------------------------------------------------
452 /// @class AllocationRecord IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h"
453 /// @brief Enacpsulates a single allocation request made by the JIT.
455 /// Allocations made by the JIT are first queued up and then applied in
456 /// bulk to the underlying process.
457 //----------------------------------------------------------------------
458 struct AllocationRecord {
459 lldb::addr_t m_process_address;
460 uintptr_t m_host_address;
461 uint32_t m_permissions;
463 unsigned m_alignment;
464 unsigned m_section_id;
466 AllocationRecord (uintptr_t host_address,
467 uint32_t permissions,
470 unsigned section_id = eSectionIDInvalid) :
471 m_process_address(LLDB_INVALID_ADDRESS),
472 m_host_address(host_address),
473 m_permissions(permissions),
475 m_alignment(alignment),
476 m_section_id(section_id)
480 void dump (Log *log);
483 typedef std::vector<AllocationRecord> RecordVector;
484 RecordVector m_records;
486 std::unique_ptr<llvm::LLVMContext> m_context_ap;
487 std::unique_ptr<llvm::ExecutionEngine> m_execution_engine_ap;
488 std::unique_ptr<llvm::Module> m_module_ap; ///< Holder for the module until it's been handed off
489 llvm::Module *m_module; ///< Owned by the execution engine
490 std::vector<std::string> m_cpu_features;
491 llvm::SmallVector<JittedFunction, 1> m_jitted_functions; ///< A vector of all functions that have been JITted into machine code
492 const ConstString m_name;
494 std::atomic<bool> m_did_jit;
496 lldb::addr_t m_function_load_addr;
497 lldb::addr_t m_function_end_load_addr;
500 } // namespace lldb_private
502 #endif // lldb_IRExecutionUnit_h_