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
;
241 /* Get a pointer to the name of this library. */
242 status
= target_read_memory (addr
, buf
, 4);
246 name_addr
= extract_unsigned_integer (buf
, 4);
250 target_read_memory (name_addr
+ name_len
, buf
, 1);
258 name
= alloca (name_len
);
259 status
= target_read_memory (name_addr
, name
, name_len
);
263 /* See if we've already loaded something with this name. */
266 if (!strcmp (so_list
->som_solib
.name
, name
))
268 so_list
= so_list
->next
;
271 /* We've already loaded this one or it's the main program, skip it. */
272 if (so_list
|| !strcmp (name
, symfile_objfile
->name
))
274 status
= target_read_memory (addr
+ 36, buf
, 4);
278 addr
= (CORE_ADDR
) extract_unsigned_integer (buf
, 4);
282 name
= obsavestring (name
, name_len
- 1,
283 &symfile_objfile
->symbol_obstack
);
285 status
= target_read_memory (addr
+ 8, buf
, 4);
289 text_addr
= extract_unsigned_integer (buf
, 4);
292 new_so
= (struct so_list
*) malloc (sizeof (struct so_list
));
293 memset ((char *)new_so
, 0, sizeof (struct so_list
));
294 if (so_list_head
== NULL
)
296 so_list_head
= new_so
;
297 so_list_tail
= new_so
;
301 so_list_tail
->next
= new_so
;
302 so_list_tail
= new_so
;
305 /* Fill in all the entries in GDB's shared library list. */
306 new_so
->som_solib
.name
= name
;
307 status
= target_read_memory (addr
+ 4, buf
, 4);
311 new_so
->som_solib
.struct_version
= extract_unsigned_integer (buf
+ 3, 1);
312 new_so
->som_solib
.bind_mode
= extract_unsigned_integer (buf
+ 2, 1);
313 new_so
->som_solib
.library_version
= extract_unsigned_integer (buf
, 2);
314 new_so
->som_solib
.text_addr
= text_addr
;
316 status
= target_read_memory (addr
+ 12, buf
, 4);
320 new_so
->som_solib
.text_link_addr
= extract_unsigned_integer (buf
, 4);
322 status
= target_read_memory (addr
+ 16, buf
, 4);
326 new_so
->som_solib
.text_end
= extract_unsigned_integer (buf
, 4);
328 status
= target_read_memory (addr
+ 20, buf
, 4);
332 new_so
->som_solib
.data_start
= extract_unsigned_integer (buf
, 4);
334 status
= target_read_memory (addr
+ 24, buf
, 4);
338 new_so
->som_solib
.bss_start
= extract_unsigned_integer (buf
, 4);
340 status
= target_read_memory (addr
+ 28, buf
, 4);
344 new_so
->som_solib
.data_end
= extract_unsigned_integer (buf
, 4);
346 status
= target_read_memory (addr
+ 32, buf
, 4);
350 new_so
->som_solib
.got_value
= extract_unsigned_integer (buf
, 4);
352 status
= target_read_memory (addr
+ 36, buf
, 4);
356 new_so
->som_solib
.next
= (void *)extract_unsigned_integer (buf
, 4);
357 addr
= (CORE_ADDR
)new_so
->som_solib
.next
;
359 new_so
->objfile
= symbol_file_add (name
, from_tty
, text_addr
, 0, 0, 0);
360 new_so
->abfd
= new_so
->objfile
->obfd
;
362 if (!bfd_check_format (new_so
->abfd
, bfd_object
))
364 error ("\"%s\": not in executable format: %s.",
365 name
, bfd_errmsg (bfd_get_error ()));
368 /* Now we need to build a section table for this library since
369 we might be debugging a core file from a dynamically linked
370 executable in which the libraries were not privately mapped. */
371 if (build_section_table (new_so
->abfd
,
373 &new_so
->sections_end
))
375 error ("Unable to build section table for shared library\n.");
379 /* Relocate all the sections based on where they got loaded. */
380 for (p
= new_so
->sections
; p
< new_so
->sections_end
; p
++)
382 if (p
->the_bfd_section
->flags
& SEC_CODE
)
384 p
->addr
+= text_addr
- new_so
->som_solib
.text_link_addr
;
385 p
->endaddr
+= text_addr
- new_so
->som_solib
.text_link_addr
;
387 else if (p
->the_bfd_section
->flags
& SEC_DATA
)
389 p
->addr
+= new_so
->som_solib
.data_start
;
390 p
->endaddr
+= new_so
->som_solib
.data_start
;
394 /* Now see if we need to map in the text and data for this shared
395 library (for example debugging a core file which does not use
396 private shared libraries.).
398 Carefully peek at the first text address in the library. If the
399 read succeeds, then the libraries were privately mapped and were
400 included in the core dump file.
402 If the peek failed, then the libraries were not privately mapped
403 and are not in the core file, we'll have to read them in ourselves. */
404 status
= target_read_memory (text_addr
, buf
, 4);
409 new = new_so
->sections_end
- new_so
->sections
;
410 /* Add sections from the shared library to the core target. */
411 if (target
->to_sections
)
413 old
= target
->to_sections_end
- target
->to_sections
;
414 target
->to_sections
= (struct section_table
*)
415 xrealloc ((char *)target
->to_sections
,
416 ((sizeof (struct section_table
)) * (old
+ new)));
421 target
->to_sections
= (struct section_table
*)
422 xmalloc ((sizeof (struct section_table
)) * new);
424 target
->to_sections_end
= (target
->to_sections
+ old
+ new);
425 memcpy ((char *)(target
->to_sections
+ old
),
427 ((sizeof (struct section_table
)) * new));
431 /* Getting new symbols may change our opinion about what is
433 reinit_frame_cache ();
437 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
441 error ("Error while reading dynamic library list.\n");
446 /* This hook gets called just before the first instruction in the
447 inferior process is executed.
449 This is our opportunity to set magic flags in the inferior so
450 that GDB can be notified when a shared library is mapped in and
451 to tell the dynamic linker that a private copy of the library is
452 needed (so GDB can set breakpoints in the library).
454 __dld_flags is the location of the magic flags; as of this implementation
455 there are 3 flags of interest:
457 bit 0 when set indicates that private copies of the libraries are needed
458 bit 1 when set indicates that the callback hook routine is valid
459 bit 2 when set indicates that the dynamic linker should maintain the
460 __dld_list structure when loading/unloading libraries.
462 Note that shared libraries are not mapped in at this time, so we have
463 run the inferior until the libraries are mapped in. Typically this
464 means running until the "_start" is called. */
467 som_solib_create_inferior_hook()
469 struct minimal_symbol
*msymbol
;
470 unsigned int dld_flags
, status
;
471 asection
*shlib_info
;
472 char shadow_contents
[BREAKPOINT_MAX
], buf
[4];
475 if (symfile_objfile
== NULL
)
478 /* First see if the objfile was dynamically linked. */
479 shlib_info
= bfd_get_section_by_name (symfile_objfile
->obfd
, "$SHLIB_INFO$");
483 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
484 if (bfd_section_size (symfile_objfile
->obfd
, shlib_info
) == 0)
487 /* Get the address of __dld_flags, if no such symbol exists, then we can
488 not debug the shared code. */
489 msymbol
= lookup_minimal_symbol ("__dld_flags", NULL
, NULL
);
492 error ("Unable to find __dld_flags symbol in object file.\n");
496 anaddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
497 /* Read the current contents. */
498 status
= target_read_memory (anaddr
, buf
, 4);
501 error ("Unable to read __dld_flags\n");
504 dld_flags
= extract_unsigned_integer (buf
, 4);
506 /* Turn on the flags we care about. */
508 store_unsigned_integer (buf
, 4, dld_flags
);
509 status
= target_write_memory (anaddr
, buf
, 4);
512 error ("Unable to write __dld_flags\n");
516 /* Now find the address of _start and set a breakpoint there. */
517 msymbol
= lookup_minimal_symbol ("_start", NULL
, symfile_objfile
);
520 error ("Unable to find _start symbol in object file.\n");
524 anaddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
525 if (target_insert_breakpoint (anaddr
, shadow_contents
))
527 error ("Unable to set breakpoint at _start.\n");
531 /* Wipe out all knowledge of old shared libraries since their
532 mapping can change from one exec to another! */
535 struct so_list
*temp
;
537 free_objfile (so_list_head
->objfile
);
540 so_list_head
= temp
->next
;
543 /* Start the process again and wait for it to hit our breakpoint. */
544 clear_proceed_status ();
545 stop_soon_quietly
= 1;
546 stop_signal
= TARGET_SIGNAL_0
;
549 target_resume (-1, 0, stop_signal
);
550 wait_for_inferior ();
552 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
553 stop_soon_quietly
= 0;
555 /* All the libraries should be mapped in now. Remove our breakpoint and
556 read in the symbol tables from the shared libraries. */
557 if (target_remove_breakpoint (anaddr
, shadow_contents
))
559 error ("Unable to remove breakpoint at _start.\n");
563 if (auto_solib_add_at_startup
)
564 som_solib_add ((char *) 0, 0, (struct target_ops
*) 0);
567 /* Return the GOT value for the shared library in which ADDR belongs. If
568 ADDR isn't in any known shared library, return zero. */
571 som_solib_get_got_by_pc (addr
)
574 struct so_list
*so_list
= so_list_head
;
575 CORE_ADDR got_value
= 0;
579 if (so_list
->som_solib
.text_addr
<= addr
580 && so_list
->som_solib
.text_end
> addr
)
582 got_value
= so_list
->som_solib
.got_value
;
585 so_list
= so_list
->next
;
591 som_solib_section_offsets (objfile
, offsets
)
592 struct objfile
*objfile
;
593 struct section_offsets
*offsets
;
595 struct so_list
*so_list
= so_list_head
;
599 /* Oh what a pain! We need the offsets before so_list->objfile
600 is valid. The BFDs will never match. Make a best guess. */
601 if (strstr (objfile
->name
, so_list
->som_solib
.name
))
603 asection
*private_section
;
605 /* The text offset is easy. */
606 ANOFFSET (offsets
, SECT_OFF_TEXT
)
607 = (so_list
->som_solib
.text_addr
608 - so_list
->som_solib
.text_link_addr
);
609 ANOFFSET (offsets
, SECT_OFF_RODATA
)
610 = ANOFFSET (offsets
, SECT_OFF_TEXT
);
612 /* We should look at presumed_dp in the SOM header, but
613 that's not easily available. This should be OK though. */
614 private_section
= bfd_get_section_by_name (objfile
->obfd
,
616 if (!private_section
)
618 warning ("Unable to find $PRIVATE$ in shared library!");
619 ANOFFSET (offsets
, SECT_OFF_DATA
) = 0;
620 ANOFFSET (offsets
, SECT_OFF_BSS
) = 0;
623 ANOFFSET (offsets
, SECT_OFF_DATA
)
624 = (so_list
->som_solib
.data_start
- private_section
->vma
);
625 ANOFFSET (offsets
, SECT_OFF_BSS
)
626 = ANOFFSET (offsets
, SECT_OFF_DATA
);
629 so_list
= so_list
->next
;
634 /* Dump information about all the currently loaded shared libraries. */
637 som_sharedlibrary_info_command (ignore
, from_tty
)
641 struct so_list
*so_list
= so_list_head
;
643 if (exec_bfd
== NULL
)
645 printf_unfiltered ("no exec file.\n");
651 printf_unfiltered ("No shared libraries loaded at this time.\n");
655 printf_unfiltered ("Shared Object Libraries\n");
656 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
657 " flags", " tstart", " tend", " dstart", " dend", " dlt");
662 flags
= so_list
->som_solib
.struct_version
<< 24;
663 flags
|= so_list
->som_solib
.bind_mode
<< 16;
664 flags
|= so_list
->som_solib
.library_version
;
665 printf_unfiltered ("%s\n", so_list
->som_solib
.name
);
666 printf_unfiltered (" %-12s", local_hex_string_custom (flags
, "08l"));
667 printf_unfiltered ("%-12s",
668 local_hex_string_custom (so_list
->som_solib
.text_addr
, "08l"));
669 printf_unfiltered ("%-12s",
670 local_hex_string_custom (so_list
->som_solib
.text_end
, "08l"));
671 printf_unfiltered ("%-12s",
672 local_hex_string_custom (so_list
->som_solib
.data_start
, "08l"));
673 printf_unfiltered ("%-12s",
674 local_hex_string_custom (so_list
->som_solib
.data_end
, "08l"));
675 printf_unfiltered ("%-12s\n",
676 local_hex_string_custom (so_list
->som_solib
.got_value
, "08l"));
677 so_list
= so_list
->next
;
682 som_solib_sharedlibrary_command (args
, from_tty
)
687 som_solib_add (args
, from_tty
, (struct target_ops
*) 0);
691 _initialize_som_solib ()
693 add_com ("sharedlibrary", class_files
, som_solib_sharedlibrary_command
,
694 "Load shared object library symbols for files matching REGEXP.");
695 add_info ("sharedlibrary", som_sharedlibrary_info_command
,
696 "Status of loaded shared object libraries.");
698 (add_set_cmd ("auto-solib-add", class_support
, var_zinteger
,
699 (char *) &auto_solib_add_at_startup
,
700 "Set autoloading of shared library symbols at startup.\n\
701 If nonzero, symbols from all shared object libraries will be loaded\n\
702 automatically when the inferior begins execution. Otherwise, symbols\n\
703 must be loaded manually, using `sharedlibrary'.",