]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/windows-tdep.c
gdb: use bfd_get_section_contents to read section contents in is_linked_with_cygwin_dll
[thirdparty/binutils-gdb.git] / gdb / windows-tdep.c
1 /* Copyright (C) 2008-2020 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 enumeration was gleaned from the
45 respective headers; the ones marked with MinGW64/Cygwin are defined
46 only by MinGW64 and Cygwin, not by mingw.org's MinGW. FIXME: We
47 should really have distinct MinGW vs Cygwin OSABIs, and two
48 separate enums, selected at runtime. */
49
50 enum
51 {
52 WINDOWS_SIGHUP = 1, /* MinGW64/Cygwin */
53 WINDOWS_SIGINT = 2,
54 WINDOWS_SIGQUIT = 3, /* MinGW64/Cygwin */
55 WINDOWS_SIGILL = 4,
56 WINDOWS_SIGTRAP = 5, /* MinGW64/Cygwin */
57 #ifdef __CYGWIN__
58 WINDOWS_SIGABRT = 6,
59 #else
60 WINDOWS_SIGIOT = 6, /* MinGW64 */
61 #endif
62 WINDOWS_SIGEMT = 7, /* MinGW64/Cygwin */
63 WINDOWS_SIGFPE = 8,
64 WINDOWS_SIGKILL = 9, /* MinGW64/Cygwin */
65 WINDOWS_SIGBUS = 10, /* MinGW64/Cygwin */
66 WINDOWS_SIGSEGV = 11,
67 WINDOWS_SIGSYS = 12, /* MinGW64/Cygwin */
68 WINDOWS_SIGPIPE = 13,/* MinGW64/Cygwin */
69 WINDOWS_SIGALRM = 14,/* MinGW64/Cygwin */
70 WINDOWS_SIGTERM = 15,
71 #ifdef __CYGWIN__
72 WINDOWS_SIGURG = 16,
73 WINDOWS_SIGSTOP = 17,
74 WINDOWS_SIGTSTP = 18,
75 WINDOWS_SIGCONT = 19,
76 WINDOWS_SIGCHLD = 20,
77 WINDOWS_SIGTTIN = 21,
78 WINDOWS_SIGTTOU = 22,
79 WINDOWS_SIGIO = 23,
80 WINDOWS_SIGXCPU = 24,
81 WINDOWS_SIGXFSZ = 25,
82 WINDOWS_SIGVTALRM = 26,
83 WINDOWS_SIGPROF = 27,
84 WINDOWS_SIGWINCH = 28,
85 WINDOWS_SIGLOST = 29,
86 WINDOWS_SIGUSR1 = 30,
87 WINDOWS_SIGUSR2 = 31
88 #else
89 WINDOWS_SIGBREAK = 21,
90 WINDOWS_SIGABRT = 22
91 #endif
92 };
93
94 struct cmd_list_element *info_w32_cmdlist;
95
96 typedef struct thread_information_block_32
97 {
98 uint32_t current_seh; /* %fs:0x0000 */
99 uint32_t current_top_of_stack; /* %fs:0x0004 */
100 uint32_t current_bottom_of_stack; /* %fs:0x0008 */
101 uint32_t sub_system_tib; /* %fs:0x000c */
102 uint32_t fiber_data; /* %fs:0x0010 */
103 uint32_t arbitrary_data_slot; /* %fs:0x0014 */
104 uint32_t linear_address_tib; /* %fs:0x0018 */
105 uint32_t environment_pointer; /* %fs:0x001c */
106 uint32_t process_id; /* %fs:0x0020 */
107 uint32_t current_thread_id; /* %fs:0x0024 */
108 uint32_t active_rpc_handle; /* %fs:0x0028 */
109 uint32_t thread_local_storage; /* %fs:0x002c */
110 uint32_t process_environment_block; /* %fs:0x0030 */
111 uint32_t last_error_number; /* %fs:0x0034 */
112 }
113 thread_information_32;
114
115 typedef struct thread_information_block_64
116 {
117 uint64_t current_seh; /* %gs:0x0000 */
118 uint64_t current_top_of_stack; /* %gs:0x0008 */
119 uint64_t current_bottom_of_stack; /* %gs:0x0010 */
120 uint64_t sub_system_tib; /* %gs:0x0018 */
121 uint64_t fiber_data; /* %gs:0x0020 */
122 uint64_t arbitrary_data_slot; /* %gs:0x0028 */
123 uint64_t linear_address_tib; /* %gs:0x0030 */
124 uint64_t environment_pointer; /* %gs:0x0038 */
125 uint64_t process_id; /* %gs:0x0040 */
126 uint64_t current_thread_id; /* %gs:0x0048 */
127 uint64_t active_rpc_handle; /* %gs:0x0050 */
128 uint64_t thread_local_storage; /* %gs:0x0058 */
129 uint64_t process_environment_block; /* %gs:0x0060 */
130 uint64_t last_error_number; /* %gs:0x0068 */
131 }
132 thread_information_64;
133
134
135 static const char* TIB_NAME[] =
136 {
137 " current_seh ", /* %fs:0x0000 */
138 " current_top_of_stack ", /* %fs:0x0004 */
139 " current_bottom_of_stack ", /* %fs:0x0008 */
140 " sub_system_tib ", /* %fs:0x000c */
141 " fiber_data ", /* %fs:0x0010 */
142 " arbitrary_data_slot ", /* %fs:0x0014 */
143 " linear_address_tib ", /* %fs:0x0018 */
144 " environment_pointer ", /* %fs:0x001c */
145 " process_id ", /* %fs:0x0020 */
146 " current_thread_id ", /* %fs:0x0024 */
147 " active_rpc_handle ", /* %fs:0x0028 */
148 " thread_local_storage ", /* %fs:0x002c */
149 " process_environment_block ", /* %fs:0x0030 */
150 " last_error_number " /* %fs:0x0034 */
151 };
152
153 static const int MAX_TIB32 =
154 sizeof (thread_information_32) / sizeof (uint32_t);
155 static const int MAX_TIB64 =
156 sizeof (thread_information_64) / sizeof (uint64_t);
157 static const int FULL_TIB_SIZE = 0x1000;
158
159 static bool maint_display_all_tib = false;
160
161 static struct gdbarch_data *windows_gdbarch_data_handle;
162
163 struct windows_gdbarch_data
164 {
165 struct type *siginfo_type;
166 struct type *tib_ptr_type; /* Type of thread information block */
167 };
168
169 /* Allocate windows_gdbarch_data for an arch. */
170
171 static void *
172 init_windows_gdbarch_data (struct gdbarch *gdbarch)
173 {
174 return GDBARCH_OBSTACK_ZALLOC (gdbarch, struct windows_gdbarch_data);
175 }
176
177 /* Get windows_gdbarch_data of an arch. */
178
179 static struct windows_gdbarch_data *
180 get_windows_gdbarch_data (struct gdbarch *gdbarch)
181 {
182 return ((struct windows_gdbarch_data *)
183 gdbarch_data (gdbarch, windows_gdbarch_data_handle));
184 }
185
186 /* Define Thread Local Base pointer type. */
187
188 static struct type *
189 windows_get_tlb_type (struct gdbarch *gdbarch)
190 {
191 struct type *dword_ptr_type, *dword32_type, *void_ptr_type;
192 struct type *peb_ldr_type, *peb_ldr_ptr_type;
193 struct type *peb_type, *peb_ptr_type, *list_type;
194 struct type *module_list_ptr_type;
195 struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
196 struct type *word_type, *wchar_type, *wchar_ptr_type;
197 struct type *uni_str_type, *rupp_type, *rupp_ptr_type;
198
199 windows_gdbarch_data *windows_gdbarch_data
200 = get_windows_gdbarch_data (gdbarch);
201 if (windows_gdbarch_data->tib_ptr_type != nullptr)
202 return windows_gdbarch_data->tib_ptr_type;
203
204 dword_ptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
205 1, "DWORD_PTR");
206 dword32_type = arch_integer_type (gdbarch, 32,
207 1, "DWORD32");
208 word_type = arch_integer_type (gdbarch, 16,
209 1, "WORD");
210 wchar_type = arch_integer_type (gdbarch, 16,
211 1, "wchar_t");
212 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
213 wchar_ptr_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
214 NULL, wchar_type);
215
216 /* list entry */
217
218 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
219 TYPE_NAME (list_type) = xstrdup ("list");
220
221 module_list_ptr_type = void_ptr_type;
222
223 append_composite_type_field (list_type, "forward_list",
224 module_list_ptr_type);
225 append_composite_type_field (list_type, "backward_list",
226 module_list_ptr_type);
227
228 /* Structured Exception Handler */
229
230 seh_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
231 TYPE_NAME (seh_type) = xstrdup ("seh");
232
233 seh_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
234 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
235 NULL);
236 TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
237
238 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
239 append_composite_type_field (seh_type, "handler",
240 builtin_type (gdbarch)->builtin_func_ptr);
241
242 /* struct _PEB_LDR_DATA */
243 peb_ldr_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
244 TYPE_NAME (peb_ldr_type) = xstrdup ("peb_ldr_data");
245
246 append_composite_type_field (peb_ldr_type, "length", dword32_type);
247 append_composite_type_field (peb_ldr_type, "initialized", dword32_type);
248 append_composite_type_field (peb_ldr_type, "ss_handle", void_ptr_type);
249 append_composite_type_field (peb_ldr_type, "in_load_order", list_type);
250 append_composite_type_field (peb_ldr_type, "in_memory_order", list_type);
251 append_composite_type_field (peb_ldr_type, "in_init_order", list_type);
252 append_composite_type_field (peb_ldr_type, "entry_in_progress",
253 void_ptr_type);
254 peb_ldr_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
255 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
256 NULL);
257 TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
258
259 /* struct UNICODE_STRING */
260 uni_str_type = arch_composite_type (gdbarch, "unicode_string",
261 TYPE_CODE_STRUCT);
262
263 append_composite_type_field (uni_str_type, "length", word_type);
264 append_composite_type_field (uni_str_type, "maximum_length", word_type);
265 append_composite_type_field_aligned (uni_str_type, "buffer",
266 wchar_ptr_type,
267 TYPE_LENGTH (wchar_ptr_type));
268
269 /* struct _RTL_USER_PROCESS_PARAMETERS */
270 rupp_type = arch_composite_type (gdbarch, "rtl_user_process_parameters",
271 TYPE_CODE_STRUCT);
272
273 append_composite_type_field (rupp_type, "maximum_length", dword32_type);
274 append_composite_type_field (rupp_type, "length", dword32_type);
275 append_composite_type_field (rupp_type, "flags", dword32_type);
276 append_composite_type_field (rupp_type, "debug_flags", dword32_type);
277 append_composite_type_field (rupp_type, "console_handle", void_ptr_type);
278 append_composite_type_field (rupp_type, "console_flags", dword32_type);
279 append_composite_type_field_aligned (rupp_type, "standard_input",
280 void_ptr_type,
281 TYPE_LENGTH (void_ptr_type));
282 append_composite_type_field (rupp_type, "standard_output", void_ptr_type);
283 append_composite_type_field (rupp_type, "standard_error", void_ptr_type);
284 append_composite_type_field (rupp_type, "current_directory", uni_str_type);
285 append_composite_type_field (rupp_type, "current_directory_handle",
286 void_ptr_type);
287 append_composite_type_field (rupp_type, "dll_path", uni_str_type);
288 append_composite_type_field (rupp_type, "image_path_name", uni_str_type);
289 append_composite_type_field (rupp_type, "command_line", uni_str_type);
290 append_composite_type_field (rupp_type, "environment", void_ptr_type);
291 append_composite_type_field (rupp_type, "starting_x", dword32_type);
292 append_composite_type_field (rupp_type, "starting_y", dword32_type);
293 append_composite_type_field (rupp_type, "count_x", dword32_type);
294 append_composite_type_field (rupp_type, "count_y", dword32_type);
295 append_composite_type_field (rupp_type, "count_chars_x", dword32_type);
296 append_composite_type_field (rupp_type, "count_chars_y", dword32_type);
297 append_composite_type_field (rupp_type, "fill_attribute", dword32_type);
298 append_composite_type_field (rupp_type, "window_flags", dword32_type);
299 append_composite_type_field (rupp_type, "show_window_flags", dword32_type);
300 append_composite_type_field_aligned (rupp_type, "window_title",
301 uni_str_type,
302 TYPE_LENGTH (void_ptr_type));
303 append_composite_type_field (rupp_type, "desktop_info", uni_str_type);
304 append_composite_type_field (rupp_type, "shell_info", uni_str_type);
305 append_composite_type_field (rupp_type, "runtime_data", uni_str_type);
306
307 rupp_ptr_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
308 NULL, rupp_type);
309
310
311 /* struct process environment block */
312 peb_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
313 TYPE_NAME (peb_type) = xstrdup ("peb");
314
315 /* First bytes contain several flags. */
316 append_composite_type_field (peb_type, "flags", dword_ptr_type);
317 append_composite_type_field (peb_type, "mutant", void_ptr_type);
318 append_composite_type_field (peb_type, "image_base_address", void_ptr_type);
319 append_composite_type_field (peb_type, "ldr", peb_ldr_ptr_type);
320 append_composite_type_field (peb_type, "process_parameters", rupp_ptr_type);
321 append_composite_type_field (peb_type, "sub_system_data", void_ptr_type);
322 append_composite_type_field (peb_type, "process_heap", void_ptr_type);
323 append_composite_type_field (peb_type, "fast_peb_lock", void_ptr_type);
324 peb_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
325 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
326 NULL);
327 TYPE_TARGET_TYPE (peb_ptr_type) = peb_type;
328
329
330 /* struct thread information block */
331 tib_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
332 TYPE_NAME (tib_type) = xstrdup ("tib");
333
334 /* uint32_t current_seh; %fs:0x0000 */
335 append_composite_type_field (tib_type, "current_seh", seh_ptr_type);
336 /* uint32_t current_top_of_stack; %fs:0x0004 */
337 append_composite_type_field (tib_type, "current_top_of_stack",
338 void_ptr_type);
339 /* uint32_t current_bottom_of_stack; %fs:0x0008 */
340 append_composite_type_field (tib_type, "current_bottom_of_stack",
341 void_ptr_type);
342 /* uint32_t sub_system_tib; %fs:0x000c */
343 append_composite_type_field (tib_type, "sub_system_tib", void_ptr_type);
344
345 /* uint32_t fiber_data; %fs:0x0010 */
346 append_composite_type_field (tib_type, "fiber_data", void_ptr_type);
347 /* uint32_t arbitrary_data_slot; %fs:0x0014 */
348 append_composite_type_field (tib_type, "arbitrary_data_slot", void_ptr_type);
349 /* uint32_t linear_address_tib; %fs:0x0018 */
350 append_composite_type_field (tib_type, "linear_address_tib", void_ptr_type);
351 /* uint32_t environment_pointer; %fs:0x001c */
352 append_composite_type_field (tib_type, "environment_pointer", void_ptr_type);
353 /* uint32_t process_id; %fs:0x0020 */
354 append_composite_type_field (tib_type, "process_id", dword_ptr_type);
355 /* uint32_t current_thread_id; %fs:0x0024 */
356 append_composite_type_field (tib_type, "thread_id", dword_ptr_type);
357 /* uint32_t active_rpc_handle; %fs:0x0028 */
358 append_composite_type_field (tib_type, "active_rpc_handle", dword_ptr_type);
359 /* uint32_t thread_local_storage; %fs:0x002c */
360 append_composite_type_field (tib_type, "thread_local_storage",
361 void_ptr_type);
362 /* uint32_t process_environment_block; %fs:0x0030 */
363 append_composite_type_field (tib_type, "process_environment_block",
364 peb_ptr_type);
365 /* uint32_t last_error_number; %fs:0x0034 */
366 append_composite_type_field (tib_type, "last_error_number", dword_ptr_type);
367
368 tib_ptr_type = arch_type (gdbarch, TYPE_CODE_PTR,
369 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
370 NULL);
371 TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
372
373 windows_gdbarch_data->tib_ptr_type = tib_ptr_type;
374
375 return tib_ptr_type;
376 }
377
378 /* The $_tlb convenience variable is a bit special. We don't know
379 for sure the type of the value until we actually have a chance to
380 fetch the data. The type can change depending on gdbarch, so it is
381 also dependent on which thread you have selected. */
382
383 /* This function implements the lval_computed support for reading a
384 $_tlb value. */
385
386 static void
387 tlb_value_read (struct value *val)
388 {
389 CORE_ADDR tlb;
390 struct type *type = check_typedef (value_type (val));
391
392 if (!target_get_tib_address (inferior_ptid, &tlb))
393 error (_("Unable to read tlb"));
394 store_typed_address (value_contents_raw (val), type, tlb);
395 }
396
397 /* This function implements the lval_computed support for writing a
398 $_tlb value. */
399
400 static void
401 tlb_value_write (struct value *v, struct value *fromval)
402 {
403 error (_("Impossible to change the Thread Local Base"));
404 }
405
406 static const struct lval_funcs tlb_value_funcs =
407 {
408 tlb_value_read,
409 tlb_value_write
410 };
411
412
413 /* Return a new value with the correct type for the tlb object of
414 the current thread using architecture GDBARCH. Return a void value
415 if there's no object available. */
416
417 static struct value *
418 tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
419 {
420 if (target_has_stack && inferior_ptid != null_ptid)
421 {
422 struct type *type = windows_get_tlb_type (gdbarch);
423 return allocate_computed_value (type, &tlb_value_funcs, NULL);
424 }
425
426 return allocate_value (builtin_type (gdbarch)->builtin_void);
427 }
428
429
430 /* Display thread information block of a given thread. */
431
432 static int
433 display_one_tib (ptid_t ptid)
434 {
435 gdb_byte *tib = NULL;
436 gdb_byte *index;
437 CORE_ADDR thread_local_base;
438 ULONGEST i, val, max, max_name, size, tib_size;
439 ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch ());
440 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
441
442 if (sizeof_ptr == 64)
443 {
444 size = sizeof (uint64_t);
445 tib_size = sizeof (thread_information_64);
446 max = MAX_TIB64;
447 }
448 else
449 {
450 size = sizeof (uint32_t);
451 tib_size = sizeof (thread_information_32);
452 max = MAX_TIB32;
453 }
454
455 max_name = max;
456
457 if (maint_display_all_tib)
458 {
459 tib_size = FULL_TIB_SIZE;
460 max = tib_size / size;
461 }
462
463 tib = (gdb_byte *) alloca (tib_size);
464
465 if (target_get_tib_address (ptid, &thread_local_base) == 0)
466 {
467 printf_filtered (_("Unable to get thread local base for %s\n"),
468 target_pid_to_str (ptid).c_str ());
469 return -1;
470 }
471
472 if (target_read (current_top_target (), TARGET_OBJECT_MEMORY,
473 NULL, tib, thread_local_base, tib_size) != tib_size)
474 {
475 printf_filtered (_("Unable to read thread information "
476 "block for %s at address %s\n"),
477 target_pid_to_str (ptid).c_str (),
478 paddress (target_gdbarch (), thread_local_base));
479 return -1;
480 }
481
482 printf_filtered (_("Thread Information Block %s at %s\n"),
483 target_pid_to_str (ptid).c_str (),
484 paddress (target_gdbarch (), thread_local_base));
485
486 index = (gdb_byte *) tib;
487
488 /* All fields have the size of a pointer, this allows to iterate
489 using the same for loop for both layouts. */
490 for (i = 0; i < max; i++)
491 {
492 val = extract_unsigned_integer (index, size, byte_order);
493 if (i < max_name)
494 printf_filtered (_("%s is 0x%s\n"), TIB_NAME[i], phex (val, size));
495 else if (val != 0)
496 printf_filtered (_("TIB[0x%s] is 0x%s\n"), phex (i * size, 2),
497 phex (val, size));
498 index += size;
499 }
500 return 1;
501 }
502
503 /* Display thread information block of the current thread. */
504
505 static void
506 display_tib (const char * args, int from_tty)
507 {
508 if (inferior_ptid != null_ptid)
509 display_one_tib (inferior_ptid);
510 }
511
512 void
513 windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
514 CORE_ADDR *text_offset_cached,
515 struct gdbarch *gdbarch, struct obstack *obstack)
516 {
517 CORE_ADDR text_offset = text_offset_cached ? *text_offset_cached : 0;
518
519 obstack_grow_str (obstack, "<library name=\"");
520 std::string p = xml_escape_text (so_name);
521 obstack_grow_str (obstack, p.c_str ());
522 obstack_grow_str (obstack, "\"><segment address=\"");
523
524 if (!text_offset)
525 {
526 gdb_bfd_ref_ptr dll (gdb_bfd_open (so_name, gnutarget, -1));
527 /* The following calls are OK even if dll is NULL.
528 The default value 0x1000 is returned by pe_text_section_offset
529 in that case. */
530 text_offset = pe_text_section_offset (dll.get ());
531 if (text_offset_cached)
532 *text_offset_cached = text_offset;
533 }
534
535 obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
536 obstack_grow_str (obstack, "\"/></library>");
537 }
538
539 /* Implement the "iterate_over_objfiles_in_search_order" gdbarch
540 method. It searches all objfiles, starting with CURRENT_OBJFILE
541 first (if not NULL).
542
543 On Windows, the system behaves a little differently when two
544 objfiles each define a global symbol using the same name, compared
545 to other platforms such as GNU/Linux for instance. On GNU/Linux,
546 all instances of the symbol effectively get merged into a single
547 one, but on Windows, they remain distinct.
548
549 As a result, it usually makes sense to start global symbol searches
550 with the current objfile before expanding it to all other objfiles.
551 This helps for instance when a user debugs some code in a DLL that
552 refers to a global variable defined inside that DLL. When trying
553 to print the value of that global variable, it would be unhelpful
554 to print the value of another global variable defined with the same
555 name, but in a different DLL. */
556
557 static void
558 windows_iterate_over_objfiles_in_search_order
559 (struct gdbarch *gdbarch,
560 iterate_over_objfiles_in_search_order_cb_ftype *cb,
561 void *cb_data, struct objfile *current_objfile)
562 {
563 int stop;
564
565 if (current_objfile)
566 {
567 stop = cb (current_objfile, cb_data);
568 if (stop)
569 return;
570 }
571
572 for (objfile *objfile : current_program_space->objfiles ())
573 {
574 if (objfile != current_objfile)
575 {
576 stop = cb (objfile, cb_data);
577 if (stop)
578 return;
579 }
580 }
581 }
582
583 static void
584 show_maint_show_all_tib (struct ui_file *file, int from_tty,
585 struct cmd_list_element *c, const char *value)
586 {
587 fprintf_filtered (file, _("Show all non-zero elements of "
588 "Thread Information Block is %s.\n"), value);
589 }
590
591 static void
592 info_w32_command (const char *args, int from_tty)
593 {
594 help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
595 }
596
597 static int w32_prefix_command_valid = 0;
598 void
599 init_w32_command_list (void)
600 {
601 if (!w32_prefix_command_valid)
602 {
603 add_prefix_cmd ("w32", class_info, info_w32_command,
604 _("Print information specific to Win32 debugging."),
605 &info_w32_cmdlist, "info w32 ", 0, &infolist);
606 w32_prefix_command_valid = 1;
607 }
608 }
609
610 /* Implementation of `gdbarch_gdb_signal_to_target'. */
611
612 static int
613 windows_gdb_signal_to_target (struct gdbarch *gdbarch, enum gdb_signal signal)
614 {
615 switch (signal)
616 {
617 case GDB_SIGNAL_0:
618 return 0;
619 case GDB_SIGNAL_HUP:
620 return WINDOWS_SIGHUP;
621 case GDB_SIGNAL_INT:
622 return WINDOWS_SIGINT;
623 case GDB_SIGNAL_QUIT:
624 return WINDOWS_SIGQUIT;
625 case GDB_SIGNAL_ILL:
626 return WINDOWS_SIGILL;
627 case GDB_SIGNAL_TRAP:
628 return WINDOWS_SIGTRAP;
629 case GDB_SIGNAL_ABRT:
630 return WINDOWS_SIGABRT;
631 case GDB_SIGNAL_EMT:
632 return WINDOWS_SIGEMT;
633 case GDB_SIGNAL_FPE:
634 return WINDOWS_SIGFPE;
635 case GDB_SIGNAL_KILL:
636 return WINDOWS_SIGKILL;
637 case GDB_SIGNAL_BUS:
638 return WINDOWS_SIGBUS;
639 case GDB_SIGNAL_SEGV:
640 return WINDOWS_SIGSEGV;
641 case GDB_SIGNAL_SYS:
642 return WINDOWS_SIGSYS;
643 case GDB_SIGNAL_PIPE:
644 return WINDOWS_SIGPIPE;
645 case GDB_SIGNAL_ALRM:
646 return WINDOWS_SIGALRM;
647 case GDB_SIGNAL_TERM:
648 return WINDOWS_SIGTERM;
649 #ifdef __CYGWIN__
650 case GDB_SIGNAL_URG:
651 return WINDOWS_SIGURG;
652 case GDB_SIGNAL_STOP:
653 return WINDOWS_SIGSTOP;
654 case GDB_SIGNAL_TSTP:
655 return WINDOWS_SIGTSTP;
656 case GDB_SIGNAL_CONT:
657 return WINDOWS_SIGCONT;
658 case GDB_SIGNAL_CHLD:
659 return WINDOWS_SIGCHLD;
660 case GDB_SIGNAL_TTIN:
661 return WINDOWS_SIGTTIN;
662 case GDB_SIGNAL_TTOU:
663 return WINDOWS_SIGTTOU;
664 case GDB_SIGNAL_IO:
665 return WINDOWS_SIGIO;
666 case GDB_SIGNAL_XCPU:
667 return WINDOWS_SIGXCPU;
668 case GDB_SIGNAL_XFSZ:
669 return WINDOWS_SIGXFSZ;
670 case GDB_SIGNAL_VTALRM:
671 return WINDOWS_SIGVTALRM;
672 case GDB_SIGNAL_PROF:
673 return WINDOWS_SIGPROF;
674 case GDB_SIGNAL_WINCH:
675 return WINDOWS_SIGWINCH;
676 case GDB_SIGNAL_PWR:
677 return WINDOWS_SIGLOST;
678 case GDB_SIGNAL_USR1:
679 return WINDOWS_SIGUSR1;
680 case GDB_SIGNAL_USR2:
681 return WINDOWS_SIGUSR2;
682 #endif /* __CYGWIN__ */
683 }
684 return -1;
685 }
686
687 struct enum_value_name
688 {
689 uint32_t value;
690 const char *name;
691 };
692
693 /* Allocate a TYPE_CODE_ENUM type structure with its named values. */
694
695 static struct type *
696 create_enum (struct gdbarch *gdbarch, int bit, const char *name,
697 const struct enum_value_name *values, int count)
698 {
699 struct type *type;
700 int i;
701
702 type = arch_type (gdbarch, TYPE_CODE_ENUM, bit, name);
703 TYPE_NFIELDS (type) = count;
704 TYPE_FIELDS (type) = (struct field *)
705 TYPE_ZALLOC (type, sizeof (struct field) * count);
706 TYPE_UNSIGNED (type) = 1;
707
708 for (i = 0; i < count; i++)
709 {
710 TYPE_FIELD_NAME (type, i) = values[i].name;
711 SET_FIELD_ENUMVAL (TYPE_FIELD (type, i), values[i].value);
712 }
713
714 return type;
715 }
716
717 static const struct enum_value_name exception_values[] =
718 {
719 { 0x40000015, "FATAL_APP_EXIT" },
720 { 0x40010005, "DBG_CONTROL_C" },
721 { 0x40010008, "DBG_CONTROL_BREAK" },
722 { 0x80000002, "DATATYPE_MISALIGNMENT" },
723 { 0x80000003, "BREAKPOINT" },
724 { 0x80000004, "SINGLE_STEP" },
725 { 0xC0000005, "ACCESS_VIOLATION" },
726 { 0xC0000006, "IN_PAGE_ERROR" },
727 { 0xC000001D, "ILLEGAL_INSTRUCTION" },
728 { 0xC0000025, "NONCONTINUABLE_EXCEPTION" },
729 { 0xC0000026, "INVALID_DISPOSITION" },
730 { 0xC000008C, "ARRAY_BOUNDS_EXCEEDED" },
731 { 0xC000008D, "FLOAT_DENORMAL_OPERAND" },
732 { 0xC000008E, "FLOAT_DIVIDE_BY_ZERO" },
733 { 0xC000008F, "FLOAT_INEXACT_RESULT" },
734 { 0xC0000090, "FLOAT_INVALID_OPERATION" },
735 { 0xC0000091, "FLOAT_OVERFLOW" },
736 { 0xC0000092, "FLOAT_STACK_CHECK" },
737 { 0xC0000093, "FLOAT_UNDERFLOW" },
738 { 0xC0000094, "INTEGER_DIVIDE_BY_ZERO" },
739 { 0xC0000095, "INTEGER_OVERFLOW" },
740 { 0xC0000096, "PRIV_INSTRUCTION" },
741 { 0xC00000FD, "STACK_OVERFLOW" },
742 { 0xC0000409, "FAST_FAIL" },
743 };
744
745 static const struct enum_value_name violation_values[] =
746 {
747 { 0, "READ_ACCESS_VIOLATION" },
748 { 1, "WRITE_ACCESS_VIOLATION" },
749 { 8, "DATA_EXECUTION_PREVENTION_VIOLATION" },
750 };
751
752 /* Implement the "get_siginfo_type" gdbarch method. */
753
754 static struct type *
755 windows_get_siginfo_type (struct gdbarch *gdbarch)
756 {
757 struct windows_gdbarch_data *windows_gdbarch_data;
758 struct type *dword_type, *pvoid_type, *ulongptr_type;
759 struct type *code_enum, *violation_enum;
760 struct type *violation_type, *para_type, *siginfo_ptr_type, *siginfo_type;
761
762 windows_gdbarch_data = get_windows_gdbarch_data (gdbarch);
763 if (windows_gdbarch_data->siginfo_type != NULL)
764 return windows_gdbarch_data->siginfo_type;
765
766 dword_type = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch),
767 1, "DWORD");
768 pvoid_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch), "PVOID",
769 builtin_type (gdbarch)->builtin_void);
770 ulongptr_type = arch_integer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
771 1, "ULONG_PTR");
772
773 /* ExceptionCode value names */
774 code_enum = create_enum (gdbarch, gdbarch_int_bit (gdbarch),
775 "ExceptionCode", exception_values,
776 ARRAY_SIZE (exception_values));
777
778 /* ACCESS_VIOLATION type names */
779 violation_enum = create_enum (gdbarch, gdbarch_ptr_bit (gdbarch),
780 "ViolationType", violation_values,
781 ARRAY_SIZE (violation_values));
782
783 /* ACCESS_VIOLATION information */
784 violation_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
785 append_composite_type_field (violation_type, "Type", violation_enum);
786 append_composite_type_field (violation_type, "Address", pvoid_type);
787
788 /* Unnamed union of the documented field ExceptionInformation,
789 and the alternative AccessViolationInformation (which displays
790 human-readable values for ExceptionCode ACCESS_VIOLATION). */
791 para_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_UNION);
792 append_composite_type_field (para_type, "ExceptionInformation",
793 lookup_array_range_type (ulongptr_type, 0, 14));
794 append_composite_type_field (para_type, "AccessViolationInformation",
795 violation_type);
796
797 siginfo_type = arch_composite_type (gdbarch, "EXCEPTION_RECORD",
798 TYPE_CODE_STRUCT);
799 siginfo_ptr_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
800 NULL, siginfo_type);
801
802 /* ExceptionCode is documented as type DWORD, but here a helper
803 enum type is used instead to display a human-readable value. */
804 append_composite_type_field (siginfo_type, "ExceptionCode", code_enum);
805 append_composite_type_field (siginfo_type, "ExceptionFlags", dword_type);
806 append_composite_type_field (siginfo_type, "ExceptionRecord",
807 siginfo_ptr_type);
808 append_composite_type_field (siginfo_type, "ExceptionAddress",
809 pvoid_type);
810 append_composite_type_field (siginfo_type, "NumberParameters", dword_type);
811 /* The 64-bit variant needs some padding. */
812 append_composite_type_field_aligned (siginfo_type, "",
813 para_type, TYPE_LENGTH (ulongptr_type));
814
815 windows_gdbarch_data->siginfo_type = siginfo_type;
816
817 return siginfo_type;
818 }
819
820 /* Implement the "solib_create_inferior_hook" target_so_ops method. */
821
822 static void
823 windows_solib_create_inferior_hook (int from_tty)
824 {
825 CORE_ADDR exec_base = 0;
826
827 /* Find base address of main executable in
828 TIB->process_environment_block->image_base_address. */
829 struct gdbarch *gdbarch = target_gdbarch ();
830 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
831 int ptr_bytes;
832 int peb_offset; /* Offset of process_environment_block in TIB. */
833 int base_offset; /* Offset of image_base_address in PEB. */
834 if (gdbarch_ptr_bit (gdbarch) == 32)
835 {
836 ptr_bytes = 4;
837 peb_offset = 48;
838 base_offset = 8;
839 }
840 else
841 {
842 ptr_bytes = 8;
843 peb_offset = 96;
844 base_offset = 16;
845 }
846 CORE_ADDR tlb;
847 gdb_byte buf[8];
848 if (target_has_execution
849 && target_get_tib_address (inferior_ptid, &tlb)
850 && !target_read_memory (tlb + peb_offset, buf, ptr_bytes))
851 {
852 CORE_ADDR peb = extract_unsigned_integer (buf, ptr_bytes, byte_order);
853 if (!target_read_memory (peb + base_offset, buf, ptr_bytes))
854 exec_base = extract_unsigned_integer (buf, ptr_bytes, byte_order);
855 }
856
857 /* Rebase executable if the base address changed because of ASLR. */
858 if (symfile_objfile != nullptr && exec_base != 0)
859 {
860 CORE_ADDR vmaddr = pe_data (exec_bfd)->pe_opthdr.ImageBase;
861 if (vmaddr != exec_base)
862 objfile_rebase (symfile_objfile, exec_base - vmaddr);
863 }
864 }
865
866 static struct target_so_ops windows_so_ops;
867
868 /* To be called from the various GDB_OSABI_CYGWIN handlers for the
869 various Windows architectures and machine types. */
870
871 void
872 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
873 {
874 set_gdbarch_wchar_bit (gdbarch, 16);
875 set_gdbarch_wchar_signed (gdbarch, 0);
876
877 /* Canonical paths on this target look like
878 `c:\Program Files\Foo App\mydll.dll', for example. */
879 set_gdbarch_has_dos_based_file_system (gdbarch, 1);
880
881 set_gdbarch_iterate_over_objfiles_in_search_order
882 (gdbarch, windows_iterate_over_objfiles_in_search_order);
883
884 set_gdbarch_gdb_signal_to_target (gdbarch, windows_gdb_signal_to_target);
885
886 windows_so_ops = solib_target_so_ops;
887 windows_so_ops.solib_create_inferior_hook
888 = windows_solib_create_inferior_hook;
889 set_solib_ops (gdbarch, &windows_so_ops);
890
891 set_gdbarch_get_siginfo_type (gdbarch, windows_get_siginfo_type);
892 }
893
894 /* Implementation of `tlb' variable. */
895
896 static const struct internalvar_funcs tlb_funcs =
897 {
898 tlb_make_value,
899 NULL,
900 NULL
901 };
902
903 /* Layout of an element of a PE's Import Directory Table. Based on:
904
905 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-directory-table
906 */
907
908 struct pe_import_directory_entry
909 {
910 uint32_t import_lookup_table_rva;
911 uint32_t timestamp;
912 uint32_t forwarder_chain;
913 uint32_t name_rva;
914 uint32_t import_address_table_rva;
915 };
916
917 gdb_static_assert (sizeof (pe_import_directory_entry) == 20);
918
919 /* See windows-tdep.h. */
920
921 bool
922 is_linked_with_cygwin_dll (bfd *abfd)
923 {
924 /* The list of DLLs a PE is linked to is in the .idata section. See:
925
926 https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#the-idata-section
927 */
928 asection *idata_section = bfd_get_section_by_name (abfd, ".idata");
929 if (idata_section == nullptr)
930 return false;
931
932 /* Find the virtual address of the .idata section. We must subtract this
933 from the RVAs (relative virtual addresses) to obtain an offset in the
934 section. */
935 bfd_vma idata_addr
936 = pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
937
938 /* Get the section's data. */
939 gdb::byte_vector idata_contents;
940 if (!gdb_bfd_get_full_section_contents (abfd, idata_section, &idata_contents))
941 {
942 warning (_("Failed to get content of .idata section."));
943 return false;
944 }
945
946 size_t idata_size = idata_contents.size ();
947 const gdb_byte *iter = idata_contents.data ();
948 const gdb_byte *end = idata_contents.data () + idata_size;
949 const pe_import_directory_entry null_dir_entry = { 0 };
950
951 /* Iterate through all directory entries. */
952 while (true)
953 {
954 /* Is there enough space left in the section for another entry? */
955 if (iter + sizeof (pe_import_directory_entry) > end)
956 {
957 warning (_("Failed to parse .idata section: unexpected end of "
958 ".idata section."));
959 break;
960 }
961
962 pe_import_directory_entry *dir_entry = (pe_import_directory_entry *) iter;
963
964 /* Is it the end of list marker? */
965 if (memcmp (dir_entry, &null_dir_entry,
966 sizeof (pe_import_directory_entry)) == 0)
967 break;
968
969 bfd_vma name_addr = dir_entry->name_rva;
970
971 /* If the name's virtual address is smaller than the section's virtual
972 address, there's a problem. */
973 if (name_addr < idata_addr
974 || name_addr >= (idata_addr + idata_size))
975 {
976 warning (_("\
977 Failed to parse .idata section: name's virtual address (0x%" BFD_VMA_FMT "x) \
978 is outside .idata section's range [0x%" BFD_VMA_FMT "x, 0x%" BFD_VMA_FMT "x[."),
979 name_addr, idata_addr, idata_addr + idata_size);
980 break;
981 }
982
983 const gdb_byte *name = &idata_contents[name_addr - idata_addr];
984
985 /* Make sure we don't overshoot the end of the section with the streq. */
986 if (name + sizeof (CYGWIN_DLL_NAME) > end)
987 continue;
988
989 /* Finally, check if this is the dll name we are looking for. */
990 if (streq ((const char *) name, CYGWIN_DLL_NAME))
991 return true;
992
993 iter += sizeof (pe_import_directory_entry);
994 }
995
996 return false;
997 }
998
999 void _initialize_windows_tdep ();
1000 void
1001 _initialize_windows_tdep ()
1002 {
1003 windows_gdbarch_data_handle
1004 = gdbarch_data_register_post_init (init_windows_gdbarch_data);
1005
1006 init_w32_command_list ();
1007 add_cmd ("thread-information-block", class_info, display_tib,
1008 _("Display thread information block."),
1009 &info_w32_cmdlist);
1010 add_alias_cmd ("tib", "thread-information-block", class_info, 1,
1011 &info_w32_cmdlist);
1012
1013 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
1014 &maint_display_all_tib, _("\
1015 Set whether to display all non-zero fields of thread information block."), _("\
1016 Show whether to display all non-zero fields of thread information block."), _("\
1017 Use \"on\" to enable, \"off\" to disable.\n\
1018 If enabled, all non-zero fields of thread information block are displayed,\n\
1019 even if their meaning is unknown."),
1020 NULL,
1021 show_maint_show_all_tib,
1022 &maintenance_set_cmdlist,
1023 &maintenance_show_cmdlist);
1024
1025 /* Explicitly create without lookup, since that tries to create a
1026 value with a void typed value, and when we get here, gdbarch
1027 isn't initialized yet. At this point, we're quite sure there
1028 isn't another convenience variable of the same name. */
1029 create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
1030 }