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