makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / dbghelp / cpu_x86_64.c
blobc2789d0d71b83bc36a0a69e6442a36b95c4e701c
1 /*
2 * File cpu_x86_64.c
4 * Copyright (C) 1999, 2005 Alexandre Julliard
5 * Copyright (C) 2009, 2011 Eric Pouech.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <assert.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "dbghelp_private.h"
29 #include "winternl.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34 /* x86-64 unwind information, for PE modules, as described on MSDN */
36 typedef enum _UNWIND_OP_CODES
38 UWOP_PUSH_NONVOL = 0,
39 UWOP_ALLOC_LARGE,
40 UWOP_ALLOC_SMALL,
41 UWOP_SET_FPREG,
42 UWOP_SAVE_NONVOL,
43 UWOP_SAVE_NONVOL_FAR,
44 UWOP_SAVE_XMM128,
45 UWOP_SAVE_XMM128_FAR,
46 UWOP_PUSH_MACHFRAME
47 } UNWIND_CODE_OPS;
49 typedef union _UNWIND_CODE
51 struct
53 BYTE CodeOffset;
54 BYTE UnwindOp : 4;
55 BYTE OpInfo : 4;
56 } u;
57 USHORT FrameOffset;
58 } UNWIND_CODE, *PUNWIND_CODE;
60 typedef struct _UNWIND_INFO
62 BYTE Version : 3;
63 BYTE Flags : 5;
64 BYTE SizeOfProlog;
65 BYTE CountOfCodes;
66 BYTE FrameRegister : 4;
67 BYTE FrameOffset : 4;
68 UNWIND_CODE UnwindCode[1]; /* actually CountOfCodes (aligned) */
70 * union
71 * {
72 * OPTIONAL ULONG ExceptionHandler;
73 * OPTIONAL ULONG FunctionEntry;
74 * };
75 * OPTIONAL ULONG ExceptionData[];
77 } UNWIND_INFO, *PUNWIND_INFO;
79 static BOOL x86_64_get_addr(HANDLE hThread, const CONTEXT* ctx,
80 enum cpu_addr ca, ADDRESS64* addr)
82 addr->Mode = AddrModeFlat;
83 switch (ca)
85 #ifdef __x86_64__
86 case cpu_addr_pc: addr->Segment = ctx->SegCs; addr->Offset = ctx->Rip; return TRUE;
87 case cpu_addr_stack: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rsp; return TRUE;
88 case cpu_addr_frame: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rbp; return TRUE;
89 #endif
90 default: addr->Mode = -1;
91 return FALSE;
95 #ifdef __x86_64__
97 enum st_mode {stm_start, stm_64bit, stm_done};
99 /* indexes in Reserved array */
100 #define __CurrentMode 0
101 #define __CurrentCount 1
102 /* #define __ 2 (unused) */
104 #define curr_mode (frame->Reserved[__CurrentMode])
105 #define curr_count (frame->Reserved[__CurrentCount])
106 /* #define ??? (frame->Reserved[__]) (unused) */
108 union handler_data
110 RUNTIME_FUNCTION chain;
111 ULONG handler;
114 static void dump_unwind_info(struct cpu_stack_walk* csw, ULONG64 base, RUNTIME_FUNCTION *function)
116 static const char * const reg_names[16] =
117 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
118 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
120 union handler_data handler_data;
121 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
122 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
123 unsigned int i, count;
124 RUNTIME_FUNCTION snext;
125 ULONG64 addr;
127 TRACE("**** func %x-%x\n", function->BeginAddress, function->EndAddress);
128 for (;;)
130 if (function->UnwindData & 1)
132 if (!sw_read_mem(csw, base + function->UnwindData, &snext, sizeof(snext)))
134 TRACE("Couldn't unwind RUNTIME_INFO at %lx\n", base + function->UnwindData);
135 return;
137 TRACE("unwind info for function %p-%p chained to function %p-%p\n",
138 (char*)base + function->BeginAddress, (char*)base + function->EndAddress,
139 (char*)base + snext.BeginAddress, (char*)base + snext.EndAddress);
140 function = &snext;
141 continue;
143 addr = base + function->UnwindData;
144 if (!sw_read_mem(csw, addr, info, FIELD_OFFSET(UNWIND_INFO, UnwindCode)) ||
145 !sw_read_mem(csw, addr + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
146 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
148 FIXME("couldn't read memory for UNWIND_INFO at %lx\n", addr);
149 return;
151 TRACE("unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
152 (char*)addr, info->Flags, info->SizeOfProlog,
153 (char*)base + function->BeginAddress, (char*)base + function->EndAddress);
155 if (info->FrameRegister)
156 TRACE(" frame register %s offset 0x%x(%%rsp)\n",
157 reg_names[info->FrameRegister], info->FrameOffset * 16);
159 for (i = 0; i < info->CountOfCodes; i++)
161 TRACE(" 0x%x: ", info->UnwindCode[i].u.CodeOffset);
162 switch (info->UnwindCode[i].u.UnwindOp)
164 case UWOP_PUSH_NONVOL:
165 TRACE("pushq %%%s\n", reg_names[info->UnwindCode[i].u.OpInfo]);
166 break;
167 case UWOP_ALLOC_LARGE:
168 if (info->UnwindCode[i].u.OpInfo)
170 count = *(DWORD*)&info->UnwindCode[i+1];
171 i += 2;
173 else
175 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
176 i++;
178 TRACE("subq $0x%x,%%rsp\n", count);
179 break;
180 case UWOP_ALLOC_SMALL:
181 count = (info->UnwindCode[i].u.OpInfo + 1) * 8;
182 TRACE("subq $0x%x,%%rsp\n", count);
183 break;
184 case UWOP_SET_FPREG:
185 TRACE("leaq 0x%x(%%rsp),%s\n",
186 info->FrameOffset * 16, reg_names[info->FrameRegister]);
187 break;
188 case UWOP_SAVE_NONVOL:
189 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
190 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].u.OpInfo], count);
191 i++;
192 break;
193 case UWOP_SAVE_NONVOL_FAR:
194 count = *(DWORD*)&info->UnwindCode[i+1];
195 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].u.OpInfo], count);
196 i += 2;
197 break;
198 case UWOP_SAVE_XMM128:
199 count = *(USHORT*)&info->UnwindCode[i+1] * 16;
200 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].u.OpInfo, count);
201 i++;
202 break;
203 case UWOP_SAVE_XMM128_FAR:
204 count = *(DWORD*)&info->UnwindCode[i+1];
205 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].u.OpInfo, count);
206 i += 2;
207 break;
208 case UWOP_PUSH_MACHFRAME:
209 TRACE("PUSH_MACHFRAME %u\n", info->UnwindCode[i].u.OpInfo);
210 break;
211 default:
212 FIXME("unknown code %u\n", info->UnwindCode[i].u.UnwindOp);
213 break;
217 addr += FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
218 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE);
219 if (info->Flags & UNW_FLAG_CHAININFO)
221 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.chain)))
223 FIXME("couldn't read memory for handler_data.chain\n");
224 return;
226 TRACE(" chained to function %p-%p\n",
227 (char*)base + handler_data.chain.BeginAddress,
228 (char*)base + handler_data.chain.EndAddress);
229 function = &handler_data.chain;
230 continue;
232 if (info->Flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
234 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.handler)))
236 FIXME("couldn't read memory for handler_data.handler\n");
237 return;
239 TRACE(" handler %p data at %p\n",
240 (char*)base + handler_data.handler, (char*)addr + sizeof(handler_data.handler));
242 break;
246 /* highly derived from dlls/ntdll/signal_x86_64.c */
247 static ULONG64 get_int_reg(CONTEXT *context, int reg)
249 return *(&context->Rax + reg);
252 static void set_int_reg(CONTEXT *context, int reg, ULONG64 val)
254 *(&context->Rax + reg) = val;
257 static void set_float_reg(CONTEXT *context, int reg, M128A val)
259 *(&context->u.s.Xmm0 + reg) = val;
262 static int get_opcode_size(UNWIND_CODE op)
264 switch (op.u.UnwindOp)
266 case UWOP_ALLOC_LARGE:
267 return 2 + (op.u.OpInfo != 0);
268 case UWOP_SAVE_NONVOL:
269 case UWOP_SAVE_XMM128:
270 return 2;
271 case UWOP_SAVE_NONVOL_FAR:
272 case UWOP_SAVE_XMM128_FAR:
273 return 3;
274 default:
275 return 1;
279 static BOOL is_inside_epilog(struct cpu_stack_walk* csw, DWORD64 pc,
280 DWORD64 base, const RUNTIME_FUNCTION *function )
282 BYTE op0, op1, op2;
283 LONG val32;
285 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
287 /* add or lea must be the first instruction, and it must have a rex.W prefix */
288 if ((op0 & 0xf8) == 0x48)
290 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
291 switch (op1)
293 case 0x81: /* add $nnnn,%rsp */
294 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
295 if (op0 == 0x48 && op2 == 0xc4)
297 pc += 7;
298 break;
300 return FALSE;
301 case 0x83: /* add $n,%rsp */
302 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
303 if (op0 == 0x48 && op2 == 0xc4)
305 pc += 4;
306 break;
308 return FALSE;
309 case 0x8d: /* lea n(reg),%rsp */
310 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
311 if (op0 & 0x06) return FALSE; /* rex.RX must be cleared */
312 if (((op2 >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
313 if ((op2 & 7) == 4) return FALSE; /* no SIB byte allowed */
314 if ((op2 >> 6) == 1) /* 8-bit offset */
316 pc += 4;
317 break;
319 if ((op2 >> 6) == 2) /* 32-bit offset */
321 pc += 7;
322 break;
324 return FALSE;
328 /* now check for various pop instructions */
329 for (;;)
331 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
332 if ((op0 & 0xf0) == 0x40) /* rex prefix */
334 if (!sw_read_mem(csw, ++pc, &op0, 1)) return FALSE;
337 switch (op0)
339 case 0x58: /* pop %rax/%r8 */
340 case 0x59: /* pop %rcx/%r9 */
341 case 0x5a: /* pop %rdx/%r10 */
342 case 0x5b: /* pop %rbx/%r11 */
343 case 0x5c: /* pop %rsp/%r12 */
344 case 0x5d: /* pop %rbp/%r13 */
345 case 0x5e: /* pop %rsi/%r14 */
346 case 0x5f: /* pop %rdi/%r15 */
347 pc++;
348 continue;
349 case 0xc2: /* ret $nn */
350 case 0xc3: /* ret */
351 return TRUE;
352 case 0xe9: /* jmp nnnn */
353 if (!sw_read_mem(csw, pc + 1, &val32, sizeof(LONG))) return FALSE;
354 pc += 5 + val32;
355 if (pc - base >= function->BeginAddress && pc - base < function->EndAddress)
356 continue;
357 break;
358 case 0xeb: /* jmp n */
359 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
360 pc += 2 + (signed char)op1;
361 if (pc - base >= function->BeginAddress && pc - base < function->EndAddress)
362 continue;
363 break;
364 case 0xf3: /* rep; ret (for amd64 prediction bug) */
365 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
366 return op1 == 0xc3;
368 return FALSE;
372 static BOOL interpret_epilog(struct cpu_stack_walk* csw, ULONG64 pc, CONTEXT *context )
374 BYTE insn, val8;
375 WORD val16;
376 LONG val32;
377 DWORD64 val64;
379 for (;;)
381 BYTE rex = 0;
383 if (!sw_read_mem(csw, pc, &insn, 1)) return FALSE;
384 if ((insn & 0xf0) == 0x40)
386 rex = insn & 0x0f; /* rex prefix */
387 if (!sw_read_mem(csw, ++pc, &insn, 1)) return FALSE;
390 switch (insn)
392 case 0x58: /* pop %rax/r8 */
393 case 0x59: /* pop %rcx/r9 */
394 case 0x5a: /* pop %rdx/r10 */
395 case 0x5b: /* pop %rbx/r11 */
396 case 0x5c: /* pop %rsp/r12 */
397 case 0x5d: /* pop %rbp/r13 */
398 case 0x5e: /* pop %rsi/r14 */
399 case 0x5f: /* pop %rdi/r15 */
400 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
401 set_int_reg(context, insn - 0x58 + (rex & 1) * 8, val64);
402 context->Rsp += sizeof(ULONG64);
403 pc++;
404 continue;
405 case 0x81: /* add $nnnn,%rsp */
406 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(LONG))) return FALSE;
407 context->Rsp += val32;
408 pc += 2 + sizeof(LONG);
409 continue;
410 case 0x83: /* add $n,%rsp */
411 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
412 context->Rsp += (signed char)val8;
413 pc += 3;
414 continue;
415 case 0x8d:
416 if (!sw_read_mem(csw, pc + 1, &insn, sizeof(BYTE))) return FALSE;
417 if ((insn >> 6) == 1) /* lea n(reg),%rsp */
419 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
420 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + (signed char)val8;
421 pc += 3;
423 else /* lea nnnn(reg),%rsp */
425 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(LONG))) return FALSE;
426 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + val32;
427 pc += 2 + sizeof(LONG);
429 continue;
430 case 0xc2: /* ret $nn */
431 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
432 if (!sw_read_mem(csw, pc + 1, &val16, sizeof(WORD))) return FALSE;
433 context->Rip = val64;
434 context->Rsp += sizeof(ULONG64) + val16;
435 return TRUE;
436 case 0xc3: /* ret */
437 case 0xf3: /* rep; ret */
438 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
439 context->Rip = val64;
440 context->Rsp += sizeof(ULONG64);
441 return TRUE;
442 case 0xe9: /* jmp nnnn */
443 if (!sw_read_mem(csw, pc + 1, &val32, sizeof(LONG))) return FALSE;
444 pc += 5 + val32;
445 continue;
446 case 0xeb: /* jmp n */
447 if (!sw_read_mem(csw, pc + 1, &val8, sizeof(BYTE))) return FALSE;
448 pc += 2 + (signed char)val8;
449 continue;
451 FIXME("unsupported insn %x\n", insn);
452 return FALSE;
456 static BOOL default_unwind(struct cpu_stack_walk* csw, CONTEXT* context)
458 if (!sw_read_mem(csw, context->Rsp, &context->Rip, sizeof(DWORD64)))
460 WARN("Cannot read new frame offset %s\n", wine_dbgstr_longlong(context->Rsp));
461 return FALSE;
463 context->Rsp += sizeof(DWORD64);
464 return TRUE;
467 static BOOL interpret_function_table_entry(struct cpu_stack_walk* csw,
468 CONTEXT* context, RUNTIME_FUNCTION* function, DWORD64 base)
470 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
471 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
472 unsigned i;
473 DWORD64 newframe, prolog_offset, off, value;
474 M128A floatvalue;
475 union handler_data handler_data;
477 /* FIXME: we have some assumptions here */
478 assert(context);
479 dump_unwind_info(csw, sw_module_base(csw, context->Rip), function);
480 newframe = context->Rsp;
481 for (;;)
483 if (!sw_read_mem(csw, base + function->UnwindData, info, sizeof(*info)) ||
484 !sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
485 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
487 WARN("Couldn't read unwind_code at %lx\n", base + function->UnwindData);
488 return FALSE;
491 if (info->Version != 1)
493 WARN("unknown unwind info version %u at %lx\n", info->Version, base + function->UnwindData);
494 return FALSE;
497 if (info->FrameRegister)
498 newframe = get_int_reg(context, info->FrameRegister) - info->FrameOffset * 16;
500 /* check if in prolog */
501 if (context->Rip >= base + function->BeginAddress &&
502 context->Rip < base + function->BeginAddress + info->SizeOfProlog)
504 prolog_offset = context->Rip - base - function->BeginAddress;
506 else
508 prolog_offset = ~0;
509 if (is_inside_epilog(csw, context->Rip, base, function))
511 interpret_epilog(csw, context->Rip, context);
512 return TRUE;
516 for (i = 0; i < info->CountOfCodes; i += get_opcode_size(info->UnwindCode[i]))
518 if (prolog_offset < info->UnwindCode[i].u.CodeOffset) continue; /* skip it */
520 switch (info->UnwindCode[i].u.UnwindOp)
522 case UWOP_PUSH_NONVOL: /* pushq %reg */
523 if (!sw_read_mem(csw, context->Rsp, &value, sizeof(DWORD64))) return FALSE;
524 set_int_reg(context, info->UnwindCode[i].u.OpInfo, value);
525 context->Rsp += sizeof(ULONG64);
526 break;
527 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
528 if (info->UnwindCode[i].u.OpInfo) context->Rsp += *(DWORD*)&info->UnwindCode[i+1];
529 else context->Rsp += *(USHORT*)&info->UnwindCode[i+1] * 8;
530 break;
531 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
532 context->Rsp += (info->UnwindCode[i].u.OpInfo + 1) * 8;
533 break;
534 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
535 context->Rsp = newframe;
536 break;
537 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
538 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 8;
539 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
540 set_int_reg(context, info->UnwindCode[i].u.OpInfo, value);
541 break;
542 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
543 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
544 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
545 set_int_reg(context, info->UnwindCode[i].u.OpInfo, value);
546 break;
547 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
548 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 16;
549 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
550 set_float_reg(context, info->UnwindCode[i].u.OpInfo, floatvalue);
551 break;
552 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
553 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
554 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
555 set_float_reg(context, info->UnwindCode[i].u.OpInfo, floatvalue);
556 break;
557 case UWOP_PUSH_MACHFRAME:
558 FIXME("PUSH_MACHFRAME %u\n", info->UnwindCode[i].u.OpInfo);
559 break;
560 default:
561 FIXME("unknown code %u\n", info->UnwindCode[i].u.UnwindOp);
562 break;
565 if (!(info->Flags & UNW_FLAG_CHAININFO)) break;
566 if (!sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
567 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE),
568 &handler_data, sizeof(handler_data))) return FALSE;
569 function = &handler_data.chain; /* restart with the chained info */
571 return default_unwind(csw, context);
574 /* fetch_next_frame()
576 * modify (at least) context.{rip, rsp, rbp} using unwind information
577 * either out of PE exception handlers, debug info (dwarf), or simple stack unwind
579 static BOOL fetch_next_frame(struct cpu_stack_walk *csw, union ctx *pcontext,
580 DWORD_PTR curr_pc, void** prtf)
582 DWORD64 cfa;
583 RUNTIME_FUNCTION* rtf;
584 DWORD64 base;
585 CONTEXT *context = &pcontext->ctx;
587 if (!curr_pc || !(base = sw_module_base(csw, curr_pc))) return FALSE;
588 rtf = sw_table_access(csw, curr_pc);
589 if (prtf) *prtf = rtf;
590 if (rtf)
592 return interpret_function_table_entry(csw, context, rtf, base);
594 else if (dwarf2_virtual_unwind(csw, curr_pc, pcontext, &cfa))
596 context->Rsp = cfa;
597 TRACE("next function rip=%016lx\n", context->Rip);
598 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
599 context->Rax, context->Rbx, context->Rcx, context->Rdx);
600 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
601 context->Rsi, context->Rdi, context->Rbp, context->Rsp);
602 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
603 context->R8, context->R9, context->R10, context->R11);
604 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
605 context->R12, context->R13, context->R14, context->R15);
606 return TRUE;
608 else
609 return default_unwind(csw, context);
612 static BOOL x86_64_stack_walk(struct cpu_stack_walk *csw, STACKFRAME64 *frame,
613 union ctx *context)
615 unsigned deltapc = curr_count <= 1 ? 0 : 1;
617 /* sanity check */
618 if (curr_mode >= stm_done) return FALSE;
619 assert(!csw->is32);
621 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s\n",
622 wine_dbgstr_addr(&frame->AddrPC),
623 wine_dbgstr_addr(&frame->AddrFrame),
624 wine_dbgstr_addr(&frame->AddrReturn),
625 wine_dbgstr_addr(&frame->AddrStack),
626 curr_mode == stm_start ? "start" : "64bit",
627 wine_dbgstr_longlong(curr_count));
629 if (curr_mode == stm_start)
631 if ((frame->AddrPC.Mode == AddrModeFlat) &&
632 (frame->AddrFrame.Mode != AddrModeFlat))
634 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
635 goto done_err;
638 /* Init done */
639 curr_mode = stm_64bit;
640 frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
641 /* don't set up AddrStack on first call. Either the caller has set it up, or
642 * we will get it in the next frame
644 memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
646 else
648 if (context->ctx.Rsp != frame->AddrStack.Offset) FIXME("inconsistent Stack Pointer\n");
649 if (context->ctx.Rip != frame->AddrPC.Offset) FIXME("inconsistent Instruction Pointer\n");
651 if (frame->AddrReturn.Offset == 0) goto done_err;
652 if (!fetch_next_frame(csw, context, frame->AddrPC.Offset - deltapc, &frame->FuncTableEntry))
653 goto done_err;
654 deltapc = 1;
657 memset(&frame->Params, 0, sizeof(frame->Params));
659 /* set frame information */
660 frame->AddrStack.Offset = context->ctx.Rsp;
661 frame->AddrFrame.Offset = context->ctx.Rbp;
662 frame->AddrPC.Offset = context->ctx.Rip;
663 if (1)
665 union ctx newctx = *context;
667 if (!fetch_next_frame(csw, &newctx, frame->AddrPC.Offset - deltapc, NULL))
668 goto done_err;
669 frame->AddrReturn.Mode = AddrModeFlat;
670 frame->AddrReturn.Offset = newctx.ctx.Rip;
673 frame->Far = TRUE;
674 frame->Virtual = TRUE;
675 curr_count++;
677 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s FuncTable=%p\n",
678 wine_dbgstr_addr(&frame->AddrPC),
679 wine_dbgstr_addr(&frame->AddrFrame),
680 wine_dbgstr_addr(&frame->AddrReturn),
681 wine_dbgstr_addr(&frame->AddrStack),
682 curr_mode == stm_start ? "start" : "64bit",
683 wine_dbgstr_longlong(curr_count),
684 frame->FuncTableEntry);
686 return TRUE;
687 done_err:
688 curr_mode = stm_done;
689 return FALSE;
691 #else
692 static BOOL x86_64_stack_walk(struct cpu_stack_walk *csw, STACKFRAME64 *frame,
693 union ctx *ctx)
695 return FALSE;
697 #endif
699 static void* x86_64_find_runtime_function(struct module* module, DWORD64 addr)
701 #ifdef __x86_64__
702 RUNTIME_FUNCTION* rtf;
703 ULONG size;
704 int min, max;
706 rtf = (RUNTIME_FUNCTION*)pe_map_directory(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size);
707 if (rtf) for (min = 0, max = size / sizeof(*rtf); min <= max; )
709 int pos = (min + max) / 2;
710 if (addr < module->module.BaseOfImage + rtf[pos].BeginAddress) max = pos - 1;
711 else if (addr >= module->module.BaseOfImage + rtf[pos].EndAddress) min = pos + 1;
712 else
714 rtf += pos;
715 while (rtf->UnwindData & 1) /* follow chained entry */
717 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
718 return NULL;
719 /* we need to read into the other process */
720 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
722 return rtf;
725 #endif
726 return NULL;
729 static unsigned x86_64_map_dwarf_register(unsigned regno, const struct module* module, BOOL eh_frame)
731 unsigned reg;
733 if (regno >= 17 && regno <= 24)
734 reg = CV_AMD64_XMM0 + regno - 17;
735 else if (regno >= 25 && regno <= 32)
736 reg = CV_AMD64_XMM8 + regno - 25;
737 else if (regno >= 33 && regno <= 40)
738 reg = CV_AMD64_ST0 + regno - 33;
739 else switch (regno)
741 case 0: reg = CV_AMD64_RAX; break;
742 case 1: reg = CV_AMD64_RDX; break;
743 case 2: reg = CV_AMD64_RCX; break;
744 case 3: reg = CV_AMD64_RBX; break;
745 case 4: reg = CV_AMD64_RSI; break;
746 case 5: reg = CV_AMD64_RDI; break;
747 case 6: reg = CV_AMD64_RBP; break;
748 case 7: reg = CV_AMD64_RSP; break;
749 case 8: reg = CV_AMD64_R8; break;
750 case 9: reg = CV_AMD64_R9; break;
751 case 10: reg = CV_AMD64_R10; break;
752 case 11: reg = CV_AMD64_R11; break;
753 case 12: reg = CV_AMD64_R12; break;
754 case 13: reg = CV_AMD64_R13; break;
755 case 14: reg = CV_AMD64_R14; break;
756 case 15: reg = CV_AMD64_R15; break;
757 case 16: reg = CV_AMD64_RIP; break;
758 case 49: reg = CV_AMD64_EFLAGS; break;
759 case 50: reg = CV_AMD64_ES; break;
760 case 51: reg = CV_AMD64_CS; break;
761 case 52: reg = CV_AMD64_SS; break;
762 case 53: reg = CV_AMD64_DS; break;
763 case 54: reg = CV_AMD64_FS; break;
764 case 55: reg = CV_AMD64_GS; break;
765 case 62: reg = CV_AMD64_TR; break;
766 case 63: reg = CV_AMD64_LDTR; break;
767 case 64: reg = CV_AMD64_MXCSR; break;
768 case 65: reg = CV_AMD64_CTRL; break;
769 case 66: reg = CV_AMD64_STAT; break;
771 * 56-57 reserved
772 * 58 %fs.base
773 * 59 %gs.base
774 * 60-61 reserved
776 default:
777 FIXME("Don't know how to map register %d\n", regno);
778 return 0;
780 return reg;
783 static void *x86_64_fetch_context_reg(union ctx *pctx, unsigned regno, unsigned *size)
785 #ifdef __x86_64__
786 CONTEXT *ctx = &pctx->ctx;
788 switch (regno)
790 case CV_AMD64_RAX: *size = sizeof(ctx->Rax); return &ctx->Rax;
791 case CV_AMD64_RDX: *size = sizeof(ctx->Rdx); return &ctx->Rdx;
792 case CV_AMD64_RCX: *size = sizeof(ctx->Rcx); return &ctx->Rcx;
793 case CV_AMD64_RBX: *size = sizeof(ctx->Rbx); return &ctx->Rbx;
794 case CV_AMD64_RSI: *size = sizeof(ctx->Rsi); return &ctx->Rsi;
795 case CV_AMD64_RDI: *size = sizeof(ctx->Rdi); return &ctx->Rdi;
796 case CV_AMD64_RBP: *size = sizeof(ctx->Rbp); return &ctx->Rbp;
797 case CV_AMD64_RSP: *size = sizeof(ctx->Rsp); return &ctx->Rsp;
798 case CV_AMD64_R8: *size = sizeof(ctx->R8); return &ctx->R8;
799 case CV_AMD64_R9: *size = sizeof(ctx->R9); return &ctx->R9;
800 case CV_AMD64_R10: *size = sizeof(ctx->R10); return &ctx->R10;
801 case CV_AMD64_R11: *size = sizeof(ctx->R11); return &ctx->R11;
802 case CV_AMD64_R12: *size = sizeof(ctx->R12); return &ctx->R12;
803 case CV_AMD64_R13: *size = sizeof(ctx->R13); return &ctx->R13;
804 case CV_AMD64_R14: *size = sizeof(ctx->R14); return &ctx->R14;
805 case CV_AMD64_R15: *size = sizeof(ctx->R15); return &ctx->R15;
806 case CV_AMD64_RIP: *size = sizeof(ctx->Rip); return &ctx->Rip;
808 case CV_AMD64_XMM0 + 0: *size = sizeof(ctx->u.s.Xmm0 ); return &ctx->u.s.Xmm0;
809 case CV_AMD64_XMM0 + 1: *size = sizeof(ctx->u.s.Xmm1 ); return &ctx->u.s.Xmm1;
810 case CV_AMD64_XMM0 + 2: *size = sizeof(ctx->u.s.Xmm2 ); return &ctx->u.s.Xmm2;
811 case CV_AMD64_XMM0 + 3: *size = sizeof(ctx->u.s.Xmm3 ); return &ctx->u.s.Xmm3;
812 case CV_AMD64_XMM0 + 4: *size = sizeof(ctx->u.s.Xmm4 ); return &ctx->u.s.Xmm4;
813 case CV_AMD64_XMM0 + 5: *size = sizeof(ctx->u.s.Xmm5 ); return &ctx->u.s.Xmm5;
814 case CV_AMD64_XMM0 + 6: *size = sizeof(ctx->u.s.Xmm6 ); return &ctx->u.s.Xmm6;
815 case CV_AMD64_XMM0 + 7: *size = sizeof(ctx->u.s.Xmm7 ); return &ctx->u.s.Xmm7;
816 case CV_AMD64_XMM8 + 0: *size = sizeof(ctx->u.s.Xmm8 ); return &ctx->u.s.Xmm8;
817 case CV_AMD64_XMM8 + 1: *size = sizeof(ctx->u.s.Xmm9 ); return &ctx->u.s.Xmm9;
818 case CV_AMD64_XMM8 + 2: *size = sizeof(ctx->u.s.Xmm10); return &ctx->u.s.Xmm10;
819 case CV_AMD64_XMM8 + 3: *size = sizeof(ctx->u.s.Xmm11); return &ctx->u.s.Xmm11;
820 case CV_AMD64_XMM8 + 4: *size = sizeof(ctx->u.s.Xmm12); return &ctx->u.s.Xmm12;
821 case CV_AMD64_XMM8 + 5: *size = sizeof(ctx->u.s.Xmm13); return &ctx->u.s.Xmm13;
822 case CV_AMD64_XMM8 + 6: *size = sizeof(ctx->u.s.Xmm14); return &ctx->u.s.Xmm14;
823 case CV_AMD64_XMM8 + 7: *size = sizeof(ctx->u.s.Xmm15); return &ctx->u.s.Xmm15;
825 case CV_AMD64_ST0 + 0: *size = sizeof(ctx->u.s.Legacy[0]); return &ctx->u.s.Legacy[0];
826 case CV_AMD64_ST0 + 1: *size = sizeof(ctx->u.s.Legacy[1]); return &ctx->u.s.Legacy[1];
827 case CV_AMD64_ST0 + 2: *size = sizeof(ctx->u.s.Legacy[2]); return &ctx->u.s.Legacy[2];
828 case CV_AMD64_ST0 + 3: *size = sizeof(ctx->u.s.Legacy[3]); return &ctx->u.s.Legacy[3];
829 case CV_AMD64_ST0 + 4: *size = sizeof(ctx->u.s.Legacy[4]); return &ctx->u.s.Legacy[4];
830 case CV_AMD64_ST0 + 5: *size = sizeof(ctx->u.s.Legacy[5]); return &ctx->u.s.Legacy[5];
831 case CV_AMD64_ST0 + 6: *size = sizeof(ctx->u.s.Legacy[6]); return &ctx->u.s.Legacy[6];
832 case CV_AMD64_ST0 + 7: *size = sizeof(ctx->u.s.Legacy[7]); return &ctx->u.s.Legacy[7];
834 case CV_AMD64_EFLAGS: *size = sizeof(ctx->EFlags); return &ctx->EFlags;
835 case CV_AMD64_ES: *size = sizeof(ctx->SegEs); return &ctx->SegEs;
836 case CV_AMD64_CS: *size = sizeof(ctx->SegCs); return &ctx->SegCs;
837 case CV_AMD64_SS: *size = sizeof(ctx->SegSs); return &ctx->SegSs;
838 case CV_AMD64_DS: *size = sizeof(ctx->SegDs); return &ctx->SegDs;
839 case CV_AMD64_FS: *size = sizeof(ctx->SegFs); return &ctx->SegFs;
840 case CV_AMD64_GS: *size = sizeof(ctx->SegGs); return &ctx->SegGs;
843 #endif
844 FIXME("Unknown register %x\n", regno);
845 return NULL;
848 static const char* x86_64_fetch_regname(unsigned regno)
850 switch (regno)
852 case CV_AMD64_RAX: return "rax";
853 case CV_AMD64_RDX: return "rdx";
854 case CV_AMD64_RCX: return "rcx";
855 case CV_AMD64_RBX: return "rbx";
856 case CV_AMD64_RSI: return "rsi";
857 case CV_AMD64_RDI: return "rdi";
858 case CV_AMD64_RBP: return "rbp";
859 case CV_AMD64_RSP: return "rsp";
860 case CV_AMD64_R8: return "r8";
861 case CV_AMD64_R9: return "r9";
862 case CV_AMD64_R10: return "r10";
863 case CV_AMD64_R11: return "r11";
864 case CV_AMD64_R12: return "r12";
865 case CV_AMD64_R13: return "r13";
866 case CV_AMD64_R14: return "r14";
867 case CV_AMD64_R15: return "r15";
868 case CV_AMD64_RIP: return "rip";
870 case CV_AMD64_XMM0 + 0: return "xmm0";
871 case CV_AMD64_XMM0 + 1: return "xmm1";
872 case CV_AMD64_XMM0 + 2: return "xmm2";
873 case CV_AMD64_XMM0 + 3: return "xmm3";
874 case CV_AMD64_XMM0 + 4: return "xmm4";
875 case CV_AMD64_XMM0 + 5: return "xmm5";
876 case CV_AMD64_XMM0 + 6: return "xmm6";
877 case CV_AMD64_XMM0 + 7: return "xmm7";
878 case CV_AMD64_XMM8 + 0: return "xmm8";
879 case CV_AMD64_XMM8 + 1: return "xmm9";
880 case CV_AMD64_XMM8 + 2: return "xmm10";
881 case CV_AMD64_XMM8 + 3: return "xmm11";
882 case CV_AMD64_XMM8 + 4: return "xmm12";
883 case CV_AMD64_XMM8 + 5: return "xmm13";
884 case CV_AMD64_XMM8 + 6: return "xmm14";
885 case CV_AMD64_XMM8 + 7: return "xmm15";
887 case CV_AMD64_ST0 + 0: return "st0";
888 case CV_AMD64_ST0 + 1: return "st1";
889 case CV_AMD64_ST0 + 2: return "st2";
890 case CV_AMD64_ST0 + 3: return "st3";
891 case CV_AMD64_ST0 + 4: return "st4";
892 case CV_AMD64_ST0 + 5: return "st5";
893 case CV_AMD64_ST0 + 6: return "st6";
894 case CV_AMD64_ST0 + 7: return "st7";
896 case CV_AMD64_EFLAGS: return "eflags";
897 case CV_AMD64_ES: return "es";
898 case CV_AMD64_CS: return "cs";
899 case CV_AMD64_SS: return "ss";
900 case CV_AMD64_DS: return "ds";
901 case CV_AMD64_FS: return "fs";
902 case CV_AMD64_GS: return "gs";
904 FIXME("Unknown register %x\n", regno);
905 return NULL;
908 static BOOL x86_64_fetch_minidump_thread(struct dump_context* dc, unsigned index, unsigned flags, const CONTEXT* ctx)
910 if (ctx->ContextFlags && (flags & ThreadWriteInstructionWindow))
912 /* FIXME: crop values across module boundaries, */
913 #ifdef __x86_64__
914 ULONG64 base = ctx->Rip <= 0x80 ? 0 : ctx->Rip - 0x80;
915 minidump_add_memory_block(dc, base, ctx->Rip + 0x80 - base, 0);
916 #endif
919 return TRUE;
922 static BOOL x86_64_fetch_minidump_module(struct dump_context* dc, unsigned index, unsigned flags)
924 /* FIXME: not sure about the flags... */
925 if (1)
927 /* FIXME: crop values across module boundaries, */
928 #ifdef __x86_64__
929 struct process* pcs;
930 struct module* module;
931 const RUNTIME_FUNCTION* rtf;
932 ULONG size;
934 if (!(pcs = process_find_by_handle(dc->process->handle)) ||
935 !(module = module_find_by_addr(pcs, dc->modules[index].base, DMT_UNKNOWN)))
936 return FALSE;
937 rtf = (const RUNTIME_FUNCTION*)pe_map_directory(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size);
938 if (rtf)
940 const RUNTIME_FUNCTION* end = (const RUNTIME_FUNCTION*)((const char*)rtf + size);
941 UNWIND_INFO ui;
943 while (rtf + 1 < end)
945 while (rtf->UnwindData & 1) /* follow chained entry */
947 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
948 return FALSE;
949 /* we need to read into the other process */
950 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
952 if (read_process_memory(dc->process, dc->modules[index].base + rtf->UnwindData, &ui, sizeof(ui)))
953 minidump_add_memory_block(dc, dc->modules[index].base + rtf->UnwindData,
954 FIELD_OFFSET(UNWIND_INFO, UnwindCode) + ui.CountOfCodes * sizeof(UNWIND_CODE), 0);
955 rtf++;
958 #endif
961 return TRUE;
964 DECLSPEC_HIDDEN struct cpu cpu_x86_64 = {
965 IMAGE_FILE_MACHINE_AMD64,
967 CV_AMD64_RSP,
968 x86_64_get_addr,
969 x86_64_stack_walk,
970 x86_64_find_runtime_function,
971 x86_64_map_dwarf_register,
972 x86_64_fetch_context_reg,
973 x86_64_fetch_regname,
974 x86_64_fetch_minidump_thread,
975 x86_64_fetch_minidump_module,