* elflink.h (elf_bfd_final_link): Check if dynobj is not NULL
[binutils-gdb.git] / gdb / x86-64-linux-tdep.c
1 /* Target-dependent code for Linux running on x86-64, for GDB.
2 Copyright 2001
3 Free Software Foundation, Inc.
4 Contributed by Jiri Smid, SuSE Labs.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "gdbcore.h"
26 #include "regcache.h"
27 #include "x86-64-tdep.h"
28 #include "dwarf2cfi.h"
29
30 #define LINUX_SIGTRAMP_INSN0 (0x48) /* mov $NNNNNNNN,%rax */
31 #define LINUX_SIGTRAMP_OFFSET0 (0)
32 #define LINUX_SIGTRAMP_INSN1 (0x0f) /* syscall */
33 #define LINUX_SIGTRAMP_OFFSET1 (7)
34
35 static const unsigned char linux_sigtramp_code[] = {
36 LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x89, 0x00, 0x00, 0x00, /* mov $0x89,%rax */
37 LINUX_SIGTRAMP_INSN1, 0x05 /* syscall */
38 };
39
40 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
41
42 /* If PC is in a sigtramp routine, return the address of the start of
43 the routine. Otherwise, return 0. */
44
45 static CORE_ADDR
46 x86_64_linux_sigtramp_start (CORE_ADDR pc)
47 {
48 unsigned char buf[LINUX_SIGTRAMP_LEN];
49 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
50 return 0;
51
52 if (buf[0] != LINUX_SIGTRAMP_INSN0)
53 {
54 if (buf[0] != LINUX_SIGTRAMP_INSN1)
55 return 0;
56
57 pc -= LINUX_SIGTRAMP_OFFSET1;
58
59 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
60 return 0;
61 }
62
63 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
64 return 0;
65
66 return pc;
67 }
68
69 #define LINUX_SIGINFO_SIZE 128
70
71 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
72 #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36)
73
74 /* Assuming FRAME is for a Linux sigtramp routine, return the address
75 of the associated sigcontext structure. */
76 CORE_ADDR
77 x86_64_linux_sigcontext_addr (struct frame_info *frame)
78 {
79 CORE_ADDR pc;
80
81 pc = x86_64_linux_sigtramp_start (frame->pc);
82 if (pc)
83 {
84 if (frame->next)
85 /* If this isn't the top frame, the next frame must be for the
86 signal handler itself. The sigcontext structure is part of
87 the user context. */
88 return frame->next->frame + LINUX_SIGINFO_SIZE +
89 LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
90
91
92 /* This is the top frame. */
93 return read_register (SP_REGNUM) + LINUX_SIGINFO_SIZE +
94 LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
95
96 }
97
98 error ("Couldn't recognize signal trampoline.");
99 return 0;
100 }
101
102 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
103 #define LINUX_SIGCONTEXT_PC_OFFSET (136)
104
105 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
106 program counter. */
107
108 CORE_ADDR
109 x86_64_linux_sigtramp_saved_pc (struct frame_info *frame)
110 {
111 CORE_ADDR addr;
112
113 addr = x86_64_linux_sigcontext_addr (frame);
114 return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 8);
115 }
116
117 /* Immediately after a function call, return the saved pc. */
118
119 CORE_ADDR
120 x86_64_linux_saved_pc_after_call (struct frame_info *frame)
121 {
122 if (frame->signal_handler_caller)
123 return x86_64_linux_sigtramp_saved_pc (frame);
124
125 return read_memory_integer (read_register (SP_REGNUM), 8);
126 }
127
128 /* Saved Pc. Get it from sigcontext if within sigtramp. */
129 CORE_ADDR
130 x86_64_linux_frame_saved_pc (struct frame_info *frame)
131 {
132 if (frame->signal_handler_caller)
133 return x86_64_linux_sigtramp_saved_pc (frame);
134 return cfi_get_ra (frame);
135 }