i386-gnu-tdep.c 5.5 KB

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