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