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