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