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