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);
410 /* We must update the to_sections field in the core_ops structure
411 here, otherwise we dereference a potential dangling pointer
412 for each call to target_read/write_memory within this routine. */
413 update_coreops
= core_ops
.to_sections
== target
->to_sections
;
415 new = new_so
->sections_end
- new_so
->sections
;
416 /* Add sections from the shared library to the core target. */
417 if (target
->to_sections
)
419 old
= target
->to_sections_end
- target
->to_sections
;
420 target
->to_sections
= (struct section_table
*)
421 xrealloc ((char *)target
->to_sections
,
422 ((sizeof (struct section_table
)) * (old
+ new)));
427 target
->to_sections
= (struct section_table
*)
428 xmalloc ((sizeof (struct section_table
)) * new);
430 target
->to_sections_end
= (target
->to_sections
+ old
+ new);
432 /* Update the to_sections field in the core_ops structure
436 core_ops
.to_sections
= target
->to_sections
;
437 core_ops
.to_sections_end
= target
->to_sections_end
;
440 /* Copy over the old data before it gets clobbered. */
441 memcpy ((char *)(target
->to_sections
+ old
),
443 ((sizeof (struct section_table
)) * new));
447 /* Getting new symbols may change our opinion about what is
449 reinit_frame_cache ();
453 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
457 error ("Error while reading dynamic library list.\n");
462 /* This hook gets called just before the first instruction in the
463 inferior process is executed.
465 This is our opportunity to set magic flags in the inferior so
466 that GDB can be notified when a shared library is mapped in and
467 to tell the dynamic linker that a private copy of the library is
468 needed (so GDB can set breakpoints in the library).
470 __dld_flags is the location of the magic flags; as of this implementation
471 there are 3 flags of interest:
473 bit 0 when set indicates that private copies of the libraries are needed
474 bit 1 when set indicates that the callback hook routine is valid
475 bit 2 when set indicates that the dynamic linker should maintain the
476 __dld_list structure when loading/unloading libraries.
478 Note that shared libraries are not mapped in at this time, so we have
479 run the inferior until the libraries are mapped in. Typically this
480 means running until the "_start" is called. */
483 som_solib_create_inferior_hook()
485 struct minimal_symbol
*msymbol
;
486 unsigned int dld_flags
, status
;
487 asection
*shlib_info
;
488 char shadow_contents
[BREAKPOINT_MAX
], buf
[4];
491 if (symfile_objfile
== NULL
)
494 /* First see if the objfile was dynamically linked. */
495 shlib_info
= bfd_get_section_by_name (symfile_objfile
->obfd
, "$SHLIB_INFO$");
499 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
500 if (bfd_section_size (symfile_objfile
->obfd
, shlib_info
) == 0)
503 /* Get the address of __dld_flags, if no such symbol exists, then we can
504 not debug the shared code. */
505 msymbol
= lookup_minimal_symbol ("__dld_flags", NULL
, NULL
);
508 error ("Unable to find __dld_flags symbol in object file.\n");
512 anaddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
513 /* Read the current contents. */
514 status
= target_read_memory (anaddr
, buf
, 4);
517 error ("Unable to read __dld_flags\n");
520 dld_flags
= extract_unsigned_integer (buf
, 4);
522 /* Turn on the flags we care about. */
524 store_unsigned_integer (buf
, 4, dld_flags
);
525 status
= target_write_memory (anaddr
, buf
, 4);
528 error ("Unable to write __dld_flags\n");
532 /* Now find the address of _start and set a breakpoint there. */
533 msymbol
= lookup_minimal_symbol ("_start", NULL
, symfile_objfile
);
536 error ("Unable to find _start symbol in object file.\n");
540 anaddr
= SYMBOL_VALUE_ADDRESS (msymbol
);
541 if (target_insert_breakpoint (anaddr
, shadow_contents
))
543 error ("Unable to set breakpoint at _start.\n");
547 /* Wipe out all knowledge of old shared libraries since their
548 mapping can change from one exec to another! */
551 struct so_list
*temp
;
553 free_objfile (so_list_head
->objfile
);
556 so_list_head
= temp
->next
;
559 /* Start the process again and wait for it to hit our breakpoint. */
560 clear_proceed_status ();
561 stop_soon_quietly
= 1;
562 stop_signal
= TARGET_SIGNAL_0
;
565 target_resume (-1, 0, stop_signal
);
566 wait_for_inferior ();
568 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
569 stop_soon_quietly
= 0;
571 /* All the libraries should be mapped in now. Remove our breakpoint and
572 read in the symbol tables from the shared libraries. */
573 if (target_remove_breakpoint (anaddr
, shadow_contents
))
575 error ("Unable to remove breakpoint at _start.\n");
579 if (auto_solib_add_at_startup
)
580 som_solib_add ((char *) 0, 0, (struct target_ops
*) 0);
583 /* Return the GOT value for the shared library in which ADDR belongs. If
584 ADDR isn't in any known shared library, return zero. */
587 som_solib_get_got_by_pc (addr
)
590 struct so_list
*so_list
= so_list_head
;
591 CORE_ADDR got_value
= 0;
595 if (so_list
->som_solib
.text_addr
<= addr
596 && so_list
->som_solib
.text_end
> addr
)
598 got_value
= so_list
->som_solib
.got_value
;
601 so_list
= so_list
->next
;
607 som_solib_section_offsets (objfile
, offsets
)
608 struct objfile
*objfile
;
609 struct section_offsets
*offsets
;
611 struct so_list
*so_list
= so_list_head
;
615 /* Oh what a pain! We need the offsets before so_list->objfile
616 is valid. The BFDs will never match. Make a best guess. */
617 if (strstr (objfile
->name
, so_list
->som_solib
.name
))
619 asection
*private_section
;
621 /* The text offset is easy. */
622 ANOFFSET (offsets
, SECT_OFF_TEXT
)
623 = (so_list
->som_solib
.text_addr
624 - so_list
->som_solib
.text_link_addr
);
625 ANOFFSET (offsets
, SECT_OFF_RODATA
)
626 = ANOFFSET (offsets
, SECT_OFF_TEXT
);
628 /* We should look at presumed_dp in the SOM header, but
629 that's not easily available. This should be OK though. */
630 private_section
= bfd_get_section_by_name (objfile
->obfd
,
632 if (!private_section
)
634 warning ("Unable to find $PRIVATE$ in shared library!");
635 ANOFFSET (offsets
, SECT_OFF_DATA
) = 0;
636 ANOFFSET (offsets
, SECT_OFF_BSS
) = 0;
639 ANOFFSET (offsets
, SECT_OFF_DATA
)
640 = (so_list
->som_solib
.data_start
- private_section
->vma
);
641 ANOFFSET (offsets
, SECT_OFF_BSS
)
642 = ANOFFSET (offsets
, SECT_OFF_DATA
);
645 so_list
= so_list
->next
;
650 /* Dump information about all the currently loaded shared libraries. */
653 som_sharedlibrary_info_command (ignore
, from_tty
)
657 struct so_list
*so_list
= so_list_head
;
659 if (exec_bfd
== NULL
)
661 printf_unfiltered ("no exec file.\n");
667 printf_unfiltered ("No shared libraries loaded at this time.\n");
671 printf_unfiltered ("Shared Object Libraries\n");
672 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
673 " flags", " tstart", " tend", " dstart", " dend", " dlt");
678 flags
= so_list
->som_solib
.struct_version
<< 24;
679 flags
|= so_list
->som_solib
.bind_mode
<< 16;
680 flags
|= so_list
->som_solib
.library_version
;
681 printf_unfiltered ("%s\n", so_list
->som_solib
.name
);
682 printf_unfiltered (" %-12s", local_hex_string_custom (flags
, "08l"));
683 printf_unfiltered ("%-12s",
684 local_hex_string_custom (so_list
->som_solib
.text_addr
, "08l"));
685 printf_unfiltered ("%-12s",
686 local_hex_string_custom (so_list
->som_solib
.text_end
, "08l"));
687 printf_unfiltered ("%-12s",
688 local_hex_string_custom (so_list
->som_solib
.data_start
, "08l"));
689 printf_unfiltered ("%-12s",
690 local_hex_string_custom (so_list
->som_solib
.data_end
, "08l"));
691 printf_unfiltered ("%-12s\n",
692 local_hex_string_custom (so_list
->som_solib
.got_value
, "08l"));
693 so_list
= so_list
->next
;
698 som_solib_sharedlibrary_command (args
, from_tty
)
703 som_solib_add (args
, from_tty
, (struct target_ops
*) 0);
707 _initialize_som_solib ()
709 add_com ("sharedlibrary", class_files
, som_solib_sharedlibrary_command
,
710 "Load shared object library symbols for files matching REGEXP.");
711 add_info ("sharedlibrary", som_sharedlibrary_info_command
,
712 "Status of loaded shared object libraries.");
714 (add_set_cmd ("auto-solib-add", class_support
, var_zinteger
,
715 (char *) &auto_solib_add_at_startup
,
716 "Set autoloading of shared library symbols at startup.\n\
717 If nonzero, symbols from all shared object libraries will be loaded\n\
718 automatically when the inferior begins execution. Otherwise, symbols\n\
719 must be loaded manually, using `sharedlibrary'.",