frv-tdep.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /* Target-dependent code for the Fujitsu FR-V, for GDB, the GNU Debugger.
  2. Copyright (C) 2002-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 "inferior.h"
  16. #include "gdbcore.h"
  17. #include "arch-utils.h"
  18. #include "regcache.h"
  19. #include "frame.h"
  20. #include "frame-unwind.h"
  21. #include "frame-base.h"
  22. #include "trad-frame.h"
  23. #include "dis-asm.h"
  24. #include "sim-regno.h"
  25. #include "gdb/sim-frv.h"
  26. #include "opcodes/frv-desc.h" /* for the H_SPR_... enums */
  27. #include "symtab.h"
  28. #include "elf-bfd.h"
  29. #include "elf/frv.h"
  30. #include "osabi.h"
  31. #include "infcall.h"
  32. #include "solib.h"
  33. #include "frv-tdep.h"
  34. #include "objfiles.h"
  35. #include "gdbarch.h"
  36. struct frv_unwind_cache /* was struct frame_extra_info */
  37. {
  38. /* The previous frame's inner-most stack address. Used as this
  39. frame ID's stack_addr. */
  40. CORE_ADDR prev_sp;
  41. /* The frame's base, optionally used by the high-level debug info. */
  42. CORE_ADDR base;
  43. /* Table indicating the location of each and every register. */
  44. trad_frame_saved_reg *saved_regs;
  45. };
  46. /* A structure describing a particular variant of the FRV.
  47. We allocate and initialize one of these structures when we create
  48. the gdbarch object for a variant.
  49. At the moment, all the FR variants we support differ only in which
  50. registers are present; the portable code of GDB knows that
  51. registers whose names are the empty string don't exist, so the
  52. `register_names' array captures all the per-variant information we
  53. need.
  54. in the future, if we need to have per-variant maps for raw size,
  55. virtual type, etc., we should replace register_names with an array
  56. of structures, each of which gives all the necessary info for one
  57. register. Don't stick parallel arrays in here --- that's so
  58. Fortran. */
  59. struct frv_gdbarch_tdep : gdbarch_tdep
  60. {
  61. /* Which ABI is in use? */
  62. enum frv_abi frv_abi {};
  63. /* How many general-purpose registers does this variant have? */
  64. int num_gprs = 0;
  65. /* How many floating-point registers does this variant have? */
  66. int num_fprs = 0;
  67. /* How many hardware watchpoints can it support? */
  68. int num_hw_watchpoints = 0;
  69. /* How many hardware breakpoints can it support? */
  70. int num_hw_breakpoints = 0;
  71. /* Register names. */
  72. const char **register_names = nullptr;
  73. };
  74. /* Return the FR-V ABI associated with GDBARCH. */
  75. enum frv_abi
  76. frv_abi (struct gdbarch *gdbarch)
  77. {
  78. frv_gdbarch_tdep *tdep = (frv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  79. return tdep->frv_abi;
  80. }
  81. /* Fetch the interpreter and executable loadmap addresses (for shared
  82. library support) for the FDPIC ABI. Return 0 if successful, -1 if
  83. not. (E.g, -1 will be returned if the ABI isn't the FDPIC ABI.) */
  84. int
  85. frv_fdpic_loadmap_addresses (struct gdbarch *gdbarch, CORE_ADDR *interp_addr,
  86. CORE_ADDR *exec_addr)
  87. {
  88. if (frv_abi (gdbarch) != FRV_ABI_FDPIC)
  89. return -1;
  90. else
  91. {
  92. struct regcache *regcache = get_current_regcache ();
  93. if (interp_addr != NULL)
  94. {
  95. ULONGEST val;
  96. regcache_cooked_read_unsigned (regcache,
  97. fdpic_loadmap_interp_regnum, &val);
  98. *interp_addr = val;
  99. }
  100. if (exec_addr != NULL)
  101. {
  102. ULONGEST val;
  103. regcache_cooked_read_unsigned (regcache,
  104. fdpic_loadmap_exec_regnum, &val);
  105. *exec_addr = val;
  106. }
  107. return 0;
  108. }
  109. }
  110. /* Allocate a new variant structure, and set up default values for all
  111. the fields. */
  112. static frv_gdbarch_tdep *
  113. new_variant (void)
  114. {
  115. int r;
  116. frv_gdbarch_tdep *var = new frv_gdbarch_tdep;
  117. var->frv_abi = FRV_ABI_EABI;
  118. var->num_gprs = 64;
  119. var->num_fprs = 64;
  120. var->num_hw_watchpoints = 0;
  121. var->num_hw_breakpoints = 0;
  122. /* By default, don't supply any general-purpose or floating-point
  123. register names. */
  124. var->register_names
  125. = (const char **) xmalloc ((frv_num_regs + frv_num_pseudo_regs)
  126. * sizeof (const char *));
  127. for (r = 0; r < frv_num_regs + frv_num_pseudo_regs; r++)
  128. var->register_names[r] = "";
  129. /* Do, however, supply default names for the known special-purpose
  130. registers. */
  131. var->register_names[pc_regnum] = "pc";
  132. var->register_names[lr_regnum] = "lr";
  133. var->register_names[lcr_regnum] = "lcr";
  134. var->register_names[psr_regnum] = "psr";
  135. var->register_names[ccr_regnum] = "ccr";
  136. var->register_names[cccr_regnum] = "cccr";
  137. var->register_names[tbr_regnum] = "tbr";
  138. /* Debug registers. */
  139. var->register_names[brr_regnum] = "brr";
  140. var->register_names[dbar0_regnum] = "dbar0";
  141. var->register_names[dbar1_regnum] = "dbar1";
  142. var->register_names[dbar2_regnum] = "dbar2";
  143. var->register_names[dbar3_regnum] = "dbar3";
  144. /* iacc0 (Only found on MB93405.) */
  145. var->register_names[iacc0h_regnum] = "iacc0h";
  146. var->register_names[iacc0l_regnum] = "iacc0l";
  147. var->register_names[iacc0_regnum] = "iacc0";
  148. /* fsr0 (Found on FR555 and FR501.) */
  149. var->register_names[fsr0_regnum] = "fsr0";
  150. /* acc0 - acc7. The architecture provides for the possibility of many
  151. more (up to 64 total), but we don't want to make that big of a hole
  152. in the G packet. If we need more in the future, we'll add them
  153. elsewhere. */
  154. for (r = acc0_regnum; r <= acc7_regnum; r++)
  155. var->register_names[r]
  156. = xstrprintf ("acc%d", r - acc0_regnum).release ();
  157. /* accg0 - accg7: These are one byte registers. The remote protocol
  158. provides the raw values packed four into a slot. accg0123 and
  159. accg4567 correspond to accg0 - accg3 and accg4-accg7 respectively.
  160. We don't provide names for accg0123 and accg4567 since the user will
  161. likely not want to see these raw values. */
  162. for (r = accg0_regnum; r <= accg7_regnum; r++)
  163. var->register_names[r]
  164. = xstrprintf ("accg%d", r - accg0_regnum).release ();
  165. /* msr0 and msr1. */
  166. var->register_names[msr0_regnum] = "msr0";
  167. var->register_names[msr1_regnum] = "msr1";
  168. /* gner and fner registers. */
  169. var->register_names[gner0_regnum] = "gner0";
  170. var->register_names[gner1_regnum] = "gner1";
  171. var->register_names[fner0_regnum] = "fner0";
  172. var->register_names[fner1_regnum] = "fner1";
  173. return var;
  174. }
  175. /* Indicate that the variant VAR has NUM_GPRS general-purpose
  176. registers, and fill in the names array appropriately. */
  177. static void
  178. set_variant_num_gprs (frv_gdbarch_tdep *var, int num_gprs)
  179. {
  180. int r;
  181. var->num_gprs = num_gprs;
  182. for (r = 0; r < num_gprs; ++r)
  183. {
  184. char buf[20];
  185. xsnprintf (buf, sizeof (buf), "gr%d", r);
  186. var->register_names[first_gpr_regnum + r] = xstrdup (buf);
  187. }
  188. }
  189. /* Indicate that the variant VAR has NUM_FPRS floating-point
  190. registers, and fill in the names array appropriately. */
  191. static void
  192. set_variant_num_fprs (frv_gdbarch_tdep *var, int num_fprs)
  193. {
  194. int r;
  195. var->num_fprs = num_fprs;
  196. for (r = 0; r < num_fprs; ++r)
  197. {
  198. char buf[20];
  199. xsnprintf (buf, sizeof (buf), "fr%d", r);
  200. var->register_names[first_fpr_regnum + r] = xstrdup (buf);
  201. }
  202. }
  203. static void
  204. set_variant_abi_fdpic (frv_gdbarch_tdep *var)
  205. {
  206. var->frv_abi = FRV_ABI_FDPIC;
  207. var->register_names[fdpic_loadmap_exec_regnum] = xstrdup ("loadmap_exec");
  208. var->register_names[fdpic_loadmap_interp_regnum]
  209. = xstrdup ("loadmap_interp");
  210. }
  211. static void
  212. set_variant_scratch_registers (frv_gdbarch_tdep *var)
  213. {
  214. var->register_names[scr0_regnum] = xstrdup ("scr0");
  215. var->register_names[scr1_regnum] = xstrdup ("scr1");
  216. var->register_names[scr2_regnum] = xstrdup ("scr2");
  217. var->register_names[scr3_regnum] = xstrdup ("scr3");
  218. }
  219. static const char *
  220. frv_register_name (struct gdbarch *gdbarch, int reg)
  221. {
  222. if (reg < 0)
  223. return "?toosmall?";
  224. if (reg >= frv_num_regs + frv_num_pseudo_regs)
  225. return "?toolarge?";
  226. frv_gdbarch_tdep *tdep = (frv_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  227. return tdep->register_names[reg];
  228. }
  229. static struct type *
  230. frv_register_type (struct gdbarch *gdbarch, int reg)
  231. {
  232. if (reg >= first_fpr_regnum && reg <= last_fpr_regnum)
  233. return builtin_type (gdbarch)->builtin_float;
  234. else if (reg == iacc0_regnum)
  235. return builtin_type (gdbarch)->builtin_int64;
  236. else
  237. return builtin_type (gdbarch)->builtin_int32;
  238. }
  239. static enum register_status
  240. frv_pseudo_register_read (struct gdbarch *gdbarch, readable_regcache *regcache,
  241. int reg, gdb_byte *buffer)
  242. {
  243. enum register_status status;
  244. if (reg == iacc0_regnum)
  245. {
  246. status = regcache->raw_read (iacc0h_regnum, buffer);
  247. if (status == REG_VALID)
  248. status = regcache->raw_read (iacc0l_regnum, (bfd_byte *) buffer + 4);
  249. }
  250. else if (accg0_regnum <= reg && reg <= accg7_regnum)
  251. {
  252. /* The accg raw registers have four values in each slot with the
  253. lowest register number occupying the first byte. */
  254. int raw_regnum = accg0123_regnum + (reg - accg0_regnum) / 4;
  255. int byte_num = (reg - accg0_regnum) % 4;
  256. gdb_byte buf[4];
  257. status = regcache->raw_read (raw_regnum, buf);
  258. if (status == REG_VALID)
  259. {
  260. memset (buffer, 0, 4);
  261. /* FR-V is big endian, so put the requested byte in the
  262. first byte of the buffer allocated to hold the
  263. pseudo-register. */
  264. buffer[0] = buf[byte_num];
  265. }
  266. }
  267. else
  268. gdb_assert_not_reached ("invalid pseudo register number");
  269. return status;
  270. }
  271. static void
  272. frv_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  273. int reg, const gdb_byte *buffer)
  274. {
  275. if (reg == iacc0_regnum)
  276. {
  277. regcache->raw_write (iacc0h_regnum, buffer);
  278. regcache->raw_write (iacc0l_regnum, (bfd_byte *) buffer + 4);
  279. }
  280. else if (accg0_regnum <= reg && reg <= accg7_regnum)
  281. {
  282. /* The accg raw registers have four values in each slot with the
  283. lowest register number occupying the first byte. */
  284. int raw_regnum = accg0123_regnum + (reg - accg0_regnum) / 4;
  285. int byte_num = (reg - accg0_regnum) % 4;
  286. gdb_byte buf[4];
  287. regcache->raw_read (raw_regnum, buf);
  288. buf[byte_num] = ((bfd_byte *) buffer)[0];
  289. regcache->raw_write (raw_regnum, buf);
  290. }
  291. }
  292. static int
  293. frv_register_sim_regno (struct gdbarch *gdbarch, int reg)
  294. {
  295. static const int spr_map[] =
  296. {
  297. H_SPR_PSR, /* psr_regnum */
  298. H_SPR_CCR, /* ccr_regnum */
  299. H_SPR_CCCR, /* cccr_regnum */
  300. -1, /* fdpic_loadmap_exec_regnum */
  301. -1, /* fdpic_loadmap_interp_regnum */
  302. -1, /* 134 */
  303. H_SPR_TBR, /* tbr_regnum */
  304. H_SPR_BRR, /* brr_regnum */
  305. H_SPR_DBAR0, /* dbar0_regnum */
  306. H_SPR_DBAR1, /* dbar1_regnum */
  307. H_SPR_DBAR2, /* dbar2_regnum */
  308. H_SPR_DBAR3, /* dbar3_regnum */
  309. H_SPR_SCR0, /* scr0_regnum */
  310. H_SPR_SCR1, /* scr1_regnum */
  311. H_SPR_SCR2, /* scr2_regnum */
  312. H_SPR_SCR3, /* scr3_regnum */
  313. H_SPR_LR, /* lr_regnum */
  314. H_SPR_LCR, /* lcr_regnum */
  315. H_SPR_IACC0H, /* iacc0h_regnum */
  316. H_SPR_IACC0L, /* iacc0l_regnum */
  317. H_SPR_FSR0, /* fsr0_regnum */
  318. /* FIXME: Add infrastructure for fetching/setting ACC and ACCG regs. */
  319. -1, /* acc0_regnum */
  320. -1, /* acc1_regnum */
  321. -1, /* acc2_regnum */
  322. -1, /* acc3_regnum */
  323. -1, /* acc4_regnum */
  324. -1, /* acc5_regnum */
  325. -1, /* acc6_regnum */
  326. -1, /* acc7_regnum */
  327. -1, /* acc0123_regnum */
  328. -1, /* acc4567_regnum */
  329. H_SPR_MSR0, /* msr0_regnum */
  330. H_SPR_MSR1, /* msr1_regnum */
  331. H_SPR_GNER0, /* gner0_regnum */
  332. H_SPR_GNER1, /* gner1_regnum */
  333. H_SPR_FNER0, /* fner0_regnum */
  334. H_SPR_FNER1, /* fner1_regnum */
  335. };
  336. gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));
  337. if (first_gpr_regnum <= reg && reg <= last_gpr_regnum)
  338. return reg - first_gpr_regnum + SIM_FRV_GR0_REGNUM;
  339. else if (first_fpr_regnum <= reg && reg <= last_fpr_regnum)
  340. return reg - first_fpr_regnum + SIM_FRV_FR0_REGNUM;
  341. else if (pc_regnum == reg)
  342. return SIM_FRV_PC_REGNUM;
  343. else if (reg >= first_spr_regnum
  344. && reg < first_spr_regnum + sizeof (spr_map) / sizeof (spr_map[0]))
  345. {
  346. int spr_reg_offset = spr_map[reg - first_spr_regnum];
  347. if (spr_reg_offset < 0)
  348. return SIM_REGNO_DOES_NOT_EXIST;
  349. else
  350. return SIM_FRV_SPR0_REGNUM + spr_reg_offset;
  351. }
  352. internal_error (__FILE__, __LINE__, _("Bad register number %d"), reg);
  353. }
  354. constexpr gdb_byte frv_break_insn[] = {0xc0, 0x70, 0x00, 0x01};
  355. typedef BP_MANIPULATION (frv_break_insn) frv_breakpoint;
  356. /* Define the maximum number of instructions which may be packed into a
  357. bundle (VLIW instruction). */
  358. static const int max_instrs_per_bundle = 8;
  359. /* Define the size (in bytes) of an FR-V instruction. */
  360. static const int frv_instr_size = 4;
  361. /* Adjust a breakpoint's address to account for the FR-V architecture's
  362. constraint that a break instruction must not appear as any but the
  363. first instruction in the bundle. */
  364. static CORE_ADDR
  365. frv_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
  366. {
  367. int count = max_instrs_per_bundle;
  368. CORE_ADDR addr = bpaddr - frv_instr_size;
  369. CORE_ADDR func_start = get_pc_function_start (bpaddr);
  370. /* Find the end of the previous packing sequence. This will be indicated
  371. by either attempting to access some inaccessible memory or by finding
  372. an instruction word whose packing bit is set to one. */
  373. while (count-- > 0 && addr >= func_start)
  374. {
  375. gdb_byte instr[frv_instr_size];
  376. int status;
  377. status = target_read_memory (addr, instr, sizeof instr);
  378. if (status != 0)
  379. break;
  380. /* This is a big endian architecture, so byte zero will have most
  381. significant byte. The most significant bit of this byte is the
  382. packing bit. */
  383. if (instr[0] & 0x80)
  384. break;
  385. addr -= frv_instr_size;
  386. }
  387. if (count > 0)
  388. bpaddr = addr + frv_instr_size;
  389. return bpaddr;
  390. }
  391. /* Return true if REG is a caller-saves ("scratch") register,
  392. false otherwise. */
  393. static int
  394. is_caller_saves_reg (int reg)
  395. {
  396. return ((4 <= reg && reg <= 7)
  397. || (14 <= reg && reg <= 15)
  398. || (32 <= reg && reg <= 47));
  399. }
  400. /* Return true if REG is a callee-saves register, false otherwise. */
  401. static int
  402. is_callee_saves_reg (int reg)
  403. {
  404. return ((16 <= reg && reg <= 31)
  405. || (48 <= reg && reg <= 63));
  406. }
  407. /* Return true if REG is an argument register, false otherwise. */
  408. static int
  409. is_argument_reg (int reg)
  410. {
  411. return (8 <= reg && reg <= 13);
  412. }
  413. /* Scan an FR-V prologue, starting at PC, until frame->PC.
  414. If FRAME is non-zero, fill in its saved_regs with appropriate addresses.
  415. We assume FRAME's saved_regs array has already been allocated and cleared.
  416. Return the first PC value after the prologue.
  417. Note that, for unoptimized code, we almost don't need this function
  418. at all; all arguments and locals live on the stack, so we just need
  419. the FP to find everything. The catch: structures passed by value
  420. have their addresses living in registers; they're never spilled to
  421. the stack. So if you ever want to be able to get to these
  422. arguments in any frame but the top, you'll need to do this serious
  423. prologue analysis. */
  424. static CORE_ADDR
  425. frv_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  426. struct frame_info *this_frame,
  427. struct frv_unwind_cache *info)
  428. {
  429. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  430. /* When writing out instruction bitpatterns, we use the following
  431. letters to label instruction fields:
  432. P - The parallel bit. We don't use this.
  433. J - The register number of GRj in the instruction description.
  434. K - The register number of GRk in the instruction description.
  435. I - The register number of GRi.
  436. S - a signed immediate offset.
  437. U - an unsigned immediate offset.
  438. The dots below the numbers indicate where hex digit boundaries
  439. fall, to make it easier to check the numbers. */
  440. /* Non-zero iff we've seen the instruction that initializes the
  441. frame pointer for this function's frame. */
  442. int fp_set = 0;
  443. /* If fp_set is non_zero, then this is the distance from
  444. the stack pointer to frame pointer: fp = sp + fp_offset. */
  445. int fp_offset = 0;
  446. /* Total size of frame prior to any alloca operations. */
  447. int framesize = 0;
  448. /* Flag indicating if lr has been saved on the stack. */
  449. int lr_saved_on_stack = 0;
  450. /* The number of the general-purpose register we saved the return
  451. address ("link register") in, or -1 if we haven't moved it yet. */
  452. int lr_save_reg = -1;
  453. /* Offset (from sp) at which lr has been saved on the stack. */
  454. int lr_sp_offset = 0;
  455. /* If gr_saved[i] is non-zero, then we've noticed that general
  456. register i has been saved at gr_sp_offset[i] from the stack
  457. pointer. */
  458. char gr_saved[64];
  459. int gr_sp_offset[64];
  460. /* The address of the most recently scanned prologue instruction. */
  461. CORE_ADDR last_prologue_pc;
  462. /* The address of the next instruction. */
  463. CORE_ADDR next_pc;
  464. /* The upper bound to of the pc values to scan. */
  465. CORE_ADDR lim_pc;
  466. memset (gr_saved, 0, sizeof (gr_saved));
  467. last_prologue_pc = pc;
  468. /* Try to compute an upper limit (on how far to scan) based on the
  469. line number info. */
  470. lim_pc = skip_prologue_using_sal (gdbarch, pc);
  471. /* If there's no line number info, lim_pc will be 0. In that case,
  472. set the limit to be 100 instructions away from pc. Hopefully, this
  473. will be far enough away to account for the entire prologue. Don't
  474. worry about overshooting the end of the function. The scan loop
  475. below contains some checks to avoid scanning unreasonably far. */
  476. if (lim_pc == 0)
  477. lim_pc = pc + 400;
  478. /* If we have a frame, we don't want to scan past the frame's pc. This
  479. will catch those cases where the pc is in the prologue. */
  480. if (this_frame)
  481. {
  482. CORE_ADDR frame_pc = get_frame_pc (this_frame);
  483. if (frame_pc < lim_pc)
  484. lim_pc = frame_pc;
  485. }
  486. /* Scan the prologue. */
  487. while (pc < lim_pc)
  488. {
  489. gdb_byte buf[frv_instr_size];
  490. LONGEST op;
  491. if (target_read_memory (pc, buf, sizeof buf) != 0)
  492. break;
  493. op = extract_signed_integer (buf, byte_order);
  494. next_pc = pc + 4;
  495. /* The tests in this chain of ifs should be in order of
  496. decreasing selectivity, so that more particular patterns get
  497. to fire before less particular patterns. */
  498. /* Some sort of control transfer instruction: stop scanning prologue.
  499. Integer Conditional Branch:
  500. X XXXX XX 0000110 XX XXXXXXXXXXXXXXXX
  501. Floating-point / media Conditional Branch:
  502. X XXXX XX 0000111 XX XXXXXXXXXXXXXXXX
  503. LCR Conditional Branch to LR
  504. X XXXX XX 0001110 XX XX 001 X XXXXXXXXXX
  505. Integer conditional Branches to LR
  506. X XXXX XX 0001110 XX XX 010 X XXXXXXXXXX
  507. X XXXX XX 0001110 XX XX 011 X XXXXXXXXXX
  508. Floating-point/Media Branches to LR
  509. X XXXX XX 0001110 XX XX 110 X XXXXXXXXXX
  510. X XXXX XX 0001110 XX XX 111 X XXXXXXXXXX
  511. Jump and Link
  512. X XXXXX X 0001100 XXXXXX XXXXXX XXXXXX
  513. X XXXXX X 0001101 XXXXXX XXXXXX XXXXXX
  514. Call
  515. X XXXXXX 0001111 XXXXXXXXXXXXXXXXXX
  516. Return from Trap
  517. X XXXXX X 0000101 XXXXXX XXXXXX XXXXXX
  518. Integer Conditional Trap
  519. X XXXX XX 0000100 XXXXXX XXXX 00 XXXXXX
  520. X XXXX XX 0011100 XXXXXX XXXXXXXXXXXX
  521. Floating-point /media Conditional Trap
  522. X XXXX XX 0000100 XXXXXX XXXX 01 XXXXXX
  523. X XXXX XX 0011101 XXXXXX XXXXXXXXXXXX
  524. Break
  525. X XXXX XX 0000100 XXXXXX XXXX 11 XXXXXX
  526. Media Trap
  527. X XXXX XX 0000100 XXXXXX XXXX 10 XXXXXX */
  528. if ((op & 0x01d80000) == 0x00180000 /* Conditional branches and Call */
  529. || (op & 0x01f80000) == 0x00300000 /* Jump and Link */
  530. || (op & 0x01f80000) == 0x00100000 /* Return from Trap, Trap */
  531. || (op & 0x01f80000) == 0x00700000) /* Trap immediate */
  532. {
  533. /* Stop scanning; not in prologue any longer. */
  534. break;
  535. }
  536. /* Loading something from memory into fp probably means that
  537. we're in the epilogue. Stop scanning the prologue.
  538. ld @(GRi, GRk), fp
  539. X 000010 0000010 XXXXXX 000100 XXXXXX
  540. ldi @(GRi, d12), fp
  541. X 000010 0110010 XXXXXX XXXXXXXXXXXX */
  542. else if ((op & 0x7ffc0fc0) == 0x04080100
  543. || (op & 0x7ffc0000) == 0x04c80000)
  544. {
  545. break;
  546. }
  547. /* Setting the FP from the SP:
  548. ori sp, 0, fp
  549. P 000010 0100010 000001 000000000000 = 0x04881000
  550. 0 111111 1111111 111111 111111111111 = 0x7fffffff
  551. . . . . . . . .
  552. We treat this as part of the prologue. */
  553. else if ((op & 0x7fffffff) == 0x04881000)
  554. {
  555. fp_set = 1;
  556. fp_offset = 0;
  557. last_prologue_pc = next_pc;
  558. }
  559. /* Move the link register to the scratch register grJ, before saving:
  560. movsg lr, grJ
  561. P 000100 0000011 010000 000111 JJJJJJ = 0x080d01c0
  562. 0 111111 1111111 111111 111111 000000 = 0x7fffffc0
  563. . . . . . . . .
  564. We treat this as part of the prologue. */
  565. else if ((op & 0x7fffffc0) == 0x080d01c0)
  566. {
  567. int gr_j = op & 0x3f;
  568. /* If we're moving it to a scratch register, that's fine. */
  569. if (is_caller_saves_reg (gr_j))
  570. {
  571. lr_save_reg = gr_j;
  572. last_prologue_pc = next_pc;
  573. }
  574. }
  575. /* To save multiple callee-saves registers on the stack, at
  576. offset zero:
  577. std grK,@(sp,gr0)
  578. P KKKKKK 0000011 000001 000011 000000 = 0x000c10c0
  579. 0 000000 1111111 111111 111111 111111 = 0x01ffffff
  580. stq grK,@(sp,gr0)
  581. P KKKKKK 0000011 000001 000100 000000 = 0x000c1100
  582. 0 000000 1111111 111111 111111 111111 = 0x01ffffff
  583. . . . . . . . .
  584. We treat this as part of the prologue, and record the register's
  585. saved address in the frame structure. */
  586. else if ((op & 0x01ffffff) == 0x000c10c0
  587. || (op & 0x01ffffff) == 0x000c1100)
  588. {
  589. int gr_k = ((op >> 25) & 0x3f);
  590. int ope = ((op >> 6) & 0x3f);
  591. int count;
  592. int i;
  593. /* Is it an std or an stq? */
  594. if (ope == 0x03)
  595. count = 2;
  596. else
  597. count = 4;
  598. /* Is it really a callee-saves register? */
  599. if (is_callee_saves_reg (gr_k))
  600. {
  601. for (i = 0; i < count; i++)
  602. {
  603. gr_saved[gr_k + i] = 1;
  604. gr_sp_offset[gr_k + i] = 4 * i;
  605. }
  606. last_prologue_pc = next_pc;
  607. }
  608. }
  609. /* Adjusting the stack pointer. (The stack pointer is GR1.)
  610. addi sp, S, sp
  611. P 000001 0010000 000001 SSSSSSSSSSSS = 0x02401000
  612. 0 111111 1111111 111111 000000000000 = 0x7ffff000
  613. . . . . . . . .
  614. We treat this as part of the prologue. */
  615. else if ((op & 0x7ffff000) == 0x02401000)
  616. {
  617. if (framesize == 0)
  618. {
  619. /* Sign-extend the twelve-bit field.
  620. (Isn't there a better way to do this?) */
  621. int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
  622. framesize -= s;
  623. last_prologue_pc = pc;
  624. }
  625. else
  626. {
  627. /* If the prologue is being adjusted again, we've
  628. likely gone too far; i.e. we're probably in the
  629. epilogue. */
  630. break;
  631. }
  632. }
  633. /* Setting the FP to a constant distance from the SP:
  634. addi sp, S, fp
  635. P 000010 0010000 000001 SSSSSSSSSSSS = 0x04401000
  636. 0 111111 1111111 111111 000000000000 = 0x7ffff000
  637. . . . . . . . .
  638. We treat this as part of the prologue. */
  639. else if ((op & 0x7ffff000) == 0x04401000)
  640. {
  641. /* Sign-extend the twelve-bit field.
  642. (Isn't there a better way to do this?) */
  643. int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
  644. fp_set = 1;
  645. fp_offset = s;
  646. last_prologue_pc = pc;
  647. }
  648. /* To spill an argument register to a scratch register:
  649. ori GRi, 0, GRk
  650. P KKKKKK 0100010 IIIIII 000000000000 = 0x00880000
  651. 0 000000 1111111 000000 111111111111 = 0x01fc0fff
  652. . . . . . . . .
  653. For the time being, we treat this as a prologue instruction,
  654. assuming that GRi is an argument register. This one's kind
  655. of suspicious, because it seems like it could be part of a
  656. legitimate body instruction. But we only come here when the
  657. source info wasn't helpful, so we have to do the best we can.
  658. Hopefully once GCC and GDB agree on how to emit line number
  659. info for prologues, then this code will never come into play. */
  660. else if ((op & 0x01fc0fff) == 0x00880000)
  661. {
  662. int gr_i = ((op >> 12) & 0x3f);
  663. /* Make sure that the source is an arg register; if it is, we'll
  664. treat it as a prologue instruction. */
  665. if (is_argument_reg (gr_i))
  666. last_prologue_pc = next_pc;
  667. }
  668. /* To spill 16-bit values to the stack:
  669. sthi GRk, @(fp, s)
  670. P KKKKKK 1010001 000010 SSSSSSSSSSSS = 0x01442000
  671. 0 000000 1111111 111111 000000000000 = 0x01fff000
  672. . . . . . . . .
  673. And for 8-bit values, we use STB instructions.
  674. stbi GRk, @(fp, s)
  675. P KKKKKK 1010000 000010 SSSSSSSSSSSS = 0x01402000
  676. 0 000000 1111111 111111 000000000000 = 0x01fff000
  677. . . . . . . . .
  678. We check that GRk is really an argument register, and treat
  679. all such as part of the prologue. */
  680. else if ( (op & 0x01fff000) == 0x01442000
  681. || (op & 0x01fff000) == 0x01402000)
  682. {
  683. int gr_k = ((op >> 25) & 0x3f);
  684. /* Make sure that GRk is really an argument register; treat
  685. it as a prologue instruction if so. */
  686. if (is_argument_reg (gr_k))
  687. last_prologue_pc = next_pc;
  688. }
  689. /* To save multiple callee-saves register on the stack, at a
  690. non-zero offset:
  691. stdi GRk, @(sp, s)
  692. P KKKKKK 1010011 000001 SSSSSSSSSSSS = 0x014c1000
  693. 0 000000 1111111 111111 000000000000 = 0x01fff000
  694. . . . . . . . .
  695. stqi GRk, @(sp, s)
  696. P KKKKKK 1010100 000001 SSSSSSSSSSSS = 0x01501000
  697. 0 000000 1111111 111111 000000000000 = 0x01fff000
  698. . . . . . . . .
  699. We treat this as part of the prologue, and record the register's
  700. saved address in the frame structure. */
  701. else if ((op & 0x01fff000) == 0x014c1000
  702. || (op & 0x01fff000) == 0x01501000)
  703. {
  704. int gr_k = ((op >> 25) & 0x3f);
  705. int count;
  706. int i;
  707. /* Is it a stdi or a stqi? */
  708. if ((op & 0x01fff000) == 0x014c1000)
  709. count = 2;
  710. else
  711. count = 4;
  712. /* Is it really a callee-saves register? */
  713. if (is_callee_saves_reg (gr_k))
  714. {
  715. /* Sign-extend the twelve-bit field.
  716. (Isn't there a better way to do this?) */
  717. int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
  718. for (i = 0; i < count; i++)
  719. {
  720. gr_saved[gr_k + i] = 1;
  721. gr_sp_offset[gr_k + i] = s + (4 * i);
  722. }
  723. last_prologue_pc = next_pc;
  724. }
  725. }
  726. /* Storing any kind of integer register at any constant offset
  727. from any other register.
  728. st GRk, @(GRi, gr0)
  729. P KKKKKK 0000011 IIIIII 000010 000000 = 0x000c0080
  730. 0 000000 1111111 000000 111111 111111 = 0x01fc0fff
  731. . . . . . . . .
  732. sti GRk, @(GRi, d12)
  733. P KKKKKK 1010010 IIIIII SSSSSSSSSSSS = 0x01480000
  734. 0 000000 1111111 000000 000000000000 = 0x01fc0000
  735. . . . . . . . .
  736. These could be almost anything, but a lot of prologue
  737. instructions fall into this pattern, so let's decode the
  738. instruction once, and then work at a higher level. */
  739. else if (((op & 0x01fc0fff) == 0x000c0080)
  740. || ((op & 0x01fc0000) == 0x01480000))
  741. {
  742. int gr_k = ((op >> 25) & 0x3f);
  743. int gr_i = ((op >> 12) & 0x3f);
  744. int offset;
  745. /* Are we storing with gr0 as an offset, or using an
  746. immediate value? */
  747. if ((op & 0x01fc0fff) == 0x000c0080)
  748. offset = 0;
  749. else
  750. offset = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
  751. /* If the address isn't relative to the SP or FP, it's not a
  752. prologue instruction. */
  753. if (gr_i != sp_regnum && gr_i != fp_regnum)
  754. {
  755. /* Do nothing; not a prologue instruction. */
  756. }
  757. /* Saving the old FP in the new frame (relative to the SP). */
  758. else if (gr_k == fp_regnum && gr_i == sp_regnum)
  759. {
  760. gr_saved[fp_regnum] = 1;
  761. gr_sp_offset[fp_regnum] = offset;
  762. last_prologue_pc = next_pc;
  763. }
  764. /* Saving callee-saves register(s) on the stack, relative to
  765. the SP. */
  766. else if (gr_i == sp_regnum
  767. && is_callee_saves_reg (gr_k))
  768. {
  769. gr_saved[gr_k] = 1;
  770. if (gr_i == sp_regnum)
  771. gr_sp_offset[gr_k] = offset;
  772. else
  773. gr_sp_offset[gr_k] = offset + fp_offset;
  774. last_prologue_pc = next_pc;
  775. }
  776. /* Saving the scratch register holding the return address. */
  777. else if (lr_save_reg != -1
  778. && gr_k == lr_save_reg)
  779. {
  780. lr_saved_on_stack = 1;
  781. if (gr_i == sp_regnum)
  782. lr_sp_offset = offset;
  783. else
  784. lr_sp_offset = offset + fp_offset;
  785. last_prologue_pc = next_pc;
  786. }
  787. /* Spilling int-sized arguments to the stack. */
  788. else if (is_argument_reg (gr_k))
  789. last_prologue_pc = next_pc;
  790. }
  791. pc = next_pc;
  792. }
  793. if (this_frame && info)
  794. {
  795. int i;
  796. ULONGEST this_base;
  797. /* If we know the relationship between the stack and frame
  798. pointers, record the addresses of the registers we noticed.
  799. Note that we have to do this as a separate step at the end,
  800. because instructions may save relative to the SP, but we need
  801. their addresses relative to the FP. */
  802. if (fp_set)
  803. this_base = get_frame_register_unsigned (this_frame, fp_regnum);
  804. else
  805. this_base = get_frame_register_unsigned (this_frame, sp_regnum);
  806. for (i = 0; i < 64; i++)
  807. if (gr_saved[i])
  808. info->saved_regs[i].set_addr (this_base - fp_offset
  809. + gr_sp_offset[i]);
  810. info->prev_sp = this_base - fp_offset + framesize;
  811. info->base = this_base;
  812. /* If LR was saved on the stack, record its location. */
  813. if (lr_saved_on_stack)
  814. info->saved_regs[lr_regnum].set_addr (this_base - fp_offset
  815. + lr_sp_offset);
  816. /* The call instruction moves the caller's PC in the callee's LR.
  817. Since this is an unwind, do the reverse. Copy the location of LR
  818. into PC (the address / regnum) so that a request for PC will be
  819. converted into a request for the LR. */
  820. info->saved_regs[pc_regnum] = info->saved_regs[lr_regnum];
  821. /* Save the previous frame's computed SP value. */
  822. info->saved_regs[sp_regnum].set_value (info->prev_sp);
  823. }
  824. return last_prologue_pc;
  825. }
  826. static CORE_ADDR
  827. frv_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  828. {
  829. CORE_ADDR func_addr, func_end, new_pc;
  830. new_pc = pc;
  831. /* If the line table has entry for a line *within* the function
  832. (i.e., not in the prologue, and not past the end), then that's
  833. our location. */
  834. if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  835. {
  836. struct symtab_and_line sal;
  837. sal = find_pc_line (func_addr, 0);
  838. if (sal.line != 0 && sal.end < func_end)
  839. {
  840. new_pc = sal.end;
  841. }
  842. }
  843. /* The FR-V prologue is at least five instructions long (twenty bytes).
  844. If we didn't find a real source location past that, then
  845. do a full analysis of the prologue. */
  846. if (new_pc < pc + 20)
  847. new_pc = frv_analyze_prologue (gdbarch, pc, 0, 0);
  848. return new_pc;
  849. }
  850. /* Examine the instruction pointed to by PC. If it corresponds to
  851. a call to __main, return the address of the next instruction.
  852. Otherwise, return PC. */
  853. static CORE_ADDR
  854. frv_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  855. {
  856. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  857. gdb_byte buf[4];
  858. unsigned long op;
  859. CORE_ADDR orig_pc = pc;
  860. if (target_read_memory (pc, buf, 4))
  861. return pc;
  862. op = extract_unsigned_integer (buf, 4, byte_order);
  863. /* In PIC code, GR15 may be loaded from some offset off of FP prior
  864. to the call instruction.
  865. Skip over this instruction if present. It won't be present in
  866. non-PIC code, and even in PIC code, it might not be present.
  867. (This is due to the fact that GR15, the FDPIC register, already
  868. contains the correct value.)
  869. The general form of the LDI is given first, followed by the
  870. specific instruction with the GRi and GRk filled in as FP and
  871. GR15.
  872. ldi @(GRi, d12), GRk
  873. P KKKKKK 0110010 IIIIII SSSSSSSSSSSS = 0x00c80000
  874. 0 000000 1111111 000000 000000000000 = 0x01fc0000
  875. . . . . . . . .
  876. ldi @(FP, d12), GR15
  877. P KKKKKK 0110010 IIIIII SSSSSSSSSSSS = 0x1ec82000
  878. 0 001111 1111111 000010 000000000000 = 0x7ffff000
  879. . . . . . . . . */
  880. if ((op & 0x7ffff000) == 0x1ec82000)
  881. {
  882. pc += 4;
  883. if (target_read_memory (pc, buf, 4))
  884. return orig_pc;
  885. op = extract_unsigned_integer (buf, 4, byte_order);
  886. }
  887. /* The format of an FRV CALL instruction is as follows:
  888. call label24
  889. P HHHHHH 0001111 LLLLLLLLLLLLLLLLLL = 0x003c0000
  890. 0 000000 1111111 000000000000000000 = 0x01fc0000
  891. . . . . . . . .
  892. where label24 is constructed by concatenating the H bits with the
  893. L bits. The call target is PC + (4 * sign_ext(label24)). */
  894. if ((op & 0x01fc0000) == 0x003c0000)
  895. {
  896. LONGEST displ;
  897. CORE_ADDR call_dest;
  898. struct bound_minimal_symbol s;
  899. displ = ((op & 0xfe000000) >> 7) | (op & 0x0003ffff);
  900. if ((displ & 0x00800000) != 0)
  901. displ |= ~((LONGEST) 0x00ffffff);
  902. call_dest = pc + 4 * displ;
  903. s = lookup_minimal_symbol_by_pc (call_dest);
  904. if (s.minsym != NULL
  905. && s.minsym->linkage_name () != NULL
  906. && strcmp (s.minsym->linkage_name (), "__main") == 0)
  907. {
  908. pc += 4;
  909. return pc;
  910. }
  911. }
  912. return orig_pc;
  913. }
  914. static struct frv_unwind_cache *
  915. frv_frame_unwind_cache (struct frame_info *this_frame,
  916. void **this_prologue_cache)
  917. {
  918. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  919. struct frv_unwind_cache *info;
  920. if ((*this_prologue_cache))
  921. return (struct frv_unwind_cache *) (*this_prologue_cache);
  922. info = FRAME_OBSTACK_ZALLOC (struct frv_unwind_cache);
  923. (*this_prologue_cache) = info;
  924. info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  925. /* Prologue analysis does the rest... */
  926. frv_analyze_prologue (gdbarch,
  927. get_frame_func (this_frame), this_frame, info);
  928. return info;
  929. }
  930. static void
  931. frv_extract_return_value (struct type *type, struct regcache *regcache,
  932. gdb_byte *valbuf)
  933. {
  934. struct gdbarch *gdbarch = regcache->arch ();
  935. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  936. int len = TYPE_LENGTH (type);
  937. if (len <= 4)
  938. {
  939. ULONGEST gpr8_val;
  940. regcache_cooked_read_unsigned (regcache, 8, &gpr8_val);
  941. store_unsigned_integer (valbuf, len, byte_order, gpr8_val);
  942. }
  943. else if (len == 8)
  944. {
  945. ULONGEST regval;
  946. regcache_cooked_read_unsigned (regcache, 8, &regval);
  947. store_unsigned_integer (valbuf, 4, byte_order, regval);
  948. regcache_cooked_read_unsigned (regcache, 9, &regval);
  949. store_unsigned_integer ((bfd_byte *) valbuf + 4, 4, byte_order, regval);
  950. }
  951. else
  952. internal_error (__FILE__, __LINE__,
  953. _("Illegal return value length: %d"), len);
  954. }
  955. static CORE_ADDR
  956. frv_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  957. {
  958. /* Require dword alignment. */
  959. return align_down (sp, 8);
  960. }
  961. static CORE_ADDR
  962. find_func_descr (struct gdbarch *gdbarch, CORE_ADDR entry_point)
  963. {
  964. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  965. CORE_ADDR descr;
  966. gdb_byte valbuf[4];
  967. CORE_ADDR start_addr;
  968. /* If we can't find the function in the symbol table, then we assume
  969. that the function address is already in descriptor form. */
  970. if (!find_pc_partial_function (entry_point, NULL, &start_addr, NULL)
  971. || entry_point != start_addr)
  972. return entry_point;
  973. descr = frv_fdpic_find_canonical_descriptor (entry_point);
  974. if (descr != 0)
  975. return descr;
  976. /* Construct a non-canonical descriptor from space allocated on
  977. the stack. */
  978. descr = value_as_long (value_allocate_space_in_inferior (8));
  979. store_unsigned_integer (valbuf, 4, byte_order, entry_point);
  980. write_memory (descr, valbuf, 4);
  981. store_unsigned_integer (valbuf, 4, byte_order,
  982. frv_fdpic_find_global_pointer (entry_point));
  983. write_memory (descr + 4, valbuf, 4);
  984. return descr;
  985. }
  986. static CORE_ADDR
  987. frv_convert_from_func_ptr_addr (struct gdbarch *gdbarch, CORE_ADDR addr,
  988. struct target_ops *targ)
  989. {
  990. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  991. CORE_ADDR entry_point;
  992. CORE_ADDR got_address;
  993. entry_point = get_target_memory_unsigned (targ, addr, 4, byte_order);
  994. got_address = get_target_memory_unsigned (targ, addr + 4, 4, byte_order);
  995. if (got_address == frv_fdpic_find_global_pointer (entry_point))
  996. return entry_point;
  997. else
  998. return addr;
  999. }
  1000. static CORE_ADDR
  1001. frv_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1002. struct regcache *regcache, CORE_ADDR bp_addr,
  1003. int nargs, struct value **args, CORE_ADDR sp,
  1004. function_call_return_method return_method,
  1005. CORE_ADDR struct_addr)
  1006. {
  1007. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1008. int argreg;
  1009. int argnum;
  1010. const gdb_byte *val;
  1011. gdb_byte valbuf[4];
  1012. struct value *arg;
  1013. struct type *arg_type;
  1014. int len;
  1015. enum type_code typecode;
  1016. CORE_ADDR regval;
  1017. int stack_space;
  1018. int stack_offset;
  1019. enum frv_abi abi = frv_abi (gdbarch);
  1020. CORE_ADDR func_addr = find_function_addr (function, NULL);
  1021. #if 0
  1022. printf("Push %d args at sp = %x, struct_return=%d (%x)\n",
  1023. nargs, (int) sp, struct_return, struct_addr);
  1024. #endif
  1025. stack_space = 0;
  1026. for (argnum = 0; argnum < nargs; ++argnum)
  1027. stack_space += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);
  1028. stack_space -= (6 * 4);
  1029. if (stack_space > 0)
  1030. sp -= stack_space;
  1031. /* Make sure stack is dword aligned. */
  1032. sp = align_down (sp, 8);
  1033. stack_offset = 0;
  1034. argreg = 8;
  1035. if (return_method == return_method_struct)
  1036. regcache_cooked_write_unsigned (regcache, struct_return_regnum,
  1037. struct_addr);
  1038. for (argnum = 0; argnum < nargs; ++argnum)
  1039. {
  1040. arg = args[argnum];
  1041. arg_type = check_typedef (value_type (arg));
  1042. len = TYPE_LENGTH (arg_type);
  1043. typecode = arg_type->code ();
  1044. if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
  1045. {
  1046. store_unsigned_integer (valbuf, 4, byte_order,
  1047. value_address (arg));
  1048. typecode = TYPE_CODE_PTR;
  1049. len = 4;
  1050. val = valbuf;
  1051. }
  1052. else if (abi == FRV_ABI_FDPIC
  1053. && len == 4
  1054. && typecode == TYPE_CODE_PTR
  1055. && TYPE_TARGET_TYPE (arg_type)->code () == TYPE_CODE_FUNC)
  1056. {
  1057. /* The FDPIC ABI requires function descriptors to be passed instead
  1058. of entry points. */
  1059. CORE_ADDR addr = extract_unsigned_integer
  1060. (value_contents (arg).data (), 4, byte_order);
  1061. addr = find_func_descr (gdbarch, addr);
  1062. store_unsigned_integer (valbuf, 4, byte_order, addr);
  1063. typecode = TYPE_CODE_PTR;
  1064. len = 4;
  1065. val = valbuf;
  1066. }
  1067. else
  1068. {
  1069. val = value_contents (arg).data ();
  1070. }
  1071. while (len > 0)
  1072. {
  1073. int partial_len = (len < 4 ? len : 4);
  1074. if (argreg < 14)
  1075. {
  1076. regval = extract_unsigned_integer (val, partial_len, byte_order);
  1077. #if 0
  1078. printf(" Argnum %d data %x -> reg %d\n",
  1079. argnum, (int) regval, argreg);
  1080. #endif
  1081. regcache_cooked_write_unsigned (regcache, argreg, regval);
  1082. ++argreg;
  1083. }
  1084. else
  1085. {
  1086. #if 0
  1087. printf(" Argnum %d data %x -> offset %d (%x)\n",
  1088. argnum, *((int *)val), stack_offset,
  1089. (int) (sp + stack_offset));
  1090. #endif
  1091. write_memory (sp + stack_offset, val, partial_len);
  1092. stack_offset += align_up (partial_len, 4);
  1093. }
  1094. len -= partial_len;
  1095. val += partial_len;
  1096. }
  1097. }
  1098. /* Set the return address. For the frv, the return breakpoint is
  1099. always at BP_ADDR. */
  1100. regcache_cooked_write_unsigned (regcache, lr_regnum, bp_addr);
  1101. if (abi == FRV_ABI_FDPIC)
  1102. {
  1103. /* Set the GOT register for the FDPIC ABI. */
  1104. regcache_cooked_write_unsigned
  1105. (regcache, first_gpr_regnum + 15,
  1106. frv_fdpic_find_global_pointer (func_addr));
  1107. }
  1108. /* Finally, update the SP register. */
  1109. regcache_cooked_write_unsigned (regcache, sp_regnum, sp);
  1110. return sp;
  1111. }
  1112. static void
  1113. frv_store_return_value (struct type *type, struct regcache *regcache,
  1114. const gdb_byte *valbuf)
  1115. {
  1116. int len = TYPE_LENGTH (type);
  1117. if (len <= 4)
  1118. {
  1119. bfd_byte val[4];
  1120. memset (val, 0, sizeof (val));
  1121. memcpy (val + (4 - len), valbuf, len);
  1122. regcache->cooked_write (8, val);
  1123. }
  1124. else if (len == 8)
  1125. {
  1126. regcache->cooked_write (8, valbuf);
  1127. regcache->cooked_write (9, (bfd_byte *) valbuf + 4);
  1128. }
  1129. else
  1130. internal_error (__FILE__, __LINE__,
  1131. _("Don't know how to return a %d-byte value."), len);
  1132. }
  1133. static enum return_value_convention
  1134. frv_return_value (struct gdbarch *gdbarch, struct value *function,
  1135. struct type *valtype, struct regcache *regcache,
  1136. gdb_byte *readbuf, const gdb_byte *writebuf)
  1137. {
  1138. int struct_return = valtype->code () == TYPE_CODE_STRUCT
  1139. || valtype->code () == TYPE_CODE_UNION
  1140. || valtype->code () == TYPE_CODE_ARRAY;
  1141. if (writebuf != NULL)
  1142. {
  1143. gdb_assert (!struct_return);
  1144. frv_store_return_value (valtype, regcache, writebuf);
  1145. }
  1146. if (readbuf != NULL)
  1147. {
  1148. gdb_assert (!struct_return);
  1149. frv_extract_return_value (valtype, regcache, readbuf);
  1150. }
  1151. if (struct_return)
  1152. return RETURN_VALUE_STRUCT_CONVENTION;
  1153. else
  1154. return RETURN_VALUE_REGISTER_CONVENTION;
  1155. }
  1156. /* Given a GDB frame, determine the address of the calling function's
  1157. frame. This will be used to create a new GDB frame struct. */
  1158. static void
  1159. frv_frame_this_id (struct frame_info *this_frame,
  1160. void **this_prologue_cache, struct frame_id *this_id)
  1161. {
  1162. struct frv_unwind_cache *info
  1163. = frv_frame_unwind_cache (this_frame, this_prologue_cache);
  1164. CORE_ADDR base;
  1165. CORE_ADDR func;
  1166. struct bound_minimal_symbol msym_stack;
  1167. struct frame_id id;
  1168. /* The FUNC is easy. */
  1169. func = get_frame_func (this_frame);
  1170. /* Check if the stack is empty. */
  1171. msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
  1172. if (msym_stack.minsym && info->base == BMSYMBOL_VALUE_ADDRESS (msym_stack))
  1173. return;
  1174. /* Hopefully the prologue analysis either correctly determined the
  1175. frame's base (which is the SP from the previous frame), or set
  1176. that base to "NULL". */
  1177. base = info->prev_sp;
  1178. if (base == 0)
  1179. return;
  1180. id = frame_id_build (base, func);
  1181. (*this_id) = id;
  1182. }
  1183. static struct value *
  1184. frv_frame_prev_register (struct frame_info *this_frame,
  1185. void **this_prologue_cache, int regnum)
  1186. {
  1187. struct frv_unwind_cache *info
  1188. = frv_frame_unwind_cache (this_frame, this_prologue_cache);
  1189. return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  1190. }
  1191. static const struct frame_unwind frv_frame_unwind = {
  1192. "frv prologue",
  1193. NORMAL_FRAME,
  1194. default_frame_unwind_stop_reason,
  1195. frv_frame_this_id,
  1196. frv_frame_prev_register,
  1197. NULL,
  1198. default_frame_sniffer
  1199. };
  1200. static CORE_ADDR
  1201. frv_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1202. {
  1203. struct frv_unwind_cache *info
  1204. = frv_frame_unwind_cache (this_frame, this_cache);
  1205. return info->base;
  1206. }
  1207. static const struct frame_base frv_frame_base = {
  1208. &frv_frame_unwind,
  1209. frv_frame_base_address,
  1210. frv_frame_base_address,
  1211. frv_frame_base_address
  1212. };
  1213. static struct gdbarch *
  1214. frv_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1215. {
  1216. struct gdbarch *gdbarch;
  1217. int elf_flags = 0;
  1218. /* Check to see if we've already built an appropriate architecture
  1219. object for this executable. */
  1220. arches = gdbarch_list_lookup_by_info (arches, &info);
  1221. if (arches)
  1222. return arches->gdbarch;
  1223. /* Select the right tdep structure for this variant. */
  1224. frv_gdbarch_tdep *var = new_variant ();
  1225. switch (info.bfd_arch_info->mach)
  1226. {
  1227. case bfd_mach_frv:
  1228. case bfd_mach_frvsimple:
  1229. case bfd_mach_fr300:
  1230. case bfd_mach_fr500:
  1231. case bfd_mach_frvtomcat:
  1232. case bfd_mach_fr550:
  1233. set_variant_num_gprs (var, 64);
  1234. set_variant_num_fprs (var, 64);
  1235. break;
  1236. case bfd_mach_fr400:
  1237. case bfd_mach_fr450:
  1238. set_variant_num_gprs (var, 32);
  1239. set_variant_num_fprs (var, 32);
  1240. break;
  1241. default:
  1242. /* Never heard of this variant. */
  1243. return 0;
  1244. }
  1245. /* Extract the ELF flags, if available. */
  1246. if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1247. elf_flags = elf_elfheader (info.abfd)->e_flags;
  1248. if (elf_flags & EF_FRV_FDPIC)
  1249. set_variant_abi_fdpic (var);
  1250. if (elf_flags & EF_FRV_CPU_FR450)
  1251. set_variant_scratch_registers (var);
  1252. gdbarch = gdbarch_alloc (&info, var);
  1253. set_gdbarch_short_bit (gdbarch, 16);
  1254. set_gdbarch_int_bit (gdbarch, 32);
  1255. set_gdbarch_long_bit (gdbarch, 32);
  1256. set_gdbarch_long_long_bit (gdbarch, 64);
  1257. set_gdbarch_float_bit (gdbarch, 32);
  1258. set_gdbarch_double_bit (gdbarch, 64);
  1259. set_gdbarch_long_double_bit (gdbarch, 64);
  1260. set_gdbarch_ptr_bit (gdbarch, 32);
  1261. set_gdbarch_num_regs (gdbarch, frv_num_regs);
  1262. set_gdbarch_num_pseudo_regs (gdbarch, frv_num_pseudo_regs);
  1263. set_gdbarch_sp_regnum (gdbarch, sp_regnum);
  1264. set_gdbarch_deprecated_fp_regnum (gdbarch, fp_regnum);
  1265. set_gdbarch_pc_regnum (gdbarch, pc_regnum);
  1266. set_gdbarch_register_name (gdbarch, frv_register_name);
  1267. set_gdbarch_register_type (gdbarch, frv_register_type);
  1268. set_gdbarch_register_sim_regno (gdbarch, frv_register_sim_regno);
  1269. set_gdbarch_pseudo_register_read (gdbarch, frv_pseudo_register_read);
  1270. set_gdbarch_pseudo_register_write (gdbarch, frv_pseudo_register_write);
  1271. set_gdbarch_skip_prologue (gdbarch, frv_skip_prologue);
  1272. set_gdbarch_skip_main_prologue (gdbarch, frv_skip_main_prologue);
  1273. set_gdbarch_breakpoint_kind_from_pc (gdbarch, frv_breakpoint::kind_from_pc);
  1274. set_gdbarch_sw_breakpoint_from_kind (gdbarch, frv_breakpoint::bp_from_kind);
  1275. set_gdbarch_adjust_breakpoint_address
  1276. (gdbarch, frv_adjust_breakpoint_address);
  1277. set_gdbarch_return_value (gdbarch, frv_return_value);
  1278. /* Frame stuff. */
  1279. set_gdbarch_frame_align (gdbarch, frv_frame_align);
  1280. frame_base_set_default (gdbarch, &frv_frame_base);
  1281. /* We set the sniffer lower down after the OSABI hooks have been
  1282. established. */
  1283. /* Settings for calling functions in the inferior. */
  1284. set_gdbarch_push_dummy_call (gdbarch, frv_push_dummy_call);
  1285. /* Settings that should be unnecessary. */
  1286. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1287. /* Hardware watchpoint / breakpoint support. */
  1288. switch (info.bfd_arch_info->mach)
  1289. {
  1290. case bfd_mach_frv:
  1291. case bfd_mach_frvsimple:
  1292. case bfd_mach_fr300:
  1293. case bfd_mach_fr500:
  1294. case bfd_mach_frvtomcat:
  1295. /* fr500-style hardware debugging support. */
  1296. var->num_hw_watchpoints = 4;
  1297. var->num_hw_breakpoints = 4;
  1298. break;
  1299. case bfd_mach_fr400:
  1300. case bfd_mach_fr450:
  1301. /* fr400-style hardware debugging support. */
  1302. var->num_hw_watchpoints = 2;
  1303. var->num_hw_breakpoints = 4;
  1304. break;
  1305. default:
  1306. /* Otherwise, assume we don't have hardware debugging support. */
  1307. var->num_hw_watchpoints = 0;
  1308. var->num_hw_breakpoints = 0;
  1309. break;
  1310. }
  1311. if (frv_abi (gdbarch) == FRV_ABI_FDPIC)
  1312. set_gdbarch_convert_from_func_ptr_addr (gdbarch,
  1313. frv_convert_from_func_ptr_addr);
  1314. set_solib_ops (gdbarch, &frv_so_ops);
  1315. /* Hook in ABI-specific overrides, if they have been registered. */
  1316. gdbarch_init_osabi (info, gdbarch);
  1317. /* Set the fallback (prologue based) frame sniffer. */
  1318. frame_unwind_append_unwinder (gdbarch, &frv_frame_unwind);
  1319. /* Enable TLS support. */
  1320. set_gdbarch_fetch_tls_load_module_address (gdbarch,
  1321. frv_fetch_objfile_link_map);
  1322. return gdbarch;
  1323. }
  1324. void _initialize_frv_tdep ();
  1325. void
  1326. _initialize_frv_tdep ()
  1327. {
  1328. register_gdbarch_init (bfd_arch_frv, frv_gdbarch_init);
  1329. }