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