1 /* Handle HP SOM shared libraries for GDB, the GNU Debugger.
2 Copyright 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 Written by the Center for Software Science at the Univerity of Utah
21 and by Cygnus Support. */
32 #include "breakpoint.h"
36 #include "gdb-stabs.h"
41 * Most of this code should work for hp300 shared libraries. Does
42 anyone care enough to weed out any SOM-isms.
44 * Do we need/want a command to load a shared library?
46 * Support for hpux8 dynamic linker.
48 * Support for tracking user calls to dld_load, dld_unload. */
50 /* The basic structure which describes a dynamically loaded object. This
51 data structure is private to the dynamic linker and isn't found in
52 any HPUX include file. */
54 struct som_solib_mapped_entry
56 /* The name of the library. */
59 /* Version of this structure (it is expected to change again in hpux10). */
60 unsigned char struct_version
;
62 /* Binding mode for this library. */
63 unsigned char bind_mode
;
65 /* Version of this library. */
66 short library_version
;
68 /* Start of text address, link-time text location, end of text address. */
70 CORE_ADDR text_link_addr
;
73 /* Start of data, start of bss and end of data. */
78 /* Value of linkage pointer (%r19). */
82 struct som_solib_mapped_entry
*next
;
84 /* There are other fields, but I don't have information as to what is
88 /* A structure to keep track of all the known shared objects. */
91 struct som_solib_mapped_entry som_solib
;
92 struct objfile
*objfile
;
94 struct section_table
*sections
;
95 struct section_table
*sections_end
;
99 static struct so_list
*so_list_head
;
101 static void som_sharedlibrary_info_command
PARAMS ((char *, int));
103 /* Add symbols from shared libraries into the symtab list. */
106 som_solib_add (arg_string
, from_tty
, target
)
109 struct target_ops
*target
;
111 struct minimal_symbol
*msymbol
;
112 struct so_list
*so_list_tail
;
114 asection
*shlib_info
;
116 unsigned int dld_flags
;
117 char buf
[4], *re_err
;
119 /* First validate our arguments. */
120 if ((re_err
= re_comp (arg_string
? arg_string
: ".")) != NULL
)
122 error ("Invalid regexp: %s", re_err
);
125 /* If we're debugging a core file, or have attached to a running
126 process, then som_solib_create_inferior_hook will not have been
129 We need to first determine if we're dealing with a dynamically
130 linked executable. If not, then return without an error or warning.
132 We also need to examine __dld_flags to determine if the shared library
133 list is valid and to determine if the libraries have been privately
135 if (symfile_objfile
== NULL
)
138 /* First see if the objfile was dynamically linked. */
139 shlib_info
= bfd_get_section_by_name (symfile_objfile
->obfd
, "$SHLIB_INFO$");
143 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
144 if (bfd_section_size (symfile_objfile
->obfd
, shlib_info
) == 0)
147 msymbol
= lookup_minimal_symbol ("__dld_flags", NULL
, NULL
);
150 error ("Unable to find __dld_flags symbol in object file.\n");
154 addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
155 /* Read the current contents. */
156 status
= target_read_memory (addr
, buf
, 4);
159 error ("Unable to read __dld_flags\n");
162 dld_flags
= extract_unsigned_integer (buf
, 4);
164 /* __dld_list may not be valid. If it's not valid tell the user. */
165 if ((dld_flags
& 4) == 0)
167 error ("__dld_list is not valid according to __dld_flags.\n");
171 /* If the libraries were not mapped private, warn the user. */
172 if ((dld_flags
& 1) == 0)
173 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
175 msymbol
= lookup_minimal_symbol ("__dld_list", NULL
, NULL
);
178 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
179 but the data is still available if you know where to look. */
180 msymbol
= lookup_minimal_symbol ("__dld_flags", NULL
, NULL
);
183 error ("Unable to find dynamic library list.\n");
186 addr
= SYMBOL_VALUE_ADDRESS (msymbol
) - 8;
189 addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
191 status
= target_read_memory (addr
, buf
, 4);
194 error ("Unable to find dynamic library list.\n");
198 addr
= extract_unsigned_integer (buf
, 4);
200 /* If addr is zero, then we're using an old dynamic loader which
201 doesn't maintain __dld_list. We'll have to use a completely
202 different approach to get shared library information. */
206 /* Using the information in __dld_list is the preferred method
207 to get at shared library information. It doesn't depend on
208 any functions in /usr/lib/end.o and has a chance of working
209 with hpux10 when it is released. */
210 status
= target_read_memory (addr
, buf
, 4);
213 error ("Unable to find dynamic library list.\n");
217 /* addr now holds the address of the first entry in the dynamic
219 addr
= extract_unsigned_integer (buf
, 4);
221 /* Now that we have a pointer to the dynamic library list, walk
222 through it and add the symbols for each library. */
224 so_list_tail
= so_list_head
;
225 /* Find the end of the list of shared objects. */
226 while (so_list_tail
&& so_list_tail
->next
)
227 so_list_tail
= so_list_tail
->next
;
231 CORE_ADDR name_addr
, text_addr
;
232 unsigned int name_len
;
234 struct so_list
*new_so
;
235 struct so_list
*so_list
= so_list_head
;
236 struct section_table
*p
;
242 /* Get a pointer to the name of this library. */
243 status
= target_read_memory (addr
, buf
, 4);
247 name_addr
= extract_unsigned_integer (buf
, 4);
251 target_read_memory (name_addr
+ name_len
, buf
, 1);
259 name
= alloca (name_len
);
260 status
= target_read_memory (name_addr
, name
, name_len
);
264 /* See if we've already loaded something with this name. */
267 if (!strcmp (so_list
->som_solib
.name
, name
))
269 so_list
= so_list
->next
;
272 /* See if the file exists. If not, give a warning, but don't
274 status
= stat (name
, &statbuf
);
277 warning ("Can't find file %s referenced in dld_list.", name
);
279 status
= target_read_memory (addr
+ 36, buf
, 4);
283 addr
= (CORE_ADDR
) extract_unsigned_integer (buf
, 4);
287 /* If we've already loaded this one or it's the main program, skip it. */
288 if (so_list
|| !strcmp (name
, symfile_objfile
->name
))
290 status
= target_read_memory (addr
+ 36, buf
, 4);
294 addr
= (CORE_ADDR
) extract_unsigned_integer (buf
, 4);
298 name
= obsavestring (name
, name_len
- 1,
299 &symfile_objfile
->symbol_obstack
);
301 status
= target_read_memory (addr
+ 8, buf
, 4);
305 text_addr
= extract_unsigned_integer (buf
, 4);
308 new_so
= (struct so_list
*) malloc (sizeof (struct so_list
));
309 memset ((char *)new_so
, 0, sizeof (struct so_list
));
310 if (so_list_head
== NULL
)
312 so_list_head
= new_so
;
313 so_list_tail
= new_so
;
317 so_list_tail
->next
= new_so
;
318 so_list_tail
= new_so
;
321 /* Fill in all the entries in GDB's shared library list. */
322 new_so
->som_solib
.name
= name
;
323 status
= target_read_memory (addr
+ 4, buf
, 4);
327 new_so
->som_solib
.struct_version
= extract_unsigned_integer (buf
+ 3, 1);
328 new_so
->som_solib
.bind_mode
= extract_unsigned_integer (buf
+ 2, 1);
329 new_so
->som_solib
.library_version
= extract_unsigned_integer (buf
, 2);
330 new_so
->som_solib
.text_addr
= text_addr
;
332 status
= target_read_memory (addr
+ 12, buf
, 4);
336 new_so
->som_solib
.text_link_addr
= extract_unsigned_integer (buf
, 4);
338 status
= target_read_memory (addr
+ 16, buf
, 4);
342 new_so
->som_solib
.text_end
= extract_unsigned_integer (buf
, 4);
344 status
= target_read_memory (addr
+ 20, buf
, 4);
348 new_so
->som_solib
.data_start
= extract_unsigned_integer (buf
, 4);
350 status
= target_read_memory (addr
+ 24, buf
, 4);
354 new_so
->som_solib
.bss_start
= extract_unsigned_integer (buf
, 4);
356 status
= target_read_memory (addr
+ 28, buf
, 4);
360 new_so
->som_solib
.data_end
= extract_unsigned_integer (buf
, 4);
362 status
= target_read_memory (addr
+ 32, buf
, 4);
366 new_so
->som_solib
.got_value
= extract_unsigned_integer (buf
, 4);
368 status
= target_read_memory (addr
+ 36, buf
, 4);
372 new_so
->som_solib
.next
= (void *)extract_unsigned_integer (buf
, 4);
373 addr
= (CORE_ADDR
)new_so
->som_solib
.next
;
375 new_so
->objfile
= symbol_file_add (name
, from_tty
, text_addr
, 0, 0, 0);
376 new_so
->abfd
= new_so
->objfile
->obfd
;
378 if (!bfd_check_format (new_so
->abfd
, bfd_object
))
380 error ("\"%s\": not in executable format: %s.",
381 name
, bfd_errmsg (bfd_get_error ()));
384 /* Now we need to build a section table for this library since
385 we might be debugging a core file from a dynamically linked
386 executable in which the libraries were not privately mapped. */
387 if (build_section_table (new_so
->abfd
,
389 &new_so
->sections_end
))
391 error ("Unable to build section table for shared library\n.");
395 /* Relocate all the sections based on where they got loaded. */
396 for (p
= new_so
->sections
; p
< new_so
->sections_end
; p
++)
398 if (p
->the_bfd_section
->flags
& SEC_CODE
)
400 p
->addr
+= text_addr
- new_so
->som_solib
.text_link_addr
;
401 p
->endaddr
+= text_addr
- new_so
->som_solib
.text_link_addr
;
403 else if (p
->the_bfd_section
->flags
& SEC_DATA
)
405 p
->addr
+= new_so
->som_solib
.data_start
;
406 p
->endaddr
+= new_so
->som_solib
.data_start
;
410 /* Now see if we need to map in the text and data for this shared
411 library (for example debugging a core file which does not use
412 private shared libraries.).
414 Carefully peek at the first text address in the library. If the
415 read succeeds, then the libraries were privately mapped and were
416 included in the core dump file.
418 If the peek failed, then the libraries were not privately mapped
419 and are not in the core file, we'll have to read them in ourselves. */
420 status
= target_read_memory (text_addr
, buf
, 4);
426 /* We must update the to_sections field in the core_ops structure
427 here, otherwise we dereference a potential dangling pointer
428 for each call to target_read/write_memory within this routine. */
429 update_coreops
= core_ops
.to_sections
== target
->to_sections
;
431 new = new_so
->sections_end
- new_so
->sections
;
432 /* Add sections from the shared library to the core target. */
433 if (target
->to_sections
)
435 old
= target
->to_sections_end
- target
->to_sections
;
436 target
->to_sections
= (struct section_table
*)
437 xrealloc ((char *)target
->to_sections
,
438 ((sizeof (struct section_table
)) * (old
+ new)));
443 target
->to_sections
= (struct section_table
*)
444 xmalloc ((sizeof (struct section_table
)) * new);
446 target
->to_sections_end
= (target
->to_sections
+ old
+ new);
448 /* Update the to_sections field in the core_ops structure
452 core_ops
.to_sections
= target
->to_sections
;
453 core_ops
.to_sections_end
= target
->to_sections_end
;
456 /* Copy over the old data before it gets clobbered. */
457 memcpy ((char *)(target
->to_sections
+ old
),
459 ((sizeof (struct section_table
)) * new));
463 /* Getting new symbols may change our opinion about what is
465 reinit_frame_cache ();
469 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
473 error ("Error while reading dynamic library list.\n");
478 /* This hook gets called just before the first instruction in the
479 inferior process is executed.
481 This is our opportunity to set magic flags in the inferior so
482 that GDB can be notified when a shared library is mapped in and
483 to tell the dynamic linker that a private copy of the library is
484 needed (so GDB can set breakpoints in the library).
486 __dld_flags is the location of the magic flags; as of this implementation
487 there are 3 flags of interest:
489 bit 0 when set indicates that private copies of the libraries are needed
490 bit 1 when set indicates that the callback hook routine is valid
491 bit 2 when set indicates that the dynamic linker should maintain the
492 __dld_list structure when loading/unloading libraries.
494 Note that shared libraries are not mapped in at this time, so we have
495 run the inferior until the libraries are mapped in. Typically this
496 means running until the "_start" is called. */
499 som_solib_create_inferior_hook()
501 struct minimal_symbol
*msymbol
;
502 unsigned int dld_flags
, status
;
503 asection
*shlib_info
;
504 char shadow_contents
[BREAKPOINT_MAX
], buf
[4];
507 if (symfile_objfile
== NULL
)
510 /* First see if the objfile was dynamically linked. */
511 shlib_info
= bfd_get_section_by_name (symfile_objfile
->obfd
, "$SHLIB_INFO$");
515 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
516 if (bfd_section_size (symfile_objfile
->obfd
, shlib_info
) == 0)
519 /* Get the address of __dld_flags, if no such symbol exists, then we can
520 not debug the shared code. */
521 msymbol
= lookup_minimal_symbol ("__dld_flags", NULL
, NULL
);
524 error ("Unable to find __dld_flags symbol in object file.\n");
528 anaddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
529 /* Read the current contents. */
530 status
= target_read_memory (anaddr
, buf
, 4);
533 error ("Unable to read __dld_flags\n");
536 dld_flags
= extract_unsigned_integer (buf
, 4);
538 /* Turn on the flags we care about. */
540 store_unsigned_integer (buf
, 4, dld_flags
);
541 status
= target_write_memory (anaddr
, buf
, 4);
544 error ("Unable to write __dld_flags\n");
548 /* Now find the address of _start and set a breakpoint there. */
549 msymbol
= lookup_minimal_symbol ("_start", NULL
, symfile_objfile
);
552 error ("Unable to find _start symbol in object file.\n");
556 anaddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
557 if (target_insert_breakpoint (anaddr
, shadow_contents
))
559 error ("Unable to set breakpoint at _start.\n");
563 /* Wipe out all knowledge of old shared libraries since their
564 mapping can change from one exec to another! */
567 struct so_list
*temp
;
569 free_objfile (so_list_head
->objfile
);
572 so_list_head
= temp
->next
;
575 /* Start the process again and wait for it to hit our breakpoint. */
576 clear_proceed_status ();
577 stop_soon_quietly
= 1;
578 stop_signal
= TARGET_SIGNAL_0
;
581 target_resume (-1, 0, stop_signal
);
582 wait_for_inferior ();
584 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
585 stop_soon_quietly
= 0;
587 /* All the libraries should be mapped in now. Remove our breakpoint and
588 read in the symbol tables from the shared libraries. */
589 if (target_remove_breakpoint (anaddr
, shadow_contents
))
591 error ("Unable to remove breakpoint at _start.\n");
595 if (auto_solib_add_at_startup
)
596 som_solib_add ((char *) 0, 0, (struct target_ops
*) 0);
599 /* Return the GOT value for the shared library in which ADDR belongs. If
600 ADDR isn't in any known shared library, return zero. */
603 som_solib_get_got_by_pc (addr
)
606 struct so_list
*so_list
= so_list_head
;
607 CORE_ADDR got_value
= 0;
611 if (so_list
->som_solib
.text_addr
<= addr
612 && so_list
->som_solib
.text_end
> addr
)
614 got_value
= so_list
->som_solib
.got_value
;
617 so_list
= so_list
->next
;
623 som_solib_section_offsets (objfile
, offsets
)
624 struct objfile
*objfile
;
625 struct section_offsets
*offsets
;
627 struct so_list
*so_list
= so_list_head
;
631 /* Oh what a pain! We need the offsets before so_list->objfile
632 is valid. The BFDs will never match. Make a best guess. */
633 if (strstr (objfile
->name
, so_list
->som_solib
.name
))
635 asection
*private_section
;
637 /* The text offset is easy. */
638 ANOFFSET (offsets
, SECT_OFF_TEXT
)
639 = (so_list
->som_solib
.text_addr
640 - so_list
->som_solib
.text_link_addr
);
641 ANOFFSET (offsets
, SECT_OFF_RODATA
)
642 = ANOFFSET (offsets
, SECT_OFF_TEXT
);
644 /* We should look at presumed_dp in the SOM header, but
645 that's not easily available. This should be OK though. */
646 private_section
= bfd_get_section_by_name (objfile
->obfd
,
648 if (!private_section
)
650 warning ("Unable to find $PRIVATE$ in shared library!");
651 ANOFFSET (offsets
, SECT_OFF_DATA
) = 0;
652 ANOFFSET (offsets
, SECT_OFF_BSS
) = 0;
655 ANOFFSET (offsets
, SECT_OFF_DATA
)
656 = (so_list
->som_solib
.data_start
- private_section
->vma
);
657 ANOFFSET (offsets
, SECT_OFF_BSS
)
658 = ANOFFSET (offsets
, SECT_OFF_DATA
);
661 so_list
= so_list
->next
;
666 /* Dump information about all the currently loaded shared libraries. */
669 som_sharedlibrary_info_command (ignore
, from_tty
)
673 struct so_list
*so_list
= so_list_head
;
675 if (exec_bfd
== NULL
)
677 printf_unfiltered ("no exec file.\n");
683 printf_unfiltered ("No shared libraries loaded at this time.\n");
687 printf_unfiltered ("Shared Object Libraries\n");
688 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
689 " flags", " tstart", " tend", " dstart", " dend", " dlt");
694 flags
= so_list
->som_solib
.struct_version
<< 24;
695 flags
|= so_list
->som_solib
.bind_mode
<< 16;
696 flags
|= so_list
->som_solib
.library_version
;
697 printf_unfiltered ("%s\n", so_list
->som_solib
.name
);
698 printf_unfiltered (" %-12s", local_hex_string_custom (flags
, "08l"));
699 printf_unfiltered ("%-12s",
700 local_hex_string_custom (so_list
->som_solib
.text_addr
, "08l"));
701 printf_unfiltered ("%-12s",
702 local_hex_string_custom (so_list
->som_solib
.text_end
, "08l"));
703 printf_unfiltered ("%-12s",
704 local_hex_string_custom (so_list
->som_solib
.data_start
, "08l"));
705 printf_unfiltered ("%-12s",
706 local_hex_string_custom (so_list
->som_solib
.data_end
, "08l"));
707 printf_unfiltered ("%-12s\n",
708 local_hex_string_custom (so_list
->som_solib
.got_value
, "08l"));
709 so_list
= so_list
->next
;
714 som_solib_sharedlibrary_command (args
, from_tty
)
719 som_solib_add (args
, from_tty
, (struct target_ops
*) 0);
723 _initialize_som_solib ()
725 add_com ("sharedlibrary", class_files
, som_solib_sharedlibrary_command
,
726 "Load shared object library symbols for files matching REGEXP.");
727 add_info ("sharedlibrary", som_sharedlibrary_info_command
,
728 "Status of loaded shared object libraries.");
730 (add_set_cmd ("auto-solib-add", class_support
, var_zinteger
,
731 (char *) &auto_solib_add_at_startup
,
732 "Set autoloading of shared library symbols at startup.\n\
733 If nonzero, symbols from all shared object libraries will be loaded\n\
734 automatically when the inferior begins execution. Otherwise, symbols\n\
735 must be loaded manually, using `sharedlibrary'.",