1 /* Copyright (C) 2008-2021 Free Software Foundation, Inc.
2 
3    This file is part of GDB.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include "defs.h"
19 #include "windows-tdep.h"
20 #include "gdb_obstack.h"
21 #include "xml-support.h"
22 #include "gdbarch.h"
23 #include "target.h"
24 #include "value.h"
25 #include "inferior.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "gdbthread.h"
29 #include "objfiles.h"
30 #include "symfile.h"
31 #include "coff-pe-read.h"
32 #include "gdb_bfd.h"
33 #include "complaints.h"
34 #include "solib.h"
35 #include "solib-target.h"
36 #include "gdbcore.h"
37 #include "coff/internal.h"
38 #include "libcoff.h"
39 #include "solist.h"
40 
41 #define CYGWIN_DLL_NAME "cygwin1.dll"
42 
43 /* Windows signal numbers differ between MinGW flavors and between
44    those and Cygwin.  The below enumerations were gleaned from the
45    respective headers.  */
46 
47 /* Signal numbers for the various MinGW flavors.  The ones marked with
48    MinGW-w64 are defined by MinGW-w64, not by mingw.org's MinGW.  */
49 
50 enum
51 {
52   WINDOWS_SIGHUP = 1,	/* MinGW-w64 */
53   WINDOWS_SIGINT = 2,
54   WINDOWS_SIGQUIT = 3,	/* MinGW-w64 */
55   WINDOWS_SIGILL = 4,
56   WINDOWS_SIGTRAP = 5,	/* MinGW-w64 */
57   WINDOWS_SIGIOT = 6,	/* MinGW-w64 */
58   WINDOWS_SIGEMT = 7,	/* MinGW-w64 */
59   WINDOWS_SIGFPE = 8,
60   WINDOWS_SIGKILL = 9,	/* MinGW-w64 */
61   WINDOWS_SIGBUS = 10,	/* MinGW-w64 */
62   WINDOWS_SIGSEGV = 11,
63   WINDOWS_SIGSYS = 12,	/* MinGW-w64 */
64   WINDOWS_SIGPIPE = 13,	/* MinGW-w64 */
65   WINDOWS_SIGALRM = 14,	/* MinGW-w64 */
66   WINDOWS_SIGTERM = 15,
67   WINDOWS_SIGBREAK = 21,
68   WINDOWS_SIGABRT = 22,
69 };
70 
71 /* Signal numbers for Cygwin.  */
72 
73 enum
74 {
75   CYGWIN_SIGHUP = 1,
76   CYGWIN_SIGINT = 2,
77   CYGWIN_SIGQUIT = 3,
78   CYGWIN_SIGILL = 4,
79   CYGWIN_SIGTRAP = 5,
80   CYGWIN_SIGABRT = 6,
81   CYGWIN_SIGEMT = 7,
82   CYGWIN_SIGFPE = 8,
83   CYGWIN_SIGKILL = 9,
84   CYGWIN_SIGBUS = 10,
85   CYGWIN_SIGSEGV = 11,
86   CYGWIN_SIGSYS = 12,
87   CYGWIN_SIGPIPE = 13,
88   CYGWIN_SIGALRM = 14,
89   CYGWIN_SIGTERM = 15,
90   CYGWIN_SIGURG = 16,
91   CYGWIN_SIGSTOP = 17,
92   CYGWIN_SIGTSTP = 18,
93   CYGWIN_SIGCONT = 19,
94   CYGWIN_SIGCHLD = 20,
95   CYGWIN_SIGTTIN = 21,
96   CYGWIN_SIGTTOU = 22,
97   CYGWIN_SIGIO = 23,
98   CYGWIN_SIGXCPU = 24,
99   CYGWIN_SIGXFSZ = 25,
100   CYGWIN_SIGVTALRM = 26,
101   CYGWIN_SIGPROF = 27,
102   CYGWIN_SIGWINCH = 28,
103   CYGWIN_SIGLOST = 29,
104   CYGWIN_SIGUSR1 = 30,
105   CYGWIN_SIGUSR2 = 31,
106 };
107 
108 /* These constants are defined by Cygwin's core_dump.h */
109 static constexpr unsigned int NOTE_INFO_MODULE = 3;
110 static constexpr unsigned int NOTE_INFO_MODULE64 = 4;
111 
112 struct cmd_list_element *info_w32_cmdlist;
113 
114 typedef struct thread_information_block_32
115   {
116     uint32_t current_seh;			/* %fs:0x0000 */
117     uint32_t current_top_of_stack; 		/* %fs:0x0004 */
118     uint32_t current_bottom_of_stack;		/* %fs:0x0008 */
119     uint32_t sub_system_tib;			/* %fs:0x000c */
120     uint32_t fiber_data;			/* %fs:0x0010 */
121     uint32_t arbitrary_data_slot;		/* %fs:0x0014 */
122     uint32_t linear_address_tib;		/* %fs:0x0018 */
123     uint32_t environment_pointer;		/* %fs:0x001c */
124     uint32_t process_id;			/* %fs:0x0020 */
125     uint32_t current_thread_id;			/* %fs:0x0024 */
126     uint32_t active_rpc_handle;			/* %fs:0x0028 */
127     uint32_t thread_local_storage;		/* %fs:0x002c */
128     uint32_t process_environment_block;		/* %fs:0x0030 */
129     uint32_t last_error_number;			/* %fs:0x0034 */
130   }
131 thread_information_32;
132 
133 typedef struct thread_information_block_64
134   {
135     uint64_t current_seh;			/* %gs:0x0000 */
136     uint64_t current_top_of_stack; 		/* %gs:0x0008 */
137     uint64_t current_bottom_of_stack;		/* %gs:0x0010 */
138     uint64_t sub_system_tib;			/* %gs:0x0018 */
139     uint64_t fiber_data;			/* %gs:0x0020 */
140     uint64_t arbitrary_data_slot;		/* %gs:0x0028 */
141     uint64_t linear_address_tib;		/* %gs:0x0030 */
142     uint64_t environment_pointer;		/* %gs:0x0038 */
143     uint64_t process_id;			/* %gs:0x0040 */
144     uint64_t current_thread_id;			/* %gs:0x0048 */
145     uint64_t active_rpc_handle;			/* %gs:0x0050 */
146     uint64_t thread_local_storage;		/* %gs:0x0058 */
147     uint64_t process_environment_block;		/* %gs:0x0060 */
148     uint64_t last_error_number;			/* %gs:0x0068 */
149   }
150 thread_information_64;
151 
152 
153 static const char* TIB_NAME[] =
154   {
155     " current_seh                 ",	/* %fs:0x0000 */
156     " current_top_of_stack        ", 	/* %fs:0x0004 */
157     " current_bottom_of_stack     ",	/* %fs:0x0008 */
158     " sub_system_tib              ",	/* %fs:0x000c */
159     " fiber_data                  ",	/* %fs:0x0010 */
160     " arbitrary_data_slot         ",	/* %fs:0x0014 */
161     " linear_address_tib          ",	/* %fs:0x0018 */
162     " environment_pointer         ",	/* %fs:0x001c */
163     " process_id                  ",	/* %fs:0x0020 */
164     " current_thread_id           ",	/* %fs:0x0024 */
165     " active_rpc_handle           ",	/* %fs:0x0028 */
166     " thread_local_storage        ",	/* %fs:0x002c */
167     " process_environment_block   ",	/* %fs:0x0030 */
168     " last_error_number           "	/* %fs:0x0034 */
169   };
170 
171 static const int MAX_TIB32 =
172   sizeof (thread_information_32) / sizeof (uint32_t);
173 static const int MAX_TIB64 =
174   sizeof (thread_information_64) / sizeof (uint64_t);
175 static const int FULL_TIB_SIZE = 0x1000;
176 
177 static bool maint_display_all_tib = false;
178 
179 static struct gdbarch_data *windows_gdbarch_data_handle;
180 
181 struct windows_gdbarch_data
182 {
183   struct type *siginfo_type;
184   struct type *tib_ptr_type; /* Type of thread information block */
185 };
186 
187 /* Allocate windows_gdbarch_data for an arch.  */
188 
189 static void *
init_windows_gdbarch_data(struct gdbarch * gdbarch)190 init_windows_gdbarch_data (struct gdbarch *gdbarch)
191 {
192   return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct windows_gdbarch_data);
193 }
194 
195 /* Get windows_gdbarch_data of an arch.  */
196 
197 static struct windows_gdbarch_data *
get_windows_gdbarch_data(struct gdbarch * gdbarch)198 get_windows_gdbarch_data (struct gdbarch *gdbarch)
199 {
200   return ((struct windows_gdbarch_data *)
201 	  gdbarch_data (gdbarch, windows_gdbarch_data_handle));
202 }
203 
204 /* Define Thread Local Base pointer type.  */
205 
206 static struct type *
windows_get_tlb_type(struct gdbarch * gdbarch)207 windows_get_tlb_type (struct gdbarch *gdbarch)
208 {
209   struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
210   struct type *peb_ldr_type, *peb_ldr_ptr_type;
211   struct type *peb_type, *peb_ptr_type, *list_type;
212   struct type *module_list_ptr_type;
213   struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
214   struct type *word_type, *wchar_type, *wchar_ptr_type;
215   struct type *uni_str_type, *rupp_type, *rupp_ptr_type;
216 
217   windows_gdbarch_data *windows_gdbarch_data
218     = get_windows_gdbarch_data (gdbarch);
219   if (windows_gdbarch_data->tib_ptr_type != nullptr)
220     return windows_gdbarch_data->tib_ptr_type;
221 
222   dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
223 				 1, "DWORD_PTR");
224   dword32_type = arch_integer_type (gdbarch, 32,
225 				 1, "DWORD32");
226   word_type = arch_integer_type (gdbarch, 16,
227 				 1, "WORD");
228   wchar_type = arch_integer_type (gdbarch, 16,
229 				  1, "wchar_t");
230   void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
231   wchar_ptr_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
232 				      NULL, wchar_type);
233 
234   /* list entry */
235 
236   list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
237   list_type->set_name (xstrdup ("list"));
238 
239   module_list_ptr_type = void_ptr_type;
240 
241   append_composite_type_field (list_type, "forward_list",
242 			       module_list_ptr_type);
243   append_composite_type_field (list_type, "backward_list",
244 			       module_list_ptr_type);
245 
246   /* Structured Exception Handler */
247 
248   seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
249   seh_type->set_name (xstrdup ("seh"));
250 
251   seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
252 			    TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
253 			    NULL);
254   TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
255 
256   append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
257   append_composite_type_field (seh_type, "handler",
258 			       builtin_type (gdbarch)->builtin_func_ptr);
259 
260   /* struct _PEB_LDR_DATA */
261   peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
262   peb_ldr_type->set_name (xstrdup ("peb_ldr_data"));
263 
264   append_composite_type_field (peb_ldr_type, "length", dword32_type);
265   append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
266   append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
267   append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
268   append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
269   append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
270   append_composite_type_field (peb_ldr_type, "entry_in_progress",
271 			       void_ptr_type);
272   peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
273 				TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
274 				NULL);
275   TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
276 
277   /* struct UNICODE_STRING */
278   uni_str_type = arch_composite_type (gdbarch, "unicode_string",
279 				      TYPE_CODE_STRUCT);
280 
281   append_composite_type_field (uni_str_type, "length", word_type);
282   append_composite_type_field (uni_str_type, "maximum_length", word_type);
283   append_composite_type_field_aligned (uni_str_type, "buffer",
284 				       wchar_ptr_type,
285 				       TYPE_LENGTH (wchar_ptr_type));
286 
287   /* struct _RTL_USER_PROCESS_PARAMETERS */
288   rupp_type = arch_composite_type (gdbarch, "rtl_user_process_parameters",
289 				   TYPE_CODE_STRUCT);
290 
291   append_composite_type_field (rupp_type, "maximum_length", dword32_type);
292   append_composite_type_field (rupp_type, "length", dword32_type);
293   append_composite_type_field (rupp_type, "flags", dword32_type);
294   append_composite_type_field (rupp_type, "debug_flags", dword32_type);
295   append_composite_type_field (rupp_type, "console_handle", void_ptr_type);
296   append_composite_type_field (rupp_type, "console_flags", dword32_type);
297   append_composite_type_field_aligned (rupp_type, "standard_input",
298 				       void_ptr_type,
299 				       TYPE_LENGTH (void_ptr_type));
300   append_composite_type_field (rupp_type, "standard_output", void_ptr_type);
301   append_composite_type_field (rupp_type, "standard_error", void_ptr_type);
302   append_composite_type_field (rupp_type, "current_directory", uni_str_type);
303   append_composite_type_field (rupp_type, "current_directory_handle",
304 			       void_ptr_type);
305   append_composite_type_field (rupp_type, "dll_path", uni_str_type);
306   append_composite_type_field (rupp_type, "image_path_name", uni_str_type);
307   append_composite_type_field (rupp_type, "command_line", uni_str_type);
308   append_composite_type_field (rupp_type, "environment", void_ptr_type);
309   append_composite_type_field (rupp_type, "starting_x", dword32_type);
310   append_composite_type_field (rupp_type, "starting_y", dword32_type);
311   append_composite_type_field (rupp_type, "count_x", dword32_type);
312   append_composite_type_field (rupp_type, "count_y", dword32_type);
313   append_composite_type_field (rupp_type, "count_chars_x", dword32_type);
314   append_composite_type_field (rupp_type, "count_chars_y", dword32_type);
315   append_composite_type_field (rupp_type, "fill_attribute", dword32_type);
316   append_composite_type_field (rupp_type, "window_flags", dword32_type);
317   append_composite_type_field (rupp_type, "show_window_flags", dword32_type);
318   append_composite_type_field_aligned (rupp_type, "window_title",
319 				       uni_str_type,
320 				       TYPE_LENGTH (void_ptr_type));
321   append_composite_type_field (rupp_type, "desktop_info", uni_str_type);
322   append_composite_type_field (rupp_type, "shell_info", uni_str_type);
323   append_composite_type_field (rupp_type, "runtime_data", uni_str_type);
324 
325   rupp_ptr_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
326 				     NULL, rupp_type);
327 
328 
329   /* struct process environment block */
330   peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
331   peb_type->set_name (xstrdup ("peb"));
332 
333   /* First bytes contain several flags.  */
334   append_composite_type_field (peb_type, "flags", dword_ptr_type);
335   append_composite_type_field (peb_type, "mutant", void_ptr_type);
336   append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
337   append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
338   append_composite_type_field (peb_type, "process_parameters", rupp_ptr_type);
339   append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
340   append_composite_type_field (peb_type, "process_heap", void_ptr_type);
341   append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
342   peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
343 			    TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
344 			    NULL);
345   TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;
346 
347 
348   /* struct thread information block */
349   tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
350   tib_type->set_name (xstrdup ("tib"));
351 
352   /* uint32_t current_seh;			%fs:0x0000 */
353   append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
354   /* uint32_t current_top_of_stack; 		%fs:0x0004 */
355   append_composite_type_field (tib_type, "current_top_of_stack",
356 			       void_ptr_type);
357   /* uint32_t current_bottom_of_stack;		%fs:0x0008 */
358   append_composite_type_field (tib_type, "current_bottom_of_stack",
359 			       void_ptr_type);
360   /* uint32_t sub_system_tib;			%fs:0x000c */
361   append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
362 
363   /* uint32_t fiber_data;			%fs:0x0010 */
364   append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
365   /* uint32_t arbitrary_data_slot;		%fs:0x0014 */
366   append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
367   /* uint32_t linear_address_tib;		%fs:0x0018 */
368   append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
369   /* uint32_t environment_pointer;		%fs:0x001c */
370   append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
371   /* uint32_t process_id;			%fs:0x0020 */
372   append_composite_type_field (tib_type, "process_id", dword_ptr_type);
373   /* uint32_t current_thread_id;		%fs:0x0024 */
374   append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
375   /* uint32_t active_rpc_handle;		%fs:0x0028 */
376   append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
377   /* uint32_t thread_local_storage;		%fs:0x002c */
378   append_composite_type_field (tib_type, "thread_local_storage",
379 			       void_ptr_type);
380   /* uint32_t process_environment_block;	%fs:0x0030 */
381   append_composite_type_field (tib_type, "process_environment_block",
382 			       peb_ptr_type);
383   /* uint32_t last_error_number;		%fs:0x0034 */
384   append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
385 
386   tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
387 			    TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
388 			    NULL);
389   TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
390 
391   windows_gdbarch_data->tib_ptr_type = tib_ptr_type;
392 
393   return tib_ptr_type;
394 }
395 
396 /* The $_tlb convenience variable is a bit special.  We don't know
397    for sure the type of the value until we actually have a chance to
398    fetch the data.  The type can change depending on gdbarch, so it is
399    also dependent on which thread you have selected.  */
400 
401 /* This function implements the lval_computed support for reading a
402    $_tlb value.  */
403 
404 static void
tlb_value_read(struct value * val)405 tlb_value_read (struct value *val)
406 {
407   CORE_ADDR tlb;
408   struct type *type = check_typedef (value_type (val));
409 
410   if (!target_get_tib_address (inferior_ptid, &tlb))
411     error (_("Unable to read tlb"));
412   store_typed_address (value_contents_raw (val), type, tlb);
413 }
414 
415 /* This function implements the lval_computed support for writing a
416    $_tlb value.  */
417 
418 static void
tlb_value_write(struct value * v,struct value * fromval)419 tlb_value_write (struct value *v, struct value *fromval)
420 {
421   error (_("Impossible to change the Thread Local Base"));
422 }
423 
424 static const struct lval_funcs tlb_value_funcs =
425   {
426     tlb_value_read,
427     tlb_value_write
428   };
429 
430 
431 /* Return a new value with the correct type for the tlb object of
432    the current thread using architecture GDBARCH.  Return a void value
433    if there's no object available.  */
434 
435 static struct value *
tlb_make_value(struct gdbarch * gdbarch,struct internalvar * var,void * ignore)436 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
437 {
438   if (target_has_stack () && inferior_ptid != null_ptid)
439     {
440       struct type *type = windows_get_tlb_type (gdbarch);
441       return allocate_computed_value (type, &tlb_value_funcs, NULL);
442     }
443 
444   return allocate_value (builtin_type (gdbarch)->builtin_void);
445 }
446 
447 
448 /* Display thread information block of a given thread.  */
449 
450 static int
display_one_tib(ptid_t ptid)451 display_one_tib (ptid_t ptid)
452 {
453   gdb_byte *tib = NULL;
454   gdb_byte *index;
455   CORE_ADDR thread_local_base;
456   ULONGEST i, val, max, max_name, size, tib_size;
457   ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch ());
458   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
459 
460   if (sizeof_ptr == 64)
461     {
462       size = sizeof (uint64_t);
463       tib_size = sizeof (thread_information_64);
464       max = MAX_TIB64;
465     }
466   else
467     {
468       size = sizeof (uint32_t);
469       tib_size = sizeof (thread_information_32);
470       max = MAX_TIB32;
471     }
472 
473   max_name = max;
474 
475   if (maint_display_all_tib)
476     {
477       tib_size = FULL_TIB_SIZE;
478       max = tib_size / size;
479     }
480 
481   tib = (gdb_byte *) alloca (tib_size);
482 
483   if (target_get_tib_address (ptid, &thread_local_base) == 0)
484     {
485       printf_filtered (_("Unable to get thread local base for %s\n"),
486 		       target_pid_to_str (ptid).c_str ());
487       return -1;
488     }
489 
490   if (target_read (current_inferior ()->top_target (), TARGET_OBJECT_MEMORY,
491 		   NULL, tib, thread_local_base, tib_size) != tib_size)
492     {
493       printf_filtered (_("Unable to read thread information "
494 			 "block for %s at address %s\n"),
495 		       target_pid_to_str (ptid).c_str (),
496 		       paddress (target_gdbarch (), thread_local_base));
497       return -1;
498     }
499 
500   printf_filtered (_("Thread Information Block %s at %s\n"),
501 		   target_pid_to_str (ptid).c_str (),
502 		   paddress (target_gdbarch (), thread_local_base));
503 
504   index = (gdb_byte *) tib;
505 
506   /* All fields have the size of a pointer, this allows to iterate
507      using the same for loop for both layouts.  */
508   for (i = 0; i < max; i++)
509     {
510       val = extract_unsigned_integer (index, size, byte_order);
511       if (i < max_name)
512 	printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
513       else if (val != 0)
514 	printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
515 			 phex (val, size));
516       index += size;
517     }
518   return 1;
519 }
520 
521 /* Display thread information block of the current thread.  */
522 
523 static void
display_tib(const char * args,int from_tty)524 display_tib (const char * args, int from_tty)
525 {
526   if (inferior_ptid != null_ptid)
527     display_one_tib (inferior_ptid);
528 }
529 
530 void
windows_xfer_shared_library(const char * so_name,CORE_ADDR load_addr,CORE_ADDR * text_offset_cached,struct gdbarch * gdbarch,struct obstack * obstack)531 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
532 			     CORE_ADDR *text_offset_cached,
533 			     struct gdbarch *gdbarch, struct obstack *obstack)
534 {
535   CORE_ADDR text_offset = text_offset_cached ? *text_offset_cached : 0;
536 
537   obstack_grow_str (obstack, "<library name=\"");
538   std::string p = xml_escape_text (so_name);
539   obstack_grow_str (obstack, p.c_str ());
540   obstack_grow_str (obstack, "\"><segment address=\"");
541 
542   if (!text_offset)
543     {
544       gdb_bfd_ref_ptr dll (gdb_bfd_open (so_name, gnutarget));
545       /* The following calls are OK even if dll is NULL.
546 	 The default value 0x1000 is returned by pe_text_section_offset
547 	 in that case.  */
548       text_offset = pe_text_section_offset (dll.get ());
549       if (text_offset_cached)
550 	*text_offset_cached = text_offset;
551     }
552 
553   obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
554   obstack_grow_str (obstack, "\"/></library>");
555 }
556 
557 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
558    method.  It searches all objfiles, starting with CURRENT_OBJFILE
559    first (if not NULL).
560 
561    On Windows, the system behaves a little differently when two
562    objfiles each define a global symbol using the same name, compared
563    to other platforms such as GNU/Linux for instance.  On GNU/Linux,
564    all instances of the symbol effectively get merged into a single
565    one, but on Windows, they remain distinct.
566 
567    As a result, it usually makes sense to start global symbol searches
568    with the current objfile before expanding it to all other objfiles.
569    This helps for instance when a user debugs some code in a DLL that
570    refers to a global variable defined inside that DLL.  When trying
571    to print the value of that global variable, it would be unhelpful
572    to print the value of another global variable defined with the same
573    name, but in a different DLL.  */
574 
575 static void
windows_iterate_over_objfiles_in_search_order(struct gdbarch * gdbarch,iterate_over_objfiles_in_search_order_cb_ftype * cb,void * cb_data,struct objfile * current_objfile)576 windows_iterate_over_objfiles_in_search_order
577   (struct gdbarch *gdbarch,
578    iterate_over_objfiles_in_search_order_cb_ftype *cb,
579    void *cb_data, struct objfile *current_objfile)
580 {
581   int stop;
582 
583   if (current_objfile)
584     {
585       stop = cb (current_objfile, cb_data);
586       if (stop)
587 	return;
588     }
589 
590   for (objfile *objfile : current_program_space->objfiles ())
591     {
592       if (objfile != current_objfile)
593 	{
594 	  stop = cb (objfile, cb_data);
595 	  if (stop)
596 	    return;
597 	}
598     }
599 }
600 
601 static void
show_maint_show_all_tib(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)602 show_maint_show_all_tib (struct ui_file *file, int from_tty,
603 		struct cmd_list_element *c, const char *value)
604 {
605   fprintf_filtered (file, _("Show all non-zero elements of "
606 			    "Thread Information Block is %s.\n"), value);
607 }
608 
609 
610 static int w32_prefix_command_valid = 0;
611 void
init_w32_command_list(void)612 init_w32_command_list (void)
613 {
614   if (!w32_prefix_command_valid)
615     {
616       add_basic_prefix_cmd
617 	("w32", class_info,
618 	 _("Print information specific to Win32 debugging."),
619 	 &info_w32_cmdlist, 0, &infolist);
620       w32_prefix_command_valid = 1;
621     }
622 }
623 
624 /* Implementation of `gdbarch_gdb_signal_to_target' for Windows.  */
625 
626 static int
windows_gdb_signal_to_target(struct gdbarch * gdbarch,enum gdb_signal signal)627 windows_gdb_signal_to_target (struct gdbarch *gdbarch, enum gdb_signal signal)
628 {
629   switch (signal)
630     {
631     case GDB_SIGNAL_0:
632       return 0;
633     case GDB_SIGNAL_HUP:
634       return WINDOWS_SIGHUP;
635     case GDB_SIGNAL_INT:
636       return WINDOWS_SIGINT;
637     case GDB_SIGNAL_QUIT:
638       return WINDOWS_SIGQUIT;
639     case GDB_SIGNAL_ILL:
640       return WINDOWS_SIGILL;
641     case GDB_SIGNAL_TRAP:
642       return WINDOWS_SIGTRAP;
643     case GDB_SIGNAL_ABRT:
644       return WINDOWS_SIGABRT;
645     case GDB_SIGNAL_EMT:
646       return WINDOWS_SIGEMT;
647     case GDB_SIGNAL_FPE:
648       return WINDOWS_SIGFPE;
649     case GDB_SIGNAL_KILL:
650       return WINDOWS_SIGKILL;
651     case GDB_SIGNAL_BUS:
652       return WINDOWS_SIGBUS;
653     case GDB_SIGNAL_SEGV:
654       return WINDOWS_SIGSEGV;
655     case GDB_SIGNAL_SYS:
656       return WINDOWS_SIGSYS;
657     case GDB_SIGNAL_PIPE:
658       return WINDOWS_SIGPIPE;
659     case GDB_SIGNAL_ALRM:
660       return WINDOWS_SIGALRM;
661     case GDB_SIGNAL_TERM:
662       return WINDOWS_SIGTERM;
663     }
664   return -1;
665 }
666 
667 /* Implementation of `gdbarch_gdb_signal_to_target' for Cygwin.  */
668 
669 static int
cygwin_gdb_signal_to_target(struct gdbarch * gdbarch,enum gdb_signal signal)670 cygwin_gdb_signal_to_target (struct gdbarch *gdbarch, enum gdb_signal signal)
671 {
672   switch (signal)
673     {
674     case GDB_SIGNAL_0:
675       return 0;
676     case GDB_SIGNAL_HUP:
677       return CYGWIN_SIGHUP;
678     case GDB_SIGNAL_INT:
679       return CYGWIN_SIGINT;
680     case GDB_SIGNAL_QUIT:
681       return CYGWIN_SIGQUIT;
682     case GDB_SIGNAL_ILL:
683       return CYGWIN_SIGILL;
684     case GDB_SIGNAL_TRAP:
685       return CYGWIN_SIGTRAP;
686     case GDB_SIGNAL_ABRT:
687       return CYGWIN_SIGABRT;
688     case GDB_SIGNAL_EMT:
689       return CYGWIN_SIGEMT;
690     case GDB_SIGNAL_FPE:
691       return CYGWIN_SIGFPE;
692     case GDB_SIGNAL_KILL:
693       return CYGWIN_SIGKILL;
694     case GDB_SIGNAL_BUS:
695       return CYGWIN_SIGBUS;
696     case GDB_SIGNAL_SEGV:
697       return CYGWIN_SIGSEGV;
698     case GDB_SIGNAL_SYS:
699       return CYGWIN_SIGSYS;
700     case GDB_SIGNAL_PIPE:
701       return CYGWIN_SIGPIPE;
702     case GDB_SIGNAL_ALRM:
703       return CYGWIN_SIGALRM;
704     case GDB_SIGNAL_TERM:
705       return CYGWIN_SIGTERM;
706     case GDB_SIGNAL_URG:
707       return CYGWIN_SIGURG;
708     case GDB_SIGNAL_STOP:
709       return CYGWIN_SIGSTOP;
710     case GDB_SIGNAL_TSTP:
711       return CYGWIN_SIGTSTP;
712     case GDB_SIGNAL_CONT:
713       return CYGWIN_SIGCONT;
714     case GDB_SIGNAL_CHLD:
715       return CYGWIN_SIGCHLD;
716     case GDB_SIGNAL_TTIN:
717       return CYGWIN_SIGTTIN;
718     case GDB_SIGNAL_TTOU:
719       return CYGWIN_SIGTTOU;
720     case GDB_SIGNAL_IO:
721       return CYGWIN_SIGIO;
722     case GDB_SIGNAL_XCPU:
723       return CYGWIN_SIGXCPU;
724     case GDB_SIGNAL_XFSZ:
725       return CYGWIN_SIGXFSZ;
726     case GDB_SIGNAL_VTALRM:
727       return CYGWIN_SIGVTALRM;
728     case GDB_SIGNAL_PROF:
729       return CYGWIN_SIGPROF;
730     case GDB_SIGNAL_WINCH:
731       return CYGWIN_SIGWINCH;
732     case GDB_SIGNAL_PWR:
733       return CYGWIN_SIGLOST;
734     case GDB_SIGNAL_USR1:
735       return CYGWIN_SIGUSR1;
736     case GDB_SIGNAL_USR2:
737       return CYGWIN_SIGUSR2;
738     }
739   return -1;
740 }
741 
742 struct enum_value_name
743 {
744   uint32_t value;
745   const char *name;
746 };
747 
748 /* Allocate a TYPE_CODE_ENUM type structure with its named values.  */
749 
750 static struct type *
create_enum(struct gdbarch * gdbarch,int bit,const char * name,const struct enum_value_name * values,int count)751 create_enum (struct gdbarch *gdbarch, int bit, const char *name,
752 	     const struct enum_value_name *values, int count)
753 {
754   struct type *type;
755   int i;
756 
757   type = arch_type (gdbarch, TYPE_CODE_ENUM, bit, name);
758   type->set_num_fields (count);
759   type->set_fields
760     ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * count));
761   type->set_is_unsigned (true);
762 
763   for (i = 0; i < count; i++)
764     {
765       TYPE_FIELD_NAME (type, i) = values[i].name;
766       SET_FIELD_ENUMVAL (type->field (i), values[i].value);
767     }
768 
769   return type;
770 }
771 
772 static const struct enum_value_name exception_values[] =
773 {
774   { 0x40000015, "FATAL_APP_EXIT" },
775   { 0x4000001E, "WX86_SINGLE_STEP" },
776   { 0x4000001F, "WX86_BREAKPOINT" },
777   { 0x40010005, "DBG_CONTROL_C" },
778   { 0x40010008, "DBG_CONTROL_BREAK" },
779   { 0x80000002, "DATATYPE_MISALIGNMENT" },
780   { 0x80000003, "BREAKPOINT" },
781   { 0x80000004, "SINGLE_STEP" },
782   { 0xC0000005, "ACCESS_VIOLATION" },
783   { 0xC0000006, "IN_PAGE_ERROR" },
784   { 0xC000001D, "ILLEGAL_INSTRUCTION" },
785   { 0xC0000025, "NONCONTINUABLE_EXCEPTION" },
786   { 0xC0000026, "INVALID_DISPOSITION" },
787   { 0xC000008C, "ARRAY_BOUNDS_EXCEEDED" },
788   { 0xC000008D, "FLOAT_DENORMAL_OPERAND" },
789   { 0xC000008E, "FLOAT_DIVIDE_BY_ZERO" },
790   { 0xC000008F, "FLOAT_INEXACT_RESULT" },
791   { 0xC0000090, "FLOAT_INVALID_OPERATION" },
792   { 0xC0000091, "FLOAT_OVERFLOW" },
793   { 0xC0000092, "FLOAT_STACK_CHECK" },
794   { 0xC0000093, "FLOAT_UNDERFLOW" },
795   { 0xC0000094, "INTEGER_DIVIDE_BY_ZERO" },
796   { 0xC0000095, "INTEGER_OVERFLOW" },
797   { 0xC0000096, "PRIV_INSTRUCTION" },
798   { 0xC00000FD, "STACK_OVERFLOW" },
799   { 0xC0000409, "FAST_FAIL" },
800 };
801 
802 static const struct enum_value_name violation_values[] =
803 {
804   { 0, "READ_ACCESS_VIOLATION" },
805   { 1, "WRITE_ACCESS_VIOLATION" },
806   { 8, "DATA_EXECUTION_PREVENTION_VIOLATION" },
807 };
808 
809 /* Implement the "get_siginfo_type" gdbarch method.  */
810 
811 static struct type *
windows_get_siginfo_type(struct gdbarch * gdbarch)812 windows_get_siginfo_type (struct gdbarch *gdbarch)
813 {
814   struct windows_gdbarch_data *windows_gdbarch_data;
815   struct type *dword_type, *pvoid_type, *ulongptr_type;
816   struct type *code_enum, *violation_enum;
817   struct type *violation_type, *para_type, *siginfo_ptr_type, *siginfo_type;
818 
819   windows_gdbarch_data = get_windows_gdbarch_data (gdbarch);
820   if (windows_gdbarch_data->siginfo_type != NULL)
821     return windows_gdbarch_data->siginfo_type;
822 
823   dword_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
824 				  1, "DWORD");
825   pvoid_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch), "PVOID",
826 				  builtin_type (gdbarch)->builtin_void);
827   ulongptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
828 				     1, "ULONG_PTR");
829 
830   /* ExceptionCode value names */
831   code_enum = create_enum (gdbarch, gdbarch_int_bit (gdbarch),
832 			   "ExceptionCode", exception_values,
833 			   ARRAY_SIZE (exception_values));
834 
835   /* ACCESS_VIOLATION type names */
836   violation_enum = create_enum (gdbarch, gdbarch_ptr_bit (gdbarch),
837 				"ViolationType", violation_values,
838 				ARRAY_SIZE (violation_values));
839 
840   /* ACCESS_VIOLATION information */
841   violation_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
842   append_composite_type_field (violation_type, "Type", violation_enum);
843   append_composite_type_field (violation_type, "Address", pvoid_type);
844 
845   /* Unnamed union of the documented field ExceptionInformation,
846      and the alternative AccessViolationInformation (which displays
847      human-readable values for ExceptionCode ACCESS_VIOLATION).  */
848   para_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
849   append_composite_type_field (para_type, "ExceptionInformation",
850 			       lookup_array_range_type (ulongptr_type, 0, 14));
851   append_composite_type_field (para_type, "AccessViolationInformation",
852 			       violation_type);
853 
854   siginfo_type = arch_composite_type (gdbarch, "EXCEPTION_RECORD",
855 				      TYPE_CODE_STRUCT);
856   siginfo_ptr_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
857 					NULL, siginfo_type);
858 
859   /* ExceptionCode is documented as type DWORD, but here a helper
860      enum type is used instead to display a human-readable value.  */
861   append_composite_type_field (siginfo_type, "ExceptionCode", code_enum);
862   append_composite_type_field (siginfo_type, "ExceptionFlags", dword_type);
863   append_composite_type_field (siginfo_type, "ExceptionRecord",
864 			       siginfo_ptr_type);
865   append_composite_type_field (siginfo_type, "ExceptionAddress",
866 			       pvoid_type);
867   append_composite_type_field (siginfo_type, "NumberParameters", dword_type);
868   /* The 64-bit variant needs some padding.  */
869   append_composite_type_field_aligned (siginfo_type, "",
870 				       para_type, TYPE_LENGTH (ulongptr_type));
871 
872   windows_gdbarch_data->siginfo_type = siginfo_type;
873 
874   return siginfo_type;
875 }
876 
877 /* Implement the "solib_create_inferior_hook" target_so_ops method.  */
878 
879 static void
windows_solib_create_inferior_hook(int from_tty)880 windows_solib_create_inferior_hook (int from_tty)
881 {
882   CORE_ADDR exec_base = 0;
883 
884   /* Find base address of main executable in
885      TIB->process_environment_block->image_base_address.  */
886   struct gdbarch *gdbarch = target_gdbarch ();
887   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
888   int ptr_bytes;
889   int peb_offset;  /* Offset of process_environment_block in TIB.  */
890   int base_offset; /* Offset of image_base_address in PEB.  */
891   if (gdbarch_ptr_bit (gdbarch) == 32)
892     {
893       ptr_bytes = 4;
894       peb_offset = 48;
895       base_offset = 8;
896     }
897   else
898     {
899       ptr_bytes = 8;
900       peb_offset = 96;
901       base_offset = 16;
902     }
903   CORE_ADDR tlb;
904   gdb_byte buf[8];
905   if (target_has_execution ()
906       && target_get_tib_address (inferior_ptid, &tlb)
907       && !target_read_memory (tlb + peb_offset, buf, ptr_bytes))
908     {
909       CORE_ADDR peb = extract_unsigned_integer (buf, ptr_bytes, byte_order);
910       if (!target_read_memory (peb + base_offset, buf, ptr_bytes))
911 	exec_base = extract_unsigned_integer (buf, ptr_bytes, byte_order);
912     }
913 
914   /* Rebase executable if the base address changed because of ASLR.  */
915   if (current_program_space->symfile_object_file != nullptr && exec_base != 0)
916     {
917       CORE_ADDR vmaddr
918 	= pe_data (current_program_space->exec_bfd ())->pe_opthdr.ImageBase;
919       if (vmaddr != exec_base)
920 	objfile_rebase (current_program_space->symfile_object_file,
921 			exec_base - vmaddr);
922     }
923 }
924 
925 static struct target_so_ops windows_so_ops;
926 
927 /* Common parts for gdbarch initialization for the Windows and Cygwin OS
928    ABIs.  */
929 
930 static void
windows_init_abi_common(struct gdbarch_info info,struct gdbarch * gdbarch)931 windows_init_abi_common (struct gdbarch_info info, struct gdbarch *gdbarch)
932 {
933   set_gdbarch_wchar_bit (gdbarch, 16);
934   set_gdbarch_wchar_signed (gdbarch, 0);
935 
936   /* Canonical paths on this target look like
937      `c:\Program Files\Foo App\mydll.dll', for example.  */
938   set_gdbarch_has_dos_based_file_system (gdbarch, 1);
939 
940   set_gdbarch_iterate_over_objfiles_in_search_order
941     (gdbarch, windows_iterate_over_objfiles_in_search_order);
942 
943   windows_so_ops = solib_target_so_ops;
944   windows_so_ops.solib_create_inferior_hook
945     = windows_solib_create_inferior_hook;
946   set_solib_ops (gdbarch, &windows_so_ops);
947 
948   set_gdbarch_get_siginfo_type (gdbarch, windows_get_siginfo_type);
949 }
950 
951 /* See windows-tdep.h.  */
952 void
windows_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)953 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
954 {
955   windows_init_abi_common (info, gdbarch);
956   set_gdbarch_gdb_signal_to_target (gdbarch, windows_gdb_signal_to_target);
957 }
958 
959 /* See windows-tdep.h.  */
960 
961 void
cygwin_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch)962 cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
963 {
964   windows_init_abi_common (info, gdbarch);
965   set_gdbarch_gdb_signal_to_target (gdbarch, cygwin_gdb_signal_to_target);
966 }
967 
968 /* Implementation of `tlb' variable.  */
969 
970 static const struct internalvar_funcs tlb_funcs =
971 {
972   tlb_make_value,
973   NULL,
974   NULL
975 };
976 
977 /* Layout of an element of a PE's Import Directory Table.  Based on:
978 
979      https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-directory-table
980  */
981 
982 struct pe_import_directory_entry
983 {
984   uint32_t import_lookup_table_rva;
985   uint32_t timestamp;
986   uint32_t forwarder_chain;
987   uint32_t name_rva;
988   uint32_t import_address_table_rva;
989 };
990 
991 gdb_static_assert (sizeof (pe_import_directory_entry) == 20);
992 
993 /* See windows-tdep.h.  */
994 
995 bool
is_linked_with_cygwin_dll(bfd * abfd)996 is_linked_with_cygwin_dll (bfd *abfd)
997 {
998   /* The list of DLLs a PE is linked to is in the .idata section.  See:
999 
1000      https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#the-idata-section
1001    */
1002   asection *idata_section = bfd_get_section_by_name (abfd, ".idata");
1003   if (idata_section == nullptr)
1004     return false;
1005 
1006   bfd_size_type idata_section_size = bfd_section_size (idata_section);
1007   internal_extra_pe_aouthdr *pe_extra = &pe_data (abfd)->pe_opthdr;
1008   bfd_vma import_table_va = pe_extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
1009   bfd_vma idata_section_va = bfd_section_vma (idata_section);
1010 
1011   /* The section's virtual address as reported by BFD has the image base applied,
1012      remove it.  */
1013   gdb_assert (idata_section_va >= pe_extra->ImageBase);
1014   idata_section_va -= pe_extra->ImageBase;
1015 
1016   bfd_vma idata_section_end_va = idata_section_va + idata_section_size;
1017 
1018   /* Make sure that the import table is indeed within the .idata section's range.  */
1019   if (import_table_va < idata_section_va
1020       || import_table_va >= idata_section_end_va)
1021     {
1022       warning (_("\
1023 %s: import table's virtual address (0x%" BFD_VMA_FMT "x) is outside .idata \
1024 section's range [0x%" BFD_VMA_FMT "x, 0x%" BFD_VMA_FMT "x[."),
1025 	       bfd_get_filename (abfd), import_table_va, idata_section_va,
1026 	       idata_section_end_va);
1027       return false;
1028     }
1029 
1030   /* The import table starts at this offset into the .idata section.  */
1031   bfd_vma import_table_offset_in_sect = import_table_va - idata_section_va;
1032 
1033   /* Get the section's data.  */
1034   gdb::byte_vector idata_contents;
1035   if (!gdb_bfd_get_full_section_contents (abfd, idata_section, &idata_contents))
1036     {
1037       warning (_("%s: failed to get contents of .idata section."),
1038 	       bfd_get_filename (abfd));
1039       return false;
1040     }
1041 
1042   gdb_assert (idata_contents.size () == idata_section_size);
1043 
1044   const gdb_byte *iter = idata_contents.data () + import_table_offset_in_sect;
1045   const gdb_byte *end = idata_contents.data () + idata_section_size;
1046   const pe_import_directory_entry null_dir_entry = { 0 };
1047 
1048   /* Iterate through all directory entries.  */
1049   while (true)
1050     {
1051       /* Is there enough space left in the section for another entry?  */
1052       if (iter + sizeof (pe_import_directory_entry) > end)
1053 	{
1054 	  warning (_("%s: unexpected end of .idata section."),
1055 		   bfd_get_filename (abfd));
1056 	  break;
1057 	}
1058 
1059       pe_import_directory_entry *dir_entry = (pe_import_directory_entry *) iter;
1060 
1061       /* Is it the end of list marker?  */
1062       if (memcmp (dir_entry, &null_dir_entry,
1063 		  sizeof (pe_import_directory_entry)) == 0)
1064 	break;
1065 
1066       bfd_vma name_va = dir_entry->name_rva;
1067 
1068       /* If the name's virtual address is smaller than the section's virtual
1069 	 address, there's a problem.  */
1070       if (name_va < idata_section_va || name_va >= idata_section_end_va)
1071 	{
1072 	  warning (_("\
1073 %s: name's virtual address (0x%" BFD_VMA_FMT "x) is outside .idata section's \
1074 range [0x%" BFD_VMA_FMT "x, 0x%" BFD_VMA_FMT "x[."),
1075 		   bfd_get_filename (abfd), name_va, idata_section_va,
1076 		   idata_section_end_va);
1077 	  break;
1078 	}
1079 
1080       const gdb_byte *name = &idata_contents[name_va - idata_section_va];
1081 
1082       /* Make sure we don't overshoot the end of the section with the
1083 	 streq.  */
1084       if (name + sizeof (CYGWIN_DLL_NAME) <= end)
1085 	{
1086 	  /* Finally, check if this is the dll name we are looking for.  */
1087 	  if (streq ((const char *) name, CYGWIN_DLL_NAME))
1088 	    return true;
1089 	}
1090 
1091       iter += sizeof (pe_import_directory_entry);
1092     }
1093 
1094   return false;
1095 }
1096 
1097 struct cpms_data
1098 {
1099   struct gdbarch *gdbarch;
1100   struct obstack *obstack;
1101   int module_count;
1102 };
1103 
1104 static void
core_process_module_section(bfd * abfd,asection * sect,void * obj)1105 core_process_module_section (bfd *abfd, asection *sect, void *obj)
1106 {
1107   struct cpms_data *data = (struct cpms_data *) obj;
1108   enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);
1109 
1110   unsigned int data_type;
1111   char *module_name;
1112   size_t module_name_size;
1113   size_t module_name_offset;
1114   CORE_ADDR base_addr;
1115 
1116   gdb_byte *buf = NULL;
1117 
1118   if (!startswith (sect->name, ".module"))
1119     return;
1120 
1121   buf = (gdb_byte *) xmalloc (bfd_section_size (sect) + 1);
1122   if (!buf)
1123     {
1124       printf_unfiltered ("memory allocation failed for %s\n", sect->name);
1125       goto out;
1126     }
1127   if (!bfd_get_section_contents (abfd, sect,
1128 				 buf, 0, bfd_section_size (sect)))
1129     goto out;
1130 
1131 
1132 
1133   /* A DWORD (data_type) followed by struct windows_core_module_info.  */
1134   data_type = extract_unsigned_integer (buf, 4, byte_order);
1135 
1136   if (data_type == NOTE_INFO_MODULE)
1137     {
1138       base_addr = extract_unsigned_integer (buf + 4, 4, byte_order);
1139       module_name_size = extract_unsigned_integer (buf + 8, 4, byte_order);
1140       module_name_offset = 12;
1141     }
1142   else if (data_type == NOTE_INFO_MODULE64)
1143     {
1144       base_addr = extract_unsigned_integer (buf + 4, 8, byte_order);
1145       module_name_size = extract_unsigned_integer (buf + 12, 4, byte_order);
1146       module_name_offset = 16;
1147     }
1148   else
1149     goto out;
1150 
1151   if (module_name_offset + module_name_size > bfd_section_size (sect))
1152     goto out;
1153   module_name = (char *) buf + module_name_offset;
1154 
1155   /* The first module is the .exe itself.  */
1156   if (data->module_count != 0)
1157     windows_xfer_shared_library (module_name, base_addr,
1158 				 NULL, data->gdbarch, data->obstack);
1159   data->module_count++;
1160 
1161 out:
1162   xfree (buf);
1163   return;
1164 }
1165 
1166 ULONGEST
windows_core_xfer_shared_libraries(struct gdbarch * gdbarch,gdb_byte * readbuf,ULONGEST offset,ULONGEST len)1167 windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
1168 				  gdb_byte *readbuf,
1169 				  ULONGEST offset, ULONGEST len)
1170 {
1171   struct obstack obstack;
1172   const char *buf;
1173   ULONGEST len_avail;
1174   struct cpms_data data = { gdbarch, &obstack, 0 };
1175 
1176   obstack_init (&obstack);
1177   obstack_grow_str (&obstack, "<library-list>\n");
1178   bfd_map_over_sections (core_bfd,
1179 			 core_process_module_section,
1180 			 &data);
1181   obstack_grow_str0 (&obstack, "</library-list>\n");
1182 
1183   buf = (const char *) obstack_finish (&obstack);
1184   len_avail = strlen (buf);
1185   if (offset >= len_avail)
1186     return 0;
1187 
1188   if (len > len_avail - offset)
1189     len = len_avail - offset;
1190   memcpy (readbuf, buf + offset, len);
1191 
1192   obstack_free (&obstack, NULL);
1193   return len;
1194 }
1195 
1196 /* This is how we want PTIDs from core files to be printed.  */
1197 
1198 std::string
windows_core_pid_to_str(struct gdbarch * gdbarch,ptid_t ptid)1199 windows_core_pid_to_str (struct gdbarch *gdbarch, ptid_t ptid)
1200 {
1201   if (ptid.lwp () != 0)
1202     return string_printf ("Thread 0x%lx", ptid.lwp ());
1203 
1204   return normal_pid_to_str (ptid);
1205 }
1206 
1207 void _initialize_windows_tdep ();
1208 void
_initialize_windows_tdep()1209 _initialize_windows_tdep ()
1210 {
1211   windows_gdbarch_data_handle
1212     = gdbarch_data_register_post_init (init_windows_gdbarch_data);
1213 
1214   init_w32_command_list ();
1215   cmd_list_element *info_w32_thread_information_block_cmd
1216     = add_cmd ("thread-information-block", class_info, display_tib,
1217 	       _("Display thread information block."),
1218 	       &info_w32_cmdlist);
1219   add_alias_cmd ("tib", info_w32_thread_information_block_cmd, class_info, 1,
1220 		 &info_w32_cmdlist);
1221 
1222   add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
1223 			   &maint_display_all_tib, _("\
1224 Set whether to display all non-zero fields of thread information block."), _("\
1225 Show whether to display all non-zero fields of thread information block."), _("\
1226 Use \"on\" to enable, \"off\" to disable.\n\
1227 If enabled, all non-zero fields of thread information block are displayed,\n\
1228 even if their meaning is unknown."),
1229 			   NULL,
1230 			   show_maint_show_all_tib,
1231 			   &maintenance_set_cmdlist,
1232 			   &maintenance_show_cmdlist);
1233 
1234   /* Explicitly create without lookup, since that tries to create a
1235      value with a void typed value, and when we get here, gdbarch
1236      isn't initialized yet.  At this point, we're quite sure there
1237      isn't another convenience variable of the same name.  */
1238   create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
1239 }
1240