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