1 /* Handle HP SOM shared libraries for GDB, the GNU Debugger.
3 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
4 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Written by the Center for Software Science at the Univerity of Utah
24 and by Cygnus Support. */
35 #include "breakpoint.h"
39 #include "gdb-stabs.h"
44 #include "gdb_assert.h"
52 /* Uncomment this to turn on some debugging output.
55 /* #define SOLIB_DEBUG
58 /* Defined in exec.c; used to prevent dangling pointer bug.
60 extern struct target_ops exec_ops;
62 /* This lives in hppa-tdep.c. */
63 extern struct unwind_table_entry *find_unwind_entry (CORE_ADDR pc);
65 /* These ought to be defined in some public interface, but aren't. They
66 define the meaning of the various bits in the distinguished __dld_flags
67 variable that is declared in every debuggable a.out on HP-UX, and that
68 is shared between the debugger and the dynamic linker.
70 #define DLD_FLAGS_MAPPRIVATE 0x1
71 #define DLD_FLAGS_HOOKVALID 0x2
72 #define DLD_FLAGS_LISTVALID 0x4
73 #define DLD_FLAGS_BOR_ENABLE 0x8
77 * Most of this code should work for hp300 shared libraries. Does
78 anyone care enough to weed out any SOM-isms.
80 * Support for hpux8 dynamic linker. */
82 /* The basic structure which describes a dynamically loaded object. This
83 data structure is private to the dynamic linker and isn't found in
84 any HPUX include file. */
86 struct som_solib_mapped_entry
88 /* The name of the library. */
91 /* Version of this structure (it is expected to change again in hpux10). */
92 unsigned char struct_version;
94 /* Binding mode for this library. */
95 unsigned char bind_mode;
97 /* Version of this library. */
98 short library_version;
100 /* Start of text address,
101 * link-time text location (length of text area),
102 * end of text address. */
104 CORE_ADDR text_link_addr;
107 /* Start of data, start of bss and end of data. */
108 CORE_ADDR data_start;
112 /* Value of linkage pointer (%r19). */
116 struct som_solib_mapped_entry *next;
118 /* There are other fields, but I don't have information as to what is
119 contained in them. */
121 /* For versions from HPUX-10.30 and up */
123 /* Address in target of offset from thread-local register of
124 * start of this thread's data. I.e., the first thread-local
125 * variable in this shared library starts at *(tsd_start_addr)
126 * from that area pointed to by cr27 (mpsfu_hi).
128 * We do the indirection as soon as we read it, so from then
129 * on it's the offset itself.
131 CORE_ADDR tsd_start_addr;
133 /* Following this are longwords holding:
135 * ?, ?, ?, ptr to -1, ptr to-1, ptr to lib name (leaf name),
136 * ptr to __data_start, ptr to __data_end
142 /* A structure to keep track of all the known shared objects. */
145 struct som_solib_mapped_entry som_solib;
146 struct objfile *objfile;
148 struct section_table *sections;
149 struct section_table *sections_end;
150 /* elz: added this field to store the address in target space (in the
151 library) of the library descriptor (handle) which we read into
152 som_solib_mapped_entry structure */
153 CORE_ADDR solib_addr;
154 struct so_list *next;
158 static struct so_list *so_list_head;
161 /* This is the cumulative size in bytes of the symbol tables of all
162 shared objects on the so_list_head list. (When we say size, here
163 we mean of the information before it is brought into memory and
164 potentially expanded by GDB.) When adding a new shlib, this value
165 is compared against the threshold size, held by auto_solib_limit
166 (in megabytes). If adding symbols for the new shlib would cause
167 the total size to exceed the threshold, then the new shlib's
168 symbols are not loaded. */
169 static LONGEST som_solib_total_st_size;
171 /* When the threshold is reached for any shlib, we refuse to add
172 symbols for subsequent shlibs, even if those shlibs' symbols would
173 be small enough to fit under the threshold. (Although this may
174 result in one, early large shlib preventing the loading of later,
175 smalller shlibs' symbols, it allows us to issue one informational
176 message. The alternative, to issue a message for each shlib whose
177 symbols aren't loaded, could be a big annoyance where the threshold
178 is exceeded due to a very large number of shlibs.)
180 static int som_solib_st_size_threshold_exceeded;
182 /* These addresses should be filled in by som_solib_create_inferior_hook.
183 They are also used elsewhere in this module.
188 struct unwind_table_entry *unwind;
192 /* When adding fields, be sure to clear them in _initialize_som_solib. */
196 addr_and_unwind_t hook;
197 addr_and_unwind_t hook_stub;
198 addr_and_unwind_t load;
199 addr_and_unwind_t load_stub;
200 addr_and_unwind_t unload;
201 addr_and_unwind_t unload2;
202 addr_and_unwind_t unload_stub;
208 static void som_sharedlibrary_info_command (char *, int);
210 static void som_solib_sharedlibrary_command (char *, int);
213 som_solib_sizeof_symbol_table (char *filename)
218 LONGEST st_size = (LONGEST) 0;
221 /* We believe that filename was handed to us by the dynamic linker, and
222 is therefore always an absolute path.
224 desc = openp (getenv ("PATH"), 1, filename, O_RDONLY | O_BINARY, 0, &absolute_name);
227 perror_with_name (filename);
229 filename = absolute_name;
231 abfd = bfd_fdopenr (filename, gnutarget, desc);
235 make_cleanup (xfree, filename);
236 error ("\"%s\": can't open to read symbols: %s.", filename,
237 bfd_errmsg (bfd_get_error ()));
240 if (!bfd_check_format (abfd, bfd_object)) /* Reads in section info */
242 bfd_close (abfd); /* This also closes desc */
243 make_cleanup (xfree, filename);
244 error ("\"%s\": can't read symbols: %s.", filename,
245 bfd_errmsg (bfd_get_error ()));
248 /* Sum the sizes of the various sections that compose debug info. */
250 /* This contains non-DOC information. */
251 sect = bfd_get_section_by_name (abfd, "$DEBUG$");
253 st_size += (LONGEST) bfd_section_size (abfd, sect);
255 /* This contains DOC information. */
256 sect = bfd_get_section_by_name (abfd, "$PINFO$");
258 st_size += (LONGEST) bfd_section_size (abfd, sect);
260 bfd_close (abfd); /* This also closes desc */
263 /* Unfortunately, just summing the sizes of various debug info
264 sections isn't a very accurate measurement of how much heap
265 space the debugger will need to hold them. It also doesn't
266 account for space needed by linker (aka "minimal") symbols.
268 Anecdotal evidence suggests that just summing the sizes of
269 debug-info-related sections understates the heap space needed
270 to represent it internally by about an order of magnitude.
272 Since it's not exactly brain surgery we're doing here, rather
273 than attempt to more accurately measure the size of a shlib's
274 symbol table in GDB's heap, we'll just apply a 10x fudge-
275 factor to the debug info sections' size-sum. No, this doesn't
276 account for minimal symbols in non-debuggable shlibs. But it
277 all roughly washes out in the end.
279 return st_size * (LONGEST) 10;
284 som_solib_add_solib_objfile (struct so_list *so, char *name, int from_tty,
287 obj_private_data_t *obj_private;
288 struct obj_section *s;
290 so->objfile = symbol_file_add (name, from_tty, NULL, 0, OBJF_SHARED);
291 so->abfd = so->objfile->obfd;
293 /* syms_from_objfile has bizarre section offset code,
294 so I do my own right here. */
295 for (s = so->objfile->sections; s < so->objfile->sections_end; s++)
297 flagword aflag = bfd_get_section_flags(so->abfd, s->the_bfd_section);
298 if (aflag & SEC_CODE)
300 s->addr += so->som_solib.text_addr - so->som_solib.text_link_addr;
301 s->endaddr += so->som_solib.text_addr - so->som_solib.text_link_addr;
303 else if (aflag & SEC_DATA)
305 s->addr += so->som_solib.data_start;
306 s->endaddr += so->som_solib.data_start;
312 /* Mark this as a shared library and save private data.
314 so->objfile->flags |= OBJF_SHARED;
316 if (so->objfile->obj_private == NULL)
318 obj_private = (obj_private_data_t *)
319 obstack_alloc (&so->objfile->psymbol_obstack,
320 sizeof (obj_private_data_t));
321 obj_private->unwind_info = NULL;
322 obj_private->so_info = NULL;
323 so->objfile->obj_private = obj_private;
326 obj_private = (obj_private_data_t *) so->objfile->obj_private;
327 obj_private->so_info = so;
329 if (!bfd_check_format (so->abfd, bfd_object))
331 error ("\"%s\": not in executable format: %s.",
332 name, bfd_errmsg (bfd_get_error ()));
338 som_solib_load_symbols (struct so_list *so, char *name, int from_tty,
339 CORE_ADDR text_addr, struct target_ops *target)
341 struct section_table *p;
344 CORE_ADDR presumed_data_start;
347 printf ("--Adding symbols for shared library \"%s\"\n", name);
350 som_solib_add_solib_objfile (so, name, from_tty, text_addr);
352 /* Now we need to build a section table for this library since
353 we might be debugging a core file from a dynamically linked
354 executable in which the libraries were not privately mapped. */
355 if (build_section_table (so->abfd,
359 error ("Unable to build section table for shared library\n.");
363 /* Relocate all the sections based on where they got loaded. */
364 for (p = so->sections; p < so->sections_end; p++)
366 if (p->the_bfd_section->flags & SEC_CODE)
368 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
369 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
371 else if (p->the_bfd_section->flags & SEC_DATA)
373 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
374 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
378 /* Now see if we need to map in the text and data for this shared
379 library (for example debugging a core file which does not use
380 private shared libraries.).
382 Carefully peek at the first text address in the library. If the
383 read succeeds, then the libraries were privately mapped and were
384 included in the core dump file.
386 If the peek failed, then the libraries were not privately mapped
387 and are not in the core file, we'll have to read them in ourselves. */
388 status = target_read_memory (text_addr, buf, 4);
393 new = so->sections_end - so->sections;
395 old = target_resize_to_sections (target, new);
397 /* Copy over the old data before it gets clobbered. */
398 memcpy ((char *) (target->to_sections + old),
400 ((sizeof (struct section_table)) * new));
405 /* FIXME: cagney/2003-02-01: This just isn't right. Given an address
406 within the target's address space, this converts the value into an
407 address within the host's (i.e., GDB's) address space. Given that
408 the host/target address spaces are separate, this can't be right. */
411 hpux_address_to_host_pointer_hack (CORE_ADDR addr)
415 gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
416 ADDRESS_TO_POINTER (builtin_type_void_data_ptr, &ptr, addr);
420 /* Add symbols from shared libraries into the symtab list, unless the
421 size threshold specified by auto_solib_limit (in megabytes) would
425 som_solib_add (char *arg_string, int from_tty, struct target_ops *target, int readsyms)
427 struct minimal_symbol *msymbol;
428 struct so_list *so_list_tail;
430 asection *shlib_info;
432 unsigned int dld_flags;
433 char buf[4], *re_err;
434 int threshold_warning_given = 0;
436 /* First validate our arguments. */
437 re_err = re_comp (arg_string ? arg_string : ".");
440 error ("Invalid regexp: %s", re_err);
443 /* If we're debugging a core file, or have attached to a running
444 process, then som_solib_create_inferior_hook will not have been
447 We need to first determine if we're dealing with a dynamically
448 linked executable. If not, then return without an error or warning.
450 We also need to examine __dld_flags to determine if the shared library
451 list is valid and to determine if the libraries have been privately
453 if (symfile_objfile == NULL)
456 /* First see if the objfile was dynamically linked. */
457 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
461 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
462 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
465 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
468 error ("Unable to find __dld_flags symbol in object file.\n");
472 addr = SYMBOL_VALUE_ADDRESS (msymbol);
473 /* Read the current contents. */
474 status = target_read_memory (addr, buf, 4);
477 error ("Unable to read __dld_flags\n");
480 dld_flags = extract_unsigned_integer (buf, 4);
482 /* __dld_list may not be valid. If not, then we punt, warning the user if
483 we were called as a result of the add-symfile command.
485 if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
488 error ("__dld_list is not valid according to __dld_flags.\n");
492 /* If the libraries were not mapped private, warn the user. */
493 if ((dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
494 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
496 msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL);
499 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
500 but the data is still available if you know where to look. */
501 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
504 error ("Unable to find dynamic library list.\n");
507 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
510 addr = SYMBOL_VALUE_ADDRESS (msymbol);
512 status = target_read_memory (addr, buf, 4);
515 error ("Unable to find dynamic library list.\n");
519 addr = extract_unsigned_integer (buf, 4);
521 /* If addr is zero, then we're using an old dynamic loader which
522 doesn't maintain __dld_list. We'll have to use a completely
523 different approach to get shared library information. */
527 /* Using the information in __dld_list is the preferred method
528 to get at shared library information. It doesn't depend on
529 any functions in /opt/langtools/lib/end.o and has a chance of working
530 with hpux10 when it is released. */
531 status = target_read_memory (addr, buf, 4);
534 error ("Unable to find dynamic library list.\n");
538 /* addr now holds the address of the first entry in the dynamic
540 addr = extract_unsigned_integer (buf, 4);
542 /* Now that we have a pointer to the dynamic library list, walk
543 through it and add the symbols for each library. */
545 so_list_tail = so_list_head;
546 /* Find the end of the list of shared objects. */
547 while (so_list_tail && so_list_tail->next)
548 so_list_tail = so_list_tail->next;
551 printf ("--About to read shared library list data\n");
554 /* "addr" will always point to the base of the
555 * current data entry describing the current
560 CORE_ADDR name_addr, text_addr;
561 unsigned int name_len;
563 struct so_list *new_so;
564 struct so_list *so_list = so_list_head;
572 /* Get a pointer to the name of this library. */
573 status = target_read_memory (addr, buf, 4);
577 name_addr = extract_unsigned_integer (buf, 4);
581 target_read_memory (name_addr + name_len, buf, 1);
589 name = alloca (name_len);
590 status = target_read_memory (name_addr, name, name_len);
594 /* See if we've already loaded something with this name. */
597 if (!strcmp (so_list->som_solib.name, name))
599 so_list = so_list->next;
602 /* See if the file exists. If not, give a warning, but don't
604 status = stat (name, &statbuf);
607 warning ("Can't find file %s referenced in dld_list.", name);
609 status = target_read_memory (addr + 36, buf, 4);
613 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
617 /* If we've already loaded this one or it's the main program, skip it. */
618 is_main_program = (strcmp (name, symfile_objfile->name) == 0);
619 if (so_list || is_main_program)
621 /* This is the "next" pointer in the strcuture.
623 status = target_read_memory (addr + 36, buf, 4);
627 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
629 /* Record the main program's symbol table size. */
630 if (is_main_program && !so_list)
632 st_size = som_solib_sizeof_symbol_table (name);
633 som_solib_total_st_size += st_size;
636 /* Was this a shlib that we noted but didn't load the symbols for?
637 If so, were we invoked this time from the command-line, via
638 a 'sharedlibrary' or 'add-symbol-file' command? If yes to
639 both, we'd better load the symbols this time.
641 if (from_tty && so_list && !is_main_program && (so_list->objfile == NULL))
642 som_solib_load_symbols (so_list,
645 so_list->som_solib.text_addr,
651 name = obsavestring (name, name_len - 1,
652 &symfile_objfile->symbol_obstack);
654 status = target_read_memory (addr + 8, buf, 4);
658 text_addr = extract_unsigned_integer (buf, 4);
660 new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
661 memset ((char *) new_so, 0, sizeof (struct so_list));
662 if (so_list_head == NULL)
664 so_list_head = new_so;
665 so_list_tail = new_so;
669 so_list_tail->next = new_so;
670 so_list_tail = new_so;
673 /* Fill in all the entries in GDB's shared library list.
676 new_so->solib_addr = addr;
677 new_so->som_solib.name = name;
678 status = target_read_memory (addr + 4, buf, 4);
682 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
683 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
684 /* Following is "high water mark", highest version number
685 * seen, rather than plain version number.
687 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
688 new_so->som_solib.text_addr = text_addr;
690 /* Q: What about longword at "addr + 8"?
691 * A: It's read above, out of order, into "text_addr".
694 status = target_read_memory (addr + 12, buf, 4);
698 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
700 status = target_read_memory (addr + 16, buf, 4);
704 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
706 status = target_read_memory (addr + 20, buf, 4);
710 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
712 status = target_read_memory (addr + 24, buf, 4);
716 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
718 status = target_read_memory (addr + 28, buf, 4);
722 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
724 status = target_read_memory (addr + 32, buf, 4);
728 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
730 status = target_read_memory (addr + 36, buf, 4);
734 /* FIXME: cagney/2003-02-01: I think som_solib.next should be a
736 new_so->som_solib.next =
737 hpux_address_to_host_pointer_hack (extract_unsigned_integer (buf, 4));
739 /* Note that we don't re-set "addr" to the next pointer
740 * until after we've read the trailing data.
743 status = target_read_memory (addr + 40, buf, 4);
744 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
748 /* Now indirect via that value!
750 status = target_read_memory (new_so->som_solib.tsd_start_addr, buf, 4);
751 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
755 printf ("\n+ library \"%s\" is described at 0x%x\n", name, addr);
756 printf (" 'version' is %d\n", new_so->som_solib.struct_version);
757 printf (" 'bind_mode' is %d\n", new_so->som_solib.bind_mode);
758 printf (" 'library_version' is %d\n", new_so->som_solib.library_version);
759 printf (" 'text_addr' is 0x%x\n", new_so->som_solib.text_addr);
760 printf (" 'text_link_addr' is 0x%x\n", new_so->som_solib.text_link_addr);
761 printf (" 'text_end' is 0x%x\n", new_so->som_solib.text_end);
762 printf (" 'data_start' is 0x%x\n", new_so->som_solib.data_start);
763 printf (" 'bss_start' is 0x%x\n", new_so->som_solib.bss_start);
764 printf (" 'data_end' is 0x%x\n", new_so->som_solib.data_end);
765 printf (" 'got_value' is %x\n", new_so->som_solib.got_value);
766 printf (" 'next' is 0x%x\n", new_so->som_solib.next);
767 printf (" 'tsd_start_addr' is 0x%x\n", new_so->som_solib.tsd_start_addr);
770 /* Go on to the next shared library descriptor.
772 addr = (CORE_ADDR) new_so->som_solib.next;
776 /* At this point, we have essentially hooked the shlib into the
777 "info share" command. However, we haven't yet loaded its
778 symbol table. We must now decide whether we ought to, i.e.,
779 whether doing so would exceed the symbol table size threshold.
781 If the threshold has just now been exceeded, then we'll issue
782 a warning message (which explains how to load symbols manually,
783 if the user so desires).
785 If the threshold has just now or previously been exceeded,
786 we'll just add the shlib to the list of object files, but won't
787 actually load its symbols. (This is more useful than it might
788 sound, for it allows us to e.g., still load and use the shlibs'
789 unwind information for stack tracebacks.)
792 /* Note that we DON'T want to preclude the user from using the
793 add-symbol-file command! Thus, we only worry about the threshold
794 when we're invoked for other reasons.
796 st_size = som_solib_sizeof_symbol_table (name);
797 som_solib_st_size_threshold_exceeded =
799 auto_solib_limit > 0 &&
801 ((st_size + som_solib_total_st_size) > (auto_solib_limit * (LONGEST) (1024 * 1024)));
803 if (som_solib_st_size_threshold_exceeded)
805 if (!threshold_warning_given)
806 warning ("Symbols for some libraries have not been loaded, because\ndoing so would exceed the size threshold specified by auto-solib-limit.\nTo manually load symbols, use the 'sharedlibrary' command.\nTo raise the threshold, set auto-solib-limit to a larger value and rerun\nthe program.\n");
807 threshold_warning_given = 1;
809 /* We'll still make note of this shlib, even if we don't
810 read its symbols. This allows us to use its unwind
811 information well enough to know how to e.g., correctly
812 do a traceback from a PC within the shlib, even if we
813 can't symbolize those PCs...
815 som_solib_add_solib_objfile (new_so, name, from_tty, text_addr);
819 som_solib_total_st_size += st_size;
821 /* This fills in new_so->objfile, among others. */
822 som_solib_load_symbols (new_so, name, from_tty, text_addr, target);
826 printf ("--Done reading shared library data\n");
829 /* Getting new symbols may change our opinion about what is
831 reinit_frame_cache ();
835 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
839 error ("Error while reading dynamic library list.\n");
844 /* This hook gets called just before the first instruction in the
845 inferior process is executed.
847 This is our opportunity to set magic flags in the inferior so
848 that GDB can be notified when a shared library is mapped in and
849 to tell the dynamic linker that a private copy of the library is
850 needed (so GDB can set breakpoints in the library).
852 __dld_flags is the location of the magic flags; as of this implementation
853 there are 3 flags of interest:
855 bit 0 when set indicates that private copies of the libraries are needed
856 bit 1 when set indicates that the callback hook routine is valid
857 bit 2 when set indicates that the dynamic linker should maintain the
858 __dld_list structure when loading/unloading libraries.
860 Note that shared libraries are not mapped in at this time, so we have
861 run the inferior until the libraries are mapped in. Typically this
862 means running until the "_start" is called. */
865 som_solib_create_inferior_hook (void)
867 struct minimal_symbol *msymbol;
868 unsigned int dld_flags, status, have_endo;
869 asection *shlib_info;
871 struct objfile *objfile;
874 /* First, remove all the solib event breakpoints. Their addresses
875 may have changed since the last time we ran the program. */
876 remove_solib_event_breakpoints ();
878 if (symfile_objfile == NULL)
881 /* First see if the objfile was dynamically linked. */
882 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
886 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
887 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
891 /* Slam the pid of the process into __d_pid.
893 We used to warn when this failed, but that warning is only useful
894 on very old HP systems (hpux9 and older). The warnings are an
895 annoyance to users of modern systems and foul up the testsuite as
896 well. As a result, the warnings have been disabled. */
897 msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
901 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
902 store_unsigned_integer (buf, 4, PIDGET (inferior_ptid));
903 status = target_write_memory (anaddr, buf, 4);
906 warning ("Unable to write __d_pid");
907 warning ("Suggest linking with /opt/langtools/lib/end.o.");
908 warning ("GDB will be unable to track shl_load/shl_unload calls");
912 /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
913 This will force the dynamic linker to call __d_trap when significant
916 Note that the above is the pre-HP-UX 9.0 behaviour. At 9.0 and above,
917 the dld provides an export stub named "__d_trap" as well as the
918 function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
919 We'll look first for the old flavor and then the new.
921 msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
923 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
926 warning ("Unable to find _DLD_HOOK symbol in object file.");
927 warning ("Suggest linking with /opt/langtools/lib/end.o.");
928 warning ("GDB will be unable to track shl_load/shl_unload calls");
931 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
932 dld_cache.hook.address = anaddr;
934 /* Grrr, this might not be an export symbol! We have to find the
936 ALL_OBJFILES (objfile)
938 struct unwind_table_entry *u;
939 struct minimal_symbol *msymbol2;
942 msymbol2 = lookup_minimal_symbol_solib_trampoline (DEPRECATED_SYMBOL_NAME (msymbol),
944 /* Found a symbol with the right name. */
947 struct unwind_table_entry *u;
948 /* It must be a shared library trampoline. */
949 if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
952 /* It must also be an export stub. */
953 u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
954 if (!u || u->stub_unwind.stub_type != EXPORT)
957 /* OK. Looks like the correct import stub. */
958 anaddr = SYMBOL_VALUE (msymbol2);
959 dld_cache.hook_stub.address = anaddr;
962 store_unsigned_integer (buf, 4, anaddr);
964 msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
967 warning ("Unable to find __dld_hook symbol in object file.");
968 warning ("Suggest linking with /opt/langtools/lib/end.o.");
969 warning ("GDB will be unable to track shl_load/shl_unload calls");
972 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
973 status = target_write_memory (anaddr, buf, 4);
975 /* Now set a shlib_event breakpoint at __d_trap so we can track
976 significant shared library events. */
977 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
980 warning ("Unable to find __dld_d_trap symbol in object file.");
981 warning ("Suggest linking with /opt/langtools/lib/end.o.");
982 warning ("GDB will be unable to track shl_load/shl_unload calls");
985 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
987 /* We have all the support usually found in end.o, so we can track
988 shl_load and shl_unload calls. */
993 /* Get the address of __dld_flags, if no such symbol exists, then we can
994 not debug the shared code. */
995 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
998 error ("Unable to find __dld_flags symbol in object file.\n");
1001 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
1003 /* Read the current contents. */
1004 status = target_read_memory (anaddr, buf, 4);
1007 error ("Unable to read __dld_flags\n");
1009 dld_flags = extract_unsigned_integer (buf, 4);
1011 /* Turn on the flags we care about. */
1012 dld_flags |= DLD_FLAGS_MAPPRIVATE;
1014 dld_flags |= DLD_FLAGS_HOOKVALID;
1015 store_unsigned_integer (buf, 4, dld_flags);
1016 status = target_write_memory (anaddr, buf, 4);
1019 error ("Unable to write __dld_flags\n");
1022 /* Now find the address of _start and set a breakpoint there.
1023 We still need this code for two reasons:
1025 * Not all sites have /opt/langtools/lib/end.o, so it's not always
1026 possible to track the dynamic linker's events.
1028 * At this time no events are triggered for shared libraries
1029 loaded at startup time (what a crock). */
1031 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1032 if (msymbol == NULL)
1034 error ("Unable to find _start symbol in object file.\n");
1037 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
1039 /* Make the breakpoint at "_start" a shared library event breakpoint. */
1040 create_solib_event_breakpoint (anaddr);
1042 /* Wipe out all knowledge of old shared libraries since their
1043 mapping can change from one exec to another! */
1044 while (so_list_head)
1046 struct so_list *temp;
1048 temp = so_list_head;
1049 xfree (so_list_head);
1050 so_list_head = temp->next;
1052 clear_symtab_users ();
1055 /* This operation removes the "hook" between GDB and the dynamic linker,
1056 which causes the dld to notify GDB of shared library events.
1058 After this operation completes, the dld will no longer notify GDB of
1059 shared library events. To resume notifications, GDB must call
1060 som_solib_create_inferior_hook.
1062 This operation does not remove any knowledge of shared libraries which
1063 GDB may already have been notified of.
1066 som_solib_remove_inferior_hook (int pid)
1069 struct minimal_symbol *msymbol;
1071 char dld_flags_buffer[TARGET_INT_BIT / TARGET_CHAR_BIT];
1072 unsigned int dld_flags_value;
1073 struct cleanup *old_cleanups = save_inferior_ptid ();
1075 /* Ensure that we're really operating on the specified process. */
1076 inferior_ptid = pid_to_ptid (pid);
1078 /* We won't bother to remove the solib breakpoints from this process.
1080 In fact, on PA64 the breakpoint is hard-coded into the dld callback,
1081 and thus we're not supposed to remove it.
1083 Rather, we'll merely clear the dld_flags bit that enables callbacks.
1085 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
1087 addr = SYMBOL_VALUE_ADDRESS (msymbol);
1088 status = target_read_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT);
1090 dld_flags_value = extract_unsigned_integer (dld_flags_buffer,
1091 sizeof (dld_flags_value));
1093 dld_flags_value &= ~DLD_FLAGS_HOOKVALID;
1094 store_unsigned_integer (dld_flags_buffer,
1095 sizeof (dld_flags_value),
1097 status = target_write_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT);
1099 do_cleanups (old_cleanups);
1103 /* This function creates a breakpoint on the dynamic linker hook, which
1104 is called when e.g., a shl_load or shl_unload call is made. This
1105 breakpoint will only trigger when a shl_load call is made.
1107 If filename is NULL, then loads of any dll will be caught. Else,
1108 only loads of the file whose pathname is the string contained by
1109 filename will be caught.
1111 Undefined behaviour is guaranteed if this function is called before
1112 som_solib_create_inferior_hook.
1115 som_solib_create_catch_load_hook (int pid, int tempflag, char *filename,
1118 create_solib_load_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1121 /* This function creates a breakpoint on the dynamic linker hook, which
1122 is called when e.g., a shl_load or shl_unload call is made. This
1123 breakpoint will only trigger when a shl_unload call is made.
1125 If filename is NULL, then unloads of any dll will be caught. Else,
1126 only unloads of the file whose pathname is the string contained by
1127 filename will be caught.
1129 Undefined behaviour is guaranteed if this function is called before
1130 som_solib_create_inferior_hook.
1133 som_solib_create_catch_unload_hook (int pid, int tempflag, char *filename,
1136 create_solib_unload_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1140 som_solib_have_load_event (int pid)
1142 CORE_ADDR event_kind;
1144 event_kind = read_register (ARG0_REGNUM);
1145 return (event_kind == SHL_LOAD);
1149 som_solib_have_unload_event (int pid)
1151 CORE_ADDR event_kind;
1153 event_kind = read_register (ARG0_REGNUM);
1154 return (event_kind == SHL_UNLOAD);
1158 som_solib_library_pathname (int pid)
1160 CORE_ADDR dll_handle_address;
1161 CORE_ADDR dll_pathname_address;
1162 struct som_solib_mapped_entry dll_descriptor;
1164 static char dll_pathname[1024];
1166 /* Read the descriptor of this newly-loaded library. */
1167 dll_handle_address = read_register (ARG1_REGNUM);
1168 read_memory (dll_handle_address, (char *) &dll_descriptor, sizeof (dll_descriptor));
1170 /* We can find a pointer to the dll's pathname within the descriptor. */
1171 dll_pathname_address = (CORE_ADDR) dll_descriptor.name;
1173 /* Read the pathname, one byte at a time. */
1178 read_memory (dll_pathname_address++, (char *) &b, 1);
1184 return dll_pathname;
1188 som_solib_loaded_library_pathname (int pid)
1190 if (!som_solib_have_load_event (pid))
1191 error ("Must have a load event to use this query");
1193 return som_solib_library_pathname (pid);
1197 som_solib_unloaded_library_pathname (int pid)
1199 if (!som_solib_have_unload_event (pid))
1200 error ("Must have an unload event to use this query");
1202 return som_solib_library_pathname (pid);
1206 som_solib_desire_dynamic_linker_symbols (void)
1208 struct objfile *objfile;
1209 struct unwind_table_entry *u;
1210 struct minimal_symbol *dld_msymbol;
1212 /* Do we already know the value of these symbols? If so, then
1213 we've no work to do.
1215 (If you add clauses to this test, be sure to likewise update the
1216 test within the loop.)
1218 if (dld_cache.is_valid)
1221 ALL_OBJFILES (objfile)
1223 dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
1224 if (dld_msymbol != NULL)
1226 dld_cache.load.address = SYMBOL_VALUE (dld_msymbol);
1227 dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
1230 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
1233 if (dld_msymbol != NULL)
1235 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1237 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1238 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1240 dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol);
1241 dld_cache.load_stub.unwind = u;
1246 dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
1247 if (dld_msymbol != NULL)
1249 dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol);
1250 dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);
1252 /* ??rehrauer: I'm not sure exactly what this is, but it appears
1253 that on some HPUX 10.x versions, there's two unwind regions to
1254 cover the body of "shl_unload", the second being 4 bytes past
1255 the end of the first. This is a large hack to handle that
1256 case, but since I don't seem to have any legitimate way to
1257 look for this thing via the symbol table...
1259 if (dld_cache.unload.unwind != NULL)
1261 u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
1264 dld_cache.unload2.address = u->region_start;
1265 dld_cache.unload2.unwind = u;
1270 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
1273 if (dld_msymbol != NULL)
1275 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1277 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1278 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1280 dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol);
1281 dld_cache.unload_stub.unwind = u;
1286 /* Did we find everything we were looking for? If so, stop. */
1287 if ((dld_cache.load.address != 0)
1288 && (dld_cache.load_stub.address != 0)
1289 && (dld_cache.unload.address != 0)
1290 && (dld_cache.unload_stub.address != 0))
1292 dld_cache.is_valid = 1;
1297 dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
1298 dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);
1300 /* We're prepared not to find some of these symbols, which is why
1301 this function is a "desire" operation, and not a "require".
1306 som_solib_in_dynamic_linker (int pid, CORE_ADDR pc)
1308 struct unwind_table_entry *u_pc;
1310 /* Are we in the dld itself?
1312 ??rehrauer: Large hack -- We'll assume that any address in a
1313 shared text region is the dld's text. This would obviously
1314 fall down if the user attached to a process, whose shlibs
1315 weren't mapped to a (writeable) private region. However, in
1316 that case the debugger probably isn't able to set the fundamental
1317 breakpoint in the dld callback anyways, so this hack should be
1320 if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
1323 /* Cache the address of some symbols that are part of the dynamic
1324 linker, if not already known.
1326 som_solib_desire_dynamic_linker_symbols ();
1328 /* Are we in the dld callback? Or its export stub? */
1329 u_pc = find_unwind_entry (pc);
1333 if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
1336 /* Or the interface of the dld (i.e., "shl_load" or friends)? */
1337 if ((u_pc == dld_cache.load.unwind)
1338 || (u_pc == dld_cache.unload.unwind)
1339 || (u_pc == dld_cache.unload2.unwind)
1340 || (u_pc == dld_cache.load_stub.unwind)
1341 || (u_pc == dld_cache.unload_stub.unwind))
1344 /* Apparently this address isn't part of the dld's text. */
1349 /* Return the GOT value for the shared library in which ADDR belongs. If
1350 ADDR isn't in any known shared library, return zero. */
1353 som_solib_get_got_by_pc (CORE_ADDR addr)
1355 struct so_list *so_list = so_list_head;
1356 CORE_ADDR got_value = 0;
1360 if (so_list->som_solib.text_addr <= addr
1361 && so_list->som_solib.text_end > addr)
1363 got_value = so_list->som_solib.got_value;
1366 so_list = so_list->next;
1372 Return the address of the handle of the shared library
1373 in which ADDR belongs. If
1374 ADDR isn't in any known shared library, return zero. */
1375 /* this function is used in hppa_fix_call_dummy in hppa-tdep.c */
1378 som_solib_get_solib_by_pc (CORE_ADDR addr)
1380 struct so_list *so_list = so_list_head;
1384 if (so_list->som_solib.text_addr <= addr
1385 && so_list->som_solib.text_end > addr)
1389 so_list = so_list->next;
1392 return so_list->solib_addr;
1399 som_solib_section_offsets (struct objfile *objfile,
1400 struct section_offsets *offsets)
1402 struct so_list *so_list = so_list_head;
1406 /* Oh what a pain! We need the offsets before so_list->objfile
1407 is valid. The BFDs will never match. Make a best guess. */
1408 if (strstr (objfile->name, so_list->som_solib.name))
1410 asection *private_section;
1412 /* The text offset is easy. */
1413 offsets->offsets[SECT_OFF_TEXT (objfile)]
1414 = (so_list->som_solib.text_addr
1415 - so_list->som_solib.text_link_addr);
1416 offsets->offsets[SECT_OFF_RODATA (objfile)]
1417 = ANOFFSET (offsets, SECT_OFF_TEXT (objfile));
1419 /* We should look at presumed_dp in the SOM header, but
1420 that's not easily available. This should be OK though. */
1421 private_section = bfd_get_section_by_name (objfile->obfd,
1423 if (!private_section)
1425 warning ("Unable to find $PRIVATE$ in shared library!");
1426 offsets->offsets[SECT_OFF_DATA (objfile)] = 0;
1427 offsets->offsets[SECT_OFF_BSS (objfile)] = 0;
1430 offsets->offsets[SECT_OFF_DATA (objfile)]
1431 = (so_list->som_solib.data_start - private_section->vma);
1432 offsets->offsets[SECT_OFF_BSS (objfile)]
1433 = ANOFFSET (offsets, SECT_OFF_DATA (objfile));
1436 so_list = so_list->next;
1441 /* Dump information about all the currently loaded shared libraries. */
1444 som_sharedlibrary_info_command (char *ignore, int from_tty)
1446 struct so_list *so_list = so_list_head;
1448 if (exec_bfd == NULL)
1450 printf_unfiltered ("No executable file.\n");
1454 if (so_list == NULL)
1456 printf_unfiltered ("No shared libraries loaded at this time.\n");
1460 printf_unfiltered ("Shared Object Libraries\n");
1461 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
1462 " flags", " tstart", " tend", " dstart", " dend", " dlt");
1467 flags = so_list->som_solib.struct_version << 24;
1468 flags |= so_list->som_solib.bind_mode << 16;
1469 flags |= so_list->som_solib.library_version;
1470 printf_unfiltered ("%s", so_list->som_solib.name);
1471 if (so_list->objfile == NULL)
1472 printf_unfiltered (" (symbols not loaded)");
1473 printf_unfiltered ("\n");
1474 printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l"));
1475 printf_unfiltered ("%-12s",
1476 local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
1477 printf_unfiltered ("%-12s",
1478 local_hex_string_custom (so_list->som_solib.text_end, "08l"));
1479 printf_unfiltered ("%-12s",
1480 local_hex_string_custom (so_list->som_solib.data_start, "08l"));
1481 printf_unfiltered ("%-12s",
1482 local_hex_string_custom (so_list->som_solib.data_end, "08l"));
1483 printf_unfiltered ("%-12s\n",
1484 local_hex_string_custom (so_list->som_solib.got_value, "08l"));
1485 so_list = so_list->next;
1490 som_solib_sharedlibrary_command (char *args, int from_tty)
1493 som_solib_add (args, from_tty, (struct target_ops *) 0, 1);
1499 som_solib_address (CORE_ADDR addr)
1501 struct so_list *so = so_list_head;
1505 /* Is this address within this shlib's text range? If so,
1506 return the shlib's name.
1508 if ((addr >= so->som_solib.text_addr) && (addr <= so->som_solib.text_end))
1509 return so->som_solib.name;
1511 /* Nope, keep looking... */
1515 /* No, we couldn't prove that the address is within a shlib. */
1521 som_solib_restart (void)
1523 struct so_list *sl = so_list_head;
1525 /* Before the shlib info vanishes, use it to disable any breakpoints
1526 that may still be active in those shlibs.
1528 disable_breakpoints_in_shlibs (0);
1530 /* Discard all the shlib descriptors.
1534 struct so_list *next_sl = sl->next;
1538 so_list_head = NULL;
1540 som_solib_total_st_size = (LONGEST) 0;
1541 som_solib_st_size_threshold_exceeded = 0;
1543 dld_cache.is_valid = 0;
1545 dld_cache.hook.address = 0;
1546 dld_cache.hook.unwind = NULL;
1548 dld_cache.hook_stub.address = 0;
1549 dld_cache.hook_stub.unwind = NULL;
1551 dld_cache.load.address = 0;
1552 dld_cache.load.unwind = NULL;
1554 dld_cache.load_stub.address = 0;
1555 dld_cache.load_stub.unwind = NULL;
1557 dld_cache.unload.address = 0;
1558 dld_cache.unload.unwind = NULL;
1560 dld_cache.unload2.address = 0;
1561 dld_cache.unload2.unwind = NULL;
1563 dld_cache.unload_stub.address = 0;
1564 dld_cache.unload_stub.unwind = NULL;
1570 no_shared_libraries -- handle command to explicitly discard symbols
1571 from shared libraries.
1575 Implements the command "nosharedlibrary", which discards symbols
1576 that have been auto-loaded from shared libraries. Symbols from
1577 shared libraries that were added by explicit request of the user
1578 are not discarded. Also called from remote.c. */
1581 no_shared_libraries (char *ignored, int from_tty)
1588 _initialize_som_solib (void)
1590 add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
1591 "Load shared object library symbols for files matching REGEXP.");
1592 add_info ("sharedlibrary", som_sharedlibrary_info_command,
1593 "Status of loaded shared object libraries.");
1596 (add_set_cmd ("auto-solib-add", class_support, var_boolean,
1597 (char *) &auto_solib_add,
1598 "Set autoloading of shared library symbols.\n\
1599 If \"on\", symbols from all shared object libraries will be loaded\n\
1600 automatically when the inferior begins execution, when the dynamic linker\n\
1601 informs gdb that a new library has been loaded, or when attaching to the\n\
1602 inferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
1607 (add_set_cmd ("auto-solib-limit", class_support, var_zinteger,
1608 (char *) &auto_solib_limit,
1609 "Set threshold (in Mb) for autoloading shared library symbols.\n\
1610 When shared library autoloading is enabled, new libraries will be loaded\n\
1611 only until the total size of shared library symbols exceeds this\n\
1612 threshold in megabytes. Is ignored when using `sharedlibrary'.",
1616 /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how
1617 much data space a process can use. We ought to be reading
1618 MAXDSIZ and setting auto_solib_limit to some large fraction of
1619 that value. If not that, we maybe ought to be setting it smaller
1620 than the default for MAXDSIZ (that being 64Mb, I believe).
1621 However, [1] this threshold is only crudely approximated rather
1622 than actually measured, and [2] 50 Mbytes is too small for
1623 debugging gdb itself. Thus, the arbitrary 100 figure. */
1624 auto_solib_limit = 100; /* Megabytes */
1626 som_solib_restart ();
1629 /* Get some HPUX-specific data from a shared lib.
1632 so_lib_thread_start_addr (struct so_list *so)
1634 return so->som_solib.tsd_start_addr;