Home | History | Annotate | Line # | Download | only in gdb
      1 /* Target-dependent code for the GNU Hurd.
      2    Copyright (C) 2002-2024 Free Software Foundation, Inc.
      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
      8    the Free Software Foundation; either version 3 of the License, or
      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
     17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18 
     19 #include "extract-store-integer.h"
     20 #include "gdbcore.h"
     21 #include "osabi.h"
     22 #include "solib-svr4.h"
     23 
     24 #include "i386-tdep.h"
     25 
     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 (const frame_info_ptr &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,
     66 				 buf))
     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 (const frame_info_ptr &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 (const frame_info_ptr &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 
    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 
    173 static void
    174 i386gnu_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
    175 {
    176   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
    177 
    178   /* GNU uses ELF.  */
    179   i386_elf_init_abi (info, gdbarch);
    180 
    181   set_solib_svr4_fetch_link_map_offsets
    182     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
    183 
    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 
    188   tdep->jb_pc_offset = 20;	/* From <bits/setjmp.h>.  */
    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);
    194 }
    195 
    196 void _initialize_i386gnu_tdep ();
    197 void
    198 _initialize_i386gnu_tdep ()
    199 {
    200   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_HURD, i386gnu_init_abi);
    201 }
    202