1 //===-- ABI.cpp -------------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Target/ABI.h" 10 #include "lldb/Core/PluginManager.h" 11 #include "lldb/Core/Value.h" 12 #include "lldb/Core/ValueObjectConstResult.h" 13 #include "lldb/Expression/ExpressionVariable.h" 14 #include "lldb/Symbol/CompilerType.h" 15 #include "lldb/Symbol/TypeSystem.h" 16 #include "lldb/Target/Target.h" 17 #include "lldb/Target/Thread.h" 18 #include "lldb/Utility/Log.h" 19 #include "llvm/Support/TargetRegistry.h" 20 21 using namespace lldb; 22 using namespace lldb_private; 23 24 ABISP 25 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) { 26 ABISP abi_sp; 27 ABICreateInstance create_callback; 28 29 for (uint32_t idx = 0; 30 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) != 31 nullptr; 32 ++idx) { 33 abi_sp = create_callback(process_sp, arch); 34 35 if (abi_sp) 36 return abi_sp; 37 } 38 abi_sp.reset(); 39 return abi_sp; 40 } 41 42 ABI::~ABI() = default; 43 44 bool ABI::GetRegisterInfoByName(ConstString name, RegisterInfo &info) { 45 uint32_t count = 0; 46 const RegisterInfo *register_info_array = GetRegisterInfoArray(count); 47 if (register_info_array) { 48 const char *unique_name_cstr = name.GetCString(); 49 uint32_t i; 50 for (i = 0; i < count; ++i) { 51 if (register_info_array[i].name == unique_name_cstr) { 52 info = register_info_array[i]; 53 return true; 54 } 55 } 56 for (i = 0; i < count; ++i) { 57 if (register_info_array[i].alt_name == unique_name_cstr) { 58 info = register_info_array[i]; 59 return true; 60 } 61 } 62 } 63 return false; 64 } 65 66 bool ABI::GetRegisterInfoByKind(RegisterKind reg_kind, uint32_t reg_num, 67 RegisterInfo &info) { 68 if (reg_kind < eRegisterKindEHFrame || reg_kind >= kNumRegisterKinds) 69 return false; 70 71 uint32_t count = 0; 72 const RegisterInfo *register_info_array = GetRegisterInfoArray(count); 73 if (register_info_array) { 74 for (uint32_t i = 0; i < count; ++i) { 75 if (register_info_array[i].kinds[reg_kind] == reg_num) { 76 info = register_info_array[i]; 77 return true; 78 } 79 } 80 } 81 return false; 82 } 83 84 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type, 85 bool persistent) const { 86 if (!ast_type.IsValid()) 87 return ValueObjectSP(); 88 89 ValueObjectSP return_valobj_sp; 90 91 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type); 92 if (!return_valobj_sp) 93 return return_valobj_sp; 94 95 // Now turn this into a persistent variable. 96 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is 97 // used in similar form in a couple 98 // of other places. Figure out the correct Create function to do all this 99 // work. 100 101 if (persistent) { 102 Target &target = *thread.CalculateTarget(); 103 PersistentExpressionState *persistent_expression_state = 104 target.GetPersistentExpressionStateForLanguage( 105 ast_type.GetMinimumLanguage()); 106 107 if (!persistent_expression_state) 108 return ValueObjectSP(); 109 110 auto prefix = persistent_expression_state->GetPersistentVariablePrefix(); 111 ConstString persistent_variable_name = 112 persistent_expression_state->GetNextPersistentVariableName(target, 113 prefix); 114 115 lldb::ValueObjectSP const_valobj_sp; 116 117 // Check in case our value is already a constant value 118 if (return_valobj_sp->GetIsConstant()) { 119 const_valobj_sp = return_valobj_sp; 120 const_valobj_sp->SetName(persistent_variable_name); 121 } else 122 const_valobj_sp = 123 return_valobj_sp->CreateConstantValue(persistent_variable_name); 124 125 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp; 126 127 return_valobj_sp = const_valobj_sp; 128 129 ExpressionVariableSP expr_variable_sp( 130 persistent_expression_state->CreatePersistentVariable( 131 return_valobj_sp)); 132 133 assert(expr_variable_sp); 134 135 // Set flags and live data as appropriate 136 137 const Value &result_value = live_valobj_sp->GetValue(); 138 139 switch (result_value.GetValueType()) { 140 case Value::eValueTypeHostAddress: 141 case Value::eValueTypeFileAddress: 142 // we don't do anything with these for now 143 break; 144 case Value::eValueTypeScalar: 145 case Value::eValueTypeVector: 146 expr_variable_sp->m_flags |= 147 ExpressionVariable::EVIsFreezeDried; 148 expr_variable_sp->m_flags |= 149 ExpressionVariable::EVIsLLDBAllocated; 150 expr_variable_sp->m_flags |= 151 ExpressionVariable::EVNeedsAllocation; 152 break; 153 case Value::eValueTypeLoadAddress: 154 expr_variable_sp->m_live_sp = live_valobj_sp; 155 expr_variable_sp->m_flags |= 156 ExpressionVariable::EVIsProgramReference; 157 break; 158 } 159 160 return_valobj_sp = expr_variable_sp->GetValueObject(); 161 } 162 return return_valobj_sp; 163 } 164 165 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type, 166 bool persistent) const { 167 ValueObjectSP return_valobj_sp; 168 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type); 169 return return_valobj_sp; 170 } 171 172 // specialized to work with llvm IR types 173 // 174 // for now we will specify a default implementation so that we don't need to 175 // modify other ABIs 176 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread, 177 llvm::Type &ir_type) const { 178 ValueObjectSP return_valobj_sp; 179 180 /* this is a dummy and will only be called if an ABI does not override this */ 181 182 return return_valobj_sp; 183 } 184 185 bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp, 186 lldb::addr_t functionAddress, 187 lldb::addr_t returnAddress, llvm::Type &returntype, 188 llvm::ArrayRef<ABI::CallArgument> args) const { 189 // dummy prepare trivial call 190 llvm_unreachable("Should never get here!"); 191 } 192 193 bool ABI::GetFallbackRegisterLocation( 194 const RegisterInfo *reg_info, 195 UnwindPlan::Row::RegisterLocation &unwind_regloc) { 196 // Did the UnwindPlan fail to give us the caller's stack pointer? The stack 197 // pointer is defined to be the same as THIS frame's CFA, so return the CFA 198 // value as the caller's stack pointer. This is true on x86-32/x86-64 at 199 // least. 200 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) { 201 unwind_regloc.SetIsCFAPlusOffset(0); 202 return true; 203 } 204 205 // If a volatile register is being requested, we don't want to forward the 206 // next frame's register contents up the stack -- the register is not 207 // retrievable at this frame. 208 if (RegisterIsVolatile(reg_info)) { 209 unwind_regloc.SetUndefined(); 210 return true; 211 } 212 213 return false; 214 } 215 216 std::unique_ptr<llvm::MCRegisterInfo> ABI::MakeMCRegisterInfo(const ArchSpec &arch) { 217 std::string triple = arch.GetTriple().getTriple(); 218 std::string lookup_error; 219 const llvm::Target *target = 220 llvm::TargetRegistry::lookupTarget(triple, lookup_error); 221 if (!target) { 222 LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS), 223 "Failed to create an llvm target for {0}: {1}", triple, 224 lookup_error); 225 return nullptr; 226 } 227 std::unique_ptr<llvm::MCRegisterInfo> info_up( 228 target->createMCRegInfo(triple)); 229 assert(info_up); 230 return info_up; 231 } 232