Protoization.
[external/binutils.git] / gdb / solib.c
1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2    Copyright 1990, 91, 92, 93, 94, 95, 96, 98, 1999, 2000
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #define _SYSCALL32      /* for Sparc64 cross Sparc32 */
23 #include "defs.h"
24
25 /* This file is only compilable if link.h is available. */
26
27 #ifdef HAVE_LINK_H
28
29 #include <sys/types.h>
30 #include <signal.h>
31 #include "gdb_string.h"
32 #include <sys/param.h>
33 #include <fcntl.h>
34
35 #ifndef SVR4_SHARED_LIBS
36  /* SunOS shared libs need the nlist structure.  */
37 #include <a.out.h>
38 #else
39 #include "elf/external.h"
40 #endif
41
42 #include <link.h>
43
44 #include "symtab.h"
45 #include "bfd.h"
46 #include "symfile.h"
47 #include "objfiles.h"
48 #include "gdbcore.h"
49 #include "command.h"
50 #include "target.h"
51 #include "frame.h"
52 #include "gdb_regex.h"
53 #include "inferior.h"
54 #include "environ.h"
55 #include "language.h"
56 #include "gdbcmd.h"
57
58 #define MAX_PATH_SIZE 512       /* FIXME: Should be dynamic */
59
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
69 static char *solib_break_names[] =
70 {
71   "r_debug_state",
72   "_r_debug_state",
73   "_dl_debug_state",
74   "rtld_db_dlactivity",
75   NULL
76 };
77 #endif
78
79 #define BKPT_AT_SYMBOL 1
80
81 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
82 static char *bkpt_names[] =
83 {
84 #ifdef SOLIB_BKPT_NAME
85   SOLIB_BKPT_NAME,              /* Prefer configured name if it exists. */
86 #endif
87   "_start",
88   "main",
89   NULL
90 };
91 #endif
92
93 /* Symbols which are used to locate the base of the link map structures. */
94
95 #ifndef SVR4_SHARED_LIBS
96 static char *debug_base_symbols[] =
97 {
98   "_DYNAMIC",
99   "_DYNAMIC__MGC",
100   NULL
101 };
102 #endif
103
104 static char *main_name_list[] =
105 {
106   "main_$main",
107   NULL
108 };
109
110 /* Function to extract an address from a solib structure.
111    When GDB is configured for some 32-bit targets (e.g. Solaris 2.7
112    sparc), BFD is configured to handle 64-bit targets, so CORE_ADDR is
113    64 bits.  We have to extract only the significant bits of addresses
114    to get the right address when accessing the core file BFD.  
115
116    We'll use the BFD itself to determine the number of significant bits.  
117    MVS, June 2000  */
118
119 static CORE_ADDR
120 solib_extract_address (void *memberp)
121 {
122   return extract_address (memberp, 
123                           bfd_get_arch_size (exec_bfd) / 8);
124 }
125
126 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
127         solib_extract_address (&MEMBER)
128
129 /* local data declarations */
130
131 #ifndef SVR4_SHARED_LIBS
132
133 /* NOTE: converted the macros LM_ADDR, LM_NEXT, LM_NAME and
134    IGNORE_FIRST_LINK_MAP_ENTRY into functions (see below).
135    MVS, June 2000  */
136
137 static struct link_dynamic dynamic_copy;
138 static struct link_dynamic_2 ld_2_copy;
139 static struct ld_debug debug_copy;
140 static CORE_ADDR debug_addr;
141 static CORE_ADDR flag_addr;
142
143 #else /* SVR4_SHARED_LIBS */
144
145 static struct r_debug debug_copy;
146 #if defined (HAVE_STRUCT_LINK_MAP32)
147 static struct r_debug32 debug32_copy;   /* Sparc64 cross Sparc32 */
148 #endif
149
150 char shadow_contents[BREAKPOINT_MAX];   /* Stash old bkpt addr contents */
151
152 #endif /* !SVR4_SHARED_LIBS */
153
154 struct so_list
155   {
156     /* The following fields of the structure come directly from the
157        dynamic linker's tables in the inferior, and are initialized by
158        current_sos.  */
159
160     struct so_list *next;       /* next structure in linked list */
161     struct link_map lm;         /* copy of link map from inferior */
162 #if defined (HAVE_STRUCT_LINK_MAP32)
163     struct link_map32 lm32;     /* copy of link map from 32-bit inferior */
164 #endif
165     CORE_ADDR lmaddr;           /* addr in inferior lm was read from */
166
167     /* Shared object file name, exactly as it appears in the
168        inferior's link map.  This may be a relative path, or something
169        which needs to be looked up in LD_LIBRARY_PATH, etc.  We use it
170        to tell which entries in the inferior's dynamic linker's link
171        map we've already loaded.  */
172     char so_original_name[MAX_PATH_SIZE];
173
174     /* shared object file name, expanded to something GDB can open */
175     char so_name[MAX_PATH_SIZE];
176
177     /* The following fields of the structure are built from
178        information gathered from the shared object file itself, and
179        are initialized when we actually add it to our symbol tables.  */
180
181     bfd *abfd;
182     CORE_ADDR lmend;            /* upper addr bound of mapped object */
183     char symbols_loaded;        /* flag: symbols read in yet? */
184     char from_tty;              /* flag: print msgs? */
185     struct objfile *objfile;    /* objfile for loaded lib */
186     struct section_table *sections;
187     struct section_table *sections_end;
188     struct section_table *textsection;
189   };
190
191 static struct so_list *so_list_head;    /* List of known shared objects */
192
193 /* link map access functions */
194
195 #ifndef SVR4_SHARED_LIBS
196
197 static CORE_ADDR
198 LM_ADDR (struct so_list *so)
199 {
200 #if defined (HAVE_STRUCT_LINK_MAP32)
201   if (bfd_get_arch_size (exec_bfd) == 32)
202     return extract_address (&so->lm32.lm_addr, sizeof (so->lm32.lm_addr));
203   else
204 #endif
205     return extract_address (&so->lm.lm_addr, sizeof (so->lm.lm_addr));
206 }
207
208 static CORE_ADDR
209 LM_NEXT (struct so_list *so)
210 {
211 #if defined (HAVE_STRUCT_LINK_MAP32)
212   if (bfd_get_arch_size (exec_bfd) == 32)
213     return extract_address (&so->lm32.lm_next, sizeof (so->lm32.lm_next));
214   else
215 #endif
216     return extract_address (&so->lm.lm_next, sizeof (so->lm.lm_next));
217 }
218
219 static CORE_ADDR
220 LM_NAME (struct so_list *so)
221 {
222 #if defined (HAVE_STRUCT_LINK_MAP32)
223   if (bfd_get_arch_size (exec_bfd) == 32)
224     return extract_address (&so->lm32.lm_name, sizeof (so->lm32.lm_name));
225   else
226 #endif
227     return extract_address (&so->lm.lm_name, sizeof (so->lm.lm_name));
228 }
229
230 static int 
231 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
232 {
233   return 0;
234 }
235
236 #else /* SVR4_SHARED_LIBS */
237
238 static CORE_ADDR
239 LM_ADDR (struct so_list *so)
240 {
241 #if defined (HAVE_STRUCT_LINK_MAP32)
242   if (bfd_get_arch_size (exec_bfd) == 32)
243     return extract_address (&so->lm32.l_addr, sizeof (so->lm32.l_addr));
244   else
245 #endif
246     return extract_address (&so->lm.l_addr, sizeof (so->lm.l_addr));
247 }
248
249 static CORE_ADDR
250 LM_NEXT (struct so_list *so)
251 {
252 #if defined (HAVE_STRUCT_LINK_MAP32)
253   if (bfd_get_arch_size (exec_bfd) == 32)
254     return extract_address (&so->lm32.l_next, sizeof (so->lm32.l_next));
255   else
256 #endif
257     return extract_address (&so->lm.l_next, sizeof (so->lm.l_next));
258 }
259
260 static CORE_ADDR
261 LM_NAME (struct so_list *so)
262 {
263 #if defined (HAVE_STRUCT_LINK_MAP32)
264   if (bfd_get_arch_size (exec_bfd) == 32)
265     return extract_address (&so->lm32.l_name, sizeof (so->lm32.l_name));
266   else
267 #endif
268     return extract_address (&so->lm.l_name, sizeof (so->lm.l_name));
269 }
270
271 static int
272 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
273 {
274 #if defined (HAVE_STRUCT_LINK_MAP32)
275   if (bfd_get_arch_size (exec_bfd) == 32)
276     return (solib_extract_address (&(so) -> lm32.l_prev) == 0);
277   else
278 #endif
279     return (solib_extract_address (&(so) -> lm.l_prev) == 0);
280 }
281
282 #endif /* !SVR4_SHARED_LIBS */
283
284
285 static CORE_ADDR debug_base;    /* Base of dynamic linker structures */
286 static CORE_ADDR breakpoint_addr;       /* Address where end bkpt is set */
287
288 static int solib_cleanup_queued = 0;    /* make_run_cleanup called */
289
290 extern int fdmatch (int, int);  /* In libiberty */
291
292 /* Local function prototypes */
293
294 static void do_clear_solib (PTR);
295
296 static int match_main (char *);
297
298 static void special_symbol_handling (void);
299
300 static void sharedlibrary_command (char *, int);
301
302 static int enable_break (void);
303
304 static void info_sharedlibrary_command (char *, int);
305
306 static int symbol_add_stub (PTR);
307
308 static CORE_ADDR first_link_map_member (void);
309
310 static CORE_ADDR locate_base (void);
311
312 static int solib_map_sections (PTR);
313
314 #ifdef SVR4_SHARED_LIBS
315
316 static CORE_ADDR elf_locate_base (void);
317
318 #else
319
320 static struct so_list *current_sos (void);
321 static void free_so (struct so_list *node);
322
323 static int disable_break (void);
324
325 static void allocate_rt_common_objfile (void);
326
327 static void
328 solib_add_common_symbols (CORE_ADDR);
329
330 #endif
331
332 void _initialize_solib (void);
333
334 /* If non-zero, this is a prefix that will be added to the front of the name
335    shared libraries with an absolute filename for loading.  */
336 static char *solib_absolute_prefix = NULL;
337
338 /* If non-empty, this is a search path for loading non-absolute shared library
339    symbol files.  This takes precedence over the environment variables PATH
340    and LD_LIBRARY_PATH.  */
341 static char *solib_search_path = NULL;
342
343 /*
344
345    LOCAL FUNCTION
346
347    solib_map_sections -- open bfd and build sections for shared lib
348
349    SYNOPSIS
350
351    static int solib_map_sections (struct so_list *so)
352
353    DESCRIPTION
354
355    Given a pointer to one of the shared objects in our list
356    of mapped objects, use the recorded name to open a bfd
357    descriptor for the object, build a section table, and then
358    relocate all the section addresses by the base address at
359    which the shared object was mapped.
360
361    FIXMES
362
363    In most (all?) cases the shared object file name recorded in the
364    dynamic linkage tables will be a fully qualified pathname.  For
365    cases where it isn't, do we really mimic the systems search
366    mechanism correctly in the below code (particularly the tilde
367    expansion stuff?).
368  */
369
370 static int
371 solib_map_sections (PTR arg)
372 {
373   struct so_list *so = (struct so_list *) arg;  /* catch_errors bogon */
374   char *filename;
375   char *scratch_pathname;
376   int scratch_chan;
377   struct section_table *p;
378   struct cleanup *old_chain;
379   bfd *abfd;
380
381   filename = tilde_expand (so->so_name);
382
383   if (solib_absolute_prefix && ROOTED_P (filename))
384     /* Prefix shared libraries with absolute filenames with
385        SOLIB_ABSOLUTE_PREFIX.  */
386     {
387       char *pfxed_fn;
388       int pfx_len;
389
390       pfx_len = strlen (solib_absolute_prefix);
391
392       /* Remove trailing slashes.  */
393       while (pfx_len > 0 && SLASH_P (solib_absolute_prefix[pfx_len - 1]))
394         pfx_len--;
395
396       pfxed_fn = xmalloc (pfx_len + strlen (filename) + 1);
397       strcpy (pfxed_fn, solib_absolute_prefix);
398       strcat (pfxed_fn, filename);
399       free (filename);
400
401       filename = pfxed_fn;
402     }
403
404   old_chain = make_cleanup (free, filename);
405
406   scratch_chan = -1;
407
408   if (solib_search_path)
409     scratch_chan = openp (solib_search_path,
410                           1, filename, O_RDONLY, 0, &scratch_pathname);
411   if (scratch_chan < 0)
412     scratch_chan = openp (get_in_environ (inferior_environ, "PATH"),
413                           1, filename, O_RDONLY, 0, &scratch_pathname);
414   if (scratch_chan < 0)
415     {
416       scratch_chan = openp (get_in_environ
417                             (inferior_environ, "LD_LIBRARY_PATH"),
418                             1, filename, O_RDONLY, 0, &scratch_pathname);
419     }
420   if (scratch_chan < 0)
421     {
422       perror_with_name (filename);
423     }
424   /* Leave scratch_pathname allocated.  abfd->name will point to it.  */
425
426   abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
427   if (!abfd)
428     {
429       close (scratch_chan);
430       error ("Could not open `%s' as an executable file: %s",
431              scratch_pathname, bfd_errmsg (bfd_get_error ()));
432     }
433   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
434   so->abfd = abfd;
435   abfd->cacheable = true;
436
437   /* copy full path name into so_name, so that later symbol_file_add can find
438      it */
439   if (strlen (scratch_pathname) >= MAX_PATH_SIZE)
440     error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
441   strcpy (so->so_name, scratch_pathname);
442
443   if (!bfd_check_format (abfd, bfd_object))
444     {
445       error ("\"%s\": not in executable format: %s.",
446              scratch_pathname, bfd_errmsg (bfd_get_error ()));
447     }
448   if (build_section_table (abfd, &so->sections, &so->sections_end))
449     {
450       error ("Can't find the file sections in `%s': %s",
451              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
452     }
453
454   for (p = so->sections; p < so->sections_end; p++)
455     {
456       /* Relocate the section binding addresses as recorded in the shared
457          object's file by the base address to which the object was actually
458          mapped. */
459       p->addr += LM_ADDR (so);
460       p->endaddr += LM_ADDR (so);
461       so->lmend = max (p->endaddr, so->lmend);
462       if (STREQ (p->the_bfd_section->name, ".text"))
463         {
464           so->textsection = p;
465         }
466     }
467
468   /* Free the file names, close the file now.  */
469   do_cleanups (old_chain);
470
471   return (1);
472 }
473
474 #ifndef SVR4_SHARED_LIBS
475
476 /* Allocate the runtime common object file.  */
477
478 static void
479 allocate_rt_common_objfile (void)
480 {
481   struct objfile *objfile;
482   struct objfile *last_one;
483
484   objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
485   memset (objfile, 0, sizeof (struct objfile));
486   objfile->md = NULL;
487   obstack_specify_allocation (&objfile->psymbol_cache.cache, 0, 0,
488                               xmalloc, free);
489   obstack_specify_allocation (&objfile->psymbol_obstack, 0, 0, xmalloc,
490                               free);
491   obstack_specify_allocation (&objfile->symbol_obstack, 0, 0, xmalloc,
492                               free);
493   obstack_specify_allocation (&objfile->type_obstack, 0, 0, xmalloc,
494                               free);
495   objfile->name = mstrsave (objfile->md, "rt_common");
496
497   /* Add this file onto the tail of the linked list of other such files. */
498
499   objfile->next = NULL;
500   if (object_files == NULL)
501     object_files = objfile;
502   else
503     {
504       for (last_one = object_files;
505            last_one->next;
506            last_one = last_one->next);
507       last_one->next = objfile;
508     }
509
510   rt_common_objfile = objfile;
511 }
512
513 /* Read all dynamically loaded common symbol definitions from the inferior
514    and put them into the minimal symbol table for the runtime common
515    objfile.  */
516
517 static void
518 solib_add_common_symbols (CORE_ADDR rtc_symp)
519 {
520   struct rtc_symb inferior_rtc_symb;
521   struct nlist inferior_rtc_nlist;
522   int len;
523   char *name;
524
525   /* Remove any runtime common symbols from previous runs.  */
526
527   if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count)
528     {
529       obstack_free (&rt_common_objfile->symbol_obstack, 0);
530       obstack_specify_allocation (&rt_common_objfile->symbol_obstack, 0, 0,
531                                   xmalloc, free);
532       rt_common_objfile->minimal_symbol_count = 0;
533       rt_common_objfile->msymbols = NULL;
534     }
535
536   init_minimal_symbol_collection ();
537   make_cleanup_discard_minimal_symbols ();
538
539   while (rtc_symp)
540     {
541       read_memory (rtc_symp,
542                    (char *) &inferior_rtc_symb,
543                    sizeof (inferior_rtc_symb));
544       read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp),
545                    (char *) &inferior_rtc_nlist,
546                    sizeof (inferior_rtc_nlist));
547       if (inferior_rtc_nlist.n_type == N_COMM)
548         {
549           /* FIXME: The length of the symbol name is not available, but in the
550              current implementation the common symbol is allocated immediately
551              behind the name of the symbol. */
552           len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
553
554           name = xmalloc (len);
555           read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name),
556                        name, len);
557
558           /* Allocate the runtime common objfile if necessary. */
559           if (rt_common_objfile == NULL)
560             allocate_rt_common_objfile ();
561
562           prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
563                                       mst_bss, rt_common_objfile);
564           free (name);
565         }
566       rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next);
567     }
568
569   /* Install any minimal symbols that have been collected as the current
570      minimal symbols for the runtime common objfile.  */
571
572   install_minimal_symbols (rt_common_objfile);
573 }
574
575 #endif /* SVR4_SHARED_LIBS */
576
577
578 #ifdef SVR4_SHARED_LIBS
579
580 static CORE_ADDR bfd_lookup_symbol (bfd *, char *);
581
582 /*
583
584    LOCAL FUNCTION
585
586    bfd_lookup_symbol -- lookup the value for a specific symbol
587
588    SYNOPSIS
589
590    CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
591
592    DESCRIPTION
593
594    An expensive way to lookup the value of a single symbol for
595    bfd's that are only temporary anyway.  This is used by the
596    shared library support to find the address of the debugger
597    interface structures in the shared library.
598
599    Note that 0 is specifically allowed as an error return (no
600    such symbol).
601  */
602
603 static CORE_ADDR
604 bfd_lookup_symbol (bfd *abfd, char *symname)
605 {
606   unsigned int storage_needed;
607   asymbol *sym;
608   asymbol **symbol_table;
609   unsigned int number_of_symbols;
610   unsigned int i;
611   struct cleanup *back_to;
612   CORE_ADDR symaddr = 0;
613
614   storage_needed = bfd_get_symtab_upper_bound (abfd);
615
616   if (storage_needed > 0)
617     {
618       symbol_table = (asymbol **) xmalloc (storage_needed);
619       back_to = make_cleanup (free, (PTR) symbol_table);
620       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
621
622       for (i = 0; i < number_of_symbols; i++)
623         {
624           sym = *symbol_table++;
625           if (STREQ (sym->name, symname))
626             {
627               /* Bfd symbols are section relative. */
628               symaddr = sym->value + sym->section->vma;
629               break;
630             }
631         }
632       do_cleanups (back_to);
633     }
634
635   if (symaddr)
636     return symaddr;
637
638   /* On FreeBSD, the dynamic linker is stripped by default.  So we'll
639      have to check the dynamic string table too.  */
640
641   storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
642
643   if (storage_needed > 0)
644     {
645       symbol_table = (asymbol **) xmalloc (storage_needed);
646       back_to = make_cleanup (free, (PTR) symbol_table);
647       number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
648
649       for (i = 0; i < number_of_symbols; i++)
650         {
651           sym = *symbol_table++;
652           if (STREQ (sym->name, symname))
653             {
654               /* Bfd symbols are section relative. */
655               symaddr = sym->value + sym->section->vma;
656               break;
657             }
658         }
659       do_cleanups (back_to);
660     }
661
662   return symaddr;
663 }
664
665 #ifdef HANDLE_SVR4_EXEC_EMULATORS
666
667 /*
668    Solaris BCP (the part of Solaris which allows it to run SunOS4
669    a.out files) throws in another wrinkle. Solaris does not fill
670    in the usual a.out link map structures when running BCP programs,
671    the only way to get at them is via groping around in the dynamic
672    linker.
673    The dynamic linker and it's structures are located in the shared
674    C library, which gets run as the executable's "interpreter" by
675    the kernel.
676
677    Note that we can assume nothing about the process state at the time
678    we need to find these structures.  We may be stopped on the first
679    instruction of the interpreter (C shared library), the first
680    instruction of the executable itself, or somewhere else entirely
681    (if we attached to the process for example).
682  */
683
684 static char *debug_base_symbols[] =
685 {
686   "r_debug",                    /* Solaris 2.3 */
687   "_r_debug",                   /* Solaris 2.1, 2.2 */
688   NULL
689 };
690
691 static int look_for_base (int, CORE_ADDR);
692
693 /*
694
695    LOCAL FUNCTION
696
697    look_for_base -- examine file for each mapped address segment
698
699    SYNOPSYS
700
701    static int look_for_base (int fd, CORE_ADDR baseaddr)
702
703    DESCRIPTION
704
705    This function is passed to proc_iterate_over_mappings, which
706    causes it to get called once for each mapped address space, with
707    an open file descriptor for the file mapped to that space, and the
708    base address of that mapped space.
709
710    Our job is to find the debug base symbol in the file that this
711    fd is open on, if it exists, and if so, initialize the dynamic
712    linker structure base address debug_base.
713
714    Note that this is a computationally expensive proposition, since
715    we basically have to open a bfd on every call, so we specifically
716    avoid opening the exec file.
717  */
718
719 static int
720 look_for_base (int fd, CORE_ADDR baseaddr)
721 {
722   bfd *interp_bfd;
723   CORE_ADDR address = 0;
724   char **symbolp;
725
726   /* If the fd is -1, then there is no file that corresponds to this
727      mapped memory segment, so skip it.  Also, if the fd corresponds
728      to the exec file, skip it as well. */
729
730   if (fd == -1
731       || (exec_bfd != NULL
732           && fdmatch (fileno ((FILE *) (exec_bfd->iostream)), fd)))
733     {
734       return (0);
735     }
736
737   /* Try to open whatever random file this fd corresponds to.  Note that
738      we have no way currently to find the filename.  Don't gripe about
739      any problems we might have, just fail. */
740
741   if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
742     {
743       return (0);
744     }
745   if (!bfd_check_format (interp_bfd, bfd_object))
746     {
747       /* FIXME-leak: on failure, might not free all memory associated with
748          interp_bfd.  */
749       bfd_close (interp_bfd);
750       return (0);
751     }
752
753   /* Now try to find our debug base symbol in this file, which we at
754      least know to be a valid ELF executable or shared library. */
755
756   for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
757     {
758       address = bfd_lookup_symbol (interp_bfd, *symbolp);
759       if (address != 0)
760         {
761           break;
762         }
763     }
764   if (address == 0)
765     {
766       /* FIXME-leak: on failure, might not free all memory associated with
767          interp_bfd.  */
768       bfd_close (interp_bfd);
769       return (0);
770     }
771
772   /* Eureka!  We found the symbol.  But now we may need to relocate it
773      by the base address.  If the symbol's value is less than the base
774      address of the shared library, then it hasn't yet been relocated
775      by the dynamic linker, and we have to do it ourself.  FIXME: Note
776      that we make the assumption that the first segment that corresponds
777      to the shared library has the base address to which the library
778      was relocated. */
779
780   if (address < baseaddr)
781     {
782       address += baseaddr;
783     }
784   debug_base = address;
785   /* FIXME-leak: on failure, might not free all memory associated with
786      interp_bfd.  */
787   bfd_close (interp_bfd);
788   return (1);
789 }
790 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
791
792 /*
793
794    LOCAL FUNCTION
795
796    elf_locate_base -- locate the base address of dynamic linker structs
797    for SVR4 elf targets.
798
799    SYNOPSIS
800
801    CORE_ADDR elf_locate_base (void)
802
803    DESCRIPTION
804
805    For SVR4 elf targets the address of the dynamic linker's runtime
806    structure is contained within the dynamic info section in the
807    executable file.  The dynamic section is also mapped into the
808    inferior address space.  Because the runtime loader fills in the
809    real address before starting the inferior, we have to read in the
810    dynamic info section from the inferior address space.
811    If there are any errors while trying to find the address, we
812    silently return 0, otherwise the found address is returned.
813
814  */
815
816 static CORE_ADDR
817 elf_locate_base (void)
818 {
819   sec_ptr dyninfo_sect;
820   int dyninfo_sect_size;
821   CORE_ADDR dyninfo_addr;
822   char *buf;
823   char *bufend;
824   int arch_size;
825
826   /* Find the start address of the .dynamic section.  */
827   dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
828   if (dyninfo_sect == NULL)
829     return 0;
830   dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
831
832   /* Read in .dynamic section, silently ignore errors.  */
833   dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
834   buf = alloca (dyninfo_sect_size);
835   if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
836     return 0;
837
838   /* Find the DT_DEBUG entry in the the .dynamic section.
839      For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
840      no DT_DEBUG entries.  */
841
842   arch_size = bfd_get_arch_size (exec_bfd);
843   if (arch_size == -1)  /* failure */
844     return 0;
845
846   if (arch_size == 32)
847     { /* 32-bit elf */
848       for (bufend = buf + dyninfo_sect_size;
849            buf < bufend;
850            buf += sizeof (Elf32_External_Dyn))
851         {
852           Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *) buf;
853           long dyn_tag;
854           CORE_ADDR dyn_ptr;
855
856           dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
857           if (dyn_tag == DT_NULL)
858             break;
859           else if (dyn_tag == DT_DEBUG)
860             {
861               dyn_ptr = bfd_h_get_32 (exec_bfd, 
862                                       (bfd_byte *) x_dynp->d_un.d_ptr);
863               return dyn_ptr;
864             }
865 #ifdef DT_MIPS_RLD_MAP
866           else if (dyn_tag == DT_MIPS_RLD_MAP)
867             {
868               char *pbuf;
869
870               pbuf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
871               /* DT_MIPS_RLD_MAP contains a pointer to the address
872                  of the dynamic link structure.  */
873               dyn_ptr = bfd_h_get_32 (exec_bfd, 
874                                       (bfd_byte *) x_dynp->d_un.d_ptr);
875               if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
876                 return 0;
877               return extract_unsigned_integer (pbuf, sizeof (pbuf));
878             }
879 #endif
880         }
881     }
882   else /* 64-bit elf */
883     {
884       for (bufend = buf + dyninfo_sect_size;
885            buf < bufend;
886            buf += sizeof (Elf64_External_Dyn))
887         {
888           Elf64_External_Dyn *x_dynp = (Elf64_External_Dyn *) buf;
889           long dyn_tag;
890           CORE_ADDR dyn_ptr;
891
892           dyn_tag = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
893           if (dyn_tag == DT_NULL)
894             break;
895           else if (dyn_tag == DT_DEBUG)
896             {
897               dyn_ptr = bfd_h_get_64 (exec_bfd, 
898                                       (bfd_byte *) x_dynp->d_un.d_ptr);
899               return dyn_ptr;
900             }
901         }
902     }
903
904   /* DT_DEBUG entry not found.  */
905   return 0;
906 }
907
908 #endif /* SVR4_SHARED_LIBS */
909
910 /*
911
912    LOCAL FUNCTION
913
914    locate_base -- locate the base address of dynamic linker structs
915
916    SYNOPSIS
917
918    CORE_ADDR locate_base (void)
919
920    DESCRIPTION
921
922    For both the SunOS and SVR4 shared library implementations, if the
923    inferior executable has been linked dynamically, there is a single
924    address somewhere in the inferior's data space which is the key to
925    locating all of the dynamic linker's runtime structures.  This
926    address is the value of the debug base symbol.  The job of this
927    function is to find and return that address, or to return 0 if there
928    is no such address (the executable is statically linked for example).
929
930    For SunOS, the job is almost trivial, since the dynamic linker and
931    all of it's structures are statically linked to the executable at
932    link time.  Thus the symbol for the address we are looking for has
933    already been added to the minimal symbol table for the executable's
934    objfile at the time the symbol file's symbols were read, and all we
935    have to do is look it up there.  Note that we explicitly do NOT want
936    to find the copies in the shared library.
937
938    The SVR4 version is a bit more complicated because the address
939    is contained somewhere in the dynamic info section.  We have to go
940    to a lot more work to discover the address of the debug base symbol.
941    Because of this complexity, we cache the value we find and return that
942    value on subsequent invocations.  Note there is no copy in the
943    executable symbol tables.
944
945  */
946
947 static CORE_ADDR
948 locate_base (void)
949 {
950
951 #ifndef SVR4_SHARED_LIBS
952
953   struct minimal_symbol *msymbol;
954   CORE_ADDR address = 0;
955   char **symbolp;
956
957   /* For SunOS, we want to limit the search for the debug base symbol to the
958      executable being debugged, since there is a duplicate named symbol in the
959      shared library.  We don't want the shared library versions. */
960
961   for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
962     {
963       msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
964       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
965         {
966           address = SYMBOL_VALUE_ADDRESS (msymbol);
967           return (address);
968         }
969     }
970   return (0);
971
972 #else /* SVR4_SHARED_LIBS */
973
974   /* Check to see if we have a currently valid address, and if so, avoid
975      doing all this work again and just return the cached address.  If
976      we have no cached address, try to locate it in the dynamic info
977      section for ELF executables.  */
978
979   if (debug_base == 0)
980     {
981       if (exec_bfd != NULL
982           && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
983         debug_base = elf_locate_base ();
984 #ifdef HANDLE_SVR4_EXEC_EMULATORS
985       /* Try it the hard way for emulated executables.  */
986       else if (inferior_pid != 0 && target_has_execution)
987         proc_iterate_over_mappings (look_for_base);
988 #endif
989     }
990   return (debug_base);
991
992 #endif /* !SVR4_SHARED_LIBS */
993
994 }
995
996 /*
997
998    LOCAL FUNCTION
999
1000    first_link_map_member -- locate first member in dynamic linker's map
1001
1002    SYNOPSIS
1003
1004    static CORE_ADDR first_link_map_member (void)
1005
1006    DESCRIPTION
1007
1008    Find the first element in the inferior's dynamic link map, and
1009    return its address in the inferior.  This function doesn't copy the
1010    link map entry itself into our address space; current_sos actually
1011    does the reading.  */
1012
1013 static CORE_ADDR
1014 first_link_map_member (void)
1015 {
1016   CORE_ADDR lm = 0;
1017
1018 #ifndef SVR4_SHARED_LIBS
1019
1020   read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
1021   if (dynamic_copy.ld_version >= 2)
1022     {
1023       /* It is a version that we can deal with, so read in the secondary
1024          structure and find the address of the link map list from it. */
1025       read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2),
1026                    (char *) &ld_2_copy, sizeof (struct link_dynamic_2));
1027       lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded);
1028     }
1029
1030 #else /* SVR4_SHARED_LIBS */
1031 #if defined (HAVE_STRUCT_LINK_MAP32)
1032   if (bfd_get_arch_size (exec_bfd) == 32)
1033     {
1034       read_memory (debug_base, (char *) &debug32_copy, 
1035                    sizeof (struct r_debug32));
1036       lm = SOLIB_EXTRACT_ADDRESS (debug32_copy.r_map);
1037     }
1038   else
1039 #endif
1040     {
1041       read_memory (debug_base, (char *) &debug_copy, 
1042                    sizeof (struct r_debug));
1043       lm = SOLIB_EXTRACT_ADDRESS (debug_copy.r_map);
1044     }
1045   /* FIXME:  Perhaps we should validate the info somehow, perhaps by
1046      checking r_version for a known version number, or r_state for
1047      RT_CONSISTENT. */
1048
1049 #endif /* !SVR4_SHARED_LIBS */
1050
1051   return (lm);
1052 }
1053
1054 #ifdef SVR4_SHARED_LIBS
1055 /*
1056
1057   LOCAL FUNCTION
1058
1059   open_symbol_file_object
1060
1061   SYNOPSIS
1062
1063   void open_symbol_file_object (int *from_tty)
1064
1065   DESCRIPTION
1066
1067   If no open symbol file, attempt to locate and open the main symbol
1068   file.  On SVR4 systems, this is the first link map entry.  If its
1069   name is here, we can open it.  Useful when attaching to a process
1070   without first loading its symbol file.
1071
1072   If FROM_TTYP dereferences to a non-zero integer, allow messages to
1073   be printed.  This parameter is a pointer rather than an int because
1074   open_symbol_file_object() is called via catch_errors() and
1075   catch_errors() requires a pointer argument. */
1076
1077 static int
1078 open_symbol_file_object (int *from_ttyp)
1079 {
1080   CORE_ADDR lm;
1081   char *filename;
1082   int errcode;
1083
1084   if (symfile_objfile)
1085     if (!query ("Attempt to reload symbols from process? "))
1086       return 0;
1087
1088   if ((debug_base = locate_base ()) == 0)
1089     return 0;   /* failed somehow... */
1090
1091   /* First link map member should be the executable.  */
1092   if ((lm = first_link_map_member ()) == 0)
1093     return 0;   /* failed somehow... */
1094
1095 #if defined (HAVE_STRUCT_LINK_MAP32)
1096   if (bfd_get_arch_size (exec_bfd) == 32)
1097     {
1098       struct link_map32 lmcopy;
1099       /* Read from target memory to GDB.  */
1100       read_memory (lm, (void *) &lmcopy, sizeof (lmcopy));
1101
1102       if (lmcopy.l_name == 0)
1103         return 0;       /* no filename.  */
1104
1105       /* Now fetch the filename from target memory.  */
1106       target_read_string (SOLIB_EXTRACT_ADDRESS (lmcopy.l_name), 
1107                           &filename, MAX_PATH_SIZE - 1, &errcode);
1108     }
1109   else
1110 #endif /* HAVE_STRUCT_LINK_MAP32 */
1111     {
1112       struct link_map lmcopy;
1113       /* Read from target memory to GDB.  */
1114       read_memory (lm, (void *) &lmcopy, sizeof (lmcopy));
1115
1116       if (lmcopy.l_name == 0)
1117         return 0;       /* no filename.  */
1118
1119       /* Now fetch the filename from target memory.  */
1120       target_read_string (SOLIB_EXTRACT_ADDRESS (lmcopy.l_name), &filename, 
1121                           MAX_PATH_SIZE - 1, &errcode);
1122     }
1123
1124   if (errcode)
1125     {
1126       warning ("failed to read exec filename from attached file: %s",
1127                safe_strerror (errcode));
1128       return 0;
1129     }
1130
1131   make_cleanup (free, filename);
1132   /* Have a pathname: read the symbol file.  */
1133   symbol_file_command (filename, *from_ttyp);
1134
1135   return 1;
1136 }
1137 #endif /* SVR4_SHARED_LIBS */
1138
1139
1140 /* LOCAL FUNCTION
1141
1142    free_so --- free a `struct so_list' object
1143
1144    SYNOPSIS
1145
1146    void free_so (struct so_list *so)
1147
1148    DESCRIPTION
1149
1150    Free the storage associated with the `struct so_list' object SO.
1151    If we have opened a BFD for SO, close it.  
1152
1153    The caller is responsible for removing SO from whatever list it is
1154    a member of.  If we have placed SO's sections in some target's
1155    section table, the caller is responsible for removing them.
1156
1157    This function doesn't mess with objfiles at all.  If there is an
1158    objfile associated with SO that needs to be removed, the caller is
1159    responsible for taking care of that.  */
1160
1161 static void
1162 free_so (struct so_list *so)
1163 {
1164   char *bfd_filename = 0;
1165
1166   if (so->sections)
1167     free (so->sections);
1168       
1169   if (so->abfd)
1170     {
1171       bfd_filename = bfd_get_filename (so->abfd);
1172       if (! bfd_close (so->abfd))
1173         warning ("cannot close \"%s\": %s",
1174                  bfd_filename, bfd_errmsg (bfd_get_error ()));
1175     }
1176
1177   if (bfd_filename)
1178     free (bfd_filename);
1179
1180   free (so);
1181 }
1182
1183
1184 /* On some systems, the only way to recognize the link map entry for
1185    the main executable file is by looking at its name.  Return
1186    non-zero iff SONAME matches one of the known main executable names.  */
1187
1188 static int
1189 match_main (char *soname)
1190 {
1191   char **mainp;
1192
1193   for (mainp = main_name_list; *mainp != NULL; mainp++)
1194     {
1195       if (strcmp (soname, *mainp) == 0)
1196         return (1);
1197     }
1198
1199   return (0);
1200 }
1201
1202
1203 /* LOCAL FUNCTION
1204
1205    current_sos -- build a list of currently loaded shared objects
1206
1207    SYNOPSIS
1208
1209    struct so_list *current_sos ()
1210
1211    DESCRIPTION
1212
1213    Build a list of `struct so_list' objects describing the shared
1214    objects currently loaded in the inferior.  This list does not
1215    include an entry for the main executable file.
1216
1217    Note that we only gather information directly available from the
1218    inferior --- we don't examine any of the shared library files
1219    themselves.  The declaration of `struct so_list' says which fields
1220    we provide values for.  */
1221
1222 static struct so_list *
1223 current_sos (void)
1224 {
1225   CORE_ADDR lm;
1226   struct so_list *head = 0;
1227   struct so_list **link_ptr = &head;
1228
1229   /* Make sure we've looked up the inferior's dynamic linker's base
1230      structure.  */
1231   if (! debug_base)
1232     {
1233       debug_base = locate_base ();
1234
1235       /* If we can't find the dynamic linker's base structure, this
1236          must not be a dynamically linked executable.  Hmm.  */
1237       if (! debug_base)
1238         return 0;
1239     }
1240
1241   /* Walk the inferior's link map list, and build our list of
1242      `struct so_list' nodes.  */
1243   lm = first_link_map_member ();  
1244   while (lm)
1245     {
1246       struct so_list *new
1247         = (struct so_list *) xmalloc (sizeof (struct so_list));
1248       struct cleanup *old_chain = make_cleanup (free, new);
1249       memset (new, 0, sizeof (*new));
1250
1251       new->lmaddr = lm;
1252
1253 #if defined (HAVE_STRUCT_LINK_MAP32)
1254       if (bfd_get_arch_size (exec_bfd) == 32)
1255         read_memory (lm, (char *) &(new->lm32), sizeof (struct link_map32));
1256       else
1257 #endif
1258         read_memory (lm, (char *) &(new->lm), sizeof (struct link_map));
1259
1260       lm = LM_NEXT (new);
1261
1262       /* For SVR4 versions, the first entry in the link map is for the
1263          inferior executable, so we must ignore it.  For some versions of
1264          SVR4, it has no name.  For others (Solaris 2.3 for example), it
1265          does have a name, so we can no longer use a missing name to
1266          decide when to ignore it. */
1267       if (IGNORE_FIRST_LINK_MAP_ENTRY (new))
1268         free_so (new);
1269       else
1270         {
1271           int errcode;
1272           char *buffer;
1273
1274           /* Extract this shared object's name.  */
1275           target_read_string (LM_NAME (new), &buffer,
1276                               MAX_PATH_SIZE - 1, &errcode);
1277           if (errcode != 0)
1278             {
1279               warning ("current_sos: Can't read pathname for load map: %s\n",
1280                        safe_strerror (errcode));
1281             }
1282           else
1283             {
1284               strncpy (new->so_name, buffer, MAX_PATH_SIZE - 1);
1285               new->so_name[MAX_PATH_SIZE - 1] = '\0';
1286               free (buffer);
1287               strcpy (new->so_original_name, new->so_name);
1288             }
1289
1290           /* If this entry has no name, or its name matches the name
1291              for the main executable, don't include it in the list.  */
1292           if (! new->so_name[0]
1293               || match_main (new->so_name))
1294             free_so (new);
1295           else
1296             {
1297               new->next = 0;
1298               *link_ptr = new;
1299               link_ptr = &new->next;
1300             }
1301         }
1302
1303       discard_cleanups (old_chain);
1304     }
1305
1306   return head;
1307 }
1308
1309
1310 /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
1311
1312 static int
1313 symbol_add_stub (PTR arg)
1314 {
1315   register struct so_list *so = (struct so_list *) arg;  /* catch_errs bogon */
1316   struct section_addr_info *sap;
1317   CORE_ADDR lowest_addr = 0;
1318   int lowest_index;
1319   asection *lowest_sect = NULL;
1320
1321   /* Have we already loaded this shared object?  */
1322   ALL_OBJFILES (so->objfile)
1323     {
1324       if (strcmp (so->objfile->name, so->so_name) == 0)
1325         return 1;
1326     }
1327
1328   /* Find the shared object's text segment.  */
1329   if (so->textsection)
1330     {
1331       lowest_addr = so->textsection->addr;
1332       lowest_sect = bfd_get_section_by_name (so->abfd, ".text");
1333       lowest_index = lowest_sect->index;
1334     }
1335   else if (so->abfd != NULL)
1336     {
1337       /* If we didn't find a mapped non zero sized .text section, set
1338          up lowest_addr so that the relocation in symbol_file_add does
1339          no harm.  */
1340       lowest_sect = bfd_get_section_by_name (so->abfd, ".text");
1341       if (lowest_sect == NULL)
1342         bfd_map_over_sections (so->abfd, find_lowest_section,
1343                                (PTR) &lowest_sect);
1344       if (lowest_sect)
1345         {
1346           lowest_addr = bfd_section_vma (so->abfd, lowest_sect)
1347             + LM_ADDR (so);
1348           lowest_index = lowest_sect->index;
1349         }
1350     }
1351
1352   sap = build_section_addr_info_from_section_table (so->sections,
1353                                                     so->sections_end);
1354
1355   sap->other[lowest_index].addr = lowest_addr;
1356
1357   so->objfile = symbol_file_add (so->so_name, so->from_tty,
1358                                  sap, 0, OBJF_SHARED);
1359   free_section_addr_info (sap);
1360
1361   return (1);
1362 }
1363
1364
1365 /* LOCAL FUNCTION
1366
1367    update_solib_list --- synchronize GDB's shared object list with inferior's
1368
1369    SYNOPSIS
1370
1371    void update_solib_list (int from_tty, struct target_ops *TARGET)
1372
1373    Extract the list of currently loaded shared objects from the
1374    inferior, and compare it with the list of shared objects currently
1375    in GDB's so_list_head list.  Edit so_list_head to bring it in sync
1376    with the inferior's new list.
1377
1378    If we notice that the inferior has unloaded some shared objects,
1379    free any symbolic info GDB had read about those shared objects.
1380
1381    Don't load symbolic info for any new shared objects; just add them
1382    to the list, and leave their symbols_loaded flag clear.
1383
1384    If FROM_TTY is non-null, feel free to print messages about what
1385    we're doing.
1386
1387    If TARGET is non-null, add the sections of all new shared objects
1388    to TARGET's section table.  Note that this doesn't remove any
1389    sections for shared objects that have been unloaded, and it
1390    doesn't check to see if the new shared objects are already present in
1391    the section table.  But we only use this for core files and
1392    processes we've just attached to, so that's okay.  */
1393
1394 void
1395 update_solib_list (int from_tty, struct target_ops *target)
1396 {
1397   struct so_list *inferior = current_sos ();
1398   struct so_list *gdb, **gdb_link;
1399
1400 #ifdef SVR4_SHARED_LIBS
1401   /* If we are attaching to a running process for which we 
1402      have not opened a symbol file, we may be able to get its 
1403      symbols now!  */
1404   if (attach_flag &&
1405       symfile_objfile == NULL)
1406     catch_errors (open_symbol_file_object, (PTR) &from_tty, 
1407                   "Error reading attached process's symbol file.\n",
1408                   RETURN_MASK_ALL);
1409
1410 #endif SVR4_SHARED_LIBS
1411
1412   /* Since this function might actually add some elements to the
1413      so_list_head list, arrange for it to be cleaned up when
1414      appropriate.  */
1415   if (!solib_cleanup_queued)
1416     {
1417       make_run_cleanup (do_clear_solib, NULL);
1418       solib_cleanup_queued = 1;
1419     }
1420
1421   /* GDB and the inferior's dynamic linker each maintain their own
1422      list of currently loaded shared objects; we want to bring the
1423      former in sync with the latter.  Scan both lists, seeing which
1424      shared objects appear where.  There are three cases:
1425
1426      - A shared object appears on both lists.  This means that GDB
1427      knows about it already, and it's still loaded in the inferior.
1428      Nothing needs to happen.
1429
1430      - A shared object appears only on GDB's list.  This means that
1431      the inferior has unloaded it.  We should remove the shared
1432      object from GDB's tables.
1433
1434      - A shared object appears only on the inferior's list.  This
1435      means that it's just been loaded.  We should add it to GDB's
1436      tables.
1437
1438      So we walk GDB's list, checking each entry to see if it appears
1439      in the inferior's list too.  If it does, no action is needed, and
1440      we remove it from the inferior's list.  If it doesn't, the
1441      inferior has unloaded it, and we remove it from GDB's list.  By
1442      the time we're done walking GDB's list, the inferior's list
1443      contains only the new shared objects, which we then add.  */
1444
1445   gdb = so_list_head;
1446   gdb_link = &so_list_head;
1447   while (gdb)
1448     {
1449       struct so_list *i = inferior;
1450       struct so_list **i_link = &inferior;
1451
1452       /* Check to see whether the shared object *gdb also appears in
1453          the inferior's current list.  */
1454       while (i)
1455         {
1456           if (! strcmp (gdb->so_original_name, i->so_original_name))
1457             break;
1458
1459           i_link = &i->next;
1460           i = *i_link;
1461         }
1462
1463       /* If the shared object appears on the inferior's list too, then
1464          it's still loaded, so we don't need to do anything.  Delete
1465          it from the inferior's list, and leave it on GDB's list.  */
1466       if (i)
1467         {
1468           *i_link = i->next;
1469           free_so (i);
1470           gdb_link = &gdb->next;
1471           gdb = *gdb_link;
1472         }
1473
1474       /* If it's not on the inferior's list, remove it from GDB's tables.  */
1475       else
1476         {
1477           *gdb_link = gdb->next;
1478
1479           /* Unless the user loaded it explicitly, free SO's objfile.  */
1480           if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED))
1481             free_objfile (gdb->objfile);
1482
1483           /* Some targets' section tables might be referring to
1484              sections from so->abfd; remove them.  */
1485           remove_target_sections (gdb->abfd);
1486
1487           free_so (gdb);
1488           gdb = *gdb_link;
1489         }
1490     }
1491
1492   /* Now the inferior's list contains only shared objects that don't
1493      appear in GDB's list --- those that are newly loaded.  Add them
1494      to GDB's shared object list.  */
1495   if (inferior)
1496     {
1497       struct so_list *i;
1498
1499       /* Add the new shared objects to GDB's list.  */
1500       *gdb_link = inferior;
1501
1502       /* Fill in the rest of each of the `struct so_list' nodes.  */
1503       for (i = inferior; i; i = i->next)
1504         {
1505           i->from_tty = from_tty;
1506
1507           /* Fill in the rest of the `struct so_list' node.  */
1508           catch_errors (solib_map_sections, i,
1509                         "Error while mapping shared library sections:\n",
1510                         RETURN_MASK_ALL);
1511         }
1512
1513       /* If requested, add the shared objects' sections to the the
1514          TARGET's section table.  */
1515       if (target)
1516         {
1517           int new_sections;
1518
1519           /* Figure out how many sections we'll need to add in total.  */
1520           new_sections = 0;
1521           for (i = inferior; i; i = i->next)
1522             new_sections += (i->sections_end - i->sections);
1523
1524           if (new_sections > 0)
1525             {
1526               int space = target_resize_to_sections (target, new_sections);
1527
1528               for (i = inferior; i; i = i->next)
1529                 {
1530                   int count = (i->sections_end - i->sections);
1531                   memcpy (target->to_sections + space,
1532                           i->sections,
1533                           count * sizeof (i->sections[0]));
1534                   space += count;
1535                 }
1536             }
1537         }
1538     }
1539 }
1540
1541
1542 /* GLOBAL FUNCTION
1543
1544    solib_add -- read in symbol info for newly added shared libraries
1545
1546    SYNOPSIS
1547
1548    void solib_add (char *pattern, int from_tty, struct target_ops *TARGET)
1549
1550    DESCRIPTION
1551
1552    Read in symbolic information for any shared objects whose names
1553    match PATTERN.  (If we've already read a shared object's symbol
1554    info, leave it alone.)  If PATTERN is zero, read them all.
1555
1556    FROM_TTY and TARGET are as described for update_solib_list, above.  */
1557
1558 void
1559 solib_add (char *pattern, int from_tty, struct target_ops *target)
1560 {
1561   struct so_list *gdb;
1562
1563   if (pattern)
1564     {
1565       char *re_err = re_comp (pattern);
1566
1567       if (re_err)
1568         error ("Invalid regexp: %s", re_err);
1569     }
1570
1571   update_solib_list (from_tty, target);
1572
1573   /* Walk the list of currently loaded shared libraries, and read
1574      symbols for any that match the pattern --- or any whose symbols
1575      aren't already loaded, if no pattern was given.  */
1576   {
1577     int any_matches = 0;
1578     int loaded_any_symbols = 0;
1579
1580     for (gdb = so_list_head; gdb; gdb = gdb->next)
1581       if (! pattern || re_exec (gdb->so_name))
1582         {
1583           any_matches = 1;
1584
1585           if (gdb->symbols_loaded)
1586             {
1587               if (from_tty)
1588                 printf_unfiltered ("Symbols already loaded for %s\n",
1589                                    gdb->so_name);
1590             }
1591           else
1592             {
1593               if (catch_errors
1594                   (symbol_add_stub, gdb,
1595                    "Error while reading shared library symbols:\n",
1596                    RETURN_MASK_ALL))
1597                 {
1598                   if (from_tty)
1599                     printf_unfiltered ("Loaded symbols for %s\n",
1600                                        gdb->so_name);
1601                   gdb->symbols_loaded = 1;
1602                   loaded_any_symbols = 1;
1603                 }
1604             }
1605         }
1606
1607     if (from_tty && pattern && ! any_matches)
1608       printf_unfiltered
1609         ("No loaded shared libraries match the pattern `%s'.\n", pattern);
1610
1611     if (loaded_any_symbols)
1612       {
1613         /* Getting new symbols may change our opinion about what is
1614            frameless.  */
1615         reinit_frame_cache ();
1616
1617         special_symbol_handling ();
1618       }
1619   }
1620 }
1621
1622
1623 /*
1624
1625    LOCAL FUNCTION
1626
1627    info_sharedlibrary_command -- code for "info sharedlibrary"
1628
1629    SYNOPSIS
1630
1631    static void info_sharedlibrary_command ()
1632
1633    DESCRIPTION
1634
1635    Walk through the shared library list and print information
1636    about each attached library.
1637  */
1638
1639 static void
1640 info_sharedlibrary_command (char *ignore, int from_tty)
1641 {
1642   register struct so_list *so = NULL;   /* link map state variable */
1643   int header_done = 0;
1644   int addr_width;
1645   char *addr_fmt;
1646   int arch_size;
1647
1648   if (exec_bfd == NULL)
1649     {
1650       printf_unfiltered ("No executable file.\n");
1651       return;
1652     }
1653
1654   arch_size = bfd_get_arch_size (exec_bfd);
1655   /* Default to 32-bit in case of failure (non-elf). */
1656   if (arch_size == 32 || arch_size == -1)
1657     {
1658       addr_width = 8 + 4;
1659       addr_fmt = "08l";
1660     }
1661   else if (arch_size == 64)
1662     {
1663       addr_width = 16 + 4;
1664       addr_fmt = "016l";
1665     }
1666
1667   update_solib_list (from_tty, 0);
1668
1669   for (so = so_list_head; so; so = so->next)
1670     {
1671       if (so->so_name[0])
1672         {
1673           if (!header_done)
1674             {
1675               printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width, "From",
1676                                  addr_width, "To", "Syms Read",
1677                                  "Shared Object Library");
1678               header_done++;
1679             }
1680
1681           printf_unfiltered ("%-*s", addr_width,
1682                       local_hex_string_custom ((unsigned long) LM_ADDR (so),
1683                                                addr_fmt));
1684           printf_unfiltered ("%-*s", addr_width,
1685                          local_hex_string_custom ((unsigned long) so->lmend,
1686                                                   addr_fmt));
1687           printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No");
1688           printf_unfiltered ("%s\n", so->so_name);
1689         }
1690     }
1691   if (so_list_head == NULL)
1692     {
1693       printf_unfiltered ("No shared libraries loaded at this time.\n");
1694     }
1695 }
1696
1697 /*
1698
1699    GLOBAL FUNCTION
1700
1701    solib_address -- check to see if an address is in a shared lib
1702
1703    SYNOPSIS
1704
1705    char * solib_address (CORE_ADDR address)
1706
1707    DESCRIPTION
1708
1709    Provides a hook for other gdb routines to discover whether or
1710    not a particular address is within the mapped address space of
1711    a shared library.  Any address between the base mapping address
1712    and the first address beyond the end of the last mapping, is
1713    considered to be within the shared library address space, for
1714    our purposes.
1715
1716    For example, this routine is called at one point to disable
1717    breakpoints which are in shared libraries that are not currently
1718    mapped in.
1719  */
1720
1721 char *
1722 solib_address (CORE_ADDR address)
1723 {
1724   register struct so_list *so = 0;      /* link map state variable */
1725
1726   for (so = so_list_head; so; so = so->next)
1727     {
1728       if (LM_ADDR (so) <= address && address < so->lmend)
1729         return (so->so_name);
1730     }
1731
1732   return (0);
1733 }
1734
1735 /* Called by free_all_symtabs */
1736
1737 void
1738 clear_solib (void)
1739 {
1740   /* This function is expected to handle ELF shared libraries.  It is
1741      also used on Solaris, which can run either ELF or a.out binaries
1742      (for compatibility with SunOS 4), both of which can use shared
1743      libraries.  So we don't know whether we have an ELF executable or
1744      an a.out executable until the user chooses an executable file.
1745
1746      ELF shared libraries don't get mapped into the address space
1747      until after the program starts, so we'd better not try to insert
1748      breakpoints in them immediately.  We have to wait until the
1749      dynamic linker has loaded them; we'll hit a bp_shlib_event
1750      breakpoint (look for calls to create_solib_event_breakpoint) when
1751      it's ready.
1752
1753      SunOS shared libraries seem to be different --- they're present
1754      as soon as the process begins execution, so there's no need to
1755      put off inserting breakpoints.  There's also nowhere to put a
1756      bp_shlib_event breakpoint, so if we put it off, we'll never get
1757      around to it.
1758
1759      So: disable breakpoints only if we're using ELF shared libs.  */
1760   if (exec_bfd != NULL
1761       && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
1762     disable_breakpoints_in_shlibs (1);
1763
1764   while (so_list_head)
1765     {
1766       struct so_list *so = so_list_head;
1767       so_list_head = so->next;
1768       free_so (so);
1769     }
1770
1771   debug_base = 0;
1772 }
1773
1774 static void
1775 do_clear_solib (PTR dummy)
1776 {
1777   solib_cleanup_queued = 0;
1778   clear_solib ();
1779 }
1780
1781 #ifdef SVR4_SHARED_LIBS
1782
1783 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1784    SVR4 run time loader.  */
1785
1786 static CORE_ADDR interp_text_sect_low;
1787 static CORE_ADDR interp_text_sect_high;
1788 static CORE_ADDR interp_plt_sect_low;
1789 static CORE_ADDR interp_plt_sect_high;
1790
1791 int
1792 in_svr4_dynsym_resolve_code (CORE_ADDR pc)
1793 {
1794   return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
1795           || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
1796           || in_plt_section (pc, NULL));
1797 }
1798 #endif
1799
1800 /*
1801
1802    LOCAL FUNCTION
1803
1804    disable_break -- remove the "mapping changed" breakpoint
1805
1806    SYNOPSIS
1807
1808    static int disable_break ()
1809
1810    DESCRIPTION
1811
1812    Removes the breakpoint that gets hit when the dynamic linker
1813    completes a mapping change.
1814
1815  */
1816
1817 #ifndef SVR4_SHARED_LIBS
1818
1819 static int
1820 disable_break (void)
1821 {
1822   int status = 1;
1823
1824 #ifndef SVR4_SHARED_LIBS
1825
1826   int in_debugger = 0;
1827
1828   /* Read the debugger structure from the inferior to retrieve the
1829      address of the breakpoint and the original contents of the
1830      breakpoint address.  Remove the breakpoint by writing the original
1831      contents back. */
1832
1833   read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1834
1835   /* Set `in_debugger' to zero now. */
1836
1837   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1838
1839   breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr);
1840   write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1841                 sizeof (debug_copy.ldd_bp_inst));
1842
1843 #else /* SVR4_SHARED_LIBS */
1844
1845   /* Note that breakpoint address and original contents are in our address
1846      space, so we just need to write the original contents back. */
1847
1848   if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
1849     {
1850       status = 0;
1851     }
1852
1853 #endif /* !SVR4_SHARED_LIBS */
1854
1855   /* For the SVR4 version, we always know the breakpoint address.  For the
1856      SunOS version we don't know it until the above code is executed.
1857      Grumble if we are stopped anywhere besides the breakpoint address. */
1858
1859   if (stop_pc != breakpoint_addr)
1860     {
1861       warning ("stopped at unknown breakpoint while handling shared libraries");
1862     }
1863
1864   return (status);
1865 }
1866
1867 #endif /* #ifdef SVR4_SHARED_LIBS */
1868
1869 /*
1870
1871    LOCAL FUNCTION
1872
1873    enable_break -- arrange for dynamic linker to hit breakpoint
1874
1875    SYNOPSIS
1876
1877    int enable_break (void)
1878
1879    DESCRIPTION
1880
1881    Both the SunOS and the SVR4 dynamic linkers have, as part of their
1882    debugger interface, support for arranging for the inferior to hit
1883    a breakpoint after mapping in the shared libraries.  This function
1884    enables that breakpoint.
1885
1886    For SunOS, there is a special flag location (in_debugger) which we
1887    set to 1.  When the dynamic linker sees this flag set, it will set
1888    a breakpoint at a location known only to itself, after saving the
1889    original contents of that place and the breakpoint address itself,
1890    in it's own internal structures.  When we resume the inferior, it
1891    will eventually take a SIGTRAP when it runs into the breakpoint.
1892    We handle this (in a different place) by restoring the contents of
1893    the breakpointed location (which is only known after it stops),
1894    chasing around to locate the shared libraries that have been
1895    loaded, then resuming.
1896
1897    For SVR4, the debugger interface structure contains a member (r_brk)
1898    which is statically initialized at the time the shared library is
1899    built, to the offset of a function (_r_debug_state) which is guaran-
1900    teed to be called once before mapping in a library, and again when
1901    the mapping is complete.  At the time we are examining this member,
1902    it contains only the unrelocated offset of the function, so we have
1903    to do our own relocation.  Later, when the dynamic linker actually
1904    runs, it relocates r_brk to be the actual address of _r_debug_state().
1905
1906    The debugger interface structure also contains an enumeration which
1907    is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1908    depending upon whether or not the library is being mapped or unmapped,
1909    and then set to RT_CONSISTENT after the library is mapped/unmapped.
1910  */
1911
1912 static int
1913 enable_break (void)
1914 {
1915   int success = 0;
1916
1917 #ifndef SVR4_SHARED_LIBS
1918
1919   int j;
1920   int in_debugger;
1921
1922   /* Get link_dynamic structure */
1923
1924   j = target_read_memory (debug_base, (char *) &dynamic_copy,
1925                           sizeof (dynamic_copy));
1926   if (j)
1927     {
1928       /* unreadable */
1929       return (0);
1930     }
1931
1932   /* Calc address of debugger interface structure */
1933
1934   debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
1935
1936   /* Calc address of `in_debugger' member of debugger interface structure */
1937
1938   flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1939                                         (char *) &debug_copy);
1940
1941   /* Write a value of 1 to this member.  */
1942
1943   in_debugger = 1;
1944   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1945   success = 1;
1946
1947 #else /* SVR4_SHARED_LIBS */
1948
1949 #ifdef BKPT_AT_SYMBOL
1950
1951   struct minimal_symbol *msymbol;
1952   char **bkpt_namep;
1953   asection *interp_sect;
1954
1955   /* First, remove all the solib event breakpoints.  Their addresses
1956      may have changed since the last time we ran the program.  */
1957   remove_solib_event_breakpoints ();
1958
1959 #ifdef SVR4_SHARED_LIBS
1960   interp_text_sect_low = interp_text_sect_high = 0;
1961   interp_plt_sect_low = interp_plt_sect_high = 0;
1962
1963   /* Find the .interp section; if not found, warn the user and drop
1964      into the old breakpoint at symbol code.  */
1965   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1966   if (interp_sect)
1967     {
1968       unsigned int interp_sect_size;
1969       char *buf;
1970       CORE_ADDR load_addr;
1971       bfd *tmp_bfd;
1972       CORE_ADDR sym_addr = 0;
1973
1974       /* Read the contents of the .interp section into a local buffer;
1975          the contents specify the dynamic linker this program uses.  */
1976       interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
1977       buf = alloca (interp_sect_size);
1978       bfd_get_section_contents (exec_bfd, interp_sect,
1979                                 buf, 0, interp_sect_size);
1980
1981       /* Now we need to figure out where the dynamic linker was
1982          loaded so that we can load its symbols and place a breakpoint
1983          in the dynamic linker itself.
1984
1985          This address is stored on the stack.  However, I've been unable
1986          to find any magic formula to find it for Solaris (appears to
1987          be trivial on GNU/Linux).  Therefore, we have to try an alternate
1988          mechanism to find the dynamic linker's base address.  */
1989       tmp_bfd = bfd_openr (buf, gnutarget);
1990       if (tmp_bfd == NULL)
1991         goto bkpt_at_symbol;
1992
1993       /* Make sure the dynamic linker's really a useful object.  */
1994       if (!bfd_check_format (tmp_bfd, bfd_object))
1995         {
1996           warning ("Unable to grok dynamic linker %s as an object file", buf);
1997           bfd_close (tmp_bfd);
1998           goto bkpt_at_symbol;
1999         }
2000
2001       /* We find the dynamic linker's base address by examining the
2002          current pc (which point at the entry point for the dynamic
2003          linker) and subtracting the offset of the entry point.  */
2004       load_addr = read_pc () - tmp_bfd->start_address;
2005
2006       /* Record the relocated start and end address of the dynamic linker
2007          text and plt section for in_svr4_dynsym_resolve_code.  */
2008       interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
2009       if (interp_sect)
2010         {
2011           interp_text_sect_low =
2012             bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
2013           interp_text_sect_high =
2014             interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
2015         }
2016       interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
2017       if (interp_sect)
2018         {
2019           interp_plt_sect_low =
2020             bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
2021           interp_plt_sect_high =
2022             interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
2023         }
2024
2025       /* Now try to set a breakpoint in the dynamic linker.  */
2026       for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
2027         {
2028           sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
2029           if (sym_addr != 0)
2030             break;
2031         }
2032
2033       /* We're done with the temporary bfd.  */
2034       bfd_close (tmp_bfd);
2035
2036       if (sym_addr != 0)
2037         {
2038           create_solib_event_breakpoint (load_addr + sym_addr);
2039           return 1;
2040         }
2041
2042       /* For whatever reason we couldn't set a breakpoint in the dynamic
2043          linker.  Warn and drop into the old code.  */
2044     bkpt_at_symbol:
2045       warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
2046     }
2047 #endif
2048
2049   /* Scan through the list of symbols, trying to look up the symbol and
2050      set a breakpoint there.  Terminate loop when we/if we succeed. */
2051
2052   breakpoint_addr = 0;
2053   for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
2054     {
2055       msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
2056       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
2057         {
2058           create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
2059           return 1;
2060         }
2061     }
2062
2063   /* Nothing good happened.  */
2064   success = 0;
2065
2066 #endif /* BKPT_AT_SYMBOL */
2067
2068 #endif /* !SVR4_SHARED_LIBS */
2069
2070   return (success);
2071 }
2072
2073 /*
2074
2075    GLOBAL FUNCTION
2076
2077    solib_create_inferior_hook -- shared library startup support
2078
2079    SYNOPSIS
2080
2081    void solib_create_inferior_hook()
2082
2083    DESCRIPTION
2084
2085    When gdb starts up the inferior, it nurses it along (through the
2086    shell) until it is ready to execute it's first instruction.  At this
2087    point, this function gets called via expansion of the macro
2088    SOLIB_CREATE_INFERIOR_HOOK.
2089
2090    For SunOS executables, this first instruction is typically the
2091    one at "_start", or a similar text label, regardless of whether
2092    the executable is statically or dynamically linked.  The runtime
2093    startup code takes care of dynamically linking in any shared
2094    libraries, once gdb allows the inferior to continue.
2095
2096    For SVR4 executables, this first instruction is either the first
2097    instruction in the dynamic linker (for dynamically linked
2098    executables) or the instruction at "start" for statically linked
2099    executables.  For dynamically linked executables, the system
2100    first exec's /lib/libc.so.N, which contains the dynamic linker,
2101    and starts it running.  The dynamic linker maps in any needed
2102    shared libraries, maps in the actual user executable, and then
2103    jumps to "start" in the user executable.
2104
2105    For both SunOS shared libraries, and SVR4 shared libraries, we
2106    can arrange to cooperate with the dynamic linker to discover the
2107    names of shared libraries that are dynamically linked, and the
2108    base addresses to which they are linked.
2109
2110    This function is responsible for discovering those names and
2111    addresses, and saving sufficient information about them to allow
2112    their symbols to be read at a later time.
2113
2114    FIXME
2115
2116    Between enable_break() and disable_break(), this code does not
2117    properly handle hitting breakpoints which the user might have
2118    set in the startup code or in the dynamic linker itself.  Proper
2119    handling will probably have to wait until the implementation is
2120    changed to use the "breakpoint handler function" method.
2121
2122    Also, what if child has exit()ed?  Must exit loop somehow.
2123  */
2124
2125 void
2126 solib_create_inferior_hook (void)
2127 {
2128   /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
2129      yet.  In fact, in the case of a SunOS4 executable being run on
2130      Solaris, we can't get it yet.  current_sos will get it when it needs
2131      it.  */
2132 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
2133   if ((debug_base = locate_base ()) == 0)
2134     {
2135       /* Can't find the symbol or the executable is statically linked. */
2136       return;
2137     }
2138 #endif
2139
2140   if (!enable_break ())
2141     {
2142       warning ("shared library handler failed to enable breakpoint");
2143       return;
2144     }
2145
2146 #if !defined(SVR4_SHARED_LIBS) || defined(_SCO_DS)
2147   /* SCO and SunOS need the loop below, other systems should be using the
2148      special shared library breakpoints and the shared library breakpoint
2149      service routine.
2150
2151      Now run the target.  It will eventually hit the breakpoint, at
2152      which point all of the libraries will have been mapped in and we
2153      can go groveling around in the dynamic linker structures to find
2154      out what we need to know about them. */
2155
2156   clear_proceed_status ();
2157   stop_soon_quietly = 1;
2158   stop_signal = TARGET_SIGNAL_0;
2159   do
2160     {
2161       target_resume (-1, 0, stop_signal);
2162       wait_for_inferior ();
2163     }
2164   while (stop_signal != TARGET_SIGNAL_TRAP);
2165   stop_soon_quietly = 0;
2166
2167 #if !defined(_SCO_DS)
2168   /* We are now either at the "mapping complete" breakpoint (or somewhere
2169      else, a condition we aren't prepared to deal with anyway), so adjust
2170      the PC as necessary after a breakpoint, disable the breakpoint, and
2171      add any shared libraries that were mapped in. */
2172
2173   if (DECR_PC_AFTER_BREAK)
2174     {
2175       stop_pc -= DECR_PC_AFTER_BREAK;
2176       write_register (PC_REGNUM, stop_pc);
2177     }
2178
2179   if (!disable_break ())
2180     {
2181       warning ("shared library handler failed to disable breakpoint");
2182     }
2183
2184   if (auto_solib_add)
2185     solib_add ((char *) 0, 0, (struct target_ops *) 0);
2186 #endif /* ! _SCO_DS */
2187 #endif
2188 }
2189
2190 /*
2191
2192    LOCAL FUNCTION
2193
2194    special_symbol_handling -- additional shared library symbol handling
2195
2196    SYNOPSIS
2197
2198    void special_symbol_handling ()
2199
2200    DESCRIPTION
2201
2202    Once the symbols from a shared object have been loaded in the usual
2203    way, we are called to do any system specific symbol handling that 
2204    is needed.
2205
2206    For SunOS4, this consists of grunging around in the dynamic
2207    linkers structures to find symbol definitions for "common" symbols
2208    and adding them to the minimal symbol table for the runtime common
2209    objfile.
2210
2211  */
2212
2213 static void
2214 special_symbol_handling (void)
2215 {
2216 #ifndef SVR4_SHARED_LIBS
2217   int j;
2218
2219   if (debug_addr == 0)
2220     {
2221       /* Get link_dynamic structure */
2222
2223       j = target_read_memory (debug_base, (char *) &dynamic_copy,
2224                               sizeof (dynamic_copy));
2225       if (j)
2226         {
2227           /* unreadable */
2228           return;
2229         }
2230
2231       /* Calc address of debugger interface structure */
2232       /* FIXME, this needs work for cross-debugging of core files
2233          (byteorder, size, alignment, etc).  */
2234
2235       debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
2236     }
2237
2238   /* Read the debugger structure from the inferior, just to make sure
2239      we have a current copy. */
2240
2241   j = target_read_memory (debug_addr, (char *) &debug_copy,
2242                           sizeof (debug_copy));
2243   if (j)
2244     return;                     /* unreadable */
2245
2246   /* Get common symbol definitions for the loaded object. */
2247
2248   if (debug_copy.ldd_cp)
2249     {
2250       solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp));
2251     }
2252
2253 #endif /* !SVR4_SHARED_LIBS */
2254 }
2255
2256
2257 /*
2258
2259    LOCAL FUNCTION
2260
2261    sharedlibrary_command -- handle command to explicitly add library
2262
2263    SYNOPSIS
2264
2265    static void sharedlibrary_command (char *args, int from_tty)
2266
2267    DESCRIPTION
2268
2269  */
2270
2271 static void
2272 sharedlibrary_command (char *args, int from_tty)
2273 {
2274   dont_repeat ();
2275   solib_add (args, from_tty, (struct target_ops *) 0);
2276 }
2277
2278 #endif /* HAVE_LINK_H */
2279
2280 void
2281 _initialize_solib (void)
2282 {
2283 #ifdef HAVE_LINK_H
2284
2285   add_com ("sharedlibrary", class_files, sharedlibrary_command,
2286            "Load shared object library symbols for files matching REGEXP.");
2287   add_info ("sharedlibrary", info_sharedlibrary_command,
2288             "Status of loaded shared object libraries.");
2289
2290   add_show_from_set
2291     (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
2292                   (char *) &auto_solib_add,
2293                   "Set autoloading of shared library symbols.\n\
2294 If nonzero, symbols from all shared object libraries will be loaded\n\
2295 automatically when the inferior begins execution or when the dynamic linker\n\
2296 informs gdb that a new library has been loaded.  Otherwise, symbols\n\
2297 must be loaded manually, using `sharedlibrary'.",
2298                   &setlist),
2299      &showlist);
2300
2301   add_show_from_set
2302     (add_set_cmd ("solib-absolute-prefix", class_support, var_filename,
2303                   (char *) &solib_absolute_prefix,
2304                   "Set prefix for loading absolute shared library symbol files.\n\
2305 For other (relative) files, you can add values using `set solib-search-path'.",
2306                   &setlist),
2307      &showlist);
2308   add_show_from_set
2309     (add_set_cmd ("solib-search-path", class_support, var_string,
2310                   (char *) &solib_search_path,
2311                   "Set the search path for loading non-absolute shared library symbol files.\n\
2312 This takes precedence over the environment variables PATH and LD_LIBRARY_PATH.",
2313                   &setlist),
2314      &showlist);
2315
2316 #endif /* HAVE_LINK_H */
2317 }