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