1 /* Handle HP SOM shared libraries for GDB, the GNU Debugger.
3 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002 Free
4 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"
51 /* Uncomment this to turn on some debugging output.
54 /* #define SOLIB_DEBUG
57 /* Defined in exec.c; used to prevent dangling pointer bug.
59 extern struct target_ops exec_ops;
61 /* This lives in hppa-tdep.c. */
62 extern struct unwind_table_entry *find_unwind_entry (CORE_ADDR pc);
64 /* These ought to be defined in some public interface, but aren't. They
65 define the meaning of the various bits in the distinguished __dld_flags
66 variable that is declared in every debuggable a.out on HP-UX, and that
67 is shared between the debugger and the dynamic linker.
69 #define DLD_FLAGS_MAPPRIVATE 0x1
70 #define DLD_FLAGS_HOOKVALID 0x2
71 #define DLD_FLAGS_LISTVALID 0x4
72 #define DLD_FLAGS_BOR_ENABLE 0x8
76 * Most of this code should work for hp300 shared libraries. Does
77 anyone care enough to weed out any SOM-isms.
79 * Support for hpux8 dynamic linker. */
81 /* The basic structure which describes a dynamically loaded object. This
82 data structure is private to the dynamic linker and isn't found in
83 any HPUX include file. */
85 struct som_solib_mapped_entry
87 /* The name of the library. */
90 /* Version of this structure (it is expected to change again in hpux10). */
91 unsigned char struct_version;
93 /* Binding mode for this library. */
94 unsigned char bind_mode;
96 /* Version of this library. */
97 short library_version;
99 /* Start of text address,
100 * link-time text location (length of text area),
101 * end of text address. */
103 CORE_ADDR text_link_addr;
106 /* Start of data, start of bss and end of data. */
107 CORE_ADDR data_start;
111 /* Value of linkage pointer (%r19). */
115 struct som_solib_mapped_entry *next;
117 /* There are other fields, but I don't have information as to what is
118 contained in them. */
120 /* For versions from HPUX-10.30 and up */
122 /* Address in target of offset from thread-local register of
123 * start of this thread's data. I.e., the first thread-local
124 * variable in this shared library starts at *(tsd_start_addr)
125 * from that area pointed to by cr27 (mpsfu_hi).
127 * We do the indirection as soon as we read it, so from then
128 * on it's the offset itself.
130 CORE_ADDR tsd_start_addr;
132 /* Following this are longwords holding:
134 * ?, ?, ?, ptr to -1, ptr to-1, ptr to lib name (leaf name),
135 * ptr to __data_start, ptr to __data_end
141 /* A structure to keep track of all the known shared objects. */
144 struct som_solib_mapped_entry som_solib;
145 struct objfile *objfile;
147 struct section_table *sections;
148 struct section_table *sections_end;
149 /* elz: added this field to store the address in target space (in the
150 library) of the library descriptor (handle) which we read into
151 som_solib_mapped_entry structure */
152 CORE_ADDR solib_addr;
153 struct so_list *next;
157 static struct so_list *so_list_head;
160 /* This is the cumulative size in bytes of the symbol tables of all
161 shared objects on the so_list_head list. (When we say size, here
162 we mean of the information before it is brought into memory and
163 potentially expanded by GDB.) When adding a new shlib, this value
164 is compared against the threshold size, held by auto_solib_limit
165 (in megabytes). If adding symbols for the new shlib would cause
166 the total size to exceed the threshold, then the new shlib's
167 symbols are not loaded. */
168 static LONGEST som_solib_total_st_size;
170 /* When the threshold is reached for any shlib, we refuse to add
171 symbols for subsequent shlibs, even if those shlibs' symbols would
172 be small enough to fit under the threshold. (Although this may
173 result in one, early large shlib preventing the loading of later,
174 smalller shlibs' symbols, it allows us to issue one informational
175 message. The alternative, to issue a message for each shlib whose
176 symbols aren't loaded, could be a big annoyance where the threshold
177 is exceeded due to a very large number of shlibs.)
179 static int som_solib_st_size_threshold_exceeded;
181 /* These addresses should be filled in by som_solib_create_inferior_hook.
182 They are also used elsewhere in this module.
187 struct unwind_table_entry *unwind;
191 /* When adding fields, be sure to clear them in _initialize_som_solib. */
195 addr_and_unwind_t hook;
196 addr_and_unwind_t hook_stub;
197 addr_and_unwind_t load;
198 addr_and_unwind_t load_stub;
199 addr_and_unwind_t unload;
200 addr_and_unwind_t unload2;
201 addr_and_unwind_t unload_stub;
207 static void som_sharedlibrary_info_command (char *, int);
209 static void som_solib_sharedlibrary_command (char *, int);
212 som_solib_sizeof_symbol_table (char *filename)
217 LONGEST st_size = (LONGEST) 0;
220 /* We believe that filename was handed to us by the dynamic linker, and
221 is therefore always an absolute path.
223 desc = openp (getenv ("PATH"), 1, filename, O_RDONLY | O_BINARY, 0, &absolute_name);
226 perror_with_name (filename);
228 filename = absolute_name;
230 abfd = bfd_fdopenr (filename, gnutarget, desc);
234 make_cleanup (xfree, filename);
235 error ("\"%s\": can't open to read symbols: %s.", filename,
236 bfd_errmsg (bfd_get_error ()));
239 if (!bfd_check_format (abfd, bfd_object)) /* Reads in section info */
241 bfd_close (abfd); /* This also closes desc */
242 make_cleanup (xfree, filename);
243 error ("\"%s\": can't read symbols: %s.", filename,
244 bfd_errmsg (bfd_get_error ()));
247 /* Sum the sizes of the various sections that compose debug info. */
249 /* This contains non-DOC information. */
250 sect = bfd_get_section_by_name (abfd, "$DEBUG$");
252 st_size += (LONGEST) bfd_section_size (abfd, sect);
254 /* This contains DOC information. */
255 sect = bfd_get_section_by_name (abfd, "$PINFO$");
257 st_size += (LONGEST) bfd_section_size (abfd, sect);
259 bfd_close (abfd); /* This also closes desc */
262 /* Unfortunately, just summing the sizes of various debug info
263 sections isn't a very accurate measurement of how much heap
264 space the debugger will need to hold them. It also doesn't
265 account for space needed by linker (aka "minimal") symbols.
267 Anecdotal evidence suggests that just summing the sizes of
268 debug-info-related sections understates the heap space needed
269 to represent it internally by about an order of magnitude.
271 Since it's not exactly brain surgery we're doing here, rather
272 than attempt to more accurately measure the size of a shlib's
273 symbol table in GDB's heap, we'll just apply a 10x fudge-
274 factor to the debug info sections' size-sum. No, this doesn't
275 account for minimal symbols in non-debuggable shlibs. But it
276 all roughly washes out in the end.
278 return st_size * (LONGEST) 10;
283 som_solib_add_solib_objfile (struct so_list *so, char *name, int from_tty,
286 obj_private_data_t *obj_private;
287 struct obj_section *s;
289 so->objfile = symbol_file_add (name, from_tty, NULL, 0, OBJF_SHARED);
290 so->abfd = so->objfile->obfd;
292 /* syms_from_objfile has bizarre section offset code,
293 so I do my own right here. */
294 for (s = so->objfile->sections; s < so->objfile->sections_end; s++)
296 flagword aflag = bfd_get_section_flags(so->abfd, s->the_bfd_section);
297 if (aflag & SEC_CODE)
299 s->addr += so->som_solib.text_addr - so->som_solib.text_link_addr;
300 s->endaddr += so->som_solib.text_addr - so->som_solib.text_link_addr;
302 else if (aflag & SEC_DATA)
304 s->addr += so->som_solib.data_start;
305 s->endaddr += so->som_solib.data_start;
311 /* Mark this as a shared library and save private data.
313 so->objfile->flags |= OBJF_SHARED;
315 if (so->objfile->obj_private == NULL)
317 obj_private = (obj_private_data_t *)
318 obstack_alloc (&so->objfile->psymbol_obstack,
319 sizeof (obj_private_data_t));
320 obj_private->unwind_info = NULL;
321 obj_private->so_info = NULL;
322 so->objfile->obj_private = obj_private;
325 obj_private = (obj_private_data_t *) so->objfile->obj_private;
326 obj_private->so_info = so;
328 if (!bfd_check_format (so->abfd, bfd_object))
330 error ("\"%s\": not in executable format: %s.",
331 name, bfd_errmsg (bfd_get_error ()));
337 som_solib_load_symbols (struct so_list *so, char *name, int from_tty,
338 CORE_ADDR text_addr, struct target_ops *target)
340 struct section_table *p;
343 CORE_ADDR presumed_data_start;
346 printf ("--Adding symbols for shared library \"%s\"\n", name);
349 som_solib_add_solib_objfile (so, name, from_tty, text_addr);
351 /* Now we need to build a section table for this library since
352 we might be debugging a core file from a dynamically linked
353 executable in which the libraries were not privately mapped. */
354 if (build_section_table (so->abfd,
358 error ("Unable to build section table for shared library\n.");
362 /* Relocate all the sections based on where they got loaded. */
363 for (p = so->sections; p < so->sections_end; p++)
365 if (p->the_bfd_section->flags & SEC_CODE)
367 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
368 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
370 else if (p->the_bfd_section->flags & SEC_DATA)
372 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
373 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
377 /* Now see if we need to map in the text and data for this shared
378 library (for example debugging a core file which does not use
379 private shared libraries.).
381 Carefully peek at the first text address in the library. If the
382 read succeeds, then the libraries were privately mapped and were
383 included in the core dump file.
385 If the peek failed, then the libraries were not privately mapped
386 and are not in the core file, we'll have to read them in ourselves. */
387 status = target_read_memory (text_addr, buf, 4);
392 new = so->sections_end - so->sections;
394 old = target_resize_to_sections (target, new);
396 /* Copy over the old data before it gets clobbered. */
397 memcpy ((char *) (target->to_sections + old),
399 ((sizeof (struct section_table)) * new));
404 /* Add symbols from shared libraries into the symtab list, unless the
405 size threshold specified by auto_solib_limit (in megabytes) would
409 som_solib_add (char *arg_string, int from_tty, struct target_ops *target, int readsyms)
411 struct minimal_symbol *msymbol;
412 struct so_list *so_list_tail;
414 asection *shlib_info;
416 unsigned int dld_flags;
417 char buf[4], *re_err;
418 int threshold_warning_given = 0;
420 /* First validate our arguments. */
421 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
423 error ("Invalid regexp: %s", re_err);
426 /* If we're debugging a core file, or have attached to a running
427 process, then som_solib_create_inferior_hook will not have been
430 We need to first determine if we're dealing with a dynamically
431 linked executable. If not, then return without an error or warning.
433 We also need to examine __dld_flags to determine if the shared library
434 list is valid and to determine if the libraries have been privately
436 if (symfile_objfile == NULL)
439 /* First see if the objfile was dynamically linked. */
440 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
444 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
445 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
448 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
451 error ("Unable to find __dld_flags symbol in object file.\n");
455 addr = SYMBOL_VALUE_ADDRESS (msymbol);
456 /* Read the current contents. */
457 status = target_read_memory (addr, buf, 4);
460 error ("Unable to read __dld_flags\n");
463 dld_flags = extract_unsigned_integer (buf, 4);
465 /* __dld_list may not be valid. If not, then we punt, warning the user if
466 we were called as a result of the add-symfile command.
468 if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
471 error ("__dld_list is not valid according to __dld_flags.\n");
475 /* If the libraries were not mapped private, warn the user. */
476 if ((dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
477 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
479 msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL);
482 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
483 but the data is still available if you know where to look. */
484 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
487 error ("Unable to find dynamic library list.\n");
490 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
493 addr = SYMBOL_VALUE_ADDRESS (msymbol);
495 status = target_read_memory (addr, buf, 4);
498 error ("Unable to find dynamic library list.\n");
502 addr = extract_unsigned_integer (buf, 4);
504 /* If addr is zero, then we're using an old dynamic loader which
505 doesn't maintain __dld_list. We'll have to use a completely
506 different approach to get shared library information. */
510 /* Using the information in __dld_list is the preferred method
511 to get at shared library information. It doesn't depend on
512 any functions in /opt/langtools/lib/end.o and has a chance of working
513 with hpux10 when it is released. */
514 status = target_read_memory (addr, buf, 4);
517 error ("Unable to find dynamic library list.\n");
521 /* addr now holds the address of the first entry in the dynamic
523 addr = extract_unsigned_integer (buf, 4);
525 /* Now that we have a pointer to the dynamic library list, walk
526 through it and add the symbols for each library. */
528 so_list_tail = so_list_head;
529 /* Find the end of the list of shared objects. */
530 while (so_list_tail && so_list_tail->next)
531 so_list_tail = so_list_tail->next;
534 printf ("--About to read shared library list data\n");
537 /* "addr" will always point to the base of the
538 * current data entry describing the current
543 CORE_ADDR name_addr, text_addr;
544 unsigned int name_len;
546 struct so_list *new_so;
547 struct so_list *so_list = so_list_head;
555 /* Get a pointer to the name of this library. */
556 status = target_read_memory (addr, buf, 4);
560 name_addr = extract_unsigned_integer (buf, 4);
564 target_read_memory (name_addr + name_len, buf, 1);
572 name = alloca (name_len);
573 status = target_read_memory (name_addr, name, name_len);
577 /* See if we've already loaded something with this name. */
580 if (!strcmp (so_list->som_solib.name, name))
582 so_list = so_list->next;
585 /* See if the file exists. If not, give a warning, but don't
587 status = stat (name, &statbuf);
590 warning ("Can't find file %s referenced in dld_list.", name);
592 status = target_read_memory (addr + 36, buf, 4);
596 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
600 /* If we've already loaded this one or it's the main program, skip it. */
601 is_main_program = (strcmp (name, symfile_objfile->name) == 0);
602 if (so_list || is_main_program)
604 /* This is the "next" pointer in the strcuture.
606 status = target_read_memory (addr + 36, buf, 4);
610 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
612 /* Record the main program's symbol table size. */
613 if (is_main_program && !so_list)
615 st_size = som_solib_sizeof_symbol_table (name);
616 som_solib_total_st_size += st_size;
619 /* Was this a shlib that we noted but didn't load the symbols for?
620 If so, were we invoked this time from the command-line, via
621 a 'sharedlibrary' or 'add-symbol-file' command? If yes to
622 both, we'd better load the symbols this time.
624 if (from_tty && so_list && !is_main_program && (so_list->objfile == NULL))
625 som_solib_load_symbols (so_list,
628 so_list->som_solib.text_addr,
634 name = obsavestring (name, name_len - 1,
635 &symfile_objfile->symbol_obstack);
637 status = target_read_memory (addr + 8, buf, 4);
641 text_addr = extract_unsigned_integer (buf, 4);
643 new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
644 memset ((char *) new_so, 0, sizeof (struct so_list));
645 if (so_list_head == NULL)
647 so_list_head = new_so;
648 so_list_tail = new_so;
652 so_list_tail->next = new_so;
653 so_list_tail = new_so;
656 /* Fill in all the entries in GDB's shared library list.
659 new_so->solib_addr = addr;
660 new_so->som_solib.name = name;
661 status = target_read_memory (addr + 4, buf, 4);
665 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
666 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
667 /* Following is "high water mark", highest version number
668 * seen, rather than plain version number.
670 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
671 new_so->som_solib.text_addr = text_addr;
673 /* Q: What about longword at "addr + 8"?
674 * A: It's read above, out of order, into "text_addr".
677 status = target_read_memory (addr + 12, buf, 4);
681 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
683 status = target_read_memory (addr + 16, buf, 4);
687 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
689 status = target_read_memory (addr + 20, buf, 4);
693 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
695 status = target_read_memory (addr + 24, buf, 4);
699 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
701 status = target_read_memory (addr + 28, buf, 4);
705 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
707 status = target_read_memory (addr + 32, buf, 4);
711 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
713 status = target_read_memory (addr + 36, buf, 4);
717 new_so->som_solib.next =
718 address_to_host_pointer (extract_unsigned_integer (buf, 4));
720 /* Note that we don't re-set "addr" to the next pointer
721 * until after we've read the trailing data.
724 status = target_read_memory (addr + 40, buf, 4);
725 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
729 /* Now indirect via that value!
731 status = target_read_memory (new_so->som_solib.tsd_start_addr, buf, 4);
732 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
736 printf ("\n+ library \"%s\" is described at 0x%x\n", name, addr);
737 printf (" 'version' is %d\n", new_so->som_solib.struct_version);
738 printf (" 'bind_mode' is %d\n", new_so->som_solib.bind_mode);
739 printf (" 'library_version' is %d\n", new_so->som_solib.library_version);
740 printf (" 'text_addr' is 0x%x\n", new_so->som_solib.text_addr);
741 printf (" 'text_link_addr' is 0x%x\n", new_so->som_solib.text_link_addr);
742 printf (" 'text_end' is 0x%x\n", new_so->som_solib.text_end);
743 printf (" 'data_start' is 0x%x\n", new_so->som_solib.data_start);
744 printf (" 'bss_start' is 0x%x\n", new_so->som_solib.bss_start);
745 printf (" 'data_end' is 0x%x\n", new_so->som_solib.data_end);
746 printf (" 'got_value' is %x\n", new_so->som_solib.got_value);
747 printf (" 'next' is 0x%x\n", new_so->som_solib.next);
748 printf (" 'tsd_start_addr' is 0x%x\n", new_so->som_solib.tsd_start_addr);
751 /* Go on to the next shared library descriptor.
753 addr = (CORE_ADDR) new_so->som_solib.next;
757 /* At this point, we have essentially hooked the shlib into the
758 "info share" command. However, we haven't yet loaded its
759 symbol table. We must now decide whether we ought to, i.e.,
760 whether doing so would exceed the symbol table size threshold.
762 If the threshold has just now been exceeded, then we'll issue
763 a warning message (which explains how to load symbols manually,
764 if the user so desires).
766 If the threshold has just now or previously been exceeded,
767 we'll just add the shlib to the list of object files, but won't
768 actually load its symbols. (This is more useful than it might
769 sound, for it allows us to e.g., still load and use the shlibs'
770 unwind information for stack tracebacks.)
773 /* Note that we DON'T want to preclude the user from using the
774 add-symbol-file command! Thus, we only worry about the threshold
775 when we're invoked for other reasons.
777 st_size = som_solib_sizeof_symbol_table (name);
778 som_solib_st_size_threshold_exceeded =
780 auto_solib_limit > 0 &&
782 ((st_size + som_solib_total_st_size) > (auto_solib_limit * (LONGEST) (1024 * 1024)));
784 if (som_solib_st_size_threshold_exceeded)
786 if (!threshold_warning_given)
787 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");
788 threshold_warning_given = 1;
790 /* We'll still make note of this shlib, even if we don't
791 read its symbols. This allows us to use its unwind
792 information well enough to know how to e.g., correctly
793 do a traceback from a PC within the shlib, even if we
794 can't symbolize those PCs...
796 som_solib_add_solib_objfile (new_so, name, from_tty, text_addr);
800 som_solib_total_st_size += st_size;
802 /* This fills in new_so->objfile, among others. */
803 som_solib_load_symbols (new_so, name, from_tty, text_addr, target);
807 printf ("--Done reading shared library data\n");
810 /* Getting new symbols may change our opinion about what is
812 reinit_frame_cache ();
816 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
820 error ("Error while reading dynamic library list.\n");
825 /* This hook gets called just before the first instruction in the
826 inferior process is executed.
828 This is our opportunity to set magic flags in the inferior so
829 that GDB can be notified when a shared library is mapped in and
830 to tell the dynamic linker that a private copy of the library is
831 needed (so GDB can set breakpoints in the library).
833 __dld_flags is the location of the magic flags; as of this implementation
834 there are 3 flags of interest:
836 bit 0 when set indicates that private copies of the libraries are needed
837 bit 1 when set indicates that the callback hook routine is valid
838 bit 2 when set indicates that the dynamic linker should maintain the
839 __dld_list structure when loading/unloading libraries.
841 Note that shared libraries are not mapped in at this time, so we have
842 run the inferior until the libraries are mapped in. Typically this
843 means running until the "_start" is called. */
846 som_solib_create_inferior_hook (void)
848 struct minimal_symbol *msymbol;
849 unsigned int dld_flags, status, have_endo;
850 asection *shlib_info;
852 struct objfile *objfile;
855 /* First, remove all the solib event breakpoints. Their addresses
856 may have changed since the last time we ran the program. */
857 remove_solib_event_breakpoints ();
859 if (symfile_objfile == NULL)
862 /* First see if the objfile was dynamically linked. */
863 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
867 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
868 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
872 /* Slam the pid of the process into __d_pid.
874 We used to warn when this failed, but that warning is only useful
875 on very old HP systems (hpux9 and older). The warnings are an
876 annoyance to users of modern systems and foul up the testsuite as
877 well. As a result, the warnings have been disabled. */
878 msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
882 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
883 store_unsigned_integer (buf, 4, PIDGET (inferior_ptid));
884 status = target_write_memory (anaddr, buf, 4);
887 warning ("Unable to write __d_pid");
888 warning ("Suggest linking with /opt/langtools/lib/end.o.");
889 warning ("GDB will be unable to track shl_load/shl_unload calls");
893 /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
894 This will force the dynamic linker to call __d_trap when significant
897 Note that the above is the pre-HP-UX 9.0 behaviour. At 9.0 and above,
898 the dld provides an export stub named "__d_trap" as well as the
899 function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
900 We'll look first for the old flavor and then the new.
902 msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
904 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
907 warning ("Unable to find _DLD_HOOK symbol in object file.");
908 warning ("Suggest linking with /opt/langtools/lib/end.o.");
909 warning ("GDB will be unable to track shl_load/shl_unload calls");
912 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
913 dld_cache.hook.address = anaddr;
915 /* Grrr, this might not be an export symbol! We have to find the
917 ALL_OBJFILES (objfile)
919 struct unwind_table_entry *u;
920 struct minimal_symbol *msymbol2;
923 msymbol2 = lookup_minimal_symbol_solib_trampoline (SYMBOL_NAME (msymbol),
925 /* Found a symbol with the right name. */
928 struct unwind_table_entry *u;
929 /* It must be a shared library trampoline. */
930 if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
933 /* It must also be an export stub. */
934 u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
935 if (!u || u->stub_unwind.stub_type != EXPORT)
938 /* OK. Looks like the correct import stub. */
939 anaddr = SYMBOL_VALUE (msymbol2);
940 dld_cache.hook_stub.address = anaddr;
943 store_unsigned_integer (buf, 4, anaddr);
945 msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
948 warning ("Unable to find __dld_hook symbol in object file.");
949 warning ("Suggest linking with /opt/langtools/lib/end.o.");
950 warning ("GDB will be unable to track shl_load/shl_unload calls");
953 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
954 status = target_write_memory (anaddr, buf, 4);
956 /* Now set a shlib_event breakpoint at __d_trap so we can track
957 significant shared library events. */
958 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
961 warning ("Unable to find __dld_d_trap symbol in object file.");
962 warning ("Suggest linking with /opt/langtools/lib/end.o.");
963 warning ("GDB will be unable to track shl_load/shl_unload calls");
966 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
968 /* We have all the support usually found in end.o, so we can track
969 shl_load and shl_unload calls. */
974 /* Get the address of __dld_flags, if no such symbol exists, then we can
975 not debug the shared code. */
976 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
979 error ("Unable to find __dld_flags symbol in object file.\n");
982 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
984 /* Read the current contents. */
985 status = target_read_memory (anaddr, buf, 4);
988 error ("Unable to read __dld_flags\n");
990 dld_flags = extract_unsigned_integer (buf, 4);
992 /* Turn on the flags we care about. */
993 dld_flags |= DLD_FLAGS_MAPPRIVATE;
995 dld_flags |= DLD_FLAGS_HOOKVALID;
996 store_unsigned_integer (buf, 4, dld_flags);
997 status = target_write_memory (anaddr, buf, 4);
1000 error ("Unable to write __dld_flags\n");
1003 /* Now find the address of _start and set a breakpoint there.
1004 We still need this code for two reasons:
1006 * Not all sites have /opt/langtools/lib/end.o, so it's not always
1007 possible to track the dynamic linker's events.
1009 * At this time no events are triggered for shared libraries
1010 loaded at startup time (what a crock). */
1012 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1013 if (msymbol == NULL)
1015 error ("Unable to find _start symbol in object file.\n");
1018 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
1020 /* Make the breakpoint at "_start" a shared library event breakpoint. */
1021 create_solib_event_breakpoint (anaddr);
1023 /* Wipe out all knowledge of old shared libraries since their
1024 mapping can change from one exec to another! */
1025 while (so_list_head)
1027 struct so_list *temp;
1029 temp = so_list_head;
1030 xfree (so_list_head);
1031 so_list_head = temp->next;
1033 clear_symtab_users ();
1036 /* This operation removes the "hook" between GDB and the dynamic linker,
1037 which causes the dld to notify GDB of shared library events.
1039 After this operation completes, the dld will no longer notify GDB of
1040 shared library events. To resume notifications, GDB must call
1041 som_solib_create_inferior_hook.
1043 This operation does not remove any knowledge of shared libraries which
1044 GDB may already have been notified of.
1047 som_solib_remove_inferior_hook (int pid)
1050 struct minimal_symbol *msymbol;
1052 char dld_flags_buffer[TARGET_INT_BIT / TARGET_CHAR_BIT];
1053 unsigned int dld_flags_value;
1054 struct cleanup *old_cleanups = save_inferior_ptid ();
1056 /* Ensure that we're really operating on the specified process. */
1057 inferior_ptid = pid_to_ptid (pid);
1059 /* We won't bother to remove the solib breakpoints from this process.
1061 In fact, on PA64 the breakpoint is hard-coded into the dld callback,
1062 and thus we're not supposed to remove it.
1064 Rather, we'll merely clear the dld_flags bit that enables callbacks.
1066 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
1068 addr = SYMBOL_VALUE_ADDRESS (msymbol);
1069 status = target_read_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT);
1071 dld_flags_value = extract_unsigned_integer (dld_flags_buffer,
1072 sizeof (dld_flags_value));
1074 dld_flags_value &= ~DLD_FLAGS_HOOKVALID;
1075 store_unsigned_integer (dld_flags_buffer,
1076 sizeof (dld_flags_value),
1078 status = target_write_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT);
1080 do_cleanups (old_cleanups);
1084 /* This function creates a breakpoint on the dynamic linker hook, which
1085 is called when e.g., a shl_load or shl_unload call is made. This
1086 breakpoint will only trigger when a shl_load call is made.
1088 If filename is NULL, then loads of any dll will be caught. Else,
1089 only loads of the file whose pathname is the string contained by
1090 filename will be caught.
1092 Undefined behaviour is guaranteed if this function is called before
1093 som_solib_create_inferior_hook.
1096 som_solib_create_catch_load_hook (int pid, int tempflag, char *filename,
1099 create_solib_load_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1102 /* This function creates a breakpoint on the dynamic linker hook, which
1103 is called when e.g., a shl_load or shl_unload call is made. This
1104 breakpoint will only trigger when a shl_unload call is made.
1106 If filename is NULL, then unloads of any dll will be caught. Else,
1107 only unloads of the file whose pathname is the string contained by
1108 filename will be caught.
1110 Undefined behaviour is guaranteed if this function is called before
1111 som_solib_create_inferior_hook.
1114 som_solib_create_catch_unload_hook (int pid, int tempflag, char *filename,
1117 create_solib_unload_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1121 som_solib_have_load_event (int pid)
1123 CORE_ADDR event_kind;
1125 event_kind = read_register (ARG0_REGNUM);
1126 return (event_kind == SHL_LOAD);
1130 som_solib_have_unload_event (int pid)
1132 CORE_ADDR event_kind;
1134 event_kind = read_register (ARG0_REGNUM);
1135 return (event_kind == SHL_UNLOAD);
1139 som_solib_library_pathname (int pid)
1141 CORE_ADDR dll_handle_address;
1142 CORE_ADDR dll_pathname_address;
1143 struct som_solib_mapped_entry dll_descriptor;
1145 static char dll_pathname[1024];
1147 /* Read the descriptor of this newly-loaded library. */
1148 dll_handle_address = read_register (ARG1_REGNUM);
1149 read_memory (dll_handle_address, (char *) &dll_descriptor, sizeof (dll_descriptor));
1151 /* We can find a pointer to the dll's pathname within the descriptor. */
1152 dll_pathname_address = (CORE_ADDR) dll_descriptor.name;
1154 /* Read the pathname, one byte at a time. */
1159 read_memory (dll_pathname_address++, (char *) &b, 1);
1165 return dll_pathname;
1169 som_solib_loaded_library_pathname (int pid)
1171 if (!som_solib_have_load_event (pid))
1172 error ("Must have a load event to use this query");
1174 return som_solib_library_pathname (pid);
1178 som_solib_unloaded_library_pathname (int pid)
1180 if (!som_solib_have_unload_event (pid))
1181 error ("Must have an unload event to use this query");
1183 return som_solib_library_pathname (pid);
1187 som_solib_desire_dynamic_linker_symbols (void)
1189 struct objfile *objfile;
1190 struct unwind_table_entry *u;
1191 struct minimal_symbol *dld_msymbol;
1193 /* Do we already know the value of these symbols? If so, then
1194 we've no work to do.
1196 (If you add clauses to this test, be sure to likewise update the
1197 test within the loop.)
1199 if (dld_cache.is_valid)
1202 ALL_OBJFILES (objfile)
1204 dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
1205 if (dld_msymbol != NULL)
1207 dld_cache.load.address = SYMBOL_VALUE (dld_msymbol);
1208 dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
1211 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
1214 if (dld_msymbol != NULL)
1216 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1218 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1219 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1221 dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol);
1222 dld_cache.load_stub.unwind = u;
1227 dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
1228 if (dld_msymbol != NULL)
1230 dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol);
1231 dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);
1233 /* ??rehrauer: I'm not sure exactly what this is, but it appears
1234 that on some HPUX 10.x versions, there's two unwind regions to
1235 cover the body of "shl_unload", the second being 4 bytes past
1236 the end of the first. This is a large hack to handle that
1237 case, but since I don't seem to have any legitimate way to
1238 look for this thing via the symbol table...
1240 if (dld_cache.unload.unwind != NULL)
1242 u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
1245 dld_cache.unload2.address = u->region_start;
1246 dld_cache.unload2.unwind = u;
1251 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
1254 if (dld_msymbol != NULL)
1256 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1258 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1259 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1261 dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol);
1262 dld_cache.unload_stub.unwind = u;
1267 /* Did we find everything we were looking for? If so, stop. */
1268 if ((dld_cache.load.address != 0)
1269 && (dld_cache.load_stub.address != 0)
1270 && (dld_cache.unload.address != 0)
1271 && (dld_cache.unload_stub.address != 0))
1273 dld_cache.is_valid = 1;
1278 dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
1279 dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);
1281 /* We're prepared not to find some of these symbols, which is why
1282 this function is a "desire" operation, and not a "require".
1287 som_solib_in_dynamic_linker (int pid, CORE_ADDR pc)
1289 struct unwind_table_entry *u_pc;
1291 /* Are we in the dld itself?
1293 ??rehrauer: Large hack -- We'll assume that any address in a
1294 shared text region is the dld's text. This would obviously
1295 fall down if the user attached to a process, whose shlibs
1296 weren't mapped to a (writeable) private region. However, in
1297 that case the debugger probably isn't able to set the fundamental
1298 breakpoint in the dld callback anyways, so this hack should be
1301 if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
1304 /* Cache the address of some symbols that are part of the dynamic
1305 linker, if not already known.
1307 som_solib_desire_dynamic_linker_symbols ();
1309 /* Are we in the dld callback? Or its export stub? */
1310 u_pc = find_unwind_entry (pc);
1314 if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
1317 /* Or the interface of the dld (i.e., "shl_load" or friends)? */
1318 if ((u_pc == dld_cache.load.unwind)
1319 || (u_pc == dld_cache.unload.unwind)
1320 || (u_pc == dld_cache.unload2.unwind)
1321 || (u_pc == dld_cache.load_stub.unwind)
1322 || (u_pc == dld_cache.unload_stub.unwind))
1325 /* Apparently this address isn't part of the dld's text. */
1330 /* Return the GOT value for the shared library in which ADDR belongs. If
1331 ADDR isn't in any known shared library, return zero. */
1334 som_solib_get_got_by_pc (CORE_ADDR addr)
1336 struct so_list *so_list = so_list_head;
1337 CORE_ADDR got_value = 0;
1341 if (so_list->som_solib.text_addr <= addr
1342 && so_list->som_solib.text_end > addr)
1344 got_value = so_list->som_solib.got_value;
1347 so_list = so_list->next;
1353 Return the address of the handle of the shared library
1354 in which ADDR belongs. If
1355 ADDR isn't in any known shared library, return zero. */
1356 /* this function is used in hppa_fix_call_dummy in hppa-tdep.c */
1359 som_solib_get_solib_by_pc (CORE_ADDR addr)
1361 struct so_list *so_list = so_list_head;
1365 if (so_list->som_solib.text_addr <= addr
1366 && so_list->som_solib.text_end > addr)
1370 so_list = so_list->next;
1373 return so_list->solib_addr;
1380 som_solib_section_offsets (struct objfile *objfile,
1381 struct section_offsets *offsets)
1383 struct so_list *so_list = so_list_head;
1387 /* Oh what a pain! We need the offsets before so_list->objfile
1388 is valid. The BFDs will never match. Make a best guess. */
1389 if (strstr (objfile->name, so_list->som_solib.name))
1391 asection *private_section;
1393 /* The text offset is easy. */
1394 offsets->offsets[SECT_OFF_TEXT (objfile)]
1395 = (so_list->som_solib.text_addr
1396 - so_list->som_solib.text_link_addr);
1397 offsets->offsets[SECT_OFF_RODATA (objfile)]
1398 = ANOFFSET (offsets, SECT_OFF_TEXT (objfile));
1400 /* We should look at presumed_dp in the SOM header, but
1401 that's not easily available. This should be OK though. */
1402 private_section = bfd_get_section_by_name (objfile->obfd,
1404 if (!private_section)
1406 warning ("Unable to find $PRIVATE$ in shared library!");
1407 offsets->offsets[SECT_OFF_DATA (objfile)] = 0;
1408 offsets->offsets[SECT_OFF_BSS (objfile)] = 0;
1411 offsets->offsets[SECT_OFF_DATA (objfile)]
1412 = (so_list->som_solib.data_start - private_section->vma);
1413 offsets->offsets[SECT_OFF_BSS (objfile)]
1414 = ANOFFSET (offsets, SECT_OFF_DATA (objfile));
1417 so_list = so_list->next;
1422 /* Dump information about all the currently loaded shared libraries. */
1425 som_sharedlibrary_info_command (char *ignore, int from_tty)
1427 struct so_list *so_list = so_list_head;
1429 if (exec_bfd == NULL)
1431 printf_unfiltered ("No executable file.\n");
1435 if (so_list == NULL)
1437 printf_unfiltered ("No shared libraries loaded at this time.\n");
1441 printf_unfiltered ("Shared Object Libraries\n");
1442 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
1443 " flags", " tstart", " tend", " dstart", " dend", " dlt");
1448 flags = so_list->som_solib.struct_version << 24;
1449 flags |= so_list->som_solib.bind_mode << 16;
1450 flags |= so_list->som_solib.library_version;
1451 printf_unfiltered ("%s", so_list->som_solib.name);
1452 if (so_list->objfile == NULL)
1453 printf_unfiltered (" (symbols not loaded)");
1454 printf_unfiltered ("\n");
1455 printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l"));
1456 printf_unfiltered ("%-12s",
1457 local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
1458 printf_unfiltered ("%-12s",
1459 local_hex_string_custom (so_list->som_solib.text_end, "08l"));
1460 printf_unfiltered ("%-12s",
1461 local_hex_string_custom (so_list->som_solib.data_start, "08l"));
1462 printf_unfiltered ("%-12s",
1463 local_hex_string_custom (so_list->som_solib.data_end, "08l"));
1464 printf_unfiltered ("%-12s\n",
1465 local_hex_string_custom (so_list->som_solib.got_value, "08l"));
1466 so_list = so_list->next;
1471 som_solib_sharedlibrary_command (char *args, int from_tty)
1474 som_solib_add (args, from_tty, (struct target_ops *) 0, 1);
1480 som_solib_address (CORE_ADDR addr)
1482 struct so_list *so = so_list_head;
1486 /* Is this address within this shlib's text range? If so,
1487 return the shlib's name.
1489 if ((addr >= so->som_solib.text_addr) && (addr <= so->som_solib.text_end))
1490 return so->som_solib.name;
1492 /* Nope, keep looking... */
1496 /* No, we couldn't prove that the address is within a shlib. */
1502 som_solib_restart (void)
1504 struct so_list *sl = so_list_head;
1506 /* Before the shlib info vanishes, use it to disable any breakpoints
1507 that may still be active in those shlibs.
1509 disable_breakpoints_in_shlibs (0);
1511 /* Discard all the shlib descriptors.
1515 struct so_list *next_sl = sl->next;
1519 so_list_head = NULL;
1521 som_solib_total_st_size = (LONGEST) 0;
1522 som_solib_st_size_threshold_exceeded = 0;
1524 dld_cache.is_valid = 0;
1526 dld_cache.hook.address = 0;
1527 dld_cache.hook.unwind = NULL;
1529 dld_cache.hook_stub.address = 0;
1530 dld_cache.hook_stub.unwind = NULL;
1532 dld_cache.load.address = 0;
1533 dld_cache.load.unwind = NULL;
1535 dld_cache.load_stub.address = 0;
1536 dld_cache.load_stub.unwind = NULL;
1538 dld_cache.unload.address = 0;
1539 dld_cache.unload.unwind = NULL;
1541 dld_cache.unload2.address = 0;
1542 dld_cache.unload2.unwind = NULL;
1544 dld_cache.unload_stub.address = 0;
1545 dld_cache.unload_stub.unwind = NULL;
1551 no_shared_libraries -- handle command to explicitly discard symbols
1552 from shared libraries.
1556 Implements the command "nosharedlibrary", which discards symbols
1557 that have been auto-loaded from shared libraries. Symbols from
1558 shared libraries that were added by explicit request of the user
1559 are not discarded. Also called from remote.c. */
1562 no_shared_libraries (char *ignored, int from_tty)
1569 _initialize_som_solib (void)
1571 add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
1572 "Load shared object library symbols for files matching REGEXP.");
1573 add_info ("sharedlibrary", som_sharedlibrary_info_command,
1574 "Status of loaded shared object libraries.");
1577 (add_set_cmd ("auto-solib-add", class_support, var_boolean,
1578 (char *) &auto_solib_add,
1579 "Set autoloading of shared library symbols.\n\
1580 If \"on\", symbols from all shared object libraries will be loaded\n\
1581 automatically when the inferior begins execution, when the dynamic linker\n\
1582 informs gdb that a new library has been loaded, or when attaching to the\n\
1583 inferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
1588 (add_set_cmd ("auto-solib-limit", class_support, var_zinteger,
1589 (char *) &auto_solib_limit,
1590 "Set threshold (in Mb) for autoloading shared library symbols.\n\
1591 When shared library autoloading is enabled, new libraries will be loaded\n\
1592 only until the total size of shared library symbols exceeds this\n\
1593 threshold in megabytes. Is ignored when using `sharedlibrary'.",
1597 /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how
1598 much data space a process can use. We ought to be reading
1599 MAXDSIZ and setting auto_solib_limit to some large fraction of
1600 that value. If not that, we maybe ought to be setting it smaller
1601 than the default for MAXDSIZ (that being 64Mb, I believe).
1602 However, [1] this threshold is only crudely approximated rather
1603 than actually measured, and [2] 50 Mbytes is too small for
1604 debugging gdb itself. Thus, the arbitrary 100 figure. */
1605 auto_solib_limit = 100; /* Megabytes */
1607 som_solib_restart ();
1610 /* Get some HPUX-specific data from a shared lib.
1613 so_lib_thread_start_addr (struct so_list *so)
1615 return so->som_solib.tsd_start_addr;