]> Git Repo - binutils.git/blob - gdb/osfsolib.c
* breakpoint.c (breakpoint_re_set): #ifdef GET_LONGJMP_TARGET
[binutils.git] / gdb / osfsolib.c
1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2    Copyright 1993, 1994 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* FIXME: Most of this code could be merged with solib.c by using
21    next_link_map_member and xfer_link_map_member in solib.c.  */
22
23 #include "defs.h"
24
25 #include <sys/types.h>
26 #include <signal.h>
27 #include "gdb_string.h"
28 #include <fcntl.h>
29
30 #include "symtab.h"
31 #include "bfd.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "gdbcore.h"
35 #include "command.h"
36 #include "target.h"
37 #include "frame.h"
38 #include "gnu-regex.h"
39 #include "inferior.h"
40 #include "language.h"
41
42 #define MAX_PATH_SIZE 1024              /* FIXME: Should be dynamic */
43
44 /* When handling shared libraries, GDB has to find out the pathnames
45    of all shared libraries that are currently loaded (to read in their
46    symbols) and where the shared libraries are loaded in memory
47    (to relocate them properly from their prelinked addresses to the
48    current load address).
49
50    Under OSF/1 there are two possibilities to get at this information:
51    1) Peek around in the runtime loader structures.
52       These are not documented, and they are not defined in the system
53       header files. The definitions below were obtained by experimentation,
54       but they seem stable enough.
55    2) Use the undocumented libxproc.a library, which contains the
56       equivalent ldr_* routines.
57       This approach is somewhat cleaner, but it requires that the GDB
58       executable is dynamically linked. In addition it requires a
59       NAT_CLIBS= -lxproc -Wl,-expect_unresolved,ldr_process_context
60       linker specification for GDB and all applications that are using
61       libgdb.
62    We will use the peeking approach until it becomes unwieldy.  */
63
64 #ifndef USE_LDR_ROUTINES
65
66 /* Definition of runtime loader structures, found by experimentation.  */
67 #define RLD_CONTEXT_ADDRESS     0x3ffc0000000
68
69 typedef struct
70 {
71         CORE_ADDR next;
72         CORE_ADDR previous;
73         CORE_ADDR unknown1;
74         char *module_name;
75         CORE_ADDR modinfo_addr;
76         long module_id;
77         CORE_ADDR unknown2;
78         CORE_ADDR unknown3;
79         long region_count;
80         CORE_ADDR regioninfo_addr;
81 } ldr_module_info_t;
82
83 typedef struct
84 {
85         long unknown1;
86         CORE_ADDR regionname_addr;
87         long protection;
88         CORE_ADDR vaddr;
89         CORE_ADDR mapaddr;
90         long size;
91         long unknown2[5];
92 } ldr_region_info_t;
93
94 typedef struct
95 {
96         CORE_ADDR unknown1;
97         CORE_ADDR unknown2;
98         CORE_ADDR head;
99         CORE_ADDR tail;
100 } ldr_context_t;
101
102 static ldr_context_t ldr_context;
103
104 #else
105
106 #include <loader.h>
107 static ldr_process_t fake_ldr_process;
108
109 /* Called by ldr_* routines to read memory from the current target.  */
110
111 static int ldr_read_memory PARAMS ((CORE_ADDR, char *, int, int));
112
113 static int
114 ldr_read_memory (memaddr, myaddr, len, readstring)
115      CORE_ADDR memaddr;
116      char *myaddr;
117      int len;
118      int readstring;
119 {
120   int result;
121   char *buffer;
122
123   if (readstring)
124     {
125       target_read_string (memaddr, &buffer, len, &result);
126       if (result == 0)
127         strcpy (myaddr, buffer);
128       free (buffer);
129     }
130   else
131     result = target_read_memory (memaddr, myaddr, len);
132
133   if (result != 0)
134     result = -result;
135   return result;
136 }
137
138 #endif
139
140 /* Define our own link_map structure.
141    This will help to share code with solib.c.  */
142
143 struct link_map {
144   CORE_ADDR l_offset;                   /* prelink to load address offset */
145   char *l_name;                         /* full name of loaded object */
146   ldr_module_info_t module_info;        /* corresponding module info */
147 };
148
149 #define LM_OFFSET(so) ((so) -> lm.l_offset)
150 #define LM_NAME(so) ((so) -> lm.l_name)
151
152 struct so_list {
153   struct so_list *next;                 /* next structure in linked list */
154   struct link_map lm;                   /* copy of link map from inferior */
155   struct link_map *lmaddr;              /* addr in inferior lm was read from */
156   CORE_ADDR lmend;                      /* upper addr bound of mapped object */
157   char so_name[MAX_PATH_SIZE];          /* shared object lib name (FIXME) */
158   char symbols_loaded;                  /* flag: symbols read in yet? */
159   char from_tty;                        /* flag: print msgs? */
160   struct objfile *objfile;              /* objfile for loaded lib */
161   struct section_table *sections;
162   struct section_table *sections_end;
163   struct section_table *textsection;
164   bfd *abfd;
165 };
166
167 static struct so_list *so_list_head;    /* List of known shared objects */
168
169 extern int
170 fdmatch PARAMS ((int, int));            /* In libiberty */
171
172 /* Local function prototypes */
173
174 static void
175 sharedlibrary_command PARAMS ((char *, int));
176
177 static void
178 info_sharedlibrary_command PARAMS ((char *, int));
179
180 static int
181 symbol_add_stub PARAMS ((char *));
182
183 static struct so_list *
184 find_solib PARAMS ((struct so_list *));
185
186 static struct link_map *
187 first_link_map_member PARAMS ((void));
188
189 static struct link_map *
190 next_link_map_member PARAMS ((struct so_list *));
191
192 static void
193 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
194
195 static void
196 solib_map_sections PARAMS ((struct so_list *));
197
198 /*
199
200 LOCAL FUNCTION
201
202         solib_map_sections -- open bfd and build sections for shared lib
203
204 SYNOPSIS
205
206         static void solib_map_sections (struct so_list *so)
207
208 DESCRIPTION
209
210         Given a pointer to one of the shared objects in our list
211         of mapped objects, use the recorded name to open a bfd
212         descriptor for the object, build a section table, and then
213         relocate all the section addresses by the base address at
214         which the shared object was mapped.
215
216 FIXMES
217
218         In most (all?) cases the shared object file name recorded in the
219         dynamic linkage tables will be a fully qualified pathname.  For
220         cases where it isn't, do we really mimic the systems search
221         mechanism correctly in the below code (particularly the tilde
222         expansion stuff?).
223  */
224
225 static void
226 solib_map_sections (so)
227      struct so_list *so;
228 {
229   char *filename;
230   char *scratch_pathname;
231   int scratch_chan;
232   struct section_table *p;
233   struct cleanup *old_chain;
234   bfd *abfd;
235   
236   filename = tilde_expand (so -> so_name);
237   old_chain = make_cleanup (free, filename);
238   
239   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
240                         &scratch_pathname);
241   if (scratch_chan < 0)
242     {
243       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
244                             O_RDONLY, 0, &scratch_pathname);
245     }
246   if (scratch_chan < 0)
247     {
248       perror_with_name (filename);
249     }
250   /* Leave scratch_pathname allocated.  bfd->name will point to it.  */
251
252   abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
253   if (!abfd)
254     {
255       close (scratch_chan);
256       error ("Could not open `%s' as an executable file: %s",
257              scratch_pathname, bfd_errmsg (bfd_get_error ()));
258     }
259   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
260   so -> abfd = abfd;
261   abfd -> cacheable = true;
262
263   if (!bfd_check_format (abfd, bfd_object))
264     {
265       error ("\"%s\": not in executable format: %s.",
266              scratch_pathname, bfd_errmsg (bfd_get_error ()));
267     }
268   if (build_section_table (abfd, &so -> sections, &so -> sections_end))
269     {
270       error ("Can't find the file sections in `%s': %s", 
271              bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
272     }
273
274   for (p = so -> sections; p < so -> sections_end; p++)
275     {
276       /* Relocate the section binding addresses as recorded in the shared
277          object's file by the offset to get the address to which the
278          object was actually mapped.  */
279       p -> addr += LM_OFFSET (so);
280       p -> endaddr += LM_OFFSET (so);
281       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
282       if (STREQ (p -> the_bfd_section -> name, ".text"))
283         {
284           so -> textsection = p;
285         }
286     }
287
288   /* Free the file names, close the file now.  */
289   do_cleanups (old_chain);
290 }
291
292 /*
293
294 LOCAL FUNCTION
295
296         first_link_map_member -- locate first member in dynamic linker's map
297
298 SYNOPSIS
299
300         static struct link_map *first_link_map_member (void)
301
302 DESCRIPTION
303
304         Read in a copy of the first member in the inferior's dynamic
305         link map from the inferior's dynamic linker structures, and return
306         a pointer to the copy in our address space.
307 */
308
309 static struct link_map *
310 first_link_map_member ()
311 {
312   struct link_map *lm = NULL;
313   static struct link_map first_lm;
314
315 #ifdef USE_LDR_ROUTINES
316   ldr_module_t mod_id = LDR_NULL_MODULE;
317   size_t retsize;
318
319   fake_ldr_process = ldr_core_process ();
320   ldr_set_core_reader (ldr_read_memory);
321   ldr_xdetach (fake_ldr_process);
322   if (ldr_xattach (fake_ldr_process) != 0
323       || ldr_next_module(fake_ldr_process, &mod_id) != 0
324       || mod_id == LDR_NULL_MODULE
325       || ldr_inq_module(fake_ldr_process, mod_id,
326                         &first_lm.module_info, sizeof(ldr_module_info_t),
327                         &retsize) != 0)
328     return lm;
329 #else
330   CORE_ADDR ldr_context_addr;
331
332   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
333                           (char *) &ldr_context_addr,
334                           sizeof (CORE_ADDR)) != 0
335       || target_read_memory (ldr_context_addr,
336                              (char *) &ldr_context,
337                              sizeof (ldr_context_t)) != 0
338       || target_read_memory ((CORE_ADDR) ldr_context.head,
339                              (char *) &first_lm.module_info,
340                              sizeof (ldr_module_info_t)) != 0)
341     return lm;
342 #endif
343
344   lm = &first_lm;
345
346   /* The first entry is for the main program and should be skipped.  */
347   lm->l_name = NULL;
348
349   return lm;
350 }
351
352 static struct link_map *
353 next_link_map_member (so_list_ptr)
354      struct so_list *so_list_ptr;
355 {
356   struct link_map *lm = NULL;
357   static struct link_map next_lm;
358 #ifdef USE_LDR_ROUTINES
359   ldr_module_t mod_id = so_list_ptr->lm.module_info.lmi_modid;
360   size_t retsize;
361
362   if (ldr_next_module(fake_ldr_process, &mod_id) != 0
363       || mod_id == LDR_NULL_MODULE
364       || ldr_inq_module(fake_ldr_process, mod_id,
365                         &next_lm.module_info, sizeof(ldr_module_info_t),
366                         &retsize) != 0)
367     return lm;
368
369   lm = &next_lm;
370   lm->l_name = lm->module_info.lmi_name;
371 #else
372   CORE_ADDR ldr_context_addr;
373
374   /* Reread context in case ldr_context.tail was updated.  */
375
376   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
377                           (char *) &ldr_context_addr,
378                           sizeof (CORE_ADDR)) != 0
379       || target_read_memory (ldr_context_addr,
380                              (char *) &ldr_context,
381                              sizeof (ldr_context_t)) != 0
382       || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
383       || target_read_memory (so_list_ptr->lm.module_info.next,
384                              (char *) &next_lm.module_info,
385                              sizeof (ldr_module_info_t)) != 0)
386     return lm;
387
388   lm = &next_lm;
389   lm->l_name = lm->module_info.module_name;
390 #endif
391   return lm;
392 }
393
394 static void
395 xfer_link_map_member (so_list_ptr, lm)
396      struct so_list *so_list_ptr;
397      struct link_map *lm;
398 {
399   int i;
400   so_list_ptr->lm = *lm;
401
402   /* OSF/1 shared libraries are pre-linked to particular addresses,
403      but the runtime loader may have to relocate them if the
404      address ranges of the libraries used by the target executable clash,
405      or if the target executable is linked with the -taso option.
406      The offset is the difference between the address where the shared
407      library is mapped and the pre-linked address of the shared library.
408
409      FIXME:  GDB is currently unable to relocate the shared library
410      sections by different offsets. If sections are relocated by
411      different offsets, put out a warning and use the offset of the
412      first section for all remaining sections.  */
413   LM_OFFSET (so_list_ptr) = 0;
414
415   /* There is one entry that has no name (for the inferior executable)
416      since it is not a shared object. */
417   if (LM_NAME (so_list_ptr) != 0)
418     {
419
420 #ifdef USE_LDR_ROUTINES
421       int len = strlen (LM_NAME (so_list_ptr) + 1);
422
423       if (len > MAX_PATH_SIZE)
424         len = MAX_PATH_SIZE;
425       strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
426       so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
427
428       for (i = 0; i < lm->module_info.lmi_nregion; i++)
429         {
430           ldr_region_info_t region_info;
431           size_t retsize;
432           CORE_ADDR region_offset;
433
434           if (ldr_inq_region (fake_ldr_process, lm->module_info.lmi_modid,
435                               i, &region_info, sizeof (region_info),
436                               &retsize) != 0)
437             break;
438           region_offset = (CORE_ADDR) region_info.lri_mapaddr
439                           - (CORE_ADDR) region_info.lri_vaddr;
440           if (i == 0)
441             LM_OFFSET (so_list_ptr) = region_offset;
442           else if (LM_OFFSET (so_list_ptr) != region_offset)
443             warning ("cannot handle shared library relocation for %s (%s)",
444                      so_list_ptr->so_name, region_info.lri_name);
445         }
446 #else
447       int errcode;
448       char *buffer;
449       target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
450                           MAX_PATH_SIZE - 1, &errcode);
451       if (errcode != 0)
452         error ("xfer_link_map_member: Can't read pathname for load map: %s\n",
453                safe_strerror (errcode));
454       strncpy (so_list_ptr->so_name, buffer, MAX_PATH_SIZE - 1);
455       free (buffer);
456       so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
457
458       for (i = 0; i < lm->module_info.region_count; i++)
459         {
460           ldr_region_info_t region_info;
461           CORE_ADDR region_offset;
462
463           if (target_read_memory (lm->module_info.regioninfo_addr
464                                    + i * sizeof (region_info),
465                                   (char *) &region_info,
466                                   sizeof (region_info)) != 0)
467             break;
468           region_offset = region_info.mapaddr - region_info.vaddr;
469           if (i == 0)
470             LM_OFFSET (so_list_ptr) = region_offset;
471           else if (LM_OFFSET (so_list_ptr) != region_offset)
472             {
473               char *region_name;
474               target_read_string (region_info.regionname_addr, &buffer,
475                                   MAX_PATH_SIZE - 1, &errcode);
476               if (errcode == 0)
477                 region_name = buffer;
478               else
479                 region_name = "??";
480               warning ("cannot handle shared library relocation for %s (%s)",
481                         so_list_ptr->so_name, region_name);
482               free (buffer);
483             }
484         }
485 #endif
486
487       solib_map_sections (so_list_ptr);
488     }
489 }
490
491 /*
492
493 LOCAL FUNCTION
494
495         find_solib -- step through list of shared objects
496
497 SYNOPSIS
498
499         struct so_list *find_solib (struct so_list *so_list_ptr)
500
501 DESCRIPTION
502
503         This module contains the routine which finds the names of any
504         loaded "images" in the current process. The argument in must be
505         NULL on the first call, and then the returned value must be passed
506         in on subsequent calls. This provides the capability to "step" down
507         the list of loaded objects. On the last object, a NULL value is
508         returned.
509
510         The arg and return value are "struct link_map" pointers, as defined
511         in <link.h>.
512  */
513
514 static struct so_list *
515 find_solib (so_list_ptr)
516      struct so_list *so_list_ptr;       /* Last lm or NULL for first one */
517 {
518   struct so_list *so_list_next = NULL;
519   struct link_map *lm = NULL;
520   struct so_list *new;
521   
522   if (so_list_ptr == NULL)
523     {
524       /* We are setting up for a new scan through the loaded images. */
525       if ((so_list_next = so_list_head) == NULL)
526         {
527           /* Find the first link map list member. */
528           lm = first_link_map_member ();
529         }
530     }
531   else
532     {
533       /* We have been called before, and are in the process of walking
534          the shared library list.  Advance to the next shared object. */
535       lm = next_link_map_member (so_list_ptr);
536       so_list_next = so_list_ptr -> next;
537     }
538   if ((so_list_next == NULL) && (lm != NULL))
539     {
540       /* Get next link map structure from inferior image and build a local
541          abbreviated load_map structure */
542       new = (struct so_list *) xmalloc (sizeof (struct so_list));
543       memset ((char *) new, 0, sizeof (struct so_list));
544       new -> lmaddr = lm;
545       /* Add the new node as the next node in the list, or as the root
546          node if this is the first one. */
547       if (so_list_ptr != NULL)
548         {
549           so_list_ptr -> next = new;
550         }
551       else
552         {
553           so_list_head = new;
554         }      
555       so_list_next = new;
556       xfer_link_map_member (new, lm);
557     }
558   return (so_list_next);
559 }
560
561 /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
562
563 static int
564 symbol_add_stub (arg)
565      char *arg;
566 {
567   register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
568   
569   so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
570                                    so -> textsection -> addr,
571                                    0, 0, 0);
572   return (1);
573 }
574
575 /*
576
577 GLOBAL FUNCTION
578
579         solib_add -- add a shared library file to the symtab and section list
580
581 SYNOPSIS
582
583         void solib_add (char *arg_string, int from_tty,
584                         struct target_ops *target)
585
586 DESCRIPTION
587
588 */
589
590 void
591 solib_add (arg_string, from_tty, target)
592      char *arg_string;
593      int from_tty;
594      struct target_ops *target;
595 {       
596   register struct so_list *so = NULL;           /* link map state variable */
597
598   /* Last shared library that we read.  */
599   struct so_list *so_last = NULL;
600
601   char *re_err;
602   int count;
603   int old;
604   
605   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
606     {
607       error ("Invalid regexp: %s", re_err);
608     }
609   
610   
611   /* Add the shared library sections to the section table of the
612      specified target, if any.  */
613   if (target)
614     {
615       /* Count how many new section_table entries there are.  */
616       so = NULL;
617       count = 0;
618       while ((so = find_solib (so)) != NULL)
619         {
620           if (so -> so_name[0])
621             {
622               count += so -> sections_end - so -> sections;
623             }
624         }
625       
626       if (count)
627         {
628           int update_coreops;
629
630           /* We must update the to_sections field in the core_ops structure
631              here, otherwise we dereference a potential dangling pointer
632              for each call to target_read/write_memory within this routine.  */
633           update_coreops = core_ops.to_sections == target->to_sections;
634              
635           /* Reallocate the target's section table including the new size.  */
636           if (target -> to_sections)
637             {
638               old = target -> to_sections_end - target -> to_sections;
639               target -> to_sections = (struct section_table *)
640                 xrealloc ((char *)target -> to_sections,
641                          (sizeof (struct section_table)) * (count + old));
642             }
643           else
644             {
645               old = 0;
646               target -> to_sections = (struct section_table *)
647                 xmalloc ((sizeof (struct section_table)) * count);
648             }
649           target -> to_sections_end = target -> to_sections + (count + old);
650           
651           /* Update the to_sections field in the core_ops structure
652              if needed.  */
653           if (update_coreops)
654             {
655               core_ops.to_sections = target->to_sections;
656               core_ops.to_sections_end = target->to_sections_end;
657             }
658
659           /* Add these section table entries to the target's table.  */
660           while ((so = find_solib (so)) != NULL)
661             {
662               if (so -> so_name[0])
663                 {
664                   count = so -> sections_end - so -> sections;
665                   memcpy ((char *) (target -> to_sections + old),
666                           so -> sections, 
667                           (sizeof (struct section_table)) * count);
668                   old += count;
669                 }
670             }
671         }
672     }
673   
674   /* Now add the symbol files.  */
675   so = NULL;
676   while ((so = find_solib (so)) != NULL)
677     {
678       if (so -> so_name[0] && re_exec (so -> so_name))
679         {
680           so -> from_tty = from_tty;
681           if (so -> symbols_loaded)
682             {
683               if (from_tty)
684                 {
685                   printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
686                 }
687             }
688           else if (catch_errors
689                    (symbol_add_stub, (char *) so,
690                     "Error while reading shared library symbols:\n",
691                     RETURN_MASK_ALL))
692             {
693               so_last = so;
694               so -> symbols_loaded = 1;
695             }
696         }
697     }
698
699   /* Getting new symbols may change our opinion about what is
700      frameless.  */
701   if (so_last)
702     reinit_frame_cache ();
703 }
704
705 /*
706
707 LOCAL FUNCTION
708
709         info_sharedlibrary_command -- code for "info sharedlibrary"
710
711 SYNOPSIS
712
713         static void info_sharedlibrary_command ()
714
715 DESCRIPTION
716
717         Walk through the shared library list and print information
718         about each attached library.
719 */
720
721 static void
722 info_sharedlibrary_command (ignore, from_tty)
723      char *ignore;
724      int from_tty;
725 {
726   register struct so_list *so = NULL;   /* link map state variable */
727   int header_done = 0;
728   
729   if (exec_bfd == NULL)
730     {
731       printf_unfiltered ("No exec file.\n");
732       return;
733     }
734   while ((so = find_solib (so)) != NULL)
735     {
736       if (so -> so_name[0])
737         {
738           unsigned long txt_start = 0;
739           unsigned long txt_end = 0;
740
741           if (!header_done)
742             {
743               printf_unfiltered("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
744                      "Shared Object Library");
745               header_done++;
746             }
747           if (so -> textsection)
748             {
749               txt_start = (unsigned long) so -> textsection -> addr;
750               txt_end = (unsigned long) so -> textsection -> endaddr;
751             }
752           printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
753           printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
754           printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
755           printf_unfiltered ("%s\n",  so -> so_name);
756         }
757     }
758   if (so_list_head == NULL)
759     {
760       printf_unfiltered ("No shared libraries loaded at this time.\n"); 
761     }
762 }
763
764 /*
765
766 GLOBAL FUNCTION
767
768         solib_address -- check to see if an address is in a shared lib
769
770 SYNOPSIS
771
772         int solib_address (CORE_ADDR address)
773
774 DESCRIPTION
775
776         Provides a hook for other gdb routines to discover whether or
777         not a particular address is within the mapped address space of
778         a shared library.  Any address between the base mapping address
779         and the first address beyond the end of the last mapping, is
780         considered to be within the shared library address space, for
781         our purposes.
782
783         For example, this routine is called at one point to disable
784         breakpoints which are in shared libraries that are not currently
785         mapped in.
786  */
787
788 int
789 solib_address (address)
790      CORE_ADDR address;
791 {
792   register struct so_list *so = 0;      /* link map state variable */
793   
794   while ((so = find_solib (so)) != NULL)
795     {
796       if (so -> so_name[0] && so -> textsection)
797         {
798           if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
799               (address < (CORE_ADDR) so -> textsection -> endaddr))
800             {
801               return (1);
802             }
803         }
804     }
805   return (0);
806 }
807
808 /* Called by free_all_symtabs */
809
810 void 
811 clear_solib()
812 {
813   struct so_list *next;
814   char *bfd_filename;
815   
816   while (so_list_head)
817     {
818       if (so_list_head -> sections)
819         {
820           free ((PTR)so_list_head -> sections);
821         }
822       if (so_list_head -> abfd)
823         {
824           bfd_filename = bfd_get_filename (so_list_head -> abfd);
825           if (!bfd_close (so_list_head -> abfd))
826             warning ("cannot close \"%s\": %s",
827                      bfd_filename, bfd_errmsg (bfd_get_error ()));
828         }
829       else
830         /* This happens for the executable on SVR4.  */
831         bfd_filename = NULL;
832       
833       next = so_list_head -> next;
834       if (bfd_filename)
835         free ((PTR)bfd_filename);
836       free ((PTR)so_list_head);
837       so_list_head = next;
838     }
839 }
840   
841 /*
842   
843 GLOBAL FUNCTION
844   
845         solib_create_inferior_hook -- shared library startup support
846   
847 SYNOPSIS
848   
849         void solib_create_inferior_hook()
850   
851 DESCRIPTION
852
853         When gdb starts up the inferior, it nurses it along (through the
854         shell) until it is ready to execute it's first instruction.  At this
855         point, this function gets called via expansion of the macro
856         SOLIB_CREATE_INFERIOR_HOOK.
857         For a statically bound executable, this first instruction is the
858         one at "_start", or a similar text label. No further processing is
859         needed in that case.
860         For a dynamically bound executable, this first instruction is somewhere
861         in the rld, and the actual user executable is not yet mapped in.
862         We continue the inferior again, rld then maps in the actual user
863         executable and any needed shared libraries and then sends
864         itself a SIGTRAP.
865         At that point we discover the names of all shared libraries and
866         read their symbols in.
867
868 FIXME
869
870         This code does not properly handle hitting breakpoints which the
871         user might have set in the rld itself.  Proper handling would have
872         to check if the SIGTRAP happened due to a kill call.
873
874         Also, what if child has exit()ed?  Must exit loop somehow.
875   */
876
877 void
878 solib_create_inferior_hook()
879 {
880
881   /* Nothing to do for statically bound executables.  */
882
883   if (symfile_objfile == NULL
884       || symfile_objfile->obfd == NULL
885       || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
886     return;
887
888   /* Now run the target.  It will eventually get a SIGTRAP, at
889      which point all of the libraries will have been mapped in and we
890      can go groveling around in the rld structures to find
891      out what we need to know about them. */
892  
893   clear_proceed_status ();
894   stop_soon_quietly = 1;
895   stop_signal = TARGET_SIGNAL_0;
896   do
897     {
898       target_resume (-1, 0, stop_signal);
899       wait_for_inferior ();
900     }
901   while (stop_signal != TARGET_SIGNAL_TRAP);
902
903   /*  solib_add will call reinit_frame_cache.
904       But we are stopped in the runtime loader and we do not have symbols
905       for the runtime loader. So heuristic_proc_start will be called
906       and will put out an annoying warning.
907       Delaying the resetting of stop_soon_quietly until after symbol loading
908       suppresses the warning.  */
909   solib_add ((char *) 0, 0, (struct target_ops *) 0);
910   stop_soon_quietly = 0;
911 }
912
913
914 /*
915
916 LOCAL FUNCTION
917
918         sharedlibrary_command -- handle command to explicitly add library
919
920 SYNOPSIS
921
922         static void sharedlibrary_command (char *args, int from_tty)
923
924 DESCRIPTION
925
926 */
927
928 static void
929 sharedlibrary_command (args, from_tty)
930 char *args;
931 int from_tty;
932 {
933   dont_repeat ();
934   solib_add (args, from_tty, (struct target_ops *) 0);
935 }
936
937 void
938 _initialize_solib()
939 {
940   
941   add_com ("sharedlibrary", class_files, sharedlibrary_command,
942            "Load shared object library symbols for files matching REGEXP.");
943   add_info ("sharedlibrary", info_sharedlibrary_command, 
944             "Status of loaded shared object libraries.");
945 }
This page took 0.080389 seconds and 4 git commands to generate.