]> Git Repo - binutils.git/blob - gdb/solib.c
* core.c (dis_asm_read_memory): drop fourth arg which conflicts with
[binutils.git] / gdb / solib.c
1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3    
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 #include "defs.h"
22
23 #include <sys/types.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <link.h>
27 #include <sys/param.h>
28 #include <fcntl.h>
29
30 #ifndef SVR4_SHARED_LIBS
31  /* SunOS shared libs need the nlist structure.  */
32 #include <a.out.h> 
33 #endif
34
35 #include "symtab.h"
36 #include "bfd.h"
37 #include "symfile.h"
38 #include "objfiles.h"
39 #include "gdbcore.h"
40 #include "command.h"
41 #include "target.h"
42 #include "frame.h"
43 #include "regex.h"
44 #include "inferior.h"
45
46 #define MAX_PATH_SIZE 256               /* FIXME: Should be dynamic */
47
48 /* On SVR4 systems, for the initial implementation, use main() as the
49    "startup mapping complete" breakpoint address.  The models for SunOS
50    and SVR4 dynamic linking debugger support are different in that SunOS
51    hits one breakpoint when all mapping is complete while using the SVR4
52    debugger support takes two breakpoint hits for each file mapped, and
53    there is no way to know when the "last" one is hit.  Both these
54    mechanisms should be tied to a "breakpoint service routine" that
55    gets automatically executed whenever one of the breakpoints indicating
56    a change in mapping is hit.  This is a future enhancement.  (FIXME) */
57
58 #define BKPT_AT_MAIN 1
59
60 /* local data declarations */
61
62 #ifndef SVR4_SHARED_LIBS
63
64 #define DEBUG_BASE "_DYNAMIC"
65 #define LM_ADDR(so) ((so) -> lm.lm_addr)
66 #define LM_NEXT(so) ((so) -> lm.lm_next)
67 #define LM_NAME(so) ((so) -> lm.lm_name)
68 static struct link_dynamic dynamic_copy;
69 static struct link_dynamic_2 ld_2_copy;
70 static struct ld_debug debug_copy;
71 static CORE_ADDR debug_addr;
72 static CORE_ADDR flag_addr;
73
74 #else   /* SVR4_SHARED_LIBS */
75
76 #define DEBUG_BASE "_r_debug"
77 #define LM_ADDR(so) ((so) -> lm.l_addr)
78 #define LM_NEXT(so) ((so) -> lm.l_next)
79 #define LM_NAME(so) ((so) -> lm.l_name)
80 static struct r_debug debug_copy;
81 char shadow_contents[BREAKPOINT_MAX];   /* Stash old bkpt addr contents */
82
83 #endif  /* !SVR4_SHARED_LIBS */
84
85 struct so_list {
86   struct so_list *next;                 /* next structure in linked list */
87   struct link_map lm;                   /* copy of link map from inferior */
88   struct link_map *lmaddr;              /* addr in inferior lm was read from */
89   CORE_ADDR lmend;                      /* upper addr bound of mapped object */
90   char so_name[MAX_PATH_SIZE];          /* shared object lib name (FIXME) */
91   char symbols_loaded;                  /* flag: symbols read in yet? */
92   char from_tty;                        /* flag: print msgs? */
93   struct objfile *objfile;              /* objfile for loaded lib */
94   struct section_table *sections;
95   struct section_table *sections_end;
96   struct section_table *textsection;
97 };
98
99 static struct so_list *so_list_head;    /* List of known shared objects */
100 static CORE_ADDR debug_base;            /* Base of dynamic linker structures */
101 static CORE_ADDR breakpoint_addr;       /* Address where end bkpt is set */
102
103 extern int
104 fdmatch PARAMS ((int, int));            /* In libiberty */
105
106 /* Local function prototypes */
107
108 static void
109 special_symbol_handling PARAMS ((struct so_list *));
110
111 static void
112 sharedlibrary_command PARAMS ((char *, int));
113
114 static int
115 enable_break PARAMS ((void));
116
117 static int
118 disable_break PARAMS ((void));
119
120 static void
121 info_sharedlibrary_command PARAMS ((char *, int));
122
123 static int
124 symbol_add_stub PARAMS ((char *));
125
126 static struct so_list *
127 find_solib PARAMS ((struct so_list *));
128
129 static struct link_map *
130 first_link_map_member PARAMS ((void));
131
132 static CORE_ADDR
133 locate_base PARAMS ((void));
134
135 static void
136 solib_map_sections PARAMS ((struct so_list *));
137
138 #ifdef SVR4_SHARED_LIBS
139
140 static int
141 look_for_base PARAMS ((int, CORE_ADDR));
142
143 static CORE_ADDR
144 bfd_lookup_symbol PARAMS ((bfd *, char *));
145
146 #else
147
148 static void
149 solib_add_common_symbols PARAMS ((struct rtc_symb *, struct objfile *));
150
151 #endif
152
153 /*
154
155 LOCAL FUNCTION
156
157         solib_map_sections -- open bfd and build sections for shared lib
158
159 SYNOPSIS
160
161         static void solib_map_sections (struct so_list *so)
162
163 DESCRIPTION
164
165         Given a pointer to one of the shared objects in our list
166         of mapped objects, use the recorded name to open a bfd
167         descriptor for the object, build a section table, and then
168         relocate all the section addresses by the base address at
169         which the shared object was mapped.
170
171 FIXMES
172
173         In most (all?) cases the shared object file name recorded in the
174         dynamic linkage tables will be a fully qualified pathname.  For
175         cases where it isn't, do we really mimic the systems search
176         mechanism correctly in the below code (particularly the tilde
177         expansion stuff?).
178  */
179
180 static void
181 solib_map_sections (so)
182      struct so_list *so;
183 {
184   char *filename;
185   char *scratch_pathname;
186   int scratch_chan;
187   struct section_table *p;
188   struct cleanup *old_chain;
189   bfd *abfd;
190   
191   filename = tilde_expand (so -> so_name);
192   old_chain = make_cleanup (free, filename);
193   
194   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
195                         &scratch_pathname);
196   if (scratch_chan < 0)
197     {
198       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
199                             O_RDONLY, 0, &scratch_pathname);
200     }
201   if (scratch_chan < 0)
202     {
203       perror_with_name (filename);
204     }  
205   make_cleanup (free, scratch_pathname);
206
207   abfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
208   if (!abfd)
209     {
210       close (scratch_chan);
211       error ("Could not open `%s' as an executable file: %s",
212              scratch_pathname, bfd_errmsg (bfd_error));
213     }
214
215   make_cleanup (bfd_close, abfd);       /* Zap bfd, close scratch_chan. */
216
217   if (!bfd_check_format (abfd, bfd_object))
218     {
219       error ("\"%s\": not in executable format: %s.",
220              scratch_pathname, bfd_errmsg (bfd_error));
221     }
222   if (build_section_table (abfd, &so -> sections, &so -> sections_end))
223     {
224       error ("Can't find the file sections in `%s': %s", 
225              exec_bfd -> filename, bfd_errmsg (bfd_error));
226     }
227
228   for (p = so -> sections; p < so -> sections_end; p++)
229     {
230       /* Relocate the section binding addresses as recorded in the shared
231          object's file by the base address to which the object was actually
232          mapped. */
233       p -> addr += (CORE_ADDR) LM_ADDR (so);
234       p -> endaddr += (CORE_ADDR) LM_ADDR (so);
235       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
236       if (STREQ (p -> sec_ptr -> name, ".text"))
237         {
238           so -> textsection = p;
239         }
240     }
241
242   /* Free the file names, close the file now.  */
243   do_cleanups (old_chain);
244 }
245
246 /* Read all dynamically loaded common symbol definitions from the inferior
247    and add them to the minimal symbol table for the shared library objfile.  */
248
249 #ifndef SVR4_SHARED_LIBS
250
251 static void
252 solib_add_common_symbols (rtc_symp, objfile)
253     struct rtc_symb *rtc_symp;
254     struct objfile *objfile;
255 {
256   struct rtc_symb inferior_rtc_symb;
257   struct nlist inferior_rtc_nlist;
258   int len;
259   char *name;
260   char *origname;
261
262   init_minimal_symbol_collection ();
263   make_cleanup (discard_minimal_symbols, 0);
264
265   while (rtc_symp)
266     {
267       read_memory ((CORE_ADDR) rtc_symp,
268                    (char *) &inferior_rtc_symb,
269                    sizeof (inferior_rtc_symb));
270       read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
271                    (char *) &inferior_rtc_nlist,
272                    sizeof(inferior_rtc_nlist));
273       if (inferior_rtc_nlist.n_type == N_COMM)
274         {
275           /* FIXME: The length of the symbol name is not available, but in the
276              current implementation the common symbol is allocated immediately
277              behind the name of the symbol. */
278           len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
279
280           origname = name = xmalloc (len);
281           read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
282
283           /* Don't enter the symbol twice if the target is re-run. */
284
285           if (name[0] == bfd_get_symbol_leading_char (objfile->obfd))
286             {
287               name++;
288             }
289
290           /* FIXME:  Do we really want to exclude symbols which happen
291              to match symbols for other locations in the inferior's
292              address space, even when they are in different linkage units? */
293           if (lookup_minimal_symbol (name, (struct objfile *) NULL) == NULL)
294             {
295               name = obsavestring (name, strlen (name),
296                                    &objfile -> symbol_obstack);
297               prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
298                                           mst_bss);
299             }
300           free (origname);
301         }
302       rtc_symp = inferior_rtc_symb.rtc_next;
303     }
304
305   /* Install any minimal symbols that have been collected as the current
306      minimal symbols for this objfile. */
307
308   install_minimal_symbols (objfile);
309 }
310
311 #endif  /* SVR4_SHARED_LIBS */
312
313 #ifdef SVR4_SHARED_LIBS
314
315 /*
316
317 LOCAL FUNCTION
318
319         bfd_lookup_symbol -- lookup the value for a specific symbol
320
321 SYNOPSIS
322
323         CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
324
325 DESCRIPTION
326
327         An expensive way to lookup the value of a single symbol for
328         bfd's that are only temporary anyway.  This is used by the
329         shared library support to find the address of the debugger
330         interface structures in the shared library.
331
332         Note that 0 is specifically allowed as an error return (no
333         such symbol).
334
335         FIXME:  See if there is a less "expensive" way of doing this.
336         Also see if there is already another bfd or gdb function
337         that specifically does this, and if so, use it.
338 */
339
340 static CORE_ADDR
341 bfd_lookup_symbol (abfd, symname)
342      bfd *abfd;
343      char *symname;
344 {
345   unsigned int storage_needed;
346   asymbol *sym;
347   asymbol **symbol_table;
348   unsigned int number_of_symbols;
349   unsigned int i;
350   struct cleanup *back_to;
351   CORE_ADDR symaddr = 0;
352   
353   storage_needed = get_symtab_upper_bound (abfd);
354
355   if (storage_needed > 0)
356     {
357       symbol_table = (asymbol **) xmalloc (storage_needed);
358       back_to = make_cleanup (free, (PTR)symbol_table);
359       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
360   
361       for (i = 0; i < number_of_symbols; i++)
362         {
363           sym = *symbol_table++;
364           if (STREQ (sym -> name, symname))
365             {
366               symaddr = sym -> value;
367               break;
368             }
369         }
370       do_cleanups (back_to);
371     }
372   return (symaddr);
373 }
374
375 /*
376
377 LOCAL FUNCTION
378
379         look_for_base -- examine file for each mapped address segment
380
381 SYNOPSYS
382
383         static int look_for_base (int fd, CORE_ADDR baseaddr)
384
385 DESCRIPTION
386
387         This function is passed to proc_iterate_over_mappings, which
388         causes it to get called once for each mapped address space, with
389         an open file descriptor for the file mapped to that space, and the
390         base address of that mapped space.
391
392         Our job is to find the symbol DEBUG_BASE in the file that this
393         fd is open on, if it exists, and if so, initialize the dynamic
394         linker structure base address debug_base.
395
396         Note that this is a computationally expensive proposition, since
397         we basically have to open a bfd on every call, so we specifically
398         avoid opening the exec file.
399  */
400
401 static int
402 look_for_base (fd, baseaddr)
403      int fd;
404      CORE_ADDR baseaddr;
405 {
406   bfd *interp_bfd;
407   CORE_ADDR address;
408
409   /* If the fd is -1, then there is no file that corresponds to this
410      mapped memory segment, so skip it.  Also, if the fd corresponds
411      to the exec file, skip it as well. */
412
413   if ((fd == -1) || fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd))
414     {
415       return (0);
416     }
417
418   /* Try to open whatever random file this fd corresponds to.  Note that
419      we have no way currently to find the filename.  Don't gripe about
420      any problems we might have, just fail. */
421
422   if ((interp_bfd = bfd_fdopenr ("unnamed", NULL, fd)) == NULL)
423     {
424       return (0);
425     }
426   if (!bfd_check_format (interp_bfd, bfd_object))
427     {
428       bfd_close (interp_bfd);
429       return (0);
430     }
431
432   /* Now try to find our DEBUG_BASE symbol in this file, which we at
433      least know to be a valid ELF executable or shared library. */
434
435   if ((address = bfd_lookup_symbol (interp_bfd, DEBUG_BASE)) == 0)
436     {
437       bfd_close (interp_bfd);
438       return (0);
439     }
440
441   /* Eureka!  We found the symbol.  But now we may need to relocate it
442      by the base address.  If the symbol's value is less than the base
443      address of the shared library, then it hasn't yet been relocated
444      by the dynamic linker, and we have to do it ourself.  FIXME: Note
445      that we make the assumption that the first segment that corresponds
446      to the shared library has the base address to which the library
447      was relocated. */
448
449   if (address < baseaddr)
450     {
451       address += baseaddr;
452     }
453   debug_base = address;
454   bfd_close (interp_bfd);
455   return (1);
456 }
457
458 #endif
459
460 /*
461
462 LOCAL FUNCTION
463
464         locate_base -- locate the base address of dynamic linker structs
465
466 SYNOPSIS
467
468         CORE_ADDR locate_base (void)
469
470 DESCRIPTION
471
472         For both the SunOS and SVR4 shared library implementations, if the
473         inferior executable has been linked dynamically, there is a single
474         address somewhere in the inferior's data space which is the key to
475         locating all of the dynamic linker's runtime structures.  This
476         address is the value of the symbol defined by the macro DEBUG_BASE.
477         The job of this function is to find and return that address, or to
478         return 0 if there is no such address (the executable is statically
479         linked for example).
480
481         For SunOS, the job is almost trivial, since the dynamic linker and
482         all of it's structures are statically linked to the executable at
483         link time.  Thus the symbol for the address we are looking for has
484         already been added to the minimal symbol table for the executable's
485         objfile at the time the symbol file's symbols were read, and all we
486         have to do is look it up there.  Note that we explicitly do NOT want
487         to find the copies in the shared library.
488
489         The SVR4 version is much more complicated because the dynamic linker
490         and it's structures are located in the shared C library, which gets
491         run as the executable's "interpreter" by the kernel.  We have to go
492         to a lot more work to discover the address of DEBUG_BASE.  Because
493         of this complexity, we cache the value we find and return that value
494         on subsequent invocations.  Note there is no copy in the executable
495         symbol tables.
496
497         Note that we can assume nothing about the process state at the time
498         we need to find this address.  We may be stopped on the first instruc-
499         tion of the interpreter (C shared library), the first instruction of
500         the executable itself, or somewhere else entirely (if we attached
501         to the process for example).
502
503  */
504
505 static CORE_ADDR
506 locate_base ()
507 {
508
509 #ifndef SVR4_SHARED_LIBS
510
511   struct minimal_symbol *msymbol;
512   CORE_ADDR address = 0;
513
514   /* For SunOS, we want to limit the search for DEBUG_BASE to the executable
515      being debugged, since there is a duplicate named symbol in the shared
516      library.  We don't want the shared library versions. */
517
518   msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
519   if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
520     {
521       address = SYMBOL_VALUE_ADDRESS (msymbol);
522     }
523   return (address);
524
525 #else   /* SVR4_SHARED_LIBS */
526
527   /* Check to see if we have a currently valid address, and if so, avoid
528      doing all this work again and just return the cached address.  If
529      we have no cached address, ask the /proc support interface to iterate
530      over the list of mapped address segments, calling look_for_base() for
531      each segment.  When we are done, we will have either found the base
532      address or not. */
533
534   if (debug_base == 0)
535     {
536       proc_iterate_over_mappings (look_for_base);
537     }
538   return (debug_base);
539
540 #endif  /* !SVR4_SHARED_LIBS */
541
542 }
543
544 static struct link_map *
545 first_link_map_member ()
546 {
547   struct link_map *lm = NULL;
548
549 #ifndef SVR4_SHARED_LIBS
550
551   read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
552   if (dynamic_copy.ld_version >= 2)
553     {
554       /* It is a version that we can deal with, so read in the secondary
555          structure and find the address of the link map list from it. */
556       read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
557                    sizeof (struct link_dynamic_2));
558       lm = ld_2_copy.ld_loaded;
559     }
560
561 #else   /* SVR4_SHARED_LIBS */
562
563   read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
564   lm = debug_copy.r_map;
565
566 #endif  /* !SVR4_SHARED_LIBS */
567
568   return (lm);
569 }
570
571 /*
572
573 LOCAL FUNCTION
574
575         find_solib -- step through list of shared objects
576
577 SYNOPSIS
578
579         struct so_list *find_solib (struct so_list *so_list_ptr)
580
581 DESCRIPTION
582
583         This module contains the routine which finds the names of any
584         loaded "images" in the current process. The argument in must be
585         NULL on the first call, and then the returned value must be passed
586         in on subsequent calls. This provides the capability to "step" down
587         the list of loaded objects. On the last object, a NULL value is
588         returned.
589
590         The arg and return value are "struct link_map" pointers, as defined
591         in <link.h>.
592  */
593
594 static struct so_list *
595 find_solib (so_list_ptr)
596      struct so_list *so_list_ptr;       /* Last lm or NULL for first one */
597 {
598   struct so_list *so_list_next = NULL;
599   struct link_map *lm = NULL;
600   struct so_list *new;
601   
602   if (so_list_ptr == NULL)
603     {
604       /* We are setting up for a new scan through the loaded images. */
605       if ((so_list_next = so_list_head) == NULL)
606         {
607           /* We have not already read in the dynamic linking structures
608              from the inferior, lookup the address of the base structure. */
609           debug_base = locate_base ();
610           if (debug_base > 0)
611             {
612               /* Read the base structure in and find the address of the first
613                  link map list member. */
614               lm = first_link_map_member ();
615             }
616         }
617     }
618   else
619     {
620       /* We have been called before, and are in the process of walking
621          the shared library list.  Advance to the next shared object. */
622       if ((lm = LM_NEXT (so_list_ptr)) == NULL)
623         {
624           /* We have hit the end of the list, so check to see if any were
625              added, but be quiet if we can't read from the target any more. */
626           int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
627                                            (char *) &(so_list_ptr -> lm),
628                                            sizeof (struct link_map));
629           if (status == 0)
630             {
631               lm = LM_NEXT (so_list_ptr);
632             }
633           else
634             {
635               lm = NULL;
636             }
637         }
638       so_list_next = so_list_ptr -> next;
639     }
640   if ((so_list_next == NULL) && (lm != NULL))
641     {
642       /* Get next link map structure from inferior image and build a local
643          abbreviated load_map structure */
644       new = (struct so_list *) xmalloc (sizeof (struct so_list));
645       memset ((char *) new, 0, sizeof (struct so_list));
646       new -> lmaddr = lm;
647       /* Add the new node as the next node in the list, or as the root
648          node if this is the first one. */
649       if (so_list_ptr != NULL)
650         {
651           so_list_ptr -> next = new;
652         }
653       else
654         {
655           so_list_head = new;
656         }      
657       so_list_next = new;
658       read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
659                    sizeof (struct link_map));
660       /* For the SVR4 version, there is one entry that has no name
661          (for the inferior executable) since it is not a shared object. */
662       if (LM_NAME (new) != 0)
663         {
664           if (!target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name,
665                       MAX_PATH_SIZE - 1))
666               error ("find_solib: Can't read pathname for load map\n");
667           new -> so_name[MAX_PATH_SIZE - 1] = 0;
668           solib_map_sections (new);
669         }      
670     }
671   return (so_list_next);
672 }
673
674 /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
675
676 static int
677 symbol_add_stub (arg)
678      char *arg;
679 {
680   register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
681   
682   so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
683                                    (unsigned int) so -> textsection -> addr,
684                                    0, 0, 0);
685   return (1);
686 }
687
688 /*
689
690 GLOBAL FUNCTION
691
692         solib_add -- add a shared library file to the symtab and section list
693
694 SYNOPSIS
695
696         void solib_add (char *arg_string, int from_tty,
697                         struct target_ops *target)
698
699 DESCRIPTION
700
701 */
702
703 void
704 solib_add (arg_string, from_tty, target)
705      char *arg_string;
706      int from_tty;
707      struct target_ops *target;
708 {       
709   register struct so_list *so = NULL;           /* link map state variable */
710   char *re_err;
711   int count;
712   int old;
713   
714   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
715     {
716       error ("Invalid regexp: %s", re_err);
717     }
718   
719   /* Getting new symbols may change our opinion about what is
720      frameless.  */
721   reinit_frame_cache ();
722   
723   while ((so = find_solib (so)) != NULL)
724     {
725       if (so -> so_name[0] && re_exec (so -> so_name))
726         {
727           if (so -> symbols_loaded)
728             {
729               if (from_tty)
730                 {
731                   printf ("Symbols already loaded for %s\n", so -> so_name);
732                 }
733             }
734           else
735             {
736               catch_errors (symbol_add_stub, (char *) so,
737                             "Error while reading shared library symbols:\n");
738               
739               special_symbol_handling (so);
740               so -> symbols_loaded = 1;
741               so -> from_tty = from_tty;
742             }
743         }
744     }
745   
746   /* Now add the shared library sections to the section table of the
747      specified target, if any.  */
748   if (target)
749     {
750       /* Count how many new section_table entries there are.  */
751       so = NULL;
752       count = 0;
753       while ((so = find_solib (so)) != NULL)
754         {
755           if (so -> so_name[0])
756             {
757               count += so -> sections_end - so -> sections;
758             }
759         }
760       
761       if (count)
762         {
763           /* Reallocate the target's section table including the new size.  */
764           if (target -> to_sections)
765             {
766               old = target -> to_sections_end - target -> to_sections;
767               target -> to_sections = (struct section_table *)
768                 realloc ((char *)target -> to_sections,
769                          (sizeof (struct section_table)) * (count + old));
770             }
771           else
772             {
773               old = 0;
774               target -> to_sections = (struct section_table *)
775                 malloc ((sizeof (struct section_table)) * count);
776             }
777           target -> to_sections_end = target -> to_sections + (count + old);
778           
779           /* Add these section table entries to the target's table.  */
780           while ((so = find_solib (so)) != NULL)
781             {
782               if (so -> so_name[0])
783                 {
784                   count = so -> sections_end - so -> sections;
785                   memcpy ((char *) (target -> to_sections + old),
786                           so -> sections, 
787                           (sizeof (struct section_table)) * count);
788                   old += count;
789                 }
790             }
791         }
792     }
793 }
794
795 /*
796
797 LOCAL FUNCTION
798
799         info_sharedlibrary_command -- code for "info sharedlibrary"
800
801 SYNOPSIS
802
803         static void info_sharedlibrary_command ()
804
805 DESCRIPTION
806
807         Walk through the shared library list and print information
808         about each attached library.
809 */
810
811 static void
812 info_sharedlibrary_command (ignore, from_tty)
813      char *ignore;
814      int from_tty;
815 {
816   register struct so_list *so = NULL;   /* link map state variable */
817   int header_done = 0;
818   
819   if (exec_bfd == NULL)
820     {
821       printf ("No exec file.\n");
822       return;
823     }
824   while ((so = find_solib (so)) != NULL)
825     {
826       if (so -> so_name[0])
827         {
828           if (!header_done)
829             {
830               printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
831                      "Shared Object Library");
832               header_done++;
833             }
834           printf ("%-12s", local_hex_string_custom ((int) LM_ADDR (so), "08"));
835           printf ("%-12s", local_hex_string_custom (so -> lmend, "08"));
836           printf ("%-12s", so -> symbols_loaded ? "Yes" : "No");
837           printf ("%s\n",  so -> so_name);
838         }
839     }
840   if (so_list_head == NULL)
841     {
842       printf ("No shared libraries loaded at this time.\n");    
843     }
844 }
845
846 /*
847
848 GLOBAL FUNCTION
849
850         solib_address -- check to see if an address is in a shared lib
851
852 SYNOPSIS
853
854         int solib_address (CORE_ADDR address)
855
856 DESCRIPTION
857
858         Provides a hook for other gdb routines to discover whether or
859         not a particular address is within the mapped address space of
860         a shared library.  Any address between the base mapping address
861         and the first address beyond the end of the last mapping, is
862         considered to be within the shared library address space, for
863         our purposes.
864
865         For example, this routine is called at one point to disable
866         breakpoints which are in shared libraries that are not currently
867         mapped in.
868  */
869
870 int
871 solib_address (address)
872      CORE_ADDR address;
873 {
874   register struct so_list *so = 0;      /* link map state variable */
875   
876   while ((so = find_solib (so)) != NULL)
877     {
878       if (so -> so_name[0])
879         {
880           if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
881               (address < (CORE_ADDR) so -> lmend))
882             {
883               return (1);
884             }
885         }
886     }
887   return (0);
888 }
889
890 /* Called by free_all_symtabs */
891
892 void 
893 clear_solib()
894 {
895   struct so_list *next;
896   
897   while (so_list_head)
898     {
899       if (so_list_head -> sections)
900         {
901           free ((PTR)so_list_head -> sections);
902         }
903       next = so_list_head -> next;
904       free((PTR)so_list_head);
905       so_list_head = next;
906     }
907   debug_base = 0;
908 }
909
910 /*
911
912 LOCAL FUNCTION
913
914         disable_break -- remove the "mapping changed" breakpoint
915
916 SYNOPSIS
917
918         static int disable_break ()
919
920 DESCRIPTION
921
922         Removes the breakpoint that gets hit when the dynamic linker
923         completes a mapping change.
924
925 */
926
927 static int
928 disable_break ()
929 {
930   int status = 1;
931
932 #ifndef SVR4_SHARED_LIBS
933
934   int in_debugger = 0;
935   
936   /* Read the debugger structure from the inferior to retrieve the
937      address of the breakpoint and the original contents of the
938      breakpoint address.  Remove the breakpoint by writing the original
939      contents back. */
940
941   read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
942
943   /* Set `in_debugger' to zero now. */
944
945   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
946
947   breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
948   write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
949                 sizeof (debug_copy.ldd_bp_inst));
950
951 #else   /* SVR4_SHARED_LIBS */
952
953   /* Note that breakpoint address and original contents are in our address
954      space, so we just need to write the original contents back. */
955
956   if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
957     {
958       status = 0;
959     }
960
961 #endif  /* !SVR4_SHARED_LIBS */
962
963   /* For the SVR4 version, we always know the breakpoint address.  For the
964      SunOS version we don't know it until the above code is executed.
965      Grumble if we are stopped anywhere besides the breakpoint address. */
966
967   if (stop_pc != breakpoint_addr)
968     {
969       warning ("stopped at unknown breakpoint while handling shared libraries");
970     }
971
972   return (status);
973 }
974
975 /*
976
977 LOCAL FUNCTION
978
979         enable_break -- arrange for dynamic linker to hit breakpoint
980
981 SYNOPSIS
982
983         int enable_break (void)
984
985 DESCRIPTION
986
987         Both the SunOS and the SVR4 dynamic linkers have, as part of their
988         debugger interface, support for arranging for the inferior to hit
989         a breakpoint after mapping in the shared libraries.  This function
990         enables that breakpoint.
991
992         For SunOS, there is a special flag location (in_debugger) which we
993         set to 1.  When the dynamic linker sees this flag set, it will set
994         a breakpoint at a location known only to itself, after saving the
995         original contents of that place and the breakpoint address itself,
996         in it's own internal structures.  When we resume the inferior, it
997         will eventually take a SIGTRAP when it runs into the breakpoint.
998         We handle this (in a different place) by restoring the contents of
999         the breakpointed location (which is only known after it stops),
1000         chasing around to locate the shared libraries that have been
1001         loaded, then resuming.
1002
1003         For SVR4, the debugger interface structure contains a member (r_brk)
1004         which is statically initialized at the time the shared library is
1005         built, to the offset of a function (_r_debug_state) which is guaran-
1006         teed to be called once before mapping in a library, and again when
1007         the mapping is complete.  At the time we are examining this member,
1008         it contains only the unrelocated offset of the function, so we have
1009         to do our own relocation.  Later, when the dynamic linker actually
1010         runs, it relocates r_brk to be the actual address of _r_debug_state().
1011
1012         The debugger interface structure also contains an enumeration which
1013         is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1014         depending upon whether or not the library is being mapped or unmapped,
1015         and then set to RT_CONSISTENT after the library is mapped/unmapped.
1016 */
1017
1018 static int
1019 enable_break ()
1020 {
1021
1022 #ifndef SVR4_SHARED_LIBS
1023
1024   int j;
1025   int in_debugger;
1026
1027   /* Get link_dynamic structure */
1028
1029   j = target_read_memory (debug_base, (char *) &dynamic_copy,
1030                           sizeof (dynamic_copy));
1031   if (j)
1032     {
1033       /* unreadable */
1034       return (0);
1035     }
1036
1037   /* Calc address of debugger interface structure */
1038
1039   debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1040
1041   /* Calc address of `in_debugger' member of debugger interface structure */
1042
1043   flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1044                                         (char *) &debug_copy);
1045
1046   /* Write a value of 1 to this member.  */
1047
1048   in_debugger = 1;
1049
1050   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1051
1052 #else   /* SVR4_SHARED_LIBS */
1053
1054 #ifdef BKPT_AT_MAIN
1055
1056   struct minimal_symbol *msymbol;
1057
1058   msymbol = lookup_minimal_symbol ("main", symfile_objfile);
1059   if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1060     {
1061       breakpoint_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1062     }
1063   else
1064     {
1065       return (0);
1066     }
1067
1068   if (target_insert_breakpoint (breakpoint_addr, shadow_contents) != 0)
1069     {
1070       return (0);
1071     }
1072
1073 #else   /* !BKPT_AT_MAIN */
1074
1075   struct symtab_and_line sal;
1076
1077   /* Read the debugger interface structure directly. */
1078
1079   read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
1080
1081   /* Set breakpoint at the debugger interface stub routine that will
1082      be called just prior to each mapping change and again after the
1083      mapping change is complete.  Set up the (nonexistent) handler to
1084      deal with hitting these breakpoints.  (FIXME). */
1085
1086   warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
1087
1088 #endif  /* BKPT_AT_MAIN */
1089
1090 #endif  /* !SVR4_SHARED_LIBS */
1091
1092   return (1);
1093 }
1094   
1095 /*
1096   
1097 GLOBAL FUNCTION
1098   
1099         solib_create_inferior_hook -- shared library startup support
1100   
1101 SYNOPSIS
1102   
1103         void solib_create_inferior_hook()
1104   
1105 DESCRIPTION
1106   
1107         When gdb starts up the inferior, it nurses it along (through the
1108         shell) until it is ready to execute it's first instruction.  At this
1109         point, this function gets called via expansion of the macro
1110         SOLIB_CREATE_INFERIOR_HOOK.
1111
1112         For both SunOS shared libraries, and SVR4 shared libraries, we
1113         can arrange to cooperate with the dynamic linker to discover the
1114         names of shared libraries that are dynamically linked, and the
1115         base addresses to which they are linked.
1116
1117         This function is responsible for discovering those names and
1118         addresses, and saving sufficient information about them to allow
1119         their symbols to be read at a later time.
1120
1121 FIXME
1122
1123         Between enable_break() and disable_break(), this code does not
1124         properly handle hitting breakpoints which the user might have
1125         set in the startup code or in the dynamic linker itself.  Proper
1126         handling will probably have to wait until the implementation is
1127         changed to use the "breakpoint handler function" method.
1128
1129         Also, what if child has exit()ed?  Must exit loop somehow.
1130   */
1131
1132 void 
1133 solib_create_inferior_hook()
1134 {
1135   
1136   if ((debug_base = locate_base ()) == 0)
1137     {
1138       /* Can't find the symbol or the executable is statically linked. */
1139       return;
1140     }
1141
1142   if (!enable_break ())
1143     {
1144       warning ("shared library handler failed to enable breakpoint");
1145       return;
1146     }
1147
1148   /* Now run the target.  It will eventually hit the breakpoint, at
1149      which point all of the libraries will have been mapped in and we
1150      can go groveling around in the dynamic linker structures to find
1151      out what we need to know about them. */
1152
1153   clear_proceed_status ();
1154   stop_soon_quietly = 1;
1155   stop_signal = 0;
1156   do
1157     {
1158       target_resume (0, stop_signal);
1159       wait_for_inferior ();
1160     }
1161   while (stop_signal != SIGTRAP);
1162   stop_soon_quietly = 0;
1163   
1164   /* We are now either at the "mapping complete" breakpoint (or somewhere
1165      else, a condition we aren't prepared to deal with anyway), so adjust
1166      the PC as necessary after a breakpoint, disable the breakpoint, and
1167      add any shared libraries that were mapped in. */
1168
1169   if (DECR_PC_AFTER_BREAK)
1170     {
1171       stop_pc -= DECR_PC_AFTER_BREAK;
1172       write_register (PC_REGNUM, stop_pc);
1173     }
1174
1175   if (!disable_break ())
1176     {
1177       warning ("shared library handler failed to disable breakpoint");
1178     }
1179
1180   solib_add ((char *) 0, 0, (struct target_ops *) 0);
1181 }
1182
1183 /*
1184
1185 LOCAL FUNCTION
1186
1187         special_symbol_handling -- additional shared library symbol handling
1188
1189 SYNOPSIS
1190
1191         void special_symbol_handling (struct so_list *so)
1192
1193 DESCRIPTION
1194
1195         Once the symbols from a shared object have been loaded in the usual
1196         way, we are called to do any system specific symbol handling that 
1197         is needed.
1198
1199         For Suns, this consists of grunging around in the dynamic linkers
1200         structures to find symbol definitions for "common" symbols and 
1201         adding them to the minimal symbol table for the corresponding
1202         objfile.
1203
1204 */
1205
1206 static void
1207 special_symbol_handling (so)
1208 struct so_list *so;
1209 {
1210 #ifndef SVR4_SHARED_LIBS
1211   int j;
1212
1213   if (debug_addr == 0)
1214     {
1215       /* Get link_dynamic structure */
1216
1217       j = target_read_memory (debug_base, (char *) &dynamic_copy,
1218                               sizeof (dynamic_copy));
1219       if (j)
1220         {
1221           /* unreadable */
1222           return;
1223         }
1224
1225       /* Calc address of debugger interface structure */
1226       /* FIXME, this needs work for cross-debugging of core files
1227          (byteorder, size, alignment, etc).  */
1228
1229       debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1230     }
1231
1232   /* Read the debugger structure from the inferior, just to make sure
1233      we have a current copy. */
1234
1235   j = target_read_memory (debug_addr, (char *) &debug_copy,
1236                           sizeof (debug_copy));
1237   if (j)
1238     return;             /* unreadable */
1239
1240   /* Get common symbol definitions for the loaded object. */
1241
1242   if (debug_copy.ldd_cp)
1243     {
1244       solib_add_common_symbols (debug_copy.ldd_cp, so -> objfile);
1245     }
1246
1247 #endif  /* !SVR4_SHARED_LIBS */
1248 }
1249
1250
1251 /*
1252
1253 LOCAL FUNCTION
1254
1255         sharedlibrary_command -- handle command to explicitly add library
1256
1257 SYNOPSIS
1258
1259         static void sharedlibrary_command (char *args, int from_tty)
1260
1261 DESCRIPTION
1262
1263 */
1264
1265 static void
1266 sharedlibrary_command (args, from_tty)
1267 char *args;
1268 int from_tty;
1269 {
1270   dont_repeat ();
1271   solib_add (args, from_tty, (struct target_ops *) 0);
1272 }
1273
1274 void
1275 _initialize_solib()
1276 {
1277   
1278   add_com ("sharedlibrary", class_files, sharedlibrary_command,
1279            "Load shared object library symbols for files matching REGEXP.");
1280   add_info ("sharedlibrary", info_sharedlibrary_command, 
1281             "Status of loaded shared object libraries.");
1282 }
This page took 0.094162 seconds and 4 git commands to generate.