amd64-tdep.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480
  1. /* Target-dependent code for AMD64.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. Contributed by Jiri Smid, SuSE Labs.
  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. #include "defs.h"
  16. #include "opcode/i386.h"
  17. #include "dis-asm.h"
  18. #include "arch-utils.h"
  19. #include "block.h"
  20. #include "dummy-frame.h"
  21. #include "frame.h"
  22. #include "frame-base.h"
  23. #include "frame-unwind.h"
  24. #include "inferior.h"
  25. #include "infrun.h"
  26. #include "gdbcmd.h"
  27. #include "gdbcore.h"
  28. #include "objfiles.h"
  29. #include "regcache.h"
  30. #include "regset.h"
  31. #include "symfile.h"
  32. #include "disasm.h"
  33. #include "amd64-tdep.h"
  34. #include "i387-tdep.h"
  35. #include "gdbsupport/x86-xstate.h"
  36. #include <algorithm>
  37. #include "target-descriptions.h"
  38. #include "arch/amd64.h"
  39. #include "producer.h"
  40. #include "ax.h"
  41. #include "ax-gdb.h"
  42. #include "gdbsupport/byte-vector.h"
  43. #include "osabi.h"
  44. #include "x86-tdep.h"
  45. #include "amd64-ravenscar-thread.h"
  46. /* Note that the AMD64 architecture was previously known as x86-64.
  47. The latter is (forever) engraved into the canonical system name as
  48. returned by config.guess, and used as the name for the AMD64 port
  49. of GNU/Linux. The BSD's have renamed their ports to amd64; they
  50. don't like to shout. For GDB we prefer the amd64_-prefix over the
  51. x86_64_-prefix since it's so much easier to type. */
  52. /* Register information. */
  53. static const char * const amd64_register_names[] =
  54. {
  55. "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
  56. /* %r8 is indeed register number 8. */
  57. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  58. "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
  59. /* %st0 is register number 24. */
  60. "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
  61. "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
  62. /* %xmm0 is register number 40. */
  63. "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
  64. "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
  65. "mxcsr",
  66. };
  67. static const char * const amd64_ymm_names[] =
  68. {
  69. "ymm0", "ymm1", "ymm2", "ymm3",
  70. "ymm4", "ymm5", "ymm6", "ymm7",
  71. "ymm8", "ymm9", "ymm10", "ymm11",
  72. "ymm12", "ymm13", "ymm14", "ymm15"
  73. };
  74. static const char * const amd64_ymm_avx512_names[] =
  75. {
  76. "ymm16", "ymm17", "ymm18", "ymm19",
  77. "ymm20", "ymm21", "ymm22", "ymm23",
  78. "ymm24", "ymm25", "ymm26", "ymm27",
  79. "ymm28", "ymm29", "ymm30", "ymm31"
  80. };
  81. static const char * const amd64_ymmh_names[] =
  82. {
  83. "ymm0h", "ymm1h", "ymm2h", "ymm3h",
  84. "ymm4h", "ymm5h", "ymm6h", "ymm7h",
  85. "ymm8h", "ymm9h", "ymm10h", "ymm11h",
  86. "ymm12h", "ymm13h", "ymm14h", "ymm15h"
  87. };
  88. static const char * const amd64_ymmh_avx512_names[] =
  89. {
  90. "ymm16h", "ymm17h", "ymm18h", "ymm19h",
  91. "ymm20h", "ymm21h", "ymm22h", "ymm23h",
  92. "ymm24h", "ymm25h", "ymm26h", "ymm27h",
  93. "ymm28h", "ymm29h", "ymm30h", "ymm31h"
  94. };
  95. static const char * const amd64_mpx_names[] =
  96. {
  97. "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
  98. };
  99. static const char * const amd64_k_names[] =
  100. {
  101. "k0", "k1", "k2", "k3",
  102. "k4", "k5", "k6", "k7"
  103. };
  104. static const char * const amd64_zmmh_names[] =
  105. {
  106. "zmm0h", "zmm1h", "zmm2h", "zmm3h",
  107. "zmm4h", "zmm5h", "zmm6h", "zmm7h",
  108. "zmm8h", "zmm9h", "zmm10h", "zmm11h",
  109. "zmm12h", "zmm13h", "zmm14h", "zmm15h",
  110. "zmm16h", "zmm17h", "zmm18h", "zmm19h",
  111. "zmm20h", "zmm21h", "zmm22h", "zmm23h",
  112. "zmm24h", "zmm25h", "zmm26h", "zmm27h",
  113. "zmm28h", "zmm29h", "zmm30h", "zmm31h"
  114. };
  115. static const char * const amd64_zmm_names[] =
  116. {
  117. "zmm0", "zmm1", "zmm2", "zmm3",
  118. "zmm4", "zmm5", "zmm6", "zmm7",
  119. "zmm8", "zmm9", "zmm10", "zmm11",
  120. "zmm12", "zmm13", "zmm14", "zmm15",
  121. "zmm16", "zmm17", "zmm18", "zmm19",
  122. "zmm20", "zmm21", "zmm22", "zmm23",
  123. "zmm24", "zmm25", "zmm26", "zmm27",
  124. "zmm28", "zmm29", "zmm30", "zmm31"
  125. };
  126. static const char * const amd64_xmm_avx512_names[] = {
  127. "xmm16", "xmm17", "xmm18", "xmm19",
  128. "xmm20", "xmm21", "xmm22", "xmm23",
  129. "xmm24", "xmm25", "xmm26", "xmm27",
  130. "xmm28", "xmm29", "xmm30", "xmm31"
  131. };
  132. static const char * const amd64_pkeys_names[] = {
  133. "pkru"
  134. };
  135. /* DWARF Register Number Mapping as defined in the System V psABI,
  136. section 3.6. */
  137. static int amd64_dwarf_regmap[] =
  138. {
  139. /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
  140. AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
  141. AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
  142. AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
  143. /* Frame Pointer Register RBP. */
  144. AMD64_RBP_REGNUM,
  145. /* Stack Pointer Register RSP. */
  146. AMD64_RSP_REGNUM,
  147. /* Extended Integer Registers 8 - 15. */
  148. AMD64_R8_REGNUM, /* %r8 */
  149. AMD64_R9_REGNUM, /* %r9 */
  150. AMD64_R10_REGNUM, /* %r10 */
  151. AMD64_R11_REGNUM, /* %r11 */
  152. AMD64_R12_REGNUM, /* %r12 */
  153. AMD64_R13_REGNUM, /* %r13 */
  154. AMD64_R14_REGNUM, /* %r14 */
  155. AMD64_R15_REGNUM, /* %r15 */
  156. /* Return Address RA. Mapped to RIP. */
  157. AMD64_RIP_REGNUM,
  158. /* SSE Registers 0 - 7. */
  159. AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
  160. AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
  161. AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
  162. AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
  163. /* Extended SSE Registers 8 - 15. */
  164. AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
  165. AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
  166. AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
  167. AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
  168. /* Floating Point Registers 0-7. */
  169. AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
  170. AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
  171. AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
  172. AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
  173. /* MMX Registers 0 - 7.
  174. We have to handle those registers specifically, as their register
  175. number within GDB depends on the target (or they may even not be
  176. available at all). */
  177. -1, -1, -1, -1, -1, -1, -1, -1,
  178. /* Control and Status Flags Register. */
  179. AMD64_EFLAGS_REGNUM,
  180. /* Selector Registers. */
  181. AMD64_ES_REGNUM,
  182. AMD64_CS_REGNUM,
  183. AMD64_SS_REGNUM,
  184. AMD64_DS_REGNUM,
  185. AMD64_FS_REGNUM,
  186. AMD64_GS_REGNUM,
  187. -1,
  188. -1,
  189. /* Segment Base Address Registers. */
  190. -1,
  191. -1,
  192. -1,
  193. -1,
  194. /* Special Selector Registers. */
  195. -1,
  196. -1,
  197. /* Floating Point Control Registers. */
  198. AMD64_MXCSR_REGNUM,
  199. AMD64_FCTRL_REGNUM,
  200. AMD64_FSTAT_REGNUM
  201. };
  202. static const int amd64_dwarf_regmap_len =
  203. (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
  204. /* Convert DWARF register number REG to the appropriate register
  205. number used by GDB. */
  206. static int
  207. amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  208. {
  209. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  210. int ymm0_regnum = tdep->ymm0_regnum;
  211. int regnum = -1;
  212. if (reg >= 0 && reg < amd64_dwarf_regmap_len)
  213. regnum = amd64_dwarf_regmap[reg];
  214. if (ymm0_regnum >= 0
  215. && i386_xmm_regnum_p (gdbarch, regnum))
  216. regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);
  217. return regnum;
  218. }
  219. /* Map architectural register numbers to gdb register numbers. */
  220. static const int amd64_arch_regmap[16] =
  221. {
  222. AMD64_RAX_REGNUM, /* %rax */
  223. AMD64_RCX_REGNUM, /* %rcx */
  224. AMD64_RDX_REGNUM, /* %rdx */
  225. AMD64_RBX_REGNUM, /* %rbx */
  226. AMD64_RSP_REGNUM, /* %rsp */
  227. AMD64_RBP_REGNUM, /* %rbp */
  228. AMD64_RSI_REGNUM, /* %rsi */
  229. AMD64_RDI_REGNUM, /* %rdi */
  230. AMD64_R8_REGNUM, /* %r8 */
  231. AMD64_R9_REGNUM, /* %r9 */
  232. AMD64_R10_REGNUM, /* %r10 */
  233. AMD64_R11_REGNUM, /* %r11 */
  234. AMD64_R12_REGNUM, /* %r12 */
  235. AMD64_R13_REGNUM, /* %r13 */
  236. AMD64_R14_REGNUM, /* %r14 */
  237. AMD64_R15_REGNUM /* %r15 */
  238. };
  239. static const int amd64_arch_regmap_len =
  240. (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));
  241. /* Convert architectural register number REG to the appropriate register
  242. number used by GDB. */
  243. static int
  244. amd64_arch_reg_to_regnum (int reg)
  245. {
  246. gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);
  247. return amd64_arch_regmap[reg];
  248. }
  249. /* Register names for byte pseudo-registers. */
  250. static const char * const amd64_byte_names[] =
  251. {
  252. "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
  253. "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
  254. "ah", "bh", "ch", "dh"
  255. };
  256. /* Number of lower byte registers. */
  257. #define AMD64_NUM_LOWER_BYTE_REGS 16
  258. /* Register names for word pseudo-registers. */
  259. static const char * const amd64_word_names[] =
  260. {
  261. "ax", "bx", "cx", "dx", "si", "di", "bp", "",
  262. "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
  263. };
  264. /* Register names for dword pseudo-registers. */
  265. static const char * const amd64_dword_names[] =
  266. {
  267. "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
  268. "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
  269. "eip"
  270. };
  271. /* Return the name of register REGNUM. */
  272. static const char *
  273. amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
  274. {
  275. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  276. if (i386_byte_regnum_p (gdbarch, regnum))
  277. return amd64_byte_names[regnum - tdep->al_regnum];
  278. else if (i386_zmm_regnum_p (gdbarch, regnum))
  279. return amd64_zmm_names[regnum - tdep->zmm0_regnum];
  280. else if (i386_ymm_regnum_p (gdbarch, regnum))
  281. return amd64_ymm_names[regnum - tdep->ymm0_regnum];
  282. else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
  283. return amd64_ymm_avx512_names[regnum - tdep->ymm16_regnum];
  284. else if (i386_word_regnum_p (gdbarch, regnum))
  285. return amd64_word_names[regnum - tdep->ax_regnum];
  286. else if (i386_dword_regnum_p (gdbarch, regnum))
  287. return amd64_dword_names[regnum - tdep->eax_regnum];
  288. else
  289. return i386_pseudo_register_name (gdbarch, regnum);
  290. }
  291. static struct value *
  292. amd64_pseudo_register_read_value (struct gdbarch *gdbarch,
  293. readable_regcache *regcache,
  294. int regnum)
  295. {
  296. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  297. value *result_value = allocate_value (register_type (gdbarch, regnum));
  298. VALUE_LVAL (result_value) = lval_register;
  299. VALUE_REGNUM (result_value) = regnum;
  300. gdb_byte *buf = value_contents_raw (result_value).data ();
  301. if (i386_byte_regnum_p (gdbarch, regnum))
  302. {
  303. int gpnum = regnum - tdep->al_regnum;
  304. /* Extract (always little endian). */
  305. if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
  306. {
  307. gpnum -= AMD64_NUM_LOWER_BYTE_REGS;
  308. gdb_byte raw_buf[register_size (gdbarch, gpnum)];
  309. /* Special handling for AH, BH, CH, DH. */
  310. register_status status = regcache->raw_read (gpnum, raw_buf);
  311. if (status == REG_VALID)
  312. memcpy (buf, raw_buf + 1, 1);
  313. else
  314. mark_value_bytes_unavailable (result_value, 0,
  315. TYPE_LENGTH (value_type (result_value)));
  316. }
  317. else
  318. {
  319. gdb_byte raw_buf[register_size (gdbarch, gpnum)];
  320. register_status status = regcache->raw_read (gpnum, raw_buf);
  321. if (status == REG_VALID)
  322. memcpy (buf, raw_buf, 1);
  323. else
  324. mark_value_bytes_unavailable (result_value, 0,
  325. TYPE_LENGTH (value_type (result_value)));
  326. }
  327. }
  328. else if (i386_dword_regnum_p (gdbarch, regnum))
  329. {
  330. int gpnum = regnum - tdep->eax_regnum;
  331. gdb_byte raw_buf[register_size (gdbarch, gpnum)];
  332. /* Extract (always little endian). */
  333. register_status status = regcache->raw_read (gpnum, raw_buf);
  334. if (status == REG_VALID)
  335. memcpy (buf, raw_buf, 4);
  336. else
  337. mark_value_bytes_unavailable (result_value, 0,
  338. TYPE_LENGTH (value_type (result_value)));
  339. }
  340. else
  341. i386_pseudo_register_read_into_value (gdbarch, regcache, regnum,
  342. result_value);
  343. return result_value;
  344. }
  345. static void
  346. amd64_pseudo_register_write (struct gdbarch *gdbarch,
  347. struct regcache *regcache,
  348. int regnum, const gdb_byte *buf)
  349. {
  350. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  351. if (i386_byte_regnum_p (gdbarch, regnum))
  352. {
  353. int gpnum = regnum - tdep->al_regnum;
  354. if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
  355. {
  356. gpnum -= AMD64_NUM_LOWER_BYTE_REGS;
  357. gdb_byte raw_buf[register_size (gdbarch, gpnum)];
  358. /* Read ... AH, BH, CH, DH. */
  359. regcache->raw_read (gpnum, raw_buf);
  360. /* ... Modify ... (always little endian). */
  361. memcpy (raw_buf + 1, buf, 1);
  362. /* ... Write. */
  363. regcache->raw_write (gpnum, raw_buf);
  364. }
  365. else
  366. {
  367. gdb_byte raw_buf[register_size (gdbarch, gpnum)];
  368. /* Read ... */
  369. regcache->raw_read (gpnum, raw_buf);
  370. /* ... Modify ... (always little endian). */
  371. memcpy (raw_buf, buf, 1);
  372. /* ... Write. */
  373. regcache->raw_write (gpnum, raw_buf);
  374. }
  375. }
  376. else if (i386_dword_regnum_p (gdbarch, regnum))
  377. {
  378. int gpnum = regnum - tdep->eax_regnum;
  379. gdb_byte raw_buf[register_size (gdbarch, gpnum)];
  380. /* Read ... */
  381. regcache->raw_read (gpnum, raw_buf);
  382. /* ... Modify ... (always little endian). */
  383. memcpy (raw_buf, buf, 4);
  384. /* ... Write. */
  385. regcache->raw_write (gpnum, raw_buf);
  386. }
  387. else
  388. i386_pseudo_register_write (gdbarch, regcache, regnum, buf);
  389. }
  390. /* Implement the 'ax_pseudo_register_collect' gdbarch method. */
  391. static int
  392. amd64_ax_pseudo_register_collect (struct gdbarch *gdbarch,
  393. struct agent_expr *ax, int regnum)
  394. {
  395. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  396. if (i386_byte_regnum_p (gdbarch, regnum))
  397. {
  398. int gpnum = regnum - tdep->al_regnum;
  399. if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
  400. ax_reg_mask (ax, gpnum - AMD64_NUM_LOWER_BYTE_REGS);
  401. else
  402. ax_reg_mask (ax, gpnum);
  403. return 0;
  404. }
  405. else if (i386_dword_regnum_p (gdbarch, regnum))
  406. {
  407. int gpnum = regnum - tdep->eax_regnum;
  408. ax_reg_mask (ax, gpnum);
  409. return 0;
  410. }
  411. else
  412. return i386_ax_pseudo_register_collect (gdbarch, ax, regnum);
  413. }
  414. /* Register classes as defined in the psABI. */
  415. enum amd64_reg_class
  416. {
  417. AMD64_INTEGER,
  418. AMD64_SSE,
  419. AMD64_SSEUP,
  420. AMD64_X87,
  421. AMD64_X87UP,
  422. AMD64_COMPLEX_X87,
  423. AMD64_NO_CLASS,
  424. AMD64_MEMORY
  425. };
  426. /* Return the union class of CLASS1 and CLASS2. See the psABI for
  427. details. */
  428. static enum amd64_reg_class
  429. amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
  430. {
  431. /* Rule (a): If both classes are equal, this is the resulting class. */
  432. if (class1 == class2)
  433. return class1;
  434. /* Rule (b): If one of the classes is NO_CLASS, the resulting class
  435. is the other class. */
  436. if (class1 == AMD64_NO_CLASS)
  437. return class2;
  438. if (class2 == AMD64_NO_CLASS)
  439. return class1;
  440. /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
  441. if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
  442. return AMD64_MEMORY;
  443. /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
  444. if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
  445. return AMD64_INTEGER;
  446. /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
  447. MEMORY is used as class. */
  448. if (class1 == AMD64_X87 || class1 == AMD64_X87UP
  449. || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
  450. || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
  451. return AMD64_MEMORY;
  452. /* Rule (f): Otherwise class SSE is used. */
  453. return AMD64_SSE;
  454. }
  455. static void amd64_classify (struct type *type, enum amd64_reg_class theclass[2]);
  456. /* Return true if TYPE is a structure or union with unaligned fields. */
  457. static bool
  458. amd64_has_unaligned_fields (struct type *type)
  459. {
  460. if (type->code () == TYPE_CODE_STRUCT
  461. || type->code () == TYPE_CODE_UNION)
  462. {
  463. for (int i = 0; i < type->num_fields (); i++)
  464. {
  465. struct type *subtype = check_typedef (type->field (i).type ());
  466. /* Ignore static fields, empty fields (for example nested
  467. empty structures), and bitfields (these are handled by
  468. the caller). */
  469. if (field_is_static (&type->field (i))
  470. || (TYPE_FIELD_BITSIZE (type, i) == 0
  471. && TYPE_LENGTH (subtype) == 0)
  472. || TYPE_FIELD_PACKED (type, i))
  473. continue;
  474. int bitpos = type->field (i).loc_bitpos ();
  475. if (bitpos % 8 != 0)
  476. return true;
  477. int align = type_align (subtype);
  478. if (align == 0)
  479. error (_("could not determine alignment of type"));
  480. int bytepos = bitpos / 8;
  481. if (bytepos % align != 0)
  482. return true;
  483. if (amd64_has_unaligned_fields (subtype))
  484. return true;
  485. }
  486. }
  487. return false;
  488. }
  489. /* Classify field I of TYPE starting at BITOFFSET according to the rules for
  490. structures and union types, and store the result in THECLASS. */
  491. static void
  492. amd64_classify_aggregate_field (struct type *type, int i,
  493. enum amd64_reg_class theclass[2],
  494. unsigned int bitoffset)
  495. {
  496. struct type *subtype = check_typedef (type->field (i).type ());
  497. enum amd64_reg_class subclass[2];
  498. int bitsize = TYPE_FIELD_BITSIZE (type, i);
  499. if (bitsize == 0)
  500. bitsize = TYPE_LENGTH (subtype) * 8;
  501. /* Ignore static fields, or empty fields, for example nested
  502. empty structures.*/
  503. if (field_is_static (&type->field (i)) || bitsize == 0)
  504. return;
  505. int bitpos = bitoffset + type->field (i).loc_bitpos ();
  506. int pos = bitpos / 64;
  507. int endpos = (bitpos + bitsize - 1) / 64;
  508. if (subtype->code () == TYPE_CODE_STRUCT
  509. || subtype->code () == TYPE_CODE_UNION)
  510. {
  511. /* Each field of an object is classified recursively. */
  512. int j;
  513. for (j = 0; j < subtype->num_fields (); j++)
  514. amd64_classify_aggregate_field (subtype, j, theclass, bitpos);
  515. return;
  516. }
  517. gdb_assert (pos == 0 || pos == 1);
  518. amd64_classify (subtype, subclass);
  519. theclass[pos] = amd64_merge_classes (theclass[pos], subclass[0]);
  520. if (bitsize <= 64 && pos == 0 && endpos == 1)
  521. /* This is a bit of an odd case: We have a field that would
  522. normally fit in one of the two eightbytes, except that
  523. it is placed in a way that this field straddles them.
  524. This has been seen with a structure containing an array.
  525. The ABI is a bit unclear in this case, but we assume that
  526. this field's class (stored in subclass[0]) must also be merged
  527. into class[1]. In other words, our field has a piece stored
  528. in the second eight-byte, and thus its class applies to
  529. the second eight-byte as well.
  530. In the case where the field length exceeds 8 bytes,
  531. it should not be necessary to merge the field class
  532. into class[1]. As LEN > 8, subclass[1] is necessarily
  533. different from AMD64_NO_CLASS. If subclass[1] is equal
  534. to subclass[0], then the normal class[1]/subclass[1]
  535. merging will take care of everything. For subclass[1]
  536. to be different from subclass[0], I can only see the case
  537. where we have a SSE/SSEUP or X87/X87UP pair, which both
  538. use up all 16 bytes of the aggregate, and are already
  539. handled just fine (because each portion sits on its own
  540. 8-byte). */
  541. theclass[1] = amd64_merge_classes (theclass[1], subclass[0]);
  542. if (pos == 0)
  543. theclass[1] = amd64_merge_classes (theclass[1], subclass[1]);
  544. }
  545. /* Classify TYPE according to the rules for aggregate (structures and
  546. arrays) and union types, and store the result in CLASS. */
  547. static void
  548. amd64_classify_aggregate (struct type *type, enum amd64_reg_class theclass[2])
  549. {
  550. /* 1. If the size of an object is larger than two times eight bytes, or
  551. it is a non-trivial C++ object, or it has unaligned fields, then it
  552. has class memory.
  553. It is important that the trivially_copyable check is before the
  554. unaligned fields check, as C++ classes with virtual base classes
  555. will have fields (for the virtual base classes) with non-constant
  556. loc_bitpos attributes, which will cause an assert to trigger within
  557. the unaligned field check. As classes with virtual bases are not
  558. trivially copyable, checking that first avoids this problem. */
  559. if (TYPE_LENGTH (type) > 16
  560. || !language_pass_by_reference (type).trivially_copyable
  561. || amd64_has_unaligned_fields (type))
  562. {
  563. theclass[0] = theclass[1] = AMD64_MEMORY;
  564. return;
  565. }
  566. /* 2. Both eightbytes get initialized to class NO_CLASS. */
  567. theclass[0] = theclass[1] = AMD64_NO_CLASS;
  568. /* 3. Each field of an object is classified recursively so that
  569. always two fields are considered. The resulting class is
  570. calculated according to the classes of the fields in the
  571. eightbyte: */
  572. if (type->code () == TYPE_CODE_ARRAY)
  573. {
  574. struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));
  575. /* All fields in an array have the same type. */
  576. amd64_classify (subtype, theclass);
  577. if (TYPE_LENGTH (type) > 8 && theclass[1] == AMD64_NO_CLASS)
  578. theclass[1] = theclass[0];
  579. }
  580. else
  581. {
  582. int i;
  583. /* Structure or union. */
  584. gdb_assert (type->code () == TYPE_CODE_STRUCT
  585. || type->code () == TYPE_CODE_UNION);
  586. for (i = 0; i < type->num_fields (); i++)
  587. amd64_classify_aggregate_field (type, i, theclass, 0);
  588. }
  589. /* 4. Then a post merger cleanup is done: */
  590. /* Rule (a): If one of the classes is MEMORY, the whole argument is
  591. passed in memory. */
  592. if (theclass[0] == AMD64_MEMORY || theclass[1] == AMD64_MEMORY)
  593. theclass[0] = theclass[1] = AMD64_MEMORY;
  594. /* Rule (b): If SSEUP is not preceded by SSE, it is converted to
  595. SSE. */
  596. if (theclass[0] == AMD64_SSEUP)
  597. theclass[0] = AMD64_SSE;
  598. if (theclass[1] == AMD64_SSEUP && theclass[0] != AMD64_SSE)
  599. theclass[1] = AMD64_SSE;
  600. }
  601. /* Classify TYPE, and store the result in CLASS. */
  602. static void
  603. amd64_classify (struct type *type, enum amd64_reg_class theclass[2])
  604. {
  605. enum type_code code = type->code ();
  606. int len = TYPE_LENGTH (type);
  607. theclass[0] = theclass[1] = AMD64_NO_CLASS;
  608. /* Arguments of types (signed and unsigned) _Bool, char, short, int,
  609. long, long long, and pointers are in the INTEGER class. Similarly,
  610. range types, used by languages such as Ada, are also in the INTEGER
  611. class. */
  612. if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
  613. || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
  614. || code == TYPE_CODE_CHAR
  615. || code == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type))
  616. && (len == 1 || len == 2 || len == 4 || len == 8))
  617. theclass[0] = AMD64_INTEGER;
  618. /* Arguments of types _Float16, float, double, _Decimal32, _Decimal64 and
  619. __m64 are in class SSE. */
  620. else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
  621. && (len == 2 || len == 4 || len == 8))
  622. /* FIXME: __m64 . */
  623. theclass[0] = AMD64_SSE;
  624. /* Arguments of types __float128, _Decimal128 and __m128 are split into
  625. two halves. The least significant ones belong to class SSE, the most
  626. significant one to class SSEUP. */
  627. else if (code == TYPE_CODE_DECFLOAT && len == 16)
  628. /* FIXME: __float128, __m128. */
  629. theclass[0] = AMD64_SSE, theclass[1] = AMD64_SSEUP;
  630. /* The 64-bit mantissa of arguments of type long double belongs to
  631. class X87, the 16-bit exponent plus 6 bytes of padding belongs to
  632. class X87UP. */
  633. else if (code == TYPE_CODE_FLT && len == 16)
  634. /* Class X87 and X87UP. */
  635. theclass[0] = AMD64_X87, theclass[1] = AMD64_X87UP;
  636. /* Arguments of complex T - where T is one of the types _Float16, float or
  637. double - get treated as if they are implemented as:
  638. struct complexT {
  639. T real;
  640. T imag;
  641. };
  642. */
  643. else if (code == TYPE_CODE_COMPLEX && (len == 8 || len == 4))
  644. theclass[0] = AMD64_SSE;
  645. else if (code == TYPE_CODE_COMPLEX && len == 16)
  646. theclass[0] = theclass[1] = AMD64_SSE;
  647. /* A variable of type complex long double is classified as type
  648. COMPLEX_X87. */
  649. else if (code == TYPE_CODE_COMPLEX && len == 32)
  650. theclass[0] = AMD64_COMPLEX_X87;
  651. /* Aggregates. */
  652. else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
  653. || code == TYPE_CODE_UNION)
  654. amd64_classify_aggregate (type, theclass);
  655. }
  656. static enum return_value_convention
  657. amd64_return_value (struct gdbarch *gdbarch, struct value *function,
  658. struct type *type, struct regcache *regcache,
  659. gdb_byte *readbuf, const gdb_byte *writebuf)
  660. {
  661. enum amd64_reg_class theclass[2];
  662. int len = TYPE_LENGTH (type);
  663. static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
  664. static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
  665. int integer_reg = 0;
  666. int sse_reg = 0;
  667. int i;
  668. gdb_assert (!(readbuf && writebuf));
  669. /* 1. Classify the return type with the classification algorithm. */
  670. amd64_classify (type, theclass);
  671. /* 2. If the type has class MEMORY, then the caller provides space
  672. for the return value and passes the address of this storage in
  673. %rdi as if it were the first argument to the function. In effect,
  674. this address becomes a hidden first argument.
  675. On return %rax will contain the address that has been passed in
  676. by the caller in %rdi. */
  677. if (theclass[0] == AMD64_MEMORY)
  678. {
  679. /* As indicated by the comment above, the ABI guarantees that we
  680. can always find the return value just after the function has
  681. returned. */
  682. if (readbuf)
  683. {
  684. ULONGEST addr;
  685. regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
  686. read_memory (addr, readbuf, TYPE_LENGTH (type));
  687. }
  688. return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  689. }
  690. /* 8. If the class is COMPLEX_X87, the real part of the value is
  691. returned in %st0 and the imaginary part in %st1. */
  692. if (theclass[0] == AMD64_COMPLEX_X87)
  693. {
  694. if (readbuf)
  695. {
  696. regcache->raw_read (AMD64_ST0_REGNUM, readbuf);
  697. regcache->raw_read (AMD64_ST1_REGNUM, readbuf + 16);
  698. }
  699. if (writebuf)
  700. {
  701. i387_return_value (gdbarch, regcache);
  702. regcache->raw_write (AMD64_ST0_REGNUM, writebuf);
  703. regcache->raw_write (AMD64_ST1_REGNUM, writebuf + 16);
  704. /* Fix up the tag word such that both %st(0) and %st(1) are
  705. marked as valid. */
  706. regcache_raw_write_unsigned (regcache, AMD64_FTAG_REGNUM, 0xfff);
  707. }
  708. return RETURN_VALUE_REGISTER_CONVENTION;
  709. }
  710. gdb_assert (theclass[1] != AMD64_MEMORY);
  711. gdb_assert (len <= 16);
  712. for (i = 0; len > 0; i++, len -= 8)
  713. {
  714. int regnum = -1;
  715. int offset = 0;
  716. switch (theclass[i])
  717. {
  718. case AMD64_INTEGER:
  719. /* 3. If the class is INTEGER, the next available register
  720. of the sequence %rax, %rdx is used. */
  721. regnum = integer_regnum[integer_reg++];
  722. break;
  723. case AMD64_SSE:
  724. /* 4. If the class is SSE, the next available SSE register
  725. of the sequence %xmm0, %xmm1 is used. */
  726. regnum = sse_regnum[sse_reg++];
  727. break;
  728. case AMD64_SSEUP:
  729. /* 5. If the class is SSEUP, the eightbyte is passed in the
  730. upper half of the last used SSE register. */
  731. gdb_assert (sse_reg > 0);
  732. regnum = sse_regnum[sse_reg - 1];
  733. offset = 8;
  734. break;
  735. case AMD64_X87:
  736. /* 6. If the class is X87, the value is returned on the X87
  737. stack in %st0 as 80-bit x87 number. */
  738. regnum = AMD64_ST0_REGNUM;
  739. if (writebuf)
  740. i387_return_value (gdbarch, regcache);
  741. break;
  742. case AMD64_X87UP:
  743. /* 7. If the class is X87UP, the value is returned together
  744. with the previous X87 value in %st0. */
  745. gdb_assert (i > 0 && theclass[0] == AMD64_X87);
  746. regnum = AMD64_ST0_REGNUM;
  747. offset = 8;
  748. len = 2;
  749. break;
  750. case AMD64_NO_CLASS:
  751. continue;
  752. default:
  753. gdb_assert (!"Unexpected register class.");
  754. }
  755. gdb_assert (regnum != -1);
  756. if (readbuf)
  757. regcache->raw_read_part (regnum, offset, std::min (len, 8),
  758. readbuf + i * 8);
  759. if (writebuf)
  760. regcache->raw_write_part (regnum, offset, std::min (len, 8),
  761. writebuf + i * 8);
  762. }
  763. return RETURN_VALUE_REGISTER_CONVENTION;
  764. }
  765. static CORE_ADDR
  766. amd64_push_arguments (struct regcache *regcache, int nargs, struct value **args,
  767. CORE_ADDR sp, function_call_return_method return_method)
  768. {
  769. static int integer_regnum[] =
  770. {
  771. AMD64_RDI_REGNUM, /* %rdi */
  772. AMD64_RSI_REGNUM, /* %rsi */
  773. AMD64_RDX_REGNUM, /* %rdx */
  774. AMD64_RCX_REGNUM, /* %rcx */
  775. AMD64_R8_REGNUM, /* %r8 */
  776. AMD64_R9_REGNUM /* %r9 */
  777. };
  778. static int sse_regnum[] =
  779. {
  780. /* %xmm0 ... %xmm7 */
  781. AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
  782. AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
  783. AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
  784. AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
  785. };
  786. struct value **stack_args = XALLOCAVEC (struct value *, nargs);
  787. int num_stack_args = 0;
  788. int num_elements = 0;
  789. int element = 0;
  790. int integer_reg = 0;
  791. int sse_reg = 0;
  792. int i;
  793. /* Reserve a register for the "hidden" argument. */
  794. if (return_method == return_method_struct)
  795. integer_reg++;
  796. for (i = 0; i < nargs; i++)
  797. {
  798. struct type *type = value_type (args[i]);
  799. int len = TYPE_LENGTH (type);
  800. enum amd64_reg_class theclass[2];
  801. int needed_integer_regs = 0;
  802. int needed_sse_regs = 0;
  803. int j;
  804. /* Classify argument. */
  805. amd64_classify (type, theclass);
  806. /* Calculate the number of integer and SSE registers needed for
  807. this argument. */
  808. for (j = 0; j < 2; j++)
  809. {
  810. if (theclass[j] == AMD64_INTEGER)
  811. needed_integer_regs++;
  812. else if (theclass[j] == AMD64_SSE)
  813. needed_sse_regs++;
  814. }
  815. /* Check whether enough registers are available, and if the
  816. argument should be passed in registers at all. */
  817. if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
  818. || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
  819. || (needed_integer_regs == 0 && needed_sse_regs == 0))
  820. {
  821. /* The argument will be passed on the stack. */
  822. num_elements += ((len + 7) / 8);
  823. stack_args[num_stack_args++] = args[i];
  824. }
  825. else
  826. {
  827. /* The argument will be passed in registers. */
  828. const gdb_byte *valbuf = value_contents (args[i]).data ();
  829. gdb_byte buf[8];
  830. gdb_assert (len <= 16);
  831. for (j = 0; len > 0; j++, len -= 8)
  832. {
  833. int regnum = -1;
  834. int offset = 0;
  835. switch (theclass[j])
  836. {
  837. case AMD64_INTEGER:
  838. regnum = integer_regnum[integer_reg++];
  839. break;
  840. case AMD64_SSE:
  841. regnum = sse_regnum[sse_reg++];
  842. break;
  843. case AMD64_SSEUP:
  844. gdb_assert (sse_reg > 0);
  845. regnum = sse_regnum[sse_reg - 1];
  846. offset = 8;
  847. break;
  848. case AMD64_NO_CLASS:
  849. continue;
  850. default:
  851. gdb_assert (!"Unexpected register class.");
  852. }
  853. gdb_assert (regnum != -1);
  854. memset (buf, 0, sizeof buf);
  855. memcpy (buf, valbuf + j * 8, std::min (len, 8));
  856. regcache->raw_write_part (regnum, offset, 8, buf);
  857. }
  858. }
  859. }
  860. /* Allocate space for the arguments on the stack. */
  861. sp -= num_elements * 8;
  862. /* The psABI says that "The end of the input argument area shall be
  863. aligned on a 16 byte boundary." */
  864. sp &= ~0xf;
  865. /* Write out the arguments to the stack. */
  866. for (i = 0; i < num_stack_args; i++)
  867. {
  868. struct type *type = value_type (stack_args[i]);
  869. const gdb_byte *valbuf = value_contents (stack_args[i]).data ();
  870. int len = TYPE_LENGTH (type);
  871. write_memory (sp + element * 8, valbuf, len);
  872. element += ((len + 7) / 8);
  873. }
  874. /* The psABI says that "For calls that may call functions that use
  875. varargs or stdargs (prototype-less calls or calls to functions
  876. containing ellipsis (...) in the declaration) %al is used as
  877. hidden argument to specify the number of SSE registers used. */
  878. regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
  879. return sp;
  880. }
  881. static CORE_ADDR
  882. amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  883. struct regcache *regcache, CORE_ADDR bp_addr,
  884. int nargs, struct value **args, CORE_ADDR sp,
  885. function_call_return_method return_method,
  886. CORE_ADDR struct_addr)
  887. {
  888. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  889. gdb_byte buf[8];
  890. /* BND registers can be in arbitrary values at the moment of the
  891. inferior call. This can cause boundary violations that are not
  892. due to a real bug or even desired by the user. The best to be done
  893. is set the BND registers to allow access to the whole memory, INIT
  894. state, before pushing the inferior call. */
  895. i387_reset_bnd_regs (gdbarch, regcache);
  896. /* Pass arguments. */
  897. sp = amd64_push_arguments (regcache, nargs, args, sp, return_method);
  898. /* Pass "hidden" argument". */
  899. if (return_method == return_method_struct)
  900. {
  901. store_unsigned_integer (buf, 8, byte_order, struct_addr);
  902. regcache->cooked_write (AMD64_RDI_REGNUM, buf);
  903. }
  904. /* Store return address. */
  905. sp -= 8;
  906. store_unsigned_integer (buf, 8, byte_order, bp_addr);
  907. write_memory (sp, buf, 8);
  908. /* Finally, update the stack pointer... */
  909. store_unsigned_integer (buf, 8, byte_order, sp);
  910. regcache->cooked_write (AMD64_RSP_REGNUM, buf);
  911. /* ...and fake a frame pointer. */
  912. regcache->cooked_write (AMD64_RBP_REGNUM, buf);
  913. return sp + 16;
  914. }
  915. /* Displaced instruction handling. */
  916. /* A partially decoded instruction.
  917. This contains enough details for displaced stepping purposes. */
  918. struct amd64_insn
  919. {
  920. /* The number of opcode bytes. */
  921. int opcode_len;
  922. /* The offset of the REX/VEX instruction encoding prefix or -1 if
  923. not present. */
  924. int enc_prefix_offset;
  925. /* The offset to the first opcode byte. */
  926. int opcode_offset;
  927. /* The offset to the modrm byte or -1 if not present. */
  928. int modrm_offset;
  929. /* The raw instruction. */
  930. gdb_byte *raw_insn;
  931. };
  932. struct amd64_displaced_step_copy_insn_closure
  933. : public displaced_step_copy_insn_closure
  934. {
  935. amd64_displaced_step_copy_insn_closure (int insn_buf_len)
  936. : insn_buf (insn_buf_len, 0)
  937. {}
  938. /* For rip-relative insns, saved copy of the reg we use instead of %rip. */
  939. int tmp_used = 0;
  940. int tmp_regno;
  941. ULONGEST tmp_save;
  942. /* Details of the instruction. */
  943. struct amd64_insn insn_details;
  944. /* The possibly modified insn. */
  945. gdb::byte_vector insn_buf;
  946. };
  947. /* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
  948. ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
  949. at which point delete these in favor of libopcodes' versions). */
  950. static const unsigned char onebyte_has_modrm[256] = {
  951. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  952. /* ------------------------------- */
  953. /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
  954. /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
  955. /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
  956. /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
  957. /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
  958. /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
  959. /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
  960. /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
  961. /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
  962. /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
  963. /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
  964. /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
  965. /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
  966. /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
  967. /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
  968. /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
  969. /* ------------------------------- */
  970. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  971. };
  972. static const unsigned char twobyte_has_modrm[256] = {
  973. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  974. /* ------------------------------- */
  975. /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
  976. /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
  977. /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
  978. /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
  979. /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
  980. /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
  981. /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
  982. /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
  983. /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
  984. /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
  985. /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
  986. /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
  987. /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
  988. /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
  989. /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
  990. /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
  991. /* ------------------------------- */
  992. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  993. };
  994. static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);
  995. static int
  996. rex_prefix_p (gdb_byte pfx)
  997. {
  998. return REX_PREFIX_P (pfx);
  999. }
  1000. /* True if PFX is the start of the 2-byte VEX prefix. */
  1001. static bool
  1002. vex2_prefix_p (gdb_byte pfx)
  1003. {
  1004. return pfx == 0xc5;
  1005. }
  1006. /* True if PFX is the start of the 3-byte VEX prefix. */
  1007. static bool
  1008. vex3_prefix_p (gdb_byte pfx)
  1009. {
  1010. return pfx == 0xc4;
  1011. }
  1012. /* Skip the legacy instruction prefixes in INSN.
  1013. We assume INSN is properly sentineled so we don't have to worry
  1014. about falling off the end of the buffer. */
  1015. static gdb_byte *
  1016. amd64_skip_prefixes (gdb_byte *insn)
  1017. {
  1018. while (1)
  1019. {
  1020. switch (*insn)
  1021. {
  1022. case DATA_PREFIX_OPCODE:
  1023. case ADDR_PREFIX_OPCODE:
  1024. case CS_PREFIX_OPCODE:
  1025. case DS_PREFIX_OPCODE:
  1026. case ES_PREFIX_OPCODE:
  1027. case FS_PREFIX_OPCODE:
  1028. case GS_PREFIX_OPCODE:
  1029. case SS_PREFIX_OPCODE:
  1030. case LOCK_PREFIX_OPCODE:
  1031. case REPE_PREFIX_OPCODE:
  1032. case REPNE_PREFIX_OPCODE:
  1033. ++insn;
  1034. continue;
  1035. default:
  1036. break;
  1037. }
  1038. break;
  1039. }
  1040. return insn;
  1041. }
  1042. /* Return an integer register (other than RSP) that is unused as an input
  1043. operand in INSN.
  1044. In order to not require adding a rex prefix if the insn doesn't already
  1045. have one, the result is restricted to RAX ... RDI, sans RSP.
  1046. The register numbering of the result follows architecture ordering,
  1047. e.g. RDI = 7. */
  1048. static int
  1049. amd64_get_unused_input_int_reg (const struct amd64_insn *details)
  1050. {
  1051. /* 1 bit for each reg */
  1052. int used_regs_mask = 0;
  1053. /* There can be at most 3 int regs used as inputs in an insn, and we have
  1054. 7 to choose from (RAX ... RDI, sans RSP).
  1055. This allows us to take a conservative approach and keep things simple.
  1056. E.g. By avoiding RAX, we don't have to specifically watch for opcodes
  1057. that implicitly specify RAX. */
  1058. /* Avoid RAX. */
  1059. used_regs_mask |= 1 << EAX_REG_NUM;
  1060. /* Similarily avoid RDX, implicit operand in divides. */
  1061. used_regs_mask |= 1 << EDX_REG_NUM;
  1062. /* Avoid RSP. */
  1063. used_regs_mask |= 1 << ESP_REG_NUM;
  1064. /* If the opcode is one byte long and there's no ModRM byte,
  1065. assume the opcode specifies a register. */
  1066. if (details->opcode_len == 1 && details->modrm_offset == -1)
  1067. used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);
  1068. /* Mark used regs in the modrm/sib bytes. */
  1069. if (details->modrm_offset != -1)
  1070. {
  1071. int modrm = details->raw_insn[details->modrm_offset];
  1072. int mod = MODRM_MOD_FIELD (modrm);
  1073. int reg = MODRM_REG_FIELD (modrm);
  1074. int rm = MODRM_RM_FIELD (modrm);
  1075. int have_sib = mod != 3 && rm == 4;
  1076. /* Assume the reg field of the modrm byte specifies a register. */
  1077. used_regs_mask |= 1 << reg;
  1078. if (have_sib)
  1079. {
  1080. int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
  1081. int idx = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
  1082. used_regs_mask |= 1 << base;
  1083. used_regs_mask |= 1 << idx;
  1084. }
  1085. else
  1086. {
  1087. used_regs_mask |= 1 << rm;
  1088. }
  1089. }
  1090. gdb_assert (used_regs_mask < 256);
  1091. gdb_assert (used_regs_mask != 255);
  1092. /* Finally, find a free reg. */
  1093. {
  1094. int i;
  1095. for (i = 0; i < 8; ++i)
  1096. {
  1097. if (! (used_regs_mask & (1 << i)))
  1098. return i;
  1099. }
  1100. /* We shouldn't get here. */
  1101. internal_error (__FILE__, __LINE__, _("unable to find free reg"));
  1102. }
  1103. }
  1104. /* Extract the details of INSN that we need. */
  1105. static void
  1106. amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
  1107. {
  1108. gdb_byte *start = insn;
  1109. int need_modrm;
  1110. details->raw_insn = insn;
  1111. details->opcode_len = -1;
  1112. details->enc_prefix_offset = -1;
  1113. details->opcode_offset = -1;
  1114. details->modrm_offset = -1;
  1115. /* Skip legacy instruction prefixes. */
  1116. insn = amd64_skip_prefixes (insn);
  1117. /* Skip REX/VEX instruction encoding prefixes. */
  1118. if (rex_prefix_p (*insn))
  1119. {
  1120. details->enc_prefix_offset = insn - start;
  1121. ++insn;
  1122. }
  1123. else if (vex2_prefix_p (*insn))
  1124. {
  1125. /* Don't record the offset in this case because this prefix has
  1126. no REX.B equivalent. */
  1127. insn += 2;
  1128. }
  1129. else if (vex3_prefix_p (*insn))
  1130. {
  1131. details->enc_prefix_offset = insn - start;
  1132. insn += 3;
  1133. }
  1134. details->opcode_offset = insn - start;
  1135. if (*insn == TWO_BYTE_OPCODE_ESCAPE)
  1136. {
  1137. /* Two or three-byte opcode. */
  1138. ++insn;
  1139. need_modrm = twobyte_has_modrm[*insn];
  1140. /* Check for three-byte opcode. */
  1141. switch (*insn)
  1142. {
  1143. case 0x24:
  1144. case 0x25:
  1145. case 0x38:
  1146. case 0x3a:
  1147. case 0x7a:
  1148. case 0x7b:
  1149. ++insn;
  1150. details->opcode_len = 3;
  1151. break;
  1152. default:
  1153. details->opcode_len = 2;
  1154. break;
  1155. }
  1156. }
  1157. else
  1158. {
  1159. /* One-byte opcode. */
  1160. need_modrm = onebyte_has_modrm[*insn];
  1161. details->opcode_len = 1;
  1162. }
  1163. if (need_modrm)
  1164. {
  1165. ++insn;
  1166. details->modrm_offset = insn - start;
  1167. }
  1168. }
  1169. /* Update %rip-relative addressing in INSN.
  1170. %rip-relative addressing only uses a 32-bit displacement.
  1171. 32 bits is not enough to be guaranteed to cover the distance between where
  1172. the real instruction is and where its copy is.
  1173. Convert the insn to use base+disp addressing.
  1174. We set base = pc + insn_length so we can leave disp unchanged. */
  1175. static void
  1176. fixup_riprel (struct gdbarch *gdbarch,
  1177. amd64_displaced_step_copy_insn_closure *dsc,
  1178. CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
  1179. {
  1180. const struct amd64_insn *insn_details = &dsc->insn_details;
  1181. int modrm_offset = insn_details->modrm_offset;
  1182. gdb_byte *insn = insn_details->raw_insn + modrm_offset;
  1183. CORE_ADDR rip_base;
  1184. int insn_length;
  1185. int arch_tmp_regno, tmp_regno;
  1186. ULONGEST orig_value;
  1187. /* %rip+disp32 addressing mode, displacement follows ModRM byte. */
  1188. ++insn;
  1189. /* Compute the rip-relative address. */
  1190. insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf.data (),
  1191. dsc->insn_buf.size (), from);
  1192. rip_base = from + insn_length;
  1193. /* We need a register to hold the address.
  1194. Pick one not used in the insn.
  1195. NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7. */
  1196. arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
  1197. tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);
  1198. /* Position of the not-B bit in the 3-byte VEX prefix (in byte 1). */
  1199. static constexpr gdb_byte VEX3_NOT_B = 0x20;
  1200. /* REX.B should be unset (VEX.!B set) as we were using rip-relative
  1201. addressing, but ensure it's unset (set for VEX) anyway, tmp_regno
  1202. is not r8-r15. */
  1203. if (insn_details->enc_prefix_offset != -1)
  1204. {
  1205. gdb_byte *pfx = &dsc->insn_buf[insn_details->enc_prefix_offset];
  1206. if (rex_prefix_p (pfx[0]))
  1207. pfx[0] &= ~REX_B;
  1208. else if (vex3_prefix_p (pfx[0]))
  1209. pfx[1] |= VEX3_NOT_B;
  1210. else
  1211. gdb_assert_not_reached ("unhandled prefix");
  1212. }
  1213. regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
  1214. dsc->tmp_regno = tmp_regno;
  1215. dsc->tmp_save = orig_value;
  1216. dsc->tmp_used = 1;
  1217. /* Convert the ModRM field to be base+disp. */
  1218. dsc->insn_buf[modrm_offset] &= ~0xc7;
  1219. dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;
  1220. regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);
  1221. displaced_debug_printf ("%%rip-relative addressing used.");
  1222. displaced_debug_printf ("using temp reg %d, old value %s, new value %s",
  1223. dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
  1224. paddress (gdbarch, rip_base));
  1225. }
  1226. static void
  1227. fixup_displaced_copy (struct gdbarch *gdbarch,
  1228. amd64_displaced_step_copy_insn_closure *dsc,
  1229. CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
  1230. {
  1231. const struct amd64_insn *details = &dsc->insn_details;
  1232. if (details->modrm_offset != -1)
  1233. {
  1234. gdb_byte modrm = details->raw_insn[details->modrm_offset];
  1235. if ((modrm & 0xc7) == 0x05)
  1236. {
  1237. /* The insn uses rip-relative addressing.
  1238. Deal with it. */
  1239. fixup_riprel (gdbarch, dsc, from, to, regs);
  1240. }
  1241. }
  1242. }
  1243. displaced_step_copy_insn_closure_up
  1244. amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
  1245. CORE_ADDR from, CORE_ADDR to,
  1246. struct regcache *regs)
  1247. {
  1248. int len = gdbarch_max_insn_length (gdbarch);
  1249. /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
  1250. continually watch for running off the end of the buffer. */
  1251. int fixup_sentinel_space = len;
  1252. std::unique_ptr<amd64_displaced_step_copy_insn_closure> dsc
  1253. (new amd64_displaced_step_copy_insn_closure (len + fixup_sentinel_space));
  1254. gdb_byte *buf = &dsc->insn_buf[0];
  1255. struct amd64_insn *details = &dsc->insn_details;
  1256. read_memory (from, buf, len);
  1257. /* Set up the sentinel space so we don't have to worry about running
  1258. off the end of the buffer. An excessive number of leading prefixes
  1259. could otherwise cause this. */
  1260. memset (buf + len, 0, fixup_sentinel_space);
  1261. amd64_get_insn_details (buf, details);
  1262. /* GDB may get control back after the insn after the syscall.
  1263. Presumably this is a kernel bug.
  1264. If this is a syscall, make sure there's a nop afterwards. */
  1265. {
  1266. int syscall_length;
  1267. if (amd64_syscall_p (details, &syscall_length))
  1268. buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
  1269. }
  1270. /* Modify the insn to cope with the address where it will be executed from.
  1271. In particular, handle any rip-relative addressing. */
  1272. fixup_displaced_copy (gdbarch, dsc.get (), from, to, regs);
  1273. write_memory (to, buf, len);
  1274. displaced_debug_printf ("copy %s->%s: %s",
  1275. paddress (gdbarch, from), paddress (gdbarch, to),
  1276. displaced_step_dump_bytes (buf, len).c_str ());
  1277. /* This is a work around for a problem with g++ 4.8. */
  1278. return displaced_step_copy_insn_closure_up (dsc.release ());
  1279. }
  1280. static int
  1281. amd64_absolute_jmp_p (const struct amd64_insn *details)
  1282. {
  1283. const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
  1284. if (insn[0] == 0xff)
  1285. {
  1286. /* jump near, absolute indirect (/4) */
  1287. if ((insn[1] & 0x38) == 0x20)
  1288. return 1;
  1289. /* jump far, absolute indirect (/5) */
  1290. if ((insn[1] & 0x38) == 0x28)
  1291. return 1;
  1292. }
  1293. return 0;
  1294. }
  1295. /* Return non-zero if the instruction DETAILS is a jump, zero otherwise. */
  1296. static int
  1297. amd64_jmp_p (const struct amd64_insn *details)
  1298. {
  1299. const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
  1300. /* jump short, relative. */
  1301. if (insn[0] == 0xeb)
  1302. return 1;
  1303. /* jump near, relative. */
  1304. if (insn[0] == 0xe9)
  1305. return 1;
  1306. return amd64_absolute_jmp_p (details);
  1307. }
  1308. static int
  1309. amd64_absolute_call_p (const struct amd64_insn *details)
  1310. {
  1311. const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
  1312. if (insn[0] == 0xff)
  1313. {
  1314. /* Call near, absolute indirect (/2) */
  1315. if ((insn[1] & 0x38) == 0x10)
  1316. return 1;
  1317. /* Call far, absolute indirect (/3) */
  1318. if ((insn[1] & 0x38) == 0x18)
  1319. return 1;
  1320. }
  1321. return 0;
  1322. }
  1323. static int
  1324. amd64_ret_p (const struct amd64_insn *details)
  1325. {
  1326. /* NOTE: gcc can emit "repz ; ret". */
  1327. const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
  1328. switch (insn[0])
  1329. {
  1330. case 0xc2: /* ret near, pop N bytes */
  1331. case 0xc3: /* ret near */
  1332. case 0xca: /* ret far, pop N bytes */
  1333. case 0xcb: /* ret far */
  1334. case 0xcf: /* iret */
  1335. return 1;
  1336. default:
  1337. return 0;
  1338. }
  1339. }
  1340. static int
  1341. amd64_call_p (const struct amd64_insn *details)
  1342. {
  1343. const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
  1344. if (amd64_absolute_call_p (details))
  1345. return 1;
  1346. /* call near, relative */
  1347. if (insn[0] == 0xe8)
  1348. return 1;
  1349. return 0;
  1350. }
  1351. /* Return non-zero if INSN is a system call, and set *LENGTHP to its
  1352. length in bytes. Otherwise, return zero. */
  1353. static int
  1354. amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
  1355. {
  1356. const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
  1357. if (insn[0] == 0x0f && insn[1] == 0x05)
  1358. {
  1359. *lengthp = 2;
  1360. return 1;
  1361. }
  1362. return 0;
  1363. }
  1364. /* Classify the instruction at ADDR using PRED.
  1365. Throw an error if the memory can't be read. */
  1366. static int
  1367. amd64_classify_insn_at (struct gdbarch *gdbarch, CORE_ADDR addr,
  1368. int (*pred) (const struct amd64_insn *))
  1369. {
  1370. struct amd64_insn details;
  1371. gdb_byte *buf;
  1372. int len, classification;
  1373. len = gdbarch_max_insn_length (gdbarch);
  1374. buf = (gdb_byte *) alloca (len);
  1375. read_code (addr, buf, len);
  1376. amd64_get_insn_details (buf, &details);
  1377. classification = pred (&details);
  1378. return classification;
  1379. }
  1380. /* The gdbarch insn_is_call method. */
  1381. static int
  1382. amd64_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
  1383. {
  1384. return amd64_classify_insn_at (gdbarch, addr, amd64_call_p);
  1385. }
  1386. /* The gdbarch insn_is_ret method. */
  1387. static int
  1388. amd64_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
  1389. {
  1390. return amd64_classify_insn_at (gdbarch, addr, amd64_ret_p);
  1391. }
  1392. /* The gdbarch insn_is_jump method. */
  1393. static int
  1394. amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
  1395. {
  1396. return amd64_classify_insn_at (gdbarch, addr, amd64_jmp_p);
  1397. }
  1398. /* Fix up the state of registers and memory after having single-stepped
  1399. a displaced instruction. */
  1400. void
  1401. amd64_displaced_step_fixup (struct gdbarch *gdbarch,
  1402. struct displaced_step_copy_insn_closure *dsc_,
  1403. CORE_ADDR from, CORE_ADDR to,
  1404. struct regcache *regs)
  1405. {
  1406. amd64_displaced_step_copy_insn_closure *dsc
  1407. = (amd64_displaced_step_copy_insn_closure *) dsc_;
  1408. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1409. /* The offset we applied to the instruction's address. */
  1410. ULONGEST insn_offset = to - from;
  1411. gdb_byte *insn = dsc->insn_buf.data ();
  1412. const struct amd64_insn *insn_details = &dsc->insn_details;
  1413. displaced_debug_printf ("fixup (%s, %s), insn = 0x%02x 0x%02x ...",
  1414. paddress (gdbarch, from), paddress (gdbarch, to),
  1415. insn[0], insn[1]);
  1416. /* If we used a tmp reg, restore it. */
  1417. if (dsc->tmp_used)
  1418. {
  1419. displaced_debug_printf ("restoring reg %d to %s",
  1420. dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
  1421. regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
  1422. }
  1423. /* The list of issues to contend with here is taken from
  1424. resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
  1425. Yay for Free Software! */
  1426. /* Relocate the %rip back to the program's instruction stream,
  1427. if necessary. */
  1428. /* Except in the case of absolute or indirect jump or call
  1429. instructions, or a return instruction, the new rip is relative to
  1430. the displaced instruction; make it relative to the original insn.
  1431. Well, signal handler returns don't need relocation either, but we use the
  1432. value of %rip to recognize those; see below. */
  1433. if (! amd64_absolute_jmp_p (insn_details)
  1434. && ! amd64_absolute_call_p (insn_details)
  1435. && ! amd64_ret_p (insn_details))
  1436. {
  1437. ULONGEST orig_rip;
  1438. int insn_len;
  1439. regcache_cooked_read_unsigned (regs, AMD64_RIP_REGNUM, &orig_rip);
  1440. /* A signal trampoline system call changes the %rip, resuming
  1441. execution of the main program after the signal handler has
  1442. returned. That makes them like 'return' instructions; we
  1443. shouldn't relocate %rip.
  1444. But most system calls don't, and we do need to relocate %rip.
  1445. Our heuristic for distinguishing these cases: if stepping
  1446. over the system call instruction left control directly after
  1447. the instruction, the we relocate --- control almost certainly
  1448. doesn't belong in the displaced copy. Otherwise, we assume
  1449. the instruction has put control where it belongs, and leave
  1450. it unrelocated. Goodness help us if there are PC-relative
  1451. system calls. */
  1452. if (amd64_syscall_p (insn_details, &insn_len)
  1453. && orig_rip != to + insn_len
  1454. /* GDB can get control back after the insn after the syscall.
  1455. Presumably this is a kernel bug.
  1456. Fixup ensures its a nop, we add one to the length for it. */
  1457. && orig_rip != to + insn_len + 1)
  1458. displaced_debug_printf ("syscall changed %%rip; not relocating");
  1459. else
  1460. {
  1461. ULONGEST rip = orig_rip - insn_offset;
  1462. /* If we just stepped over a breakpoint insn, we don't backup
  1463. the pc on purpose; this is to match behaviour without
  1464. stepping. */
  1465. regcache_cooked_write_unsigned (regs, AMD64_RIP_REGNUM, rip);
  1466. displaced_debug_printf ("relocated %%rip from %s to %s",
  1467. paddress (gdbarch, orig_rip),
  1468. paddress (gdbarch, rip));
  1469. }
  1470. }
  1471. /* If the instruction was PUSHFL, then the TF bit will be set in the
  1472. pushed value, and should be cleared. We'll leave this for later,
  1473. since GDB already messes up the TF flag when stepping over a
  1474. pushfl. */
  1475. /* If the instruction was a call, the return address now atop the
  1476. stack is the address following the copied instruction. We need
  1477. to make it the address following the original instruction. */
  1478. if (amd64_call_p (insn_details))
  1479. {
  1480. ULONGEST rsp;
  1481. ULONGEST retaddr;
  1482. const ULONGEST retaddr_len = 8;
  1483. regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
  1484. retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
  1485. retaddr = (retaddr - insn_offset) & 0xffffffffffffffffULL;
  1486. write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);
  1487. displaced_debug_printf ("relocated return addr at %s to %s",
  1488. paddress (gdbarch, rsp),
  1489. paddress (gdbarch, retaddr));
  1490. }
  1491. }
  1492. /* If the instruction INSN uses RIP-relative addressing, return the
  1493. offset into the raw INSN where the displacement to be adjusted is
  1494. found. Returns 0 if the instruction doesn't use RIP-relative
  1495. addressing. */
  1496. static int
  1497. rip_relative_offset (struct amd64_insn *insn)
  1498. {
  1499. if (insn->modrm_offset != -1)
  1500. {
  1501. gdb_byte modrm = insn->raw_insn[insn->modrm_offset];
  1502. if ((modrm & 0xc7) == 0x05)
  1503. {
  1504. /* The displacement is found right after the ModRM byte. */
  1505. return insn->modrm_offset + 1;
  1506. }
  1507. }
  1508. return 0;
  1509. }
  1510. static void
  1511. append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
  1512. {
  1513. target_write_memory (*to, buf, len);
  1514. *to += len;
  1515. }
  1516. static void
  1517. amd64_relocate_instruction (struct gdbarch *gdbarch,
  1518. CORE_ADDR *to, CORE_ADDR oldloc)
  1519. {
  1520. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1521. int len = gdbarch_max_insn_length (gdbarch);
  1522. /* Extra space for sentinels. */
  1523. int fixup_sentinel_space = len;
  1524. gdb_byte *buf = (gdb_byte *) xmalloc (len + fixup_sentinel_space);
  1525. struct amd64_insn insn_details;
  1526. int offset = 0;
  1527. LONGEST rel32, newrel;
  1528. gdb_byte *insn;
  1529. int insn_length;
  1530. read_memory (oldloc, buf, len);
  1531. /* Set up the sentinel space so we don't have to worry about running
  1532. off the end of the buffer. An excessive number of leading prefixes
  1533. could otherwise cause this. */
  1534. memset (buf + len, 0, fixup_sentinel_space);
  1535. insn = buf;
  1536. amd64_get_insn_details (insn, &insn_details);
  1537. insn_length = gdb_buffered_insn_length (gdbarch, insn, len, oldloc);
  1538. /* Skip legacy instruction prefixes. */
  1539. insn = amd64_skip_prefixes (insn);
  1540. /* Adjust calls with 32-bit relative addresses as push/jump, with
  1541. the address pushed being the location where the original call in
  1542. the user program would return to. */
  1543. if (insn[0] == 0xe8)
  1544. {
  1545. gdb_byte push_buf[32];
  1546. CORE_ADDR ret_addr;
  1547. int i = 0;
  1548. /* Where "ret" in the original code will return to. */
  1549. ret_addr = oldloc + insn_length;
  1550. /* If pushing an address higher than or equal to 0x80000000,
  1551. avoid 'pushq', as that sign extends its 32-bit operand, which
  1552. would be incorrect. */
  1553. if (ret_addr <= 0x7fffffff)
  1554. {
  1555. push_buf[0] = 0x68; /* pushq $... */
  1556. store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
  1557. i = 5;
  1558. }
  1559. else
  1560. {
  1561. push_buf[i++] = 0x48; /* sub $0x8,%rsp */
  1562. push_buf[i++] = 0x83;
  1563. push_buf[i++] = 0xec;
  1564. push_buf[i++] = 0x08;
  1565. push_buf[i++] = 0xc7; /* movl $imm,(%rsp) */
  1566. push_buf[i++] = 0x04;
  1567. push_buf[i++] = 0x24;
  1568. store_unsigned_integer (&push_buf[i], 4, byte_order,
  1569. ret_addr & 0xffffffff);
  1570. i += 4;
  1571. push_buf[i++] = 0xc7; /* movl $imm,4(%rsp) */
  1572. push_buf[i++] = 0x44;
  1573. push_buf[i++] = 0x24;
  1574. push_buf[i++] = 0x04;
  1575. store_unsigned_integer (&push_buf[i], 4, byte_order,
  1576. ret_addr >> 32);
  1577. i += 4;
  1578. }
  1579. gdb_assert (i <= sizeof (push_buf));
  1580. /* Push the push. */
  1581. append_insns (to, i, push_buf);
  1582. /* Convert the relative call to a relative jump. */
  1583. insn[0] = 0xe9;
  1584. /* Adjust the destination offset. */
  1585. rel32 = extract_signed_integer (insn + 1, 4, byte_order);
  1586. newrel = (oldloc - *to) + rel32;
  1587. store_signed_integer (insn + 1, 4, byte_order, newrel);
  1588. displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
  1589. hex_string (rel32), paddress (gdbarch, oldloc),
  1590. hex_string (newrel), paddress (gdbarch, *to));
  1591. /* Write the adjusted jump into its displaced location. */
  1592. append_insns (to, 5, insn);
  1593. return;
  1594. }
  1595. offset = rip_relative_offset (&insn_details);
  1596. if (!offset)
  1597. {
  1598. /* Adjust jumps with 32-bit relative addresses. Calls are
  1599. already handled above. */
  1600. if (insn[0] == 0xe9)
  1601. offset = 1;
  1602. /* Adjust conditional jumps. */
  1603. else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
  1604. offset = 2;
  1605. }
  1606. if (offset)
  1607. {
  1608. rel32 = extract_signed_integer (insn + offset, 4, byte_order);
  1609. newrel = (oldloc - *to) + rel32;
  1610. store_signed_integer (insn + offset, 4, byte_order, newrel);
  1611. displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
  1612. hex_string (rel32), paddress (gdbarch, oldloc),
  1613. hex_string (newrel), paddress (gdbarch, *to));
  1614. }
  1615. /* Write the adjusted instruction into its displaced location. */
  1616. append_insns (to, insn_length, buf);
  1617. }
  1618. /* The maximum number of saved registers. This should include %rip. */
  1619. #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
  1620. struct amd64_frame_cache
  1621. {
  1622. /* Base address. */
  1623. CORE_ADDR base;
  1624. int base_p;
  1625. CORE_ADDR sp_offset;
  1626. CORE_ADDR pc;
  1627. /* Saved registers. */
  1628. CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
  1629. CORE_ADDR saved_sp;
  1630. int saved_sp_reg;
  1631. /* Do we have a frame? */
  1632. int frameless_p;
  1633. };
  1634. /* Initialize a frame cache. */
  1635. static void
  1636. amd64_init_frame_cache (struct amd64_frame_cache *cache)
  1637. {
  1638. int i;
  1639. /* Base address. */
  1640. cache->base = 0;
  1641. cache->base_p = 0;
  1642. cache->sp_offset = -8;
  1643. cache->pc = 0;
  1644. /* Saved registers. We initialize these to -1 since zero is a valid
  1645. offset (that's where %rbp is supposed to be stored).
  1646. The values start out as being offsets, and are later converted to
  1647. addresses (at which point -1 is interpreted as an address, still meaning
  1648. "invalid"). */
  1649. for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
  1650. cache->saved_regs[i] = -1;
  1651. cache->saved_sp = 0;
  1652. cache->saved_sp_reg = -1;
  1653. /* Frameless until proven otherwise. */
  1654. cache->frameless_p = 1;
  1655. }
  1656. /* Allocate and initialize a frame cache. */
  1657. static struct amd64_frame_cache *
  1658. amd64_alloc_frame_cache (void)
  1659. {
  1660. struct amd64_frame_cache *cache;
  1661. cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
  1662. amd64_init_frame_cache (cache);
  1663. return cache;
  1664. }
  1665. /* GCC 4.4 and later, can put code in the prologue to realign the
  1666. stack pointer. Check whether PC points to such code, and update
  1667. CACHE accordingly. Return the first instruction after the code
  1668. sequence or CURRENT_PC, whichever is smaller. If we don't
  1669. recognize the code, return PC. */
  1670. static CORE_ADDR
  1671. amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
  1672. struct amd64_frame_cache *cache)
  1673. {
  1674. /* There are 2 code sequences to re-align stack before the frame
  1675. gets set up:
  1676. 1. Use a caller-saved saved register:
  1677. leaq 8(%rsp), %reg
  1678. andq $-XXX, %rsp
  1679. pushq -8(%reg)
  1680. 2. Use a callee-saved saved register:
  1681. pushq %reg
  1682. leaq 16(%rsp), %reg
  1683. andq $-XXX, %rsp
  1684. pushq -8(%reg)
  1685. "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
  1686. 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
  1687. 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
  1688. */
  1689. gdb_byte buf[18];
  1690. int reg, r;
  1691. int offset, offset_and;
  1692. if (target_read_code (pc, buf, sizeof buf))
  1693. return pc;
  1694. /* Check caller-saved saved register. The first instruction has
  1695. to be "leaq 8(%rsp), %reg". */
  1696. if ((buf[0] & 0xfb) == 0x48
  1697. && buf[1] == 0x8d
  1698. && buf[3] == 0x24
  1699. && buf[4] == 0x8)
  1700. {
  1701. /* MOD must be binary 10 and R/M must be binary 100. */
  1702. if ((buf[2] & 0xc7) != 0x44)
  1703. return pc;
  1704. /* REG has register number. */
  1705. reg = (buf[2] >> 3) & 7;
  1706. /* Check the REX.R bit. */
  1707. if (buf[0] == 0x4c)
  1708. reg += 8;
  1709. offset = 5;
  1710. }
  1711. else
  1712. {
  1713. /* Check callee-saved saved register. The first instruction
  1714. has to be "pushq %reg". */
  1715. reg = 0;
  1716. if ((buf[0] & 0xf8) == 0x50)
  1717. offset = 0;
  1718. else if ((buf[0] & 0xf6) == 0x40
  1719. && (buf[1] & 0xf8) == 0x50)
  1720. {
  1721. /* Check the REX.B bit. */
  1722. if ((buf[0] & 1) != 0)
  1723. reg = 8;
  1724. offset = 1;
  1725. }
  1726. else
  1727. return pc;
  1728. /* Get register. */
  1729. reg += buf[offset] & 0x7;
  1730. offset++;
  1731. /* The next instruction has to be "leaq 16(%rsp), %reg". */
  1732. if ((buf[offset] & 0xfb) != 0x48
  1733. || buf[offset + 1] != 0x8d
  1734. || buf[offset + 3] != 0x24
  1735. || buf[offset + 4] != 0x10)
  1736. return pc;
  1737. /* MOD must be binary 10 and R/M must be binary 100. */
  1738. if ((buf[offset + 2] & 0xc7) != 0x44)
  1739. return pc;
  1740. /* REG has register number. */
  1741. r = (buf[offset + 2] >> 3) & 7;
  1742. /* Check the REX.R bit. */
  1743. if (buf[offset] == 0x4c)
  1744. r += 8;
  1745. /* Registers in pushq and leaq have to be the same. */
  1746. if (reg != r)
  1747. return pc;
  1748. offset += 5;
  1749. }
  1750. /* Rigister can't be %rsp nor %rbp. */
  1751. if (reg == 4 || reg == 5)
  1752. return pc;
  1753. /* The next instruction has to be "andq $-XXX, %rsp". */
  1754. if (buf[offset] != 0x48
  1755. || buf[offset + 2] != 0xe4
  1756. || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
  1757. return pc;
  1758. offset_and = offset;
  1759. offset += buf[offset + 1] == 0x81 ? 7 : 4;
  1760. /* The next instruction has to be "pushq -8(%reg)". */
  1761. r = 0;
  1762. if (buf[offset] == 0xff)
  1763. offset++;
  1764. else if ((buf[offset] & 0xf6) == 0x40
  1765. && buf[offset + 1] == 0xff)
  1766. {
  1767. /* Check the REX.B bit. */
  1768. if ((buf[offset] & 0x1) != 0)
  1769. r = 8;
  1770. offset += 2;
  1771. }
  1772. else
  1773. return pc;
  1774. /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
  1775. 01. */
  1776. if (buf[offset + 1] != 0xf8
  1777. || (buf[offset] & 0xf8) != 0x70)
  1778. return pc;
  1779. /* R/M has register. */
  1780. r += buf[offset] & 7;
  1781. /* Registers in leaq and pushq have to be the same. */
  1782. if (reg != r)
  1783. return pc;
  1784. if (current_pc > pc + offset_and)
  1785. cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
  1786. return std::min (pc + offset + 2, current_pc);
  1787. }
  1788. /* Similar to amd64_analyze_stack_align for x32. */
  1789. static CORE_ADDR
  1790. amd64_x32_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
  1791. struct amd64_frame_cache *cache)
  1792. {
  1793. /* There are 2 code sequences to re-align stack before the frame
  1794. gets set up:
  1795. 1. Use a caller-saved saved register:
  1796. leaq 8(%rsp), %reg
  1797. andq $-XXX, %rsp
  1798. pushq -8(%reg)
  1799. or
  1800. [addr32] leal 8(%rsp), %reg
  1801. andl $-XXX, %esp
  1802. [addr32] pushq -8(%reg)
  1803. 2. Use a callee-saved saved register:
  1804. pushq %reg
  1805. leaq 16(%rsp), %reg
  1806. andq $-XXX, %rsp
  1807. pushq -8(%reg)
  1808. or
  1809. pushq %reg
  1810. [addr32] leal 16(%rsp), %reg
  1811. andl $-XXX, %esp
  1812. [addr32] pushq -8(%reg)
  1813. "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
  1814. 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
  1815. 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
  1816. "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
  1817. 0x83 0xe4 0xf0 andl $-16, %esp
  1818. 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
  1819. */
  1820. gdb_byte buf[19];
  1821. int reg, r;
  1822. int offset, offset_and;
  1823. if (target_read_memory (pc, buf, sizeof buf))
  1824. return pc;
  1825. /* Skip optional addr32 prefix. */
  1826. offset = buf[0] == 0x67 ? 1 : 0;
  1827. /* Check caller-saved saved register. The first instruction has
  1828. to be "leaq 8(%rsp), %reg" or "leal 8(%rsp), %reg". */
  1829. if (((buf[offset] & 0xfb) == 0x48 || (buf[offset] & 0xfb) == 0x40)
  1830. && buf[offset + 1] == 0x8d
  1831. && buf[offset + 3] == 0x24
  1832. && buf[offset + 4] == 0x8)
  1833. {
  1834. /* MOD must be binary 10 and R/M must be binary 100. */
  1835. if ((buf[offset + 2] & 0xc7) != 0x44)
  1836. return pc;
  1837. /* REG has register number. */
  1838. reg = (buf[offset + 2] >> 3) & 7;
  1839. /* Check the REX.R bit. */
  1840. if ((buf[offset] & 0x4) != 0)
  1841. reg += 8;
  1842. offset += 5;
  1843. }
  1844. else
  1845. {
  1846. /* Check callee-saved saved register. The first instruction
  1847. has to be "pushq %reg". */
  1848. reg = 0;
  1849. if ((buf[offset] & 0xf6) == 0x40
  1850. && (buf[offset + 1] & 0xf8) == 0x50)
  1851. {
  1852. /* Check the REX.B bit. */
  1853. if ((buf[offset] & 1) != 0)
  1854. reg = 8;
  1855. offset += 1;
  1856. }
  1857. else if ((buf[offset] & 0xf8) != 0x50)
  1858. return pc;
  1859. /* Get register. */
  1860. reg += buf[offset] & 0x7;
  1861. offset++;
  1862. /* Skip optional addr32 prefix. */
  1863. if (buf[offset] == 0x67)
  1864. offset++;
  1865. /* The next instruction has to be "leaq 16(%rsp), %reg" or
  1866. "leal 16(%rsp), %reg". */
  1867. if (((buf[offset] & 0xfb) != 0x48 && (buf[offset] & 0xfb) != 0x40)
  1868. || buf[offset + 1] != 0x8d
  1869. || buf[offset + 3] != 0x24
  1870. || buf[offset + 4] != 0x10)
  1871. return pc;
  1872. /* MOD must be binary 10 and R/M must be binary 100. */
  1873. if ((buf[offset + 2] & 0xc7) != 0x44)
  1874. return pc;
  1875. /* REG has register number. */
  1876. r = (buf[offset + 2] >> 3) & 7;
  1877. /* Check the REX.R bit. */
  1878. if ((buf[offset] & 0x4) != 0)
  1879. r += 8;
  1880. /* Registers in pushq and leaq have to be the same. */
  1881. if (reg != r)
  1882. return pc;
  1883. offset += 5;
  1884. }
  1885. /* Rigister can't be %rsp nor %rbp. */
  1886. if (reg == 4 || reg == 5)
  1887. return pc;
  1888. /* The next instruction may be "andq $-XXX, %rsp" or
  1889. "andl $-XXX, %esp". */
  1890. if (buf[offset] != 0x48)
  1891. offset--;
  1892. if (buf[offset + 2] != 0xe4
  1893. || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
  1894. return pc;
  1895. offset_and = offset;
  1896. offset += buf[offset + 1] == 0x81 ? 7 : 4;
  1897. /* Skip optional addr32 prefix. */
  1898. if (buf[offset] == 0x67)
  1899. offset++;
  1900. /* The next instruction has to be "pushq -8(%reg)". */
  1901. r = 0;
  1902. if (buf[offset] == 0xff)
  1903. offset++;
  1904. else if ((buf[offset] & 0xf6) == 0x40
  1905. && buf[offset + 1] == 0xff)
  1906. {
  1907. /* Check the REX.B bit. */
  1908. if ((buf[offset] & 0x1) != 0)
  1909. r = 8;
  1910. offset += 2;
  1911. }
  1912. else
  1913. return pc;
  1914. /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
  1915. 01. */
  1916. if (buf[offset + 1] != 0xf8
  1917. || (buf[offset] & 0xf8) != 0x70)
  1918. return pc;
  1919. /* R/M has register. */
  1920. r += buf[offset] & 7;
  1921. /* Registers in leaq and pushq have to be the same. */
  1922. if (reg != r)
  1923. return pc;
  1924. if (current_pc > pc + offset_and)
  1925. cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
  1926. return std::min (pc + offset + 2, current_pc);
  1927. }
  1928. /* Do a limited analysis of the prologue at PC and update CACHE
  1929. accordingly. Bail out early if CURRENT_PC is reached. Return the
  1930. address where the analysis stopped.
  1931. We will handle only functions beginning with:
  1932. pushq %rbp 0x55
  1933. movq %rsp, %rbp 0x48 0x89 0xe5 (or 0x48 0x8b 0xec)
  1934. or (for the X32 ABI):
  1935. pushq %rbp 0x55
  1936. movl %esp, %ebp 0x89 0xe5 (or 0x8b 0xec)
  1937. The `endbr64` instruction can be found before these sequences, and will be
  1938. skipped if found.
  1939. Any function that doesn't start with one of these sequences will be
  1940. assumed to have no prologue and thus no valid frame pointer in
  1941. %rbp. */
  1942. static CORE_ADDR
  1943. amd64_analyze_prologue (struct gdbarch *gdbarch,
  1944. CORE_ADDR pc, CORE_ADDR current_pc,
  1945. struct amd64_frame_cache *cache)
  1946. {
  1947. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1948. /* The `endbr64` instruction. */
  1949. static const gdb_byte endbr64[4] = { 0xf3, 0x0f, 0x1e, 0xfa };
  1950. /* There are two variations of movq %rsp, %rbp. */
  1951. static const gdb_byte mov_rsp_rbp_1[3] = { 0x48, 0x89, 0xe5 };
  1952. static const gdb_byte mov_rsp_rbp_2[3] = { 0x48, 0x8b, 0xec };
  1953. /* Ditto for movl %esp, %ebp. */
  1954. static const gdb_byte mov_esp_ebp_1[2] = { 0x89, 0xe5 };
  1955. static const gdb_byte mov_esp_ebp_2[2] = { 0x8b, 0xec };
  1956. gdb_byte buf[3];
  1957. gdb_byte op;
  1958. if (current_pc <= pc)
  1959. return current_pc;
  1960. if (gdbarch_ptr_bit (gdbarch) == 32)
  1961. pc = amd64_x32_analyze_stack_align (pc, current_pc, cache);
  1962. else
  1963. pc = amd64_analyze_stack_align (pc, current_pc, cache);
  1964. op = read_code_unsigned_integer (pc, 1, byte_order);
  1965. /* Check for the `endbr64` instruction, skip it if found. */
  1966. if (op == endbr64[0])
  1967. {
  1968. read_code (pc + 1, buf, 3);
  1969. if (memcmp (buf, &endbr64[1], 3) == 0)
  1970. pc += 4;
  1971. op = read_code_unsigned_integer (pc, 1, byte_order);
  1972. }
  1973. if (current_pc <= pc)
  1974. return current_pc;
  1975. if (op == 0x55) /* pushq %rbp */
  1976. {
  1977. /* Take into account that we've executed the `pushq %rbp' that
  1978. starts this instruction sequence. */
  1979. cache->saved_regs[AMD64_RBP_REGNUM] = 0;
  1980. cache->sp_offset += 8;
  1981. /* If that's all, return now. */
  1982. if (current_pc <= pc + 1)
  1983. return current_pc;
  1984. read_code (pc + 1, buf, 3);
  1985. /* Check for `movq %rsp, %rbp'. */
  1986. if (memcmp (buf, mov_rsp_rbp_1, 3) == 0
  1987. || memcmp (buf, mov_rsp_rbp_2, 3) == 0)
  1988. {
  1989. /* OK, we actually have a frame. */
  1990. cache->frameless_p = 0;
  1991. return pc + 4;
  1992. }
  1993. /* For X32, also check for `movl %esp, %ebp'. */
  1994. if (gdbarch_ptr_bit (gdbarch) == 32)
  1995. {
  1996. if (memcmp (buf, mov_esp_ebp_1, 2) == 0
  1997. || memcmp (buf, mov_esp_ebp_2, 2) == 0)
  1998. {
  1999. /* OK, we actually have a frame. */
  2000. cache->frameless_p = 0;
  2001. return pc + 3;
  2002. }
  2003. }
  2004. return pc + 1;
  2005. }
  2006. return pc;
  2007. }
  2008. /* Work around false termination of prologue - GCC PR debug/48827.
  2009. START_PC is the first instruction of a function, PC is its minimal already
  2010. determined advanced address. Function returns PC if it has nothing to do.
  2011. 84 c0 test %al,%al
  2012. 74 23 je after
  2013. <-- here is 0 lines advance - the false prologue end marker.
  2014. 0f 29 85 70 ff ff ff movaps %xmm0,-0x90(%rbp)
  2015. 0f 29 4d 80 movaps %xmm1,-0x80(%rbp)
  2016. 0f 29 55 90 movaps %xmm2,-0x70(%rbp)
  2017. 0f 29 5d a0 movaps %xmm3,-0x60(%rbp)
  2018. 0f 29 65 b0 movaps %xmm4,-0x50(%rbp)
  2019. 0f 29 6d c0 movaps %xmm5,-0x40(%rbp)
  2020. 0f 29 75 d0 movaps %xmm6,-0x30(%rbp)
  2021. 0f 29 7d e0 movaps %xmm7,-0x20(%rbp)
  2022. after: */
  2023. static CORE_ADDR
  2024. amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
  2025. {
  2026. struct symtab_and_line start_pc_sal, next_sal;
  2027. gdb_byte buf[4 + 8 * 7];
  2028. int offset, xmmreg;
  2029. if (pc == start_pc)
  2030. return pc;
  2031. start_pc_sal = find_pc_sect_line (start_pc, NULL, 0);
  2032. if (start_pc_sal.symtab == NULL
  2033. || producer_is_gcc_ge_4 (start_pc_sal.symtab->compunit ()
  2034. ->producer ()) < 6
  2035. || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
  2036. return pc;
  2037. next_sal = find_pc_sect_line (start_pc_sal.end, NULL, 0);
  2038. if (next_sal.line != start_pc_sal.line)
  2039. return pc;
  2040. /* START_PC can be from overlayed memory, ignored here. */
  2041. if (target_read_code (next_sal.pc - 4, buf, sizeof (buf)) != 0)
  2042. return pc;
  2043. /* test %al,%al */
  2044. if (buf[0] != 0x84 || buf[1] != 0xc0)
  2045. return pc;
  2046. /* je AFTER */
  2047. if (buf[2] != 0x74)
  2048. return pc;
  2049. offset = 4;
  2050. for (xmmreg = 0; xmmreg < 8; xmmreg++)
  2051. {
  2052. /* 0x0f 0x29 0b??000101 movaps %xmmreg?,-0x??(%rbp) */
  2053. if (buf[offset] != 0x0f || buf[offset + 1] != 0x29
  2054. || (buf[offset + 2] & 0x3f) != (xmmreg << 3 | 0x5))
  2055. return pc;
  2056. /* 0b01?????? */
  2057. if ((buf[offset + 2] & 0xc0) == 0x40)
  2058. {
  2059. /* 8-bit displacement. */
  2060. offset += 4;
  2061. }
  2062. /* 0b10?????? */
  2063. else if ((buf[offset + 2] & 0xc0) == 0x80)
  2064. {
  2065. /* 32-bit displacement. */
  2066. offset += 7;
  2067. }
  2068. else
  2069. return pc;
  2070. }
  2071. /* je AFTER */
  2072. if (offset - 4 != buf[3])
  2073. return pc;
  2074. return next_sal.end;
  2075. }
  2076. /* Return PC of first real instruction. */
  2077. static CORE_ADDR
  2078. amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  2079. {
  2080. struct amd64_frame_cache cache;
  2081. CORE_ADDR pc;
  2082. CORE_ADDR func_addr;
  2083. if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
  2084. {
  2085. CORE_ADDR post_prologue_pc
  2086. = skip_prologue_using_sal (gdbarch, func_addr);
  2087. struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
  2088. /* LLVM backend (Clang/Flang) always emits a line note before the
  2089. prologue and another one after. We trust clang and newer Intel
  2090. compilers to emit usable line notes. */
  2091. if (post_prologue_pc
  2092. && (cust != NULL
  2093. && cust->producer () != nullptr
  2094. && (producer_is_llvm (cust->producer ())
  2095. || producer_is_icc_ge_19 (cust->producer ()))))
  2096. return std::max (start_pc, post_prologue_pc);
  2097. }
  2098. amd64_init_frame_cache (&cache);
  2099. pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
  2100. &cache);
  2101. if (cache.frameless_p)
  2102. return start_pc;
  2103. return amd64_skip_xmm_prologue (pc, start_pc);
  2104. }
  2105. /* Normal frames. */
  2106. static void
  2107. amd64_frame_cache_1 (struct frame_info *this_frame,
  2108. struct amd64_frame_cache *cache)
  2109. {
  2110. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2111. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2112. gdb_byte buf[8];
  2113. int i;
  2114. cache->pc = get_frame_func (this_frame);
  2115. if (cache->pc != 0)
  2116. amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
  2117. cache);
  2118. if (cache->frameless_p)
  2119. {
  2120. /* We didn't find a valid frame. If we're at the start of a
  2121. function, or somewhere half-way its prologue, the function's
  2122. frame probably hasn't been fully setup yet. Try to
  2123. reconstruct the base address for the stack frame by looking
  2124. at the stack pointer. For truly "frameless" functions this
  2125. might work too. */
  2126. if (cache->saved_sp_reg != -1)
  2127. {
  2128. /* Stack pointer has been saved. */
  2129. get_frame_register (this_frame, cache->saved_sp_reg, buf);
  2130. cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);
  2131. /* We're halfway aligning the stack. */
  2132. cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
  2133. cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;
  2134. /* This will be added back below. */
  2135. cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
  2136. }
  2137. else
  2138. {
  2139. get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
  2140. cache->base = extract_unsigned_integer (buf, 8, byte_order)
  2141. + cache->sp_offset;
  2142. }
  2143. }
  2144. else
  2145. {
  2146. get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
  2147. cache->base = extract_unsigned_integer (buf, 8, byte_order);
  2148. }
  2149. /* Now that we have the base address for the stack frame we can
  2150. calculate the value of %rsp in the calling frame. */
  2151. cache->saved_sp = cache->base + 16;
  2152. /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
  2153. frame we find it at the same offset from the reconstructed base
  2154. address. If we're halfway aligning the stack, %rip is handled
  2155. differently (see above). */
  2156. if (!cache->frameless_p || cache->saved_sp_reg == -1)
  2157. cache->saved_regs[AMD64_RIP_REGNUM] = 8;
  2158. /* Adjust all the saved registers such that they contain addresses
  2159. instead of offsets. */
  2160. for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
  2161. if (cache->saved_regs[i] != -1)
  2162. cache->saved_regs[i] += cache->base;
  2163. cache->base_p = 1;
  2164. }
  2165. static struct amd64_frame_cache *
  2166. amd64_frame_cache (struct frame_info *this_frame, void **this_cache)
  2167. {
  2168. struct amd64_frame_cache *cache;
  2169. if (*this_cache)
  2170. return (struct amd64_frame_cache *) *this_cache;
  2171. cache = amd64_alloc_frame_cache ();
  2172. *this_cache = cache;
  2173. try
  2174. {
  2175. amd64_frame_cache_1 (this_frame, cache);
  2176. }
  2177. catch (const gdb_exception_error &ex)
  2178. {
  2179. if (ex.error != NOT_AVAILABLE_ERROR)
  2180. throw;
  2181. }
  2182. return cache;
  2183. }
  2184. static enum unwind_stop_reason
  2185. amd64_frame_unwind_stop_reason (struct frame_info *this_frame,
  2186. void **this_cache)
  2187. {
  2188. struct amd64_frame_cache *cache =
  2189. amd64_frame_cache (this_frame, this_cache);
  2190. if (!cache->base_p)
  2191. return UNWIND_UNAVAILABLE;
  2192. /* This marks the outermost frame. */
  2193. if (cache->base == 0)
  2194. return UNWIND_OUTERMOST;
  2195. return UNWIND_NO_REASON;
  2196. }
  2197. static void
  2198. amd64_frame_this_id (struct frame_info *this_frame, void **this_cache,
  2199. struct frame_id *this_id)
  2200. {
  2201. struct amd64_frame_cache *cache =
  2202. amd64_frame_cache (this_frame, this_cache);
  2203. if (!cache->base_p)
  2204. (*this_id) = frame_id_build_unavailable_stack (cache->pc);
  2205. else if (cache->base == 0)
  2206. {
  2207. /* This marks the outermost frame. */
  2208. return;
  2209. }
  2210. else
  2211. (*this_id) = frame_id_build (cache->base + 16, cache->pc);
  2212. }
  2213. static struct value *
  2214. amd64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  2215. int regnum)
  2216. {
  2217. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2218. struct amd64_frame_cache *cache =
  2219. amd64_frame_cache (this_frame, this_cache);
  2220. gdb_assert (regnum >= 0);
  2221. if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
  2222. return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
  2223. if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
  2224. return frame_unwind_got_memory (this_frame, regnum,
  2225. cache->saved_regs[regnum]);
  2226. return frame_unwind_got_register (this_frame, regnum, regnum);
  2227. }
  2228. static const struct frame_unwind amd64_frame_unwind =
  2229. {
  2230. "amd64 prologue",
  2231. NORMAL_FRAME,
  2232. amd64_frame_unwind_stop_reason,
  2233. amd64_frame_this_id,
  2234. amd64_frame_prev_register,
  2235. NULL,
  2236. default_frame_sniffer
  2237. };
  2238. /* Generate a bytecode expression to get the value of the saved PC. */
  2239. static void
  2240. amd64_gen_return_address (struct gdbarch *gdbarch,
  2241. struct agent_expr *ax, struct axs_value *value,
  2242. CORE_ADDR scope)
  2243. {
  2244. /* The following sequence assumes the traditional use of the base
  2245. register. */
  2246. ax_reg (ax, AMD64_RBP_REGNUM);
  2247. ax_const_l (ax, 8);
  2248. ax_simple (ax, aop_add);
  2249. value->type = register_type (gdbarch, AMD64_RIP_REGNUM);
  2250. value->kind = axs_lvalue_memory;
  2251. }
  2252. /* Signal trampolines. */
  2253. /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
  2254. 64-bit variants. This would require using identical frame caches
  2255. on both platforms. */
  2256. static struct amd64_frame_cache *
  2257. amd64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
  2258. {
  2259. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2260. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2261. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2262. struct amd64_frame_cache *cache;
  2263. CORE_ADDR addr;
  2264. gdb_byte buf[8];
  2265. int i;
  2266. if (*this_cache)
  2267. return (struct amd64_frame_cache *) *this_cache;
  2268. cache = amd64_alloc_frame_cache ();
  2269. try
  2270. {
  2271. get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
  2272. cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;
  2273. addr = tdep->sigcontext_addr (this_frame);
  2274. gdb_assert (tdep->sc_reg_offset);
  2275. gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
  2276. for (i = 0; i < tdep->sc_num_regs; i++)
  2277. if (tdep->sc_reg_offset[i] != -1)
  2278. cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
  2279. cache->base_p = 1;
  2280. }
  2281. catch (const gdb_exception_error &ex)
  2282. {
  2283. if (ex.error != NOT_AVAILABLE_ERROR)
  2284. throw;
  2285. }
  2286. *this_cache = cache;
  2287. return cache;
  2288. }
  2289. static enum unwind_stop_reason
  2290. amd64_sigtramp_frame_unwind_stop_reason (struct frame_info *this_frame,
  2291. void **this_cache)
  2292. {
  2293. struct amd64_frame_cache *cache =
  2294. amd64_sigtramp_frame_cache (this_frame, this_cache);
  2295. if (!cache->base_p)
  2296. return UNWIND_UNAVAILABLE;
  2297. return UNWIND_NO_REASON;
  2298. }
  2299. static void
  2300. amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
  2301. void **this_cache, struct frame_id *this_id)
  2302. {
  2303. struct amd64_frame_cache *cache =
  2304. amd64_sigtramp_frame_cache (this_frame, this_cache);
  2305. if (!cache->base_p)
  2306. (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
  2307. else if (cache->base == 0)
  2308. {
  2309. /* This marks the outermost frame. */
  2310. return;
  2311. }
  2312. else
  2313. (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
  2314. }
  2315. static struct value *
  2316. amd64_sigtramp_frame_prev_register (struct frame_info *this_frame,
  2317. void **this_cache, int regnum)
  2318. {
  2319. /* Make sure we've initialized the cache. */
  2320. amd64_sigtramp_frame_cache (this_frame, this_cache);
  2321. return amd64_frame_prev_register (this_frame, this_cache, regnum);
  2322. }
  2323. static int
  2324. amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
  2325. struct frame_info *this_frame,
  2326. void **this_cache)
  2327. {
  2328. gdbarch *arch = get_frame_arch (this_frame);
  2329. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (arch);
  2330. /* We shouldn't even bother if we don't have a sigcontext_addr
  2331. handler. */
  2332. if (tdep->sigcontext_addr == NULL)
  2333. return 0;
  2334. if (tdep->sigtramp_p != NULL)
  2335. {
  2336. if (tdep->sigtramp_p (this_frame))
  2337. return 1;
  2338. }
  2339. if (tdep->sigtramp_start != 0)
  2340. {
  2341. CORE_ADDR pc = get_frame_pc (this_frame);
  2342. gdb_assert (tdep->sigtramp_end != 0);
  2343. if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
  2344. return 1;
  2345. }
  2346. return 0;
  2347. }
  2348. static const struct frame_unwind amd64_sigtramp_frame_unwind =
  2349. {
  2350. "amd64 sigtramp",
  2351. SIGTRAMP_FRAME,
  2352. amd64_sigtramp_frame_unwind_stop_reason,
  2353. amd64_sigtramp_frame_this_id,
  2354. amd64_sigtramp_frame_prev_register,
  2355. NULL,
  2356. amd64_sigtramp_frame_sniffer
  2357. };
  2358. static CORE_ADDR
  2359. amd64_frame_base_address (struct frame_info *this_frame, void **this_cache)
  2360. {
  2361. struct amd64_frame_cache *cache =
  2362. amd64_frame_cache (this_frame, this_cache);
  2363. return cache->base;
  2364. }
  2365. static const struct frame_base amd64_frame_base =
  2366. {
  2367. &amd64_frame_unwind,
  2368. amd64_frame_base_address,
  2369. amd64_frame_base_address,
  2370. amd64_frame_base_address
  2371. };
  2372. /* Normal frames, but in a function epilogue. */
  2373. /* Implement the stack_frame_destroyed_p gdbarch method.
  2374. The epilogue is defined here as the 'ret' instruction, which will
  2375. follow any instruction such as 'leave' or 'pop %ebp' that destroys
  2376. the function's stack frame. */
  2377. static int
  2378. amd64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  2379. {
  2380. gdb_byte insn;
  2381. struct compunit_symtab *cust;
  2382. cust = find_pc_compunit_symtab (pc);
  2383. if (cust != NULL && cust->epilogue_unwind_valid ())
  2384. return 0;
  2385. if (target_read_memory (pc, &insn, 1))
  2386. return 0; /* Can't read memory at pc. */
  2387. if (insn != 0xc3) /* 'ret' instruction. */
  2388. return 0;
  2389. return 1;
  2390. }
  2391. static int
  2392. amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
  2393. struct frame_info *this_frame,
  2394. void **this_prologue_cache)
  2395. {
  2396. if (frame_relative_level (this_frame) == 0)
  2397. return amd64_stack_frame_destroyed_p (get_frame_arch (this_frame),
  2398. get_frame_pc (this_frame));
  2399. else
  2400. return 0;
  2401. }
  2402. static struct amd64_frame_cache *
  2403. amd64_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
  2404. {
  2405. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  2406. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2407. struct amd64_frame_cache *cache;
  2408. gdb_byte buf[8];
  2409. if (*this_cache)
  2410. return (struct amd64_frame_cache *) *this_cache;
  2411. cache = amd64_alloc_frame_cache ();
  2412. *this_cache = cache;
  2413. try
  2414. {
  2415. /* Cache base will be %esp plus cache->sp_offset (-8). */
  2416. get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
  2417. cache->base = extract_unsigned_integer (buf, 8,
  2418. byte_order) + cache->sp_offset;
  2419. /* Cache pc will be the frame func. */
  2420. cache->pc = get_frame_pc (this_frame);
  2421. /* The saved %esp will be at cache->base plus 16. */
  2422. cache->saved_sp = cache->base + 16;
  2423. /* The saved %eip will be at cache->base plus 8. */
  2424. cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;
  2425. cache->base_p = 1;
  2426. }
  2427. catch (const gdb_exception_error &ex)
  2428. {
  2429. if (ex.error != NOT_AVAILABLE_ERROR)
  2430. throw;
  2431. }
  2432. return cache;
  2433. }
  2434. static enum unwind_stop_reason
  2435. amd64_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
  2436. void **this_cache)
  2437. {
  2438. struct amd64_frame_cache *cache
  2439. = amd64_epilogue_frame_cache (this_frame, this_cache);
  2440. if (!cache->base_p)
  2441. return UNWIND_UNAVAILABLE;
  2442. return UNWIND_NO_REASON;
  2443. }
  2444. static void
  2445. amd64_epilogue_frame_this_id (struct frame_info *this_frame,
  2446. void **this_cache,
  2447. struct frame_id *this_id)
  2448. {
  2449. struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
  2450. this_cache);
  2451. if (!cache->base_p)
  2452. (*this_id) = frame_id_build_unavailable_stack (cache->pc);
  2453. else
  2454. (*this_id) = frame_id_build (cache->base + 8, cache->pc);
  2455. }
  2456. static const struct frame_unwind amd64_epilogue_frame_unwind =
  2457. {
  2458. "amd64 epilogue",
  2459. NORMAL_FRAME,
  2460. amd64_epilogue_frame_unwind_stop_reason,
  2461. amd64_epilogue_frame_this_id,
  2462. amd64_frame_prev_register,
  2463. NULL,
  2464. amd64_epilogue_frame_sniffer
  2465. };
  2466. static struct frame_id
  2467. amd64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2468. {
  2469. CORE_ADDR fp;
  2470. fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);
  2471. return frame_id_build (fp + 16, get_frame_pc (this_frame));
  2472. }
  2473. /* 16 byte align the SP per frame requirements. */
  2474. static CORE_ADDR
  2475. amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  2476. {
  2477. return sp & -(CORE_ADDR)16;
  2478. }
  2479. /* Supply register REGNUM from the buffer specified by FPREGS and LEN
  2480. in the floating-point register set REGSET to register cache
  2481. REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
  2482. static void
  2483. amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
  2484. int regnum, const void *fpregs, size_t len)
  2485. {
  2486. struct gdbarch *gdbarch = regcache->arch ();
  2487. const i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2488. gdb_assert (len >= tdep->sizeof_fpregset);
  2489. amd64_supply_fxsave (regcache, regnum, fpregs);
  2490. }
  2491. /* Collect register REGNUM from the register cache REGCACHE and store
  2492. it in the buffer specified by FPREGS and LEN as described by the
  2493. floating-point register set REGSET. If REGNUM is -1, do this for
  2494. all registers in REGSET. */
  2495. static void
  2496. amd64_collect_fpregset (const struct regset *regset,
  2497. const struct regcache *regcache,
  2498. int regnum, void *fpregs, size_t len)
  2499. {
  2500. struct gdbarch *gdbarch = regcache->arch ();
  2501. const i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2502. gdb_assert (len >= tdep->sizeof_fpregset);
  2503. amd64_collect_fxsave (regcache, regnum, fpregs);
  2504. }
  2505. const struct regset amd64_fpregset =
  2506. {
  2507. NULL, amd64_supply_fpregset, amd64_collect_fpregset
  2508. };
  2509. /* Figure out where the longjmp will land. Slurp the jmp_buf out of
  2510. %rdi. We expect its value to be a pointer to the jmp_buf structure
  2511. from which we extract the address that we will land at. This
  2512. address is copied into PC. This routine returns non-zero on
  2513. success. */
  2514. static int
  2515. amd64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  2516. {
  2517. gdb_byte buf[8];
  2518. CORE_ADDR jb_addr;
  2519. struct gdbarch *gdbarch = get_frame_arch (frame);
  2520. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2521. int jb_pc_offset = tdep->jb_pc_offset;
  2522. int len = TYPE_LENGTH (builtin_type (gdbarch)->builtin_func_ptr);
  2523. /* If JB_PC_OFFSET is -1, we have no way to find out where the
  2524. longjmp will land. */
  2525. if (jb_pc_offset == -1)
  2526. return 0;
  2527. get_frame_register (frame, AMD64_RDI_REGNUM, buf);
  2528. jb_addr= extract_typed_address
  2529. (buf, builtin_type (gdbarch)->builtin_data_ptr);
  2530. if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
  2531. return 0;
  2532. *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
  2533. return 1;
  2534. }
  2535. static const int amd64_record_regmap[] =
  2536. {
  2537. AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
  2538. AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
  2539. AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
  2540. AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
  2541. AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
  2542. AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
  2543. };
  2544. /* Implement the "in_indirect_branch_thunk" gdbarch function. */
  2545. static bool
  2546. amd64_in_indirect_branch_thunk (struct gdbarch *gdbarch, CORE_ADDR pc)
  2547. {
  2548. return x86_in_indirect_branch_thunk (pc, amd64_register_names,
  2549. AMD64_RAX_REGNUM,
  2550. AMD64_RIP_REGNUM);
  2551. }
  2552. void
  2553. amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
  2554. const target_desc *default_tdesc)
  2555. {
  2556. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2557. const struct target_desc *tdesc = info.target_desc;
  2558. static const char *const stap_integer_prefixes[] = { "$", NULL };
  2559. static const char *const stap_register_prefixes[] = { "%", NULL };
  2560. static const char *const stap_register_indirection_prefixes[] = { "(",
  2561. NULL };
  2562. static const char *const stap_register_indirection_suffixes[] = { ")",
  2563. NULL };
  2564. /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
  2565. floating-point registers. */
  2566. tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
  2567. tdep->fpregset = &amd64_fpregset;
  2568. if (! tdesc_has_registers (tdesc))
  2569. tdesc = default_tdesc;
  2570. tdep->tdesc = tdesc;
  2571. tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
  2572. tdep->register_names = amd64_register_names;
  2573. if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512") != NULL)
  2574. {
  2575. tdep->zmmh_register_names = amd64_zmmh_names;
  2576. tdep->k_register_names = amd64_k_names;
  2577. tdep->xmm_avx512_register_names = amd64_xmm_avx512_names;
  2578. tdep->ymm16h_register_names = amd64_ymmh_avx512_names;
  2579. tdep->num_zmm_regs = 32;
  2580. tdep->num_xmm_avx512_regs = 16;
  2581. tdep->num_ymm_avx512_regs = 16;
  2582. tdep->zmm0h_regnum = AMD64_ZMM0H_REGNUM;
  2583. tdep->k0_regnum = AMD64_K0_REGNUM;
  2584. tdep->xmm16_regnum = AMD64_XMM16_REGNUM;
  2585. tdep->ymm16h_regnum = AMD64_YMM16H_REGNUM;
  2586. }
  2587. if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
  2588. {
  2589. tdep->ymmh_register_names = amd64_ymmh_names;
  2590. tdep->num_ymm_regs = 16;
  2591. tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
  2592. }
  2593. if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx") != NULL)
  2594. {
  2595. tdep->mpx_register_names = amd64_mpx_names;
  2596. tdep->bndcfgu_regnum = AMD64_BNDCFGU_REGNUM;
  2597. tdep->bnd0r_regnum = AMD64_BND0R_REGNUM;
  2598. }
  2599. if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments") != NULL)
  2600. {
  2601. tdep->fsbase_regnum = AMD64_FSBASE_REGNUM;
  2602. }
  2603. if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys") != NULL)
  2604. {
  2605. tdep->pkeys_register_names = amd64_pkeys_names;
  2606. tdep->pkru_regnum = AMD64_PKRU_REGNUM;
  2607. tdep->num_pkeys_regs = 1;
  2608. }
  2609. tdep->num_byte_regs = 20;
  2610. tdep->num_word_regs = 16;
  2611. tdep->num_dword_regs = 16;
  2612. /* Avoid wiring in the MMX registers for now. */
  2613. tdep->num_mmx_regs = 0;
  2614. set_gdbarch_pseudo_register_read_value (gdbarch,
  2615. amd64_pseudo_register_read_value);
  2616. set_gdbarch_pseudo_register_write (gdbarch,
  2617. amd64_pseudo_register_write);
  2618. set_gdbarch_ax_pseudo_register_collect (gdbarch,
  2619. amd64_ax_pseudo_register_collect);
  2620. set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);
  2621. /* AMD64 has an FPU and 16 SSE registers. */
  2622. tdep->st0_regnum = AMD64_ST0_REGNUM;
  2623. tdep->num_xmm_regs = 16;
  2624. /* This is what all the fuss is about. */
  2625. set_gdbarch_long_bit (gdbarch, 64);
  2626. set_gdbarch_long_long_bit (gdbarch, 64);
  2627. set_gdbarch_ptr_bit (gdbarch, 64);
  2628. /* In contrast to the i386, on AMD64 a `long double' actually takes
  2629. up 128 bits, even though it's still based on the i387 extended
  2630. floating-point format which has only 80 significant bits. */
  2631. set_gdbarch_long_double_bit (gdbarch, 128);
  2632. set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
  2633. /* Register numbers of various important registers. */
  2634. set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
  2635. set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
  2636. set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
  2637. set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
  2638. /* The "default" register numbering scheme for AMD64 is referred to
  2639. as the "DWARF Register Number Mapping" in the System V psABI.
  2640. The preferred debugging format for all known AMD64 targets is
  2641. actually DWARF2, and GCC doesn't seem to support DWARF (that is
  2642. DWARF-1), but we provide the same mapping just in case. This
  2643. mapping is also used for stabs, which GCC does support. */
  2644. set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
  2645. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
  2646. /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
  2647. be in use on any of the supported AMD64 targets. */
  2648. /* Call dummy code. */
  2649. set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
  2650. set_gdbarch_frame_align (gdbarch, amd64_frame_align);
  2651. set_gdbarch_frame_red_zone_size (gdbarch, 128);
  2652. set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
  2653. set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
  2654. set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
  2655. set_gdbarch_return_value (gdbarch, amd64_return_value);
  2656. set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
  2657. tdep->record_regmap = amd64_record_regmap;
  2658. set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);
  2659. /* Hook the function epilogue frame unwinder. This unwinder is
  2660. appended to the list first, so that it supercedes the other
  2661. unwinders in function epilogues. */
  2662. frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_frame_unwind);
  2663. /* Hook the prologue-based frame unwinders. */
  2664. frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
  2665. frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
  2666. frame_base_set_default (gdbarch, &amd64_frame_base);
  2667. set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);
  2668. set_gdbarch_relocate_instruction (gdbarch, amd64_relocate_instruction);
  2669. set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);
  2670. /* SystemTap variables and functions. */
  2671. set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
  2672. set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
  2673. set_gdbarch_stap_register_indirection_prefixes (gdbarch,
  2674. stap_register_indirection_prefixes);
  2675. set_gdbarch_stap_register_indirection_suffixes (gdbarch,
  2676. stap_register_indirection_suffixes);
  2677. set_gdbarch_stap_is_single_operand (gdbarch,
  2678. i386_stap_is_single_operand);
  2679. set_gdbarch_stap_parse_special_token (gdbarch,
  2680. i386_stap_parse_special_token);
  2681. set_gdbarch_insn_is_call (gdbarch, amd64_insn_is_call);
  2682. set_gdbarch_insn_is_ret (gdbarch, amd64_insn_is_ret);
  2683. set_gdbarch_insn_is_jump (gdbarch, amd64_insn_is_jump);
  2684. set_gdbarch_in_indirect_branch_thunk (gdbarch,
  2685. amd64_in_indirect_branch_thunk);
  2686. register_amd64_ravenscar_ops (gdbarch);
  2687. }
  2688. /* Initialize ARCH for x86-64, no osabi. */
  2689. static void
  2690. amd64_none_init_abi (gdbarch_info info, gdbarch *arch)
  2691. {
  2692. amd64_init_abi (info, arch, amd64_target_description (X86_XSTATE_SSE_MASK,
  2693. true));
  2694. }
  2695. static struct type *
  2696. amd64_x32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
  2697. {
  2698. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2699. switch (regnum - tdep->eax_regnum)
  2700. {
  2701. case AMD64_RBP_REGNUM: /* %ebp */
  2702. case AMD64_RSP_REGNUM: /* %esp */
  2703. return builtin_type (gdbarch)->builtin_data_ptr;
  2704. case AMD64_RIP_REGNUM: /* %eip */
  2705. return builtin_type (gdbarch)->builtin_func_ptr;
  2706. }
  2707. return i386_pseudo_register_type (gdbarch, regnum);
  2708. }
  2709. void
  2710. amd64_x32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
  2711. const target_desc *default_tdesc)
  2712. {
  2713. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2714. amd64_init_abi (info, gdbarch, default_tdesc);
  2715. tdep->num_dword_regs = 17;
  2716. set_tdesc_pseudo_register_type (gdbarch, amd64_x32_pseudo_register_type);
  2717. set_gdbarch_long_bit (gdbarch, 32);
  2718. set_gdbarch_ptr_bit (gdbarch, 32);
  2719. }
  2720. /* Initialize ARCH for x64-32, no osabi. */
  2721. static void
  2722. amd64_x32_none_init_abi (gdbarch_info info, gdbarch *arch)
  2723. {
  2724. amd64_x32_init_abi (info, arch,
  2725. amd64_target_description (X86_XSTATE_SSE_MASK, true));
  2726. }
  2727. /* Return the target description for a specified XSAVE feature mask. */
  2728. const struct target_desc *
  2729. amd64_target_description (uint64_t xcr0, bool segments)
  2730. {
  2731. static target_desc *amd64_tdescs \
  2732. [2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/][2/*segments*/] = {};
  2733. target_desc **tdesc;
  2734. tdesc = &amd64_tdescs[(xcr0 & X86_XSTATE_AVX) ? 1 : 0]
  2735. [(xcr0 & X86_XSTATE_MPX) ? 1 : 0]
  2736. [(xcr0 & X86_XSTATE_AVX512) ? 1 : 0]
  2737. [(xcr0 & X86_XSTATE_PKRU) ? 1 : 0]
  2738. [segments ? 1 : 0];
  2739. if (*tdesc == NULL)
  2740. *tdesc = amd64_create_target_description (xcr0, false, false,
  2741. segments);
  2742. return *tdesc;
  2743. }
  2744. void _initialize_amd64_tdep ();
  2745. void
  2746. _initialize_amd64_tdep ()
  2747. {
  2748. gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_NONE,
  2749. amd64_none_init_abi);
  2750. gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x64_32, GDB_OSABI_NONE,
  2751. amd64_x32_none_init_abi);
  2752. }
  2753. /* The 64-bit FXSAVE format differs from the 32-bit format in the
  2754. sense that the instruction pointer and data pointer are simply
  2755. 64-bit offsets into the code segment and the data segment instead
  2756. of a selector offset pair. The functions below store the upper 32
  2757. bits of these pointers (instead of just the 16-bits of the segment
  2758. selector). */
  2759. /* Fill register REGNUM in REGCACHE with the appropriate
  2760. floating-point or SSE register value from *FXSAVE. If REGNUM is
  2761. -1, do this for all registers. This function masks off any of the
  2762. reserved bits in *FXSAVE. */
  2763. void
  2764. amd64_supply_fxsave (struct regcache *regcache, int regnum,
  2765. const void *fxsave)
  2766. {
  2767. struct gdbarch *gdbarch = regcache->arch ();
  2768. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2769. i387_supply_fxsave (regcache, regnum, fxsave);
  2770. if (fxsave
  2771. && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2772. {
  2773. const gdb_byte *regs = (const gdb_byte *) fxsave;
  2774. if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2775. regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
  2776. if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2777. regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
  2778. }
  2779. }
  2780. /* Similar to amd64_supply_fxsave, but use XSAVE extended state. */
  2781. void
  2782. amd64_supply_xsave (struct regcache *regcache, int regnum,
  2783. const void *xsave)
  2784. {
  2785. struct gdbarch *gdbarch = regcache->arch ();
  2786. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2787. i387_supply_xsave (regcache, regnum, xsave);
  2788. if (xsave
  2789. && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2790. {
  2791. const gdb_byte *regs = (const gdb_byte *) xsave;
  2792. ULONGEST clear_bv;
  2793. clear_bv = i387_xsave_get_clear_bv (gdbarch, xsave);
  2794. /* If the FISEG and FOSEG registers have not been initialised yet
  2795. (their CLEAR_BV bit is set) then their default values of zero will
  2796. have already been setup by I387_SUPPLY_XSAVE. */
  2797. if (!(clear_bv & X86_XSTATE_X87))
  2798. {
  2799. if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2800. regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
  2801. if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2802. regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
  2803. }
  2804. }
  2805. }
  2806. /* Fill register REGNUM (if it is a floating-point or SSE register) in
  2807. *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
  2808. all registers. This function doesn't touch any of the reserved
  2809. bits in *FXSAVE. */
  2810. void
  2811. amd64_collect_fxsave (const struct regcache *regcache, int regnum,
  2812. void *fxsave)
  2813. {
  2814. struct gdbarch *gdbarch = regcache->arch ();
  2815. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2816. gdb_byte *regs = (gdb_byte *) fxsave;
  2817. i387_collect_fxsave (regcache, regnum, fxsave);
  2818. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2819. {
  2820. if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2821. regcache->raw_collect (I387_FISEG_REGNUM (tdep), regs + 12);
  2822. if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2823. regcache->raw_collect (I387_FOSEG_REGNUM (tdep), regs + 20);
  2824. }
  2825. }
  2826. /* Similar to amd64_collect_fxsave, but use XSAVE extended state. */
  2827. void
  2828. amd64_collect_xsave (const struct regcache *regcache, int regnum,
  2829. void *xsave, int gcore)
  2830. {
  2831. struct gdbarch *gdbarch = regcache->arch ();
  2832. i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2833. gdb_byte *regs = (gdb_byte *) xsave;
  2834. i387_collect_xsave (regcache, regnum, xsave, gcore);
  2835. if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
  2836. {
  2837. if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
  2838. regcache->raw_collect (I387_FISEG_REGNUM (tdep),
  2839. regs + 12);
  2840. if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
  2841. regcache->raw_collect (I387_FOSEG_REGNUM (tdep),
  2842. regs + 20);
  2843. }
  2844. }