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