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