ABI.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef LLDB_TARGET_ABI_H
  9. #define LLDB_TARGET_ABI_H
  10. #include "lldb/Core/PluginInterface.h"
  11. #include "lldb/Symbol/UnwindPlan.h"
  12. #include "lldb/Utility/Status.h"
  13. #include "lldb/lldb-private.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/MC/MCRegisterInfo.h"
  16. namespace llvm {
  17. class Type;
  18. }
  19. namespace lldb_private {
  20. class ABI : public PluginInterface {
  21. public:
  22. struct CallArgument {
  23. enum eType {
  24. HostPointer = 0, /* pointer to host data */
  25. TargetValue, /* value is on the target or literal */
  26. };
  27. eType type; /* value of eType */
  28. size_t size; /* size in bytes of this argument */
  29. lldb::addr_t value; /* literal value */
  30. std::unique_ptr<uint8_t[]> data_up; /* host data pointer */
  31. };
  32. ~ABI() override;
  33. virtual size_t GetRedZoneSize() const = 0;
  34. virtual bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp,
  35. lldb::addr_t functionAddress,
  36. lldb::addr_t returnAddress,
  37. llvm::ArrayRef<lldb::addr_t> args) const = 0;
  38. // Prepare trivial call used from ThreadPlanFunctionCallUsingABI
  39. // AD:
  40. // . Because i don't want to change other ABI's this is not declared pure
  41. // virtual.
  42. // The dummy implementation will simply fail. Only HexagonABI will
  43. // currently
  44. // use this method.
  45. // . Two PrepareTrivialCall's is not good design so perhaps this should be
  46. // combined.
  47. //
  48. virtual bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp,
  49. lldb::addr_t functionAddress,
  50. lldb::addr_t returnAddress,
  51. llvm::Type &prototype,
  52. llvm::ArrayRef<CallArgument> args) const;
  53. virtual bool GetArgumentValues(Thread &thread, ValueList &values) const = 0;
  54. lldb::ValueObjectSP GetReturnValueObject(Thread &thread, CompilerType &type,
  55. bool persistent = true) const;
  56. // specialized to work with llvm IR types
  57. lldb::ValueObjectSP GetReturnValueObject(Thread &thread, llvm::Type &type,
  58. bool persistent = true) const;
  59. // Set the Return value object in the current frame as though a function with
  60. virtual Status SetReturnValueObject(lldb::StackFrameSP &frame_sp,
  61. lldb::ValueObjectSP &new_value) = 0;
  62. protected:
  63. // This is the method the ABI will call to actually calculate the return
  64. // value. Don't put it in a persistent value object, that will be done by the
  65. // ABI::GetReturnValueObject.
  66. virtual lldb::ValueObjectSP
  67. GetReturnValueObjectImpl(Thread &thread, CompilerType &ast_type) const = 0;
  68. // specialized to work with llvm IR types
  69. virtual lldb::ValueObjectSP
  70. GetReturnValueObjectImpl(Thread &thread, llvm::Type &ir_type) const;
  71. /// Request to get a Process shared pointer.
  72. ///
  73. /// This ABI object may not have been created with a Process object,
  74. /// or the Process object may no longer be alive. Be sure to handle
  75. /// the case where the shared pointer returned does not have an
  76. /// object inside it.
  77. lldb::ProcessSP GetProcessSP() const { return m_process_wp.lock(); }
  78. public:
  79. virtual bool CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) = 0;
  80. virtual bool CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) = 0;
  81. virtual bool RegisterIsVolatile(const RegisterInfo *reg_info) = 0;
  82. virtual bool
  83. GetFallbackRegisterLocation(const RegisterInfo *reg_info,
  84. UnwindPlan::Row::RegisterLocation &unwind_regloc);
  85. // Should take a look at a call frame address (CFA) which is just the stack
  86. // pointer value upon entry to a function. ABIs usually impose alignment
  87. // restrictions (4, 8 or 16 byte aligned), and zero is usually not allowed.
  88. // This function should return true if "cfa" is valid call frame address for
  89. // the ABI, and false otherwise. This is used by the generic stack frame
  90. // unwinding code to help determine when a stack ends.
  91. virtual bool CallFrameAddressIsValid(lldb::addr_t cfa) = 0;
  92. // Validates a possible PC value and returns true if an opcode can be at
  93. // "pc".
  94. virtual bool CodeAddressIsValid(lldb::addr_t pc) = 0;
  95. /// Some targets might use bits in a code address to indicate a mode switch.
  96. /// ARM uses bit zero to signify a code address is thumb, so any ARM ABI
  97. /// plug-ins would strip those bits.
  98. /// @{
  99. virtual lldb::addr_t FixCodeAddress(lldb::addr_t pc) { return pc; }
  100. virtual lldb::addr_t FixDataAddress(lldb::addr_t pc) { return pc; }
  101. /// @}
  102. llvm::MCRegisterInfo &GetMCRegisterInfo() { return *m_mc_register_info_up; }
  103. virtual void AugmentRegisterInfo(RegisterInfo &info) = 0;
  104. virtual bool GetPointerReturnRegister(const char *&name) { return false; }
  105. static lldb::ABISP FindPlugin(lldb::ProcessSP process_sp, const ArchSpec &arch);
  106. protected:
  107. ABI(lldb::ProcessSP process_sp, std::unique_ptr<llvm::MCRegisterInfo> info_up)
  108. : m_process_wp(process_sp), m_mc_register_info_up(std::move(info_up)) {
  109. assert(m_mc_register_info_up && "ABI must have MCRegisterInfo");
  110. }
  111. /// Utility function to construct a MCRegisterInfo using the ArchSpec triple.
  112. /// Plugins wishing to customize the construction can construct the
  113. /// MCRegisterInfo themselves.
  114. static std::unique_ptr<llvm::MCRegisterInfo>
  115. MakeMCRegisterInfo(const ArchSpec &arch);
  116. lldb::ProcessWP m_process_wp;
  117. std::unique_ptr<llvm::MCRegisterInfo> m_mc_register_info_up;
  118. virtual lldb::addr_t FixCodeAddress(lldb::addr_t pc, lldb::addr_t mask) {
  119. return pc;
  120. }
  121. private:
  122. ABI(const ABI &) = delete;
  123. const ABI &operator=(const ABI &) = delete;
  124. };
  125. class RegInfoBasedABI : public ABI {
  126. public:
  127. void AugmentRegisterInfo(RegisterInfo &info) override;
  128. protected:
  129. using ABI::ABI;
  130. bool GetRegisterInfoByName(llvm::StringRef name, RegisterInfo &info);
  131. virtual const RegisterInfo *GetRegisterInfoArray(uint32_t &count) = 0;
  132. };
  133. class MCBasedABI : public ABI {
  134. public:
  135. void AugmentRegisterInfo(RegisterInfo &info) override;
  136. /// If the register name is of the form "<from_prefix>[<number>]" then change
  137. /// the name to "<to_prefix>[<number>]". Otherwise, leave the name unchanged.
  138. static void MapRegisterName(std::string &reg, llvm::StringRef from_prefix,
  139. llvm::StringRef to_prefix);
  140. protected:
  141. using ABI::ABI;
  142. /// Return eh_frame and dwarf numbers for the given register.
  143. virtual std::pair<uint32_t, uint32_t> GetEHAndDWARFNums(llvm::StringRef reg);
  144. /// Return the generic number of the given register.
  145. virtual uint32_t GetGenericNum(llvm::StringRef reg) = 0;
  146. /// For the given (capitalized) lldb register name, return the name of this
  147. /// register in the MCRegisterInfo struct.
  148. virtual std::string GetMCName(std::string reg) { return reg; }
  149. };
  150. } // namespace lldb_private
  151. #endif // LLDB_TARGET_ABI_H