1 //===-- IRForTarget.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "IRForTarget.h"
10 
11 #include "ClangExpressionDeclMap.h"
12 #include "ClangUtil.h"
13 
14 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/Operator.h"
18 #include "llvm/IR/InstrTypes.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/LegacyPassManager.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/ValueSymbolTable.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Transforms/IPO.h"
27 
28 #include "clang/AST/ASTContext.h"
29 
30 #include "lldb/Core/dwarf.h"
31 #include "lldb/Expression/IRExecutionUnit.h"
32 #include "lldb/Expression/IRInterpreter.h"
33 #include "lldb/Symbol/CompilerType.h"
34 #include "lldb/Utility/ConstString.h"
35 #include "lldb/Utility/DataBufferHeap.h"
36 #include "lldb/Utility/Endian.h"
37 #include "lldb/Utility/LLDBLog.h"
38 #include "lldb/Utility/Log.h"
39 #include "lldb/Utility/Scalar.h"
40 #include "lldb/Utility/StreamString.h"
41 
42 #include <map>
43 
44 using namespace llvm;
45 using lldb_private::LLDBLog;
46 
47 typedef SmallVector<Instruction *, 2> InstrList;
48 
49 IRForTarget::FunctionValueCache::FunctionValueCache(Maker const &maker)
50     : m_maker(maker), m_values() {}
51 
52 IRForTarget::FunctionValueCache::~FunctionValueCache() = default;
53 
54 llvm::Value *
55 IRForTarget::FunctionValueCache::GetValue(llvm::Function *function) {
56   if (!m_values.count(function)) {
57     llvm::Value *ret = m_maker(function);
58     m_values[function] = ret;
59     return ret;
60   }
61   return m_values[function];
62 }
63 
64 static llvm::Value *FindEntryInstruction(llvm::Function *function) {
65   if (function->empty())
66     return nullptr;
67 
68   return function->getEntryBlock().getFirstNonPHIOrDbg();
69 }
70 
71 IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
72                          bool resolve_vars,
73                          lldb_private::IRExecutionUnit &execution_unit,
74                          lldb_private::Stream &error_stream,
75                          const char *func_name)
76     : m_resolve_vars(resolve_vars), m_func_name(func_name),
77       m_decl_map(decl_map), m_error_stream(error_stream),
78       m_execution_unit(execution_unit),
79       m_entry_instruction_finder(FindEntryInstruction) {}
80 
81 /* Handy utility functions used at several places in the code */
82 
83 static std::string PrintValue(const Value *value, bool truncate = false) {
84   std::string s;
85   if (value) {
86     raw_string_ostream rso(s);
87     value->print(rso);
88     rso.flush();
89     if (truncate)
90       s.resize(s.length() - 1);
91   }
92   return s;
93 }
94 
95 static std::string PrintType(const llvm::Type *type, bool truncate = false) {
96   std::string s;
97   raw_string_ostream rso(s);
98   type->print(rso);
99   rso.flush();
100   if (truncate)
101     s.resize(s.length() - 1);
102   return s;
103 }
104 
105 bool IRForTarget::FixFunctionLinkage(llvm::Function &llvm_function) {
106   llvm_function.setLinkage(GlobalValue::ExternalLinkage);
107 
108   return true;
109 }
110 
111 clang::NamedDecl *IRForTarget::DeclForGlobal(const GlobalValue *global_val,
112                                              Module *module) {
113   NamedMDNode *named_metadata =
114       module->getNamedMetadata("clang.global.decl.ptrs");
115 
116   if (!named_metadata)
117     return nullptr;
118 
119   unsigned num_nodes = named_metadata->getNumOperands();
120   unsigned node_index;
121 
122   for (node_index = 0; node_index < num_nodes; ++node_index) {
123     llvm::MDNode *metadata_node =
124         dyn_cast<llvm::MDNode>(named_metadata->getOperand(node_index));
125     if (!metadata_node)
126       return nullptr;
127 
128     if (metadata_node->getNumOperands() != 2)
129       continue;
130 
131     if (mdconst::dyn_extract_or_null<GlobalValue>(
132             metadata_node->getOperand(0)) != global_val)
133       continue;
134 
135     ConstantInt *constant_int =
136         mdconst::dyn_extract<ConstantInt>(metadata_node->getOperand(1));
137 
138     if (!constant_int)
139       return nullptr;
140 
141     uintptr_t ptr = constant_int->getZExtValue();
142 
143     return reinterpret_cast<clang::NamedDecl *>(ptr);
144   }
145 
146   return nullptr;
147 }
148 
149 clang::NamedDecl *IRForTarget::DeclForGlobal(GlobalValue *global_val) {
150   return DeclForGlobal(global_val, m_module);
151 }
152 
153 /// Returns true iff the mangled symbol is for a static guard variable.
154 static bool isGuardVariableSymbol(llvm::StringRef mangled_symbol,
155                                   bool check_ms_abi = true) {
156   bool result = mangled_symbol.startswith("_ZGV"); // Itanium ABI guard variable
157   if (check_ms_abi)
158     result |= mangled_symbol.endswith("@4IA"); // Microsoft ABI
159   return result;
160 }
161 
162 bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) {
163   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
164 
165   if (!m_resolve_vars)
166     return true;
167 
168   // Find the result variable.  If it doesn't exist, we can give up right here.
169 
170   ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable();
171 
172   llvm::StringRef result_name;
173   bool found_result = false;
174 
175   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
176     result_name = value_symbol.first();
177 
178     // Check if this is a guard variable. It seems this causes some hiccups
179     // on Windows, so let's only check for Itanium guard variables.
180     bool is_guard_var = isGuardVariableSymbol(result_name, /*MS ABI*/ false);
181 
182     if (result_name.contains("$__lldb_expr_result_ptr") && !is_guard_var) {
183       found_result = true;
184       m_result_is_pointer = true;
185       break;
186     }
187 
188     if (result_name.contains("$__lldb_expr_result") && !is_guard_var) {
189       found_result = true;
190       m_result_is_pointer = false;
191       break;
192     }
193   }
194 
195   if (!found_result) {
196     LLDB_LOG(log, "Couldn't find result variable");
197 
198     return true;
199   }
200 
201   LLDB_LOG(log, "Result name: \"{0}\"", result_name);
202 
203   Value *result_value = m_module->getNamedValue(result_name);
204 
205   if (!result_value) {
206     LLDB_LOG(log, "Result variable had no data");
207 
208     m_error_stream.Format("Internal error [IRForTarget]: Result variable's "
209                           "name ({0}) exists, but not its definition\n",
210                           result_name);
211 
212     return false;
213   }
214 
215   LLDB_LOG(log, "Found result in the IR: \"{0}\"",
216            PrintValue(result_value, false));
217 
218   GlobalVariable *result_global = dyn_cast<GlobalVariable>(result_value);
219 
220   if (!result_global) {
221     LLDB_LOG(log, "Result variable isn't a GlobalVariable");
222 
223     m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
224                           "is defined, but is not a global variable\n",
225                           result_name);
226 
227     return false;
228   }
229 
230   clang::NamedDecl *result_decl = DeclForGlobal(result_global);
231   if (!result_decl) {
232     LLDB_LOG(log, "Result variable doesn't have a corresponding Decl");
233 
234     m_error_stream.Format("Internal error [IRForTarget]: Result variable ({0}) "
235                           "does not have a corresponding Clang entity\n",
236                           result_name);
237 
238     return false;
239   }
240 
241   if (log) {
242     std::string decl_desc_str;
243     raw_string_ostream decl_desc_stream(decl_desc_str);
244     result_decl->print(decl_desc_stream);
245     decl_desc_stream.flush();
246 
247     LLDB_LOG(log, "Found result decl: \"{0}\"", decl_desc_str);
248   }
249 
250   clang::VarDecl *result_var = dyn_cast<clang::VarDecl>(result_decl);
251   if (!result_var) {
252     LLDB_LOG(log, "Result variable Decl isn't a VarDecl");
253 
254     m_error_stream.Format("Internal error [IRForTarget]: Result variable "
255                           "({0})'s corresponding Clang entity isn't a "
256                           "variable\n",
257                           result_name);
258 
259     return false;
260   }
261 
262   // Get the next available result name from m_decl_map and create the
263   // persistent variable for it
264 
265   // If the result is an Lvalue, it is emitted as a pointer; see
266   // ASTResultSynthesizer::SynthesizeBodyResult.
267   if (m_result_is_pointer) {
268     clang::QualType pointer_qual_type = result_var->getType();
269     const clang::Type *pointer_type = pointer_qual_type.getTypePtr();
270 
271     const clang::PointerType *pointer_pointertype =
272         pointer_type->getAs<clang::PointerType>();
273     const clang::ObjCObjectPointerType *pointer_objcobjpointertype =
274         pointer_type->getAs<clang::ObjCObjectPointerType>();
275 
276     if (pointer_pointertype) {
277       clang::QualType element_qual_type = pointer_pointertype->getPointeeType();
278 
279       m_result_type = lldb_private::TypeFromParser(
280           m_decl_map->GetTypeSystem()->GetType(element_qual_type));
281     } else if (pointer_objcobjpointertype) {
282       clang::QualType element_qual_type =
283           clang::QualType(pointer_objcobjpointertype->getObjectType(), 0);
284 
285       m_result_type = lldb_private::TypeFromParser(
286           m_decl_map->GetTypeSystem()->GetType(element_qual_type));
287     } else {
288       LLDB_LOG(log, "Expected result to have pointer type, but it did not");
289 
290       m_error_stream.Format("Internal error [IRForTarget]: Lvalue result ({0}) "
291                             "is not a pointer variable\n",
292                             result_name);
293 
294       return false;
295     }
296   } else {
297     m_result_type = lldb_private::TypeFromParser(
298         m_decl_map->GetTypeSystem()->GetType(result_var->getType()));
299   }
300 
301   lldb::TargetSP target_sp(m_execution_unit.GetTarget());
302   llvm::Optional<uint64_t> bit_size = m_result_type.GetBitSize(target_sp.get());
303   if (!bit_size) {
304     lldb_private::StreamString type_desc_stream;
305     m_result_type.DumpTypeDescription(&type_desc_stream);
306 
307     LLDB_LOG(log, "Result type has unknown size");
308 
309     m_error_stream.Printf("Error [IRForTarget]: Size of result type '%s' "
310                           "couldn't be determined\n",
311                           type_desc_stream.GetData());
312     return false;
313   }
314 
315   if (log) {
316     lldb_private::StreamString type_desc_stream;
317     m_result_type.DumpTypeDescription(&type_desc_stream);
318 
319     LLDB_LOG(log, "Result decl type: \"{0}\"", type_desc_stream.GetData());
320   }
321 
322   m_result_name = lldb_private::ConstString("$RESULT_NAME");
323 
324   LLDB_LOG(log, "Creating a new result global: \"{0}\" with size {1}",
325            m_result_name,
326            m_result_type.GetByteSize(target_sp.get()).getValueOr(0));
327 
328   // Construct a new result global and set up its metadata
329 
330   GlobalVariable *new_result_global = new GlobalVariable(
331       (*m_module), result_global->getType()->getElementType(),
332       false,                                 /* not constant */
333       GlobalValue::ExternalLinkage, nullptr, /* no initializer */
334       m_result_name.GetCString());
335 
336   // It's too late in compilation to create a new VarDecl for this, but we
337   // don't need to.  We point the metadata at the old VarDecl.  This creates an
338   // odd anomaly: a variable with a Value whose name is something like $0 and a
339   // Decl whose name is $__lldb_expr_result.  This condition is handled in
340   // ClangExpressionDeclMap::DoMaterialize, and the name of the variable is
341   // fixed up.
342 
343   ConstantInt *new_constant_int =
344       ConstantInt::get(llvm::Type::getInt64Ty(m_module->getContext()),
345                        reinterpret_cast<uintptr_t>(result_decl), false);
346 
347   llvm::Metadata *values[2];
348   values[0] = ConstantAsMetadata::get(new_result_global);
349   values[1] = ConstantAsMetadata::get(new_constant_int);
350 
351   ArrayRef<Metadata *> value_ref(values, 2);
352 
353   MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
354   NamedMDNode *named_metadata =
355       m_module->getNamedMetadata("clang.global.decl.ptrs");
356   named_metadata->addOperand(persistent_global_md);
357 
358   LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(result_global),
359            PrintValue(new_result_global));
360 
361   if (result_global->use_empty()) {
362     // We need to synthesize a store for this variable, because otherwise
363     // there's nothing to put into its equivalent persistent variable.
364 
365     BasicBlock &entry_block(llvm_function.getEntryBlock());
366     Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
367 
368     if (!first_entry_instruction)
369       return false;
370 
371     if (!result_global->hasInitializer()) {
372       LLDB_LOG(log, "Couldn't find initializer for unused variable");
373 
374       m_error_stream.Format("Internal error [IRForTarget]: Result variable "
375                             "({0}) has no writes and no initializer\n",
376                             result_name);
377 
378       return false;
379     }
380 
381     Constant *initializer = result_global->getInitializer();
382 
383     StoreInst *synthesized_store =
384         new StoreInst(initializer, new_result_global, first_entry_instruction);
385 
386     LLDB_LOG(log, "Synthesized result store \"{0}\"\n",
387              PrintValue(synthesized_store));
388   } else {
389     result_global->replaceAllUsesWith(new_result_global);
390   }
391 
392   if (!m_decl_map->AddPersistentVariable(
393           result_decl, m_result_name, m_result_type, true, m_result_is_pointer))
394     return false;
395 
396   result_global->eraseFromParent();
397 
398   return true;
399 }
400 
401 bool IRForTarget::RewriteObjCConstString(llvm::GlobalVariable *ns_str,
402                                          llvm::GlobalVariable *cstr) {
403   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
404 
405   Type *ns_str_ty = ns_str->getType();
406 
407   Type *i8_ptr_ty = Type::getInt8PtrTy(m_module->getContext());
408   Type *i32_ty = Type::getInt32Ty(m_module->getContext());
409   Type *i8_ty = Type::getInt8Ty(m_module->getContext());
410 
411   if (!m_CFStringCreateWithBytes) {
412     lldb::addr_t CFStringCreateWithBytes_addr;
413 
414     static lldb_private::ConstString g_CFStringCreateWithBytes_str(
415         "CFStringCreateWithBytes");
416 
417     bool missing_weak = false;
418     CFStringCreateWithBytes_addr =
419         m_execution_unit.FindSymbol(g_CFStringCreateWithBytes_str,
420                                     missing_weak);
421     if (CFStringCreateWithBytes_addr == LLDB_INVALID_ADDRESS || missing_weak) {
422       LLDB_LOG(log, "Couldn't find CFStringCreateWithBytes in the target");
423 
424       m_error_stream.Printf("Error [IRForTarget]: Rewriting an Objective-C "
425                             "constant string requires "
426                             "CFStringCreateWithBytes\n");
427 
428       return false;
429     }
430 
431     LLDB_LOG(log, "Found CFStringCreateWithBytes at {0}",
432              CFStringCreateWithBytes_addr);
433 
434     // Build the function type:
435     //
436     // CFStringRef CFStringCreateWithBytes (
437     //   CFAllocatorRef alloc,
438     //   const UInt8 *bytes,
439     //   CFIndex numBytes,
440     //   CFStringEncoding encoding,
441     //   Boolean isExternalRepresentation
442     // );
443     //
444     // We make the following substitutions:
445     //
446     // CFStringRef -> i8*
447     // CFAllocatorRef -> i8*
448     // UInt8 * -> i8*
449     // CFIndex -> long (i32 or i64, as appropriate; we ask the module for its
450     // pointer size for now) CFStringEncoding -> i32 Boolean -> i8
451 
452     Type *arg_type_array[5];
453 
454     arg_type_array[0] = i8_ptr_ty;
455     arg_type_array[1] = i8_ptr_ty;
456     arg_type_array[2] = m_intptr_ty;
457     arg_type_array[3] = i32_ty;
458     arg_type_array[4] = i8_ty;
459 
460     ArrayRef<Type *> CFSCWB_arg_types(arg_type_array, 5);
461 
462     llvm::FunctionType *CFSCWB_ty =
463         FunctionType::get(ns_str_ty, CFSCWB_arg_types, false);
464 
465     // Build the constant containing the pointer to the function
466     PointerType *CFSCWB_ptr_ty = PointerType::getUnqual(CFSCWB_ty);
467     Constant *CFSCWB_addr_int =
468         ConstantInt::get(m_intptr_ty, CFStringCreateWithBytes_addr, false);
469     m_CFStringCreateWithBytes = {
470         CFSCWB_ty, ConstantExpr::getIntToPtr(CFSCWB_addr_int, CFSCWB_ptr_ty)};
471   }
472 
473   ConstantDataSequential *string_array = nullptr;
474 
475   if (cstr)
476     string_array = dyn_cast<ConstantDataSequential>(cstr->getInitializer());
477 
478   Constant *alloc_arg = Constant::getNullValue(i8_ptr_ty);
479   Constant *bytes_arg = cstr ? ConstantExpr::getBitCast(cstr, i8_ptr_ty)
480                              : Constant::getNullValue(i8_ptr_ty);
481   Constant *numBytes_arg = ConstantInt::get(
482       m_intptr_ty, cstr ? (string_array->getNumElements() - 1) * string_array->getElementByteSize() : 0, false);
483  int encoding_flags = 0;
484  switch (cstr ? string_array->getElementByteSize() : 1) {
485  case 1:
486    encoding_flags = 0x08000100; /* 0x08000100 is kCFStringEncodingUTF8 */
487    break;
488  case 2:
489    encoding_flags = 0x0100; /* 0x0100 is kCFStringEncodingUTF16 */
490    break;
491  case 4:
492    encoding_flags = 0x0c000100; /* 0x0c000100 is kCFStringEncodingUTF32 */
493    break;
494  default:
495    encoding_flags = 0x0600; /* fall back to 0x0600, kCFStringEncodingASCII */
496    LLDB_LOG(log, "Encountered an Objective-C constant string with unusual "
497                  "element size {0}",
498             string_array->getElementByteSize());
499  }
500  Constant *encoding_arg = ConstantInt::get(i32_ty, encoding_flags, false);
501  Constant *isExternal_arg =
502      ConstantInt::get(i8_ty, 0x0, false); /* 0x0 is false */
503 
504  Value *argument_array[5];
505 
506  argument_array[0] = alloc_arg;
507  argument_array[1] = bytes_arg;
508  argument_array[2] = numBytes_arg;
509  argument_array[3] = encoding_arg;
510  argument_array[4] = isExternal_arg;
511 
512  ArrayRef<Value *> CFSCWB_arguments(argument_array, 5);
513 
514  FunctionValueCache CFSCWB_Caller(
515      [this, &CFSCWB_arguments](llvm::Function *function) -> llvm::Value * {
516        return CallInst::Create(
517            m_CFStringCreateWithBytes, CFSCWB_arguments,
518            "CFStringCreateWithBytes",
519            llvm::cast<Instruction>(
520                m_entry_instruction_finder.GetValue(function)));
521      });
522 
523  if (!UnfoldConstant(ns_str, nullptr, CFSCWB_Caller, m_entry_instruction_finder,
524                      m_error_stream)) {
525    LLDB_LOG(log, "Couldn't replace the NSString with the result of the call");
526 
527    m_error_stream.Printf("error [IRForTarget internal]: Couldn't replace an "
528                          "Objective-C constant string with a dynamic "
529                          "string\n");
530 
531    return false;
532   }
533 
534   ns_str->eraseFromParent();
535 
536   return true;
537 }
538 
539 bool IRForTarget::RewriteObjCConstStrings() {
540   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
541 
542   ValueSymbolTable &value_symbol_table = m_module->getValueSymbolTable();
543 
544   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
545     llvm::StringRef value_name = value_symbol.first();
546 
547     if (value_name.contains("_unnamed_cfstring_")) {
548       Value *nsstring_value = value_symbol.second;
549 
550       GlobalVariable *nsstring_global =
551           dyn_cast<GlobalVariable>(nsstring_value);
552 
553       if (!nsstring_global) {
554         LLDB_LOG(log, "NSString variable is not a GlobalVariable");
555 
556         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
557                               "constant string is not a global variable\n");
558 
559         return false;
560       }
561 
562       if (!nsstring_global->hasInitializer()) {
563         LLDB_LOG(log, "NSString variable does not have an initializer");
564 
565         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
566                               "constant string does not have an initializer\n");
567 
568         return false;
569       }
570 
571       ConstantStruct *nsstring_struct =
572           dyn_cast<ConstantStruct>(nsstring_global->getInitializer());
573 
574       if (!nsstring_struct) {
575         LLDB_LOG(log,
576                  "NSString variable's initializer is not a ConstantStruct");
577 
578         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
579                               "constant string is not a structure constant\n");
580 
581         return false;
582       }
583 
584       // We expect the following structure:
585       //
586       // struct {
587       //   int *isa;
588       //   int flags;
589       //   char *str;
590       //   long length;
591       // };
592 
593       if (nsstring_struct->getNumOperands() != 4) {
594 
595         LLDB_LOG(log,
596                  "NSString variable's initializer structure has an "
597                  "unexpected number of members.  Should be 4, is {0}",
598                  nsstring_struct->getNumOperands());
599 
600         m_error_stream.Printf("Internal error [IRForTarget]: The struct for an "
601                               "Objective-C constant string is not as "
602                               "expected\n");
603 
604         return false;
605       }
606 
607       Constant *nsstring_member = nsstring_struct->getOperand(2);
608 
609       if (!nsstring_member) {
610         LLDB_LOG(log, "NSString initializer's str element was empty");
611 
612         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
613                               "constant string does not have a string "
614                               "initializer\n");
615 
616         return false;
617       }
618 
619       ConstantExpr *nsstring_expr = dyn_cast<ConstantExpr>(nsstring_member);
620 
621       if (!nsstring_expr) {
622         LLDB_LOG(log,
623                  "NSString initializer's str element is not a ConstantExpr");
624 
625         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
626                               "constant string's string initializer is not "
627                               "constant\n");
628 
629         return false;
630       }
631 
632       GlobalVariable *cstr_global = nullptr;
633 
634       if (nsstring_expr->getOpcode() == Instruction::GetElementPtr) {
635         Constant *nsstring_cstr = nsstring_expr->getOperand(0);
636         cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
637       } else if (nsstring_expr->getOpcode() == Instruction::BitCast) {
638         Constant *nsstring_cstr = nsstring_expr->getOperand(0);
639         cstr_global = dyn_cast<GlobalVariable>(nsstring_cstr);
640       }
641 
642       if (!cstr_global) {
643         LLDB_LOG(log,
644                  "NSString initializer's str element is not a GlobalVariable");
645 
646         m_error_stream.Printf("Internal error [IRForTarget]: Unhandled"
647                               "constant string initializer\n");
648 
649         return false;
650       }
651 
652       if (!cstr_global->hasInitializer()) {
653         LLDB_LOG(log, "NSString initializer's str element does not have an "
654                       "initializer");
655 
656         m_error_stream.Printf("Internal error [IRForTarget]: An Objective-C "
657                               "constant string's string initializer doesn't "
658                               "point to initialized data\n");
659 
660         return false;
661       }
662 
663       /*
664       if (!cstr_array)
665       {
666           if (log)
667               log->PutCString("NSString initializer's str element is not a
668       ConstantArray");
669 
670           if (m_error_stream)
671               m_error_stream.Printf("Internal error [IRForTarget]: An
672       Objective-C constant string's string initializer doesn't point to an
673       array\n");
674 
675           return false;
676       }
677 
678       if (!cstr_array->isCString())
679       {
680           if (log)
681               log->PutCString("NSString initializer's str element is not a C
682       string array");
683 
684           if (m_error_stream)
685               m_error_stream.Printf("Internal error [IRForTarget]: An
686       Objective-C constant string's string initializer doesn't point to a C
687       string\n");
688 
689           return false;
690       }
691       */
692 
693       ConstantDataArray *cstr_array =
694           dyn_cast<ConstantDataArray>(cstr_global->getInitializer());
695 
696       if (cstr_array)
697         LLDB_LOG(log, "Found NSString constant {0}, which contains \"{1}\"",
698                  value_name, cstr_array->getAsString());
699       else
700         LLDB_LOG(log, "Found NSString constant {0}, which contains \"\"",
701                  value_name);
702 
703       if (!cstr_array)
704         cstr_global = nullptr;
705 
706       if (!RewriteObjCConstString(nsstring_global, cstr_global)) {
707         LLDB_LOG(log, "Error rewriting the constant string");
708 
709         // We don't print an error message here because RewriteObjCConstString
710         // has done so for us.
711 
712         return false;
713       }
714     }
715   }
716 
717   for (StringMapEntry<llvm::Value *> &value_symbol : value_symbol_table) {
718     llvm::StringRef value_name = value_symbol.first();
719 
720     if (value_name == "__CFConstantStringClassReference") {
721       GlobalVariable *gv = dyn_cast<GlobalVariable>(value_symbol.second);
722 
723       if (!gv) {
724         LLDB_LOG(log,
725                  "__CFConstantStringClassReference is not a global variable");
726 
727         m_error_stream.Printf("Internal error [IRForTarget]: Found a "
728                               "CFConstantStringClassReference, but it is not a "
729                               "global object\n");
730 
731         return false;
732       }
733 
734       gv->eraseFromParent();
735 
736       break;
737     }
738   }
739 
740   return true;
741 }
742 
743 static bool IsObjCSelectorRef(Value *value) {
744   GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
745 
746   return !(!global_variable || !global_variable->hasName() ||
747            !global_variable->getName().startswith("OBJC_SELECTOR_REFERENCES_"));
748 }
749 
750 // This function does not report errors; its callers are responsible.
751 bool IRForTarget::RewriteObjCSelector(Instruction *selector_load) {
752   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
753 
754   LoadInst *load = dyn_cast<LoadInst>(selector_load);
755 
756   if (!load)
757     return false;
758 
759   // Unpack the message name from the selector.  In LLVM IR, an objc_msgSend
760   // gets represented as
761   //
762   // %tmp     = load i8** @"OBJC_SELECTOR_REFERENCES_" ; <i8*> %call    = call
763   // i8* (i8*, i8*, ...)* @objc_msgSend(i8* %obj, i8* %tmp, ...) ; <i8*>
764   //
765   // where %obj is the object pointer and %tmp is the selector.
766   //
767   // @"OBJC_SELECTOR_REFERENCES_" is a pointer to a character array called
768   // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_".
769   // @"\01L_OBJC_llvm_moduleETH_VAR_NAllvm_moduleE_" contains the string.
770 
771   // Find the pointer's initializer (a ConstantExpr with opcode GetElementPtr)
772   // and get the string from its target
773 
774   GlobalVariable *_objc_selector_references_ =
775       dyn_cast<GlobalVariable>(load->getPointerOperand());
776 
777   if (!_objc_selector_references_ ||
778       !_objc_selector_references_->hasInitializer())
779     return false;
780 
781   Constant *osr_initializer = _objc_selector_references_->getInitializer();
782 
783   ConstantExpr *osr_initializer_expr = dyn_cast<ConstantExpr>(osr_initializer);
784 
785   if (!osr_initializer_expr ||
786       osr_initializer_expr->getOpcode() != Instruction::GetElementPtr)
787     return false;
788 
789   Value *osr_initializer_base = osr_initializer_expr->getOperand(0);
790 
791   if (!osr_initializer_base)
792     return false;
793 
794   // Find the string's initializer (a ConstantArray) and get the string from it
795 
796   GlobalVariable *_objc_meth_var_name_ =
797       dyn_cast<GlobalVariable>(osr_initializer_base);
798 
799   if (!_objc_meth_var_name_ || !_objc_meth_var_name_->hasInitializer())
800     return false;
801 
802   Constant *omvn_initializer = _objc_meth_var_name_->getInitializer();
803 
804   ConstantDataArray *omvn_initializer_array =
805       dyn_cast<ConstantDataArray>(omvn_initializer);
806 
807   if (!omvn_initializer_array->isString())
808     return false;
809 
810   std::string omvn_initializer_string =
811       std::string(omvn_initializer_array->getAsString());
812 
813   LLDB_LOG(log, "Found Objective-C selector reference \"{0}\"",
814            omvn_initializer_string);
815 
816   // Construct a call to sel_registerName
817 
818   if (!m_sel_registerName) {
819     lldb::addr_t sel_registerName_addr;
820 
821     bool missing_weak = false;
822     static lldb_private::ConstString g_sel_registerName_str("sel_registerName");
823     sel_registerName_addr = m_execution_unit.FindSymbol(g_sel_registerName_str,
824                                                         missing_weak);
825     if (sel_registerName_addr == LLDB_INVALID_ADDRESS || missing_weak)
826       return false;
827 
828     LLDB_LOG(log, "Found sel_registerName at {0}", sel_registerName_addr);
829 
830     // Build the function type: struct objc_selector
831     // *sel_registerName(uint8_t*)
832 
833     // The below code would be "more correct," but in actuality what's required
834     // is uint8_t*
835     // Type *sel_type = StructType::get(m_module->getContext());
836     // Type *sel_ptr_type = PointerType::getUnqual(sel_type);
837     Type *sel_ptr_type = Type::getInt8PtrTy(m_module->getContext());
838 
839     Type *type_array[1];
840 
841     type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
842 
843     ArrayRef<Type *> srN_arg_types(type_array, 1);
844 
845     llvm::FunctionType *srN_type =
846         FunctionType::get(sel_ptr_type, srN_arg_types, false);
847 
848     // Build the constant containing the pointer to the function
849     PointerType *srN_ptr_ty = PointerType::getUnqual(srN_type);
850     Constant *srN_addr_int =
851         ConstantInt::get(m_intptr_ty, sel_registerName_addr, false);
852     m_sel_registerName = {srN_type,
853                           ConstantExpr::getIntToPtr(srN_addr_int, srN_ptr_ty)};
854   }
855 
856   Value *argument_array[1];
857 
858   Constant *omvn_pointer = ConstantExpr::getBitCast(
859       _objc_meth_var_name_, Type::getInt8PtrTy(m_module->getContext()));
860 
861   argument_array[0] = omvn_pointer;
862 
863   ArrayRef<Value *> srN_arguments(argument_array, 1);
864 
865   CallInst *srN_call = CallInst::Create(m_sel_registerName, srN_arguments,
866                                         "sel_registerName", selector_load);
867 
868   // Replace the load with the call in all users
869 
870   selector_load->replaceAllUsesWith(srN_call);
871 
872   selector_load->eraseFromParent();
873 
874   return true;
875 }
876 
877 bool IRForTarget::RewriteObjCSelectors(BasicBlock &basic_block) {
878   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
879 
880   InstrList selector_loads;
881 
882   for (Instruction &inst : basic_block) {
883     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
884       if (IsObjCSelectorRef(load->getPointerOperand()))
885         selector_loads.push_back(&inst);
886   }
887 
888   for (Instruction *inst : selector_loads) {
889     if (!RewriteObjCSelector(inst)) {
890       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a "
891                             "static reference to an Objective-C selector to a "
892                             "dynamic reference\n");
893 
894       LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C selector");
895 
896       return false;
897     }
898   }
899 
900   return true;
901 }
902 
903 static bool IsObjCClassReference(Value *value) {
904   GlobalVariable *global_variable = dyn_cast<GlobalVariable>(value);
905 
906   return !(!global_variable || !global_variable->hasName() ||
907            !global_variable->getName().startswith("OBJC_CLASS_REFERENCES_"));
908 }
909 
910 // This function does not report errors; its callers are responsible.
911 bool IRForTarget::RewriteObjCClassReference(Instruction *class_load) {
912   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
913 
914   LoadInst *load = dyn_cast<LoadInst>(class_load);
915 
916   if (!load)
917     return false;
918 
919   // Unpack the class name from the reference.  In LLVM IR, a reference to an
920   // Objective-C class gets represented as
921   //
922   // %tmp     = load %struct._objc_class*,
923   //            %struct._objc_class** @OBJC_CLASS_REFERENCES_, align 4
924   //
925   // @"OBJC_CLASS_REFERENCES_ is a bitcast of a character array called
926   // @OBJC_CLASS_NAME_. @OBJC_CLASS_NAME contains the string.
927 
928   // Find the pointer's initializer (a ConstantExpr with opcode BitCast) and
929   // get the string from its target
930 
931   GlobalVariable *_objc_class_references_ =
932       dyn_cast<GlobalVariable>(load->getPointerOperand());
933 
934   if (!_objc_class_references_ ||
935       !_objc_class_references_->hasInitializer())
936     return false;
937 
938   Constant *ocr_initializer = _objc_class_references_->getInitializer();
939 
940   ConstantExpr *ocr_initializer_expr = dyn_cast<ConstantExpr>(ocr_initializer);
941 
942   if (!ocr_initializer_expr ||
943       ocr_initializer_expr->getOpcode() != Instruction::BitCast)
944     return false;
945 
946   Value *ocr_initializer_base = ocr_initializer_expr->getOperand(0);
947 
948   if (!ocr_initializer_base)
949     return false;
950 
951   // Find the string's initializer (a ConstantArray) and get the string from it
952 
953   GlobalVariable *_objc_class_name_ =
954       dyn_cast<GlobalVariable>(ocr_initializer_base);
955 
956   if (!_objc_class_name_ || !_objc_class_name_->hasInitializer())
957     return false;
958 
959   Constant *ocn_initializer = _objc_class_name_->getInitializer();
960 
961   ConstantDataArray *ocn_initializer_array =
962       dyn_cast<ConstantDataArray>(ocn_initializer);
963 
964   if (!ocn_initializer_array->isString())
965     return false;
966 
967   std::string ocn_initializer_string =
968       std::string(ocn_initializer_array->getAsString());
969 
970   LLDB_LOG(log, "Found Objective-C class reference \"{0}\"",
971            ocn_initializer_string);
972 
973   // Construct a call to objc_getClass
974 
975   if (!m_objc_getClass) {
976     lldb::addr_t objc_getClass_addr;
977 
978     bool missing_weak = false;
979     static lldb_private::ConstString g_objc_getClass_str("objc_getClass");
980     objc_getClass_addr = m_execution_unit.FindSymbol(g_objc_getClass_str,
981                                                      missing_weak);
982     if (objc_getClass_addr == LLDB_INVALID_ADDRESS || missing_weak)
983       return false;
984 
985     LLDB_LOG(log, "Found objc_getClass at {0}", objc_getClass_addr);
986 
987     // Build the function type: %struct._objc_class *objc_getClass(i8*)
988 
989     Type *class_type = load->getType();
990     Type *type_array[1];
991     type_array[0] = llvm::Type::getInt8PtrTy(m_module->getContext());
992 
993     ArrayRef<Type *> ogC_arg_types(type_array, 1);
994 
995     llvm::FunctionType *ogC_type =
996         FunctionType::get(class_type, ogC_arg_types, false);
997 
998     // Build the constant containing the pointer to the function
999     PointerType *ogC_ptr_ty = PointerType::getUnqual(ogC_type);
1000     Constant *ogC_addr_int =
1001         ConstantInt::get(m_intptr_ty, objc_getClass_addr, false);
1002     m_objc_getClass = {ogC_type,
1003                        ConstantExpr::getIntToPtr(ogC_addr_int, ogC_ptr_ty)};
1004   }
1005 
1006   Value *argument_array[1];
1007 
1008   Constant *ocn_pointer = ConstantExpr::getBitCast(
1009       _objc_class_name_, Type::getInt8PtrTy(m_module->getContext()));
1010 
1011   argument_array[0] = ocn_pointer;
1012 
1013   ArrayRef<Value *> ogC_arguments(argument_array, 1);
1014 
1015   CallInst *ogC_call = CallInst::Create(m_objc_getClass, ogC_arguments,
1016                                         "objc_getClass", class_load);
1017 
1018   // Replace the load with the call in all users
1019 
1020   class_load->replaceAllUsesWith(ogC_call);
1021 
1022   class_load->eraseFromParent();
1023 
1024   return true;
1025 }
1026 
1027 bool IRForTarget::RewriteObjCClassReferences(BasicBlock &basic_block) {
1028   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1029 
1030   InstrList class_loads;
1031 
1032   for (Instruction &inst : basic_block) {
1033     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1034       if (IsObjCClassReference(load->getPointerOperand()))
1035         class_loads.push_back(&inst);
1036   }
1037 
1038   for (Instruction *inst : class_loads) {
1039     if (!RewriteObjCClassReference(inst)) {
1040       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't change a "
1041                             "static reference to an Objective-C class to a "
1042                             "dynamic reference\n");
1043 
1044       LLDB_LOG(log, "Couldn't rewrite a reference to an Objective-C class");
1045 
1046       return false;
1047     }
1048   }
1049 
1050   return true;
1051 }
1052 
1053 // This function does not report errors; its callers are responsible.
1054 bool IRForTarget::RewritePersistentAlloc(llvm::Instruction *persistent_alloc) {
1055   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1056 
1057   AllocaInst *alloc = dyn_cast<AllocaInst>(persistent_alloc);
1058 
1059   MDNode *alloc_md = alloc->getMetadata("clang.decl.ptr");
1060 
1061   if (!alloc_md || !alloc_md->getNumOperands())
1062     return false;
1063 
1064   ConstantInt *constant_int =
1065       mdconst::dyn_extract<ConstantInt>(alloc_md->getOperand(0));
1066 
1067   if (!constant_int)
1068     return false;
1069 
1070   // We attempt to register this as a new persistent variable with the DeclMap.
1071 
1072   uintptr_t ptr = constant_int->getZExtValue();
1073 
1074   clang::VarDecl *decl = reinterpret_cast<clang::VarDecl *>(ptr);
1075 
1076   lldb_private::TypeFromParser result_decl_type(
1077       m_decl_map->GetTypeSystem()->GetType(decl->getType()));
1078 
1079   StringRef decl_name(decl->getName());
1080   lldb_private::ConstString persistent_variable_name(decl_name.data(),
1081                                                      decl_name.size());
1082   if (!m_decl_map->AddPersistentVariable(decl, persistent_variable_name,
1083                                          result_decl_type, false, false))
1084     return false;
1085 
1086   GlobalVariable *persistent_global = new GlobalVariable(
1087       (*m_module), alloc->getType(), false,  /* not constant */
1088       GlobalValue::ExternalLinkage, nullptr, /* no initializer */
1089       alloc->getName().str());
1090 
1091   // What we're going to do here is make believe this was a regular old
1092   // external variable.  That means we need to make the metadata valid.
1093 
1094   NamedMDNode *named_metadata =
1095       m_module->getOrInsertNamedMetadata("clang.global.decl.ptrs");
1096 
1097   llvm::Metadata *values[2];
1098   values[0] = ConstantAsMetadata::get(persistent_global);
1099   values[1] = ConstantAsMetadata::get(constant_int);
1100 
1101   ArrayRef<llvm::Metadata *> value_ref(values, 2);
1102 
1103   MDNode *persistent_global_md = MDNode::get(m_module->getContext(), value_ref);
1104   named_metadata->addOperand(persistent_global_md);
1105 
1106   // Now, since the variable is a pointer variable, we will drop in a load of
1107   // that pointer variable.
1108 
1109   LoadInst *persistent_load =
1110       new LoadInst(persistent_global->getType()->getPointerElementType(),
1111                    persistent_global, "", alloc);
1112 
1113   LLDB_LOG(log, "Replacing \"{0}\" with \"{1}\"", PrintValue(alloc),
1114            PrintValue(persistent_load));
1115 
1116   alloc->replaceAllUsesWith(persistent_load);
1117   alloc->eraseFromParent();
1118 
1119   return true;
1120 }
1121 
1122 bool IRForTarget::RewritePersistentAllocs(llvm::BasicBlock &basic_block) {
1123   if (!m_resolve_vars)
1124     return true;
1125 
1126   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1127 
1128   InstrList pvar_allocs;
1129 
1130   for (Instruction &inst : basic_block) {
1131 
1132     if (AllocaInst *alloc = dyn_cast<AllocaInst>(&inst)) {
1133       llvm::StringRef alloc_name = alloc->getName();
1134 
1135       if (alloc_name.startswith("$") && !alloc_name.startswith("$__lldb")) {
1136         if (alloc_name.find_first_of("0123456789") == 1) {
1137           LLDB_LOG(log, "Rejecting a numeric persistent variable.");
1138 
1139           m_error_stream.Printf("Error [IRForTarget]: Names starting with $0, "
1140                                 "$1, ... are reserved for use as result "
1141                                 "names\n");
1142 
1143           return false;
1144         }
1145 
1146         pvar_allocs.push_back(alloc);
1147       }
1148     }
1149   }
1150 
1151   for (Instruction *inst : pvar_allocs) {
1152     if (!RewritePersistentAlloc(inst)) {
1153       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
1154                             "the creation of a persistent variable\n");
1155 
1156       LLDB_LOG(log, "Couldn't rewrite the creation of a persistent variable");
1157 
1158       return false;
1159     }
1160   }
1161 
1162   return true;
1163 }
1164 
1165 // This function does not report errors; its callers are responsible.
1166 bool IRForTarget::MaybeHandleVariable(Value *llvm_value_ptr) {
1167   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1168 
1169   LLDB_LOG(log, "MaybeHandleVariable ({0})", PrintValue(llvm_value_ptr));
1170 
1171   if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(llvm_value_ptr)) {
1172     switch (constant_expr->getOpcode()) {
1173     default:
1174       break;
1175     case Instruction::GetElementPtr:
1176     case Instruction::BitCast:
1177       Value *s = constant_expr->getOperand(0);
1178       if (!MaybeHandleVariable(s))
1179         return false;
1180     }
1181   } else if (GlobalVariable *global_variable =
1182                  dyn_cast<GlobalVariable>(llvm_value_ptr)) {
1183     if (!GlobalValue::isExternalLinkage(global_variable->getLinkage()))
1184       return true;
1185 
1186     clang::NamedDecl *named_decl = DeclForGlobal(global_variable);
1187 
1188     if (!named_decl) {
1189       if (IsObjCSelectorRef(llvm_value_ptr))
1190         return true;
1191 
1192       if (!global_variable->hasExternalLinkage())
1193         return true;
1194 
1195       LLDB_LOG(log, "Found global variable \"{0}\" without metadata",
1196                global_variable->getName());
1197 
1198       return false;
1199     }
1200 
1201     llvm::StringRef name(named_decl->getName());
1202 
1203     clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl);
1204     if (value_decl == nullptr)
1205       return false;
1206 
1207     lldb_private::CompilerType compiler_type =
1208         m_decl_map->GetTypeSystem()->GetType(value_decl->getType());
1209 
1210     const Type *value_type = nullptr;
1211 
1212     if (name.startswith("$")) {
1213       // The $__lldb_expr_result name indicates the return value has allocated
1214       // as a static variable.  Per the comment at
1215       // ASTResultSynthesizer::SynthesizeBodyResult, accesses to this static
1216       // variable need to be redirected to the result of dereferencing a
1217       // pointer that is passed in as one of the arguments.
1218       //
1219       // Consequently, when reporting the size of the type, we report a pointer
1220       // type pointing to the type of $__lldb_expr_result, not the type itself.
1221       //
1222       // We also do this for any user-declared persistent variables.
1223       compiler_type = compiler_type.GetPointerType();
1224       value_type = PointerType::get(global_variable->getType(), 0);
1225     } else {
1226       value_type = global_variable->getType();
1227     }
1228 
1229     auto *target = m_execution_unit.GetTarget().get();
1230     llvm::Optional<uint64_t> value_size = compiler_type.GetByteSize(target);
1231     if (!value_size)
1232       return false;
1233     llvm::Optional<size_t> opt_alignment =
1234         compiler_type.GetTypeBitAlign(target);
1235     if (!opt_alignment)
1236       return false;
1237     lldb::offset_t value_alignment = (*opt_alignment + 7ull) / 8ull;
1238 
1239     LLDB_LOG(log,
1240              "Type of \"{0}\" is [clang \"{1}\", llvm \"{2}\"] [size {3}, "
1241              "align {4}]",
1242              name,
1243              lldb_private::ClangUtil::GetQualType(compiler_type).getAsString(),
1244              PrintType(value_type), *value_size, value_alignment);
1245 
1246     if (named_decl)
1247       m_decl_map->AddValueToStruct(named_decl, lldb_private::ConstString(name),
1248                                    llvm_value_ptr, *value_size,
1249                                    value_alignment);
1250   } else if (isa<llvm::Function>(llvm_value_ptr)) {
1251     LLDB_LOG(log, "Function pointers aren't handled right now");
1252 
1253     return false;
1254   }
1255 
1256   return true;
1257 }
1258 
1259 // This function does not report errors; its callers are responsible.
1260 bool IRForTarget::HandleSymbol(Value *symbol) {
1261   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1262 
1263   lldb_private::ConstString name(symbol->getName().str().c_str());
1264 
1265   lldb::addr_t symbol_addr =
1266       m_decl_map->GetSymbolAddress(name, lldb::eSymbolTypeAny);
1267 
1268   if (symbol_addr == LLDB_INVALID_ADDRESS) {
1269     LLDB_LOG(log, "Symbol \"{0}\" had no address", name);
1270 
1271     return false;
1272   }
1273 
1274   LLDB_LOG(log, "Found \"{0}\" at {1}", name, symbol_addr);
1275 
1276   Type *symbol_type = symbol->getType();
1277 
1278   Constant *symbol_addr_int = ConstantInt::get(m_intptr_ty, symbol_addr, false);
1279 
1280   Value *symbol_addr_ptr =
1281       ConstantExpr::getIntToPtr(symbol_addr_int, symbol_type);
1282 
1283   LLDB_LOG(log, "Replacing {0} with {1}", PrintValue(symbol),
1284            PrintValue(symbol_addr_ptr));
1285 
1286   symbol->replaceAllUsesWith(symbol_addr_ptr);
1287 
1288   return true;
1289 }
1290 
1291 bool IRForTarget::MaybeHandleCallArguments(CallInst *Old) {
1292   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1293 
1294   LLDB_LOG(log, "MaybeHandleCallArguments({0})", PrintValue(Old));
1295 
1296   for (unsigned op_index = 0, num_ops = Old->arg_size();
1297        op_index < num_ops; ++op_index)
1298     // conservatively believe that this is a store
1299     if (!MaybeHandleVariable(Old->getArgOperand(op_index))) {
1300       m_error_stream.Printf("Internal error [IRForTarget]: Couldn't rewrite "
1301                             "one of the arguments of a function call.\n");
1302 
1303       return false;
1304     }
1305 
1306   return true;
1307 }
1308 
1309 bool IRForTarget::HandleObjCClass(Value *classlist_reference) {
1310   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1311 
1312   GlobalVariable *global_variable =
1313       dyn_cast<GlobalVariable>(classlist_reference);
1314 
1315   if (!global_variable)
1316     return false;
1317 
1318   Constant *initializer = global_variable->getInitializer();
1319 
1320   if (!initializer)
1321     return false;
1322 
1323   if (!initializer->hasName())
1324     return false;
1325 
1326   StringRef name(initializer->getName());
1327   lldb_private::ConstString name_cstr(name.str().c_str());
1328   lldb::addr_t class_ptr =
1329       m_decl_map->GetSymbolAddress(name_cstr, lldb::eSymbolTypeObjCClass);
1330 
1331   LLDB_LOG(log, "Found reference to Objective-C class {0} ({1})", name,
1332            (unsigned long long)class_ptr);
1333 
1334   if (class_ptr == LLDB_INVALID_ADDRESS)
1335     return false;
1336 
1337   if (global_variable->use_empty())
1338     return false;
1339 
1340   SmallVector<LoadInst *, 2> load_instructions;
1341 
1342   for (llvm::User *u : global_variable->users()) {
1343     if (LoadInst *load_instruction = dyn_cast<LoadInst>(u))
1344       load_instructions.push_back(load_instruction);
1345   }
1346 
1347   if (load_instructions.empty())
1348     return false;
1349 
1350   Constant *class_addr = ConstantInt::get(m_intptr_ty, (uint64_t)class_ptr);
1351 
1352   for (LoadInst *load_instruction : load_instructions) {
1353     Constant *class_bitcast =
1354         ConstantExpr::getIntToPtr(class_addr, load_instruction->getType());
1355 
1356     load_instruction->replaceAllUsesWith(class_bitcast);
1357 
1358     load_instruction->eraseFromParent();
1359   }
1360 
1361   return true;
1362 }
1363 
1364 bool IRForTarget::RemoveCXAAtExit(BasicBlock &basic_block) {
1365   std::vector<CallInst *> calls_to_remove;
1366 
1367   for (Instruction &inst : basic_block) {
1368     CallInst *call = dyn_cast<CallInst>(&inst);
1369 
1370     // MaybeHandleCallArguments handles error reporting; we are silent here
1371     if (!call)
1372       continue;
1373 
1374     bool remove = false;
1375 
1376     llvm::Function *func = call->getCalledFunction();
1377 
1378     if (func && func->getName() == "__cxa_atexit")
1379       remove = true;
1380 
1381     llvm::Value *val = call->getCalledOperand();
1382 
1383     if (val && val->getName() == "__cxa_atexit")
1384       remove = true;
1385 
1386     if (remove)
1387       calls_to_remove.push_back(call);
1388   }
1389 
1390   for (CallInst *ci : calls_to_remove)
1391     ci->eraseFromParent();
1392 
1393   return true;
1394 }
1395 
1396 bool IRForTarget::ResolveCalls(BasicBlock &basic_block) {
1397   // Prepare the current basic block for execution in the remote process
1398 
1399   for (Instruction &inst : basic_block) {
1400     CallInst *call = dyn_cast<CallInst>(&inst);
1401 
1402     // MaybeHandleCallArguments handles error reporting; we are silent here
1403     if (call && !MaybeHandleCallArguments(call))
1404       return false;
1405   }
1406 
1407   return true;
1408 }
1409 
1410 bool IRForTarget::ResolveExternals(Function &llvm_function) {
1411   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1412 
1413   for (GlobalVariable &global_var : m_module->globals()) {
1414     llvm::StringRef global_name = global_var.getName();
1415 
1416     LLDB_LOG(log, "Examining {0}, DeclForGlobalValue returns {1}", global_name,
1417              static_cast<void *>(DeclForGlobal(&global_var)));
1418 
1419     if (global_name.startswith("OBJC_IVAR")) {
1420       if (!HandleSymbol(&global_var)) {
1421         m_error_stream.Format("Error [IRForTarget]: Couldn't find Objective-C "
1422                               "indirect ivar symbol {0}\n",
1423                               global_name);
1424 
1425         return false;
1426       }
1427     } else if (global_name.contains("OBJC_CLASSLIST_REFERENCES_$")) {
1428       if (!HandleObjCClass(&global_var)) {
1429         m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1430                               "for an Objective-C static method call\n");
1431 
1432         return false;
1433       }
1434     } else if (global_name.contains("OBJC_CLASSLIST_SUP_REFS_$")) {
1435       if (!HandleObjCClass(&global_var)) {
1436         m_error_stream.Printf("Error [IRForTarget]: Couldn't resolve the class "
1437                               "for an Objective-C static method call\n");
1438 
1439         return false;
1440       }
1441     } else if (DeclForGlobal(&global_var)) {
1442       if (!MaybeHandleVariable(&global_var)) {
1443         m_error_stream.Format("Internal error [IRForTarget]: Couldn't rewrite "
1444                               "external variable {0}\n",
1445                               global_name);
1446 
1447         return false;
1448       }
1449     }
1450   }
1451 
1452   return true;
1453 }
1454 
1455 static bool isGuardVariableRef(Value *V) {
1456   Constant *Old = dyn_cast<Constant>(V);
1457 
1458   if (!Old)
1459     return false;
1460 
1461   if (auto CE = dyn_cast<ConstantExpr>(V)) {
1462     if (CE->getOpcode() != Instruction::BitCast)
1463       return false;
1464 
1465     Old = CE->getOperand(0);
1466   }
1467 
1468   GlobalVariable *GV = dyn_cast<GlobalVariable>(Old);
1469 
1470   if (!GV || !GV->hasName() || !isGuardVariableSymbol(GV->getName()))
1471     return false;
1472 
1473   return true;
1474 }
1475 
1476 void IRForTarget::TurnGuardLoadIntoZero(llvm::Instruction *guard_load) {
1477   Constant *zero(Constant::getNullValue(guard_load->getType()));
1478   guard_load->replaceAllUsesWith(zero);
1479   guard_load->eraseFromParent();
1480 }
1481 
1482 static void ExciseGuardStore(Instruction *guard_store) {
1483   guard_store->eraseFromParent();
1484 }
1485 
1486 bool IRForTarget::RemoveGuards(BasicBlock &basic_block) {
1487   // Eliminate any reference to guard variables found.
1488 
1489   InstrList guard_loads;
1490   InstrList guard_stores;
1491 
1492   for (Instruction &inst : basic_block) {
1493 
1494     if (LoadInst *load = dyn_cast<LoadInst>(&inst))
1495       if (isGuardVariableRef(load->getPointerOperand()))
1496         guard_loads.push_back(&inst);
1497 
1498     if (StoreInst *store = dyn_cast<StoreInst>(&inst))
1499       if (isGuardVariableRef(store->getPointerOperand()))
1500         guard_stores.push_back(&inst);
1501   }
1502 
1503   for (Instruction *inst : guard_loads)
1504     TurnGuardLoadIntoZero(inst);
1505 
1506   for (Instruction *inst : guard_stores)
1507     ExciseGuardStore(inst);
1508 
1509   return true;
1510 }
1511 
1512 // This function does not report errors; its callers are responsible.
1513 bool IRForTarget::UnfoldConstant(Constant *old_constant,
1514                                  llvm::Function *llvm_function,
1515                                  FunctionValueCache &value_maker,
1516                                  FunctionValueCache &entry_instruction_finder,
1517                                  lldb_private::Stream &error_stream) {
1518   SmallVector<User *, 16> users;
1519 
1520   // We do this because the use list might change, invalidating our iterator.
1521   // Much better to keep a work list ourselves.
1522   for (llvm::User *u : old_constant->users())
1523     users.push_back(u);
1524 
1525   for (size_t i = 0; i < users.size(); ++i) {
1526     User *user = users[i];
1527 
1528     if (Constant *constant = dyn_cast<Constant>(user)) {
1529       // synthesize a new non-constant equivalent of the constant
1530 
1531       if (ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) {
1532         switch (constant_expr->getOpcode()) {
1533         default:
1534           error_stream.Printf("error [IRForTarget internal]: Unhandled "
1535                               "constant expression type: \"%s\"",
1536                               PrintValue(constant_expr).c_str());
1537           return false;
1538         case Instruction::BitCast: {
1539           FunctionValueCache bit_cast_maker(
1540               [&value_maker, &entry_instruction_finder, old_constant,
1541                constant_expr](llvm::Function *function) -> llvm::Value * {
1542                 // UnaryExpr
1543                 //   OperandList[0] is value
1544 
1545                 if (constant_expr->getOperand(0) != old_constant)
1546                   return constant_expr;
1547 
1548                 return new BitCastInst(
1549                     value_maker.GetValue(function), constant_expr->getType(),
1550                     "", llvm::cast<Instruction>(
1551                             entry_instruction_finder.GetValue(function)));
1552               });
1553 
1554           if (!UnfoldConstant(constant_expr, llvm_function, bit_cast_maker,
1555                               entry_instruction_finder, error_stream))
1556             return false;
1557         } break;
1558         case Instruction::GetElementPtr: {
1559           // GetElementPtrConstantExpr
1560           //   OperandList[0] is base
1561           //   OperandList[1]... are indices
1562 
1563           FunctionValueCache get_element_pointer_maker(
1564               [&value_maker, &entry_instruction_finder, old_constant,
1565                constant_expr](llvm::Function *function) -> llvm::Value * {
1566                 auto *gep = cast<llvm::GEPOperator>(constant_expr);
1567                 Value *ptr = gep->getPointerOperand();
1568 
1569                 if (ptr == old_constant)
1570                   ptr = value_maker.GetValue(function);
1571 
1572                 std::vector<Value *> index_vector;
1573                 for (Value *operand : gep->indices()) {
1574                   if (operand == old_constant)
1575                     operand = value_maker.GetValue(function);
1576 
1577                   index_vector.push_back(operand);
1578                 }
1579 
1580                 ArrayRef<Value *> indices(index_vector);
1581 
1582                 return GetElementPtrInst::Create(
1583                     gep->getSourceElementType(), ptr, indices, "",
1584                     llvm::cast<Instruction>(
1585                         entry_instruction_finder.GetValue(function)));
1586               });
1587 
1588           if (!UnfoldConstant(constant_expr, llvm_function,
1589                               get_element_pointer_maker,
1590                               entry_instruction_finder, error_stream))
1591             return false;
1592         } break;
1593         }
1594       } else {
1595         error_stream.Printf(
1596             "error [IRForTarget internal]: Unhandled constant type: \"%s\"",
1597             PrintValue(constant).c_str());
1598         return false;
1599       }
1600     } else {
1601       if (Instruction *inst = llvm::dyn_cast<Instruction>(user)) {
1602         if (llvm_function && inst->getParent()->getParent() != llvm_function) {
1603           error_stream.PutCString("error: Capturing non-local variables in "
1604                                   "expressions is unsupported.\n");
1605           return false;
1606         }
1607         inst->replaceUsesOfWith(
1608             old_constant, value_maker.GetValue(inst->getParent()->getParent()));
1609       } else {
1610         error_stream.Printf(
1611             "error [IRForTarget internal]: Unhandled non-constant type: \"%s\"",
1612             PrintValue(user).c_str());
1613         return false;
1614       }
1615     }
1616   }
1617 
1618   if (!isa<GlobalValue>(old_constant)) {
1619     old_constant->destroyConstant();
1620   }
1621 
1622   return true;
1623 }
1624 
1625 bool IRForTarget::ReplaceVariables(Function &llvm_function) {
1626   if (!m_resolve_vars)
1627     return true;
1628 
1629   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1630 
1631   m_decl_map->DoStructLayout();
1632 
1633   LLDB_LOG(log, "Element arrangement:");
1634 
1635   uint32_t num_elements;
1636   uint32_t element_index;
1637 
1638   size_t size;
1639   lldb::offset_t alignment;
1640 
1641   if (!m_decl_map->GetStructInfo(num_elements, size, alignment))
1642     return false;
1643 
1644   Function::arg_iterator iter(llvm_function.arg_begin());
1645 
1646   if (iter == llvm_function.arg_end()) {
1647     m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes no "
1648                           "arguments (should take at least a struct pointer)");
1649 
1650     return false;
1651   }
1652 
1653   Argument *argument = &*iter;
1654 
1655   if (argument->getName().equals("this")) {
1656     ++iter;
1657 
1658     if (iter == llvm_function.arg_end()) {
1659       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1660                             "'this' argument (should take a struct pointer "
1661                             "too)");
1662 
1663       return false;
1664     }
1665 
1666     argument = &*iter;
1667   } else if (argument->getName().equals("self")) {
1668     ++iter;
1669 
1670     if (iter == llvm_function.arg_end()) {
1671       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1672                             "'self' argument (should take '_cmd' and a struct "
1673                             "pointer too)");
1674 
1675       return false;
1676     }
1677 
1678     if (!iter->getName().equals("_cmd")) {
1679       m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes '{0}' "
1680                             "after 'self' argument (should take '_cmd')",
1681                             iter->getName());
1682 
1683       return false;
1684     }
1685 
1686     ++iter;
1687 
1688     if (iter == llvm_function.arg_end()) {
1689       m_error_stream.Printf("Internal error [IRForTarget]: Wrapper takes only "
1690                             "'self' and '_cmd' arguments (should take a struct "
1691                             "pointer too)");
1692 
1693       return false;
1694     }
1695 
1696     argument = &*iter;
1697   }
1698 
1699   if (!argument->getName().equals("$__lldb_arg")) {
1700     m_error_stream.Format("Internal error [IRForTarget]: Wrapper takes an "
1701                           "argument named '{0}' instead of the struct pointer",
1702                           argument->getName());
1703 
1704     return false;
1705   }
1706 
1707   LLDB_LOG(log, "Arg: \"{0}\"", PrintValue(argument));
1708 
1709   BasicBlock &entry_block(llvm_function.getEntryBlock());
1710   Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
1711 
1712   if (!FirstEntryInstruction) {
1713     m_error_stream.Printf("Internal error [IRForTarget]: Couldn't find the "
1714                           "first instruction in the wrapper for use in "
1715                           "rewriting");
1716 
1717     return false;
1718   }
1719 
1720   LLVMContext &context(m_module->getContext());
1721   IntegerType *offset_type(Type::getInt32Ty(context));
1722 
1723   if (!offset_type) {
1724     m_error_stream.Printf(
1725         "Internal error [IRForTarget]: Couldn't produce an offset type");
1726 
1727     return false;
1728   }
1729 
1730   for (element_index = 0; element_index < num_elements; ++element_index) {
1731     const clang::NamedDecl *decl = nullptr;
1732     Value *value = nullptr;
1733     lldb::offset_t offset;
1734     lldb_private::ConstString name;
1735 
1736     if (!m_decl_map->GetStructElement(decl, value, offset, name,
1737                                       element_index)) {
1738       m_error_stream.Printf(
1739           "Internal error [IRForTarget]: Structure information is incomplete");
1740 
1741       return false;
1742     }
1743 
1744     LLDB_LOG(log, "  \"{0}\" (\"{1}\") placed at {2}", name,
1745              decl->getNameAsString(), offset);
1746 
1747     if (value) {
1748       LLDB_LOG(log, "    Replacing [{0}]", PrintValue(value));
1749 
1750       FunctionValueCache body_result_maker(
1751           [this, name, offset_type, offset, argument,
1752            value](llvm::Function *function) -> llvm::Value * {
1753             // Per the comment at ASTResultSynthesizer::SynthesizeBodyResult,
1754             // in cases where the result variable is an rvalue, we have to
1755             // synthesize a dereference of the appropriate structure entry in
1756             // order to produce the static variable that the AST thinks it is
1757             // accessing.
1758 
1759             llvm::Instruction *entry_instruction = llvm::cast<Instruction>(
1760                 m_entry_instruction_finder.GetValue(function));
1761 
1762             ConstantInt *offset_int(
1763                 ConstantInt::get(offset_type, offset, true));
1764             GetElementPtrInst *get_element_ptr = GetElementPtrInst::Create(
1765                 argument->getType()->getPointerElementType(), argument,
1766                 offset_int, "", entry_instruction);
1767 
1768             if (name == m_result_name && !m_result_is_pointer) {
1769               BitCastInst *bit_cast = new BitCastInst(
1770                   get_element_ptr, value->getType()->getPointerTo(), "",
1771                   entry_instruction);
1772 
1773               LoadInst *load =
1774                   new LoadInst(bit_cast->getType()->getPointerElementType(),
1775                                bit_cast, "", entry_instruction);
1776 
1777               return load;
1778             } else {
1779               BitCastInst *bit_cast = new BitCastInst(
1780                   get_element_ptr, value->getType(), "", entry_instruction);
1781 
1782               return bit_cast;
1783             }
1784           });
1785 
1786       if (Constant *constant = dyn_cast<Constant>(value)) {
1787         if (!UnfoldConstant(constant, &llvm_function, body_result_maker,
1788                             m_entry_instruction_finder, m_error_stream)) {
1789           return false;
1790         }
1791       } else if (Instruction *instruction = dyn_cast<Instruction>(value)) {
1792         if (instruction->getParent()->getParent() != &llvm_function) {
1793           m_error_stream.PutCString("error: Capturing non-local variables in "
1794                                     "expressions is unsupported.\n");
1795           return false;
1796         }
1797         value->replaceAllUsesWith(
1798             body_result_maker.GetValue(instruction->getParent()->getParent()));
1799       } else {
1800         LLDB_LOG(log, "Unhandled non-constant type: \"{0}\"",
1801                  PrintValue(value));
1802         return false;
1803       }
1804 
1805       if (GlobalVariable *var = dyn_cast<GlobalVariable>(value))
1806         var->eraseFromParent();
1807     }
1808   }
1809 
1810   LLDB_LOG(log, "Total structure [align {0}, size {1}]", (int64_t)alignment,
1811            (uint64_t)size);
1812 
1813   return true;
1814 }
1815 
1816 bool IRForTarget::runOnModule(Module &llvm_module) {
1817   lldb_private::Log *log(GetLog(LLDBLog::Expressions));
1818 
1819   m_module = &llvm_module;
1820   m_target_data = std::make_unique<DataLayout>(m_module);
1821   m_intptr_ty = llvm::Type::getIntNTy(m_module->getContext(),
1822                                       m_target_data->getPointerSizeInBits());
1823 
1824   if (log) {
1825     std::string s;
1826     raw_string_ostream oss(s);
1827 
1828     m_module->print(oss, nullptr);
1829 
1830     oss.flush();
1831 
1832     LLDB_LOG(log, "Module as passed in to IRForTarget: \n\"{0}\"", s);
1833   }
1834 
1835   Function *const main_function =
1836       m_func_name.IsEmpty() ? nullptr
1837                             : m_module->getFunction(m_func_name.GetStringRef());
1838 
1839   if (!m_func_name.IsEmpty() && !main_function) {
1840     LLDB_LOG(log, "Couldn't find \"{0}()\" in the module", m_func_name);
1841 
1842     m_error_stream.Format("Internal error [IRForTarget]: Couldn't find wrapper "
1843                           "'{0}' in the module",
1844                           m_func_name);
1845 
1846     return false;
1847   }
1848 
1849   if (main_function) {
1850     if (!FixFunctionLinkage(*main_function)) {
1851       LLDB_LOG(log, "Couldn't fix the linkage for the function");
1852 
1853       return false;
1854     }
1855   }
1856 
1857   llvm::Type *int8_ty = Type::getInt8Ty(m_module->getContext());
1858 
1859   m_reloc_placeholder = new llvm::GlobalVariable(
1860       (*m_module), int8_ty, false /* IsConstant */,
1861       GlobalVariable::InternalLinkage, Constant::getNullValue(int8_ty),
1862       "reloc_placeholder", nullptr /* InsertBefore */,
1863       GlobalVariable::NotThreadLocal /* ThreadLocal */, 0 /* AddressSpace */);
1864 
1865   ////////////////////////////////////////////////////////////
1866   // Replace $__lldb_expr_result with a persistent variable
1867   //
1868 
1869   if (main_function) {
1870     if (!CreateResultVariable(*main_function)) {
1871       LLDB_LOG(log, "CreateResultVariable() failed");
1872 
1873       // CreateResultVariable() reports its own errors, so we don't do so here
1874 
1875       return false;
1876     }
1877   }
1878 
1879   if (log && log->GetVerbose()) {
1880     std::string s;
1881     raw_string_ostream oss(s);
1882 
1883     m_module->print(oss, nullptr);
1884 
1885     oss.flush();
1886 
1887     LLDB_LOG(log, "Module after creating the result variable: \n\"{0}\"", s);
1888   }
1889 
1890   for (llvm::Function &function : *m_module) {
1891     for (BasicBlock &bb : function) {
1892       if (!RemoveGuards(bb)) {
1893         LLDB_LOG(log, "RemoveGuards() failed");
1894 
1895         // RemoveGuards() reports its own errors, so we don't do so here
1896 
1897         return false;
1898       }
1899 
1900       if (!RewritePersistentAllocs(bb)) {
1901         LLDB_LOG(log, "RewritePersistentAllocs() failed");
1902 
1903         // RewritePersistentAllocs() reports its own errors, so we don't do so
1904         // here
1905 
1906         return false;
1907       }
1908 
1909       if (!RemoveCXAAtExit(bb)) {
1910         LLDB_LOG(log, "RemoveCXAAtExit() failed");
1911 
1912         // RemoveCXAAtExit() reports its own errors, so we don't do so here
1913 
1914         return false;
1915       }
1916     }
1917   }
1918 
1919   ///////////////////////////////////////////////////////////////////////////////
1920   // Fix all Objective-C constant strings to use NSStringWithCString:encoding:
1921   //
1922 
1923   if (!RewriteObjCConstStrings()) {
1924     LLDB_LOG(log, "RewriteObjCConstStrings() failed");
1925 
1926     // RewriteObjCConstStrings() reports its own errors, so we don't do so here
1927 
1928     return false;
1929   }
1930 
1931   for (llvm::Function &function : *m_module) {
1932     for (llvm::BasicBlock &bb : function) {
1933       if (!RewriteObjCSelectors(bb)) {
1934         LLDB_LOG(log, "RewriteObjCSelectors() failed");
1935 
1936         // RewriteObjCSelectors() reports its own errors, so we don't do so
1937         // here
1938 
1939         return false;
1940       }
1941 
1942       if (!RewriteObjCClassReferences(bb)) {
1943         LLDB_LOG(log, "RewriteObjCClassReferences() failed");
1944 
1945         // RewriteObjCClasses() reports its own errors, so we don't do so here
1946 
1947         return false;
1948       }
1949     }
1950   }
1951 
1952   for (llvm::Function &function : *m_module) {
1953     for (BasicBlock &bb : function) {
1954       if (!ResolveCalls(bb)) {
1955         LLDB_LOG(log, "ResolveCalls() failed");
1956 
1957         // ResolveCalls() reports its own errors, so we don't do so here
1958 
1959         return false;
1960       }
1961     }
1962   }
1963 
1964   ////////////////////////////////////////////////////////////////////////
1965   // Run function-level passes that only make sense on the main function
1966   //
1967 
1968   if (main_function) {
1969     if (!ResolveExternals(*main_function)) {
1970       LLDB_LOG(log, "ResolveExternals() failed");
1971 
1972       // ResolveExternals() reports its own errors, so we don't do so here
1973 
1974       return false;
1975     }
1976 
1977     if (!ReplaceVariables(*main_function)) {
1978       LLDB_LOG(log, "ReplaceVariables() failed");
1979 
1980       // ReplaceVariables() reports its own errors, so we don't do so here
1981 
1982       return false;
1983     }
1984   }
1985 
1986   if (log && log->GetVerbose()) {
1987     std::string s;
1988     raw_string_ostream oss(s);
1989 
1990     m_module->print(oss, nullptr);
1991 
1992     oss.flush();
1993 
1994     LLDB_LOG(log, "Module after preparing for execution: \n\"{0}\"", s);
1995   }
1996 
1997   return true;
1998 }
1999