No longer need to sanitize away h8s stuff.
[deliverable/binutils-gdb.git] / gdb / solib.c
CommitLineData
f8b76e70 1/* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
464c6c5f 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
1a494973 3 Free Software Foundation, Inc.
f8b76e70 4
bd5635a1
RP
5This file is part of GDB.
6
bdbd5f50 7This program is free software; you can redistribute it and/or modify
bd5635a1 8it under the terms of the GNU General Public License as published by
bdbd5f50
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
bd5635a1 11
bdbd5f50 12This program is distributed in the hope that it will be useful,
bd5635a1
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
bdbd5f50 18along with this program; if not, write to the Free Software
2858b1f2 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1 20
f8b76e70 21
b0246b3b
FF
22#include "defs.h"
23
bd5635a1 24#include <sys/types.h>
f8b76e70 25#include <signal.h>
2b576293 26#include "gdb_string.h"
d0237a54
JK
27#include <sys/param.h>
28#include <fcntl.h>
1a494973 29#include <unistd.h>
be772100
JG
30
31#ifndef SVR4_SHARED_LIBS
32 /* SunOS shared libs need the nlist structure. */
33#include <a.out.h>
2fe3b329 34#else
1a494973 35#include "elf/external.h"
be772100 36#endif
f8b76e70 37
1a494973
C
38#include <link.h>
39
bd5635a1 40#include "symtab.h"
b0246b3b
FF
41#include "bfd.h"
42#include "symfile.h"
be772100 43#include "objfiles.h"
bd5635a1
RP
44#include "gdbcore.h"
45#include "command.h"
b3fdaf3d 46#include "target.h"
2403f49b 47#include "frame.h"
464c6c5f 48#include "gnu-regex.h"
bdbd5f50 49#include "inferior.h"
6047ab6a 50#include "environ.h"
a71c0593 51#include "language.h"
1a494973 52#include "gdbcmd.h"
bdbd5f50 53
2858b1f2 54#define MAX_PATH_SIZE 512 /* FIXME: Should be dynamic */
f8b76e70 55
464c6c5f
JL
56/* On SVR4 systems, a list of symbols in the dynamic linker where
57 GDB can try to place a breakpoint to monitor shared library
58 events.
59
60 If none of these symbols are found, or other errors occur, then
61 SVR4 systems will fall back to using a symbol as the "startup
62 mapping complete" breakpoint address. */
63
64#ifdef SVR4_SHARED_LIBS
65static char *solib_break_names[] = {
66 "r_debug_state",
a404ea25 67 "_r_debug_state",
464c6c5f
JL
68 "_dl_debug_state",
69 NULL
70};
71#endif
f8b76e70 72
a608f919
FF
73#define BKPT_AT_SYMBOL 1
74
a71c0593 75#if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
a608f919
FF
76static char *bkpt_names[] = {
77#ifdef SOLIB_BKPT_NAME
78 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
79#endif
80 "_start",
81 "main",
82 NULL
83};
a71c0593 84#endif
f8b76e70 85
4ad0021e
JK
86/* Symbols which are used to locate the base of the link map structures. */
87
2fe3b329 88#ifndef SVR4_SHARED_LIBS
4ad0021e 89static char *debug_base_symbols[] = {
2fe3b329 90 "_DYNAMIC",
1a494973 91 "_DYNAMIC__MGC",
4ad0021e
JK
92 NULL
93};
2fe3b329 94#endif
4ad0021e 95
1a494973
C
96static char *main_name_list[] = {
97 "main_$main",
98 NULL
99};
100
f8b76e70
FF
101/* local data declarations */
102
d261ece7 103#ifndef SVR4_SHARED_LIBS
f8b76e70 104
f8b76e70
FF
105#define LM_ADDR(so) ((so) -> lm.lm_addr)
106#define LM_NEXT(so) ((so) -> lm.lm_next)
107#define LM_NAME(so) ((so) -> lm.lm_name)
4ad0021e
JK
108/* Test for first link map entry; first entry is a shared library. */
109#define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
f8b76e70
FF
110static struct link_dynamic dynamic_copy;
111static struct link_dynamic_2 ld_2_copy;
112static struct ld_debug debug_copy;
113static CORE_ADDR debug_addr;
114static CORE_ADDR flag_addr;
115
d261ece7 116#else /* SVR4_SHARED_LIBS */
f8b76e70 117
f8b76e70
FF
118#define LM_ADDR(so) ((so) -> lm.l_addr)
119#define LM_NEXT(so) ((so) -> lm.l_next)
120#define LM_NAME(so) ((so) -> lm.l_name)
4ad0021e
JK
121/* Test for first link map entry; first entry is the exec-file. */
122#define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
f8b76e70 123static struct r_debug debug_copy;
f8b76e70 124char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
f8b76e70 125
d261ece7 126#endif /* !SVR4_SHARED_LIBS */
bd5635a1 127
bd5635a1 128struct so_list {
f8b76e70
FF
129 struct so_list *next; /* next structure in linked list */
130 struct link_map lm; /* copy of link map from inferior */
131 struct link_map *lmaddr; /* addr in inferior lm was read from */
132 CORE_ADDR lmend; /* upper addr bound of mapped object */
133 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
134 char symbols_loaded; /* flag: symbols read in yet? */
135 char from_tty; /* flag: print msgs? */
b0246b3b 136 struct objfile *objfile; /* objfile for loaded lib */
f8b76e70
FF
137 struct section_table *sections;
138 struct section_table *sections_end;
51b57ded 139 struct section_table *textsection;
a71c0593 140 bfd *abfd;
bd5635a1
RP
141};
142
f8b76e70
FF
143static struct so_list *so_list_head; /* List of known shared objects */
144static CORE_ADDR debug_base; /* Base of dynamic linker structures */
145static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
146
51b57ded
FF
147extern int
148fdmatch PARAMS ((int, int)); /* In libiberty */
149
b0246b3b
FF
150/* Local function prototypes */
151
152static void
153special_symbol_handling PARAMS ((struct so_list *));
154
155static void
156sharedlibrary_command PARAMS ((char *, int));
157
158static int
159enable_break PARAMS ((void));
160
161static int
162disable_break PARAMS ((void));
163
164static void
51b57ded 165info_sharedlibrary_command PARAMS ((char *, int));
b0246b3b
FF
166
167static int
168symbol_add_stub PARAMS ((char *));
169
170static struct so_list *
171find_solib PARAMS ((struct so_list *));
172
173static struct link_map *
174first_link_map_member PARAMS ((void));
175
176static CORE_ADDR
177locate_base PARAMS ((void));
178
be772100
JG
179static void
180solib_map_sections PARAMS ((struct so_list *));
181
182#ifdef SVR4_SHARED_LIBS
183
b0246b3b 184static CORE_ADDR
2fe3b329 185elf_locate_base PARAMS ((void));
b0246b3b 186
be772100 187#else
b0246b3b
FF
188
189static void
1a494973
C
190allocate_rt_common_objfile PARAMS ((void));
191
192static void
193solib_add_common_symbols PARAMS ((struct rtc_symb *));
b0246b3b
FF
194
195#endif
bd5635a1 196
d0237a54 197/*
f8b76e70
FF
198
199LOCAL FUNCTION
200
201 solib_map_sections -- open bfd and build sections for shared lib
202
203SYNOPSIS
204
205 static void solib_map_sections (struct so_list *so)
206
207DESCRIPTION
208
209 Given a pointer to one of the shared objects in our list
210 of mapped objects, use the recorded name to open a bfd
211 descriptor for the object, build a section table, and then
212 relocate all the section addresses by the base address at
213 which the shared object was mapped.
214
215FIXMES
216
217 In most (all?) cases the shared object file name recorded in the
218 dynamic linkage tables will be a fully qualified pathname. For
219 cases where it isn't, do we really mimic the systems search
220 mechanism correctly in the below code (particularly the tilde
221 expansion stuff?).
222 */
223
d0237a54 224static void
f8b76e70
FF
225solib_map_sections (so)
226 struct so_list *so;
d0237a54
JK
227{
228 char *filename;
229 char *scratch_pathname;
230 int scratch_chan;
231 struct section_table *p;
de9bef49
JG
232 struct cleanup *old_chain;
233 bfd *abfd;
d0237a54 234
f8b76e70 235 filename = tilde_expand (so -> so_name);
de9bef49 236 old_chain = make_cleanup (free, filename);
d0237a54 237
6047ab6a
KH
238 scratch_chan = openp (get_in_environ (inferior_environ, "PATH"),
239 1, filename, O_RDONLY, 0, &scratch_pathname);
d0237a54 240 if (scratch_chan < 0)
f8b76e70 241 {
6047ab6a
KH
242 scratch_chan = openp (get_in_environ
243 (inferior_environ, "LD_LIBRARY_PATH"),
244 1, filename, O_RDONLY, 0, &scratch_pathname);
f8b76e70 245 }
d0237a54 246 if (scratch_chan < 0)
f8b76e70
FF
247 {
248 perror_with_name (filename);
a608f919 249 }
a71c0593 250 /* Leave scratch_pathname allocated. abfd->name will point to it. */
f8b76e70 251
a71c0593 252 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
de9bef49 253 if (!abfd)
f8b76e70 254 {
de9bef49 255 close (scratch_chan);
f8b76e70 256 error ("Could not open `%s' as an executable file: %s",
4ad0021e 257 scratch_pathname, bfd_errmsg (bfd_get_error ()));
f8b76e70 258 }
a608f919 259 /* Leave bfd open, core_xfer_memory and "info files" need it. */
a71c0593 260 so -> abfd = abfd;
a608f919 261 abfd -> cacheable = true;
de9bef49 262
2858b1f2
KH
263 /* copy full path name into so_name, so that later symbol_file_add can find
264 it */
265 if (strlen (scratch_pathname) >= MAX_PATH_SIZE)
266 error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
267 strcpy (so->so_name, scratch_pathname);
268
de9bef49 269 if (!bfd_check_format (abfd, bfd_object))
f8b76e70
FF
270 {
271 error ("\"%s\": not in executable format: %s.",
4ad0021e 272 scratch_pathname, bfd_errmsg (bfd_get_error ()));
f8b76e70 273 }
de9bef49 274 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
f8b76e70
FF
275 {
276 error ("Can't find the file sections in `%s': %s",
2fe3b329 277 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
f8b76e70
FF
278 }
279
280 for (p = so -> sections; p < so -> sections_end; p++)
281 {
282 /* Relocate the section binding addresses as recorded in the shared
283 object's file by the base address to which the object was actually
284 mapped. */
285 p -> addr += (CORE_ADDR) LM_ADDR (so);
286 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
287 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
2fe3b329 288 if (STREQ (p -> the_bfd_section -> name, ".text"))
51b57ded
FF
289 {
290 so -> textsection = p;
291 }
f8b76e70 292 }
de9bef49
JG
293
294 /* Free the file names, close the file now. */
295 do_cleanups (old_chain);
f8b76e70
FF
296}
297
7f435241
FF
298#ifndef SVR4_SHARED_LIBS
299
1a494973
C
300/* Allocate the runtime common object file. */
301
302static void
303allocate_rt_common_objfile ()
304{
305 struct objfile *objfile;
306 struct objfile *last_one;
307
308 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
309 memset (objfile, 0, sizeof (struct objfile));
310 objfile -> md = NULL;
464c6c5f
JL
311 obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
312 xmalloc, free);
1a494973
C
313 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, xmalloc,
314 free);
315 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, xmalloc,
316 free);
317 obstack_specify_allocation (&objfile -> type_obstack, 0, 0, xmalloc,
318 free);
319 objfile -> name = mstrsave (objfile -> md, "rt_common");
320
321 /* Add this file onto the tail of the linked list of other such files. */
322
323 objfile -> next = NULL;
324 if (object_files == NULL)
325 object_files = objfile;
326 else
327 {
328 for (last_one = object_files;
329 last_one -> next;
330 last_one = last_one -> next);
331 last_one -> next = objfile;
332 }
333
334 rt_common_objfile = objfile;
335}
2a4e8cc3 336
1a494973
C
337/* Read all dynamically loaded common symbol definitions from the inferior
338 and put them into the minimal symbol table for the runtime common
339 objfile. */
2a4e8cc3 340
d261ece7 341static void
1a494973 342solib_add_common_symbols (rtc_symp)
d261ece7
SG
343 struct rtc_symb *rtc_symp;
344{
345 struct rtc_symb inferior_rtc_symb;
346 struct nlist inferior_rtc_nlist;
b0246b3b
FF
347 int len;
348 char *name;
349 char *origname;
d261ece7 350
1a494973
C
351 /* Remove any runtime common symbols from previous runs. */
352
353 if (rt_common_objfile != NULL && rt_common_objfile -> minimal_symbol_count)
354 {
355 obstack_free (&rt_common_objfile -> symbol_obstack, 0);
356 obstack_specify_allocation (&rt_common_objfile -> symbol_obstack, 0, 0,
357 xmalloc, free);
358 rt_common_objfile -> minimal_symbol_count = 0;
359 rt_common_objfile -> msymbols = NULL;
360 }
361
b0246b3b
FF
362 init_minimal_symbol_collection ();
363 make_cleanup (discard_minimal_symbols, 0);
d261ece7
SG
364
365 while (rtc_symp)
366 {
b0246b3b
FF
367 read_memory ((CORE_ADDR) rtc_symp,
368 (char *) &inferior_rtc_symb,
369 sizeof (inferior_rtc_symb));
370 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
371 (char *) &inferior_rtc_nlist,
372 sizeof(inferior_rtc_nlist));
373 if (inferior_rtc_nlist.n_type == N_COMM)
374 {
375 /* FIXME: The length of the symbol name is not available, but in the
376 current implementation the common symbol is allocated immediately
377 behind the name of the symbol. */
378 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
379
380 origname = name = xmalloc (len);
381 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
382
1a494973
C
383 /* Allocate the runtime common objfile if necessary. */
384 if (rt_common_objfile == NULL)
385 allocate_rt_common_objfile ();
d261ece7 386
1a494973
C
387 name = obsavestring (name, strlen (name),
388 &rt_common_objfile -> symbol_obstack);
389 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
390 mst_bss, rt_common_objfile);
b0246b3b
FF
391 free (origname);
392 }
393 rtc_symp = inferior_rtc_symb.rtc_next;
d261ece7
SG
394 }
395
b0246b3b 396 /* Install any minimal symbols that have been collected as the current
1a494973 397 minimal symbols for the runtime common objfile. */
b0246b3b 398
1a494973 399 install_minimal_symbols (rt_common_objfile);
d261ece7
SG
400}
401
7f435241
FF
402#endif /* SVR4_SHARED_LIBS */
403
2fe3b329 404
be772100
JG
405#ifdef SVR4_SHARED_LIBS
406
54d478cd
PS
407static CORE_ADDR
408bfd_lookup_symbol PARAMS ((bfd *, char *));
409
410/*
411
412LOCAL FUNCTION
413
414 bfd_lookup_symbol -- lookup the value for a specific symbol
415
416SYNOPSIS
417
418 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
419
420DESCRIPTION
421
422 An expensive way to lookup the value of a single symbol for
423 bfd's that are only temporary anyway. This is used by the
424 shared library support to find the address of the debugger
425 interface structures in the shared library.
426
427 Note that 0 is specifically allowed as an error return (no
428 such symbol).
429*/
430
431static CORE_ADDR
432bfd_lookup_symbol (abfd, symname)
433 bfd *abfd;
434 char *symname;
435{
436 unsigned int storage_needed;
437 asymbol *sym;
438 asymbol **symbol_table;
439 unsigned int number_of_symbols;
440 unsigned int i;
441 struct cleanup *back_to;
442 CORE_ADDR symaddr = 0;
443
444 storage_needed = bfd_get_symtab_upper_bound (abfd);
445
446 if (storage_needed > 0)
447 {
448 symbol_table = (asymbol **) xmalloc (storage_needed);
449 back_to = make_cleanup (free, (PTR)symbol_table);
450 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
451
452 for (i = 0; i < number_of_symbols; i++)
453 {
454 sym = *symbol_table++;
455 if (STREQ (sym -> name, symname))
456 {
457 /* Bfd symbols are section relative. */
458 symaddr = sym -> value + sym -> section -> vma;
459 break;
460 }
461 }
462 do_cleanups (back_to);
463 }
464 return (symaddr);
465}
466
a404ea25
JL
467#ifdef HANDLE_SVR4_EXEC_EMULATORS
468
469/*
470 Solaris BCP (the part of Solaris which allows it to run SunOS4
471 a.out files) throws in another wrinkle. Solaris does not fill
472 in the usual a.out link map structures when running BCP programs,
473 the only way to get at them is via groping around in the dynamic
474 linker.
475 The dynamic linker and it's structures are located in the shared
476 C library, which gets run as the executable's "interpreter" by
477 the kernel.
478
479 Note that we can assume nothing about the process state at the time
480 we need to find these structures. We may be stopped on the first
481 instruction of the interpreter (C shared library), the first
482 instruction of the executable itself, or somewhere else entirely
483 (if we attached to the process for example).
484*/
485
486static char *debug_base_symbols[] = {
487 "r_debug", /* Solaris 2.3 */
488 "_r_debug", /* Solaris 2.1, 2.2 */
489 NULL
490};
491
492static int
493look_for_base PARAMS ((int, CORE_ADDR));
494
54d478cd
PS
495/*
496
497LOCAL FUNCTION
498
499 look_for_base -- examine file for each mapped address segment
500
501SYNOPSYS
502
503 static int look_for_base (int fd, CORE_ADDR baseaddr)
504
505DESCRIPTION
506
507 This function is passed to proc_iterate_over_mappings, which
508 causes it to get called once for each mapped address space, with
509 an open file descriptor for the file mapped to that space, and the
510 base address of that mapped space.
511
512 Our job is to find the debug base symbol in the file that this
513 fd is open on, if it exists, and if so, initialize the dynamic
514 linker structure base address debug_base.
515
516 Note that this is a computationally expensive proposition, since
517 we basically have to open a bfd on every call, so we specifically
518 avoid opening the exec file.
519 */
520
521static int
522look_for_base (fd, baseaddr)
523 int fd;
524 CORE_ADDR baseaddr;
525{
526 bfd *interp_bfd;
527 CORE_ADDR address = 0;
528 char **symbolp;
529
530 /* If the fd is -1, then there is no file that corresponds to this
531 mapped memory segment, so skip it. Also, if the fd corresponds
532 to the exec file, skip it as well. */
533
534 if (fd == -1
535 || (exec_bfd != NULL
536 && fdmatch (fileno ((GDB_FILE *)(exec_bfd -> iostream)), fd)))
537 {
538 return (0);
539 }
540
541 /* Try to open whatever random file this fd corresponds to. Note that
542 we have no way currently to find the filename. Don't gripe about
543 any problems we might have, just fail. */
544
545 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
546 {
547 return (0);
548 }
549 if (!bfd_check_format (interp_bfd, bfd_object))
550 {
1a494973
C
551 /* FIXME-leak: on failure, might not free all memory associated with
552 interp_bfd. */
54d478cd
PS
553 bfd_close (interp_bfd);
554 return (0);
555 }
556
557 /* Now try to find our debug base symbol in this file, which we at
558 least know to be a valid ELF executable or shared library. */
559
560 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
561 {
562 address = bfd_lookup_symbol (interp_bfd, *symbolp);
563 if (address != 0)
564 {
565 break;
566 }
567 }
568 if (address == 0)
569 {
1a494973
C
570 /* FIXME-leak: on failure, might not free all memory associated with
571 interp_bfd. */
54d478cd
PS
572 bfd_close (interp_bfd);
573 return (0);
574 }
575
576 /* Eureka! We found the symbol. But now we may need to relocate it
577 by the base address. If the symbol's value is less than the base
578 address of the shared library, then it hasn't yet been relocated
579 by the dynamic linker, and we have to do it ourself. FIXME: Note
580 that we make the assumption that the first segment that corresponds
581 to the shared library has the base address to which the library
582 was relocated. */
583
584 if (address < baseaddr)
585 {
586 address += baseaddr;
587 }
588 debug_base = address;
1a494973
C
589 /* FIXME-leak: on failure, might not free all memory associated with
590 interp_bfd. */
54d478cd
PS
591 bfd_close (interp_bfd);
592 return (1);
593}
594#endif /* HANDLE_SVR4_EXEC_EMULATORS */
595
f8b76e70
FF
596/*
597
598LOCAL FUNCTION
599
2fe3b329
PS
600 elf_locate_base -- locate the base address of dynamic linker structs
601 for SVR4 elf targets.
f8b76e70
FF
602
603SYNOPSIS
604
2fe3b329 605 CORE_ADDR elf_locate_base (void)
f8b76e70
FF
606
607DESCRIPTION
608
2fe3b329
PS
609 For SVR4 elf targets the address of the dynamic linker's runtime
610 structure is contained within the dynamic info section in the
611 executable file. The dynamic section is also mapped into the
612 inferior address space. Because the runtime loader fills in the
613 real address before starting the inferior, we have to read in the
614 dynamic info section from the inferior address space.
615 If there are any errors while trying to find the address, we
616 silently return 0, otherwise the found address is returned.
f8b76e70 617
2fe3b329 618 */
f8b76e70
FF
619
620static CORE_ADDR
2fe3b329 621elf_locate_base ()
f8b76e70 622{
1a494973 623 sec_ptr dyninfo_sect;
2fe3b329
PS
624 int dyninfo_sect_size;
625 CORE_ADDR dyninfo_addr;
626 char *buf;
627 char *bufend;
628
629 /* Find the start address of the .dynamic section. */
1a494973 630 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
2fe3b329
PS
631 if (dyninfo_sect == NULL)
632 return 0;
1a494973 633 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
2fe3b329
PS
634
635 /* Read in .dynamic section, silently ignore errors. */
1a494973 636 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
2fe3b329
PS
637 buf = alloca (dyninfo_sect_size);
638 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
639 return 0;
640
641 /* Find the DT_DEBUG entry in the the .dynamic section.
642 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
643 no DT_DEBUG entries. */
644 /* FIXME: In lack of a 64 bit ELF ABI the following code assumes
645 a 32 bit ELF ABI target. */
646 for (bufend = buf + dyninfo_sect_size;
647 buf < bufend;
648 buf += sizeof (Elf32_External_Dyn))
f8b76e70 649 {
2fe3b329
PS
650 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *)buf;
651 long dyn_tag;
652 CORE_ADDR dyn_ptr;
653
654 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
655 if (dyn_tag == DT_NULL)
656 break;
657 else if (dyn_tag == DT_DEBUG)
d0237a54 658 {
2fe3b329
PS
659 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
660 return dyn_ptr;
d0237a54 661 }
1a494973 662#ifdef DT_MIPS_RLD_MAP
2fe3b329 663 else if (dyn_tag == DT_MIPS_RLD_MAP)
4ad0021e 664 {
2fe3b329
PS
665 char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
666
667 /* DT_MIPS_RLD_MAP contains a pointer to the address
668 of the dynamic link structure. */
669 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
670 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
671 return 0;
672 return extract_unsigned_integer (pbuf, sizeof (pbuf));
4ad0021e 673 }
1a494973 674#endif
4ad0021e 675 }
d261ece7 676
2fe3b329
PS
677 /* DT_DEBUG entry not found. */
678 return 0;
d261ece7
SG
679}
680
2fe3b329 681#endif /* SVR4_SHARED_LIBS */
be772100 682
d261ece7
SG
683/*
684
f8b76e70
FF
685LOCAL FUNCTION
686
687 locate_base -- locate the base address of dynamic linker structs
688
689SYNOPSIS
690
691 CORE_ADDR locate_base (void)
692
693DESCRIPTION
694
695 For both the SunOS and SVR4 shared library implementations, if the
696 inferior executable has been linked dynamically, there is a single
697 address somewhere in the inferior's data space which is the key to
d261ece7 698 locating all of the dynamic linker's runtime structures. This
4ad0021e
JK
699 address is the value of the debug base symbol. The job of this
700 function is to find and return that address, or to return 0 if there
701 is no such address (the executable is statically linked for example).
f8b76e70
FF
702
703 For SunOS, the job is almost trivial, since the dynamic linker and
704 all of it's structures are statically linked to the executable at
705 link time. Thus the symbol for the address we are looking for has
b0246b3b
FF
706 already been added to the minimal symbol table for the executable's
707 objfile at the time the symbol file's symbols were read, and all we
708 have to do is look it up there. Note that we explicitly do NOT want
709 to find the copies in the shared library.
f8b76e70 710
2fe3b329
PS
711 The SVR4 version is a bit more complicated because the address
712 is contained somewhere in the dynamic info section. We have to go
4ad0021e
JK
713 to a lot more work to discover the address of the debug base symbol.
714 Because of this complexity, we cache the value we find and return that
715 value on subsequent invocations. Note there is no copy in the
716 executable symbol tables.
f8b76e70 717
f8b76e70
FF
718 */
719
720static CORE_ADDR
721locate_base ()
722{
f8b76e70 723
d261ece7 724#ifndef SVR4_SHARED_LIBS
f8b76e70 725
b0246b3b 726 struct minimal_symbol *msymbol;
d261ece7 727 CORE_ADDR address = 0;
4ad0021e 728 char **symbolp;
f8b76e70 729
4ad0021e
JK
730 /* For SunOS, we want to limit the search for the debug base symbol to the
731 executable being debugged, since there is a duplicate named symbol in the
732 shared library. We don't want the shared library versions. */
b0246b3b 733
4ad0021e 734 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
f8b76e70 735 {
1a494973 736 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
4ad0021e
JK
737 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
738 {
739 address = SYMBOL_VALUE_ADDRESS (msymbol);
740 return (address);
741 }
f8b76e70 742 }
4ad0021e 743 return (0);
f8b76e70 744
d261ece7 745#else /* SVR4_SHARED_LIBS */
f8b76e70 746
d261ece7
SG
747 /* Check to see if we have a currently valid address, and if so, avoid
748 doing all this work again and just return the cached address. If
2fe3b329 749 we have no cached address, try to locate it in the dynamic info
54d478cd 750 section for ELF executables. */
f8b76e70 751
d261ece7 752 if (debug_base == 0)
f8b76e70 753 {
54d478cd
PS
754 if (exec_bfd != NULL
755 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
756 debug_base = elf_locate_base ();
757#ifdef HANDLE_SVR4_EXEC_EMULATORS
758 /* Try it the hard way for emulated executables. */
759 else if (inferior_pid != 0)
760 proc_iterate_over_mappings (look_for_base);
761#endif
f8b76e70 762 }
d261ece7 763 return (debug_base);
f8b76e70 764
d261ece7 765#endif /* !SVR4_SHARED_LIBS */
f8b76e70
FF
766
767}
bd5635a1 768
a608f919
FF
769/*
770
771LOCAL FUNCTION
772
773 first_link_map_member -- locate first member in dynamic linker's map
774
775SYNOPSIS
776
777 static struct link_map *first_link_map_member (void)
778
779DESCRIPTION
780
781 Read in a copy of the first member in the inferior's dynamic
782 link map from the inferior's dynamic linker structures, and return
783 a pointer to the copy in our address space.
784*/
785
f8b76e70
FF
786static struct link_map *
787first_link_map_member ()
bd5635a1 788{
f8b76e70
FF
789 struct link_map *lm = NULL;
790
d261ece7 791#ifndef SVR4_SHARED_LIBS
f8b76e70 792
b0246b3b 793 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
f8b76e70
FF
794 if (dynamic_copy.ld_version >= 2)
795 {
796 /* It is a version that we can deal with, so read in the secondary
797 structure and find the address of the link map list from it. */
b0246b3b 798 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
f8b76e70
FF
799 sizeof (struct link_dynamic_2));
800 lm = ld_2_copy.ld_loaded;
801 }
802
d261ece7 803#else /* SVR4_SHARED_LIBS */
f8b76e70 804
b0246b3b 805 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
a608f919
FF
806 /* FIXME: Perhaps we should validate the info somehow, perhaps by
807 checking r_version for a known version number, or r_state for
808 RT_CONSISTENT. */
f8b76e70
FF
809 lm = debug_copy.r_map;
810
d261ece7 811#endif /* !SVR4_SHARED_LIBS */
d0237a54 812
f8b76e70
FF
813 return (lm);
814}
815
816/*
817
b0246b3b 818LOCAL FUNCTION
f8b76e70
FF
819
820 find_solib -- step through list of shared objects
821
822SYNOPSIS
823
824 struct so_list *find_solib (struct so_list *so_list_ptr)
825
826DESCRIPTION
827
828 This module contains the routine which finds the names of any
829 loaded "images" in the current process. The argument in must be
830 NULL on the first call, and then the returned value must be passed
831 in on subsequent calls. This provides the capability to "step" down
832 the list of loaded objects. On the last object, a NULL value is
833 returned.
d0237a54 834
f8b76e70
FF
835 The arg and return value are "struct link_map" pointers, as defined
836 in <link.h>.
837 */
d0237a54 838
b0246b3b 839static struct so_list *
f8b76e70
FF
840find_solib (so_list_ptr)
841 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
842{
843 struct so_list *so_list_next = NULL;
844 struct link_map *lm = NULL;
845 struct so_list *new;
846
847 if (so_list_ptr == NULL)
848 {
849 /* We are setting up for a new scan through the loaded images. */
850 if ((so_list_next = so_list_head) == NULL)
851 {
852 /* We have not already read in the dynamic linking structures
853 from the inferior, lookup the address of the base structure. */
854 debug_base = locate_base ();
a608f919 855 if (debug_base != 0)
f8b76e70
FF
856 {
857 /* Read the base structure in and find the address of the first
858 link map list member. */
859 lm = first_link_map_member ();
860 }
861 }
862 }
863 else
864 {
865 /* We have been called before, and are in the process of walking
866 the shared library list. Advance to the next shared object. */
867 if ((lm = LM_NEXT (so_list_ptr)) == NULL)
868 {
869 /* We have hit the end of the list, so check to see if any were
870 added, but be quiet if we can't read from the target any more. */
871 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
872 (char *) &(so_list_ptr -> lm),
873 sizeof (struct link_map));
874 if (status == 0)
875 {
876 lm = LM_NEXT (so_list_ptr);
877 }
878 else
879 {
880 lm = NULL;
881 }
882 }
883 so_list_next = so_list_ptr -> next;
884 }
885 if ((so_list_next == NULL) && (lm != NULL))
886 {
887 /* Get next link map structure from inferior image and build a local
888 abbreviated load_map structure */
889 new = (struct so_list *) xmalloc (sizeof (struct so_list));
de9bef49 890 memset ((char *) new, 0, sizeof (struct so_list));
f8b76e70
FF
891 new -> lmaddr = lm;
892 /* Add the new node as the next node in the list, or as the root
893 node if this is the first one. */
894 if (so_list_ptr != NULL)
895 {
896 so_list_ptr -> next = new;
897 }
898 else
899 {
900 so_list_head = new;
901 }
902 so_list_next = new;
b0246b3b
FF
903 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
904 sizeof (struct link_map));
4ad0021e
JK
905 /* For SVR4 versions, the first entry in the link map is for the
906 inferior executable, so we must ignore it. For some versions of
907 SVR4, it has no name. For others (Solaris 2.3 for example), it
908 does have a name, so we can no longer use a missing name to
909 decide when to ignore it. */
910 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm))
f8b76e70 911 {
4ad0021e
JK
912 int errcode;
913 char *buffer;
914 target_read_string ((CORE_ADDR) LM_NAME (new), &buffer,
915 MAX_PATH_SIZE - 1, &errcode);
916 if (errcode != 0)
917 error ("find_solib: Can't read pathname for load map: %s\n",
918 safe_strerror (errcode));
919 strncpy (new -> so_name, buffer, MAX_PATH_SIZE - 1);
920 new -> so_name[MAX_PATH_SIZE - 1] = '\0';
921 free (buffer);
f8b76e70
FF
922 solib_map_sections (new);
923 }
924 }
925 return (so_list_next);
bd5635a1 926}
d0237a54 927
bdbd5f50
JG
928/* A small stub to get us past the arg-passing pinhole of catch_errors. */
929
930static int
931symbol_add_stub (arg)
932 char *arg;
d0237a54 933{
f8b76e70
FF
934 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
935
54d478cd
PS
936 so -> objfile =
937 symbol_file_add (so -> so_name, so -> from_tty,
938 (so->textsection == NULL
939 ? 0
940 : (unsigned int) so -> textsection -> addr),
941 0, 0, 0);
f8b76e70 942 return (1);
d0237a54 943}
bd5635a1 944
1a494973
C
945/* This function will check the so name to see if matches the main list.
946 In some system the main object is in the list, which we want to exclude */
947
948static int match_main (soname)
949 char *soname;
950{
2858b1f2 951 char **mainp;
1a494973 952
2858b1f2
KH
953 for (mainp = main_name_list; *mainp != NULL; mainp++)
954 {
955 if (strcmp (soname, *mainp) == 0)
1a494973 956 return (1);
2858b1f2 957 }
1a494973 958
2858b1f2 959 return (0);
1a494973
C
960}
961
f8b76e70
FF
962/*
963
964GLOBAL FUNCTION
965
966 solib_add -- add a shared library file to the symtab and section list
967
968SYNOPSIS
969
970 void solib_add (char *arg_string, int from_tty,
971 struct target_ops *target)
972
973DESCRIPTION
974
975*/
bdbd5f50
JG
976
977void
978solib_add (arg_string, from_tty, target)
979 char *arg_string;
980 int from_tty;
981 struct target_ops *target;
bd5635a1 982{
f8b76e70 983 register struct so_list *so = NULL; /* link map state variable */
a71c0593
FF
984
985 /* Last shared library that we read. */
986 struct so_list *so_last = NULL;
987
f8b76e70
FF
988 char *re_err;
989 int count;
990 int old;
991
992 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
993 {
994 error ("Invalid regexp: %s", re_err);
995 }
996
2fe3b329 997 /* Add the shared library sections to the section table of the
54d478cd 998 specified target, if any. */
f8b76e70
FF
999 if (target)
1000 {
1001 /* Count how many new section_table entries there are. */
1002 so = NULL;
1003 count = 0;
1004 while ((so = find_solib (so)) != NULL)
1005 {
1a494973 1006 if (so -> so_name[0] && !match_main (so -> so_name))
f8b76e70
FF
1007 {
1008 count += so -> sections_end - so -> sections;
1009 }
1010 }
1011
1012 if (count)
1013 {
464c6c5f
JL
1014 int update_coreops;
1015
1016 /* We must update the to_sections field in the core_ops structure
1017 here, otherwise we dereference a potential dangling pointer
1018 for each call to target_read/write_memory within this routine. */
1019 update_coreops = core_ops.to_sections == target->to_sections;
1020
f8b76e70 1021 /* Reallocate the target's section table including the new size. */
ee0613d1 1022 if (target -> to_sections)
f8b76e70 1023 {
ee0613d1
JG
1024 old = target -> to_sections_end - target -> to_sections;
1025 target -> to_sections = (struct section_table *)
a71c0593 1026 xrealloc ((char *)target -> to_sections,
f8b76e70
FF
1027 (sizeof (struct section_table)) * (count + old));
1028 }
1029 else
1030 {
1031 old = 0;
ee0613d1 1032 target -> to_sections = (struct section_table *)
a71c0593 1033 xmalloc ((sizeof (struct section_table)) * count);
f8b76e70 1034 }
ee0613d1 1035 target -> to_sections_end = target -> to_sections + (count + old);
f8b76e70 1036
464c6c5f
JL
1037 /* Update the to_sections field in the core_ops structure
1038 if needed. */
1039 if (update_coreops)
1040 {
1041 core_ops.to_sections = target->to_sections;
1042 core_ops.to_sections_end = target->to_sections_end;
1043 }
1044
f8b76e70
FF
1045 /* Add these section table entries to the target's table. */
1046 while ((so = find_solib (so)) != NULL)
1047 {
1048 if (so -> so_name[0])
1049 {
1050 count = so -> sections_end - so -> sections;
de9bef49
JG
1051 memcpy ((char *) (target -> to_sections + old),
1052 so -> sections,
1053 (sizeof (struct section_table)) * count);
f8b76e70
FF
1054 old += count;
1055 }
1056 }
1057 }
1058 }
2fe3b329
PS
1059
1060 /* Now add the symbol files. */
1061 while ((so = find_solib (so)) != NULL)
1062 {
1a494973
C
1063 if (so -> so_name[0] && re_exec (so -> so_name) &&
1064 !match_main (so -> so_name))
2fe3b329
PS
1065 {
1066 so -> from_tty = from_tty;
1067 if (so -> symbols_loaded)
1068 {
1069 if (from_tty)
1070 {
1071 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
1072 }
1073 }
1074 else if (catch_errors
1075 (symbol_add_stub, (char *) so,
1076 "Error while reading shared library symbols:\n",
1077 RETURN_MASK_ALL))
1078 {
1079 so_last = so;
1080 so -> symbols_loaded = 1;
1081 }
1082 }
1083 }
a71c0593 1084
54d478cd
PS
1085 /* Getting new symbols may change our opinion about what is
1086 frameless. */
1087 if (so_last)
1088 reinit_frame_cache ();
1089
a71c0593
FF
1090 if (so_last)
1091 special_symbol_handling (so_last);
bd5635a1 1092}
bdbd5f50 1093
f8b76e70 1094/*
bd5635a1 1095
f8b76e70
FF
1096LOCAL FUNCTION
1097
1098 info_sharedlibrary_command -- code for "info sharedlibrary"
1099
1100SYNOPSIS
1101
1102 static void info_sharedlibrary_command ()
1103
1104DESCRIPTION
bd5635a1 1105
f8b76e70
FF
1106 Walk through the shared library list and print information
1107 about each attached library.
1108*/
1109
1110static void
51b57ded
FF
1111info_sharedlibrary_command (ignore, from_tty)
1112 char *ignore;
1113 int from_tty;
f8b76e70
FF
1114{
1115 register struct so_list *so = NULL; /* link map state variable */
1116 int header_done = 0;
1117
1118 if (exec_bfd == NULL)
1119 {
8d60affd 1120 printf_unfiltered ("No exec file.\n");
f8b76e70
FF
1121 return;
1122 }
1123 while ((so = find_solib (so)) != NULL)
1124 {
1125 if (so -> so_name[0])
1126 {
1127 if (!header_done)
1128 {
8d60affd 1129 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
f8b76e70
FF
1130 "Shared Object Library");
1131 header_done++;
1132 }
4ad0021e
JK
1133 /* FIXME-32x64: need print_address_numeric with field width or
1134 some such. */
8d60affd 1135 printf_unfiltered ("%-12s",
a71c0593
FF
1136 local_hex_string_custom ((unsigned long) LM_ADDR (so),
1137 "08l"));
8d60affd 1138 printf_unfiltered ("%-12s",
a71c0593
FF
1139 local_hex_string_custom ((unsigned long) so -> lmend,
1140 "08l"));
8d60affd
JK
1141 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
1142 printf_unfiltered ("%s\n", so -> so_name);
bd5635a1 1143 }
bd5635a1 1144 }
f8b76e70
FF
1145 if (so_list_head == NULL)
1146 {
8d60affd 1147 printf_unfiltered ("No shared libraries loaded at this time.\n");
bd5635a1
RP
1148 }
1149}
1150
1151/*
f8b76e70
FF
1152
1153GLOBAL FUNCTION
1154
1155 solib_address -- check to see if an address is in a shared lib
1156
1157SYNOPSIS
1158
464c6c5f 1159 char * solib_address (CORE_ADDR address)
f8b76e70
FF
1160
1161DESCRIPTION
1162
1163 Provides a hook for other gdb routines to discover whether or
1164 not a particular address is within the mapped address space of
1165 a shared library. Any address between the base mapping address
1166 and the first address beyond the end of the last mapping, is
1167 considered to be within the shared library address space, for
1168 our purposes.
1169
1170 For example, this routine is called at one point to disable
1171 breakpoints which are in shared libraries that are not currently
1172 mapped in.
1173 */
1174
464c6c5f 1175char *
f8b76e70 1176solib_address (address)
bd5635a1
RP
1177 CORE_ADDR address;
1178{
f8b76e70
FF
1179 register struct so_list *so = 0; /* link map state variable */
1180
1181 while ((so = find_solib (so)) != NULL)
1182 {
1183 if (so -> so_name[0])
1184 {
1185 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
1186 (address < (CORE_ADDR) so -> lmend))
464c6c5f 1187 return (so->so_name);
f8b76e70
FF
1188 }
1189 }
1190 return (0);
1191}
1192
1193/* Called by free_all_symtabs */
bd5635a1 1194
f8b76e70
FF
1195void
1196clear_solib()
1197{
1198 struct so_list *next;
a608f919 1199 char *bfd_filename;
f8b76e70
FF
1200
1201 while (so_list_head)
1202 {
1203 if (so_list_head -> sections)
1204 {
be772100 1205 free ((PTR)so_list_head -> sections);
f8b76e70 1206 }
a71c0593 1207 if (so_list_head -> abfd)
a608f919 1208 {
a71c0593 1209 bfd_filename = bfd_get_filename (so_list_head -> abfd);
1a494973
C
1210 if (!bfd_close (so_list_head -> abfd))
1211 warning ("cannot close \"%s\": %s",
1212 bfd_filename, bfd_errmsg (bfd_get_error ()));
a608f919
FF
1213 }
1214 else
1215 /* This happens for the executable on SVR4. */
1216 bfd_filename = NULL;
1217
f8b76e70 1218 next = so_list_head -> next;
a608f919
FF
1219 if (bfd_filename)
1220 free ((PTR)bfd_filename);
1221 free ((PTR)so_list_head);
f8b76e70 1222 so_list_head = next;
bd5635a1 1223 }
f8b76e70 1224 debug_base = 0;
bd5635a1
RP
1225}
1226
1227/*
f8b76e70
FF
1228
1229LOCAL FUNCTION
1230
1231 disable_break -- remove the "mapping changed" breakpoint
1232
1233SYNOPSIS
1234
1235 static int disable_break ()
1236
1237DESCRIPTION
1238
1239 Removes the breakpoint that gets hit when the dynamic linker
1240 completes a mapping change.
1241
bd5635a1 1242*/
f8b76e70
FF
1243
1244static int
1245disable_break ()
bd5635a1 1246{
f8b76e70
FF
1247 int status = 1;
1248
d261ece7 1249#ifndef SVR4_SHARED_LIBS
f8b76e70
FF
1250
1251 int in_debugger = 0;
1252
f8b76e70
FF
1253 /* Read the debugger structure from the inferior to retrieve the
1254 address of the breakpoint and the original contents of the
1255 breakpoint address. Remove the breakpoint by writing the original
1256 contents back. */
1257
b0246b3b 1258 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
d261ece7
SG
1259
1260 /* Set `in_debugger' to zero now. */
1261
b0246b3b 1262 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
d261ece7 1263
f8b76e70 1264 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
b0246b3b 1265 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
f8b76e70
FF
1266 sizeof (debug_copy.ldd_bp_inst));
1267
d261ece7 1268#else /* SVR4_SHARED_LIBS */
f8b76e70
FF
1269
1270 /* Note that breakpoint address and original contents are in our address
1271 space, so we just need to write the original contents back. */
1272
1273 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
1274 {
1275 status = 0;
1276 }
1277
d261ece7 1278#endif /* !SVR4_SHARED_LIBS */
f8b76e70
FF
1279
1280 /* For the SVR4 version, we always know the breakpoint address. For the
1281 SunOS version we don't know it until the above code is executed.
1282 Grumble if we are stopped anywhere besides the breakpoint address. */
1283
1284 if (stop_pc != breakpoint_addr)
1285 {
1286 warning ("stopped at unknown breakpoint while handling shared libraries");
1287 }
1288
1289 return (status);
bdbd5f50
JG
1290}
1291
f8b76e70 1292/*
bdbd5f50 1293
f8b76e70
FF
1294LOCAL FUNCTION
1295
1296 enable_break -- arrange for dynamic linker to hit breakpoint
1297
1298SYNOPSIS
1299
1300 int enable_break (void)
1301
1302DESCRIPTION
1303
1304 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1305 debugger interface, support for arranging for the inferior to hit
1306 a breakpoint after mapping in the shared libraries. This function
1307 enables that breakpoint.
1308
1309 For SunOS, there is a special flag location (in_debugger) which we
1310 set to 1. When the dynamic linker sees this flag set, it will set
1311 a breakpoint at a location known only to itself, after saving the
1312 original contents of that place and the breakpoint address itself,
1313 in it's own internal structures. When we resume the inferior, it
1314 will eventually take a SIGTRAP when it runs into the breakpoint.
1315 We handle this (in a different place) by restoring the contents of
1316 the breakpointed location (which is only known after it stops),
1317 chasing around to locate the shared libraries that have been
1318 loaded, then resuming.
1319
1320 For SVR4, the debugger interface structure contains a member (r_brk)
1321 which is statically initialized at the time the shared library is
1322 built, to the offset of a function (_r_debug_state) which is guaran-
1323 teed to be called once before mapping in a library, and again when
1324 the mapping is complete. At the time we are examining this member,
1325 it contains only the unrelocated offset of the function, so we have
1326 to do our own relocation. Later, when the dynamic linker actually
1327 runs, it relocates r_brk to be the actual address of _r_debug_state().
1328
1329 The debugger interface structure also contains an enumeration which
1330 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1331 depending upon whether or not the library is being mapped or unmapped,
1332 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1333*/
1334
1335static int
1336enable_break ()
bdbd5f50 1337{
a608f919 1338 int success = 0;
bdbd5f50 1339
d261ece7 1340#ifndef SVR4_SHARED_LIBS
bdbd5f50 1341
51b57ded 1342 int j;
f8b76e70 1343 int in_debugger;
51b57ded 1344
bdbd5f50 1345 /* Get link_dynamic structure */
f8b76e70
FF
1346
1347 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1348 sizeof (dynamic_copy));
1349 if (j)
1350 {
1351 /* unreadable */
1352 return (0);
1353 }
06b6c733 1354
bdbd5f50 1355 /* Calc address of debugger interface structure */
f8b76e70
FF
1356
1357 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1358
bdbd5f50 1359 /* Calc address of `in_debugger' member of debugger interface structure */
f8b76e70
FF
1360
1361 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1362 (char *) &debug_copy);
1363
bdbd5f50 1364 /* Write a value of 1 to this member. */
f8b76e70 1365
bdbd5f50 1366 in_debugger = 1;
b0246b3b 1367 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
a608f919 1368 success = 1;
f8b76e70 1369
d261ece7 1370#else /* SVR4_SHARED_LIBS */
f8b76e70 1371
a608f919 1372#ifdef BKPT_AT_SYMBOL
f8b76e70 1373
b0246b3b 1374 struct minimal_symbol *msymbol;
a608f919 1375 char **bkpt_namep;
464c6c5f
JL
1376 asection *interp_sect;
1377
1378 /* First, remove all the solib event breakpoints. Their addresses
1379 may have changed since the last time we ran the program. */
1380 remove_solib_event_breakpoints ();
1381
1382#ifdef SVR4_SHARED_LIBS
1383 /* Find the .interp section; if not found, warn the user and drop
1384 into the old breakpoint at symbol code. */
1385 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1386 if (interp_sect)
1387 {
1388 unsigned int interp_sect_size;
1389 char *buf;
1390 CORE_ADDR load_addr;
1391 bfd *tmp_bfd;
11be829f 1392 CORE_ADDR sym_addr = 0;
464c6c5f
JL
1393
1394 /* Read the contents of the .interp section into a local buffer;
1395 the contents specify the dynamic linker this program uses. */
1396 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
1397 buf = alloca (interp_sect_size);
1398 bfd_get_section_contents (exec_bfd, interp_sect,
1399 buf, 0, interp_sect_size);
1400
1401 /* Now we need to figure out where the dynamic linker was
1402 loaded so that we can load its symbols and place a breakpoint
1403 in the dynamic linker itself.
1404
1405 This address is stored on the stack. However, I've been unable
1406 to find any magic formula to find it for Solaris (appears to
1407 be trivial on Linux). Therefore, we have to try an alternate
1408 mechanism to find the dynamic linker's base address. */
1409 tmp_bfd = bfd_openr (buf, gnutarget);
1410 if (tmp_bfd == NULL)
1411 goto bkpt_at_symbol;
1412
1413 /* Make sure the dynamic linker's really a useful object. */
1414 if (!bfd_check_format (tmp_bfd, bfd_object))
1415 {
1416 warning ("Unable to grok dynamic linker %s as an object file", buf);
1417 bfd_close (tmp_bfd);
1418 goto bkpt_at_symbol;
1419 }
1420
1421 /* We find the dynamic linker's base address by examining the
1422 current pc (which point at the entry point for the dynamic
1423 linker) and subtracting the offset of the entry point. */
1424 load_addr = read_pc () - tmp_bfd->start_address;
1425
11be829f
JL
1426 /* Now try to set a breakpoint in the dynamic linker. */
1427 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
464c6c5f 1428 {
11be829f
JL
1429 sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
1430 if (sym_addr != 0)
1431 break;
464c6c5f
JL
1432 }
1433
464c6c5f
JL
1434 /* We're done with the temporary bfd. */
1435 bfd_close (tmp_bfd);
1436
11be829f 1437 if (sym_addr != 0)
464c6c5f 1438 {
11be829f
JL
1439 create_solib_event_breakpoint (load_addr + sym_addr);
1440 return 1;
464c6c5f
JL
1441 }
1442
1443 /* For whatever reason we couldn't set a breakpoint in the dynamic
1444 linker. Warn and drop into the old code. */
1445bkpt_at_symbol:
1446 warning ("Unable to find dynamic linker breakpoint function.");
1447 warning ("GDB will be unable to debug shared library initializers");
1448 warning ("and track explicitly loaded dynamic code.");
1449 }
1450#endif
f8b76e70 1451
a608f919
FF
1452 /* Scan through the list of symbols, trying to look up the symbol and
1453 set a breakpoint there. Terminate loop when we/if we succeed. */
f8b76e70 1454
a608f919
FF
1455 breakpoint_addr = 0;
1456 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
f8b76e70 1457 {
1a494973 1458 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
a608f919
FF
1459 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1460 {
464c6c5f
JL
1461 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1462 return 1;
a608f919 1463 }
f8b76e70
FF
1464 }
1465
464c6c5f
JL
1466 /* Nothing good happened. */
1467 return 0;
f8b76e70 1468
a608f919 1469#endif /* BKPT_AT_SYMBOL */
f8b76e70 1470
d261ece7 1471#endif /* !SVR4_SHARED_LIBS */
f8b76e70 1472
a608f919 1473 return (success);
f8b76e70
FF
1474}
1475
1476/*
1477
1478GLOBAL FUNCTION
1479
1480 solib_create_inferior_hook -- shared library startup support
1481
1482SYNOPSIS
1483
1484 void solib_create_inferior_hook()
1485
1486DESCRIPTION
1487
1488 When gdb starts up the inferior, it nurses it along (through the
1489 shell) until it is ready to execute it's first instruction. At this
1490 point, this function gets called via expansion of the macro
1491 SOLIB_CREATE_INFERIOR_HOOK.
1492
a608f919
FF
1493 For SunOS executables, this first instruction is typically the
1494 one at "_start", or a similar text label, regardless of whether
1495 the executable is statically or dynamically linked. The runtime
1496 startup code takes care of dynamically linking in any shared
1497 libraries, once gdb allows the inferior to continue.
1498
1499 For SVR4 executables, this first instruction is either the first
1500 instruction in the dynamic linker (for dynamically linked
1501 executables) or the instruction at "start" for statically linked
1502 executables. For dynamically linked executables, the system
1503 first exec's /lib/libc.so.N, which contains the dynamic linker,
1504 and starts it running. The dynamic linker maps in any needed
1505 shared libraries, maps in the actual user executable, and then
1506 jumps to "start" in the user executable.
1507
f8b76e70
FF
1508 For both SunOS shared libraries, and SVR4 shared libraries, we
1509 can arrange to cooperate with the dynamic linker to discover the
1510 names of shared libraries that are dynamically linked, and the
1511 base addresses to which they are linked.
1512
1513 This function is responsible for discovering those names and
1514 addresses, and saving sufficient information about them to allow
1515 their symbols to be read at a later time.
1516
1517FIXME
1518
1519 Between enable_break() and disable_break(), this code does not
1520 properly handle hitting breakpoints which the user might have
1521 set in the startup code or in the dynamic linker itself. Proper
1522 handling will probably have to wait until the implementation is
1523 changed to use the "breakpoint handler function" method.
1524
1525 Also, what if child has exit()ed? Must exit loop somehow.
1526 */
1527
1528void
1529solib_create_inferior_hook()
1530{
ff56144e
JK
1531 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1532 yet. In fact, in the case of a SunOS4 executable being run on
1533 Solaris, we can't get it yet. find_solib will get it when it needs
1534 it. */
1535#if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
f8b76e70
FF
1536 if ((debug_base = locate_base ()) == 0)
1537 {
1538 /* Can't find the symbol or the executable is statically linked. */
1539 return;
1540 }
ff56144e 1541#endif
f8b76e70
FF
1542
1543 if (!enable_break ())
1544 {
1545 warning ("shared library handler failed to enable breakpoint");
1546 return;
1547 }
1548
13f6c7ea 1549#ifndef SVR4_SHARED_LIBS
464c6c5f
JL
1550 /* Only SunOS needs the loop below, other systems should be using the
1551 special shared library breakpoints and the shared library breakpoint
1552 service routine.
1553
1554 Now run the target. It will eventually hit the breakpoint, at
f8b76e70
FF
1555 which point all of the libraries will have been mapped in and we
1556 can go groveling around in the dynamic linker structures to find
1557 out what we need to know about them. */
bdbd5f50
JG
1558
1559 clear_proceed_status ();
1560 stop_soon_quietly = 1;
4ad0021e 1561 stop_signal = TARGET_SIGNAL_0;
f8b76e70 1562 do
bdbd5f50 1563 {
8d60affd 1564 target_resume (-1, 0, stop_signal);
bdbd5f50
JG
1565 wait_for_inferior ();
1566 }
4ad0021e 1567 while (stop_signal != TARGET_SIGNAL_TRAP);
bdbd5f50 1568 stop_soon_quietly = 0;
f8b76e70
FF
1569
1570 /* We are now either at the "mapping complete" breakpoint (or somewhere
1571 else, a condition we aren't prepared to deal with anyway), so adjust
1572 the PC as necessary after a breakpoint, disable the breakpoint, and
1573 add any shared libraries that were mapped in. */
bdbd5f50 1574
f8b76e70
FF
1575 if (DECR_PC_AFTER_BREAK)
1576 {
1577 stop_pc -= DECR_PC_AFTER_BREAK;
1578 write_register (PC_REGNUM, stop_pc);
1579 }
1580
1581 if (!disable_break ())
1582 {
1583 warning ("shared library handler failed to disable breakpoint");
1584 }
1585
464c6c5f 1586 if (auto_solib_add)
1a494973 1587 solib_add ((char *) 0, 0, (struct target_ops *) 0);
464c6c5f 1588#endif
bdbd5f50
JG
1589}
1590
f8b76e70
FF
1591/*
1592
b0246b3b
FF
1593LOCAL FUNCTION
1594
1595 special_symbol_handling -- additional shared library symbol handling
1596
1597SYNOPSIS
1598
1599 void special_symbol_handling (struct so_list *so)
1600
1601DESCRIPTION
1602
1603 Once the symbols from a shared object have been loaded in the usual
1604 way, we are called to do any system specific symbol handling that
1605 is needed.
1606
1a494973
C
1607 For SunOS4, this consists of grunging around in the dynamic
1608 linkers structures to find symbol definitions for "common" symbols
1609 and adding them to the minimal symbol table for the runtime common
b0246b3b
FF
1610 objfile.
1611
1612*/
1613
1614static void
1615special_symbol_handling (so)
1616struct so_list *so;
1617{
1618#ifndef SVR4_SHARED_LIBS
51b57ded
FF
1619 int j;
1620
1621 if (debug_addr == 0)
1622 {
1623 /* Get link_dynamic structure */
1624
1625 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1626 sizeof (dynamic_copy));
1627 if (j)
1628 {
1629 /* unreadable */
1630 return;
1631 }
1632
1633 /* Calc address of debugger interface structure */
1634 /* FIXME, this needs work for cross-debugging of core files
1635 (byteorder, size, alignment, etc). */
1636
1637 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1638 }
b0246b3b
FF
1639
1640 /* Read the debugger structure from the inferior, just to make sure
1641 we have a current copy. */
1642
51b57ded
FF
1643 j = target_read_memory (debug_addr, (char *) &debug_copy,
1644 sizeof (debug_copy));
1645 if (j)
1646 return; /* unreadable */
b0246b3b
FF
1647
1648 /* Get common symbol definitions for the loaded object. */
1649
1650 if (debug_copy.ldd_cp)
1651 {
1a494973 1652 solib_add_common_symbols (debug_copy.ldd_cp);
b0246b3b
FF
1653 }
1654
1655#endif /* !SVR4_SHARED_LIBS */
1656}
1657
1658
1659/*
1660
1661LOCAL FUNCTION
f8b76e70
FF
1662
1663 sharedlibrary_command -- handle command to explicitly add library
1664
1665SYNOPSIS
1666
b0246b3b 1667 static void sharedlibrary_command (char *args, int from_tty)
f8b76e70
FF
1668
1669DESCRIPTION
1670
1671*/
1672
b0246b3b 1673static void
bdbd5f50 1674sharedlibrary_command (args, from_tty)
f8b76e70
FF
1675char *args;
1676int from_tty;
bdbd5f50 1677{
f8b76e70
FF
1678 dont_repeat ();
1679 solib_add (args, from_tty, (struct target_ops *) 0);
bd5635a1
RP
1680}
1681
1682void
1683_initialize_solib()
1684{
f8b76e70
FF
1685
1686 add_com ("sharedlibrary", class_files, sharedlibrary_command,
bd5635a1 1687 "Load shared object library symbols for files matching REGEXP.");
f8b76e70
FF
1688 add_info ("sharedlibrary", info_sharedlibrary_command,
1689 "Status of loaded shared object libraries.");
1a494973
C
1690
1691 add_show_from_set
1692 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
464c6c5f
JL
1693 (char *) &auto_solib_add,
1694 "Set autoloading of shared library symbols.\n\
1a494973 1695If nonzero, symbols from all shared object libraries will be loaded\n\
464c6c5f
JL
1696automatically when the inferior begins execution or when the dynamic linker\n\
1697informs gdb that a new library has been loaded. Otherwise, symbols\n\
1a494973
C
1698must be loaded manually, using `sharedlibrary'.",
1699 &setlist),
1700 &showlist);
bd5635a1 1701}
This page took 0.345983 seconds and 4 git commands to generate.