gdb: select "Cygwin" OS ABI for Cygwin binaries
[deliverable/binutils-gdb.git] / gdb / windows-tdep.c
CommitLineData
b811d2c2 1/* Copyright (C) 2008-2020 Free Software Foundation, Inc.
bfb87e33
JB
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"
31b060a2 19#include "windows-tdep.h"
bfb87e33
JB
20#include "gdb_obstack.h"
21#include "xml-support.h"
711e434b
PM
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"
a8e1bb34 29#include "objfiles.h"
3999122f
PM
30#include "symfile.h"
31#include "coff-pe-read.h"
32#include "gdb_bfd.h"
33#include "complaints.h"
64870a42
YQ
34#include "solib.h"
35#include "solib-target.h"
2938e6cf 36#include "gdbcore.h"
584cf46d
HD
37#include "coff/internal.h"
38#include "libcoff.h"
39#include "solist.h"
711e434b 40
8db52437
SM
41#define CYGWIN_DLL_NAME "cygwin1.dll"
42
559e7e50
EZ
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
50enum
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__
89a65580 58 WINDOWS_SIGABRT = 6,
559e7e50
EZ
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
711e434b
PM
94struct cmd_list_element *info_w32_cmdlist;
95
96typedef 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 }
113thread_information_32;
114
115typedef 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 }
132thread_information_64;
133
134
135static 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
581e13c1
MS
153static const int MAX_TIB32 =
154 sizeof (thread_information_32) / sizeof (uint32_t);
155static const int MAX_TIB64 =
156 sizeof (thread_information_64) / sizeof (uint64_t);
711e434b
PM
157static const int FULL_TIB_SIZE = 0x1000;
158
491144b5 159static bool maint_display_all_tib = false;
711e434b 160
7928d571
HD
161static struct gdbarch_data *windows_gdbarch_data_handle;
162
163struct windows_gdbarch_data
164{
165 struct type *siginfo_type;
cd5900f3 166 struct type *tib_ptr_type; /* Type of thread information block */
7928d571
HD
167};
168
169/* Allocate windows_gdbarch_data for an arch. */
170
171static void *
172init_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
179static struct windows_gdbarch_data *
180get_windows_gdbarch_data (struct gdbarch *gdbarch)
181{
182 return ((struct windows_gdbarch_data *)
183 gdbarch_data (gdbarch, windows_gdbarch_data_handle));
184}
185
711e434b
PM
186/* Define Thread Local Base pointer type. */
187
188static struct type *
189windows_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;
870f88f7 193 struct type *peb_type, *peb_ptr_type, *list_type;
711e434b
PM
194 struct type *module_list_ptr_type;
195 struct type *tib_type, *seh_type, *tib_ptr_type, *seh_ptr_type;
e0cdfe3c
HD
196 struct type *word_type, *wchar_type, *wchar_ptr_type;
197 struct type *uni_str_type, *rupp_type, *rupp_ptr_type;
711e434b 198
cd5900f3
HD
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
711e434b
PM
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");
e0cdfe3c
HD
208 word_type = arch_integer_type (gdbarch, 16,
209 1, "WORD");
210 wchar_type = arch_integer_type (gdbarch, 16,
211 1, "wchar_t");
711e434b 212 void_ptr_type = lookup_pointer_type (builtin_type (gdbarch)->builtin_void);
e0cdfe3c
HD
213 wchar_ptr_type = arch_pointer_type (gdbarch, gdbarch_ptr_bit (gdbarch),
214 NULL, wchar_type);
711e434b
PM
215
216 /* list entry */
217
218 list_type = arch_composite_type (gdbarch, NULL, TYPE_CODE_STRUCT);
219 TYPE_NAME (list_type) = xstrdup ("list");
220
711e434b
PM
221 module_list_ptr_type = void_ptr_type;
222
581e13c1
MS
223 append_composite_type_field (list_type, "forward_list",
224 module_list_ptr_type);
711e434b
PM
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,
77b7c781
UW
234 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
235 NULL);
711e434b
PM
236 TYPE_TARGET_TYPE (seh_ptr_type) = seh_type;
237
238 append_composite_type_field (seh_type, "next_seh", seh_ptr_type);
e8e6c82e
PM
239 append_composite_type_field (seh_type, "handler",
240 builtin_type (gdbarch)->builtin_func_ptr);
711e434b
PM
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,
77b7c781
UW
255 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
256 NULL);
711e434b
PM
257 TYPE_TARGET_TYPE (peb_ldr_ptr_type) = peb_ldr_type;
258
e0cdfe3c
HD
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
711e434b
PM
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);
e0cdfe3c 320 append_composite_type_field (peb_type, "process_parameters", rupp_ptr_type);
711e434b
PM
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,
77b7c781
UW
325 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
326 NULL);
711e434b
PM
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 */
581e13c1
MS
337 append_composite_type_field (tib_type, "current_top_of_stack",
338 void_ptr_type);
711e434b
PM
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 */
581e13c1
MS
360 append_composite_type_field (tib_type, "thread_local_storage",
361 void_ptr_type);
711e434b
PM
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,
77b7c781
UW
369 TYPE_LENGTH (void_ptr_type) * TARGET_CHAR_BIT,
370 NULL);
711e434b
PM
371 TYPE_TARGET_TYPE (tib_ptr_type) = tib_type;
372
cd5900f3 373 windows_gdbarch_data->tib_ptr_type = tib_ptr_type;
ea1fae46 374
711e434b
PM
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
386static void
387tlb_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
400static void
401tlb_value_write (struct value *v, struct value *fromval)
402{
403 error (_("Impossible to change the Thread Local Base"));
404}
405
c8f2448a 406static const struct lval_funcs tlb_value_funcs =
711e434b
PM
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
417static struct value *
22d2b532 418tlb_make_value (struct gdbarch *gdbarch, struct internalvar *var, void *ignore)
711e434b 419{
d7e15655 420 if (target_has_stack && inferior_ptid != null_ptid)
711e434b
PM
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
432static int
433display_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;
f5656ead
TT
439 ULONGEST sizeof_ptr = gdbarch_ptr_bit (target_gdbarch ());
440 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
711e434b
PM
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
224c3ddb 463 tib = (gdb_byte *) alloca (tib_size);
711e434b
PM
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"),
a068643d 468 target_pid_to_str (ptid).c_str ());
711e434b
PM
469 return -1;
470 }
471
8b88a78e 472 if (target_read (current_top_target (), TARGET_OBJECT_MEMORY,
711e434b
PM
473 NULL, tib, thread_local_base, tib_size) != tib_size)
474 {
581e13c1
MS
475 printf_filtered (_("Unable to read thread information "
476 "block for %s at address %s\n"),
a068643d
TT
477 target_pid_to_str (ptid).c_str (),
478 paddress (target_gdbarch (), thread_local_base));
711e434b
PM
479 return -1;
480 }
481
482 printf_filtered (_("Thread Information Block %s at %s\n"),
a068643d 483 target_pid_to_str (ptid).c_str (),
f5656ead 484 paddress (target_gdbarch (), thread_local_base));
711e434b
PM
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
5d5658a1 503/* Display thread information block of the current thread. */
711e434b
PM
504
505static void
c281872e 506display_tib (const char * args, int from_tty)
711e434b 507{
d7e15655 508 if (inferior_ptid != null_ptid)
711e434b
PM
509 display_one_tib (inferior_ptid);
510}
bfb87e33
JB
511
512void
dc05df57 513windows_xfer_shared_library (const char* so_name, CORE_ADDR load_addr,
c162ed3e 514 CORE_ADDR *text_offset_cached,
5af949e3 515 struct gdbarch *gdbarch, struct obstack *obstack)
bfb87e33 516{
c162ed3e 517 CORE_ADDR text_offset = text_offset_cached ? *text_offset_cached : 0;
3999122f 518
bfb87e33 519 obstack_grow_str (obstack, "<library name=\"");
5e187554
SM
520 std::string p = xml_escape_text (so_name);
521 obstack_grow_str (obstack, p.c_str ());
5af949e3 522 obstack_grow_str (obstack, "\"><segment address=\"");
c162ed3e
HD
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
3999122f 535 obstack_grow_str (obstack, paddress (gdbarch, load_addr + text_offset));
bfb87e33
JB
536 obstack_grow_str (obstack, "\"/></library>");
537}
711e434b 538
a8e1bb34
JB
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
64870a42 557static void
a8e1bb34
JB
558windows_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;
a8e1bb34
JB
564
565 if (current_objfile)
566 {
567 stop = cb (current_objfile, cb_data);
568 if (stop)
569 return;
570 }
571
2030c079 572 for (objfile *objfile : current_program_space->objfiles ())
a8e1bb34
JB
573 {
574 if (objfile != current_objfile)
575 {
576 stop = cb (objfile, cb_data);
577 if (stop)
578 return;
579 }
580 }
581}
582
711e434b
PM
583static void
584show_maint_show_all_tib (struct ui_file *file, int from_tty,
585 struct cmd_list_element *c, const char *value)
586{
581e13c1
MS
587 fprintf_filtered (file, _("Show all non-zero elements of "
588 "Thread Information Block is %s.\n"), value);
711e434b
PM
589}
590
591static void
981a3fb3 592info_w32_command (const char *args, int from_tty)
711e434b
PM
593{
594 help_list (info_w32_cmdlist, "info w32 ", class_info, gdb_stdout);
595}
596
597static int w32_prefix_command_valid = 0;
598void
599init_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
559e7e50
EZ
610/* Implementation of `gdbarch_gdb_signal_to_target'. */
611
612static int
613windows_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
6bafc845
HD
687struct 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
695static struct type *
696create_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
717static 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
745static 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
7928d571
HD
752/* Implement the "get_siginfo_type" gdbarch method. */
753
754static struct type *
755windows_get_siginfo_type (struct gdbarch *gdbarch)
756{
757 struct windows_gdbarch_data *windows_gdbarch_data;
758 struct type *dword_type, *pvoid_type, *ulongptr_type;
6bafc845
HD
759 struct type *code_enum, *violation_enum;
760 struct type *violation_type, *para_type, *siginfo_ptr_type, *siginfo_type;
7928d571
HD
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
6bafc845
HD
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
7928d571
HD
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
6bafc845
HD
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);
7928d571
HD
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. */
6bafc845
HD
812 append_composite_type_field_aligned (siginfo_type, "",
813 para_type, TYPE_LENGTH (ulongptr_type));
7928d571
HD
814
815 windows_gdbarch_data->siginfo_type = siginfo_type;
816
817 return siginfo_type;
818}
819
584cf46d
HD
820/* Implement the "solib_create_inferior_hook" target_so_ops method. */
821
822static void
823windows_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];
b7d64b29
HD
848 if (target_has_execution
849 && target_get_tib_address (inferior_ptid, &tlb)
584cf46d
HD
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
866static struct target_so_ops windows_so_ops;
867
64870a42
YQ
868/* To be called from the various GDB_OSABI_CYGWIN handlers for the
869 various Windows architectures and machine types. */
870
871void
872windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
873{
53375380
PA
874 set_gdbarch_wchar_bit (gdbarch, 16);
875 set_gdbarch_wchar_signed (gdbarch, 0);
876
64870a42
YQ
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
559e7e50
EZ
884 set_gdbarch_gdb_signal_to_target (gdbarch, windows_gdb_signal_to_target);
885
584cf46d
HD
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);
7928d571
HD
890
891 set_gdbarch_get_siginfo_type (gdbarch, windows_get_siginfo_type);
64870a42
YQ
892}
893
22d2b532
SDJ
894/* Implementation of `tlb' variable. */
895
896static const struct internalvar_funcs tlb_funcs =
897{
898 tlb_make_value,
899 NULL,
900 NULL
901};
902
8db52437
SM
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
908struct 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
917gdb_static_assert (sizeof (pe_import_directory_entry) == 20);
918
919/* See windows-tdep.h. */
920
921bool
922is_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 /* Map the section's data. */
939 bfd_size_type idata_size;
940 const gdb_byte *const idata_contents
941 = gdb_bfd_map_section (idata_section, &idata_size);
942 if (idata_contents == nullptr)
943 {
944 warning (_("Failed to get content of .idata section."));
945 return false;
946 }
947
948 const gdb_byte *iter = idata_contents;
949 const gdb_byte *end = idata_contents + idata_size;
950 const pe_import_directory_entry null_dir_entry = { 0 };
951
952 /* Iterate through all directory entries. */
953 while (true)
954 {
955 /* Is there enough space left in the section for another entry? */
956 if (iter + sizeof (pe_import_directory_entry) > end)
957 {
958 warning (_("Failed to parse .idata section: unexpected end of "
959 ".idata section."));
960 break;
961 }
962
963 pe_import_directory_entry *dir_entry = (pe_import_directory_entry *) iter;
964
965 /* Is it the end of list marker? */
966 if (memcmp (dir_entry, &null_dir_entry,
967 sizeof (pe_import_directory_entry)) == 0)
968 break;
969
970 bfd_vma name_addr = dir_entry->name_rva;
971
972 /* If the name's virtual address is smaller than the section's virtual
973 address, there's a problem. */
974 if (name_addr < idata_addr
975 || name_addr >= (idata_addr + idata_size))
976 {
977 warning (_("\
978Failed to parse .idata section: name's virtual address (0x%" BFD_VMA_FMT "x) \
979is outside .idata section's range [0x%" BFD_VMA_FMT "x, 0x%" BFD_VMA_FMT "x[."),
980 name_addr, idata_addr, idata_addr + idata_size);
981 break;
982 }
983
984 const gdb_byte *name = &idata_contents[name_addr - idata_addr];
985
986 /* Make sure we don't overshoot the end of the section with the streq. */
987 if (name + sizeof(CYGWIN_DLL_NAME) > end)
988 continue;
989
990 /* Finally, check if this is the dll name we are looking for. */
991 if (streq ((const char *) name, CYGWIN_DLL_NAME))
992 return true;
993
994 iter += sizeof(pe_import_directory_entry);
995 }
996
997 return false;
998}
999
6c265988 1000void _initialize_windows_tdep ();
711e434b 1001void
6c265988 1002_initialize_windows_tdep ()
711e434b 1003{
7928d571
HD
1004 windows_gdbarch_data_handle
1005 = gdbarch_data_register_post_init (init_windows_gdbarch_data);
1006
711e434b
PM
1007 init_w32_command_list ();
1008 add_cmd ("thread-information-block", class_info, display_tib,
1009 _("Display thread information block."),
1010 &info_w32_cmdlist);
1011 add_alias_cmd ("tib", "thread-information-block", class_info, 1,
1012 &info_w32_cmdlist);
1013
1014 add_setshow_boolean_cmd ("show-all-tib", class_maintenance,
1015 &maint_display_all_tib, _("\
1016Set whether to display all non-zero fields of thread information block."), _("\
1017Show whether to display all non-zero fields of thread information block."), _("\
1018Use \"on\" to enable, \"off\" to disable.\n\
1019If enabled, all non-zero fields of thread information block are displayed,\n\
1020even if their meaning is unknown."),
1021 NULL,
1022 show_maint_show_all_tib,
1023 &maintenance_set_cmdlist,
1024 &maintenance_show_cmdlist);
1025
1026 /* Explicitly create without lookup, since that tries to create a
1027 value with a void typed value, and when we get here, gdbarch
1028 isn't initialized yet. At this point, we're quite sure there
1029 isn't another convenience variable of the same name. */
22d2b532 1030 create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL);
711e434b 1031}
This page took 3.933424 seconds and 4 git commands to generate.