sparc-tdep.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /* Target-dependent code for SPARC.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include "dis-asm.h"
  17. #include "dwarf2.h"
  18. #include "dwarf2/frame.h"
  19. #include "frame.h"
  20. #include "frame-base.h"
  21. #include "frame-unwind.h"
  22. #include "gdbcore.h"
  23. #include "gdbtypes.h"
  24. #include "inferior.h"
  25. #include "symtab.h"
  26. #include "objfiles.h"
  27. #include "osabi.h"
  28. #include "regcache.h"
  29. #include "target.h"
  30. #include "target-descriptions.h"
  31. #include "value.h"
  32. #include "sparc-tdep.h"
  33. #include "sparc-ravenscar-thread.h"
  34. #include <algorithm>
  35. struct regset;
  36. /* This file implements the SPARC 32-bit ABI as defined by the section
  37. "Low-Level System Information" of the SPARC Compliance Definition
  38. (SCD) 2.4.1, which is the 32-bit System V psABI for SPARC. The SCD
  39. lists changes with respect to the original 32-bit psABI as defined
  40. in the "System V ABI, SPARC Processor Supplement".
  41. Note that if we talk about SunOS, we mean SunOS 4.x, which was
  42. BSD-based, which is sometimes (retroactively?) referred to as
  43. Solaris 1.x. If we talk about Solaris we mean Solaris 2.x and
  44. above (Solaris 7, 8 and 9 are nothing but Solaris 2.7, 2.8 and 2.9
  45. suffering from severe version number inflation). Solaris 2.x is
  46. also known as SunOS 5.x, since that's what uname(1) says. Solaris
  47. 2.x is SVR4-based. */
  48. /* Please use the sparc32_-prefix for 32-bit specific code, the
  49. sparc64_-prefix for 64-bit specific code and the sparc_-prefix for
  50. code that can handle both. The 64-bit specific code lives in
  51. sparc64-tdep.c; don't add any here. */
  52. /* The stack pointer is offset from the stack frame by a BIAS of 2047
  53. (0x7ff) for 64-bit code. BIAS is likely to be defined on SPARC
  54. hosts, so undefine it first. */
  55. #undef BIAS
  56. #define BIAS 2047
  57. /* Macros to extract fields from SPARC instructions. */
  58. #define X_OP(i) (((i) >> 30) & 0x3)
  59. #define X_RD(i) (((i) >> 25) & 0x1f)
  60. #define X_A(i) (((i) >> 29) & 1)
  61. #define X_COND(i) (((i) >> 25) & 0xf)
  62. #define X_OP2(i) (((i) >> 22) & 0x7)
  63. #define X_IMM22(i) ((i) & 0x3fffff)
  64. #define X_OP3(i) (((i) >> 19) & 0x3f)
  65. #define X_RS1(i) (((i) >> 14) & 0x1f)
  66. #define X_RS2(i) ((i) & 0x1f)
  67. #define X_I(i) (((i) >> 13) & 1)
  68. /* Sign extension macros. */
  69. #define X_DISP22(i) ((X_IMM22 (i) ^ 0x200000) - 0x200000)
  70. #define X_DISP19(i) ((((i) & 0x7ffff) ^ 0x40000) - 0x40000)
  71. #define X_DISP10(i) ((((((i) >> 11) && 0x300) | (((i) >> 5) & 0xff)) ^ 0x200) - 0x200)
  72. #define X_SIMM13(i) ((((i) & 0x1fff) ^ 0x1000) - 0x1000)
  73. /* Macros to identify some instructions. */
  74. /* RETURN (RETT in V8) */
  75. #define X_RETTURN(i) ((X_OP (i) == 0x2) && (X_OP3 (i) == 0x39))
  76. /* Fetch the instruction at PC. Instructions are always big-endian
  77. even if the processor operates in little-endian mode. */
  78. unsigned long
  79. sparc_fetch_instruction (CORE_ADDR pc)
  80. {
  81. gdb_byte buf[4];
  82. unsigned long insn;
  83. int i;
  84. /* If we can't read the instruction at PC, return zero. */
  85. if (target_read_memory (pc, buf, sizeof (buf)))
  86. return 0;
  87. insn = 0;
  88. for (i = 0; i < sizeof (buf); i++)
  89. insn = (insn << 8) | buf[i];
  90. return insn;
  91. }
  92. /* Return non-zero if the instruction corresponding to PC is an "unimp"
  93. instruction. */
  94. static int
  95. sparc_is_unimp_insn (CORE_ADDR pc)
  96. {
  97. const unsigned long insn = sparc_fetch_instruction (pc);
  98. return ((insn & 0xc1c00000) == 0);
  99. }
  100. /* Return non-zero if the instruction corresponding to PC is an
  101. "annulled" branch, i.e. the annul bit is set. */
  102. int
  103. sparc_is_annulled_branch_insn (CORE_ADDR pc)
  104. {
  105. /* The branch instructions featuring an annul bit can be identified
  106. by the following bit patterns:
  107. OP=0
  108. OP2=1: Branch on Integer Condition Codes with Prediction (BPcc).
  109. OP2=2: Branch on Integer Condition Codes (Bcc).
  110. OP2=5: Branch on FP Condition Codes with Prediction (FBfcc).
  111. OP2=6: Branch on FP Condition Codes (FBcc).
  112. OP2=3 && Bit28=0:
  113. Branch on Integer Register with Prediction (BPr).
  114. This leaves out ILLTRAP (OP2=0), SETHI/NOP (OP2=4) and the V8
  115. coprocessor branch instructions (Op2=7). */
  116. const unsigned long insn = sparc_fetch_instruction (pc);
  117. const unsigned op2 = X_OP2 (insn);
  118. if ((X_OP (insn) == 0)
  119. && ((op2 == 1) || (op2 == 2) || (op2 == 5) || (op2 == 6)
  120. || ((op2 == 3) && ((insn & 0x10000000) == 0))))
  121. return X_A (insn);
  122. else
  123. return 0;
  124. }
  125. /* OpenBSD/sparc includes StackGhost, which according to the author's
  126. website http://stackghost.cerias.purdue.edu "... transparently and
  127. automatically protects applications' stack frames; more
  128. specifically, it guards the return pointers. The protection
  129. mechanisms require no application source or binary modification and
  130. imposes only a negligible performance penalty."
  131. The same website provides the following description of how
  132. StackGhost works:
  133. "StackGhost interfaces with the kernel trap handler that would
  134. normally write out registers to the stack and the handler that
  135. would read them back in. By XORing a cookie into the
  136. return-address saved in the user stack when it is actually written
  137. to the stack, and then XOR it out when the return-address is pulled
  138. from the stack, StackGhost can cause attacker corrupted return
  139. pointers to behave in a manner the attacker cannot predict.
  140. StackGhost can also use several unused bits in the return pointer
  141. to detect a smashed return pointer and abort the process."
  142. For GDB this means that whenever we're reading %i7 from a stack
  143. frame's window save area, we'll have to XOR the cookie.
  144. More information on StackGuard can be found on in:
  145. Mike Frantzen and Mike Shuey. "StackGhost: Hardware Facilitated
  146. Stack Protection." 2001. Published in USENIX Security Symposium
  147. '01. */
  148. /* Fetch StackGhost Per-Process XOR cookie. */
  149. ULONGEST
  150. sparc_fetch_wcookie (struct gdbarch *gdbarch)
  151. {
  152. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  153. struct target_ops *ops = current_inferior ()->top_target ();
  154. gdb_byte buf[8];
  155. int len;
  156. len = target_read (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8);
  157. if (len == -1)
  158. return 0;
  159. /* We should have either an 32-bit or an 64-bit cookie. */
  160. gdb_assert (len == 4 || len == 8);
  161. return extract_unsigned_integer (buf, len, byte_order);
  162. }
  163. /* The functions on this page are intended to be used to classify
  164. function arguments. */
  165. /* Check whether TYPE is "Integral or Pointer". */
  166. static int
  167. sparc_integral_or_pointer_p (const struct type *type)
  168. {
  169. int len = TYPE_LENGTH (type);
  170. switch (type->code ())
  171. {
  172. case TYPE_CODE_INT:
  173. case TYPE_CODE_BOOL:
  174. case TYPE_CODE_CHAR:
  175. case TYPE_CODE_ENUM:
  176. case TYPE_CODE_RANGE:
  177. /* We have byte, half-word, word and extended-word/doubleword
  178. integral types. The doubleword is an extension to the
  179. original 32-bit ABI by the SCD 2.4.x. */
  180. return (len == 1 || len == 2 || len == 4 || len == 8);
  181. case TYPE_CODE_PTR:
  182. case TYPE_CODE_REF:
  183. case TYPE_CODE_RVALUE_REF:
  184. /* Allow either 32-bit or 64-bit pointers. */
  185. return (len == 4 || len == 8);
  186. default:
  187. break;
  188. }
  189. return 0;
  190. }
  191. /* Check whether TYPE is "Floating". */
  192. static int
  193. sparc_floating_p (const struct type *type)
  194. {
  195. switch (type->code ())
  196. {
  197. case TYPE_CODE_FLT:
  198. {
  199. int len = TYPE_LENGTH (type);
  200. return (len == 4 || len == 8 || len == 16);
  201. }
  202. default:
  203. break;
  204. }
  205. return 0;
  206. }
  207. /* Check whether TYPE is "Complex Floating". */
  208. static int
  209. sparc_complex_floating_p (const struct type *type)
  210. {
  211. switch (type->code ())
  212. {
  213. case TYPE_CODE_COMPLEX:
  214. {
  215. int len = TYPE_LENGTH (type);
  216. return (len == 8 || len == 16 || len == 32);
  217. }
  218. default:
  219. break;
  220. }
  221. return 0;
  222. }
  223. /* Check whether TYPE is "Structure or Union".
  224. In terms of Ada subprogram calls, arrays are treated the same as
  225. struct and union types. So this function also returns non-zero
  226. for array types. */
  227. static int
  228. sparc_structure_or_union_p (const struct type *type)
  229. {
  230. switch (type->code ())
  231. {
  232. case TYPE_CODE_STRUCT:
  233. case TYPE_CODE_UNION:
  234. case TYPE_CODE_ARRAY:
  235. return 1;
  236. default:
  237. break;
  238. }
  239. return 0;
  240. }
  241. /* Return true if TYPE is returned by memory, false if returned by
  242. register. */
  243. static bool
  244. sparc_structure_return_p (const struct type *type)
  245. {
  246. if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
  247. {
  248. /* Float vectors are always returned by memory. */
  249. if (sparc_floating_p (check_typedef (TYPE_TARGET_TYPE (type))))
  250. return true;
  251. /* Integer vectors are returned by memory if the vector size
  252. is greater than 8 bytes long. */
  253. return (TYPE_LENGTH (type) > 8);
  254. }
  255. if (sparc_floating_p (type))
  256. {
  257. /* Floating point types are passed by register for size 4 and
  258. 8 bytes, and by memory for size 16 bytes. */
  259. return (TYPE_LENGTH (type) == 16);
  260. }
  261. /* Other than that, only aggregates of all sizes get returned by
  262. memory. */
  263. return sparc_structure_or_union_p (type);
  264. }
  265. /* Return true if arguments of the given TYPE are passed by
  266. memory; false if returned by register. */
  267. static bool
  268. sparc_arg_by_memory_p (const struct type *type)
  269. {
  270. if (type->code () == TYPE_CODE_ARRAY && type->is_vector ())
  271. {
  272. /* Float vectors are always passed by memory. */
  273. if (sparc_floating_p (check_typedef (TYPE_TARGET_TYPE (type))))
  274. return true;
  275. /* Integer vectors are passed by memory if the vector size
  276. is greater than 8 bytes long. */
  277. return (TYPE_LENGTH (type) > 8);
  278. }
  279. /* Floats are passed by register for size 4 and 8 bytes, and by memory
  280. for size 16 bytes. */
  281. if (sparc_floating_p (type))
  282. return (TYPE_LENGTH (type) == 16);
  283. /* Complex floats and aggregates of all sizes are passed by memory. */
  284. if (sparc_complex_floating_p (type) || sparc_structure_or_union_p (type))
  285. return true;
  286. /* Everything else gets passed by register. */
  287. return false;
  288. }
  289. /* Register information. */
  290. #define SPARC32_FPU_REGISTERS \
  291. "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", \
  292. "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", \
  293. "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23", \
  294. "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
  295. #define SPARC32_CP0_REGISTERS \
  296. "y", "psr", "wim", "tbr", "pc", "npc", "fsr", "csr"
  297. static const char * const sparc_core_register_names[] = {
  298. SPARC_CORE_REGISTERS
  299. };
  300. static const char * const sparc32_fpu_register_names[] = {
  301. SPARC32_FPU_REGISTERS
  302. };
  303. static const char * const sparc32_cp0_register_names[] = {
  304. SPARC32_CP0_REGISTERS
  305. };
  306. static const char * const sparc32_register_names[] =
  307. {
  308. SPARC_CORE_REGISTERS,
  309. SPARC32_FPU_REGISTERS,
  310. SPARC32_CP0_REGISTERS
  311. };
  312. /* Total number of registers. */
  313. #define SPARC32_NUM_REGS ARRAY_SIZE (sparc32_register_names)
  314. /* We provide the aliases %d0..%d30 for the floating registers as
  315. "psuedo" registers. */
  316. static const char * const sparc32_pseudo_register_names[] =
  317. {
  318. "d0", "d2", "d4", "d6", "d8", "d10", "d12", "d14",
  319. "d16", "d18", "d20", "d22", "d24", "d26", "d28", "d30"
  320. };
  321. /* Total number of pseudo registers. */
  322. #define SPARC32_NUM_PSEUDO_REGS ARRAY_SIZE (sparc32_pseudo_register_names)
  323. /* Return the name of pseudo register REGNUM. */
  324. static const char *
  325. sparc32_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
  326. {
  327. regnum -= gdbarch_num_regs (gdbarch);
  328. if (regnum < SPARC32_NUM_PSEUDO_REGS)
  329. return sparc32_pseudo_register_names[regnum];
  330. internal_error (__FILE__, __LINE__,
  331. _("sparc32_pseudo_register_name: bad register number %d"),
  332. regnum);
  333. }
  334. /* Return the name of register REGNUM. */
  335. static const char *
  336. sparc32_register_name (struct gdbarch *gdbarch, int regnum)
  337. {
  338. if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  339. return tdesc_register_name (gdbarch, regnum);
  340. if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
  341. return sparc32_register_names[regnum];
  342. return sparc32_pseudo_register_name (gdbarch, regnum);
  343. }
  344. /* Construct types for ISA-specific registers. */
  345. static struct type *
  346. sparc_psr_type (struct gdbarch *gdbarch)
  347. {
  348. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  349. if (!tdep->sparc_psr_type)
  350. {
  351. struct type *type;
  352. type = arch_flags_type (gdbarch, "builtin_type_sparc_psr", 32);
  353. append_flags_type_flag (type, 5, "ET");
  354. append_flags_type_flag (type, 6, "PS");
  355. append_flags_type_flag (type, 7, "S");
  356. append_flags_type_flag (type, 12, "EF");
  357. append_flags_type_flag (type, 13, "EC");
  358. tdep->sparc_psr_type = type;
  359. }
  360. return tdep->sparc_psr_type;
  361. }
  362. static struct type *
  363. sparc_fsr_type (struct gdbarch *gdbarch)
  364. {
  365. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  366. if (!tdep->sparc_fsr_type)
  367. {
  368. struct type *type;
  369. type = arch_flags_type (gdbarch, "builtin_type_sparc_fsr", 32);
  370. append_flags_type_flag (type, 0, "NXA");
  371. append_flags_type_flag (type, 1, "DZA");
  372. append_flags_type_flag (type, 2, "UFA");
  373. append_flags_type_flag (type, 3, "OFA");
  374. append_flags_type_flag (type, 4, "NVA");
  375. append_flags_type_flag (type, 5, "NXC");
  376. append_flags_type_flag (type, 6, "DZC");
  377. append_flags_type_flag (type, 7, "UFC");
  378. append_flags_type_flag (type, 8, "OFC");
  379. append_flags_type_flag (type, 9, "NVC");
  380. append_flags_type_flag (type, 22, "NS");
  381. append_flags_type_flag (type, 23, "NXM");
  382. append_flags_type_flag (type, 24, "DZM");
  383. append_flags_type_flag (type, 25, "UFM");
  384. append_flags_type_flag (type, 26, "OFM");
  385. append_flags_type_flag (type, 27, "NVM");
  386. tdep->sparc_fsr_type = type;
  387. }
  388. return tdep->sparc_fsr_type;
  389. }
  390. /* Return the GDB type object for the "standard" data type of data in
  391. pseudo register REGNUM. */
  392. static struct type *
  393. sparc32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
  394. {
  395. regnum -= gdbarch_num_regs (gdbarch);
  396. if (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM)
  397. return builtin_type (gdbarch)->builtin_double;
  398. internal_error (__FILE__, __LINE__,
  399. _("sparc32_pseudo_register_type: bad register number %d"),
  400. regnum);
  401. }
  402. /* Return the GDB type object for the "standard" data type of data in
  403. register REGNUM. */
  404. static struct type *
  405. sparc32_register_type (struct gdbarch *gdbarch, int regnum)
  406. {
  407. if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  408. return tdesc_register_type (gdbarch, regnum);
  409. if (regnum >= SPARC_F0_REGNUM && regnum <= SPARC_F31_REGNUM)
  410. return builtin_type (gdbarch)->builtin_float;
  411. if (regnum == SPARC_SP_REGNUM || regnum == SPARC_FP_REGNUM)
  412. return builtin_type (gdbarch)->builtin_data_ptr;
  413. if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
  414. return builtin_type (gdbarch)->builtin_func_ptr;
  415. if (regnum == SPARC32_PSR_REGNUM)
  416. return sparc_psr_type (gdbarch);
  417. if (regnum == SPARC32_FSR_REGNUM)
  418. return sparc_fsr_type (gdbarch);
  419. if (regnum >= gdbarch_num_regs (gdbarch))
  420. return sparc32_pseudo_register_type (gdbarch, regnum);
  421. return builtin_type (gdbarch)->builtin_int32;
  422. }
  423. static enum register_status
  424. sparc32_pseudo_register_read (struct gdbarch *gdbarch,
  425. readable_regcache *regcache,
  426. int regnum, gdb_byte *buf)
  427. {
  428. enum register_status status;
  429. regnum -= gdbarch_num_regs (gdbarch);
  430. gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
  431. regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
  432. status = regcache->raw_read (regnum, buf);
  433. if (status == REG_VALID)
  434. status = regcache->raw_read (regnum + 1, buf + 4);
  435. return status;
  436. }
  437. static void
  438. sparc32_pseudo_register_write (struct gdbarch *gdbarch,
  439. struct regcache *regcache,
  440. int regnum, const gdb_byte *buf)
  441. {
  442. regnum -= gdbarch_num_regs (gdbarch);
  443. gdb_assert (regnum >= SPARC32_D0_REGNUM && regnum <= SPARC32_D30_REGNUM);
  444. regnum = SPARC_F0_REGNUM + 2 * (regnum - SPARC32_D0_REGNUM);
  445. regcache->raw_write (regnum, buf);
  446. regcache->raw_write (regnum + 1, buf + 4);
  447. }
  448. /* Implement the stack_frame_destroyed_p gdbarch method. */
  449. int
  450. sparc_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  451. {
  452. /* This function must return true if we are one instruction after an
  453. instruction that destroyed the stack frame of the current
  454. function. The SPARC instructions used to restore the callers
  455. stack frame are RESTORE and RETURN/RETT.
  456. Of these RETURN/RETT is a branch instruction and thus we return
  457. true if we are in its delay slot.
  458. RESTORE is almost always found in the delay slot of a branch
  459. instruction that transfers control to the caller, such as JMPL.
  460. Thus the next instruction is in the caller frame and we don't
  461. need to do anything about it. */
  462. unsigned int insn = sparc_fetch_instruction (pc - 4);
  463. return X_RETTURN (insn);
  464. }
  465. static CORE_ADDR
  466. sparc32_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  467. {
  468. /* The ABI requires double-word alignment. */
  469. return address & ~0x7;
  470. }
  471. static CORE_ADDR
  472. sparc32_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
  473. CORE_ADDR funcaddr,
  474. struct value **args, int nargs,
  475. struct type *value_type,
  476. CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
  477. struct regcache *regcache)
  478. {
  479. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  480. *bp_addr = sp - 4;
  481. *real_pc = funcaddr;
  482. if (using_struct_return (gdbarch, NULL, value_type))
  483. {
  484. gdb_byte buf[4];
  485. /* This is an UNIMP instruction. */
  486. store_unsigned_integer (buf, 4, byte_order,
  487. TYPE_LENGTH (value_type) & 0x1fff);
  488. write_memory (sp - 8, buf, 4);
  489. return sp - 8;
  490. }
  491. return sp - 4;
  492. }
  493. static CORE_ADDR
  494. sparc32_store_arguments (struct regcache *regcache, int nargs,
  495. struct value **args, CORE_ADDR sp,
  496. function_call_return_method return_method,
  497. CORE_ADDR struct_addr)
  498. {
  499. struct gdbarch *gdbarch = regcache->arch ();
  500. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  501. /* Number of words in the "parameter array". */
  502. int num_elements = 0;
  503. int element = 0;
  504. int i;
  505. for (i = 0; i < nargs; i++)
  506. {
  507. struct type *type = value_type (args[i]);
  508. int len = TYPE_LENGTH (type);
  509. if (sparc_arg_by_memory_p (type))
  510. {
  511. /* Structure, Union and Quad-Precision Arguments. */
  512. sp -= len;
  513. /* Use doubleword alignment for these values. That's always
  514. correct, and wasting a few bytes shouldn't be a problem. */
  515. sp &= ~0x7;
  516. write_memory (sp, value_contents (args[i]).data (), len);
  517. args[i] = value_from_pointer (lookup_pointer_type (type), sp);
  518. num_elements++;
  519. }
  520. else if (sparc_floating_p (type))
  521. {
  522. /* Floating arguments. */
  523. gdb_assert (len == 4 || len == 8);
  524. num_elements += (len / 4);
  525. }
  526. else
  527. {
  528. /* Arguments passed via the General Purpose Registers. */
  529. num_elements += ((len + 3) / 4);
  530. }
  531. }
  532. /* Always allocate at least six words. */
  533. sp -= std::max (6, num_elements) * 4;
  534. /* The psABI says that "Software convention requires space for the
  535. struct/union return value pointer, even if the word is unused." */
  536. sp -= 4;
  537. /* The psABI says that "Although software convention and the
  538. operating system require every stack frame to be doubleword
  539. aligned." */
  540. sp &= ~0x7;
  541. for (i = 0; i < nargs; i++)
  542. {
  543. const bfd_byte *valbuf = value_contents (args[i]).data ();
  544. struct type *type = value_type (args[i]);
  545. int len = TYPE_LENGTH (type);
  546. gdb_byte buf[4];
  547. if (len < 4)
  548. {
  549. memset (buf, 0, 4 - len);
  550. memcpy (buf + 4 - len, valbuf, len);
  551. valbuf = buf;
  552. len = 4;
  553. }
  554. gdb_assert (len == 4 || len == 8);
  555. if (element < 6)
  556. {
  557. int regnum = SPARC_O0_REGNUM + element;
  558. regcache->cooked_write (regnum, valbuf);
  559. if (len > 4 && element < 5)
  560. regcache->cooked_write (regnum + 1, valbuf + 4);
  561. }
  562. /* Always store the argument in memory. */
  563. write_memory (sp + 4 + element * 4, valbuf, len);
  564. element += len / 4;
  565. }
  566. gdb_assert (element == num_elements);
  567. if (return_method == return_method_struct)
  568. {
  569. gdb_byte buf[4];
  570. store_unsigned_integer (buf, 4, byte_order, struct_addr);
  571. write_memory (sp, buf, 4);
  572. }
  573. return sp;
  574. }
  575. static CORE_ADDR
  576. sparc32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  577. struct regcache *regcache, CORE_ADDR bp_addr,
  578. int nargs, struct value **args, CORE_ADDR sp,
  579. function_call_return_method return_method,
  580. CORE_ADDR struct_addr)
  581. {
  582. CORE_ADDR call_pc = (return_method == return_method_struct
  583. ? (bp_addr - 12) : (bp_addr - 8));
  584. /* Set return address. */
  585. regcache_cooked_write_unsigned (regcache, SPARC_O7_REGNUM, call_pc);
  586. /* Set up function arguments. */
  587. sp = sparc32_store_arguments (regcache, nargs, args, sp, return_method,
  588. struct_addr);
  589. /* Allocate the 16-word window save area. */
  590. sp -= 16 * 4;
  591. /* Stack should be doubleword aligned at this point. */
  592. gdb_assert (sp % 8 == 0);
  593. /* Finally, update the stack pointer. */
  594. regcache_cooked_write_unsigned (regcache, SPARC_SP_REGNUM, sp);
  595. return sp;
  596. }
  597. /* Use the program counter to determine the contents and size of a
  598. breakpoint instruction. Return a pointer to a string of bytes that
  599. encode a breakpoint instruction, store the length of the string in
  600. *LEN and optionally adjust *PC to point to the correct memory
  601. location for inserting the breakpoint. */
  602. constexpr gdb_byte sparc_break_insn[] = { 0x91, 0xd0, 0x20, 0x01 };
  603. typedef BP_MANIPULATION (sparc_break_insn) sparc_breakpoint;
  604. /* Allocate and initialize a frame cache. */
  605. static struct sparc_frame_cache *
  606. sparc_alloc_frame_cache (void)
  607. {
  608. struct sparc_frame_cache *cache;
  609. cache = FRAME_OBSTACK_ZALLOC (struct sparc_frame_cache);
  610. /* Base address. */
  611. cache->base = 0;
  612. cache->pc = 0;
  613. /* Frameless until proven otherwise. */
  614. cache->frameless_p = 1;
  615. cache->frame_offset = 0;
  616. cache->saved_regs_mask = 0;
  617. cache->copied_regs_mask = 0;
  618. cache->struct_return_p = 0;
  619. return cache;
  620. }
  621. /* GCC generates several well-known sequences of instructions at the begining
  622. of each function prologue when compiling with -fstack-check. If one of
  623. such sequences starts at START_PC, then return the address of the
  624. instruction immediately past this sequence. Otherwise, return START_PC. */
  625. static CORE_ADDR
  626. sparc_skip_stack_check (const CORE_ADDR start_pc)
  627. {
  628. CORE_ADDR pc = start_pc;
  629. unsigned long insn;
  630. int probing_loop = 0;
  631. /* With GCC, all stack checking sequences begin with the same two
  632. instructions, plus an optional one in the case of a probing loop:
  633. sethi <some immediate>, %g1
  634. sub %sp, %g1, %g1
  635. or:
  636. sethi <some immediate>, %g1
  637. sethi <some immediate>, %g4
  638. sub %sp, %g1, %g1
  639. or:
  640. sethi <some immediate>, %g1
  641. sub %sp, %g1, %g1
  642. sethi <some immediate>, %g4
  643. If the optional instruction is found (setting g4), assume that a
  644. probing loop will follow. */
  645. /* sethi <some immediate>, %g1 */
  646. insn = sparc_fetch_instruction (pc);
  647. pc = pc + 4;
  648. if (!(X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 1))
  649. return start_pc;
  650. /* optional: sethi <some immediate>, %g4 */
  651. insn = sparc_fetch_instruction (pc);
  652. pc = pc + 4;
  653. if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
  654. {
  655. probing_loop = 1;
  656. insn = sparc_fetch_instruction (pc);
  657. pc = pc + 4;
  658. }
  659. /* sub %sp, %g1, %g1 */
  660. if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
  661. && X_RD (insn) == 1 && X_RS1 (insn) == 14 && X_RS2 (insn) == 1))
  662. return start_pc;
  663. insn = sparc_fetch_instruction (pc);
  664. pc = pc + 4;
  665. /* optional: sethi <some immediate>, %g4 */
  666. if (X_OP (insn) == 0 && X_OP2 (insn) == 0x4 && X_RD (insn) == 4)
  667. {
  668. probing_loop = 1;
  669. insn = sparc_fetch_instruction (pc);
  670. pc = pc + 4;
  671. }
  672. /* First possible sequence:
  673. [first two instructions above]
  674. clr [%g1 - some immediate] */
  675. /* clr [%g1 - some immediate] */
  676. if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
  677. && X_RS1 (insn) == 1 && X_RD (insn) == 0)
  678. {
  679. /* Valid stack-check sequence, return the new PC. */
  680. return pc;
  681. }
  682. /* Second possible sequence: A small number of probes.
  683. [first two instructions above]
  684. clr [%g1]
  685. add %g1, -<some immediate>, %g1
  686. clr [%g1]
  687. [repeat the two instructions above any (small) number of times]
  688. clr [%g1 - some immediate] */
  689. /* clr [%g1] */
  690. else if (X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
  691. && X_RS1 (insn) == 1 && X_RD (insn) == 0)
  692. {
  693. while (1)
  694. {
  695. /* add %g1, -<some immediate>, %g1 */
  696. insn = sparc_fetch_instruction (pc);
  697. pc = pc + 4;
  698. if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
  699. && X_RS1 (insn) == 1 && X_RD (insn) == 1))
  700. break;
  701. /* clr [%g1] */
  702. insn = sparc_fetch_instruction (pc);
  703. pc = pc + 4;
  704. if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && !X_I(insn)
  705. && X_RD (insn) == 0 && X_RS1 (insn) == 1))
  706. return start_pc;
  707. }
  708. /* clr [%g1 - some immediate] */
  709. if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
  710. && X_RS1 (insn) == 1 && X_RD (insn) == 0))
  711. return start_pc;
  712. /* We found a valid stack-check sequence, return the new PC. */
  713. return pc;
  714. }
  715. /* Third sequence: A probing loop.
  716. [first three instructions above]
  717. sub %g1, %g4, %g4
  718. cmp %g1, %g4
  719. be <disp>
  720. add %g1, -<some immediate>, %g1
  721. ba <disp>
  722. clr [%g1]
  723. And an optional last probe for the remainder:
  724. clr [%g4 - some immediate] */
  725. if (probing_loop)
  726. {
  727. /* sub %g1, %g4, %g4 */
  728. if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x4 && !X_I(insn)
  729. && X_RD (insn) == 4 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
  730. return start_pc;
  731. /* cmp %g1, %g4 */
  732. insn = sparc_fetch_instruction (pc);
  733. pc = pc + 4;
  734. if (!(X_OP (insn) == 2 && X_OP3 (insn) == 0x14 && !X_I(insn)
  735. && X_RD (insn) == 0 && X_RS1 (insn) == 1 && X_RS2 (insn) == 4))
  736. return start_pc;
  737. /* be <disp> */
  738. insn = sparc_fetch_instruction (pc);
  739. pc = pc + 4;
  740. if (!(X_OP (insn) == 0 && X_COND (insn) == 0x1))
  741. return start_pc;
  742. /* add %g1, -<some immediate>, %g1 */
  743. insn = sparc_fetch_instruction (pc);
  744. pc = pc + 4;
  745. if (!(X_OP (insn) == 2 && X_OP3(insn) == 0 && X_I(insn)
  746. && X_RS1 (insn) == 1 && X_RD (insn) == 1))
  747. return start_pc;
  748. /* ba <disp> */
  749. insn = sparc_fetch_instruction (pc);
  750. pc = pc + 4;
  751. if (!(X_OP (insn) == 0 && X_COND (insn) == 0x8))
  752. return start_pc;
  753. /* clr [%g1] (st %g0, [%g1] or st %g0, [%g1+0]) */
  754. insn = sparc_fetch_instruction (pc);
  755. pc = pc + 4;
  756. if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4
  757. && X_RD (insn) == 0 && X_RS1 (insn) == 1
  758. && (!X_I(insn) || X_SIMM13 (insn) == 0)))
  759. return start_pc;
  760. /* We found a valid stack-check sequence, return the new PC. */
  761. /* optional: clr [%g4 - some immediate] */
  762. insn = sparc_fetch_instruction (pc);
  763. pc = pc + 4;
  764. if (!(X_OP (insn) == 3 && X_OP3(insn) == 0x4 && X_I(insn)
  765. && X_RS1 (insn) == 4 && X_RD (insn) == 0))
  766. return pc - 4;
  767. else
  768. return pc;
  769. }
  770. /* No stack check code in our prologue, return the start_pc. */
  771. return start_pc;
  772. }
  773. /* Record the effect of a SAVE instruction on CACHE. */
  774. void
  775. sparc_record_save_insn (struct sparc_frame_cache *cache)
  776. {
  777. /* The frame is set up. */
  778. cache->frameless_p = 0;
  779. /* The frame pointer contains the CFA. */
  780. cache->frame_offset = 0;
  781. /* The `local' and `in' registers are all saved. */
  782. cache->saved_regs_mask = 0xffff;
  783. /* The `out' registers are all renamed. */
  784. cache->copied_regs_mask = 0xff;
  785. }
  786. /* Do a full analysis of the prologue at PC and update CACHE accordingly.
  787. Bail out early if CURRENT_PC is reached. Return the address where
  788. the analysis stopped.
  789. We handle both the traditional register window model and the single
  790. register window (aka flat) model. */
  791. CORE_ADDR
  792. sparc_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  793. CORE_ADDR current_pc, struct sparc_frame_cache *cache)
  794. {
  795. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  796. unsigned long insn;
  797. int offset = 0;
  798. int dest = -1;
  799. pc = sparc_skip_stack_check (pc);
  800. if (current_pc <= pc)
  801. return current_pc;
  802. /* We have to handle to "Procedure Linkage Table" (PLT) special. On
  803. SPARC the linker usually defines a symbol (typically
  804. _PROCEDURE_LINKAGE_TABLE_) at the start of the .plt section.
  805. This symbol makes us end up here with PC pointing at the start of
  806. the PLT and CURRENT_PC probably pointing at a PLT entry. If we
  807. would do our normal prologue analysis, we would probably conclude
  808. that we've got a frame when in reality we don't, since the
  809. dynamic linker patches up the first PLT with some code that
  810. starts with a SAVE instruction. Patch up PC such that it points
  811. at the start of our PLT entry. */
  812. if (tdep->plt_entry_size > 0 && in_plt_section (current_pc))
  813. pc = current_pc - ((current_pc - pc) % tdep->plt_entry_size);
  814. insn = sparc_fetch_instruction (pc);
  815. /* Recognize store insns and record their sources. */
  816. while (X_OP (insn) == 3
  817. && (X_OP3 (insn) == 0x4 /* stw */
  818. || X_OP3 (insn) == 0x7 /* std */
  819. || X_OP3 (insn) == 0xe) /* stx */
  820. && X_RS1 (insn) == SPARC_SP_REGNUM)
  821. {
  822. int regnum = X_RD (insn);
  823. /* Recognize stores into the corresponding stack slots. */
  824. if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
  825. && ((X_I (insn)
  826. && X_SIMM13 (insn) == (X_OP3 (insn) == 0xe
  827. ? (regnum - SPARC_L0_REGNUM) * 8 + BIAS
  828. : (regnum - SPARC_L0_REGNUM) * 4))
  829. || (!X_I (insn) && regnum == SPARC_L0_REGNUM)))
  830. {
  831. cache->saved_regs_mask |= (1 << (regnum - SPARC_L0_REGNUM));
  832. if (X_OP3 (insn) == 0x7)
  833. cache->saved_regs_mask |= (1 << (regnum + 1 - SPARC_L0_REGNUM));
  834. }
  835. offset += 4;
  836. insn = sparc_fetch_instruction (pc + offset);
  837. }
  838. /* Recognize a SETHI insn and record its destination. */
  839. if (X_OP (insn) == 0 && X_OP2 (insn) == 0x04)
  840. {
  841. dest = X_RD (insn);
  842. offset += 4;
  843. insn = sparc_fetch_instruction (pc + offset);
  844. }
  845. /* Allow for an arithmetic operation on DEST or %g1. */
  846. if (X_OP (insn) == 2 && X_I (insn)
  847. && (X_RD (insn) == 1 || X_RD (insn) == dest))
  848. {
  849. offset += 4;
  850. insn = sparc_fetch_instruction (pc + offset);
  851. }
  852. /* Check for the SAVE instruction that sets up the frame. */
  853. if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3c)
  854. {
  855. sparc_record_save_insn (cache);
  856. offset += 4;
  857. return pc + offset;
  858. }
  859. /* Check for an arithmetic operation on %sp. */
  860. if (X_OP (insn) == 2
  861. && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
  862. && X_RS1 (insn) == SPARC_SP_REGNUM
  863. && X_RD (insn) == SPARC_SP_REGNUM)
  864. {
  865. if (X_I (insn))
  866. {
  867. cache->frame_offset = X_SIMM13 (insn);
  868. if (X_OP3 (insn) == 0)
  869. cache->frame_offset = -cache->frame_offset;
  870. }
  871. offset += 4;
  872. insn = sparc_fetch_instruction (pc + offset);
  873. /* Check for an arithmetic operation that sets up the frame. */
  874. if (X_OP (insn) == 2
  875. && (X_OP3 (insn) == 0 || X_OP3 (insn) == 0x4)
  876. && X_RS1 (insn) == SPARC_SP_REGNUM
  877. && X_RD (insn) == SPARC_FP_REGNUM)
  878. {
  879. cache->frameless_p = 0;
  880. cache->frame_offset = 0;
  881. /* We could check that the amount subtracted to %sp above is the
  882. same as the one added here, but this seems superfluous. */
  883. cache->copied_regs_mask |= 0x40;
  884. offset += 4;
  885. insn = sparc_fetch_instruction (pc + offset);
  886. }
  887. /* Check for a move (or) operation that copies the return register. */
  888. if (X_OP (insn) == 2
  889. && X_OP3 (insn) == 0x2
  890. && !X_I (insn)
  891. && X_RS1 (insn) == SPARC_G0_REGNUM
  892. && X_RS2 (insn) == SPARC_O7_REGNUM
  893. && X_RD (insn) == SPARC_I7_REGNUM)
  894. {
  895. cache->copied_regs_mask |= 0x80;
  896. offset += 4;
  897. }
  898. return pc + offset;
  899. }
  900. return pc;
  901. }
  902. /* Return PC of first real instruction of the function starting at
  903. START_PC. */
  904. static CORE_ADDR
  905. sparc32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  906. {
  907. CORE_ADDR func_addr;
  908. struct sparc_frame_cache cache;
  909. /* This is the preferred method, find the end of the prologue by
  910. using the debugging information. */
  911. if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
  912. {
  913. CORE_ADDR post_prologue_pc
  914. = skip_prologue_using_sal (gdbarch, func_addr);
  915. if (post_prologue_pc != 0)
  916. return std::max (start_pc, post_prologue_pc);
  917. }
  918. start_pc = sparc_analyze_prologue (gdbarch, start_pc, 0xffffffffUL, &cache);
  919. /* The psABI says that "Although the first 6 words of arguments
  920. reside in registers, the standard stack frame reserves space for
  921. them.". It also suggests that a function may use that space to
  922. "write incoming arguments 0 to 5" into that space, and that's
  923. indeed what GCC seems to be doing. In that case GCC will
  924. generate debug information that points to the stack slots instead
  925. of the registers, so we should consider the instructions that
  926. write out these incoming arguments onto the stack. */
  927. while (1)
  928. {
  929. unsigned long insn = sparc_fetch_instruction (start_pc);
  930. /* Recognize instructions that store incoming arguments into the
  931. corresponding stack slots. */
  932. if (X_OP (insn) == 3 && (X_OP3 (insn) & 0x3c) == 0x04
  933. && X_I (insn) && X_RS1 (insn) == SPARC_FP_REGNUM)
  934. {
  935. int regnum = X_RD (insn);
  936. /* Case of arguments still in %o[0..5]. */
  937. if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O5_REGNUM
  938. && !(cache.copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM)))
  939. && X_SIMM13 (insn) == 68 + (regnum - SPARC_O0_REGNUM) * 4)
  940. {
  941. start_pc += 4;
  942. continue;
  943. }
  944. /* Case of arguments copied into %i[0..5]. */
  945. if (regnum >= SPARC_I0_REGNUM && regnum <= SPARC_I5_REGNUM
  946. && (cache.copied_regs_mask & (1 << (regnum - SPARC_I0_REGNUM)))
  947. && X_SIMM13 (insn) == 68 + (regnum - SPARC_I0_REGNUM) * 4)
  948. {
  949. start_pc += 4;
  950. continue;
  951. }
  952. }
  953. break;
  954. }
  955. return start_pc;
  956. }
  957. /* Normal frames. */
  958. struct sparc_frame_cache *
  959. sparc_frame_cache (struct frame_info *this_frame, void **this_cache)
  960. {
  961. struct sparc_frame_cache *cache;
  962. if (*this_cache)
  963. return (struct sparc_frame_cache *) *this_cache;
  964. cache = sparc_alloc_frame_cache ();
  965. *this_cache = cache;
  966. cache->pc = get_frame_func (this_frame);
  967. if (cache->pc != 0)
  968. sparc_analyze_prologue (get_frame_arch (this_frame), cache->pc,
  969. get_frame_pc (this_frame), cache);
  970. if (cache->frameless_p)
  971. {
  972. /* This function is frameless, so %fp (%i6) holds the frame
  973. pointer for our calling frame. Use %sp (%o6) as this frame's
  974. base address. */
  975. cache->base =
  976. get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  977. }
  978. else
  979. {
  980. /* For normal frames, %fp (%i6) holds the frame pointer, the
  981. base address for the current stack frame. */
  982. cache->base =
  983. get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  984. }
  985. cache->base += cache->frame_offset;
  986. if (cache->base & 1)
  987. cache->base += BIAS;
  988. return cache;
  989. }
  990. static int
  991. sparc32_struct_return_from_sym (struct symbol *sym)
  992. {
  993. struct type *type = check_typedef (sym->type ());
  994. enum type_code code = type->code ();
  995. if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
  996. {
  997. type = check_typedef (TYPE_TARGET_TYPE (type));
  998. if (sparc_structure_or_union_p (type)
  999. || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16))
  1000. return 1;
  1001. }
  1002. return 0;
  1003. }
  1004. struct sparc_frame_cache *
  1005. sparc32_frame_cache (struct frame_info *this_frame, void **this_cache)
  1006. {
  1007. struct sparc_frame_cache *cache;
  1008. struct symbol *sym;
  1009. if (*this_cache)
  1010. return (struct sparc_frame_cache *) *this_cache;
  1011. cache = sparc_frame_cache (this_frame, this_cache);
  1012. sym = find_pc_function (cache->pc);
  1013. if (sym)
  1014. {
  1015. cache->struct_return_p = sparc32_struct_return_from_sym (sym);
  1016. }
  1017. else
  1018. {
  1019. /* There is no debugging information for this function to
  1020. help us determine whether this function returns a struct
  1021. or not. So we rely on another heuristic which is to check
  1022. the instruction at the return address and see if this is
  1023. an "unimp" instruction. If it is, then it is a struct-return
  1024. function. */
  1025. CORE_ADDR pc;
  1026. int regnum =
  1027. (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
  1028. pc = get_frame_register_unsigned (this_frame, regnum) + 8;
  1029. if (sparc_is_unimp_insn (pc))
  1030. cache->struct_return_p = 1;
  1031. }
  1032. return cache;
  1033. }
  1034. static void
  1035. sparc32_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1036. struct frame_id *this_id)
  1037. {
  1038. struct sparc_frame_cache *cache =
  1039. sparc32_frame_cache (this_frame, this_cache);
  1040. /* This marks the outermost frame. */
  1041. if (cache->base == 0)
  1042. return;
  1043. (*this_id) = frame_id_build (cache->base, cache->pc);
  1044. }
  1045. static struct value *
  1046. sparc32_frame_prev_register (struct frame_info *this_frame,
  1047. void **this_cache, int regnum)
  1048. {
  1049. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1050. struct sparc_frame_cache *cache =
  1051. sparc32_frame_cache (this_frame, this_cache);
  1052. if (regnum == SPARC32_PC_REGNUM || regnum == SPARC32_NPC_REGNUM)
  1053. {
  1054. CORE_ADDR pc = (regnum == SPARC32_NPC_REGNUM) ? 4 : 0;
  1055. /* If this functions has a Structure, Union or Quad-Precision
  1056. return value, we have to skip the UNIMP instruction that encodes
  1057. the size of the structure. */
  1058. if (cache->struct_return_p)
  1059. pc += 4;
  1060. regnum =
  1061. (cache->copied_regs_mask & 0x80) ? SPARC_I7_REGNUM : SPARC_O7_REGNUM;
  1062. pc += get_frame_register_unsigned (this_frame, regnum) + 8;
  1063. return frame_unwind_got_constant (this_frame, regnum, pc);
  1064. }
  1065. /* Handle StackGhost. */
  1066. {
  1067. ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1068. if (wcookie != 0 && !cache->frameless_p && regnum == SPARC_I7_REGNUM)
  1069. {
  1070. CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
  1071. ULONGEST i7;
  1072. /* Read the value in from memory. */
  1073. i7 = get_frame_memory_unsigned (this_frame, addr, 4);
  1074. return frame_unwind_got_constant (this_frame, regnum, i7 ^ wcookie);
  1075. }
  1076. }
  1077. /* The previous frame's `local' and `in' registers may have been saved
  1078. in the register save area. */
  1079. if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM
  1080. && (cache->saved_regs_mask & (1 << (regnum - SPARC_L0_REGNUM))))
  1081. {
  1082. CORE_ADDR addr = cache->base + (regnum - SPARC_L0_REGNUM) * 4;
  1083. return frame_unwind_got_memory (this_frame, regnum, addr);
  1084. }
  1085. /* The previous frame's `out' registers may be accessible as the current
  1086. frame's `in' registers. */
  1087. if (regnum >= SPARC_O0_REGNUM && regnum <= SPARC_O7_REGNUM
  1088. && (cache->copied_regs_mask & (1 << (regnum - SPARC_O0_REGNUM))))
  1089. regnum += (SPARC_I0_REGNUM - SPARC_O0_REGNUM);
  1090. return frame_unwind_got_register (this_frame, regnum, regnum);
  1091. }
  1092. static const struct frame_unwind sparc32_frame_unwind =
  1093. {
  1094. "sparc32 prologue",
  1095. NORMAL_FRAME,
  1096. default_frame_unwind_stop_reason,
  1097. sparc32_frame_this_id,
  1098. sparc32_frame_prev_register,
  1099. NULL,
  1100. default_frame_sniffer
  1101. };
  1102. static CORE_ADDR
  1103. sparc32_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1104. {
  1105. struct sparc_frame_cache *cache =
  1106. sparc32_frame_cache (this_frame, this_cache);
  1107. return cache->base;
  1108. }
  1109. static const struct frame_base sparc32_frame_base =
  1110. {
  1111. &sparc32_frame_unwind,
  1112. sparc32_frame_base_address,
  1113. sparc32_frame_base_address,
  1114. sparc32_frame_base_address
  1115. };
  1116. static struct frame_id
  1117. sparc_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1118. {
  1119. CORE_ADDR sp;
  1120. sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  1121. if (sp & 1)
  1122. sp += BIAS;
  1123. return frame_id_build (sp, get_frame_pc (this_frame));
  1124. }
  1125. /* Extract a function return value of TYPE from REGCACHE, and copy
  1126. that into VALBUF. */
  1127. static void
  1128. sparc32_extract_return_value (struct type *type, struct regcache *regcache,
  1129. gdb_byte *valbuf)
  1130. {
  1131. int len = TYPE_LENGTH (type);
  1132. gdb_byte buf[32];
  1133. gdb_assert (!sparc_structure_return_p (type));
  1134. if (sparc_floating_p (type) || sparc_complex_floating_p (type)
  1135. || type->code () == TYPE_CODE_ARRAY)
  1136. {
  1137. /* Floating return values. */
  1138. regcache->cooked_read (SPARC_F0_REGNUM, buf);
  1139. if (len > 4)
  1140. regcache->cooked_read (SPARC_F1_REGNUM, buf + 4);
  1141. if (len > 8)
  1142. {
  1143. regcache->cooked_read (SPARC_F2_REGNUM, buf + 8);
  1144. regcache->cooked_read (SPARC_F3_REGNUM, buf + 12);
  1145. }
  1146. if (len > 16)
  1147. {
  1148. regcache->cooked_read (SPARC_F4_REGNUM, buf + 16);
  1149. regcache->cooked_read (SPARC_F5_REGNUM, buf + 20);
  1150. regcache->cooked_read (SPARC_F6_REGNUM, buf + 24);
  1151. regcache->cooked_read (SPARC_F7_REGNUM, buf + 28);
  1152. }
  1153. memcpy (valbuf, buf, len);
  1154. }
  1155. else
  1156. {
  1157. /* Integral and pointer return values. */
  1158. gdb_assert (sparc_integral_or_pointer_p (type));
  1159. regcache->cooked_read (SPARC_O0_REGNUM, buf);
  1160. if (len > 4)
  1161. {
  1162. regcache->cooked_read (SPARC_O1_REGNUM, buf + 4);
  1163. gdb_assert (len == 8);
  1164. memcpy (valbuf, buf, 8);
  1165. }
  1166. else
  1167. {
  1168. /* Just stripping off any unused bytes should preserve the
  1169. signed-ness just fine. */
  1170. memcpy (valbuf, buf + 4 - len, len);
  1171. }
  1172. }
  1173. }
  1174. /* Store the function return value of type TYPE from VALBUF into
  1175. REGCACHE. */
  1176. static void
  1177. sparc32_store_return_value (struct type *type, struct regcache *regcache,
  1178. const gdb_byte *valbuf)
  1179. {
  1180. int len = TYPE_LENGTH (type);
  1181. gdb_byte buf[32];
  1182. gdb_assert (!sparc_structure_return_p (type));
  1183. if (sparc_floating_p (type) || sparc_complex_floating_p (type))
  1184. {
  1185. /* Floating return values. */
  1186. memcpy (buf, valbuf, len);
  1187. regcache->cooked_write (SPARC_F0_REGNUM, buf);
  1188. if (len > 4)
  1189. regcache->cooked_write (SPARC_F1_REGNUM, buf + 4);
  1190. if (len > 8)
  1191. {
  1192. regcache->cooked_write (SPARC_F2_REGNUM, buf + 8);
  1193. regcache->cooked_write (SPARC_F3_REGNUM, buf + 12);
  1194. }
  1195. if (len > 16)
  1196. {
  1197. regcache->cooked_write (SPARC_F4_REGNUM, buf + 16);
  1198. regcache->cooked_write (SPARC_F5_REGNUM, buf + 20);
  1199. regcache->cooked_write (SPARC_F6_REGNUM, buf + 24);
  1200. regcache->cooked_write (SPARC_F7_REGNUM, buf + 28);
  1201. }
  1202. }
  1203. else
  1204. {
  1205. /* Integral and pointer return values. */
  1206. gdb_assert (sparc_integral_or_pointer_p (type));
  1207. if (len > 4)
  1208. {
  1209. gdb_assert (len == 8);
  1210. memcpy (buf, valbuf, 8);
  1211. regcache->cooked_write (SPARC_O1_REGNUM, buf + 4);
  1212. }
  1213. else
  1214. {
  1215. /* ??? Do we need to do any sign-extension here? */
  1216. memcpy (buf + 4 - len, valbuf, len);
  1217. }
  1218. regcache->cooked_write (SPARC_O0_REGNUM, buf);
  1219. }
  1220. }
  1221. static enum return_value_convention
  1222. sparc32_return_value (struct gdbarch *gdbarch, struct value *function,
  1223. struct type *type, struct regcache *regcache,
  1224. gdb_byte *readbuf, const gdb_byte *writebuf)
  1225. {
  1226. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1227. /* The psABI says that "...every stack frame reserves the word at
  1228. %fp+64. If a function returns a structure, union, or
  1229. quad-precision value, this word should hold the address of the
  1230. object into which the return value should be copied." This
  1231. guarantees that we can always find the return value, not just
  1232. before the function returns. */
  1233. if (sparc_structure_return_p (type))
  1234. {
  1235. ULONGEST sp;
  1236. CORE_ADDR addr;
  1237. if (readbuf)
  1238. {
  1239. regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1240. addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
  1241. read_memory (addr, readbuf, TYPE_LENGTH (type));
  1242. }
  1243. if (writebuf)
  1244. {
  1245. regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1246. addr = read_memory_unsigned_integer (sp + 64, 4, byte_order);
  1247. write_memory (addr, writebuf, TYPE_LENGTH (type));
  1248. }
  1249. return RETURN_VALUE_ABI_PRESERVES_ADDRESS;
  1250. }
  1251. if (readbuf)
  1252. sparc32_extract_return_value (type, regcache, readbuf);
  1253. if (writebuf)
  1254. sparc32_store_return_value (type, regcache, writebuf);
  1255. return RETURN_VALUE_REGISTER_CONVENTION;
  1256. }
  1257. static int
  1258. sparc32_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
  1259. {
  1260. return (sparc_structure_or_union_p (type)
  1261. || (sparc_floating_p (type) && TYPE_LENGTH (type) == 16)
  1262. || sparc_complex_floating_p (type));
  1263. }
  1264. static int
  1265. sparc32_dwarf2_struct_return_p (struct frame_info *this_frame)
  1266. {
  1267. CORE_ADDR pc = get_frame_address_in_block (this_frame);
  1268. struct symbol *sym = find_pc_function (pc);
  1269. if (sym)
  1270. return sparc32_struct_return_from_sym (sym);
  1271. return 0;
  1272. }
  1273. static void
  1274. sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1275. struct dwarf2_frame_state_reg *reg,
  1276. struct frame_info *this_frame)
  1277. {
  1278. int off;
  1279. switch (regnum)
  1280. {
  1281. case SPARC_G0_REGNUM:
  1282. /* Since %g0 is always zero, there is no point in saving it, and
  1283. people will be inclined omit it from the CFI. Make sure we
  1284. don't warn about that. */
  1285. reg->how = DWARF2_FRAME_REG_SAME_VALUE;
  1286. break;
  1287. case SPARC_SP_REGNUM:
  1288. reg->how = DWARF2_FRAME_REG_CFA;
  1289. break;
  1290. case SPARC32_PC_REGNUM:
  1291. case SPARC32_NPC_REGNUM:
  1292. reg->how = DWARF2_FRAME_REG_RA_OFFSET;
  1293. off = 8;
  1294. if (sparc32_dwarf2_struct_return_p (this_frame))
  1295. off += 4;
  1296. if (regnum == SPARC32_NPC_REGNUM)
  1297. off += 4;
  1298. reg->loc.offset = off;
  1299. break;
  1300. }
  1301. }
  1302. /* Implement the execute_dwarf_cfa_vendor_op method. */
  1303. static bool
  1304. sparc_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
  1305. struct dwarf2_frame_state *fs)
  1306. {
  1307. /* Only DW_CFA_GNU_window_save is expected on SPARC. */
  1308. if (op != DW_CFA_GNU_window_save)
  1309. return false;
  1310. uint64_t reg;
  1311. int size = register_size (gdbarch, 0);
  1312. fs->regs.alloc_regs (32);
  1313. for (reg = 8; reg < 16; reg++)
  1314. {
  1315. fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
  1316. fs->regs.reg[reg].loc.reg = reg + 16;
  1317. }
  1318. for (reg = 16; reg < 32; reg++)
  1319. {
  1320. fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
  1321. fs->regs.reg[reg].loc.offset = (reg - 16) * size;
  1322. }
  1323. return true;
  1324. }
  1325. /* The SPARC Architecture doesn't have hardware single-step support,
  1326. and most operating systems don't implement it either, so we provide
  1327. software single-step mechanism. */
  1328. static CORE_ADDR
  1329. sparc_analyze_control_transfer (struct regcache *regcache,
  1330. CORE_ADDR pc, CORE_ADDR *npc)
  1331. {
  1332. unsigned long insn = sparc_fetch_instruction (pc);
  1333. int conditional_p = X_COND (insn) & 0x7;
  1334. int branch_p = 0, fused_p = 0;
  1335. long offset = 0; /* Must be signed for sign-extend. */
  1336. if (X_OP (insn) == 0 && X_OP2 (insn) == 3)
  1337. {
  1338. if ((insn & 0x10000000) == 0)
  1339. {
  1340. /* Branch on Integer Register with Prediction (BPr). */
  1341. branch_p = 1;
  1342. conditional_p = 1;
  1343. }
  1344. else
  1345. {
  1346. /* Compare and Branch */
  1347. branch_p = 1;
  1348. fused_p = 1;
  1349. offset = 4 * X_DISP10 (insn);
  1350. }
  1351. }
  1352. else if (X_OP (insn) == 0 && X_OP2 (insn) == 6)
  1353. {
  1354. /* Branch on Floating-Point Condition Codes (FBfcc). */
  1355. branch_p = 1;
  1356. offset = 4 * X_DISP22 (insn);
  1357. }
  1358. else if (X_OP (insn) == 0 && X_OP2 (insn) == 5)
  1359. {
  1360. /* Branch on Floating-Point Condition Codes with Prediction
  1361. (FBPfcc). */
  1362. branch_p = 1;
  1363. offset = 4 * X_DISP19 (insn);
  1364. }
  1365. else if (X_OP (insn) == 0 && X_OP2 (insn) == 2)
  1366. {
  1367. /* Branch on Integer Condition Codes (Bicc). */
  1368. branch_p = 1;
  1369. offset = 4 * X_DISP22 (insn);
  1370. }
  1371. else if (X_OP (insn) == 0 && X_OP2 (insn) == 1)
  1372. {
  1373. /* Branch on Integer Condition Codes with Prediction (BPcc). */
  1374. branch_p = 1;
  1375. offset = 4 * X_DISP19 (insn);
  1376. }
  1377. else if (X_OP (insn) == 2 && X_OP3 (insn) == 0x3a)
  1378. {
  1379. struct frame_info *frame = get_current_frame ();
  1380. /* Trap instruction (TRAP). */
  1381. gdbarch *arch = regcache->arch ();
  1382. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (arch);
  1383. return tdep->step_trap (frame, insn);
  1384. }
  1385. /* FIXME: Handle DONE and RETRY instructions. */
  1386. if (branch_p)
  1387. {
  1388. if (fused_p)
  1389. {
  1390. /* Fused compare-and-branch instructions are non-delayed,
  1391. and do not have an annulling capability. So we need to
  1392. always set a breakpoint on both the NPC and the branch
  1393. target address. */
  1394. gdb_assert (offset != 0);
  1395. return pc + offset;
  1396. }
  1397. else if (conditional_p)
  1398. {
  1399. /* For conditional branches, return nPC + 4 iff the annul
  1400. bit is 1. */
  1401. return (X_A (insn) ? *npc + 4 : 0);
  1402. }
  1403. else
  1404. {
  1405. /* For unconditional branches, return the target if its
  1406. specified condition is "always" and return nPC + 4 if the
  1407. condition is "never". If the annul bit is 1, set *NPC to
  1408. zero. */
  1409. if (X_COND (insn) == 0x0)
  1410. pc = *npc, offset = 4;
  1411. if (X_A (insn))
  1412. *npc = 0;
  1413. return pc + offset;
  1414. }
  1415. }
  1416. return 0;
  1417. }
  1418. static CORE_ADDR
  1419. sparc_step_trap (struct frame_info *frame, unsigned long insn)
  1420. {
  1421. return 0;
  1422. }
  1423. static std::vector<CORE_ADDR>
  1424. sparc_software_single_step (struct regcache *regcache)
  1425. {
  1426. struct gdbarch *arch = regcache->arch ();
  1427. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (arch);
  1428. CORE_ADDR npc, nnpc;
  1429. CORE_ADDR pc, orig_npc;
  1430. std::vector<CORE_ADDR> next_pcs;
  1431. pc = regcache_raw_get_unsigned (regcache, tdep->pc_regnum);
  1432. orig_npc = npc = regcache_raw_get_unsigned (regcache, tdep->npc_regnum);
  1433. /* Analyze the instruction at PC. */
  1434. nnpc = sparc_analyze_control_transfer (regcache, pc, &npc);
  1435. if (npc != 0)
  1436. next_pcs.push_back (npc);
  1437. if (nnpc != 0)
  1438. next_pcs.push_back (nnpc);
  1439. /* Assert that we have set at least one breakpoint, and that
  1440. they're not set at the same spot - unless we're going
  1441. from here straight to NULL, i.e. a call or jump to 0. */
  1442. gdb_assert (npc != 0 || nnpc != 0 || orig_npc == 0);
  1443. gdb_assert (nnpc != npc || orig_npc == 0);
  1444. return next_pcs;
  1445. }
  1446. static void
  1447. sparc_write_pc (struct regcache *regcache, CORE_ADDR pc)
  1448. {
  1449. gdbarch *arch = regcache->arch ();
  1450. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (arch);
  1451. regcache_cooked_write_unsigned (regcache, tdep->pc_regnum, pc);
  1452. regcache_cooked_write_unsigned (regcache, tdep->npc_regnum, pc + 4);
  1453. }
  1454. /* Iterate over core file register note sections. */
  1455. static void
  1456. sparc_iterate_over_regset_sections (struct gdbarch *gdbarch,
  1457. iterate_over_regset_sections_cb *cb,
  1458. void *cb_data,
  1459. const struct regcache *regcache)
  1460. {
  1461. sparc_gdbarch_tdep *tdep = (sparc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1462. cb (".reg", tdep->sizeof_gregset, tdep->sizeof_gregset, tdep->gregset, NULL,
  1463. cb_data);
  1464. cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, tdep->fpregset,
  1465. NULL, cb_data);
  1466. }
  1467. static int
  1468. validate_tdesc_registers (const struct target_desc *tdesc,
  1469. struct tdesc_arch_data *tdesc_data,
  1470. const char *feature_name,
  1471. const char * const register_names[],
  1472. unsigned int registers_num,
  1473. unsigned int reg_start)
  1474. {
  1475. int valid_p = 1;
  1476. const struct tdesc_feature *feature;
  1477. feature = tdesc_find_feature (tdesc, feature_name);
  1478. if (feature == NULL)
  1479. return 0;
  1480. for (unsigned int i = 0; i < registers_num; i++)
  1481. valid_p &= tdesc_numbered_register (feature, tdesc_data,
  1482. reg_start + i,
  1483. register_names[i]);
  1484. return valid_p;
  1485. }
  1486. static struct gdbarch *
  1487. sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1488. {
  1489. const struct target_desc *tdesc = info.target_desc;
  1490. struct gdbarch *gdbarch;
  1491. int valid_p = 1;
  1492. /* If there is already a candidate, use it. */
  1493. arches = gdbarch_list_lookup_by_info (arches, &info);
  1494. if (arches != NULL)
  1495. return arches->gdbarch;
  1496. /* Allocate space for the new architecture. */
  1497. sparc_gdbarch_tdep *tdep = new sparc_gdbarch_tdep;
  1498. gdbarch = gdbarch_alloc (&info, tdep);
  1499. tdep->pc_regnum = SPARC32_PC_REGNUM;
  1500. tdep->npc_regnum = SPARC32_NPC_REGNUM;
  1501. tdep->step_trap = sparc_step_trap;
  1502. tdep->fpu_register_names = sparc32_fpu_register_names;
  1503. tdep->fpu_registers_num = ARRAY_SIZE (sparc32_fpu_register_names);
  1504. tdep->cp0_register_names = sparc32_cp0_register_names;
  1505. tdep->cp0_registers_num = ARRAY_SIZE (sparc32_cp0_register_names);
  1506. set_gdbarch_long_double_bit (gdbarch, 128);
  1507. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
  1508. set_gdbarch_wchar_bit (gdbarch, 16);
  1509. set_gdbarch_wchar_signed (gdbarch, 1);
  1510. set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
  1511. set_gdbarch_register_name (gdbarch, sparc32_register_name);
  1512. set_gdbarch_register_type (gdbarch, sparc32_register_type);
  1513. set_gdbarch_num_pseudo_regs (gdbarch, SPARC32_NUM_PSEUDO_REGS);
  1514. set_tdesc_pseudo_register_name (gdbarch, sparc32_pseudo_register_name);
  1515. set_tdesc_pseudo_register_type (gdbarch, sparc32_pseudo_register_type);
  1516. set_gdbarch_pseudo_register_read (gdbarch, sparc32_pseudo_register_read);
  1517. set_gdbarch_pseudo_register_write (gdbarch, sparc32_pseudo_register_write);
  1518. /* Register numbers of various important registers. */
  1519. set_gdbarch_sp_regnum (gdbarch, SPARC_SP_REGNUM); /* %sp */
  1520. set_gdbarch_pc_regnum (gdbarch, SPARC32_PC_REGNUM); /* %pc */
  1521. set_gdbarch_fp0_regnum (gdbarch, SPARC_F0_REGNUM); /* %f0 */
  1522. /* Call dummy code. */
  1523. set_gdbarch_frame_align (gdbarch, sparc32_frame_align);
  1524. set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  1525. set_gdbarch_push_dummy_code (gdbarch, sparc32_push_dummy_code);
  1526. set_gdbarch_push_dummy_call (gdbarch, sparc32_push_dummy_call);
  1527. set_gdbarch_return_value (gdbarch, sparc32_return_value);
  1528. set_gdbarch_stabs_argument_has_addr
  1529. (gdbarch, sparc32_stabs_argument_has_addr);
  1530. set_gdbarch_skip_prologue (gdbarch, sparc32_skip_prologue);
  1531. /* Stack grows downward. */
  1532. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1533. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  1534. sparc_breakpoint::kind_from_pc);
  1535. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  1536. sparc_breakpoint::bp_from_kind);
  1537. set_gdbarch_frame_args_skip (gdbarch, 8);
  1538. set_gdbarch_software_single_step (gdbarch, sparc_software_single_step);
  1539. set_gdbarch_write_pc (gdbarch, sparc_write_pc);
  1540. set_gdbarch_dummy_id (gdbarch, sparc_dummy_id);
  1541. frame_base_set_default (gdbarch, &sparc32_frame_base);
  1542. /* Hook in the DWARF CFI frame unwinder. */
  1543. dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
  1544. /* Register DWARF vendor CFI handler. */
  1545. set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
  1546. sparc_execute_dwarf_cfa_vendor_op);
  1547. /* FIXME: kettenis/20050423: Don't enable the unwinder until the
  1548. StackGhost issues have been resolved. */
  1549. /* Hook in ABI-specific overrides, if they have been registered. */
  1550. gdbarch_init_osabi (info, gdbarch);
  1551. frame_unwind_append_unwinder (gdbarch, &sparc32_frame_unwind);
  1552. if (tdesc_has_registers (tdesc))
  1553. {
  1554. tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
  1555. /* Validate that the descriptor provides the mandatory registers
  1556. and allocate their numbers. */
  1557. valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
  1558. "org.gnu.gdb.sparc.cpu",
  1559. sparc_core_register_names,
  1560. ARRAY_SIZE (sparc_core_register_names),
  1561. SPARC_G0_REGNUM);
  1562. valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
  1563. "org.gnu.gdb.sparc.fpu",
  1564. tdep->fpu_register_names,
  1565. tdep->fpu_registers_num,
  1566. SPARC_F0_REGNUM);
  1567. valid_p &= validate_tdesc_registers (tdesc, tdesc_data.get (),
  1568. "org.gnu.gdb.sparc.cp0",
  1569. tdep->cp0_register_names,
  1570. tdep->cp0_registers_num,
  1571. SPARC_F0_REGNUM
  1572. + tdep->fpu_registers_num);
  1573. if (!valid_p)
  1574. return NULL;
  1575. /* Target description may have changed. */
  1576. info.tdesc_data = tdesc_data.get ();
  1577. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  1578. }
  1579. /* If we have register sets, enable the generic core file support. */
  1580. if (tdep->gregset)
  1581. set_gdbarch_iterate_over_regset_sections
  1582. (gdbarch, sparc_iterate_over_regset_sections);
  1583. register_sparc_ravenscar_ops (gdbarch);
  1584. return gdbarch;
  1585. }
  1586. /* Helper functions for dealing with register windows. */
  1587. void
  1588. sparc_supply_rwindow (struct regcache *regcache, CORE_ADDR sp, int regnum)
  1589. {
  1590. struct gdbarch *gdbarch = regcache->arch ();
  1591. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1592. int offset = 0;
  1593. gdb_byte buf[8];
  1594. int i;
  1595. /* This function calls functions that depend on the global current thread. */
  1596. gdb_assert (regcache->ptid () == inferior_ptid);
  1597. if (sp & 1)
  1598. {
  1599. /* Registers are 64-bit. */
  1600. sp += BIAS;
  1601. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1602. {
  1603. if (regnum == i || regnum == -1)
  1604. {
  1605. target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
  1606. /* Handle StackGhost. */
  1607. if (i == SPARC_I7_REGNUM)
  1608. {
  1609. ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1610. ULONGEST i7;
  1611. i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
  1612. store_unsigned_integer (buf + offset, 8, byte_order,
  1613. i7 ^ wcookie);
  1614. }
  1615. regcache->raw_supply (i, buf);
  1616. }
  1617. }
  1618. }
  1619. else
  1620. {
  1621. /* Registers are 32-bit. Toss any sign-extension of the stack
  1622. pointer. */
  1623. sp &= 0xffffffffUL;
  1624. /* Clear out the top half of the temporary buffer, and put the
  1625. register value in the bottom half if we're in 64-bit mode. */
  1626. if (gdbarch_ptr_bit (regcache->arch ()) == 64)
  1627. {
  1628. memset (buf, 0, 4);
  1629. offset = 4;
  1630. }
  1631. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1632. {
  1633. if (regnum == i || regnum == -1)
  1634. {
  1635. target_read_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
  1636. buf + offset, 4);
  1637. /* Handle StackGhost. */
  1638. if (i == SPARC_I7_REGNUM)
  1639. {
  1640. ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1641. ULONGEST i7;
  1642. i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
  1643. store_unsigned_integer (buf + offset, 4, byte_order,
  1644. i7 ^ wcookie);
  1645. }
  1646. regcache->raw_supply (i, buf);
  1647. }
  1648. }
  1649. }
  1650. }
  1651. void
  1652. sparc_collect_rwindow (const struct regcache *regcache,
  1653. CORE_ADDR sp, int regnum)
  1654. {
  1655. struct gdbarch *gdbarch = regcache->arch ();
  1656. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1657. int offset = 0;
  1658. gdb_byte buf[8];
  1659. int i;
  1660. /* This function calls functions that depend on the global current thread. */
  1661. gdb_assert (regcache->ptid () == inferior_ptid);
  1662. if (sp & 1)
  1663. {
  1664. /* Registers are 64-bit. */
  1665. sp += BIAS;
  1666. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1667. {
  1668. if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
  1669. {
  1670. regcache->raw_collect (i, buf);
  1671. /* Handle StackGhost. */
  1672. if (i == SPARC_I7_REGNUM)
  1673. {
  1674. ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1675. ULONGEST i7;
  1676. i7 = extract_unsigned_integer (buf + offset, 8, byte_order);
  1677. store_unsigned_integer (buf, 8, byte_order, i7 ^ wcookie);
  1678. }
  1679. target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 8), buf, 8);
  1680. }
  1681. }
  1682. }
  1683. else
  1684. {
  1685. /* Registers are 32-bit. Toss any sign-extension of the stack
  1686. pointer. */
  1687. sp &= 0xffffffffUL;
  1688. /* Only use the bottom half if we're in 64-bit mode. */
  1689. if (gdbarch_ptr_bit (regcache->arch ()) == 64)
  1690. offset = 4;
  1691. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1692. {
  1693. if (regnum == -1 || regnum == SPARC_SP_REGNUM || regnum == i)
  1694. {
  1695. regcache->raw_collect (i, buf);
  1696. /* Handle StackGhost. */
  1697. if (i == SPARC_I7_REGNUM)
  1698. {
  1699. ULONGEST wcookie = sparc_fetch_wcookie (gdbarch);
  1700. ULONGEST i7;
  1701. i7 = extract_unsigned_integer (buf + offset, 4, byte_order);
  1702. store_unsigned_integer (buf + offset, 4, byte_order,
  1703. i7 ^ wcookie);
  1704. }
  1705. target_write_memory (sp + ((i - SPARC_L0_REGNUM) * 4),
  1706. buf + offset, 4);
  1707. }
  1708. }
  1709. }
  1710. }
  1711. /* Helper functions for dealing with register sets. */
  1712. void
  1713. sparc32_supply_gregset (const struct sparc_gregmap *gregmap,
  1714. struct regcache *regcache,
  1715. int regnum, const void *gregs)
  1716. {
  1717. const gdb_byte *regs = (const gdb_byte *) gregs;
  1718. gdb_byte zero[4] = { 0 };
  1719. int i;
  1720. if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1721. regcache->raw_supply (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
  1722. if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1723. regcache->raw_supply (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
  1724. if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1725. regcache->raw_supply (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
  1726. if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1727. regcache->raw_supply (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
  1728. if (regnum == SPARC_G0_REGNUM || regnum == -1)
  1729. regcache->raw_supply (SPARC_G0_REGNUM, &zero);
  1730. if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1731. {
  1732. int offset = gregmap->r_g1_offset;
  1733. for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1734. {
  1735. if (regnum == i || regnum == -1)
  1736. regcache->raw_supply (i, regs + offset);
  1737. offset += 4;
  1738. }
  1739. }
  1740. if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1741. {
  1742. /* Not all of the register set variants include Locals and
  1743. Inputs. For those that don't, we read them off the stack. */
  1744. if (gregmap->r_l0_offset == -1)
  1745. {
  1746. ULONGEST sp;
  1747. regcache_cooked_read_unsigned (regcache, SPARC_SP_REGNUM, &sp);
  1748. sparc_supply_rwindow (regcache, sp, regnum);
  1749. }
  1750. else
  1751. {
  1752. int offset = gregmap->r_l0_offset;
  1753. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1754. {
  1755. if (regnum == i || regnum == -1)
  1756. regcache->raw_supply (i, regs + offset);
  1757. offset += 4;
  1758. }
  1759. }
  1760. }
  1761. }
  1762. void
  1763. sparc32_collect_gregset (const struct sparc_gregmap *gregmap,
  1764. const struct regcache *regcache,
  1765. int regnum, void *gregs)
  1766. {
  1767. gdb_byte *regs = (gdb_byte *) gregs;
  1768. int i;
  1769. if (regnum == SPARC32_PSR_REGNUM || regnum == -1)
  1770. regcache->raw_collect (SPARC32_PSR_REGNUM, regs + gregmap->r_psr_offset);
  1771. if (regnum == SPARC32_PC_REGNUM || regnum == -1)
  1772. regcache->raw_collect (SPARC32_PC_REGNUM, regs + gregmap->r_pc_offset);
  1773. if (regnum == SPARC32_NPC_REGNUM || regnum == -1)
  1774. regcache->raw_collect (SPARC32_NPC_REGNUM, regs + gregmap->r_npc_offset);
  1775. if (regnum == SPARC32_Y_REGNUM || regnum == -1)
  1776. regcache->raw_collect (SPARC32_Y_REGNUM, regs + gregmap->r_y_offset);
  1777. if ((regnum >= SPARC_G1_REGNUM && regnum <= SPARC_O7_REGNUM) || regnum == -1)
  1778. {
  1779. int offset = gregmap->r_g1_offset;
  1780. /* %g0 is always zero. */
  1781. for (i = SPARC_G1_REGNUM; i <= SPARC_O7_REGNUM; i++)
  1782. {
  1783. if (regnum == i || regnum == -1)
  1784. regcache->raw_collect (i, regs + offset);
  1785. offset += 4;
  1786. }
  1787. }
  1788. if ((regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) || regnum == -1)
  1789. {
  1790. /* Not all of the register set variants include Locals and
  1791. Inputs. For those that don't, we read them off the stack. */
  1792. if (gregmap->r_l0_offset != -1)
  1793. {
  1794. int offset = gregmap->r_l0_offset;
  1795. for (i = SPARC_L0_REGNUM; i <= SPARC_I7_REGNUM; i++)
  1796. {
  1797. if (regnum == i || regnum == -1)
  1798. regcache->raw_collect (i, regs + offset);
  1799. offset += 4;
  1800. }
  1801. }
  1802. }
  1803. }
  1804. void
  1805. sparc32_supply_fpregset (const struct sparc_fpregmap *fpregmap,
  1806. struct regcache *regcache,
  1807. int regnum, const void *fpregs)
  1808. {
  1809. const gdb_byte *regs = (const gdb_byte *) fpregs;
  1810. int i;
  1811. for (i = 0; i < 32; i++)
  1812. {
  1813. if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1814. regcache->raw_supply (SPARC_F0_REGNUM + i,
  1815. regs + fpregmap->r_f0_offset + (i * 4));
  1816. }
  1817. if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1818. regcache->raw_supply (SPARC32_FSR_REGNUM, regs + fpregmap->r_fsr_offset);
  1819. }
  1820. void
  1821. sparc32_collect_fpregset (const struct sparc_fpregmap *fpregmap,
  1822. const struct regcache *regcache,
  1823. int regnum, void *fpregs)
  1824. {
  1825. gdb_byte *regs = (gdb_byte *) fpregs;
  1826. int i;
  1827. for (i = 0; i < 32; i++)
  1828. {
  1829. if (regnum == (SPARC_F0_REGNUM + i) || regnum == -1)
  1830. regcache->raw_collect (SPARC_F0_REGNUM + i,
  1831. regs + fpregmap->r_f0_offset + (i * 4));
  1832. }
  1833. if (regnum == SPARC32_FSR_REGNUM || regnum == -1)
  1834. regcache->raw_collect (SPARC32_FSR_REGNUM,
  1835. regs + fpregmap->r_fsr_offset);
  1836. }
  1837. /* SunOS 4. */
  1838. /* From <machine/reg.h>. */
  1839. const struct sparc_gregmap sparc32_sunos4_gregmap =
  1840. {
  1841. 0 * 4, /* %psr */
  1842. 1 * 4, /* %pc */
  1843. 2 * 4, /* %npc */
  1844. 3 * 4, /* %y */
  1845. -1, /* %wim */
  1846. -1, /* %tbr */
  1847. 4 * 4, /* %g1 */
  1848. -1 /* %l0 */
  1849. };
  1850. const struct sparc_fpregmap sparc32_sunos4_fpregmap =
  1851. {
  1852. 0 * 4, /* %f0 */
  1853. 33 * 4, /* %fsr */
  1854. };
  1855. const struct sparc_fpregmap sparc32_bsd_fpregmap =
  1856. {
  1857. 0 * 4, /* %f0 */
  1858. 32 * 4, /* %fsr */
  1859. };
  1860. void _initialize_sparc_tdep ();
  1861. void
  1862. _initialize_sparc_tdep ()
  1863. {
  1864. register_gdbarch_init (bfd_arch_sparc, sparc32_gdbarch_init);
  1865. }