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