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