loongarch-tdep.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* Target-dependent code for the LoongArch architecture, for GDB.
  2. Copyright (C) 2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "dwarf2/frame.h"
  17. #include "elf-bfd.h"
  18. #include "frame-unwind.h"
  19. #include "loongarch-tdep.h"
  20. #include "target-descriptions.h"
  21. #include "trad-frame.h"
  22. #include "user-regs.h"
  23. /* Implement the loongarch_skip_prologue gdbarch method. */
  24. static CORE_ADDR
  25. loongarch_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  26. {
  27. CORE_ADDR func_addr;
  28. /* See if we can determine the end of the prologue via the symbol table.
  29. If so, then return either PC, or the PC after the prologue, whichever
  30. is greater. */
  31. if (find_pc_partial_function (pc, nullptr, &func_addr, nullptr))
  32. {
  33. CORE_ADDR post_prologue_pc
  34. = skip_prologue_using_sal (gdbarch, func_addr);
  35. if (post_prologue_pc != 0)
  36. return std::max (pc, post_prologue_pc);
  37. }
  38. return 0;
  39. }
  40. /* Adjust the address downward (direction of stack growth) so that it
  41. is correctly aligned for a new stack frame. */
  42. static CORE_ADDR
  43. loongarch_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  44. {
  45. return align_down (addr, 16);
  46. }
  47. /* Generate, or return the cached frame cache for LoongArch frame unwinder. */
  48. static struct trad_frame_cache *
  49. loongarch_frame_cache (struct frame_info *this_frame, void **this_cache)
  50. {
  51. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  52. struct trad_frame_cache *cache;
  53. CORE_ADDR pc;
  54. if (*this_cache != nullptr)
  55. return (struct trad_frame_cache *) *this_cache;
  56. cache = trad_frame_cache_zalloc (this_frame);
  57. *this_cache = cache;
  58. loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  59. trad_frame_set_reg_realreg (cache, gdbarch_pc_regnum (gdbarch), tdep->regs.ra);
  60. pc = get_frame_address_in_block (this_frame);
  61. trad_frame_set_id (cache, frame_id_build_unavailable_stack (pc));
  62. return cache;
  63. }
  64. /* Implement the this_id callback for LoongArch frame unwinder. */
  65. static void
  66. loongarch_frame_this_id (struct frame_info *this_frame, void **prologue_cache,
  67. struct frame_id *this_id)
  68. {
  69. struct trad_frame_cache *info;
  70. info = loongarch_frame_cache (this_frame, prologue_cache);
  71. trad_frame_get_id (info, this_id);
  72. }
  73. /* Implement the prev_register callback for LoongArch frame unwinder. */
  74. static struct value *
  75. loongarch_frame_prev_register (struct frame_info *this_frame,
  76. void **prologue_cache, int regnum)
  77. {
  78. struct trad_frame_cache *info;
  79. info = loongarch_frame_cache (this_frame, prologue_cache);
  80. return trad_frame_get_register (info, this_frame, regnum);
  81. }
  82. static const struct frame_unwind loongarch_frame_unwind = {
  83. "loongarch prologue",
  84. /*.type =*/NORMAL_FRAME,
  85. /*.stop_reason =*/default_frame_unwind_stop_reason,
  86. /*.this_id =*/loongarch_frame_this_id,
  87. /*.prev_register =*/loongarch_frame_prev_register,
  88. /*.unwind_data =*/nullptr,
  89. /*.sniffer =*/default_frame_sniffer,
  90. /*.dealloc_cache =*/nullptr,
  91. /*.prev_arch =*/nullptr,
  92. };
  93. /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
  94. static int
  95. loongarch_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
  96. {
  97. loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  98. auto regs = tdep->regs;
  99. if (0 <= num && num < 32)
  100. return regs.r + num;
  101. else
  102. return -1;
  103. }
  104. static constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
  105. typedef BP_MANIPULATION (loongarch_default_breakpoint) loongarch_breakpoint;
  106. /* Extract a set of required target features out of ABFD. If ABFD is nullptr
  107. then a LOONGARCH_GDBARCH_FEATURES is returned in its default state. */
  108. static struct loongarch_gdbarch_features
  109. loongarch_features_from_bfd (const bfd *abfd)
  110. {
  111. struct loongarch_gdbarch_features features;
  112. /* Now try to improve on the defaults by looking at the binary we are
  113. going to execute. We assume the user knows what they are doing and
  114. that the target will match the binary. Remember, this code path is
  115. only used at all if the target hasn't given us a description, so this
  116. is really a last ditched effort to do something sane before giving
  117. up. */
  118. if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  119. {
  120. unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
  121. if (eclass == ELFCLASS32)
  122. features.xlen = 4;
  123. else if (eclass == ELFCLASS64)
  124. features.xlen = 8;
  125. else
  126. internal_error (__FILE__, __LINE__,
  127. _("unknown ELF header class %d"), eclass);
  128. }
  129. return features;
  130. }
  131. /* Find a suitable default target description. Use the contents of INFO,
  132. specifically the bfd object being executed, to guide the selection of a
  133. suitable default target description. */
  134. static const struct target_desc *
  135. loongarch_find_default_target_description (const struct gdbarch_info info)
  136. {
  137. /* Extract desired feature set from INFO. */
  138. struct loongarch_gdbarch_features features
  139. = loongarch_features_from_bfd (info.abfd);
  140. /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
  141. maybe there was no bfd object. In this case we fall back to a minimal
  142. useful target with no floating point, the x-register size is selected
  143. based on the architecture from INFO. */
  144. if (features.xlen == 0)
  145. features.xlen = info.bfd_arch_info->bits_per_address == 32 ? 4 : 8;
  146. /* Now build a target description based on the feature set. */
  147. return loongarch_lookup_target_description (features);
  148. }
  149. /* Initialize the current architecture based on INFO */
  150. static struct gdbarch *
  151. loongarch_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  152. {
  153. const struct target_desc *tdesc = info.target_desc;
  154. /* Ensure we always have a target description. */
  155. if (!tdesc_has_registers (tdesc))
  156. tdesc = loongarch_find_default_target_description (info);
  157. const struct tdesc_feature *feature_cpu
  158. = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.base");
  159. if (feature_cpu == nullptr)
  160. return nullptr;
  161. int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
  162. struct loongarch_gdbarch_features features;
  163. features.xlen = (xlen_bitsize / 8);
  164. size_t regnum = 0;
  165. tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
  166. loongarch_gdbarch_tdep *tdep = new loongarch_gdbarch_tdep;
  167. tdep->regs.r = regnum;
  168. /* Validate the description provides the mandatory base registers
  169. and allocate their numbers. */
  170. bool valid_p = true;
  171. for (int i = 0; i < 32; i++)
  172. valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++,
  173. loongarch_r_normal_name[i] + 1);
  174. valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (),
  175. tdep->regs.pc = regnum++, "pc");
  176. valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (),
  177. tdep->regs.badv = regnum++, "badv");
  178. if (!valid_p)
  179. return nullptr;
  180. /* LoongArch code is always little-endian. */
  181. info.byte_order_for_code = BFD_ENDIAN_LITTLE;
  182. /* Have a look at what the supplied (if any) bfd object requires of the
  183. target, then check that this matches with what the target is
  184. providing. */
  185. struct loongarch_gdbarch_features abi_features
  186. = loongarch_features_from_bfd (info.abfd);
  187. /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
  188. features from the INFO object. In this case we just treat the
  189. hardware features as defining the abi. */
  190. if (abi_features.xlen == 0)
  191. abi_features = features;
  192. /* Find a candidate among the list of pre-declared architectures. */
  193. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  194. arches != nullptr;
  195. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  196. {
  197. /* Check that the feature set of the ARCHES matches the feature set
  198. we are looking for. If it doesn't then we can't reuse this
  199. gdbarch. */
  200. loongarch_gdbarch_tdep *candidate_tdep
  201. = (loongarch_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  202. if (candidate_tdep->abi_features != abi_features)
  203. continue;
  204. break;
  205. }
  206. if (arches != nullptr)
  207. return arches->gdbarch;
  208. /* None found, so create a new architecture from the information provided. */
  209. struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
  210. tdep->abi_features = abi_features;
  211. /* Target data types. */
  212. set_gdbarch_short_bit (gdbarch, 16);
  213. set_gdbarch_int_bit (gdbarch, 32);
  214. set_gdbarch_long_bit (gdbarch, info.bfd_arch_info->bits_per_address);
  215. set_gdbarch_long_long_bit (gdbarch, 64);
  216. set_gdbarch_float_bit (gdbarch, 32);
  217. set_gdbarch_double_bit (gdbarch, 64);
  218. set_gdbarch_long_double_bit (gdbarch, 128);
  219. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
  220. set_gdbarch_ptr_bit (gdbarch, info.bfd_arch_info->bits_per_address);
  221. set_gdbarch_char_signed (gdbarch, 0);
  222. info.target_desc = tdesc;
  223. info.tdesc_data = tdesc_data.get ();
  224. /* Information about registers. */
  225. tdep->regs.ra = tdep->regs.r + 1;
  226. tdep->regs.sp = tdep->regs.r + 3;
  227. set_gdbarch_num_regs (gdbarch, regnum);
  228. set_gdbarch_sp_regnum (gdbarch, tdep->regs.sp);
  229. set_gdbarch_pc_regnum (gdbarch, tdep->regs.pc);
  230. /* Finalise the target description registers. */
  231. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  232. /* Advance PC across function entry code. */
  233. set_gdbarch_skip_prologue (gdbarch, loongarch_skip_prologue);
  234. /* Stack grows downward. */
  235. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  236. /* Frame info. */
  237. set_gdbarch_frame_align (gdbarch, loongarch_frame_align);
  238. /* Breakpoint manipulation. */
  239. set_gdbarch_breakpoint_kind_from_pc (gdbarch, loongarch_breakpoint::kind_from_pc);
  240. set_gdbarch_sw_breakpoint_from_kind (gdbarch, loongarch_breakpoint::bp_from_kind);
  241. /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder. */
  242. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, loongarch_dwarf2_reg_to_regnum);
  243. dwarf2_append_unwinders (gdbarch);
  244. frame_unwind_append_unwinder (gdbarch, &loongarch_frame_unwind);
  245. /* Hook in OS ABI-specific overrides, if they have been registered. */
  246. gdbarch_init_osabi (info, gdbarch);
  247. return gdbarch;
  248. }
  249. void _initialize_loongarch_tdep ();
  250. void
  251. _initialize_loongarch_tdep ()
  252. {
  253. gdbarch_register (bfd_arch_loongarch, loongarch_gdbarch_init, nullptr);
  254. }