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