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