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