1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993, 94, 95, 96, 98, 1999 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. */
25 #include <sys/types.h>
27 #include "gdb_string.h"
38 #include "gnu-regex.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
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
76 CORE_ADDR modinfo_addr;
81 CORE_ADDR regioninfo_addr;
87 CORE_ADDR regionname_addr;
103 static ldr_context_t ldr_context;
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));
115 ldr_read_memory (memaddr, myaddr, len, readstring)
126 target_read_string (memaddr, &buffer, len, &result);
128 strcpy (myaddr, buffer);
132 result = target_read_memory (memaddr, myaddr, len);
141 /* Define our own link_map structure.
142 This will help to share code with solib.c. */
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)
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;
168 static struct so_list *so_list_head; /* List of known shared objects */
171 fdmatch PARAMS ((int, int)); /* In libiberty */
173 /* Local function prototypes */
176 sharedlibrary_command PARAMS ((char *, int));
179 info_sharedlibrary_command PARAMS ((char *, 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 *));
194 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
197 solib_map_sections PARAMS ((char *));
203 solib_map_sections -- open bfd and build sections for shared lib
207 static int solib_map_sections (struct so_list *so)
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.
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
227 solib_map_sections (arg)
230 struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */
232 char *scratch_pathname;
234 struct section_table *p;
235 struct cleanup *old_chain;
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,
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);
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. */
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);
300 first_link_map_member -- locate first member in dynamic linker's map
304 static struct link_map *first_link_map_member (void)
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;
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),
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)
350 /* The first entry is for the main program and should be skipped. */
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;
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),
374 lm->l_name = lm->module_info.lmi_name;
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)
393 lm->l_name = lm->module_info.module_name;
399 xfer_link_map_member (so_list_ptr, lm)
400 struct so_list *so_list_ptr;
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)
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;
436 CORE_ADDR region_offset;
438 if (ldr_inq_region (fake_ldr_process, lm->module_info.lmi_modid,
439 i, ®ion_info, sizeof (region_info),
442 region_offset = (CORE_ADDR) region_info.lri_mapaddr
443 - (CORE_ADDR) region_info.lri_vaddr;
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);
453 target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
454 MAX_PATH_SIZE - 1, &errcode);
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);
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 *) ®ion_info,
470 sizeof (region_info)) != 0)
472 region_offset = region_info.mapaddr - region_info.vaddr;
474 LM_OFFSET (so_list_ptr) = region_offset;
475 else if (LM_OFFSET (so_list_ptr) != region_offset)
478 target_read_string (region_info.regionname_addr, &buffer,
479 MAX_PATH_SIZE - 1, &errcode);
481 region_name = buffer;
484 warning ("cannot handle shared library relocation for %s (%s)",
485 so_list_ptr->so_name, region_name);
491 catch_errors (solib_map_sections, (char *) so_list_ptr,
492 "Error while mapping shared library sections:\n",
501 find_solib -- step through list of shared objects
505 struct so_list *find_solib (struct so_list *so_list_ptr)
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
516 The arg and return value are "struct link_map" pointers, as defined
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;
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 ();
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));
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;
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. */
570 symbol_add_stub (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,
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,
603 solib_add -- add a shared library file to the symtab and section list
607 void solib_add (char *arg_string, int from_tty,
608 struct target_ops *target)
615 solib_add (arg_string, from_tty, target)
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;
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. */
639 /* Count how many new section_table entries there are. */
642 while ((so = find_solib (so)) != NULL)
644 if (so -> so_name[0])
646 count += so -> sections_end - so -> sections;
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));
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
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),
691 (sizeof (struct section_table)) * count);
698 /* Now add the symbol files. */
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)
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",
718 so -> symbols_loaded = 1;
723 /* Getting new symbols may change our opinion about what is
726 reinit_frame_cache ();
733 info_sharedlibrary_command -- code for "info sharedlibrary"
737 static void info_sharedlibrary_command ()
741 Walk through the shared library list and print information
742 about each attached library.
746 info_sharedlibrary_command (ignore, from_tty)
750 register struct so_list *so = NULL; /* link map state variable */
753 if (exec_bfd == NULL)
755 printf_unfiltered ("No exec file.\n");
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;
767 printf_unfiltered("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
768 "Shared Object Library");
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");
792 solib_address -- check to see if an address is in a shared lib
796 char *solib_address (CORE_ADDR address)
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
807 For example, this routine is called at one point to disable
808 breakpoints which are in shared libraries that are not currently
813 solib_address (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);
830 /* Called by free_all_symtabs */
835 struct so_list *next;
838 disable_breakpoints_in_shlibs (1);
842 if (so_list_head -> sections)
844 free ((PTR)so_list_head -> sections);
846 if (so_list_head -> abfd)
848 bfd_filename = bfd_get_filename (so_list_head -> abfd);
849 if (!bfd_close (so_list_head -> abfd))
850 warning ("cannot close \"%s\": %s",
851 bfd_filename, bfd_errmsg (bfd_get_error ()));
854 /* This happens for the executable on SVR4. */
857 next = so_list_head -> next;
859 free ((PTR)bfd_filename);
860 free ((PTR)so_list_head);
869 solib_create_inferior_hook -- shared library startup support
873 void solib_create_inferior_hook()
877 When gdb starts up the inferior, it nurses it along (through the
878 shell) until it is ready to execute it's first instruction. At this
879 point, this function gets called via expansion of the macro
880 SOLIB_CREATE_INFERIOR_HOOK.
881 For a statically bound executable, this first instruction is the
882 one at "_start", or a similar text label. No further processing is
884 For a dynamically bound executable, this first instruction is somewhere
885 in the rld, and the actual user executable is not yet mapped in.
886 We continue the inferior again, rld then maps in the actual user
887 executable and any needed shared libraries and then sends
889 At that point we discover the names of all shared libraries and
890 read their symbols in.
894 This code does not properly handle hitting breakpoints which the
895 user might have set in the rld itself. Proper handling would have
896 to check if the SIGTRAP happened due to a kill call.
898 Also, what if child has exit()ed? Must exit loop somehow.
902 solib_create_inferior_hook()
905 /* Nothing to do for statically bound executables. */
907 if (symfile_objfile == NULL
908 || symfile_objfile->obfd == NULL
909 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
912 /* Now run the target. It will eventually get a SIGTRAP, at
913 which point all of the libraries will have been mapped in and we
914 can go groveling around in the rld structures to find
915 out what we need to know about them. */
917 clear_proceed_status ();
918 stop_soon_quietly = 1;
919 stop_signal = TARGET_SIGNAL_0;
922 target_resume (-1, 0, stop_signal);
923 wait_for_inferior ();
925 while (stop_signal != TARGET_SIGNAL_TRAP);
927 /* solib_add will call reinit_frame_cache.
928 But we are stopped in the runtime loader and we do not have symbols
929 for the runtime loader. So heuristic_proc_start will be called
930 and will put out an annoying warning.
931 Delaying the resetting of stop_soon_quietly until after symbol loading
932 suppresses the warning. */
934 solib_add ((char *) 0, 0, (struct target_ops *) 0);
935 stop_soon_quietly = 0;
943 sharedlibrary_command -- handle command to explicitly add library
947 static void sharedlibrary_command (char *args, int from_tty)
954 sharedlibrary_command (args, from_tty)
959 solib_add (args, from_tty, (struct target_ops *) 0);
965 add_com ("sharedlibrary", class_files, sharedlibrary_command,
966 "Load shared object library symbols for files matching REGEXP.");
967 add_info ("sharedlibrary", info_sharedlibrary_command,
968 "Status of loaded shared object libraries.");
971 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
972 (char *) &auto_solib_add,
973 "Set autoloading of shared library symbols.\n\
974 If nonzero, symbols from all shared object libraries will be loaded\n\
975 automatically when the inferior begins execution or when the dynamic linker\n\
976 informs gdb that a new library has been loaded. Otherwise, symbols\n\
977 must be loaded manually, using `sharedlibrary'.",