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 1024 /* FIXME: Should be dynamic */
44 /* When handling shared libraries, GDB has to find out the pathnames
45 of all shared libraries that are currently loaded (to read in their
46 symbols) and where the shared libraries are loaded in memory
47 (to relocate them properly from their prelinked addresses to the
48 current load address).
50 Under OSF/1 there are two possibilities to get at this information:
51 1) Peek around in the runtime loader structures.
52 These are not documented, and they are not defined in the system
53 header files. The definitions below were obtained by experimentation,
54 but they seem stable enough.
55 2) Use the undocumented libxproc.a library, which contains the
56 equivalent ldr_* routines.
57 This approach is somewhat cleaner, but it requires that the GDB
58 executable is dynamically linked. In addition it requires a
59 NAT_CLIBS= -lxproc -Wl,-expect_unresolved,ldr_process_context
60 linker specification for GDB and all applications that are using
62 We will use the peeking approach until it becomes unwieldy. */
64 #ifndef USE_LDR_ROUTINES
66 /* Definition of runtime loader structures, found by experimentation. */
67 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
75 CORE_ADDR modinfo_addr;
80 CORE_ADDR regioninfo_addr;
86 CORE_ADDR regionname_addr;
102 static ldr_context_t ldr_context;
107 static ldr_process_t fake_ldr_process;
109 /* Called by ldr_* routines to read memory from the current target. */
111 static int ldr_read_memory PARAMS ((CORE_ADDR, char *, int, int));
114 ldr_read_memory (memaddr, myaddr, len, readstring)
125 target_read_string (memaddr, &buffer, len, &result);
127 strcpy (myaddr, buffer);
131 result = target_read_memory (memaddr, myaddr, len);
140 /* Define our own link_map structure.
141 This will help to share code with solib.c. */
144 CORE_ADDR l_offset; /* prelink to load address offset */
145 char *l_name; /* full name of loaded object */
146 ldr_module_info_t module_info; /* corresponding module info */
149 #define LM_OFFSET(so) ((so) -> lm.l_offset)
150 #define LM_NAME(so) ((so) -> lm.l_name)
153 struct so_list *next; /* next structure in linked list */
154 struct link_map lm; /* copy of link map from inferior */
155 struct link_map *lmaddr; /* addr in inferior lm was read from */
156 CORE_ADDR lmend; /* upper addr bound of mapped object */
157 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
158 char symbols_loaded; /* flag: symbols read in yet? */
159 char from_tty; /* flag: print msgs? */
160 struct objfile *objfile; /* objfile for loaded lib */
161 struct section_table *sections;
162 struct section_table *sections_end;
163 struct section_table *textsection;
167 static struct so_list *so_list_head; /* List of known shared objects */
170 fdmatch PARAMS ((int, int)); /* In libiberty */
172 /* Local function prototypes */
175 sharedlibrary_command PARAMS ((char *, int));
178 info_sharedlibrary_command PARAMS ((char *, int));
181 symbol_add_stub PARAMS ((char *));
183 static struct so_list *
184 find_solib PARAMS ((struct so_list *));
186 static struct link_map *
187 first_link_map_member PARAMS ((void));
189 static struct link_map *
190 next_link_map_member PARAMS ((struct so_list *));
193 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
196 solib_map_sections PARAMS ((struct so_list *));
202 solib_map_sections -- open bfd and build sections for shared lib
206 static void solib_map_sections (struct so_list *so)
210 Given a pointer to one of the shared objects in our list
211 of mapped objects, use the recorded name to open a bfd
212 descriptor for the object, build a section table, and then
213 relocate all the section addresses by the base address at
214 which the shared object was mapped.
218 In most (all?) cases the shared object file name recorded in the
219 dynamic linkage tables will be a fully qualified pathname. For
220 cases where it isn't, do we really mimic the systems search
221 mechanism correctly in the below code (particularly the tilde
226 solib_map_sections (so)
230 char *scratch_pathname;
232 struct section_table *p;
233 struct cleanup *old_chain;
236 filename = tilde_expand (so -> so_name);
237 old_chain = make_cleanup (free, filename);
239 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
241 if (scratch_chan < 0)
243 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
244 O_RDONLY, 0, &scratch_pathname);
246 if (scratch_chan < 0)
248 perror_with_name (filename);
250 /* Leave scratch_pathname allocated. bfd->name will point to it. */
252 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
255 close (scratch_chan);
256 error ("Could not open `%s' as an executable file: %s",
257 scratch_pathname, bfd_errmsg (bfd_get_error ()));
259 /* Leave bfd open, core_xfer_memory and "info files" need it. */
261 abfd -> cacheable = true;
263 if (!bfd_check_format (abfd, bfd_object))
265 error ("\"%s\": not in executable format: %s.",
266 scratch_pathname, bfd_errmsg (bfd_get_error ()));
268 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
270 error ("Can't find the file sections in `%s': %s",
271 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
274 for (p = so -> sections; p < so -> sections_end; p++)
276 /* Relocate the section binding addresses as recorded in the shared
277 object's file by the offset to get the address to which the
278 object was actually mapped. */
279 p -> addr += LM_OFFSET (so);
280 p -> endaddr += LM_OFFSET (so);
281 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
282 if (STREQ (p -> the_bfd_section -> name, ".text"))
284 so -> textsection = p;
288 /* Free the file names, close the file now. */
289 do_cleanups (old_chain);
296 first_link_map_member -- locate first member in dynamic linker's map
300 static struct link_map *first_link_map_member (void)
304 Read in a copy of the first member in the inferior's dynamic
305 link map from the inferior's dynamic linker structures, and return
306 a pointer to the copy in our address space.
309 static struct link_map *
310 first_link_map_member ()
312 struct link_map *lm = NULL;
313 static struct link_map first_lm;
315 #ifdef USE_LDR_ROUTINES
316 ldr_module_t mod_id = LDR_NULL_MODULE;
319 fake_ldr_process = ldr_core_process ();
320 ldr_set_core_reader (ldr_read_memory);
321 ldr_xdetach (fake_ldr_process);
322 if (ldr_xattach (fake_ldr_process) != 0
323 || ldr_next_module(fake_ldr_process, &mod_id) != 0
324 || mod_id == LDR_NULL_MODULE
325 || ldr_inq_module(fake_ldr_process, mod_id,
326 &first_lm.module_info, sizeof(ldr_module_info_t),
330 CORE_ADDR ldr_context_addr;
332 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
333 (char *) &ldr_context_addr,
334 sizeof (CORE_ADDR)) != 0
335 || target_read_memory (ldr_context_addr,
336 (char *) &ldr_context,
337 sizeof (ldr_context_t)) != 0
338 || target_read_memory ((CORE_ADDR) ldr_context.head,
339 (char *) &first_lm.module_info,
340 sizeof (ldr_module_info_t)) != 0)
346 /* The first entry is for the main program and should be skipped. */
352 static struct link_map *
353 next_link_map_member (so_list_ptr)
354 struct so_list *so_list_ptr;
356 struct link_map *lm = NULL;
357 static struct link_map next_lm;
358 #ifdef USE_LDR_ROUTINES
359 ldr_module_t mod_id = so_list_ptr->lm.module_info.lmi_modid;
362 if (ldr_next_module(fake_ldr_process, &mod_id) != 0
363 || mod_id == LDR_NULL_MODULE
364 || ldr_inq_module(fake_ldr_process, mod_id,
365 &next_lm.module_info, sizeof(ldr_module_info_t),
370 lm->l_name = lm->module_info.lmi_name;
372 CORE_ADDR ldr_context_addr;
374 /* Reread context in case ldr_context.tail was updated. */
376 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
377 (char *) &ldr_context_addr,
378 sizeof (CORE_ADDR)) != 0
379 || target_read_memory (ldr_context_addr,
380 (char *) &ldr_context,
381 sizeof (ldr_context_t)) != 0
382 || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
383 || target_read_memory (so_list_ptr->lm.module_info.next,
384 (char *) &next_lm.module_info,
385 sizeof (ldr_module_info_t)) != 0)
389 lm->l_name = lm->module_info.module_name;
395 xfer_link_map_member (so_list_ptr, lm)
396 struct so_list *so_list_ptr;
400 so_list_ptr->lm = *lm;
402 /* OSF/1 shared libraries are pre-linked to particular addresses,
403 but the runtime loader may have to relocate them if the
404 address ranges of the libraries used by the target executable clash,
405 or if the target executable is linked with the -taso option.
406 The offset is the difference between the address where the shared
407 library is mapped and the pre-linked address of the shared library.
409 FIXME: GDB is currently unable to relocate the shared library
410 sections by different offsets. If sections are relocated by
411 different offsets, put out a warning and use the offset of the
412 first section for all remaining sections. */
413 LM_OFFSET (so_list_ptr) = 0;
415 /* There is one entry that has no name (for the inferior executable)
416 since it is not a shared object. */
417 if (LM_NAME (so_list_ptr) != 0)
420 #ifdef USE_LDR_ROUTINES
421 int len = strlen (LM_NAME (so_list_ptr) + 1);
423 if (len > MAX_PATH_SIZE)
425 strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
426 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
428 for (i = 0; i < lm->module_info.lmi_nregion; i++)
430 ldr_region_info_t region_info;
432 CORE_ADDR region_offset;
434 if (ldr_inq_region (fake_ldr_process, lm->module_info.lmi_modid,
435 i, ®ion_info, sizeof (region_info),
438 region_offset = (CORE_ADDR) region_info.lri_mapaddr
439 - (CORE_ADDR) region_info.lri_vaddr;
441 LM_OFFSET (so_list_ptr) = region_offset;
442 else if (LM_OFFSET (so_list_ptr) != region_offset)
443 warning ("cannot handle shared library relocation for %s (%s)",
444 so_list_ptr->so_name, region_info.lri_name);
449 target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
450 MAX_PATH_SIZE - 1, &errcode);
452 error ("xfer_link_map_member: Can't read pathname for load map: %s\n",
453 safe_strerror (errcode));
454 strncpy (so_list_ptr->so_name, buffer, MAX_PATH_SIZE - 1);
456 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
458 for (i = 0; i < lm->module_info.region_count; i++)
460 ldr_region_info_t region_info;
461 CORE_ADDR region_offset;
463 if (target_read_memory (lm->module_info.regioninfo_addr
464 + i * sizeof (region_info),
465 (char *) ®ion_info,
466 sizeof (region_info)) != 0)
468 region_offset = region_info.mapaddr - region_info.vaddr;
470 LM_OFFSET (so_list_ptr) = region_offset;
471 else if (LM_OFFSET (so_list_ptr) != region_offset)
474 target_read_string (region_info.regionname_addr, &buffer,
475 MAX_PATH_SIZE - 1, &errcode);
477 region_name = buffer;
480 warning ("cannot handle shared library relocation for %s (%s)",
481 so_list_ptr->so_name, region_name);
487 solib_map_sections (so_list_ptr);
495 find_solib -- step through list of shared objects
499 struct so_list *find_solib (struct so_list *so_list_ptr)
503 This module contains the routine which finds the names of any
504 loaded "images" in the current process. The argument in must be
505 NULL on the first call, and then the returned value must be passed
506 in on subsequent calls. This provides the capability to "step" down
507 the list of loaded objects. On the last object, a NULL value is
510 The arg and return value are "struct link_map" pointers, as defined
514 static struct so_list *
515 find_solib (so_list_ptr)
516 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
518 struct so_list *so_list_next = NULL;
519 struct link_map *lm = NULL;
522 if (so_list_ptr == NULL)
524 /* We are setting up for a new scan through the loaded images. */
525 if ((so_list_next = so_list_head) == NULL)
527 /* Find the first link map list member. */
528 lm = first_link_map_member ();
533 /* We have been called before, and are in the process of walking
534 the shared library list. Advance to the next shared object. */
535 lm = next_link_map_member (so_list_ptr);
536 so_list_next = so_list_ptr -> next;
538 if ((so_list_next == NULL) && (lm != NULL))
540 /* Get next link map structure from inferior image and build a local
541 abbreviated load_map structure */
542 new = (struct so_list *) xmalloc (sizeof (struct so_list));
543 memset ((char *) new, 0, sizeof (struct so_list));
545 /* Add the new node as the next node in the list, or as the root
546 node if this is the first one. */
547 if (so_list_ptr != NULL)
549 so_list_ptr -> next = new;
556 xfer_link_map_member (new, lm);
558 return (so_list_next);
561 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
564 symbol_add_stub (arg)
567 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
569 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
570 so -> textsection -> addr,
579 solib_add -- add a shared library file to the symtab and section list
583 void solib_add (char *arg_string, int from_tty,
584 struct target_ops *target)
591 solib_add (arg_string, from_tty, target)
594 struct target_ops *target;
596 register struct so_list *so = NULL; /* link map state variable */
598 /* Last shared library that we read. */
599 struct so_list *so_last = NULL;
605 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
607 error ("Invalid regexp: %s", re_err);
611 /* Add the shared library sections to the section table of the
612 specified target, if any. */
615 /* Count how many new section_table entries there are. */
618 while ((so = find_solib (so)) != NULL)
620 if (so -> so_name[0])
622 count += so -> sections_end - so -> sections;
628 /* Reallocate the target's section table including the new size. */
629 if (target -> to_sections)
631 old = target -> to_sections_end - target -> to_sections;
632 target -> to_sections = (struct section_table *)
633 xrealloc ((char *)target -> to_sections,
634 (sizeof (struct section_table)) * (count + old));
639 target -> to_sections = (struct section_table *)
640 xmalloc ((sizeof (struct section_table)) * count);
642 target -> to_sections_end = target -> to_sections + (count + old);
644 /* Add these section table entries to the target's table. */
645 while ((so = find_solib (so)) != NULL)
647 if (so -> so_name[0])
649 count = so -> sections_end - so -> sections;
650 memcpy ((char *) (target -> to_sections + old),
652 (sizeof (struct section_table)) * count);
659 /* Now add the symbol files. */
661 while ((so = find_solib (so)) != NULL)
663 if (so -> so_name[0] && re_exec (so -> so_name))
665 so -> from_tty = from_tty;
666 if (so -> symbols_loaded)
670 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
673 else if (catch_errors
674 (symbol_add_stub, (char *) so,
675 "Error while reading shared library symbols:\n",
679 so -> symbols_loaded = 1;
684 /* Getting new symbols may change our opinion about what is
687 reinit_frame_cache ();
694 info_sharedlibrary_command -- code for "info sharedlibrary"
698 static void info_sharedlibrary_command ()
702 Walk through the shared library list and print information
703 about each attached library.
707 info_sharedlibrary_command (ignore, from_tty)
711 register struct so_list *so = NULL; /* link map state variable */
714 if (exec_bfd == NULL)
716 printf_unfiltered ("No exec file.\n");
719 while ((so = find_solib (so)) != NULL)
721 if (so -> so_name[0])
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");
732 if (so -> textsection)
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 int 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 (address)
777 register struct so_list *so = 0; /* link map state variable */
779 while ((so = find_solib (so)) != NULL)
781 if (so -> so_name[0] && so -> textsection)
783 if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
784 (address < (CORE_ADDR) so -> textsection -> endaddr))
793 /* Called by free_all_symtabs */
798 struct so_list *next;
803 if (so_list_head -> sections)
805 free ((PTR)so_list_head -> sections);
807 if (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 free ((PTR)bfd_filename);
821 free ((PTR)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()
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 (-1, 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);
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 (args, from_tty)
919 solib_add (args, from_tty, (struct target_ops *) 0);
926 add_com ("sharedlibrary", class_files, sharedlibrary_command,
927 "Load shared object library symbols for files matching REGEXP.");
928 add_info ("sharedlibrary", info_sharedlibrary_command,
929 "Status of loaded shared object libraries.");