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