GDB (API)
/home/stan/gdb/src/gdb/aarch64-tdep.c
Go to the documentation of this file.
00001 /* Common target dependent code for GDB on AArch64 systems.
00002 
00003    Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004    Contributed by ARM Ltd.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 
00023 #include "frame.h"
00024 #include "inferior.h"
00025 #include "gdbcmd.h"
00026 #include "gdbcore.h"
00027 #include "gdb_string.h"
00028 #include "dis-asm.h"
00029 #include "regcache.h"
00030 #include "reggroups.h"
00031 #include "doublest.h"
00032 #include "value.h"
00033 #include "arch-utils.h"
00034 #include "osabi.h"
00035 #include "frame-unwind.h"
00036 #include "frame-base.h"
00037 #include "trad-frame.h"
00038 #include "objfiles.h"
00039 #include "dwarf2-frame.h"
00040 #include "gdbtypes.h"
00041 #include "prologue-value.h"
00042 #include "target-descriptions.h"
00043 #include "user-regs.h"
00044 #include "language.h"
00045 #include "infcall.h"
00046 
00047 #include "aarch64-tdep.h"
00048 
00049 #include "elf-bfd.h"
00050 #include "elf/aarch64.h"
00051 
00052 #include "gdb_assert.h"
00053 #include "vec.h"
00054 
00055 #include "features/aarch64.c"
00056 
00057 /* Pseudo register base numbers.  */
00058 #define AARCH64_Q0_REGNUM 0
00059 #define AARCH64_D0_REGNUM (AARCH64_Q0_REGNUM + 32)
00060 #define AARCH64_S0_REGNUM (AARCH64_D0_REGNUM + 32)
00061 #define AARCH64_H0_REGNUM (AARCH64_S0_REGNUM + 32)
00062 #define AARCH64_B0_REGNUM (AARCH64_H0_REGNUM + 32)
00063 
00064 /* The standard register names, and all the valid aliases for them.  */
00065 static const struct
00066 {
00067   const char *const name;
00068   int regnum;
00069 } aarch64_register_aliases[] =
00070 {
00071   /* 64-bit register names.  */
00072   {"fp", AARCH64_FP_REGNUM},
00073   {"lr", AARCH64_LR_REGNUM},
00074   {"sp", AARCH64_SP_REGNUM},
00075 
00076   /* 32-bit register names.  */
00077   {"w0", AARCH64_X0_REGNUM + 0},
00078   {"w1", AARCH64_X0_REGNUM + 1},
00079   {"w2", AARCH64_X0_REGNUM + 2},
00080   {"w3", AARCH64_X0_REGNUM + 3},
00081   {"w4", AARCH64_X0_REGNUM + 4},
00082   {"w5", AARCH64_X0_REGNUM + 5},
00083   {"w6", AARCH64_X0_REGNUM + 6},
00084   {"w7", AARCH64_X0_REGNUM + 7},
00085   {"w8", AARCH64_X0_REGNUM + 8},
00086   {"w9", AARCH64_X0_REGNUM + 9},
00087   {"w10", AARCH64_X0_REGNUM + 10},
00088   {"w11", AARCH64_X0_REGNUM + 11},
00089   {"w12", AARCH64_X0_REGNUM + 12},
00090   {"w13", AARCH64_X0_REGNUM + 13},
00091   {"w14", AARCH64_X0_REGNUM + 14},
00092   {"w15", AARCH64_X0_REGNUM + 15},
00093   {"w16", AARCH64_X0_REGNUM + 16},
00094   {"w17", AARCH64_X0_REGNUM + 17},
00095   {"w18", AARCH64_X0_REGNUM + 18},
00096   {"w19", AARCH64_X0_REGNUM + 19},
00097   {"w20", AARCH64_X0_REGNUM + 20},
00098   {"w21", AARCH64_X0_REGNUM + 21},
00099   {"w22", AARCH64_X0_REGNUM + 22},
00100   {"w23", AARCH64_X0_REGNUM + 23},
00101   {"w24", AARCH64_X0_REGNUM + 24},
00102   {"w25", AARCH64_X0_REGNUM + 25},
00103   {"w26", AARCH64_X0_REGNUM + 26},
00104   {"w27", AARCH64_X0_REGNUM + 27},
00105   {"w28", AARCH64_X0_REGNUM + 28},
00106   {"w29", AARCH64_X0_REGNUM + 29},
00107   {"w30", AARCH64_X0_REGNUM + 30},
00108 
00109   /*  specials */
00110   {"ip0", AARCH64_X0_REGNUM + 16},
00111   {"ip1", AARCH64_X0_REGNUM + 17}
00112 };
00113 
00114 /* The required core 'R' registers.  */
00115 static const char *const aarch64_r_register_names[] =
00116 {
00117   /* These registers must appear in consecutive RAW register number
00118      order and they must begin with AARCH64_X0_REGNUM! */
00119   "x0", "x1", "x2", "x3",
00120   "x4", "x5", "x6", "x7",
00121   "x8", "x9", "x10", "x11",
00122   "x12", "x13", "x14", "x15",
00123   "x16", "x17", "x18", "x19",
00124   "x20", "x21", "x22", "x23",
00125   "x24", "x25", "x26", "x27",
00126   "x28", "x29", "x30", "sp",
00127   "pc", "cpsr"
00128 };
00129 
00130 /* The FP/SIMD 'V' registers.  */
00131 static const char *const aarch64_v_register_names[] =
00132 {
00133   /* These registers must appear in consecutive RAW register number
00134      order and they must begin with AARCH64_V0_REGNUM! */
00135   "v0", "v1", "v2", "v3",
00136   "v4", "v5", "v6", "v7",
00137   "v8", "v9", "v10", "v11",
00138   "v12", "v13", "v14", "v15",
00139   "v16", "v17", "v18", "v19",
00140   "v20", "v21", "v22", "v23",
00141   "v24", "v25", "v26", "v27",
00142   "v28", "v29", "v30", "v31",
00143   "fpsr",
00144   "fpcr"
00145 };
00146 
00147 /* AArch64 prologue cache structure.  */
00148 struct aarch64_prologue_cache
00149 {
00150   /* The stack pointer at the time this frame was created; i.e. the
00151      caller's stack pointer when this function was called.  It is used
00152      to identify this frame.  */
00153   CORE_ADDR prev_sp;
00154 
00155   /* The frame base for this frame is just prev_sp - frame size.
00156      FRAMESIZE is the distance from the frame pointer to the
00157      initial stack pointer.  */
00158   int framesize;
00159 
00160   /* The register used to hold the frame pointer for this frame.  */
00161   int framereg;
00162 
00163   /* Saved register offsets.  */
00164   struct trad_frame_saved_reg *saved_regs;
00165 };
00166 
00167 /* Toggle this file's internal debugging dump.  */
00168 static int aarch64_debug;
00169 
00170 static void
00171 show_aarch64_debug (struct ui_file *file, int from_tty,
00172                     struct cmd_list_element *c, const char *value)
00173 {
00174   fprintf_filtered (file, _("AArch64 debugging is %s.\n"), value);
00175 }
00176 
00177 /* Extract a signed value from a bit field within an instruction
00178    encoding.
00179 
00180    INSN is the instruction opcode.
00181 
00182    WIDTH specifies the width of the bit field to extract (in bits).
00183 
00184    OFFSET specifies the least significant bit of the field where bits
00185    are numbered zero counting from least to most significant.  */
00186 
00187 static int32_t
00188 extract_signed_bitfield (uint32_t insn, unsigned width, unsigned offset)
00189 {
00190   unsigned shift_l = sizeof (int32_t) * 8 - (offset + width);
00191   unsigned shift_r = sizeof (int32_t) * 8 - width;
00192 
00193   return ((int32_t) insn << shift_l) >> shift_r;
00194 }
00195 
00196 /* Determine if specified bits within an instruction opcode matches a
00197    specific pattern.
00198 
00199    INSN is the instruction opcode.
00200 
00201    MASK specifies the bits within the opcode that are to be tested
00202    agsinst for a match with PATTERN.  */
00203 
00204 static int
00205 decode_masked_match (uint32_t insn, uint32_t mask, uint32_t pattern)
00206 {
00207   return (insn & mask) == pattern;
00208 }
00209 
00210 /* Decode an opcode if it represents an immediate ADD or SUB instruction.
00211 
00212    ADDR specifies the address of the opcode.
00213    INSN specifies the opcode to test.
00214    RD receives the 'rd' field from the decoded instruction.
00215    RN receives the 'rn' field from the decoded instruction.
00216 
00217    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00218 static int
00219 decode_add_sub_imm (CORE_ADDR addr, uint32_t insn, unsigned *rd, unsigned *rn,
00220                     int32_t *imm)
00221 {
00222   if ((insn & 0x9f000000) == 0x91000000)
00223     {
00224       unsigned shift;
00225       unsigned op_is_sub;
00226 
00227       *rd = (insn >> 0) & 0x1f;
00228       *rn = (insn >> 5) & 0x1f;
00229       *imm = (insn >> 10) & 0xfff;
00230       shift = (insn >> 22) & 0x3;
00231       op_is_sub = (insn >> 30) & 0x1;
00232 
00233       switch (shift)
00234         {
00235         case 0:
00236           break;
00237         case 1:
00238           *imm <<= 12;
00239           break;
00240         default:
00241           /* UNDEFINED */
00242           return 0;
00243         }
00244 
00245       if (op_is_sub)
00246         *imm = -*imm;
00247 
00248       if (aarch64_debug)
00249         fprintf_unfiltered (gdb_stdlog,
00250                             "decode: 0x%s 0x%x add x%u, x%u, #%d\n",
00251                             core_addr_to_string_nz (addr), insn, *rd, *rn,
00252                             *imm);
00253       return 1;
00254     }
00255   return 0;
00256 }
00257 
00258 /* Decode an opcode if it represents an ADRP instruction.
00259 
00260    ADDR specifies the address of the opcode.
00261    INSN specifies the opcode to test.
00262    RD receives the 'rd' field from the decoded instruction.
00263 
00264    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00265 
00266 static int
00267 decode_adrp (CORE_ADDR addr, uint32_t insn, unsigned *rd)
00268 {
00269   if (decode_masked_match (insn, 0x9f000000, 0x90000000))
00270     {
00271       *rd = (insn >> 0) & 0x1f;
00272 
00273       if (aarch64_debug)
00274         fprintf_unfiltered (gdb_stdlog,
00275                             "decode: 0x%s 0x%x adrp x%u, #?\n",
00276                             core_addr_to_string_nz (addr), insn, *rd);
00277       return 1;
00278     }
00279   return 0;
00280 }
00281 
00282 /* Decode an opcode if it represents an branch immediate or branch
00283    and link immediate instruction.
00284 
00285    ADDR specifies the address of the opcode.
00286    INSN specifies the opcode to test.
00287    LINK receives the 'link' bit from the decoded instruction.
00288    OFFSET receives the immediate offset from the decoded instruction.
00289 
00290    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00291 
00292 static int
00293 decode_b (CORE_ADDR addr, uint32_t insn, unsigned *link, int32_t *offset)
00294 {
00295   /* b  0001 01ii iiii iiii iiii iiii iiii iiii */
00296   /* bl 1001 01ii iiii iiii iiii iiii iiii iiii */
00297   if (decode_masked_match (insn, 0x7c000000, 0x14000000))
00298     {
00299       *link = insn >> 31;
00300       *offset = extract_signed_bitfield (insn, 26, 0) << 2;
00301 
00302       if (aarch64_debug)
00303         fprintf_unfiltered (gdb_stdlog,
00304                             "decode: 0x%s 0x%x %s 0x%s\n",
00305                             core_addr_to_string_nz (addr), insn,
00306                             *link ? "bl" : "b",
00307                             core_addr_to_string_nz (addr + *offset));
00308 
00309       return 1;
00310     }
00311   return 0;
00312 }
00313 
00314 /* Decode an opcode if it represents a conditional branch instruction.
00315 
00316    ADDR specifies the address of the opcode.
00317    INSN specifies the opcode to test.
00318    COND receives the branch condition field from the decoded
00319    instruction.
00320    OFFSET receives the immediate offset from the decoded instruction.
00321 
00322    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00323 
00324 static int
00325 decode_bcond (CORE_ADDR addr, uint32_t insn, unsigned *cond, int32_t *offset)
00326 {
00327   if (decode_masked_match (insn, 0xfe000000, 0x54000000))
00328     {
00329       *cond = (insn >> 0) & 0xf;
00330       *offset = extract_signed_bitfield (insn, 19, 5) << 2;
00331 
00332       if (aarch64_debug)
00333         fprintf_unfiltered (gdb_stdlog,
00334                             "decode: 0x%s 0x%x b<%u> 0x%s\n",
00335                             core_addr_to_string_nz (addr), insn, *cond,
00336                             core_addr_to_string_nz (addr + *offset));
00337       return 1;
00338     }
00339   return 0;
00340 }
00341 
00342 /* Decode an opcode if it represents a branch via register instruction.
00343 
00344    ADDR specifies the address of the opcode.
00345    INSN specifies the opcode to test.
00346    LINK receives the 'link' bit from the decoded instruction.
00347    RN receives the 'rn' field from the decoded instruction.
00348 
00349    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00350 
00351 static int
00352 decode_br (CORE_ADDR addr, uint32_t insn, unsigned *link, unsigned *rn)
00353 {
00354   /*         8   4   0   6   2   8   4   0 */
00355   /* blr  110101100011111100000000000rrrrr */
00356   /* br   110101100001111100000000000rrrrr */
00357   if (decode_masked_match (insn, 0xffdffc1f, 0xd61f0000))
00358     {
00359       *link = (insn >> 21) & 1;
00360       *rn = (insn >> 5) & 0x1f;
00361 
00362       if (aarch64_debug)
00363         fprintf_unfiltered (gdb_stdlog,
00364                             "decode: 0x%s 0x%x %s 0x%x\n",
00365                             core_addr_to_string_nz (addr), insn,
00366                             *link ? "blr" : "br", *rn);
00367 
00368       return 1;
00369     }
00370   return 0;
00371 }
00372 
00373 /* Decode an opcode if it represents a CBZ or CBNZ instruction.
00374 
00375    ADDR specifies the address of the opcode.
00376    INSN specifies the opcode to test.
00377    IS64 receives the 'sf' field from the decoded instruction.
00378    OP receives the 'op' field from the decoded instruction.
00379    RN receives the 'rn' field from the decoded instruction.
00380    OFFSET receives the 'imm19' field from the decoded instruction.
00381 
00382    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00383 
00384 static int
00385 decode_cb (CORE_ADDR addr,
00386            uint32_t insn, int *is64, unsigned *op, unsigned *rn,
00387            int32_t *offset)
00388 {
00389   if (decode_masked_match (insn, 0x7e000000, 0x34000000))
00390     {
00391       /* cbz  T011 010o iiii iiii iiii iiii iiir rrrr */
00392       /* cbnz T011 010o iiii iiii iiii iiii iiir rrrr */
00393 
00394       *rn = (insn >> 0) & 0x1f;
00395       *is64 = (insn >> 31) & 0x1;
00396       *op = (insn >> 24) & 0x1;
00397       *offset = extract_signed_bitfield (insn, 19, 5) << 2;
00398 
00399       if (aarch64_debug)
00400         fprintf_unfiltered (gdb_stdlog,
00401                             "decode: 0x%s 0x%x %s 0x%s\n",
00402                             core_addr_to_string_nz (addr), insn,
00403                             *op ? "cbnz" : "cbz",
00404                             core_addr_to_string_nz (addr + *offset));
00405       return 1;
00406     }
00407   return 0;
00408 }
00409 
00410 /* Decode an opcode if it represents a ERET instruction.
00411 
00412    ADDR specifies the address of the opcode.
00413    INSN specifies the opcode to test.
00414 
00415    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00416 
00417 static int
00418 decode_eret (CORE_ADDR addr, uint32_t insn)
00419 {
00420   /* eret 1101 0110 1001 1111 0000 0011 1110 0000 */
00421   if (insn == 0xd69f03e0)
00422     {
00423       if (aarch64_debug)
00424         fprintf_unfiltered (gdb_stdlog, "decode: 0x%s 0x%x eret\n",
00425                             core_addr_to_string_nz (addr), insn);
00426       return 1;
00427     }
00428   return 0;
00429 }
00430 
00431 /* Decode an opcode if it represents a MOVZ instruction.
00432 
00433    ADDR specifies the address of the opcode.
00434    INSN specifies the opcode to test.
00435    RD receives the 'rd' field from the decoded instruction.
00436 
00437    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00438 
00439 static int
00440 decode_movz (CORE_ADDR addr, uint32_t insn, unsigned *rd)
00441 {
00442   if (decode_masked_match (insn, 0xff800000, 0x52800000))
00443     {
00444       *rd = (insn >> 0) & 0x1f;
00445 
00446       if (aarch64_debug)
00447         fprintf_unfiltered (gdb_stdlog,
00448                             "decode: 0x%s 0x%x movz x%u, #?\n",
00449                             core_addr_to_string_nz (addr), insn, *rd);
00450       return 1;
00451     }
00452   return 0;
00453 }
00454 
00455 /* Decode an opcode if it represents a ORR (shifted register)
00456    instruction.
00457 
00458    ADDR specifies the address of the opcode.
00459    INSN specifies the opcode to test.
00460    RD receives the 'rd' field from the decoded instruction.
00461    RN receives the 'rn' field from the decoded instruction.
00462    RM receives the 'rm' field from the decoded instruction.
00463    IMM receives the 'imm6' field from the decoded instruction.
00464 
00465    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00466 
00467 static int
00468 decode_orr_shifted_register_x (CORE_ADDR addr,
00469                                uint32_t insn, unsigned *rd, unsigned *rn,
00470                                unsigned *rm, int32_t *imm)
00471 {
00472   if (decode_masked_match (insn, 0xff200000, 0xaa000000))
00473     {
00474       *rd = (insn >> 0) & 0x1f;
00475       *rn = (insn >> 5) & 0x1f;
00476       *rm = (insn >> 16) & 0x1f;
00477       *imm = (insn >> 10) & 0x3f;
00478 
00479       if (aarch64_debug)
00480         fprintf_unfiltered (gdb_stdlog,
00481                             "decode: 0x%s 0x%x orr x%u, x%u, x%u, #%u\n",
00482                             core_addr_to_string_nz (addr), insn, *rd,
00483                             *rn, *rm, *imm);
00484       return 1;
00485     }
00486   return 0;
00487 }
00488 
00489 /* Decode an opcode if it represents a RET instruction.
00490 
00491    ADDR specifies the address of the opcode.
00492    INSN specifies the opcode to test.
00493    RN receives the 'rn' field from the decoded instruction.
00494 
00495    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00496 
00497 static int
00498 decode_ret (CORE_ADDR addr, uint32_t insn, unsigned *rn)
00499 {
00500   if (decode_masked_match (insn, 0xfffffc1f, 0xd65f0000))
00501     {
00502       *rn = (insn >> 5) & 0x1f;
00503       if (aarch64_debug)
00504         fprintf_unfiltered (gdb_stdlog,
00505                             "decode: 0x%s 0x%x ret x%u\n",
00506                             core_addr_to_string_nz (addr), insn, *rn);
00507       return 1;
00508     }
00509   return 0;
00510 }
00511 
00512 /* Decode an opcode if it represents the following instruction:
00513    STP rt, rt2, [rn, #imm]
00514 
00515    ADDR specifies the address of the opcode.
00516    INSN specifies the opcode to test.
00517    RT1 receives the 'rt' field from the decoded instruction.
00518    RT2 receives the 'rt2' field from the decoded instruction.
00519    RN receives the 'rn' field from the decoded instruction.
00520    IMM receives the 'imm' field from the decoded instruction.
00521 
00522    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00523 
00524 static int
00525 decode_stp_offset (CORE_ADDR addr,
00526                    uint32_t insn,
00527                    unsigned *rt1, unsigned *rt2, unsigned *rn, int32_t *imm)
00528 {
00529   if (decode_masked_match (insn, 0xffc00000, 0xa9000000))
00530     {
00531       *rt1 = (insn >> 0) & 0x1f;
00532       *rn = (insn >> 5) & 0x1f;
00533       *rt2 = (insn >> 10) & 0x1f;
00534       *imm = extract_signed_bitfield (insn, 7, 15);
00535       *imm <<= 3;
00536 
00537       if (aarch64_debug)
00538         fprintf_unfiltered (gdb_stdlog,
00539                             "decode: 0x%s 0x%x stp x%u, x%u, [x%u + #%d]\n",
00540                             core_addr_to_string_nz (addr), insn,
00541                             *rt1, *rt2, *rn, *imm);
00542       return 1;
00543     }
00544   return 0;
00545 }
00546 
00547 /* Decode an opcode if it represents the following instruction:
00548    STP rt, rt2, [rn, #imm]!
00549 
00550    ADDR specifies the address of the opcode.
00551    INSN specifies the opcode to test.
00552    RT1 receives the 'rt' field from the decoded instruction.
00553    RT2 receives the 'rt2' field from the decoded instruction.
00554    RN receives the 'rn' field from the decoded instruction.
00555    IMM receives the 'imm' field from the decoded instruction.
00556 
00557    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00558 
00559 static int
00560 decode_stp_offset_wb (CORE_ADDR addr,
00561                       uint32_t insn,
00562                       unsigned *rt1, unsigned *rt2, unsigned *rn,
00563                       int32_t *imm)
00564 {
00565   if (decode_masked_match (insn, 0xffc00000, 0xa9800000))
00566     {
00567       *rt1 = (insn >> 0) & 0x1f;
00568       *rn = (insn >> 5) & 0x1f;
00569       *rt2 = (insn >> 10) & 0x1f;
00570       *imm = extract_signed_bitfield (insn, 7, 15);
00571       *imm <<= 3;
00572 
00573       if (aarch64_debug)
00574         fprintf_unfiltered (gdb_stdlog,
00575                             "decode: 0x%s 0x%x stp x%u, x%u, [x%u + #%d]!\n",
00576                             core_addr_to_string_nz (addr), insn,
00577                             *rt1, *rt2, *rn, *imm);
00578       return 1;
00579     }
00580   return 0;
00581 }
00582 
00583 /* Decode an opcode if it represents the following instruction:
00584    STUR rt, [rn, #imm]
00585 
00586    ADDR specifies the address of the opcode.
00587    INSN specifies the opcode to test.
00588    IS64 receives size field from the decoded instruction.
00589    RT receives the 'rt' field from the decoded instruction.
00590    RN receives the 'rn' field from the decoded instruction.
00591    IMM receives the 'imm' field from the decoded instruction.
00592 
00593    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00594 
00595 static int
00596 decode_stur (CORE_ADDR addr, uint32_t insn, int *is64, unsigned *rt,
00597              unsigned *rn, int32_t *imm)
00598 {
00599   if (decode_masked_match (insn, 0xbfe00c00, 0xb8000000))
00600     {
00601       *is64 = (insn >> 30) & 1;
00602       *rt = (insn >> 0) & 0x1f;
00603       *rn = (insn >> 5) & 0x1f;
00604       *imm = extract_signed_bitfield (insn, 9, 12);
00605 
00606       if (aarch64_debug)
00607         fprintf_unfiltered (gdb_stdlog,
00608                             "decode: 0x%s 0x%x stur %c%u, [x%u + #%d]\n",
00609                             core_addr_to_string_nz (addr), insn,
00610                             *is64 ? 'x' : 'w', *rt, *rn, *imm);
00611       return 1;
00612     }
00613   return 0;
00614 }
00615 
00616 /* Decode an opcode if it represents a TB or TBNZ instruction.
00617 
00618    ADDR specifies the address of the opcode.
00619    INSN specifies the opcode to test.
00620    OP receives the 'op' field from the decoded instruction.
00621    BIT receives the bit position field from the decoded instruction.
00622    RT receives 'rt' field from the decoded instruction.
00623    IMM receives 'imm' field from the decoded instruction.
00624 
00625    Return 1 if the opcodes matches and is decoded, otherwise 0.  */
00626 
00627 static int
00628 decode_tb (CORE_ADDR addr,
00629            uint32_t insn, unsigned *op, unsigned *bit, unsigned *rt,
00630            int32_t *imm)
00631 {
00632   if (decode_masked_match (insn, 0x7e000000, 0x36000000))
00633     {
00634       /* tbz  b011 0110 bbbb biii iiii iiii iiir rrrr */
00635       /* tbnz B011 0111 bbbb biii iiii iiii iiir rrrr */
00636 
00637       *rt = (insn >> 0) & 0x1f;
00638       *op = insn & (1 << 24);
00639       *bit = ((insn >> (31 - 4)) & 0x20) | ((insn >> 19) & 0x1f);
00640       *imm = extract_signed_bitfield (insn, 14, 5) << 2;
00641 
00642       if (aarch64_debug)
00643         fprintf_unfiltered (gdb_stdlog,
00644                             "decode: 0x%s 0x%x %s x%u, #%u, 0x%s\n",
00645                             core_addr_to_string_nz (addr), insn,
00646                             *op ? "tbnz" : "tbz", *rt, *bit,
00647                             core_addr_to_string_nz (addr + *imm));
00648       return 1;
00649     }
00650   return 0;
00651 }
00652 
00653 /* Analyze a prologue, looking for a recognizable stack frame
00654    and frame pointer.  Scan until we encounter a store that could
00655    clobber the stack frame unexpectedly, or an unknown instruction.  */
00656 
00657 static CORE_ADDR
00658 aarch64_analyze_prologue (struct gdbarch *gdbarch,
00659                           CORE_ADDR start, CORE_ADDR limit,
00660                           struct aarch64_prologue_cache *cache)
00661 {
00662   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
00663   int i;
00664   pv_t regs[AARCH64_X_REGISTER_COUNT];
00665   struct pv_area *stack;
00666   struct cleanup *back_to;
00667 
00668   for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
00669     regs[i] = pv_register (i, 0);
00670   stack = make_pv_area (AARCH64_SP_REGNUM, gdbarch_addr_bit (gdbarch));
00671   back_to = make_cleanup_free_pv_area (stack);
00672 
00673   for (; start < limit; start += 4)
00674     {
00675       uint32_t insn;
00676       unsigned rd;
00677       unsigned rn;
00678       unsigned rm;
00679       unsigned rt;
00680       unsigned rt1;
00681       unsigned rt2;
00682       int op_is_sub;
00683       int32_t imm;
00684       unsigned cond;
00685       int is64;
00686       unsigned is_link;
00687       unsigned op;
00688       unsigned bit;
00689       int32_t offset;
00690 
00691       insn = read_memory_unsigned_integer (start, 4, byte_order_for_code);
00692 
00693       if (decode_add_sub_imm (start, insn, &rd, &rn, &imm))
00694         regs[rd] = pv_add_constant (regs[rn], imm);
00695       else if (decode_adrp (start, insn, &rd))
00696         regs[rd] = pv_unknown ();
00697       else if (decode_b (start, insn, &is_link, &offset))
00698         {
00699           /* Stop analysis on branch.  */
00700           break;
00701         }
00702       else if (decode_bcond (start, insn, &cond, &offset))
00703         {
00704           /* Stop analysis on branch.  */
00705           break;
00706         }
00707       else if (decode_br (start, insn, &is_link, &rn))
00708         {
00709           /* Stop analysis on branch.  */
00710           break;
00711         }
00712       else if (decode_cb (start, insn, &is64, &op, &rn, &offset))
00713         {
00714           /* Stop analysis on branch.  */
00715           break;
00716         }
00717       else if (decode_eret (start, insn))
00718         {
00719           /* Stop analysis on branch.  */
00720           break;
00721         }
00722       else if (decode_movz (start, insn, &rd))
00723         regs[rd] = pv_unknown ();
00724       else
00725         if (decode_orr_shifted_register_x (start, insn, &rd, &rn, &rm, &imm))
00726         {
00727           if (imm == 0 && rn == 31)
00728             regs[rd] = regs[rm];
00729           else
00730             {
00731               if (aarch64_debug)
00732                 fprintf_unfiltered
00733                   (gdb_stdlog,
00734                    "aarch64: prologue analysis gave up addr=0x%s "
00735                    "opcode=0x%x (orr x register)\n",
00736                    core_addr_to_string_nz (start),
00737                    insn);
00738               break;
00739             }
00740         }
00741       else if (decode_ret (start, insn, &rn))
00742         {
00743           /* Stop analysis on branch.  */
00744           break;
00745         }
00746       else if (decode_stur (start, insn, &is64, &rt, &rn, &offset))
00747         {
00748           pv_area_store (stack, pv_add_constant (regs[rn], offset),
00749                          is64 ? 8 : 4, regs[rt]);
00750         }
00751       else if (decode_stp_offset (start, insn, &rt1, &rt2, &rn, &imm))
00752         {
00753           /* If recording this store would invalidate the store area
00754              (perhaps because rn is not known) then we should abandon
00755              further prologue analysis.  */
00756           if (pv_area_store_would_trash (stack,
00757                                          pv_add_constant (regs[rn], imm)))
00758             break;
00759 
00760           if (pv_area_store_would_trash (stack,
00761                                          pv_add_constant (regs[rn], imm + 8)))
00762             break;
00763 
00764           pv_area_store (stack, pv_add_constant (regs[rn], imm), 8,
00765                          regs[rt1]);
00766           pv_area_store (stack, pv_add_constant (regs[rn], imm + 8), 8,
00767                          regs[rt2]);
00768         }
00769       else if (decode_stp_offset_wb (start, insn, &rt1, &rt2, &rn, &imm))
00770         {
00771           /* If recording this store would invalidate the store area
00772              (perhaps because rn is not known) then we should abandon
00773              further prologue analysis.  */
00774           if (pv_area_store_would_trash (stack,
00775                                          pv_add_constant (regs[rn], imm)))
00776             break;
00777 
00778           if (pv_area_store_would_trash (stack,
00779                                          pv_add_constant (regs[rn], imm + 8)))
00780             break;
00781 
00782           pv_area_store (stack, pv_add_constant (regs[rn], imm), 8,
00783                          regs[rt1]);
00784           pv_area_store (stack, pv_add_constant (regs[rn], imm + 8), 8,
00785                          regs[rt2]);
00786           regs[rn] = pv_add_constant (regs[rn], imm);
00787         }
00788       else if (decode_tb (start, insn, &op, &bit, &rn, &offset))
00789         {
00790           /* Stop analysis on branch.  */
00791           break;
00792         }
00793       else
00794         {
00795           if (aarch64_debug)
00796             fprintf_unfiltered (gdb_stdlog,
00797                                 "aarch64: prologue analysis gave up addr=0x%s"
00798                                 " opcode=0x%x\n",
00799                                 core_addr_to_string_nz (start), insn);
00800           break;
00801         }
00802     }
00803 
00804   if (cache == NULL)
00805     {
00806       do_cleanups (back_to);
00807       return start;
00808     }
00809 
00810   if (pv_is_register (regs[AARCH64_FP_REGNUM], AARCH64_SP_REGNUM))
00811     {
00812       /* Frame pointer is fp.  Frame size is constant.  */
00813       cache->framereg = AARCH64_FP_REGNUM;
00814       cache->framesize = -regs[AARCH64_FP_REGNUM].k;
00815     }
00816   else if (pv_is_register (regs[AARCH64_SP_REGNUM], AARCH64_SP_REGNUM))
00817     {
00818       /* Try the stack pointer.  */
00819       cache->framesize = -regs[AARCH64_SP_REGNUM].k;
00820       cache->framereg = AARCH64_SP_REGNUM;
00821     }
00822   else
00823     {
00824       /* We're just out of luck.  We don't know where the frame is.  */
00825       cache->framereg = -1;
00826       cache->framesize = 0;
00827     }
00828 
00829   for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
00830     {
00831       CORE_ADDR offset;
00832 
00833       if (pv_area_find_reg (stack, gdbarch, i, &offset))
00834         cache->saved_regs[i].addr = offset;
00835     }
00836 
00837   do_cleanups (back_to);
00838   return start;
00839 }
00840 
00841 /* Implement the "skip_prologue" gdbarch method.  */
00842 
00843 static CORE_ADDR
00844 aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
00845 {
00846   unsigned long inst;
00847   CORE_ADDR skip_pc;
00848   CORE_ADDR func_addr, limit_pc;
00849   struct symtab_and_line sal;
00850 
00851   /* See if we can determine the end of the prologue via the symbol
00852      table.  If so, then return either PC, or the PC after the
00853      prologue, whichever is greater.  */
00854   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
00855     {
00856       CORE_ADDR post_prologue_pc
00857         = skip_prologue_using_sal (gdbarch, func_addr);
00858 
00859       if (post_prologue_pc != 0)
00860         return max (pc, post_prologue_pc);
00861     }
00862 
00863   /* Can't determine prologue from the symbol table, need to examine
00864      instructions.  */
00865 
00866   /* Find an upper limit on the function prologue using the debug
00867      information.  If the debug information could not be used to
00868      provide that bound, then use an arbitrary large number as the
00869      upper bound.  */
00870   limit_pc = skip_prologue_using_sal (gdbarch, pc);
00871   if (limit_pc == 0)
00872     limit_pc = pc + 128;        /* Magic.  */
00873 
00874   /* Try disassembling prologue.  */
00875   return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
00876 }
00877 
00878 /* Scan the function prologue for THIS_FRAME and populate the prologue
00879    cache CACHE.  */
00880 
00881 static void
00882 aarch64_scan_prologue (struct frame_info *this_frame,
00883                        struct aarch64_prologue_cache *cache)
00884 {
00885   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
00886   CORE_ADDR prologue_start;
00887   CORE_ADDR prologue_end;
00888   CORE_ADDR prev_pc = get_frame_pc (this_frame);
00889   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00890 
00891   /* Assume we do not find a frame.  */
00892   cache->framereg = -1;
00893   cache->framesize = 0;
00894 
00895   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
00896                                 &prologue_end))
00897     {
00898       struct symtab_and_line sal = find_pc_line (prologue_start, 0);
00899 
00900       if (sal.line == 0)
00901         {
00902           /* No line info so use the current PC.  */
00903           prologue_end = prev_pc;
00904         }
00905       else if (sal.end < prologue_end)
00906         {
00907           /* The next line begins after the function end.  */
00908           prologue_end = sal.end;
00909         }
00910 
00911       prologue_end = min (prologue_end, prev_pc);
00912       aarch64_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
00913     }
00914   else
00915     {
00916       CORE_ADDR frame_loc;
00917       LONGEST saved_fp;
00918       LONGEST saved_lr;
00919       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00920 
00921       frame_loc = get_frame_register_unsigned (this_frame, AARCH64_FP_REGNUM);
00922       if (frame_loc == 0)
00923         return;
00924 
00925       cache->framereg = AARCH64_FP_REGNUM;
00926       cache->framesize = 16;
00927       cache->saved_regs[29].addr = 0;
00928       cache->saved_regs[30].addr = 8;
00929     }
00930 }
00931 
00932 /* Allocate an aarch64_prologue_cache and fill it with information
00933    about the prologue of *THIS_FRAME.  */
00934 
00935 static struct aarch64_prologue_cache *
00936 aarch64_make_prologue_cache (struct frame_info *this_frame)
00937 {
00938   struct aarch64_prologue_cache *cache;
00939   CORE_ADDR unwound_fp;
00940   int reg;
00941 
00942   cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
00943   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
00944 
00945   aarch64_scan_prologue (this_frame, cache);
00946 
00947   if (cache->framereg == -1)
00948     return cache;
00949 
00950   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
00951   if (unwound_fp == 0)
00952     return cache;
00953 
00954   cache->prev_sp = unwound_fp + cache->framesize;
00955 
00956   /* Calculate actual addresses of saved registers using offsets
00957      determined by aarch64_analyze_prologue.  */
00958   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
00959     if (trad_frame_addr_p (cache->saved_regs, reg))
00960       cache->saved_regs[reg].addr += cache->prev_sp;
00961 
00962   return cache;
00963 }
00964 
00965 /* Our frame ID for a normal frame is the current function's starting
00966    PC and the caller's SP when we were called.  */
00967 
00968 static void
00969 aarch64_prologue_this_id (struct frame_info *this_frame,
00970                           void **this_cache, struct frame_id *this_id)
00971 {
00972   struct aarch64_prologue_cache *cache;
00973   struct frame_id id;
00974   CORE_ADDR pc, func;
00975 
00976   if (*this_cache == NULL)
00977     *this_cache = aarch64_make_prologue_cache (this_frame);
00978   cache = *this_cache;
00979 
00980   /* This is meant to halt the backtrace at "_start".  */
00981   pc = get_frame_pc (this_frame);
00982   if (pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
00983     return;
00984 
00985   /* If we've hit a wall, stop.  */
00986   if (cache->prev_sp == 0)
00987     return;
00988 
00989   func = get_frame_func (this_frame);
00990   id = frame_id_build (cache->prev_sp, func);
00991   *this_id = id;
00992 }
00993 
00994 /* Implement the "prev_register" frame_unwind method.  */
00995 
00996 static struct value *
00997 aarch64_prologue_prev_register (struct frame_info *this_frame,
00998                                 void **this_cache, int prev_regnum)
00999 {
01000   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01001   struct aarch64_prologue_cache *cache;
01002 
01003   if (*this_cache == NULL)
01004     *this_cache = aarch64_make_prologue_cache (this_frame);
01005   cache = *this_cache;
01006 
01007   /* If we are asked to unwind the PC, then we need to return the LR
01008      instead.  The prologue may save PC, but it will point into this
01009      frame's prologue, not the next frame's resume location.  */
01010   if (prev_regnum == AARCH64_PC_REGNUM)
01011     {
01012       CORE_ADDR lr;
01013 
01014       lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
01015       return frame_unwind_got_constant (this_frame, prev_regnum, lr);
01016     }
01017 
01018   /* SP is generally not saved to the stack, but this frame is
01019      identified by the next frame's stack pointer at the time of the
01020      call.  The value was already reconstructed into PREV_SP.  */
01021   /*
01022          +----------+  ^
01023          | saved lr |  |
01024       +->| saved fp |--+
01025       |  |          |
01026       |  |          |     <- Previous SP
01027       |  +----------+
01028       |  | saved lr |
01029       +--| saved fp |<- FP
01030          |          |
01031          |          |<- SP
01032          +----------+  */
01033   if (prev_regnum == AARCH64_SP_REGNUM)
01034     return frame_unwind_got_constant (this_frame, prev_regnum,
01035                                       cache->prev_sp);
01036 
01037   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
01038                                        prev_regnum);
01039 }
01040 
01041 /* AArch64 prologue unwinder.  */
01042 struct frame_unwind aarch64_prologue_unwind =
01043 {
01044   NORMAL_FRAME,
01045   default_frame_unwind_stop_reason,
01046   aarch64_prologue_this_id,
01047   aarch64_prologue_prev_register,
01048   NULL,
01049   default_frame_sniffer
01050 };
01051 
01052 /* Allocate an aarch64_prologue_cache and fill it with information
01053    about the prologue of *THIS_FRAME.  */
01054 
01055 static struct aarch64_prologue_cache *
01056 aarch64_make_stub_cache (struct frame_info *this_frame)
01057 {
01058   int reg;
01059   struct aarch64_prologue_cache *cache;
01060   CORE_ADDR unwound_fp;
01061 
01062   cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
01063   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
01064 
01065   cache->prev_sp
01066     = get_frame_register_unsigned (this_frame, AARCH64_SP_REGNUM);
01067 
01068   return cache;
01069 }
01070 
01071 /* Our frame ID for a stub frame is the current SP and LR.  */
01072 
01073 static void
01074 aarch64_stub_this_id (struct frame_info *this_frame,
01075                       void **this_cache, struct frame_id *this_id)
01076 {
01077   struct aarch64_prologue_cache *cache;
01078 
01079   if (*this_cache == NULL)
01080     *this_cache = aarch64_make_stub_cache (this_frame);
01081   cache = *this_cache;
01082 
01083   *this_id = frame_id_build (cache->prev_sp, get_frame_pc (this_frame));
01084 }
01085 
01086 /* Implement the "sniffer" frame_unwind method.  */
01087 
01088 static int
01089 aarch64_stub_unwind_sniffer (const struct frame_unwind *self,
01090                              struct frame_info *this_frame,
01091                              void **this_prologue_cache)
01092 {
01093   CORE_ADDR addr_in_block;
01094   gdb_byte dummy[4];
01095 
01096   addr_in_block = get_frame_address_in_block (this_frame);
01097   if (in_plt_section (addr_in_block)
01098       /* We also use the stub winder if the target memory is unreadable
01099          to avoid having the prologue unwinder trying to read it.  */
01100       || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
01101     return 1;
01102 
01103   return 0;
01104 }
01105 
01106 /* AArch64 stub unwinder.  */
01107 struct frame_unwind aarch64_stub_unwind =
01108 {
01109   NORMAL_FRAME,
01110   default_frame_unwind_stop_reason,
01111   aarch64_stub_this_id,
01112   aarch64_prologue_prev_register,
01113   NULL,
01114   aarch64_stub_unwind_sniffer
01115 };
01116 
01117 /* Return the frame base address of *THIS_FRAME.  */
01118 
01119 static CORE_ADDR
01120 aarch64_normal_frame_base (struct frame_info *this_frame, void **this_cache)
01121 {
01122   struct aarch64_prologue_cache *cache;
01123 
01124   if (*this_cache == NULL)
01125     *this_cache = aarch64_make_prologue_cache (this_frame);
01126   cache = *this_cache;
01127 
01128   return cache->prev_sp - cache->framesize;
01129 }
01130 
01131 /* AArch64 default frame base information.  */
01132 struct frame_base aarch64_normal_base =
01133 {
01134   &aarch64_prologue_unwind,
01135   aarch64_normal_frame_base,
01136   aarch64_normal_frame_base,
01137   aarch64_normal_frame_base
01138 };
01139 
01140 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
01141    dummy frame.  The frame ID's base needs to match the TOS value
01142    saved by save_dummy_frame_tos () and returned from
01143    aarch64_push_dummy_call, and the PC needs to match the dummy
01144    frame's breakpoint.  */
01145 
01146 static struct frame_id
01147 aarch64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
01148 {
01149   return frame_id_build (get_frame_register_unsigned (this_frame,
01150                                                       AARCH64_SP_REGNUM),
01151                          get_frame_pc (this_frame));
01152 }
01153 
01154 /* Implement the "unwind_pc" gdbarch method.  */
01155 
01156 static CORE_ADDR
01157 aarch64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
01158 {
01159   CORE_ADDR pc
01160     = frame_unwind_register_unsigned (this_frame, AARCH64_PC_REGNUM);
01161 
01162   return pc;
01163 }
01164 
01165 /* Implement the "unwind_sp" gdbarch method.  */
01166 
01167 static CORE_ADDR
01168 aarch64_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
01169 {
01170   return frame_unwind_register_unsigned (this_frame, AARCH64_SP_REGNUM);
01171 }
01172 
01173 /* Return the value of the REGNUM register in the previous frame of
01174    *THIS_FRAME.  */
01175 
01176 static struct value *
01177 aarch64_dwarf2_prev_register (struct frame_info *this_frame,
01178                               void **this_cache, int regnum)
01179 {
01180   struct gdbarch *gdbarch = get_frame_arch (this_frame);
01181   CORE_ADDR lr;
01182 
01183   switch (regnum)
01184     {
01185     case AARCH64_PC_REGNUM:
01186       lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
01187       return frame_unwind_got_constant (this_frame, regnum, lr);
01188 
01189     default:
01190       internal_error (__FILE__, __LINE__,
01191                       _("Unexpected register %d"), regnum);
01192     }
01193 }
01194 
01195 /* Implement the "init_reg" dwarf2_frame_ops method.  */
01196 
01197 static void
01198 aarch64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
01199                                struct dwarf2_frame_state_reg *reg,
01200                                struct frame_info *this_frame)
01201 {
01202   switch (regnum)
01203     {
01204     case AARCH64_PC_REGNUM:
01205       reg->how = DWARF2_FRAME_REG_FN;
01206       reg->loc.fn = aarch64_dwarf2_prev_register;
01207       break;
01208     case AARCH64_SP_REGNUM:
01209       reg->how = DWARF2_FRAME_REG_CFA;
01210       break;
01211     }
01212 }
01213 
01214 /* When arguments must be pushed onto the stack, they go on in reverse
01215    order.  The code below implements a FILO (stack) to do this.  */
01216 
01217 typedef struct
01218 {
01219   /* Value to pass on stack.  */
01220   const void *data;
01221 
01222   /* Size in bytes of value to pass on stack.  */
01223   int len;
01224 } stack_item_t;
01225 
01226 DEF_VEC_O (stack_item_t);
01227 
01228 /* Return the alignment (in bytes) of the given type.  */
01229 
01230 static int
01231 aarch64_type_align (struct type *t)
01232 {
01233   int n;
01234   int align;
01235   int falign;
01236 
01237   t = check_typedef (t);
01238   switch (TYPE_CODE (t))
01239     {
01240     default:
01241       /* Should never happen.  */
01242       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
01243       return 4;
01244 
01245     case TYPE_CODE_PTR:
01246     case TYPE_CODE_ENUM:
01247     case TYPE_CODE_INT:
01248     case TYPE_CODE_FLT:
01249     case TYPE_CODE_SET:
01250     case TYPE_CODE_RANGE:
01251     case TYPE_CODE_BITSTRING:
01252     case TYPE_CODE_REF:
01253     case TYPE_CODE_CHAR:
01254     case TYPE_CODE_BOOL:
01255       return TYPE_LENGTH (t);
01256 
01257     case TYPE_CODE_ARRAY:
01258     case TYPE_CODE_COMPLEX:
01259       return aarch64_type_align (TYPE_TARGET_TYPE (t));
01260 
01261     case TYPE_CODE_STRUCT:
01262     case TYPE_CODE_UNION:
01263       align = 1;
01264       for (n = 0; n < TYPE_NFIELDS (t); n++)
01265         {
01266           falign = aarch64_type_align (TYPE_FIELD_TYPE (t, n));
01267           if (falign > align)
01268             align = falign;
01269         }
01270       return align;
01271     }
01272 }
01273 
01274 /* Return 1 if *TY is a homogeneous floating-point aggregate as
01275    defined in the AAPCS64 ABI document; otherwise return 0.  */
01276 
01277 static int
01278 is_hfa (struct type *ty)
01279 {
01280   switch (TYPE_CODE (ty))
01281     {
01282     case TYPE_CODE_ARRAY:
01283       {
01284         struct type *target_ty = TYPE_TARGET_TYPE (ty);
01285         if (TYPE_CODE (target_ty) == TYPE_CODE_FLT && TYPE_LENGTH (ty) <= 4)
01286           return 1;
01287         break;
01288       }
01289 
01290     case TYPE_CODE_UNION:
01291     case TYPE_CODE_STRUCT:
01292       {
01293         if (TYPE_NFIELDS (ty) > 0 && TYPE_NFIELDS (ty) <= 4)
01294           {
01295             struct type *member0_type;
01296 
01297             member0_type = check_typedef (TYPE_FIELD_TYPE (ty, 0));
01298             if (TYPE_CODE (member0_type) == TYPE_CODE_FLT)
01299               {
01300                 int i;
01301 
01302                 for (i = 0; i < TYPE_NFIELDS (ty); i++)
01303                   {
01304                     struct type *member1_type;
01305 
01306                     member1_type = check_typedef (TYPE_FIELD_TYPE (ty, i));
01307                     if (TYPE_CODE (member0_type) != TYPE_CODE (member1_type)
01308                         || (TYPE_LENGTH (member0_type)
01309                             != TYPE_LENGTH (member1_type)))
01310                       return 0;
01311                   }
01312                 return 1;
01313               }
01314           }
01315         return 0;
01316       }
01317 
01318     default:
01319       break;
01320     }
01321 
01322   return 0;
01323 }
01324 
01325 /* AArch64 function call information structure.  */
01326 struct aarch64_call_info
01327 {
01328   /* the current argument number.  */
01329   unsigned argnum;
01330 
01331   /* The next general purpose register number, equivalent to NGRN as
01332      described in the AArch64 Procedure Call Standard.  */
01333   unsigned ngrn;
01334 
01335   /* The next SIMD and floating point register number, equivalent to
01336      NSRN as described in the AArch64 Procedure Call Standard.  */
01337   unsigned nsrn;
01338 
01339   /* The next stacked argument address, equivalent to NSAA as
01340      described in the AArch64 Procedure Call Standard.  */
01341   unsigned nsaa;
01342 
01343   /* Stack item vector.  */
01344   VEC(stack_item_t) *si;
01345 };
01346 
01347 /* Pass a value in a sequence of consecutive X registers.  The caller
01348    is responsbile for ensuring sufficient registers are available.  */
01349 
01350 static void
01351 pass_in_x (struct gdbarch *gdbarch, struct regcache *regcache,
01352            struct aarch64_call_info *info, struct type *type,
01353            const bfd_byte *buf)
01354 {
01355   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01356   int len = TYPE_LENGTH (type);
01357   enum type_code typecode = TYPE_CODE (type);
01358   int regnum = AARCH64_X0_REGNUM + info->ngrn;
01359 
01360   info->argnum++;
01361 
01362   while (len > 0)
01363     {
01364       int partial_len = len < X_REGISTER_SIZE ? len : X_REGISTER_SIZE;
01365       CORE_ADDR regval = extract_unsigned_integer (buf, partial_len,
01366                                                    byte_order);
01367 
01368 
01369       /* Adjust sub-word struct/union args when big-endian.  */
01370       if (byte_order == BFD_ENDIAN_BIG
01371           && partial_len < X_REGISTER_SIZE
01372           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
01373         regval <<= ((X_REGISTER_SIZE - partial_len) * TARGET_CHAR_BIT);
01374 
01375       if (aarch64_debug)
01376         fprintf_unfiltered (gdb_stdlog, "arg %d in %s = 0x%s\n",
01377                             info->argnum,
01378                             gdbarch_register_name (gdbarch, regnum),
01379                             phex (regval, X_REGISTER_SIZE));
01380       regcache_cooked_write_unsigned (regcache, regnum, regval);
01381       len -= partial_len;
01382       buf += partial_len;
01383       regnum++;
01384     }
01385 }
01386 
01387 /* Attempt to marshall a value in a V register.  Return 1 if
01388    successful, or 0 if insufficient registers are available.  This
01389    function, unlike the equivalent pass_in_x() function does not
01390    handle arguments spread across multiple registers.  */
01391 
01392 static int
01393 pass_in_v (struct gdbarch *gdbarch,
01394            struct regcache *regcache,
01395            struct aarch64_call_info *info,
01396            const bfd_byte *buf)
01397 {
01398   if (info->nsrn < 8)
01399     {
01400       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01401       int regnum = AARCH64_V0_REGNUM + info->nsrn;
01402 
01403       info->argnum++;
01404       info->nsrn++;
01405 
01406       regcache_cooked_write (regcache, regnum, buf);
01407       if (aarch64_debug)
01408         fprintf_unfiltered (gdb_stdlog, "arg %d in %s\n",
01409                             info->argnum,
01410                             gdbarch_register_name (gdbarch, regnum));
01411       return 1;
01412     }
01413   info->nsrn = 8;
01414   return 0;
01415 }
01416 
01417 /* Marshall an argument onto the stack.  */
01418 
01419 static void
01420 pass_on_stack (struct aarch64_call_info *info, struct type *type,
01421                const bfd_byte *buf)
01422 {
01423   int len = TYPE_LENGTH (type);
01424   int align;
01425   stack_item_t item;
01426 
01427   info->argnum++;
01428 
01429   align = aarch64_type_align (type);
01430 
01431   /* PCS C.17 Stack should be aligned to the larger of 8 bytes or the
01432      Natural alignment of the argument's type.  */
01433   align = align_up (align, 8);
01434 
01435   /* The AArch64 PCS requires at most doubleword alignment.  */
01436   if (align > 16)
01437     align = 16;
01438 
01439   if (aarch64_debug)
01440     fprintf_unfiltered (gdb_stdlog, "arg %d len=%d @ sp + %d\n",
01441                         info->argnum, len, info->nsaa);
01442 
01443   item.len = len;
01444   item.data = buf;
01445   VEC_safe_push (stack_item_t, info->si, &item);
01446 
01447   info->nsaa += len;
01448   if (info->nsaa & (align - 1))
01449     {
01450       /* Push stack alignment padding.  */
01451       int pad = align - (info->nsaa & (align - 1));
01452 
01453       item.len = pad;
01454       item.data = buf;
01455 
01456       VEC_safe_push (stack_item_t, info->si, &item);
01457       info->nsaa += pad;
01458     }
01459 }
01460 
01461 /* Marshall an argument into a sequence of one or more consecutive X
01462    registers or, if insufficient X registers are available then onto
01463    the stack.  */
01464 
01465 static void
01466 pass_in_x_or_stack (struct gdbarch *gdbarch, struct regcache *regcache,
01467                     struct aarch64_call_info *info, struct type *type,
01468                     const bfd_byte *buf)
01469 {
01470   int len = TYPE_LENGTH (type);
01471   int nregs = (len + X_REGISTER_SIZE - 1) / X_REGISTER_SIZE;
01472 
01473   /* PCS C.13 - Pass in registers if we have enough spare */
01474   if (info->ngrn + nregs <= 8)
01475     {
01476       pass_in_x (gdbarch, regcache, info, type, buf);
01477       info->ngrn += nregs;
01478     }
01479   else
01480     {
01481       info->ngrn = 8;
01482       pass_on_stack (info, type, buf);
01483     }
01484 }
01485 
01486 /* Pass a value in a V register, or on the stack if insufficient are
01487    available.  */
01488 
01489 static void
01490 pass_in_v_or_stack (struct gdbarch *gdbarch,
01491                     struct regcache *regcache,
01492                     struct aarch64_call_info *info,
01493                     struct type *type,
01494                     const bfd_byte *buf)
01495 {
01496   if (!pass_in_v (gdbarch, regcache, info, buf))
01497     pass_on_stack (info, type, buf);
01498 }
01499 
01500 /* Implement the "push_dummy_call" gdbarch method.  */
01501 
01502 static CORE_ADDR
01503 aarch64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
01504                          struct regcache *regcache, CORE_ADDR bp_addr,
01505                          int nargs,
01506                          struct value **args, CORE_ADDR sp, int struct_return,
01507                          CORE_ADDR struct_addr)
01508 {
01509   int nstack = 0;
01510   int argnum;
01511   int x_argreg;
01512   int v_argreg;
01513   struct aarch64_call_info info;
01514   struct type *func_type;
01515   struct type *return_type;
01516   int lang_struct_return;
01517 
01518   memset (&info, 0, sizeof (info));
01519 
01520   /* We need to know what the type of the called function is in order
01521      to determine the number of named/anonymous arguments for the
01522      actual argument placement, and the return type in order to handle
01523      return value correctly.
01524 
01525      The generic code above us views the decision of return in memory
01526      or return in registers as a two stage processes.  The language
01527      handler is consulted first and may decide to return in memory (eg
01528      class with copy constructor returned by value), this will cause
01529      the generic code to allocate space AND insert an initial leading
01530      argument.
01531 
01532      If the language code does not decide to pass in memory then the
01533      target code is consulted.
01534 
01535      If the language code decides to pass in memory we want to move
01536      the pointer inserted as the initial argument from the argument
01537      list and into X8, the conventional AArch64 struct return pointer
01538      register.
01539 
01540      This is slightly awkward, ideally the flag "lang_struct_return"
01541      would be passed to the targets implementation of push_dummy_call.
01542      Rather that change the target interface we call the language code
01543      directly ourselves.  */
01544 
01545   func_type = check_typedef (value_type (function));
01546 
01547   /* Dereference function pointer types.  */
01548   if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
01549     func_type = TYPE_TARGET_TYPE (func_type);
01550 
01551   gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
01552               || TYPE_CODE (func_type) == TYPE_CODE_METHOD);
01553 
01554   /* If language_pass_by_reference () returned true we will have been
01555      given an additional initial argument, a hidden pointer to the
01556      return slot in memory.  */
01557   return_type = TYPE_TARGET_TYPE (func_type);
01558   lang_struct_return = language_pass_by_reference (return_type);
01559 
01560   /* Set the return address.  For the AArch64, the return breakpoint
01561      is always at BP_ADDR.  */
01562   regcache_cooked_write_unsigned (regcache, AARCH64_LR_REGNUM, bp_addr);
01563 
01564   /* If we were given an initial argument for the return slot because
01565      lang_struct_return was true, lose it.  */
01566   if (lang_struct_return)
01567     {
01568       args++;
01569       nargs--;
01570     }
01571 
01572   /* The struct_return pointer occupies X8.  */
01573   if (struct_return || lang_struct_return)
01574     {
01575       if (aarch64_debug)
01576         fprintf_unfiltered (gdb_stdlog, "struct return in %s = 0x%s\n",
01577                             gdbarch_register_name
01578                             (gdbarch,
01579                              AARCH64_STRUCT_RETURN_REGNUM),
01580                             paddress (gdbarch, struct_addr));
01581       regcache_cooked_write_unsigned (regcache, AARCH64_STRUCT_RETURN_REGNUM,
01582                                       struct_addr);
01583     }
01584 
01585   for (argnum = 0; argnum < nargs; argnum++)
01586     {
01587       struct value *arg = args[argnum];
01588       struct type *arg_type;
01589       int len;
01590 
01591       arg_type = check_typedef (value_type (arg));
01592       len = TYPE_LENGTH (arg_type);
01593 
01594       switch (TYPE_CODE (arg_type))
01595         {
01596         case TYPE_CODE_INT:
01597         case TYPE_CODE_BOOL:
01598         case TYPE_CODE_CHAR:
01599         case TYPE_CODE_RANGE:
01600         case TYPE_CODE_ENUM:
01601           if (len < 4)
01602             {
01603               /* Promote to 32 bit integer.  */
01604               if (TYPE_UNSIGNED (arg_type))
01605                 arg_type = builtin_type (gdbarch)->builtin_uint32;
01606               else
01607                 arg_type = builtin_type (gdbarch)->builtin_int32;
01608               arg = value_cast (arg_type, arg);
01609             }
01610           pass_in_x_or_stack (gdbarch, regcache, &info, arg_type,
01611                               value_contents (arg));
01612           break;
01613 
01614         case TYPE_CODE_COMPLEX:
01615           if (info.nsrn <= 6)
01616             {
01617               const bfd_byte *buf = value_contents (arg);
01618               struct type *target_type =
01619                 check_typedef (TYPE_TARGET_TYPE (arg_type));
01620 
01621               pass_in_v (gdbarch, regcache, &info, buf);
01622               pass_in_v (gdbarch, regcache, &info,
01623                          buf + TYPE_LENGTH (target_type));
01624             }
01625           else
01626             {
01627               info.nsrn = 8;
01628               pass_on_stack (&info, arg_type, value_contents (arg));
01629             }
01630           break;
01631         case TYPE_CODE_FLT:
01632           pass_in_v_or_stack (gdbarch, regcache, &info, arg_type,
01633                               value_contents (arg));
01634           break;
01635 
01636         case TYPE_CODE_STRUCT:
01637         case TYPE_CODE_ARRAY:
01638         case TYPE_CODE_UNION:
01639           if (is_hfa (arg_type))
01640             {
01641               int elements = TYPE_NFIELDS (arg_type);
01642 
01643               /* Homogeneous Aggregates */
01644               if (info.nsrn + elements < 8)
01645                 {
01646                   int i;
01647 
01648                   for (i = 0; i < elements; i++)
01649                     {
01650                       /* We know that we have sufficient registers
01651                          available therefore this will never fallback
01652                          to the stack.  */
01653                       struct value *field =
01654                         value_primitive_field (arg, 0, i, arg_type);
01655                       struct type *field_type =
01656                         check_typedef (value_type (field));
01657 
01658                       pass_in_v_or_stack (gdbarch, regcache, &info, field_type,
01659                                           value_contents_writeable (field));
01660                     }
01661                 }
01662               else
01663                 {
01664                   info.nsrn = 8;
01665                   pass_on_stack (&info, arg_type, value_contents (arg));
01666                 }
01667             }
01668           else if (len > 16)
01669             {
01670               /* PCS B.7 Aggregates larger than 16 bytes are passed by
01671                  invisible reference.  */
01672 
01673               /* Allocate aligned storage.  */
01674               sp = align_down (sp - len, 16);
01675 
01676               /* Write the real data into the stack.  */
01677               write_memory (sp, value_contents (arg), len);
01678 
01679               /* Construct the indirection.  */
01680               arg_type = lookup_pointer_type (arg_type);
01681               arg = value_from_pointer (arg_type, sp);
01682               pass_in_x_or_stack (gdbarch, regcache, &info, arg_type,
01683                                   value_contents (arg));
01684             }
01685           else
01686             /* PCS C.15 / C.18 multiple values pass.  */
01687             pass_in_x_or_stack (gdbarch, regcache, &info, arg_type,
01688                                 value_contents (arg));
01689           break;
01690 
01691         default:
01692           pass_in_x_or_stack (gdbarch, regcache, &info, arg_type,
01693                               value_contents (arg));
01694           break;
01695         }
01696     }
01697 
01698   /* Make sure stack retains 16 byte alignment.  */
01699   if (info.nsaa & 15)
01700     sp -= 16 - (info.nsaa & 15);
01701 
01702   while (!VEC_empty (stack_item_t, info.si))
01703     {
01704       stack_item_t *si = VEC_last (stack_item_t, info.si);
01705 
01706       sp -= si->len;
01707       write_memory (sp, si->data, si->len);
01708       VEC_pop (stack_item_t, info.si);
01709     }
01710 
01711   VEC_free (stack_item_t, info.si);
01712 
01713   /* Finally, update the SP register.  */
01714   regcache_cooked_write_unsigned (regcache, AARCH64_SP_REGNUM, sp);
01715 
01716   return sp;
01717 }
01718 
01719 /* Implement the "frame_align" gdbarch method.  */
01720 
01721 static CORE_ADDR
01722 aarch64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
01723 {
01724   /* Align the stack to sixteen bytes.  */
01725   return sp & ~(CORE_ADDR) 15;
01726 }
01727 
01728 /* Return the type for an AdvSISD Q register.  */
01729 
01730 static struct type *
01731 aarch64_vnq_type (struct gdbarch *gdbarch)
01732 {
01733   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01734 
01735   if (tdep->vnq_type == NULL)
01736     {
01737       struct type *t;
01738       struct type *elem;
01739 
01740       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnq",
01741                                TYPE_CODE_UNION);
01742 
01743       elem = builtin_type (gdbarch)->builtin_uint128;
01744       append_composite_type_field (t, "u", elem);
01745 
01746       elem = builtin_type (gdbarch)->builtin_int128;
01747       append_composite_type_field (t, "s", elem);
01748 
01749       tdep->vnq_type = t;
01750     }
01751 
01752   return tdep->vnq_type;
01753 }
01754 
01755 /* Return the type for an AdvSISD D register.  */
01756 
01757 static struct type *
01758 aarch64_vnd_type (struct gdbarch *gdbarch)
01759 {
01760   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01761 
01762   if (tdep->vnd_type == NULL)
01763     {
01764       struct type *t;
01765       struct type *elem;
01766 
01767       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnd",
01768                                TYPE_CODE_UNION);
01769 
01770       elem = builtin_type (gdbarch)->builtin_double;
01771       append_composite_type_field (t, "f", elem);
01772 
01773       elem = builtin_type (gdbarch)->builtin_uint64;
01774       append_composite_type_field (t, "u", elem);
01775 
01776       elem = builtin_type (gdbarch)->builtin_int64;
01777       append_composite_type_field (t, "s", elem);
01778 
01779       tdep->vnd_type = t;
01780     }
01781 
01782   return tdep->vnd_type;
01783 }
01784 
01785 /* Return the type for an AdvSISD S register.  */
01786 
01787 static struct type *
01788 aarch64_vns_type (struct gdbarch *gdbarch)
01789 {
01790   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01791 
01792   if (tdep->vns_type == NULL)
01793     {
01794       struct type *t;
01795       struct type *elem;
01796 
01797       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vns",
01798                                TYPE_CODE_UNION);
01799 
01800       elem = builtin_type (gdbarch)->builtin_float;
01801       append_composite_type_field (t, "f", elem);
01802 
01803       elem = builtin_type (gdbarch)->builtin_uint32;
01804       append_composite_type_field (t, "u", elem);
01805 
01806       elem = builtin_type (gdbarch)->builtin_int32;
01807       append_composite_type_field (t, "s", elem);
01808 
01809       tdep->vns_type = t;
01810     }
01811 
01812   return tdep->vns_type;
01813 }
01814 
01815 /* Return the type for an AdvSISD H register.  */
01816 
01817 static struct type *
01818 aarch64_vnh_type (struct gdbarch *gdbarch)
01819 {
01820   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01821 
01822   if (tdep->vnh_type == NULL)
01823     {
01824       struct type *t;
01825       struct type *elem;
01826 
01827       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnh",
01828                                TYPE_CODE_UNION);
01829 
01830       elem = builtin_type (gdbarch)->builtin_uint16;
01831       append_composite_type_field (t, "u", elem);
01832 
01833       elem = builtin_type (gdbarch)->builtin_int16;
01834       append_composite_type_field (t, "s", elem);
01835 
01836       tdep->vnh_type = t;
01837     }
01838 
01839   return tdep->vnh_type;
01840 }
01841 
01842 /* Return the type for an AdvSISD B register.  */
01843 
01844 static struct type *
01845 aarch64_vnb_type (struct gdbarch *gdbarch)
01846 {
01847   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01848 
01849   if (tdep->vnb_type == NULL)
01850     {
01851       struct type *t;
01852       struct type *elem;
01853 
01854       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnb",
01855                                TYPE_CODE_UNION);
01856 
01857       elem = builtin_type (gdbarch)->builtin_uint8;
01858       append_composite_type_field (t, "u", elem);
01859 
01860       elem = builtin_type (gdbarch)->builtin_int8;
01861       append_composite_type_field (t, "s", elem);
01862 
01863       tdep->vnb_type = t;
01864     }
01865 
01866   return tdep->vnb_type;
01867 }
01868 
01869 /* Implement the "dwarf2_reg_to_regnum" gdbarch method.  */
01870 
01871 static int
01872 aarch64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
01873 {
01874   if (reg >= AARCH64_DWARF_X0 && reg <= AARCH64_DWARF_X0 + 30)
01875     return AARCH64_X0_REGNUM + reg - AARCH64_DWARF_X0;
01876 
01877   if (reg == AARCH64_DWARF_SP)
01878     return AARCH64_SP_REGNUM;
01879 
01880   if (reg >= AARCH64_DWARF_V0 && reg <= AARCH64_DWARF_V0 + 31)
01881     return AARCH64_V0_REGNUM + reg - AARCH64_DWARF_V0;
01882 
01883   return -1;
01884 }
01885 
01886 
01887 /* Implement the "print_insn" gdbarch method.  */
01888 
01889 static int
01890 aarch64_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
01891 {
01892   info->symbols = NULL;
01893   return print_insn_aarch64 (memaddr, info);
01894 }
01895 
01896 /* AArch64 BRK software debug mode instruction.
01897    Note that AArch64 code is always little-endian.
01898    1101.0100.0010.0000.0000.0000.0000.0000 = 0xd4200000.  */
01899 static const gdb_byte aarch64_default_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
01900 
01901 /* Implement the "breakpoint_from_pc" gdbarch method.  */
01902 
01903 static const gdb_byte *
01904 aarch64_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
01905                             int *lenptr)
01906 {
01907   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
01908 
01909   *lenptr = sizeof (aarch64_default_breakpoint);
01910   return aarch64_default_breakpoint;
01911 }
01912 
01913 /* Extract from an array REGS containing the (raw) register state a
01914    function return value of type TYPE, and copy that, in virtual
01915    format, into VALBUF.  */
01916 
01917 static void
01918 aarch64_extract_return_value (struct type *type, struct regcache *regs,
01919                               gdb_byte *valbuf)
01920 {
01921   struct gdbarch *gdbarch = get_regcache_arch (regs);
01922   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
01923 
01924   if (TYPE_CODE (type) == TYPE_CODE_FLT)
01925     {
01926       bfd_byte buf[V_REGISTER_SIZE];
01927       int len = TYPE_LENGTH (type);
01928 
01929       regcache_cooked_read (regs, AARCH64_V0_REGNUM, buf);
01930       memcpy (valbuf, buf, len);
01931     }
01932   else if (TYPE_CODE (type) == TYPE_CODE_INT
01933            || TYPE_CODE (type) == TYPE_CODE_CHAR
01934            || TYPE_CODE (type) == TYPE_CODE_BOOL
01935            || TYPE_CODE (type) == TYPE_CODE_PTR
01936            || TYPE_CODE (type) == TYPE_CODE_REF
01937            || TYPE_CODE (type) == TYPE_CODE_ENUM)
01938     {
01939       /* If the the type is a plain integer, then the access is
01940          straight-forward.  Otherwise we have to play around a bit
01941          more.  */
01942       int len = TYPE_LENGTH (type);
01943       int regno = AARCH64_X0_REGNUM;
01944       ULONGEST tmp;
01945 
01946       while (len > 0)
01947         {
01948           /* By using store_unsigned_integer we avoid having to do
01949              anything special for small big-endian values.  */
01950           regcache_cooked_read_unsigned (regs, regno++, &tmp);
01951           store_unsigned_integer (valbuf,
01952                                   (len > X_REGISTER_SIZE
01953                                    ? X_REGISTER_SIZE : len), byte_order, tmp);
01954           len -= X_REGISTER_SIZE;
01955           valbuf += X_REGISTER_SIZE;
01956         }
01957     }
01958   else if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
01959     {
01960       int regno = AARCH64_V0_REGNUM;
01961       bfd_byte buf[V_REGISTER_SIZE];
01962       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
01963       int len = TYPE_LENGTH (target_type);
01964 
01965       regcache_cooked_read (regs, regno, buf);
01966       memcpy (valbuf, buf, len);
01967       valbuf += len;
01968       regcache_cooked_read (regs, regno + 1, buf);
01969       memcpy (valbuf, buf, len);
01970       valbuf += len;
01971     }
01972   else if (is_hfa (type))
01973     {
01974       int elements = TYPE_NFIELDS (type);
01975       struct type *member_type = check_typedef (TYPE_FIELD_TYPE (type, 0));
01976       int len = TYPE_LENGTH (member_type);
01977       int i;
01978 
01979       for (i = 0; i < elements; i++)
01980         {
01981           int regno = AARCH64_V0_REGNUM + i;
01982           bfd_byte buf[X_REGISTER_SIZE];
01983 
01984           if (aarch64_debug)
01985             fprintf_unfiltered (gdb_stdlog,
01986                                 "read HFA return value element %d from %s\n",
01987                                 i + 1,
01988                                 gdbarch_register_name (gdbarch, regno));
01989           regcache_cooked_read (regs, regno, buf);
01990 
01991           memcpy (valbuf, buf, len);
01992           valbuf += len;
01993         }
01994     }
01995   else
01996     {
01997       /* For a structure or union the behaviour is as if the value had
01998          been stored to word-aligned memory and then loaded into
01999          registers with 64-bit load instruction(s).  */
02000       int len = TYPE_LENGTH (type);
02001       int regno = AARCH64_X0_REGNUM;
02002       bfd_byte buf[X_REGISTER_SIZE];
02003 
02004       while (len > 0)
02005         {
02006           regcache_cooked_read (regs, regno++, buf);
02007           memcpy (valbuf, buf, len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
02008           len -= X_REGISTER_SIZE;
02009           valbuf += X_REGISTER_SIZE;
02010         }
02011     }
02012 }
02013 
02014 
02015 /* Will a function return an aggregate type in memory or in a
02016    register?  Return 0 if an aggregate type can be returned in a
02017    register, 1 if it must be returned in memory.  */
02018 
02019 static int
02020 aarch64_return_in_memory (struct gdbarch *gdbarch, struct type *type)
02021 {
02022   int nRc;
02023   enum type_code code;
02024 
02025   CHECK_TYPEDEF (type);
02026 
02027   /* In the AArch64 ABI, "integer" like aggregate types are returned
02028      in registers.  For an aggregate type to be integer like, its size
02029      must be less than or equal to 4 * X_REGISTER_SIZE.  */
02030 
02031   if (is_hfa (type))
02032     {
02033       /* PCS B.5 If the argument is a Named HFA, then the argument is
02034          used unmodified.  */
02035       return 0;
02036     }
02037 
02038   if (TYPE_LENGTH (type) > 16)
02039     {
02040       /* PCS B.6 Aggregates larger than 16 bytes are passed by
02041          invisible reference.  */
02042 
02043       return 1;
02044     }
02045 
02046   return 0;
02047 }
02048 
02049 /* Write into appropriate registers a function return value of type
02050    TYPE, given in virtual format.  */
02051 
02052 static void
02053 aarch64_store_return_value (struct type *type, struct regcache *regs,
02054                             const gdb_byte *valbuf)
02055 {
02056   struct gdbarch *gdbarch = get_regcache_arch (regs);
02057   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02058 
02059   if (TYPE_CODE (type) == TYPE_CODE_FLT)
02060     {
02061       bfd_byte buf[V_REGISTER_SIZE];
02062       int len = TYPE_LENGTH (type);
02063 
02064       memcpy (buf, valbuf, len > V_REGISTER_SIZE ? V_REGISTER_SIZE : len);
02065       regcache_cooked_write (regs, AARCH64_V0_REGNUM, buf);
02066     }
02067   else if (TYPE_CODE (type) == TYPE_CODE_INT
02068            || TYPE_CODE (type) == TYPE_CODE_CHAR
02069            || TYPE_CODE (type) == TYPE_CODE_BOOL
02070            || TYPE_CODE (type) == TYPE_CODE_PTR
02071            || TYPE_CODE (type) == TYPE_CODE_REF
02072            || TYPE_CODE (type) == TYPE_CODE_ENUM)
02073     {
02074       if (TYPE_LENGTH (type) <= X_REGISTER_SIZE)
02075         {
02076           /* Values of one word or less are zero/sign-extended and
02077              returned in r0.  */
02078           bfd_byte tmpbuf[X_REGISTER_SIZE];
02079           LONGEST val = unpack_long (type, valbuf);
02080 
02081           store_signed_integer (tmpbuf, X_REGISTER_SIZE, byte_order, val);
02082           regcache_cooked_write (regs, AARCH64_X0_REGNUM, tmpbuf);
02083         }
02084       else
02085         {
02086           /* Integral values greater than one word are stored in
02087              consecutive registers starting with r0.  This will always
02088              be a multiple of the regiser size.  */
02089           int len = TYPE_LENGTH (type);
02090           int regno = AARCH64_X0_REGNUM;
02091 
02092           while (len > 0)
02093             {
02094               regcache_cooked_write (regs, regno++, valbuf);
02095               len -= X_REGISTER_SIZE;
02096               valbuf += X_REGISTER_SIZE;
02097             }
02098         }
02099     }
02100   else if (is_hfa (type))
02101     {
02102       int elements = TYPE_NFIELDS (type);
02103       struct type *member_type = check_typedef (TYPE_FIELD_TYPE (type, 0));
02104       int len = TYPE_LENGTH (member_type);
02105       int i;
02106 
02107       for (i = 0; i < elements; i++)
02108         {
02109           int regno = AARCH64_V0_REGNUM + i;
02110           bfd_byte tmpbuf[MAX_REGISTER_SIZE];
02111 
02112           if (aarch64_debug)
02113             fprintf_unfiltered (gdb_stdlog,
02114                                 "write HFA return value element %d to %s\n",
02115                                 i + 1,
02116                                 gdbarch_register_name (gdbarch, regno));
02117 
02118           memcpy (tmpbuf, valbuf, len);
02119           regcache_cooked_write (regs, regno, tmpbuf);
02120           valbuf += len;
02121         }
02122     }
02123   else
02124     {
02125       /* For a structure or union the behaviour is as if the value had
02126          been stored to word-aligned memory and then loaded into
02127          registers with 64-bit load instruction(s).  */
02128       int len = TYPE_LENGTH (type);
02129       int regno = AARCH64_X0_REGNUM;
02130       bfd_byte tmpbuf[X_REGISTER_SIZE];
02131 
02132       while (len > 0)
02133         {
02134           memcpy (tmpbuf, valbuf,
02135                   len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
02136           regcache_cooked_write (regs, regno++, tmpbuf);
02137           len -= X_REGISTER_SIZE;
02138           valbuf += X_REGISTER_SIZE;
02139         }
02140     }
02141 }
02142 
02143 /* Implement the "return_value" gdbarch method.  */
02144 
02145 static enum return_value_convention
02146 aarch64_return_value (struct gdbarch *gdbarch, struct value *func_value,
02147                       struct type *valtype, struct regcache *regcache,
02148                       gdb_byte *readbuf, const gdb_byte *writebuf)
02149 {
02150   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02151 
02152   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
02153       || TYPE_CODE (valtype) == TYPE_CODE_UNION
02154       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
02155     {
02156       if (aarch64_return_in_memory (gdbarch, valtype))
02157         {
02158           if (aarch64_debug)
02159             fprintf_unfiltered (gdb_stdlog, "return value in memory\n");
02160           return RETURN_VALUE_STRUCT_CONVENTION;
02161         }
02162     }
02163 
02164   if (writebuf)
02165     aarch64_store_return_value (valtype, regcache, writebuf);
02166 
02167   if (readbuf)
02168     aarch64_extract_return_value (valtype, regcache, readbuf);
02169 
02170   if (aarch64_debug)
02171     fprintf_unfiltered (gdb_stdlog, "return value in registers\n");
02172 
02173   return RETURN_VALUE_REGISTER_CONVENTION;
02174 }
02175 
02176 /* Implement the "get_longjmp_target" gdbarch method.  */
02177 
02178 static int
02179 aarch64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
02180 {
02181   CORE_ADDR jb_addr;
02182   gdb_byte buf[X_REGISTER_SIZE];
02183   struct gdbarch *gdbarch = get_frame_arch (frame);
02184   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02185   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02186 
02187   jb_addr = get_frame_register_unsigned (frame, AARCH64_X0_REGNUM);
02188 
02189   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
02190                           X_REGISTER_SIZE))
02191     return 0;
02192 
02193   *pc = extract_unsigned_integer (buf, X_REGISTER_SIZE, byte_order);
02194   return 1;
02195 }
02196 
02197 
02198 /* Return the pseudo register name corresponding to register regnum.  */
02199 
02200 static const char *
02201 aarch64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
02202 {
02203   static const char *const q_name[] =
02204     {
02205       "q0", "q1", "q2", "q3",
02206       "q4", "q5", "q6", "q7",
02207       "q8", "q9", "q10", "q11",
02208       "q12", "q13", "q14", "q15",
02209       "q16", "q17", "q18", "q19",
02210       "q20", "q21", "q22", "q23",
02211       "q24", "q25", "q26", "q27",
02212       "q28", "q29", "q30", "q31",
02213     };
02214 
02215   static const char *const d_name[] =
02216     {
02217       "d0", "d1", "d2", "d3",
02218       "d4", "d5", "d6", "d7",
02219       "d8", "d9", "d10", "d11",
02220       "d12", "d13", "d14", "d15",
02221       "d16", "d17", "d18", "d19",
02222       "d20", "d21", "d22", "d23",
02223       "d24", "d25", "d26", "d27",
02224       "d28", "d29", "d30", "d31",
02225     };
02226 
02227   static const char *const s_name[] =
02228     {
02229       "s0", "s1", "s2", "s3",
02230       "s4", "s5", "s6", "s7",
02231       "s8", "s9", "s10", "s11",
02232       "s12", "s13", "s14", "s15",
02233       "s16", "s17", "s18", "s19",
02234       "s20", "s21", "s22", "s23",
02235       "s24", "s25", "s26", "s27",
02236       "s28", "s29", "s30", "s31",
02237     };
02238 
02239   static const char *const h_name[] =
02240     {
02241       "h0", "h1", "h2", "h3",
02242       "h4", "h5", "h6", "h7",
02243       "h8", "h9", "h10", "h11",
02244       "h12", "h13", "h14", "h15",
02245       "h16", "h17", "h18", "h19",
02246       "h20", "h21", "h22", "h23",
02247       "h24", "h25", "h26", "h27",
02248       "h28", "h29", "h30", "h31",
02249     };
02250 
02251   static const char *const b_name[] =
02252     {
02253       "b0", "b1", "b2", "b3",
02254       "b4", "b5", "b6", "b7",
02255       "b8", "b9", "b10", "b11",
02256       "b12", "b13", "b14", "b15",
02257       "b16", "b17", "b18", "b19",
02258       "b20", "b21", "b22", "b23",
02259       "b24", "b25", "b26", "b27",
02260       "b28", "b29", "b30", "b31",
02261     };
02262 
02263   regnum -= gdbarch_num_regs (gdbarch);
02264 
02265   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
02266     return q_name[regnum - AARCH64_Q0_REGNUM];
02267 
02268   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
02269     return d_name[regnum - AARCH64_D0_REGNUM];
02270 
02271   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
02272     return s_name[regnum - AARCH64_S0_REGNUM];
02273 
02274   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
02275     return h_name[regnum - AARCH64_H0_REGNUM];
02276 
02277   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
02278     return b_name[regnum - AARCH64_B0_REGNUM];
02279 
02280   internal_error (__FILE__, __LINE__,
02281                   _("aarch64_pseudo_register_name: bad register number %d"),
02282                   regnum);
02283 }
02284 
02285 /* Implement the "pseudo_register_type" tdesc_arch_data method.  */
02286 
02287 static struct type *
02288 aarch64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
02289 {
02290   regnum -= gdbarch_num_regs (gdbarch);
02291 
02292   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
02293     return aarch64_vnq_type (gdbarch);
02294 
02295   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
02296     return aarch64_vnd_type (gdbarch);
02297 
02298   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
02299     return aarch64_vns_type (gdbarch);
02300 
02301   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
02302     return aarch64_vnh_type (gdbarch);
02303 
02304   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
02305     return aarch64_vnb_type (gdbarch);
02306 
02307   internal_error (__FILE__, __LINE__,
02308                   _("aarch64_pseudo_register_type: bad register number %d"),
02309                   regnum);
02310 }
02311 
02312 /* Implement the "pseudo_register_reggroup_p" tdesc_arch_data method.  */
02313 
02314 static int
02315 aarch64_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
02316                                     struct reggroup *group)
02317 {
02318   regnum -= gdbarch_num_regs (gdbarch);
02319 
02320   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
02321     return group == all_reggroup || group == vector_reggroup;
02322   else if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
02323     return (group == all_reggroup || group == vector_reggroup
02324             || group == float_reggroup);
02325   else if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
02326     return (group == all_reggroup || group == vector_reggroup
02327             || group == float_reggroup);
02328   else if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
02329     return group == all_reggroup || group == vector_reggroup;
02330   else if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
02331     return group == all_reggroup || group == vector_reggroup;
02332 
02333   return group == all_reggroup;
02334 }
02335 
02336 /* Implement the "pseudo_register_read_value" gdbarch method.  */
02337 
02338 static struct value *
02339 aarch64_pseudo_read_value (struct gdbarch *gdbarch,
02340                            struct regcache *regcache,
02341                            int regnum)
02342 {
02343   gdb_byte reg_buf[MAX_REGISTER_SIZE];
02344   struct value *result_value;
02345   gdb_byte *buf;
02346 
02347   result_value = allocate_value (register_type (gdbarch, regnum));
02348   VALUE_LVAL (result_value) = lval_register;
02349   VALUE_REGNUM (result_value) = regnum;
02350   buf = value_contents_raw (result_value);
02351 
02352   regnum -= gdbarch_num_regs (gdbarch);
02353 
02354   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
02355     {
02356       enum register_status status;
02357       unsigned v_regnum;
02358 
02359       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_Q0_REGNUM;
02360       status = regcache_raw_read (regcache, v_regnum, reg_buf);
02361       if (status != REG_VALID)
02362         mark_value_bytes_unavailable (result_value, 0,
02363                                       TYPE_LENGTH (value_type (result_value)));
02364       else
02365         memcpy (buf, reg_buf, Q_REGISTER_SIZE);
02366       return result_value;
02367     }
02368 
02369   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
02370     {
02371       enum register_status status;
02372       unsigned v_regnum;
02373 
02374       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_D0_REGNUM;
02375       status = regcache_raw_read (regcache, v_regnum, reg_buf);
02376       if (status != REG_VALID)
02377         mark_value_bytes_unavailable (result_value, 0,
02378                                       TYPE_LENGTH (value_type (result_value)));
02379       else
02380         memcpy (buf, reg_buf, D_REGISTER_SIZE);
02381       return result_value;
02382     }
02383 
02384   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
02385     {
02386       enum register_status status;
02387       unsigned v_regnum;
02388 
02389       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_S0_REGNUM;
02390       status = regcache_raw_read (regcache, v_regnum, reg_buf);
02391       memcpy (buf, reg_buf, S_REGISTER_SIZE);
02392       return result_value;
02393     }
02394 
02395   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
02396     {
02397       enum register_status status;
02398       unsigned v_regnum;
02399 
02400       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_H0_REGNUM;
02401       status = regcache_raw_read (regcache, v_regnum, reg_buf);
02402       if (status != REG_VALID)
02403         mark_value_bytes_unavailable (result_value, 0,
02404                                       TYPE_LENGTH (value_type (result_value)));
02405       else
02406         memcpy (buf, reg_buf, H_REGISTER_SIZE);
02407       return result_value;
02408     }
02409 
02410   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
02411     {
02412       enum register_status status;
02413       unsigned v_regnum;
02414 
02415       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_B0_REGNUM;
02416       status = regcache_raw_read (regcache, v_regnum, reg_buf);
02417       if (status != REG_VALID)
02418         mark_value_bytes_unavailable (result_value, 0,
02419                                       TYPE_LENGTH (value_type (result_value)));
02420       else
02421         memcpy (buf, reg_buf, B_REGISTER_SIZE);
02422       return result_value;
02423     }
02424 
02425   gdb_assert_not_reached ("regnum out of bound");
02426 }
02427 
02428 /* Implement the "pseudo_register_write" gdbarch method.  */
02429 
02430 static void
02431 aarch64_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
02432                       int regnum, const gdb_byte *buf)
02433 {
02434   gdb_byte reg_buf[MAX_REGISTER_SIZE];
02435 
02436   /* Ensure the register buffer is zero, we want gdb writes of the
02437      various 'scalar' pseudo registers to behavior like architectural
02438      writes, register width bytes are written the remainder are set to
02439      zero.  */
02440   memset (reg_buf, 0, sizeof (reg_buf));
02441 
02442   regnum -= gdbarch_num_regs (gdbarch);
02443 
02444   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
02445     {
02446       /* pseudo Q registers */
02447       unsigned v_regnum;
02448 
02449       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_Q0_REGNUM;
02450       memcpy (reg_buf, buf, Q_REGISTER_SIZE);
02451       regcache_raw_write (regcache, v_regnum, reg_buf);
02452       return;
02453     }
02454 
02455   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
02456     {
02457       /* pseudo D registers */
02458       unsigned v_regnum;
02459 
02460       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_D0_REGNUM;
02461       memcpy (reg_buf, buf, D_REGISTER_SIZE);
02462       regcache_raw_write (regcache, v_regnum, reg_buf);
02463       return;
02464     }
02465 
02466   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
02467     {
02468       unsigned v_regnum;
02469 
02470       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_S0_REGNUM;
02471       memcpy (reg_buf, buf, S_REGISTER_SIZE);
02472       regcache_raw_write (regcache, v_regnum, reg_buf);
02473       return;
02474     }
02475 
02476   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
02477     {
02478       /* pseudo H registers */
02479       unsigned v_regnum;
02480 
02481       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_H0_REGNUM;
02482       memcpy (reg_buf, buf, H_REGISTER_SIZE);
02483       regcache_raw_write (regcache, v_regnum, reg_buf);
02484       return;
02485     }
02486 
02487   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
02488     {
02489       /* pseudo B registers */
02490       unsigned v_regnum;
02491 
02492       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_B0_REGNUM;
02493       memcpy (reg_buf, buf, B_REGISTER_SIZE);
02494       regcache_raw_write (regcache, v_regnum, reg_buf);
02495       return;
02496     }
02497 
02498   gdb_assert_not_reached ("regnum out of bound");
02499 }
02500 
02501 /* Callback function for user_reg_add.  */
02502 
02503 static struct value *
02504 value_of_aarch64_user_reg (struct frame_info *frame, const void *baton)
02505 {
02506   const int *reg_p = baton;
02507 
02508   return value_of_register (*reg_p, frame);
02509 }
02510 
02511 
02512 /* Initialize the current architecture based on INFO.  If possible,
02513    re-use an architecture from ARCHES, which is a list of
02514    architectures already created during this debugging session.
02515 
02516    Called e.g. at program startup, when reading a core file, and when
02517    reading a binary file.  */
02518 
02519 static struct gdbarch *
02520 aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
02521 {
02522   struct gdbarch_tdep *tdep;
02523   struct gdbarch *gdbarch;
02524   struct gdbarch_list *best_arch;
02525   struct tdesc_arch_data *tdesc_data = NULL;
02526   const struct target_desc *tdesc = info.target_desc;
02527   int i;
02528   int have_fpa_registers = 1;
02529   int valid_p = 1;
02530   const struct tdesc_feature *feature;
02531   int num_regs = 0;
02532   int num_pseudo_regs = 0;
02533 
02534   /* Ensure we always have a target descriptor.  */
02535   if (!tdesc_has_registers (tdesc))
02536     tdesc = tdesc_aarch64;
02537 
02538   gdb_assert (tdesc);
02539 
02540   feature = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.core");
02541 
02542   if (feature == NULL)
02543     return NULL;
02544 
02545   tdesc_data = tdesc_data_alloc ();
02546 
02547   /* Validate the descriptor provides the mandatory core R registers
02548      and allocate their numbers.  */
02549   for (i = 0; i < ARRAY_SIZE (aarch64_r_register_names); i++)
02550     valid_p &=
02551       tdesc_numbered_register (feature, tdesc_data, AARCH64_X0_REGNUM + i,
02552                                aarch64_r_register_names[i]);
02553 
02554   num_regs = AARCH64_X0_REGNUM + i;
02555 
02556   /* Look for the V registers.  */
02557   feature = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.fpu");
02558   if (feature)
02559     {
02560       /* Validate the descriptor provides the mandatory V registers
02561          and allocate their numbers.  */
02562       for (i = 0; i < ARRAY_SIZE (aarch64_v_register_names); i++)
02563         valid_p &=
02564           tdesc_numbered_register (feature, tdesc_data, AARCH64_V0_REGNUM + i,
02565                                    aarch64_v_register_names[i]);
02566 
02567       num_regs = AARCH64_V0_REGNUM + i;
02568 
02569       num_pseudo_regs += 32;    /* add the Qn scalar register pseudos */
02570       num_pseudo_regs += 32;    /* add the Dn scalar register pseudos */
02571       num_pseudo_regs += 32;    /* add the Sn scalar register pseudos */
02572       num_pseudo_regs += 32;    /* add the Hn scalar register pseudos */
02573       num_pseudo_regs += 32;    /* add the Bn scalar register pseudos */
02574     }
02575 
02576   if (!valid_p)
02577     {
02578       tdesc_data_cleanup (tdesc_data);
02579       return NULL;
02580     }
02581 
02582   /* AArch64 code is always little-endian.  */
02583   info.byte_order_for_code = BFD_ENDIAN_LITTLE;
02584 
02585   /* If there is already a candidate, use it.  */
02586   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
02587        best_arch != NULL;
02588        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
02589     {
02590       /* Found a match.  */
02591       break;
02592     }
02593 
02594   if (best_arch != NULL)
02595     {
02596       if (tdesc_data != NULL)
02597         tdesc_data_cleanup (tdesc_data);
02598       return best_arch->gdbarch;
02599     }
02600 
02601   tdep = xcalloc (1, sizeof (struct gdbarch_tdep));
02602   gdbarch = gdbarch_alloc (&info, tdep);
02603 
02604   /* This should be low enough for everything.  */
02605   tdep->lowest_pc = 0x20;
02606   tdep->jb_pc = -1;             /* Longjump support not enabled by default.  */
02607   tdep->jb_elt_size = 8;
02608 
02609   set_gdbarch_push_dummy_call (gdbarch, aarch64_push_dummy_call);
02610   set_gdbarch_frame_align (gdbarch, aarch64_frame_align);
02611 
02612   /* Frame handling.  */
02613   set_gdbarch_dummy_id (gdbarch, aarch64_dummy_id);
02614   set_gdbarch_unwind_pc (gdbarch, aarch64_unwind_pc);
02615   set_gdbarch_unwind_sp (gdbarch, aarch64_unwind_sp);
02616 
02617   /* Advance PC across function entry code.  */
02618   set_gdbarch_skip_prologue (gdbarch, aarch64_skip_prologue);
02619 
02620   /* The stack grows downward.  */
02621   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
02622 
02623   /* Breakpoint manipulation.  */
02624   set_gdbarch_breakpoint_from_pc (gdbarch, aarch64_breakpoint_from_pc);
02625   set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
02626   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
02627 
02628   /* Information about registers, etc.  */
02629   set_gdbarch_sp_regnum (gdbarch, AARCH64_SP_REGNUM);
02630   set_gdbarch_pc_regnum (gdbarch, AARCH64_PC_REGNUM);
02631   set_gdbarch_num_regs (gdbarch, num_regs);
02632 
02633   set_gdbarch_num_pseudo_regs (gdbarch, num_pseudo_regs);
02634   set_gdbarch_pseudo_register_read_value (gdbarch, aarch64_pseudo_read_value);
02635   set_gdbarch_pseudo_register_write (gdbarch, aarch64_pseudo_write);
02636   set_tdesc_pseudo_register_name (gdbarch, aarch64_pseudo_register_name);
02637   set_tdesc_pseudo_register_type (gdbarch, aarch64_pseudo_register_type);
02638   set_tdesc_pseudo_register_reggroup_p (gdbarch,
02639                                         aarch64_pseudo_register_reggroup_p);
02640 
02641   /* ABI */
02642   set_gdbarch_short_bit (gdbarch, 16);
02643   set_gdbarch_int_bit (gdbarch, 32);
02644   set_gdbarch_float_bit (gdbarch, 32);
02645   set_gdbarch_double_bit (gdbarch, 64);
02646   set_gdbarch_long_double_bit (gdbarch, 128);
02647   set_gdbarch_long_bit (gdbarch, 64);
02648   set_gdbarch_long_long_bit (gdbarch, 64);
02649   set_gdbarch_ptr_bit (gdbarch, 64);
02650   set_gdbarch_char_signed (gdbarch, 0);
02651   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
02652   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
02653   set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
02654 
02655   /* Internal <-> external register number maps.  */
02656   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, aarch64_dwarf_reg_to_regnum);
02657 
02658   /* Returning results.  */
02659   set_gdbarch_return_value (gdbarch, aarch64_return_value);
02660 
02661   /* Disassembly.  */
02662   set_gdbarch_print_insn (gdbarch, aarch64_gdb_print_insn);
02663 
02664   /* Virtual tables.  */
02665   set_gdbarch_vbit_in_delta (gdbarch, 1);
02666 
02667   /* Hook in the ABI-specific overrides, if they have been registered.  */
02668   info.target_desc = tdesc;
02669   info.tdep_info = (void *) tdesc_data;
02670   gdbarch_init_osabi (info, gdbarch);
02671 
02672   dwarf2_frame_set_init_reg (gdbarch, aarch64_dwarf2_frame_init_reg);
02673 
02674   /* Add some default predicates.  */
02675   frame_unwind_append_unwinder (gdbarch, &aarch64_stub_unwind);
02676   dwarf2_append_unwinders (gdbarch);
02677   frame_unwind_append_unwinder (gdbarch, &aarch64_prologue_unwind);
02678 
02679   frame_base_set_default (gdbarch, &aarch64_normal_base);
02680 
02681   /* Now we have tuned the configuration, set a few final things,
02682      based on what the OS ABI has told us.  */
02683 
02684   if (tdep->jb_pc >= 0)
02685     set_gdbarch_get_longjmp_target (gdbarch, aarch64_get_longjmp_target);
02686 
02687   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
02688 
02689   /* Add standard register aliases.  */
02690   for (i = 0; i < ARRAY_SIZE (aarch64_register_aliases); i++)
02691     user_reg_add (gdbarch, aarch64_register_aliases[i].name,
02692                   value_of_aarch64_user_reg,
02693                   &aarch64_register_aliases[i].regnum);
02694 
02695   return gdbarch;
02696 }
02697 
02698 static void
02699 aarch64_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
02700 {
02701   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02702 
02703   if (tdep == NULL)
02704     return;
02705 
02706   fprintf_unfiltered (file, _("aarch64_dump_tdep: Lowest pc = 0x%s"),
02707                       paddress (gdbarch, tdep->lowest_pc));
02708 }
02709 
02710 /* Suppress warning from -Wmissing-prototypes.  */
02711 extern initialize_file_ftype _initialize_aarch64_tdep;
02712 
02713 void
02714 _initialize_aarch64_tdep (void)
02715 {
02716   gdbarch_register (bfd_arch_aarch64, aarch64_gdbarch_init,
02717                     aarch64_dump_tdep);
02718 
02719   initialize_tdesc_aarch64 ();
02720 
02721   /* Debug this file's internals.  */
02722   add_setshow_boolean_cmd ("aarch64", class_maintenance, &aarch64_debug, _("\
02723 Set AArch64 debugging."), _("\
02724 Show AArch64 debugging."), _("\
02725 When on, AArch64 specific debugging is enabled."),
02726                             NULL,
02727                             show_aarch64_debug,
02728                             &setdebuglist, &showdebuglist);
02729 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines