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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 Written by the Center for Software Science at the Univerity of Utah
21 and by Cygnus Support. */
32 #include "breakpoint.h"
39 * Relocate data addresses in the shared library.
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;
119 /* If we're debugging a core file, or have attached to a running
120 process, then som_solib_create_inferior_hook will not have been
123 We need to first determine if we're dealing with a dynamically
124 linked executable. If not, then return without an error or warning.
126 We also need to examine __dld_flags to determine if the shared library
127 list is valid and to determine if the libraries have been privately
129 /* First see if the objfile was dynamically linked. */
130 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
134 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
135 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
138 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *) NULL);
141 error ("Unable to find __dld_flags symbol in object file.\n");
145 addr = SYMBOL_VALUE_ADDRESS (msymbol);
146 /* Read the current contents. */
147 status = target_read_memory (addr, buf, 4);
150 error ("Unable to read __dld_flags\n");
153 dld_flags = extract_unsigned_integer (buf, 4);
155 /* __dld_list may not be valid. If it's not valid tell the user. */
156 if ((dld_flags & 4) == 0)
158 error ("__dld_list is not valid according to __dld_flags.\n");
162 /* If the libraries were not mapped private, warn the user. */
163 if ((dld_flags & 1) == 0)
164 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
166 msymbol = lookup_minimal_symbol ("__dld_list", (struct objfile *) NULL);
169 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
170 but the data is still available if you know where to look. */
171 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *)NULL);
174 error ("Unable to find dynamic library list.\n");
177 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
180 addr = SYMBOL_VALUE_ADDRESS (msymbol);
182 status = target_read_memory (addr, buf, 4);
185 error ("Unable to find dynamic library list.\n");
189 addr = extract_unsigned_integer (buf, 4);
191 /* If addr is zero, then we're using an old dynamic loader which
192 doesn't maintain __dld_list. We'll have to use a completely
193 different approach to get shared library information. */
197 /* Using the information in __dld_list is the preferred method
198 to get at shared library information. It doesn't depend on
199 any functions in /usr/lib/end.o and has a chance of working
200 with hpux10 when it is released. */
201 status = target_read_memory (addr, buf, 4);
204 error ("Unable to find dynamic library list.\n");
208 /* addr now holds the address of the first entry in the dynamic
210 addr = extract_unsigned_integer (buf, 4);
212 /* Now that we have a pointer to the dynamic library list, walk
213 through it and add the symbols for each library.
215 Skip the first entry since it's our executable. */
216 status = target_read_memory (addr + 36, buf, 4);
220 addr = extract_unsigned_integer (buf, 4);
222 so_list_tail = so_list_head;
223 /* Find the end of the list of shared objects. */
224 while (so_list_tail && so_list_tail->next)
225 so_list_tail = so_list_tail->next;
229 CORE_ADDR name_addr, text_addr;
230 unsigned int name_len;
232 struct so_list *new_so;
233 struct section_table *p;
238 /* Get a pointer to the name of this library. */
239 status = target_read_memory (addr, buf, 4);
243 name_addr = extract_unsigned_integer (buf, 4);
247 target_read_memory (name_addr + name_len, buf, 1);
255 name = alloca (name_len);
256 status = target_read_memory (name_addr, name, name_len);
260 name = obsavestring (name, name_len - 1,
261 &symfile_objfile->symbol_obstack);
263 status = target_read_memory (addr + 8, buf, 4);
267 text_addr = extract_unsigned_integer (buf, 4);
270 new_so = (struct so_list *) malloc (sizeof (struct so_list));
271 memset ((char *)new_so, 0, sizeof (struct so_list));
272 if (so_list_head == NULL)
274 so_list_head = new_so;
275 so_list_tail = new_so;
279 so_list_tail->next = new_so;
280 so_list_tail = new_so;
283 /* Fill in all the entries in GDB's shared library list. */
284 new_so->som_solib.name = name;
285 status = target_read_memory (addr + 4, buf, 4);
289 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
290 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
291 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
292 new_so->som_solib.text_addr = text_addr;
294 status = target_read_memory (addr + 12, buf, 4);
298 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
300 status = target_read_memory (addr + 16, buf, 4);
304 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
306 status = target_read_memory (addr + 20, buf, 4);
310 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
312 status = target_read_memory (addr + 24, buf, 4);
316 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
318 status = target_read_memory (addr + 28, buf, 4);
322 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
324 status = target_read_memory (addr + 32, buf, 4);
328 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
330 status = target_read_memory (addr + 36, buf, 4);
334 new_so->som_solib.next = (void *)extract_unsigned_integer (buf, 4);
335 addr = (CORE_ADDR)new_so->som_solib.next;
337 new_so->objfile = symbol_file_add (name, from_tty, text_addr, 0, 0, 0);
338 new_so->abfd = bfd_openr (name, gnutarget);
339 new_so->abfd->cacheable = true;
341 if (!bfd_check_format (new_so->abfd, bfd_object))
343 error ("\"%s\": not in executable format: %s.",
344 name, bfd_errmsg (bfd_get_error ()));
347 /* Now we need to build a section table for this library since
348 we might be debugging a core file from a dynamically linked
349 executable in which the libraries were not privately mapped. */
350 if (build_section_table (new_so->abfd,
352 &new_so->sections_end))
354 error ("Unable to build section table for shared library\n.");
358 /* Relocate all the sections based on where they got loaded. */
359 for (p = new_so->sections; p < new_so->sections_end; p++)
361 if (p->the_bfd_section->flags & SEC_CODE)
363 p->addr += text_addr - new_so->som_solib.text_link_addr;
364 p->endaddr += text_addr - new_so->som_solib.text_link_addr;
366 else if (p->the_bfd_section->flags & SEC_DATA)
368 p->addr += new_so->som_solib.data_start;
369 p->endaddr += new_so->som_solib.data_start;
373 /* Now see if we need to map in the text and data for this shared
374 library (for example debugging a core file which does not use
375 private shared libraries.).
377 Carefully peek at the first text address in the library. If the
378 read succeeds, then the libraries were privately mapped and were
379 included in the core dump file.
381 If the peek failed, then the libraries were not privately mapped
382 and are not in the core file, we'll have to read them in ourselves. */
383 status = target_read_memory (text_addr, buf, 4);
388 /* Add sections from the shared library to the core target. */
389 if (target->to_sections)
391 old = target->to_sections_end - target->to_sections;
392 target->to_sections = (struct section_table *)
393 xrealloc ((char *)target->to_sections,
394 ((sizeof (struct section_table))
395 * (old + bfd_count_sections (new_so->abfd))));
400 target->to_sections = (struct section_table *)
401 xmalloc ((sizeof (struct section_table))
402 * bfd_count_sections (new_so->abfd));
404 target->to_sections_end = (target->to_sections
405 + old + bfd_count_sections (new_so->abfd));
406 memcpy ((char *)(target->to_sections + old),
408 ((sizeof (struct section_table))
409 * bfd_count_sections (new_so->abfd)));
413 /* Getting new symbols may change our opinion about what is
415 reinit_frame_cache ();
419 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
423 error ("Error while reading dynamic library list.\n");
428 /* This hook gets called just before the first instruction in the
429 inferior process is executed.
431 This is our opportunity to set magic flags in the inferior so
432 that GDB can be notified when a shared library is mapped in and
433 to tell the dynamic linker that a private copy of the library is
434 needed (so GDB can set breakpoints in the library).
436 __dld_flags is the location of the magic flags; as of this implementation
437 there are 3 flags of interest:
439 bit 0 when set indicates that private copies of the libraries are needed
440 bit 1 when set indicates that the callback hook routine is valid
441 bit 2 when set indicates that the dynamic linker should maintain the
442 __dld_list structure when loading/unloading libraries.
444 Note that shared libraries are not mapped in at this time, so we have
445 run the inferior until the libraries are mapped in. Typically this
446 means running until the "_start" is called. */
449 som_solib_create_inferior_hook()
451 struct minimal_symbol *msymbol;
452 unsigned int dld_flags, status;
453 asection *shlib_info;
454 char shadow_contents[BREAKPOINT_MAX], buf[4];
457 /* First see if the objfile was dynamically linked. */
458 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
462 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
463 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
466 /* Get the address of __dld_flags, if no such symbol exists, then we can
467 not debug the shared code. */
468 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *) NULL);
471 error ("Unable to find __dld_flags symbol in object file.\n");
475 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
476 /* Read the current contents. */
477 status = target_read_memory (anaddr, buf, 4);
480 error ("Unable to read __dld_flags\n");
483 dld_flags = extract_unsigned_integer (buf, 4);
485 /* Turn on the flags we care about. */
487 store_unsigned_integer (buf, 4, dld_flags);
488 status = target_write_memory (anaddr, buf, 4);
491 error ("Unable to write __dld_flags\n");
495 /* Now find the address of _start and set a breakpoint there. */
496 msymbol = lookup_minimal_symbol ("_start", symfile_objfile);
499 error ("Unable to find _start symbol in object file.\n");
503 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
504 if (target_insert_breakpoint (anaddr, shadow_contents))
506 error ("Unable to set breakpoint at _start.\n");
510 /* Wipe out all knowledge of old shared libraries since their
511 mapping can change from one exec to another! */
514 struct so_list *temp;
516 free_objfile (so_list_head->objfile);
519 so_list_head = temp->next;
522 /* Start the process again and wait for it to hit our breakpoint. */
523 clear_proceed_status ();
524 stop_soon_quietly = 1;
525 stop_signal = TARGET_SIGNAL_0;
528 target_resume (-1, 0, stop_signal);
529 wait_for_inferior ();
531 while (stop_signal != TARGET_SIGNAL_TRAP);
532 stop_soon_quietly = 0;
534 /* All the libraries should be mapped in now. Remove our breakpoint and
535 read in the symbol tables from the shared libraries. */
536 if (target_remove_breakpoint (anaddr, shadow_contents))
538 error ("Unable to remove breakpoint at _start.\n");
542 som_solib_add ((char *) 0, 0, (struct target_ops *) 0);
545 /* Return the GOT value for the shared library in which ADDR belongs. If
546 ADDR isn't in any known shared library, return zero. */
549 som_solib_get_got_by_pc (addr)
552 struct so_list *so_list = so_list_head;
553 CORE_ADDR got_value = 0;
557 if (so_list->som_solib.text_addr <= addr
558 && so_list->som_solib.text_end > addr)
560 got_value = so_list->som_solib.got_value;
563 so_list = so_list->next;
568 /* Dump information about all the currently loaded shared libraries. */
571 som_sharedlibrary_info_command (ignore, from_tty)
575 struct so_list *so_list = so_list_head;
577 if (exec_bfd == NULL)
579 printf_unfiltered ("no exec file.\n");
585 printf_unfiltered ("No shared libraries loaded at this time.\n");
589 printf_unfiltered ("Shared Object Libraries\n");
590 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
591 " flags", " tstart", " tend", " dstart", " dend", " dlt");
596 flags = so_list->som_solib.struct_version << 24;
597 flags |= so_list->som_solib.bind_mode << 16;
598 flags |= so_list->som_solib.library_version;
599 printf_unfiltered ("%s\n", so_list->som_solib.name);
600 printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l"));
601 printf_unfiltered ("%-12s",
602 local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
603 printf_unfiltered ("%-12s",
604 local_hex_string_custom (so_list->som_solib.text_end, "08l"));
605 printf_unfiltered ("%-12s",
606 local_hex_string_custom (so_list->som_solib.data_start, "08l"));
607 printf_unfiltered ("%-12s",
608 local_hex_string_custom (so_list->som_solib.data_end, "08l"));
609 printf_unfiltered ("%-12s\n",
610 local_hex_string_custom (so_list->som_solib.got_value, "08l"));
611 so_list = so_list->next;
616 _initialize_som_solib ()
618 add_info ("sharedlibrary", som_sharedlibrary_info_command,
619 "Status of loaded shared object libraries.");