1//===-- IRForTarget.h ---------------------------------------------*- C++ 2//-*-===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_IRForTarget_h_ 11#define liblldb_IRForTarget_h_ 12 13#include "lldb/Core/ClangForward.h" 14#include "lldb/Symbol/TaggedASTType.h" 15#include "lldb/Utility/ConstString.h" 16#include "lldb/Utility/Status.h" 17#include "lldb/Utility/Stream.h" 18#include "lldb/Utility/StreamString.h" 19#include "lldb/lldb-public.h" 20#include "llvm/IR/DerivedTypes.h" 21#include "llvm/Pass.h" 22 23#include <functional> 24#include <map> 25 26namespace llvm { 27class BasicBlock; 28class CallInst; 29class Constant; 30class ConstantInt; 31class Function; 32class GlobalValue; 33class GlobalVariable; 34class Instruction; 35class Module; 36class StoreInst; 37class DataLayout; 38class Value; 39} 40 41namespace lldb_private { 42class ClangExpressionDeclMap; 43class IRExecutionUnit; 44class IRMemoryMap; 45} 46 47/// \class IRForTarget IRForTarget.h "lldb/Expression/IRForTarget.h" 48/// Transforms the IR for a function to run in the target 49/// 50/// Once an expression has been parsed and converted to IR, it can run in two 51/// contexts: interpreted by LLDB as a DWARF location expression, or compiled 52/// by the JIT and inserted into the target process for 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. 58class IRForTarget : public llvm::ModulePass { 59public: 60 enum class LookupResult { Success, Fail, Ignore }; 61 62 /// Constructor 63 /// 64 /// \param[in] decl_map 65 /// The list of externally-referenced variables for the expression, 66 /// for use in looking up globals and allocating the argument 67 /// struct. See the documentation for ClangExpressionDeclMap. 68 /// 69 /// \param[in] resolve_vars 70 /// True if the external variable references (including persistent 71 /// variables) should be resolved. If not, only external functions 72 /// are resolved. 73 /// 74 /// \param[in] execution_unit 75 /// The holder for raw data associated with the expression. 76 /// 77 /// \param[in] error_stream 78 /// If non-NULL, a stream on which errors can be printed. 79 /// 80 /// \param[in] func_name 81 /// The name of the function to prepare for execution in the target. 82 IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map, bool resolve_vars, 83 lldb_private::IRExecutionUnit &execution_unit, 84 lldb_private::Stream &error_stream, 85 const char *func_name = "$__lldb_expr"); 86 87 /// Destructor 88 ~IRForTarget() override; 89 90 /// Run this IR transformer on a single module 91 /// 92 /// Implementation of the llvm::ModulePass::runOnModule() function. 93 /// 94 /// \param[in] llvm_module 95 /// The module to run on. This module is searched for the function 96 /// $__lldb_expr, and that function is passed to the passes one by 97 /// one. 98 /// 99 /// \return 100 /// True on success; false otherwise 101 bool runOnModule(llvm::Module &llvm_module) override; 102 103 /// Interface stub 104 /// 105 /// Implementation of the llvm::ModulePass::assignPassManager() function. 106 void assignPassManager(llvm::PMStack &pass_mgr_stack, 107 llvm::PassManagerType pass_mgr_type = 108 llvm::PMT_ModulePassManager) override; 109 110 /// Returns PMT_ModulePassManager 111 /// 112 /// Implementation of the llvm::ModulePass::getPotentialPassManagerType() 113 /// function. 114 llvm::PassManagerType getPotentialPassManagerType() const override; 115 116private: 117 /// Ensures that the current function's linkage is set to external. 118 /// Otherwise the JIT may not return an address for it. 119 /// 120 /// \param[in] llvm_function 121 /// The function whose linkage is to be fixed. 122 /// 123 /// \return 124 /// True on success; false otherwise. 125 bool FixFunctionLinkage(llvm::Function &llvm_function); 126 127 /// A module-level pass to replace all function pointers with their 128 /// integer equivalents. 129 130 /// The top-level pass implementation 131 /// 132 /// \param[in] llvm_function 133 /// The function currently being processed. 134 /// 135 /// \return 136 /// True on success; false otherwise. 137 bool HasSideEffects(llvm::Function &llvm_function); 138 139 /// A function-level pass to check whether the function has side 140 /// effects. 141 142 /// Get the address of a function, and a location to put the complete Value 143 /// of the function if one is available. 144 /// 145 /// \param[in] function 146 /// The function to find the location of. 147 /// 148 /// \param[out] ptr 149 /// The location of the function in the target. 150 /// 151 /// \param[out] name 152 /// The resolved name of the function (matters for intrinsics). 153 /// 154 /// \param[out] value_ptr 155 /// A variable to put the function's completed Value* in, or NULL 156 /// if the Value* shouldn't be stored anywhere. 157 /// 158 /// \return 159 /// The pointer. 160 LookupResult GetFunctionAddress(llvm::Function *function, uint64_t &ptr, 161 lldb_private::ConstString &name, 162 llvm::Constant **&value_ptr); 163 164 /// A function-level pass to take the generated global value 165 /// $__lldb_expr_result and make it into a persistent variable. Also see 166 /// ASTResultSynthesizer. 167 168 /// Find the NamedDecl corresponding to a Value. This interface is exposed 169 /// for the IR interpreter. 170 /// 171 /// \param[in] global_val 172 /// The global entity to search for 173 /// 174 /// \param[in] module 175 /// The module containing metadata to search 176 /// 177 /// \return 178 /// The corresponding variable declaration 179public: 180 static clang::NamedDecl *DeclForGlobal(const llvm::GlobalValue *global_val, 181 llvm::Module *module); 182 183private: 184 clang::NamedDecl *DeclForGlobal(llvm::GlobalValue *global); 185 186 /// Set the constant result variable m_const_result to the provided 187 /// constant, assuming it can be evaluated. The result variable will be 188 /// reset to NULL later if the expression has side effects. 189 /// 190 /// \param[in] initializer 191 /// The constant initializer for the variable. 192 /// 193 /// \param[in] name 194 /// The name of the result variable. 195 /// 196 /// \param[in] type 197 /// The Clang type of the result variable. 198 void MaybeSetConstantResult(llvm::Constant *initializer, 199 lldb_private::ConstString name, 200 lldb_private::TypeFromParser type); 201 202 /// If the IR represents a cast of a variable, set m_const_result to the 203 /// result of the cast. The result variable will be reset to 204 /// NULL latger if the expression has side effects. 205 /// 206 /// \param[in] type 207 /// The Clang type of the result variable. 208 void MaybeSetCastResult(lldb_private::TypeFromParser type); 209 210 /// The top-level pass implementation 211 /// 212 /// \param[in] llvm_function 213 /// The function currently being processed. 214 /// 215 /// \return 216 /// True on success; false otherwise 217 bool CreateResultVariable(llvm::Function &llvm_function); 218 219 /// A module-level pass to find Objective-C constant strings and 220 /// transform them to calls to CFStringCreateWithBytes. 221 222 /// Rewrite a single Objective-C constant string. 223 /// 224 /// \param[in] NSStr 225 /// The constant NSString to be transformed 226 /// 227 /// \param[in] CStr 228 /// The constant C string inside the NSString. This will be 229 /// passed as the bytes argument to CFStringCreateWithBytes. 230 /// 231 /// \return 232 /// True on success; false otherwise 233 bool RewriteObjCConstString(llvm::GlobalVariable *NSStr, 234 llvm::GlobalVariable *CStr); 235 236 /// The top-level pass implementation 237 /// 238 /// \return 239 /// True on success; false otherwise 240 bool RewriteObjCConstStrings(); 241 242 /// A basic block-level pass to find all Objective-C method calls and 243 /// rewrite them to use sel_registerName instead of statically allocated 244 /// selectors. The reason is that the selectors are created on the 245 /// assumption that the Objective-C runtime will scan the appropriate 246 /// section and prepare them. This doesn't happen when code is copied into 247 /// the target, though, and there's no easy way to induce the runtime to 248 /// scan them. So instead we get our selectors from sel_registerName. 249 250 /// Replace a single selector reference 251 /// 252 /// \param[in] selector_load 253 /// The load of the statically-allocated selector. 254 /// 255 /// \return 256 /// True on success; false otherwise 257 bool RewriteObjCSelector(llvm::Instruction *selector_load); 258 259 /// The top-level pass implementation 260 /// 261 /// \param[in] basic_block 262 /// The basic block currently being processed. 263 /// 264 /// \return 265 /// True on success; false otherwise 266 bool RewriteObjCSelectors(llvm::BasicBlock &basic_block); 267 268 /// A basic block-level pass to find all Objective-C class references that 269 /// use the old-style Objective-C runtime and rewrite them to use 270 /// class_getClass instead of statically allocated class references. 271 272 /// Replace a single old-style class reference 273 /// 274 /// \param[in] class_load 275 /// The load of the statically-allocated selector. 276 /// 277 /// \return 278 /// True on success; false otherwise 279 bool RewriteObjCClassReference(llvm::Instruction *class_load); 280 281 /// The top-level pass implementation 282 /// 283 /// \param[in] basic_block 284 /// The basic block currently being processed. 285 /// 286 /// \return 287 /// True on success; false otherwise 288 bool RewriteObjCClassReferences(llvm::BasicBlock &basic_block); 289 290 /// A basic block-level pass to find all newly-declared persistent 291 /// variables and register them with the ClangExprDeclMap. This allows them 292 /// to be materialized and dematerialized like normal external variables. 293 /// Before transformation, these persistent variables look like normal 294 /// locals, so they have an allocation. This pass excises these allocations 295 /// and makes references look like external references where they will be 296 /// resolved -- like all other external references -- by ResolveExternals(). 297 298 /// Handle a single allocation of a persistent variable 299 /// 300 /// \param[in] persistent_alloc 301 /// The allocation of the persistent variable. 302 /// 303 /// \return 304 /// True on success; false otherwise 305 bool RewritePersistentAlloc(llvm::Instruction *persistent_alloc); 306 307 /// The top-level pass implementation 308 /// 309 /// \param[in] basic_block 310 /// The basic block currently being processed. 311 bool RewritePersistentAllocs(llvm::BasicBlock &basic_block); 312 313 /// A function-level pass to find all external variables and functions 314 /// used in the IR. Each found external variable is added to the struct, 315 /// and each external function is resolved in place, its call replaced with 316 /// a call to a function pointer whose value is the address of the function 317 /// in the target process. 318 319 /// Handle a single externally-defined variable 320 /// 321 /// \param[in] value 322 /// The variable. 323 /// 324 /// \return 325 /// True on success; false otherwise 326 bool MaybeHandleVariable(llvm::Value *value); 327 328 /// Handle a single externally-defined symbol 329 /// 330 /// \param[in] symbol 331 /// The symbol. 332 /// 333 /// \return 334 /// True on success; false otherwise 335 bool HandleSymbol(llvm::Value *symbol); 336 337 /// Handle a single externally-defined Objective-C class 338 /// 339 /// \param[in] classlist_reference 340 /// The reference, usually "01L_OBJC_CLASSLIST_REFERENCES_$_n" 341 /// where n (if present) is an index. 342 /// 343 /// \return 344 /// True on success; false otherwise 345 bool HandleObjCClass(llvm::Value *classlist_reference); 346 347 /// Handle all the arguments to a function call 348 /// 349 /// \param[in] call_inst 350 /// The call instruction. 351 /// 352 /// \return 353 /// True on success; false otherwise 354 bool MaybeHandleCallArguments(llvm::CallInst *call_inst); 355 356 /// Resolve variable references in calls to external functions 357 /// 358 /// \param[in] basic_block 359 /// The basic block currently being processed. 360 /// 361 /// \return 362 /// True on success; false otherwise 363 bool ResolveCalls(llvm::BasicBlock &basic_block); 364 365 /// Remove calls to __cxa_atexit, which should never be generated by 366 /// expressions. 367 /// 368 /// \param[in] basic_block 369 /// The basic block currently being processed. 370 /// 371 /// \return 372 /// True if the scan was successful; false if some operation 373 /// failed 374 bool RemoveCXAAtExit(llvm::BasicBlock &basic_block); 375 376 /// The top-level pass implementation 377 /// 378 /// \param[in] llvm_function 379 /// The function currently being processed. 380 /// 381 /// \return 382 /// True on success; false otherwise 383 bool ResolveExternals(llvm::Function &llvm_function); 384 385 /// A basic block-level pass to excise guard variables from the code. 386 /// The result for the function is passed through Clang as a static 387 /// variable. Static variables normally have guard variables to ensure that 388 /// they are only initialized once. 389 390 /// Rewrite a load to a guard variable to return constant 0. 391 /// 392 /// \param[in] guard_load 393 /// The load instruction to zero out. 394 void TurnGuardLoadIntoZero(llvm::Instruction *guard_load); 395 396 /// The top-level pass implementation 397 /// 398 /// \param[in] basic_block 399 /// The basic block currently being processed. 400 /// 401 /// \return 402 /// True on success; false otherwise 403 bool RemoveGuards(llvm::BasicBlock &basic_block); 404 405 /// A function-level pass to make all external variable references 406 /// point at the correct offsets from the void* passed into the function. 407 /// ClangExpressionDeclMap::DoStructLayout() must be called beforehand, so 408 /// that the offsets are valid. 409 410 /// The top-level pass implementation 411 /// 412 /// \param[in] llvm_function 413 /// The function currently being processed. 414 /// 415 /// \return 416 /// True on success; false otherwise 417 bool ReplaceVariables(llvm::Function &llvm_function); 418 419 /// Flags 420 bool m_resolve_vars; ///< True if external variable references and persistent 421 ///variable references should be resolved 422 lldb_private::ConstString 423 m_func_name; ///< The name of the function to translate 424 lldb_private::ConstString 425 m_result_name; ///< The name of the result variable ($0, $1, ...) 426 lldb_private::TypeFromParser 427 m_result_type; ///< The type of the result variable. 428 llvm::Module *m_module; ///< The module being processed, or NULL if that has 429 ///not been determined yet. 430 std::unique_ptr<llvm::DataLayout> m_target_data; ///< The target data for the 431 ///module being processed, or 432 ///NULL if there is no 433 ///module. 434 lldb_private::ClangExpressionDeclMap 435 *m_decl_map; ///< The DeclMap containing the Decls 436 llvm::FunctionCallee 437 m_CFStringCreateWithBytes; ///< The address of the function 438 /// CFStringCreateWithBytes, cast to the 439 /// appropriate function pointer type 440 llvm::FunctionCallee m_sel_registerName; ///< The address of the function 441 /// sel_registerName, cast to the 442 /// appropriate function pointer type 443 llvm::FunctionCallee m_objc_getClass; ///< The address of the function 444 /// objc_getClass, cast to the 445 /// appropriate function pointer type 446 llvm::IntegerType 447 *m_intptr_ty; ///< The type of an integer large enough to hold a pointer. 448 lldb_private::Stream 449 &m_error_stream; ///< The stream on which errors should be printed 450 lldb_private::IRExecutionUnit & 451 m_execution_unit; ///< The execution unit containing the IR being created. 452 453 llvm::StoreInst *m_result_store; ///< If non-NULL, the store instruction that 454 ///writes to the result variable. If 455 /// m_has_side_effects is true, this is 456 /// NULL. 457 bool m_result_is_pointer; ///< True if the function's result in the AST is a 458 ///pointer (see comments in 459 /// ASTResultSynthesizer::SynthesizeBodyResult) 460 461 /// A placeholder that will be replaced by a pointer to the final location of 462 /// the static allocation. 463 llvm::GlobalVariable *m_reloc_placeholder; 464 465 class FunctionValueCache { 466 public: 467 typedef std::function<llvm::Value *(llvm::Function *)> Maker; 468 469 FunctionValueCache(Maker const &maker); 470 ~FunctionValueCache(); 471 llvm::Value *GetValue(llvm::Function *function); 472 473 private: 474 Maker const m_maker; 475 typedef std::map<llvm::Function *, llvm::Value *> FunctionValueMap; 476 FunctionValueMap m_values; 477 }; 478 479 FunctionValueCache m_entry_instruction_finder; 480 481 /// UnfoldConstant operates on a constant [Old] which has just been replaced 482 /// with a value [New]. We assume that new_value has been properly placed 483 /// early in the function, in front of the first instruction in the entry 484 /// basic block [FirstEntryInstruction]. 485 /// 486 /// UnfoldConstant reads through the uses of Old and replaces Old in those 487 /// uses with New. Where those uses are constants, the function generates 488 /// new instructions to compute the result of the new, non-constant 489 /// expression and places them before FirstEntryInstruction. These 490 /// instructions replace the constant uses, so UnfoldConstant calls itself 491 /// recursively for those. 492 /// 493 /// \return 494 /// True on success; false otherwise 495 static bool UnfoldConstant(llvm::Constant *old_constant, 496 llvm::Function *llvm_function, 497 FunctionValueCache &value_maker, 498 FunctionValueCache &entry_instruction_finder, 499 lldb_private::Stream &error_stream); 500 501 /// Commit the allocation in m_data_allocator and use its final location to 502 /// replace m_reloc_placeholder. 503 /// 504 /// \return 505 /// True on success; false otherwise 506 bool CompleteDataAllocation(); 507}; 508 509#endif // liblldb_IRForTarget_h_ 510