m68k-tdep.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /* Target-dependent code for the Motorola 68000 series.
  2. Copyright (C) 1990-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 "dwarf2/frame.h"
  16. #include "frame.h"
  17. #include "frame-base.h"
  18. #include "frame-unwind.h"
  19. #include "gdbtypes.h"
  20. #include "symtab.h"
  21. #include "gdbcore.h"
  22. #include "value.h"
  23. #include "inferior.h"
  24. #include "regcache.h"
  25. #include "arch-utils.h"
  26. #include "osabi.h"
  27. #include "dis-asm.h"
  28. #include "target-descriptions.h"
  29. #include "floatformat.h"
  30. #include "target-float.h"
  31. #include "elf-bfd.h"
  32. #include "elf/m68k.h"
  33. #include "m68k-tdep.h"
  34. #define P_LINKL_FP 0x480e
  35. #define P_LINKW_FP 0x4e56
  36. #define P_PEA_FP 0x4856
  37. #define P_MOVEAL_SP_FP 0x2c4f
  38. #define P_ADDAW_SP 0xdefc
  39. #define P_ADDAL_SP 0xdffc
  40. #define P_SUBQW_SP 0x514f
  41. #define P_SUBQL_SP 0x518f
  42. #define P_LEA_SP_SP 0x4fef
  43. #define P_LEA_PC_A5 0x4bfb0170
  44. #define P_FMOVEMX_SP 0xf227
  45. #define P_MOVEL_SP 0x2f00
  46. #define P_MOVEML_SP 0x48e7
  47. /* Offset from SP to first arg on stack at first instruction of a function. */
  48. #define SP_ARG0 (1 * 4)
  49. #if !defined (BPT_VECTOR)
  50. #define BPT_VECTOR 0xf
  51. #endif
  52. constexpr gdb_byte m68k_break_insn[] = {0x4e, (0x40 | BPT_VECTOR)};
  53. typedef BP_MANIPULATION (m68k_break_insn) m68k_breakpoint;
  54. /* Construct types for ISA-specific registers. */
  55. static struct type *
  56. m68k_ps_type (struct gdbarch *gdbarch)
  57. {
  58. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  59. if (!tdep->m68k_ps_type)
  60. {
  61. struct type *type;
  62. type = arch_flags_type (gdbarch, "builtin_type_m68k_ps", 32);
  63. append_flags_type_flag (type, 0, "C");
  64. append_flags_type_flag (type, 1, "V");
  65. append_flags_type_flag (type, 2, "Z");
  66. append_flags_type_flag (type, 3, "N");
  67. append_flags_type_flag (type, 4, "X");
  68. append_flags_type_flag (type, 8, "I0");
  69. append_flags_type_flag (type, 9, "I1");
  70. append_flags_type_flag (type, 10, "I2");
  71. append_flags_type_flag (type, 12, "M");
  72. append_flags_type_flag (type, 13, "S");
  73. append_flags_type_flag (type, 14, "T0");
  74. append_flags_type_flag (type, 15, "T1");
  75. tdep->m68k_ps_type = type;
  76. }
  77. return tdep->m68k_ps_type;
  78. }
  79. static struct type *
  80. m68881_ext_type (struct gdbarch *gdbarch)
  81. {
  82. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  83. if (!tdep->m68881_ext_type)
  84. tdep->m68881_ext_type
  85. = arch_float_type (gdbarch, -1, "builtin_type_m68881_ext",
  86. floatformats_m68881_ext);
  87. return tdep->m68881_ext_type;
  88. }
  89. /* Return the GDB type object for the "standard" data type of data in
  90. register N. This should be int for D0-D7, SR, FPCONTROL and
  91. FPSTATUS, long double for FP0-FP7, and void pointer for all others
  92. (A0-A7, PC, FPIADDR). Note, for registers which contain
  93. addresses return pointer to void, not pointer to char, because we
  94. don't want to attempt to print the string after printing the
  95. address. */
  96. static struct type *
  97. m68k_register_type (struct gdbarch *gdbarch, int regnum)
  98. {
  99. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  100. if (tdep->fpregs_present)
  101. {
  102. if (regnum >= gdbarch_fp0_regnum (gdbarch)
  103. && regnum <= gdbarch_fp0_regnum (gdbarch) + 7)
  104. {
  105. if (tdep->flavour == m68k_coldfire_flavour)
  106. return builtin_type (gdbarch)->builtin_double;
  107. else
  108. return m68881_ext_type (gdbarch);
  109. }
  110. if (regnum == M68K_FPI_REGNUM)
  111. return builtin_type (gdbarch)->builtin_func_ptr;
  112. if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM)
  113. return builtin_type (gdbarch)->builtin_int32;
  114. }
  115. else
  116. {
  117. if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM)
  118. return builtin_type (gdbarch)->builtin_int0;
  119. }
  120. if (regnum == gdbarch_pc_regnum (gdbarch))
  121. return builtin_type (gdbarch)->builtin_func_ptr;
  122. if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
  123. return builtin_type (gdbarch)->builtin_data_ptr;
  124. if (regnum == M68K_PS_REGNUM)
  125. return m68k_ps_type (gdbarch);
  126. return builtin_type (gdbarch)->builtin_int32;
  127. }
  128. static const char * const m68k_register_names[] = {
  129. "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
  130. "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
  131. "ps", "pc",
  132. "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
  133. "fpcontrol", "fpstatus", "fpiaddr"
  134. };
  135. /* Function: m68k_register_name
  136. Returns the name of the standard m68k register regnum. */
  137. static const char *
  138. m68k_register_name (struct gdbarch *gdbarch, int regnum)
  139. {
  140. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  141. if (regnum < 0 || regnum >= ARRAY_SIZE (m68k_register_names))
  142. internal_error (__FILE__, __LINE__,
  143. _("m68k_register_name: illegal register number %d"),
  144. regnum);
  145. else if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM
  146. && tdep->fpregs_present == 0)
  147. return "";
  148. else
  149. return m68k_register_names[regnum];
  150. }
  151. /* Return nonzero if a value of type TYPE stored in register REGNUM
  152. needs any special handling. */
  153. static int
  154. m68k_convert_register_p (struct gdbarch *gdbarch,
  155. int regnum, struct type *type)
  156. {
  157. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  158. if (!tdep->fpregs_present)
  159. return 0;
  160. return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FP0_REGNUM + 7
  161. /* We only support floating-point values. */
  162. && type->code () == TYPE_CODE_FLT
  163. && type != register_type (gdbarch, M68K_FP0_REGNUM));
  164. }
  165. /* Read a value of type TYPE from register REGNUM in frame FRAME, and
  166. return its contents in TO. */
  167. static int
  168. m68k_register_to_value (struct frame_info *frame, int regnum,
  169. struct type *type, gdb_byte *to,
  170. int *optimizedp, int *unavailablep)
  171. {
  172. struct gdbarch *gdbarch = get_frame_arch (frame);
  173. gdb_byte from[M68K_MAX_REGISTER_SIZE];
  174. struct type *fpreg_type = register_type (gdbarch, M68K_FP0_REGNUM);
  175. gdb_assert (type->code () == TYPE_CODE_FLT);
  176. /* Convert to TYPE. */
  177. if (!get_frame_register_bytes (frame, regnum, 0,
  178. gdb::make_array_view (from,
  179. register_size (gdbarch,
  180. regnum)),
  181. optimizedp, unavailablep))
  182. return 0;
  183. target_float_convert (from, fpreg_type, to, type);
  184. *optimizedp = *unavailablep = 0;
  185. return 1;
  186. }
  187. /* Write the contents FROM of a value of type TYPE into register
  188. REGNUM in frame FRAME. */
  189. static void
  190. m68k_value_to_register (struct frame_info *frame, int regnum,
  191. struct type *type, const gdb_byte *from)
  192. {
  193. gdb_byte to[M68K_MAX_REGISTER_SIZE];
  194. struct type *fpreg_type = register_type (get_frame_arch (frame),
  195. M68K_FP0_REGNUM);
  196. /* We only support floating-point values. */
  197. if (type->code () != TYPE_CODE_FLT)
  198. {
  199. warning (_("Cannot convert non-floating-point type "
  200. "to floating-point register value."));
  201. return;
  202. }
  203. /* Convert from TYPE. */
  204. target_float_convert (from, type, to, fpreg_type);
  205. put_frame_register (frame, regnum, to);
  206. }
  207. /* There is a fair number of calling conventions that are in somewhat
  208. wide use. The 68000/08/10 don't support an FPU, not even as a
  209. coprocessor. All function return values are stored in %d0/%d1.
  210. Structures are returned in a static buffer, a pointer to which is
  211. returned in %d0. This means that functions returning a structure
  212. are not re-entrant. To avoid this problem some systems use a
  213. convention where the caller passes a pointer to a buffer in %a1
  214. where the return values is to be stored. This convention is the
  215. default, and is implemented in the function m68k_return_value.
  216. The 68020/030/040/060 do support an FPU, either as a coprocessor
  217. (68881/2) or built-in (68040/68060). That's why System V release 4
  218. (SVR4) introduces a new calling convention specified by the SVR4
  219. psABI. Integer values are returned in %d0/%d1, pointer return
  220. values in %a0 and floating values in %fp0. When calling functions
  221. returning a structure the caller should pass a pointer to a buffer
  222. for the return value in %a0. This convention is implemented in the
  223. function m68k_svr4_return_value, and by appropriately setting the
  224. struct_value_regnum member of `struct gdbarch_tdep'.
  225. GNU/Linux returns values in the same way as SVR4 does, but uses %a1
  226. for passing the structure return value buffer.
  227. GCC can also generate code where small structures are returned in
  228. %d0/%d1 instead of in memory by using -freg-struct-return. This is
  229. the default on NetBSD a.out, OpenBSD and GNU/Linux and several
  230. embedded systems. This convention is implemented by setting the
  231. struct_return member of `struct gdbarch_tdep' to reg_struct_return.
  232. GCC also has an "embedded" ABI. This works like the SVR4 ABI,
  233. except that pointers are returned in %D0. This is implemented by
  234. setting the pointer_result_regnum member of `struct gdbarch_tdep'
  235. as appropriate. */
  236. /* Read a function return value of TYPE from REGCACHE, and copy that
  237. into VALBUF. */
  238. static void
  239. m68k_extract_return_value (struct type *type, struct regcache *regcache,
  240. gdb_byte *valbuf)
  241. {
  242. int len = TYPE_LENGTH (type);
  243. gdb_byte buf[M68K_MAX_REGISTER_SIZE];
  244. if (type->code () == TYPE_CODE_PTR && len == 4)
  245. {
  246. struct gdbarch *gdbarch = regcache->arch ();
  247. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  248. regcache->raw_read (tdep->pointer_result_regnum, valbuf);
  249. }
  250. else if (len <= 4)
  251. {
  252. regcache->raw_read (M68K_D0_REGNUM, buf);
  253. memcpy (valbuf, buf + (4 - len), len);
  254. }
  255. else if (len <= 8)
  256. {
  257. regcache->raw_read (M68K_D0_REGNUM, buf);
  258. memcpy (valbuf, buf + (8 - len), len - 4);
  259. regcache->raw_read (M68K_D1_REGNUM, valbuf + (len - 4));
  260. }
  261. else
  262. internal_error (__FILE__, __LINE__,
  263. _("Cannot extract return value of %d bytes long."), len);
  264. }
  265. static void
  266. m68k_svr4_extract_return_value (struct type *type, struct regcache *regcache,
  267. gdb_byte *valbuf)
  268. {
  269. gdb_byte buf[M68K_MAX_REGISTER_SIZE];
  270. struct gdbarch *gdbarch = regcache->arch ();
  271. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  272. if (tdep->float_return && type->code () == TYPE_CODE_FLT)
  273. {
  274. struct type *fpreg_type = register_type (gdbarch, M68K_FP0_REGNUM);
  275. regcache->raw_read (M68K_FP0_REGNUM, buf);
  276. target_float_convert (buf, fpreg_type, valbuf, type);
  277. }
  278. else
  279. m68k_extract_return_value (type, regcache, valbuf);
  280. }
  281. /* Write a function return value of TYPE from VALBUF into REGCACHE. */
  282. static void
  283. m68k_store_return_value (struct type *type, struct regcache *regcache,
  284. const gdb_byte *valbuf)
  285. {
  286. int len = TYPE_LENGTH (type);
  287. if (type->code () == TYPE_CODE_PTR && len == 4)
  288. {
  289. struct gdbarch *gdbarch = regcache->arch ();
  290. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  291. regcache->raw_write (tdep->pointer_result_regnum, valbuf);
  292. /* gdb historically also set D0 in the SVR4 case. */
  293. if (tdep->pointer_result_regnum != M68K_D0_REGNUM)
  294. regcache->raw_write (M68K_D0_REGNUM, valbuf);
  295. }
  296. else if (len <= 4)
  297. regcache->raw_write_part (M68K_D0_REGNUM, 4 - len, len, valbuf);
  298. else if (len <= 8)
  299. {
  300. regcache->raw_write_part (M68K_D0_REGNUM, 8 - len, len - 4, valbuf);
  301. regcache->raw_write (M68K_D1_REGNUM, valbuf + (len - 4));
  302. }
  303. else
  304. internal_error (__FILE__, __LINE__,
  305. _("Cannot store return value of %d bytes long."), len);
  306. }
  307. static void
  308. m68k_svr4_store_return_value (struct type *type, struct regcache *regcache,
  309. const gdb_byte *valbuf)
  310. {
  311. struct gdbarch *gdbarch = regcache->arch ();
  312. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  313. if (tdep->float_return && type->code () == TYPE_CODE_FLT)
  314. {
  315. struct type *fpreg_type = register_type (gdbarch, M68K_FP0_REGNUM);
  316. gdb_byte buf[M68K_MAX_REGISTER_SIZE];
  317. target_float_convert (valbuf, type, buf, fpreg_type);
  318. regcache->raw_write (M68K_FP0_REGNUM, buf);
  319. }
  320. else
  321. m68k_store_return_value (type, regcache, valbuf);
  322. }
  323. /* Return non-zero if TYPE, which is assumed to be a structure, union or
  324. complex type, should be returned in registers for architecture
  325. GDBARCH. */
  326. static int
  327. m68k_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
  328. {
  329. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  330. enum type_code code = type->code ();
  331. int len = TYPE_LENGTH (type);
  332. gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
  333. || code == TYPE_CODE_COMPLEX || code == TYPE_CODE_ARRAY);
  334. if (tdep->struct_return == pcc_struct_return)
  335. return 0;
  336. const bool is_vector = code == TYPE_CODE_ARRAY && type->is_vector ();
  337. if (is_vector
  338. && check_typedef (TYPE_TARGET_TYPE (type))->code () == TYPE_CODE_FLT)
  339. return 0;
  340. /* According to m68k_return_in_memory in the m68k GCC back-end,
  341. strange things happen for small aggregate types. Aggregate types
  342. with only one component are always returned like the type of the
  343. component. Aggregate types whose size is 2, 4, or 8 are returned
  344. in registers if their natural alignment is at least 16 bits.
  345. We reject vectors here, as experimentally this gives the correct
  346. answer. */
  347. if (!is_vector && (len == 2 || len == 4 || len == 8))
  348. return type_align (type) >= 2;
  349. return (len == 1 || len == 2 || len == 4 || len == 8);
  350. }
  351. /* Determine, for architecture GDBARCH, how a return value of TYPE
  352. should be returned. If it is supposed to be returned in registers,
  353. and READBUF is non-zero, read the appropriate value from REGCACHE,
  354. and copy it into READBUF. If WRITEBUF is non-zero, write the value
  355. from WRITEBUF into REGCACHE. */
  356. static enum return_value_convention
  357. m68k_return_value (struct gdbarch *gdbarch, struct value *function,
  358. struct type *type, struct regcache *regcache,
  359. gdb_byte *readbuf, const gdb_byte *writebuf)
  360. {
  361. enum type_code code = type->code ();
  362. /* GCC returns a `long double' in memory too. */
  363. if (((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
  364. || code == TYPE_CODE_COMPLEX || code == TYPE_CODE_ARRAY)
  365. && !m68k_reg_struct_return_p (gdbarch, type))
  366. || (code == TYPE_CODE_FLT && TYPE_LENGTH (type) == 12))
  367. {
  368. /* The default on m68k is to return structures in static memory.
  369. Consequently a function must return the address where we can
  370. find the return value. */
  371. if (readbuf)
  372. {
  373. ULONGEST addr;
  374. regcache_raw_read_unsigned (regcache, M68K_D0_REGNUM, &addr);
  375. read_memory (addr, readbuf, TYPE_LENGTH (type));
  376. }
  377. return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  378. }
  379. if (readbuf)
  380. m68k_extract_return_value (type, regcache, readbuf);
  381. if (writebuf)
  382. m68k_store_return_value (type, regcache, writebuf);
  383. return RETURN_VALUE_REGISTER_CONVENTION;
  384. }
  385. static enum return_value_convention
  386. m68k_svr4_return_value (struct gdbarch *gdbarch, struct value *function,
  387. struct type *type, struct regcache *regcache,
  388. gdb_byte *readbuf, const gdb_byte *writebuf)
  389. {
  390. enum type_code code = type->code ();
  391. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  392. /* Aggregates with a single member are always returned like their
  393. sole element. */
  394. if ((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
  395. && type->num_fields () == 1)
  396. {
  397. type = check_typedef (type->field (0).type ());
  398. return m68k_svr4_return_value (gdbarch, function, type, regcache,
  399. readbuf, writebuf);
  400. }
  401. if (((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
  402. || code == TYPE_CODE_COMPLEX || code == TYPE_CODE_ARRAY)
  403. && !m68k_reg_struct_return_p (gdbarch, type))
  404. /* GCC may return a `long double' in memory too. */
  405. || (!tdep->float_return
  406. && code == TYPE_CODE_FLT
  407. && TYPE_LENGTH (type) == 12))
  408. {
  409. /* The System V ABI says that:
  410. "A function returning a structure or union also sets %a0 to
  411. the value it finds in %a0. Thus when the caller receives
  412. control again, the address of the returned object resides in
  413. register %a0."
  414. So the ABI guarantees that we can always find the return
  415. value just after the function has returned.
  416. However, GCC also implements the "embedded" ABI. That ABI
  417. does not preserve %a0 across calls, but does write the value
  418. back to %d0. */
  419. if (readbuf)
  420. {
  421. ULONGEST addr;
  422. regcache_raw_read_unsigned (regcache, tdep->pointer_result_regnum,
  423. &addr);
  424. read_memory (addr, readbuf, TYPE_LENGTH (type));
  425. }
  426. return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  427. }
  428. if (readbuf)
  429. m68k_svr4_extract_return_value (type, regcache, readbuf);
  430. if (writebuf)
  431. m68k_svr4_store_return_value (type, regcache, writebuf);
  432. return RETURN_VALUE_REGISTER_CONVENTION;
  433. }
  434. /* Always align the frame to a 4-byte boundary. This is required on
  435. coldfire and harmless on the rest. */
  436. static CORE_ADDR
  437. m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  438. {
  439. /* Align the stack to four bytes. */
  440. return sp & ~3;
  441. }
  442. static CORE_ADDR
  443. m68k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  444. struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  445. struct value **args, CORE_ADDR sp,
  446. function_call_return_method return_method,
  447. CORE_ADDR struct_addr)
  448. {
  449. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  450. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  451. gdb_byte buf[4];
  452. int i;
  453. /* Push arguments in reverse order. */
  454. for (i = nargs - 1; i >= 0; i--)
  455. {
  456. struct type *value_type = value_enclosing_type (args[i]);
  457. int len = TYPE_LENGTH (value_type);
  458. int container_len = (len + 3) & ~3;
  459. int offset;
  460. /* Non-scalars bigger than 4 bytes are left aligned, others are
  461. right aligned. */
  462. if ((value_type->code () == TYPE_CODE_STRUCT
  463. || value_type->code () == TYPE_CODE_UNION
  464. || value_type->code () == TYPE_CODE_ARRAY)
  465. && len > 4)
  466. offset = 0;
  467. else
  468. offset = container_len - len;
  469. sp -= container_len;
  470. write_memory (sp + offset, value_contents_all (args[i]).data (), len);
  471. }
  472. /* Store struct value address. */
  473. if (return_method == return_method_struct)
  474. {
  475. store_unsigned_integer (buf, 4, byte_order, struct_addr);
  476. regcache->cooked_write (tdep->struct_value_regnum, buf);
  477. }
  478. /* Store return address. */
  479. sp -= 4;
  480. store_unsigned_integer (buf, 4, byte_order, bp_addr);
  481. write_memory (sp, buf, 4);
  482. /* Finally, update the stack pointer... */
  483. store_unsigned_integer (buf, 4, byte_order, sp);
  484. regcache->cooked_write (M68K_SP_REGNUM, buf);
  485. /* ...and fake a frame pointer. */
  486. regcache->cooked_write (M68K_FP_REGNUM, buf);
  487. /* DWARF2/GCC uses the stack address *before* the function call as a
  488. frame's CFA. */
  489. return sp + 8;
  490. }
  491. /* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
  492. static int
  493. m68k_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int num)
  494. {
  495. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  496. if (num < 8)
  497. /* d0..7 */
  498. return (num - 0) + M68K_D0_REGNUM;
  499. else if (num < 16)
  500. /* a0..7 */
  501. return (num - 8) + M68K_A0_REGNUM;
  502. else if (num < 24 && tdep->fpregs_present)
  503. /* fp0..7 */
  504. return (num - 16) + M68K_FP0_REGNUM;
  505. else if (num == 25)
  506. /* pc */
  507. return M68K_PC_REGNUM;
  508. else
  509. return -1;
  510. }
  511. struct m68k_frame_cache
  512. {
  513. /* Base address. */
  514. CORE_ADDR base;
  515. CORE_ADDR sp_offset;
  516. CORE_ADDR pc;
  517. /* Saved registers. */
  518. CORE_ADDR saved_regs[M68K_NUM_REGS];
  519. CORE_ADDR saved_sp;
  520. /* Stack space reserved for local variables. */
  521. long locals;
  522. };
  523. /* Allocate and initialize a frame cache. */
  524. static struct m68k_frame_cache *
  525. m68k_alloc_frame_cache (void)
  526. {
  527. struct m68k_frame_cache *cache;
  528. int i;
  529. cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
  530. /* Base address. */
  531. cache->base = 0;
  532. cache->sp_offset = -4;
  533. cache->pc = 0;
  534. /* Saved registers. We initialize these to -1 since zero is a valid
  535. offset (that's where %fp is supposed to be stored). */
  536. for (i = 0; i < M68K_NUM_REGS; i++)
  537. cache->saved_regs[i] = -1;
  538. /* Frameless until proven otherwise. */
  539. cache->locals = -1;
  540. return cache;
  541. }
  542. /* Check whether PC points at a code that sets up a new stack frame.
  543. If so, it updates CACHE and returns the address of the first
  544. instruction after the sequence that sets removes the "hidden"
  545. argument from the stack or CURRENT_PC, whichever is smaller.
  546. Otherwise, return PC. */
  547. static CORE_ADDR
  548. m68k_analyze_frame_setup (struct gdbarch *gdbarch,
  549. CORE_ADDR pc, CORE_ADDR current_pc,
  550. struct m68k_frame_cache *cache)
  551. {
  552. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  553. int op;
  554. if (pc >= current_pc)
  555. return current_pc;
  556. op = read_memory_unsigned_integer (pc, 2, byte_order);
  557. if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
  558. {
  559. cache->saved_regs[M68K_FP_REGNUM] = 0;
  560. cache->sp_offset += 4;
  561. if (op == P_LINKW_FP)
  562. {
  563. /* link.w %fp, #-N */
  564. /* link.w %fp, #0; adda.l #-N, %sp */
  565. cache->locals = -read_memory_integer (pc + 2, 2, byte_order);
  566. if (pc + 4 < current_pc && cache->locals == 0)
  567. {
  568. op = read_memory_unsigned_integer (pc + 4, 2, byte_order);
  569. if (op == P_ADDAL_SP)
  570. {
  571. cache->locals = read_memory_integer (pc + 6, 4, byte_order);
  572. return pc + 10;
  573. }
  574. }
  575. return pc + 4;
  576. }
  577. else if (op == P_LINKL_FP)
  578. {
  579. /* link.l %fp, #-N */
  580. cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
  581. return pc + 6;
  582. }
  583. else
  584. {
  585. /* pea (%fp); movea.l %sp, %fp */
  586. cache->locals = 0;
  587. if (pc + 2 < current_pc)
  588. {
  589. op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  590. if (op == P_MOVEAL_SP_FP)
  591. {
  592. /* move.l %sp, %fp */
  593. return pc + 4;
  594. }
  595. }
  596. return pc + 2;
  597. }
  598. }
  599. else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
  600. {
  601. /* subq.[wl] #N,%sp */
  602. /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
  603. cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
  604. if (pc + 2 < current_pc)
  605. {
  606. op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  607. if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
  608. {
  609. cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
  610. return pc + 4;
  611. }
  612. }
  613. return pc + 2;
  614. }
  615. else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
  616. {
  617. /* adda.w #-N,%sp */
  618. /* lea (-N,%sp),%sp */
  619. cache->locals = -read_memory_integer (pc + 2, 2, byte_order);
  620. return pc + 4;
  621. }
  622. else if (op == P_ADDAL_SP)
  623. {
  624. /* adda.l #-N,%sp */
  625. cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
  626. return pc + 6;
  627. }
  628. return pc;
  629. }
  630. /* Check whether PC points at code that saves registers on the stack.
  631. If so, it updates CACHE and returns the address of the first
  632. instruction after the register saves or CURRENT_PC, whichever is
  633. smaller. Otherwise, return PC. */
  634. static CORE_ADDR
  635. m68k_analyze_register_saves (struct gdbarch *gdbarch, CORE_ADDR pc,
  636. CORE_ADDR current_pc,
  637. struct m68k_frame_cache *cache)
  638. {
  639. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  640. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  641. if (cache->locals >= 0)
  642. {
  643. CORE_ADDR offset;
  644. int op;
  645. int i, mask, regno;
  646. offset = -4 - cache->locals;
  647. while (pc < current_pc)
  648. {
  649. op = read_memory_unsigned_integer (pc, 2, byte_order);
  650. if (op == P_FMOVEMX_SP
  651. && tdep->fpregs_present)
  652. {
  653. /* fmovem.x REGS,-(%sp) */
  654. op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  655. if ((op & 0xff00) == 0xe000)
  656. {
  657. mask = op & 0xff;
  658. for (i = 0; i < 16; i++, mask >>= 1)
  659. {
  660. if (mask & 1)
  661. {
  662. cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
  663. offset -= 12;
  664. }
  665. }
  666. pc += 4;
  667. }
  668. else
  669. break;
  670. }
  671. else if ((op & 0177760) == P_MOVEL_SP)
  672. {
  673. /* move.l %R,-(%sp) */
  674. regno = op & 017;
  675. cache->saved_regs[regno] = offset;
  676. offset -= 4;
  677. pc += 2;
  678. }
  679. else if (op == P_MOVEML_SP)
  680. {
  681. /* movem.l REGS,-(%sp) */
  682. mask = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  683. for (i = 0; i < 16; i++, mask >>= 1)
  684. {
  685. if (mask & 1)
  686. {
  687. cache->saved_regs[15 - i] = offset;
  688. offset -= 4;
  689. }
  690. }
  691. pc += 4;
  692. }
  693. else
  694. break;
  695. }
  696. }
  697. return pc;
  698. }
  699. /* Do a full analysis of the prologue at PC and update CACHE
  700. accordingly. Bail out early if CURRENT_PC is reached. Return the
  701. address where the analysis stopped.
  702. We handle all cases that can be generated by gcc.
  703. For allocating a stack frame:
  704. link.w %a6,#-N
  705. link.l %a6,#-N
  706. pea (%fp); move.l %sp,%fp
  707. link.w %a6,#0; add.l #-N,%sp
  708. subq.l #N,%sp
  709. subq.w #N,%sp
  710. subq.w #8,%sp; subq.w #N-8,%sp
  711. add.w #-N,%sp
  712. lea (-N,%sp),%sp
  713. add.l #-N,%sp
  714. For saving registers:
  715. fmovem.x REGS,-(%sp)
  716. move.l R1,-(%sp)
  717. move.l R1,-(%sp); move.l R2,-(%sp)
  718. movem.l REGS,-(%sp)
  719. For setting up the PIC register:
  720. lea (%pc,N),%a5
  721. */
  722. static CORE_ADDR
  723. m68k_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  724. CORE_ADDR current_pc, struct m68k_frame_cache *cache)
  725. {
  726. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  727. unsigned int op;
  728. pc = m68k_analyze_frame_setup (gdbarch, pc, current_pc, cache);
  729. pc = m68k_analyze_register_saves (gdbarch, pc, current_pc, cache);
  730. if (pc >= current_pc)
  731. return current_pc;
  732. /* Check for GOT setup. */
  733. op = read_memory_unsigned_integer (pc, 4, byte_order);
  734. if (op == P_LEA_PC_A5)
  735. {
  736. /* lea (%pc,N),%a5 */
  737. return pc + 8;
  738. }
  739. return pc;
  740. }
  741. /* Return PC of first real instruction. */
  742. static CORE_ADDR
  743. m68k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  744. {
  745. struct m68k_frame_cache cache;
  746. CORE_ADDR pc;
  747. cache.locals = -1;
  748. pc = m68k_analyze_prologue (gdbarch, start_pc, (CORE_ADDR) -1, &cache);
  749. if (cache.locals < 0)
  750. return start_pc;
  751. return pc;
  752. }
  753. static CORE_ADDR
  754. m68k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  755. {
  756. gdb_byte buf[8];
  757. frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
  758. return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
  759. }
  760. /* Normal frames. */
  761. static struct m68k_frame_cache *
  762. m68k_frame_cache (struct frame_info *this_frame, void **this_cache)
  763. {
  764. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  765. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  766. struct m68k_frame_cache *cache;
  767. gdb_byte buf[4];
  768. int i;
  769. if (*this_cache)
  770. return (struct m68k_frame_cache *) *this_cache;
  771. cache = m68k_alloc_frame_cache ();
  772. *this_cache = cache;
  773. /* In principle, for normal frames, %fp holds the frame pointer,
  774. which holds the base address for the current stack frame.
  775. However, for functions that don't need it, the frame pointer is
  776. optional. For these "frameless" functions the frame pointer is
  777. actually the frame pointer of the calling frame. Signal
  778. trampolines are just a special case of a "frameless" function.
  779. They (usually) share their frame pointer with the frame that was
  780. in progress when the signal occurred. */
  781. get_frame_register (this_frame, M68K_FP_REGNUM, buf);
  782. cache->base = extract_unsigned_integer (buf, 4, byte_order);
  783. if (cache->base == 0)
  784. return cache;
  785. /* For normal frames, %pc is stored at 4(%fp). */
  786. cache->saved_regs[M68K_PC_REGNUM] = 4;
  787. cache->pc = get_frame_func (this_frame);
  788. if (cache->pc != 0)
  789. m68k_analyze_prologue (get_frame_arch (this_frame), cache->pc,
  790. get_frame_pc (this_frame), cache);
  791. if (cache->locals < 0)
  792. {
  793. /* We didn't find a valid frame, which means that CACHE->base
  794. currently holds the frame pointer for our calling frame. If
  795. we're at the start of a function, or somewhere half-way its
  796. prologue, the function's frame probably hasn't been fully
  797. setup yet. Try to reconstruct the base address for the stack
  798. frame by looking at the stack pointer. For truly "frameless"
  799. functions this might work too. */
  800. get_frame_register (this_frame, M68K_SP_REGNUM, buf);
  801. cache->base = extract_unsigned_integer (buf, 4, byte_order)
  802. + cache->sp_offset;
  803. }
  804. /* Now that we have the base address for the stack frame we can
  805. calculate the value of %sp in the calling frame. */
  806. cache->saved_sp = cache->base + 8;
  807. /* Adjust all the saved registers such that they contain addresses
  808. instead of offsets. */
  809. for (i = 0; i < M68K_NUM_REGS; i++)
  810. if (cache->saved_regs[i] != -1)
  811. cache->saved_regs[i] += cache->base;
  812. return cache;
  813. }
  814. static void
  815. m68k_frame_this_id (struct frame_info *this_frame, void **this_cache,
  816. struct frame_id *this_id)
  817. {
  818. struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
  819. /* This marks the outermost frame. */
  820. if (cache->base == 0)
  821. return;
  822. /* See the end of m68k_push_dummy_call. */
  823. *this_id = frame_id_build (cache->base + 8, cache->pc);
  824. }
  825. static struct value *
  826. m68k_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  827. int regnum)
  828. {
  829. struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
  830. gdb_assert (regnum >= 0);
  831. if (regnum == M68K_SP_REGNUM && cache->saved_sp)
  832. return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
  833. if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
  834. return frame_unwind_got_memory (this_frame, regnum,
  835. cache->saved_regs[regnum]);
  836. return frame_unwind_got_register (this_frame, regnum, regnum);
  837. }
  838. static const struct frame_unwind m68k_frame_unwind =
  839. {
  840. "m68k prologue",
  841. NORMAL_FRAME,
  842. default_frame_unwind_stop_reason,
  843. m68k_frame_this_id,
  844. m68k_frame_prev_register,
  845. NULL,
  846. default_frame_sniffer
  847. };
  848. static CORE_ADDR
  849. m68k_frame_base_address (struct frame_info *this_frame, void **this_cache)
  850. {
  851. struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
  852. return cache->base;
  853. }
  854. static const struct frame_base m68k_frame_base =
  855. {
  856. &m68k_frame_unwind,
  857. m68k_frame_base_address,
  858. m68k_frame_base_address,
  859. m68k_frame_base_address
  860. };
  861. static struct frame_id
  862. m68k_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  863. {
  864. CORE_ADDR fp;
  865. fp = get_frame_register_unsigned (this_frame, M68K_FP_REGNUM);
  866. /* See the end of m68k_push_dummy_call. */
  867. return frame_id_build (fp + 8, get_frame_pc (this_frame));
  868. }
  869. /* Figure out where the longjmp will land. Slurp the args out of the stack.
  870. We expect the first arg to be a pointer to the jmp_buf structure from which
  871. we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
  872. This routine returns true on success. */
  873. static int
  874. m68k_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  875. {
  876. gdb_byte *buf;
  877. CORE_ADDR sp, jb_addr;
  878. struct gdbarch *gdbarch = get_frame_arch (frame);
  879. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  880. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  881. if (tdep->jb_pc < 0)
  882. {
  883. internal_error (__FILE__, __LINE__,
  884. _("m68k_get_longjmp_target: not implemented"));
  885. return 0;
  886. }
  887. buf = (gdb_byte *) alloca (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
  888. sp = get_frame_register_unsigned (frame, gdbarch_sp_regnum (gdbarch));
  889. if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
  890. buf, gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
  891. return 0;
  892. jb_addr = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
  893. / TARGET_CHAR_BIT, byte_order);
  894. if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
  895. gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT),
  896. byte_order)
  897. return 0;
  898. *pc = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
  899. / TARGET_CHAR_BIT, byte_order);
  900. return 1;
  901. }
  902. /* This is the implementation of gdbarch method
  903. return_in_first_hidden_param_p. */
  904. static int
  905. m68k_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
  906. struct type *type)
  907. {
  908. return 0;
  909. }
  910. /* System V Release 4 (SVR4). */
  911. void
  912. m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  913. {
  914. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  915. /* SVR4 uses a different calling convention. */
  916. set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
  917. /* SVR4 uses %a0 instead of %a1. */
  918. tdep->struct_value_regnum = M68K_A0_REGNUM;
  919. /* SVR4 returns pointers in %a0. */
  920. tdep->pointer_result_regnum = M68K_A0_REGNUM;
  921. }
  922. /* GCC's m68k "embedded" ABI. This is like the SVR4 ABI, but pointer
  923. values are returned in %d0, not %a0. */
  924. static void
  925. m68k_embedded_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  926. {
  927. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  928. m68k_svr4_init_abi (info, gdbarch);
  929. tdep->pointer_result_regnum = M68K_D0_REGNUM;
  930. }
  931. /* Function: m68k_gdbarch_init
  932. Initializer function for the m68k gdbarch vector.
  933. Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
  934. static struct gdbarch *
  935. m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  936. {
  937. struct gdbarch *gdbarch;
  938. struct gdbarch_list *best_arch;
  939. tdesc_arch_data_up tdesc_data;
  940. int i;
  941. enum m68k_flavour flavour = m68k_no_flavour;
  942. int has_fp = 1;
  943. const struct floatformat **long_double_format = floatformats_m68881_ext;
  944. /* Check any target description for validity. */
  945. if (tdesc_has_registers (info.target_desc))
  946. {
  947. const struct tdesc_feature *feature;
  948. int valid_p;
  949. feature = tdesc_find_feature (info.target_desc,
  950. "org.gnu.gdb.m68k.core");
  951. if (feature == NULL)
  952. {
  953. feature = tdesc_find_feature (info.target_desc,
  954. "org.gnu.gdb.coldfire.core");
  955. if (feature != NULL)
  956. flavour = m68k_coldfire_flavour;
  957. }
  958. if (feature == NULL)
  959. {
  960. feature = tdesc_find_feature (info.target_desc,
  961. "org.gnu.gdb.fido.core");
  962. if (feature != NULL)
  963. flavour = m68k_fido_flavour;
  964. }
  965. if (feature == NULL)
  966. return NULL;
  967. tdesc_data = tdesc_data_alloc ();
  968. valid_p = 1;
  969. for (i = 0; i <= M68K_PC_REGNUM; i++)
  970. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  971. m68k_register_names[i]);
  972. if (!valid_p)
  973. return NULL;
  974. feature = tdesc_find_feature (info.target_desc,
  975. "org.gnu.gdb.coldfire.fp");
  976. if (feature != NULL)
  977. {
  978. valid_p = 1;
  979. for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
  980. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  981. m68k_register_names[i]);
  982. if (!valid_p)
  983. return NULL;
  984. }
  985. else
  986. has_fp = 0;
  987. }
  988. /* The mechanism for returning floating values from function
  989. and the type of long double depend on whether we're
  990. on ColdFire or standard m68k. */
  991. if (info.bfd_arch_info && info.bfd_arch_info->mach != 0)
  992. {
  993. const bfd_arch_info_type *coldfire_arch =
  994. bfd_lookup_arch (bfd_arch_m68k, bfd_mach_mcf_isa_a_nodiv);
  995. if (coldfire_arch
  996. && ((*info.bfd_arch_info->compatible)
  997. (info.bfd_arch_info, coldfire_arch)))
  998. flavour = m68k_coldfire_flavour;
  999. }
  1000. /* Try to figure out if the arch uses floating registers to return
  1001. floating point values from functions. On ColdFire, floating
  1002. point values are returned in D0. */
  1003. int float_return = 0;
  1004. if (has_fp && flavour != m68k_coldfire_flavour)
  1005. float_return = 1;
  1006. #ifdef HAVE_ELF
  1007. if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1008. {
  1009. int fp_abi = bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
  1010. Tag_GNU_M68K_ABI_FP);
  1011. if (fp_abi == 1)
  1012. float_return = 1;
  1013. else if (fp_abi == 2)
  1014. float_return = 0;
  1015. }
  1016. #endif /* HAVE_ELF */
  1017. /* If there is already a candidate, use it. */
  1018. for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
  1019. best_arch != NULL;
  1020. best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
  1021. {
  1022. m68k_gdbarch_tdep *tdep
  1023. = (m68k_gdbarch_tdep *) gdbarch_tdep (best_arch->gdbarch);
  1024. if (flavour != tdep->flavour)
  1025. continue;
  1026. if (has_fp != tdep->fpregs_present)
  1027. continue;
  1028. if (float_return != tdep->float_return)
  1029. continue;
  1030. break;
  1031. }
  1032. if (best_arch != NULL)
  1033. return best_arch->gdbarch;
  1034. m68k_gdbarch_tdep *tdep = new m68k_gdbarch_tdep;
  1035. gdbarch = gdbarch_alloc (&info, tdep);
  1036. tdep->fpregs_present = has_fp;
  1037. tdep->float_return = float_return;
  1038. tdep->flavour = flavour;
  1039. if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
  1040. long_double_format = floatformats_ieee_double;
  1041. set_gdbarch_long_double_format (gdbarch, long_double_format);
  1042. set_gdbarch_long_double_bit (gdbarch, long_double_format[0]->totalsize);
  1043. set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
  1044. set_gdbarch_breakpoint_kind_from_pc (gdbarch, m68k_breakpoint::kind_from_pc);
  1045. set_gdbarch_sw_breakpoint_from_kind (gdbarch, m68k_breakpoint::bp_from_kind);
  1046. /* Stack grows down. */
  1047. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1048. set_gdbarch_frame_align (gdbarch, m68k_frame_align);
  1049. set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  1050. if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
  1051. set_gdbarch_decr_pc_after_break (gdbarch, 2);
  1052. set_gdbarch_frame_args_skip (gdbarch, 8);
  1053. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
  1054. set_gdbarch_register_type (gdbarch, m68k_register_type);
  1055. set_gdbarch_register_name (gdbarch, m68k_register_name);
  1056. set_gdbarch_num_regs (gdbarch, M68K_NUM_REGS);
  1057. set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
  1058. set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
  1059. set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
  1060. set_gdbarch_convert_register_p (gdbarch, m68k_convert_register_p);
  1061. set_gdbarch_register_to_value (gdbarch, m68k_register_to_value);
  1062. set_gdbarch_value_to_register (gdbarch, m68k_value_to_register);
  1063. if (has_fp)
  1064. set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
  1065. /* Function call & return. */
  1066. set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
  1067. set_gdbarch_return_value (gdbarch, m68k_return_value);
  1068. set_gdbarch_return_in_first_hidden_param_p (gdbarch,
  1069. m68k_return_in_first_hidden_param_p);
  1070. #if defined JB_PC && defined JB_ELEMENT_SIZE
  1071. tdep->jb_pc = JB_PC;
  1072. tdep->jb_elt_size = JB_ELEMENT_SIZE;
  1073. #else
  1074. tdep->jb_pc = -1;
  1075. #endif
  1076. tdep->pointer_result_regnum = M68K_D0_REGNUM;
  1077. tdep->struct_value_regnum = M68K_A1_REGNUM;
  1078. tdep->struct_return = reg_struct_return;
  1079. /* Frame unwinder. */
  1080. set_gdbarch_dummy_id (gdbarch, m68k_dummy_id);
  1081. set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
  1082. /* Hook in the DWARF CFI frame unwinder. */
  1083. dwarf2_append_unwinders (gdbarch);
  1084. frame_base_set_default (gdbarch, &m68k_frame_base);
  1085. /* Hook in ABI-specific overrides, if they have been registered. */
  1086. gdbarch_init_osabi (info, gdbarch);
  1087. /* Now we have tuned the configuration, set a few final things,
  1088. based on what the OS ABI has told us. */
  1089. if (tdep->jb_pc >= 0)
  1090. set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
  1091. frame_unwind_append_unwinder (gdbarch, &m68k_frame_unwind);
  1092. if (tdesc_data != nullptr)
  1093. tdesc_use_registers (gdbarch, info.target_desc, std::move (tdesc_data));
  1094. return gdbarch;
  1095. }
  1096. static void
  1097. m68k_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  1098. {
  1099. m68k_gdbarch_tdep *tdep = (m68k_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1100. if (tdep == NULL)
  1101. return;
  1102. }
  1103. /* OSABI sniffer for m68k. */
  1104. static enum gdb_osabi
  1105. m68k_osabi_sniffer (bfd *abfd)
  1106. {
  1107. unsigned int elfosabi = elf_elfheader (abfd)->e_ident[EI_OSABI];
  1108. if (elfosabi == ELFOSABI_NONE)
  1109. return GDB_OSABI_SVR4;
  1110. return GDB_OSABI_UNKNOWN;
  1111. }
  1112. void _initialize_m68k_tdep ();
  1113. void
  1114. _initialize_m68k_tdep ()
  1115. {
  1116. gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
  1117. gdbarch_register_osabi_sniffer (bfd_arch_m68k, bfd_target_elf_flavour,
  1118. m68k_osabi_sniffer);
  1119. gdbarch_register_osabi (bfd_arch_m68k, 0, GDB_OSABI_SVR4,
  1120. m68k_embedded_init_abi);
  1121. }