1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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. */
25 #include <sys/types.h>
42 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
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). */
50 #ifndef USE_LDR_ROUTINES
51 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
59 CORE_ADDR modinfo_addr;
70 static ldr_context_t ldr_context;
75 /* Define our own link_map structure.
76 This will help to share code with solib.c. */
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 */
84 #define LM_ADDR(so) ((so) -> lm.l_addr)
85 #define LM_NAME(so) ((so) -> lm.l_name)
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;
102 static struct so_list *so_list_head; /* List of known shared objects */
105 fdmatch PARAMS ((int, int)); /* In libiberty */
107 /* Local function prototypes */
110 sharedlibrary_command PARAMS ((char *, int));
113 info_sharedlibrary_command PARAMS ((char *, int));
116 symbol_add_stub PARAMS ((char *));
118 static struct so_list *
119 find_solib PARAMS ((struct so_list *));
121 static struct link_map *
122 first_link_map_member PARAMS ((void));
124 static struct link_map *
125 next_link_map_member PARAMS ((struct so_list *));
128 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
131 solib_map_sections PARAMS ((struct so_list *));
137 solib_map_sections -- open bfd and build sections for shared lib
141 static void solib_map_sections (struct so_list *so)
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.
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
161 solib_map_sections (so)
165 char *scratch_pathname;
167 struct section_table *p;
168 struct cleanup *old_chain;
171 filename = tilde_expand (so -> so_name);
172 old_chain = make_cleanup (free, filename);
174 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
176 if (scratch_chan < 0)
178 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
179 O_RDONLY, 0, &scratch_pathname);
181 if (scratch_chan < 0)
183 perror_with_name (filename);
185 /* Leave scratch_pathname allocated. bfd->name will point to it. */
187 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
190 close (scratch_chan);
191 error ("Could not open `%s' as an executable file: %s",
192 scratch_pathname, bfd_errmsg (bfd_get_error ()));
194 /* Leave bfd open, core_xfer_memory and "info files" need it. */
196 abfd -> cacheable = true;
198 if (!bfd_check_format (abfd, bfd_object))
200 error ("\"%s\": not in executable format: %s.",
201 scratch_pathname, bfd_errmsg (bfd_get_error ()));
203 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
205 error ("Can't find the file sections in `%s': %s",
206 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
209 for (p = so -> sections; p < so -> sections_end; p++)
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
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 -> the_bfd_section -> name, ".text"))
219 so -> textsection = p;
223 /* Free the file names, close the file now. */
224 do_cleanups (old_chain);
231 first_link_map_member -- locate first member in dynamic linker's map
235 static struct link_map *first_link_map_member (void)
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.
244 static struct link_map *
245 first_link_map_member ()
247 struct link_map *lm = NULL;
248 static struct link_map first_lm;
250 #ifdef USE_LDR_ROUTINES
251 ldr_module_t mod_id = LDR_NULL_MODULE;
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),
261 CORE_ADDR ldr_context_addr;
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)
277 /* The first entry is for the main program and should be skipped. */
283 static struct link_map *
284 next_link_map_member (so_list_ptr)
285 struct so_list *so_list_ptr;
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;
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),
301 lm->l_name = lm->module_info.lmi_name;
303 CORE_ADDR ldr_context_addr;
305 /* Reread context in case ldr_context.tail was updated. */
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)
320 lm->l_name = lm->module_info.module_name;
326 xfer_link_map_member (so_list_ptr, lm)
327 struct so_list *so_list_ptr;
330 so_list_ptr->lm = *lm;
332 /* OSF/1 has absolute addresses in shared libraries. */
333 LM_ADDR (so_list_ptr) = 0;
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)
340 #ifdef USE_LDR_ROUTINES
341 int len = strlen (LM_NAME (so_list_ptr) + 1);
343 if (len > MAX_PATH_SIZE)
345 strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
349 target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
350 MAX_PATH_SIZE - 1, &errcode);
352 error ("xfer_link_map_member: Can't read pathname for load map: %s\n",
353 safe_strerror (errcode));
354 strncpy (so_list_ptr->so_name, buffer, MAX_PATH_SIZE - 1);
357 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
359 solib_map_sections (so_list_ptr);
367 find_solib -- step through list of shared objects
371 struct so_list *find_solib (struct so_list *so_list_ptr)
375 This module contains the routine which finds the names of any
376 loaded "images" in the current process. The argument in must be
377 NULL on the first call, and then the returned value must be passed
378 in on subsequent calls. This provides the capability to "step" down
379 the list of loaded objects. On the last object, a NULL value is
382 The arg and return value are "struct link_map" pointers, as defined
386 static struct so_list *
387 find_solib (so_list_ptr)
388 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
390 struct so_list *so_list_next = NULL;
391 struct link_map *lm = NULL;
394 if (so_list_ptr == NULL)
396 /* We are setting up for a new scan through the loaded images. */
397 if ((so_list_next = so_list_head) == NULL)
399 /* Find the first link map list member. */
400 lm = first_link_map_member ();
405 /* We have been called before, and are in the process of walking
406 the shared library list. Advance to the next shared object. */
407 lm = next_link_map_member (so_list_ptr);
408 so_list_next = so_list_ptr -> next;
410 if ((so_list_next == NULL) && (lm != NULL))
412 /* Get next link map structure from inferior image and build a local
413 abbreviated load_map structure */
414 new = (struct so_list *) xmalloc (sizeof (struct so_list));
415 memset ((char *) new, 0, sizeof (struct so_list));
417 /* Add the new node as the next node in the list, or as the root
418 node if this is the first one. */
419 if (so_list_ptr != NULL)
421 so_list_ptr -> next = new;
428 xfer_link_map_member (new, lm);
430 return (so_list_next);
433 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
436 symbol_add_stub (arg)
439 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
441 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
442 so -> textsection -> addr,
451 solib_add -- add a shared library file to the symtab and section list
455 void solib_add (char *arg_string, int from_tty,
456 struct target_ops *target)
463 solib_add (arg_string, from_tty, target)
466 struct target_ops *target;
468 register struct so_list *so = NULL; /* link map state variable */
470 /* Last shared library that we read. */
471 struct so_list *so_last = NULL;
476 int symbols_added = 0;
478 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
480 error ("Invalid regexp: %s", re_err);
484 /* Add the shared library sections to the section table of the
485 specified target, if any. */
488 /* Count how many new section_table entries there are. */
491 while ((so = find_solib (so)) != NULL)
493 if (so -> so_name[0])
495 count += so -> sections_end - so -> sections;
501 /* Reallocate the target's section table including the new size. */
502 if (target -> to_sections)
504 old = target -> to_sections_end - target -> to_sections;
505 target -> to_sections = (struct section_table *)
506 xrealloc ((char *)target -> to_sections,
507 (sizeof (struct section_table)) * (count + old));
512 target -> to_sections = (struct section_table *)
513 xmalloc ((sizeof (struct section_table)) * count);
515 target -> to_sections_end = target -> to_sections + (count + old);
517 /* Add these section table entries to the target's table. */
518 while ((so = find_solib (so)) != NULL)
520 if (so -> so_name[0])
522 count = so -> sections_end - so -> sections;
523 memcpy ((char *) (target -> to_sections + old),
525 (sizeof (struct section_table)) * count);
532 /* Now add the symbol files. */
534 while ((so = find_solib (so)) != NULL)
536 if (so -> so_name[0] && re_exec (so -> so_name))
538 so -> from_tty = from_tty;
539 if (so -> symbols_loaded)
543 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
546 else if (catch_errors
547 (symbol_add_stub, (char *) so,
548 "Error while reading shared library symbols:\n",
552 so -> symbols_loaded = 1;
558 /* Getting new symbols may change our opinion about what is
561 reinit_frame_cache ();
568 info_sharedlibrary_command -- code for "info sharedlibrary"
572 static void info_sharedlibrary_command ()
576 Walk through the shared library list and print information
577 about each attached library.
581 info_sharedlibrary_command (ignore, from_tty)
585 register struct so_list *so = NULL; /* link map state variable */
588 if (exec_bfd == NULL)
590 printf_unfiltered ("No exec file.\n");
593 while ((so = find_solib (so)) != NULL)
595 if (so -> so_name[0])
597 unsigned long txt_start = 0;
598 unsigned long txt_end = 0;
602 printf_unfiltered("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
603 "Shared Object Library");
606 if (so -> textsection)
608 txt_start = (unsigned long) so -> textsection -> addr;
609 txt_end = (unsigned long) so -> textsection -> endaddr;
611 printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
612 printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
613 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
614 printf_unfiltered ("%s\n", so -> so_name);
617 if (so_list_head == NULL)
619 printf_unfiltered ("No shared libraries loaded at this time.\n");
627 solib_address -- check to see if an address is in a shared lib
631 int solib_address (CORE_ADDR address)
635 Provides a hook for other gdb routines to discover whether or
636 not a particular address is within the mapped address space of
637 a shared library. Any address between the base mapping address
638 and the first address beyond the end of the last mapping, is
639 considered to be within the shared library address space, for
642 For example, this routine is called at one point to disable
643 breakpoints which are in shared libraries that are not currently
648 solib_address (address)
651 register struct so_list *so = 0; /* link map state variable */
653 while ((so = find_solib (so)) != NULL)
655 if (so -> so_name[0] && so -> textsection)
657 if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
658 (address < (CORE_ADDR) so -> textsection -> endaddr))
667 /* Called by free_all_symtabs */
672 struct so_list *next;
677 if (so_list_head -> sections)
679 free ((PTR)so_list_head -> sections);
681 if (so_list_head -> abfd)
683 bfd_filename = bfd_get_filename (so_list_head -> abfd);
684 bfd_close (so_list_head -> abfd);
687 /* This happens for the executable on SVR4. */
690 next = so_list_head -> next;
692 free ((PTR)bfd_filename);
693 free ((PTR)so_list_head);
702 solib_create_inferior_hook -- shared library startup support
706 void solib_create_inferior_hook()
710 When gdb starts up the inferior, it nurses it along (through the
711 shell) until it is ready to execute it's first instruction. At this
712 point, this function gets called via expansion of the macro
713 SOLIB_CREATE_INFERIOR_HOOK.
714 For a statically bound executable, this first instruction is the
715 one at "_start", or a similar text label. No further processing is
717 For a dynamically bound executable, this first instruction is somewhere
718 in the rld, and the actual user executable is not yet mapped in.
719 We continue the inferior again, rld then maps in the actual user
720 executable and any needed shared libraries and then sends
722 At that point we discover the names of all shared libraries and
723 read their symbols in.
727 This code does not properly handle hitting breakpoints which the
728 user might have set in the rld itself. Proper handling would have
729 to check if the SIGTRAP happened due to a kill call.
731 Also, what if child has exit()ed? Must exit loop somehow.
735 solib_create_inferior_hook()
738 /* Nothing to do for statically bound executables. */
740 if (symfile_objfile == NULL
741 || symfile_objfile->obfd == NULL
742 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
745 /* Now run the target. It will eventually get a SIGTRAP, at
746 which point all of the libraries will have been mapped in and we
747 can go groveling around in the rld structures to find
748 out what we need to know about them. */
750 clear_proceed_status ();
751 stop_soon_quietly = 1;
752 stop_signal = TARGET_SIGNAL_0;
755 target_resume (-1, 0, stop_signal);
756 wait_for_inferior ();
758 while (stop_signal != TARGET_SIGNAL_TRAP);
760 /* solib_add will call reinit_frame_cache.
761 But we are stopped in the runtime loader and we do not have symbols
762 for the runtime loader. So heuristic_proc_start will be called
763 and will put out an annoying warning.
764 Delaying the resetting of stop_soon_quietly until after symbol loading
765 suppresses the warning. */
766 solib_add ((char *) 0, 0, (struct target_ops *) 0);
767 stop_soon_quietly = 0;
775 sharedlibrary_command -- handle command to explicitly add library
779 static void sharedlibrary_command (char *args, int from_tty)
786 sharedlibrary_command (args, from_tty)
791 solib_add (args, from_tty, (struct target_ops *) 0);
798 add_com ("sharedlibrary", class_files, sharedlibrary_command,
799 "Load shared object library symbols for files matching REGEXP.");
800 add_info ("sharedlibrary", info_sharedlibrary_command,
801 "Status of loaded shared object libraries.");