blob: 51c253d2dfe684517d870035e7af08c53622213f [file] [log] [blame]
Mark Kettenise0ca2bb2002-08-15 22:24:01 +00001/* Target-dependent code for the GNU Hurd.
Joel Brobecker4a94e362022-01-01 18:56:03 +04002 Copyright (C) 2002-2022 Free Software Foundation, Inc.
Mark Kettenise0ca2bb2002-08-15 22:24:01 +00003
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
Joel Brobeckera9762ec2007-08-23 18:08:50 +00008 the Free Software Foundation; either version 3 of the License, or
Mark Kettenise0ca2bb2002-08-15 22:24:01 +00009 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
Joel Brobeckera9762ec2007-08-23 18:08:50 +000017 along with this program. If not, see <http://www.gnu.org/licenses/>. */
Mark Kettenise0ca2bb2002-08-15 22:24:01 +000018
19#include "defs.h"
Samuel Thibault0af5e102020-05-30 18:45:30 +000020#include "gdbcore.h"
Daniel Jacobowitz4be87832003-01-04 23:38:46 +000021#include "osabi.h"
Ulrich Weigand8d005782007-10-24 21:21:16 +000022#include "solib-svr4.h"
Mark Kettenise0ca2bb2002-08-15 22:24:01 +000023
24#include "i386-tdep.h"
25
Samuel Thibault0af5e102020-05-30 18:45:30 +000026/* Recognizing signal handler frames. */
27
28/* When the GNU/Hurd libc calls a signal handler, the return address points
29 inside the trampoline assembly snippet.
30
31 If the trampoline function name can not be identified, we resort to reading
32 memory from the process in order to identify it. */
33
34static const gdb_byte gnu_sigtramp_code[] =
35{
36/* rpc_wait_trampoline: */
37 0xb8, 0xe7, 0xff, 0xff, 0xff, /* mov $-25,%eax */
38 0x9a, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, /* lcall $7,$0 */
39 0x89, 0x01, /* movl %eax, (%ecx) */
40 0x89, 0xdc, /* movl %ebx, %esp */
41
42/* trampoline: */
43 0xff, 0xd2, /* call *%edx */
44/* RA HERE */
45 0x83, 0xc4, 0x0c, /* addl $12, %esp */
46 0xc3, /* ret */
47
48/* firewall: */
49 0xf4, /* hlt */
50};
51
52#define GNU_SIGTRAMP_LEN (sizeof gnu_sigtramp_code)
53#define GNU_SIGTRAMP_TAIL 5 /* length of tail after RA */
54
55/* If THIS_FRAME is a sigtramp routine, return the address of the
56 start of the routine. Otherwise, return 0. */
57
58static CORE_ADDR
59i386_gnu_sigtramp_start (struct frame_info *this_frame)
60{
61 CORE_ADDR pc = get_frame_pc (this_frame);
62 gdb_byte buf[GNU_SIGTRAMP_LEN];
63
64 if (!safe_frame_unwind_memory (this_frame,
65 pc + GNU_SIGTRAMP_TAIL - GNU_SIGTRAMP_LEN,
Luis Machadobdec2912021-01-15 13:16:04 -030066 buf))
Samuel Thibault0af5e102020-05-30 18:45:30 +000067 return 0;
68
69 if (memcmp (buf, gnu_sigtramp_code, GNU_SIGTRAMP_LEN) != 0)
70 return 0;
71
72 return pc;
73}
74
75/* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
76 routine. */
77
78static int
79i386_gnu_sigtramp_p (struct frame_info *this_frame)
80{
81 CORE_ADDR pc = get_frame_pc (this_frame);
82 const char *name;
83
84 find_pc_partial_function (pc, &name, NULL, NULL);
85
86 /* If we have a NAME, we can check for the trampoline function */
87 if (name != NULL && strcmp (name, "trampoline") == 0)
88 return 1;
89
90 return i386_gnu_sigtramp_start (this_frame) != 0;
91}
92
93/* Offset to sc_i386_thread_state in sigcontext, from <bits/sigcontext.h>. */
94#define I386_GNU_SIGCONTEXT_THREAD_STATE_OFFSET 20
95
96/* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
97 address of the associated sigcontext structure. */
98
99static CORE_ADDR
100i386_gnu_sigcontext_addr (struct frame_info *this_frame)
101{
102 struct gdbarch *gdbarch = get_frame_arch (this_frame);
103 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
104 CORE_ADDR pc;
105 CORE_ADDR sp;
106 gdb_byte buf[4];
107
108 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
109 sp = extract_unsigned_integer (buf, 4, byte_order);
110
111 pc = i386_gnu_sigtramp_start (this_frame);
112 if (pc)
113 {
114 CORE_ADDR sigcontext_addr;
115
116 /* The sigcontext structure address is passed as the third argument to
117 the signal handler. */
118 read_memory (sp + 8, buf, 4);
119 sigcontext_addr = extract_unsigned_integer (buf, 4, byte_order);
120 return sigcontext_addr + I386_GNU_SIGCONTEXT_THREAD_STATE_OFFSET;
121 }
122
123 error (_("Couldn't recognize signal trampoline."));
124 return 0;
125}
126
127/* Mapping between the general-purpose registers in `struct
128 sigcontext' format (starting at sc_i386_thread_state)
129 and GDB's register cache layout. */
130
131/* From <bits/sigcontext.h>. */
132static int i386_gnu_sc_reg_offset[] =
133{
134 11 * 4, /* %eax */
135 10 * 4, /* %ecx */
136 9 * 4, /* %edx */
137 8 * 4, /* %ebx */
138 7 * 4, /* %esp */
139 6 * 4, /* %ebp */
140 5 * 4, /* %esi */
141 4 * 4, /* %edi */
142 12 * 4, /* %eip */
143 14 * 4, /* %eflags */
144 13 * 4, /* %cs */
145 16 * 4, /* %ss */
146 3 * 4, /* %ds */
147 2 * 4, /* %es */
148 1 * 4, /* %fs */
149 0 * 4 /* %gs */
150};
151
Ulrich Weigandac3d87c2014-12-01 13:42:41 +0100152/* From <sys/ucontext.h>. */
153static int i386gnu_gregset_reg_offset[] =
154{
155 11 * 4, /* %eax */
156 10 * 4, /* %ecx */
157 9 * 4, /* %edx */
158 8 * 4, /* %ebx */
159 17 * 4, /* %uesp */
160 6 * 4, /* %ebp */
161 5 * 4, /* %esi */
162 4 * 4, /* %edi */
163 14 * 4, /* %eip */
164 16 * 4, /* %efl */
165 15 * 4, /* %cs */
166 18 * 4, /* %ss */
167 3 * 4, /* %ds */
168 2 * 4, /* %es */
169 1 * 4, /* %fs */
170 0 * 4, /* %gs */
171};
172
Mark Kettenise0ca2bb2002-08-15 22:24:01 +0000173static void
174i386gnu_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
175{
Simon Marchi345bd072021-11-15 11:29:39 -0500176 i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
Mark Kettenise0ca2bb2002-08-15 22:24:01 +0000177
178 /* GNU uses ELF. */
179 i386_elf_init_abi (info, gdbarch);
180
Ulrich Weigand8d005782007-10-24 21:21:16 +0000181 set_solib_svr4_fetch_link_map_offsets
182 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
183
Ulrich Weigandac3d87c2014-12-01 13:42:41 +0100184 tdep->gregset_reg_offset = i386gnu_gregset_reg_offset;
185 tdep->gregset_num_regs = ARRAY_SIZE (i386gnu_gregset_reg_offset);
186 tdep->sizeof_gregset = 19 * 4;
187
Mark Kettenise0ca2bb2002-08-15 22:24:01 +0000188 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
Samuel Thibault0af5e102020-05-30 18:45:30 +0000189
190 tdep->sigtramp_p = i386_gnu_sigtramp_p;
191 tdep->sigcontext_addr = i386_gnu_sigcontext_addr;
192 tdep->sc_reg_offset = i386_gnu_sc_reg_offset;
193 tdep->sc_num_regs = ARRAY_SIZE (i386_gnu_sc_reg_offset);
Mark Kettenise0ca2bb2002-08-15 22:24:01 +0000194}
195
Simon Marchi6c265982020-01-13 14:01:38 -0500196void _initialize_i386gnu_tdep ();
Mark Kettenise0ca2bb2002-08-15 22:24:01 +0000197void
Simon Marchi6c265982020-01-13 14:01:38 -0500198_initialize_i386gnu_tdep ()
Mark Kettenise0ca2bb2002-08-15 22:24:01 +0000199{
Mark Kettenis05816f72002-12-21 19:58:07 +0000200 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_HURD, i386gnu_init_abi);
Mark Kettenise0ca2bb2002-08-15 22:24:01 +0000201}