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