1 /* Copyright (C) 2009-2017 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
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.
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/>. */
20 #include "amd64-tdep.h"
24 #include "windows-tdep.h"
27 #include "frame-unwind.h"
28 #include "coff/internal.h"
29 #include "coff/i386.h"
35 /* The registers used to pass integer arguments during a function call. */
36 static int amd64_windows_dummy_call_integer_regs
[] =
38 AMD64_RCX_REGNUM
, /* %rcx */
39 AMD64_RDX_REGNUM
, /* %rdx */
40 AMD64_R8_REGNUM
, /* %r8 */
41 AMD64_R9_REGNUM
/* %r9 */
44 /* Return nonzero if an argument of type TYPE should be passed
45 via one of the integer registers. */
48 amd64_windows_passed_by_integer_register (struct type
*type
)
50 switch (TYPE_CODE (type
))
59 case TYPE_CODE_RVALUE_REF
:
60 case TYPE_CODE_STRUCT
:
62 return (TYPE_LENGTH (type
) == 1
63 || TYPE_LENGTH (type
) == 2
64 || TYPE_LENGTH (type
) == 4
65 || TYPE_LENGTH (type
) == 8);
72 /* Return nonzero if an argument of type TYPE should be passed
73 via one of the XMM registers. */
76 amd64_windows_passed_by_xmm_register (struct type
*type
)
78 return ((TYPE_CODE (type
) == TYPE_CODE_FLT
79 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
80 && (TYPE_LENGTH (type
) == 4 || TYPE_LENGTH (type
) == 8));
83 /* Return non-zero iff an argument of the given TYPE should be passed
87 amd64_windows_passed_by_pointer (struct type
*type
)
89 if (amd64_windows_passed_by_integer_register (type
))
92 if (amd64_windows_passed_by_xmm_register (type
))
98 /* For each argument that should be passed by pointer, reserve some
99 stack space, store a copy of the argument on the stack, and replace
100 the argument by its address. Return the new Stack Pointer value.
102 NARGS is the number of arguments. ARGS is the array containing
103 the value of each argument. SP is value of the Stack Pointer. */
106 amd64_windows_adjust_args_passed_by_pointer (struct value
**args
,
107 int nargs
, CORE_ADDR sp
)
111 for (i
= 0; i
< nargs
; i
++)
112 if (amd64_windows_passed_by_pointer (value_type (args
[i
])))
114 struct type
*type
= value_type (args
[i
]);
115 const gdb_byte
*valbuf
= value_contents (args
[i
]);
116 const int len
= TYPE_LENGTH (type
);
118 /* Store a copy of that argument on the stack, aligned to
119 a 16 bytes boundary, and then use the copy's address as
124 write_memory (sp
, valbuf
, len
);
127 = value_addr (value_from_contents_and_address (type
, valbuf
, sp
));
133 /* Store the value of ARG in register REGNO (right-justified).
134 REGCACHE is the register cache. */
137 amd64_windows_store_arg_in_reg (struct regcache
*regcache
,
138 struct value
*arg
, int regno
)
140 struct type
*type
= value_type (arg
);
141 const gdb_byte
*valbuf
= value_contents (arg
);
144 gdb_assert (TYPE_LENGTH (type
) <= 8);
145 memset (buf
, 0, sizeof buf
);
146 memcpy (buf
, valbuf
, std::min (TYPE_LENGTH (type
), (unsigned int) 8));
147 regcache_cooked_write (regcache
, regno
, buf
);
150 /* Push the arguments for an inferior function call, and return
151 the updated value of the SP (Stack Pointer).
153 All arguments are identical to the arguments used in
154 amd64_windows_push_dummy_call. */
157 amd64_windows_push_arguments (struct regcache
*regcache
, int nargs
,
158 struct value
**args
, CORE_ADDR sp
,
163 struct value
**stack_args
= XALLOCAVEC (struct value
*, nargs
);
164 int num_stack_args
= 0;
165 int num_elements
= 0;
168 /* First, handle the arguments passed by pointer.
170 These arguments are replaced by pointers to a copy we are making
171 in inferior memory. So use a copy of the ARGS table, to avoid
172 modifying the original one. */
174 struct value
**args1
= XALLOCAVEC (struct value
*, nargs
);
176 memcpy (args1
, args
, nargs
* sizeof (struct value
*));
177 sp
= amd64_windows_adjust_args_passed_by_pointer (args1
, nargs
, sp
);
181 /* Reserve a register for the "hidden" argument. */
185 for (i
= 0; i
< nargs
; i
++)
187 struct type
*type
= value_type (args
[i
]);
188 int len
= TYPE_LENGTH (type
);
191 if (reg_idx
< ARRAY_SIZE (amd64_windows_dummy_call_integer_regs
))
193 if (amd64_windows_passed_by_integer_register (type
))
195 amd64_windows_store_arg_in_reg
197 amd64_windows_dummy_call_integer_regs
[reg_idx
]);
201 else if (amd64_windows_passed_by_xmm_register (type
))
203 amd64_windows_store_arg_in_reg
204 (regcache
, args
[i
], AMD64_XMM0_REGNUM
+ reg_idx
);
205 /* In case of varargs, these parameters must also be
206 passed via the integer registers. */
207 amd64_windows_store_arg_in_reg
209 amd64_windows_dummy_call_integer_regs
[reg_idx
]);
217 num_elements
+= ((len
+ 7) / 8);
218 stack_args
[num_stack_args
++] = args
[i
];
222 /* Allocate space for the arguments on the stack, keeping it
223 aligned on a 16 byte boundary. */
224 sp
-= num_elements
* 8;
227 /* Write out the arguments to the stack. */
228 for (i
= 0; i
< num_stack_args
; i
++)
230 struct type
*type
= value_type (stack_args
[i
]);
231 const gdb_byte
*valbuf
= value_contents (stack_args
[i
]);
233 write_memory (sp
+ element
* 8, valbuf
, TYPE_LENGTH (type
));
234 element
+= ((TYPE_LENGTH (type
) + 7) / 8);
240 /* Implement the "push_dummy_call" gdbarch method. */
243 amd64_windows_push_dummy_call
244 (struct gdbarch
*gdbarch
, struct value
*function
,
245 struct regcache
*regcache
, CORE_ADDR bp_addr
,
246 int nargs
, struct value
**args
,
247 CORE_ADDR sp
, int struct_return
, CORE_ADDR struct_addr
)
249 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
252 /* Pass arguments. */
253 sp
= amd64_windows_push_arguments (regcache
, nargs
, args
, sp
,
256 /* Pass "hidden" argument". */
259 /* The "hidden" argument is passed throught the first argument
261 const int arg_regnum
= amd64_windows_dummy_call_integer_regs
[0];
263 store_unsigned_integer (buf
, 8, byte_order
, struct_addr
);
264 regcache_cooked_write (regcache
, arg_regnum
, buf
);
267 /* Reserve some memory on the stack for the integer-parameter
268 registers, as required by the ABI. */
269 sp
-= ARRAY_SIZE (amd64_windows_dummy_call_integer_regs
) * 8;
271 /* Store return address. */
273 store_unsigned_integer (buf
, 8, byte_order
, bp_addr
);
274 write_memory (sp
, buf
, 8);
276 /* Update the stack pointer... */
277 store_unsigned_integer (buf
, 8, byte_order
, sp
);
278 regcache_cooked_write (regcache
, AMD64_RSP_REGNUM
, buf
);
280 /* ...and fake a frame pointer. */
281 regcache_cooked_write (regcache
, AMD64_RBP_REGNUM
, buf
);
286 /* Implement the "return_value" gdbarch method for amd64-windows. */
288 static enum return_value_convention
289 amd64_windows_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
290 struct type
*type
, struct regcache
*regcache
,
291 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
293 int len
= TYPE_LENGTH (type
);
296 /* See if our value is returned through a register. If it is, then
297 store the associated register number in REGNUM. */
298 switch (TYPE_CODE (type
))
301 case TYPE_CODE_DECFLOAT
:
302 /* __m128, __m128i, __m128d, floats, and doubles are returned
304 if (len
== 4 || len
== 8 || len
== 16)
305 regnum
= AMD64_XMM0_REGNUM
;
308 /* All other values that are 1, 2, 4 or 8 bytes long are returned
310 if (len
== 1 || len
== 2 || len
== 4 || len
== 8)
311 regnum
= AMD64_RAX_REGNUM
;
317 /* RAX contains the address where the return value has been stored. */
322 regcache_raw_read_unsigned (regcache
, AMD64_RAX_REGNUM
, &addr
);
323 read_memory (addr
, readbuf
, TYPE_LENGTH (type
));
325 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
329 /* Extract the return value from the register where it was stored. */
331 regcache_raw_read_part (regcache
, regnum
, 0, len
, readbuf
);
333 regcache_raw_write_part (regcache
, regnum
, 0, len
, writebuf
);
334 return RETURN_VALUE_REGISTER_CONVENTION
;
338 /* Check that the code pointed to by PC corresponds to a call to
339 __main, skip it if so. Return PC otherwise. */
342 amd64_skip_main_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
344 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
347 target_read_memory (pc
, &op
, 1);
352 if (target_read_memory (pc
+ 1, buf
, sizeof buf
) == 0)
354 struct bound_minimal_symbol s
;
357 call_dest
= pc
+ 5 + extract_signed_integer (buf
, 4, byte_order
);
358 s
= lookup_minimal_symbol_by_pc (call_dest
);
360 && MSYMBOL_LINKAGE_NAME (s
.minsym
) != NULL
361 && strcmp (MSYMBOL_LINKAGE_NAME (s
.minsym
), "__main") == 0)
369 struct amd64_windows_frame_cache
371 /* ImageBase for the module. */
372 CORE_ADDR image_base
;
374 /* Function start and end rva. */
378 /* Next instruction to be executed. */
384 /* Address of saved integer and xmm registers. */
385 CORE_ADDR prev_reg_addr
[16];
386 CORE_ADDR prev_xmm_addr
[16];
388 /* These two next fields are set only for machine info frames. */
390 /* Likewise for RIP. */
391 CORE_ADDR prev_rip_addr
;
393 /* Likewise for RSP. */
394 CORE_ADDR prev_rsp_addr
;
396 /* Address of the previous frame. */
400 /* Convert a Windows register number to gdb. */
401 static const enum amd64_regnum amd64_windows_w2gdb_regnum
[] =
421 /* Return TRUE iff PC is the the range of the function corresponding to
425 pc_in_range (CORE_ADDR pc
, const struct amd64_windows_frame_cache
*cache
)
427 return (pc
>= cache
->image_base
+ cache
->start_rva
428 && pc
< cache
->image_base
+ cache
->end_rva
);
431 /* Try to recognize and decode an epilogue sequence.
433 Return -1 if we fail to read the instructions for any reason.
434 Return 1 if an epilogue sequence was recognized, 0 otherwise. */
437 amd64_windows_frame_decode_epilogue (struct frame_info
*this_frame
,
438 struct amd64_windows_frame_cache
*cache
)
440 /* According to MSDN an epilogue "must consist of either an add RSP,constant
441 or lea RSP,constant[FPReg], followed by a series of zero or more 8-byte
442 register pops and a return or a jmp".
444 Furthermore, according to RtlVirtualUnwind, the complete list of
449 - jmp imm8 | imm32 [eb rel8] or [e9 rel32]
450 - jmp qword ptr imm32 - not handled
451 - rex.w jmp reg [4X ff eY]
454 CORE_ADDR pc
= cache
->pc
;
455 CORE_ADDR cur_sp
= cache
->sp
;
456 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
457 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
461 /* We don't care about the instruction deallocating the frame:
462 if it hasn't been executed, the pc is still in the body,
463 if it has been executed, the following epilog decoding will work. */
466 - pop reg [41 58-5f] or [58-5f]. */
471 if (target_read_memory (pc
, &op
, 1) != 0)
474 if (op
>= 0x40 && op
<= 0x4f)
480 if (target_read_memory (pc
+ 1, &op
, 1) != 0)
486 if (op
>= 0x58 && op
<= 0x5f)
489 gdb_byte reg
= (op
& 0x0f) | ((rex
& 1) << 3);
491 cache
->prev_reg_addr
[amd64_windows_w2gdb_regnum
[reg
]] = cur_sp
;
498 /* Allow the user to break this loop. This shouldn't happen as the
499 number of consecutive pop should be small. */
503 /* Then decode the marker. */
506 if (target_read_memory (pc
, &op
, 1) != 0)
513 cache
->prev_rip_addr
= cur_sp
;
514 cache
->prev_sp
= cur_sp
+ 8;
523 if (target_read_memory (pc
+ 1, &rel8
, 1) != 0)
525 npc
= pc
+ 2 + (signed char) rel8
;
527 /* If the jump is within the function, then this is not a marker,
528 otherwise this is a tail-call. */
529 return !pc_in_range (npc
, cache
);
538 if (target_read_memory (pc
+ 1, rel32
, 4) != 0)
540 npc
= pc
+ 5 + extract_signed_integer (rel32
, 4, byte_order
);
542 /* If the jump is within the function, then this is not a marker,
543 otherwise this is a tail-call. */
544 return !pc_in_range (npc
, cache
);
552 if (target_read_memory (pc
+ 1, imm16
, 2) != 0)
554 cache
->prev_rip_addr
= cur_sp
;
555 cache
->prev_sp
= cur_sp
556 + extract_unsigned_integer (imm16
, 4, byte_order
);
565 if (target_read_memory (pc
+ 2, &op1
, 1) != 0)
570 cache
->prev_rip_addr
= cur_sp
;
571 cache
->prev_sp
= cur_sp
+ 8;
591 /* Got a REX prefix, read next byte. */
593 if (target_read_memory (pc
+ 1, &op
, 1) != 0)
601 if (target_read_memory (pc
+ 2, &op1
, 1) != 0)
603 return (op1
& 0xf8) == 0xe0;
609 /* Not REX, so unknown. */
614 /* Decode and execute unwind insns at UNWIND_INFO. */
617 amd64_windows_frame_decode_insns (struct frame_info
*this_frame
,
618 struct amd64_windows_frame_cache
*cache
,
619 CORE_ADDR unwind_info
)
621 CORE_ADDR save_addr
= 0;
622 CORE_ADDR cur_sp
= cache
->sp
;
623 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
624 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
627 /* There are at least 3 possibilities to share an unwind info entry:
628 1. Two different runtime_function entries (in .pdata) can point to the
629 same unwind info entry. There is no such indication while unwinding,
630 so we don't really care about that case. We suppose this scheme is
631 used to save memory when the unwind entries are exactly the same.
632 2. Chained unwind_info entries, with no unwind codes (no prologue).
633 There is a major difference with the previous case: the pc range for
634 the function is different (in case 1, the pc range comes from the
635 runtime_function entry; in case 2, the pc range for the chained entry
636 comes from the first unwind entry). Case 1 cannot be used instead as
637 the pc is not in the prologue. This case is officially documented.
638 (There might be unwind code in the first unwind entry to handle
639 additional unwinding). GCC (at least until gcc 5.0) doesn't chain
641 3. Undocumented unwind info redirection. Hard to know the exact purpose,
642 so it is considered as a memory optimization of case 2.
647 /* Unofficially documented unwind info redirection, when UNWIND_INFO
648 address is odd (http://www.codemachine.com/article_x64deepdive.html).
650 struct external_pex64_runtime_function d
;
652 if (target_read_memory (cache
->image_base
+ (unwind_info
& ~1),
653 (gdb_byte
*) &d
, sizeof (d
)) != 0)
657 = extract_unsigned_integer (d
.rva_BeginAddress
, 4, byte_order
);
659 = extract_unsigned_integer (d
.rva_EndAddress
, 4, byte_order
);
661 = extract_unsigned_integer (d
.rva_UnwindData
, 4, byte_order
);
666 struct external_pex64_unwind_info ex_ui
;
667 /* There are at most 256 16-bit unwind insns. */
668 gdb_byte insns
[2 * 256];
671 unsigned char codes_count
;
672 unsigned char frame_reg
;
675 /* Read and decode header. */
676 if (target_read_memory (cache
->image_base
+ unwind_info
,
677 (gdb_byte
*) &ex_ui
, sizeof (ex_ui
)) != 0)
683 "amd64_windows_frame_decodes_insn: "
684 "%s: ver: %02x, plgsz: %02x, cnt: %02x, frame: %02x\n",
685 paddress (gdbarch
, unwind_info
),
686 ex_ui
.Version_Flags
, ex_ui
.SizeOfPrologue
,
687 ex_ui
.CountOfCodes
, ex_ui
.FrameRegisterOffset
);
690 if (PEX64_UWI_VERSION (ex_ui
.Version_Flags
) != 1
691 && PEX64_UWI_VERSION (ex_ui
.Version_Flags
) != 2)
694 start
= cache
->image_base
+ cache
->start_rva
;
696 && !(cache
->pc
>= start
&& cache
->pc
< start
+ ex_ui
.SizeOfPrologue
))
698 /* We want to detect if the PC points to an epilogue. This needs
699 to be checked only once, and an epilogue can be anywhere but in
700 the prologue. If so, the epilogue detection+decoding function is
701 sufficient. Otherwise, the unwinder will consider that the PC
702 is in the body of the function and will need to decode unwind
704 if (amd64_windows_frame_decode_epilogue (this_frame
, cache
) == 1)
707 /* Not in an epilog. Clear possible side effects. */
708 memset (cache
->prev_reg_addr
, 0, sizeof (cache
->prev_reg_addr
));
711 codes_count
= ex_ui
.CountOfCodes
;
712 frame_reg
= PEX64_UWI_FRAMEREG (ex_ui
.FrameRegisterOffset
);
716 /* According to msdn:
717 If an FP reg is used, then any unwind code taking an offset must
718 only be used after the FP reg is established in the prolog. */
720 int frreg
= amd64_windows_w2gdb_regnum
[frame_reg
];
722 get_frame_register (this_frame
, frreg
, buf
);
723 save_addr
= extract_unsigned_integer (buf
, 8, byte_order
);
726 fprintf_unfiltered (gdb_stdlog
, " frame_reg=%s, val=%s\n",
727 gdbarch_register_name (gdbarch
, frreg
),
728 paddress (gdbarch
, save_addr
));
733 && target_read_memory (cache
->image_base
+ unwind_info
735 insns
, codes_count
* 2) != 0)
738 end_insns
= &insns
[codes_count
* 2];
741 /* Skip opcodes 6 of version 2. This opcode is not documented. */
742 if (PEX64_UWI_VERSION (ex_ui
.Version_Flags
) == 2)
744 for (; p
< end_insns
; p
+= 2)
745 if (PEX64_UNWCODE_CODE (p
[1]) != 6)
749 for (; p
< end_insns
; p
+= 2)
753 /* Virtually execute the operation if the pc is after the
754 corresponding instruction (that does matter in case of break
755 within the prologue). Note that for chained info (!first), the
756 prologue has been fully executed. */
757 if (cache
->pc
>= start
+ p
[0] || cache
->pc
< start
)
761 (gdb_stdlog
, " op #%u: off=0x%02x, insn=0x%02x\n",
762 (unsigned) (p
- insns
), p
[0], p
[1]);
764 /* If there is no frame registers defined, the current value of
765 rsp is used instead. */
771 switch (PEX64_UNWCODE_CODE (p
[1]))
773 case UWOP_PUSH_NONVOL
:
774 /* Push pre-decrements RSP. */
775 reg
= amd64_windows_w2gdb_regnum
[PEX64_UNWCODE_INFO (p
[1])];
776 cache
->prev_reg_addr
[reg
] = cur_sp
;
779 case UWOP_ALLOC_LARGE
:
780 if (PEX64_UNWCODE_INFO (p
[1]) == 0)
782 8 * extract_unsigned_integer (p
+ 2, 2, byte_order
);
783 else if (PEX64_UNWCODE_INFO (p
[1]) == 1)
784 cur_sp
+= extract_unsigned_integer (p
+ 2, 4, byte_order
);
788 case UWOP_ALLOC_SMALL
:
789 cur_sp
+= 8 + 8 * PEX64_UNWCODE_INFO (p
[1]);
793 - PEX64_UWI_FRAMEOFF (ex_ui
.FrameRegisterOffset
) * 16;
795 case UWOP_SAVE_NONVOL
:
796 reg
= amd64_windows_w2gdb_regnum
[PEX64_UNWCODE_INFO (p
[1])];
797 cache
->prev_reg_addr
[reg
] = save_addr
798 + 8 * extract_unsigned_integer (p
+ 2, 2, byte_order
);
800 case UWOP_SAVE_NONVOL_FAR
:
801 reg
= amd64_windows_w2gdb_regnum
[PEX64_UNWCODE_INFO (p
[1])];
802 cache
->prev_reg_addr
[reg
] = save_addr
803 + 8 * extract_unsigned_integer (p
+ 2, 4, byte_order
);
805 case UWOP_SAVE_XMM128
:
806 cache
->prev_xmm_addr
[PEX64_UNWCODE_INFO (p
[1])] =
808 - 16 * extract_unsigned_integer (p
+ 2, 2, byte_order
);
810 case UWOP_SAVE_XMM128_FAR
:
811 cache
->prev_xmm_addr
[PEX64_UNWCODE_INFO (p
[1])] =
813 - 16 * extract_unsigned_integer (p
+ 2, 4, byte_order
);
815 case UWOP_PUSH_MACHFRAME
:
816 if (PEX64_UNWCODE_INFO (p
[1]) == 0)
818 cache
->prev_rip_addr
= cur_sp
+ 0;
819 cache
->prev_rsp_addr
= cur_sp
+ 24;
822 else if (PEX64_UNWCODE_INFO (p
[1]) == 1)
824 cache
->prev_rip_addr
= cur_sp
+ 8;
825 cache
->prev_rsp_addr
= cur_sp
+ 32;
835 /* Display address where the register was saved. */
836 if (frame_debug
&& reg
>= 0)
838 (gdb_stdlog
, " [reg %s at %s]\n",
839 gdbarch_register_name (gdbarch
, reg
),
840 paddress (gdbarch
, cache
->prev_reg_addr
[reg
]));
843 /* Adjust with the length of the opcode. */
844 switch (PEX64_UNWCODE_CODE (p
[1]))
846 case UWOP_PUSH_NONVOL
:
847 case UWOP_ALLOC_SMALL
:
849 case UWOP_PUSH_MACHFRAME
:
851 case UWOP_ALLOC_LARGE
:
852 if (PEX64_UNWCODE_INFO (p
[1]) == 0)
854 else if (PEX64_UNWCODE_INFO (p
[1]) == 1)
859 case UWOP_SAVE_NONVOL
:
860 case UWOP_SAVE_XMM128
:
863 case UWOP_SAVE_NONVOL_FAR
:
864 case UWOP_SAVE_XMM128_FAR
:
871 if (PEX64_UWI_FLAGS (ex_ui
.Version_Flags
) != UNW_FLAG_CHAININFO
)
873 /* End of unwind info. */
878 /* Read the chained unwind info. */
879 struct external_pex64_runtime_function d
;
882 /* Not anymore the first entry. */
885 /* Stay aligned on word boundary. */
886 chain_vma
= cache
->image_base
+ unwind_info
887 + sizeof (ex_ui
) + ((codes_count
+ 1) & ~1) * 2;
889 if (target_read_memory (chain_vma
, (gdb_byte
*) &d
, sizeof (d
)) != 0)
892 /* Decode begin/end. This may be different from .pdata index, as
893 an unwind info may be shared by several functions (in particular
894 if many functions have the same prolog and handler. */
896 extract_unsigned_integer (d
.rva_BeginAddress
, 4, byte_order
);
898 extract_unsigned_integer (d
.rva_EndAddress
, 4, byte_order
);
900 extract_unsigned_integer (d
.rva_UnwindData
, 4, byte_order
);
905 "amd64_windows_frame_decodes_insn (next in chain):"
906 " unwind_data=%s, start_rva=%s, end_rva=%s\n",
907 paddress (gdbarch
, unwind_info
),
908 paddress (gdbarch
, cache
->start_rva
),
909 paddress (gdbarch
, cache
->end_rva
));
912 /* Allow the user to break this loop. */
915 /* PC is saved by the call. */
916 if (cache
->prev_rip_addr
== 0)
917 cache
->prev_rip_addr
= cur_sp
;
918 cache
->prev_sp
= cur_sp
+ 8;
921 fprintf_unfiltered (gdb_stdlog
, " prev_sp: %s, prev_pc @%s\n",
922 paddress (gdbarch
, cache
->prev_sp
),
923 paddress (gdbarch
, cache
->prev_rip_addr
));
926 /* Find SEH unwind info for PC, returning 0 on success.
928 UNWIND_INFO is set to the rva of unwind info address, IMAGE_BASE
929 to the base address of the corresponding image, and START_RVA
930 to the rva of the function containing PC. */
933 amd64_windows_find_unwind_info (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
934 CORE_ADDR
*unwind_info
,
935 CORE_ADDR
*image_base
,
936 CORE_ADDR
*start_rva
,
939 struct obj_section
*sec
;
941 IMAGE_DATA_DIRECTORY
*dir
;
942 struct objfile
*objfile
;
943 unsigned long lo
, hi
;
945 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
947 /* Get the corresponding exception directory. */
948 sec
= find_pc_section (pc
);
951 objfile
= sec
->objfile
;
952 pe
= pe_data (sec
->objfile
->obfd
);
953 dir
= &pe
->pe_opthdr
.DataDirectory
[PE_EXCEPTION_TABLE
];
955 base
= pe
->pe_opthdr
.ImageBase
956 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
961 Note: This does not handle dynamically added entries (for JIT
962 engines). For this, we would need to ask the kernel directly,
963 which means getting some info from the native layer. For the
964 rest of the code, however, it's probably faster to search
965 the entry ourselves. */
967 hi
= dir
->Size
/ sizeof (struct external_pex64_runtime_function
);
971 unsigned long mid
= lo
+ (hi
- lo
) / 2;
972 struct external_pex64_runtime_function d
;
975 if (target_read_memory (base
+ dir
->VirtualAddress
+ mid
* sizeof (d
),
976 (gdb_byte
*) &d
, sizeof (d
)) != 0)
979 sa
= extract_unsigned_integer (d
.rva_BeginAddress
, 4, byte_order
);
980 ea
= extract_unsigned_integer (d
.rva_EndAddress
, 4, byte_order
);
983 else if (pc
>= base
+ ea
)
985 else if (pc
>= base
+ sa
&& pc
< base
+ ea
)
991 extract_unsigned_integer (d
.rva_UnwindData
, 4, byte_order
);
1001 "amd64_windows_find_unwind_data: image_base=%s, unwind_data=%s\n",
1002 paddress (gdbarch
, base
), paddress (gdbarch
, *unwind_info
));
1007 /* Fill THIS_CACHE using the native amd64-windows unwinding data
1010 static struct amd64_windows_frame_cache
*
1011 amd64_windows_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
1013 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1014 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1015 struct amd64_windows_frame_cache
*cache
;
1018 CORE_ADDR unwind_info
= 0;
1021 return (struct amd64_windows_frame_cache
*) *this_cache
;
1023 cache
= FRAME_OBSTACK_ZALLOC (struct amd64_windows_frame_cache
);
1024 *this_cache
= cache
;
1026 /* Get current PC and SP. */
1027 pc
= get_frame_pc (this_frame
);
1028 get_frame_register (this_frame
, AMD64_RSP_REGNUM
, buf
);
1029 cache
->sp
= extract_unsigned_integer (buf
, 8, byte_order
);
1032 if (amd64_windows_find_unwind_info (gdbarch
, pc
, &unwind_info
,
1038 if (unwind_info
== 0)
1040 /* Assume a leaf function. */
1041 cache
->prev_sp
= cache
->sp
+ 8;
1042 cache
->prev_rip_addr
= cache
->sp
;
1046 /* Decode unwind insns to compute saved addresses. */
1047 amd64_windows_frame_decode_insns (this_frame
, cache
, unwind_info
);
1052 /* Implement the "prev_register" method of struct frame_unwind
1053 using the standard Windows x64 SEH info. */
1055 static struct value
*
1056 amd64_windows_frame_prev_register (struct frame_info
*this_frame
,
1057 void **this_cache
, int regnum
)
1059 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1060 struct amd64_windows_frame_cache
*cache
=
1061 amd64_windows_frame_cache (this_frame
, this_cache
);
1065 fprintf_unfiltered (gdb_stdlog
,
1066 "amd64_windows_frame_prev_register %s for sp=%s\n",
1067 gdbarch_register_name (gdbarch
, regnum
),
1068 paddress (gdbarch
, cache
->prev_sp
));
1070 if (regnum
>= AMD64_XMM0_REGNUM
&& regnum
<= AMD64_XMM0_REGNUM
+ 15)
1071 prev
= cache
->prev_xmm_addr
[regnum
- AMD64_XMM0_REGNUM
];
1072 else if (regnum
== AMD64_RSP_REGNUM
)
1074 prev
= cache
->prev_rsp_addr
;
1076 return frame_unwind_got_constant (this_frame
, regnum
, cache
->prev_sp
);
1078 else if (regnum
>= AMD64_RAX_REGNUM
&& regnum
<= AMD64_R15_REGNUM
)
1079 prev
= cache
->prev_reg_addr
[regnum
- AMD64_RAX_REGNUM
];
1080 else if (regnum
== AMD64_RIP_REGNUM
)
1081 prev
= cache
->prev_rip_addr
;
1085 if (prev
&& frame_debug
)
1086 fprintf_unfiltered (gdb_stdlog
, " -> at %s\n", paddress (gdbarch
, prev
));
1090 /* Register was saved. */
1091 return frame_unwind_got_memory (this_frame
, regnum
, prev
);
1095 /* Register is either volatile or not modified. */
1096 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1100 /* Implement the "this_id" method of struct frame_unwind using
1101 the standard Windows x64 SEH info. */
1104 amd64_windows_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
1105 struct frame_id
*this_id
)
1107 struct amd64_windows_frame_cache
*cache
=
1108 amd64_windows_frame_cache (this_frame
, this_cache
);
1110 *this_id
= frame_id_build (cache
->prev_sp
,
1111 cache
->image_base
+ cache
->start_rva
);
1114 /* Windows x64 SEH unwinder. */
1116 static const struct frame_unwind amd64_windows_frame_unwind
=
1119 default_frame_unwind_stop_reason
,
1120 &amd64_windows_frame_this_id
,
1121 &amd64_windows_frame_prev_register
,
1123 default_frame_sniffer
1126 /* Implement the "skip_prologue" gdbarch method. */
1129 amd64_windows_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1131 CORE_ADDR func_addr
;
1132 CORE_ADDR unwind_info
= 0;
1133 CORE_ADDR image_base
, start_rva
, end_rva
;
1134 struct external_pex64_unwind_info ex_ui
;
1136 /* Use prologue size from unwind info. */
1137 if (amd64_windows_find_unwind_info (gdbarch
, pc
, &unwind_info
,
1138 &image_base
, &start_rva
, &end_rva
) == 0)
1140 if (unwind_info
== 0)
1142 /* Leaf function. */
1145 else if (target_read_memory (image_base
+ unwind_info
,
1146 (gdb_byte
*) &ex_ui
, sizeof (ex_ui
)) == 0
1147 && PEX64_UWI_VERSION (ex_ui
.Version_Flags
) == 1)
1148 return std::max (pc
, image_base
+ start_rva
+ ex_ui
.SizeOfPrologue
);
1151 /* See if we can determine the end of the prologue via the symbol
1152 table. If so, then return either the PC, or the PC after
1153 the prologue, whichever is greater. */
1154 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
))
1156 CORE_ADDR post_prologue_pc
1157 = skip_prologue_using_sal (gdbarch
, func_addr
);
1159 if (post_prologue_pc
!= 0)
1160 return std::max (pc
, post_prologue_pc
);
1166 /* Check Win64 DLL jmp trampolines and find jump destination. */
1169 amd64_windows_skip_trampoline_code (struct frame_info
*frame
, CORE_ADDR pc
)
1171 CORE_ADDR destination
= 0;
1172 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1173 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1175 /* Check for jmp *<offset>(%rip) (jump near, absolute indirect (/4)). */
1176 if (pc
&& read_memory_unsigned_integer (pc
, 2, byte_order
) == 0x25ff)
1178 /* Get opcode offset and see if we can find a reference in our data. */
1180 = read_memory_unsigned_integer (pc
+ 2, 4, byte_order
);
1182 /* Get address of function pointer at end of pc. */
1183 CORE_ADDR indirect_addr
= pc
+ offset
+ 6;
1185 struct minimal_symbol
*indsym
1187 ? lookup_minimal_symbol_by_pc (indirect_addr
).minsym
1189 const char *symname
= indsym
? MSYMBOL_LINKAGE_NAME (indsym
) : NULL
;
1193 if (startswith (symname
, "__imp_")
1194 || startswith (symname
, "_imp_"))
1196 = read_memory_unsigned_integer (indirect_addr
, 8, byte_order
);
1203 /* Implement the "auto_wide_charset" gdbarch method. */
1206 amd64_windows_auto_wide_charset (void)
1212 amd64_windows_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1214 /* The dwarf2 unwinder (appended very early by i386_gdbarch_init) is
1215 preferred over the SEH one. The reasons are:
1216 - binaries without SEH but with dwarf2 debug info are correcly handled
1217 (although they aren't ABI compliant, gcc before 4.7 didn't emit SEH
1219 - dwarf3 DW_OP_call_frame_cfa is correctly handled (it can only be
1220 handled if the dwarf2 unwinder is used).
1222 The call to amd64_init_abi appends default unwinders, that aren't
1223 compatible with the SEH one.
1225 frame_unwind_append_unwinder (gdbarch
, &amd64_windows_frame_unwind
);
1227 amd64_init_abi (info
, gdbarch
);
1229 windows_init_abi (info
, gdbarch
);
1231 /* On Windows, "long"s are only 32bit. */
1232 set_gdbarch_long_bit (gdbarch
, 32);
1234 /* Function calls. */
1235 set_gdbarch_push_dummy_call (gdbarch
, amd64_windows_push_dummy_call
);
1236 set_gdbarch_return_value (gdbarch
, amd64_windows_return_value
);
1237 set_gdbarch_skip_main_prologue (gdbarch
, amd64_skip_main_prologue
);
1238 set_gdbarch_skip_trampoline_code (gdbarch
,
1239 amd64_windows_skip_trampoline_code
);
1241 set_gdbarch_skip_prologue (gdbarch
, amd64_windows_skip_prologue
);
1243 set_gdbarch_auto_wide_charset (gdbarch
, amd64_windows_auto_wide_charset
);
1246 /* -Wmissing-prototypes */
1247 extern initialize_file_ftype _initialize_amd64_windows_tdep
;
1250 _initialize_amd64_windows_tdep (void)
1252 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
, GDB_OSABI_CYGWIN
,
1253 amd64_windows_init_abi
);