1 /* Copyright (C) 2009-2013 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"
34 /* The registers used to pass integer arguments during a function call. */
35 static int amd64_windows_dummy_call_integer_regs
[] =
37 AMD64_RCX_REGNUM
, /* %rcx */
38 AMD64_RDX_REGNUM
, /* %rdx */
39 AMD64_R8_REGNUM
, /* %r8 */
40 AMD64_R9_REGNUM
/* %r9 */
43 /* Return nonzero if an argument of type TYPE should be passed
44 via one of the integer registers. */
47 amd64_windows_passed_by_integer_register (struct type
*type
)
49 switch (TYPE_CODE (type
))
58 case TYPE_CODE_STRUCT
:
60 return (TYPE_LENGTH (type
) == 1
61 || TYPE_LENGTH (type
) == 2
62 || TYPE_LENGTH (type
) == 4
63 || TYPE_LENGTH (type
) == 8);
70 /* Return nonzero if an argument of type TYPE should be passed
71 via one of the XMM registers. */
74 amd64_windows_passed_by_xmm_register (struct type
*type
)
76 return ((TYPE_CODE (type
) == TYPE_CODE_FLT
77 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
78 && (TYPE_LENGTH (type
) == 4 || TYPE_LENGTH (type
) == 8));
81 /* Return non-zero iff an argument of the given TYPE should be passed
85 amd64_windows_passed_by_pointer (struct type
*type
)
87 if (amd64_windows_passed_by_integer_register (type
))
90 if (amd64_windows_passed_by_xmm_register (type
))
96 /* For each argument that should be passed by pointer, reserve some
97 stack space, store a copy of the argument on the stack, and replace
98 the argument by its address. Return the new Stack Pointer value.
100 NARGS is the number of arguments. ARGS is the array containing
101 the value of each argument. SP is value of the Stack Pointer. */
104 amd64_windows_adjust_args_passed_by_pointer (struct value
**args
,
105 int nargs
, CORE_ADDR sp
)
109 for (i
= 0; i
< nargs
; i
++)
110 if (amd64_windows_passed_by_pointer (value_type (args
[i
])))
112 struct type
*type
= value_type (args
[i
]);
113 const gdb_byte
*valbuf
= value_contents (args
[i
]);
114 const int len
= TYPE_LENGTH (type
);
116 /* Store a copy of that argument on the stack, aligned to
117 a 16 bytes boundary, and then use the copy's address as
122 write_memory (sp
, valbuf
, len
);
125 = value_addr (value_from_contents_and_address (type
, valbuf
, sp
));
131 /* Store the value of ARG in register REGNO (right-justified).
132 REGCACHE is the register cache. */
135 amd64_windows_store_arg_in_reg (struct regcache
*regcache
,
136 struct value
*arg
, int regno
)
138 struct type
*type
= value_type (arg
);
139 const gdb_byte
*valbuf
= value_contents (arg
);
142 gdb_assert (TYPE_LENGTH (type
) <= 8);
143 memset (buf
, 0, sizeof buf
);
144 memcpy (buf
, valbuf
, min (TYPE_LENGTH (type
), 8));
145 regcache_cooked_write (regcache
, regno
, buf
);
148 /* Push the arguments for an inferior function call, and return
149 the updated value of the SP (Stack Pointer).
151 All arguments are identical to the arguments used in
152 amd64_windows_push_dummy_call. */
155 amd64_windows_push_arguments (struct regcache
*regcache
, int nargs
,
156 struct value
**args
, CORE_ADDR sp
,
161 struct value
**stack_args
= alloca (nargs
* sizeof (struct value
*));
162 int num_stack_args
= 0;
163 int num_elements
= 0;
166 /* First, handle the arguments passed by pointer.
168 These arguments are replaced by pointers to a copy we are making
169 in inferior memory. So use a copy of the ARGS table, to avoid
170 modifying the original one. */
172 struct value
**args1
= alloca (nargs
* sizeof (struct value
*));
174 memcpy (args1
, args
, nargs
* sizeof (struct value
*));
175 sp
= amd64_windows_adjust_args_passed_by_pointer (args1
, nargs
, sp
);
179 /* Reserve a register for the "hidden" argument. */
183 for (i
= 0; i
< nargs
; i
++)
185 struct type
*type
= value_type (args
[i
]);
186 int len
= TYPE_LENGTH (type
);
189 if (reg_idx
< ARRAY_SIZE (amd64_windows_dummy_call_integer_regs
))
191 if (amd64_windows_passed_by_integer_register (type
))
193 amd64_windows_store_arg_in_reg
195 amd64_windows_dummy_call_integer_regs
[reg_idx
]);
199 else if (amd64_windows_passed_by_xmm_register (type
))
201 amd64_windows_store_arg_in_reg
202 (regcache
, args
[i
], AMD64_XMM0_REGNUM
+ reg_idx
);
203 /* In case of varargs, these parameters must also be
204 passed via the integer registers. */
205 amd64_windows_store_arg_in_reg
207 amd64_windows_dummy_call_integer_regs
[reg_idx
]);
215 num_elements
+= ((len
+ 7) / 8);
216 stack_args
[num_stack_args
++] = args
[i
];
220 /* Allocate space for the arguments on the stack, keeping it
221 aligned on a 16 byte boundary. */
222 sp
-= num_elements
* 8;
225 /* Write out the arguments to the stack. */
226 for (i
= 0; i
< num_stack_args
; i
++)
228 struct type
*type
= value_type (stack_args
[i
]);
229 const gdb_byte
*valbuf
= value_contents (stack_args
[i
]);
231 write_memory (sp
+ element
* 8, valbuf
, TYPE_LENGTH (type
));
232 element
+= ((TYPE_LENGTH (type
) + 7) / 8);
238 /* Implement the "push_dummy_call" gdbarch method. */
241 amd64_windows_push_dummy_call
242 (struct gdbarch
*gdbarch
, struct value
*function
,
243 struct regcache
*regcache
, CORE_ADDR bp_addr
,
244 int nargs
, struct value
**args
,
245 CORE_ADDR sp
, int struct_return
, CORE_ADDR struct_addr
)
247 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
250 /* Pass arguments. */
251 sp
= amd64_windows_push_arguments (regcache
, nargs
, args
, sp
,
254 /* Pass "hidden" argument". */
257 /* The "hidden" argument is passed throught the first argument
259 const int arg_regnum
= amd64_windows_dummy_call_integer_regs
[0];
261 store_unsigned_integer (buf
, 8, byte_order
, struct_addr
);
262 regcache_cooked_write (regcache
, arg_regnum
, buf
);
265 /* Reserve some memory on the stack for the integer-parameter
266 registers, as required by the ABI. */
267 sp
-= ARRAY_SIZE (amd64_windows_dummy_call_integer_regs
) * 8;
269 /* Store return address. */
271 store_unsigned_integer (buf
, 8, byte_order
, bp_addr
);
272 write_memory (sp
, buf
, 8);
274 /* Update the stack pointer... */
275 store_unsigned_integer (buf
, 8, byte_order
, sp
);
276 regcache_cooked_write (regcache
, AMD64_RSP_REGNUM
, buf
);
278 /* ...and fake a frame pointer. */
279 regcache_cooked_write (regcache
, AMD64_RBP_REGNUM
, buf
);
284 /* Implement the "return_value" gdbarch method for amd64-windows. */
286 static enum return_value_convention
287 amd64_windows_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
288 struct type
*type
, struct regcache
*regcache
,
289 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
291 int len
= TYPE_LENGTH (type
);
294 /* See if our value is returned through a register. If it is, then
295 store the associated register number in REGNUM. */
296 switch (TYPE_CODE (type
))
299 case TYPE_CODE_DECFLOAT
:
300 /* __m128, __m128i, __m128d, floats, and doubles are returned
302 if (len
== 4 || len
== 8 || len
== 16)
303 regnum
= AMD64_XMM0_REGNUM
;
306 /* All other values that are 1, 2, 4 or 8 bytes long are returned
308 if (len
== 1 || len
== 2 || len
== 4 || len
== 8)
309 regnum
= AMD64_RAX_REGNUM
;
315 /* RAX contains the address where the return value has been stored. */
320 regcache_raw_read_unsigned (regcache
, AMD64_RAX_REGNUM
, &addr
);
321 read_memory (addr
, readbuf
, TYPE_LENGTH (type
));
323 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
327 /* Extract the return value from the register where it was stored. */
329 regcache_raw_read_part (regcache
, regnum
, 0, len
, readbuf
);
331 regcache_raw_write_part (regcache
, regnum
, 0, len
, writebuf
);
332 return RETURN_VALUE_REGISTER_CONVENTION
;
336 /* Check that the code pointed to by PC corresponds to a call to
337 __main, skip it if so. Return PC otherwise. */
340 amd64_skip_main_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
342 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
345 target_read_memory (pc
, &op
, 1);
350 if (target_read_memory (pc
+ 1, buf
, sizeof buf
) == 0)
352 struct bound_minimal_symbol s
;
355 call_dest
= pc
+ 5 + extract_signed_integer (buf
, 4, byte_order
);
356 s
= lookup_minimal_symbol_by_pc (call_dest
);
358 && SYMBOL_LINKAGE_NAME (s
.minsym
) != NULL
359 && strcmp (SYMBOL_LINKAGE_NAME (s
.minsym
), "__main") == 0)
367 struct amd64_windows_frame_cache
369 /* ImageBase for the module. */
370 CORE_ADDR image_base
;
372 /* Function start and end rva. */
376 /* Next instruction to be executed. */
382 /* Address of saved integer and xmm registers. */
383 CORE_ADDR prev_reg_addr
[16];
384 CORE_ADDR prev_xmm_addr
[16];
386 /* These two next fields are set only for machine info frames. */
388 /* Likewise for RIP. */
389 CORE_ADDR prev_rip_addr
;
391 /* Likewise for RSP. */
392 CORE_ADDR prev_rsp_addr
;
394 /* Address of the previous frame. */
398 /* Convert a Windows register number to gdb. */
399 static const enum amd64_regnum amd64_windows_w2gdb_regnum
[] =
419 /* Return TRUE iff PC is the the range of the function corresponding to
423 pc_in_range (CORE_ADDR pc
, const struct amd64_windows_frame_cache
*cache
)
425 return (pc
>= cache
->image_base
+ cache
->start_rva
426 && pc
< cache
->image_base
+ cache
->end_rva
);
429 /* Try to recognize and decode an epilogue sequence.
431 Return -1 if we fail to read the instructions for any reason.
432 Return 1 if an epilogue sequence was recognized, 0 otherwise. */
435 amd64_windows_frame_decode_epilogue (struct frame_info
*this_frame
,
436 struct amd64_windows_frame_cache
*cache
)
438 /* According to MSDN an epilogue "must consist of either an add RSP,constant
439 or lea RSP,constant[FPReg], followed by a series of zero or more 8-byte
440 register pops and a return or a jmp".
442 Furthermore, according to RtlVirtualUnwind, the complete list of
447 - jmp imm8 | imm32 [eb rel8] or [e9 rel32]
448 - jmp qword ptr imm32 - not handled
449 - rex.w jmp reg [4X ff eY]
452 CORE_ADDR pc
= cache
->pc
;
453 CORE_ADDR cur_sp
= cache
->sp
;
454 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
455 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
459 /* We don't care about the instruction deallocating the frame:
460 if it hasn't been executed, the pc is still in the body,
461 if it has been executed, the following epilog decoding will work. */
464 - pop reg [41 58-5f] or [58-5f]. */
469 if (target_read_memory (pc
, &op
, 1) != 0)
472 if (op
>= 0x40 && op
<= 0x4f)
478 if (target_read_memory (pc
+ 1, &op
, 1) != 0)
484 if (op
>= 0x58 && op
<= 0x5f)
487 gdb_byte reg
= (op
& 0x0f) | ((rex
& 1) << 3);
489 cache
->prev_reg_addr
[amd64_windows_w2gdb_regnum
[reg
]] = cur_sp
;
495 /* Allow the user to break this loop. This shouldn't happen as the
496 number of consecutive pop should be small. */
500 /* Then decode the marker. */
503 if (target_read_memory (pc
, &op
, 1) != 0)
510 cache
->prev_rip_addr
= cur_sp
;
511 cache
->prev_sp
= cur_sp
+ 8;
520 if (target_read_memory (pc
+ 1, &rel8
, 1) != 0)
522 npc
= pc
+ 2 + (signed char) rel8
;
524 /* If the jump is within the function, then this is not a marker,
525 otherwise this is a tail-call. */
526 return !pc_in_range (npc
, cache
);
535 if (target_read_memory (pc
+ 1, rel32
, 4) != 0)
537 npc
= pc
+ 5 + extract_signed_integer (rel32
, 4, byte_order
);
539 /* If the jump is within the function, then this is not a marker,
540 otherwise this is a tail-call. */
541 return !pc_in_range (npc
, cache
);
549 if (target_read_memory (pc
+ 1, imm16
, 2) != 0)
551 cache
->prev_rip_addr
= cur_sp
;
552 cache
->prev_sp
= cur_sp
553 + extract_unsigned_integer (imm16
, 4, byte_order
);
562 if (target_read_memory (pc
+ 2, &op1
, 1) != 0)
567 cache
->prev_rip_addr
= cur_sp
;
568 cache
->prev_sp
= cur_sp
+ 8;
588 /* Got a REX prefix, read next byte. */
590 if (target_read_memory (pc
+ 1, &op
, 1) != 0)
600 if (target_read_memory (pc
+ 2, &op1
, 1) != 0)
602 return (op1
& 0xf8) == 0xe0;
608 /* Not REX, so unknown. */
613 /* Decode and execute unwind insns at UNWIND_INFO. */
616 amd64_windows_frame_decode_insns (struct frame_info
*this_frame
,
617 struct amd64_windows_frame_cache
*cache
,
618 CORE_ADDR unwind_info
)
620 CORE_ADDR save_addr
= 0;
621 CORE_ADDR cur_sp
= cache
->sp
;
622 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
623 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
628 struct external_pex64_unwind_info ex_ui
;
629 /* There are at most 256 16-bit unwind insns. */
630 gdb_byte insns
[2 * 256];
633 unsigned char codes_count
;
634 unsigned char frame_reg
;
635 unsigned char frame_off
;
637 /* Read and decode header. */
638 if (target_read_memory (cache
->image_base
+ unwind_info
,
639 (gdb_byte
*) &ex_ui
, sizeof (ex_ui
)) != 0)
645 "amd64_windows_frame_decodes_insn: "
646 "%s: ver: %02x, plgsz: %02x, cnt: %02x, frame: %02x\n",
647 paddress (gdbarch
, unwind_info
),
648 ex_ui
.Version_Flags
, ex_ui
.SizeOfPrologue
,
649 ex_ui
.CountOfCodes
, ex_ui
.FrameRegisterOffset
);
652 if (PEX64_UWI_VERSION (ex_ui
.Version_Flags
) != 1)
657 cache
->image_base
+ cache
->start_rva
+ ex_ui
.SizeOfPrologue
))
659 /* Not in the prologue. We want to detect if the PC points to an
660 epilogue. If so, the epilogue detection+decoding function is
661 sufficient. Otherwise, the unwinder will consider that the PC
662 is in the body of the function and will need to decode unwind
664 if (amd64_windows_frame_decode_epilogue (this_frame
, cache
) == 1)
667 /* Not in an epilog. Clear possible side effects. */
668 memset (cache
->prev_reg_addr
, 0, sizeof (cache
->prev_reg_addr
));
671 codes_count
= ex_ui
.CountOfCodes
;
672 frame_reg
= PEX64_UWI_FRAMEREG (ex_ui
.FrameRegisterOffset
);
676 /* According to msdn:
677 If an FP reg is used, then any unwind code taking an offset must
678 only be used after the FP reg is established in the prolog. */
680 int frreg
= amd64_windows_w2gdb_regnum
[frame_reg
];
682 get_frame_register (this_frame
, frreg
, buf
);
683 save_addr
= extract_unsigned_integer (buf
, 8, byte_order
);
686 fprintf_unfiltered (gdb_stdlog
, " frame_reg=%s, val=%s\n",
687 gdbarch_register_name (gdbarch
, frreg
),
688 paddress (gdbarch
, save_addr
));
693 && target_read_memory (cache
->image_base
+ unwind_info
695 insns
, codes_count
* 2) != 0)
698 end_insns
= &insns
[codes_count
* 2];
699 for (p
= insns
; p
< end_insns
; p
+= 2)
705 (gdb_stdlog
, " op #%u: off=0x%02x, insn=0x%02x\n",
706 (unsigned) (p
- insns
), p
[0], p
[1]);
708 /* Virtually execute the operation. */
709 if (cache
->pc
>= cache
->image_base
+ cache
->start_rva
+ p
[0])
711 /* If there is no frame registers defined, the current value of
712 rsp is used instead. */
716 switch (PEX64_UNWCODE_CODE (p
[1]))
718 case UWOP_PUSH_NONVOL
:
719 /* Push pre-decrements RSP. */
720 reg
= amd64_windows_w2gdb_regnum
[PEX64_UNWCODE_INFO (p
[1])];
721 cache
->prev_reg_addr
[reg
] = cur_sp
;
724 case UWOP_ALLOC_LARGE
:
725 if (PEX64_UNWCODE_INFO (p
[1]) == 0)
727 8 * extract_unsigned_integer (p
+ 2, 2, byte_order
);
728 else if (PEX64_UNWCODE_INFO (p
[1]) == 1)
729 cur_sp
+= extract_unsigned_integer (p
+ 2, 4, byte_order
);
733 case UWOP_ALLOC_SMALL
:
734 cur_sp
+= 8 + 8 * PEX64_UNWCODE_INFO (p
[1]);
738 - PEX64_UWI_FRAMEOFF (ex_ui
.FrameRegisterOffset
) * 16;
740 case UWOP_SAVE_NONVOL
:
741 reg
= amd64_windows_w2gdb_regnum
[PEX64_UNWCODE_INFO (p
[1])];
742 cache
->prev_reg_addr
[reg
] = save_addr
743 - 8 * extract_unsigned_integer (p
+ 2, 2, byte_order
);
745 case UWOP_SAVE_NONVOL_FAR
:
746 reg
= amd64_windows_w2gdb_regnum
[PEX64_UNWCODE_INFO (p
[1])];
747 cache
->prev_reg_addr
[reg
] = save_addr
748 - 8 * extract_unsigned_integer (p
+ 2, 4, byte_order
);
750 case UWOP_SAVE_XMM128
:
751 cache
->prev_xmm_addr
[PEX64_UNWCODE_INFO (p
[1])] =
753 - 16 * extract_unsigned_integer (p
+ 2, 2, byte_order
);
755 case UWOP_SAVE_XMM128_FAR
:
756 cache
->prev_xmm_addr
[PEX64_UNWCODE_INFO (p
[1])] =
758 - 16 * extract_unsigned_integer (p
+ 2, 4, byte_order
);
760 case UWOP_PUSH_MACHFRAME
:
761 if (PEX64_UNWCODE_INFO (p
[1]) == 0)
763 cache
->prev_rip_addr
= cur_sp
+ 0;
764 cache
->prev_rsp_addr
= cur_sp
+ 24;
767 else if (PEX64_UNWCODE_INFO (p
[1]) == 1)
769 cache
->prev_rip_addr
= cur_sp
+ 8;
770 cache
->prev_rsp_addr
= cur_sp
+ 32;
781 /* Adjust with the length of the opcode. */
782 switch (PEX64_UNWCODE_CODE (p
[1]))
784 case UWOP_PUSH_NONVOL
:
785 case UWOP_ALLOC_SMALL
:
787 case UWOP_PUSH_MACHFRAME
:
789 case UWOP_ALLOC_LARGE
:
790 if (PEX64_UNWCODE_INFO (p
[1]) == 0)
792 else if (PEX64_UNWCODE_INFO (p
[1]) == 1)
797 case UWOP_SAVE_NONVOL
:
798 case UWOP_SAVE_XMM128
:
801 case UWOP_SAVE_NONVOL_FAR
:
802 case UWOP_SAVE_XMM128_FAR
:
809 if (PEX64_UWI_FLAGS (ex_ui
.Version_Flags
) != UNW_FLAG_CHAININFO
)
813 /* Read the chained unwind info. */
814 struct external_pex64_runtime_function d
;
817 chain_vma
= cache
->image_base
+ unwind_info
818 + sizeof (ex_ui
) + ((codes_count
+ 1) & ~1) * 2 + 8;
820 if (target_read_memory (chain_vma
, (gdb_byte
*) &d
, sizeof (d
)) != 0)
824 extract_unsigned_integer (d
.rva_BeginAddress
, 4, byte_order
);
826 extract_unsigned_integer (d
.rva_EndAddress
, 4, byte_order
);
828 extract_unsigned_integer (d
.rva_UnwindData
, 4, byte_order
);
831 /* Allow the user to break this loop. */
834 /* PC is saved by the call. */
835 if (cache
->prev_rip_addr
== 0)
836 cache
->prev_rip_addr
= cur_sp
;
837 cache
->prev_sp
= cur_sp
+ 8;
840 fprintf_unfiltered (gdb_stdlog
, " prev_sp: %s, prev_pc @%s\n",
841 paddress (gdbarch
, cache
->prev_sp
),
842 paddress (gdbarch
, cache
->prev_rip_addr
));
845 /* Find SEH unwind info for PC, returning 0 on success.
847 UNWIND_INFO is set to the rva of unwind info address, IMAGE_BASE
848 to the base address of the corresponding image, and START_RVA
849 to the rva of the function containing PC. */
852 amd64_windows_find_unwind_info (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
853 CORE_ADDR
*unwind_info
,
854 CORE_ADDR
*image_base
,
855 CORE_ADDR
*start_rva
,
858 struct obj_section
*sec
;
860 IMAGE_DATA_DIRECTORY
*dir
;
861 struct objfile
*objfile
;
862 unsigned long lo
, hi
;
864 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
866 /* Get the corresponding exception directory. */
867 sec
= find_pc_section (pc
);
870 objfile
= sec
->objfile
;
871 pe
= pe_data (sec
->objfile
->obfd
);
872 dir
= &pe
->pe_opthdr
.DataDirectory
[PE_EXCEPTION_TABLE
];
874 base
= pe
->pe_opthdr
.ImageBase
875 + ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
880 Note: This does not handle dynamically added entries (for JIT
881 engines). For this, we would need to ask the kernel directly,
882 which means getting some info from the native layer. For the
883 rest of the code, however, it's probably faster to search
884 the entry ourselves. */
886 hi
= dir
->Size
/ sizeof (struct external_pex64_runtime_function
);
890 unsigned long mid
= lo
+ (hi
- lo
) / 2;
891 struct external_pex64_runtime_function d
;
894 if (target_read_memory (base
+ dir
->VirtualAddress
+ mid
* sizeof (d
),
895 (gdb_byte
*) &d
, sizeof (d
)) != 0)
898 sa
= extract_unsigned_integer (d
.rva_BeginAddress
, 4, byte_order
);
899 ea
= extract_unsigned_integer (d
.rva_EndAddress
, 4, byte_order
);
902 else if (pc
>= base
+ ea
)
904 else if (pc
>= base
+ sa
&& pc
< base
+ ea
)
910 extract_unsigned_integer (d
.rva_UnwindData
, 4, byte_order
);
920 "amd64_windows_find_unwind_data: image_base=%s, unwind_data=%s\n",
921 paddress (gdbarch
, base
), paddress (gdbarch
, *unwind_info
));
923 if (*unwind_info
& 1)
925 /* Unofficially documented unwind info redirection, when UNWIND_INFO
926 address is odd (http://www.codemachine.com/article_x64deepdive.html).
928 struct external_pex64_runtime_function d
;
931 if (target_read_memory (base
+ (*unwind_info
& ~1),
932 (gdb_byte
*) &d
, sizeof (d
)) != 0)
936 extract_unsigned_integer (d
.rva_BeginAddress
, 4, byte_order
);
937 *end_rva
= extract_unsigned_integer (d
.rva_EndAddress
, 4, byte_order
);
939 extract_unsigned_integer (d
.rva_UnwindData
, 4, byte_order
);
945 /* Fill THIS_CACHE using the native amd64-windows unwinding data
948 static struct amd64_windows_frame_cache
*
949 amd64_windows_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
951 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
952 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
953 struct amd64_windows_frame_cache
*cache
;
955 struct obj_section
*sec
;
957 IMAGE_DATA_DIRECTORY
*dir
;
958 CORE_ADDR image_base
;
960 struct objfile
*objfile
;
961 unsigned long lo
, hi
;
962 CORE_ADDR unwind_info
= 0;
967 cache
= FRAME_OBSTACK_ZALLOC (struct amd64_windows_frame_cache
);
970 /* Get current PC and SP. */
971 pc
= get_frame_pc (this_frame
);
972 get_frame_register (this_frame
, AMD64_RSP_REGNUM
, buf
);
973 cache
->sp
= extract_unsigned_integer (buf
, 8, byte_order
);
976 if (amd64_windows_find_unwind_info (gdbarch
, pc
, &unwind_info
,
982 if (unwind_info
== 0)
984 /* Assume a leaf function. */
985 cache
->prev_sp
= cache
->sp
+ 8;
986 cache
->prev_rip_addr
= cache
->sp
;
990 /* Decode unwind insns to compute saved addresses. */
991 amd64_windows_frame_decode_insns (this_frame
, cache
, unwind_info
);
996 /* Implement the "prev_register" method of struct frame_unwind
997 using the standard Windows x64 SEH info. */
999 static struct value
*
1000 amd64_windows_frame_prev_register (struct frame_info
*this_frame
,
1001 void **this_cache
, int regnum
)
1003 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1004 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1005 struct amd64_windows_frame_cache
*cache
=
1006 amd64_windows_frame_cache (this_frame
, this_cache
);
1011 fprintf_unfiltered (gdb_stdlog
,
1012 "amd64_windows_frame_prev_register %s for sp=%s\n",
1013 gdbarch_register_name (gdbarch
, regnum
),
1014 paddress (gdbarch
, cache
->prev_sp
));
1016 if (regnum
>= AMD64_XMM0_REGNUM
&& regnum
<= AMD64_XMM0_REGNUM
+ 15)
1017 prev
= cache
->prev_xmm_addr
[regnum
- AMD64_XMM0_REGNUM
];
1018 else if (regnum
== AMD64_RSP_REGNUM
)
1020 prev
= cache
->prev_rsp_addr
;
1022 return frame_unwind_got_constant (this_frame
, regnum
, cache
->prev_sp
);
1024 else if (regnum
>= AMD64_RAX_REGNUM
&& regnum
<= AMD64_R15_REGNUM
)
1025 prev
= cache
->prev_reg_addr
[regnum
- AMD64_RAX_REGNUM
];
1026 else if (regnum
== AMD64_RIP_REGNUM
)
1027 prev
= cache
->prev_rip_addr
;
1031 if (prev
&& frame_debug
)
1032 fprintf_unfiltered (gdb_stdlog
, " -> at %s\n", paddress (gdbarch
, prev
));
1036 /* Register was saved. */
1037 return frame_unwind_got_memory (this_frame
, regnum
, prev
);
1041 /* Register is either volatile or not modified. */
1042 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1046 /* Implement the "this_id" method of struct frame_unwind using
1047 the standard Windows x64 SEH info. */
1050 amd64_windows_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
1051 struct frame_id
*this_id
)
1053 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1054 struct amd64_windows_frame_cache
*cache
=
1055 amd64_windows_frame_cache (this_frame
, this_cache
);
1057 *this_id
= frame_id_build (cache
->prev_sp
,
1058 cache
->image_base
+ cache
->start_rva
);
1061 /* Windows x64 SEH unwinder. */
1063 static const struct frame_unwind amd64_windows_frame_unwind
=
1066 default_frame_unwind_stop_reason
,
1067 &amd64_windows_frame_this_id
,
1068 &amd64_windows_frame_prev_register
,
1070 default_frame_sniffer
1073 /* Implement the "skip_prologue" gdbarch method. */
1076 amd64_windows_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
1078 CORE_ADDR func_addr
;
1079 CORE_ADDR unwind_info
= 0;
1080 CORE_ADDR image_base
, start_rva
, end_rva
;
1081 struct external_pex64_unwind_info ex_ui
;
1083 /* Use prologue size from unwind info. */
1084 if (amd64_windows_find_unwind_info (gdbarch
, pc
, &unwind_info
,
1085 &image_base
, &start_rva
, &end_rva
) == 0)
1087 if (unwind_info
== 0)
1089 /* Leaf function. */
1092 else if (target_read_memory (image_base
+ unwind_info
,
1093 (gdb_byte
*) &ex_ui
, sizeof (ex_ui
)) == 0
1094 && PEX64_UWI_VERSION (ex_ui
.Version_Flags
) == 1)
1095 return max (pc
, image_base
+ start_rva
+ ex_ui
.SizeOfPrologue
);
1098 /* See if we can determine the end of the prologue via the symbol
1099 table. If so, then return either the PC, or the PC after
1100 the prologue, whichever is greater. */
1101 if (find_pc_partial_function (pc
, NULL
, &func_addr
, NULL
))
1103 CORE_ADDR post_prologue_pc
1104 = skip_prologue_using_sal (gdbarch
, func_addr
);
1106 if (post_prologue_pc
!= 0)
1107 return max (pc
, post_prologue_pc
);
1113 /* Check Win64 DLL jmp trampolines and find jump destination. */
1116 amd64_windows_skip_trampoline_code (struct frame_info
*frame
, CORE_ADDR pc
)
1118 CORE_ADDR destination
= 0;
1119 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
1120 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1122 /* Check for jmp *<offset>(%rip) (jump near, absolute indirect (/4)). */
1123 if (pc
&& read_memory_unsigned_integer (pc
, 2, byte_order
) == 0x25ff)
1125 /* Get opcode offset and see if we can find a reference in our data. */
1127 = read_memory_unsigned_integer (pc
+ 2, 4, byte_order
);
1129 /* Get address of function pointer at end of pc. */
1130 CORE_ADDR indirect_addr
= pc
+ offset
+ 6;
1132 struct minimal_symbol
*indsym
1134 ? lookup_minimal_symbol_by_pc (indirect_addr
).minsym
1136 const char *symname
= indsym
? SYMBOL_LINKAGE_NAME (indsym
) : NULL
;
1140 if (strncmp (symname
, "__imp_", 6) == 0
1141 || strncmp (symname
, "_imp_", 5) == 0)
1143 = read_memory_unsigned_integer (indirect_addr
, 8, byte_order
);
1150 /* Implement the "auto_wide_charset" gdbarch method. */
1153 amd64_windows_auto_wide_charset (void)
1159 amd64_windows_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1161 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1163 /* The dwarf2 unwinder (appended very early by i386_gdbarch_init) is
1164 preferred over the SEH one. The reasons are:
1165 - binaries without SEH but with dwarf2 debug info are correcly handled
1166 (although they aren't ABI compliant, gcc before 4.7 didn't emit SEH
1168 - dwarf3 DW_OP_call_frame_cfa is correctly handled (it can only be
1169 handled if the dwarf2 unwinder is used).
1171 The call to amd64_init_abi appends default unwinders, that aren't
1172 compatible with the SEH one.
1174 frame_unwind_append_unwinder (gdbarch
, &amd64_windows_frame_unwind
);
1176 amd64_init_abi (info
, gdbarch
);
1178 windows_init_abi (info
, gdbarch
);
1180 /* On Windows, "long"s are only 32bit. */
1181 set_gdbarch_long_bit (gdbarch
, 32);
1183 /* Function calls. */
1184 set_gdbarch_push_dummy_call (gdbarch
, amd64_windows_push_dummy_call
);
1185 set_gdbarch_return_value (gdbarch
, amd64_windows_return_value
);
1186 set_gdbarch_skip_main_prologue (gdbarch
, amd64_skip_main_prologue
);
1187 set_gdbarch_skip_trampoline_code (gdbarch
,
1188 amd64_windows_skip_trampoline_code
);
1190 set_gdbarch_skip_prologue (gdbarch
, amd64_windows_skip_prologue
);
1192 set_gdbarch_auto_wide_charset (gdbarch
, amd64_windows_auto_wide_charset
);
1195 /* -Wmissing-prototypes */
1196 extern initialize_file_ftype _initialize_amd64_windows_tdep
;
1199 _initialize_amd64_windows_tdep (void)
1201 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
, GDB_OSABI_CYGWIN
,
1202 amd64_windows_init_abi
);