1/* Handle HP ELF shared libraries for GDB, the GNU Debugger.
2
3   Copyright 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation,
4   Inc.
5
6   This file is part of GDB.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330,
21   Boston, MA 02111-1307, USA.
22
23   HP in their infinite stupidity choose not to use standard ELF dynamic
24   linker interfaces.  They also choose not to make their ELF dymamic
25   linker interfaces compatible with the SOM dynamic linker.  The
26   net result is we can not use either of the existing somsolib.c or
27   solib.c.  What a crock.
28
29   Even more disgusting.  This file depends on functions provided only
30   in certain PA64 libraries.  Thus this file is supposed to only be
31   used native.  When will HP ever learn that they need to provide the
32   same functionality in all their libraries!  */
33
34#include <dlfcn.h>
35#include <elf.h>
36#include <elf_hp.h>
37
38#include "defs.h"
39
40#include "frame.h"
41#include "bfd.h"
42#include "libhppa.h"
43#include "gdbcore.h"
44#include "symtab.h"
45#include "breakpoint.h"
46#include "symfile.h"
47#include "objfiles.h"
48#include "inferior.h"
49#include "gdb-stabs.h"
50#include "gdb_stat.h"
51#include "gdbcmd.h"
52#include "language.h"
53#include "regcache.h"
54#include "exec.h"
55
56#include <fcntl.h>
57
58#ifndef O_BINARY
59#define O_BINARY 0
60#endif
61
62static CORE_ADDR bfd_lookup_symbol (bfd *, char *);
63/* This lives in hppa-tdep.c. */
64extern struct unwind_table_entry *find_unwind_entry (CORE_ADDR pc);
65
66/* These ought to be defined in some public interface, but aren't.  They
67   identify dynamic linker events.  */
68#define DLD_CB_LOAD     1
69#define DLD_CB_UNLOAD   0
70
71/* A structure to keep track of all the known shared objects.  */
72struct so_list
73  {
74    bfd *abfd;
75    char *name;
76    struct so_list *next;
77    struct objfile *objfile;
78    CORE_ADDR pa64_solib_desc_addr;
79    struct load_module_desc pa64_solib_desc;
80    struct section_table *sections;
81    struct section_table *sections_end;
82    int loaded;
83  };
84
85static struct so_list *so_list_head;
86
87/* This is the cumulative size in bytes of the symbol tables of all
88   shared objects on the so_list_head list.  (When we say size, here
89   we mean of the information before it is brought into memory and
90   potentially expanded by GDB.)  When adding a new shlib, this value
91   is compared against a threshold size, held by auto_solib_limit (in
92   megabytes).  If adding symbols for the new shlib would cause the
93   total size to exceed the threshold, then the new shlib's symbols
94   are not loaded. */
95static LONGEST pa64_solib_total_st_size;
96
97/* When the threshold is reached for any shlib, we refuse to add
98   symbols for subsequent shlibs, even if those shlibs' symbols would
99   be small enough to fit under the threshold.  Although this may
100   result in one, early large shlib preventing the loading of later,
101   smaller shlibs' symbols, it allows us to issue one informational
102   message.  The alternative, to issue a message for each shlib whose
103   symbols aren't loaded, could be a big annoyance where the threshold
104   is exceeded due to a very large number of shlibs. */
105static int pa64_solib_st_size_threshold_exceeded;
106
107/* When adding fields, be sure to clear them in _initialize_pa64_solib. */
108typedef struct
109  {
110    CORE_ADDR dld_flags_addr;
111    LONGEST dld_flags;
112    struct bfd_section *dyninfo_sect;
113    int have_read_dld_descriptor;
114    int is_valid;
115    CORE_ADDR load_map;
116    CORE_ADDR load_map_addr;
117    struct load_module_desc dld_desc;
118  }
119dld_cache_t;
120
121static dld_cache_t dld_cache;
122
123static void pa64_sharedlibrary_info_command (char *, int);
124
125static void pa64_solib_sharedlibrary_command (char *, int);
126
127static void *pa64_target_read_memory (void *, CORE_ADDR, size_t, int);
128
129static int read_dld_descriptor (struct target_ops *, int readsyms);
130
131static int read_dynamic_info (asection *, dld_cache_t *);
132
133static void add_to_solist (int, char *, int, struct load_module_desc *,
134			   CORE_ADDR, struct target_ops *);
135
136/* When examining the shared library for debugging information we have to
137   look for HP debug symbols, stabs and dwarf2 debug symbols.  */
138static char *pa64_debug_section_names[] = {
139  ".debug_header", ".debug_gntt", ".debug_lntt", ".debug_slt", ".debug_vt",
140  ".stabs", ".stabstr", ".debug_info", ".debug_abbrev", ".debug_aranges",
141  ".debug_macinfo", ".debug_line", ".debug_loc", ".debug_pubnames",
142  ".debug_str", NULL
143};
144
145/* Return a ballbark figure for the amount of memory GDB will need to
146   allocate to read in the debug symbols from FILENAME.  */
147static LONGEST
148pa64_solib_sizeof_symbol_table (char *filename)
149{
150  bfd *abfd;
151  int i;
152  int desc;
153  char *absolute_name;
154  LONGEST st_size = (LONGEST) 0;
155  asection *sect;
156
157  /* We believe that filename was handed to us by the dynamic linker, and
158     is therefore always an absolute path.  */
159  desc = openp (getenv ("PATH"), 1, filename, O_RDONLY | O_BINARY,
160		0, &absolute_name);
161  if (desc < 0)
162    {
163      perror_with_name (filename);
164    }
165  filename = absolute_name;
166
167  abfd = bfd_fdopenr (filename, gnutarget, desc);
168  if (!abfd)
169    {
170      close (desc);
171      make_cleanup (xfree, filename);
172      error ("\"%s\": can't open to read symbols: %s.", filename,
173	     bfd_errmsg (bfd_get_error ()));
174    }
175
176  if (!bfd_check_format (abfd, bfd_object))
177    {
178      bfd_close (abfd);
179      make_cleanup (xfree, filename);
180      error ("\"%s\": can't read symbols: %s.", filename,
181	     bfd_errmsg (bfd_get_error ()));
182    }
183
184  /* Sum the sizes of the various sections that compose debug info. */
185  for (i = 0; pa64_debug_section_names[i] != NULL; i++)
186    {
187      asection *sect;
188
189      sect = bfd_get_section_by_name (abfd, pa64_debug_section_names[i]);
190      if (sect)
191	st_size += (LONGEST)bfd_section_size (abfd, sect);
192    }
193
194  bfd_close (abfd);
195  xfree (filename);
196
197  /* Unfortunately, just summing the sizes of various debug info
198     sections isn't a very accurate measurement of how much heap
199     space the debugger will need to hold them.  It also doesn't
200     account for space needed by linker (aka "minimal") symbols.
201
202     Anecdotal evidence suggests that just summing the sizes of
203     debug-info-related sections understates the heap space needed
204     to represent it internally by about an order of magnitude.
205
206     Since it's not exactly brain surgery we're doing here, rather
207     than attempt to more accurately measure the size of a shlib's
208     symbol table in GDB's heap, we'll just apply a 10x fudge-
209     factor to the debug info sections' size-sum.  No, this doesn't
210     account for minimal symbols in non-debuggable shlibs.  But it
211     all roughly washes out in the end.  */
212  return st_size * (LONGEST) 10;
213}
214
215/* Add a shared library to the objfile list and load its symbols into
216   GDB's symbol table.  */
217static void
218pa64_solib_add_solib_objfile (struct so_list *so, char *name, int from_tty,
219			      CORE_ADDR text_addr)
220{
221  bfd *tmp_bfd;
222  asection *sec;
223  obj_private_data_t *obj_private;
224  struct section_addr_info *section_addrs;
225  struct cleanup *my_cleanups;
226
227  /* We need the BFD so that we can look at its sections.  We open up the
228     file temporarily, then close it when we are done.  */
229  tmp_bfd = bfd_openr (name, gnutarget);
230  if (tmp_bfd == NULL)
231    {
232      perror_with_name (name);
233      return;
234    }
235
236  if (!bfd_check_format (tmp_bfd, bfd_object))
237    {
238      bfd_close (tmp_bfd);
239      error ("\"%s\" is not an object file: %s", name,
240	     bfd_errmsg (bfd_get_error ()));
241    }
242
243
244  /* Undo some braindamage from symfile.c.
245
246     First, symfile.c will subtract the VMA of the first .text section
247     in the shared library that it finds.  Undo that.  */
248  sec = bfd_get_section_by_name (tmp_bfd, ".text");
249  text_addr += bfd_section_vma (tmp_bfd, sec);
250
251  /* Now find the true lowest section in the shared library.  */
252  sec = NULL;
253  bfd_map_over_sections (tmp_bfd, find_lowest_section, &sec);
254
255  if (sec)
256    {
257      /* Subtract out the VMA of the lowest section.  */
258      text_addr -= bfd_section_vma (tmp_bfd, sec);
259
260      /* ??? Add back in the filepos of that lowest section. */
261      text_addr += sec->filepos;
262    }
263
264  section_addrs = alloc_section_addr_info (bfd_count_sections (tmp_bfd));
265  my_cleanups = make_cleanup (xfree, section_addrs);
266
267  /* We are done with the temporary bfd.  Get rid of it and make sure
268     nobody else can us it.  */
269  bfd_close (tmp_bfd);
270  tmp_bfd = NULL;
271
272  /* Now let the generic code load up symbols for this library.  */
273  section_addrs->other[0].addr = text_addr;
274  section_addrs->other[0].name = ".text";
275  so->objfile = symbol_file_add (name, from_tty, section_addrs, 0, OBJF_SHARED);
276  so->abfd = so->objfile->obfd;
277
278  /* Mark this as a shared library and save private data.  */
279  so->objfile->flags |= OBJF_SHARED;
280
281  if (so->objfile->obj_private == NULL)
282    {
283      obj_private = (obj_private_data_t *)
284	obstack_alloc (&so->objfile->objfile_obstack,
285		       sizeof (obj_private_data_t));
286      obj_private->unwind_info = NULL;
287      obj_private->so_info = NULL;
288      so->objfile->obj_private = obj_private;
289    }
290
291  obj_private = (obj_private_data_t *) so->objfile->obj_private;
292  obj_private->so_info = so;
293  obj_private->dp = so->pa64_solib_desc.linkage_ptr;
294  do_cleanups (my_cleanups);
295}
296
297/* Load debugging information for a shared library.  TARGET may be
298   NULL if we are not attaching to a process or reading a core file.  */
299
300static void
301pa64_solib_load_symbols (struct so_list *so, char *name, int from_tty,
302			 CORE_ADDR text_addr, struct target_ops *target)
303{
304  struct section_table *p;
305  asection *sec;
306  int status;
307  char buf[4];
308  CORE_ADDR presumed_data_start;
309
310  if (text_addr == 0)
311    text_addr = so->pa64_solib_desc.text_base;
312
313  pa64_solib_add_solib_objfile (so, name, from_tty, text_addr);
314
315  /* Now we need to build a section table for this library since
316     we might be debugging a core file from a dynamically linked
317     executable in which the libraries were not privately mapped.  */
318  if (build_section_table (so->abfd,
319			   &so->sections,
320			   &so->sections_end))
321    {
322      error ("Unable to build section table for shared library\n.");
323      return;
324    }
325
326  (so->objfile->section_offsets)->offsets[SECT_OFF_TEXT (so->objfile)]
327    = so->pa64_solib_desc.text_base;
328  (so->objfile->section_offsets)->offsets[SECT_OFF_DATA (so->objfile)]
329    = so->pa64_solib_desc.data_base;
330
331  /* Relocate all the sections based on where they got loaded.  */
332  for (p = so->sections; p < so->sections_end; p++)
333    {
334      if (p->the_bfd_section->flags & SEC_CODE)
335	{
336	  p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
337	  p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
338	}
339      else if (p->the_bfd_section->flags & SEC_DATA)
340	{
341	  p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
342	  p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
343	}
344    }
345
346  /* Now see if we need to map in the text and data for this shared
347     library (for example debugging a core file which does not use
348     private shared libraries.).
349
350     Carefully peek at the first text address in the library.  If the
351     read succeeds, then the libraries were privately mapped and were
352     included in the core dump file.
353
354     If the peek failed, then the libraries were not privately mapped
355     and are not in the core file, we'll have to read them in ourselves.  */
356  status = target_read_memory (text_addr, buf, 4);
357  if (status != 0)
358    {
359      int new, old;
360
361      new = so->sections_end - so->sections;
362
363      old = target_resize_to_sections (target, new);
364
365      /* Copy over the old data before it gets clobbered.  */
366      memcpy ((char *) (target->to_sections + old),
367	      so->sections,
368	      ((sizeof (struct section_table)) * new));
369    }
370}
371
372
373/* Add symbols from shared libraries into the symtab list, unless the
374   size threshold specified by auto_solib_limit (in megabytes) would
375   be exceeded.  */
376
377void
378pa64_solib_add (char *arg_string, int from_tty, struct target_ops *target, int readsyms)
379{
380  struct minimal_symbol *msymbol;
381  CORE_ADDR addr;
382  asection *shlib_info;
383  int status;
384  unsigned int dld_flags;
385  char buf[4], *re_err;
386  int threshold_warning_given = 0;
387  int dll_index;
388  struct load_module_desc dll_desc;
389  char *dll_path;
390
391  /* First validate our arguments.  */
392  if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
393    {
394      error ("Invalid regexp: %s", re_err);
395    }
396
397  /* If we're debugging a core file, or have attached to a running
398     process, then pa64_solib_create_inferior_hook will not have been
399     called.
400
401     We need to first determine if we're dealing with a dynamically
402     linked executable.  If not, then return without an error or warning.
403
404     We also need to examine __dld_flags to determine if the shared library
405     list is valid and to determine if the libraries have been privately
406     mapped.  */
407  if (symfile_objfile == NULL)
408    return;
409
410  /* First see if the objfile was dynamically linked.  */
411  shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, ".dynamic");
412  if (!shlib_info)
413    return;
414
415  /* It's got a .dynamic section, make sure it's not empty.  */
416  if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
417    return;
418
419  /* Read in the load map pointer if we have not done so already.  */
420  if (! dld_cache.have_read_dld_descriptor)
421    if (! read_dld_descriptor (target, readsyms))
422      return;
423
424  /* If the libraries were not mapped private, warn the user.  */
425  if ((dld_cache.dld_flags & DT_HP_DEBUG_PRIVATE) == 0)
426    warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
427
428  /* For each shaerd library, add it to the shared library list.  */
429  for (dll_index = 1; ; dll_index++)
430    {
431      /* Read in the load module descriptor.  */
432      if (dlgetmodinfo (dll_index, &dll_desc, sizeof (dll_desc),
433			pa64_target_read_memory, 0, dld_cache.load_map)
434	  == 0)
435	return;
436
437      /* Get the name of the shared library.  */
438      dll_path = (char *)dlgetname (&dll_desc, sizeof (dll_desc),
439			    pa64_target_read_memory,
440			    0, dld_cache.load_map);
441
442      if (!dll_path)
443	error ("pa64_solib_add, unable to read shared library path.");
444
445      add_to_solist (from_tty, dll_path, readsyms, &dll_desc, 0, target);
446    }
447}
448
449
450/* This hook gets called just before the first instruction in the
451   inferior process is executed.
452
453   This is our opportunity to set magic flags in the inferior so
454   that GDB can be notified when a shared library is mapped in and
455   to tell the dynamic linker that a private copy of the library is
456   needed (so GDB can set breakpoints in the library).
457
458   We need to set two flag bits in this routine.
459
460     DT_HP_DEBUG_PRIVATE to indicate that shared libraries should be
461     mapped private.
462
463     DT_HP_DEBUG_CALLBACK to indicate that we want the dynamic linker to
464     call the breakpoint routine for significant events.  */
465
466void
467pa64_solib_create_inferior_hook (void)
468{
469  struct minimal_symbol *msymbol;
470  unsigned int dld_flags, status;
471  asection *shlib_info, *interp_sect;
472  char buf[4];
473  struct objfile *objfile;
474  CORE_ADDR anaddr;
475
476  /* First, remove all the solib event breakpoints.  Their addresses
477     may have changed since the last time we ran the program.  */
478  remove_solib_event_breakpoints ();
479
480  if (symfile_objfile == NULL)
481    return;
482
483  /* First see if the objfile was dynamically linked.  */
484  shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, ".dynamic");
485  if (!shlib_info)
486    return;
487
488  /* It's got a .dynamic section, make sure it's not empty.  */
489  if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
490    return;
491
492  /* Read in the .dynamic section.  */
493  if (! read_dynamic_info (shlib_info, &dld_cache))
494    error ("Unable to read the .dynamic section.");
495
496  /* Turn on the flags we care about.  */
497  dld_cache.dld_flags |= DT_HP_DEBUG_PRIVATE;
498  dld_cache.dld_flags |= DT_HP_DEBUG_CALLBACK;
499  status = target_write_memory (dld_cache.dld_flags_addr,
500				(char *) &dld_cache.dld_flags,
501				sizeof (dld_cache.dld_flags));
502  if (status != 0)
503    error ("Unable to modify dynamic linker flags.");
504
505  /* Now we have to create a shared library breakpoint in the dynamic
506     linker.  This can be somewhat tricky since the symbol is inside
507     the dynamic linker (for which we do not have symbols or a base
508     load address!   Luckily I wrote this code for solib.c years ago.  */
509  interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
510  if (interp_sect)
511    {
512      unsigned int interp_sect_size;
513      char *buf;
514      CORE_ADDR load_addr;
515      bfd *tmp_bfd;
516      CORE_ADDR sym_addr = 0;
517
518      /* Read the contents of the .interp section into a local buffer;
519	 the contents specify the dynamic linker this program uses.  */
520      interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
521      buf = alloca (interp_sect_size);
522      bfd_get_section_contents (exec_bfd, interp_sect,
523				buf, 0, interp_sect_size);
524
525      /* Now we need to figure out where the dynamic linker was
526	 loaded so that we can load its symbols and place a breakpoint
527	 in the dynamic linker itself.
528
529	 This address is stored on the stack.  However, I've been unable
530	 to find any magic formula to find it for Solaris (appears to
531	 be trivial on GNU/Linux).  Therefore, we have to try an alternate
532	 mechanism to find the dynamic linker's base address.  */
533      tmp_bfd = bfd_openr (buf, gnutarget);
534      if (tmp_bfd == NULL)
535	goto get_out;
536
537      /* Make sure the dynamic linker's really a useful object.  */
538      if (!bfd_check_format (tmp_bfd, bfd_object))
539	{
540	  warning ("Unable to grok dynamic linker %s as an object file", buf);
541	  bfd_close (tmp_bfd);
542	  goto get_out;
543	}
544
545      /* We find the dynamic linker's base address by examining the
546	 current pc (which point at the entry point for the dynamic
547	 linker) and subtracting the offset of the entry point.
548
549	 Also note the breakpoint is the second instruction in the
550	 routine.  */
551      load_addr = read_pc () - tmp_bfd->start_address;
552      sym_addr = bfd_lookup_symbol (tmp_bfd, "__dld_break");
553      sym_addr = load_addr + sym_addr + 4;
554
555      /* Create the shared library breakpoint.  */
556      {
557	struct breakpoint *b
558	  = create_solib_event_breakpoint (sym_addr);
559
560	/* The breakpoint is actually hard-coded into the dynamic linker,
561	   so we don't need to actually insert a breakpoint instruction
562	   there.  In fact, the dynamic linker's code is immutable, even to
563	   ttrace, so we shouldn't even try to do that.  For cases like
564	   this, we have "permanent" breakpoints.  */
565	make_breakpoint_permanent (b);
566      }
567
568      /* We're done with the temporary bfd.  */
569      bfd_close (tmp_bfd);
570    }
571
572get_out:
573  /* Wipe out all knowledge of old shared libraries since their
574     mapping can change from one exec to another!  */
575  while (so_list_head)
576    {
577      struct so_list *temp;
578
579      temp = so_list_head;
580      xfree (so_list_head);
581      so_list_head = temp->next;
582    }
583  clear_symtab_users ();
584}
585
586/* This operation removes the "hook" between GDB and the dynamic linker,
587   which causes the dld to notify GDB of shared library events.
588
589   After this operation completes, the dld will no longer notify GDB of
590   shared library events.  To resume notifications, GDB must call
591   pa64_solib_create_inferior_hook.
592
593   This operation does not remove any knowledge of shared libraries which
594   GDB may already have been notified of.  */
595
596void
597pa64_solib_remove_inferior_hook (int pid)
598{
599  /* Turn off the DT_HP_DEBUG_CALLBACK bit in the dynamic linker flags.  */
600  dld_cache.dld_flags &= ~DT_HP_DEBUG_CALLBACK;
601  target_write_memory (dld_cache.dld_flags_addr,
602		       (char *)&dld_cache.dld_flags,
603		       sizeof (dld_cache.dld_flags));
604}
605
606/* This function creates a breakpoint on the dynamic linker hook, which
607   is called when e.g., a shl_load or shl_unload call is made.  This
608   breakpoint will only trigger when a shl_load call is made.
609
610   If filename is NULL, then loads of any dll will be caught.  Else,
611   only loads of the file whose pathname is the string contained by
612   filename will be caught.
613
614   Undefined behaviour is guaranteed if this function is called before
615   pa64_solib_create_inferior_hook.  */
616
617void
618pa64_solib_create_catch_load_hook (int pid, int tempflag, char *filename,
619				   char *cond_string)
620{
621  create_solib_load_event_breakpoint ("", tempflag, filename, cond_string);
622}
623
624/* This function creates a breakpoint on the dynamic linker hook, which
625   is called when e.g., a shl_load or shl_unload call is made.  This
626   breakpoint will only trigger when a shl_unload call is made.
627
628   If filename is NULL, then unloads of any dll will be caught.  Else,
629   only unloads of the file whose pathname is the string contained by
630   filename will be caught.
631
632   Undefined behaviour is guaranteed if this function is called before
633   pa64_solib_create_inferior_hook.  */
634
635void
636pa64_solib_create_catch_unload_hook (int pid, int tempflag, char *filename,
637				     char *cond_string)
638{
639  create_solib_unload_event_breakpoint ("", tempflag, filename, cond_string);
640}
641
642/* Return nonzero if the dynamic linker has reproted that a library
643   has been loaded.  */
644
645int
646pa64_solib_have_load_event (int pid)
647{
648  CORE_ADDR event_kind;
649
650  event_kind = read_register (ARG0_REGNUM);
651  return (event_kind == DLD_CB_LOAD);
652}
653
654/* Return nonzero if the dynamic linker has reproted that a library
655   has been unloaded.  */
656int
657pa64_solib_have_unload_event (int pid)
658{
659  CORE_ADDR event_kind;
660
661  event_kind = read_register (ARG0_REGNUM);
662  return (event_kind == DLD_CB_UNLOAD);
663}
664
665/* Return a pointer to a string indicating the pathname of the most
666   recently loaded library.
667
668   The caller is reposible for copying the string before the inferior is
669   restarted.  */
670
671char *
672pa64_solib_loaded_library_pathname (int pid)
673{
674  static char dll_path[MAXPATHLEN];
675  CORE_ADDR  dll_path_addr = read_register (ARG3_REGNUM);
676  read_memory_string (dll_path_addr, dll_path, MAXPATHLEN);
677  return dll_path;
678}
679
680/* Return a pointer to a string indicating the pathname of the most
681   recently unloaded library.
682
683   The caller is reposible for copying the string before the inferior is
684   restarted.  */
685
686char *
687pa64_solib_unloaded_library_pathname (int pid)
688{
689  static char dll_path[MAXPATHLEN];
690  CORE_ADDR dll_path_addr = read_register (ARG3_REGNUM);
691  read_memory_string (dll_path_addr, dll_path, MAXPATHLEN);
692  return dll_path;
693}
694
695/* Return nonzero if PC is an address inside the dynamic linker.  */
696
697int
698pa64_solib_in_dynamic_linker (int pid, CORE_ADDR pc)
699{
700  asection *shlib_info;
701
702  if (symfile_objfile == NULL)
703    return 0;
704
705  if (!dld_cache.have_read_dld_descriptor)
706    if (!read_dld_descriptor (&current_target, auto_solib_add))
707      return 0;
708
709  return (pc >= dld_cache.dld_desc.text_base
710	  && pc < dld_cache.dld_desc.text_base + dld_cache.dld_desc.text_size);
711}
712
713
714/* Return the GOT value for the shared library in which ADDR belongs.  If
715   ADDR isn't in any known shared library, return zero.  */
716
717CORE_ADDR
718pa64_solib_get_got_by_pc (CORE_ADDR addr)
719{
720  struct so_list *so_list = so_list_head;
721  CORE_ADDR got_value = 0;
722
723  while (so_list)
724    {
725      if (so_list->pa64_solib_desc.text_base <= addr
726	  && ((so_list->pa64_solib_desc.text_base
727	       + so_list->pa64_solib_desc.text_size)
728	      > addr))
729	{
730	  got_value = so_list->pa64_solib_desc.linkage_ptr;
731	  break;
732	}
733      so_list = so_list->next;
734    }
735  return got_value;
736}
737
738/* Return the address of the handle of the shared library in which ADDR
739   belongs.  If ADDR isn't in any known shared library, return zero.
740
741   This function is used in hppa_fix_call_dummy in hppa-tdep.c.  */
742
743CORE_ADDR
744pa64_solib_get_solib_by_pc (CORE_ADDR addr)
745{
746  struct so_list *so_list = so_list_head;
747  CORE_ADDR retval = 0;
748
749  while (so_list)
750    {
751      if (so_list->pa64_solib_desc.text_base <= addr
752	  && ((so_list->pa64_solib_desc.text_base
753	       + so_list->pa64_solib_desc.text_size)
754	      > addr))
755	{
756	  retval = so_list->pa64_solib_desc_addr;
757	  break;
758	}
759      so_list = so_list->next;
760    }
761  return retval;
762}
763
764/* Dump information about all the currently loaded shared libraries.  */
765
766static void
767pa64_sharedlibrary_info_command (char *ignore, int from_tty)
768{
769  struct so_list *so_list = so_list_head;
770
771  if (exec_bfd == NULL)
772    {
773      printf_unfiltered ("No executable file.\n");
774      return;
775    }
776
777  if (so_list == NULL)
778    {
779      printf_unfiltered ("No shared libraries loaded at this time.\n");
780      return;
781    }
782
783  printf_unfiltered ("Shared Object Libraries\n");
784  printf_unfiltered ("   %-19s%-19s%-19s%-19s\n",
785		     "  text start", "   text end",
786		     "  data start", "   data end");
787  while (so_list)
788    {
789      unsigned int flags;
790
791      printf_unfiltered ("%s", so_list->name);
792      if (so_list->objfile == NULL)
793	printf_unfiltered ("  (symbols not loaded)");
794      if (so_list->loaded == 0)
795	printf_unfiltered ("  (shared library unloaded)");
796      printf_unfiltered ("  %-18s",
797	local_hex_string_custom (so_list->pa64_solib_desc.linkage_ptr,
798				 "016l"));
799      printf_unfiltered ("\n");
800      printf_unfiltered ("%-18s",
801	local_hex_string_custom (so_list->pa64_solib_desc.text_base,
802				 "016l"));
803      printf_unfiltered (" %-18s",
804	local_hex_string_custom ((so_list->pa64_solib_desc.text_base
805				  + so_list->pa64_solib_desc.text_size),
806				 "016l"));
807      printf_unfiltered (" %-18s",
808	local_hex_string_custom (so_list->pa64_solib_desc.data_base,
809				 "016l"));
810      printf_unfiltered (" %-18s\n",
811	local_hex_string_custom ((so_list->pa64_solib_desc.data_base
812				  + so_list->pa64_solib_desc.data_size),
813				 "016l"));
814      so_list = so_list->next;
815    }
816}
817
818/* Load up one or more shared libraries as directed by the user.  */
819
820static void
821pa64_solib_sharedlibrary_command (char *args, int from_tty)
822{
823  dont_repeat ();
824  pa64_solib_add (args, from_tty, (struct target_ops *) 0, 1);
825}
826
827/* Return the name of the shared library containing ADDR or NULL if ADDR
828   is not contained in any known shared library.  */
829
830char *
831pa64_solib_address (CORE_ADDR addr)
832{
833  struct so_list *so = so_list_head;
834
835  while (so)
836    {
837      /* Is this address within this shlib's text range?  If so,
838	 return the shlib's name.  */
839      if (addr >= so->pa64_solib_desc.text_base
840	  && addr < (so->pa64_solib_desc.text_base
841		     | so->pa64_solib_desc.text_size))
842	return so->name;
843
844      /* Nope, keep looking... */
845      so = so->next;
846    }
847
848  /* No, we couldn't prove that the address is within a shlib. */
849  return NULL;
850}
851
852/* We are killing the inferior and restarting the program.  */
853
854void
855pa64_solib_restart (void)
856{
857  struct so_list *sl = so_list_head;
858
859  /* Before the shlib info vanishes, use it to disable any breakpoints
860     that may still be active in those shlibs.  */
861  disable_breakpoints_in_shlibs (0);
862
863  /* Discard all the shlib descriptors.  */
864  while (sl)
865    {
866      struct so_list *next_sl = sl->next;
867      xfree (sl);
868      sl = next_sl;
869    }
870  so_list_head = NULL;
871
872  pa64_solib_total_st_size = (LONGEST) 0;
873  pa64_solib_st_size_threshold_exceeded = 0;
874
875  dld_cache.is_valid = 0;
876  dld_cache.have_read_dld_descriptor = 0;
877  dld_cache.dld_flags_addr = 0;
878  dld_cache.load_map = 0;
879  dld_cache.load_map_addr = 0;
880  dld_cache.dld_desc.data_base = 0;
881  dld_cache.dld_flags = 0;
882  dld_cache.dyninfo_sect = 0;
883}
884
885void
886_initialize_pa64_solib (void)
887{
888  add_com ("sharedlibrary", class_files, pa64_solib_sharedlibrary_command,
889	   "Load shared object library symbols for files matching REGEXP.");
890  add_info ("sharedlibrary", pa64_sharedlibrary_info_command,
891	    "Status of loaded shared object libraries.");
892
893  add_show_from_set
894    (add_set_cmd ("auto-solib-add", class_support, var_boolean,
895		  (char *) &auto_solib_add,
896		  "Set autoloading of shared library symbols.\n\
897If \"on\", symbols from all shared object libraries will be loaded\n\
898automatically when the inferior begins execution, when the dynamic linker\n\
899informs gdb that a new library has been loaded, or when attaching to the\n\
900inferior.  Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
901		  &setlist),
902     &showlist);
903
904  add_show_from_set
905    (add_set_cmd ("auto-solib-limit", class_support, var_zinteger,
906		  (char *) &auto_solib_limit,
907		  "Set threshold (in Mb) for autoloading shared library symbols.\n\
908When shared library autoloading is enabled, new libraries will be loaded\n\
909only until the total size of shared library symbols exceeds this\n\
910threshold in megabytes.  Is ignored when using `sharedlibrary'.",
911		  &setlist),
912     &showlist);
913
914  /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how
915     much data space a process can use.  We ought to be reading
916     MAXDSIZ and setting auto_solib_limit to some large fraction of
917     that value.  If not that, we maybe ought to be setting it smaller
918     than the default for MAXDSIZ (that being 64Mb, I believe).
919     However, [1] this threshold is only crudely approximated rather
920     than actually measured, and [2] 50 Mbytes is too small for
921     debugging gdb itself.  Thus, the arbitrary 100 figure.  */
922  auto_solib_limit = 100;	/* Megabytes */
923
924  pa64_solib_restart ();
925}
926
927/* Get some HPUX-specific data from a shared lib.  */
928CORE_ADDR
929so_lib_thread_start_addr (struct so_list *so)
930{
931  return so->pa64_solib_desc.tls_start_addr;
932}
933
934/* Read the dynamic linker's internal shared library descriptor.
935
936   This must happen after dld starts running, so we can't do it in
937   read_dynamic_info.  Record the fact that we have loaded the
938   descriptor.  If the library is archive bound, then return zero, else
939   return nonzero.  */
940
941static int
942read_dld_descriptor (struct target_ops *target, int readsyms)
943{
944  char *dll_path;
945  asection *dyninfo_sect;
946
947  /* If necessary call read_dynamic_info to extract the contents of the
948     .dynamic section from the shared library.  */
949  if (!dld_cache.is_valid)
950    {
951      if (symfile_objfile == NULL)
952	error ("No object file symbols.");
953
954      dyninfo_sect = bfd_get_section_by_name (symfile_objfile->obfd,
955					      ".dynamic");
956      if (!dyninfo_sect)
957	{
958	  return 0;
959	}
960
961      if (!read_dynamic_info (dyninfo_sect, &dld_cache))
962	error ("Unable to read in .dynamic section information.");
963    }
964
965  /* Read the load map pointer.  */
966  if (target_read_memory (dld_cache.load_map_addr,
967			  (char*) &dld_cache.load_map,
968			  sizeof(dld_cache.load_map))
969      != 0)
970    {
971      error ("Error while reading in load map pointer.");
972    }
973
974  /* Read in the dld load module descriptor */
975  if (dlgetmodinfo (-1,
976		    &dld_cache.dld_desc,
977		    sizeof(dld_cache.dld_desc),
978		    pa64_target_read_memory,
979		    0,
980		    dld_cache.load_map)
981      == 0)
982    {
983      error ("Error trying to get information about dynamic linker.");
984    }
985
986  /* Indicate that we have loaded the dld descriptor.  */
987  dld_cache.have_read_dld_descriptor = 1;
988
989  /* Add dld.sl to the list of known shared libraries so that we can
990     do unwind, etc.
991
992     ?!? This may not be correct.  Consider of dld.sl contains symbols
993     which are also referenced/defined by the user program or some user
994     shared library.  We need to make absolutely sure that we do not
995     pollute the namespace from GDB's point of view.  */
996  dll_path = dlgetname (&dld_cache.dld_desc,
997			sizeof(dld_cache.dld_desc),
998			pa64_target_read_memory,
999			0,
1000			dld_cache.load_map);
1001  add_to_solist(0, dll_path, readsyms, &dld_cache.dld_desc, 0, target);
1002
1003  return 1;
1004}
1005
1006/* Read the .dynamic section and extract the information of interest,
1007   which is stored in dld_cache.  The routine elf_locate_base in solib.c
1008   was used as a model for this.  */
1009
1010static int
1011read_dynamic_info (asection *dyninfo_sect, dld_cache_t *dld_cache_p)
1012{
1013  char *buf;
1014  char *bufend;
1015  CORE_ADDR dyninfo_addr;
1016  int dyninfo_sect_size;
1017  CORE_ADDR entry_addr;
1018
1019  /* Read in .dynamic section, silently ignore errors.  */
1020  dyninfo_addr = bfd_section_vma (symfile_objfile->obfd, dyninfo_sect);
1021  dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
1022  buf = alloca (dyninfo_sect_size);
1023  if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
1024    return 0;
1025
1026  /* Scan the .dynamic section and record the items of interest.
1027     In particular, DT_HP_DLD_FLAGS */
1028  for (bufend = buf + dyninfo_sect_size, entry_addr = dyninfo_addr;
1029       buf < bufend;
1030       buf += sizeof (Elf64_Dyn), entry_addr += sizeof (Elf64_Dyn))
1031    {
1032      Elf64_Dyn *x_dynp = (Elf64_Dyn*)buf;
1033      Elf64_Sxword dyn_tag;
1034      CORE_ADDR	dyn_ptr;
1035      char *pbuf;
1036
1037      pbuf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
1038      dyn_tag = bfd_h_get_64 (symfile_objfile->obfd,
1039			      (bfd_byte*) &x_dynp->d_tag);
1040
1041      /* We can't use a switch here because dyn_tag is 64 bits and HP's
1042	 lame comiler does not handle 64bit items in switch statements.  */
1043      if (dyn_tag == DT_NULL)
1044	break;
1045      else if (dyn_tag == DT_HP_DLD_FLAGS)
1046	{
1047	  /* Set dld_flags_addr and dld_flags in *dld_cache_p */
1048	  dld_cache_p->dld_flags_addr = entry_addr + offsetof(Elf64_Dyn, d_un);
1049	  if (target_read_memory (dld_cache_p->dld_flags_addr,
1050	  			  (char*) &dld_cache_p->dld_flags,
1051				  sizeof(dld_cache_p->dld_flags))
1052	      != 0)
1053	    {
1054	      error ("Error while reading in .dynamic section of the program.");
1055	    }
1056	}
1057      else if (dyn_tag == DT_HP_LOAD_MAP)
1058	{
1059	  /* Dld will place the address of the load map at load_map_addr
1060	     after it starts running.  */
1061	  if (target_read_memory (entry_addr + offsetof(Elf64_Dyn,
1062							d_un.d_ptr),
1063				  (char*) &dld_cache_p->load_map_addr,
1064				  sizeof(dld_cache_p->load_map_addr))
1065	      != 0)
1066	    {
1067	      error ("Error while reading in .dynamic section of the program.");
1068	    }
1069	}
1070      else
1071	{
1072	  /* tag is not of interest */
1073	}
1074    }
1075
1076  /* Record other information and set is_valid to 1. */
1077  dld_cache_p->dyninfo_sect = dyninfo_sect;
1078
1079  /* Verify that we read in required info.  These fields are re-set to zero
1080     in pa64_solib_restart.  */
1081
1082  if (dld_cache_p->dld_flags_addr != 0 && dld_cache_p->load_map_addr != 0)
1083    dld_cache_p->is_valid = 1;
1084  else
1085    return 0;
1086
1087  return 1;
1088}
1089
1090/* Wrapper for target_read_memory to make dlgetmodinfo happy.  */
1091
1092static void *
1093pa64_target_read_memory (void *buffer, CORE_ADDR ptr, size_t bufsiz, int ident)
1094{
1095  if (target_read_memory (ptr, buffer, bufsiz) != 0)
1096    return 0;
1097  return buffer;
1098}
1099
1100/* Called from handle_dynlink_load_event and pa64_solib_add to add
1101   a shared library to so_list_head list and possibly to read in the
1102   debug information for the library.
1103
1104   If load_module_desc_p is NULL, then the load module descriptor must
1105   be read from the inferior process at the address load_module_desc_addr.  */
1106
1107static void
1108add_to_solist (int from_tty, char *dll_path, int readsyms,
1109	       struct load_module_desc *load_module_desc_p,
1110	       CORE_ADDR load_module_desc_addr, struct target_ops *target)
1111{
1112  struct so_list *new_so, *so_list_tail;
1113  int pa64_solib_st_size_threshhold_exceeded;
1114  LONGEST st_size;
1115
1116  if (symfile_objfile == NULL)
1117    return;
1118
1119  so_list_tail = so_list_head;
1120  /* Find the end of the list of shared objects.  */
1121  while (so_list_tail && so_list_tail->next)
1122    {
1123      if (strcmp (so_list_tail->name, dll_path) == 0)
1124	return;
1125      so_list_tail = so_list_tail->next;
1126    }
1127
1128  if (so_list_tail && strcmp (so_list_tail->name, dll_path) == 0)
1129    return;
1130
1131  /* Add the shared library to the so_list_head list */
1132  new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
1133  memset ((char *)new_so, 0, sizeof (struct so_list));
1134  if (so_list_head == NULL)
1135    {
1136      so_list_head = new_so;
1137      so_list_tail = new_so;
1138    }
1139  else
1140    {
1141      so_list_tail->next = new_so;
1142      so_list_tail = new_so;
1143    }
1144
1145  /* Initialize the new_so */
1146  if (load_module_desc_p)
1147    {
1148      new_so->pa64_solib_desc = *load_module_desc_p;
1149    }
1150  else
1151    {
1152      if (target_read_memory (load_module_desc_addr,
1153			      (char*) &new_so->pa64_solib_desc,
1154			      sizeof(struct load_module_desc))
1155	  != 0)
1156      {
1157	error ("Error while reading in dynamic library %s", dll_path);
1158      }
1159    }
1160
1161  new_so->pa64_solib_desc_addr = load_module_desc_addr;
1162  new_so->loaded = 1;
1163  new_so->name = obsavestring (dll_path, strlen(dll_path),
1164			       &symfile_objfile->objfile_obstack);
1165
1166  /* If we are not going to load the library, tell the user if we
1167     haven't already and return.  */
1168
1169  st_size = pa64_solib_sizeof_symbol_table (dll_path);
1170  pa64_solib_st_size_threshhold_exceeded =
1171       !from_tty
1172    && readsyms
1173    && (  (st_size + pa64_solib_total_st_size)
1174	> (auto_solib_limit * (LONGEST) (1024 * 1024)));
1175  if (pa64_solib_st_size_threshhold_exceeded)
1176    {
1177      pa64_solib_add_solib_objfile (new_so, dll_path, from_tty, 1);
1178      return;
1179    }
1180
1181  /* Now read in debug info. */
1182  pa64_solib_total_st_size += st_size;
1183
1184  /* This fills in new_so->objfile, among others. */
1185  pa64_solib_load_symbols (new_so,
1186			   dll_path,
1187			   from_tty,
1188			   0,
1189			   target);
1190  return;
1191}
1192
1193
1194/*
1195   LOCAL FUNCTION
1196
1197   bfd_lookup_symbol -- lookup the value for a specific symbol
1198
1199   SYNOPSIS
1200
1201   CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
1202
1203   DESCRIPTION
1204
1205   An expensive way to lookup the value of a single symbol for
1206   bfd's that are only temporary anyway.  This is used by the
1207   shared library support to find the address of the debugger
1208   interface structures in the shared library.
1209
1210   Note that 0 is specifically allowed as an error return (no
1211   such symbol).
1212 */
1213
1214static CORE_ADDR
1215bfd_lookup_symbol (bfd *abfd, char *symname)
1216{
1217  unsigned int storage_needed;
1218  asymbol *sym;
1219  asymbol **symbol_table;
1220  unsigned int number_of_symbols;
1221  unsigned int i;
1222  struct cleanup *back_to;
1223  CORE_ADDR symaddr = 0;
1224
1225  storage_needed = bfd_get_symtab_upper_bound (abfd);
1226
1227  if (storage_needed > 0)
1228    {
1229      symbol_table = (asymbol **) xmalloc (storage_needed);
1230      back_to = make_cleanup (xfree, symbol_table);
1231      number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1232
1233      for (i = 0; i < number_of_symbols; i++)
1234	{
1235	  sym = *symbol_table++;
1236	  if (strcmp (sym->name, symname) == 0)
1237	    {
1238	      /* Bfd symbols are section relative. */
1239	      symaddr = sym->value + sym->section->vma;
1240	      break;
1241	    }
1242	}
1243      do_cleanups (back_to);
1244    }
1245  return (symaddr);
1246}
1247
1248