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