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