1 //===-- IRForTarget.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 liblldb_IRForTarget_h_ 11 #define liblldb_IRForTarget_h_ 12 13 #include "lldb/lldb-public.h" 14 #include "lldb/Core/ConstString.h" 15 #include "lldb/Core/Error.h" 16 #include "lldb/Core/Stream.h" 17 #include "lldb/Core/StreamString.h" 18 #include "lldb/Symbol/TaggedASTType.h" 19 #include "llvm/Pass.h" 20 21 #include <map> 22 23 namespace llvm { 24 class BasicBlock; 25 class CallInst; 26 class Constant; 27 class ConstantInt; 28 class Function; 29 class GlobalValue; 30 class GlobalVariable; 31 class Instruction; 32 class Module; 33 class StoreInst; 34 class DataLayout; 35 class Type; 36 class Value; 37 } 38 39 namespace lldb_private { 40 class ClangExpressionDeclMap; 41 class IRExecutionUnit; 42 class IRMemoryMap; 43 } 44 45 //---------------------------------------------------------------------- 46 /// @class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h" 47 /// @brief Transforms the IR for a function to run in the target 48 /// 49 /// Once an expression has been parsed and converted to IR, it can run 50 /// in two contexts: interpreted by LLDB as a DWARF location expression, 51 /// or compiled by the JIT and inserted into the target process for 52 /// execution. 53 /// 54 /// IRForTarget makes the second possible, by applying a series of 55 /// transformations to the IR which make it relocatable. These 56 /// transformations are discussed in more detail next to their relevant 57 /// functions. 58 //---------------------------------------------------------------------- 59 class IRForTarget : public llvm::ModulePass 60 { 61 public: 62 //------------------------------------------------------------------ 63 /// Constructor 64 /// 65 /// @param[in] decl_map 66 /// The list of externally-referenced variables for the expression, 67 /// for use in looking up globals and allocating the argument 68 /// struct. See the documentation for ClangExpressionDeclMap. 69 /// 70 /// @param[in] resolve_vars 71 /// True if the external variable references (including persistent 72 /// variables) should be resolved. If not, only external functions 73 /// are resolved. 74 /// 75 /// @param[in] execution_policy 76 /// Determines whether an IR interpreter can be used to statically 77 /// evaluate the expression. 78 /// 79 /// @param[in] const_result 80 /// This variable is populated with the statically-computed result 81 /// of the function, if it has no side-effects and the result can 82 /// be computed statically. 83 /// 84 /// @param[in] execution_unit 85 /// The holder for raw data associated with the expression. 86 /// 87 /// @param[in] error_stream 88 /// If non-NULL, a stream on which errors can be printed. 89 /// 90 /// @param[in] func_name 91 /// The name of the function to prepare for execution in the target. 92 //------------------------------------------------------------------ 93 IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, 94 bool resolve_vars, 95 lldb_private::IRExecutionUnit &execution_unit, 96 lldb_private::Stream *error_stream, 97 const char* func_name = "$__lldb_expr"); 98 99 //------------------------------------------------------------------ 100 /// Destructor 101 //------------------------------------------------------------------ 102 virtual ~IRForTarget(); 103 104 //------------------------------------------------------------------ 105 /// Run this IR transformer on a single module 106 /// 107 /// Implementation of the llvm::ModulePass::runOnModule() function. 108 /// 109 /// @param[in] llvm_module 110 /// The module to run on. This module is searched for the function 111 /// $__lldb_expr, and that function is passed to the passes one by 112 /// one. 113 /// 114 /// @param[in] interpreter_error 115 /// An error. If the expression fails to be interpreted, this error 116 /// is set to a reason why. 117 /// 118 /// @return 119 /// True on success; false otherwise 120 //------------------------------------------------------------------ 121 virtual bool 122 runOnModule (llvm::Module &llvm_module); 123 124 //------------------------------------------------------------------ 125 /// Interface stub 126 /// 127 /// Implementation of the llvm::ModulePass::assignPassManager() 128 /// function. 129 //------------------------------------------------------------------ 130 virtual void 131 assignPassManager (llvm::PMStack &pass_mgr_stack, 132 llvm::PassManagerType pass_mgr_type = llvm::PMT_ModulePassManager); 133 134 //------------------------------------------------------------------ 135 /// Returns PMT_ModulePassManager 136 /// 137 /// Implementation of the llvm::ModulePass::getPotentialPassManagerType() 138 /// function. 139 //------------------------------------------------------------------ 140 virtual llvm::PassManagerType 141 getPotentialPassManagerType() const; 142 143 private: 144 //------------------------------------------------------------------ 145 /// Ensures that the current function's linkage is set to external. 146 /// Otherwise the JIT may not return an address for it. 147 /// 148 /// @param[in] llvm_function 149 /// The function whose linkage is to be fixed. 150 /// 151 /// @return 152 /// True on success; false otherwise. 153 //------------------------------------------------------------------ 154 bool 155 FixFunctionLinkage (llvm::Function &llvm_function); 156 157 //------------------------------------------------------------------ 158 /// A module-level pass to replace all function pointers with their 159 /// integer equivalents. 160 //------------------------------------------------------------------ 161 162 //------------------------------------------------------------------ 163 /// The top-level pass implementation 164 /// 165 /// @param[in] llvm_module 166 /// The module currently being processed. 167 /// 168 /// @param[in] llvm_function 169 /// The function currently being processed. 170 /// 171 /// @return 172 /// True on success; false otherwise. 173 //------------------------------------------------------------------ 174 bool 175 HasSideEffects (llvm::Function &llvm_function); 176 177 //------------------------------------------------------------------ 178 /// A function-level pass to check whether the function has side 179 /// effects. 180 //------------------------------------------------------------------ 181 182 //------------------------------------------------------------------ 183 /// Get the address of a fuction, and a location to put the complete 184 /// Value of the function if one is available. 185 /// 186 /// @param[in] function 187 /// The function to find the location of. 188 /// 189 /// @param[out] ptr 190 /// The location of the function in the target. 191 /// 192 /// @param[out] name 193 /// The resolved name of the function (matters for intrinsics). 194 /// 195 /// @param[out] value_ptr 196 /// A variable to put the function's completed Value* in, or NULL 197 /// if the Value* shouldn't be stored anywhere. 198 /// 199 /// @return 200 /// The pointer. 201 //------------------------------------------------------------------ 202 bool 203 GetFunctionAddress (llvm::Function *function, 204 uint64_t &ptr, 205 lldb_private::ConstString &name, 206 llvm::Constant **&value_ptr); 207 208 //------------------------------------------------------------------ 209 /// Build a function pointer given a type and a raw pointer. 210 /// 211 /// @param[in] type 212 /// The type of the function pointer to be built. 213 /// 214 /// @param[in] ptr 215 /// The value of the pointer. 216 /// 217 /// @return 218 /// The pointer. 219 //------------------------------------------------------------------ 220 llvm::Constant * 221 BuildFunctionPointer (llvm::Type *type, 222 uint64_t ptr); 223 224 void 225 RegisterFunctionMetadata (llvm::LLVMContext &context, 226 llvm::Value *function_ptr, 227 const char *name); 228 229 //------------------------------------------------------------------ 230 /// The top-level pass implementation 231 /// 232 /// @param[in] llvm_function 233 /// The function currently being processed. 234 /// 235 /// @return 236 /// True if the function has side effects (or if this cannot 237 /// be determined); false otherwise. 238 //------------------------------------------------------------------ 239 bool 240 ResolveFunctionPointers (llvm::Module &llvm_module); 241 242 //------------------------------------------------------------------ 243 /// A function-level pass to take the generated global value 244 /// $__lldb_expr_result and make it into a persistent variable. 245 /// Also see ASTResultSynthesizer. 246 //------------------------------------------------------------------ 247 248 //------------------------------------------------------------------ 249 /// Find the NamedDecl corresponding to a Value. This interface is 250 /// exposed for the IR interpreter. 251 /// 252 /// @param[in] module 253 /// The module containing metadata to search 254 /// 255 /// @param[in] global 256 /// The global entity to search for 257 /// 258 /// @return 259 /// The corresponding variable declaration 260 //------------------------------------------------------------------ 261 public: 262 static clang::NamedDecl * 263 DeclForGlobal (const llvm::GlobalValue *global_val, llvm::Module *module); 264 private: 265 clang::NamedDecl * 266 DeclForGlobal (llvm::GlobalValue *global); 267 268 //------------------------------------------------------------------ 269 /// Set the constant result variable m_const_result to the provided 270 /// constant, assuming it can be evaluated. The result variable 271 /// will be reset to NULL later if the expression has side effects. 272 /// 273 /// @param[in] initializer 274 /// The constant initializer for the variable. 275 /// 276 /// @param[in] name 277 /// The name of the result variable. 278 /// 279 /// @param[in] type 280 /// The Clang type of the result variable. 281 //------------------------------------------------------------------ 282 void 283 MaybeSetConstantResult (llvm::Constant *initializer, 284 const lldb_private::ConstString &name, 285 lldb_private::TypeFromParser type); 286 287 //------------------------------------------------------------------ 288 /// If the IR represents a cast of a variable, set m_const_result 289 /// to the result of the cast. The result variable will be reset to 290 /// NULL latger if the expression has side effects. 291 /// 292 /// @param[in] type 293 /// The Clang type of the result variable. 294 //------------------------------------------------------------------ 295 void 296 MaybeSetCastResult (lldb_private::TypeFromParser type); 297 298 //------------------------------------------------------------------ 299 /// The top-level pass implementation 300 /// 301 /// @param[in] llvm_function 302 /// The function currently being processed. 303 /// 304 /// @return 305 /// True on success; false otherwise 306 //------------------------------------------------------------------ 307 bool 308 CreateResultVariable (llvm::Function &llvm_function); 309 310 //------------------------------------------------------------------ 311 /// A module-level pass to find Objective-C constant strings and 312 /// transform them to calls to CFStringCreateWithBytes. 313 //------------------------------------------------------------------ 314 315 //------------------------------------------------------------------ 316 /// Rewrite a single Objective-C constant string. 317 /// 318 /// @param[in] NSStr 319 /// The constant NSString to be transformed 320 /// 321 /// @param[in] CStr 322 /// The constant C string inside the NSString. This will be 323 /// passed as the bytes argument to CFStringCreateWithBytes. 324 /// 325 /// @return 326 /// True on success; false otherwise 327 //------------------------------------------------------------------ 328 bool 329 RewriteObjCConstString (llvm::GlobalVariable *NSStr, 330 llvm::GlobalVariable *CStr); 331 332 //------------------------------------------------------------------ 333 /// The top-level pass implementation 334 /// 335 /// @return 336 /// True on success; false otherwise 337 //------------------------------------------------------------------ 338 bool 339 RewriteObjCConstStrings (); 340 341 //------------------------------------------------------------------ 342 /// A basic block-level pass to find all Objective-C method calls and 343 /// rewrite them to use sel_registerName instead of statically allocated 344 /// selectors. The reason is that the selectors are created on the 345 /// assumption that the Objective-C runtime will scan the appropriate 346 /// section and prepare them. This doesn't happen when code is copied 347 /// into the target, though, and there's no easy way to induce the 348 /// runtime to scan them. So instead we get our selectors from 349 /// sel_registerName. 350 //------------------------------------------------------------------ 351 352 //------------------------------------------------------------------ 353 /// Replace a single selector reference 354 /// 355 /// @param[in] selector_load 356 /// The load of the statically-allocated selector. 357 /// 358 /// @return 359 /// True on success; false otherwise 360 //------------------------------------------------------------------ 361 bool 362 RewriteObjCSelector (llvm::Instruction* selector_load); 363 364 //------------------------------------------------------------------ 365 /// The top-level pass implementation 366 /// 367 /// @param[in] basic_block 368 /// The basic block currently being processed. 369 /// 370 /// @return 371 /// True on success; false otherwise 372 //------------------------------------------------------------------ 373 bool 374 RewriteObjCSelectors (llvm::BasicBlock &basic_block); 375 376 //------------------------------------------------------------------ 377 /// A basic block-level pass to find all newly-declared persistent 378 /// variables and register them with the ClangExprDeclMap. This 379 /// allows them to be materialized and dematerialized like normal 380 /// external variables. Before transformation, these persistent 381 /// variables look like normal locals, so they have an allocation. 382 /// This pass excises these allocations and makes references look 383 /// like external references where they will be resolved -- like all 384 /// other external references -- by ResolveExternals(). 385 //------------------------------------------------------------------ 386 387 //------------------------------------------------------------------ 388 /// Handle a single allocation of a persistent variable 389 /// 390 /// @param[in] persistent_alloc 391 /// The allocation of the persistent variable. 392 /// 393 /// @return 394 /// True on success; false otherwise 395 //------------------------------------------------------------------ 396 bool 397 RewritePersistentAlloc (llvm::Instruction *persistent_alloc); 398 399 //------------------------------------------------------------------ 400 /// The top-level pass implementation 401 /// 402 /// @param[in] basic_block 403 /// The basic block currently being processed. 404 //------------------------------------------------------------------ 405 bool 406 RewritePersistentAllocs (llvm::BasicBlock &basic_block); 407 408 //------------------------------------------------------------------ 409 /// A function-level pass to find all external variables and functions 410 /// used in the IR. Each found external variable is added to the 411 /// struct, and each external function is resolved in place, its call 412 /// replaced with a call to a function pointer whose value is the 413 /// address of the function in the target process. 414 //------------------------------------------------------------------ 415 416 //------------------------------------------------------------------ 417 /// Write an initializer to a memory array of assumed sufficient 418 /// size. 419 /// 420 /// @param[in] data 421 /// A pointer to the data to write to. 422 /// 423 /// @param[in] initializer 424 /// The initializer itself. 425 /// 426 /// @return 427 /// True on success; false otherwise 428 //------------------------------------------------------------------ 429 bool 430 MaterializeInitializer (uint8_t *data, llvm::Constant *initializer); 431 432 //------------------------------------------------------------------ 433 /// Move an internal variable into the static allocation section. 434 /// 435 /// @param[in] global_variable 436 /// The variable. 437 /// 438 /// @return 439 /// True on success; false otherwise 440 //------------------------------------------------------------------ 441 bool 442 MaterializeInternalVariable (llvm::GlobalVariable *global_variable); 443 444 //------------------------------------------------------------------ 445 /// Handle a single externally-defined variable 446 /// 447 /// @param[in] value 448 /// The variable. 449 /// 450 /// @return 451 /// True on success; false otherwise 452 //------------------------------------------------------------------ 453 bool 454 MaybeHandleVariable (llvm::Value *value); 455 456 //------------------------------------------------------------------ 457 /// Handle a single externally-defined symbol 458 /// 459 /// @param[in] symbol 460 /// The symbol. 461 /// 462 /// @return 463 /// True on success; false otherwise 464 //------------------------------------------------------------------ 465 bool 466 HandleSymbol (llvm::Value *symbol); 467 468 //------------------------------------------------------------------ 469 /// Handle a single externally-defined Objective-C class 470 /// 471 /// @param[in] classlist_reference 472 /// The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n" 473 /// where n (if present) is an index. 474 /// 475 /// @return 476 /// True on success; false otherwise 477 //------------------------------------------------------------------ 478 bool 479 HandleObjCClass(llvm::Value *classlist_reference); 480 481 //------------------------------------------------------------------ 482 /// Handle all the arguments to a function call 483 /// 484 /// @param[in] C 485 /// The call instruction. 486 /// 487 /// @return 488 /// True on success; false otherwise 489 //------------------------------------------------------------------ 490 bool 491 MaybeHandleCallArguments (llvm::CallInst *call_inst); 492 493 //------------------------------------------------------------------ 494 /// Resolve variable references in calls to external functions 495 /// 496 /// @param[in] basic_block 497 /// The basic block currently being processed. 498 /// 499 /// @return 500 /// True on success; false otherwise 501 //------------------------------------------------------------------ 502 bool 503 ResolveCalls (llvm::BasicBlock &basic_block); 504 505 //------------------------------------------------------------------ 506 /// Remove calls to __cxa_atexit, which should never be generated by 507 /// expressions. 508 /// 509 /// @param[in] call_inst 510 /// The call instruction. 511 /// 512 /// @return 513 /// True if the scan was successful; false if some operation 514 /// failed 515 //------------------------------------------------------------------ 516 bool 517 RemoveCXAAtExit (llvm::BasicBlock &basic_block); 518 519 //------------------------------------------------------------------ 520 /// The top-level pass implementation 521 /// 522 /// @param[in] basic_block 523 /// The function currently being processed. 524 /// 525 /// @return 526 /// True on success; false otherwise 527 //------------------------------------------------------------------ 528 bool 529 ResolveExternals (llvm::Function &llvm_function); 530 531 //------------------------------------------------------------------ 532 /// A basic block-level pass to excise guard variables from the code. 533 /// The result for the function is passed through Clang as a static 534 /// variable. Static variables normally have guard variables to 535 /// ensure that they are only initialized once. 536 //------------------------------------------------------------------ 537 538 //------------------------------------------------------------------ 539 /// Rewrite a load to a guard variable to return constant 0. 540 /// 541 /// @param[in] guard_load 542 /// The load instruction to zero out. 543 //------------------------------------------------------------------ 544 void 545 TurnGuardLoadIntoZero(llvm::Instruction* guard_load); 546 547 //------------------------------------------------------------------ 548 /// The top-level pass implementation 549 /// 550 /// @param[in] basic_block 551 /// The basic block currently being processed. 552 /// 553 /// @return 554 /// True on success; false otherwise 555 //------------------------------------------------------------------ 556 bool 557 RemoveGuards (llvm::BasicBlock &basic_block); 558 559 //------------------------------------------------------------------ 560 /// A module-level pass to allocate all string literals in a separate 561 /// allocation and redirect references to them. 562 //------------------------------------------------------------------ 563 564 //------------------------------------------------------------------ 565 /// The top-level pass implementation 566 /// 567 /// @return 568 /// True on success; false otherwise 569 //------------------------------------------------------------------ 570 bool 571 ReplaceStrings (); 572 573 //------------------------------------------------------------------ 574 /// A basick block-level pass to find all literals that will be 575 /// allocated as statics by the JIT (in contrast to the Strings, 576 /// which already are statics) and synthesize loads for them. 577 //------------------------------------------------------------------ 578 579 //------------------------------------------------------------------ 580 /// The top-level pass implementation 581 /// 582 /// @param[in] basic_block 583 /// The basic block currently being processed. 584 /// 585 /// @return 586 /// True on success; false otherwise 587 //------------------------------------------------------------------ 588 bool 589 ReplaceStaticLiterals (llvm::BasicBlock &basic_block); 590 591 //------------------------------------------------------------------ 592 /// A function-level pass to make all external variable references 593 /// point at the correct offsets from the void* passed into the 594 /// function. ClangExpressionDeclMap::DoStructLayout() must be called 595 /// beforehand, so that the offsets are valid. 596 //------------------------------------------------------------------ 597 598 //------------------------------------------------------------------ 599 /// The top-level pass implementation 600 /// 601 /// @param[in] llvm_function 602 /// The function currently being processed. 603 /// 604 /// @return 605 /// True on success; false otherwise 606 //------------------------------------------------------------------ 607 bool 608 ReplaceVariables (llvm::Function &llvm_function); 609 610 //------------------------------------------------------------------ 611 /// A module-level pass to remove all global variables from the 612 /// module since it no longer should export or import any symbols. 613 //------------------------------------------------------------------ 614 615 //------------------------------------------------------------------ 616 /// The top-level pass implementation 617 /// 618 /// @param[in] llvm_module 619 /// The module currently being processed. 620 /// 621 /// @return 622 /// True on success; false otherwise 623 //------------------------------------------------------------------ 624 bool 625 StripAllGVs (llvm::Module &llvm_module); 626 627 class StaticDataAllocator { 628 public: 629 StaticDataAllocator(lldb_private::IRExecutionUnit &execution_unit); GetStream()630 lldb_private::StreamString &GetStream() 631 { 632 return m_stream_string; 633 } 634 lldb::addr_t Allocate(); 635 private: 636 lldb_private::IRExecutionUnit &m_execution_unit; 637 lldb_private::StreamString m_stream_string; 638 lldb::addr_t m_allocation; 639 }; 640 641 /// Flags 642 bool m_resolve_vars; ///< True if external variable references and persistent variable references should be resolved 643 std::string m_func_name; ///< The name of the function to translate 644 lldb_private::ConstString m_result_name; ///< The name of the result variable ($0, $1, ...) 645 lldb_private::TypeFromParser m_result_type; ///< The type of the result variable. 646 llvm::Module *m_module; ///< The module being processed, or NULL if that has not been determined yet. 647 std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the module being processed, or NULL if there is no module. 648 lldb_private::ClangExpressionDeclMap *m_decl_map; ///< The DeclMap containing the Decls 649 StaticDataAllocator m_data_allocator; ///< The allocator to use for constant strings 650 llvm::Constant *m_CFStringCreateWithBytes; ///< The address of the function CFStringCreateWithBytes, cast to the appropriate function pointer type 651 llvm::Constant *m_sel_registerName; ///< The address of the function sel_registerName, cast to the appropriate function pointer type 652 lldb_private::Stream *m_error_stream; ///< If non-NULL, the stream on which errors should be printed 653 654 llvm::StoreInst *m_result_store; ///< If non-NULL, the store instruction that writes to the result variable. If m_has_side_effects is true, this is NULL. 655 bool m_result_is_pointer; ///< True if the function's result in the AST is a pointer (see comments in ASTResultSynthesizer::SynthesizeBodyResult) 656 657 llvm::GlobalVariable *m_reloc_placeholder; ///< A placeholder that will be replaced by a pointer to the final location of the static allocation. 658 659 //------------------------------------------------------------------ 660 /// UnfoldConstant operates on a constant [Old] which has just been 661 /// replaced with a value [New]. We assume that new_value has 662 /// been properly placed early in the function, in front of the 663 /// first instruction in the entry basic block 664 /// [FirstEntryInstruction]. 665 /// 666 /// UnfoldConstant reads through the uses of Old and replaces Old 667 /// in those uses with New. Where those uses are constants, the 668 /// function generates new instructions to compute the result of the 669 /// new, non-constant expression and places them before 670 /// FirstEntryInstruction. These instructions replace the constant 671 /// uses, so UnfoldConstant calls itself recursively for those. 672 /// 673 /// @param[in] llvm_function 674 /// The function currently being processed. 675 /// 676 /// @return 677 /// True on success; false otherwise 678 //------------------------------------------------------------------ 679 680 class FunctionValueCache { 681 public: 682 typedef std::function <llvm::Value *(llvm::Function *)> Maker; 683 684 FunctionValueCache (Maker const &maker); 685 ~FunctionValueCache (); 686 llvm::Value *GetValue (llvm::Function *function); 687 private: 688 Maker const m_maker; 689 typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap; 690 FunctionValueMap m_values; 691 }; 692 693 FunctionValueCache m_entry_instruction_finder; 694 695 static bool 696 UnfoldConstant (llvm::Constant *old_constant, 697 FunctionValueCache &value_maker, 698 FunctionValueCache &entry_instruction_finder); 699 700 //------------------------------------------------------------------ 701 /// Construct a reference to m_reloc_placeholder with a given type 702 /// and offset. This typically happens after inserting data into 703 /// m_data_allocator. 704 /// 705 /// @param[in] type 706 /// The type of the value being loaded. 707 /// 708 /// @param[in] offset 709 /// The offset of the value from the base of m_data_allocator. 710 /// 711 /// @return 712 /// The Constant for the reference, usually a ConstantExpr. 713 //------------------------------------------------------------------ 714 llvm::Constant * 715 BuildRelocation(llvm::Type *type, 716 uint64_t offset); 717 718 //------------------------------------------------------------------ 719 /// Commit the allocation in m_data_allocator and use its final 720 /// location to replace m_reloc_placeholder. 721 /// 722 /// @param[in] module 723 /// The module that m_data_allocator resides in 724 /// 725 /// @return 726 /// True on success; false otherwise 727 //------------------------------------------------------------------ 728 bool 729 CompleteDataAllocation (); 730 731 }; 732 733 #endif 734