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 /* If true, then shared library symbols will be added automatically
100 when the inferior is created. This is almost always what users
101 will want to have happen; but for very large programs, the startup
102 time will be excessive, and so if this is a problem, the user can
103 clear this flag and then add the shared library symbols as needed.
104 Note that there is a potential for confusion, since if the shared
105 library symbols are not loaded, commands like "info fun" will *not*
106 report all the functions that are actually present. */
108 int auto_solib_add_at_startup = 1;
110 static struct so_list *so_list_head;
112 static void som_sharedlibrary_info_command PARAMS ((char *, int));
114 /* Add symbols from shared libraries into the symtab list. */
117 som_solib_add (arg_string, from_tty, target)
120 struct target_ops *target;
122 struct minimal_symbol *msymbol;
123 struct so_list *so_list_tail;
125 asection *shlib_info;
127 unsigned int dld_flags;
128 char buf[4], *re_err;
130 /* First validate our arguments. */
131 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
133 error ("Invalid regexp: %s", re_err);
136 /* If we're debugging a core file, or have attached to a running
137 process, then som_solib_create_inferior_hook will not have been
140 We need to first determine if we're dealing with a dynamically
141 linked executable. If not, then return without an error or warning.
143 We also need to examine __dld_flags to determine if the shared library
144 list is valid and to determine if the libraries have been privately
146 if (symfile_objfile == NULL)
149 /* First see if the objfile was dynamically linked. */
150 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
154 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
155 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
158 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
161 error ("Unable to find __dld_flags symbol in object file.\n");
165 addr = SYMBOL_VALUE_ADDRESS (msymbol);
166 /* Read the current contents. */
167 status = target_read_memory (addr, buf, 4);
170 error ("Unable to read __dld_flags\n");
173 dld_flags = extract_unsigned_integer (buf, 4);
175 /* __dld_list may not be valid. If it's not valid tell the user. */
176 if ((dld_flags & 4) == 0)
178 error ("__dld_list is not valid according to __dld_flags.\n");
182 /* If the libraries were not mapped private, warn the user. */
183 if ((dld_flags & 1) == 0)
184 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
186 msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL);
189 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
190 but the data is still available if you know where to look. */
191 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
194 error ("Unable to find dynamic library list.\n");
197 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
200 addr = SYMBOL_VALUE_ADDRESS (msymbol);
202 status = target_read_memory (addr, buf, 4);
205 error ("Unable to find dynamic library list.\n");
209 addr = extract_unsigned_integer (buf, 4);
211 /* If addr is zero, then we're using an old dynamic loader which
212 doesn't maintain __dld_list. We'll have to use a completely
213 different approach to get shared library information. */
217 /* Using the information in __dld_list is the preferred method
218 to get at shared library information. It doesn't depend on
219 any functions in /usr/lib/end.o and has a chance of working
220 with hpux10 when it is released. */
221 status = target_read_memory (addr, buf, 4);
224 error ("Unable to find dynamic library list.\n");
228 /* addr now holds the address of the first entry in the dynamic
230 addr = extract_unsigned_integer (buf, 4);
232 /* Now that we have a pointer to the dynamic library list, walk
233 through it and add the symbols for each library. */
235 so_list_tail = so_list_head;
236 /* Find the end of the list of shared objects. */
237 while (so_list_tail && so_list_tail->next)
238 so_list_tail = so_list_tail->next;
242 CORE_ADDR name_addr, text_addr;
243 unsigned int name_len;
245 struct so_list *new_so;
246 struct so_list *so_list = so_list_head;
247 struct section_table *p;
252 /* Get a pointer to the name of this library. */
253 status = target_read_memory (addr, buf, 4);
257 name_addr = extract_unsigned_integer (buf, 4);
261 target_read_memory (name_addr + name_len, buf, 1);
269 name = alloca (name_len);
270 status = target_read_memory (name_addr, name, name_len);
274 /* See if we've already loaded something with this name. */
277 if (!strcmp (so_list->som_solib.name, name))
279 so_list = so_list->next;
282 /* We've already loaded this one or it's the main program, skip it. */
283 if (so_list || !strcmp (name, symfile_objfile->name))
285 status = target_read_memory (addr + 36, buf, 4);
289 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
293 name = obsavestring (name, name_len - 1,
294 &symfile_objfile->symbol_obstack);
296 status = target_read_memory (addr + 8, buf, 4);
300 text_addr = extract_unsigned_integer (buf, 4);
303 new_so = (struct so_list *) malloc (sizeof (struct so_list));
304 memset ((char *)new_so, 0, sizeof (struct so_list));
305 if (so_list_head == NULL)
307 so_list_head = new_so;
308 so_list_tail = new_so;
312 so_list_tail->next = new_so;
313 so_list_tail = new_so;
316 /* Fill in all the entries in GDB's shared library list. */
317 new_so->som_solib.name = name;
318 status = target_read_memory (addr + 4, buf, 4);
322 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
323 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
324 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
325 new_so->som_solib.text_addr = text_addr;
327 status = target_read_memory (addr + 12, buf, 4);
331 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
333 status = target_read_memory (addr + 16, buf, 4);
337 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
339 status = target_read_memory (addr + 20, buf, 4);
343 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
345 status = target_read_memory (addr + 24, buf, 4);
349 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
351 status = target_read_memory (addr + 28, buf, 4);
355 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
357 status = target_read_memory (addr + 32, buf, 4);
361 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
363 status = target_read_memory (addr + 36, buf, 4);
367 new_so->som_solib.next = (void *)extract_unsigned_integer (buf, 4);
368 addr = (CORE_ADDR)new_so->som_solib.next;
370 new_so->objfile = symbol_file_add (name, from_tty, text_addr, 0, 0, 0);
371 new_so->abfd = new_so->objfile->obfd;
373 if (!bfd_check_format (new_so->abfd, bfd_object))
375 error ("\"%s\": not in executable format: %s.",
376 name, bfd_errmsg (bfd_get_error ()));
379 /* Now we need to build a section table for this library since
380 we might be debugging a core file from a dynamically linked
381 executable in which the libraries were not privately mapped. */
382 if (build_section_table (new_so->abfd,
384 &new_so->sections_end))
386 error ("Unable to build section table for shared library\n.");
390 /* Relocate all the sections based on where they got loaded. */
391 for (p = new_so->sections; p < new_so->sections_end; p++)
393 if (p->the_bfd_section->flags & SEC_CODE)
395 p->addr += text_addr - new_so->som_solib.text_link_addr;
396 p->endaddr += text_addr - new_so->som_solib.text_link_addr;
398 else if (p->the_bfd_section->flags & SEC_DATA)
400 p->addr += new_so->som_solib.data_start;
401 p->endaddr += new_so->som_solib.data_start;
405 /* Now see if we need to map in the text and data for this shared
406 library (for example debugging a core file which does not use
407 private shared libraries.).
409 Carefully peek at the first text address in the library. If the
410 read succeeds, then the libraries were privately mapped and were
411 included in the core dump file.
413 If the peek failed, then the libraries were not privately mapped
414 and are not in the core file, we'll have to read them in ourselves. */
415 status = target_read_memory (text_addr, buf, 4);
420 new = new_so->sections_end - new_so->sections;
421 /* Add sections from the shared library to the core target. */
422 if (target->to_sections)
424 old = target->to_sections_end - target->to_sections;
425 target->to_sections = (struct section_table *)
426 xrealloc ((char *)target->to_sections,
427 ((sizeof (struct section_table)) * (old + new)));
432 target->to_sections = (struct section_table *)
433 xmalloc ((sizeof (struct section_table)) * new);
435 target->to_sections_end = (target->to_sections + old + new);
436 memcpy ((char *)(target->to_sections + old),
438 ((sizeof (struct section_table)) * new));
442 /* Getting new symbols may change our opinion about what is
444 reinit_frame_cache ();
448 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
452 error ("Error while reading dynamic library list.\n");
457 /* This hook gets called just before the first instruction in the
458 inferior process is executed.
460 This is our opportunity to set magic flags in the inferior so
461 that GDB can be notified when a shared library is mapped in and
462 to tell the dynamic linker that a private copy of the library is
463 needed (so GDB can set breakpoints in the library).
465 __dld_flags is the location of the magic flags; as of this implementation
466 there are 3 flags of interest:
468 bit 0 when set indicates that private copies of the libraries are needed
469 bit 1 when set indicates that the callback hook routine is valid
470 bit 2 when set indicates that the dynamic linker should maintain the
471 __dld_list structure when loading/unloading libraries.
473 Note that shared libraries are not mapped in at this time, so we have
474 run the inferior until the libraries are mapped in. Typically this
475 means running until the "_start" is called. */
478 som_solib_create_inferior_hook()
480 struct minimal_symbol *msymbol;
481 unsigned int dld_flags, status;
482 asection *shlib_info;
483 char shadow_contents[BREAKPOINT_MAX], buf[4];
486 if (symfile_objfile == NULL)
489 /* First see if the objfile was dynamically linked. */
490 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
494 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
495 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
498 /* Get the address of __dld_flags, if no such symbol exists, then we can
499 not debug the shared code. */
500 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
503 error ("Unable to find __dld_flags symbol in object file.\n");
507 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
508 /* Read the current contents. */
509 status = target_read_memory (anaddr, buf, 4);
512 error ("Unable to read __dld_flags\n");
515 dld_flags = extract_unsigned_integer (buf, 4);
517 /* Turn on the flags we care about. */
519 store_unsigned_integer (buf, 4, dld_flags);
520 status = target_write_memory (anaddr, buf, 4);
523 error ("Unable to write __dld_flags\n");
527 /* Now find the address of _start and set a breakpoint there. */
528 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
531 error ("Unable to find _start symbol in object file.\n");
535 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
536 if (target_insert_breakpoint (anaddr, shadow_contents))
538 error ("Unable to set breakpoint at _start.\n");
542 /* Wipe out all knowledge of old shared libraries since their
543 mapping can change from one exec to another! */
546 struct so_list *temp;
548 free_objfile (so_list_head->objfile);
551 so_list_head = temp->next;
554 /* Start the process again and wait for it to hit our breakpoint. */
555 clear_proceed_status ();
556 stop_soon_quietly = 1;
557 stop_signal = TARGET_SIGNAL_0;
560 target_resume (-1, 0, stop_signal);
561 wait_for_inferior ();
563 while (stop_signal != TARGET_SIGNAL_TRAP);
564 stop_soon_quietly = 0;
566 /* All the libraries should be mapped in now. Remove our breakpoint and
567 read in the symbol tables from the shared libraries. */
568 if (target_remove_breakpoint (anaddr, shadow_contents))
570 error ("Unable to remove breakpoint at _start.\n");
574 if (auto_solib_add_at_startup)
575 som_solib_add ((char *) 0, 0, (struct target_ops *) 0);
578 /* Return the GOT value for the shared library in which ADDR belongs. If
579 ADDR isn't in any known shared library, return zero. */
582 som_solib_get_got_by_pc (addr)
585 struct so_list *so_list = so_list_head;
586 CORE_ADDR got_value = 0;
590 if (so_list->som_solib.text_addr <= addr
591 && so_list->som_solib.text_end > addr)
593 got_value = so_list->som_solib.got_value;
596 so_list = so_list->next;
602 som_solib_section_offsets (objfile, offsets)
603 struct objfile *objfile;
604 struct section_offsets *offsets;
606 struct so_list *so_list = so_list_head;
610 /* Oh what a pain! We need the offsets before so_list->objfile
611 is valid. The BFDs will never match. Make a best guess. */
612 if (strstr (objfile->name, so_list->som_solib.name))
614 asection *private_section;
616 /* The text offset is easy. */
617 ANOFFSET (offsets, SECT_OFF_TEXT)
618 = (so_list->som_solib.text_addr
619 - so_list->som_solib.text_link_addr);
620 ANOFFSET (offsets, SECT_OFF_RODATA)
621 = ANOFFSET (offsets, SECT_OFF_TEXT);
623 /* We should look at presumed_dp in the SOM header, but
624 that's not easily available. This should be OK though. */
625 private_section = bfd_get_section_by_name (objfile->obfd,
627 if (!private_section)
629 warning ("Unable to find $PRIVATE$ in shared library!");
630 ANOFFSET (offsets, SECT_OFF_DATA) = 0;
631 ANOFFSET (offsets, SECT_OFF_BSS) = 0;
634 ANOFFSET (offsets, SECT_OFF_DATA)
635 = (so_list->som_solib.data_start - private_section->vma);
636 ANOFFSET (offsets, SECT_OFF_BSS)
637 = ANOFFSET (offsets, SECT_OFF_DATA);
640 so_list = so_list->next;
645 /* Dump information about all the currently loaded shared libraries. */
648 som_sharedlibrary_info_command (ignore, from_tty)
652 struct so_list *so_list = so_list_head;
654 if (exec_bfd == NULL)
656 printf_unfiltered ("no exec file.\n");
662 printf_unfiltered ("No shared libraries loaded at this time.\n");
666 printf_unfiltered ("Shared Object Libraries\n");
667 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
668 " flags", " tstart", " tend", " dstart", " dend", " dlt");
673 flags = so_list->som_solib.struct_version << 24;
674 flags |= so_list->som_solib.bind_mode << 16;
675 flags |= so_list->som_solib.library_version;
676 printf_unfiltered ("%s\n", so_list->som_solib.name);
677 printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l"));
678 printf_unfiltered ("%-12s",
679 local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
680 printf_unfiltered ("%-12s",
681 local_hex_string_custom (so_list->som_solib.text_end, "08l"));
682 printf_unfiltered ("%-12s",
683 local_hex_string_custom (so_list->som_solib.data_start, "08l"));
684 printf_unfiltered ("%-12s",
685 local_hex_string_custom (so_list->som_solib.data_end, "08l"));
686 printf_unfiltered ("%-12s\n",
687 local_hex_string_custom (so_list->som_solib.got_value, "08l"));
688 so_list = so_list->next;
693 som_solib_sharedlibrary_command (args, from_tty)
698 som_solib_add (args, from_tty, (struct target_ops *) 0);
702 _initialize_som_solib ()
704 add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
705 "Load shared object library symbols for files matching REGEXP.");
706 add_info ("sharedlibrary", som_sharedlibrary_info_command,
707 "Status of loaded shared object libraries.");
709 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
710 (char *) &auto_solib_add_at_startup,
711 "Set autoloading of shared library symbols at startup.\n\
712 If nonzero, symbols from all shared object libraries will be loaded\n\
713 automatically when the inferior begins execution. Otherwise, symbols\n\
714 must be loaded manually, using `sharedlibrary'.",