IRExecutionUnit.h revision faafd193bf7dc065e5f6fb99e4c538cc452af7e4
1//===-- IRExecutionUnit.h ---------------------------------------*- C++ -*-===// 2// 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 10#ifndef lldb_IRExecutionUnit_h_ 11#define lldb_IRExecutionUnit_h_ 12 13// C Includes 14// C++ Includes 15#include <atomic> 16#include <string> 17#include <vector> 18#include <map> 19 20// Other libraries and framework includes 21// Project includes 22#include "lldb/lldb-forward.h" 23#include "lldb/lldb-private.h" 24#include "lldb/Core/ClangForward.h" 25#include "lldb/Core/DataBufferHeap.h" 26#include "llvm/ExecutionEngine/JITMemoryManager.h" 27#include "lldb/Expression/ClangExpression.h" 28#include "lldb/Expression/ClangExpressionParser.h" 29#include "lldb/Expression/IRMemoryMap.h" 30#include "lldb/Host/Mutex.h" 31 32namespace llvm { 33 34class Module; 35class ExecutionEngine; 36 37} 38 39namespace lldb_private { 40 41class Error; 42 43//---------------------------------------------------------------------- 44/// @class IRExecutionUnit IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h" 45/// @brief Contains the IR and, optionally, JIT-compiled code for a module. 46/// 47/// This class encapsulates the compiled version of an expression, in IR 48/// form (for interpretation purposes) and in raw machine code form (for 49/// execution in the target). 50/// 51/// This object wraps an IR module that comes from the expression parser, 52/// and knows how to use the JIT to make it into executable code. It can 53/// then be used as input to the IR interpreter, or the address of the 54/// executable code can be passed to a thread plan to run in the target. 55/// 56/// This class creates a subclass of LLVM's JITMemoryManager, because that is 57/// how the JIT emits code. Because LLDB needs to move JIT-compiled code 58/// into the target process, the IRExecutionUnit knows how to copy the 59/// emitted code into the target process. 60//---------------------------------------------------------------------- 61class IRExecutionUnit : public IRMemoryMap 62{ 63public: 64 //------------------------------------------------------------------ 65 /// Constructor 66 //------------------------------------------------------------------ 67 IRExecutionUnit (std::auto_ptr<llvm::Module> &module_ap, 68 ConstString &name, 69 const lldb::TargetSP &target_sp, 70 std::vector<std::string> &cpu_features); 71 72 //------------------------------------------------------------------ 73 /// Destructor 74 //------------------------------------------------------------------ 75 ~IRExecutionUnit(); 76 77 llvm::Module *GetModule() 78 { 79 return m_module; 80 } 81 82 void GetRunnableInfo(Error &error, 83 lldb::addr_t &func_addr, 84 lldb::addr_t &func_end); 85 86 //------------------------------------------------------------------ 87 /// Accessors for IRForTarget and other clients that may want binary 88 /// data placed on their behalf. The binary data is owned by the 89 /// IRExecutionUnit unless the client explicitly chooses to free it. 90 //------------------------------------------------------------------ 91 92 lldb::addr_t WriteNow(const uint8_t *bytes, 93 size_t size, 94 Error &error); 95 96 void FreeNow(lldb::addr_t allocation); 97 98private: 99 //------------------------------------------------------------------ 100 /// Look up the object in m_address_map that contains a given address, 101 /// find where it was copied to, and return the remote address at the 102 /// same offset into the copied entity 103 /// 104 /// @param[in] local_address 105 /// The address in the debugger. 106 /// 107 /// @return 108 /// The address in the target process. 109 //------------------------------------------------------------------ 110 lldb::addr_t 111 GetRemoteAddressForLocal (lldb::addr_t local_address); 112 113 //------------------------------------------------------------------ 114 /// Look up the object in m_address_map that contains a given address, 115 /// find where it was copied to, and return its address range in the 116 /// target process 117 /// 118 /// @param[in] local_address 119 /// The address in the debugger. 120 /// 121 /// @return 122 /// The range of the containing object in the target process. 123 //------------------------------------------------------------------ 124 typedef std::pair <lldb::addr_t, uintptr_t> AddrRange; 125 AddrRange 126 GetRemoteRangeForLocal (lldb::addr_t local_address); 127 128 //------------------------------------------------------------------ 129 /// Commit all allocations to the process and record where they were stored. 130 /// 131 /// @param[in] process 132 /// The process to allocate memory in. 133 /// 134 /// @return 135 /// True <=> all allocations were performed successfully. 136 /// This method will attempt to free allocated memory if the 137 /// operation fails. 138 //------------------------------------------------------------------ 139 bool 140 CommitAllocations (lldb::ProcessSP &process_sp); 141 142 //------------------------------------------------------------------ 143 /// Report all committed allocations to the execution engine. 144 /// 145 /// @param[in] engine 146 /// The execution engine to notify. 147 //------------------------------------------------------------------ 148 void 149 ReportAllocations (llvm::ExecutionEngine &engine); 150 151 //------------------------------------------------------------------ 152 /// Write the contents of all allocations to the process. 153 /// 154 /// @param[in] local_address 155 /// The process containing the allocations. 156 /// 157 /// @return 158 /// True <=> all allocations were performed successfully. 159 //------------------------------------------------------------------ 160 bool 161 WriteData (lldb::ProcessSP &process_sp); 162 163 Error 164 DisassembleFunction (Stream &stream, 165 lldb::ProcessSP &process_sp); 166 167 class MemoryManager : public llvm::JITMemoryManager 168 { 169 public: 170 MemoryManager (IRExecutionUnit &parent); 171 172 //------------------------------------------------------------------ 173 /// Passthrough interface stub 174 //------------------------------------------------------------------ 175 virtual void setMemoryWritable (); 176 177 //------------------------------------------------------------------ 178 /// Passthrough interface stub 179 //------------------------------------------------------------------ 180 virtual void setMemoryExecutable (); 181 182 //------------------------------------------------------------------ 183 /// Passthrough interface stub 184 //------------------------------------------------------------------ 185 virtual void setPoisonMemory (bool poison) 186 { 187 m_default_mm_ap->setPoisonMemory (poison); 188 } 189 190 //------------------------------------------------------------------ 191 /// Passthrough interface stub 192 //------------------------------------------------------------------ 193 virtual void AllocateGOT() 194 { 195 m_default_mm_ap->AllocateGOT(); 196 } 197 198 //------------------------------------------------------------------ 199 /// Passthrough interface stub 200 //------------------------------------------------------------------ 201 virtual uint8_t *getGOTBase() const 202 { 203 return m_default_mm_ap->getGOTBase(); 204 } 205 206 //------------------------------------------------------------------ 207 /// Passthrough interface stub 208 //------------------------------------------------------------------ 209 virtual uint8_t *startFunctionBody(const llvm::Function *F, 210 uintptr_t &ActualSize); 211 212 //------------------------------------------------------------------ 213 /// Allocate room for a dyld stub for a lazy-referenced function, 214 /// and add it to the m_stubs map 215 /// 216 /// @param[in] F 217 /// The function being referenced. 218 /// 219 /// @param[in] StubSize 220 /// The size of the stub. 221 /// 222 /// @param[in] Alignment 223 /// The required alignment of the stub. 224 /// 225 /// @return 226 /// Allocated space for the stub. 227 //------------------------------------------------------------------ 228 virtual uint8_t *allocateStub(const llvm::GlobalValue* F, 229 unsigned StubSize, 230 unsigned Alignment); 231 232 //------------------------------------------------------------------ 233 /// Complete the body of a function, and add it to the m_functions map 234 /// 235 /// @param[in] F 236 /// The function being completed. 237 /// 238 /// @param[in] FunctionStart 239 /// The first instruction of the function. 240 /// 241 /// @param[in] FunctionEnd 242 /// The last byte of the last instruction of the function. 243 //------------------------------------------------------------------ 244 virtual void endFunctionBody(const llvm::Function *F, 245 uint8_t *FunctionStart, 246 uint8_t *FunctionEnd); 247 //------------------------------------------------------------------ 248 /// Allocate space for an unspecified purpose, and add it to the 249 /// m_spaceBlocks map 250 /// 251 /// @param[in] Size 252 /// The size of the area. 253 /// 254 /// @param[in] Alignment 255 /// The required alignment of the area. 256 /// 257 /// @return 258 /// Allocated space. 259 //------------------------------------------------------------------ 260 virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment); 261 262 //------------------------------------------------------------------ 263 /// Allocate space for executable code, and add it to the 264 /// m_spaceBlocks map 265 /// 266 /// @param[in] Size 267 /// The size of the area. 268 /// 269 /// @param[in] Alignment 270 /// The required alignment of the area. 271 /// 272 /// @param[in] SectionID 273 /// A unique identifier for the section. 274 /// 275 /// @return 276 /// Allocated space. 277 //------------------------------------------------------------------ 278 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 279 unsigned SectionID); 280 281 //------------------------------------------------------------------ 282 /// Allocate space for data, and add it to the m_spaceBlocks map 283 /// 284 /// @param[in] Size 285 /// The size of the area. 286 /// 287 /// @param[in] Alignment 288 /// The required alignment of the area. 289 /// 290 /// @param[in] SectionID 291 /// A unique identifier for the section. 292 /// 293 /// @param[in] IsReadOnly 294 /// Flag indicating the section is read-only. 295 /// 296 /// @return 297 /// Allocated space. 298 //------------------------------------------------------------------ 299 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 300 unsigned SectionID, bool IsReadOnly); 301 302 //------------------------------------------------------------------ 303 /// Allocate space for a global variable, and add it to the 304 /// m_spaceBlocks map 305 /// 306 /// @param[in] Size 307 /// The size of the variable. 308 /// 309 /// @param[in] Alignment 310 /// The required alignment of the variable. 311 /// 312 /// @return 313 /// Allocated space for the global. 314 //------------------------------------------------------------------ 315 virtual uint8_t *allocateGlobal(uintptr_t Size, 316 unsigned Alignment); 317 318 //------------------------------------------------------------------ 319 /// Called when object loading is complete and section page 320 /// permissions can be applied. Currently unimplemented for LLDB. 321 /// 322 /// @param[out] ErrMsg 323 /// The error that prevented the page protection from succeeding. 324 /// 325 /// @return 326 /// True in case of failure, false in case of success. 327 //------------------------------------------------------------------ 328 bool applyPermissions(std::string *ErrMsg) { return false; } 329 330 //------------------------------------------------------------------ 331 /// Passthrough interface stub 332 //------------------------------------------------------------------ 333 virtual void deallocateFunctionBody(void *Body); 334 335 //------------------------------------------------------------------ 336 /// Passthrough interface stub 337 //------------------------------------------------------------------ 338 virtual uint8_t* startExceptionTable(const llvm::Function* F, 339 uintptr_t &ActualSize); 340 341 //------------------------------------------------------------------ 342 /// Complete the exception table for a function, and add it to the 343 /// m_exception_tables map 344 /// 345 /// @param[in] F 346 /// The function whose exception table is being written. 347 /// 348 /// @param[in] TableStart 349 /// The first byte of the exception table. 350 /// 351 /// @param[in] TableEnd 352 /// The last byte of the exception table. 353 /// 354 /// @param[in] FrameRegister 355 /// I don't know what this does, but it's passed through. 356 //------------------------------------------------------------------ 357 virtual void endExceptionTable(const llvm::Function *F, 358 uint8_t *TableStart, 359 uint8_t *TableEnd, 360 uint8_t* FrameRegister); 361 362 //------------------------------------------------------------------ 363 /// Passthrough interface stub 364 //------------------------------------------------------------------ 365 virtual void deallocateExceptionTable(void *ET); 366 367 //------------------------------------------------------------------ 368 /// Passthrough interface stub 369 //------------------------------------------------------------------ 370 virtual size_t GetDefaultCodeSlabSize() { 371 return m_default_mm_ap->GetDefaultCodeSlabSize(); 372 } 373 374 //------------------------------------------------------------------ 375 /// Passthrough interface stub 376 //------------------------------------------------------------------ 377 virtual size_t GetDefaultDataSlabSize() { 378 return m_default_mm_ap->GetDefaultDataSlabSize(); 379 } 380 381 virtual size_t GetDefaultStubSlabSize() { 382 return m_default_mm_ap->GetDefaultStubSlabSize(); 383 } 384 385 //------------------------------------------------------------------ 386 /// Passthrough interface stub 387 //------------------------------------------------------------------ 388 virtual unsigned GetNumCodeSlabs() { 389 return m_default_mm_ap->GetNumCodeSlabs(); 390 } 391 392 //------------------------------------------------------------------ 393 /// Passthrough interface stub 394 //------------------------------------------------------------------ 395 virtual unsigned GetNumDataSlabs() { 396 return m_default_mm_ap->GetNumDataSlabs(); 397 } 398 399 //------------------------------------------------------------------ 400 /// Passthrough interface stub 401 //------------------------------------------------------------------ 402 virtual unsigned GetNumStubSlabs() { 403 return m_default_mm_ap->GetNumStubSlabs(); 404 } 405 406 //------------------------------------------------------------------ 407 /// Passthrough interface stub 408 //------------------------------------------------------------------ 409 virtual void *getPointerToNamedFunction(const std::string &Name, 410 bool AbortOnFailure = true) { 411 return m_default_mm_ap->getPointerToNamedFunction(Name, AbortOnFailure); 412 } 413 private: 414 std::auto_ptr<JITMemoryManager> m_default_mm_ap; ///< The memory allocator to use in actually creating space. All calls are passed through to it. 415 IRExecutionUnit &m_parent; ///< The execution unit this is a proxy for. 416 }; 417 418 //---------------------------------------------------------------------- 419 /// @class JittedFunction IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h" 420 /// @brief Encapsulates a single function that has been generated by the JIT. 421 /// 422 /// Functions that have been generated by the JIT are first resident in the 423 /// local process, and then placed in the target process. JittedFunction 424 /// represents a function possibly resident in both. 425 //---------------------------------------------------------------------- 426 struct JittedFunction { 427 std::string m_name; ///< The function's name 428 lldb::addr_t m_local_addr; ///< The address of the function in LLDB's memory 429 lldb::addr_t m_remote_addr; ///< The address of the function in the target's memory 430 431 //------------------------------------------------------------------ 432 /// Constructor 433 /// 434 /// Initializes class variabes. 435 /// 436 /// @param[in] name 437 /// The name of the function. 438 /// 439 /// @param[in] local_addr 440 /// The address of the function in LLDB, or LLDB_INVALID_ADDRESS if 441 /// it is not present in LLDB's memory. 442 /// 443 /// @param[in] remote_addr 444 /// The address of the function in the target, or LLDB_INVALID_ADDRESS 445 /// if it is not present in the target's memory. 446 //------------------------------------------------------------------ 447 JittedFunction (const char *name, 448 lldb::addr_t local_addr = LLDB_INVALID_ADDRESS, 449 lldb::addr_t remote_addr = LLDB_INVALID_ADDRESS) : 450 m_name (name), 451 m_local_addr (local_addr), 452 m_remote_addr (remote_addr) 453 { 454 } 455 }; 456 457 static const unsigned eSectionIDInvalid = (unsigned)-1; 458 459 //---------------------------------------------------------------------- 460 /// @class AllocationRecord IRExecutionUnit.h "lldb/Expression/IRExecutionUnit.h" 461 /// @brief Enacpsulates a single allocation request made by the JIT. 462 /// 463 /// Allocations made by the JIT are first queued up and then applied in 464 /// bulk to the underlying process. 465 //---------------------------------------------------------------------- 466 struct AllocationRecord { 467 lldb::addr_t m_process_address; 468 uintptr_t m_host_address; 469 uint32_t m_permissions; 470 size_t m_size; 471 unsigned m_alignment; 472 unsigned m_section_id; 473 474 AllocationRecord (uintptr_t host_address, 475 uint32_t permissions, 476 size_t size, 477 unsigned alignment, 478 unsigned section_id = eSectionIDInvalid) : 479 m_process_address(LLDB_INVALID_ADDRESS), 480 m_host_address(host_address), 481 m_permissions(permissions), 482 m_size(size), 483 m_alignment(alignment), 484 m_section_id(section_id) 485 { 486 } 487 488 void dump (Log *log); 489 }; 490 491 typedef std::vector<AllocationRecord> RecordVector; 492 RecordVector m_records; 493 494 std::auto_ptr<llvm::ExecutionEngine> m_execution_engine_ap; 495 std::auto_ptr<llvm::Module> m_module_ap; ///< Holder for the module until it's been handed off 496 llvm::Module *m_module; ///< Owned by the execution engine 497 std::vector<std::string> m_cpu_features; 498 llvm::SmallVector<JittedFunction, 1> m_jitted_functions; ///< A vector of all functions that have been JITted into machine code 499 const ConstString m_name; 500 501 std::atomic<bool> m_did_jit; 502 503 lldb::addr_t m_function_load_addr; 504 lldb::addr_t m_function_end_load_addr; 505}; 506 507} // namespace lldb_private 508 509#endif // lldb_IRExecutionUnit_h_ 510