Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- ABI.cpp -----------------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Target/ABI.h" |
| 10 | #include "lldb/Core/PluginManager.h" |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Value.h" |
| 12 | #include "lldb/Core/ValueObjectConstResult.h" |
Alex Langford | 4dc0acc | 2019-05-31 20:17:21 +0000 | [diff] [blame] | 13 | #include "lldb/Expression/ExpressionVariable.h" |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/CompilerType.h" |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/TypeSystem.h" |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Target.h" |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Thread.h" |
Pavel Labath | d0b44db | 2019-09-25 13:03:04 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
| 19 | #include "llvm/Support/TargetRegistry.h" |
Jonas Devlieghere | 8f95a82 | 2020-02-17 09:06:18 -0800 | [diff] [blame] | 20 | #include <cctype> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
Greg Clayton | 31f1d2f | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 25 | ABISP |
Jason Molenda | 43294c9 | 2017-06-29 02:57:03 +0000 | [diff] [blame] | 26 | ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | ABISP abi_sp; |
| 28 | ABICreateInstance create_callback; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | for (uint32_t idx = 0; |
| 31 | (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) != |
| 32 | nullptr; |
| 33 | ++idx) { |
Jason Molenda | 43294c9 | 2017-06-29 02:57:03 +0000 | [diff] [blame] | 34 | abi_sp = create_callback(process_sp, arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | if (abi_sp) |
| 37 | return abi_sp; |
| 38 | } |
| 39 | abi_sp.reset(); |
| 40 | return abi_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 43 | ABI::~ABI() = default; |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 44 | |
Raphael Isemann | 24e0757 | 2020-10-13 17:10:10 +0200 | [diff] [blame] | 45 | bool RegInfoBasedABI::GetRegisterInfoByName(llvm::StringRef name, |
| 46 | RegisterInfo &info) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | uint32_t count = 0; |
| 48 | const RegisterInfo *register_info_array = GetRegisterInfoArray(count); |
| 49 | if (register_info_array) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | uint32_t i; |
| 51 | for (i = 0; i < count; ++i) { |
Raphael Isemann | b0060c3 | 2020-02-19 11:27:10 +0100 | [diff] [blame] | 52 | const char *reg_name = register_info_array[i].name; |
Raphael Isemann | 24e0757 | 2020-10-13 17:10:10 +0200 | [diff] [blame] | 53 | if (reg_name == name) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | info = register_info_array[i]; |
| 55 | return true; |
| 56 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 57 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | for (i = 0; i < count; ++i) { |
Raphael Isemann | b0060c3 | 2020-02-19 11:27:10 +0100 | [diff] [blame] | 59 | const char *reg_alt_name = register_info_array[i].alt_name; |
Raphael Isemann | 24e0757 | 2020-10-13 17:10:10 +0200 | [diff] [blame] | 60 | if (reg_alt_name == name) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | info = register_info_array[i]; |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type, |
| 70 | bool persistent) const { |
| 71 | if (!ast_type.IsValid()) |
| 72 | return ValueObjectSP(); |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | ValueObjectSP return_valobj_sp; |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type); |
| 77 | if (!return_valobj_sp) |
| 78 | return return_valobj_sp; |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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. |
Jim Ingham | ef65160 | 2011-12-22 19:12:40 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | if (persistent) { |
Adrian Prantl | 5435f78 | 2018-04-30 23:59:15 +0000 | [diff] [blame] | 87 | Target &target = *thread.CalculateTarget(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | PersistentExpressionState *persistent_expression_state = |
Adrian Prantl | 5435f78 | 2018-04-30 23:59:15 +0000 | [diff] [blame] | 89 | target.GetPersistentExpressionStateForLanguage( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | ast_type.GetMinimumLanguage()); |
| 91 | |
| 92 | if (!persistent_expression_state) |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 93 | return {}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | |
Adrian Prantl | 03219f7 | 2018-04-30 23:59:17 +0000 | [diff] [blame] | 95 | ConstString persistent_variable_name = |
Jim Ingham | 67d67eb | 2020-03-20 14:59:54 -0700 | [diff] [blame] | 96 | persistent_expression_state->GetNextPersistentVariableName(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | |
Alex Langford | e574f8b | 2019-07-17 07:03:17 +0000 | [diff] [blame] | 112 | ExpressionVariableSP expr_variable_sp( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | persistent_expression_state->CreatePersistentVariable( |
| 114 | return_valobj_sp)); |
| 115 | |
Alex Langford | e574f8b | 2019-07-17 07:03:17 +0000 | [diff] [blame] | 116 | assert(expr_variable_sp); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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: |
Alex Langford | e574f8b | 2019-07-17 07:03:17 +0000 | [diff] [blame] | 128 | expr_variable_sp->m_flags |= |
Alex Langford | 4dc0acc | 2019-05-31 20:17:21 +0000 | [diff] [blame] | 129 | ExpressionVariable::EVIsFreezeDried; |
Alex Langford | e574f8b | 2019-07-17 07:03:17 +0000 | [diff] [blame] | 130 | expr_variable_sp->m_flags |= |
Alex Langford | 4dc0acc | 2019-05-31 20:17:21 +0000 | [diff] [blame] | 131 | ExpressionVariable::EVIsLLDBAllocated; |
Alex Langford | e574f8b | 2019-07-17 07:03:17 +0000 | [diff] [blame] | 132 | expr_variable_sp->m_flags |= |
Alex Langford | 4dc0acc | 2019-05-31 20:17:21 +0000 | [diff] [blame] | 133 | ExpressionVariable::EVNeedsAllocation; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | break; |
| 135 | case Value::eValueTypeLoadAddress: |
Alex Langford | e574f8b | 2019-07-17 07:03:17 +0000 | [diff] [blame] | 136 | expr_variable_sp->m_live_sp = live_valobj_sp; |
| 137 | expr_variable_sp->m_flags |= |
Alex Langford | 4dc0acc | 2019-05-31 20:17:21 +0000 | [diff] [blame] | 138 | ExpressionVariable::EVIsProgramReference; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | break; |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 140 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | |
Alex Langford | e574f8b | 2019-07-17 07:03:17 +0000 | [diff] [blame] | 142 | return_valobj_sp = expr_variable_sp->GetValueObject(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | } |
| 144 | return return_valobj_sp; |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | ValueObjectSP ABI::GetReturnValueObject(Thread &thread, llvm::Type &ast_type, |
| 148 | bool persistent) const { |
| 149 | ValueObjectSP return_valobj_sp; |
| 150 | return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type); |
| 151 | return return_valobj_sp; |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 152 | } |
Jim Ingham | 73ca05a | 2011-12-17 01:35:57 +0000 | [diff] [blame] | 153 | |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 154 | // specialized to work with llvm IR types |
| 155 | // |
| 156 | // for now we will specify a default implementation so that we don't need to |
| 157 | // modify other ABIs |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread, |
| 159 | llvm::Type &ir_type) const { |
| 160 | ValueObjectSP return_valobj_sp; |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | /* this is a dummy and will only be called if an ABI does not override this */ |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | return return_valobj_sp; |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | bool ABI::PrepareTrivialCall(Thread &thread, lldb::addr_t sp, |
| 168 | lldb::addr_t functionAddress, |
| 169 | lldb::addr_t returnAddress, llvm::Type &returntype, |
| 170 | llvm::ArrayRef<ABI::CallArgument> args) const { |
| 171 | // dummy prepare trivial call |
David Blaikie | a322f36 | 2017-01-06 00:38:06 +0000 | [diff] [blame] | 172 | llvm_unreachable("Should never get here!"); |
Deepak Panickal | 2526ee5 | 2014-07-21 17:21:01 +0000 | [diff] [blame] | 173 | } |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | bool ABI::GetFallbackRegisterLocation( |
| 176 | const RegisterInfo *reg_info, |
| 177 | UnwindPlan::Row::RegisterLocation &unwind_regloc) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 178 | // Did the UnwindPlan fail to give us the caller's stack pointer? The stack |
| 179 | // pointer is defined to be the same as THIS frame's CFA, so return the CFA |
| 180 | // value as the caller's stack pointer. This is true on x86-32/x86-64 at |
| 181 | // least. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) { |
| 183 | unwind_regloc.SetIsCFAPlusOffset(0); |
| 184 | return true; |
| 185 | } |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | // If a volatile register is being requested, we don't want to forward the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 188 | // next frame's register contents up the stack -- the register is not |
| 189 | // retrievable at this frame. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | if (RegisterIsVolatile(reg_info)) { |
| 191 | unwind_regloc.SetUndefined(); |
| 192 | return true; |
| 193 | } |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | return false; |
Ulrich Weigand | 7311bb3 | 2016-04-14 14:25:20 +0000 | [diff] [blame] | 196 | } |
Pavel Labath | d0b44db | 2019-09-25 13:03:04 +0000 | [diff] [blame] | 197 | |
| 198 | std::unique_ptr<llvm::MCRegisterInfo> ABI::MakeMCRegisterInfo(const ArchSpec &arch) { |
| 199 | std::string triple = arch.GetTriple().getTriple(); |
| 200 | std::string lookup_error; |
| 201 | const llvm::Target *target = |
| 202 | llvm::TargetRegistry::lookupTarget(triple, lookup_error); |
| 203 | if (!target) { |
| 204 | LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS), |
| 205 | "Failed to create an llvm target for {0}: {1}", triple, |
| 206 | lookup_error); |
| 207 | return nullptr; |
| 208 | } |
| 209 | std::unique_ptr<llvm::MCRegisterInfo> info_up( |
| 210 | target->createMCRegInfo(triple)); |
| 211 | assert(info_up); |
| 212 | return info_up; |
| 213 | } |
Pavel Labath | 2b8db38 | 2019-12-03 11:39:20 +0100 | [diff] [blame] | 214 | |
Pavel Labath | 12e32d3 | 2020-02-02 10:27:40 +0100 | [diff] [blame] | 215 | void RegInfoBasedABI::AugmentRegisterInfo(RegisterInfo &info) { |
Pavel Labath | 2b8db38 | 2019-12-03 11:39:20 +0100 | [diff] [blame] | 216 | if (info.kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM && |
| 217 | info.kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM) |
| 218 | return; |
| 219 | |
| 220 | RegisterInfo abi_info; |
Raphael Isemann | 24e0757 | 2020-10-13 17:10:10 +0200 | [diff] [blame] | 221 | if (!GetRegisterInfoByName(info.name, abi_info)) |
Pavel Labath | 2b8db38 | 2019-12-03 11:39:20 +0100 | [diff] [blame] | 222 | return; |
| 223 | |
| 224 | if (info.kinds[eRegisterKindEHFrame] == LLDB_INVALID_REGNUM) |
| 225 | info.kinds[eRegisterKindEHFrame] = abi_info.kinds[eRegisterKindEHFrame]; |
| 226 | if (info.kinds[eRegisterKindDWARF] == LLDB_INVALID_REGNUM) |
| 227 | info.kinds[eRegisterKindDWARF] = abi_info.kinds[eRegisterKindDWARF]; |
| 228 | if (info.kinds[eRegisterKindGeneric] == LLDB_INVALID_REGNUM) |
| 229 | info.kinds[eRegisterKindGeneric] = abi_info.kinds[eRegisterKindGeneric]; |
| 230 | } |
Pavel Labath | 07355c1 | 2020-02-03 17:58:37 +0100 | [diff] [blame] | 231 | |
| 232 | void MCBasedABI::AugmentRegisterInfo(RegisterInfo &info) { |
| 233 | uint32_t eh, dwarf; |
| 234 | std::tie(eh, dwarf) = GetEHAndDWARFNums(info.name); |
| 235 | |
| 236 | if (info.kinds[eRegisterKindEHFrame] == LLDB_INVALID_REGNUM) |
| 237 | info.kinds[eRegisterKindEHFrame] = eh; |
| 238 | if (info.kinds[eRegisterKindDWARF] == LLDB_INVALID_REGNUM) |
| 239 | info.kinds[eRegisterKindDWARF] = dwarf; |
| 240 | if (info.kinds[eRegisterKindGeneric] == LLDB_INVALID_REGNUM) |
| 241 | info.kinds[eRegisterKindGeneric] = GetGenericNum(info.name); |
| 242 | } |
| 243 | |
| 244 | std::pair<uint32_t, uint32_t> |
| 245 | MCBasedABI::GetEHAndDWARFNums(llvm::StringRef name) { |
| 246 | std::string mc_name = GetMCName(name.str()); |
Pavel Labath | b2d64b6 | 2020-02-17 14:12:29 +0100 | [diff] [blame] | 247 | for (char &c : mc_name) |
| 248 | c = std::toupper(c); |
Pavel Labath | 07355c1 | 2020-02-03 17:58:37 +0100 | [diff] [blame] | 249 | int eh = -1; |
| 250 | int dwarf = -1; |
| 251 | for (unsigned reg = 0; reg < m_mc_register_info_up->getNumRegs(); ++reg) { |
| 252 | if (m_mc_register_info_up->getName(reg) == mc_name) { |
| 253 | eh = m_mc_register_info_up->getDwarfRegNum(reg, /*isEH=*/true); |
| 254 | dwarf = m_mc_register_info_up->getDwarfRegNum(reg, /*isEH=*/false); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | return std::pair<uint32_t, uint32_t>(eh == -1 ? LLDB_INVALID_REGNUM : eh, |
| 259 | dwarf == -1 ? LLDB_INVALID_REGNUM |
| 260 | : dwarf); |
| 261 | } |
| 262 | |
| 263 | void MCBasedABI::MapRegisterName(std::string &name, llvm::StringRef from_prefix, |
| 264 | llvm::StringRef to_prefix) { |
| 265 | llvm::StringRef name_ref = name; |
| 266 | if (!name_ref.consume_front(from_prefix)) |
| 267 | return; |
| 268 | uint64_t _; |
| 269 | if (name_ref.empty() || to_integer(name_ref, _, 10)) |
| 270 | name = (to_prefix + name_ref).str(); |
| 271 | } |