Fix x86-64 GNU/Linux crashes
[deliverable/binutils-gdb.git] / gdb / amd64-windows-tdep.c
1 /* Copyright (C) 2009-2017 Free Software Foundation, Inc.
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 "gdbtypes.h"
22 #include "gdbcore.h"
23 #include "regcache.h"
24 #include "windows-tdep.h"
25 #include "frame.h"
26 #include "objfiles.h"
27 #include "frame-unwind.h"
28 #include "coff/internal.h"
29 #include "coff/i386.h"
30 #include "coff/pe.h"
31 #include "libcoff.h"
32 #include "value.h"
33 #include <algorithm>
34
35 /* The registers used to pass integer arguments during a function call. */
36 static int amd64_windows_dummy_call_integer_regs[] =
37 {
38 AMD64_RCX_REGNUM, /* %rcx */
39 AMD64_RDX_REGNUM, /* %rdx */
40 AMD64_R8_REGNUM, /* %r8 */
41 AMD64_R9_REGNUM /* %r9 */
42 };
43
44 /* Return nonzero if an argument of type TYPE should be passed
45 via one of the integer registers. */
46
47 static int
48 amd64_windows_passed_by_integer_register (struct type *type)
49 {
50 switch (TYPE_CODE (type))
51 {
52 case TYPE_CODE_INT:
53 case TYPE_CODE_ENUM:
54 case TYPE_CODE_BOOL:
55 case TYPE_CODE_RANGE:
56 case TYPE_CODE_CHAR:
57 case TYPE_CODE_PTR:
58 case TYPE_CODE_REF:
59 case TYPE_CODE_RVALUE_REF:
60 case TYPE_CODE_STRUCT:
61 case TYPE_CODE_UNION:
62 return (TYPE_LENGTH (type) == 1
63 || TYPE_LENGTH (type) == 2
64 || TYPE_LENGTH (type) == 4
65 || TYPE_LENGTH (type) == 8);
66
67 default:
68 return 0;
69 }
70 }
71
72 /* Return nonzero if an argument of type TYPE should be passed
73 via one of the XMM registers. */
74
75 static int
76 amd64_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
86 static int
87 amd64_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
105 static CORE_ADDR
106 amd64_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
136 static void
137 amd64_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, std::min (TYPE_LENGTH (type), (unsigned int) 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
156 static CORE_ADDR
157 amd64_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 = XALLOCAVEC (struct value *, nargs);
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 = XALLOCAVEC (struct value *, nargs);
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
242 static CORE_ADDR
243 amd64_windows_push_dummy_call
244 (struct gdbarch *gdbarch, struct value *function,
245 struct regcache *regcache, CORE_ADDR bp_addr,
246 int nargs, struct value **args,
247 CORE_ADDR sp, int struct_return, CORE_ADDR struct_addr)
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
286 /* Implement the "return_value" gdbarch method for amd64-windows. */
287
288 static enum return_value_convention
289 amd64_windows_return_value (struct gdbarch *gdbarch, struct value *function,
290 struct type *type, struct regcache *regcache,
291 gdb_byte *readbuf, const gdb_byte *writebuf)
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
338 /* Check that the code pointed to by PC corresponds to a call to
339 __main, skip it if so. Return PC otherwise. */
340
341 static CORE_ADDR
342 amd64_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 {
354 struct bound_minimal_symbol s;
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);
359 if (s.minsym != NULL
360 && MSYMBOL_LINKAGE_NAME (s.minsym) != NULL
361 && strcmp (MSYMBOL_LINKAGE_NAME (s.minsym), "__main") == 0)
362 pc += 5;
363 }
364 }
365
366 return pc;
367 }
368
369 struct 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. */
401 static 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
424 static int
425 pc_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
436 static int
437 amd64_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 pc += rex ? 2 : 1;
494 }
495 else
496 break;
497
498 /* Allow the user to break this loop. This shouldn't happen as the
499 number of consecutive pop should be small. */
500 QUIT;
501 }
502
503 /* Then decode the marker. */
504
505 /* Read opcode. */
506 if (target_read_memory (pc, &op, 1) != 0)
507 return -1;
508
509 switch (op)
510 {
511 case 0xc3:
512 /* Ret. */
513 cache->prev_rip_addr = cur_sp;
514 cache->prev_sp = cur_sp + 8;
515 return 1;
516
517 case 0xeb:
518 {
519 /* jmp rel8 */
520 gdb_byte rel8;
521 CORE_ADDR npc;
522
523 if (target_read_memory (pc + 1, &rel8, 1) != 0)
524 return -1;
525 npc = pc + 2 + (signed char) rel8;
526
527 /* If the jump is within the function, then this is not a marker,
528 otherwise this is a tail-call. */
529 return !pc_in_range (npc, cache);
530 }
531
532 case 0xec:
533 {
534 /* jmp rel32 */
535 gdb_byte rel32[4];
536 CORE_ADDR npc;
537
538 if (target_read_memory (pc + 1, rel32, 4) != 0)
539 return -1;
540 npc = pc + 5 + extract_signed_integer (rel32, 4, byte_order);
541
542 /* If the jump is within the function, then this is not a marker,
543 otherwise this is a tail-call. */
544 return !pc_in_range (npc, cache);
545 }
546
547 case 0xc2:
548 {
549 /* ret n */
550 gdb_byte imm16[2];
551
552 if (target_read_memory (pc + 1, imm16, 2) != 0)
553 return -1;
554 cache->prev_rip_addr = cur_sp;
555 cache->prev_sp = cur_sp
556 + extract_unsigned_integer (imm16, 4, byte_order);
557 return 1;
558 }
559
560 case 0xf3:
561 {
562 /* rep; ret */
563 gdb_byte op1;
564
565 if (target_read_memory (pc + 2, &op1, 1) != 0)
566 return -1;
567 if (op1 != 0xc3)
568 return 0;
569
570 cache->prev_rip_addr = cur_sp;
571 cache->prev_sp = cur_sp + 8;
572 return 1;
573 }
574
575 case 0x40:
576 case 0x41:
577 case 0x42:
578 case 0x43:
579 case 0x44:
580 case 0x45:
581 case 0x46:
582 case 0x47:
583 case 0x48:
584 case 0x49:
585 case 0x4a:
586 case 0x4b:
587 case 0x4c:
588 case 0x4d:
589 case 0x4e:
590 case 0x4f:
591 /* Got a REX prefix, read next byte. */
592 rex = op;
593 if (target_read_memory (pc + 1, &op, 1) != 0)
594 return -1;
595
596 if (op == 0xff)
597 {
598 /* rex jmp reg */
599 gdb_byte op1;
600
601 if (target_read_memory (pc + 2, &op1, 1) != 0)
602 return -1;
603 return (op1 & 0xf8) == 0xe0;
604 }
605 else
606 return 0;
607
608 default:
609 /* Not REX, so unknown. */
610 return 0;
611 }
612 }
613
614 /* Decode and execute unwind insns at UNWIND_INFO. */
615
616 static void
617 amd64_windows_frame_decode_insns (struct frame_info *this_frame,
618 struct amd64_windows_frame_cache *cache,
619 CORE_ADDR unwind_info)
620 {
621 CORE_ADDR save_addr = 0;
622 CORE_ADDR cur_sp = cache->sp;
623 struct gdbarch *gdbarch = get_frame_arch (this_frame);
624 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
625 int first = 1;
626
627 /* There are at least 3 possibilities to share an unwind info entry:
628 1. Two different runtime_function entries (in .pdata) can point to the
629 same unwind info entry. There is no such indication while unwinding,
630 so we don't really care about that case. We suppose this scheme is
631 used to save memory when the unwind entries are exactly the same.
632 2. Chained unwind_info entries, with no unwind codes (no prologue).
633 There is a major difference with the previous case: the pc range for
634 the function is different (in case 1, the pc range comes from the
635 runtime_function entry; in case 2, the pc range for the chained entry
636 comes from the first unwind entry). Case 1 cannot be used instead as
637 the pc is not in the prologue. This case is officially documented.
638 (There might be unwind code in the first unwind entry to handle
639 additional unwinding). GCC (at least until gcc 5.0) doesn't chain
640 entries.
641 3. Undocumented unwind info redirection. Hard to know the exact purpose,
642 so it is considered as a memory optimization of case 2.
643 */
644
645 if (unwind_info & 1)
646 {
647 /* Unofficially documented unwind info redirection, when UNWIND_INFO
648 address is odd (http://www.codemachine.com/article_x64deepdive.html).
649 */
650 struct external_pex64_runtime_function d;
651
652 if (target_read_memory (cache->image_base + (unwind_info & ~1),
653 (gdb_byte *) &d, sizeof (d)) != 0)
654 return;
655
656 cache->start_rva
657 = extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order);
658 cache->end_rva
659 = extract_unsigned_integer (d.rva_EndAddress, 4, byte_order);
660 unwind_info
661 = extract_unsigned_integer (d.rva_UnwindData, 4, byte_order);
662 }
663
664 while (1)
665 {
666 struct external_pex64_unwind_info ex_ui;
667 /* There are at most 256 16-bit unwind insns. */
668 gdb_byte insns[2 * 256];
669 gdb_byte *p;
670 gdb_byte *end_insns;
671 unsigned char codes_count;
672 unsigned char frame_reg;
673 CORE_ADDR start;
674
675 /* Read and decode header. */
676 if (target_read_memory (cache->image_base + unwind_info,
677 (gdb_byte *) &ex_ui, sizeof (ex_ui)) != 0)
678 return;
679
680 if (frame_debug)
681 fprintf_unfiltered
682 (gdb_stdlog,
683 "amd64_windows_frame_decodes_insn: "
684 "%s: ver: %02x, plgsz: %02x, cnt: %02x, frame: %02x\n",
685 paddress (gdbarch, unwind_info),
686 ex_ui.Version_Flags, ex_ui.SizeOfPrologue,
687 ex_ui.CountOfCodes, ex_ui.FrameRegisterOffset);
688
689 /* Check version. */
690 if (PEX64_UWI_VERSION (ex_ui.Version_Flags) != 1
691 && PEX64_UWI_VERSION (ex_ui.Version_Flags) != 2)
692 return;
693
694 start = cache->image_base + cache->start_rva;
695 if (first
696 && !(cache->pc >= start && cache->pc < start + ex_ui.SizeOfPrologue))
697 {
698 /* We want to detect if the PC points to an epilogue. This needs
699 to be checked only once, and an epilogue can be anywhere but in
700 the prologue. If so, the epilogue detection+decoding function is
701 sufficient. Otherwise, the unwinder will consider that the PC
702 is in the body of the function and will need to decode unwind
703 info. */
704 if (amd64_windows_frame_decode_epilogue (this_frame, cache) == 1)
705 return;
706
707 /* Not in an epilog. Clear possible side effects. */
708 memset (cache->prev_reg_addr, 0, sizeof (cache->prev_reg_addr));
709 }
710
711 codes_count = ex_ui.CountOfCodes;
712 frame_reg = PEX64_UWI_FRAMEREG (ex_ui.FrameRegisterOffset);
713
714 if (frame_reg != 0)
715 {
716 /* According to msdn:
717 If an FP reg is used, then any unwind code taking an offset must
718 only be used after the FP reg is established in the prolog. */
719 gdb_byte buf[8];
720 int frreg = amd64_windows_w2gdb_regnum[frame_reg];
721
722 get_frame_register (this_frame, frreg, buf);
723 save_addr = extract_unsigned_integer (buf, 8, byte_order);
724
725 if (frame_debug)
726 fprintf_unfiltered (gdb_stdlog, " frame_reg=%s, val=%s\n",
727 gdbarch_register_name (gdbarch, frreg),
728 paddress (gdbarch, save_addr));
729 }
730
731 /* Read opcodes. */
732 if (codes_count != 0
733 && target_read_memory (cache->image_base + unwind_info
734 + sizeof (ex_ui),
735 insns, codes_count * 2) != 0)
736 return;
737
738 end_insns = &insns[codes_count * 2];
739 p = insns;
740
741 /* Skip opcodes 6 of version 2. This opcode is not documented. */
742 if (PEX64_UWI_VERSION (ex_ui.Version_Flags) == 2)
743 {
744 for (; p < end_insns; p += 2)
745 if (PEX64_UNWCODE_CODE (p[1]) != 6)
746 break;
747 }
748
749 for (; p < end_insns; p += 2)
750 {
751 int reg;
752
753 /* Virtually execute the operation if the pc is after the
754 corresponding instruction (that does matter in case of break
755 within the prologue). Note that for chained info (!first), the
756 prologue has been fully executed. */
757 if (cache->pc >= start + p[0] || cache->pc < start)
758 {
759 if (frame_debug)
760 fprintf_unfiltered
761 (gdb_stdlog, " op #%u: off=0x%02x, insn=0x%02x\n",
762 (unsigned) (p - insns), p[0], p[1]);
763
764 /* If there is no frame registers defined, the current value of
765 rsp is used instead. */
766 if (frame_reg == 0)
767 save_addr = cur_sp;
768
769 reg = -1;
770
771 switch (PEX64_UNWCODE_CODE (p[1]))
772 {
773 case UWOP_PUSH_NONVOL:
774 /* Push pre-decrements RSP. */
775 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])];
776 cache->prev_reg_addr[reg] = cur_sp;
777 cur_sp += 8;
778 break;
779 case UWOP_ALLOC_LARGE:
780 if (PEX64_UNWCODE_INFO (p[1]) == 0)
781 cur_sp +=
782 8 * extract_unsigned_integer (p + 2, 2, byte_order);
783 else if (PEX64_UNWCODE_INFO (p[1]) == 1)
784 cur_sp += extract_unsigned_integer (p + 2, 4, byte_order);
785 else
786 return;
787 break;
788 case UWOP_ALLOC_SMALL:
789 cur_sp += 8 + 8 * PEX64_UNWCODE_INFO (p[1]);
790 break;
791 case UWOP_SET_FPREG:
792 cur_sp = save_addr
793 - PEX64_UWI_FRAMEOFF (ex_ui.FrameRegisterOffset) * 16;
794 break;
795 case UWOP_SAVE_NONVOL:
796 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])];
797 cache->prev_reg_addr[reg] = save_addr
798 + 8 * extract_unsigned_integer (p + 2, 2, byte_order);
799 break;
800 case UWOP_SAVE_NONVOL_FAR:
801 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])];
802 cache->prev_reg_addr[reg] = save_addr
803 + 8 * extract_unsigned_integer (p + 2, 4, byte_order);
804 break;
805 case UWOP_SAVE_XMM128:
806 cache->prev_xmm_addr[PEX64_UNWCODE_INFO (p[1])] =
807 save_addr
808 - 16 * extract_unsigned_integer (p + 2, 2, byte_order);
809 break;
810 case UWOP_SAVE_XMM128_FAR:
811 cache->prev_xmm_addr[PEX64_UNWCODE_INFO (p[1])] =
812 save_addr
813 - 16 * extract_unsigned_integer (p + 2, 4, byte_order);
814 break;
815 case UWOP_PUSH_MACHFRAME:
816 if (PEX64_UNWCODE_INFO (p[1]) == 0)
817 {
818 cache->prev_rip_addr = cur_sp + 0;
819 cache->prev_rsp_addr = cur_sp + 24;
820 cur_sp += 40;
821 }
822 else if (PEX64_UNWCODE_INFO (p[1]) == 1)
823 {
824 cache->prev_rip_addr = cur_sp + 8;
825 cache->prev_rsp_addr = cur_sp + 32;
826 cur_sp += 48;
827 }
828 else
829 return;
830 break;
831 default:
832 return;
833 }
834
835 /* Display address where the register was saved. */
836 if (frame_debug && reg >= 0)
837 fprintf_unfiltered
838 (gdb_stdlog, " [reg %s at %s]\n",
839 gdbarch_register_name (gdbarch, reg),
840 paddress (gdbarch, cache->prev_reg_addr[reg]));
841 }
842
843 /* Adjust with the length of the opcode. */
844 switch (PEX64_UNWCODE_CODE (p[1]))
845 {
846 case UWOP_PUSH_NONVOL:
847 case UWOP_ALLOC_SMALL:
848 case UWOP_SET_FPREG:
849 case UWOP_PUSH_MACHFRAME:
850 break;
851 case UWOP_ALLOC_LARGE:
852 if (PEX64_UNWCODE_INFO (p[1]) == 0)
853 p += 2;
854 else if (PEX64_UNWCODE_INFO (p[1]) == 1)
855 p += 4;
856 else
857 return;
858 break;
859 case UWOP_SAVE_NONVOL:
860 case UWOP_SAVE_XMM128:
861 p += 2;
862 break;
863 case UWOP_SAVE_NONVOL_FAR:
864 case UWOP_SAVE_XMM128_FAR:
865 p += 4;
866 break;
867 default:
868 return;
869 }
870 }
871 if (PEX64_UWI_FLAGS (ex_ui.Version_Flags) != UNW_FLAG_CHAININFO)
872 {
873 /* End of unwind info. */
874 break;
875 }
876 else
877 {
878 /* Read the chained unwind info. */
879 struct external_pex64_runtime_function d;
880 CORE_ADDR chain_vma;
881
882 /* Not anymore the first entry. */
883 first = 0;
884
885 /* Stay aligned on word boundary. */
886 chain_vma = cache->image_base + unwind_info
887 + sizeof (ex_ui) + ((codes_count + 1) & ~1) * 2;
888
889 if (target_read_memory (chain_vma, (gdb_byte *) &d, sizeof (d)) != 0)
890 return;
891
892 /* Decode begin/end. This may be different from .pdata index, as
893 an unwind info may be shared by several functions (in particular
894 if many functions have the same prolog and handler. */
895 cache->start_rva =
896 extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order);
897 cache->end_rva =
898 extract_unsigned_integer (d.rva_EndAddress, 4, byte_order);
899 unwind_info =
900 extract_unsigned_integer (d.rva_UnwindData, 4, byte_order);
901
902 if (frame_debug)
903 fprintf_unfiltered
904 (gdb_stdlog,
905 "amd64_windows_frame_decodes_insn (next in chain):"
906 " unwind_data=%s, start_rva=%s, end_rva=%s\n",
907 paddress (gdbarch, unwind_info),
908 paddress (gdbarch, cache->start_rva),
909 paddress (gdbarch, cache->end_rva));
910 }
911
912 /* Allow the user to break this loop. */
913 QUIT;
914 }
915 /* PC is saved by the call. */
916 if (cache->prev_rip_addr == 0)
917 cache->prev_rip_addr = cur_sp;
918 cache->prev_sp = cur_sp + 8;
919
920 if (frame_debug)
921 fprintf_unfiltered (gdb_stdlog, " prev_sp: %s, prev_pc @%s\n",
922 paddress (gdbarch, cache->prev_sp),
923 paddress (gdbarch, cache->prev_rip_addr));
924 }
925
926 /* Find SEH unwind info for PC, returning 0 on success.
927
928 UNWIND_INFO is set to the rva of unwind info address, IMAGE_BASE
929 to the base address of the corresponding image, and START_RVA
930 to the rva of the function containing PC. */
931
932 static int
933 amd64_windows_find_unwind_info (struct gdbarch *gdbarch, CORE_ADDR pc,
934 CORE_ADDR *unwind_info,
935 CORE_ADDR *image_base,
936 CORE_ADDR *start_rva,
937 CORE_ADDR *end_rva)
938 {
939 struct obj_section *sec;
940 pe_data_type *pe;
941 IMAGE_DATA_DIRECTORY *dir;
942 struct objfile *objfile;
943 unsigned long lo, hi;
944 CORE_ADDR base;
945 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
946
947 /* Get the corresponding exception directory. */
948 sec = find_pc_section (pc);
949 if (sec == NULL)
950 return -1;
951 objfile = sec->objfile;
952 pe = pe_data (sec->objfile->obfd);
953 dir = &pe->pe_opthdr.DataDirectory[PE_EXCEPTION_TABLE];
954
955 base = pe->pe_opthdr.ImageBase
956 + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
957 *image_base = base;
958
959 /* Find the entry.
960
961 Note: This does not handle dynamically added entries (for JIT
962 engines). For this, we would need to ask the kernel directly,
963 which means getting some info from the native layer. For the
964 rest of the code, however, it's probably faster to search
965 the entry ourselves. */
966 lo = 0;
967 hi = dir->Size / sizeof (struct external_pex64_runtime_function);
968 *unwind_info = 0;
969 while (lo <= hi)
970 {
971 unsigned long mid = lo + (hi - lo) / 2;
972 struct external_pex64_runtime_function d;
973 CORE_ADDR sa, ea;
974
975 if (target_read_memory (base + dir->VirtualAddress + mid * sizeof (d),
976 (gdb_byte *) &d, sizeof (d)) != 0)
977 return -1;
978
979 sa = extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order);
980 ea = extract_unsigned_integer (d.rva_EndAddress, 4, byte_order);
981 if (pc < base + sa)
982 hi = mid - 1;
983 else if (pc >= base + ea)
984 lo = mid + 1;
985 else if (pc >= base + sa && pc < base + ea)
986 {
987 /* Got it. */
988 *start_rva = sa;
989 *end_rva = ea;
990 *unwind_info =
991 extract_unsigned_integer (d.rva_UnwindData, 4, byte_order);
992 break;
993 }
994 else
995 break;
996 }
997
998 if (frame_debug)
999 fprintf_unfiltered
1000 (gdb_stdlog,
1001 "amd64_windows_find_unwind_data: image_base=%s, unwind_data=%s\n",
1002 paddress (gdbarch, base), paddress (gdbarch, *unwind_info));
1003
1004 return 0;
1005 }
1006
1007 /* Fill THIS_CACHE using the native amd64-windows unwinding data
1008 for THIS_FRAME. */
1009
1010 static struct amd64_windows_frame_cache *
1011 amd64_windows_frame_cache (struct frame_info *this_frame, void **this_cache)
1012 {
1013 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1014 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1015 struct amd64_windows_frame_cache *cache;
1016 gdb_byte buf[8];
1017 CORE_ADDR pc;
1018 CORE_ADDR unwind_info = 0;
1019
1020 if (*this_cache)
1021 return (struct amd64_windows_frame_cache *) *this_cache;
1022
1023 cache = FRAME_OBSTACK_ZALLOC (struct amd64_windows_frame_cache);
1024 *this_cache = cache;
1025
1026 /* Get current PC and SP. */
1027 pc = get_frame_pc (this_frame);
1028 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
1029 cache->sp = extract_unsigned_integer (buf, 8, byte_order);
1030 cache->pc = pc;
1031
1032 if (amd64_windows_find_unwind_info (gdbarch, pc, &unwind_info,
1033 &cache->image_base,
1034 &cache->start_rva,
1035 &cache->end_rva))
1036 return cache;
1037
1038 if (unwind_info == 0)
1039 {
1040 /* Assume a leaf function. */
1041 cache->prev_sp = cache->sp + 8;
1042 cache->prev_rip_addr = cache->sp;
1043 }
1044 else
1045 {
1046 /* Decode unwind insns to compute saved addresses. */
1047 amd64_windows_frame_decode_insns (this_frame, cache, unwind_info);
1048 }
1049 return cache;
1050 }
1051
1052 /* Implement the "prev_register" method of struct frame_unwind
1053 using the standard Windows x64 SEH info. */
1054
1055 static struct value *
1056 amd64_windows_frame_prev_register (struct frame_info *this_frame,
1057 void **this_cache, int regnum)
1058 {
1059 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1060 struct amd64_windows_frame_cache *cache =
1061 amd64_windows_frame_cache (this_frame, this_cache);
1062 CORE_ADDR prev;
1063
1064 if (frame_debug)
1065 fprintf_unfiltered (gdb_stdlog,
1066 "amd64_windows_frame_prev_register %s for sp=%s\n",
1067 gdbarch_register_name (gdbarch, regnum),
1068 paddress (gdbarch, cache->prev_sp));
1069
1070 if (regnum >= AMD64_XMM0_REGNUM && regnum <= AMD64_XMM0_REGNUM + 15)
1071 prev = cache->prev_xmm_addr[regnum - AMD64_XMM0_REGNUM];
1072 else if (regnum == AMD64_RSP_REGNUM)
1073 {
1074 prev = cache->prev_rsp_addr;
1075 if (prev == 0)
1076 return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
1077 }
1078 else if (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_R15_REGNUM)
1079 prev = cache->prev_reg_addr[regnum - AMD64_RAX_REGNUM];
1080 else if (regnum == AMD64_RIP_REGNUM)
1081 prev = cache->prev_rip_addr;
1082 else
1083 prev = 0;
1084
1085 if (prev && frame_debug)
1086 fprintf_unfiltered (gdb_stdlog, " -> at %s\n", paddress (gdbarch, prev));
1087
1088 if (prev)
1089 {
1090 /* Register was saved. */
1091 return frame_unwind_got_memory (this_frame, regnum, prev);
1092 }
1093 else
1094 {
1095 /* Register is either volatile or not modified. */
1096 return frame_unwind_got_register (this_frame, regnum, regnum);
1097 }
1098 }
1099
1100 /* Implement the "this_id" method of struct frame_unwind using
1101 the standard Windows x64 SEH info. */
1102
1103 static void
1104 amd64_windows_frame_this_id (struct frame_info *this_frame, void **this_cache,
1105 struct frame_id *this_id)
1106 {
1107 struct amd64_windows_frame_cache *cache =
1108 amd64_windows_frame_cache (this_frame, this_cache);
1109
1110 *this_id = frame_id_build (cache->prev_sp,
1111 cache->image_base + cache->start_rva);
1112 }
1113
1114 /* Windows x64 SEH unwinder. */
1115
1116 static const struct frame_unwind amd64_windows_frame_unwind =
1117 {
1118 NORMAL_FRAME,
1119 default_frame_unwind_stop_reason,
1120 &amd64_windows_frame_this_id,
1121 &amd64_windows_frame_prev_register,
1122 NULL,
1123 default_frame_sniffer
1124 };
1125
1126 /* Implement the "skip_prologue" gdbarch method. */
1127
1128 static CORE_ADDR
1129 amd64_windows_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1130 {
1131 CORE_ADDR func_addr;
1132 CORE_ADDR unwind_info = 0;
1133 CORE_ADDR image_base, start_rva, end_rva;
1134 struct external_pex64_unwind_info ex_ui;
1135
1136 /* Use prologue size from unwind info. */
1137 if (amd64_windows_find_unwind_info (gdbarch, pc, &unwind_info,
1138 &image_base, &start_rva, &end_rva) == 0)
1139 {
1140 if (unwind_info == 0)
1141 {
1142 /* Leaf function. */
1143 return pc;
1144 }
1145 else if (target_read_memory (image_base + unwind_info,
1146 (gdb_byte *) &ex_ui, sizeof (ex_ui)) == 0
1147 && PEX64_UWI_VERSION (ex_ui.Version_Flags) == 1)
1148 return std::max (pc, image_base + start_rva + ex_ui.SizeOfPrologue);
1149 }
1150
1151 /* See if we can determine the end of the prologue via the symbol
1152 table. If so, then return either the PC, or the PC after
1153 the prologue, whichever is greater. */
1154 if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
1155 {
1156 CORE_ADDR post_prologue_pc
1157 = skip_prologue_using_sal (gdbarch, func_addr);
1158
1159 if (post_prologue_pc != 0)
1160 return std::max (pc, post_prologue_pc);
1161 }
1162
1163 return pc;
1164 }
1165
1166 /* Check Win64 DLL jmp trampolines and find jump destination. */
1167
1168 static CORE_ADDR
1169 amd64_windows_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
1170 {
1171 CORE_ADDR destination = 0;
1172 struct gdbarch *gdbarch = get_frame_arch (frame);
1173 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1174
1175 /* Check for jmp *<offset>(%rip) (jump near, absolute indirect (/4)). */
1176 if (pc && read_memory_unsigned_integer (pc, 2, byte_order) == 0x25ff)
1177 {
1178 /* Get opcode offset and see if we can find a reference in our data. */
1179 ULONGEST offset
1180 = read_memory_unsigned_integer (pc + 2, 4, byte_order);
1181
1182 /* Get address of function pointer at end of pc. */
1183 CORE_ADDR indirect_addr = pc + offset + 6;
1184
1185 struct minimal_symbol *indsym
1186 = (indirect_addr
1187 ? lookup_minimal_symbol_by_pc (indirect_addr).minsym
1188 : NULL);
1189 const char *symname = indsym ? MSYMBOL_LINKAGE_NAME (indsym) : NULL;
1190
1191 if (symname)
1192 {
1193 if (startswith (symname, "__imp_")
1194 || startswith (symname, "_imp_"))
1195 destination
1196 = read_memory_unsigned_integer (indirect_addr, 8, byte_order);
1197 }
1198 }
1199
1200 return destination;
1201 }
1202
1203 /* Implement the "auto_wide_charset" gdbarch method. */
1204
1205 static const char *
1206 amd64_windows_auto_wide_charset (void)
1207 {
1208 return "UTF-16";
1209 }
1210
1211 static void
1212 amd64_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1213 {
1214 /* The dwarf2 unwinder (appended very early by i386_gdbarch_init) is
1215 preferred over the SEH one. The reasons are:
1216 - binaries without SEH but with dwarf2 debug info are correcly handled
1217 (although they aren't ABI compliant, gcc before 4.7 didn't emit SEH
1218 info).
1219 - dwarf3 DW_OP_call_frame_cfa is correctly handled (it can only be
1220 handled if the dwarf2 unwinder is used).
1221
1222 The call to amd64_init_abi appends default unwinders, that aren't
1223 compatible with the SEH one.
1224 */
1225 frame_unwind_append_unwinder (gdbarch, &amd64_windows_frame_unwind);
1226
1227 amd64_init_abi (info, gdbarch, tdesc_amd64);
1228
1229 windows_init_abi (info, gdbarch);
1230
1231 /* On Windows, "long"s are only 32bit. */
1232 set_gdbarch_long_bit (gdbarch, 32);
1233
1234 /* Function calls. */
1235 set_gdbarch_push_dummy_call (gdbarch, amd64_windows_push_dummy_call);
1236 set_gdbarch_return_value (gdbarch, amd64_windows_return_value);
1237 set_gdbarch_skip_main_prologue (gdbarch, amd64_skip_main_prologue);
1238 set_gdbarch_skip_trampoline_code (gdbarch,
1239 amd64_windows_skip_trampoline_code);
1240
1241 set_gdbarch_skip_prologue (gdbarch, amd64_windows_skip_prologue);
1242
1243 set_gdbarch_auto_wide_charset (gdbarch, amd64_windows_auto_wide_charset);
1244 }
1245
1246 /* -Wmissing-prototypes */
1247 extern initialize_file_ftype _initialize_amd64_windows_tdep;
1248
1249 void
1250 _initialize_amd64_windows_tdep (void)
1251 {
1252 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_CYGWIN,
1253 amd64_windows_init_abi);
1254 }
This page took 0.054725 seconds and 5 git commands to generate.