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