1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* FIXME: Most of this code could be merged with solib.c by using
23 next_link_map_member and xfer_link_map_member in solib.c. */
27 #include <sys/types.h>
29 #include "gdb_string.h"
40 #include "gdb_regex.h"
45 #define MAX_PATH_SIZE 1024 /* FIXME: Should be dynamic */
47 /* When handling shared libraries, GDB has to find out the pathnames
48 of all shared libraries that are currently loaded (to read in their
49 symbols) and where the shared libraries are loaded in memory
50 (to relocate them properly from their prelinked addresses to the
51 current load address).
53 Under OSF/1 there are two possibilities to get at this information:
54 1) Peek around in the runtime loader structures.
55 These are not documented, and they are not defined in the system
56 header files. The definitions below were obtained by experimentation,
57 but they seem stable enough.
58 2) Use the undocumented libxproc.a library, which contains the
59 equivalent ldr_* routines.
60 This approach is somewhat cleaner, but it requires that the GDB
61 executable is dynamically linked. In addition it requires a
62 NAT_CLIBS= -lxproc -Wl,-expect_unresolved,ldr_process_context
63 linker specification for GDB and all applications that are using
65 We will use the peeking approach until it becomes unwieldy. */
67 #ifndef USE_LDR_ROUTINES
69 /* Definition of runtime loader structures, found by experimentation. */
70 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
78 CORE_ADDR modinfo_addr;
83 CORE_ADDR regioninfo_addr;
90 CORE_ADDR regionname_addr;
108 static ldr_context_t ldr_context;
113 static ldr_process_t fake_ldr_process;
115 /* Called by ldr_* routines to read memory from the current target. */
117 static int ldr_read_memory (CORE_ADDR, char *, int, int);
120 ldr_read_memory (CORE_ADDR memaddr, char *myaddr, int len, int readstring)
127 target_read_string (memaddr, &buffer, len, &result);
129 strcpy (myaddr, buffer);
133 result = target_read_memory (memaddr, myaddr, len);
142 /* Define our own link_map structure.
143 This will help to share code with solib.c. */
147 CORE_ADDR l_offset; /* prelink to load address offset */
148 char *l_name; /* full name of loaded object */
149 ldr_module_info_t module_info; /* corresponding module info */
152 #define LM_OFFSET(so) ((so) -> lm.l_offset)
153 #define LM_NAME(so) ((so) -> lm.l_name)
157 struct so_list *next; /* next structure in linked list */
158 struct link_map lm; /* copy of link map from inferior */
159 struct link_map *lmaddr; /* addr in inferior lm was read from */
160 CORE_ADDR lmend; /* upper addr bound of mapped object */
161 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
162 char symbols_loaded; /* flag: symbols read in yet? */
163 char from_tty; /* flag: print msgs? */
164 struct objfile *objfile; /* objfile for loaded lib */
165 struct section_table *sections;
166 struct section_table *sections_end;
167 struct section_table *textsection;
171 static struct so_list *so_list_head; /* List of known shared objects */
173 extern int fdmatch (int, int); /* In libiberty */
175 /* Local function prototypes */
177 static void sharedlibrary_command (char *, int);
179 static void info_sharedlibrary_command (char *, int);
181 static int symbol_add_stub (char *);
183 static struct so_list *find_solib (struct so_list *);
185 static struct link_map *first_link_map_member (void);
187 static struct link_map *next_link_map_member (struct so_list *);
189 static void xfer_link_map_member (struct so_list *, struct link_map *);
191 static int solib_map_sections (char *);
197 solib_map_sections -- open bfd and build sections for shared lib
201 static int solib_map_sections (struct so_list *so)
205 Given a pointer to one of the shared objects in our list
206 of mapped objects, use the recorded name to open a bfd
207 descriptor for the object, build a section table, and then
208 relocate all the section addresses by the base address at
209 which the shared object was mapped.
213 In most (all?) cases the shared object file name recorded in the
214 dynamic linkage tables will be a fully qualified pathname. For
215 cases where it isn't, do we really mimic the systems search
216 mechanism correctly in the below code (particularly the tilde
221 solib_map_sections (char *arg)
223 struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */
225 char *scratch_pathname;
227 struct section_table *p;
228 struct cleanup *old_chain;
231 filename = tilde_expand (so->so_name);
232 old_chain = make_cleanup (xfree, filename);
234 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
236 if (scratch_chan < 0)
238 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
239 O_RDONLY, 0, &scratch_pathname);
241 if (scratch_chan < 0)
243 perror_with_name (filename);
245 /* Leave scratch_pathname allocated. bfd->name will point to it. */
247 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
250 close (scratch_chan);
251 error ("Could not open `%s' as an executable file: %s",
252 scratch_pathname, bfd_errmsg (bfd_get_error ()));
254 /* Leave bfd open, core_xfer_memory and "info files" need it. */
258 if (!bfd_check_format (abfd, bfd_object))
260 error ("\"%s\": not in executable format: %s.",
261 scratch_pathname, bfd_errmsg (bfd_get_error ()));
263 if (build_section_table (abfd, &so->sections, &so->sections_end))
265 error ("Can't find the file sections in `%s': %s",
266 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
269 for (p = so->sections; p < so->sections_end; p++)
271 /* Relocate the section binding addresses as recorded in the shared
272 object's file by the offset to get the address to which the
273 object was actually mapped. */
274 p->addr += LM_OFFSET (so);
275 p->endaddr += LM_OFFSET (so);
276 so->lmend = (CORE_ADDR) max (p->endaddr, so->lmend);
277 if (STREQ (p->the_bfd_section->name, ".text"))
283 /* Free the file names, close the file now. */
284 do_cleanups (old_chain);
293 first_link_map_member -- locate first member in dynamic linker's map
297 static struct link_map *first_link_map_member (void)
301 Read in a copy of the first member in the inferior's dynamic
302 link map from the inferior's dynamic linker structures, and return
303 a pointer to the copy in our address space.
306 static struct link_map *
307 first_link_map_member (void)
309 struct link_map *lm = NULL;
310 static struct link_map first_lm;
312 #ifdef USE_LDR_ROUTINES
313 ldr_module_t mod_id = LDR_NULL_MODULE;
316 fake_ldr_process = ldr_core_process ();
317 ldr_set_core_reader (ldr_read_memory);
318 ldr_xdetach (fake_ldr_process);
319 if (ldr_xattach (fake_ldr_process) != 0
320 || ldr_next_module (fake_ldr_process, &mod_id) != 0
321 || mod_id == LDR_NULL_MODULE
322 || ldr_inq_module (fake_ldr_process, mod_id,
323 &first_lm.module_info, sizeof (ldr_module_info_t),
327 CORE_ADDR ldr_context_addr;
329 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
330 (char *) &ldr_context_addr,
331 sizeof (CORE_ADDR)) != 0
332 || target_read_memory (ldr_context_addr,
333 (char *) &ldr_context,
334 sizeof (ldr_context_t)) != 0
335 || target_read_memory ((CORE_ADDR) ldr_context.head,
336 (char *) &first_lm.module_info,
337 sizeof (ldr_module_info_t)) != 0)
343 /* The first entry is for the main program and should be skipped. */
349 static struct link_map *
350 next_link_map_member (struct so_list *so_list_ptr)
352 struct link_map *lm = NULL;
353 static struct link_map next_lm;
354 #ifdef USE_LDR_ROUTINES
355 ldr_module_t mod_id = so_list_ptr->lm.module_info.lmi_modid;
358 if (ldr_next_module (fake_ldr_process, &mod_id) != 0
359 || mod_id == LDR_NULL_MODULE
360 || ldr_inq_module (fake_ldr_process, mod_id,
361 &next_lm.module_info, sizeof (ldr_module_info_t),
366 lm->l_name = lm->module_info.lmi_name;
368 CORE_ADDR ldr_context_addr;
370 /* Reread context in case ldr_context.tail was updated. */
372 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
373 (char *) &ldr_context_addr,
374 sizeof (CORE_ADDR)) != 0
375 || target_read_memory (ldr_context_addr,
376 (char *) &ldr_context,
377 sizeof (ldr_context_t)) != 0
378 || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
379 || target_read_memory (so_list_ptr->lm.module_info.next,
380 (char *) &next_lm.module_info,
381 sizeof (ldr_module_info_t)) != 0)
385 lm->l_name = lm->module_info.module_name;
391 xfer_link_map_member (struct so_list *so_list_ptr, struct link_map *lm)
394 so_list_ptr->lm = *lm;
396 /* OSF/1 shared libraries are pre-linked to particular addresses,
397 but the runtime loader may have to relocate them if the
398 address ranges of the libraries used by the target executable clash,
399 or if the target executable is linked with the -taso option.
400 The offset is the difference between the address where the shared
401 library is mapped and the pre-linked address of the shared library.
403 FIXME: GDB is currently unable to relocate the shared library
404 sections by different offsets. If sections are relocated by
405 different offsets, put out a warning and use the offset of the
406 first section for all remaining sections. */
407 LM_OFFSET (so_list_ptr) = 0;
409 /* There is one entry that has no name (for the inferior executable)
410 since it is not a shared object. */
411 if (LM_NAME (so_list_ptr) != 0)
414 #ifdef USE_LDR_ROUTINES
415 int len = strlen (LM_NAME (so_list_ptr) + 1);
417 if (len > MAX_PATH_SIZE)
419 strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
420 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
422 for (i = 0; i < lm->module_info.lmi_nregion; i++)
424 ldr_region_info_t region_info;
426 CORE_ADDR region_offset;
428 if (ldr_inq_region (fake_ldr_process, lm->module_info.lmi_modid,
429 i, ®ion_info, sizeof (region_info),
432 region_offset = (CORE_ADDR) region_info.lri_mapaddr
433 - (CORE_ADDR) region_info.lri_vaddr;
435 LM_OFFSET (so_list_ptr) = region_offset;
436 else if (LM_OFFSET (so_list_ptr) != region_offset)
437 warning ("cannot handle shared library relocation for %s (%s)",
438 so_list_ptr->so_name, region_info.lri_name);
443 target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
444 MAX_PATH_SIZE - 1, &errcode);
446 error ("xfer_link_map_member: Can't read pathname for load map: %s\n",
447 safe_strerror (errcode));
448 strncpy (so_list_ptr->so_name, buffer, MAX_PATH_SIZE - 1);
450 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
452 for (i = 0; i < lm->module_info.region_count; i++)
454 ldr_region_info_t region_info;
455 CORE_ADDR region_offset;
457 if (target_read_memory (lm->module_info.regioninfo_addr
458 + i * sizeof (region_info),
459 (char *) ®ion_info,
460 sizeof (region_info)) != 0)
462 region_offset = region_info.mapaddr - region_info.vaddr;
464 LM_OFFSET (so_list_ptr) = region_offset;
465 else if (LM_OFFSET (so_list_ptr) != region_offset)
468 target_read_string (region_info.regionname_addr, &buffer,
469 MAX_PATH_SIZE - 1, &errcode);
471 region_name = buffer;
474 warning ("cannot handle shared library relocation for %s (%s)",
475 so_list_ptr->so_name, region_name);
481 catch_errors (solib_map_sections, (char *) so_list_ptr,
482 "Error while mapping shared library sections:\n",
491 find_solib -- step through list of shared objects
495 struct so_list *find_solib (struct so_list *so_list_ptr)
499 This module contains the routine which finds the names of any
500 loaded "images" in the current process. The argument in must be
501 NULL on the first call, and then the returned value must be passed
502 in on subsequent calls. This provides the capability to "step" down
503 the list of loaded objects. On the last object, a NULL value is
506 The arg and return value are "struct link_map" pointers, as defined
510 static struct so_list *
511 find_solib (struct so_list *so_list_ptr)
513 struct so_list *so_list_next = NULL;
514 struct link_map *lm = NULL;
517 if (so_list_ptr == NULL)
519 /* We are setting up for a new scan through the loaded images. */
520 if ((so_list_next = so_list_head) == NULL)
522 /* Find the first link map list member. */
523 lm = first_link_map_member ();
528 /* We have been called before, and are in the process of walking
529 the shared library list. Advance to the next shared object. */
530 lm = next_link_map_member (so_list_ptr);
531 so_list_next = so_list_ptr->next;
533 if ((so_list_next == NULL) && (lm != NULL))
535 /* Get next link map structure from inferior image and build a local
536 abbreviated load_map structure */
537 new = (struct so_list *) xmalloc (sizeof (struct so_list));
538 memset ((char *) new, 0, sizeof (struct so_list));
540 /* Add the new node as the next node in the list, or as the root
541 node if this is the first one. */
542 if (so_list_ptr != NULL)
544 so_list_ptr->next = new;
551 xfer_link_map_member (new, lm);
553 return (so_list_next);
556 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
559 symbol_add_stub (char *arg)
561 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
562 CORE_ADDR text_addr = 0;
563 struct section_addr_info section_addrs;
565 memset (§ion_addrs, 0, sizeof (section_addrs));
567 text_addr = so->textsection->addr;
568 else if (so->abfd != NULL)
570 asection *lowest_sect;
572 /* If we didn't find a mapped non zero sized .text section, set up
573 text_addr so that the relocation in symbol_file_add does no harm. */
575 lowest_sect = bfd_get_section_by_name (so->abfd, ".text");
576 if (lowest_sect == NULL)
577 bfd_map_over_sections (so->abfd, find_lowest_section,
580 text_addr = bfd_section_vma (so->abfd, lowest_sect) + LM_OFFSET (so);
583 section_addrs.other[0].addr = text_addr;
584 section_addrs.other[0].name = ".text";
585 so->objfile = symbol_file_add (so->so_name, so->from_tty,
586 §ion_addrs, 0, OBJF_SHARED);
594 solib_add -- add a shared library file to the symtab and section list
598 void solib_add (char *arg_string, int from_tty,
599 struct target_ops *target, int readsyms)
606 solib_add (char *arg_string, int from_tty, struct target_ops *target, int readsyms)
608 register struct so_list *so = NULL; /* link map state variable */
610 /* Last shared library that we read. */
611 struct so_list *so_last = NULL;
620 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
622 error ("Invalid regexp: %s", re_err);
626 /* Add the shared library sections to the section table of the
627 specified target, if any. */
630 /* Count how many new section_table entries there are. */
633 while ((so = find_solib (so)) != NULL)
637 count += so->sections_end - so->sections;
643 /* Add these section table entries to the target's table. */
645 old = target_resize_to_sections (target, count);
647 while ((so = find_solib (so)) != NULL)
651 count = so->sections_end - so->sections;
652 memcpy ((char *) (target->to_sections + old),
654 (sizeof (struct section_table)) * count);
661 /* Now add the symbol files. */
663 while ((so = find_solib (so)) != NULL)
665 if (so->so_name[0] && re_exec (so->so_name))
667 so->from_tty = from_tty;
668 if (so->symbols_loaded)
672 printf_unfiltered ("Symbols already loaded for %s\n", so->so_name);
675 else if (catch_errors
676 (symbol_add_stub, (char *) so,
677 "Error while reading shared library symbols:\n",
681 so->symbols_loaded = 1;
686 /* Getting new symbols may change our opinion about what is
689 reinit_frame_cache ();
696 info_sharedlibrary_command -- code for "info sharedlibrary"
700 static void info_sharedlibrary_command ()
704 Walk through the shared library list and print information
705 about each attached library.
709 info_sharedlibrary_command (char *ignore, int from_tty)
711 register struct so_list *so = NULL; /* link map state variable */
714 if (exec_bfd == NULL)
716 printf_unfiltered ("No executable file.\n");
719 while ((so = find_solib (so)) != NULL)
723 unsigned long txt_start = 0;
724 unsigned long txt_end = 0;
728 printf_unfiltered ("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
729 "Shared Object Library");
734 txt_start = (unsigned long) so->textsection->addr;
735 txt_end = (unsigned long) so->textsection->endaddr;
737 printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
738 printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
739 printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No");
740 printf_unfiltered ("%s\n", so->so_name);
743 if (so_list_head == NULL)
745 printf_unfiltered ("No shared libraries loaded at this time.\n");
753 solib_address -- check to see if an address is in a shared lib
757 char *solib_address (CORE_ADDR address)
761 Provides a hook for other gdb routines to discover whether or
762 not a particular address is within the mapped address space of
763 a shared library. Any address between the base mapping address
764 and the first address beyond the end of the last mapping, is
765 considered to be within the shared library address space, for
768 For example, this routine is called at one point to disable
769 breakpoints which are in shared libraries that are not currently
774 solib_address (CORE_ADDR address)
776 register struct so_list *so = 0; /* link map state variable */
778 while ((so = find_solib (so)) != NULL)
780 if (so->so_name[0] && so->textsection)
782 if ((address >= (CORE_ADDR) so->textsection->addr) &&
783 (address < (CORE_ADDR) so->textsection->endaddr))
784 return (so->so_name);
790 /* Called by free_all_symtabs */
795 struct so_list *next;
798 disable_breakpoints_in_shlibs (1);
802 if (so_list_head->sections)
804 xfree (so_list_head->sections);
806 if (so_list_head->abfd)
808 remove_target_sections (so_list_head->abfd);
809 bfd_filename = bfd_get_filename (so_list_head->abfd);
810 if (!bfd_close (so_list_head->abfd))
811 warning ("cannot close \"%s\": %s",
812 bfd_filename, bfd_errmsg (bfd_get_error ()));
815 /* This happens for the executable on SVR4. */
818 next = so_list_head->next;
820 xfree (bfd_filename);
821 xfree (so_list_head);
830 solib_create_inferior_hook -- shared library startup support
834 void solib_create_inferior_hook()
838 When gdb starts up the inferior, it nurses it along (through the
839 shell) until it is ready to execute it's first instruction. At this
840 point, this function gets called via expansion of the macro
841 SOLIB_CREATE_INFERIOR_HOOK.
842 For a statically bound executable, this first instruction is the
843 one at "_start", or a similar text label. No further processing is
845 For a dynamically bound executable, this first instruction is somewhere
846 in the rld, and the actual user executable is not yet mapped in.
847 We continue the inferior again, rld then maps in the actual user
848 executable and any needed shared libraries and then sends
850 At that point we discover the names of all shared libraries and
851 read their symbols in.
855 This code does not properly handle hitting breakpoints which the
856 user might have set in the rld itself. Proper handling would have
857 to check if the SIGTRAP happened due to a kill call.
859 Also, what if child has exit()ed? Must exit loop somehow.
863 solib_create_inferior_hook (void)
866 /* Nothing to do for statically bound executables. */
868 if (symfile_objfile == NULL
869 || symfile_objfile->obfd == NULL
870 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
873 /* Now run the target. It will eventually get a SIGTRAP, at
874 which point all of the libraries will have been mapped in and we
875 can go groveling around in the rld structures to find
876 out what we need to know about them. */
878 clear_proceed_status ();
879 stop_soon_quietly = 1;
880 stop_signal = TARGET_SIGNAL_0;
883 target_resume (minus_one_ptid, 0, stop_signal);
884 wait_for_inferior ();
886 while (stop_signal != TARGET_SIGNAL_TRAP);
888 /* solib_add will call reinit_frame_cache.
889 But we are stopped in the runtime loader and we do not have symbols
890 for the runtime loader. So heuristic_proc_start will be called
891 and will put out an annoying warning.
892 Delaying the resetting of stop_soon_quietly until after symbol loading
893 suppresses the warning. */
894 solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
895 stop_soon_quietly = 0;
903 sharedlibrary_command -- handle command to explicitly add library
907 static void sharedlibrary_command (char *args, int from_tty)
914 sharedlibrary_command (char *args, int from_tty)
917 solib_add (args, from_tty, (struct target_ops *) 0, 1);
921 _initialize_solib (void)
923 add_com ("sharedlibrary", class_files, sharedlibrary_command,
924 "Load shared object library symbols for files matching REGEXP.");
925 add_info ("sharedlibrary", info_sharedlibrary_command,
926 "Status of loaded shared object libraries.");
929 (add_set_cmd ("auto-solib-add", class_support, var_boolean,
930 (char *) &auto_solib_add,
931 "Set autoloading of shared library symbols.\n\
932 If \"on\", symbols from all shared object libraries will be loaded\n\
933 automatically when the inferior begins execution, when the dynamic linker\n\
934 informs gdb that a new library has been loaded, or when attaching to the\n\
935 inferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'.",