Revert "2020-04-29 Sterling Augustine <saugustine@google.com>"
[deliverable/binutils-gdb.git] / gdb / i386-windows-tdep.c
CommitLineData
5982a56a
SM
1/* Target-dependent code for Windows (including Cygwin) running on i386's,
2 for GDB.
acd5c798 3
b811d2c2 4 Copyright (C) 2003-2020 Free Software Foundation, Inc.
1762d96d 5
acd5c798 6 This file is part of GDB.
1762d96d 7
acd5c798
MK
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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
acd5c798 11 (at your option) any later version.
1762d96d 12
acd5c798
MK
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.
1762d96d 17
acd5c798 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
1762d96d
CV
20
21#include "defs.h"
1762d96d 22#include "osabi.h"
acd5c798 23#include "i386-tdep.h"
31b060a2 24#include "windows-tdep.h"
de584861
PA
25#include "regset.h"
26#include "gdb_obstack.h"
27#include "xml-support.h"
28#include "gdbcore.h"
a5ee0f0c 29#include "inferior.h"
de584861
PA
30
31/* Core file support. */
32
33/* This vector maps GDB's idea of a register's number into an address
dc05df57 34 in the windows exception context vector. */
de584861 35
dc05df57 36static int i386_windows_gregset_reg_offset[] =
de584861
PA
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
dc05df57 90#define I386_WINDOWS_SIZEOF_GREGSET 716
de584861 91
de584861
PA
92struct cpms_data
93{
5af949e3 94 struct gdbarch *gdbarch;
de584861
PA
95 struct obstack *obstack;
96 int module_count;
97};
98
99static void
100core_process_module_section (bfd *abfd, asection *sect, void *obj)
101{
9a3c8263 102 struct cpms_data *data = (struct cpms_data *) obj;
e17a4113 103 enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
de584861
PA
104
105 char *module_name;
106 size_t module_name_size;
107 CORE_ADDR base_addr;
108
001f13d8 109 gdb_byte *buf = NULL;
de584861 110
61012eef 111 if (!startswith (sect->name, ".module"))
de584861
PA
112 return;
113
fd361982 114 buf = (gdb_byte *) xmalloc (bfd_section_size (sect) + 1);
de584861
PA
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,
fd361982 121 buf, 0, bfd_section_size (sect)))
de584861
PA
122 goto out;
123
124
125
dc05df57 126 /* A DWORD (data_type) followed by struct windows_core_module_info. */
de584861
PA
127
128 base_addr =
e17a4113 129 extract_unsigned_integer (buf + 4, 4, byte_order);
de584861
PA
130
131 module_name_size =
e17a4113 132 extract_unsigned_integer (buf + 8, 4, byte_order);
de584861 133
fd361982 134 if (12 + module_name_size > bfd_section_size (sect))
de584861 135 goto out;
001f13d8 136 module_name = (char *) buf + 12;
de584861
PA
137
138 /* The first module is the .exe itself. */
139 if (data->module_count != 0)
5af949e3 140 windows_xfer_shared_library (module_name, base_addr,
c162ed3e 141 NULL, data->gdbarch, data->obstack);
de584861
PA
142 data->module_count++;
143
144out:
145 if (buf)
146 xfree (buf);
147 return;
148}
149
c09f20e4 150static ULONGEST
dc05df57 151windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
de584861 152 gdb_byte *readbuf,
7ec1862d 153 ULONGEST offset, ULONGEST len)
de584861
PA
154{
155 struct obstack obstack;
156 const char *buf;
c09f20e4 157 ULONGEST len_avail;
5af949e3 158 struct cpms_data data = { gdbarch, &obstack, 0 };
de584861
PA
159
160 obstack_init (&obstack);
161 obstack_grow_str (&obstack, "<library-list>\n");
162 bfd_map_over_sections (core_bfd,
163 core_process_module_section,
164 &data);
165 obstack_grow_str0 (&obstack, "</library-list>\n");
166
224c3ddb 167 buf = (const char *) obstack_finish (&obstack);
de584861
PA
168 len_avail = strlen (buf);
169 if (offset >= len_avail)
170 return 0;
171
172 if (len > len_avail - offset)
173 len = len_avail - offset;
174 memcpy (readbuf, buf + offset, len);
175
176 obstack_free (&obstack, NULL);
177 return len;
178}
1762d96d 179
a5ee0f0c
PA
180/* This is how we want PTIDs from core files to be printed. */
181
a068643d 182static std::string
a5ee0f0c
PA
183i386_windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
184{
e38504b3 185 if (ptid.lwp () != 0)
a068643d 186 return string_printf ("Thread 0x%lx", ptid.lwp ());
a5ee0f0c
PA
187
188 return normal_pid_to_str (ptid);
189}
190
f7948b5f 191static CORE_ADDR
5982a56a 192i386_windows_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
f7948b5f 193{
e17a4113 194 return i386_pe_skip_trampoline_code (frame, pc, NULL);
f7948b5f
JB
195}
196
f870a310 197static const char *
5982a56a 198i386_windows_auto_wide_charset (void)
f870a310
TT
199{
200 return "UTF-16";
201}
202
627c7fb8
HD
203/* Implement the "push_dummy_call" gdbarch method. */
204
205static CORE_ADDR
206i386_windows_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
207 struct regcache *regcache, CORE_ADDR bp_addr,
208 int nargs, struct value **args, CORE_ADDR sp,
209 function_call_return_method return_method,
210 CORE_ADDR struct_addr)
211{
212 /* For non-static member functions of 32bit Windows programs, the thiscall
213 calling convention is used, so the 'this' pointer is passed in ECX. */
214 bool thiscall = false;
215
216 struct type *type = check_typedef (value_type (function));
217 if (TYPE_CODE (type) == TYPE_CODE_PTR)
218 type = check_typedef (TYPE_TARGET_TYPE (type));
219
220 /* read_subroutine_type sets for non-static member functions the
221 artificial flag of the first parameter ('this' pointer). */
222 if (TYPE_CODE (type) == TYPE_CODE_METHOD
223 && TYPE_NFIELDS (type) > 0
224 && TYPE_FIELD_ARTIFICIAL (type, 0)
225 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR)
226 thiscall = 1;
227
228 return i386_thiscall_push_dummy_call (gdbarch, function, regcache, bp_addr,
229 nargs, args, sp, return_method,
230 struct_addr, thiscall);
231}
232
0f2265e2
SM
233/* Common parts for gdbarch initialization for Windows and Cygwin on i386. */
234
1762d96d 235static void
0f2265e2 236i386_windows_init_abi_common (struct gdbarch_info info, struct gdbarch *gdbarch)
1762d96d
CV
237{
238 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
239
5982a56a 240 set_gdbarch_skip_trampoline_code (gdbarch, i386_windows_skip_trampoline_code);
f7948b5f 241
4309257c
PM
242 set_gdbarch_skip_main_prologue (gdbarch, i386_skip_main_prologue);
243
1762d96d 244 tdep->struct_return = reg_struct_return;
de584861 245
dc05df57
CF
246 tdep->gregset_reg_offset = i386_windows_gregset_reg_offset;
247 tdep->gregset_num_regs = ARRAY_SIZE (i386_windows_gregset_reg_offset);
248 tdep->sizeof_gregset = I386_WINDOWS_SIZEOF_GREGSET;
de584861 249
490496c3
AA
250 tdep->sizeof_fpregset = 0;
251
de584861 252 /* Core file support. */
de584861 253 set_gdbarch_core_xfer_shared_libraries
dc05df57 254 (gdbarch, windows_core_xfer_shared_libraries);
a5ee0f0c 255 set_gdbarch_core_pid_to_str (gdbarch, i386_windows_core_pid_to_str);
f870a310 256
5982a56a 257 set_gdbarch_auto_wide_charset (gdbarch, i386_windows_auto_wide_charset);
1762d96d
CV
258}
259
0f2265e2
SM
260/* gdbarch initialization for Windows on i386. */
261
262static void
263i386_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
264{
265 i386_windows_init_abi_common (info, gdbarch);
266 windows_init_abi (info, gdbarch);
627c7fb8
HD
267
268 set_gdbarch_push_dummy_call (gdbarch, i386_windows_push_dummy_call);
0f2265e2
SM
269}
270
271/* gdbarch initialization for Cygwin on i386. */
272
273static void
274i386_cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
275{
276 i386_windows_init_abi_common (info, gdbarch);
277 cygwin_init_abi (info, gdbarch);
278}
279
5982a56a
SM
280static gdb_osabi
281i386_windows_osabi_sniffer (bfd *abfd)
de584861 282{
1d38e9d1 283 const char *target_name = bfd_get_target (abfd);
1762d96d 284
8db52437
SM
285 if (!streq (target_name, "pei-i386"))
286 return GDB_OSABI_UNKNOWN;
053205cc 287
8db52437
SM
288 if (is_linked_with_cygwin_dll (abfd))
289 return GDB_OSABI_CYGWIN;
290
291 return GDB_OSABI_WINDOWS;
053205cc
SM
292}
293
294static enum gdb_osabi
295i386_cygwin_core_osabi_sniffer (bfd *abfd)
296{
297 const char *target_name = bfd_get_target (abfd);
1762d96d 298
c0993dbe
UW
299 /* Cygwin uses elf core dumps. Do not claim all ELF executables,
300 check whether there is a .reg section of proper size. */
de584861 301 if (strcmp (target_name, "elf32-i386") == 0)
c0993dbe
UW
302 {
303 asection *section = bfd_get_section_by_name (abfd, ".reg");
053205cc 304 if (section != nullptr
fd361982 305 && bfd_section_size (section) == I386_WINDOWS_SIZEOF_GREGSET)
c0993dbe
UW
306 return GDB_OSABI_CYGWIN;
307 }
de584861 308
1762d96d
CV
309 return GDB_OSABI_UNKNOWN;
310}
311
5982a56a 312void _initialize_i386_windows_tdep ();
1762d96d 313void
5982a56a 314_initialize_i386_windows_tdep ()
1762d96d
CV
315{
316 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
5982a56a 317 i386_windows_osabi_sniffer);
1762d96d 318
de584861
PA
319 /* Cygwin uses elf core dumps. */
320 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
053205cc 321 i386_cygwin_core_osabi_sniffer);
de584861 322
053205cc 323 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_WINDOWS,
5982a56a
SM
324 i386_windows_init_abi);
325 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_CYGWIN,
0f2265e2 326 i386_cygwin_init_abi);
1762d96d 327}
This page took 1.31564 seconds and 4 git commands to generate.