Arm: Clean up PE GAS testsuite correct THUMB tests.
[deliverable/binutils-gdb.git] / gdb / solib-dsbt.c
CommitLineData
8cd64e00 1/* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
42a4f53d 2 Copyright (C) 2010-2019 Free Software Foundation, Inc.
8cd64e00
YQ
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19
20#include "defs.h"
8cd64e00
YQ
21#include "inferior.h"
22#include "gdbcore.h"
23#include "solib.h"
24#include "solist.h"
25#include "objfiles.h"
26#include "symtab.h"
27#include "language.h"
28#include "command.h"
29#include "gdbcmd.h"
30#include "elf-bfd.h"
cbb099e8 31#include "gdb_bfd.h"
8cd64e00
YQ
32
33#define GOT_MODULE_OFFSET 4
34
35/* Flag which indicates whether internal debug messages should be printed. */
ccce17b0 36static unsigned int solib_dsbt_debug = 0;
8cd64e00
YQ
37
38/* TIC6X pointers are four bytes wide. */
39enum { TIC6X_PTR_SIZE = 4 };
40
41/* Representation of loadmap and related structs for the TIC6X DSBT. */
42
43/* External versions; the size and alignment of the fields should be
44 the same as those on the target. When loaded, the placement of
45 the bits in each field will be the same as on the target. */
46typedef gdb_byte ext_Elf32_Half[2];
47typedef gdb_byte ext_Elf32_Addr[4];
48typedef gdb_byte ext_Elf32_Word[4];
49
50struct ext_elf32_dsbt_loadseg
51{
52 /* Core address to which the segment is mapped. */
53 ext_Elf32_Addr addr;
54 /* VMA recorded in the program header. */
55 ext_Elf32_Addr p_vaddr;
56 /* Size of this segment in memory. */
57 ext_Elf32_Word p_memsz;
58};
59
60struct ext_elf32_dsbt_loadmap {
61 /* Protocol version number, must be zero. */
62 ext_Elf32_Word version;
63 /* A pointer to the DSBT table; the DSBT size and the index of this
64 module. */
65 ext_Elf32_Word dsbt_table_ptr;
66 ext_Elf32_Word dsbt_size;
67 ext_Elf32_Word dsbt_index;
68 /* Number of segments in this map. */
69 ext_Elf32_Word nsegs;
70 /* The actual memory map. */
71 struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
72};
73
74/* Internal versions; the types are GDB types and the data in each
75 of the fields is (or will be) decoded from the external struct
76 for ease of consumption. */
77struct int_elf32_dsbt_loadseg
78{
79 /* Core address to which the segment is mapped. */
80 CORE_ADDR addr;
81 /* VMA recorded in the program header. */
82 CORE_ADDR p_vaddr;
83 /* Size of this segment in memory. */
84 long p_memsz;
85};
86
87struct int_elf32_dsbt_loadmap
88{
89 /* Protocol version number, must be zero. */
90 int version;
91 CORE_ADDR dsbt_table_ptr;
92 /* A pointer to the DSBT table; the DSBT size and the index of this
93 module. */
94 int dsbt_size, dsbt_index;
95 /* Number of segments in this map. */
96 int nsegs;
97 /* The actual memory map. */
98 struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
99};
100
101/* External link_map and elf32_dsbt_loadaddr struct definitions. */
102
103typedef gdb_byte ext_ptr[4];
104
105struct ext_elf32_dsbt_loadaddr
106{
107 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
108};
109
110struct ext_link_map
111{
112 struct ext_elf32_dsbt_loadaddr l_addr;
113
114 /* Absolute file name object was found in. */
115 ext_ptr l_name; /* char *l_name; */
116
117 /* Dynamic section of the shared object. */
118 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
119
120 /* Chain of loaded objects. */
121 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
122};
123
124/* Link map info to include in an allocated so_list entry */
125
d0e449a1 126struct lm_info_dsbt : public lm_info_base
8cd64e00 127{
b0911207
SM
128 ~lm_info_dsbt ()
129 {
130 xfree (this->map);
131 }
132
8cd64e00 133 /* The loadmap, digested into an easier to use form. */
b0911207 134 int_elf32_dsbt_loadmap *map = NULL;
8cd64e00
YQ
135};
136
137/* Per pspace dsbt specific data. */
138
139struct dsbt_info
140{
141 /* The load map, got value, etc. are not available from the chain
142 of loaded shared objects. ``main_executable_lm_info'' provides
143 a way to get at this information so that it doesn't need to be
144 frequently recomputed. Initialized by dsbt_relocate_main_executable. */
d0e449a1 145 struct lm_info_dsbt *main_executable_lm_info;
8cd64e00
YQ
146
147 /* Load maps for the main executable and the interpreter. These are obtained
148 from ptrace. They are the starting point for getting into the program,
149 and are required to find the solib list with the individual load maps for
150 each module. */
151 struct int_elf32_dsbt_loadmap *exec_loadmap;
152 struct int_elf32_dsbt_loadmap *interp_loadmap;
153
154 /* Cached value for lm_base, below. */
155 CORE_ADDR lm_base_cache;
156
157 /* Link map address for main module. */
158 CORE_ADDR main_lm_addr;
159
8cd64e00
YQ
160 CORE_ADDR interp_text_sect_low;
161 CORE_ADDR interp_text_sect_high;
162 CORE_ADDR interp_plt_sect_low;
163 CORE_ADDR interp_plt_sect_high;
164};
165
166/* Per-program-space data key. */
167static const struct program_space_data *solib_dsbt_pspace_data;
168
169static void
170dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
171{
487ad57c 172 xfree (arg);
8cd64e00
YQ
173}
174
175/* Get the current dsbt data. If none is found yet, add it now. This
176 function always returns a valid object. */
177
178static struct dsbt_info *
179get_dsbt_info (void)
180{
181 struct dsbt_info *info;
182
19ba03f4
SM
183 info = (struct dsbt_info *) program_space_data (current_program_space,
184 solib_dsbt_pspace_data);
8cd64e00
YQ
185 if (info != NULL)
186 return info;
187
41bf6aca 188 info = XCNEW (struct dsbt_info);
8cd64e00
YQ
189 set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
190
8cd64e00
YQ
191 info->lm_base_cache = 0;
192 info->main_lm_addr = 0;
193
194 return info;
195}
196
197
198static void
199dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
200{
201 int i;
202
203 if (map == NULL)
204 printf_filtered ("(null)\n");
205 else if (map->version != 0)
206 printf_filtered (_("Unsupported map version: %d\n"), map->version);
207 else
208 {
209 printf_filtered ("version %d\n", map->version);
210
211 for (i = 0; i < map->nsegs; i++)
212 printf_filtered ("%s:%s -> %s:%s\n",
f5656ead 213 print_core_address (target_gdbarch (),
8cd64e00 214 map->segs[i].p_vaddr),
f5656ead 215 print_core_address (target_gdbarch (),
8cd64e00
YQ
216 map->segs[i].p_vaddr
217 + map->segs[i].p_memsz),
f5656ead
TT
218 print_core_address (target_gdbarch (), map->segs[i].addr),
219 print_core_address (target_gdbarch (), map->segs[i].addr
8cd64e00
YQ
220 + map->segs[i].p_memsz));
221 }
222}
223
224/* Decode int_elf32_dsbt_loadmap from BUF. */
225
226static struct int_elf32_dsbt_loadmap *
9018be22 227decode_loadmap (const gdb_byte *buf)
8cd64e00 228{
f5656ead 229 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
9018be22 230 const struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
8cd64e00
YQ
231 struct int_elf32_dsbt_loadmap *int_ldmbuf;
232
233 int version, seg, nsegs;
22e048c9 234 int int_ldmbuf_size;
8cd64e00
YQ
235
236 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
237
238 /* Extract the version. */
239 version = extract_unsigned_integer (ext_ldmbuf->version,
240 sizeof ext_ldmbuf->version,
241 byte_order);
242 if (version != 0)
243 {
244 /* We only handle version 0. */
245 return NULL;
246 }
247
248 /* Extract the number of segments. */
249 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
250 sizeof ext_ldmbuf->nsegs,
251 byte_order);
252
253 if (nsegs <= 0)
254 return NULL;
255
256 /* Allocate space into which to put information extract from the
257 external loadsegs. I.e, allocate the internal loadsegs. */
258 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
259 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
224c3ddb 260 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
8cd64e00
YQ
261
262 /* Place extracted information in internal structs. */
263 int_ldmbuf->version = version;
264 int_ldmbuf->nsegs = nsegs;
265 for (seg = 0; seg < nsegs; seg++)
266 {
267 int_ldmbuf->segs[seg].addr
268 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
269 sizeof (ext_ldmbuf->segs[seg].addr),
270 byte_order);
271 int_ldmbuf->segs[seg].p_vaddr
272 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
273 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
274 byte_order);
275 int_ldmbuf->segs[seg].p_memsz
276 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
277 sizeof (ext_ldmbuf->segs[seg].p_memsz),
278 byte_order);
279 }
280
8cd64e00
YQ
281 return int_ldmbuf;
282}
283
284
285static struct dsbt_info *get_dsbt_info (void);
286
287/* Interrogate the Linux kernel to find out where the program was loaded.
288 There are two load maps; one for the executable and one for the
289 interpreter (only in the case of a dynamically linked executable). */
290
291static void
292dsbt_get_initial_loadmaps (void)
293{
8cd64e00 294 struct dsbt_info *info = get_dsbt_info ();
9018be22 295 gdb::optional<gdb::byte_vector> buf
8b88a78e 296 = target_read_alloc (current_top_target (), TARGET_OBJECT_FDPIC, "exec");
8cd64e00 297
9018be22 298 if (!buf || buf->empty ())
8cd64e00
YQ
299 {
300 info->exec_loadmap = NULL;
301 error (_("Error reading DSBT exec loadmap"));
302 }
9018be22 303 info->exec_loadmap = decode_loadmap (buf->data ());
8cd64e00
YQ
304 if (solib_dsbt_debug)
305 dsbt_print_loadmap (info->exec_loadmap);
306
8b88a78e 307 buf = target_read_alloc (current_top_target (), TARGET_OBJECT_FDPIC, "exec");
9018be22 308 if (!buf || buf->empty ())
8cd64e00
YQ
309 {
310 info->interp_loadmap = NULL;
311 error (_("Error reading DSBT interp loadmap"));
312 }
9018be22 313 info->interp_loadmap = decode_loadmap (buf->data ());
8cd64e00
YQ
314 if (solib_dsbt_debug)
315 dsbt_print_loadmap (info->interp_loadmap);
316}
317
318/* Given address LDMADDR, fetch and decode the loadmap at that address.
319 Return NULL if there is a problem reading the target memory or if
320 there doesn't appear to be a loadmap at the given address. The
321 allocated space (representing the loadmap) returned by this
322 function may be freed via a single call to xfree. */
323
324static struct int_elf32_dsbt_loadmap *
325fetch_loadmap (CORE_ADDR ldmaddr)
326{
f5656ead 327 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
8cd64e00
YQ
328 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
329 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
330 struct int_elf32_dsbt_loadmap *int_ldmbuf;
331 int ext_ldmbuf_size, int_ldmbuf_size;
332 int version, seg, nsegs;
333
334 /* Fetch initial portion of the loadmap. */
335 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
7ee4732a 336 sizeof ext_ldmbuf_partial))
8cd64e00
YQ
337 {
338 /* Problem reading the target's memory. */
339 return NULL;
340 }
341
342 /* Extract the version. */
343 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
7ee4732a 344 sizeof ext_ldmbuf_partial.version,
8cd64e00
YQ
345 byte_order);
346 if (version != 0)
347 {
348 /* We only handle version 0. */
349 return NULL;
350 }
351
352 /* Extract the number of segments. */
353 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
354 sizeof ext_ldmbuf_partial.nsegs,
355 byte_order);
356
357 if (nsegs <= 0)
358 return NULL;
359
360 /* Allocate space for the complete (external) loadmap. */
361 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
362 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
224c3ddb 363 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) xmalloc (ext_ldmbuf_size);
8cd64e00
YQ
364
365 /* Copy over the portion of the loadmap that's already been read. */
366 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
367
368 /* Read the rest of the loadmap from the target. */
369 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
370 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
371 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
372 {
373 /* Couldn't read rest of the loadmap. */
374 xfree (ext_ldmbuf);
375 return NULL;
376 }
377
378 /* Allocate space into which to put information extract from the
379 external loadsegs. I.e, allocate the internal loadsegs. */
380 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
381 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
224c3ddb 382 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
8cd64e00
YQ
383
384 /* Place extracted information in internal structs. */
385 int_ldmbuf->version = version;
386 int_ldmbuf->nsegs = nsegs;
387 for (seg = 0; seg < nsegs; seg++)
388 {
389 int_ldmbuf->segs[seg].addr
390 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
391 sizeof (ext_ldmbuf->segs[seg].addr),
392 byte_order);
393 int_ldmbuf->segs[seg].p_vaddr
394 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
395 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
396 byte_order);
397 int_ldmbuf->segs[seg].p_memsz
398 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
399 sizeof (ext_ldmbuf->segs[seg].p_memsz),
400 byte_order);
401 }
402
403 xfree (ext_ldmbuf);
404 return int_ldmbuf;
405}
406
407static void dsbt_relocate_main_executable (void);
3582629f 408static int enable_break (void);
8cd64e00
YQ
409
410/* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
411 returned and the corresponding PTR is set. */
412
413static int
414scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
415{
416 int arch_size, step, sect_size;
417 long dyn_tag;
418 CORE_ADDR dyn_ptr, dyn_addr;
419 gdb_byte *bufend, *bufstart, *buf;
420 Elf32_External_Dyn *x_dynp_32;
421 Elf64_External_Dyn *x_dynp_64;
422 struct bfd_section *sect;
423 struct target_section *target_section;
424
425 if (abfd == NULL)
426 return 0;
427
428 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
429 return 0;
430
431 arch_size = bfd_get_arch_size (abfd);
432 if (arch_size == -1)
433 return 0;
434
435 /* Find the start address of the .dynamic section. */
436 sect = bfd_get_section_by_name (abfd, ".dynamic");
437 if (sect == NULL)
438 return 0;
439
440 for (target_section = current_target_sections->sections;
441 target_section < current_target_sections->sections_end;
442 target_section++)
443 if (sect == target_section->the_bfd_section)
444 break;
445 if (target_section < current_target_sections->sections_end)
446 dyn_addr = target_section->addr;
447 else
448 {
449 /* ABFD may come from OBJFILE acting only as a symbol file without being
450 loaded into the target (see add_symbol_file_command). This case is
451 such fallback to the file VMA address without the possibility of
452 having the section relocated to its actual in-memory address. */
453
454 dyn_addr = bfd_section_vma (abfd, sect);
455 }
456
457 /* Read in .dynamic from the BFD. We will get the actual value
458 from memory later. */
459 sect_size = bfd_section_size (abfd, sect);
224c3ddb 460 buf = bufstart = (gdb_byte *) alloca (sect_size);
8cd64e00
YQ
461 if (!bfd_get_section_contents (abfd, sect,
462 buf, 0, sect_size))
463 return 0;
464
465 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
466 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
467 : sizeof (Elf64_External_Dyn);
468 for (bufend = buf + sect_size;
469 buf < bufend;
470 buf += step)
471 {
472 if (arch_size == 32)
473 {
474 x_dynp_32 = (Elf32_External_Dyn *) buf;
475 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
476 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
477 }
478 else
479 {
480 x_dynp_64 = (Elf64_External_Dyn *) buf;
481 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
482 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
483 }
484 if (dyn_tag == DT_NULL)
485 return 0;
486 if (dyn_tag == dyntag)
487 {
488 /* If requested, try to read the runtime value of this .dynamic
489 entry. */
490 if (ptr)
491 {
492 struct type *ptr_type;
493 gdb_byte ptr_buf[8];
494 CORE_ADDR ptr_addr;
495
f5656ead 496 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
8cd64e00
YQ
497 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
498 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
499 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
500 *ptr = dyn_ptr;
501 }
502 return 1;
503 }
504 }
505
506 return 0;
507}
508
bf469271 509/* See solist.h. */
8cd64e00
YQ
510
511static int
bf469271 512open_symbol_file_object (int from_tty)
8cd64e00
YQ
513{
514 /* Unimplemented. */
515 return 0;
516}
517
518/* Given a loadmap and an address, return the displacement needed
519 to relocate the address. */
520
521static CORE_ADDR
522displacement_from_map (struct int_elf32_dsbt_loadmap *map,
7ee4732a 523 CORE_ADDR addr)
8cd64e00
YQ
524{
525 int seg;
526
527 for (seg = 0; seg < map->nsegs; seg++)
528 if (map->segs[seg].p_vaddr <= addr
529 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
530 return map->segs[seg].addr - map->segs[seg].p_vaddr;
531
532 return 0;
533}
534
535/* Return the address from which the link map chain may be found. On
536 DSBT, a pointer to the start of the link map will be located at the
537 word found at base of GOT + GOT_MODULE_OFFSET.
538
539 The base of GOT may be found in a number of ways. Assuming that the
540 main executable has already been relocated,
541 1 The easiest way to find this value is to look up the address of
542 _GLOBAL_OFFSET_TABLE_.
543 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
544 address of Global Offset Table. .*/
545
546static CORE_ADDR
547lm_base (void)
548{
f5656ead 549 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
3b7344d5 550 struct bound_minimal_symbol got_sym;
8cd64e00
YQ
551 CORE_ADDR addr;
552 gdb_byte buf[TIC6X_PTR_SIZE];
553 struct dsbt_info *info = get_dsbt_info ();
554
555 /* One of our assumptions is that the main executable has been relocated.
556 Bail out if this has not happened. (Note that post_create_inferior
557 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
558 If we allow this to happen, lm_base_cache will be initialized with
559 a bogus value. */
560 if (info->main_executable_lm_info == 0)
561 return 0;
562
563 /* If we already have a cached value, return it. */
564 if (info->lm_base_cache)
565 return info->lm_base_cache;
566
567 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
568 symfile_objfile);
569
3b7344d5 570 if (got_sym.minsym != 0)
8cd64e00 571 {
77e371c0 572 addr = BMSYMBOL_VALUE_ADDRESS (got_sym);
8cd64e00
YQ
573 if (solib_dsbt_debug)
574 fprintf_unfiltered (gdb_stdlog,
575 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
576 (unsigned int) addr);
577 }
578 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
579 {
580 struct int_elf32_dsbt_loadmap *ldm;
581
582 dsbt_get_initial_loadmaps ();
583 ldm = info->exec_loadmap;
584 addr += displacement_from_map (ldm, addr);
585 if (solib_dsbt_debug)
586 fprintf_unfiltered (gdb_stdlog,
587 "lm_base: get addr %x by DT_PLTGOT.\n",
588 (unsigned int) addr);
589 }
590 else
591 {
592 if (solib_dsbt_debug)
593 fprintf_unfiltered (gdb_stdlog,
594 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
595 return 0;
596 }
597 addr += GOT_MODULE_OFFSET;
598
599 if (solib_dsbt_debug)
600 fprintf_unfiltered (gdb_stdlog,
601 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
602 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
603
604 if (target_read_memory (addr, buf, sizeof buf) != 0)
605 return 0;
606 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
607
608 if (solib_dsbt_debug)
609 fprintf_unfiltered (gdb_stdlog,
610 "lm_base: lm_base_cache = %s\n",
611 hex_string_custom (info->lm_base_cache, 8));
612
613 return info->lm_base_cache;
614}
615
616
617/* Build a list of `struct so_list' objects describing the shared
618 objects currently loaded in the inferior. This list does not
619 include an entry for the main executable file.
620
621 Note that we only gather information directly available from the
622 inferior --- we don't examine any of the shared library files
623 themselves. The declaration of `struct so_list' says which fields
624 we provide values for. */
625
626static struct so_list *
627dsbt_current_sos (void)
628{
f5656ead 629 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
8cd64e00
YQ
630 CORE_ADDR lm_addr;
631 struct so_list *sos_head = NULL;
632 struct so_list **sos_next_ptr = &sos_head;
633 struct dsbt_info *info = get_dsbt_info ();
634
635 /* Make sure that the main executable has been relocated. This is
636 required in order to find the address of the global offset table,
637 which in turn is used to find the link map info. (See lm_base
638 for details.)
639
640 Note that the relocation of the main executable is also performed
4d1eb6b4 641 by solib_create_inferior_hook, however, in the case of core
8cd64e00 642 files, this hook is called too late in order to be of benefit to
4d1eb6b4 643 solib_add. solib_add eventually calls this function,
8cd64e00 644 dsbt_current_sos, and also precedes the call to
4d1eb6b4 645 solib_create_inferior_hook. (See post_create_inferior in
8cd64e00
YQ
646 infcmd.c.) */
647 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
648 dsbt_relocate_main_executable ();
649
650 /* Locate the address of the first link map struct. */
651 lm_addr = lm_base ();
652
653 /* We have at least one link map entry. Fetch the the lot of them,
654 building the solist chain. */
655 while (lm_addr)
656 {
657 struct ext_link_map lm_buf;
658 ext_Elf32_Word indexword;
659 CORE_ADDR map_addr;
660 int dsbt_index;
661 int ret;
662
663 if (solib_dsbt_debug)
664 fprintf_unfiltered (gdb_stdlog,
665 "current_sos: reading link_map entry at %s\n",
666 hex_string_custom (lm_addr, 8));
667
668 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
669 if (ret)
670 {
671 warning (_("dsbt_current_sos: Unable to read link map entry."
672 " Shared object chain may be incomplete."));
673 break;
674 }
675
676 /* Fetch the load map address. */
677 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
678 sizeof lm_buf.l_addr.map,
679 byte_order);
680
681 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
682 sizeof indexword);
683 if (ret)
684 {
685 warning (_("dsbt_current_sos: Unable to read dsbt index."
686 " Shared object chain may be incomplete."));
687 break;
688 }
689 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
690 byte_order);
691
692 /* If the DSBT index is zero, then we're looking at the entry
693 for the main executable. By convention, we don't include
694 this in the list of shared objects. */
695 if (dsbt_index != 0)
696 {
697 int errcode;
e83e4e24 698 gdb::unique_xmalloc_ptr<char> name_buf;
8cd64e00
YQ
699 struct int_elf32_dsbt_loadmap *loadmap;
700 struct so_list *sop;
701 CORE_ADDR addr;
702
703 loadmap = fetch_loadmap (map_addr);
704 if (loadmap == NULL)
705 {
706 warning (_("dsbt_current_sos: Unable to fetch load map."
707 " Shared object chain may be incomplete."));
708 break;
709 }
710
8d749320 711 sop = XCNEW (struct so_list);
b0911207 712 lm_info_dsbt *li = new lm_info_dsbt;
d0e449a1
SM
713 sop->lm_info = li;
714 li->map = loadmap;
8cd64e00
YQ
715 /* Fetch the name. */
716 addr = extract_unsigned_integer (lm_buf.l_name,
717 sizeof (lm_buf.l_name),
718 byte_order);
719 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
720 &errcode);
721
722 if (errcode != 0)
723 warning (_("Can't read pathname for link map entry: %s."),
724 safe_strerror (errcode));
725 else
726 {
727 if (solib_dsbt_debug)
728 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
e83e4e24 729 name_buf.get ());
8cd64e00 730
e83e4e24 731 strncpy (sop->so_name, name_buf.get (), SO_NAME_MAX_PATH_SIZE - 1);
8cd64e00 732 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
8cd64e00
YQ
733 strcpy (sop->so_original_name, sop->so_name);
734 }
735
736 *sos_next_ptr = sop;
737 sos_next_ptr = &sop->next;
738 }
739 else
740 {
741 info->main_lm_addr = lm_addr;
742 }
743
744 lm_addr = extract_unsigned_integer (lm_buf.l_next,
745 sizeof (lm_buf.l_next), byte_order);
746 }
747
8cd64e00
YQ
748 return sos_head;
749}
750
751/* Return 1 if PC lies in the dynamic symbol resolution code of the
752 run time loader. */
753
754static int
755dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
756{
757 struct dsbt_info *info = get_dsbt_info ();
758
759 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
760 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
3e5d3a5a 761 || in_plt_section (pc));
8cd64e00
YQ
762}
763
764/* Print a warning about being unable to set the dynamic linker
765 breakpoint. */
766
767static void
768enable_break_failure_warning (void)
769{
770 warning (_("Unable to find dynamic linker breakpoint function.\n"
7ee4732a
YQ
771 "GDB will be unable to debug shared library initializers\n"
772 "and track explicitly loaded dynamic code."));
8cd64e00
YQ
773}
774
cb457ae2
YQ
775/* Helper function for gdb_bfd_lookup_symbol. */
776
777static int
3953f15c 778cmp_name (const asymbol *sym, const void *data)
cb457ae2
YQ
779{
780 return (strcmp (sym->name, (const char *) data) == 0);
781}
782
8cd64e00
YQ
783/* The dynamic linkers has, as part of its debugger interface, support
784 for arranging for the inferior to hit a breakpoint after mapping in
785 the shared libraries. This function enables that breakpoint.
786
3582629f
YQ
787 On the TIC6X, using the shared library (DSBT), GDB can try to place
788 a breakpoint on '_dl_debug_state' to monitor the shared library
789 event. */
8cd64e00
YQ
790
791static int
3582629f 792enable_break (void)
8cd64e00 793{
8cd64e00 794 asection *interp_sect;
3582629f 795 struct dsbt_info *info;
8cd64e00
YQ
796
797 if (exec_bfd == NULL)
798 return 0;
799
800 if (!target_has_execution)
801 return 0;
802
3582629f
YQ
803 info = get_dsbt_info ();
804
8cd64e00
YQ
805 info->interp_text_sect_low = 0;
806 info->interp_text_sect_high = 0;
807 info->interp_plt_sect_low = 0;
808 info->interp_plt_sect_high = 0;
809
810 /* Find the .interp section; if not found, warn the user and drop
811 into the old breakpoint at symbol code. */
812 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
813 if (interp_sect)
814 {
815 unsigned int interp_sect_size;
001f13d8 816 char *buf;
22e048c9 817 CORE_ADDR addr;
8cd64e00 818 struct int_elf32_dsbt_loadmap *ldm;
3582629f 819 int ret;
8cd64e00
YQ
820
821 /* Read the contents of the .interp section into a local buffer;
7ee4732a 822 the contents specify the dynamic linker this program uses. */
8cd64e00 823 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
224c3ddb 824 buf = (char *) alloca (interp_sect_size);
8cd64e00
YQ
825 bfd_get_section_contents (exec_bfd, interp_sect,
826 buf, 0, interp_sect_size);
827
828 /* Now we need to figure out where the dynamic linker was
7ee4732a
YQ
829 loaded so that we can load its symbols and place a breakpoint
830 in the dynamic linker itself. */
8cd64e00 831
192b62ce 832 gdb_bfd_ref_ptr tmp_bfd;
492d29ea 833 TRY
7ee4732a
YQ
834 {
835 tmp_bfd = solib_bfd_open (buf);
836 }
492d29ea
PA
837 CATCH (ex, RETURN_MASK_ALL)
838 {
839 }
840 END_CATCH
841
8cd64e00
YQ
842 if (tmp_bfd == NULL)
843 {
844 enable_break_failure_warning ();
845 return 0;
846 }
847
848 dsbt_get_initial_loadmaps ();
849 ldm = info->interp_loadmap;
850
851 /* Record the relocated start and end address of the dynamic linker
7ee4732a 852 text and plt section for dsbt_in_dynsym_resolve_code. */
192b62ce 853 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
8cd64e00
YQ
854 if (interp_sect)
855 {
856 info->interp_text_sect_low
192b62ce 857 = bfd_section_vma (tmp_bfd.get (), interp_sect);
8cd64e00
YQ
858 info->interp_text_sect_low
859 += displacement_from_map (ldm, info->interp_text_sect_low);
860 info->interp_text_sect_high
861 = info->interp_text_sect_low
192b62ce 862 + bfd_section_size (tmp_bfd.get (), interp_sect);
8cd64e00 863 }
192b62ce 864 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
8cd64e00
YQ
865 if (interp_sect)
866 {
867 info->interp_plt_sect_low =
192b62ce 868 bfd_section_vma (tmp_bfd.get (), interp_sect);
8cd64e00
YQ
869 info->interp_plt_sect_low
870 += displacement_from_map (ldm, info->interp_plt_sect_low);
871 info->interp_plt_sect_high =
192b62ce
TT
872 info->interp_plt_sect_low + bfd_section_size (tmp_bfd.get (),
873 interp_sect);
8cd64e00
YQ
874 }
875
192b62ce
TT
876 addr = gdb_bfd_lookup_symbol (tmp_bfd.get (), cmp_name,
877 "_dl_debug_state");
3582629f 878 if (addr != 0)
8cd64e00 879 {
3582629f
YQ
880 if (solib_dsbt_debug)
881 fprintf_unfiltered (gdb_stdlog,
882 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
883 hex_string_custom (addr, 8));
884 addr += displacement_from_map (ldm, addr);
8cd64e00 885
3582629f
YQ
886 if (solib_dsbt_debug)
887 fprintf_unfiltered (gdb_stdlog,
888 "enable_break: _dl_debug_state (after relocation) = %s\n",
889 hex_string_custom (addr, 8));
8cd64e00 890
3582629f
YQ
891 /* Now (finally!) create the solib breakpoint. */
892 create_solib_event_breakpoint (target_gdbarch (), addr);
8cd64e00 893
3582629f 894 ret = 1;
8cd64e00 895 }
3582629f 896 else
8cd64e00
YQ
897 {
898 if (solib_dsbt_debug)
899 fprintf_unfiltered (gdb_stdlog,
3582629f
YQ
900 "enable_break: _dl_debug_state is not found\n");
901 ret = 0;
8cd64e00
YQ
902 }
903
192b62ce 904 /* We're done with the loadmap. */
8cd64e00
YQ
905 xfree (ldm);
906
3582629f 907 return ret;
8cd64e00
YQ
908 }
909
910 /* Tell the user we couldn't set a dynamic linker breakpoint. */
911 enable_break_failure_warning ();
912
913 /* Failure return. */
914 return 0;
915}
916
8cd64e00
YQ
917static void
918dsbt_relocate_main_executable (void)
919{
8cd64e00 920 struct int_elf32_dsbt_loadmap *ldm;
8cd64e00
YQ
921 int changed;
922 struct obj_section *osect;
923 struct dsbt_info *info = get_dsbt_info ();
924
925 dsbt_get_initial_loadmaps ();
926 ldm = info->exec_loadmap;
927
b0911207
SM
928 delete info->main_executable_lm_info;
929 info->main_executable_lm_info = new lm_info_dsbt;
8cd64e00
YQ
930 info->main_executable_lm_info->map = ldm;
931
309822ca
TT
932 gdb::unique_xmalloc_ptr<struct section_offsets> new_offsets
933 (XCNEWVEC (struct section_offsets, symfile_objfile->num_sections));
8cd64e00
YQ
934 changed = 0;
935
936 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
937 {
938 CORE_ADDR orig_addr, addr, offset;
939 int osect_idx;
940 int seg;
941
65cf3563 942 osect_idx = osect - symfile_objfile->sections;
8cd64e00
YQ
943
944 /* Current address of section. */
945 addr = obj_section_addr (osect);
946 /* Offset from where this section started. */
947 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
948 /* Original address prior to any past relocations. */
949 orig_addr = addr - offset;
950
951 for (seg = 0; seg < ldm->nsegs; seg++)
952 {
953 if (ldm->segs[seg].p_vaddr <= orig_addr
954 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
955 {
956 new_offsets->offsets[osect_idx]
957 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
958
959 if (new_offsets->offsets[osect_idx] != offset)
960 changed = 1;
961 break;
962 }
963 }
964 }
965
966 if (changed)
309822ca 967 objfile_relocate (symfile_objfile, new_offsets.get ());
8cd64e00
YQ
968
969 /* Now that symfile_objfile has been relocated, we can compute the
970 GOT value and stash it away. */
971}
972
973/* When gdb starts up the inferior, it nurses it along (through the
974 shell) until it is ready to execute it's first instruction. At this
4d1eb6b4 975 point, this function gets called via solib_create_inferior_hook.
8cd64e00
YQ
976
977 For the DSBT shared library, the main executable needs to be relocated.
4d1eb6b4 978 The shared library breakpoints also need to be enabled. */
8cd64e00
YQ
979
980static void
981dsbt_solib_create_inferior_hook (int from_tty)
982{
983 /* Relocate main executable. */
984 dsbt_relocate_main_executable ();
985
986 /* Enable shared library breakpoints. */
987 if (!enable_break ())
988 {
989 warning (_("shared library handler failed to enable breakpoint"));
990 return;
991 }
992}
993
994static void
995dsbt_clear_solib (void)
996{
997 struct dsbt_info *info = get_dsbt_info ();
998
999 info->lm_base_cache = 0;
8cd64e00 1000 info->main_lm_addr = 0;
b0911207
SM
1001
1002 delete info->main_executable_lm_info;
1003 info->main_executable_lm_info = NULL;
8cd64e00
YQ
1004}
1005
1006static void
1007dsbt_free_so (struct so_list *so)
1008{
d0e449a1
SM
1009 lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
1010
b0911207 1011 delete li;
8cd64e00
YQ
1012}
1013
1014static void
1015dsbt_relocate_section_addresses (struct so_list *so,
7ee4732a 1016 struct target_section *sec)
8cd64e00
YQ
1017{
1018 int seg;
d0e449a1
SM
1019 lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
1020 int_elf32_dsbt_loadmap *map = li->map;
8cd64e00
YQ
1021
1022 for (seg = 0; seg < map->nsegs; seg++)
1023 {
1024 if (map->segs[seg].p_vaddr <= sec->addr
7ee4732a 1025 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
8cd64e00
YQ
1026 {
1027 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1028
1029 sec->addr += displ;
1030 sec->endaddr += displ;
1031 break;
1032 }
1033 }
1034}
1035static void
1036show_dsbt_debug (struct ui_file *file, int from_tty,
1037 struct cmd_list_element *c, const char *value)
1038{
1039 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1040}
1041
1042struct target_so_ops dsbt_so_ops;
1043
8cd64e00
YQ
1044void
1045_initialize_dsbt_solib (void)
1046{
1047 solib_dsbt_pspace_data
8e260fc0 1048 = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);
8cd64e00
YQ
1049
1050 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1051 dsbt_so_ops.free_so = dsbt_free_so;
1052 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1053 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
8cd64e00
YQ
1054 dsbt_so_ops.current_sos = dsbt_current_sos;
1055 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1056 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1057 dsbt_so_ops.bfd_open = solib_bfd_open;
1058
1059 /* Debug this file's internals. */
ccce17b0
YQ
1060 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
1061 &solib_dsbt_debug, _("\
8cd64e00
YQ
1062Set internal debugging of shared library code for DSBT ELF."), _("\
1063Show internal debugging of shared library code for DSBT ELF."), _("\
1064When non-zero, DSBT solib specific internal debugging is enabled."),
ccce17b0
YQ
1065 NULL,
1066 show_dsbt_debug,
1067 &setdebuglist, &showdebuglist);
8cd64e00 1068}
This page took 0.868456 seconds and 4 git commands to generate.