* Sanitization fixes to retain new files.
[binutils-gdb.git] / gdb / osfsolib.c
blob21b423b956529119048e5eb742d289fffb38b52a
1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993, 1994, 1995, 1996, 1998 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
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. */
23 #include "defs.h"
25 #include <sys/types.h>
26 #include <signal.h>
27 #include "gdb_string.h"
28 #include <fcntl.h>
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"
43 #define MAX_PATH_SIZE 1024 /* FIXME: Should be dynamic */
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).
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. */
65 #ifndef USE_LDR_ROUTINES
67 /* Definition of runtime loader structures, found by experimentation. */
68 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
70 typedef struct
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;
84 typedef struct
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;
95 typedef struct
97 CORE_ADDR unknown1;
98 CORE_ADDR unknown2;
99 CORE_ADDR head;
100 CORE_ADDR tail;
101 } ldr_context_t;
103 static ldr_context_t ldr_context;
105 #else
107 #include <loader.h>
108 static ldr_process_t fake_ldr_process;
110 /* Called by ldr_* routines to read memory from the current target. */
112 static int ldr_read_memory PARAMS ((CORE_ADDR, char *, int, int));
114 static int
115 ldr_read_memory (memaddr, myaddr, len, readstring)
116 CORE_ADDR memaddr;
117 char *myaddr;
118 int len;
119 int readstring;
121 int result;
122 char *buffer;
124 if (readstring)
126 target_read_string (memaddr, &buffer, len, &result);
127 if (result == 0)
128 strcpy (myaddr, buffer);
129 free (buffer);
131 else
132 result = target_read_memory (memaddr, myaddr, len);
134 if (result != 0)
135 result = -result;
136 return result;
139 #endif
141 /* Define our own link_map structure.
142 This will help to share code with solib.c. */
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 */
150 #define LM_OFFSET(so) ((so) -> lm.l_offset)
151 #define LM_NAME(so) ((so) -> lm.l_name)
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;
168 static struct so_list *so_list_head; /* List of known shared objects */
170 extern int
171 fdmatch PARAMS ((int, int)); /* In libiberty */
173 /* Local function prototypes */
175 static void
176 sharedlibrary_command PARAMS ((char *, int));
178 static void
179 info_sharedlibrary_command PARAMS ((char *, int));
181 static int
182 symbol_add_stub PARAMS ((char *));
184 static struct so_list *
185 find_solib PARAMS ((struct so_list *));
187 static struct link_map *
188 first_link_map_member PARAMS ((void));
190 static struct link_map *
191 next_link_map_member PARAMS ((struct so_list *));
193 static void
194 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
196 static int
197 solib_map_sections PARAMS ((char *));
201 LOCAL FUNCTION
203 solib_map_sections -- open bfd and build sections for shared lib
205 SYNOPSIS
207 static int solib_map_sections (struct so_list *so)
209 DESCRIPTION
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.
217 FIXMES
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?).
226 static int
227 solib_map_sections (arg)
228 char *arg;
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;
238 filename = tilde_expand (so -> so_name);
239 old_chain = make_cleanup (free, filename);
241 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
242 &scratch_pathname);
243 if (scratch_chan < 0)
245 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
246 O_RDONLY, 0, &scratch_pathname);
248 if (scratch_chan < 0)
250 perror_with_name (filename);
252 /* Leave scratch_pathname allocated. bfd->name will point to it. */
254 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
255 if (!abfd)
257 close (scratch_chan);
258 error ("Could not open `%s' as an executable file: %s",
259 scratch_pathname, bfd_errmsg (bfd_get_error ()));
261 /* Leave bfd open, core_xfer_memory and "info files" need it. */
262 so -> abfd = abfd;
263 abfd -> cacheable = true;
265 if (!bfd_check_format (abfd, bfd_object))
267 error ("\"%s\": not in executable format: %s.",
268 scratch_pathname, bfd_errmsg (bfd_get_error ()));
270 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
272 error ("Can't find the file sections in `%s': %s",
273 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
276 for (p = so -> sections; p < so -> sections_end; p++)
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"))
286 so -> textsection = p;
290 /* Free the file names, close the file now. */
291 do_cleanups (old_chain);
293 return (1);
298 LOCAL FUNCTION
300 first_link_map_member -- locate first member in dynamic linker's map
302 SYNOPSIS
304 static struct link_map *first_link_map_member (void)
306 DESCRIPTION
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.
313 static struct link_map *
314 first_link_map_member ()
316 struct link_map *lm = NULL;
317 static struct link_map first_lm;
319 #ifdef USE_LDR_ROUTINES
320 ldr_module_t mod_id = LDR_NULL_MODULE;
321 size_t retsize;
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;
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
348 lm = &first_lm;
350 /* The first entry is for the main program and should be skipped. */
351 lm->l_name = NULL;
353 return lm;
356 static struct link_map *
357 next_link_map_member (so_list_ptr)
358 struct so_list *so_list_ptr;
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;
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;
373 lm = &next_lm;
374 lm->l_name = lm->module_info.lmi_name;
375 #else
376 CORE_ADDR ldr_context_addr;
378 /* Reread context in case ldr_context.tail was updated. */
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;
392 lm = &next_lm;
393 lm->l_name = lm->module_info.module_name;
394 #endif
395 return lm;
398 static void
399 xfer_link_map_member (so_list_ptr, lm)
400 struct so_list *so_list_ptr;
401 struct link_map *lm;
403 int i;
404 so_list_ptr->lm = *lm;
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.
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;
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)
424 #ifdef USE_LDR_ROUTINES
425 int len = strlen (LM_NAME (so_list_ptr) + 1);
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';
432 for (i = 0; i < lm->module_info.lmi_nregion; i++)
434 ldr_region_info_t region_info;
435 size_t retsize;
436 CORE_ADDR region_offset;
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);
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';
462 for (i = 0; i < lm->module_info.region_count; i++)
464 ldr_region_info_t region_info;
465 CORE_ADDR region_offset;
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)
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);
489 #endif
491 catch_errors (solib_map_sections, (char *) so_list_ptr,
492 "Error while mapping shared library sections:\n",
493 RETURN_MASK_ALL);
499 LOCAL FUNCTION
501 find_solib -- step through list of shared objects
503 SYNOPSIS
505 struct so_list *find_solib (struct so_list *so_list_ptr)
507 DESCRIPTION
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.
516 The arg and return value are "struct link_map" pointers, as defined
517 in <link.h>.
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 */
524 struct so_list *so_list_next = NULL;
525 struct link_map *lm = NULL;
526 struct so_list *new;
528 if (so_list_ptr == NULL)
530 /* We are setting up for a new scan through the loaded images. */
531 if ((so_list_next = so_list_head) == NULL)
533 /* Find the first link map list member. */
534 lm = first_link_map_member ();
537 else
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;
544 if ((so_list_next == NULL) && (lm != NULL))
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)
555 so_list_ptr -> next = new;
557 else
559 so_list_head = new;
561 so_list_next = new;
562 xfer_link_map_member (new, lm);
564 return (so_list_next);
567 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
569 static int
570 symbol_add_stub (arg)
571 char *arg;
573 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
574 CORE_ADDR text_addr = 0;
576 if (so -> textsection)
577 text_addr = so -> textsection -> addr;
578 else if (so -> abfd != NULL)
580 asection *lowest_sect;
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. */
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);
593 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
594 text_addr,
595 0, 0, 0);
596 return (1);
601 GLOBAL FUNCTION
603 solib_add -- add a shared library file to the symtab and section list
605 SYNOPSIS
607 void solib_add (char *arg_string, int from_tty,
608 struct target_ops *target)
610 DESCRIPTION
614 void
615 solib_add (arg_string, from_tty, target)
616 char *arg_string;
617 int from_tty;
618 struct target_ops *target;
620 register struct so_list *so = NULL; /* link map state variable */
622 /* Last shared library that we read. */
623 struct so_list *so_last = NULL;
625 char *re_err;
626 int count;
627 int old;
629 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
631 error ("Invalid regexp: %s", re_err);
635 /* Add the shared library sections to the section table of the
636 specified target, if any. */
637 if (target)
639 /* Count how many new section_table entries there are. */
640 so = NULL;
641 count = 0;
642 while ((so = find_solib (so)) != NULL)
644 if (so -> so_name[0])
646 count += so -> sections_end - so -> sections;
650 if (count)
652 int update_coreops;
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;
659 /* Reallocate the target's section table including the new size. */
660 if (target -> to_sections)
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));
667 else
669 old = 0;
670 target -> to_sections = (struct section_table *)
671 xmalloc ((sizeof (struct section_table)) * count);
673 target -> to_sections_end = target -> to_sections + (count + old);
675 /* Update the to_sections field in the core_ops structure
676 if needed. */
677 if (update_coreops)
679 core_ops.to_sections = target->to_sections;
680 core_ops.to_sections_end = target->to_sections_end;
683 /* Add these section table entries to the target's table. */
684 while ((so = find_solib (so)) != NULL)
686 if (so -> so_name[0])
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;
698 /* Now add the symbol files. */
699 so = NULL;
700 while ((so = find_solib (so)) != NULL)
702 if (so -> so_name[0] && re_exec (so -> so_name))
704 so -> from_tty = from_tty;
705 if (so -> symbols_loaded)
707 if (from_tty)
709 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
712 else if (catch_errors
713 (symbol_add_stub, (char *) so,
714 "Error while reading shared library symbols:\n",
715 RETURN_MASK_ALL))
717 so_last = so;
718 so -> symbols_loaded = 1;
723 /* Getting new symbols may change our opinion about what is
724 frameless. */
725 if (so_last)
726 reinit_frame_cache ();
731 LOCAL FUNCTION
733 info_sharedlibrary_command -- code for "info sharedlibrary"
735 SYNOPSIS
737 static void info_sharedlibrary_command ()
739 DESCRIPTION
741 Walk through the shared library list and print information
742 about each attached library.
745 static void
746 info_sharedlibrary_command (ignore, from_tty)
747 char *ignore;
748 int from_tty;
750 register struct so_list *so = NULL; /* link map state variable */
751 int header_done = 0;
753 if (exec_bfd == NULL)
755 printf_unfiltered ("No exec file.\n");
756 return;
758 while ((so = find_solib (so)) != NULL)
760 if (so -> so_name[0])
762 unsigned long txt_start = 0;
763 unsigned long txt_end = 0;
765 if (!header_done)
767 printf_unfiltered("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
768 "Shared Object Library");
769 header_done++;
771 if (so -> textsection)
773 txt_start = (unsigned long) so -> textsection -> addr;
774 txt_end = (unsigned long) so -> textsection -> endaddr;
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);
782 if (so_list_head == NULL)
784 printf_unfiltered ("No shared libraries loaded at this time.\n");
790 GLOBAL FUNCTION
792 solib_address -- check to see if an address is in a shared lib
794 SYNOPSIS
796 char *solib_address (CORE_ADDR address)
798 DESCRIPTION
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.
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.
812 char *
813 solib_address (address)
814 CORE_ADDR address;
816 register struct so_list *so = 0; /* link map state variable */
818 while ((so = find_solib (so)) != NULL)
820 if (so -> so_name[0] && so -> textsection)
822 if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
823 (address < (CORE_ADDR) so -> textsection -> endaddr))
824 return (so->so_name);
827 return (0);
830 /* Called by free_all_symtabs */
832 void
833 clear_solib()
835 struct so_list *next;
836 char *bfd_filename;
838 while (so_list_head)
840 if (so_list_head -> sections)
842 free ((PTR)so_list_head -> sections);
844 if (so_list_head -> abfd)
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 ()));
851 else
852 /* This happens for the executable on SVR4. */
853 bfd_filename = NULL;
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;
865 GLOBAL FUNCTION
867 solib_create_inferior_hook -- shared library startup support
869 SYNOPSIS
871 void solib_create_inferior_hook()
873 DESCRIPTION
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.
890 FIXME
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.
896 Also, what if child has exit()ed? Must exit loop somehow.
899 void
900 solib_create_inferior_hook()
903 /* Nothing to do for statically bound executables. */
905 if (symfile_objfile == NULL
906 || symfile_objfile->obfd == NULL
907 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
908 return;
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. */
915 clear_proceed_status ();
916 stop_soon_quietly = 1;
917 stop_signal = TARGET_SIGNAL_0;
920 target_resume (-1, 0, stop_signal);
921 wait_for_inferior ();
923 while (stop_signal != TARGET_SIGNAL_TRAP);
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;
939 LOCAL FUNCTION
941 sharedlibrary_command -- handle command to explicitly add library
943 SYNOPSIS
945 static void sharedlibrary_command (char *args, int from_tty)
947 DESCRIPTION
951 static void
952 sharedlibrary_command (args, from_tty)
953 char *args;
954 int from_tty;
956 dont_repeat ();
957 solib_add (args, from_tty, (struct target_ops *) 0);
960 void
961 _initialize_solib()
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.");
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);