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