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