]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/amd64-windows-tdep.c
gdb, gdbserver, gdbsupport: remove includes of early headers
[thirdparty/binutils-gdb.git] / gdb / amd64-windows-tdep.c
CommitLineData
1d506c26 1/* Copyright (C) 2009-2024 Free Software Foundation, Inc.
d0761299
JB
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
4de283e4 18#include "osabi.h"
d0761299 19#include "amd64-tdep.h"
268a13a5 20#include "gdbsupport/x86-xstate.h"
d55e5aa6 21#include "gdbtypes.h"
4de283e4 22#include "gdbcore.h"
d55e5aa6 23#include "regcache.h"
d55e5aa6 24#include "windows-tdep.h"
4de283e4
TT
25#include "frame.h"
26#include "objfiles.h"
27#include "frame-unwind.h"
28#include "coff/internal.h"
29#include "coff/i386.h"
30#include "coff/pe.h"
31#include "libcoff.h"
32#include "value.h"
33#include <algorithm>
ba581dc1
JB
34
35/* The registers used to pass integer arguments during a function call. */
36static int amd64_windows_dummy_call_integer_regs[] =
37{
38 AMD64_RCX_REGNUM, /* %rcx */
39 AMD64_RDX_REGNUM, /* %rdx */
5b856f36
PM
40 AMD64_R8_REGNUM, /* %r8 */
41 AMD64_R9_REGNUM /* %r9 */
ba581dc1
JB
42};
43
aff9d387
JT
44/* This vector maps GDB's idea of a register's number into an offset into
45 the Windows API CONTEXT structure. */
46static int amd64_windows_gregset_reg_offset[] =
47{
48 120, /* Rax */
49 144, /* Rbx */
50 128, /* Rcx */
51 136, /* Rdx */
52 168, /* Rsi */
53 176, /* Rdi */
54 160, /* Rbp */
55 152, /* Rsp */
56 184, /* R8 */
57 192, /* R9 */
58 200, /* R10 */
59 208, /* R11 */
60 216, /* R12 */
61 224, /* R13 */
62 232, /* R14 */
63 240, /* R15 */
64 248, /* Rip */
65 68, /* EFlags */
66 56, /* SegCs */
67 66, /* SegSs */
68 58, /* SegDs */
69 60, /* SegEs */
70 62, /* SegFs */
71 64, /* SegGs */
72 288, /* FloatSave.FloatRegisters[0] */
73 304, /* FloatSave.FloatRegisters[1] */
74 320, /* FloatSave.FloatRegisters[2] */
75 336, /* FloatSave.FloatRegisters[3] */
76 352, /* FloatSave.FloatRegisters[4] */
77 368, /* FloatSave.FloatRegisters[5] */
78 384, /* FloatSave.FloatRegisters[6] */
79 400, /* FloatSave.FloatRegisters[7] */
80 256, /* FloatSave.ControlWord */
81 258, /* FloatSave.StatusWord */
82 260, /* FloatSave.TagWord */
83 268, /* FloatSave.ErrorSelector */
84 264, /* FloatSave.ErrorOffset */
85 276, /* FloatSave.DataSelector */
86 272, /* FloatSave.DataOffset */
87 268, /* FloatSave.ErrorSelector */
88 416, /* Xmm0 */
89 432, /* Xmm1 */
90 448, /* Xmm2 */
91 464, /* Xmm3 */
92 480, /* Xmm4 */
93 496, /* Xmm5 */
94 512, /* Xmm6 */
95 528, /* Xmm7 */
96 544, /* Xmm8 */
97 560, /* Xmm9 */
98 576, /* Xmm10 */
99 592, /* Xmm11 */
100 608, /* Xmm12 */
101 624, /* Xmm13 */
102 640, /* Xmm14 */
103 656, /* Xmm15 */
104 280, /* FloatSave.MxCsr */
105};
106
7d155da3
JT
107#define AMD64_WINDOWS_SIZEOF_GREGSET 1232
108
20c2e3e0
JB
109/* Return nonzero if an argument of type TYPE should be passed
110 via one of the integer registers. */
ba581dc1 111
20c2e3e0
JB
112static int
113amd64_windows_passed_by_integer_register (struct type *type)
ba581dc1 114{
78134374 115 switch (type->code ())
ba581dc1 116 {
20c2e3e0
JB
117 case TYPE_CODE_INT:
118 case TYPE_CODE_ENUM:
119 case TYPE_CODE_BOOL:
120 case TYPE_CODE_RANGE:
121 case TYPE_CODE_CHAR:
122 case TYPE_CODE_PTR:
123 case TYPE_CODE_REF:
aa006118 124 case TYPE_CODE_RVALUE_REF:
ba581dc1
JB
125 case TYPE_CODE_STRUCT:
126 case TYPE_CODE_UNION:
cd096ec8 127 case TYPE_CODE_COMPLEX:
df86565b
SM
128 return (type->length () == 1
129 || type->length () == 2
130 || type->length () == 4
131 || type->length () == 8);
ba581dc1
JB
132
133 default:
20c2e3e0 134 return 0;
ba581dc1
JB
135 }
136}
d0761299 137
20c2e3e0
JB
138/* Return nonzero if an argument of type TYPE should be passed
139 via one of the XMM registers. */
140
141static int
142amd64_windows_passed_by_xmm_register (struct type *type)
143{
78134374
SM
144 return ((type->code () == TYPE_CODE_FLT
145 || type->code () == TYPE_CODE_DECFLOAT)
df86565b 146 && (type->length () == 4 || type->length () == 8));
20c2e3e0
JB
147}
148
149/* Return non-zero iff an argument of the given TYPE should be passed
150 by pointer. */
151
152static int
153amd64_windows_passed_by_pointer (struct type *type)
154{
155 if (amd64_windows_passed_by_integer_register (type))
156 return 0;
157
158 if (amd64_windows_passed_by_xmm_register (type))
159 return 0;
160
161 return 1;
162}
163
164/* For each argument that should be passed by pointer, reserve some
165 stack space, store a copy of the argument on the stack, and replace
166 the argument by its address. Return the new Stack Pointer value.
167
168 NARGS is the number of arguments. ARGS is the array containing
169 the value of each argument. SP is value of the Stack Pointer. */
170
171static CORE_ADDR
172amd64_windows_adjust_args_passed_by_pointer (struct value **args,
173 int nargs, CORE_ADDR sp)
174{
175 int i;
176
177 for (i = 0; i < nargs; i++)
d0c97917 178 if (amd64_windows_passed_by_pointer (args[i]->type ()))
20c2e3e0 179 {
d0c97917 180 struct type *type = args[i]->type ();
efaf1ae0 181 const gdb_byte *valbuf = args[i]->contents ().data ();
df86565b 182 const int len = type->length ();
20c2e3e0
JB
183
184 /* Store a copy of that argument on the stack, aligned to
185 a 16 bytes boundary, and then use the copy's address as
186 the argument. */
187
188 sp -= len;
189 sp &= ~0xf;
190 write_memory (sp, valbuf, len);
191
192 args[i]
193 = value_addr (value_from_contents_and_address (type, valbuf, sp));
194 }
195
196 return sp;
197}
198
199/* Store the value of ARG in register REGNO (right-justified).
200 REGCACHE is the register cache. */
201
202static void
203amd64_windows_store_arg_in_reg (struct regcache *regcache,
204 struct value *arg, int regno)
205{
d0c97917 206 struct type *type = arg->type ();
efaf1ae0 207 const gdb_byte *valbuf = arg->contents ().data ();
20c2e3e0
JB
208 gdb_byte buf[8];
209
df86565b 210 gdb_assert (type->length () <= 8);
20c2e3e0 211 memset (buf, 0, sizeof buf);
df86565b 212 memcpy (buf, valbuf, std::min (type->length (), (ULONGEST) 8));
b66f5587 213 regcache->cooked_write (regno, buf);
20c2e3e0
JB
214}
215
216/* Push the arguments for an inferior function call, and return
217 the updated value of the SP (Stack Pointer).
218
219 All arguments are identical to the arguments used in
220 amd64_windows_push_dummy_call. */
221
222static CORE_ADDR
223amd64_windows_push_arguments (struct regcache *regcache, int nargs,
224 struct value **args, CORE_ADDR sp,
cf84fa6b 225 function_call_return_method return_method)
20c2e3e0
JB
226{
227 int reg_idx = 0;
228 int i;
8d749320 229 struct value **stack_args = XALLOCAVEC (struct value *, nargs);
20c2e3e0
JB
230 int num_stack_args = 0;
231 int num_elements = 0;
232 int element = 0;
233
234 /* First, handle the arguments passed by pointer.
235
236 These arguments are replaced by pointers to a copy we are making
237 in inferior memory. So use a copy of the ARGS table, to avoid
238 modifying the original one. */
239 {
8d749320 240 struct value **args1 = XALLOCAVEC (struct value *, nargs);
20c2e3e0
JB
241
242 memcpy (args1, args, nargs * sizeof (struct value *));
243 sp = amd64_windows_adjust_args_passed_by_pointer (args1, nargs, sp);
244 args = args1;
245 }
246
247 /* Reserve a register for the "hidden" argument. */
cf84fa6b 248 if (return_method == return_method_struct)
20c2e3e0
JB
249 reg_idx++;
250
251 for (i = 0; i < nargs; i++)
252 {
d0c97917 253 struct type *type = args[i]->type ();
df86565b 254 int len = type->length ();
20c2e3e0
JB
255 int on_stack_p = 1;
256
257 if (reg_idx < ARRAY_SIZE (amd64_windows_dummy_call_integer_regs))
258 {
259 if (amd64_windows_passed_by_integer_register (type))
260 {
261 amd64_windows_store_arg_in_reg
262 (regcache, args[i],
263 amd64_windows_dummy_call_integer_regs[reg_idx]);
264 on_stack_p = 0;
265 reg_idx++;
266 }
267 else if (amd64_windows_passed_by_xmm_register (type))
268 {
269 amd64_windows_store_arg_in_reg
dda83cd7 270 (regcache, args[i], AMD64_XMM0_REGNUM + reg_idx);
20c2e3e0
JB
271 /* In case of varargs, these parameters must also be
272 passed via the integer registers. */
273 amd64_windows_store_arg_in_reg
274 (regcache, args[i],
275 amd64_windows_dummy_call_integer_regs[reg_idx]);
276 on_stack_p = 0;
277 reg_idx++;
278 }
279 }
280
281 if (on_stack_p)
282 {
283 num_elements += ((len + 7) / 8);
284 stack_args[num_stack_args++] = args[i];
285 }
286 }
287
288 /* Allocate space for the arguments on the stack, keeping it
289 aligned on a 16 byte boundary. */
290 sp -= num_elements * 8;
291 sp &= ~0xf;
292
293 /* Write out the arguments to the stack. */
294 for (i = 0; i < num_stack_args; i++)
295 {
d0c97917 296 struct type *type = stack_args[i]->type ();
efaf1ae0 297 const gdb_byte *valbuf = stack_args[i]->contents ().data ();
20c2e3e0 298
df86565b
SM
299 write_memory (sp + element * 8, valbuf, type->length ());
300 element += ((type->length () + 7) / 8);
20c2e3e0
JB
301 }
302
303 return sp;
304}
305
306/* Implement the "push_dummy_call" gdbarch method. */
307
308static CORE_ADDR
309amd64_windows_push_dummy_call
310 (struct gdbarch *gdbarch, struct value *function,
311 struct regcache *regcache, CORE_ADDR bp_addr,
cf84fa6b
AH
312 int nargs, struct value **args, CORE_ADDR sp,
313 function_call_return_method return_method, CORE_ADDR struct_addr)
20c2e3e0
JB
314{
315 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
316 gdb_byte buf[8];
317
318 /* Pass arguments. */
319 sp = amd64_windows_push_arguments (regcache, nargs, args, sp,
cf84fa6b 320 return_method);
20c2e3e0
JB
321
322 /* Pass "hidden" argument". */
cf84fa6b 323 if (return_method == return_method_struct)
20c2e3e0
JB
324 {
325 /* The "hidden" argument is passed throught the first argument
dda83cd7 326 register. */
20c2e3e0
JB
327 const int arg_regnum = amd64_windows_dummy_call_integer_regs[0];
328
329 store_unsigned_integer (buf, 8, byte_order, struct_addr);
b66f5587 330 regcache->cooked_write (arg_regnum, buf);
20c2e3e0
JB
331 }
332
333 /* Reserve some memory on the stack for the integer-parameter
334 registers, as required by the ABI. */
335 sp -= ARRAY_SIZE (amd64_windows_dummy_call_integer_regs) * 8;
336
337 /* Store return address. */
338 sp -= 8;
339 store_unsigned_integer (buf, 8, byte_order, bp_addr);
340 write_memory (sp, buf, 8);
341
342 /* Update the stack pointer... */
343 store_unsigned_integer (buf, 8, byte_order, sp);
b66f5587 344 regcache->cooked_write (AMD64_RSP_REGNUM, buf);
20c2e3e0
JB
345
346 /* ...and fake a frame pointer. */
b66f5587 347 regcache->cooked_write (AMD64_RBP_REGNUM, buf);
20c2e3e0
JB
348
349 return sp + 16;
350}
351
cba6fab5
JB
352/* Implement the "return_value" gdbarch method for amd64-windows. */
353
354static enum return_value_convention
6a3a010b 355amd64_windows_return_value (struct gdbarch *gdbarch, struct value *function,
cba6fab5 356 struct type *type, struct regcache *regcache,
5cb0f2d5 357 struct value **read_value, const gdb_byte *writebuf)
cba6fab5 358{
df86565b 359 int len = type->length ();
cba6fab5
JB
360 int regnum = -1;
361
362 /* See if our value is returned through a register. If it is, then
363 store the associated register number in REGNUM. */
78134374 364 switch (type->code ())
cba6fab5
JB
365 {
366 case TYPE_CODE_FLT:
cd096ec8
HD
367 /* floats, and doubles are returned via XMM0. */
368 if (len == 4 || len == 8)
dda83cd7
SM
369 regnum = AMD64_XMM0_REGNUM;
370 break;
cd096ec8
HD
371 case TYPE_CODE_ARRAY:
372 /* __m128, __m128i and __m128d are returned via XMM0. */
a1d217e8 373 if (type->is_vector () && len == 16)
cd096ec8 374 {
27710edb 375 enum type_code code = type->target_type ()->code ();
cd096ec8
HD
376 if (code == TYPE_CODE_INT || code == TYPE_CODE_FLT)
377 {
378 regnum = AMD64_XMM0_REGNUM;
379 break;
380 }
381 }
d182e398 382 [[fallthrough]];
cba6fab5 383 default:
dda83cd7
SM
384 /* All other values that are 1, 2, 4 or 8 bytes long are returned
385 via RAX. */
386 if (len == 1 || len == 2 || len == 4 || len == 8)
387 regnum = AMD64_RAX_REGNUM;
cd096ec8
HD
388 else if (len == 16 && type->code () == TYPE_CODE_INT)
389 regnum = AMD64_XMM0_REGNUM;
dda83cd7 390 break;
cba6fab5
JB
391 }
392
393 if (regnum < 0)
394 {
395 /* RAX contains the address where the return value has been stored. */
911627e7 396 if (read_value != nullptr)
dda83cd7 397 {
cba6fab5
JB
398 ULONGEST addr;
399
400 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
911627e7 401 *read_value = value_at_non_lval (type, addr);
cba6fab5
JB
402 }
403 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
404 }
405 else
406 {
407 /* Extract the return value from the register where it was stored. */
911627e7
TT
408 if (read_value != nullptr)
409 {
317c3ed9 410 *read_value = value::allocate (type);
911627e7 411 regcache->raw_read_part (regnum, 0, len,
bbe912ba 412 (*read_value)->contents_raw ().data ());
911627e7 413 }
cba6fab5 414 if (writebuf)
4f0420fd 415 regcache->raw_write_part (regnum, 0, len, writebuf);
cba6fab5
JB
416 return RETURN_VALUE_REGISTER_CONVENTION;
417 }
418}
419
99e24b90
PM
420/* Check that the code pointed to by PC corresponds to a call to
421 __main, skip it if so. Return PC otherwise. */
422
423static CORE_ADDR
424amd64_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
425{
426 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
427 gdb_byte op;
428
429 target_read_memory (pc, &op, 1);
430 if (op == 0xe8)
431 {
432 gdb_byte buf[4];
433
434 if (target_read_memory (pc + 1, buf, sizeof buf) == 0)
24b21115
SM
435 {
436 struct bound_minimal_symbol s;
437 CORE_ADDR call_dest;
99e24b90
PM
438
439 call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order);
24b21115
SM
440 s = lookup_minimal_symbol_by_pc (call_dest);
441 if (s.minsym != NULL
442 && s.minsym->linkage_name () != NULL
443 && strcmp (s.minsym->linkage_name (), "__main") == 0)
444 pc += 5;
445 }
99e24b90
PM
446 }
447
448 return pc;
449}
450
9058cc3a
TG
451struct amd64_windows_frame_cache
452{
453 /* ImageBase for the module. */
454 CORE_ADDR image_base;
455
456 /* Function start and end rva. */
457 CORE_ADDR start_rva;
458 CORE_ADDR end_rva;
459
460 /* Next instruction to be executed. */
461 CORE_ADDR pc;
462
463 /* Current sp. */
464 CORE_ADDR sp;
465
466 /* Address of saved integer and xmm registers. */
467 CORE_ADDR prev_reg_addr[16];
468 CORE_ADDR prev_xmm_addr[16];
469
470 /* These two next fields are set only for machine info frames. */
471
472 /* Likewise for RIP. */
473 CORE_ADDR prev_rip_addr;
474
475 /* Likewise for RSP. */
476 CORE_ADDR prev_rsp_addr;
477
478 /* Address of the previous frame. */
479 CORE_ADDR prev_sp;
480};
481
482/* Convert a Windows register number to gdb. */
483static const enum amd64_regnum amd64_windows_w2gdb_regnum[] =
484{
485 AMD64_RAX_REGNUM,
486 AMD64_RCX_REGNUM,
487 AMD64_RDX_REGNUM,
488 AMD64_RBX_REGNUM,
489 AMD64_RSP_REGNUM,
490 AMD64_RBP_REGNUM,
491 AMD64_RSI_REGNUM,
492 AMD64_RDI_REGNUM,
493 AMD64_R8_REGNUM,
494 AMD64_R9_REGNUM,
495 AMD64_R10_REGNUM,
496 AMD64_R11_REGNUM,
497 AMD64_R12_REGNUM,
498 AMD64_R13_REGNUM,
499 AMD64_R14_REGNUM,
500 AMD64_R15_REGNUM
501};
502
6471e7d2 503/* Return TRUE iff PC is the range of the function corresponding to
9058cc3a
TG
504 CACHE. */
505
506static int
507pc_in_range (CORE_ADDR pc, const struct amd64_windows_frame_cache *cache)
508{
509 return (pc >= cache->image_base + cache->start_rva
510 && pc < cache->image_base + cache->end_rva);
511}
512
513/* Try to recognize and decode an epilogue sequence.
514
515 Return -1 if we fail to read the instructions for any reason.
516 Return 1 if an epilogue sequence was recognized, 0 otherwise. */
517
518static int
8480a37e 519amd64_windows_frame_decode_epilogue (const frame_info_ptr &this_frame,
9058cc3a
TG
520 struct amd64_windows_frame_cache *cache)
521{
522 /* According to MSDN an epilogue "must consist of either an add RSP,constant
523 or lea RSP,constant[FPReg], followed by a series of zero or more 8-byte
524 register pops and a return or a jmp".
525
526 Furthermore, according to RtlVirtualUnwind, the complete list of
527 epilog marker is:
528 - ret [c3]
529 - ret n [c2 imm16]
530 - rep ret [f3 c3]
531 - jmp imm8 | imm32 [eb rel8] or [e9 rel32]
532 - jmp qword ptr imm32 - not handled
533 - rex.w jmp reg [4X ff eY]
534 */
535
536 CORE_ADDR pc = cache->pc;
537 CORE_ADDR cur_sp = cache->sp;
538 struct gdbarch *gdbarch = get_frame_arch (this_frame);
539 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
540 gdb_byte op;
541 gdb_byte rex;
542
543 /* We don't care about the instruction deallocating the frame:
544 if it hasn't been executed, the pc is still in the body,
545 if it has been executed, the following epilog decoding will work. */
546
547 /* First decode:
548 - pop reg [41 58-5f] or [58-5f]. */
549
550 while (1)
551 {
552 /* Read opcode. */
553 if (target_read_memory (pc, &op, 1) != 0)
554 return -1;
555
556 if (op >= 0x40 && op <= 0x4f)
557 {
558 /* REX prefix. */
559 rex = op;
560
561 /* Read opcode. */
562 if (target_read_memory (pc + 1, &op, 1) != 0)
563 return -1;
564 }
565 else
566 rex = 0;
567
568 if (op >= 0x58 && op <= 0x5f)
569 {
570 /* pop reg */
571 gdb_byte reg = (op & 0x0f) | ((rex & 1) << 3);
572
573 cache->prev_reg_addr[amd64_windows_w2gdb_regnum[reg]] = cur_sp;
574 cur_sp += 8;
a6a20ad7 575 pc += rex ? 2 : 1;
9058cc3a
TG
576 }
577 else
578 break;
579
580 /* Allow the user to break this loop. This shouldn't happen as the
581 number of consecutive pop should be small. */
582 QUIT;
583 }
584
585 /* Then decode the marker. */
586
587 /* Read opcode. */
588 if (target_read_memory (pc, &op, 1) != 0)
589 return -1;
590
591 switch (op)
592 {
593 case 0xc3:
594 /* Ret. */
595 cache->prev_rip_addr = cur_sp;
596 cache->prev_sp = cur_sp + 8;
597 return 1;
598
599 case 0xeb:
600 {
601 /* jmp rel8 */
602 gdb_byte rel8;
603 CORE_ADDR npc;
604
605 if (target_read_memory (pc + 1, &rel8, 1) != 0)
606 return -1;
607 npc = pc + 2 + (signed char) rel8;
608
609 /* If the jump is within the function, then this is not a marker,
610 otherwise this is a tail-call. */
611 return !pc_in_range (npc, cache);
612 }
613
614 case 0xec:
615 {
616 /* jmp rel32 */
617 gdb_byte rel32[4];
618 CORE_ADDR npc;
619
620 if (target_read_memory (pc + 1, rel32, 4) != 0)
621 return -1;
622 npc = pc + 5 + extract_signed_integer (rel32, 4, byte_order);
623
624 /* If the jump is within the function, then this is not a marker,
625 otherwise this is a tail-call. */
626 return !pc_in_range (npc, cache);
627 }
628
629 case 0xc2:
630 {
631 /* ret n */
632 gdb_byte imm16[2];
633
634 if (target_read_memory (pc + 1, imm16, 2) != 0)
635 return -1;
636 cache->prev_rip_addr = cur_sp;
637 cache->prev_sp = cur_sp
638 + extract_unsigned_integer (imm16, 4, byte_order);
639 return 1;
640 }
641
642 case 0xf3:
643 {
644 /* rep; ret */
645 gdb_byte op1;
646
647 if (target_read_memory (pc + 2, &op1, 1) != 0)
648 return -1;
649 if (op1 != 0xc3)
650 return 0;
651
652 cache->prev_rip_addr = cur_sp;
653 cache->prev_sp = cur_sp + 8;
654 return 1;
655 }
656
657 case 0x40:
658 case 0x41:
659 case 0x42:
660 case 0x43:
661 case 0x44:
662 case 0x45:
663 case 0x46:
664 case 0x47:
665 case 0x48:
666 case 0x49:
667 case 0x4a:
668 case 0x4b:
669 case 0x4c:
670 case 0x4d:
671 case 0x4e:
672 case 0x4f:
673 /* Got a REX prefix, read next byte. */
674 rex = op;
675 if (target_read_memory (pc + 1, &op, 1) != 0)
676 return -1;
677
678 if (op == 0xff)
679 {
680 /* rex jmp reg */
681 gdb_byte op1;
9058cc3a
TG
682
683 if (target_read_memory (pc + 2, &op1, 1) != 0)
684 return -1;
685 return (op1 & 0xf8) == 0xe0;
686 }
687 else
688 return 0;
689
690 default:
691 /* Not REX, so unknown. */
692 return 0;
693 }
694}
695
696/* Decode and execute unwind insns at UNWIND_INFO. */
697
698static void
8480a37e 699amd64_windows_frame_decode_insns (const frame_info_ptr &this_frame,
9058cc3a
TG
700 struct amd64_windows_frame_cache *cache,
701 CORE_ADDR unwind_info)
702{
703 CORE_ADDR save_addr = 0;
704 CORE_ADDR cur_sp = cache->sp;
705 struct gdbarch *gdbarch = get_frame_arch (this_frame);
706 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
670f82d4
TG
707 int first = 1;
708
709 /* There are at least 3 possibilities to share an unwind info entry:
710 1. Two different runtime_function entries (in .pdata) can point to the
711 same unwind info entry. There is no such indication while unwinding,
712 so we don't really care about that case. We suppose this scheme is
713 used to save memory when the unwind entries are exactly the same.
714 2. Chained unwind_info entries, with no unwind codes (no prologue).
715 There is a major difference with the previous case: the pc range for
716 the function is different (in case 1, the pc range comes from the
717 runtime_function entry; in case 2, the pc range for the chained entry
718 comes from the first unwind entry). Case 1 cannot be used instead as
719 the pc is not in the prologue. This case is officially documented.
720 (There might be unwind code in the first unwind entry to handle
721 additional unwinding). GCC (at least until gcc 5.0) doesn't chain
722 entries.
723 3. Undocumented unwind info redirection. Hard to know the exact purpose,
724 so it is considered as a memory optimization of case 2.
725 */
9058cc3a 726
670f82d4
TG
727 if (unwind_info & 1)
728 {
729 /* Unofficially documented unwind info redirection, when UNWIND_INFO
730 address is odd (http://www.codemachine.com/article_x64deepdive.html).
731 */
732 struct external_pex64_runtime_function d;
670f82d4
TG
733
734 if (target_read_memory (cache->image_base + (unwind_info & ~1),
735 (gdb_byte *) &d, sizeof (d)) != 0)
736 return;
737
738 cache->start_rva
739 = extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order);
740 cache->end_rva
741 = extract_unsigned_integer (d.rva_EndAddress, 4, byte_order);
742 unwind_info
743 = extract_unsigned_integer (d.rva_UnwindData, 4, byte_order);
744 }
745
746 while (1)
9058cc3a
TG
747 {
748 struct external_pex64_unwind_info ex_ui;
749 /* There are at most 256 16-bit unwind insns. */
750 gdb_byte insns[2 * 256];
751 gdb_byte *p;
752 gdb_byte *end_insns;
753 unsigned char codes_count;
754 unsigned char frame_reg;
670f82d4 755 CORE_ADDR start;
9058cc3a
TG
756
757 /* Read and decode header. */
758 if (target_read_memory (cache->image_base + unwind_info,
759 (gdb_byte *) &ex_ui, sizeof (ex_ui)) != 0)
760 return;
761
a05a883f
SM
762 frame_debug_printf ("%s: ver: %02x, plgsz: %02x, cnt: %02x, frame: %02x",
763 paddress (gdbarch, unwind_info),
764 ex_ui.Version_Flags, ex_ui.SizeOfPrologue,
765 ex_ui.CountOfCodes, ex_ui.FrameRegisterOffset);
9058cc3a
TG
766
767 /* Check version. */
170d82c9
JB
768 if (PEX64_UWI_VERSION (ex_ui.Version_Flags) != 1
769 && PEX64_UWI_VERSION (ex_ui.Version_Flags) != 2)
9058cc3a
TG
770 return;
771
670f82d4
TG
772 start = cache->image_base + cache->start_rva;
773 if (first
774 && !(cache->pc >= start && cache->pc < start + ex_ui.SizeOfPrologue))
9058cc3a 775 {
670f82d4
TG
776 /* We want to detect if the PC points to an epilogue. This needs
777 to be checked only once, and an epilogue can be anywhere but in
778 the prologue. If so, the epilogue detection+decoding function is
9058cc3a
TG
779 sufficient. Otherwise, the unwinder will consider that the PC
780 is in the body of the function and will need to decode unwind
781 info. */
782 if (amd64_windows_frame_decode_epilogue (this_frame, cache) == 1)
783 return;
784
785 /* Not in an epilog. Clear possible side effects. */
786 memset (cache->prev_reg_addr, 0, sizeof (cache->prev_reg_addr));
787 }
788
789 codes_count = ex_ui.CountOfCodes;
790 frame_reg = PEX64_UWI_FRAMEREG (ex_ui.FrameRegisterOffset);
791
792 if (frame_reg != 0)
793 {
794 /* According to msdn:
795 If an FP reg is used, then any unwind code taking an offset must
796 only be used after the FP reg is established in the prolog. */
797 gdb_byte buf[8];
798 int frreg = amd64_windows_w2gdb_regnum[frame_reg];
799
800 get_frame_register (this_frame, frreg, buf);
801 save_addr = extract_unsigned_integer (buf, 8, byte_order);
802
a05a883f
SM
803 frame_debug_printf (" frame_reg=%s, val=%s",
804 gdbarch_register_name (gdbarch, frreg),
805 paddress (gdbarch, save_addr));
9058cc3a
TG
806 }
807
808 /* Read opcodes. */
809 if (codes_count != 0
810 && target_read_memory (cache->image_base + unwind_info
811 + sizeof (ex_ui),
812 insns, codes_count * 2) != 0)
813 return;
814
815 end_insns = &insns[codes_count * 2];
170d82c9
JB
816 p = insns;
817
818 /* Skip opcodes 6 of version 2. This opcode is not documented. */
819 if (PEX64_UWI_VERSION (ex_ui.Version_Flags) == 2)
820 {
821 for (; p < end_insns; p += 2)
822 if (PEX64_UNWCODE_CODE (p[1]) != 6)
823 break;
824 }
825
826 for (; p < end_insns; p += 2)
9058cc3a
TG
827 {
828 int reg;
829
670f82d4
TG
830 /* Virtually execute the operation if the pc is after the
831 corresponding instruction (that does matter in case of break
832 within the prologue). Note that for chained info (!first), the
833 prologue has been fully executed. */
834 if (cache->pc >= start + p[0] || cache->pc < start)
9058cc3a 835 {
a05a883f
SM
836 frame_debug_printf (" op #%u: off=0x%02x, insn=0x%02x",
837 (unsigned) (p - insns), p[0], p[1]);
670f82d4 838
9058cc3a
TG
839 /* If there is no frame registers defined, the current value of
840 rsp is used instead. */
841 if (frame_reg == 0)
842 save_addr = cur_sp;
843
670f82d4
TG
844 reg = -1;
845
9058cc3a
TG
846 switch (PEX64_UNWCODE_CODE (p[1]))
847 {
848 case UWOP_PUSH_NONVOL:
849 /* Push pre-decrements RSP. */
850 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])];
851 cache->prev_reg_addr[reg] = cur_sp;
852 cur_sp += 8;
853 break;
854 case UWOP_ALLOC_LARGE:
855 if (PEX64_UNWCODE_INFO (p[1]) == 0)
856 cur_sp +=
857 8 * extract_unsigned_integer (p + 2, 2, byte_order);
858 else if (PEX64_UNWCODE_INFO (p[1]) == 1)
859 cur_sp += extract_unsigned_integer (p + 2, 4, byte_order);
860 else
861 return;
862 break;
863 case UWOP_ALLOC_SMALL:
864 cur_sp += 8 + 8 * PEX64_UNWCODE_INFO (p[1]);
865 break;
866 case UWOP_SET_FPREG:
867 cur_sp = save_addr
868 - PEX64_UWI_FRAMEOFF (ex_ui.FrameRegisterOffset) * 16;
869 break;
870 case UWOP_SAVE_NONVOL:
871 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])];
872 cache->prev_reg_addr[reg] = save_addr
670f82d4 873 + 8 * extract_unsigned_integer (p + 2, 2, byte_order);
9058cc3a
TG
874 break;
875 case UWOP_SAVE_NONVOL_FAR:
876 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])];
877 cache->prev_reg_addr[reg] = save_addr
670f82d4 878 + 8 * extract_unsigned_integer (p + 2, 4, byte_order);
9058cc3a
TG
879 break;
880 case UWOP_SAVE_XMM128:
881 cache->prev_xmm_addr[PEX64_UNWCODE_INFO (p[1])] =
882 save_addr
883 - 16 * extract_unsigned_integer (p + 2, 2, byte_order);
884 break;
885 case UWOP_SAVE_XMM128_FAR:
886 cache->prev_xmm_addr[PEX64_UNWCODE_INFO (p[1])] =
887 save_addr
888 - 16 * extract_unsigned_integer (p + 2, 4, byte_order);
889 break;
890 case UWOP_PUSH_MACHFRAME:
891 if (PEX64_UNWCODE_INFO (p[1]) == 0)
892 {
893 cache->prev_rip_addr = cur_sp + 0;
894 cache->prev_rsp_addr = cur_sp + 24;
895 cur_sp += 40;
896 }
897 else if (PEX64_UNWCODE_INFO (p[1]) == 1)
898 {
899 cache->prev_rip_addr = cur_sp + 8;
900 cache->prev_rsp_addr = cur_sp + 32;
901 cur_sp += 48;
902 }
903 else
904 return;
905 break;
906 default:
907 return;
908 }
670f82d4
TG
909
910 /* Display address where the register was saved. */
a05a883f
SM
911 if (reg >= 0)
912 frame_debug_printf (" [reg %s at %s]",
913 gdbarch_register_name (gdbarch, reg),
914 paddress (gdbarch,
915 cache->prev_reg_addr[reg]));
9058cc3a
TG
916 }
917
918 /* Adjust with the length of the opcode. */
919 switch (PEX64_UNWCODE_CODE (p[1]))
920 {
921 case UWOP_PUSH_NONVOL:
922 case UWOP_ALLOC_SMALL:
923 case UWOP_SET_FPREG:
924 case UWOP_PUSH_MACHFRAME:
925 break;
926 case UWOP_ALLOC_LARGE:
927 if (PEX64_UNWCODE_INFO (p[1]) == 0)
928 p += 2;
929 else if (PEX64_UNWCODE_INFO (p[1]) == 1)
930 p += 4;
931 else
932 return;
933 break;
934 case UWOP_SAVE_NONVOL:
935 case UWOP_SAVE_XMM128:
936 p += 2;
937 break;
938 case UWOP_SAVE_NONVOL_FAR:
939 case UWOP_SAVE_XMM128_FAR:
940 p += 4;
941 break;
942 default:
943 return;
944 }
945 }
946 if (PEX64_UWI_FLAGS (ex_ui.Version_Flags) != UNW_FLAG_CHAININFO)
670f82d4
TG
947 {
948 /* End of unwind info. */
949 break;
950 }
9058cc3a
TG
951 else
952 {
953 /* Read the chained unwind info. */
954 struct external_pex64_runtime_function d;
955 CORE_ADDR chain_vma;
956
670f82d4
TG
957 /* Not anymore the first entry. */
958 first = 0;
959
960 /* Stay aligned on word boundary. */
9058cc3a 961 chain_vma = cache->image_base + unwind_info
e068c55d 962 + sizeof (ex_ui) + ((codes_count + 1) & ~1) * 2;
9058cc3a
TG
963
964 if (target_read_memory (chain_vma, (gdb_byte *) &d, sizeof (d)) != 0)
965 return;
966
670f82d4
TG
967 /* Decode begin/end. This may be different from .pdata index, as
968 an unwind info may be shared by several functions (in particular
969 if many functions have the same prolog and handler. */
9058cc3a
TG
970 cache->start_rva =
971 extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order);
972 cache->end_rva =
973 extract_unsigned_integer (d.rva_EndAddress, 4, byte_order);
974 unwind_info =
975 extract_unsigned_integer (d.rva_UnwindData, 4, byte_order);
53e8f97d 976
a05a883f
SM
977 frame_debug_printf ("next in chain: unwind_data=%s, start_rva=%s, "
978 "end_rva=%s",
979 paddress (gdbarch, unwind_info),
980 paddress (gdbarch, cache->start_rva),
981 paddress (gdbarch, cache->end_rva));
9058cc3a
TG
982 }
983
984 /* Allow the user to break this loop. */
985 QUIT;
986 }
987 /* PC is saved by the call. */
988 if (cache->prev_rip_addr == 0)
989 cache->prev_rip_addr = cur_sp;
990 cache->prev_sp = cur_sp + 8;
991
a05a883f
SM
992 frame_debug_printf (" prev_sp: %s, prev_pc @%s",
993 paddress (gdbarch, cache->prev_sp),
994 paddress (gdbarch, cache->prev_rip_addr));
9058cc3a
TG
995}
996
997/* Find SEH unwind info for PC, returning 0 on success.
998
999 UNWIND_INFO is set to the rva of unwind info address, IMAGE_BASE
1000 to the base address of the corresponding image, and START_RVA
1001 to the rva of the function containing PC. */
1002
1003static int
1004amd64_windows_find_unwind_info (struct gdbarch *gdbarch, CORE_ADDR pc,
1005 CORE_ADDR *unwind_info,
1006 CORE_ADDR *image_base,
1007 CORE_ADDR *start_rva,
1008 CORE_ADDR *end_rva)
1009{
1010 struct obj_section *sec;
1011 pe_data_type *pe;
1012 IMAGE_DATA_DIRECTORY *dir;
1013 struct objfile *objfile;
1014 unsigned long lo, hi;
1015 CORE_ADDR base;
1016 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1017
1018 /* Get the corresponding exception directory. */
1019 sec = find_pc_section (pc);
1020 if (sec == NULL)
1021 return -1;
1022 objfile = sec->objfile;
1023 pe = pe_data (sec->objfile->obfd);
1024 dir = &pe->pe_opthdr.DataDirectory[PE_EXCEPTION_TABLE];
1025
b3b3bada 1026 base = pe->pe_opthdr.ImageBase + objfile->text_section_offset ();
9058cc3a
TG
1027 *image_base = base;
1028
1029 /* Find the entry.
1030
1031 Note: This does not handle dynamically added entries (for JIT
1032 engines). For this, we would need to ask the kernel directly,
1033 which means getting some info from the native layer. For the
1034 rest of the code, however, it's probably faster to search
1035 the entry ourselves. */
1036 lo = 0;
1037 hi = dir->Size / sizeof (struct external_pex64_runtime_function);
1038 *unwind_info = 0;
1039 while (lo <= hi)
1040 {
1041 unsigned long mid = lo + (hi - lo) / 2;
1042 struct external_pex64_runtime_function d;
1043 CORE_ADDR sa, ea;
1044
1045 if (target_read_memory (base + dir->VirtualAddress + mid * sizeof (d),
1046 (gdb_byte *) &d, sizeof (d)) != 0)
1047 return -1;
1048
1049 sa = extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order);
1050 ea = extract_unsigned_integer (d.rva_EndAddress, 4, byte_order);
1051 if (pc < base + sa)
1052 hi = mid - 1;
1053 else if (pc >= base + ea)
1054 lo = mid + 1;
1055 else if (pc >= base + sa && pc < base + ea)
1056 {
1057 /* Got it. */
1058 *start_rva = sa;
1059 *end_rva = ea;
1060 *unwind_info =
1061 extract_unsigned_integer (d.rva_UnwindData, 4, byte_order);
1062 break;
1063 }
1064 else
1065 break;
1066 }
1067
a05a883f
SM
1068 frame_debug_printf ("image_base=%s, unwind_data=%s",
1069 paddress (gdbarch, base),
1070 paddress (gdbarch, *unwind_info));
9058cc3a 1071
9058cc3a
TG
1072 return 0;
1073}
1074
1075/* Fill THIS_CACHE using the native amd64-windows unwinding data
1076 for THIS_FRAME. */
1077
1078static struct amd64_windows_frame_cache *
8480a37e 1079amd64_windows_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
9058cc3a
TG
1080{
1081 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1082 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1083 struct amd64_windows_frame_cache *cache;
1084 gdb_byte buf[8];
9058cc3a 1085 CORE_ADDR pc;
9058cc3a
TG
1086 CORE_ADDR unwind_info = 0;
1087
1088 if (*this_cache)
9a3c8263 1089 return (struct amd64_windows_frame_cache *) *this_cache;
9058cc3a
TG
1090
1091 cache = FRAME_OBSTACK_ZALLOC (struct amd64_windows_frame_cache);
1092 *this_cache = cache;
1093
1094 /* Get current PC and SP. */
1095 pc = get_frame_pc (this_frame);
1096 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
1097 cache->sp = extract_unsigned_integer (buf, 8, byte_order);
1098 cache->pc = pc;
1099
a0f6c61c
TT
1100 /* If we can't find the unwind info, keep trying as though this is a
1101 leaf function. This situation can happen when PC==0, see
1102 https://sourceware.org/bugzilla/show_bug.cgi?id=30255. */
9058cc3a
TG
1103 if (amd64_windows_find_unwind_info (gdbarch, pc, &unwind_info,
1104 &cache->image_base,
1105 &cache->start_rva,
a0f6c61c
TT
1106 &cache->end_rva)
1107 || unwind_info == 0)
9058cc3a
TG
1108 {
1109 /* Assume a leaf function. */
1110 cache->prev_sp = cache->sp + 8;
1111 cache->prev_rip_addr = cache->sp;
1112 }
1113 else
1114 {
1115 /* Decode unwind insns to compute saved addresses. */
1116 amd64_windows_frame_decode_insns (this_frame, cache, unwind_info);
1117 }
1118 return cache;
1119}
1120
1121/* Implement the "prev_register" method of struct frame_unwind
1122 using the standard Windows x64 SEH info. */
1123
1124static struct value *
8480a37e 1125amd64_windows_frame_prev_register (const frame_info_ptr &this_frame,
9058cc3a
TG
1126 void **this_cache, int regnum)
1127{
1128 struct gdbarch *gdbarch = get_frame_arch (this_frame);
9058cc3a
TG
1129 struct amd64_windows_frame_cache *cache =
1130 amd64_windows_frame_cache (this_frame, this_cache);
9058cc3a
TG
1131 CORE_ADDR prev;
1132
a05a883f
SM
1133 frame_debug_printf ("%s for sp=%s",
1134 gdbarch_register_name (gdbarch, regnum),
1135 paddress (gdbarch, cache->prev_sp));
9058cc3a
TG
1136
1137 if (regnum >= AMD64_XMM0_REGNUM && regnum <= AMD64_XMM0_REGNUM + 15)
1138 prev = cache->prev_xmm_addr[regnum - AMD64_XMM0_REGNUM];
1139 else if (regnum == AMD64_RSP_REGNUM)
1140 {
1141 prev = cache->prev_rsp_addr;
1142 if (prev == 0)
1143 return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
1144 }
1145 else if (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_R15_REGNUM)
1146 prev = cache->prev_reg_addr[regnum - AMD64_RAX_REGNUM];
1147 else if (regnum == AMD64_RIP_REGNUM)
1148 prev = cache->prev_rip_addr;
1149 else
1150 prev = 0;
1151
a05a883f
SM
1152 if (prev != 0)
1153 frame_debug_printf (" -> at %s", paddress (gdbarch, prev));
9058cc3a
TG
1154
1155 if (prev)
1156 {
1157 /* Register was saved. */
1158 return frame_unwind_got_memory (this_frame, regnum, prev);
1159 }
1160 else
1161 {
1162 /* Register is either volatile or not modified. */
1163 return frame_unwind_got_register (this_frame, regnum, regnum);
1164 }
1165}
1166
1167/* Implement the "this_id" method of struct frame_unwind using
1168 the standard Windows x64 SEH info. */
1169
1170static void
8480a37e 1171amd64_windows_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
9058cc3a
TG
1172 struct frame_id *this_id)
1173{
9058cc3a
TG
1174 struct amd64_windows_frame_cache *cache =
1175 amd64_windows_frame_cache (this_frame, this_cache);
1176
1177 *this_id = frame_id_build (cache->prev_sp,
1178 cache->image_base + cache->start_rva);
1179}
1180
1181/* Windows x64 SEH unwinder. */
1182
1183static const struct frame_unwind amd64_windows_frame_unwind =
1184{
a154d838 1185 "amd64 windows",
9058cc3a
TG
1186 NORMAL_FRAME,
1187 default_frame_unwind_stop_reason,
1188 &amd64_windows_frame_this_id,
1189 &amd64_windows_frame_prev_register,
1190 NULL,
1191 default_frame_sniffer
1192};
1193
1194/* Implement the "skip_prologue" gdbarch method. */
1195
1196static CORE_ADDR
1197amd64_windows_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1198{
1199 CORE_ADDR func_addr;
1200 CORE_ADDR unwind_info = 0;
1201 CORE_ADDR image_base, start_rva, end_rva;
1202 struct external_pex64_unwind_info ex_ui;
1203
1204 /* Use prologue size from unwind info. */
1205 if (amd64_windows_find_unwind_info (gdbarch, pc, &unwind_info,
1206 &image_base, &start_rva, &end_rva) == 0)
1207 {
1208 if (unwind_info == 0)
1209 {
1210 /* Leaf function. */
1211 return pc;
1212 }
1213 else if (target_read_memory (image_base + unwind_info,
1214 (gdb_byte *) &ex_ui, sizeof (ex_ui)) == 0
1215 && PEX64_UWI_VERSION (ex_ui.Version_Flags) == 1)
325fac50 1216 return std::max (pc, image_base + start_rva + ex_ui.SizeOfPrologue);
9058cc3a
TG
1217 }
1218
1219 /* See if we can determine the end of the prologue via the symbol
1220 table. If so, then return either the PC, or the PC after
1221 the prologue, whichever is greater. */
1222 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1223 {
1224 CORE_ADDR post_prologue_pc
1225 = skip_prologue_using_sal (gdbarch, func_addr);
1226
1227 if (post_prologue_pc != 0)
325fac50 1228 return std::max (pc, post_prologue_pc);
9058cc3a
TG
1229 }
1230
1231 return pc;
1232}
1233
84552b16
PA
1234/* Check Win64 DLL jmp trampolines and find jump destination. */
1235
1236static CORE_ADDR
8480a37e 1237amd64_windows_skip_trampoline_code (const frame_info_ptr &frame, CORE_ADDR pc)
84552b16
PA
1238{
1239 CORE_ADDR destination = 0;
1240 struct gdbarch *gdbarch = get_frame_arch (frame);
1241 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1242
1243 /* Check for jmp *<offset>(%rip) (jump near, absolute indirect (/4)). */
1244 if (pc && read_memory_unsigned_integer (pc, 2, byte_order) == 0x25ff)
1245 {
1246 /* Get opcode offset and see if we can find a reference in our data. */
1247 ULONGEST offset
1248 = read_memory_unsigned_integer (pc + 2, 4, byte_order);
1249
1250 /* Get address of function pointer at end of pc. */
1251 CORE_ADDR indirect_addr = pc + offset + 6;
1252
1253 struct minimal_symbol *indsym
7cbd4a93
TT
1254 = (indirect_addr
1255 ? lookup_minimal_symbol_by_pc (indirect_addr).minsym
1256 : NULL);
c9d95fa3 1257 const char *symname = indsym ? indsym->linkage_name () : NULL;
84552b16
PA
1258
1259 if (symname)
1260 {
61012eef
GB
1261 if (startswith (symname, "__imp_")
1262 || startswith (symname, "_imp_"))
84552b16
PA
1263 destination
1264 = read_memory_unsigned_integer (indirect_addr, 8, byte_order);
1265 }
1266 }
1267
1268 return destination;
1269}
99e24b90 1270
83ab93c6
JB
1271/* Implement the "auto_wide_charset" gdbarch method. */
1272
1273static const char *
1274amd64_windows_auto_wide_charset (void)
1275{
1276 return "UTF-16";
1277}
1278
0f2265e2
SM
1279/* Common parts for gdbarch initialization for Windows and Cygwin on AMD64. */
1280
d0761299 1281static void
30efb6c7 1282amd64_windows_init_abi_common (gdbarch_info info, struct gdbarch *gdbarch)
d0761299 1283{
08106042 1284 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
aff9d387 1285
9058cc3a
TG
1286 /* The dwarf2 unwinder (appended very early by i386_gdbarch_init) is
1287 preferred over the SEH one. The reasons are:
85102364 1288 - binaries without SEH but with dwarf2 debug info are correctly handled
9058cc3a
TG
1289 (although they aren't ABI compliant, gcc before 4.7 didn't emit SEH
1290 info).
1291 - dwarf3 DW_OP_call_frame_cfa is correctly handled (it can only be
1292 handled if the dwarf2 unwinder is used).
1293
1294 The call to amd64_init_abi appends default unwinders, that aren't
1295 compatible with the SEH one.
1296 */
1297 frame_unwind_append_unwinder (gdbarch, &amd64_windows_frame_unwind);
1298
2434b019 1299 amd64_init_abi (info, gdbarch,
de52b960 1300 amd64_target_description (X86_XSTATE_SSE_MASK, false));
d0761299 1301
ba581dc1 1302 /* Function calls. */
20c2e3e0 1303 set_gdbarch_push_dummy_call (gdbarch, amd64_windows_push_dummy_call);
5cb0f2d5 1304 set_gdbarch_return_value_as_value (gdbarch, amd64_windows_return_value);
99e24b90 1305 set_gdbarch_skip_main_prologue (gdbarch, amd64_skip_main_prologue);
84552b16
PA
1306 set_gdbarch_skip_trampoline_code (gdbarch,
1307 amd64_windows_skip_trampoline_code);
ba581dc1 1308
9058cc3a
TG
1309 set_gdbarch_skip_prologue (gdbarch, amd64_windows_skip_prologue);
1310
aff9d387
JT
1311 tdep->gregset_reg_offset = amd64_windows_gregset_reg_offset;
1312 tdep->gregset_num_regs = ARRAY_SIZE (amd64_windows_gregset_reg_offset);
1313 tdep->sizeof_gregset = AMD64_WINDOWS_SIZEOF_GREGSET;
1314 tdep->sizeof_fpregset = 0;
1315
62a5151b
JT
1316 /* Core file support. */
1317 set_gdbarch_core_xfer_shared_libraries
1318 (gdbarch, windows_core_xfer_shared_libraries);
1319 set_gdbarch_core_pid_to_str (gdbarch, windows_core_pid_to_str);
1320
83ab93c6 1321 set_gdbarch_auto_wide_charset (gdbarch, amd64_windows_auto_wide_charset);
d0761299
JB
1322}
1323
0f2265e2
SM
1324/* gdbarch initialization for Windows on AMD64. */
1325
30efb6c7
SM
1326static void
1327amd64_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1328{
1329 amd64_windows_init_abi_common (info, gdbarch);
0f2265e2 1330 windows_init_abi (info, gdbarch);
30efb6c7
SM
1331
1332 /* On Windows, "long"s are only 32bit. */
1333 set_gdbarch_long_bit (gdbarch, 32);
1334}
1335
ff4e2303
JT
1336/* Sigwrapper unwinder instruction patterns for AMD64. */
1337
1338static const gdb_byte amd64_sigbe_bytes[] = {
1339 0x49, 0xc7, 0xc3, 0xf8, 0xff, 0xff, 0xff, /* movq $-8,%r11 */
1340 0x4d, 0x0f, 0xc1, 0x9a, /* xaddq %r11,$tls::stackptr(%r10) */
1341 /* 4 bytes for tls::stackptr operand. */
1342};
1343
1344static const gdb_byte amd64_sigdelayed_bytes[] = {
1345 0x49, 0xc7, 0xc3, 0xf8, 0xff, 0xff, 0xff, /* movq $-8,%r11 */
1346 0x4d, 0x0f, 0xc1, 0x9c, 0x24, /* xaddq %r11,$tls::stackptr(%r12) */
1347 /* 4 bytes for tls::stackptr operand. */
1348};
1349
1350static const gdb::array_view<const gdb_byte> amd64_sig_patterns[] {
1351 { amd64_sigbe_bytes },
1352 { amd64_sigdelayed_bytes },
1353};
1354
1355/* The sigwrapper unwinder on AMD64. */
1356
1357static const cygwin_sigwrapper_frame_unwind
1358 amd64_cygwin_sigwrapper_frame_unwind (amd64_sig_patterns);
1359
0f2265e2
SM
1360/* gdbarch initialization for Cygwin on AMD64. */
1361
30efb6c7
SM
1362static void
1363amd64_cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1364{
ff4e2303
JT
1365 frame_unwind_append_unwinder (gdbarch, &amd64_cygwin_sigwrapper_frame_unwind);
1366
30efb6c7 1367 amd64_windows_init_abi_common (info, gdbarch);
0f2265e2 1368 cygwin_init_abi (info, gdbarch);
30efb6c7
SM
1369}
1370
cb9b645d
SM
1371static gdb_osabi
1372amd64_windows_osabi_sniffer (bfd *abfd)
1373{
1374 const char *target_name = bfd_get_target (abfd);
1375
8db52437
SM
1376 if (!streq (target_name, "pei-x86-64"))
1377 return GDB_OSABI_UNKNOWN;
cb9b645d 1378
8db52437
SM
1379 if (is_linked_with_cygwin_dll (abfd))
1380 return GDB_OSABI_CYGWIN;
1381
1382 return GDB_OSABI_WINDOWS;
cb9b645d
SM
1383}
1384
7d155da3
JT
1385static enum gdb_osabi
1386amd64_cygwin_core_osabi_sniffer (bfd *abfd)
1387{
1388 const char *target_name = bfd_get_target (abfd);
1389
1390 /* Cygwin uses elf core dumps. Do not claim all ELF executables,
1391 check whether there is a .reg section of proper size. */
1392 if (strcmp (target_name, "elf64-x86-64") == 0)
1393 {
1394 asection *section = bfd_get_section_by_name (abfd, ".reg");
1395 if (section != nullptr
1396 && bfd_section_size (section) == AMD64_WINDOWS_SIZEOF_GREGSET)
1397 return GDB_OSABI_CYGWIN;
1398 }
1399
1400 return GDB_OSABI_UNKNOWN;
1401}
1402
6c265988 1403void _initialize_amd64_windows_tdep ();
d0761299 1404void
6c265988 1405_initialize_amd64_windows_tdep ()
d0761299 1406{
053205cc 1407 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_WINDOWS,
dda83cd7 1408 amd64_windows_init_abi);
d0761299 1409 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_CYGWIN,
dda83cd7 1410 amd64_cygwin_init_abi);
cb9b645d
SM
1411
1412 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
1413 amd64_windows_osabi_sniffer);
7d155da3
JT
1414
1415 /* Cygwin uses elf core dumps. */
1416 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
1417 amd64_cygwin_core_osabi_sniffer);
1418
d0761299 1419}