msp430-tdep.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /* Target-dependent code for the Texas Instruments MSP430 for GDB, the
  2. GNU debugger.
  3. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  4. Contributed by Red Hat, Inc.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "arch-utils.h"
  18. #include "prologue-value.h"
  19. #include "target.h"
  20. #include "regcache.h"
  21. #include "dis-asm.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "frame-unwind.h"
  25. #include "frame-base.h"
  26. #include "value.h"
  27. #include "gdbcore.h"
  28. #include "dwarf2/frame.h"
  29. #include "reggroups.h"
  30. #include "gdbarch.h"
  31. #include "elf/msp430.h"
  32. #include "opcode/msp430-decode.h"
  33. #include "elf-bfd.h"
  34. /* Register Numbers. */
  35. enum
  36. {
  37. MSP430_PC_RAW_REGNUM,
  38. MSP430_SP_RAW_REGNUM,
  39. MSP430_SR_RAW_REGNUM,
  40. MSP430_CG_RAW_REGNUM,
  41. MSP430_R4_RAW_REGNUM,
  42. MSP430_R5_RAW_REGNUM,
  43. MSP430_R6_RAW_REGNUM,
  44. MSP430_R7_RAW_REGNUM,
  45. MSP430_R8_RAW_REGNUM,
  46. MSP430_R9_RAW_REGNUM,
  47. MSP430_R10_RAW_REGNUM,
  48. MSP430_R11_RAW_REGNUM,
  49. MSP430_R12_RAW_REGNUM,
  50. MSP430_R13_RAW_REGNUM,
  51. MSP430_R14_RAW_REGNUM,
  52. MSP430_R15_RAW_REGNUM,
  53. MSP430_NUM_REGS,
  54. MSP430_PC_REGNUM = MSP430_NUM_REGS,
  55. MSP430_SP_REGNUM,
  56. MSP430_SR_REGNUM,
  57. MSP430_CG_REGNUM,
  58. MSP430_R4_REGNUM,
  59. MSP430_R5_REGNUM,
  60. MSP430_R6_REGNUM,
  61. MSP430_R7_REGNUM,
  62. MSP430_R8_REGNUM,
  63. MSP430_R9_REGNUM,
  64. MSP430_R10_REGNUM,
  65. MSP430_R11_REGNUM,
  66. MSP430_R12_REGNUM,
  67. MSP430_R13_REGNUM,
  68. MSP430_R14_REGNUM,
  69. MSP430_R15_REGNUM,
  70. MSP430_NUM_TOTAL_REGS,
  71. MSP430_NUM_PSEUDO_REGS = MSP430_NUM_TOTAL_REGS - MSP430_NUM_REGS
  72. };
  73. enum
  74. {
  75. /* TI MSP430 Architecture. */
  76. MSP_ISA_MSP430,
  77. /* TI MSP430X Architecture. */
  78. MSP_ISA_MSP430X
  79. };
  80. enum
  81. {
  82. /* The small code model limits code addresses to 16 bits. */
  83. MSP_SMALL_CODE_MODEL,
  84. /* The large code model uses 20 bit addresses for function
  85. pointers. These are stored in memory using four bytes (32 bits). */
  86. MSP_LARGE_CODE_MODEL
  87. };
  88. /* Architecture specific data. */
  89. struct msp430_gdbarch_tdep : gdbarch_tdep
  90. {
  91. /* The ELF header flags specify the multilib used. */
  92. int elf_flags = 0;
  93. /* One of MSP_ISA_MSP430 or MSP_ISA_MSP430X. */
  94. int isa = 0;
  95. /* One of MSP_SMALL_CODE_MODEL or MSP_LARGE_CODE_MODEL. If, at
  96. some point, we support different data models too, we'll probably
  97. structure things so that we can combine values using logical
  98. "or". */
  99. int code_model = 0;
  100. };
  101. /* This structure holds the results of a prologue analysis. */
  102. struct msp430_prologue
  103. {
  104. /* The offset from the frame base to the stack pointer --- always
  105. zero or negative.
  106. Calling this a "size" is a bit misleading, but given that the
  107. stack grows downwards, using offsets for everything keeps one
  108. from going completely sign-crazy: you never change anything's
  109. sign for an ADD instruction; always change the second operand's
  110. sign for a SUB instruction; and everything takes care of
  111. itself. */
  112. int frame_size;
  113. /* Non-zero if this function has initialized the frame pointer from
  114. the stack pointer, zero otherwise. */
  115. int has_frame_ptr;
  116. /* If has_frame_ptr is non-zero, this is the offset from the frame
  117. base to where the frame pointer points. This is always zero or
  118. negative. */
  119. int frame_ptr_offset;
  120. /* The address of the first instruction at which the frame has been
  121. set up and the arguments are where the debug info says they are
  122. --- as best as we can tell. */
  123. CORE_ADDR prologue_end;
  124. /* reg_offset[R] is the offset from the CFA at which register R is
  125. saved, or 1 if register R has not been saved. (Real values are
  126. always zero or negative.) */
  127. int reg_offset[MSP430_NUM_TOTAL_REGS];
  128. };
  129. /* Implement the "register_type" gdbarch method. */
  130. static struct type *
  131. msp430_register_type (struct gdbarch *gdbarch, int reg_nr)
  132. {
  133. if (reg_nr < MSP430_NUM_REGS)
  134. return builtin_type (gdbarch)->builtin_uint32;
  135. else if (reg_nr == MSP430_PC_REGNUM)
  136. return builtin_type (gdbarch)->builtin_func_ptr;
  137. else
  138. return builtin_type (gdbarch)->builtin_uint16;
  139. }
  140. /* Implement another version of the "register_type" gdbarch method
  141. for msp430x. */
  142. static struct type *
  143. msp430x_register_type (struct gdbarch *gdbarch, int reg_nr)
  144. {
  145. if (reg_nr < MSP430_NUM_REGS)
  146. return builtin_type (gdbarch)->builtin_uint32;
  147. else if (reg_nr == MSP430_PC_REGNUM)
  148. return builtin_type (gdbarch)->builtin_func_ptr;
  149. else
  150. return builtin_type (gdbarch)->builtin_uint32;
  151. }
  152. /* Implement the "register_name" gdbarch method. */
  153. static const char *
  154. msp430_register_name (struct gdbarch *gdbarch, int regnr)
  155. {
  156. static const char *const reg_names[] = {
  157. /* Raw registers. */
  158. "", "", "", "", "", "", "", "",
  159. "", "", "", "", "", "", "", "",
  160. /* Pseudo registers. */
  161. "pc", "sp", "sr", "cg", "r4", "r5", "r6", "r7",
  162. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  163. };
  164. return reg_names[regnr];
  165. }
  166. /* Implement the "register_reggroup_p" gdbarch method. */
  167. static int
  168. msp430_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  169. const struct reggroup *group)
  170. {
  171. if (group == all_reggroup)
  172. return 1;
  173. /* All other registers are saved and restored. */
  174. if (group == save_reggroup || group == restore_reggroup)
  175. return (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS);
  176. return group == general_reggroup;
  177. }
  178. /* Implement the "pseudo_register_read" gdbarch method. */
  179. static enum register_status
  180. msp430_pseudo_register_read (struct gdbarch *gdbarch,
  181. readable_regcache *regcache,
  182. int regnum, gdb_byte *buffer)
  183. {
  184. if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
  185. {
  186. enum register_status status;
  187. ULONGEST val;
  188. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  189. int regsize = register_size (gdbarch, regnum);
  190. int raw_regnum = regnum - MSP430_NUM_REGS;
  191. status = regcache->raw_read (raw_regnum, &val);
  192. if (status == REG_VALID)
  193. store_unsigned_integer (buffer, regsize, byte_order, val);
  194. return status;
  195. }
  196. else
  197. gdb_assert_not_reached ("invalid pseudo register number");
  198. }
  199. /* Implement the "pseudo_register_write" gdbarch method. */
  200. static void
  201. msp430_pseudo_register_write (struct gdbarch *gdbarch,
  202. struct regcache *regcache,
  203. int regnum, const gdb_byte *buffer)
  204. {
  205. if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
  206. {
  207. ULONGEST val;
  208. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  209. int regsize = register_size (gdbarch, regnum);
  210. int raw_regnum = regnum - MSP430_NUM_REGS;
  211. val = extract_unsigned_integer (buffer, regsize, byte_order);
  212. regcache_raw_write_unsigned (regcache, raw_regnum, val);
  213. }
  214. else
  215. gdb_assert_not_reached ("invalid pseudo register number");
  216. }
  217. /* Implement the `register_sim_regno' gdbarch method. */
  218. static int
  219. msp430_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  220. {
  221. gdb_assert (regnum < MSP430_NUM_REGS);
  222. /* So long as regnum is in [0, RL78_NUM_REGS), it's valid. We
  223. just want to override the default here which disallows register
  224. numbers which have no names. */
  225. return regnum;
  226. }
  227. constexpr gdb_byte msp430_break_insn[] = { 0x43, 0x43 };
  228. typedef BP_MANIPULATION (msp430_break_insn) msp430_breakpoint;
  229. /* Define a "handle" struct for fetching the next opcode. */
  230. struct msp430_get_opcode_byte_handle
  231. {
  232. CORE_ADDR pc;
  233. };
  234. /* Fetch a byte on behalf of the opcode decoder. HANDLE contains
  235. the memory address of the next byte to fetch. If successful,
  236. the address in the handle is updated and the byte fetched is
  237. returned as the value of the function. If not successful, -1
  238. is returned. */
  239. static int
  240. msp430_get_opcode_byte (void *handle)
  241. {
  242. struct msp430_get_opcode_byte_handle *opcdata
  243. = (struct msp430_get_opcode_byte_handle *) handle;
  244. int status;
  245. gdb_byte byte;
  246. status = target_read_memory (opcdata->pc, &byte, 1);
  247. if (status == 0)
  248. {
  249. opcdata->pc += 1;
  250. return byte;
  251. }
  252. else
  253. return -1;
  254. }
  255. /* Function for finding saved registers in a 'struct pv_area'; this
  256. function is passed to pv_area::scan.
  257. If VALUE is a saved register, ADDR says it was saved at a constant
  258. offset from the frame base, and SIZE indicates that the whole
  259. register was saved, record its offset. */
  260. static void
  261. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  262. {
  263. struct msp430_prologue *result = (struct msp430_prologue *) result_untyped;
  264. if (value.kind == pvk_register
  265. && value.k == 0
  266. && pv_is_register (addr, MSP430_SP_REGNUM)
  267. && size == register_size (target_gdbarch (), value.reg))
  268. result->reg_offset[value.reg] = addr.k;
  269. }
  270. /* Analyze a prologue starting at START_PC, going no further than
  271. LIMIT_PC. Fill in RESULT as appropriate. */
  272. static void
  273. msp430_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc,
  274. CORE_ADDR limit_pc, struct msp430_prologue *result)
  275. {
  276. CORE_ADDR pc, next_pc;
  277. int rn;
  278. pv_t reg[MSP430_NUM_TOTAL_REGS];
  279. CORE_ADDR after_last_frame_setup_insn = start_pc;
  280. msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  281. int code_model = tdep->code_model;
  282. int sz;
  283. memset (result, 0, sizeof (*result));
  284. for (rn = 0; rn < MSP430_NUM_TOTAL_REGS; rn++)
  285. {
  286. reg[rn] = pv_register (rn, 0);
  287. result->reg_offset[rn] = 1;
  288. }
  289. pv_area stack (MSP430_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  290. /* The call instruction has saved the return address on the stack. */
  291. sz = code_model == MSP_LARGE_CODE_MODEL ? 4 : 2;
  292. reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -sz);
  293. stack.store (reg[MSP430_SP_REGNUM], sz, reg[MSP430_PC_REGNUM]);
  294. pc = start_pc;
  295. while (pc < limit_pc)
  296. {
  297. int bytes_read;
  298. struct msp430_get_opcode_byte_handle opcode_handle;
  299. MSP430_Opcode_Decoded opc;
  300. opcode_handle.pc = pc;
  301. bytes_read = msp430_decode_opcode (pc, &opc, msp430_get_opcode_byte,
  302. &opcode_handle);
  303. next_pc = pc + bytes_read;
  304. if (opc.id == MSO_push && opc.op[0].type == MSP430_Operand_Register)
  305. {
  306. int rsrc = opc.op[0].reg;
  307. reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -2);
  308. stack.store (reg[MSP430_SP_REGNUM], 2, reg[rsrc]);
  309. after_last_frame_setup_insn = next_pc;
  310. }
  311. else if (opc.id == MSO_push /* PUSHM */
  312. && opc.op[0].type == MSP430_Operand_None
  313. && opc.op[1].type == MSP430_Operand_Register)
  314. {
  315. int rsrc = opc.op[1].reg;
  316. int count = opc.repeats + 1;
  317. int size = opc.size == 16 ? 2 : 4;
  318. while (count > 0)
  319. {
  320. reg[MSP430_SP_REGNUM]
  321. = pv_add_constant (reg[MSP430_SP_REGNUM], -size);
  322. stack.store (reg[MSP430_SP_REGNUM], size, reg[rsrc]);
  323. rsrc--;
  324. count--;
  325. }
  326. after_last_frame_setup_insn = next_pc;
  327. }
  328. else if (opc.id == MSO_sub
  329. && opc.op[0].type == MSP430_Operand_Register
  330. && opc.op[0].reg == MSR_SP
  331. && opc.op[1].type == MSP430_Operand_Immediate)
  332. {
  333. int addend = opc.op[1].addend;
  334. reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM],
  335. -addend);
  336. after_last_frame_setup_insn = next_pc;
  337. }
  338. else if (opc.id == MSO_mov
  339. && opc.op[0].type == MSP430_Operand_Immediate
  340. && 12 <= opc.op[0].reg && opc.op[0].reg <= 15)
  341. after_last_frame_setup_insn = next_pc;
  342. else
  343. {
  344. /* Terminate the prologue scan. */
  345. break;
  346. }
  347. pc = next_pc;
  348. }
  349. /* Is the frame size (offset, really) a known constant? */
  350. if (pv_is_register (reg[MSP430_SP_REGNUM], MSP430_SP_REGNUM))
  351. result->frame_size = reg[MSP430_SP_REGNUM].k;
  352. /* Record where all the registers were saved. */
  353. stack.scan (check_for_saved, result);
  354. result->prologue_end = after_last_frame_setup_insn;
  355. }
  356. /* Implement the "skip_prologue" gdbarch method. */
  357. static CORE_ADDR
  358. msp430_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  359. {
  360. const char *name;
  361. CORE_ADDR func_addr, func_end;
  362. struct msp430_prologue p;
  363. /* Try to find the extent of the function that contains PC. */
  364. if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  365. return pc;
  366. msp430_analyze_prologue (gdbarch, pc, func_end, &p);
  367. return p.prologue_end;
  368. }
  369. /* Given a frame described by THIS_FRAME, decode the prologue of its
  370. associated function if there is not cache entry as specified by
  371. THIS_PROLOGUE_CACHE. Save the decoded prologue in the cache and
  372. return that struct as the value of this function. */
  373. static struct msp430_prologue *
  374. msp430_analyze_frame_prologue (struct frame_info *this_frame,
  375. void **this_prologue_cache)
  376. {
  377. if (!*this_prologue_cache)
  378. {
  379. CORE_ADDR func_start, stop_addr;
  380. *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct msp430_prologue);
  381. func_start = get_frame_func (this_frame);
  382. stop_addr = get_frame_pc (this_frame);
  383. /* If we couldn't find any function containing the PC, then
  384. just initialize the prologue cache, but don't do anything. */
  385. if (!func_start)
  386. stop_addr = func_start;
  387. msp430_analyze_prologue (get_frame_arch (this_frame), func_start,
  388. stop_addr,
  389. (struct msp430_prologue *) *this_prologue_cache);
  390. }
  391. return (struct msp430_prologue *) *this_prologue_cache;
  392. }
  393. /* Given a frame and a prologue cache, return this frame's base. */
  394. static CORE_ADDR
  395. msp430_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
  396. {
  397. struct msp430_prologue *p
  398. = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
  399. CORE_ADDR sp = get_frame_register_unsigned (this_frame, MSP430_SP_REGNUM);
  400. return sp - p->frame_size;
  401. }
  402. /* Implement the "frame_this_id" method for unwinding frames. */
  403. static void
  404. msp430_this_id (struct frame_info *this_frame,
  405. void **this_prologue_cache, struct frame_id *this_id)
  406. {
  407. *this_id = frame_id_build (msp430_frame_base (this_frame,
  408. this_prologue_cache),
  409. get_frame_func (this_frame));
  410. }
  411. /* Implement the "frame_prev_register" method for unwinding frames. */
  412. static struct value *
  413. msp430_prev_register (struct frame_info *this_frame,
  414. void **this_prologue_cache, int regnum)
  415. {
  416. struct msp430_prologue *p
  417. = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
  418. CORE_ADDR frame_base = msp430_frame_base (this_frame, this_prologue_cache);
  419. if (regnum == MSP430_SP_REGNUM)
  420. return frame_unwind_got_constant (this_frame, regnum, frame_base);
  421. /* If prologue analysis says we saved this register somewhere,
  422. return a description of the stack slot holding it. */
  423. else if (p->reg_offset[regnum] != 1)
  424. {
  425. struct value *rv = frame_unwind_got_memory (this_frame, regnum,
  426. frame_base +
  427. p->reg_offset[regnum]);
  428. if (regnum == MSP430_PC_REGNUM)
  429. {
  430. ULONGEST pc = value_as_long (rv);
  431. return frame_unwind_got_constant (this_frame, regnum, pc);
  432. }
  433. return rv;
  434. }
  435. /* Otherwise, presume we haven't changed the value of this
  436. register, and get it from the next frame. */
  437. else
  438. return frame_unwind_got_register (this_frame, regnum, regnum);
  439. }
  440. static const struct frame_unwind msp430_unwind = {
  441. "msp430 prologue",
  442. NORMAL_FRAME,
  443. default_frame_unwind_stop_reason,
  444. msp430_this_id,
  445. msp430_prev_register,
  446. NULL,
  447. default_frame_sniffer
  448. };
  449. /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
  450. static int
  451. msp430_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  452. {
  453. if (reg >= 0 && reg < MSP430_NUM_REGS)
  454. return reg + MSP430_NUM_REGS;
  455. return -1;
  456. }
  457. /* Implement the "return_value" gdbarch method. */
  458. static enum return_value_convention
  459. msp430_return_value (struct gdbarch *gdbarch,
  460. struct value *function,
  461. struct type *valtype,
  462. struct regcache *regcache,
  463. gdb_byte *readbuf, const gdb_byte *writebuf)
  464. {
  465. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  466. LONGEST valtype_len = TYPE_LENGTH (valtype);
  467. msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  468. int code_model = tdep->code_model;
  469. if (TYPE_LENGTH (valtype) > 8
  470. || valtype->code () == TYPE_CODE_STRUCT
  471. || valtype->code () == TYPE_CODE_UNION)
  472. return RETURN_VALUE_STRUCT_CONVENTION;
  473. if (readbuf)
  474. {
  475. ULONGEST u;
  476. int argreg = MSP430_R12_REGNUM;
  477. int offset = 0;
  478. while (valtype_len > 0)
  479. {
  480. int size = 2;
  481. if (code_model == MSP_LARGE_CODE_MODEL
  482. && valtype->code () == TYPE_CODE_PTR)
  483. {
  484. size = 4;
  485. }
  486. regcache_cooked_read_unsigned (regcache, argreg, &u);
  487. store_unsigned_integer (readbuf + offset, size, byte_order, u);
  488. valtype_len -= size;
  489. offset += size;
  490. argreg++;
  491. }
  492. }
  493. if (writebuf)
  494. {
  495. ULONGEST u;
  496. int argreg = MSP430_R12_REGNUM;
  497. int offset = 0;
  498. while (valtype_len > 0)
  499. {
  500. int size = 2;
  501. if (code_model == MSP_LARGE_CODE_MODEL
  502. && valtype->code () == TYPE_CODE_PTR)
  503. {
  504. size = 4;
  505. }
  506. u = extract_unsigned_integer (writebuf + offset, size, byte_order);
  507. regcache_cooked_write_unsigned (regcache, argreg, u);
  508. valtype_len -= size;
  509. offset += size;
  510. argreg++;
  511. }
  512. }
  513. return RETURN_VALUE_REGISTER_CONVENTION;
  514. }
  515. /* Implement the "frame_align" gdbarch method. */
  516. static CORE_ADDR
  517. msp430_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  518. {
  519. return align_down (sp, 2);
  520. }
  521. /* Implement the "push_dummy_call" gdbarch method. */
  522. static CORE_ADDR
  523. msp430_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  524. struct regcache *regcache, CORE_ADDR bp_addr,
  525. int nargs, struct value **args, CORE_ADDR sp,
  526. function_call_return_method return_method,
  527. CORE_ADDR struct_addr)
  528. {
  529. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  530. int write_pass;
  531. int sp_off = 0;
  532. CORE_ADDR cfa;
  533. msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  534. int code_model = tdep->code_model;
  535. struct type *func_type = value_type (function);
  536. /* Dereference function pointer types. */
  537. while (func_type->code () == TYPE_CODE_PTR)
  538. func_type = TYPE_TARGET_TYPE (func_type);
  539. /* The end result had better be a function or a method. */
  540. gdb_assert (func_type->code () == TYPE_CODE_FUNC
  541. || func_type->code () == TYPE_CODE_METHOD);
  542. /* We make two passes; the first does the stack allocation,
  543. the second actually stores the arguments. */
  544. for (write_pass = 0; write_pass <= 1; write_pass++)
  545. {
  546. int i;
  547. int arg_reg = MSP430_R12_REGNUM;
  548. int args_on_stack = 0;
  549. if (write_pass)
  550. sp = align_down (sp - sp_off, 4);
  551. sp_off = 0;
  552. if (return_method == return_method_struct)
  553. {
  554. if (write_pass)
  555. regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
  556. arg_reg++;
  557. }
  558. /* Push the arguments. */
  559. for (i = 0; i < nargs; i++)
  560. {
  561. struct value *arg = args[i];
  562. const gdb_byte *arg_bits = value_contents_all (arg).data ();
  563. struct type *arg_type = check_typedef (value_type (arg));
  564. ULONGEST arg_size = TYPE_LENGTH (arg_type);
  565. int offset;
  566. int current_arg_on_stack;
  567. gdb_byte struct_addr_buf[4];
  568. current_arg_on_stack = 0;
  569. if (arg_type->code () == TYPE_CODE_STRUCT
  570. || arg_type->code () == TYPE_CODE_UNION)
  571. {
  572. /* Aggregates of any size are passed by reference. */
  573. store_unsigned_integer (struct_addr_buf, 4, byte_order,
  574. value_address (arg));
  575. arg_bits = struct_addr_buf;
  576. arg_size = (code_model == MSP_LARGE_CODE_MODEL) ? 4 : 2;
  577. }
  578. else
  579. {
  580. /* Scalars bigger than 8 bytes such as complex doubles are passed
  581. on the stack. */
  582. if (arg_size > 8)
  583. current_arg_on_stack = 1;
  584. }
  585. for (offset = 0; offset < arg_size; offset += 2)
  586. {
  587. /* The condition below prevents 8 byte scalars from being split
  588. between registers and memory (stack). It also prevents other
  589. splits once the stack has been written to. */
  590. if (!current_arg_on_stack
  591. && (arg_reg
  592. + ((arg_size == 8 || args_on_stack)
  593. ? ((arg_size - offset) / 2 - 1)
  594. : 0) <= MSP430_R15_REGNUM))
  595. {
  596. int size = 2;
  597. if (code_model == MSP_LARGE_CODE_MODEL
  598. && (arg_type->code () == TYPE_CODE_PTR
  599. || TYPE_IS_REFERENCE (arg_type)
  600. || arg_type->code () == TYPE_CODE_STRUCT
  601. || arg_type->code () == TYPE_CODE_UNION))
  602. {
  603. /* When using the large memory model, pointer,
  604. reference, struct, and union arguments are
  605. passed using the entire register. (As noted
  606. earlier, aggregates are always passed by
  607. reference.) */
  608. if (offset != 0)
  609. continue;
  610. size = 4;
  611. }
  612. if (write_pass)
  613. regcache_cooked_write_unsigned (regcache, arg_reg,
  614. extract_unsigned_integer
  615. (arg_bits + offset, size,
  616. byte_order));
  617. arg_reg++;
  618. }
  619. else
  620. {
  621. if (write_pass)
  622. write_memory (sp + sp_off, arg_bits + offset, 2);
  623. sp_off += 2;
  624. args_on_stack = 1;
  625. current_arg_on_stack = 1;
  626. }
  627. }
  628. }
  629. }
  630. /* Keep track of the stack address prior to pushing the return address.
  631. This is the value that we'll return. */
  632. cfa = sp;
  633. /* Push the return address. */
  634. {
  635. int sz = tdep->code_model == MSP_SMALL_CODE_MODEL ? 2 : 4;
  636. sp = sp - sz;
  637. write_memory_unsigned_integer (sp, sz, byte_order, bp_addr);
  638. }
  639. /* Update the stack pointer. */
  640. regcache_cooked_write_unsigned (regcache, MSP430_SP_REGNUM, sp);
  641. return cfa;
  642. }
  643. /* In order to keep code size small, the compiler may create epilogue
  644. code through which more than one function epilogue is routed. I.e.
  645. the epilogue and return may just be a branch to some common piece of
  646. code which is responsible for tearing down the frame and performing
  647. the return. These epilog (label) names will have the common prefix
  648. defined here. */
  649. static const char msp430_epilog_name_prefix[] = "__mspabi_func_epilog_";
  650. /* Implement the "in_return_stub" gdbarch method. */
  651. static int
  652. msp430_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc,
  653. const char *name)
  654. {
  655. return (name != NULL
  656. && startswith (name, msp430_epilog_name_prefix));
  657. }
  658. /* Implement the "skip_trampoline_code" gdbarch method. */
  659. static CORE_ADDR
  660. msp430_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  661. {
  662. struct bound_minimal_symbol bms;
  663. const char *stub_name;
  664. struct gdbarch *gdbarch = get_frame_arch (frame);
  665. bms = lookup_minimal_symbol_by_pc (pc);
  666. if (!bms.minsym)
  667. return pc;
  668. stub_name = bms.minsym->linkage_name ();
  669. msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  670. if (tdep->code_model == MSP_SMALL_CODE_MODEL
  671. && msp430_in_return_stub (gdbarch, pc, stub_name))
  672. {
  673. CORE_ADDR sp = get_frame_register_unsigned (frame, MSP430_SP_REGNUM);
  674. return read_memory_integer
  675. (sp + 2 * (stub_name[strlen (msp430_epilog_name_prefix)] - '0'),
  676. 2, gdbarch_byte_order (gdbarch));
  677. }
  678. return pc;
  679. }
  680. /* Allocate and initialize a gdbarch object. */
  681. static struct gdbarch *
  682. msp430_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  683. {
  684. struct gdbarch *gdbarch;
  685. int elf_flags, isa, code_model;
  686. /* Extract the elf_flags if available. */
  687. if (info.abfd != NULL
  688. && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  689. elf_flags = elf_elfheader (info.abfd)->e_flags;
  690. else
  691. elf_flags = 0;
  692. if (info.abfd != NULL)
  693. switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  694. OFBA_MSPABI_Tag_ISA))
  695. {
  696. case 1:
  697. isa = MSP_ISA_MSP430;
  698. code_model = MSP_SMALL_CODE_MODEL;
  699. break;
  700. case 2:
  701. isa = MSP_ISA_MSP430X;
  702. switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  703. OFBA_MSPABI_Tag_Code_Model))
  704. {
  705. case 1:
  706. code_model = MSP_SMALL_CODE_MODEL;
  707. break;
  708. case 2:
  709. code_model = MSP_LARGE_CODE_MODEL;
  710. break;
  711. default:
  712. internal_error (__FILE__, __LINE__,
  713. _("Unknown msp430x code memory model"));
  714. break;
  715. }
  716. break;
  717. case 0:
  718. /* This can happen when loading a previously dumped data structure.
  719. Use the ISA and code model from the current architecture, provided
  720. it's compatible. */
  721. {
  722. struct gdbarch *ca = get_current_arch ();
  723. if (ca && gdbarch_bfd_arch_info (ca)->arch == bfd_arch_msp430)
  724. {
  725. msp430_gdbarch_tdep *ca_tdep
  726. = (msp430_gdbarch_tdep *) gdbarch_tdep (ca);
  727. elf_flags = ca_tdep->elf_flags;
  728. isa = ca_tdep->isa;
  729. code_model = ca_tdep->code_model;
  730. break;
  731. }
  732. }
  733. /* Fall through. */
  734. default:
  735. error (_("Unknown msp430 isa"));
  736. break;
  737. }
  738. else
  739. {
  740. isa = MSP_ISA_MSP430;
  741. code_model = MSP_SMALL_CODE_MODEL;
  742. }
  743. /* Try to find the architecture in the list of already defined
  744. architectures. */
  745. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  746. arches != NULL;
  747. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  748. {
  749. msp430_gdbarch_tdep *candidate_tdep
  750. = (msp430_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  751. if (candidate_tdep->elf_flags != elf_flags
  752. || candidate_tdep->isa != isa
  753. || candidate_tdep->code_model != code_model)
  754. continue;
  755. return arches->gdbarch;
  756. }
  757. /* None found, create a new architecture from the information
  758. provided. */
  759. msp430_gdbarch_tdep *tdep = new msp430_gdbarch_tdep;
  760. gdbarch = gdbarch_alloc (&info, tdep);
  761. tdep->elf_flags = elf_flags;
  762. tdep->isa = isa;
  763. tdep->code_model = code_model;
  764. /* Registers. */
  765. set_gdbarch_num_regs (gdbarch, MSP430_NUM_REGS);
  766. set_gdbarch_num_pseudo_regs (gdbarch, MSP430_NUM_PSEUDO_REGS);
  767. set_gdbarch_register_name (gdbarch, msp430_register_name);
  768. if (isa == MSP_ISA_MSP430)
  769. set_gdbarch_register_type (gdbarch, msp430_register_type);
  770. else
  771. set_gdbarch_register_type (gdbarch, msp430x_register_type);
  772. set_gdbarch_pc_regnum (gdbarch, MSP430_PC_REGNUM);
  773. set_gdbarch_sp_regnum (gdbarch, MSP430_SP_REGNUM);
  774. set_gdbarch_register_reggroup_p (gdbarch, msp430_register_reggroup_p);
  775. set_gdbarch_pseudo_register_read (gdbarch, msp430_pseudo_register_read);
  776. set_gdbarch_pseudo_register_write (gdbarch, msp430_pseudo_register_write);
  777. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, msp430_dwarf2_reg_to_regnum);
  778. set_gdbarch_register_sim_regno (gdbarch, msp430_register_sim_regno);
  779. /* Data types. */
  780. set_gdbarch_char_signed (gdbarch, 0);
  781. set_gdbarch_short_bit (gdbarch, 16);
  782. set_gdbarch_int_bit (gdbarch, 16);
  783. set_gdbarch_long_bit (gdbarch, 32);
  784. set_gdbarch_long_long_bit (gdbarch, 64);
  785. if (code_model == MSP_SMALL_CODE_MODEL)
  786. {
  787. set_gdbarch_ptr_bit (gdbarch, 16);
  788. set_gdbarch_addr_bit (gdbarch, 16);
  789. }
  790. else /* MSP_LARGE_CODE_MODEL */
  791. {
  792. set_gdbarch_ptr_bit (gdbarch, 32);
  793. set_gdbarch_addr_bit (gdbarch, 32);
  794. }
  795. set_gdbarch_dwarf2_addr_size (gdbarch, 4);
  796. set_gdbarch_float_bit (gdbarch, 32);
  797. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  798. set_gdbarch_double_bit (gdbarch, 64);
  799. set_gdbarch_long_double_bit (gdbarch, 64);
  800. set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  801. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  802. /* Breakpoints. */
  803. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  804. msp430_breakpoint::kind_from_pc);
  805. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  806. msp430_breakpoint::bp_from_kind);
  807. set_gdbarch_decr_pc_after_break (gdbarch, 1);
  808. /* Frames, prologues, etc. */
  809. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  810. set_gdbarch_skip_prologue (gdbarch, msp430_skip_prologue);
  811. set_gdbarch_frame_align (gdbarch, msp430_frame_align);
  812. dwarf2_append_unwinders (gdbarch);
  813. frame_unwind_append_unwinder (gdbarch, &msp430_unwind);
  814. /* Dummy frames, return values. */
  815. set_gdbarch_push_dummy_call (gdbarch, msp430_push_dummy_call);
  816. set_gdbarch_return_value (gdbarch, msp430_return_value);
  817. /* Trampolines. */
  818. set_gdbarch_in_solib_return_trampoline (gdbarch, msp430_in_return_stub);
  819. set_gdbarch_skip_trampoline_code (gdbarch, msp430_skip_trampoline_code);
  820. /* Virtual tables. */
  821. set_gdbarch_vbit_in_delta (gdbarch, 0);
  822. return gdbarch;
  823. }
  824. /* Register the initialization routine. */
  825. void _initialize_msp430_tdep ();
  826. void
  827. _initialize_msp430_tdep ()
  828. {
  829. register_gdbarch_init (bfd_arch_msp430, msp430_gdbarch_init);
  830. }