GDB (API)
|
00001 /* Copyright (C) 2009-2013 Free Software Foundation, Inc. 00002 00003 This file is part of GDB. 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00017 00018 #include "defs.h" 00019 #include "osabi.h" 00020 #include "amd64-tdep.h" 00021 #include "gdbtypes.h" 00022 #include "gdbcore.h" 00023 #include "regcache.h" 00024 #include "windows-tdep.h" 00025 #include "frame.h" 00026 #include "objfiles.h" 00027 #include "frame-unwind.h" 00028 #include "coff/internal.h" 00029 #include "coff/i386.h" 00030 #include "coff/pe.h" 00031 #include "libcoff.h" 00032 #include "value.h" 00033 00034 /* The registers used to pass integer arguments during a function call. */ 00035 static int amd64_windows_dummy_call_integer_regs[] = 00036 { 00037 AMD64_RCX_REGNUM, /* %rcx */ 00038 AMD64_RDX_REGNUM, /* %rdx */ 00039 AMD64_R8_REGNUM, /* %r8 */ 00040 AMD64_R9_REGNUM /* %r9 */ 00041 }; 00042 00043 /* Return nonzero if an argument of type TYPE should be passed 00044 via one of the integer registers. */ 00045 00046 static int 00047 amd64_windows_passed_by_integer_register (struct type *type) 00048 { 00049 switch (TYPE_CODE (type)) 00050 { 00051 case TYPE_CODE_INT: 00052 case TYPE_CODE_ENUM: 00053 case TYPE_CODE_BOOL: 00054 case TYPE_CODE_RANGE: 00055 case TYPE_CODE_CHAR: 00056 case TYPE_CODE_PTR: 00057 case TYPE_CODE_REF: 00058 case TYPE_CODE_STRUCT: 00059 case TYPE_CODE_UNION: 00060 return (TYPE_LENGTH (type) == 1 00061 || TYPE_LENGTH (type) == 2 00062 || TYPE_LENGTH (type) == 4 00063 || TYPE_LENGTH (type) == 8); 00064 00065 default: 00066 return 0; 00067 } 00068 } 00069 00070 /* Return nonzero if an argument of type TYPE should be passed 00071 via one of the XMM registers. */ 00072 00073 static int 00074 amd64_windows_passed_by_xmm_register (struct type *type) 00075 { 00076 return ((TYPE_CODE (type) == TYPE_CODE_FLT 00077 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT) 00078 && (TYPE_LENGTH (type) == 4 || TYPE_LENGTH (type) == 8)); 00079 } 00080 00081 /* Return non-zero iff an argument of the given TYPE should be passed 00082 by pointer. */ 00083 00084 static int 00085 amd64_windows_passed_by_pointer (struct type *type) 00086 { 00087 if (amd64_windows_passed_by_integer_register (type)) 00088 return 0; 00089 00090 if (amd64_windows_passed_by_xmm_register (type)) 00091 return 0; 00092 00093 return 1; 00094 } 00095 00096 /* For each argument that should be passed by pointer, reserve some 00097 stack space, store a copy of the argument on the stack, and replace 00098 the argument by its address. Return the new Stack Pointer value. 00099 00100 NARGS is the number of arguments. ARGS is the array containing 00101 the value of each argument. SP is value of the Stack Pointer. */ 00102 00103 static CORE_ADDR 00104 amd64_windows_adjust_args_passed_by_pointer (struct value **args, 00105 int nargs, CORE_ADDR sp) 00106 { 00107 int i; 00108 00109 for (i = 0; i < nargs; i++) 00110 if (amd64_windows_passed_by_pointer (value_type (args[i]))) 00111 { 00112 struct type *type = value_type (args[i]); 00113 const gdb_byte *valbuf = value_contents (args[i]); 00114 const int len = TYPE_LENGTH (type); 00115 00116 /* Store a copy of that argument on the stack, aligned to 00117 a 16 bytes boundary, and then use the copy's address as 00118 the argument. */ 00119 00120 sp -= len; 00121 sp &= ~0xf; 00122 write_memory (sp, valbuf, len); 00123 00124 args[i] 00125 = value_addr (value_from_contents_and_address (type, valbuf, sp)); 00126 } 00127 00128 return sp; 00129 } 00130 00131 /* Store the value of ARG in register REGNO (right-justified). 00132 REGCACHE is the register cache. */ 00133 00134 static void 00135 amd64_windows_store_arg_in_reg (struct regcache *regcache, 00136 struct value *arg, int regno) 00137 { 00138 struct type *type = value_type (arg); 00139 const gdb_byte *valbuf = value_contents (arg); 00140 gdb_byte buf[8]; 00141 00142 gdb_assert (TYPE_LENGTH (type) <= 8); 00143 memset (buf, 0, sizeof buf); 00144 memcpy (buf, valbuf, min (TYPE_LENGTH (type), 8)); 00145 regcache_cooked_write (regcache, regno, buf); 00146 } 00147 00148 /* Push the arguments for an inferior function call, and return 00149 the updated value of the SP (Stack Pointer). 00150 00151 All arguments are identical to the arguments used in 00152 amd64_windows_push_dummy_call. */ 00153 00154 static CORE_ADDR 00155 amd64_windows_push_arguments (struct regcache *regcache, int nargs, 00156 struct value **args, CORE_ADDR sp, 00157 int struct_return) 00158 { 00159 int reg_idx = 0; 00160 int i; 00161 struct value **stack_args = alloca (nargs * sizeof (struct value *)); 00162 int num_stack_args = 0; 00163 int num_elements = 0; 00164 int element = 0; 00165 00166 /* First, handle the arguments passed by pointer. 00167 00168 These arguments are replaced by pointers to a copy we are making 00169 in inferior memory. So use a copy of the ARGS table, to avoid 00170 modifying the original one. */ 00171 { 00172 struct value **args1 = alloca (nargs * sizeof (struct value *)); 00173 00174 memcpy (args1, args, nargs * sizeof (struct value *)); 00175 sp = amd64_windows_adjust_args_passed_by_pointer (args1, nargs, sp); 00176 args = args1; 00177 } 00178 00179 /* Reserve a register for the "hidden" argument. */ 00180 if (struct_return) 00181 reg_idx++; 00182 00183 for (i = 0; i < nargs; i++) 00184 { 00185 struct type *type = value_type (args[i]); 00186 int len = TYPE_LENGTH (type); 00187 int on_stack_p = 1; 00188 00189 if (reg_idx < ARRAY_SIZE (amd64_windows_dummy_call_integer_regs)) 00190 { 00191 if (amd64_windows_passed_by_integer_register (type)) 00192 { 00193 amd64_windows_store_arg_in_reg 00194 (regcache, args[i], 00195 amd64_windows_dummy_call_integer_regs[reg_idx]); 00196 on_stack_p = 0; 00197 reg_idx++; 00198 } 00199 else if (amd64_windows_passed_by_xmm_register (type)) 00200 { 00201 amd64_windows_store_arg_in_reg 00202 (regcache, args[i], AMD64_XMM0_REGNUM + reg_idx); 00203 /* In case of varargs, these parameters must also be 00204 passed via the integer registers. */ 00205 amd64_windows_store_arg_in_reg 00206 (regcache, args[i], 00207 amd64_windows_dummy_call_integer_regs[reg_idx]); 00208 on_stack_p = 0; 00209 reg_idx++; 00210 } 00211 } 00212 00213 if (on_stack_p) 00214 { 00215 num_elements += ((len + 7) / 8); 00216 stack_args[num_stack_args++] = args[i]; 00217 } 00218 } 00219 00220 /* Allocate space for the arguments on the stack, keeping it 00221 aligned on a 16 byte boundary. */ 00222 sp -= num_elements * 8; 00223 sp &= ~0xf; 00224 00225 /* Write out the arguments to the stack. */ 00226 for (i = 0; i < num_stack_args; i++) 00227 { 00228 struct type *type = value_type (stack_args[i]); 00229 const gdb_byte *valbuf = value_contents (stack_args[i]); 00230 00231 write_memory (sp + element * 8, valbuf, TYPE_LENGTH (type)); 00232 element += ((TYPE_LENGTH (type) + 7) / 8); 00233 } 00234 00235 return sp; 00236 } 00237 00238 /* Implement the "push_dummy_call" gdbarch method. */ 00239 00240 static CORE_ADDR 00241 amd64_windows_push_dummy_call 00242 (struct gdbarch *gdbarch, struct value *function, 00243 struct regcache *regcache, CORE_ADDR bp_addr, 00244 int nargs, struct value **args, 00245 CORE_ADDR sp, int struct_return, CORE_ADDR struct_addr) 00246 { 00247 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00248 gdb_byte buf[8]; 00249 00250 /* Pass arguments. */ 00251 sp = amd64_windows_push_arguments (regcache, nargs, args, sp, 00252 struct_return); 00253 00254 /* Pass "hidden" argument". */ 00255 if (struct_return) 00256 { 00257 /* The "hidden" argument is passed throught the first argument 00258 register. */ 00259 const int arg_regnum = amd64_windows_dummy_call_integer_regs[0]; 00260 00261 store_unsigned_integer (buf, 8, byte_order, struct_addr); 00262 regcache_cooked_write (regcache, arg_regnum, buf); 00263 } 00264 00265 /* Reserve some memory on the stack for the integer-parameter 00266 registers, as required by the ABI. */ 00267 sp -= ARRAY_SIZE (amd64_windows_dummy_call_integer_regs) * 8; 00268 00269 /* Store return address. */ 00270 sp -= 8; 00271 store_unsigned_integer (buf, 8, byte_order, bp_addr); 00272 write_memory (sp, buf, 8); 00273 00274 /* Update the stack pointer... */ 00275 store_unsigned_integer (buf, 8, byte_order, sp); 00276 regcache_cooked_write (regcache, AMD64_RSP_REGNUM, buf); 00277 00278 /* ...and fake a frame pointer. */ 00279 regcache_cooked_write (regcache, AMD64_RBP_REGNUM, buf); 00280 00281 return sp + 16; 00282 } 00283 00284 /* Implement the "return_value" gdbarch method for amd64-windows. */ 00285 00286 static enum return_value_convention 00287 amd64_windows_return_value (struct gdbarch *gdbarch, struct value *function, 00288 struct type *type, struct regcache *regcache, 00289 gdb_byte *readbuf, const gdb_byte *writebuf) 00290 { 00291 int len = TYPE_LENGTH (type); 00292 int regnum = -1; 00293 00294 /* See if our value is returned through a register. If it is, then 00295 store the associated register number in REGNUM. */ 00296 switch (TYPE_CODE (type)) 00297 { 00298 case TYPE_CODE_FLT: 00299 case TYPE_CODE_DECFLOAT: 00300 /* __m128, __m128i, __m128d, floats, and doubles are returned 00301 via XMM0. */ 00302 if (len == 4 || len == 8 || len == 16) 00303 regnum = AMD64_XMM0_REGNUM; 00304 break; 00305 default: 00306 /* All other values that are 1, 2, 4 or 8 bytes long are returned 00307 via RAX. */ 00308 if (len == 1 || len == 2 || len == 4 || len == 8) 00309 regnum = AMD64_RAX_REGNUM; 00310 break; 00311 } 00312 00313 if (regnum < 0) 00314 { 00315 /* RAX contains the address where the return value has been stored. */ 00316 if (readbuf) 00317 { 00318 ULONGEST addr; 00319 00320 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr); 00321 read_memory (addr, readbuf, TYPE_LENGTH (type)); 00322 } 00323 return RETURN_VALUE_ABI_RETURNS_ADDRESS; 00324 } 00325 else 00326 { 00327 /* Extract the return value from the register where it was stored. */ 00328 if (readbuf) 00329 regcache_raw_read_part (regcache, regnum, 0, len, readbuf); 00330 if (writebuf) 00331 regcache_raw_write_part (regcache, regnum, 0, len, writebuf); 00332 return RETURN_VALUE_REGISTER_CONVENTION; 00333 } 00334 } 00335 00336 /* Check that the code pointed to by PC corresponds to a call to 00337 __main, skip it if so. Return PC otherwise. */ 00338 00339 static CORE_ADDR 00340 amd64_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc) 00341 { 00342 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00343 gdb_byte op; 00344 00345 target_read_memory (pc, &op, 1); 00346 if (op == 0xe8) 00347 { 00348 gdb_byte buf[4]; 00349 00350 if (target_read_memory (pc + 1, buf, sizeof buf) == 0) 00351 { 00352 struct bound_minimal_symbol s; 00353 CORE_ADDR call_dest; 00354 00355 call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order); 00356 s = lookup_minimal_symbol_by_pc (call_dest); 00357 if (s.minsym != NULL 00358 && SYMBOL_LINKAGE_NAME (s.minsym) != NULL 00359 && strcmp (SYMBOL_LINKAGE_NAME (s.minsym), "__main") == 0) 00360 pc += 5; 00361 } 00362 } 00363 00364 return pc; 00365 } 00366 00367 struct amd64_windows_frame_cache 00368 { 00369 /* ImageBase for the module. */ 00370 CORE_ADDR image_base; 00371 00372 /* Function start and end rva. */ 00373 CORE_ADDR start_rva; 00374 CORE_ADDR end_rva; 00375 00376 /* Next instruction to be executed. */ 00377 CORE_ADDR pc; 00378 00379 /* Current sp. */ 00380 CORE_ADDR sp; 00381 00382 /* Address of saved integer and xmm registers. */ 00383 CORE_ADDR prev_reg_addr[16]; 00384 CORE_ADDR prev_xmm_addr[16]; 00385 00386 /* These two next fields are set only for machine info frames. */ 00387 00388 /* Likewise for RIP. */ 00389 CORE_ADDR prev_rip_addr; 00390 00391 /* Likewise for RSP. */ 00392 CORE_ADDR prev_rsp_addr; 00393 00394 /* Address of the previous frame. */ 00395 CORE_ADDR prev_sp; 00396 }; 00397 00398 /* Convert a Windows register number to gdb. */ 00399 static const enum amd64_regnum amd64_windows_w2gdb_regnum[] = 00400 { 00401 AMD64_RAX_REGNUM, 00402 AMD64_RCX_REGNUM, 00403 AMD64_RDX_REGNUM, 00404 AMD64_RBX_REGNUM, 00405 AMD64_RSP_REGNUM, 00406 AMD64_RBP_REGNUM, 00407 AMD64_RSI_REGNUM, 00408 AMD64_RDI_REGNUM, 00409 AMD64_R8_REGNUM, 00410 AMD64_R9_REGNUM, 00411 AMD64_R10_REGNUM, 00412 AMD64_R11_REGNUM, 00413 AMD64_R12_REGNUM, 00414 AMD64_R13_REGNUM, 00415 AMD64_R14_REGNUM, 00416 AMD64_R15_REGNUM 00417 }; 00418 00419 /* Return TRUE iff PC is the the range of the function corresponding to 00420 CACHE. */ 00421 00422 static int 00423 pc_in_range (CORE_ADDR pc, const struct amd64_windows_frame_cache *cache) 00424 { 00425 return (pc >= cache->image_base + cache->start_rva 00426 && pc < cache->image_base + cache->end_rva); 00427 } 00428 00429 /* Try to recognize and decode an epilogue sequence. 00430 00431 Return -1 if we fail to read the instructions for any reason. 00432 Return 1 if an epilogue sequence was recognized, 0 otherwise. */ 00433 00434 static int 00435 amd64_windows_frame_decode_epilogue (struct frame_info *this_frame, 00436 struct amd64_windows_frame_cache *cache) 00437 { 00438 /* According to MSDN an epilogue "must consist of either an add RSP,constant 00439 or lea RSP,constant[FPReg], followed by a series of zero or more 8-byte 00440 register pops and a return or a jmp". 00441 00442 Furthermore, according to RtlVirtualUnwind, the complete list of 00443 epilog marker is: 00444 - ret [c3] 00445 - ret n [c2 imm16] 00446 - rep ret [f3 c3] 00447 - jmp imm8 | imm32 [eb rel8] or [e9 rel32] 00448 - jmp qword ptr imm32 - not handled 00449 - rex.w jmp reg [4X ff eY] 00450 */ 00451 00452 CORE_ADDR pc = cache->pc; 00453 CORE_ADDR cur_sp = cache->sp; 00454 struct gdbarch *gdbarch = get_frame_arch (this_frame); 00455 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00456 gdb_byte op; 00457 gdb_byte rex; 00458 00459 /* We don't care about the instruction deallocating the frame: 00460 if it hasn't been executed, the pc is still in the body, 00461 if it has been executed, the following epilog decoding will work. */ 00462 00463 /* First decode: 00464 - pop reg [41 58-5f] or [58-5f]. */ 00465 00466 while (1) 00467 { 00468 /* Read opcode. */ 00469 if (target_read_memory (pc, &op, 1) != 0) 00470 return -1; 00471 00472 if (op >= 0x40 && op <= 0x4f) 00473 { 00474 /* REX prefix. */ 00475 rex = op; 00476 00477 /* Read opcode. */ 00478 if (target_read_memory (pc + 1, &op, 1) != 0) 00479 return -1; 00480 } 00481 else 00482 rex = 0; 00483 00484 if (op >= 0x58 && op <= 0x5f) 00485 { 00486 /* pop reg */ 00487 gdb_byte reg = (op & 0x0f) | ((rex & 1) << 3); 00488 00489 cache->prev_reg_addr[amd64_windows_w2gdb_regnum[reg]] = cur_sp; 00490 cur_sp += 8; 00491 } 00492 else 00493 break; 00494 00495 /* Allow the user to break this loop. This shouldn't happen as the 00496 number of consecutive pop should be small. */ 00497 QUIT; 00498 } 00499 00500 /* Then decode the marker. */ 00501 00502 /* Read opcode. */ 00503 if (target_read_memory (pc, &op, 1) != 0) 00504 return -1; 00505 00506 switch (op) 00507 { 00508 case 0xc3: 00509 /* Ret. */ 00510 cache->prev_rip_addr = cur_sp; 00511 cache->prev_sp = cur_sp + 8; 00512 return 1; 00513 00514 case 0xeb: 00515 { 00516 /* jmp rel8 */ 00517 gdb_byte rel8; 00518 CORE_ADDR npc; 00519 00520 if (target_read_memory (pc + 1, &rel8, 1) != 0) 00521 return -1; 00522 npc = pc + 2 + (signed char) rel8; 00523 00524 /* If the jump is within the function, then this is not a marker, 00525 otherwise this is a tail-call. */ 00526 return !pc_in_range (npc, cache); 00527 } 00528 00529 case 0xec: 00530 { 00531 /* jmp rel32 */ 00532 gdb_byte rel32[4]; 00533 CORE_ADDR npc; 00534 00535 if (target_read_memory (pc + 1, rel32, 4) != 0) 00536 return -1; 00537 npc = pc + 5 + extract_signed_integer (rel32, 4, byte_order); 00538 00539 /* If the jump is within the function, then this is not a marker, 00540 otherwise this is a tail-call. */ 00541 return !pc_in_range (npc, cache); 00542 } 00543 00544 case 0xc2: 00545 { 00546 /* ret n */ 00547 gdb_byte imm16[2]; 00548 00549 if (target_read_memory (pc + 1, imm16, 2) != 0) 00550 return -1; 00551 cache->prev_rip_addr = cur_sp; 00552 cache->prev_sp = cur_sp 00553 + extract_unsigned_integer (imm16, 4, byte_order); 00554 return 1; 00555 } 00556 00557 case 0xf3: 00558 { 00559 /* rep; ret */ 00560 gdb_byte op1; 00561 00562 if (target_read_memory (pc + 2, &op1, 1) != 0) 00563 return -1; 00564 if (op1 != 0xc3) 00565 return 0; 00566 00567 cache->prev_rip_addr = cur_sp; 00568 cache->prev_sp = cur_sp + 8; 00569 return 1; 00570 } 00571 00572 case 0x40: 00573 case 0x41: 00574 case 0x42: 00575 case 0x43: 00576 case 0x44: 00577 case 0x45: 00578 case 0x46: 00579 case 0x47: 00580 case 0x48: 00581 case 0x49: 00582 case 0x4a: 00583 case 0x4b: 00584 case 0x4c: 00585 case 0x4d: 00586 case 0x4e: 00587 case 0x4f: 00588 /* Got a REX prefix, read next byte. */ 00589 rex = op; 00590 if (target_read_memory (pc + 1, &op, 1) != 0) 00591 return -1; 00592 00593 if (op == 0xff) 00594 { 00595 /* rex jmp reg */ 00596 gdb_byte op1; 00597 unsigned int reg; 00598 gdb_byte buf[8]; 00599 00600 if (target_read_memory (pc + 2, &op1, 1) != 0) 00601 return -1; 00602 return (op1 & 0xf8) == 0xe0; 00603 } 00604 else 00605 return 0; 00606 00607 default: 00608 /* Not REX, so unknown. */ 00609 return 0; 00610 } 00611 } 00612 00613 /* Decode and execute unwind insns at UNWIND_INFO. */ 00614 00615 static void 00616 amd64_windows_frame_decode_insns (struct frame_info *this_frame, 00617 struct amd64_windows_frame_cache *cache, 00618 CORE_ADDR unwind_info) 00619 { 00620 CORE_ADDR save_addr = 0; 00621 CORE_ADDR cur_sp = cache->sp; 00622 struct gdbarch *gdbarch = get_frame_arch (this_frame); 00623 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00624 int j; 00625 00626 for (j = 0; ; j++) 00627 { 00628 struct external_pex64_unwind_info ex_ui; 00629 /* There are at most 256 16-bit unwind insns. */ 00630 gdb_byte insns[2 * 256]; 00631 gdb_byte *p; 00632 gdb_byte *end_insns; 00633 unsigned char codes_count; 00634 unsigned char frame_reg; 00635 unsigned char frame_off; 00636 00637 /* Read and decode header. */ 00638 if (target_read_memory (cache->image_base + unwind_info, 00639 (gdb_byte *) &ex_ui, sizeof (ex_ui)) != 0) 00640 return; 00641 00642 if (frame_debug) 00643 fprintf_unfiltered 00644 (gdb_stdlog, 00645 "amd64_windows_frame_decodes_insn: " 00646 "%s: ver: %02x, plgsz: %02x, cnt: %02x, frame: %02x\n", 00647 paddress (gdbarch, unwind_info), 00648 ex_ui.Version_Flags, ex_ui.SizeOfPrologue, 00649 ex_ui.CountOfCodes, ex_ui.FrameRegisterOffset); 00650 00651 /* Check version. */ 00652 if (PEX64_UWI_VERSION (ex_ui.Version_Flags) != 1) 00653 return; 00654 00655 if (j == 0 00656 && (cache->pc >= 00657 cache->image_base + cache->start_rva + ex_ui.SizeOfPrologue)) 00658 { 00659 /* Not in the prologue. We want to detect if the PC points to an 00660 epilogue. If so, the epilogue detection+decoding function is 00661 sufficient. Otherwise, the unwinder will consider that the PC 00662 is in the body of the function and will need to decode unwind 00663 info. */ 00664 if (amd64_windows_frame_decode_epilogue (this_frame, cache) == 1) 00665 return; 00666 00667 /* Not in an epilog. Clear possible side effects. */ 00668 memset (cache->prev_reg_addr, 0, sizeof (cache->prev_reg_addr)); 00669 } 00670 00671 codes_count = ex_ui.CountOfCodes; 00672 frame_reg = PEX64_UWI_FRAMEREG (ex_ui.FrameRegisterOffset); 00673 00674 if (frame_reg != 0) 00675 { 00676 /* According to msdn: 00677 If an FP reg is used, then any unwind code taking an offset must 00678 only be used after the FP reg is established in the prolog. */ 00679 gdb_byte buf[8]; 00680 int frreg = amd64_windows_w2gdb_regnum[frame_reg]; 00681 00682 get_frame_register (this_frame, frreg, buf); 00683 save_addr = extract_unsigned_integer (buf, 8, byte_order); 00684 00685 if (frame_debug) 00686 fprintf_unfiltered (gdb_stdlog, " frame_reg=%s, val=%s\n", 00687 gdbarch_register_name (gdbarch, frreg), 00688 paddress (gdbarch, save_addr)); 00689 } 00690 00691 /* Read opcodes. */ 00692 if (codes_count != 0 00693 && target_read_memory (cache->image_base + unwind_info 00694 + sizeof (ex_ui), 00695 insns, codes_count * 2) != 0) 00696 return; 00697 00698 end_insns = &insns[codes_count * 2]; 00699 for (p = insns; p < end_insns; p += 2) 00700 { 00701 int reg; 00702 00703 if (frame_debug) 00704 fprintf_unfiltered 00705 (gdb_stdlog, " op #%u: off=0x%02x, insn=0x%02x\n", 00706 (unsigned) (p - insns), p[0], p[1]); 00707 00708 /* Virtually execute the operation. */ 00709 if (cache->pc >= cache->image_base + cache->start_rva + p[0]) 00710 { 00711 /* If there is no frame registers defined, the current value of 00712 rsp is used instead. */ 00713 if (frame_reg == 0) 00714 save_addr = cur_sp; 00715 00716 switch (PEX64_UNWCODE_CODE (p[1])) 00717 { 00718 case UWOP_PUSH_NONVOL: 00719 /* Push pre-decrements RSP. */ 00720 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])]; 00721 cache->prev_reg_addr[reg] = cur_sp; 00722 cur_sp += 8; 00723 break; 00724 case UWOP_ALLOC_LARGE: 00725 if (PEX64_UNWCODE_INFO (p[1]) == 0) 00726 cur_sp += 00727 8 * extract_unsigned_integer (p + 2, 2, byte_order); 00728 else if (PEX64_UNWCODE_INFO (p[1]) == 1) 00729 cur_sp += extract_unsigned_integer (p + 2, 4, byte_order); 00730 else 00731 return; 00732 break; 00733 case UWOP_ALLOC_SMALL: 00734 cur_sp += 8 + 8 * PEX64_UNWCODE_INFO (p[1]); 00735 break; 00736 case UWOP_SET_FPREG: 00737 cur_sp = save_addr 00738 - PEX64_UWI_FRAMEOFF (ex_ui.FrameRegisterOffset) * 16; 00739 break; 00740 case UWOP_SAVE_NONVOL: 00741 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])]; 00742 cache->prev_reg_addr[reg] = save_addr 00743 - 8 * extract_unsigned_integer (p + 2, 2, byte_order); 00744 break; 00745 case UWOP_SAVE_NONVOL_FAR: 00746 reg = amd64_windows_w2gdb_regnum[PEX64_UNWCODE_INFO (p[1])]; 00747 cache->prev_reg_addr[reg] = save_addr 00748 - 8 * extract_unsigned_integer (p + 2, 4, byte_order); 00749 break; 00750 case UWOP_SAVE_XMM128: 00751 cache->prev_xmm_addr[PEX64_UNWCODE_INFO (p[1])] = 00752 save_addr 00753 - 16 * extract_unsigned_integer (p + 2, 2, byte_order); 00754 break; 00755 case UWOP_SAVE_XMM128_FAR: 00756 cache->prev_xmm_addr[PEX64_UNWCODE_INFO (p[1])] = 00757 save_addr 00758 - 16 * extract_unsigned_integer (p + 2, 4, byte_order); 00759 break; 00760 case UWOP_PUSH_MACHFRAME: 00761 if (PEX64_UNWCODE_INFO (p[1]) == 0) 00762 { 00763 cache->prev_rip_addr = cur_sp + 0; 00764 cache->prev_rsp_addr = cur_sp + 24; 00765 cur_sp += 40; 00766 } 00767 else if (PEX64_UNWCODE_INFO (p[1]) == 1) 00768 { 00769 cache->prev_rip_addr = cur_sp + 8; 00770 cache->prev_rsp_addr = cur_sp + 32; 00771 cur_sp += 48; 00772 } 00773 else 00774 return; 00775 break; 00776 default: 00777 return; 00778 } 00779 } 00780 00781 /* Adjust with the length of the opcode. */ 00782 switch (PEX64_UNWCODE_CODE (p[1])) 00783 { 00784 case UWOP_PUSH_NONVOL: 00785 case UWOP_ALLOC_SMALL: 00786 case UWOP_SET_FPREG: 00787 case UWOP_PUSH_MACHFRAME: 00788 break; 00789 case UWOP_ALLOC_LARGE: 00790 if (PEX64_UNWCODE_INFO (p[1]) == 0) 00791 p += 2; 00792 else if (PEX64_UNWCODE_INFO (p[1]) == 1) 00793 p += 4; 00794 else 00795 return; 00796 break; 00797 case UWOP_SAVE_NONVOL: 00798 case UWOP_SAVE_XMM128: 00799 p += 2; 00800 break; 00801 case UWOP_SAVE_NONVOL_FAR: 00802 case UWOP_SAVE_XMM128_FAR: 00803 p += 4; 00804 break; 00805 default: 00806 return; 00807 } 00808 } 00809 if (PEX64_UWI_FLAGS (ex_ui.Version_Flags) != UNW_FLAG_CHAININFO) 00810 break; 00811 else 00812 { 00813 /* Read the chained unwind info. */ 00814 struct external_pex64_runtime_function d; 00815 CORE_ADDR chain_vma; 00816 00817 chain_vma = cache->image_base + unwind_info 00818 + sizeof (ex_ui) + ((codes_count + 1) & ~1) * 2 + 8; 00819 00820 if (target_read_memory (chain_vma, (gdb_byte *) &d, sizeof (d)) != 0) 00821 return; 00822 00823 cache->start_rva = 00824 extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order); 00825 cache->end_rva = 00826 extract_unsigned_integer (d.rva_EndAddress, 4, byte_order); 00827 unwind_info = 00828 extract_unsigned_integer (d.rva_UnwindData, 4, byte_order); 00829 } 00830 00831 /* Allow the user to break this loop. */ 00832 QUIT; 00833 } 00834 /* PC is saved by the call. */ 00835 if (cache->prev_rip_addr == 0) 00836 cache->prev_rip_addr = cur_sp; 00837 cache->prev_sp = cur_sp + 8; 00838 00839 if (frame_debug) 00840 fprintf_unfiltered (gdb_stdlog, " prev_sp: %s, prev_pc @%s\n", 00841 paddress (gdbarch, cache->prev_sp), 00842 paddress (gdbarch, cache->prev_rip_addr)); 00843 } 00844 00845 /* Find SEH unwind info for PC, returning 0 on success. 00846 00847 UNWIND_INFO is set to the rva of unwind info address, IMAGE_BASE 00848 to the base address of the corresponding image, and START_RVA 00849 to the rva of the function containing PC. */ 00850 00851 static int 00852 amd64_windows_find_unwind_info (struct gdbarch *gdbarch, CORE_ADDR pc, 00853 CORE_ADDR *unwind_info, 00854 CORE_ADDR *image_base, 00855 CORE_ADDR *start_rva, 00856 CORE_ADDR *end_rva) 00857 { 00858 struct obj_section *sec; 00859 pe_data_type *pe; 00860 IMAGE_DATA_DIRECTORY *dir; 00861 struct objfile *objfile; 00862 unsigned long lo, hi; 00863 CORE_ADDR base; 00864 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00865 00866 /* Get the corresponding exception directory. */ 00867 sec = find_pc_section (pc); 00868 if (sec == NULL) 00869 return -1; 00870 objfile = sec->objfile; 00871 pe = pe_data (sec->objfile->obfd); 00872 dir = &pe->pe_opthdr.DataDirectory[PE_EXCEPTION_TABLE]; 00873 00874 base = pe->pe_opthdr.ImageBase 00875 + ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)); 00876 *image_base = base; 00877 00878 /* Find the entry. 00879 00880 Note: This does not handle dynamically added entries (for JIT 00881 engines). For this, we would need to ask the kernel directly, 00882 which means getting some info from the native layer. For the 00883 rest of the code, however, it's probably faster to search 00884 the entry ourselves. */ 00885 lo = 0; 00886 hi = dir->Size / sizeof (struct external_pex64_runtime_function); 00887 *unwind_info = 0; 00888 while (lo <= hi) 00889 { 00890 unsigned long mid = lo + (hi - lo) / 2; 00891 struct external_pex64_runtime_function d; 00892 CORE_ADDR sa, ea; 00893 00894 if (target_read_memory (base + dir->VirtualAddress + mid * sizeof (d), 00895 (gdb_byte *) &d, sizeof (d)) != 0) 00896 return -1; 00897 00898 sa = extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order); 00899 ea = extract_unsigned_integer (d.rva_EndAddress, 4, byte_order); 00900 if (pc < base + sa) 00901 hi = mid - 1; 00902 else if (pc >= base + ea) 00903 lo = mid + 1; 00904 else if (pc >= base + sa && pc < base + ea) 00905 { 00906 /* Got it. */ 00907 *start_rva = sa; 00908 *end_rva = ea; 00909 *unwind_info = 00910 extract_unsigned_integer (d.rva_UnwindData, 4, byte_order); 00911 break; 00912 } 00913 else 00914 break; 00915 } 00916 00917 if (frame_debug) 00918 fprintf_unfiltered 00919 (gdb_stdlog, 00920 "amd64_windows_find_unwind_data: image_base=%s, unwind_data=%s\n", 00921 paddress (gdbarch, base), paddress (gdbarch, *unwind_info)); 00922 00923 if (*unwind_info & 1) 00924 { 00925 /* Unofficially documented unwind info redirection, when UNWIND_INFO 00926 address is odd (http://www.codemachine.com/article_x64deepdive.html). 00927 */ 00928 struct external_pex64_runtime_function d; 00929 CORE_ADDR sa, ea; 00930 00931 if (target_read_memory (base + (*unwind_info & ~1), 00932 (gdb_byte *) &d, sizeof (d)) != 0) 00933 return -1; 00934 00935 *start_rva = 00936 extract_unsigned_integer (d.rva_BeginAddress, 4, byte_order); 00937 *end_rva = extract_unsigned_integer (d.rva_EndAddress, 4, byte_order); 00938 *unwind_info = 00939 extract_unsigned_integer (d.rva_UnwindData, 4, byte_order); 00940 00941 } 00942 return 0; 00943 } 00944 00945 /* Fill THIS_CACHE using the native amd64-windows unwinding data 00946 for THIS_FRAME. */ 00947 00948 static struct amd64_windows_frame_cache * 00949 amd64_windows_frame_cache (struct frame_info *this_frame, void **this_cache) 00950 { 00951 struct gdbarch *gdbarch = get_frame_arch (this_frame); 00952 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00953 struct amd64_windows_frame_cache *cache; 00954 gdb_byte buf[8]; 00955 struct obj_section *sec; 00956 pe_data_type *pe; 00957 IMAGE_DATA_DIRECTORY *dir; 00958 CORE_ADDR image_base; 00959 CORE_ADDR pc; 00960 struct objfile *objfile; 00961 unsigned long lo, hi; 00962 CORE_ADDR unwind_info = 0; 00963 00964 if (*this_cache) 00965 return *this_cache; 00966 00967 cache = FRAME_OBSTACK_ZALLOC (struct amd64_windows_frame_cache); 00968 *this_cache = cache; 00969 00970 /* Get current PC and SP. */ 00971 pc = get_frame_pc (this_frame); 00972 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf); 00973 cache->sp = extract_unsigned_integer (buf, 8, byte_order); 00974 cache->pc = pc; 00975 00976 if (amd64_windows_find_unwind_info (gdbarch, pc, &unwind_info, 00977 &cache->image_base, 00978 &cache->start_rva, 00979 &cache->end_rva)) 00980 return cache; 00981 00982 if (unwind_info == 0) 00983 { 00984 /* Assume a leaf function. */ 00985 cache->prev_sp = cache->sp + 8; 00986 cache->prev_rip_addr = cache->sp; 00987 } 00988 else 00989 { 00990 /* Decode unwind insns to compute saved addresses. */ 00991 amd64_windows_frame_decode_insns (this_frame, cache, unwind_info); 00992 } 00993 return cache; 00994 } 00995 00996 /* Implement the "prev_register" method of struct frame_unwind 00997 using the standard Windows x64 SEH info. */ 00998 00999 static struct value * 01000 amd64_windows_frame_prev_register (struct frame_info *this_frame, 01001 void **this_cache, int regnum) 01002 { 01003 struct gdbarch *gdbarch = get_frame_arch (this_frame); 01004 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 01005 struct amd64_windows_frame_cache *cache = 01006 amd64_windows_frame_cache (this_frame, this_cache); 01007 struct value *val; 01008 CORE_ADDR prev; 01009 01010 if (frame_debug) 01011 fprintf_unfiltered (gdb_stdlog, 01012 "amd64_windows_frame_prev_register %s for sp=%s\n", 01013 gdbarch_register_name (gdbarch, regnum), 01014 paddress (gdbarch, cache->prev_sp)); 01015 01016 if (regnum >= AMD64_XMM0_REGNUM && regnum <= AMD64_XMM0_REGNUM + 15) 01017 prev = cache->prev_xmm_addr[regnum - AMD64_XMM0_REGNUM]; 01018 else if (regnum == AMD64_RSP_REGNUM) 01019 { 01020 prev = cache->prev_rsp_addr; 01021 if (prev == 0) 01022 return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp); 01023 } 01024 else if (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_R15_REGNUM) 01025 prev = cache->prev_reg_addr[regnum - AMD64_RAX_REGNUM]; 01026 else if (regnum == AMD64_RIP_REGNUM) 01027 prev = cache->prev_rip_addr; 01028 else 01029 prev = 0; 01030 01031 if (prev && frame_debug) 01032 fprintf_unfiltered (gdb_stdlog, " -> at %s\n", paddress (gdbarch, prev)); 01033 01034 if (prev) 01035 { 01036 /* Register was saved. */ 01037 return frame_unwind_got_memory (this_frame, regnum, prev); 01038 } 01039 else 01040 { 01041 /* Register is either volatile or not modified. */ 01042 return frame_unwind_got_register (this_frame, regnum, regnum); 01043 } 01044 } 01045 01046 /* Implement the "this_id" method of struct frame_unwind using 01047 the standard Windows x64 SEH info. */ 01048 01049 static void 01050 amd64_windows_frame_this_id (struct frame_info *this_frame, void **this_cache, 01051 struct frame_id *this_id) 01052 { 01053 struct gdbarch *gdbarch = get_frame_arch (this_frame); 01054 struct amd64_windows_frame_cache *cache = 01055 amd64_windows_frame_cache (this_frame, this_cache); 01056 01057 *this_id = frame_id_build (cache->prev_sp, 01058 cache->image_base + cache->start_rva); 01059 } 01060 01061 /* Windows x64 SEH unwinder. */ 01062 01063 static const struct frame_unwind amd64_windows_frame_unwind = 01064 { 01065 NORMAL_FRAME, 01066 default_frame_unwind_stop_reason, 01067 &amd64_windows_frame_this_id, 01068 &amd64_windows_frame_prev_register, 01069 NULL, 01070 default_frame_sniffer 01071 }; 01072 01073 /* Implement the "skip_prologue" gdbarch method. */ 01074 01075 static CORE_ADDR 01076 amd64_windows_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc) 01077 { 01078 CORE_ADDR func_addr; 01079 CORE_ADDR unwind_info = 0; 01080 CORE_ADDR image_base, start_rva, end_rva; 01081 struct external_pex64_unwind_info ex_ui; 01082 01083 /* Use prologue size from unwind info. */ 01084 if (amd64_windows_find_unwind_info (gdbarch, pc, &unwind_info, 01085 &image_base, &start_rva, &end_rva) == 0) 01086 { 01087 if (unwind_info == 0) 01088 { 01089 /* Leaf function. */ 01090 return pc; 01091 } 01092 else if (target_read_memory (image_base + unwind_info, 01093 (gdb_byte *) &ex_ui, sizeof (ex_ui)) == 0 01094 && PEX64_UWI_VERSION (ex_ui.Version_Flags) == 1) 01095 return max (pc, image_base + start_rva + ex_ui.SizeOfPrologue); 01096 } 01097 01098 /* See if we can determine the end of the prologue via the symbol 01099 table. If so, then return either the PC, or the PC after 01100 the prologue, whichever is greater. */ 01101 if (find_pc_partial_function (pc, NULL, &func_addr, NULL)) 01102 { 01103 CORE_ADDR post_prologue_pc 01104 = skip_prologue_using_sal (gdbarch, func_addr); 01105 01106 if (post_prologue_pc != 0) 01107 return max (pc, post_prologue_pc); 01108 } 01109 01110 return pc; 01111 } 01112 01113 /* Check Win64 DLL jmp trampolines and find jump destination. */ 01114 01115 static CORE_ADDR 01116 amd64_windows_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc) 01117 { 01118 CORE_ADDR destination = 0; 01119 struct gdbarch *gdbarch = get_frame_arch (frame); 01120 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 01121 01122 /* Check for jmp *<offset>(%rip) (jump near, absolute indirect (/4)). */ 01123 if (pc && read_memory_unsigned_integer (pc, 2, byte_order) == 0x25ff) 01124 { 01125 /* Get opcode offset and see if we can find a reference in our data. */ 01126 ULONGEST offset 01127 = read_memory_unsigned_integer (pc + 2, 4, byte_order); 01128 01129 /* Get address of function pointer at end of pc. */ 01130 CORE_ADDR indirect_addr = pc + offset + 6; 01131 01132 struct minimal_symbol *indsym 01133 = (indirect_addr 01134 ? lookup_minimal_symbol_by_pc (indirect_addr).minsym 01135 : NULL); 01136 const char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : NULL; 01137 01138 if (symname) 01139 { 01140 if (strncmp (symname, "__imp_", 6) == 0 01141 || strncmp (symname, "_imp_", 5) == 0) 01142 destination 01143 = read_memory_unsigned_integer (indirect_addr, 8, byte_order); 01144 } 01145 } 01146 01147 return destination; 01148 } 01149 01150 /* Implement the "auto_wide_charset" gdbarch method. */ 01151 01152 static const char * 01153 amd64_windows_auto_wide_charset (void) 01154 { 01155 return "UTF-16"; 01156 } 01157 01158 static void 01159 amd64_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) 01160 { 01161 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); 01162 01163 /* The dwarf2 unwinder (appended very early by i386_gdbarch_init) is 01164 preferred over the SEH one. The reasons are: 01165 - binaries without SEH but with dwarf2 debug info are correcly handled 01166 (although they aren't ABI compliant, gcc before 4.7 didn't emit SEH 01167 info). 01168 - dwarf3 DW_OP_call_frame_cfa is correctly handled (it can only be 01169 handled if the dwarf2 unwinder is used). 01170 01171 The call to amd64_init_abi appends default unwinders, that aren't 01172 compatible with the SEH one. 01173 */ 01174 frame_unwind_append_unwinder (gdbarch, &amd64_windows_frame_unwind); 01175 01176 amd64_init_abi (info, gdbarch); 01177 01178 windows_init_abi (info, gdbarch); 01179 01180 /* On Windows, "long"s are only 32bit. */ 01181 set_gdbarch_long_bit (gdbarch, 32); 01182 01183 /* Function calls. */ 01184 set_gdbarch_push_dummy_call (gdbarch, amd64_windows_push_dummy_call); 01185 set_gdbarch_return_value (gdbarch, amd64_windows_return_value); 01186 set_gdbarch_skip_main_prologue (gdbarch, amd64_skip_main_prologue); 01187 set_gdbarch_skip_trampoline_code (gdbarch, 01188 amd64_windows_skip_trampoline_code); 01189 01190 set_gdbarch_skip_prologue (gdbarch, amd64_windows_skip_prologue); 01191 01192 set_gdbarch_auto_wide_charset (gdbarch, amd64_windows_auto_wide_charset); 01193 } 01194 01195 /* -Wmissing-prototypes */ 01196 extern initialize_file_ftype _initialize_amd64_windows_tdep; 01197 01198 void 01199 _initialize_amd64_windows_tdep (void) 01200 { 01201 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_CYGWIN, 01202 amd64_windows_init_abi); 01203 }