arc-tdep.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  1. /* Target dependent code for ARC architecture, for GDB.
  2. Copyright 2005-2022 Free Software Foundation, Inc.
  3. Contributed by Synopsys Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* GDB header files. */
  16. #include "defs.h"
  17. #include "arch-utils.h"
  18. #include "elf-bfd.h"
  19. #include "disasm.h"
  20. #include "dwarf2/frame.h"
  21. #include "frame-base.h"
  22. #include "frame-unwind.h"
  23. #include "gdbcore.h"
  24. #include "reggroups.h"
  25. #include "gdbcmd.h"
  26. #include "objfiles.h"
  27. #include "osabi.h"
  28. #include "prologue-value.h"
  29. #include "target-descriptions.h"
  30. #include "trad-frame.h"
  31. /* ARC header files. */
  32. #include "opcode/arc.h"
  33. #include "opcodes/arc-dis.h"
  34. #include "arc-tdep.h"
  35. #include "arch/arc.h"
  36. /* Standard headers. */
  37. #include <algorithm>
  38. #include <sstream>
  39. /* The frame unwind cache for ARC. */
  40. struct arc_frame_cache
  41. {
  42. /* The stack pointer at the time this frame was created; i.e. the caller's
  43. stack pointer when this function was called. It is used to identify this
  44. frame. */
  45. CORE_ADDR prev_sp;
  46. /* Register that is a base for this frame - FP for normal frame, SP for
  47. non-FP frames. */
  48. int frame_base_reg;
  49. /* Offset from the previous SP to the current frame base. If GCC uses
  50. `SUB SP,SP,offset` to allocate space for local variables, then it will be
  51. done after setting up a frame pointer, but it still will be considered
  52. part of prologue, therefore SP will be lesser than FP at the end of the
  53. prologue analysis. In this case that would be an offset from old SP to a
  54. new FP. But in case of non-FP frames, frame base is an SP and thus that
  55. would be an offset from old SP to new SP. What is important is that this
  56. is an offset from old SP to a known register, so it can be used to find
  57. old SP.
  58. Using FP is preferable, when possible, because SP can change in function
  59. body after prologue due to alloca, variadic arguments or other shenanigans.
  60. If that is the case in the caller frame, then PREV_SP will point to SP at
  61. the moment of function call, but it will be different from SP value at the
  62. end of the caller prologue. As a result it will not be possible to
  63. reconstruct caller's frame and go past it in the backtrace. Those things
  64. are unlikely to happen to FP - FP value at the moment of function call (as
  65. stored on stack in callee prologue) is also an FP value at the end of the
  66. caller's prologue. */
  67. LONGEST frame_base_offset;
  68. /* Store addresses for registers saved in prologue. During prologue analysis
  69. GDB stores offsets relatively to "old SP", then after old SP is evaluated,
  70. offsets are replaced with absolute addresses. */
  71. trad_frame_saved_reg *saved_regs;
  72. };
  73. /* Global debug flag. */
  74. bool arc_debug;
  75. /* List of "maintenance print arc" commands. */
  76. static struct cmd_list_element *maintenance_print_arc_list = NULL;
  77. /* A set of registers that we expect to find in a tdesc_feature. These
  78. are used in ARC_TDESC_INIT when processing the target description. */
  79. struct arc_register_feature
  80. {
  81. /* Information for a single register. */
  82. struct register_info
  83. {
  84. /* The GDB register number for this register. */
  85. int regnum;
  86. /* List of names for this register. The first name in this list is the
  87. preferred name, the name GDB will use when describing this register. */
  88. std::vector<const char *> names;
  89. /* When true, this register must be present in this feature set. */
  90. bool required_p;
  91. };
  92. /* The name for this feature. This is the name used to find this feature
  93. within the target description. */
  94. const char *name;
  95. /* List of all the registers that we expect to encounter in this register
  96. set. */
  97. std::vector<struct register_info> registers;
  98. };
  99. /* Obsolete feature names for backward compatibility. */
  100. static const char *ARC_CORE_V1_OBSOLETE_FEATURE_NAME
  101. = "org.gnu.gdb.arc.core.arcompact";
  102. static const char *ARC_CORE_V2_OBSOLETE_FEATURE_NAME
  103. = "org.gnu.gdb.arc.core.v2";
  104. static const char *ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME
  105. = "org.gnu.gdb.arc.core-reduced.v2";
  106. static const char *ARC_AUX_OBSOLETE_FEATURE_NAME
  107. = "org.gnu.gdb.arc.aux-minimal";
  108. /* Modern feature names. */
  109. static const char *ARC_CORE_FEATURE_NAME = "org.gnu.gdb.arc.core";
  110. static const char *ARC_AUX_FEATURE_NAME = "org.gnu.gdb.arc.aux";
  111. /* ARCv1 (ARC600, ARC601, ARC700) general core registers feature set.
  112. See also arc_update_acc_reg_names() for "accl/acch" names. */
  113. static struct arc_register_feature arc_v1_core_reg_feature =
  114. {
  115. ARC_CORE_FEATURE_NAME,
  116. {
  117. { ARC_R0_REGNUM + 0, { "r0" }, true },
  118. { ARC_R0_REGNUM + 1, { "r1" }, true },
  119. { ARC_R0_REGNUM + 2, { "r2" }, true },
  120. { ARC_R0_REGNUM + 3, { "r3" }, true },
  121. { ARC_R0_REGNUM + 4, { "r4" }, false },
  122. { ARC_R0_REGNUM + 5, { "r5" }, false },
  123. { ARC_R0_REGNUM + 6, { "r6" }, false },
  124. { ARC_R0_REGNUM + 7, { "r7" }, false },
  125. { ARC_R0_REGNUM + 8, { "r8" }, false },
  126. { ARC_R0_REGNUM + 9, { "r9" }, false },
  127. { ARC_R0_REGNUM + 10, { "r10" }, true },
  128. { ARC_R0_REGNUM + 11, { "r11" }, true },
  129. { ARC_R0_REGNUM + 12, { "r12" }, true },
  130. { ARC_R0_REGNUM + 13, { "r13" }, true },
  131. { ARC_R0_REGNUM + 14, { "r14" }, true },
  132. { ARC_R0_REGNUM + 15, { "r15" }, true },
  133. { ARC_R0_REGNUM + 16, { "r16" }, false },
  134. { ARC_R0_REGNUM + 17, { "r17" }, false },
  135. { ARC_R0_REGNUM + 18, { "r18" }, false },
  136. { ARC_R0_REGNUM + 19, { "r19" }, false },
  137. { ARC_R0_REGNUM + 20, { "r20" }, false },
  138. { ARC_R0_REGNUM + 21, { "r21" }, false },
  139. { ARC_R0_REGNUM + 22, { "r22" }, false },
  140. { ARC_R0_REGNUM + 23, { "r23" }, false },
  141. { ARC_R0_REGNUM + 24, { "r24" }, false },
  142. { ARC_R0_REGNUM + 25, { "r25" }, false },
  143. { ARC_R0_REGNUM + 26, { "gp" }, true },
  144. { ARC_R0_REGNUM + 27, { "fp" }, true },
  145. { ARC_R0_REGNUM + 28, { "sp" }, true },
  146. { ARC_R0_REGNUM + 29, { "ilink1" }, false },
  147. { ARC_R0_REGNUM + 30, { "ilink2" }, false },
  148. { ARC_R0_REGNUM + 31, { "blink" }, true },
  149. { ARC_R0_REGNUM + 32, { "r32" }, false },
  150. { ARC_R0_REGNUM + 33, { "r33" }, false },
  151. { ARC_R0_REGNUM + 34, { "r34" }, false },
  152. { ARC_R0_REGNUM + 35, { "r35" }, false },
  153. { ARC_R0_REGNUM + 36, { "r36" }, false },
  154. { ARC_R0_REGNUM + 37, { "r37" }, false },
  155. { ARC_R0_REGNUM + 38, { "r38" }, false },
  156. { ARC_R0_REGNUM + 39, { "r39" }, false },
  157. { ARC_R0_REGNUM + 40, { "r40" }, false },
  158. { ARC_R0_REGNUM + 41, { "r41" }, false },
  159. { ARC_R0_REGNUM + 42, { "r42" }, false },
  160. { ARC_R0_REGNUM + 43, { "r43" }, false },
  161. { ARC_R0_REGNUM + 44, { "r44" }, false },
  162. { ARC_R0_REGNUM + 45, { "r45" }, false },
  163. { ARC_R0_REGNUM + 46, { "r46" }, false },
  164. { ARC_R0_REGNUM + 47, { "r47" }, false },
  165. { ARC_R0_REGNUM + 48, { "r48" }, false },
  166. { ARC_R0_REGNUM + 49, { "r49" }, false },
  167. { ARC_R0_REGNUM + 50, { "r50" }, false },
  168. { ARC_R0_REGNUM + 51, { "r51" }, false },
  169. { ARC_R0_REGNUM + 52, { "r52" }, false },
  170. { ARC_R0_REGNUM + 53, { "r53" }, false },
  171. { ARC_R0_REGNUM + 54, { "r54" }, false },
  172. { ARC_R0_REGNUM + 55, { "r55" }, false },
  173. { ARC_R0_REGNUM + 56, { "r56" }, false },
  174. { ARC_R0_REGNUM + 57, { "r57" }, false },
  175. { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
  176. { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
  177. { ARC_R0_REGNUM + 60, { "lp_count" }, false },
  178. { ARC_R0_REGNUM + 61, { "reserved" }, false },
  179. { ARC_R0_REGNUM + 62, { "limm" }, false },
  180. { ARC_R0_REGNUM + 63, { "pcl" }, true }
  181. }
  182. };
  183. /* ARCv2 (ARCHS) general core registers feature set. See also
  184. arc_update_acc_reg_names() for "accl/acch" names. */
  185. static struct arc_register_feature arc_v2_core_reg_feature =
  186. {
  187. ARC_CORE_FEATURE_NAME,
  188. {
  189. { ARC_R0_REGNUM + 0, { "r0" }, true },
  190. { ARC_R0_REGNUM + 1, { "r1" }, true },
  191. { ARC_R0_REGNUM + 2, { "r2" }, true },
  192. { ARC_R0_REGNUM + 3, { "r3" }, true },
  193. { ARC_R0_REGNUM + 4, { "r4" }, false },
  194. { ARC_R0_REGNUM + 5, { "r5" }, false },
  195. { ARC_R0_REGNUM + 6, { "r6" }, false },
  196. { ARC_R0_REGNUM + 7, { "r7" }, false },
  197. { ARC_R0_REGNUM + 8, { "r8" }, false },
  198. { ARC_R0_REGNUM + 9, { "r9" }, false },
  199. { ARC_R0_REGNUM + 10, { "r10" }, true },
  200. { ARC_R0_REGNUM + 11, { "r11" }, true },
  201. { ARC_R0_REGNUM + 12, { "r12" }, true },
  202. { ARC_R0_REGNUM + 13, { "r13" }, true },
  203. { ARC_R0_REGNUM + 14, { "r14" }, true },
  204. { ARC_R0_REGNUM + 15, { "r15" }, true },
  205. { ARC_R0_REGNUM + 16, { "r16" }, false },
  206. { ARC_R0_REGNUM + 17, { "r17" }, false },
  207. { ARC_R0_REGNUM + 18, { "r18" }, false },
  208. { ARC_R0_REGNUM + 19, { "r19" }, false },
  209. { ARC_R0_REGNUM + 20, { "r20" }, false },
  210. { ARC_R0_REGNUM + 21, { "r21" }, false },
  211. { ARC_R0_REGNUM + 22, { "r22" }, false },
  212. { ARC_R0_REGNUM + 23, { "r23" }, false },
  213. { ARC_R0_REGNUM + 24, { "r24" }, false },
  214. { ARC_R0_REGNUM + 25, { "r25" }, false },
  215. { ARC_R0_REGNUM + 26, { "gp" }, true },
  216. { ARC_R0_REGNUM + 27, { "fp" }, true },
  217. { ARC_R0_REGNUM + 28, { "sp" }, true },
  218. { ARC_R0_REGNUM + 29, { "ilink" }, false },
  219. { ARC_R0_REGNUM + 30, { "r30" }, true },
  220. { ARC_R0_REGNUM + 31, { "blink" }, true },
  221. { ARC_R0_REGNUM + 32, { "r32" }, false },
  222. { ARC_R0_REGNUM + 33, { "r33" }, false },
  223. { ARC_R0_REGNUM + 34, { "r34" }, false },
  224. { ARC_R0_REGNUM + 35, { "r35" }, false },
  225. { ARC_R0_REGNUM + 36, { "r36" }, false },
  226. { ARC_R0_REGNUM + 37, { "r37" }, false },
  227. { ARC_R0_REGNUM + 38, { "r38" }, false },
  228. { ARC_R0_REGNUM + 39, { "r39" }, false },
  229. { ARC_R0_REGNUM + 40, { "r40" }, false },
  230. { ARC_R0_REGNUM + 41, { "r41" }, false },
  231. { ARC_R0_REGNUM + 42, { "r42" }, false },
  232. { ARC_R0_REGNUM + 43, { "r43" }, false },
  233. { ARC_R0_REGNUM + 44, { "r44" }, false },
  234. { ARC_R0_REGNUM + 45, { "r45" }, false },
  235. { ARC_R0_REGNUM + 46, { "r46" }, false },
  236. { ARC_R0_REGNUM + 47, { "r47" }, false },
  237. { ARC_R0_REGNUM + 48, { "r48" }, false },
  238. { ARC_R0_REGNUM + 49, { "r49" }, false },
  239. { ARC_R0_REGNUM + 50, { "r50" }, false },
  240. { ARC_R0_REGNUM + 51, { "r51" }, false },
  241. { ARC_R0_REGNUM + 52, { "r52" }, false },
  242. { ARC_R0_REGNUM + 53, { "r53" }, false },
  243. { ARC_R0_REGNUM + 54, { "r54" }, false },
  244. { ARC_R0_REGNUM + 55, { "r55" }, false },
  245. { ARC_R0_REGNUM + 56, { "r56" }, false },
  246. { ARC_R0_REGNUM + 57, { "r57" }, false },
  247. { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
  248. { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
  249. { ARC_R0_REGNUM + 60, { "lp_count" }, false },
  250. { ARC_R0_REGNUM + 61, { "reserved" }, false },
  251. { ARC_R0_REGNUM + 62, { "limm" }, false },
  252. { ARC_R0_REGNUM + 63, { "pcl" }, true }
  253. }
  254. };
  255. /* The common auxiliary registers feature set. The REGNUM field
  256. must match the ARC_REGNUM enum in arc-tdep.h. */
  257. static const struct arc_register_feature arc_common_aux_reg_feature =
  258. {
  259. ARC_AUX_FEATURE_NAME,
  260. {
  261. { ARC_FIRST_AUX_REGNUM + 0, { "pc" }, true },
  262. { ARC_FIRST_AUX_REGNUM + 1, { "status32" }, true },
  263. { ARC_FIRST_AUX_REGNUM + 2, { "lp_start" }, false },
  264. { ARC_FIRST_AUX_REGNUM + 3, { "lp_end" }, false },
  265. { ARC_FIRST_AUX_REGNUM + 4, { "bta" }, false }
  266. }
  267. };
  268. static char *arc_disassembler_options = NULL;
  269. /* Functions are sorted in the order as they are used in the
  270. _initialize_arc_tdep (), which uses the same order as gdbarch.h. Static
  271. functions are defined before the first invocation. */
  272. /* Returns an unsigned value of OPERAND_NUM in instruction INSN.
  273. For relative branch instructions returned value is an offset, not an actual
  274. branch target. */
  275. static ULONGEST
  276. arc_insn_get_operand_value (const struct arc_instruction &insn,
  277. unsigned int operand_num)
  278. {
  279. switch (insn.operands[operand_num].kind)
  280. {
  281. case ARC_OPERAND_KIND_LIMM:
  282. gdb_assert (insn.limm_p);
  283. return insn.limm_value;
  284. case ARC_OPERAND_KIND_SHIMM:
  285. return insn.operands[operand_num].value;
  286. default:
  287. /* Value in instruction is a register number. */
  288. struct regcache *regcache = get_current_regcache ();
  289. ULONGEST value;
  290. regcache_cooked_read_unsigned (regcache,
  291. insn.operands[operand_num].value,
  292. &value);
  293. return value;
  294. }
  295. }
  296. /* Like arc_insn_get_operand_value, but returns a signed value. */
  297. static LONGEST
  298. arc_insn_get_operand_value_signed (const struct arc_instruction &insn,
  299. unsigned int operand_num)
  300. {
  301. switch (insn.operands[operand_num].kind)
  302. {
  303. case ARC_OPERAND_KIND_LIMM:
  304. gdb_assert (insn.limm_p);
  305. /* Convert unsigned raw value to signed one. This assumes 2's
  306. complement arithmetic, but so is the LONG_MIN value from generic
  307. defs.h and that assumption is true for ARC. */
  308. gdb_static_assert (sizeof (insn.limm_value) == sizeof (int));
  309. return (((LONGEST) insn.limm_value) ^ INT_MIN) - INT_MIN;
  310. case ARC_OPERAND_KIND_SHIMM:
  311. /* Sign conversion has been done by binutils. */
  312. return insn.operands[operand_num].value;
  313. default:
  314. /* Value in instruction is a register number. */
  315. struct regcache *regcache = get_current_regcache ();
  316. LONGEST value;
  317. regcache_cooked_read_signed (regcache,
  318. insn.operands[operand_num].value,
  319. &value);
  320. return value;
  321. }
  322. }
  323. /* Get register with base address of memory operation. */
  324. static int
  325. arc_insn_get_memory_base_reg (const struct arc_instruction &insn)
  326. {
  327. /* POP_S and PUSH_S have SP as an implicit argument in a disassembler. */
  328. if (insn.insn_class == PUSH || insn.insn_class == POP)
  329. return ARC_SP_REGNUM;
  330. gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
  331. /* Other instructions all have at least two operands: operand 0 is data,
  332. operand 1 is address. Operand 2 is offset from address. However, see
  333. comment to arc_instruction.operands - in some cases, third operand may be
  334. missing, namely if it is 0. */
  335. gdb_assert (insn.operands_count >= 2);
  336. return insn.operands[1].value;
  337. }
  338. /* Get offset of a memory operation INSN. */
  339. static CORE_ADDR
  340. arc_insn_get_memory_offset (const struct arc_instruction &insn)
  341. {
  342. /* POP_S and PUSH_S have offset as an implicit argument in a
  343. disassembler. */
  344. if (insn.insn_class == POP)
  345. return 4;
  346. else if (insn.insn_class == PUSH)
  347. return -4;
  348. gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
  349. /* Other instructions all have at least two operands: operand 0 is data,
  350. operand 1 is address. Operand 2 is offset from address. However, see
  351. comment to arc_instruction.operands - in some cases, third operand may be
  352. missing, namely if it is 0. */
  353. if (insn.operands_count < 3)
  354. return 0;
  355. CORE_ADDR value = arc_insn_get_operand_value (insn, 2);
  356. /* Handle scaling. */
  357. if (insn.writeback_mode == ARC_WRITEBACK_AS)
  358. {
  359. /* Byte data size is not valid for AS. Halfword means shift by 1 bit.
  360. Word and double word means shift by 2 bits. */
  361. gdb_assert (insn.data_size_mode != ARC_SCALING_B);
  362. if (insn.data_size_mode == ARC_SCALING_H)
  363. value <<= 1;
  364. else
  365. value <<= 2;
  366. }
  367. return value;
  368. }
  369. CORE_ADDR
  370. arc_insn_get_branch_target (const struct arc_instruction &insn)
  371. {
  372. gdb_assert (insn.is_control_flow);
  373. /* BI [c]: PC = nextPC + (c << 2). */
  374. if (insn.insn_class == BI)
  375. {
  376. ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
  377. return arc_insn_get_linear_next_pc (insn) + (reg_value << 2);
  378. }
  379. /* BIH [c]: PC = nextPC + (c << 1). */
  380. else if (insn.insn_class == BIH)
  381. {
  382. ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
  383. return arc_insn_get_linear_next_pc (insn) + (reg_value << 1);
  384. }
  385. /* JLI and EI. */
  386. /* JLI and EI depend on optional AUX registers. Not supported right now. */
  387. else if (insn.insn_class == JLI)
  388. {
  389. gdb_printf (gdb_stderr,
  390. "JLI_S instruction is not supported by the GDB.");
  391. return 0;
  392. }
  393. else if (insn.insn_class == EI)
  394. {
  395. gdb_printf (gdb_stderr,
  396. "EI_S instruction is not supported by the GDB.");
  397. return 0;
  398. }
  399. /* LEAVE_S: PC = BLINK. */
  400. else if (insn.insn_class == LEAVE)
  401. {
  402. struct regcache *regcache = get_current_regcache ();
  403. ULONGEST value;
  404. regcache_cooked_read_unsigned (regcache, ARC_BLINK_REGNUM, &value);
  405. return value;
  406. }
  407. /* BBIT0/1, BRcc: PC = currentPC + operand. */
  408. else if (insn.insn_class == BBIT0 || insn.insn_class == BBIT1
  409. || insn.insn_class == BRCC)
  410. {
  411. /* Most instructions has branch target as their sole argument. However
  412. conditional brcc/bbit has it as a third operand. */
  413. CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 2);
  414. /* Offset is relative to the 4-byte aligned address of the current
  415. instruction, hence last two bits should be truncated. */
  416. return pcrel_addr + align_down (insn.address, 4);
  417. }
  418. /* B, Bcc, BL, BLcc, LP, LPcc: PC = currentPC + operand. */
  419. else if (insn.insn_class == BRANCH || insn.insn_class == LOOP)
  420. {
  421. CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 0);
  422. /* Offset is relative to the 4-byte aligned address of the current
  423. instruction, hence last two bits should be truncated. */
  424. return pcrel_addr + align_down (insn.address, 4);
  425. }
  426. /* J, Jcc, JL, JLcc: PC = operand. */
  427. else if (insn.insn_class == JUMP)
  428. {
  429. /* All jumps are single-operand. */
  430. return arc_insn_get_operand_value (insn, 0);
  431. }
  432. /* This is some new and unknown instruction. */
  433. gdb_assert_not_reached ("Unknown branch instruction.");
  434. }
  435. /* Dump INSN into gdb_stdlog. */
  436. static void
  437. arc_insn_dump (const struct arc_instruction &insn)
  438. {
  439. struct gdbarch *gdbarch = target_gdbarch ();
  440. arc_print ("Dumping arc_instruction at %s\n",
  441. paddress (gdbarch, insn.address));
  442. arc_print ("\tlength = %u\n", insn.length);
  443. if (!insn.valid)
  444. {
  445. arc_print ("\tThis is not a valid ARC instruction.\n");
  446. return;
  447. }
  448. arc_print ("\tlength_with_limm = %u\n", insn.length + (insn.limm_p ? 4 : 0));
  449. arc_print ("\tcc = 0x%x\n", insn.condition_code);
  450. arc_print ("\tinsn_class = %u\n", insn.insn_class);
  451. arc_print ("\tis_control_flow = %i\n", insn.is_control_flow);
  452. arc_print ("\thas_delay_slot = %i\n", insn.has_delay_slot);
  453. CORE_ADDR next_pc = arc_insn_get_linear_next_pc (insn);
  454. arc_print ("\tlinear_next_pc = %s\n", paddress (gdbarch, next_pc));
  455. if (insn.is_control_flow)
  456. {
  457. CORE_ADDR t = arc_insn_get_branch_target (insn);
  458. arc_print ("\tbranch_target = %s\n", paddress (gdbarch, t));
  459. }
  460. arc_print ("\tlimm_p = %i\n", insn.limm_p);
  461. if (insn.limm_p)
  462. arc_print ("\tlimm_value = 0x%08x\n", insn.limm_value);
  463. if (insn.insn_class == STORE || insn.insn_class == LOAD
  464. || insn.insn_class == PUSH || insn.insn_class == POP)
  465. {
  466. arc_print ("\twriteback_mode = %u\n", insn.writeback_mode);
  467. arc_print ("\tdata_size_mode = %u\n", insn.data_size_mode);
  468. arc_print ("\tmemory_base_register = %s\n",
  469. gdbarch_register_name (gdbarch,
  470. arc_insn_get_memory_base_reg (insn)));
  471. /* get_memory_offset returns an unsigned CORE_ADDR, but treat it as a
  472. LONGEST for a nicer representation. */
  473. arc_print ("\taddr_offset = %s\n",
  474. plongest (arc_insn_get_memory_offset (insn)));
  475. }
  476. arc_print ("\toperands_count = %u\n", insn.operands_count);
  477. for (unsigned int i = 0; i < insn.operands_count; ++i)
  478. {
  479. int is_reg = (insn.operands[i].kind == ARC_OPERAND_KIND_REG);
  480. arc_print ("\toperand[%u] = {\n", i);
  481. arc_print ("\t\tis_reg = %i\n", is_reg);
  482. if (is_reg)
  483. arc_print ("\t\tregister = %s\n",
  484. gdbarch_register_name (gdbarch, insn.operands[i].value));
  485. /* Don't know if this value is signed or not, so print both
  486. representations. This tends to look quite ugly, especially for big
  487. numbers. */
  488. arc_print ("\t\tunsigned value = %s\n",
  489. pulongest (arc_insn_get_operand_value (insn, i)));
  490. arc_print ("\t\tsigned value = %s\n",
  491. plongest (arc_insn_get_operand_value_signed (insn, i)));
  492. arc_print ("\t}\n");
  493. }
  494. }
  495. CORE_ADDR
  496. arc_insn_get_linear_next_pc (const struct arc_instruction &insn)
  497. {
  498. /* In ARC long immediate is always 4 bytes. */
  499. return (insn.address + insn.length + (insn.limm_p ? 4 : 0));
  500. }
  501. /* Implement the "write_pc" gdbarch method.
  502. In ARC PC register is a normal register so in most cases setting PC value
  503. is a straightforward process: debugger just writes PC value. However it
  504. gets trickier in case when current instruction is an instruction in delay
  505. slot. In this case CPU will execute instruction at current PC value, then
  506. will set PC to the current value of BTA register; also current instruction
  507. cannot be branch/jump and some of the other instruction types. Thus if
  508. debugger would try to just change PC value in this case, this instruction
  509. will get executed, but then core will "jump" to the original branch target.
  510. Whether current instruction is a delay-slot instruction or not is indicated
  511. by DE bit in STATUS32 register indicates if current instruction is a delay
  512. slot instruction. This bit is writable by debug host, which allows debug
  513. host to prevent core from jumping after the delay slot instruction. It
  514. also works in another direction: setting this bit will make core to treat
  515. any current instructions as a delay slot instruction and to set PC to the
  516. current value of BTA register.
  517. To workaround issues with changing PC register while in delay slot
  518. instruction, debugger should check for the STATUS32.DE bit and reset it if
  519. it is set. No other change is required in this function. Most common
  520. case, where this function might be required is calling inferior functions
  521. from debugger. Generic GDB logic handles this pretty well: current values
  522. of registers are stored, value of PC is changed (that is the job of this
  523. function), and after inferior function is executed, GDB restores all
  524. registers, include BTA and STATUS32, which also means that core is returned
  525. to its original state of being halted on delay slot instructions.
  526. This method is useless for ARC 600, because it doesn't have externally
  527. exposed BTA register. In the case of ARC 600 it is impossible to restore
  528. core to its state in all occasions thus core should never be halted (from
  529. the perspective of debugger host) in the delay slot. */
  530. static void
  531. arc_write_pc (struct regcache *regcache, CORE_ADDR new_pc)
  532. {
  533. struct gdbarch *gdbarch = regcache->arch ();
  534. arc_debug_printf ("Writing PC, new value=%s",
  535. paddress (gdbarch, new_pc));
  536. regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch),
  537. new_pc);
  538. ULONGEST status32;
  539. regcache_cooked_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
  540. &status32);
  541. if ((status32 & ARC_STATUS32_DE_MASK) != 0)
  542. {
  543. arc_debug_printf ("Changing PC while in delay slot. Will "
  544. "reset STATUS32.DE bit to zero. Value of STATUS32 "
  545. "register is 0x%s",
  546. phex (status32, ARC_REGISTER_SIZE));
  547. /* Reset bit and write to the cache. */
  548. status32 &= ~0x40;
  549. regcache_cooked_write_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
  550. status32);
  551. }
  552. }
  553. /* Implement the "virtual_frame_pointer" gdbarch method.
  554. According to ABI the FP (r27) is used to point to the middle of the current
  555. stack frame, just below the saved FP and before local variables, register
  556. spill area and outgoing args. However for optimization levels above O2 and
  557. in any case in leaf functions, the frame pointer is usually not set at all.
  558. The exception being when handling nested functions.
  559. We use this function to return a "virtual" frame pointer, marking the start
  560. of the current stack frame as a register-offset pair. If the FP is not
  561. being used, then it should return SP, with an offset of the frame size.
  562. The current implementation doesn't actually know the frame size, nor
  563. whether the FP is actually being used, so for now we just return SP and an
  564. offset of zero. This is no worse than other architectures, but is needed
  565. to avoid assertion failures.
  566. TODO: Can we determine the frame size to get a correct offset?
  567. PC is a program counter where we need the virtual FP. REG_PTR is the base
  568. register used for the virtual FP. OFFSET_PTR is the offset used for the
  569. virtual FP. */
  570. static void
  571. arc_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
  572. int *reg_ptr, LONGEST *offset_ptr)
  573. {
  574. *reg_ptr = gdbarch_sp_regnum (gdbarch);
  575. *offset_ptr = 0;
  576. }
  577. /* Implement the "push_dummy_call" gdbarch method.
  578. Stack Frame Layout
  579. This shows the layout of the stack frame for the general case of a
  580. function call; a given function might not have a variable number of
  581. arguments or local variables, or might not save any registers, so it would
  582. not have the corresponding frame areas. Additionally, a leaf function
  583. (i.e. one which calls no other functions) does not need to save the
  584. contents of the BLINK register (which holds its return address), and a
  585. function might not have a frame pointer.
  586. The stack grows downward, so SP points below FP in memory; SP always
  587. points to the last used word on the stack, not the first one.
  588. | | |
  589. | arg word N | | caller's
  590. | : | | frame
  591. | arg word 10 | |
  592. | arg word 9 | |
  593. old SP ---> +-----------------------+ --+
  594. | | |
  595. | callee-saved | |
  596. | registers | |
  597. | including fp, blink | |
  598. | | | callee's
  599. new FP ---> +-----------------------+ | frame
  600. | | |
  601. | local | |
  602. | variables | |
  603. | | |
  604. | register | |
  605. | spill area | |
  606. | | |
  607. | outgoing args | |
  608. | | |
  609. new SP ---> +-----------------------+ --+
  610. | |
  611. | unused |
  612. | |
  613. |
  614. |
  615. V
  616. downwards
  617. The list of arguments to be passed to a function is considered to be a
  618. sequence of _N_ words (as though all the parameters were stored in order in
  619. memory with each parameter occupying an integral number of words). Words
  620. 1..8 are passed in registers 0..7; if the function has more than 8 words of
  621. arguments then words 9..@em N are passed on the stack in the caller's frame.
  622. If the function has a variable number of arguments, e.g. it has a form such
  623. as `function (p1, p2, ...);' and _P_ words are required to hold the values
  624. of the named parameters (which are passed in registers 0..@em P -1), then
  625. the remaining 8 - _P_ words passed in registers _P_..7 are spilled into the
  626. top of the frame so that the anonymous parameter words occupy a continuous
  627. region.
  628. Any arguments are already in target byte order. We just need to store
  629. them!
  630. BP_ADDR is the return address where breakpoint must be placed. NARGS is
  631. the number of arguments to the function. ARGS is the arguments values (in
  632. target byte order). SP is the Current value of SP register. STRUCT_RETURN
  633. is TRUE if structures are returned by the function. STRUCT_ADDR is the
  634. hidden address for returning a struct. Returns SP of a new frame. */
  635. static CORE_ADDR
  636. arc_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  637. struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  638. struct value **args, CORE_ADDR sp,
  639. function_call_return_method return_method,
  640. CORE_ADDR struct_addr)
  641. {
  642. arc_debug_printf ("nargs = %d", nargs);
  643. int arg_reg = ARC_FIRST_ARG_REGNUM;
  644. /* Push the return address. */
  645. regcache_cooked_write_unsigned (regcache, ARC_BLINK_REGNUM, bp_addr);
  646. /* Are we returning a value using a structure return instead of a normal
  647. value return? If so, struct_addr is the address of the reserved space for
  648. the return structure to be written on the stack, and that address is
  649. passed to that function as a hidden first argument. */
  650. if (return_method == return_method_struct)
  651. {
  652. /* Pass the return address in the first argument register. */
  653. regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
  654. arc_debug_printf ("struct return address %s passed in R%d",
  655. print_core_address (gdbarch, struct_addr), arg_reg);
  656. arg_reg++;
  657. }
  658. if (nargs > 0)
  659. {
  660. unsigned int total_space = 0;
  661. /* How much space do the arguments occupy in total? Must round each
  662. argument's size up to an integral number of words. */
  663. for (int i = 0; i < nargs; i++)
  664. {
  665. unsigned int len = TYPE_LENGTH (value_type (args[i]));
  666. unsigned int space = align_up (len, 4);
  667. total_space += space;
  668. arc_debug_printf ("arg %d: %u bytes -> %u", i, len, space);
  669. }
  670. /* Allocate a buffer to hold a memory image of the arguments. */
  671. gdb_byte *memory_image = XCNEWVEC (gdb_byte, total_space);
  672. /* Now copy all of the arguments into the buffer, correctly aligned. */
  673. gdb_byte *data = memory_image;
  674. for (int i = 0; i < nargs; i++)
  675. {
  676. unsigned int len = TYPE_LENGTH (value_type (args[i]));
  677. unsigned int space = align_up (len, 4);
  678. memcpy (data, value_contents (args[i]).data (), (size_t) len);
  679. arc_debug_printf ("copying arg %d, val 0x%08x, len %d to mem",
  680. i, *((int *) value_contents (args[i]).data ()),
  681. len);
  682. data += space;
  683. }
  684. /* Now load as much as possible of the memory image into registers. */
  685. data = memory_image;
  686. while (arg_reg <= ARC_LAST_ARG_REGNUM)
  687. {
  688. arc_debug_printf ("passing 0x%02x%02x%02x%02x in register R%d",
  689. data[0], data[1], data[2], data[3], arg_reg);
  690. /* Note we don't use write_unsigned here, since that would convert
  691. the byte order, but we are already in the correct byte order. */
  692. regcache->cooked_write (arg_reg, data);
  693. data += ARC_REGISTER_SIZE;
  694. total_space -= ARC_REGISTER_SIZE;
  695. /* All the data is now in registers. */
  696. if (total_space == 0)
  697. break;
  698. arg_reg++;
  699. }
  700. /* If there is any data left, push it onto the stack (in a single write
  701. operation). */
  702. if (total_space > 0)
  703. {
  704. arc_debug_printf ("passing %d bytes on stack\n", total_space);
  705. sp -= total_space;
  706. write_memory (sp, data, (int) total_space);
  707. }
  708. xfree (memory_image);
  709. }
  710. /* Finally, update the SP register. */
  711. regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);
  712. return sp;
  713. }
  714. /* Implement the "push_dummy_code" gdbarch method.
  715. We don't actually push any code. We just identify where a breakpoint can
  716. be inserted to which we are can return and the resume address where we
  717. should be called.
  718. ARC does not necessarily have an executable stack, so we can't put the
  719. return breakpoint there. Instead we put it at the entry point of the
  720. function. This means the SP is unchanged.
  721. SP is a current stack pointer FUNADDR is an address of the function to be
  722. called. ARGS is arguments to pass. NARGS is a number of args to pass.
  723. VALUE_TYPE is a type of value returned. REAL_PC is a resume address when
  724. the function is called. BP_ADDR is an address where breakpoint should be
  725. set. Returns the updated stack pointer. */
  726. static CORE_ADDR
  727. arc_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
  728. struct value **args, int nargs, struct type *value_type,
  729. CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
  730. struct regcache *regcache)
  731. {
  732. *real_pc = funaddr;
  733. *bp_addr = entry_point_address ();
  734. return sp;
  735. }
  736. /* Implement the "cannot_fetch_register" gdbarch method. */
  737. static int
  738. arc_cannot_fetch_register (struct gdbarch *gdbarch, int regnum)
  739. {
  740. /* Assume that register is readable if it is unknown. LIMM and RESERVED are
  741. not real registers, but specific register numbers. They are available as
  742. regnums to align architectural register numbers with GDB internal regnums,
  743. but they shouldn't appear in target descriptions generated by
  744. GDB-servers. */
  745. switch (regnum)
  746. {
  747. case ARC_RESERVED_REGNUM:
  748. case ARC_LIMM_REGNUM:
  749. return true;
  750. default:
  751. return false;
  752. }
  753. }
  754. /* Implement the "cannot_store_register" gdbarch method. */
  755. static int
  756. arc_cannot_store_register (struct gdbarch *gdbarch, int regnum)
  757. {
  758. /* Assume that register is writable if it is unknown. See comment in
  759. arc_cannot_fetch_register about LIMM and RESERVED. */
  760. switch (regnum)
  761. {
  762. case ARC_RESERVED_REGNUM:
  763. case ARC_LIMM_REGNUM:
  764. case ARC_PCL_REGNUM:
  765. return true;
  766. default:
  767. return false;
  768. }
  769. }
  770. /* Get the return value of a function from the registers/memory used to
  771. return it, according to the convention used by the ABI - 4-bytes values are
  772. in the R0, while 8-byte values are in the R0-R1.
  773. TODO: This implementation ignores the case of "complex double", where
  774. according to ABI, value is returned in the R0-R3 registers.
  775. TYPE is a returned value's type. VALBUF is a buffer for the returned
  776. value. */
  777. static void
  778. arc_extract_return_value (struct gdbarch *gdbarch, struct type *type,
  779. struct regcache *regcache, gdb_byte *valbuf)
  780. {
  781. unsigned int len = TYPE_LENGTH (type);
  782. arc_debug_printf ("called");
  783. if (len <= ARC_REGISTER_SIZE)
  784. {
  785. ULONGEST val;
  786. /* Get the return value from one register. */
  787. regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &val);
  788. store_unsigned_integer (valbuf, (int) len,
  789. gdbarch_byte_order (gdbarch), val);
  790. arc_debug_printf ("returning 0x%s", phex (val, ARC_REGISTER_SIZE));
  791. }
  792. else if (len <= ARC_REGISTER_SIZE * 2)
  793. {
  794. ULONGEST low, high;
  795. /* Get the return value from two registers. */
  796. regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &low);
  797. regcache_cooked_read_unsigned (regcache, ARC_R1_REGNUM, &high);
  798. store_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
  799. gdbarch_byte_order (gdbarch), low);
  800. store_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
  801. (int) len - ARC_REGISTER_SIZE,
  802. gdbarch_byte_order (gdbarch), high);
  803. arc_debug_printf ("returning 0x%s%s",
  804. phex (high, ARC_REGISTER_SIZE),
  805. phex (low, ARC_REGISTER_SIZE));
  806. }
  807. else
  808. error (_("arc: extract_return_value: type length %u too large"), len);
  809. }
  810. /* Store the return value of a function into the registers/memory used to
  811. return it, according to the convention used by the ABI.
  812. TODO: This implementation ignores the case of "complex double", where
  813. according to ABI, value is returned in the R0-R3 registers.
  814. TYPE is a returned value's type. VALBUF is a buffer with the value to
  815. return. */
  816. static void
  817. arc_store_return_value (struct gdbarch *gdbarch, struct type *type,
  818. struct regcache *regcache, const gdb_byte *valbuf)
  819. {
  820. unsigned int len = TYPE_LENGTH (type);
  821. arc_debug_printf ("called");
  822. if (len <= ARC_REGISTER_SIZE)
  823. {
  824. ULONGEST val;
  825. /* Put the return value into one register. */
  826. val = extract_unsigned_integer (valbuf, (int) len,
  827. gdbarch_byte_order (gdbarch));
  828. regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, val);
  829. arc_debug_printf ("storing 0x%s", phex (val, ARC_REGISTER_SIZE));
  830. }
  831. else if (len <= ARC_REGISTER_SIZE * 2)
  832. {
  833. ULONGEST low, high;
  834. /* Put the return value into two registers. */
  835. low = extract_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
  836. gdbarch_byte_order (gdbarch));
  837. high = extract_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
  838. (int) len - ARC_REGISTER_SIZE,
  839. gdbarch_byte_order (gdbarch));
  840. regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, low);
  841. regcache_cooked_write_unsigned (regcache, ARC_R1_REGNUM, high);
  842. arc_debug_printf ("storing 0x%s%s",
  843. phex (high, ARC_REGISTER_SIZE),
  844. phex (low, ARC_REGISTER_SIZE));
  845. }
  846. else
  847. error (_("arc_store_return_value: type length too large."));
  848. }
  849. /* Implement the "get_longjmp_target" gdbarch method. */
  850. static int
  851. arc_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  852. {
  853. arc_debug_printf ("called");
  854. struct gdbarch *gdbarch = get_frame_arch (frame);
  855. arc_gdbarch_tdep *tdep = (arc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  856. int pc_offset = tdep->jb_pc * ARC_REGISTER_SIZE;
  857. gdb_byte buf[ARC_REGISTER_SIZE];
  858. CORE_ADDR jb_addr = get_frame_register_unsigned (frame, ARC_FIRST_ARG_REGNUM);
  859. if (target_read_memory (jb_addr + pc_offset, buf, ARC_REGISTER_SIZE))
  860. return 0; /* Failed to read from memory. */
  861. *pc = extract_unsigned_integer (buf, ARC_REGISTER_SIZE,
  862. gdbarch_byte_order (gdbarch));
  863. return 1;
  864. }
  865. /* Implement the "return_value" gdbarch method. */
  866. static enum return_value_convention
  867. arc_return_value (struct gdbarch *gdbarch, struct value *function,
  868. struct type *valtype, struct regcache *regcache,
  869. gdb_byte *readbuf, const gdb_byte *writebuf)
  870. {
  871. /* If the return type is a struct, or a union, or would occupy more than two
  872. registers, the ABI uses the "struct return convention": the calling
  873. function passes a hidden first parameter to the callee (in R0). That
  874. parameter is the address at which the value being returned should be
  875. stored. Otherwise, the result is returned in registers. */
  876. int is_struct_return = (valtype->code () == TYPE_CODE_STRUCT
  877. || valtype->code () == TYPE_CODE_UNION
  878. || TYPE_LENGTH (valtype) > 2 * ARC_REGISTER_SIZE);
  879. arc_debug_printf ("readbuf = %s, writebuf = %s",
  880. host_address_to_string (readbuf),
  881. host_address_to_string (writebuf));
  882. if (writebuf != NULL)
  883. {
  884. /* Case 1. GDB should not ask us to set a struct return value: it
  885. should know the struct return location and write the value there
  886. itself. */
  887. gdb_assert (!is_struct_return);
  888. arc_store_return_value (gdbarch, valtype, regcache, writebuf);
  889. }
  890. else if (readbuf != NULL)
  891. {
  892. /* Case 2. GDB should not ask us to get a struct return value: it
  893. should know the struct return location and read the value from there
  894. itself. */
  895. gdb_assert (!is_struct_return);
  896. arc_extract_return_value (gdbarch, valtype, regcache, readbuf);
  897. }
  898. return (is_struct_return
  899. ? RETURN_VALUE_STRUCT_CONVENTION
  900. : RETURN_VALUE_REGISTER_CONVENTION);
  901. }
  902. /* Return the base address of the frame. For ARC, the base address is the
  903. frame pointer. */
  904. static CORE_ADDR
  905. arc_frame_base_address (struct frame_info *this_frame, void **prologue_cache)
  906. {
  907. return (CORE_ADDR) get_frame_register_unsigned (this_frame, ARC_FP_REGNUM);
  908. }
  909. /* Helper function that returns valid pv_t for an instruction operand:
  910. either a register or a constant. */
  911. static pv_t
  912. arc_pv_get_operand (pv_t *regs, const struct arc_instruction &insn, int operand)
  913. {
  914. if (insn.operands[operand].kind == ARC_OPERAND_KIND_REG)
  915. return regs[insn.operands[operand].value];
  916. else
  917. return pv_constant (arc_insn_get_operand_value (insn, operand));
  918. }
  919. /* Determine whether the given disassembled instruction may be part of a
  920. function prologue. If it is, the information in the frame unwind cache will
  921. be updated. */
  922. static bool
  923. arc_is_in_prologue (struct gdbarch *gdbarch, const struct arc_instruction &insn,
  924. pv_t *regs, struct pv_area *stack)
  925. {
  926. /* It might be that currently analyzed address doesn't contain an
  927. instruction, hence INSN is not valid. It likely means that address points
  928. to a data, non-initialized memory, or middle of a 32-bit instruction. In
  929. practice this may happen if GDB connects to a remote target that has
  930. non-zeroed memory. GDB would read PC value and would try to analyze
  931. prologue, but there is no guarantee that memory contents at the address
  932. specified in PC is address is a valid instruction. There is not much that
  933. that can be done about that. */
  934. if (!insn.valid)
  935. return false;
  936. /* Branch/jump or a predicated instruction. */
  937. if (insn.is_control_flow || insn.condition_code != ARC_CC_AL)
  938. return false;
  939. /* Store of some register. May or may not update base address register. */
  940. if (insn.insn_class == STORE || insn.insn_class == PUSH)
  941. {
  942. /* There is definitely at least one operand - register/value being
  943. stored. */
  944. gdb_assert (insn.operands_count > 0);
  945. /* Store at some constant address. */
  946. if (insn.operands_count > 1
  947. && insn.operands[1].kind != ARC_OPERAND_KIND_REG)
  948. return false;
  949. /* Writeback modes:
  950. Mode Address used Writeback value
  951. --------------------------------------------------
  952. No reg + offset no
  953. A/AW reg + offset reg + offset
  954. AB reg reg + offset
  955. AS reg + (offset << scaling) no
  956. "PUSH reg" is an alias to "ST.AW reg, [SP, -4]" encoding. However
  957. 16-bit PUSH_S is a distinct instruction encoding, where offset and
  958. base register are implied through opcode. */
  959. /* Register with base memory address. */
  960. int base_reg = arc_insn_get_memory_base_reg (insn);
  961. /* Address where to write. arc_insn_get_memory_offset returns scaled
  962. value for ARC_WRITEBACK_AS. */
  963. pv_t addr;
  964. if (insn.writeback_mode == ARC_WRITEBACK_AB)
  965. addr = regs[base_reg];
  966. else
  967. addr = pv_add_constant (regs[base_reg],
  968. arc_insn_get_memory_offset (insn));
  969. if (stack->store_would_trash (addr))
  970. return false;
  971. if (insn.data_size_mode != ARC_SCALING_D)
  972. {
  973. /* Find the value being stored. */
  974. pv_t store_value = arc_pv_get_operand (regs, insn, 0);
  975. /* What is the size of a the stored value? */
  976. CORE_ADDR size;
  977. if (insn.data_size_mode == ARC_SCALING_B)
  978. size = 1;
  979. else if (insn.data_size_mode == ARC_SCALING_H)
  980. size = 2;
  981. else
  982. size = ARC_REGISTER_SIZE;
  983. stack->store (addr, size, store_value);
  984. }
  985. else
  986. {
  987. if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
  988. {
  989. /* If this is a double store, than write N+1 register as well. */
  990. pv_t store_value1 = regs[insn.operands[0].value];
  991. pv_t store_value2 = regs[insn.operands[0].value + 1];
  992. stack->store (addr, ARC_REGISTER_SIZE, store_value1);
  993. stack->store (pv_add_constant (addr, ARC_REGISTER_SIZE),
  994. ARC_REGISTER_SIZE, store_value2);
  995. }
  996. else
  997. {
  998. pv_t store_value
  999. = pv_constant (arc_insn_get_operand_value (insn, 0));
  1000. stack->store (addr, ARC_REGISTER_SIZE * 2, store_value);
  1001. }
  1002. }
  1003. /* Is base register updated? */
  1004. if (insn.writeback_mode == ARC_WRITEBACK_A
  1005. || insn.writeback_mode == ARC_WRITEBACK_AB)
  1006. regs[base_reg] = pv_add_constant (regs[base_reg],
  1007. arc_insn_get_memory_offset (insn));
  1008. return true;
  1009. }
  1010. else if (insn.insn_class == MOVE)
  1011. {
  1012. gdb_assert (insn.operands_count == 2);
  1013. /* Destination argument can be "0", so nothing will happen. */
  1014. if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
  1015. {
  1016. int dst_regnum = insn.operands[0].value;
  1017. regs[dst_regnum] = arc_pv_get_operand (regs, insn, 1);
  1018. }
  1019. return true;
  1020. }
  1021. else if (insn.insn_class == SUB)
  1022. {
  1023. gdb_assert (insn.operands_count == 3);
  1024. /* SUB 0,b,c. */
  1025. if (insn.operands[0].kind != ARC_OPERAND_KIND_REG)
  1026. return true;
  1027. int dst_regnum = insn.operands[0].value;
  1028. regs[dst_regnum] = pv_subtract (arc_pv_get_operand (regs, insn, 1),
  1029. arc_pv_get_operand (regs, insn, 2));
  1030. return true;
  1031. }
  1032. else if (insn.insn_class == ENTER)
  1033. {
  1034. /* ENTER_S is a prologue-in-instruction - it saves all callee-saved
  1035. registers according to given arguments thus greatly reducing code
  1036. size. Which registers will be actually saved depends on arguments.
  1037. ENTER_S {R13-...,FP,BLINK} stores registers in following order:
  1038. new SP ->
  1039. BLINK
  1040. R13
  1041. R14
  1042. R15
  1043. ...
  1044. FP
  1045. old SP ->
  1046. There are up to three arguments for this opcode, as presented by ARC
  1047. disassembler:
  1048. 1) amount of general-purpose registers to be saved - this argument is
  1049. always present even when it is 0;
  1050. 2) FP register number (27) if FP has to be stored, otherwise argument
  1051. is not present;
  1052. 3) BLINK register number (31) if BLINK has to be stored, otherwise
  1053. argument is not present. If both FP and BLINK are stored, then FP
  1054. is present before BLINK in argument list. */
  1055. gdb_assert (insn.operands_count > 0);
  1056. int regs_saved = arc_insn_get_operand_value (insn, 0);
  1057. bool is_fp_saved;
  1058. if (insn.operands_count > 1)
  1059. is_fp_saved = (insn.operands[1].value == ARC_FP_REGNUM);
  1060. else
  1061. is_fp_saved = false;
  1062. bool is_blink_saved;
  1063. if (insn.operands_count > 1)
  1064. is_blink_saved = (insn.operands[insn.operands_count - 1].value
  1065. == ARC_BLINK_REGNUM);
  1066. else
  1067. is_blink_saved = false;
  1068. /* Amount of bytes to be allocated to store specified registers. */
  1069. CORE_ADDR st_size = ((regs_saved + is_fp_saved + is_blink_saved)
  1070. * ARC_REGISTER_SIZE);
  1071. pv_t new_sp = pv_add_constant (regs[ARC_SP_REGNUM], -st_size);
  1072. /* Assume that if the last register (closest to new SP) can be written,
  1073. then it is possible to write all of them. */
  1074. if (stack->store_would_trash (new_sp))
  1075. return false;
  1076. /* Current store address. */
  1077. pv_t addr = regs[ARC_SP_REGNUM];
  1078. if (is_fp_saved)
  1079. {
  1080. addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
  1081. stack->store (addr, ARC_REGISTER_SIZE, regs[ARC_FP_REGNUM]);
  1082. }
  1083. /* Registers are stored in backward order: from GP (R26) to R13. */
  1084. for (int i = ARC_R13_REGNUM + regs_saved - 1; i >= ARC_R13_REGNUM; i--)
  1085. {
  1086. addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
  1087. stack->store (addr, ARC_REGISTER_SIZE, regs[i]);
  1088. }
  1089. if (is_blink_saved)
  1090. {
  1091. addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
  1092. stack->store (addr, ARC_REGISTER_SIZE,
  1093. regs[ARC_BLINK_REGNUM]);
  1094. }
  1095. gdb_assert (pv_is_identical (addr, new_sp));
  1096. regs[ARC_SP_REGNUM] = new_sp;
  1097. if (is_fp_saved)
  1098. regs[ARC_FP_REGNUM] = regs[ARC_SP_REGNUM];
  1099. return true;
  1100. }
  1101. /* Some other architectures, like nds32 or arm, try to continue as far as
  1102. possible when building a prologue cache (as opposed to when skipping
  1103. prologue), so that cache will be as full as possible. However current
  1104. code for ARC doesn't recognize some instructions that may modify SP, like
  1105. ADD, AND, OR, etc, hence there is no way to guarantee that SP wasn't
  1106. clobbered by the skipped instruction. Potential existence of extension
  1107. instruction, which may do anything they want makes this even more complex,
  1108. so it is just better to halt on a first unrecognized instruction. */
  1109. return false;
  1110. }
  1111. /* See arc-tdep.h. */
  1112. struct disassemble_info
  1113. arc_disassemble_info (struct gdbarch *gdbarch)
  1114. {
  1115. struct disassemble_info di;
  1116. init_disassemble_info_for_no_printing (&di);
  1117. di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  1118. di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1119. di.endian = gdbarch_byte_order (gdbarch);
  1120. di.read_memory_func = [](bfd_vma memaddr, gdb_byte *myaddr,
  1121. unsigned int len, struct disassemble_info *info)
  1122. {
  1123. return target_read_code (memaddr, myaddr, len);
  1124. };
  1125. return di;
  1126. }
  1127. /* Analyze the prologue and update the corresponding frame cache for the frame
  1128. unwinder for unwinding frames that doesn't have debug info. In such
  1129. situation GDB attempts to parse instructions in the prologue to understand
  1130. where each register is saved.
  1131. If CACHE is not NULL, then it will be filled with information about saved
  1132. registers.
  1133. There are several variations of prologue which GDB may encounter. "Full"
  1134. prologue looks like this:
  1135. sub sp,sp,<imm> ; Space for variadic arguments.
  1136. push blink ; Store return address.
  1137. push r13 ; Store callee saved registers (up to R26/GP).
  1138. push r14
  1139. push fp ; Store frame pointer.
  1140. mov fp,sp ; Update frame pointer.
  1141. sub sp,sp,<imm> ; Create space for local vars on the stack.
  1142. Depending on compiler options lots of things may change:
  1143. 1) BLINK is not saved in leaf functions.
  1144. 2) Frame pointer is not saved and updated if -fomit-frame-pointer is used.
  1145. 3) 16-bit versions of those instructions may be used.
  1146. 4) Instead of a sequence of several push'es, compiler may instead prefer to
  1147. do one subtract on stack pointer and then store registers using normal
  1148. store, that doesn't update SP. Like this:
  1149. sub sp,sp,8 ; Create space for callee-saved registers.
  1150. st r13,[sp,4] ; Store callee saved registers (up to R26/GP).
  1151. st r14,[sp,0]
  1152. 5) ENTER_S instruction can encode most of prologue sequence in one
  1153. instruction (except for those subtracts for variadic arguments and local
  1154. variables).
  1155. 6) GCC may use "millicode" functions from libgcc to store callee-saved
  1156. registers with minimal code-size requirements. This function currently
  1157. doesn't support this.
  1158. ENTRYPOINT is a function entry point where prologue starts.
  1159. LIMIT_PC is a maximum possible end address of prologue (meaning address
  1160. of first instruction after the prologue). It might also point to the middle
  1161. of prologue if execution has been stopped by the breakpoint at this address
  1162. - in this case debugger should analyze prologue only up to this address,
  1163. because further instructions haven't been executed yet.
  1164. Returns address of the first instruction after the prologue. */
  1165. static CORE_ADDR
  1166. arc_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR entrypoint,
  1167. const CORE_ADDR limit_pc, struct arc_frame_cache *cache)
  1168. {
  1169. arc_debug_printf ("entrypoint=%s, limit_pc=%s",
  1170. paddress (gdbarch, entrypoint),
  1171. paddress (gdbarch, limit_pc));
  1172. /* Prologue values. Only core registers can be stored. */
  1173. pv_t regs[ARC_LAST_CORE_REGNUM + 1];
  1174. for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
  1175. regs[i] = pv_register (i, 0);
  1176. pv_area stack (ARC_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  1177. CORE_ADDR current_prologue_end = entrypoint;
  1178. /* Look at each instruction in the prologue. */
  1179. while (current_prologue_end < limit_pc)
  1180. {
  1181. struct arc_instruction insn;
  1182. struct disassemble_info di = arc_disassemble_info (gdbarch);
  1183. arc_insn_decode (current_prologue_end, &di, arc_delayed_print_insn,
  1184. &insn);
  1185. if (arc_debug)
  1186. arc_insn_dump (insn);
  1187. /* If this instruction is in the prologue, fields in the cache will be
  1188. updated, and the saved registers mask may be updated. */
  1189. if (!arc_is_in_prologue (gdbarch, insn, regs, &stack))
  1190. {
  1191. /* Found an instruction that is not in the prologue. */
  1192. arc_debug_printf ("End of prologue reached at address %s",
  1193. paddress (gdbarch, insn.address));
  1194. break;
  1195. }
  1196. current_prologue_end = arc_insn_get_linear_next_pc (insn);
  1197. }
  1198. if (cache != NULL)
  1199. {
  1200. /* Figure out if it is a frame pointer or just a stack pointer. */
  1201. if (pv_is_register (regs[ARC_FP_REGNUM], ARC_SP_REGNUM))
  1202. {
  1203. cache->frame_base_reg = ARC_FP_REGNUM;
  1204. cache->frame_base_offset = -regs[ARC_FP_REGNUM].k;
  1205. }
  1206. else
  1207. {
  1208. cache->frame_base_reg = ARC_SP_REGNUM;
  1209. cache->frame_base_offset = -regs[ARC_SP_REGNUM].k;
  1210. }
  1211. /* Assign offset from old SP to all saved registers. */
  1212. for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
  1213. {
  1214. CORE_ADDR offset;
  1215. if (stack.find_reg (gdbarch, i, &offset))
  1216. cache->saved_regs[i].set_addr (offset);
  1217. }
  1218. }
  1219. return current_prologue_end;
  1220. }
  1221. /* Estimated maximum prologue length in bytes. This should include:
  1222. 1) Store instruction for each callee-saved register (R25 - R13 + 1)
  1223. 2) Two instructions for FP
  1224. 3) One for BLINK
  1225. 4) Three substract instructions for SP (for variadic args, for
  1226. callee saved regs and for local vars) and assuming that those SUB use
  1227. long-immediate (hence double length).
  1228. 5) Stores of arguments registers are considered part of prologue too
  1229. (R7 - R1 + 1).
  1230. This is quite an extreme case, because even with -O0 GCC will collapse first
  1231. two SUBs into one and long immediate values are quite unlikely to appear in
  1232. this case, but still better to overshoot a bit - prologue analysis will
  1233. anyway stop at the first instruction that doesn't fit prologue, so this
  1234. limit will be rarely reached. */
  1235. const static int MAX_PROLOGUE_LENGTH
  1236. = 4 * (ARC_R25_REGNUM - ARC_R13_REGNUM + 1 + 2 + 1 + 6
  1237. + ARC_LAST_ARG_REGNUM - ARC_FIRST_ARG_REGNUM + 1);
  1238. /* Implement the "skip_prologue" gdbarch method.
  1239. Skip the prologue for the function at PC. This is done by checking from
  1240. the line information read from the DWARF, if possible; otherwise, we scan
  1241. the function prologue to find its end. */
  1242. static CORE_ADDR
  1243. arc_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1244. {
  1245. arc_debug_printf ("pc = %s", paddress (gdbarch, pc));
  1246. CORE_ADDR func_addr;
  1247. const char *func_name;
  1248. /* See what the symbol table says. */
  1249. if (find_pc_partial_function (pc, &func_name, &func_addr, NULL))
  1250. {
  1251. /* Found a function. */
  1252. CORE_ADDR postprologue_pc
  1253. = skip_prologue_using_sal (gdbarch, func_addr);
  1254. if (postprologue_pc != 0)
  1255. return std::max (pc, postprologue_pc);
  1256. }
  1257. /* No prologue info in symbol table, have to analyze prologue. */
  1258. /* Find an upper limit on the function prologue using the debug
  1259. information. If there is no debug information about prologue end, then
  1260. skip_prologue_using_sal will return 0. */
  1261. CORE_ADDR limit_pc = skip_prologue_using_sal (gdbarch, pc);
  1262. /* If there is no debug information at all, it is required to give some
  1263. semi-arbitrary hard limit on amount of bytes to scan during prologue
  1264. analysis. */
  1265. if (limit_pc == 0)
  1266. limit_pc = pc + MAX_PROLOGUE_LENGTH;
  1267. /* Find the address of the first instruction after the prologue by scanning
  1268. through it - no other information is needed, so pass NULL as a cache. */
  1269. return arc_analyze_prologue (gdbarch, pc, limit_pc, NULL);
  1270. }
  1271. /* Implement the "print_insn" gdbarch method.
  1272. arc_get_disassembler () may return different functions depending on bfd
  1273. type, so it is not possible to pass print_insn directly to
  1274. set_gdbarch_print_insn (). Instead this wrapper function is used. It also
  1275. may be used by other functions to get disassemble_info for address. It is
  1276. important to note, that those print_insn from opcodes always print
  1277. instruction to the stream specified in the INFO. If this is not desired,
  1278. then either `print_insn` function in INFO should be set to some function
  1279. that will not print, or `stream` should be different from standard
  1280. gdb_stdlog. */
  1281. int
  1282. arc_delayed_print_insn (bfd_vma addr, struct disassemble_info *info)
  1283. {
  1284. /* Standard BFD "machine number" field allows libopcodes disassembler to
  1285. distinguish ARC 600, 700 and v2 cores, however v2 encompasses both ARC EM
  1286. and HS, which have some difference between. There are two ways to specify
  1287. what is the target core:
  1288. 1) via the disassemble_info->disassembler_options;
  1289. 2) otherwise libopcodes will use private (architecture-specific) ELF
  1290. header.
  1291. Using disassembler_options is preferable, because it comes directly from
  1292. GDBserver which scanned an actual ARC core identification info. However,
  1293. not all GDBservers report core architecture, so as a fallback GDB still
  1294. should support analysis of ELF header. The libopcodes disassembly code
  1295. uses the section to find the BFD and the BFD to find the ELF header,
  1296. therefore this function should set disassemble_info->section properly.
  1297. disassembler_options was already set by non-target specific code with
  1298. proper options obtained via gdbarch_disassembler_options ().
  1299. This function might be called multiple times in a sequence, reusing same
  1300. disassemble_info. */
  1301. if ((info->disassembler_options == NULL) && (info->section == NULL))
  1302. {
  1303. struct obj_section *s = find_pc_section (addr);
  1304. if (s != NULL)
  1305. info->section = s->the_bfd_section;
  1306. }
  1307. return default_print_insn (addr, info);
  1308. }
  1309. /* Baremetal breakpoint instructions.
  1310. ARC supports both big- and little-endian. However, instructions for
  1311. little-endian processors are encoded in the middle-endian: half-words are
  1312. in big-endian, while bytes inside the half-words are in little-endian; data
  1313. is represented in the "normal" little-endian. Big-endian processors treat
  1314. data and code identically.
  1315. Assuming the number 0x01020304, it will be presented this way:
  1316. Address : N N+1 N+2 N+3
  1317. little-endian : 0x04 0x03 0x02 0x01
  1318. big-endian : 0x01 0x02 0x03 0x04
  1319. ARC middle-endian : 0x02 0x01 0x04 0x03
  1320. */
  1321. static const gdb_byte arc_brk_s_be[] = { 0x7f, 0xff };
  1322. static const gdb_byte arc_brk_s_le[] = { 0xff, 0x7f };
  1323. static const gdb_byte arc_brk_be[] = { 0x25, 0x6f, 0x00, 0x3f };
  1324. static const gdb_byte arc_brk_le[] = { 0x6f, 0x25, 0x3f, 0x00 };
  1325. /* For ARC ELF, breakpoint uses the 16-bit BRK_S instruction, which is 0x7fff
  1326. (little endian) or 0xff7f (big endian). We used to insert BRK_S even
  1327. instead of 32-bit instructions, which works mostly ok, unless breakpoint is
  1328. inserted into delay slot instruction. In this case if branch is taken
  1329. BLINK value will be set to address of instruction after delay slot, however
  1330. if we replaced 32-bit instruction in delay slot with 16-bit long BRK_S,
  1331. then BLINK value will have an invalid value - it will point to the address
  1332. after the BRK_S (which was there at the moment of branch execution) while
  1333. it should point to the address after the 32-bit long instruction. To avoid
  1334. such issues this function disassembles instruction at target location and
  1335. evaluates it value.
  1336. ARC 600 supports only 16-bit BRK_S.
  1337. NB: Baremetal GDB uses BRK[_S], while user-space GDB uses TRAP_S. BRK[_S]
  1338. is much better because it doesn't commit unlike TRAP_S, so it can be set in
  1339. delay slots; however it cannot be used in user-mode, hence usage of TRAP_S
  1340. in GDB for user-space. */
  1341. /* Implement the "breakpoint_kind_from_pc" gdbarch method. */
  1342. static int
  1343. arc_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  1344. {
  1345. size_t length_with_limm = gdb_insn_length (gdbarch, *pcptr);
  1346. /* Replace 16-bit instruction with BRK_S, replace 32-bit instructions with
  1347. BRK. LIMM is part of instruction length, so it can be either 4 or 8
  1348. bytes for 32-bit instructions. */
  1349. if ((length_with_limm == 4 || length_with_limm == 8)
  1350. && !arc_mach_is_arc600 (gdbarch))
  1351. return sizeof (arc_brk_le);
  1352. else
  1353. return sizeof (arc_brk_s_le);
  1354. }
  1355. /* Implement the "sw_breakpoint_from_kind" gdbarch method. */
  1356. static const gdb_byte *
  1357. arc_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  1358. {
  1359. gdb_assert (kind == 2 || kind == 4);
  1360. *size = kind;
  1361. if (kind == sizeof (arc_brk_le))
  1362. {
  1363. return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1364. ? arc_brk_be
  1365. : arc_brk_le);
  1366. }
  1367. else
  1368. {
  1369. return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1370. ? arc_brk_s_be
  1371. : arc_brk_s_le);
  1372. }
  1373. }
  1374. /* Implement the "frame_align" gdbarch method. */
  1375. static CORE_ADDR
  1376. arc_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  1377. {
  1378. return align_down (sp, 4);
  1379. }
  1380. /* Dump the frame info. Used for internal debugging only. */
  1381. static void
  1382. arc_print_frame_cache (struct gdbarch *gdbarch, const char *message,
  1383. struct arc_frame_cache *cache, int addresses_known)
  1384. {
  1385. arc_debug_printf ("frame_info %s", message);
  1386. arc_debug_printf ("prev_sp = %s", paddress (gdbarch, cache->prev_sp));
  1387. arc_debug_printf ("frame_base_reg = %i", cache->frame_base_reg);
  1388. arc_debug_printf ("frame_base_offset = %s",
  1389. plongest (cache->frame_base_offset));
  1390. for (int i = 0; i <= ARC_BLINK_REGNUM; i++)
  1391. {
  1392. if (cache->saved_regs[i].is_addr ())
  1393. arc_debug_printf ("saved register %s at %s %s",
  1394. gdbarch_register_name (gdbarch, i),
  1395. (addresses_known) ? "address" : "offset",
  1396. paddress (gdbarch, cache->saved_regs[i].addr ()));
  1397. }
  1398. }
  1399. /* Frame unwinder for normal frames. */
  1400. static struct arc_frame_cache *
  1401. arc_make_frame_cache (struct frame_info *this_frame)
  1402. {
  1403. arc_debug_printf ("called");
  1404. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1405. CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
  1406. CORE_ADDR entrypoint, prologue_end;
  1407. if (find_pc_partial_function (block_addr, NULL, &entrypoint, &prologue_end))
  1408. {
  1409. struct symtab_and_line sal = find_pc_line (entrypoint, 0);
  1410. CORE_ADDR prev_pc = get_frame_pc (this_frame);
  1411. if (sal.line == 0)
  1412. /* No line info so use current PC. */
  1413. prologue_end = prev_pc;
  1414. else if (sal.end < prologue_end)
  1415. /* The next line begins after the function end. */
  1416. prologue_end = sal.end;
  1417. prologue_end = std::min (prologue_end, prev_pc);
  1418. }
  1419. else
  1420. {
  1421. /* If find_pc_partial_function returned nothing then there is no symbol
  1422. information at all for this PC. Currently it is assumed in this case
  1423. that current PC is entrypoint to function and try to construct the
  1424. frame from that. This is, probably, suboptimal, for example ARM
  1425. assumes in this case that program is inside the normal frame (with
  1426. frame pointer). ARC, perhaps, should try to do the same. */
  1427. entrypoint = get_frame_register_unsigned (this_frame,
  1428. gdbarch_pc_regnum (gdbarch));
  1429. prologue_end = entrypoint + MAX_PROLOGUE_LENGTH;
  1430. }
  1431. /* Allocate new frame cache instance and space for saved register info.
  1432. FRAME_OBSTACK_ZALLOC will initialize fields to zeroes. */
  1433. struct arc_frame_cache *cache
  1434. = FRAME_OBSTACK_ZALLOC (struct arc_frame_cache);
  1435. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  1436. arc_analyze_prologue (gdbarch, entrypoint, prologue_end, cache);
  1437. if (arc_debug)
  1438. arc_print_frame_cache (gdbarch, "after prologue", cache, false);
  1439. CORE_ADDR unwound_fb = get_frame_register_unsigned (this_frame,
  1440. cache->frame_base_reg);
  1441. if (unwound_fb == 0)
  1442. return cache;
  1443. cache->prev_sp = unwound_fb + cache->frame_base_offset;
  1444. for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
  1445. {
  1446. if (cache->saved_regs[i].is_addr ())
  1447. cache->saved_regs[i].set_addr (cache->saved_regs[i].addr ()
  1448. + cache->prev_sp);
  1449. }
  1450. if (arc_debug)
  1451. arc_print_frame_cache (gdbarch, "after previous SP found", cache, true);
  1452. return cache;
  1453. }
  1454. /* Implement the "this_id" frame_unwind method. */
  1455. static void
  1456. arc_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1457. struct frame_id *this_id)
  1458. {
  1459. arc_debug_printf ("called");
  1460. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1461. if (*this_cache == NULL)
  1462. *this_cache = arc_make_frame_cache (this_frame);
  1463. struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
  1464. CORE_ADDR stack_addr = cache->prev_sp;
  1465. /* There are 4 possible situation which decide how frame_id->code_addr is
  1466. evaluated:
  1467. 1) Function is compiled with option -g. Then frame_id will be created
  1468. in dwarf_* function and not in this function. NB: even if target
  1469. binary is compiled with -g, some std functions like __start and _init
  1470. are not, so they still will follow one of the following choices.
  1471. 2) Function is compiled without -g and binary hasn't been stripped in
  1472. any way. In this case GDB still has enough information to evaluate
  1473. frame code_addr properly. This case is covered by call to
  1474. get_frame_func ().
  1475. 3) Binary has been striped with option -g (strip debug symbols). In
  1476. this case there is still enough symbols for get_frame_func () to work
  1477. properly, so this case is also covered by it.
  1478. 4) Binary has been striped with option -s (strip all symbols). In this
  1479. case GDB cannot get function start address properly, so we return current
  1480. PC value instead.
  1481. */
  1482. CORE_ADDR code_addr = get_frame_func (this_frame);
  1483. if (code_addr == 0)
  1484. code_addr = get_frame_register_unsigned (this_frame,
  1485. gdbarch_pc_regnum (gdbarch));
  1486. *this_id = frame_id_build (stack_addr, code_addr);
  1487. }
  1488. /* Implement the "prev_register" frame_unwind method. */
  1489. static struct value *
  1490. arc_frame_prev_register (struct frame_info *this_frame,
  1491. void **this_cache, int regnum)
  1492. {
  1493. if (*this_cache == NULL)
  1494. *this_cache = arc_make_frame_cache (this_frame);
  1495. struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
  1496. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1497. /* If we are asked to unwind the PC, then we need to return BLINK instead:
  1498. the saved value of PC points into this frame's function's prologue, not
  1499. the next frame's function's resume location. */
  1500. if (regnum == gdbarch_pc_regnum (gdbarch))
  1501. regnum = ARC_BLINK_REGNUM;
  1502. /* SP is a special case - we should return prev_sp, because
  1503. trad_frame_get_prev_register will return _current_ SP value.
  1504. Alternatively we could have stored cache->prev_sp in the cache->saved
  1505. regs, but here we follow the lead of AArch64, ARM and Xtensa and will
  1506. leave that logic in this function, instead of prologue analyzers. That I
  1507. think is a bit more clear as `saved_regs` should contain saved regs, not
  1508. computable.
  1509. Because value has been computed, "got_constant" should be used, so that
  1510. returned value will be a "not_lval" - immutable. */
  1511. if (regnum == gdbarch_sp_regnum (gdbarch))
  1512. return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
  1513. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  1514. }
  1515. /* Implement the "init_reg" dwarf2_frame method. */
  1516. static void
  1517. arc_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1518. struct dwarf2_frame_state_reg *reg,
  1519. struct frame_info *info)
  1520. {
  1521. if (regnum == gdbarch_pc_regnum (gdbarch))
  1522. /* The return address column. */
  1523. reg->how = DWARF2_FRAME_REG_RA;
  1524. else if (regnum == gdbarch_sp_regnum (gdbarch))
  1525. /* The call frame address. */
  1526. reg->how = DWARF2_FRAME_REG_CFA;
  1527. }
  1528. /* Signal trampoline frame unwinder. Allows frame unwinding to happen
  1529. from within signal handlers. */
  1530. static struct arc_frame_cache *
  1531. arc_make_sigtramp_frame_cache (struct frame_info *this_frame)
  1532. {
  1533. arc_debug_printf ("called");
  1534. gdbarch *arch = get_frame_arch (this_frame);
  1535. arc_gdbarch_tdep *tdep = (arc_gdbarch_tdep *) gdbarch_tdep (arch);
  1536. /* Allocate new frame cache instance and space for saved register info. */
  1537. struct arc_frame_cache *cache = FRAME_OBSTACK_ZALLOC (struct arc_frame_cache);
  1538. cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  1539. /* Get the stack pointer and use it as the frame base. */
  1540. cache->prev_sp = arc_frame_base_address (this_frame, NULL);
  1541. /* If the ARC-private target-dependent info doesn't have a table of
  1542. offsets of saved register contents within an OS signal context
  1543. structure, then there is nothing to analyze. */
  1544. if (tdep->sc_reg_offset == NULL)
  1545. return cache;
  1546. /* Find the address of the sigcontext structure. */
  1547. CORE_ADDR addr = tdep->sigcontext_addr (this_frame);
  1548. /* For each register, if its contents have been saved within the
  1549. sigcontext structure, determine the address of those contents. */
  1550. gdb_assert (tdep->sc_num_regs <= (ARC_LAST_REGNUM + 1));
  1551. for (int i = 0; i < tdep->sc_num_regs; i++)
  1552. {
  1553. if (tdep->sc_reg_offset[i] != ARC_OFFSET_NO_REGISTER)
  1554. cache->saved_regs[i].set_addr (addr + tdep->sc_reg_offset[i]);
  1555. }
  1556. return cache;
  1557. }
  1558. /* Implement the "this_id" frame_unwind method for signal trampoline
  1559. frames. */
  1560. static void
  1561. arc_sigtramp_frame_this_id (struct frame_info *this_frame,
  1562. void **this_cache, struct frame_id *this_id)
  1563. {
  1564. arc_debug_printf ("called");
  1565. if (*this_cache == NULL)
  1566. *this_cache = arc_make_sigtramp_frame_cache (this_frame);
  1567. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1568. struct arc_frame_cache *cache = (struct arc_frame_cache *) *this_cache;
  1569. CORE_ADDR stack_addr = cache->prev_sp;
  1570. CORE_ADDR code_addr
  1571. = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
  1572. *this_id = frame_id_build (stack_addr, code_addr);
  1573. }
  1574. /* Get a register from a signal handler frame. */
  1575. static struct value *
  1576. arc_sigtramp_frame_prev_register (struct frame_info *this_frame,
  1577. void **this_cache, int regnum)
  1578. {
  1579. arc_debug_printf ("regnum = %d", regnum);
  1580. /* Make sure we've initialized the cache. */
  1581. if (*this_cache == NULL)
  1582. *this_cache = arc_make_sigtramp_frame_cache (this_frame);
  1583. struct arc_frame_cache *cache = (struct arc_frame_cache *) *this_cache;
  1584. return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  1585. }
  1586. /* Frame sniffer for signal handler frame. Only recognize a frame if we
  1587. have a sigcontext_addr handler in the target dependency. */
  1588. static int
  1589. arc_sigtramp_frame_sniffer (const struct frame_unwind *self,
  1590. struct frame_info *this_frame,
  1591. void **this_cache)
  1592. {
  1593. arc_debug_printf ("called");
  1594. gdbarch *arch = get_frame_arch (this_frame);
  1595. arc_gdbarch_tdep *tdep = (arc_gdbarch_tdep *) gdbarch_tdep (arch);
  1596. /* If we have a sigcontext_addr handler, then just return 1 (same as the
  1597. "default_frame_sniffer ()"). */
  1598. return (tdep->sigcontext_addr != NULL && tdep->is_sigtramp != NULL
  1599. && tdep->is_sigtramp (this_frame));
  1600. }
  1601. /* Structure defining the ARC ordinary frame unwind functions. Since we are
  1602. the fallback unwinder, we use the default frame sniffer, which always
  1603. accepts the frame. */
  1604. static const struct frame_unwind arc_frame_unwind = {
  1605. "arc prologue",
  1606. NORMAL_FRAME,
  1607. default_frame_unwind_stop_reason,
  1608. arc_frame_this_id,
  1609. arc_frame_prev_register,
  1610. NULL,
  1611. default_frame_sniffer,
  1612. NULL,
  1613. NULL
  1614. };
  1615. /* Structure defining the ARC signal frame unwind functions. Custom
  1616. sniffer is used, because this frame must be accepted only in the right
  1617. context. */
  1618. static const struct frame_unwind arc_sigtramp_frame_unwind = {
  1619. "arc sigtramp",
  1620. SIGTRAMP_FRAME,
  1621. default_frame_unwind_stop_reason,
  1622. arc_sigtramp_frame_this_id,
  1623. arc_sigtramp_frame_prev_register,
  1624. NULL,
  1625. arc_sigtramp_frame_sniffer,
  1626. NULL,
  1627. NULL
  1628. };
  1629. static const struct frame_base arc_normal_base = {
  1630. &arc_frame_unwind,
  1631. arc_frame_base_address,
  1632. arc_frame_base_address,
  1633. arc_frame_base_address
  1634. };
  1635. static enum arc_isa
  1636. mach_type_to_arc_isa (const unsigned long mach)
  1637. {
  1638. switch (mach)
  1639. {
  1640. case bfd_mach_arc_arc600:
  1641. case bfd_mach_arc_arc601:
  1642. case bfd_mach_arc_arc700:
  1643. return ARC_ISA_ARCV1;
  1644. case bfd_mach_arc_arcv2:
  1645. return ARC_ISA_ARCV2;
  1646. default:
  1647. internal_error (__FILE__, __LINE__,
  1648. _("unknown machine id %lu"), mach);
  1649. }
  1650. }
  1651. /* See arc-tdep.h. */
  1652. arc_arch_features
  1653. arc_arch_features_create (const bfd *abfd, const unsigned long mach)
  1654. {
  1655. /* Use 4 as a fallback value. */
  1656. int reg_size = 4;
  1657. /* Try to guess the features parameters by looking at the binary to be
  1658. executed. If the user is providing a binary that does not match the
  1659. target, then tough luck. This is the last effort to makes sense of
  1660. what's going on. */
  1661. if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1662. {
  1663. unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
  1664. if (eclass == ELFCLASS32)
  1665. reg_size = 4;
  1666. else if (eclass == ELFCLASS64)
  1667. reg_size = 8;
  1668. else
  1669. internal_error (__FILE__, __LINE__,
  1670. _("unknown ELF header class %d"), eclass);
  1671. }
  1672. /* MACH from a bfd_arch_info struct is used here. It should be a safe
  1673. bet, as it looks like the struct is always initialized even when we
  1674. don't pass any elf file to GDB at all (it uses default arch in that
  1675. case). */
  1676. arc_isa isa = mach_type_to_arc_isa (mach);
  1677. return arc_arch_features (reg_size, isa);
  1678. }
  1679. /* Look for obsolete core feature names in TDESC. */
  1680. static const struct tdesc_feature *
  1681. find_obsolete_core_names (const struct target_desc *tdesc)
  1682. {
  1683. const struct tdesc_feature *feat = nullptr;
  1684. feat = tdesc_find_feature (tdesc, ARC_CORE_V1_OBSOLETE_FEATURE_NAME);
  1685. if (feat == nullptr)
  1686. feat = tdesc_find_feature (tdesc, ARC_CORE_V2_OBSOLETE_FEATURE_NAME);
  1687. if (feat == nullptr)
  1688. feat = tdesc_find_feature
  1689. (tdesc, ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME);
  1690. return feat;
  1691. }
  1692. /* Look for obsolete aux feature names in TDESC. */
  1693. static const struct tdesc_feature *
  1694. find_obsolete_aux_names (const struct target_desc *tdesc)
  1695. {
  1696. return tdesc_find_feature (tdesc, ARC_AUX_OBSOLETE_FEATURE_NAME);
  1697. }
  1698. /* Based on the MACH value, determines which core register features set
  1699. must be used. */
  1700. static arc_register_feature *
  1701. determine_core_reg_feature_set (const unsigned long mach)
  1702. {
  1703. switch (mach_type_to_arc_isa (mach))
  1704. {
  1705. case ARC_ISA_ARCV1:
  1706. return &arc_v1_core_reg_feature;
  1707. case ARC_ISA_ARCV2:
  1708. return &arc_v2_core_reg_feature;
  1709. default:
  1710. gdb_assert_not_reached
  1711. ("Unknown machine type to determine the core feature set.");
  1712. }
  1713. }
  1714. /* At the moment, there is only 1 auxiliary register features set.
  1715. This is a place holder for future extendability. */
  1716. static const arc_register_feature *
  1717. determine_aux_reg_feature_set ()
  1718. {
  1719. return &arc_common_aux_reg_feature;
  1720. }
  1721. /* Update accumulator register names (ACCH/ACCL) for r58 and r59 in the
  1722. register sets. The endianness determines the assignment:
  1723. ,------.------.
  1724. | acch | accl |
  1725. ,----|------+------|
  1726. | LE | r59 | r58 |
  1727. | BE | r58 | r59 |
  1728. `----^------^------' */
  1729. static void
  1730. arc_update_acc_reg_names (const int byte_order)
  1731. {
  1732. const char *r58_alias
  1733. = byte_order == BFD_ENDIAN_LITTLE ? "accl" : "acch";
  1734. const char *r59_alias
  1735. = byte_order == BFD_ENDIAN_LITTLE ? "acch" : "accl";
  1736. /* Subscript 1 must be OK because those registers have 2 names. */
  1737. arc_v1_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
  1738. arc_v1_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
  1739. arc_v2_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
  1740. arc_v2_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
  1741. }
  1742. /* Go through all the registers in REG_SET and check if they exist
  1743. in FEATURE. The TDESC_DATA is updated with the register number
  1744. in REG_SET if it is found in the feature. If a required register
  1745. is not found, this function returns false. */
  1746. static bool
  1747. arc_check_tdesc_feature (struct tdesc_arch_data *tdesc_data,
  1748. const struct tdesc_feature *feature,
  1749. const struct arc_register_feature *reg_set)
  1750. {
  1751. for (const auto &reg : reg_set->registers)
  1752. {
  1753. bool found = false;
  1754. for (const char *name : reg.names)
  1755. {
  1756. found
  1757. = tdesc_numbered_register (feature, tdesc_data, reg.regnum, name);
  1758. if (found)
  1759. break;
  1760. }
  1761. if (!found && reg.required_p)
  1762. {
  1763. std::ostringstream reg_names;
  1764. for (std::size_t i = 0; i < reg.names.size(); ++i)
  1765. {
  1766. if (i == 0)
  1767. reg_names << "'" << reg.names[0] << "'";
  1768. else
  1769. reg_names << " or '" << reg.names[0] << "'";
  1770. }
  1771. arc_print (_("Error: Cannot find required register(s) %s "
  1772. "in feature '%s'.\n"), reg_names.str ().c_str (),
  1773. feature->name.c_str ());
  1774. return false;
  1775. }
  1776. }
  1777. return true;
  1778. }
  1779. /* Check for the existance of "lp_start" and "lp_end" in target description.
  1780. If both are present, assume there is hardware loop support in the target.
  1781. This can be improved by looking into "lpc_size" field of "isa_config"
  1782. auxiliary register. */
  1783. static bool
  1784. arc_check_for_hw_loops (const struct target_desc *tdesc,
  1785. struct tdesc_arch_data *data)
  1786. {
  1787. const auto feature_aux = tdesc_find_feature (tdesc, ARC_AUX_FEATURE_NAME);
  1788. const auto aux_regset = determine_aux_reg_feature_set ();
  1789. if (feature_aux == nullptr)
  1790. return false;
  1791. bool hw_loop_p = false;
  1792. const auto lp_start_name =
  1793. aux_regset->registers[ARC_LP_START_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
  1794. const auto lp_end_name =
  1795. aux_regset->registers[ARC_LP_END_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
  1796. hw_loop_p = tdesc_numbered_register (feature_aux, data,
  1797. ARC_LP_START_REGNUM, lp_start_name);
  1798. hw_loop_p &= tdesc_numbered_register (feature_aux, data,
  1799. ARC_LP_END_REGNUM, lp_end_name);
  1800. return hw_loop_p;
  1801. }
  1802. /* Initialize target description for the ARC.
  1803. Returns true if input TDESC was valid and in this case it will assign TDESC
  1804. and TDESC_DATA output parameters. */
  1805. static bool
  1806. arc_tdesc_init (struct gdbarch_info info, const struct target_desc **tdesc,
  1807. tdesc_arch_data_up *tdesc_data)
  1808. {
  1809. const struct target_desc *tdesc_loc = info.target_desc;
  1810. arc_debug_printf ("Target description initialization.");
  1811. /* If target doesn't provide a description, use the default ones. */
  1812. if (!tdesc_has_registers (tdesc_loc))
  1813. {
  1814. arc_arch_features features
  1815. = arc_arch_features_create (info.abfd,
  1816. info.bfd_arch_info->mach);
  1817. tdesc_loc = arc_lookup_target_description (features);
  1818. }
  1819. gdb_assert (tdesc_loc != nullptr);
  1820. arc_debug_printf ("Have got a target description");
  1821. const struct tdesc_feature *feature_core
  1822. = tdesc_find_feature (tdesc_loc, ARC_CORE_FEATURE_NAME);
  1823. const struct tdesc_feature *feature_aux
  1824. = tdesc_find_feature (tdesc_loc, ARC_AUX_FEATURE_NAME);
  1825. /* Maybe there still is a chance to salvage the input. */
  1826. if (feature_core == nullptr)
  1827. feature_core = find_obsolete_core_names (tdesc_loc);
  1828. if (feature_aux == nullptr)
  1829. feature_aux = find_obsolete_aux_names (tdesc_loc);
  1830. if (feature_core == nullptr)
  1831. {
  1832. arc_print (_("Error: Cannot find required feature '%s' in supplied "
  1833. "target description.\n"), ARC_CORE_FEATURE_NAME);
  1834. return false;
  1835. }
  1836. if (feature_aux == nullptr)
  1837. {
  1838. arc_print (_("Error: Cannot find required feature '%s' in supplied "
  1839. "target description.\n"), ARC_AUX_FEATURE_NAME);
  1840. return false;
  1841. }
  1842. const arc_register_feature *arc_core_reg_feature
  1843. = determine_core_reg_feature_set (info.bfd_arch_info->mach);
  1844. const arc_register_feature *arc_aux_reg_feature
  1845. = determine_aux_reg_feature_set ();
  1846. tdesc_arch_data_up tdesc_data_loc = tdesc_data_alloc ();
  1847. arc_update_acc_reg_names (info.byte_order);
  1848. bool valid_p = arc_check_tdesc_feature (tdesc_data_loc.get (),
  1849. feature_core,
  1850. arc_core_reg_feature);
  1851. valid_p &= arc_check_tdesc_feature (tdesc_data_loc.get (),
  1852. feature_aux,
  1853. arc_aux_reg_feature);
  1854. if (!valid_p)
  1855. {
  1856. arc_debug_printf ("Target description is not valid");
  1857. return false;
  1858. }
  1859. *tdesc = tdesc_loc;
  1860. *tdesc_data = std::move (tdesc_data_loc);
  1861. return true;
  1862. }
  1863. /* Implement the type_align gdbarch function. */
  1864. static ULONGEST
  1865. arc_type_align (struct gdbarch *gdbarch, struct type *type)
  1866. {
  1867. switch (type->code ())
  1868. {
  1869. case TYPE_CODE_PTR:
  1870. case TYPE_CODE_FUNC:
  1871. case TYPE_CODE_FLAGS:
  1872. case TYPE_CODE_INT:
  1873. case TYPE_CODE_RANGE:
  1874. case TYPE_CODE_FLT:
  1875. case TYPE_CODE_ENUM:
  1876. case TYPE_CODE_REF:
  1877. case TYPE_CODE_RVALUE_REF:
  1878. case TYPE_CODE_CHAR:
  1879. case TYPE_CODE_BOOL:
  1880. case TYPE_CODE_DECFLOAT:
  1881. case TYPE_CODE_METHODPTR:
  1882. case TYPE_CODE_MEMBERPTR:
  1883. type = check_typedef (type);
  1884. return std::min<ULONGEST> (4, TYPE_LENGTH (type));
  1885. default:
  1886. return 0;
  1887. }
  1888. }
  1889. /* Implement the "init" gdbarch method. */
  1890. static struct gdbarch *
  1891. arc_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1892. {
  1893. const struct target_desc *tdesc;
  1894. tdesc_arch_data_up tdesc_data;
  1895. arc_debug_printf ("Architecture initialization.");
  1896. if (!arc_tdesc_init (info, &tdesc, &tdesc_data))
  1897. return nullptr;
  1898. /* Allocate the ARC-private target-dependent information structure, and the
  1899. GDB target-independent information structure. */
  1900. std::unique_ptr<arc_gdbarch_tdep> tdep_holder (new arc_gdbarch_tdep);
  1901. arc_gdbarch_tdep *tdep = tdep_holder.get ();
  1902. tdep->jb_pc = -1; /* No longjmp support by default. */
  1903. tdep->has_hw_loops = arc_check_for_hw_loops (tdesc, tdesc_data.get ());
  1904. struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep_holder.release ());
  1905. /* Data types. */
  1906. set_gdbarch_short_bit (gdbarch, 16);
  1907. set_gdbarch_int_bit (gdbarch, 32);
  1908. set_gdbarch_long_bit (gdbarch, 32);
  1909. set_gdbarch_long_long_bit (gdbarch, 64);
  1910. set_gdbarch_type_align (gdbarch, arc_type_align);
  1911. set_gdbarch_float_bit (gdbarch, 32);
  1912. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  1913. set_gdbarch_double_bit (gdbarch, 64);
  1914. set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  1915. set_gdbarch_ptr_bit (gdbarch, 32);
  1916. set_gdbarch_addr_bit (gdbarch, 32);
  1917. set_gdbarch_char_signed (gdbarch, 0);
  1918. set_gdbarch_write_pc (gdbarch, arc_write_pc);
  1919. set_gdbarch_virtual_frame_pointer (gdbarch, arc_virtual_frame_pointer);
  1920. /* tdesc_use_registers expects gdbarch_num_regs to return number of registers
  1921. parsed by gdbarch_init, and then it will add all of the remaining
  1922. registers and will increase number of registers. */
  1923. set_gdbarch_num_regs (gdbarch, ARC_LAST_REGNUM + 1);
  1924. set_gdbarch_num_pseudo_regs (gdbarch, 0);
  1925. set_gdbarch_sp_regnum (gdbarch, ARC_SP_REGNUM);
  1926. set_gdbarch_pc_regnum (gdbarch, ARC_PC_REGNUM);
  1927. set_gdbarch_ps_regnum (gdbarch, ARC_STATUS32_REGNUM);
  1928. set_gdbarch_fp0_regnum (gdbarch, -1); /* No FPU registers. */
  1929. set_gdbarch_push_dummy_call (gdbarch, arc_push_dummy_call);
  1930. set_gdbarch_push_dummy_code (gdbarch, arc_push_dummy_code);
  1931. set_gdbarch_cannot_fetch_register (gdbarch, arc_cannot_fetch_register);
  1932. set_gdbarch_cannot_store_register (gdbarch, arc_cannot_store_register);
  1933. set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  1934. set_gdbarch_return_value (gdbarch, arc_return_value);
  1935. set_gdbarch_skip_prologue (gdbarch, arc_skip_prologue);
  1936. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1937. set_gdbarch_breakpoint_kind_from_pc (gdbarch, arc_breakpoint_kind_from_pc);
  1938. set_gdbarch_sw_breakpoint_from_kind (gdbarch, arc_sw_breakpoint_from_kind);
  1939. /* On ARC 600 BRK_S instruction advances PC, unlike other ARC cores. */
  1940. if (!arc_mach_is_arc600 (gdbarch))
  1941. set_gdbarch_decr_pc_after_break (gdbarch, 0);
  1942. else
  1943. set_gdbarch_decr_pc_after_break (gdbarch, 2);
  1944. set_gdbarch_frame_align (gdbarch, arc_frame_align);
  1945. set_gdbarch_print_insn (gdbarch, arc_delayed_print_insn);
  1946. set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
  1947. /* "nonsteppable" watchpoint means that watchpoint triggers before
  1948. instruction is committed, therefore it is required to remove watchpoint
  1949. to step though instruction that triggers it. ARC watchpoints trigger
  1950. only after instruction is committed, thus there is no need to remove
  1951. them. In fact on ARC watchpoint for memory writes may trigger with more
  1952. significant delay, like one or two instructions, depending on type of
  1953. memory where write is performed (CCM or external) and next instruction
  1954. after the memory write. */
  1955. set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 0);
  1956. /* This doesn't include possible long-immediate value. */
  1957. set_gdbarch_max_insn_length (gdbarch, 4);
  1958. /* Frame unwinders and sniffers. */
  1959. dwarf2_frame_set_init_reg (gdbarch, arc_dwarf2_frame_init_reg);
  1960. dwarf2_append_unwinders (gdbarch);
  1961. frame_unwind_append_unwinder (gdbarch, &arc_sigtramp_frame_unwind);
  1962. frame_unwind_append_unwinder (gdbarch, &arc_frame_unwind);
  1963. frame_base_set_default (gdbarch, &arc_normal_base);
  1964. /* Setup stuff specific to a particular environment (baremetal or Linux).
  1965. It can override functions set earlier. */
  1966. gdbarch_init_osabi (info, gdbarch);
  1967. if (tdep->jb_pc >= 0)
  1968. set_gdbarch_get_longjmp_target (gdbarch, arc_get_longjmp_target);
  1969. /* Disassembler options. Enforce CPU if it was specified in XML target
  1970. description, otherwise use default method of determining CPU (ELF private
  1971. header). */
  1972. if (info.target_desc != NULL)
  1973. {
  1974. const struct bfd_arch_info *tdesc_arch
  1975. = tdesc_architecture (info.target_desc);
  1976. if (tdesc_arch != NULL)
  1977. {
  1978. xfree (arc_disassembler_options);
  1979. /* FIXME: It is not really good to change disassembler options
  1980. behind the scene, because that might override options
  1981. specified by the user. However as of now ARC doesn't support
  1982. `set disassembler-options' hence this code is the only place
  1983. where options are changed. It also changes options for all
  1984. existing gdbarches, which also can be problematic, if
  1985. arc_gdbarch_init will start reusing existing gdbarch
  1986. instances. */
  1987. /* Target description specifies a BFD architecture, which is
  1988. different from ARC cpu, as accepted by disassembler (and most
  1989. other ARC tools), because cpu values are much more fine grained -
  1990. there can be multiple cpu values per single BFD architecture. As
  1991. a result this code should translate architecture to some cpu
  1992. value. Since there is no info on exact cpu configuration, it is
  1993. best to use the most feature-rich CPU, so that disassembler will
  1994. recognize all instructions available to the specified
  1995. architecture. */
  1996. switch (tdesc_arch->mach)
  1997. {
  1998. case bfd_mach_arc_arc601:
  1999. arc_disassembler_options = xstrdup ("cpu=arc601");
  2000. break;
  2001. case bfd_mach_arc_arc600:
  2002. arc_disassembler_options = xstrdup ("cpu=arc600");
  2003. break;
  2004. case bfd_mach_arc_arc700:
  2005. arc_disassembler_options = xstrdup ("cpu=arc700");
  2006. break;
  2007. case bfd_mach_arc_arcv2:
  2008. /* Machine arcv2 has three arches: ARCv2, EM and HS; where ARCv2
  2009. is treated as EM. */
  2010. if (arc_arch_is_hs (tdesc_arch))
  2011. arc_disassembler_options = xstrdup ("cpu=hs38_linux");
  2012. else
  2013. arc_disassembler_options = xstrdup ("cpu=em4_fpuda");
  2014. break;
  2015. default:
  2016. arc_disassembler_options = NULL;
  2017. break;
  2018. }
  2019. }
  2020. }
  2021. set_gdbarch_disassembler_options (gdbarch, &arc_disassembler_options);
  2022. set_gdbarch_valid_disassembler_options (gdbarch,
  2023. disassembler_options_arc ());
  2024. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  2025. return gdbarch;
  2026. }
  2027. /* Implement the "dump_tdep" gdbarch method. */
  2028. static void
  2029. arc_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  2030. {
  2031. arc_gdbarch_tdep *tdep = (arc_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2032. gdb_printf (file, "arc_dump_tdep: jb_pc = %i\n", tdep->jb_pc);
  2033. gdb_printf (file, "arc_dump_tdep: is_sigtramp = <%s>\n",
  2034. host_address_to_string (tdep->is_sigtramp));
  2035. gdb_printf (file, "arc_dump_tdep: sigcontext_addr = <%s>\n",
  2036. host_address_to_string (tdep->sigcontext_addr));
  2037. gdb_printf (file, "arc_dump_tdep: sc_reg_offset = <%s>\n",
  2038. host_address_to_string (tdep->sc_reg_offset));
  2039. gdb_printf (file, "arc_dump_tdep: sc_num_regs = %d\n",
  2040. tdep->sc_num_regs);
  2041. }
  2042. /* This command accepts single argument - address of instruction to
  2043. disassemble. */
  2044. static void
  2045. dump_arc_instruction_command (const char *args, int from_tty)
  2046. {
  2047. struct value *val;
  2048. if (args != NULL && strlen (args) > 0)
  2049. val = evaluate_expression (parse_expression (args).get ());
  2050. else
  2051. val = access_value_history (0);
  2052. record_latest_value (val);
  2053. CORE_ADDR address = value_as_address (val);
  2054. struct arc_instruction insn;
  2055. struct disassemble_info di = arc_disassemble_info (target_gdbarch ());
  2056. arc_insn_decode (address, &di, arc_delayed_print_insn, &insn);
  2057. arc_insn_dump (insn);
  2058. }
  2059. void _initialize_arc_tdep ();
  2060. void
  2061. _initialize_arc_tdep ()
  2062. {
  2063. gdbarch_register (bfd_arch_arc, arc_gdbarch_init, arc_dump_tdep);
  2064. /* Register ARC-specific commands with gdb. */
  2065. /* Add root prefix command for "maintenance print arc" commands. */
  2066. add_show_prefix_cmd ("arc", class_maintenance,
  2067. _("ARC-specific maintenance commands for printing GDB "
  2068. "internal state."),
  2069. &maintenance_print_arc_list,
  2070. 0, &maintenanceprintlist);
  2071. add_cmd ("arc-instruction", class_maintenance,
  2072. dump_arc_instruction_command,
  2073. _("Dump arc_instruction structure for specified address."),
  2074. &maintenance_print_arc_list);
  2075. /* Debug internals for ARC GDB. */
  2076. add_setshow_boolean_cmd ("arc", class_maintenance,
  2077. &arc_debug,
  2078. _("Set ARC specific debugging."),
  2079. _("Show ARC specific debugging."),
  2080. _("When set, ARC specific debugging is enabled."),
  2081. NULL, NULL, &setdebuglist, &showdebuglist);
  2082. }