OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [pa64solib.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.