gdb/testsuite/
[binutils-gdb.git] / gdb / i386-cygwin-tdep.c
1 /* Target-dependent code for Cygwin running on i386's, for GDB.
2
3 Copyright (C) 2003, 2007-2012 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "osabi.h"
22 #include "gdb_string.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 "solib.h"
30 #include "solib-target.h"
31 #include "inferior.h"
32
33 /* Core file support. */
34
35 /* This vector maps GDB's idea of a register's number into an address
36 in the windows exception context vector. */
37
38 static int i386_windows_gregset_reg_offset[] =
39 {
40 176, /* eax */
41 172, /* ecx */
42 168, /* edx */
43 164, /* ebx */
44
45 196, /* esp */
46 180, /* ebp */
47 160, /* esi */
48 156, /* edi */
49
50 184, /* eip */
51 192, /* eflags */
52 188, /* cs */
53 200, /* ss */
54
55 152, /* ds */
56 148, /* es */
57 144, /* fs */
58 140, /* gs */
59
60 56, /* FloatSave.RegisterArea[0 * 10] */
61 66, /* FloatSave.RegisterArea[1 * 10] */
62 76, /* FloatSave.RegisterArea[2 * 10] */
63 86, /* FloatSave.RegisterArea[3 * 10] */
64 96, /* FloatSave.RegisterArea[4 * 10] */
65 106, /* FloatSave.RegisterArea[5 * 10] */
66 116, /* FloatSave.RegisterArea[6 * 10] */
67 126, /* FloatSave.RegisterArea[7 * 10] */
68
69 28, /* FloatSave.ControlWord */
70 32, /* FloatSave.StatusWord */
71 36, /* FloatSave.TagWord */
72 44, /* FloatSave.ErrorSelector */
73 40, /* FloatSave.ErrorOffset */
74 52, /* FloatSave.DataSelector */
75 48, /* FloatSave.DataOffset */
76 44, /* FloatSave.ErrorSelector */
77
78 /* XMM0-7 */
79 364, /* ExtendedRegisters[10*16] */
80 380, /* ExtendedRegisters[11*16] */
81 396, /* ExtendedRegisters[12*16] */
82 412, /* ExtendedRegisters[13*16] */
83 428, /* ExtendedRegisters[14*16] */
84 444, /* ExtendedRegisters[15*16] */
85 460, /* ExtendedRegisters[16*16] */
86 476, /* ExtendedRegisters[17*16] */
87
88 /* MXCSR */
89 228 /* ExtendedRegisters[24] */
90 };
91
92 #define I386_WINDOWS_SIZEOF_GREGSET 716
93
94 /* Return the appropriate register set for the core section identified
95 by SECT_NAME and SECT_SIZE. */
96
97 static const struct regset *
98 i386_windows_regset_from_core_section (struct gdbarch *gdbarch,
99 const char *sect_name, size_t sect_size)
100 {
101 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
102
103 if (strcmp (sect_name, ".reg") == 0
104 && sect_size == I386_WINDOWS_SIZEOF_GREGSET)
105 {
106 if (tdep->gregset == NULL)
107 tdep->gregset = regset_alloc (gdbarch, i386_supply_gregset,
108 i386_collect_gregset);
109 return tdep->gregset;
110 }
111
112 return NULL;
113 }
114
115 struct cpms_data
116 {
117 struct gdbarch *gdbarch;
118 struct obstack *obstack;
119 int module_count;
120 };
121
122 static void
123 core_process_module_section (bfd *abfd, asection *sect, void *obj)
124 {
125 struct cpms_data *data = obj;
126 enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
127
128 char *module_name;
129 size_t module_name_size;
130 CORE_ADDR base_addr;
131
132 char *buf = NULL;
133
134 if (strncmp (sect->name, ".module", 7) != 0)
135 return;
136
137 buf = xmalloc (bfd_get_section_size (sect) + 1);
138 if (!buf)
139 {
140 printf_unfiltered ("memory allocation failed for %s\n", sect->name);
141 goto out;
142 }
143 if (!bfd_get_section_contents (abfd, sect,
144 buf, 0, bfd_get_section_size (sect)))
145 goto out;
146
147
148
149 /* A DWORD (data_type) followed by struct windows_core_module_info. */
150
151 base_addr =
152 extract_unsigned_integer (buf + 4, 4, byte_order);
153
154 module_name_size =
155 extract_unsigned_integer (buf + 8, 4, byte_order);
156
157 module_name = buf + 12;
158 if (module_name - buf + module_name_size > bfd_get_section_size (sect))
159 goto out;
160
161 /* The first module is the .exe itself. */
162 if (data->module_count != 0)
163 windows_xfer_shared_library (module_name, base_addr,
164 data->gdbarch, data->obstack);
165 data->module_count++;
166
167 out:
168 if (buf)
169 xfree (buf);
170 return;
171 }
172
173 static LONGEST
174 windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
175 gdb_byte *readbuf,
176 ULONGEST offset, LONGEST len)
177 {
178 struct obstack obstack;
179 const char *buf;
180 LONGEST len_avail;
181 struct cpms_data data = { gdbarch, &obstack, 0 };
182
183 obstack_init (&obstack);
184 obstack_grow_str (&obstack, "<library-list>\n");
185 bfd_map_over_sections (core_bfd,
186 core_process_module_section,
187 &data);
188 obstack_grow_str0 (&obstack, "</library-list>\n");
189
190 buf = obstack_finish (&obstack);
191 len_avail = strlen (buf);
192 if (offset >= len_avail)
193 return 0;
194
195 if (len > len_avail - offset)
196 len = len_avail - offset;
197 memcpy (readbuf, buf + offset, len);
198
199 obstack_free (&obstack, NULL);
200 return len;
201 }
202
203 /* This is how we want PTIDs from core files to be printed. */
204
205 static char *
206 i386_windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
207 {
208 static char buf[80];
209
210 if (ptid_get_lwp (ptid) != 0)
211 {
212 snprintf (buf, sizeof (buf), "Thread 0x%lx", ptid_get_lwp (ptid));
213 return buf;
214 }
215
216 return normal_pid_to_str (ptid);
217 }
218
219 static CORE_ADDR
220 i386_cygwin_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
221 {
222 return i386_pe_skip_trampoline_code (frame, pc, NULL);
223 }
224
225 static const char *
226 i386_cygwin_auto_wide_charset (void)
227 {
228 return "UTF-16";
229 }
230
231 static void
232 i386_cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
233 {
234 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
235
236 set_gdbarch_skip_trampoline_code (gdbarch, i386_cygwin_skip_trampoline_code);
237
238 set_gdbarch_skip_main_prologue (gdbarch, i386_skip_main_prologue);
239
240 tdep->struct_return = reg_struct_return;
241
242 tdep->gregset_reg_offset = i386_windows_gregset_reg_offset;
243 tdep->gregset_num_regs = ARRAY_SIZE (i386_windows_gregset_reg_offset);
244 tdep->sizeof_gregset = I386_WINDOWS_SIZEOF_GREGSET;
245
246 set_solib_ops (gdbarch, &solib_target_so_ops);
247
248 /* Core file support. */
249 set_gdbarch_regset_from_core_section
250 (gdbarch, i386_windows_regset_from_core_section);
251 set_gdbarch_core_xfer_shared_libraries
252 (gdbarch, windows_core_xfer_shared_libraries);
253 set_gdbarch_core_pid_to_str (gdbarch, i386_windows_core_pid_to_str);
254
255 set_gdbarch_auto_wide_charset (gdbarch, i386_cygwin_auto_wide_charset);
256
257 /* Canonical paths on this target look like
258 `c:\Program Files\Foo App\mydll.dll', for example. */
259 set_gdbarch_has_dos_based_file_system (gdbarch, 1);
260
261 set_gdbarch_iterate_over_objfiles_in_search_order
262 (gdbarch, windows_iterate_over_objfiles_in_search_order);
263 }
264
265 static enum gdb_osabi
266 i386_cygwin_osabi_sniffer (bfd *abfd)
267 {
268 char *target_name = bfd_get_target (abfd);
269
270 /* Interix also uses pei-i386.
271 We need a way to distinguish between the two. */
272 if (strcmp (target_name, "pei-i386") == 0)
273 return GDB_OSABI_CYGWIN;
274
275 /* Cygwin uses elf core dumps. Do not claim all ELF executables,
276 check whether there is a .reg section of proper size. */
277 if (strcmp (target_name, "elf32-i386") == 0)
278 {
279 asection *section = bfd_get_section_by_name (abfd, ".reg");
280 if (section
281 && bfd_section_size (abfd, section) == I386_WINDOWS_SIZEOF_GREGSET)
282 return GDB_OSABI_CYGWIN;
283 }
284
285 return GDB_OSABI_UNKNOWN;
286 }
287
288 /* Provide a prototype to silence -Wmissing-prototypes. */
289 void _initialize_i386_cygwin_tdep (void);
290
291 void
292 _initialize_i386_cygwin_tdep (void)
293 {
294 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
295 i386_cygwin_osabi_sniffer);
296
297 /* Cygwin uses elf core dumps. */
298 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
299 i386_cygwin_osabi_sniffer);
300
301 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_CYGWIN,
302 i386_cygwin_init_abi);
303 }