LLDB mainline
ABI.h
Go to the documentation of this file.
1//===-- ABI.h ---------------------------------------------------*- 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#ifndef LLDB_TARGET_ABI_H
10#define LLDB_TARGET_ABI_H
11
15#include "lldb/Utility/Status.h"
16#include "lldb/lldb-forward.h"
17#include "lldb/lldb-private.h"
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/MC/MCRegisterInfo.h"
21
22namespace llvm {
23class Type;
24}
25
26namespace lldb_private {
27
28class ABI : public PluginInterface {
29public:
30 struct CallArgument {
31 enum eType {
32 HostPointer = 0, /* pointer to host data */
33 TargetValue, /* value is on the target or literal */
34 };
35 eType type; /* value of eType */
36 size_t size; /* size in bytes of this argument */
37
38 lldb::addr_t value; /* literal value */
39 std::unique_ptr<uint8_t[]> data_up; /* host data pointer */
40 };
41
42 ~ABI() override;
43
44 virtual size_t GetRedZoneSize() const = 0;
45
47 lldb::addr_t functionAddress,
48 lldb::addr_t returnAddress,
49 llvm::ArrayRef<lldb::addr_t> args) const = 0;
50
51 // Prepare trivial call used from ThreadPlanFunctionCallUsingABI
52 // AD:
53 // . Because i don't want to change other ABI's this is not declared pure
54 // virtual.
55 // The dummy implementation will simply fail. Only HexagonABI will
56 // currently
57 // use this method.
58 // . Two PrepareTrivialCall's is not good design so perhaps this should be
59 // combined.
60 //
62 lldb::addr_t functionAddress,
63 lldb::addr_t returnAddress,
64 llvm::Type &prototype,
65 llvm::ArrayRef<CallArgument> args) const;
66
67 virtual bool GetArgumentValues(Thread &thread, ValueList &values) const = 0;
68
70 bool persistent = true) const;
71
72 // specialized to work with llvm IR types
73 lldb::ValueObjectSP GetReturnValueObject(Thread &thread, llvm::Type &type,
74 bool persistent = true) const;
75
76 // Set the Return value object in the current frame as though a function with
78 lldb::ValueObjectSP &new_value) = 0;
79
80protected:
81 // This is the method the ABI will call to actually calculate the return
82 // value. Don't put it in a persistent value object, that will be done by the
83 // ABI::GetReturnValueObject.
85 GetReturnValueObjectImpl(Thread &thread, CompilerType &ast_type) const = 0;
86
87 // specialized to work with llvm IR types
89 GetReturnValueObjectImpl(Thread &thread, llvm::Type &ir_type) const;
90
91 /// Request to get a Process shared pointer.
92 ///
93 /// This ABI object may not have been created with a Process object,
94 /// or the Process object may no longer be alive. Be sure to handle
95 /// the case where the shared pointer returned does not have an
96 /// object inside it.
97 lldb::ProcessSP GetProcessSP() const { return m_process_wp.lock(); }
98
99public:
101
103
104 virtual bool RegisterIsVolatile(const RegisterInfo *reg_info) = 0;
105
106 virtual bool GetFallbackRegisterLocation(
107 const RegisterInfo *reg_info,
109
110 // Should take a look at a call frame address (CFA) which is just the stack
111 // pointer value upon entry to a function. ABIs usually impose alignment
112 // restrictions (4, 8 or 16 byte aligned), and zero is usually not allowed.
113 // This function should return true if "cfa" is valid call frame address for
114 // the ABI, and false otherwise. This is used by the generic stack frame
115 // unwinding code to help determine when a stack ends.
117
118 // Validates a possible PC value and returns true if an opcode can be at
119 // "pc".
121
122 /// Some targets might use bits in a code address to indicate a mode switch.
123 /// ARM uses bit zero to signify a code address is thumb, so any ARM ABI
124 /// plug-ins would strip those bits.
125 /// @{
128 /// @}
129
130 /// Use this method when you do not know, or do not care what kind of address
131 /// you are fixing. On platforms where there would be a difference between the
132 /// two types, it will pick the safest option.
133 ///
134 /// Its purpose is to signal that no specific choice was made and provide an
135 /// alternative to randomly picking FixCode/FixData address. Which could break
136 /// platforms where there is a difference (only Arm Thumb at this time).
138 // On Arm Thumb fixing a code address zeroes the bottom bit, so FixData is
139 // the safe choice. On any other platform (so far) code and data addresses
140 // are fixed in the same way.
141 return FixDataAddress(pc);
142 }
143
144 llvm::MCRegisterInfo &GetMCRegisterInfo() { return *m_mc_register_info_up; }
145
146 virtual void
147 AugmentRegisterInfo(std::vector<DynamicRegisterInfo::Register> &regs) = 0;
148
149 virtual bool GetPointerReturnRegister(const char *&name) { return false; }
150
151 virtual uint64_t GetStackFrameSize() { return 512 * 1024; }
152
153 static lldb::ABISP FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch);
154
156 // Both of these are sets of lldb::Permissions values.
157 // Overlay are the permissions being applied to the original permissions.
158 uint32_t overlay;
159 // Effective is the result of applying the overlay to the original
160 // permissions. Calculating this is done by the plugin because some
161 // permission overlays are done as positive (add permissions) and some as
162 // negative (remove permissions).
163 uint32_t effective;
164 };
165
166 /// Get the effective memory permissions that result when the permissions
167 /// referred to by a protection key are applied to the original permissions.
168 ///
169 /// This is intended for architectures that have some sort of permission
170 /// overlay system. Where the protection key is used to look up a set of
171 /// permissions that modifies the original permissions.
172 ///
173 /// \returns the overlay permissions (that the protection key refers to) and
174 /// the effective permissions. If the target does not have an overlay
175 /// system, or it does and the protection key is invalid, returns nullopt.
176 virtual std::optional<MemoryPermissions>
178 unsigned protection_key, uint32_t original_permissions) {
179 return std::nullopt;
180 }
181
182protected:
183 ABI(lldb::ProcessSP process_sp, std::unique_ptr<llvm::MCRegisterInfo> info_up)
184 : m_process_wp(process_sp), m_mc_register_info_up(std::move(info_up)) {
185 assert(m_mc_register_info_up && "ABI must have MCRegisterInfo");
186 }
187
188 /// Utility function to construct a MCRegisterInfo using the ArchSpec triple.
189 /// Plugins wishing to customize the construction can construct the
190 /// MCRegisterInfo themselves.
191 static std::unique_ptr<llvm::MCRegisterInfo>
192 MakeMCRegisterInfo(const ArchSpec &arch);
193
195 std::unique_ptr<llvm::MCRegisterInfo> m_mc_register_info_up;
196
197private:
198 ABI(const ABI &) = delete;
199 const ABI &operator=(const ABI &) = delete;
200};
201
202class RegInfoBasedABI : public ABI {
203public:
205 std::vector<DynamicRegisterInfo::Register> &regs) override;
206
207protected:
208 using ABI::ABI;
209
210 bool GetRegisterInfoByName(llvm::StringRef name, RegisterInfo &info);
211
212 virtual const RegisterInfo *GetRegisterInfoArray(uint32_t &count) = 0;
213};
214
215class MCBasedABI : public ABI {
216public:
218 std::vector<DynamicRegisterInfo::Register> &regs) override;
219
220 /// If the register name is of the form "<from_prefix>[<number>]" then change
221 /// the name to "<to_prefix>[<number>]". Otherwise, leave the name unchanged.
222 static void MapRegisterName(std::string &reg, llvm::StringRef from_prefix,
223 llvm::StringRef to_prefix);
224
225protected:
226 using ABI::ABI;
227
228 /// Return eh_frame and dwarf numbers for the given register.
229 virtual std::pair<uint32_t, uint32_t> GetEHAndDWARFNums(llvm::StringRef reg);
230
231 /// Return the generic number of the given register.
232 virtual uint32_t GetGenericNum(llvm::StringRef reg) = 0;
233
234 /// For the given (capitalized) lldb register name, return the name of this
235 /// register in the MCRegisterInfo struct.
236 virtual std::string GetMCName(std::string reg) { return reg; }
237};
238
239} // namespace lldb_private
240
241#endif // LLDB_TARGET_ABI_H
virtual bool CodeAddressIsValid(lldb::addr_t pc)=0
virtual bool GetPointerReturnRegister(const char *&name)
Definition ABI.h:149
lldb::ProcessWP m_process_wp
Definition ABI.h:194
llvm::MCRegisterInfo & GetMCRegisterInfo()
Definition ABI.h:144
ABI(const ABI &)=delete
ABI(lldb::ProcessSP process_sp, std::unique_ptr< llvm::MCRegisterInfo > info_up)
Definition ABI.h:183
const ABI & operator=(const ABI &)=delete
virtual lldb::addr_t FixAnyAddress(lldb::addr_t pc)
Use this method when you do not know, or do not care what kind of address you are fixing.
Definition ABI.h:137
virtual lldb::UnwindPlanSP CreateDefaultUnwindPlan()=0
virtual bool CallFrameAddressIsValid(lldb::addr_t cfa)=0
virtual void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs)=0
virtual bool GetFallbackRegisterLocation(const RegisterInfo *reg_info, UnwindPlan::Row::AbstractRegisterLocation &unwind_regloc)
Definition ABI.cpp:202
virtual lldb::ValueObjectSP GetReturnValueObjectImpl(Thread &thread, CompilerType &ast_type) const =0
static lldb::ABISP FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch)
Definition ABI.cpp:27
virtual bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp, lldb::addr_t functionAddress, lldb::addr_t returnAddress, llvm::ArrayRef< lldb::addr_t > args) const =0
virtual bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp, lldb::addr_t functionAddress, lldb::addr_t returnAddress, llvm::Type &prototype, llvm::ArrayRef< CallArgument > args) const
virtual bool GetArgumentValues(Thread &thread, ValueList &values) const =0
lldb::ValueObjectSP GetReturnValueObject(Thread &thread, CompilerType &type, bool persistent=true) const
Definition ABI.cpp:61
static std::unique_ptr< llvm::MCRegisterInfo > MakeMCRegisterInfo(const ArchSpec &arch)
Utility function to construct a MCRegisterInfo using the ArchSpec triple.
Definition ABI.cpp:225
virtual lldb::UnwindPlanSP CreateFunctionEntryUnwindPlan()=0
virtual bool RegisterIsVolatile(const RegisterInfo *reg_info)=0
virtual size_t GetRedZoneSize() const =0
virtual std::optional< MemoryPermissions > GetMemoryPermissions(lldb_private::RegisterContext &reg_ctx, unsigned protection_key, uint32_t original_permissions)
Get the effective memory permissions that result when the permissions referred to by a protection key...
Definition ABI.h:177
std::unique_ptr< llvm::MCRegisterInfo > m_mc_register_info_up
Definition ABI.h:195
virtual lldb::addr_t FixCodeAddress(lldb::addr_t pc)
Some targets might use bits in a code address to indicate a mode switch.
Definition ABI.cpp:141
lldb::ProcessSP GetProcessSP() const
Request to get a Process shared pointer.
Definition ABI.h:97
virtual lldb::addr_t FixDataAddress(lldb::addr_t pc)
Definition ABI.cpp:158
virtual uint64_t GetStackFrameSize()
Definition ABI.h:151
virtual Status SetReturnValueObject(lldb::StackFrameSP &frame_sp, lldb::ValueObjectSP &new_value)=0
An architecture specification class.
Definition ArchSpec.h:32
Generic representation of a type in a programming language.
virtual std::string GetMCName(std::string reg)
For the given (capitalized) lldb register name, return the name of this register in the MCRegisterInf...
Definition ABI.h:236
void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs) override
Definition ABI.cpp:262
virtual uint32_t GetGenericNum(llvm::StringRef reg)=0
Return the generic number of the given register.
ABI(lldb::ProcessSP process_sp, std::unique_ptr< llvm::MCRegisterInfo > info_up)
Definition ABI.h:183
virtual std::pair< uint32_t, uint32_t > GetEHAndDWARFNums(llvm::StringRef reg)
Return eh_frame and dwarf numbers for the given register.
Definition ABI.cpp:278
static void MapRegisterName(std::string &reg, llvm::StringRef from_prefix, llvm::StringRef to_prefix)
If the register name is of the form "<from_prefix>[<number>]" then change the name to "<to_prefix>[<n...
Definition ABI.cpp:296
bool GetRegisterInfoByName(llvm::StringRef name, RegisterInfo &info)
Definition ABI.cpp:37
virtual const RegisterInfo * GetRegisterInfoArray(uint32_t &count)=0
ABI(lldb::ProcessSP process_sp, std::unique_ptr< llvm::MCRegisterInfo > info_up)
Definition ABI.h:183
void AugmentRegisterInfo(std::vector< DynamicRegisterInfo::Register > &regs) override
Definition ABI.cpp:242
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::weak_ptr< lldb_private::Process > ProcessWP
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
uint64_t addr_t
Definition lldb-types.h:80
std::unique_ptr< uint8_t[]> data_up
Definition ABI.h:39
Every register is described in detail including its name, alternate name (optional),...