Fix crash when exiting TUI with gdb -tui
[deliverable/binutils-gdb.git] / gdb / i386-windows-tdep.c
... / ...
CommitLineData
1/* Target-dependent code for Windows (including Cygwin) running on i386's,
2 for GDB.
3
4 Copyright (C) 2003-2020 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
20
21#include "defs.h"
22#include "osabi.h"
23#include "i386-tdep.h"
24#include "windows-tdep.h"
25#include "regset.h"
26#include "gdb_obstack.h"
27#include "xml-support.h"
28#include "gdbcore.h"
29#include "inferior.h"
30
31/* Core file support. */
32
33/* This vector maps GDB's idea of a register's number into an address
34 in the windows exception context vector. */
35
36static int i386_windows_gregset_reg_offset[] =
37{
38 176, /* eax */
39 172, /* ecx */
40 168, /* edx */
41 164, /* ebx */
42
43 196, /* esp */
44 180, /* ebp */
45 160, /* esi */
46 156, /* edi */
47
48 184, /* eip */
49 192, /* eflags */
50 188, /* cs */
51 200, /* ss */
52
53 152, /* ds */
54 148, /* es */
55 144, /* fs */
56 140, /* gs */
57
58 56, /* FloatSave.RegisterArea[0 * 10] */
59 66, /* FloatSave.RegisterArea[1 * 10] */
60 76, /* FloatSave.RegisterArea[2 * 10] */
61 86, /* FloatSave.RegisterArea[3 * 10] */
62 96, /* FloatSave.RegisterArea[4 * 10] */
63 106, /* FloatSave.RegisterArea[5 * 10] */
64 116, /* FloatSave.RegisterArea[6 * 10] */
65 126, /* FloatSave.RegisterArea[7 * 10] */
66
67 28, /* FloatSave.ControlWord */
68 32, /* FloatSave.StatusWord */
69 36, /* FloatSave.TagWord */
70 44, /* FloatSave.ErrorSelector */
71 40, /* FloatSave.ErrorOffset */
72 52, /* FloatSave.DataSelector */
73 48, /* FloatSave.DataOffset */
74 44, /* FloatSave.ErrorSelector */
75
76 /* XMM0-7 */
77 364, /* ExtendedRegisters[10*16] */
78 380, /* ExtendedRegisters[11*16] */
79 396, /* ExtendedRegisters[12*16] */
80 412, /* ExtendedRegisters[13*16] */
81 428, /* ExtendedRegisters[14*16] */
82 444, /* ExtendedRegisters[15*16] */
83 460, /* ExtendedRegisters[16*16] */
84 476, /* ExtendedRegisters[17*16] */
85
86 /* MXCSR */
87 228 /* ExtendedRegisters[24] */
88};
89
90#define I386_WINDOWS_SIZEOF_GREGSET 716
91
92struct cpms_data
93{
94 struct gdbarch *gdbarch;
95 struct obstack *obstack;
96 int module_count;
97};
98
99static void
100core_process_module_section (bfd *abfd, asection *sect, void *obj)
101{
102 struct cpms_data *data = (struct cpms_data *) obj;
103 enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
104
105 char *module_name;
106 size_t module_name_size;
107 CORE_ADDR base_addr;
108
109 gdb_byte *buf = NULL;
110
111 if (!startswith (sect->name, ".module"))
112 return;
113
114 buf = (gdb_byte *) xmalloc (bfd_section_size (sect) + 1);
115 if (!buf)
116 {
117 printf_unfiltered ("memory allocation failed for %s\n", sect->name);
118 goto out;
119 }
120 if (!bfd_get_section_contents (abfd, sect,
121 buf, 0, bfd_section_size (sect)))
122 goto out;
123
124
125
126 /* A DWORD (data_type) followed by struct windows_core_module_info. */
127
128 base_addr =
129 extract_unsigned_integer (buf + 4, 4, byte_order);
130
131 module_name_size =
132 extract_unsigned_integer (buf + 8, 4, byte_order);
133
134 if (12 + module_name_size > bfd_section_size (sect))
135 goto out;
136 module_name = (char *) buf + 12;
137
138 /* The first module is the .exe itself. */
139 if (data->module_count != 0)
140 windows_xfer_shared_library (module_name, base_addr,
141 NULL, data->gdbarch, data->obstack);
142 data->module_count++;
143
144out:
145 xfree (buf);
146 return;
147}
148
149static ULONGEST
150windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
151 gdb_byte *readbuf,
152 ULONGEST offset, ULONGEST len)
153{
154 struct obstack obstack;
155 const char *buf;
156 ULONGEST len_avail;
157 struct cpms_data data = { gdbarch, &obstack, 0 };
158
159 obstack_init (&obstack);
160 obstack_grow_str (&obstack, "<library-list>\n");
161 bfd_map_over_sections (core_bfd,
162 core_process_module_section,
163 &data);
164 obstack_grow_str0 (&obstack, "</library-list>\n");
165
166 buf = (const char *) obstack_finish (&obstack);
167 len_avail = strlen (buf);
168 if (offset >= len_avail)
169 return 0;
170
171 if (len > len_avail - offset)
172 len = len_avail - offset;
173 memcpy (readbuf, buf + offset, len);
174
175 obstack_free (&obstack, NULL);
176 return len;
177}
178
179/* This is how we want PTIDs from core files to be printed. */
180
181static std::string
182i386_windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
183{
184 if (ptid.lwp () != 0)
185 return string_printf ("Thread 0x%lx", ptid.lwp ());
186
187 return normal_pid_to_str (ptid);
188}
189
190static CORE_ADDR
191i386_windows_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
192{
193 return i386_pe_skip_trampoline_code (frame, pc, NULL);
194}
195
196static const char *
197i386_windows_auto_wide_charset (void)
198{
199 return "UTF-16";
200}
201
202/* Implement the "push_dummy_call" gdbarch method. */
203
204static CORE_ADDR
205i386_windows_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
206 struct regcache *regcache, CORE_ADDR bp_addr,
207 int nargs, struct value **args, CORE_ADDR sp,
208 function_call_return_method return_method,
209 CORE_ADDR struct_addr)
210{
211 /* For non-static member functions of 32bit Windows programs, the thiscall
212 calling convention is used, so the 'this' pointer is passed in ECX. */
213 bool thiscall = false;
214
215 struct type *type = check_typedef (value_type (function));
216 if (type->code () == TYPE_CODE_PTR)
217 type = check_typedef (TYPE_TARGET_TYPE (type));
218
219 /* read_subroutine_type sets for non-static member functions the
220 artificial flag of the first parameter ('this' pointer). */
221 if (type->code () == TYPE_CODE_METHOD
222 && type->num_fields () > 0
223 && TYPE_FIELD_ARTIFICIAL (type, 0)
224 && type->field (0).type ()->code () == TYPE_CODE_PTR)
225 thiscall = 1;
226
227 return i386_thiscall_push_dummy_call (gdbarch, function, regcache, bp_addr,
228 nargs, args, sp, return_method,
229 struct_addr, thiscall);
230}
231
232/* Common parts for gdbarch initialization for Windows and Cygwin on i386. */
233
234static void
235i386_windows_init_abi_common (struct gdbarch_info info, struct gdbarch *gdbarch)
236{
237 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
238
239 set_gdbarch_skip_trampoline_code (gdbarch, i386_windows_skip_trampoline_code);
240
241 set_gdbarch_skip_main_prologue (gdbarch, i386_skip_main_prologue);
242
243 tdep->struct_return = reg_struct_return;
244
245 tdep->gregset_reg_offset = i386_windows_gregset_reg_offset;
246 tdep->gregset_num_regs = ARRAY_SIZE (i386_windows_gregset_reg_offset);
247 tdep->sizeof_gregset = I386_WINDOWS_SIZEOF_GREGSET;
248
249 tdep->sizeof_fpregset = 0;
250
251 /* Core file support. */
252 set_gdbarch_core_xfer_shared_libraries
253 (gdbarch, windows_core_xfer_shared_libraries);
254 set_gdbarch_core_pid_to_str (gdbarch, i386_windows_core_pid_to_str);
255
256 set_gdbarch_auto_wide_charset (gdbarch, i386_windows_auto_wide_charset);
257}
258
259/* gdbarch initialization for Windows on i386. */
260
261static void
262i386_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
263{
264 i386_windows_init_abi_common (info, gdbarch);
265 windows_init_abi (info, gdbarch);
266
267 set_gdbarch_push_dummy_call (gdbarch, i386_windows_push_dummy_call);
268}
269
270/* gdbarch initialization for Cygwin on i386. */
271
272static void
273i386_cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
274{
275 i386_windows_init_abi_common (info, gdbarch);
276 cygwin_init_abi (info, gdbarch);
277}
278
279static gdb_osabi
280i386_windows_osabi_sniffer (bfd *abfd)
281{
282 const char *target_name = bfd_get_target (abfd);
283
284 if (!streq (target_name, "pei-i386"))
285 return GDB_OSABI_UNKNOWN;
286
287 if (is_linked_with_cygwin_dll (abfd))
288 return GDB_OSABI_CYGWIN;
289
290 return GDB_OSABI_WINDOWS;
291}
292
293static enum gdb_osabi
294i386_cygwin_core_osabi_sniffer (bfd *abfd)
295{
296 const char *target_name = bfd_get_target (abfd);
297
298 /* Cygwin uses elf core dumps. Do not claim all ELF executables,
299 check whether there is a .reg section of proper size. */
300 if (strcmp (target_name, "elf32-i386") == 0)
301 {
302 asection *section = bfd_get_section_by_name (abfd, ".reg");
303 if (section != nullptr
304 && bfd_section_size (section) == I386_WINDOWS_SIZEOF_GREGSET)
305 return GDB_OSABI_CYGWIN;
306 }
307
308 return GDB_OSABI_UNKNOWN;
309}
310
311void _initialize_i386_windows_tdep ();
312void
313_initialize_i386_windows_tdep ()
314{
315 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
316 i386_windows_osabi_sniffer);
317
318 /* Cygwin uses elf core dumps. */
319 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
320 i386_cygwin_core_osabi_sniffer);
321
322 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_WINDOWS,
323 i386_windows_init_abi);
324 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_CYGWIN,
325 i386_cygwin_init_abi);
326}
This page took 0.022465 seconds and 4 git commands to generate.