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