]> Git Repo - binutils.git/blob - gdb/osfsolib.c
Modified Files:
[binutils.git] / gdb / osfsolib.c
1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2    Copyright 1993 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 /* 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 <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 "regex.h"
39 #include "inferior.h"
40 #include "language.h"
41
42 #define MAX_PATH_SIZE 256               /* FIXME: Should be dynamic */
43
44 /* FIXME: This is a terrible hack for shared library support under OSF/1.
45    The main problem is that the needed definitions are not contained in
46    the system header files.
47    The ldr_* routines described in loader(3) would be the way to go here.
48    But they do not work for arbitrary target processes (as documented).  */
49
50 #ifndef USE_LDR_ROUTINES
51 #define RLD_CONTEXT_ADDRESS     0x3ffc0000000
52
53 typedef struct
54 {
55         CORE_ADDR next;
56         CORE_ADDR previous;
57         CORE_ADDR unknown;
58         char *module_name;
59         CORE_ADDR modinfo_addr;
60 } ldr_module_info_t;
61
62 typedef struct
63 {
64         CORE_ADDR unknown1;
65         CORE_ADDR unknown2;
66         CORE_ADDR head;
67         CORE_ADDR tail;
68 } ldr_context_t;
69
70 static ldr_context_t ldr_context;
71 #else
72 #include <loader.h>
73 #endif
74
75 /* Define our own link_map structure.
76    This will help to share code with solib.c.  */
77
78 struct link_map {
79   CORE_ADDR l_addr;                     /* address at which object mapped */
80   char *l_name;                         /* full name of loaded object */
81   ldr_module_info_t module_info;        /* corresponding module info */
82 };
83
84 #define LM_ADDR(so) ((so) -> lm.l_addr)
85 #define LM_NAME(so) ((so) -> lm.l_name)
86
87 struct so_list {
88   struct so_list *next;                 /* next structure in linked list */
89   struct link_map lm;                   /* copy of link map from inferior */
90   struct link_map *lmaddr;              /* addr in inferior lm was read from */
91   CORE_ADDR lmend;                      /* upper addr bound of mapped object */
92   char so_name[MAX_PATH_SIZE];          /* shared object lib name (FIXME) */
93   char symbols_loaded;                  /* flag: symbols read in yet? */
94   char from_tty;                        /* flag: print msgs? */
95   struct objfile *objfile;              /* objfile for loaded lib */
96   struct section_table *sections;
97   struct section_table *sections_end;
98   struct section_table *textsection;
99   bfd *abfd;
100 };
101
102 static struct so_list *so_list_head;    /* List of known shared objects */
103
104 extern int
105 fdmatch PARAMS ((int, int));            /* In libiberty */
106
107 /* Local function prototypes */
108
109 static void
110 sharedlibrary_command PARAMS ((char *, int));
111
112 static void
113 info_sharedlibrary_command PARAMS ((char *, int));
114
115 static int
116 symbol_add_stub PARAMS ((char *));
117
118 static struct so_list *
119 find_solib PARAMS ((struct so_list *));
120
121 static struct link_map *
122 first_link_map_member PARAMS ((void));
123
124 static struct link_map *
125 next_link_map_member PARAMS ((struct so_list *));
126
127 static void
128 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
129
130 static void
131 solib_map_sections PARAMS ((struct so_list *));
132
133 /*
134
135 LOCAL FUNCTION
136
137         solib_map_sections -- open bfd and build sections for shared lib
138
139 SYNOPSIS
140
141         static void solib_map_sections (struct so_list *so)
142
143 DESCRIPTION
144
145         Given a pointer to one of the shared objects in our list
146         of mapped objects, use the recorded name to open a bfd
147         descriptor for the object, build a section table, and then
148         relocate all the section addresses by the base address at
149         which the shared object was mapped.
150
151 FIXMES
152
153         In most (all?) cases the shared object file name recorded in the
154         dynamic linkage tables will be a fully qualified pathname.  For
155         cases where it isn't, do we really mimic the systems search
156         mechanism correctly in the below code (particularly the tilde
157         expansion stuff?).
158  */
159
160 static void
161 solib_map_sections (so)
162      struct so_list *so;
163 {
164   char *filename;
165   char *scratch_pathname;
166   int scratch_chan;
167   struct section_table *p;
168   struct cleanup *old_chain;
169   bfd *abfd;
170   
171   filename = tilde_expand (so -> so_name);
172   old_chain = make_cleanup (free, filename);
173   
174   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
175                         &scratch_pathname);
176   if (scratch_chan < 0)
177     {
178       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
179                             O_RDONLY, 0, &scratch_pathname);
180     }
181   if (scratch_chan < 0)
182     {
183       perror_with_name (filename);
184     }
185   /* Leave scratch_pathname allocated.  bfd->name will point to it.  */
186
187   abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
188   if (!abfd)
189     {
190       close (scratch_chan);
191       error ("Could not open `%s' as an executable file: %s",
192              scratch_pathname, bfd_errmsg (bfd_get_error ()));
193     }
194   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
195   so -> abfd = abfd;
196   abfd -> cacheable = true;
197
198   if (!bfd_check_format (abfd, bfd_object))
199     {
200       error ("\"%s\": not in executable format: %s.",
201              scratch_pathname, bfd_errmsg (bfd_get_error ()));
202     }
203   if (build_section_table (abfd, &so -> sections, &so -> sections_end))
204     {
205       error ("Can't find the file sections in `%s': %s", 
206              bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
207     }
208
209   for (p = so -> sections; p < so -> sections_end; p++)
210     {
211       /* Relocate the section binding addresses as recorded in the shared
212          object's file by the base address to which the object was actually
213          mapped. */
214       p -> addr += (CORE_ADDR) LM_ADDR (so);
215       p -> endaddr += (CORE_ADDR) LM_ADDR (so);
216       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
217       if (STREQ (p -> sec_ptr -> name, ".text"))
218         {
219           so -> textsection = p;
220         }
221     }
222
223   /* Free the file names, close the file now.  */
224   do_cleanups (old_chain);
225 }
226
227 /*
228
229 LOCAL FUNCTION
230
231         first_link_map_member -- locate first member in dynamic linker's map
232
233 SYNOPSIS
234
235         static struct link_map *first_link_map_member (void)
236
237 DESCRIPTION
238
239         Read in a copy of the first member in the inferior's dynamic
240         link map from the inferior's dynamic linker structures, and return
241         a pointer to the copy in our address space.
242 */
243
244 static struct link_map *
245 first_link_map_member ()
246 {
247   struct link_map *lm = NULL;
248   static struct link_map first_lm;
249
250 #ifdef USE_LDR_ROUTINES
251   ldr_module_t mod_id = LDR_NULL_MODULE;
252   size_t retsize;
253
254   if (ldr_next_module(inferior_pid, &mod_id) != 0
255       || mod_id == LDR_NULL_MODULE
256       || ldr_inq_module(inferior_pid, mod_id,
257                         &first_lm.module_info, sizeof(ldr_module_info_t),
258                         &retsize) != 0)
259     return lm;
260 #else
261   CORE_ADDR ldr_context_addr;
262
263   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
264                           (char *) &ldr_context_addr,
265                           sizeof (CORE_ADDR)) != 0
266       || target_read_memory (ldr_context_addr,
267                              (char *) &ldr_context,
268                              sizeof (ldr_context_t)) != 0
269       || target_read_memory ((CORE_ADDR) ldr_context.head,
270                              (char *) &first_lm.module_info,
271                              sizeof (ldr_module_info_t)) != 0)
272     return lm;
273 #endif
274
275   lm = &first_lm;
276
277   /* The first entry is for the main program and should be skipped.  */
278   lm->l_name = NULL;
279
280   return lm;
281 }
282
283 static struct link_map *
284 next_link_map_member (so_list_ptr)
285      struct so_list *so_list_ptr;
286 {
287   struct link_map *lm = NULL;
288   static struct link_map next_lm;
289 #ifdef USE_LDR_ROUTINES
290   ldr_module_t mod_id = lm->module_info.lmi_modid;
291   size_t retsize;
292
293   if (ldr_next_module(inferior_pid, &mod_id) != 0
294       || mod_id == LDR_NULL_MODULE
295       || ldr_inq_module(inferior_pid, mod_id,
296                         &next_lm.module_info, sizeof(ldr_module_info_t),
297                         &retsize) != 0)
298     return lm;
299
300   lm = &next_lm;
301   lm->l_name = lm->module_info.lmi_name;
302 #else
303   CORE_ADDR ldr_context_addr;
304
305   /* Reread context in case ldr_context.tail was updated.  */
306
307   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
308                           (char *) &ldr_context_addr,
309                           sizeof (CORE_ADDR)) != 0
310       || target_read_memory (ldr_context_addr,
311                              (char *) &ldr_context,
312                              sizeof (ldr_context_t)) != 0
313       || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
314       || target_read_memory (so_list_ptr->lm.module_info.next,
315                              (char *) &next_lm.module_info,
316                              sizeof (ldr_module_info_t)) != 0)
317     return lm;
318
319   lm = &next_lm;
320   lm->l_name = lm->module_info.module_name;
321 #endif
322   return lm;
323 }
324
325 static void
326 xfer_link_map_member (so_list_ptr, lm)
327      struct so_list *so_list_ptr;
328      struct link_map *lm;
329 {
330   so_list_ptr->lm = *lm;
331
332   /* OSF/1 has absolute addresses in shared libraries.  */
333   LM_ADDR (so_list_ptr) = 0;
334
335   /* There is one entry that has no name (for the inferior executable)
336      since it is not a shared object. */
337   if (LM_NAME (so_list_ptr) != 0)
338     {
339
340 #ifdef USE_LDR_ROUTINES
341       int len = strlen (LM_NAME (so_list_ptr) + 1);
342
343       if (len > MAX_PATH_SIZE)
344         len = MAX_PATH_SIZE;
345       strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
346 #else
347       if (!target_read_string((CORE_ADDR) LM_NAME (so_list_ptr),
348                               so_list_ptr->so_name, MAX_PATH_SIZE - 1))
349         error ("xfer_link_map_member: Can't read pathname for load map\n");
350 #endif
351       so_list_ptr->so_name[MAX_PATH_SIZE - 1] = 0;
352
353       solib_map_sections (so_list_ptr);
354     }
355 }
356
357 /*
358
359 LOCAL FUNCTION
360
361         find_solib -- step through list of shared objects
362
363 SYNOPSIS
364
365         struct so_list *find_solib (struct so_list *so_list_ptr)
366
367 DESCRIPTION
368
369         This module contains the routine which finds the names of any
370         loaded "images" in the current process. The argument in must be
371         NULL on the first call, and then the returned value must be passed
372         in on subsequent calls. This provides the capability to "step" down
373         the list of loaded objects. On the last object, a NULL value is
374         returned.
375
376         The arg and return value are "struct link_map" pointers, as defined
377         in <link.h>.
378  */
379
380 static struct so_list *
381 find_solib (so_list_ptr)
382      struct so_list *so_list_ptr;       /* Last lm or NULL for first one */
383 {
384   struct so_list *so_list_next = NULL;
385   struct link_map *lm = NULL;
386   struct so_list *new;
387   
388   if (so_list_ptr == NULL)
389     {
390       /* We are setting up for a new scan through the loaded images. */
391       if ((so_list_next = so_list_head) == NULL)
392         {
393           /* Find the first link map list member. */
394           lm = first_link_map_member ();
395         }
396     }
397   else
398     {
399       /* We have been called before, and are in the process of walking
400          the shared library list.  Advance to the next shared object. */
401       lm = next_link_map_member (so_list_ptr);
402       so_list_next = so_list_ptr -> next;
403     }
404   if ((so_list_next == NULL) && (lm != NULL))
405     {
406       /* Get next link map structure from inferior image and build a local
407          abbreviated load_map structure */
408       new = (struct so_list *) xmalloc (sizeof (struct so_list));
409       memset ((char *) new, 0, sizeof (struct so_list));
410       new -> lmaddr = lm;
411       /* Add the new node as the next node in the list, or as the root
412          node if this is the first one. */
413       if (so_list_ptr != NULL)
414         {
415           so_list_ptr -> next = new;
416         }
417       else
418         {
419           so_list_head = new;
420         }      
421       so_list_next = new;
422       xfer_link_map_member (new, lm);
423     }
424   return (so_list_next);
425 }
426
427 /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
428
429 static int
430 symbol_add_stub (arg)
431      char *arg;
432 {
433   register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
434   
435   so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
436                                    so -> textsection -> addr,
437                                    0, 0, 0);
438   return (1);
439 }
440
441 /*
442
443 GLOBAL FUNCTION
444
445         solib_add -- add a shared library file to the symtab and section list
446
447 SYNOPSIS
448
449         void solib_add (char *arg_string, int from_tty,
450                         struct target_ops *target)
451
452 DESCRIPTION
453
454 */
455
456 void
457 solib_add (arg_string, from_tty, target)
458      char *arg_string;
459      int from_tty;
460      struct target_ops *target;
461 {       
462   register struct so_list *so = NULL;           /* link map state variable */
463
464   /* Last shared library that we read.  */
465   struct so_list *so_last = NULL;
466
467   char *re_err;
468   int count;
469   int old;
470   
471   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
472     {
473       error ("Invalid regexp: %s", re_err);
474     }
475   
476   
477   /* Add the shared library sections to the section table of the
478      specified target, if any. We have to do this before reading the
479      symbol files as symbol_file_add calls reinit_frame_cache and
480      creating a new frame might access memory in the shared library.  */
481   if (target)
482     {
483       /* Count how many new section_table entries there are.  */
484       so = NULL;
485       count = 0;
486       while ((so = find_solib (so)) != NULL)
487         {
488           if (so -> so_name[0])
489             {
490               count += so -> sections_end - so -> sections;
491             }
492         }
493       
494       if (count)
495         {
496           /* Reallocate the target's section table including the new size.  */
497           if (target -> to_sections)
498             {
499               old = target -> to_sections_end - target -> to_sections;
500               target -> to_sections = (struct section_table *)
501                 xrealloc ((char *)target -> to_sections,
502                          (sizeof (struct section_table)) * (count + old));
503             }
504           else
505             {
506               old = 0;
507               target -> to_sections = (struct section_table *)
508                 xmalloc ((sizeof (struct section_table)) * count);
509             }
510           target -> to_sections_end = target -> to_sections + (count + old);
511           
512           /* Add these section table entries to the target's table.  */
513           while ((so = find_solib (so)) != NULL)
514             {
515               if (so -> so_name[0])
516                 {
517                   count = so -> sections_end - so -> sections;
518                   memcpy ((char *) (target -> to_sections + old),
519                           so -> sections, 
520                           (sizeof (struct section_table)) * count);
521                   old += count;
522                 }
523             }
524         }
525     }
526   
527   /* Now add the symbol files.  */
528   so = NULL;
529   while ((so = find_solib (so)) != NULL)
530     {
531       if (so -> so_name[0] && re_exec (so -> so_name))
532         {
533           so -> from_tty = from_tty;
534           if (so -> symbols_loaded)
535             {
536               if (from_tty)
537                 {
538                   printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
539                 }
540             }
541           else if (catch_errors
542                    (symbol_add_stub, (char *) so,
543                     "Error while reading shared library symbols:\n",
544                     RETURN_MASK_ALL))
545             {
546               so_last = so;
547               so -> symbols_loaded = 1;
548             }
549         }
550     }
551 }
552
553 /*
554
555 LOCAL FUNCTION
556
557         info_sharedlibrary_command -- code for "info sharedlibrary"
558
559 SYNOPSIS
560
561         static void info_sharedlibrary_command ()
562
563 DESCRIPTION
564
565         Walk through the shared library list and print information
566         about each attached library.
567 */
568
569 static void
570 info_sharedlibrary_command (ignore, from_tty)
571      char *ignore;
572      int from_tty;
573 {
574   register struct so_list *so = NULL;   /* link map state variable */
575   int header_done = 0;
576   
577   if (exec_bfd == NULL)
578     {
579       printf_unfiltered ("No exec file.\n");
580       return;
581     }
582   while ((so = find_solib (so)) != NULL)
583     {
584       if (so -> so_name[0])
585         {
586           unsigned long txt_start = 0;
587           unsigned long txt_end = 0;
588
589           if (!header_done)
590             {
591               printf_unfiltered("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
592                      "Shared Object Library");
593               header_done++;
594             }
595           if (so -> textsection)
596             {
597               txt_start = (unsigned long) so -> textsection -> addr;
598               txt_end = (unsigned long) so -> textsection -> endaddr;
599             }
600           printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
601           printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
602           printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
603           printf_unfiltered ("%s\n",  so -> so_name);
604         }
605     }
606   if (so_list_head == NULL)
607     {
608       printf_unfiltered ("No shared libraries loaded at this time.\n"); 
609     }
610 }
611
612 /*
613
614 GLOBAL FUNCTION
615
616         solib_address -- check to see if an address is in a shared lib
617
618 SYNOPSIS
619
620         int solib_address (CORE_ADDR address)
621
622 DESCRIPTION
623
624         Provides a hook for other gdb routines to discover whether or
625         not a particular address is within the mapped address space of
626         a shared library.  Any address between the base mapping address
627         and the first address beyond the end of the last mapping, is
628         considered to be within the shared library address space, for
629         our purposes.
630
631         For example, this routine is called at one point to disable
632         breakpoints which are in shared libraries that are not currently
633         mapped in.
634  */
635
636 int
637 solib_address (address)
638      CORE_ADDR address;
639 {
640   register struct so_list *so = 0;      /* link map state variable */
641   
642   while ((so = find_solib (so)) != NULL)
643     {
644       if (so -> so_name[0] && so -> textsection)
645         {
646           if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
647               (address < (CORE_ADDR) so -> textsection -> endaddr))
648             {
649               return (1);
650             }
651         }
652     }
653   return (0);
654 }
655
656 /* Called by free_all_symtabs */
657
658 void 
659 clear_solib()
660 {
661   struct so_list *next;
662   char *bfd_filename;
663   
664   while (so_list_head)
665     {
666       if (so_list_head -> sections)
667         {
668           free ((PTR)so_list_head -> sections);
669         }
670       if (so_list_head -> abfd)
671         {
672           bfd_filename = bfd_get_filename (so_list_head -> abfd);
673           bfd_close (so_list_head -> abfd);
674         }
675       else
676         /* This happens for the executable on SVR4.  */
677         bfd_filename = NULL;
678       
679       next = so_list_head -> next;
680       if (bfd_filename)
681         free ((PTR)bfd_filename);
682       free ((PTR)so_list_head);
683       so_list_head = next;
684     }
685 }
686   
687 /*
688   
689 GLOBAL FUNCTION
690   
691         solib_create_inferior_hook -- shared library startup support
692   
693 SYNOPSIS
694   
695         void solib_create_inferior_hook()
696   
697 DESCRIPTION
698
699         When gdb starts up the inferior, it nurses it along (through the
700         shell) until it is ready to execute it's first instruction.  At this
701         point, this function gets called via expansion of the macro
702         SOLIB_CREATE_INFERIOR_HOOK.
703         For a statically bound executable, this first instruction is the
704         one at "_start", or a similar text label. No further processing is
705         needed in that case.
706         For a dynamically bound executable, this first instruction is somewhere
707         in the rld, and the actual user executable is not yet mapped in.
708         We continue the inferior again, rld then maps in the actual user
709         executable and any needed shared libraries and then sends
710         itself a SIGTRAP.
711         At that point we discover the names of all shared libraries and
712         read their symbols in.
713
714 FIXME
715
716         This code does not properly handle hitting breakpoints which the
717         user might have set in the rld itself.  Proper handling would have
718         to check if the SIGTRAP happened due to a kill call.
719
720         Also, what if child has exit()ed?  Must exit loop somehow.
721   */
722
723 void
724 solib_create_inferior_hook()
725 {
726
727   /* Nothing to do for statically bound executables.  */
728
729   if (symfile_objfile == 0 || symfile_objfile->ei.entry_file_lowpc == stop_pc)
730     return;
731
732   /* Now run the target.  It will eventually get a SIGTRAP, at
733      which point all of the libraries will have been mapped in and we
734      can go groveling around in the rld structures to find
735      out what we need to know about them. */
736  
737   clear_proceed_status ();
738   stop_soon_quietly = 1;
739   stop_signal = TARGET_SIGNAL_0;
740   do
741     {
742       target_resume (-1, 0, stop_signal);
743       wait_for_inferior ();
744     }
745   while (stop_signal != TARGET_SIGNAL_TRAP);
746
747   /*  solib_add will call reinit_frame_cache via symbol_file_add.
748       But we are stopped in the runtime loader and we do not have symbols
749       for the runtime loader. So heuristic_proc_start will be called
750       and will put out an annoying warning.
751       Resetting stop_soon_quietly after symbol loading suppresses
752       the warning.  */
753   solib_add ((char *) 0, 0, (struct target_ops *) 0);
754   stop_soon_quietly = 0;
755 }
756
757
758 /*
759
760 LOCAL FUNCTION
761
762         sharedlibrary_command -- handle command to explicitly add library
763
764 SYNOPSIS
765
766         static void sharedlibrary_command (char *args, int from_tty)
767
768 DESCRIPTION
769
770 */
771
772 static void
773 sharedlibrary_command (args, from_tty)
774 char *args;
775 int from_tty;
776 {
777   dont_repeat ();
778   solib_add (args, from_tty, (struct target_ops *) 0);
779 }
780
781 void
782 _initialize_solib()
783 {
784   
785   add_com ("sharedlibrary", class_files, sharedlibrary_command,
786            "Load shared object library symbols for files matching REGEXP.");
787   add_info ("sharedlibrary", info_sharedlibrary_command, 
788             "Status of loaded shared object libraries.");
789 }
This page took 0.067673 seconds and 4 git commands to generate.