1 //===-- ABI.cpp -------------------------------------------------*- C++ -*-===//
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
21 using namespace lldb;
22 using namespace lldb_private;
23
24 ABISP
FindPlugin(lldb::ProcessSP process_sp,const ArchSpec & arch)25 ABI::FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch) {
26 ABISP abi_sp;
27 ABICreateInstance create_callback;
28
29 for (uint32_t idx = 0;
30 (create_callback = PluginManager::GetABICreateCallbackAtIndex(idx)) !=
31 nullptr;
32 ++idx) {
33 abi_sp = create_callback(process_sp, arch);
34
35 if (abi_sp)
36 return abi_sp;
37 }
38 abi_sp.reset();
39 return abi_sp;
40 }
41
42 ABI::~ABI() = default;
43
GetRegisterInfoByName(ConstString name,RegisterInfo & info)44 bool ABI::GetRegisterInfoByName(ConstString name, RegisterInfo &info) {
45 uint32_t count = 0;
46 const RegisterInfo *register_info_array = GetRegisterInfoArray(count);
47 if (register_info_array) {
48 const char *unique_name_cstr = name.GetCString();
49 uint32_t i;
50 for (i = 0; i < count; ++i) {
51 if (register_info_array[i].name == unique_name_cstr) {
52 info = register_info_array[i];
53 return true;
54 }
55 }
56 for (i = 0; i < count; ++i) {
57 if (register_info_array[i].alt_name == unique_name_cstr) {
58 info = register_info_array[i];
59 return true;
60 }
61 }
62 }
63 return false;
64 }
65
GetReturnValueObject(Thread & thread,CompilerType & ast_type,bool persistent) const66 ValueObjectSP ABI::GetReturnValueObject(Thread &thread, CompilerType &ast_type,
67 bool persistent) const {
68 if (!ast_type.IsValid())
69 return ValueObjectSP();
70
71 ValueObjectSP return_valobj_sp;
72
73 return_valobj_sp = GetReturnValueObjectImpl(thread, ast_type);
74 if (!return_valobj_sp)
75 return return_valobj_sp;
76
77 // Now turn this into a persistent variable.
78 // FIXME: This code is duplicated from Target::EvaluateExpression, and it is
79 // used in similar form in a couple
80 // of other places. Figure out the correct Create function to do all this
81 // work.
82
83 if (persistent) {
84 Target &target = *thread.CalculateTarget();
85 PersistentExpressionState *persistent_expression_state =
86 target.GetPersistentExpressionStateForLanguage(
87 ast_type.GetMinimumLanguage());
88
89 if (!persistent_expression_state)
90 return {};
91
92 auto prefix = persistent_expression_state->GetPersistentVariablePrefix();
93 ConstString persistent_variable_name =
94 persistent_expression_state->GetNextPersistentVariableName(target,
95 prefix);
96
97 lldb::ValueObjectSP const_valobj_sp;
98
99 // Check in case our value is already a constant value
100 if (return_valobj_sp->GetIsConstant()) {
101 const_valobj_sp = return_valobj_sp;
102 const_valobj_sp->SetName(persistent_variable_name);
103 } else
104 const_valobj_sp =
105 return_valobj_sp->CreateConstantValue(persistent_variable_name);
106
107 lldb::ValueObjectSP live_valobj_sp = return_valobj_sp;
108
109 return_valobj_sp = const_valobj_sp;
110
111 ExpressionVariableSP expr_variable_sp(
112 persistent_expression_state->CreatePersistentVariable(
113 return_valobj_sp));
114
115 assert(expr_variable_sp);
116
117 // Set flags and live data as appropriate
118
119 const Value &result_value = live_valobj_sp->GetValue();
120
121 switch (result_value.GetValueType()) {
122 case Value::eValueTypeHostAddress:
123 case Value::eValueTypeFileAddress:
124 // we don't do anything with these for now
125 break;
126 case Value::eValueTypeScalar:
127 case Value::eValueTypeVector:
128 expr_variable_sp->m_flags |=
129 ExpressionVariable::EVIsFreezeDried;
130 expr_variable_sp->m_flags |=
131 ExpressionVariable::EVIsLLDBAllocated;
132 expr_variable_sp->m_flags |=
133 ExpressionVariable::EVNeedsAllocation;
134 break;
135 case Value::eValueTypeLoadAddress:
136 expr_variable_sp->m_live_sp = live_valobj_sp;
137 expr_variable_sp->m_flags |=
138 ExpressionVariable::EVIsProgramReference;
139 break;
140 }
141
142 return_valobj_sp = expr_variable_sp->GetValueObject();
143 }
144 return return_valobj_sp;
145 }
146
GetReturnValueObject(Thread & thread,llvm::Type & ast_type,bool persistent) const147 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;
152 }
153
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
GetReturnValueObjectImpl(Thread & thread,llvm::Type & ir_type) const158 lldb::ValueObjectSP ABI::GetReturnValueObjectImpl(Thread &thread,
159 llvm::Type &ir_type) const {
160 ValueObjectSP return_valobj_sp;
161
162 /* this is a dummy and will only be called if an ABI does not override this */
163
164 return return_valobj_sp;
165 }
166
PrepareTrivialCall(Thread & thread,lldb::addr_t sp,lldb::addr_t functionAddress,lldb::addr_t returnAddress,llvm::Type & returntype,llvm::ArrayRef<ABI::CallArgument> args) const167 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
172 llvm_unreachable("Should never get here!");
173 }
174
GetFallbackRegisterLocation(const RegisterInfo * reg_info,UnwindPlan::Row::RegisterLocation & unwind_regloc)175 bool ABI::GetFallbackRegisterLocation(
176 const RegisterInfo *reg_info,
177 UnwindPlan::Row::RegisterLocation &unwind_regloc) {
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.
182 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_SP) {
183 unwind_regloc.SetIsCFAPlusOffset(0);
184 return true;
185 }
186
187 // If a volatile register is being requested, we don't want to forward the
188 // next frame's register contents up the stack -- the register is not
189 // retrievable at this frame.
190 if (RegisterIsVolatile(reg_info)) {
191 unwind_regloc.SetUndefined();
192 return true;
193 }
194
195 return false;
196 }
197
MakeMCRegisterInfo(const ArchSpec & arch)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 }
214
AugmentRegisterInfo(RegisterInfo & info)215 void ABI::AugmentRegisterInfo(RegisterInfo &info) {
216 if (info.kinds[eRegisterKindEHFrame] != LLDB_INVALID_REGNUM &&
217 info.kinds[eRegisterKindDWARF] != LLDB_INVALID_REGNUM)
218 return;
219
220 RegisterInfo abi_info;
221 if (!GetRegisterInfoByName(ConstString(info.name), abi_info))
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 }
231