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