Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 1 | /* Target-dependent code for the GNU Hurd. |
Joel Brobecker | 4a94e36 | 2022-01-01 18:56:03 +0400 | [diff] [blame] | 2 | Copyright (C) 2002-2022 Free Software Foundation, Inc. |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 3 | |
| 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 Brobecker | a9762ec | 2007-08-23 18:08:50 +0000 | [diff] [blame] | 8 | the Free Software Foundation; either version 3 of the License, or |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 9 | (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 Brobecker | a9762ec | 2007-08-23 18:08:50 +0000 | [diff] [blame] | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 18 | |
| 19 | #include "defs.h" |
Samuel Thibault | 0af5e10 | 2020-05-30 18:45:30 +0000 | [diff] [blame] | 20 | #include "gdbcore.h" |
Daniel Jacobowitz | 4be8783 | 2003-01-04 23:38:46 +0000 | [diff] [blame] | 21 | #include "osabi.h" |
Ulrich Weigand | 8d00578 | 2007-10-24 21:21:16 +0000 | [diff] [blame] | 22 | #include "solib-svr4.h" |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 23 | |
| 24 | #include "i386-tdep.h" |
| 25 | |
Samuel Thibault | 0af5e10 | 2020-05-30 18:45:30 +0000 | [diff] [blame] | 26 | /* 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 | |
| 34 | static 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 | |
| 58 | static CORE_ADDR |
| 59 | i386_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 Machado | bdec291 | 2021-01-15 13:16:04 -0300 | [diff] [blame] | 66 | buf)) |
Samuel Thibault | 0af5e10 | 2020-05-30 18:45:30 +0000 | [diff] [blame] | 67 | 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 | |
| 78 | static int |
| 79 | i386_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 | |
| 99 | static CORE_ADDR |
| 100 | i386_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>. */ |
| 132 | static 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 Weigand | ac3d87c | 2014-12-01 13:42:41 +0100 | [diff] [blame] | 152 | /* From <sys/ucontext.h>. */ |
| 153 | static 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 Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 173 | static void |
| 174 | i386gnu_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) |
| 175 | { |
Simon Marchi | 345bd07 | 2021-11-15 11:29:39 -0500 | [diff] [blame] | 176 | i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch); |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 177 | |
| 178 | /* GNU uses ELF. */ |
| 179 | i386_elf_init_abi (info, gdbarch); |
| 180 | |
Ulrich Weigand | 8d00578 | 2007-10-24 21:21:16 +0000 | [diff] [blame] | 181 | set_solib_svr4_fetch_link_map_offsets |
| 182 | (gdbarch, svr4_ilp32_fetch_link_map_offsets); |
| 183 | |
Ulrich Weigand | ac3d87c | 2014-12-01 13:42:41 +0100 | [diff] [blame] | 184 | 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 Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 188 | tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */ |
Samuel Thibault | 0af5e10 | 2020-05-30 18:45:30 +0000 | [diff] [blame] | 189 | |
| 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 Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Simon Marchi | 6c26598 | 2020-01-13 14:01:38 -0500 | [diff] [blame] | 196 | void _initialize_i386gnu_tdep (); |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 197 | void |
Simon Marchi | 6c26598 | 2020-01-13 14:01:38 -0500 | [diff] [blame] | 198 | _initialize_i386gnu_tdep () |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 199 | { |
Mark Kettenis | 05816f7 | 2002-12-21 19:58:07 +0000 | [diff] [blame] | 200 | gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_HURD, i386gnu_init_abi); |
Mark Kettenis | e0ca2bb | 2002-08-15 22:24:01 +0000 | [diff] [blame] | 201 | } |