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