* alpha-tdep.c (alpha_register_name): Add comment.
[deliverable/binutils-gdb.git] / gdb / somsolib.c
CommitLineData
c906108c 1/* Handle HP SOM shared libraries for GDB, the GNU Debugger.
c5f10366 2
1bac305b 3 Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
8b92e4d5 4 2003, 2004 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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.
c906108c 22
c5aa993b
JM
23 Written by the Center for Software Science at the Univerity of Utah
24 and by Cygnus Support. */
c906108c
SS
25
26
27#include "defs.h"
28
29#include "frame.h"
30#include "bfd.h"
31#include "som.h"
32#include "libhppa.h"
33#include "gdbcore.h"
34#include "symtab.h"
35#include "breakpoint.h"
36#include "symfile.h"
37#include "objfiles.h"
38#include "inferior.h"
39#include "gdb-stabs.h"
40#include "gdb_stat.h"
41#include "gdbcmd.h"
c906108c 42#include "language.h"
4e052eda 43#include "regcache.h"
a7ff40e7 44#include "gdb_assert.h"
4646aa9d 45#include "exec.h"
7c46b9fb 46#include "hppa-tdep.h"
c906108c
SS
47
48#include <fcntl.h>
49
50#ifndef O_BINARY
51#define O_BINARY 0
52#endif
53
89676665
RC
54#ifndef SHL_LOAD
55#define SHL_LOAD 4
56#endif
57
58#ifndef SHL_UNLOAD
59#define SHL_UNLOAD 8
60#endif
61
c906108c
SS
62/* Uncomment this to turn on some debugging output.
63 */
64
65/* #define SOLIB_DEBUG
66 */
c5aa993b 67
c906108c 68/* This lives in hppa-tdep.c. */
a14ed312 69extern struct unwind_table_entry *find_unwind_entry (CORE_ADDR pc);
c906108c
SS
70
71/* These ought to be defined in some public interface, but aren't. They
72 define the meaning of the various bits in the distinguished __dld_flags
73 variable that is declared in every debuggable a.out on HP-UX, and that
74 is shared between the debugger and the dynamic linker.
c5aa993b 75 */
c906108c
SS
76#define DLD_FLAGS_MAPPRIVATE 0x1
77#define DLD_FLAGS_HOOKVALID 0x2
78#define DLD_FLAGS_LISTVALID 0x4
79#define DLD_FLAGS_BOR_ENABLE 0x8
80
81/* TODO:
82
c906108c
SS
83 * Support for hpux8 dynamic linker. */
84
85/* The basic structure which describes a dynamically loaded object. This
86 data structure is private to the dynamic linker and isn't found in
87 any HPUX include file. */
88
89struct som_solib_mapped_entry
c5aa993b
JM
90 {
91 /* The name of the library. */
92 char *name;
c906108c 93
c5aa993b
JM
94 /* Version of this structure (it is expected to change again in hpux10). */
95 unsigned char struct_version;
c906108c 96
c5aa993b
JM
97 /* Binding mode for this library. */
98 unsigned char bind_mode;
c906108c 99
c5aa993b
JM
100 /* Version of this library. */
101 short library_version;
c906108c 102
c5aa993b
JM
103 /* Start of text address,
104 * link-time text location (length of text area),
105 * end of text address. */
106 CORE_ADDR text_addr;
107 CORE_ADDR text_link_addr;
108 CORE_ADDR text_end;
c906108c 109
c5aa993b
JM
110 /* Start of data, start of bss and end of data. */
111 CORE_ADDR data_start;
112 CORE_ADDR bss_start;
113 CORE_ADDR data_end;
c906108c 114
c5aa993b
JM
115 /* Value of linkage pointer (%r19). */
116 CORE_ADDR got_value;
c906108c 117
c5aa993b
JM
118 /* Next entry. */
119 struct som_solib_mapped_entry *next;
c906108c 120
c5aa993b
JM
121 /* There are other fields, but I don't have information as to what is
122 contained in them. */
c906108c 123
c5aa993b 124 /* For versions from HPUX-10.30 and up */
c906108c 125
c5aa993b
JM
126 /* Address in target of offset from thread-local register of
127 * start of this thread's data. I.e., the first thread-local
128 * variable in this shared library starts at *(tsd_start_addr)
129 * from that area pointed to by cr27 (mpsfu_hi).
130 *
131 * We do the indirection as soon as we read it, so from then
132 * on it's the offset itself.
133 */
134 CORE_ADDR tsd_start_addr;
c906108c 135
c5aa993b
JM
136 /* Following this are longwords holding:
137
138 * ?, ?, ?, ptr to -1, ptr to-1, ptr to lib name (leaf name),
139 * ptr to __data_start, ptr to __data_end
140 */
c906108c 141
c5aa993b
JM
142
143 };
c906108c
SS
144
145/* A structure to keep track of all the known shared objects. */
146struct so_list
c5aa993b
JM
147 {
148 struct som_solib_mapped_entry som_solib;
149 struct objfile *objfile;
150 bfd *abfd;
151 struct section_table *sections;
152 struct section_table *sections_end;
c906108c
SS
153/* elz: added this field to store the address in target space (in the
154 library) of the library descriptor (handle) which we read into
c5aa993b
JM
155 som_solib_mapped_entry structure */
156 CORE_ADDR solib_addr;
157 struct so_list *next;
158
159 };
c906108c
SS
160
161static struct so_list *so_list_head;
162
163
164/* This is the cumulative size in bytes of the symbol tables of all
165 shared objects on the so_list_head list. (When we say size, here
166 we mean of the information before it is brought into memory and
167 potentially expanded by GDB.) When adding a new shlib, this value
b7209cb4 168 is compared against the threshold size, held by auto_solib_limit
c906108c 169 (in megabytes). If adding symbols for the new shlib would cause
b7209cb4
FF
170 the total size to exceed the threshold, then the new shlib's
171 symbols are not loaded. */
c5aa993b 172static LONGEST som_solib_total_st_size;
c906108c
SS
173
174/* When the threshold is reached for any shlib, we refuse to add
175 symbols for subsequent shlibs, even if those shlibs' symbols would
176 be small enough to fit under the threshold. (Although this may
177 result in one, early large shlib preventing the loading of later,
178 smalller shlibs' symbols, it allows us to issue one informational
179 message. The alternative, to issue a message for each shlib whose
180 symbols aren't loaded, could be a big annoyance where the threshold
181 is exceeded due to a very large number of shlibs.)
c5aa993b
JM
182 */
183static int som_solib_st_size_threshold_exceeded;
c906108c
SS
184
185/* These addresses should be filled in by som_solib_create_inferior_hook.
186 They are also used elsewhere in this module.
c5aa993b
JM
187 */
188typedef struct
189 {
190 CORE_ADDR address;
191 struct unwind_table_entry *unwind;
192 }
193addr_and_unwind_t;
c906108c
SS
194
195/* When adding fields, be sure to clear them in _initialize_som_solib. */
c5aa993b
JM
196static struct
197 {
504d5c7e 198 int is_valid;
c5aa993b
JM
199 addr_and_unwind_t hook;
200 addr_and_unwind_t hook_stub;
201 addr_and_unwind_t load;
202 addr_and_unwind_t load_stub;
203 addr_and_unwind_t unload;
204 addr_and_unwind_t unload2;
205 addr_and_unwind_t unload_stub;
206 }
207dld_cache;
c906108c
SS
208
209
210
a14ed312 211static void som_sharedlibrary_info_command (char *, int);
c906108c 212
a14ed312 213static void som_solib_sharedlibrary_command (char *, int);
c906108c
SS
214
215static LONGEST
fba45db2 216som_solib_sizeof_symbol_table (char *filename)
c906108c 217{
c5aa993b
JM
218 bfd *abfd;
219 int desc;
220 char *absolute_name;
221 LONGEST st_size = (LONGEST) 0;
222 asection *sect;
c906108c
SS
223
224 /* We believe that filename was handed to us by the dynamic linker, and
225 is therefore always an absolute path.
c5aa993b 226 */
014d698b
EZ
227 desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
228 O_RDONLY | O_BINARY, 0, &absolute_name);
c906108c
SS
229 if (desc < 0)
230 {
231 perror_with_name (filename);
232 }
233 filename = absolute_name;
234
235 abfd = bfd_fdopenr (filename, gnutarget, desc);
c5aa993b 236 if (!abfd)
c906108c
SS
237 {
238 close (desc);
b8c9b27d 239 make_cleanup (xfree, filename);
c906108c
SS
240 error ("\"%s\": can't open to read symbols: %s.", filename,
241 bfd_errmsg (bfd_get_error ()));
242 }
c5aa993b
JM
243
244 if (!bfd_check_format (abfd, bfd_object)) /* Reads in section info */
c906108c 245 {
c5aa993b 246 bfd_close (abfd); /* This also closes desc */
b8c9b27d 247 make_cleanup (xfree, filename);
c906108c
SS
248 error ("\"%s\": can't read symbols: %s.", filename,
249 bfd_errmsg (bfd_get_error ()));
250 }
251
252 /* Sum the sizes of the various sections that compose debug info. */
253
254 /* This contains non-DOC information. */
255 sect = bfd_get_section_by_name (abfd, "$DEBUG$");
256 if (sect)
257 st_size += (LONGEST) bfd_section_size (abfd, sect);
258
259 /* This contains DOC information. */
260 sect = bfd_get_section_by_name (abfd, "$PINFO$");
261 if (sect)
262 st_size += (LONGEST) bfd_section_size (abfd, sect);
263
c5aa993b 264 bfd_close (abfd); /* This also closes desc */
b8c9b27d 265 xfree (filename);
c906108c
SS
266
267 /* Unfortunately, just summing the sizes of various debug info
268 sections isn't a very accurate measurement of how much heap
269 space the debugger will need to hold them. It also doesn't
270 account for space needed by linker (aka "minimal") symbols.
271
272 Anecdotal evidence suggests that just summing the sizes of
273 debug-info-related sections understates the heap space needed
274 to represent it internally by about an order of magnitude.
275
276 Since it's not exactly brain surgery we're doing here, rather
277 than attempt to more accurately measure the size of a shlib's
278 symbol table in GDB's heap, we'll just apply a 10x fudge-
279 factor to the debug info sections' size-sum. No, this doesn't
280 account for minimal symbols in non-debuggable shlibs. But it
281 all roughly washes out in the end.
c5aa993b 282 */
c906108c
SS
283 return st_size * (LONGEST) 10;
284}
285
286
287static void
fba45db2
KB
288som_solib_add_solib_objfile (struct so_list *so, char *name, int from_tty,
289 CORE_ADDR text_addr)
c906108c 290{
7c46b9fb 291 struct hppa_objfile_private *obj_private;
9ca0e47c 292 struct obj_section *s;
c5aa993b 293
9ca0e47c 294 so->objfile = symbol_file_add (name, from_tty, NULL, 0, OBJF_SHARED);
c906108c
SS
295 so->abfd = so->objfile->obfd;
296
9ca0e47c
MC
297 /* syms_from_objfile has bizarre section offset code,
298 so I do my own right here. */
299 for (s = so->objfile->sections; s < so->objfile->sections_end; s++)
300 {
301 flagword aflag = bfd_get_section_flags(so->abfd, s->the_bfd_section);
302 if (aflag & SEC_CODE)
303 {
304 s->addr += so->som_solib.text_addr - so->som_solib.text_link_addr;
305 s->endaddr += so->som_solib.text_addr - so->som_solib.text_link_addr;
306 }
307 else if (aflag & SEC_DATA)
308 {
309 s->addr += so->som_solib.data_start;
310 s->endaddr += so->som_solib.data_start;
311 }
312 else
313 ;
314 }
315
c906108c
SS
316 /* Mark this as a shared library and save private data.
317 */
318 so->objfile->flags |= OBJF_SHARED;
319
7c46b9fb
RC
320 obj_private = (struct hppa_objfile_private *)
321 objfile_data (so->objfile, hppa_objfile_priv_data);
322 if (obj_private == NULL)
c906108c 323 {
7c46b9fb 324 obj_private = (struct hppa_objfile_private *)
8b92e4d5 325 obstack_alloc (&so->objfile->objfile_obstack,
7c46b9fb
RC
326 sizeof (struct hppa_objfile_private));
327 set_objfile_data (so->objfile, hppa_objfile_priv_data, obj_private);
c906108c 328 obj_private->unwind_info = NULL;
c5aa993b 329 obj_private->so_info = NULL;
c5aa993b 330 }
c906108c 331
c906108c
SS
332 obj_private->so_info = so;
333
334 if (!bfd_check_format (so->abfd, bfd_object))
335 {
336 error ("\"%s\": not in executable format: %s.",
c5aa993b 337 name, bfd_errmsg (bfd_get_error ()));
c906108c
SS
338 }
339}
340
341
342static void
fba45db2
KB
343som_solib_load_symbols (struct so_list *so, char *name, int from_tty,
344 CORE_ADDR text_addr, struct target_ops *target)
c906108c 345{
c5aa993b
JM
346 struct section_table *p;
347 int status;
348 char buf[4];
349 CORE_ADDR presumed_data_start;
c906108c
SS
350
351#ifdef SOLIB_DEBUG
c5aa993b 352 printf ("--Adding symbols for shared library \"%s\"\n", name);
c906108c
SS
353#endif
354
355 som_solib_add_solib_objfile (so, name, from_tty, text_addr);
356
357 /* Now we need to build a section table for this library since
358 we might be debugging a core file from a dynamically linked
359 executable in which the libraries were not privately mapped. */
360 if (build_section_table (so->abfd,
c5aa993b
JM
361 &so->sections,
362 &so->sections_end))
c906108c
SS
363 {
364 error ("Unable to build section table for shared library\n.");
365 return;
366 }
367
368 /* Relocate all the sections based on where they got loaded. */
369 for (p = so->sections; p < so->sections_end; p++)
370 {
371 if (p->the_bfd_section->flags & SEC_CODE)
c5aa993b 372 {
b8fbeb18
EZ
373 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
374 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
c5aa993b 375 }
c906108c 376 else if (p->the_bfd_section->flags & SEC_DATA)
c5aa993b 377 {
b8fbeb18
EZ
378 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
379 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
c5aa993b 380 }
c906108c
SS
381 }
382
383 /* Now see if we need to map in the text and data for this shared
384 library (for example debugging a core file which does not use
385 private shared libraries.).
386
387 Carefully peek at the first text address in the library. If the
388 read succeeds, then the libraries were privately mapped and were
389 included in the core dump file.
390
391 If the peek failed, then the libraries were not privately mapped
392 and are not in the core file, we'll have to read them in ourselves. */
393 status = target_read_memory (text_addr, buf, 4);
394 if (status != 0)
395 {
396 int old, new;
c906108c
SS
397
398 new = so->sections_end - so->sections;
6426a772
JM
399
400 old = target_resize_to_sections (target, new);
401
c906108c 402 /* Copy over the old data before it gets clobbered. */
c5aa993b
JM
403 memcpy ((char *) (target->to_sections + old),
404 so->sections,
405 ((sizeof (struct section_table)) * new));
c906108c
SS
406 }
407}
408
409
9a043c1d
AC
410/* FIXME: cagney/2003-02-01: This just isn't right. Given an address
411 within the target's address space, this converts the value into an
412 address within the host's (i.e., GDB's) address space. Given that
413 the host/target address spaces are separate, this can't be right. */
414
415static void *
416hpux_address_to_host_pointer_hack (CORE_ADDR addr)
417{
418 void *ptr;
419
420 gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
421 ADDRESS_TO_POINTER (builtin_type_void_data_ptr, &ptr, addr);
422 return ptr;
423}
424
c906108c 425/* Add symbols from shared libraries into the symtab list, unless the
b7209cb4 426 size threshold specified by auto_solib_limit (in megabytes) would
c906108c
SS
427 be exceeded. */
428
429void
990f9fe3 430som_solib_add (char *arg_string, int from_tty, struct target_ops *target, int readsyms)
c906108c
SS
431{
432 struct minimal_symbol *msymbol;
433 struct so_list *so_list_tail;
434 CORE_ADDR addr;
435 asection *shlib_info;
436 int status;
437 unsigned int dld_flags;
438 char buf[4], *re_err;
c5aa993b 439 int threshold_warning_given = 0;
c906108c
SS
440
441 /* First validate our arguments. */
bde58177
AC
442 re_err = re_comp (arg_string ? arg_string : ".");
443 if (re_err != NULL)
c906108c
SS
444 {
445 error ("Invalid regexp: %s", re_err);
446 }
447
448 /* If we're debugging a core file, or have attached to a running
449 process, then som_solib_create_inferior_hook will not have been
450 called.
451
452 We need to first determine if we're dealing with a dynamically
453 linked executable. If not, then return without an error or warning.
454
455 We also need to examine __dld_flags to determine if the shared library
456 list is valid and to determine if the libraries have been privately
457 mapped. */
458 if (symfile_objfile == NULL)
459 return;
460
461 /* First see if the objfile was dynamically linked. */
462 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
463 if (!shlib_info)
464 return;
465
466 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
467 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
468 return;
469
470 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
471 if (msymbol == NULL)
472 {
473 error ("Unable to find __dld_flags symbol in object file.\n");
474 return;
475 }
476
477 addr = SYMBOL_VALUE_ADDRESS (msymbol);
478 /* Read the current contents. */
479 status = target_read_memory (addr, buf, 4);
480 if (status != 0)
481 {
482 error ("Unable to read __dld_flags\n");
483 return;
484 }
485 dld_flags = extract_unsigned_integer (buf, 4);
486
487 /* __dld_list may not be valid. If not, then we punt, warning the user if
488 we were called as a result of the add-symfile command.
c5aa993b 489 */
c906108c
SS
490 if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
491 {
492 if (from_tty)
c5aa993b 493 error ("__dld_list is not valid according to __dld_flags.\n");
c906108c
SS
494 return;
495 }
496
497 /* If the libraries were not mapped private, warn the user. */
498 if ((dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
499 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
500
501 msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL);
502 if (!msymbol)
503 {
504 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
c5aa993b 505 but the data is still available if you know where to look. */
c906108c
SS
506 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
507 if (!msymbol)
508 {
509 error ("Unable to find dynamic library list.\n");
510 return;
511 }
512 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
513 }
514 else
515 addr = SYMBOL_VALUE_ADDRESS (msymbol);
516
517 status = target_read_memory (addr, buf, 4);
518 if (status != 0)
519 {
520 error ("Unable to find dynamic library list.\n");
521 return;
522 }
523
524 addr = extract_unsigned_integer (buf, 4);
525
526 /* If addr is zero, then we're using an old dynamic loader which
527 doesn't maintain __dld_list. We'll have to use a completely
528 different approach to get shared library information. */
529 if (addr == 0)
530 goto old_dld;
531
532 /* Using the information in __dld_list is the preferred method
533 to get at shared library information. It doesn't depend on
534 any functions in /opt/langtools/lib/end.o and has a chance of working
535 with hpux10 when it is released. */
536 status = target_read_memory (addr, buf, 4);
537 if (status != 0)
538 {
539 error ("Unable to find dynamic library list.\n");
540 return;
541 }
542
543 /* addr now holds the address of the first entry in the dynamic
544 library list. */
545 addr = extract_unsigned_integer (buf, 4);
546
547 /* Now that we have a pointer to the dynamic library list, walk
548 through it and add the symbols for each library. */
549
550 so_list_tail = so_list_head;
551 /* Find the end of the list of shared objects. */
552 while (so_list_tail && so_list_tail->next)
553 so_list_tail = so_list_tail->next;
554
555#ifdef SOLIB_DEBUG
c5aa993b 556 printf ("--About to read shared library list data\n");
c906108c
SS
557#endif
558
559 /* "addr" will always point to the base of the
560 * current data entry describing the current
561 * shared library.
562 */
563 while (1)
564 {
565 CORE_ADDR name_addr, text_addr;
566 unsigned int name_len;
567 char *name;
568 struct so_list *new_so;
569 struct so_list *so_list = so_list_head;
570 struct stat statbuf;
c5aa993b
JM
571 LONGEST st_size;
572 int is_main_program;
c906108c
SS
573
574 if (addr == 0)
575 break;
576
577 /* Get a pointer to the name of this library. */
578 status = target_read_memory (addr, buf, 4);
579 if (status != 0)
580 goto err;
581
582 name_addr = extract_unsigned_integer (buf, 4);
583 name_len = 0;
584 while (1)
585 {
586 target_read_memory (name_addr + name_len, buf, 1);
587 if (status != 0)
588 goto err;
589
590 name_len++;
591 if (*buf == '\0')
592 break;
593 }
594 name = alloca (name_len);
595 status = target_read_memory (name_addr, name, name_len);
596 if (status != 0)
597 goto err;
598
599 /* See if we've already loaded something with this name. */
600 while (so_list)
601 {
602 if (!strcmp (so_list->som_solib.name, name))
603 break;
604 so_list = so_list->next;
605 }
606
607 /* See if the file exists. If not, give a warning, but don't
c5aa993b 608 die. */
c906108c
SS
609 status = stat (name, &statbuf);
610 if (status == -1)
611 {
612 warning ("Can't find file %s referenced in dld_list.", name);
613
614 status = target_read_memory (addr + 36, buf, 4);
615 if (status != 0)
616 goto err;
617
c5aa993b 618 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
c906108c
SS
619 continue;
620 }
621
622 /* If we've already loaded this one or it's the main program, skip it. */
623 is_main_program = (strcmp (name, symfile_objfile->name) == 0);
624 if (so_list || is_main_program)
625 {
c5aa993b
JM
626 /* This is the "next" pointer in the strcuture.
627 */
c906108c
SS
628 status = target_read_memory (addr + 36, buf, 4);
629 if (status != 0)
630 goto err;
631
632 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
633
c5aa993b
JM
634 /* Record the main program's symbol table size. */
635 if (is_main_program && !so_list)
636 {
637 st_size = som_solib_sizeof_symbol_table (name);
638 som_solib_total_st_size += st_size;
639 }
640
641 /* Was this a shlib that we noted but didn't load the symbols for?
642 If so, were we invoked this time from the command-line, via
643 a 'sharedlibrary' or 'add-symbol-file' command? If yes to
644 both, we'd better load the symbols this time.
645 */
646 if (from_tty && so_list && !is_main_program && (so_list->objfile == NULL))
647 som_solib_load_symbols (so_list,
648 name,
649 from_tty,
650 so_list->som_solib.text_addr,
651 target);
c906108c
SS
652
653 continue;
654 }
655
656 name = obsavestring (name, name_len - 1,
4a146b47 657 &symfile_objfile->objfile_obstack);
c906108c
SS
658
659 status = target_read_memory (addr + 8, buf, 4);
660 if (status != 0)
661 goto err;
662
663 text_addr = extract_unsigned_integer (buf, 4);
664
665 new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
c5aa993b 666 memset ((char *) new_so, 0, sizeof (struct so_list));
c906108c
SS
667 if (so_list_head == NULL)
668 {
669 so_list_head = new_so;
670 so_list_tail = new_so;
671 }
672 else
673 {
674 so_list_tail->next = new_so;
675 so_list_tail = new_so;
676 }
677
678 /* Fill in all the entries in GDB's shared library list.
679 */
c5aa993b 680
c906108c
SS
681 new_so->solib_addr = addr;
682 new_so->som_solib.name = name;
683 status = target_read_memory (addr + 4, buf, 4);
684 if (status != 0)
685 goto err;
686
687 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
688 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
c5aa993b
JM
689 /* Following is "high water mark", highest version number
690 * seen, rather than plain version number.
691 */
c906108c
SS
692 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
693 new_so->som_solib.text_addr = text_addr;
694
c5aa993b
JM
695 /* Q: What about longword at "addr + 8"?
696 * A: It's read above, out of order, into "text_addr".
697 */
c906108c
SS
698
699 status = target_read_memory (addr + 12, buf, 4);
700 if (status != 0)
701 goto err;
702
703 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
704
705 status = target_read_memory (addr + 16, buf, 4);
706 if (status != 0)
707 goto err;
708
709 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
710
711 status = target_read_memory (addr + 20, buf, 4);
712 if (status != 0)
713 goto err;
714
715 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
716
717 status = target_read_memory (addr + 24, buf, 4);
718 if (status != 0)
719 goto err;
720
721 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
722
723 status = target_read_memory (addr + 28, buf, 4);
724 if (status != 0)
725 goto err;
726
727 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
728
729 status = target_read_memory (addr + 32, buf, 4);
730 if (status != 0)
731 goto err;
732
733 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
734
735 status = target_read_memory (addr + 36, buf, 4);
736 if (status != 0)
737 goto err;
738
9a043c1d
AC
739 /* FIXME: cagney/2003-02-01: I think som_solib.next should be a
740 CORE_ADDR. */
8f236061 741 new_so->som_solib.next =
9a043c1d 742 hpux_address_to_host_pointer_hack (extract_unsigned_integer (buf, 4));
c906108c
SS
743
744 /* Note that we don't re-set "addr" to the next pointer
745 * until after we've read the trailing data.
746 */
747
748 status = target_read_memory (addr + 40, buf, 4);
749 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
750 if (status != 0)
c5aa993b 751 goto err;
c906108c
SS
752
753 /* Now indirect via that value!
754 */
755 status = target_read_memory (new_so->som_solib.tsd_start_addr, buf, 4);
756 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
757 if (status != 0)
c5aa993b 758 goto err;
c906108c 759#ifdef SOLIB_DEBUG
c5aa993b
JM
760 printf ("\n+ library \"%s\" is described at 0x%x\n", name, addr);
761 printf (" 'version' is %d\n", new_so->som_solib.struct_version);
762 printf (" 'bind_mode' is %d\n", new_so->som_solib.bind_mode);
763 printf (" 'library_version' is %d\n", new_so->som_solib.library_version);
764 printf (" 'text_addr' is 0x%x\n", new_so->som_solib.text_addr);
765 printf (" 'text_link_addr' is 0x%x\n", new_so->som_solib.text_link_addr);
766 printf (" 'text_end' is 0x%x\n", new_so->som_solib.text_end);
767 printf (" 'data_start' is 0x%x\n", new_so->som_solib.data_start);
768 printf (" 'bss_start' is 0x%x\n", new_so->som_solib.bss_start);
769 printf (" 'data_end' is 0x%x\n", new_so->som_solib.data_end);
770 printf (" 'got_value' is %x\n", new_so->som_solib.got_value);
771 printf (" 'next' is 0x%x\n", new_so->som_solib.next);
772 printf (" 'tsd_start_addr' is 0x%x\n", new_so->som_solib.tsd_start_addr);
c906108c
SS
773#endif
774
775 /* Go on to the next shared library descriptor.
776 */
c5aa993b 777 addr = (CORE_ADDR) new_so->som_solib.next;
c906108c
SS
778
779
780
781 /* At this point, we have essentially hooked the shlib into the
782 "info share" command. However, we haven't yet loaded its
783 symbol table. We must now decide whether we ought to, i.e.,
784 whether doing so would exceed the symbol table size threshold.
785
786 If the threshold has just now been exceeded, then we'll issue
787 a warning message (which explains how to load symbols manually,
788 if the user so desires).
789
790 If the threshold has just now or previously been exceeded,
791 we'll just add the shlib to the list of object files, but won't
792 actually load its symbols. (This is more useful than it might
793 sound, for it allows us to e.g., still load and use the shlibs'
794 unwind information for stack tracebacks.)
c5aa993b 795 */
c906108c
SS
796
797 /* Note that we DON'T want to preclude the user from using the
798 add-symbol-file command! Thus, we only worry about the threshold
799 when we're invoked for other reasons.
c5aa993b 800 */
c906108c
SS
801 st_size = som_solib_sizeof_symbol_table (name);
802 som_solib_st_size_threshold_exceeded =
c5aa993b 803 !from_tty &&
9b3128c4 804 auto_solib_limit > 0 &&
990f9fe3 805 readsyms &&
c3c057c8 806 ((st_size + som_solib_total_st_size) > (auto_solib_limit * (LONGEST) (1024 * 1024)));
c906108c
SS
807
808 if (som_solib_st_size_threshold_exceeded)
c5aa993b
JM
809 {
810 if (!threshold_warning_given)
b7209cb4 811 warning ("Symbols for some libraries have not been loaded, because\ndoing so would exceed the size threshold specified by auto-solib-limit.\nTo manually load symbols, use the 'sharedlibrary' command.\nTo raise the threshold, set auto-solib-limit to a larger value and rerun\nthe program.\n");
c5aa993b
JM
812 threshold_warning_given = 1;
813
814 /* We'll still make note of this shlib, even if we don't
815 read its symbols. This allows us to use its unwind
816 information well enough to know how to e.g., correctly
817 do a traceback from a PC within the shlib, even if we
818 can't symbolize those PCs...
819 */
820 som_solib_add_solib_objfile (new_so, name, from_tty, text_addr);
821 continue;
822 }
c906108c
SS
823
824 som_solib_total_st_size += st_size;
825
826 /* This fills in new_so->objfile, among others. */
827 som_solib_load_symbols (new_so, name, from_tty, text_addr, target);
828 }
829
830#ifdef SOLIB_DEBUG
c5aa993b 831 printf ("--Done reading shared library data\n");
c906108c
SS
832#endif
833
834 /* Getting new symbols may change our opinion about what is
835 frameless. */
836 reinit_frame_cache ();
837 return;
838
839old_dld:
840 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
841 return;
842
843err:
844 error ("Error while reading dynamic library list.\n");
845 return;
846}
847
848
849/* This hook gets called just before the first instruction in the
850 inferior process is executed.
851
852 This is our opportunity to set magic flags in the inferior so
853 that GDB can be notified when a shared library is mapped in and
854 to tell the dynamic linker that a private copy of the library is
855 needed (so GDB can set breakpoints in the library).
856
857 __dld_flags is the location of the magic flags; as of this implementation
858 there are 3 flags of interest:
859
860 bit 0 when set indicates that private copies of the libraries are needed
861 bit 1 when set indicates that the callback hook routine is valid
862 bit 2 when set indicates that the dynamic linker should maintain the
c5aa993b 863 __dld_list structure when loading/unloading libraries.
c906108c
SS
864
865 Note that shared libraries are not mapped in at this time, so we have
866 run the inferior until the libraries are mapped in. Typically this
867 means running until the "_start" is called. */
868
869void
fba45db2 870som_solib_create_inferior_hook (void)
c906108c
SS
871{
872 struct minimal_symbol *msymbol;
873 unsigned int dld_flags, status, have_endo;
874 asection *shlib_info;
875 char buf[4];
876 struct objfile *objfile;
877 CORE_ADDR anaddr;
878
879 /* First, remove all the solib event breakpoints. Their addresses
880 may have changed since the last time we ran the program. */
881 remove_solib_event_breakpoints ();
882
883 if (symfile_objfile == NULL)
c5aa993b 884 return;
c906108c
SS
885
886 /* First see if the objfile was dynamically linked. */
887 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
888 if (!shlib_info)
889 return;
890
891 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
892 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
893 return;
894
895 have_endo = 0;
9b3128c4
JL
896 /* Slam the pid of the process into __d_pid.
897
898 We used to warn when this failed, but that warning is only useful
899 on very old HP systems (hpux9 and older). The warnings are an
900 annoyance to users of modern systems and foul up the testsuite as
901 well. As a result, the warnings have been disabled. */
c906108c
SS
902 msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
903 if (msymbol == NULL)
9b3128c4 904 goto keep_going;
c906108c
SS
905
906 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
39f77062 907 store_unsigned_integer (buf, 4, PIDGET (inferior_ptid));
c906108c
SS
908 status = target_write_memory (anaddr, buf, 4);
909 if (status != 0)
910 {
911 warning ("Unable to write __d_pid");
912 warning ("Suggest linking with /opt/langtools/lib/end.o.");
913 warning ("GDB will be unable to track shl_load/shl_unload calls");
914 goto keep_going;
915 }
916
917 /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
918 This will force the dynamic linker to call __d_trap when significant
919 events occur.
920
921 Note that the above is the pre-HP-UX 9.0 behaviour. At 9.0 and above,
922 the dld provides an export stub named "__d_trap" as well as the
923 function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
924 We'll look first for the old flavor and then the new.
c5aa993b 925 */
c906108c
SS
926 msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
927 if (msymbol == NULL)
c5aa993b 928 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
c906108c
SS
929 if (msymbol == NULL)
930 {
931 warning ("Unable to find _DLD_HOOK symbol in object file.");
932 warning ("Suggest linking with /opt/langtools/lib/end.o.");
933 warning ("GDB will be unable to track shl_load/shl_unload calls");
934 goto keep_going;
935 }
936 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
937 dld_cache.hook.address = anaddr;
938
939 /* Grrr, this might not be an export symbol! We have to find the
940 export stub. */
941 ALL_OBJFILES (objfile)
c5aa993b
JM
942 {
943 struct unwind_table_entry *u;
944 struct minimal_symbol *msymbol2;
945
946 /* What a crock. */
22abf04a 947 msymbol2 = lookup_minimal_symbol_solib_trampoline (DEPRECATED_SYMBOL_NAME (msymbol),
40324f1b 948 objfile);
c5aa993b
JM
949 /* Found a symbol with the right name. */
950 if (msymbol2)
951 {
952 struct unwind_table_entry *u;
953 /* It must be a shared library trampoline. */
954 if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
955 continue;
956
957 /* It must also be an export stub. */
958 u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
959 if (!u || u->stub_unwind.stub_type != EXPORT)
960 continue;
961
962 /* OK. Looks like the correct import stub. */
963 anaddr = SYMBOL_VALUE (msymbol2);
964 dld_cache.hook_stub.address = anaddr;
965 }
966 }
c906108c
SS
967 store_unsigned_integer (buf, 4, anaddr);
968
969 msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
970 if (msymbol == NULL)
971 {
972 warning ("Unable to find __dld_hook symbol in object file.");
973 warning ("Suggest linking with /opt/langtools/lib/end.o.");
974 warning ("GDB will be unable to track shl_load/shl_unload calls");
975 goto keep_going;
976 }
977 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
978 status = target_write_memory (anaddr, buf, 4);
c5aa993b 979
c906108c
SS
980 /* Now set a shlib_event breakpoint at __d_trap so we can track
981 significant shared library events. */
982 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
983 if (msymbol == NULL)
984 {
985 warning ("Unable to find __dld_d_trap symbol in object file.");
986 warning ("Suggest linking with /opt/langtools/lib/end.o.");
987 warning ("GDB will be unable to track shl_load/shl_unload calls");
988 goto keep_going;
989 }
990 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
991
992 /* We have all the support usually found in end.o, so we can track
993 shl_load and shl_unload calls. */
994 have_endo = 1;
995
996keep_going:
997
998 /* Get the address of __dld_flags, if no such symbol exists, then we can
999 not debug the shared code. */
1000 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
1001 if (msymbol == NULL)
1002 {
1003 error ("Unable to find __dld_flags symbol in object file.\n");
1004 }
1005
1006 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
1007
1008 /* Read the current contents. */
1009 status = target_read_memory (anaddr, buf, 4);
1010 if (status != 0)
1011 {
1012 error ("Unable to read __dld_flags\n");
1013 }
1014 dld_flags = extract_unsigned_integer (buf, 4);
1015
1016 /* Turn on the flags we care about. */
1017 dld_flags |= DLD_FLAGS_MAPPRIVATE;
1018 if (have_endo)
1019 dld_flags |= DLD_FLAGS_HOOKVALID;
1020 store_unsigned_integer (buf, 4, dld_flags);
1021 status = target_write_memory (anaddr, buf, 4);
1022 if (status != 0)
1023 {
1024 error ("Unable to write __dld_flags\n");
1025 }
1026
1027 /* Now find the address of _start and set a breakpoint there.
1028 We still need this code for two reasons:
1029
c5aa993b
JM
1030 * Not all sites have /opt/langtools/lib/end.o, so it's not always
1031 possible to track the dynamic linker's events.
1032
1033 * At this time no events are triggered for shared libraries
1034 loaded at startup time (what a crock). */
c906108c 1035
c906108c
SS
1036 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1037 if (msymbol == NULL)
1038 {
1039 error ("Unable to find _start symbol in object file.\n");
1040 }
1041
1042 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
1043
1044 /* Make the breakpoint at "_start" a shared library event breakpoint. */
1045 create_solib_event_breakpoint (anaddr);
1046
1047 /* Wipe out all knowledge of old shared libraries since their
1048 mapping can change from one exec to another! */
1049 while (so_list_head)
1050 {
1051 struct so_list *temp;
1052
1053 temp = so_list_head;
b8c9b27d 1054 xfree (so_list_head);
c906108c
SS
1055 so_list_head = temp->next;
1056 }
1057 clear_symtab_users ();
1058}
1059
c906108c
SS
1060/* This operation removes the "hook" between GDB and the dynamic linker,
1061 which causes the dld to notify GDB of shared library events.
1062
1063 After this operation completes, the dld will no longer notify GDB of
1064 shared library events. To resume notifications, GDB must call
1065 som_solib_create_inferior_hook.
1066
1067 This operation does not remove any knowledge of shared libraries which
1068 GDB may already have been notified of.
c5aa993b 1069 */
c906108c 1070void
fba45db2 1071som_solib_remove_inferior_hook (int pid)
c906108c 1072{
c5aa993b
JM
1073 CORE_ADDR addr;
1074 struct minimal_symbol *msymbol;
1075 int status;
0d99fdb9 1076 char dld_flags_buffer[4];
c5aa993b 1077 unsigned int dld_flags_value;
ce696e05 1078 struct cleanup *old_cleanups = save_inferior_ptid ();
c906108c
SS
1079
1080 /* Ensure that we're really operating on the specified process. */
39f77062 1081 inferior_ptid = pid_to_ptid (pid);
c906108c
SS
1082
1083 /* We won't bother to remove the solib breakpoints from this process.
1084
1085 In fact, on PA64 the breakpoint is hard-coded into the dld callback,
1086 and thus we're not supposed to remove it.
1087
1088 Rather, we'll merely clear the dld_flags bit that enables callbacks.
c5aa993b 1089 */
c906108c
SS
1090 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
1091
1092 addr = SYMBOL_VALUE_ADDRESS (msymbol);
9ca80cf8 1093 status = target_read_memory (addr, dld_flags_buffer, 4);
c906108c 1094
9ca80cf8 1095 dld_flags_value = extract_unsigned_integer (dld_flags_buffer, 4);
c906108c
SS
1096
1097 dld_flags_value &= ~DLD_FLAGS_HOOKVALID;
9ca80cf8
RC
1098 store_unsigned_integer (dld_flags_buffer, 4, dld_flags_value);
1099 status = target_write_memory (addr, dld_flags_buffer, 4);
c906108c
SS
1100
1101 do_cleanups (old_cleanups);
1102}
1103
1104
1105/* This function creates a breakpoint on the dynamic linker hook, which
1106 is called when e.g., a shl_load or shl_unload call is made. This
1107 breakpoint will only trigger when a shl_load call is made.
1108
1109 If filename is NULL, then loads of any dll will be caught. Else,
1110 only loads of the file whose pathname is the string contained by
1111 filename will be caught.
1112
1113 Undefined behaviour is guaranteed if this function is called before
1114 som_solib_create_inferior_hook.
c5aa993b 1115 */
c906108c 1116void
fba45db2
KB
1117som_solib_create_catch_load_hook (int pid, int tempflag, char *filename,
1118 char *cond_string)
c906108c
SS
1119{
1120 create_solib_load_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1121}
1122
1123/* This function creates a breakpoint on the dynamic linker hook, which
1124 is called when e.g., a shl_load or shl_unload call is made. This
1125 breakpoint will only trigger when a shl_unload call is made.
1126
1127 If filename is NULL, then unloads of any dll will be caught. Else,
1128 only unloads of the file whose pathname is the string contained by
1129 filename will be caught.
1130
1131 Undefined behaviour is guaranteed if this function is called before
1132 som_solib_create_inferior_hook.
c5aa993b 1133 */
c906108c 1134void
fba45db2
KB
1135som_solib_create_catch_unload_hook (int pid, int tempflag, char *filename,
1136 char *cond_string)
c906108c
SS
1137{
1138 create_solib_unload_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1139}
1140
1141int
fba45db2 1142som_solib_have_load_event (int pid)
c906108c 1143{
c5aa993b 1144 CORE_ADDR event_kind;
c906108c 1145
34f75cc1 1146 event_kind = read_register (HPPA_ARG0_REGNUM);
c906108c
SS
1147 return (event_kind == SHL_LOAD);
1148}
1149
1150int
fba45db2 1151som_solib_have_unload_event (int pid)
c906108c 1152{
c5aa993b 1153 CORE_ADDR event_kind;
c906108c 1154
34f75cc1 1155 event_kind = read_register (HPPA_ARG0_REGNUM);
c906108c
SS
1156 return (event_kind == SHL_UNLOAD);
1157}
1158
1159static char *
fba45db2 1160som_solib_library_pathname (int pid)
c906108c 1161{
c5aa993b
JM
1162 CORE_ADDR dll_handle_address;
1163 CORE_ADDR dll_pathname_address;
1164 struct som_solib_mapped_entry dll_descriptor;
1165 char *p;
1166 static char dll_pathname[1024];
c906108c
SS
1167
1168 /* Read the descriptor of this newly-loaded library. */
34f75cc1 1169 dll_handle_address = read_register (HPPA_ARG1_REGNUM);
c906108c
SS
1170 read_memory (dll_handle_address, (char *) &dll_descriptor, sizeof (dll_descriptor));
1171
1172 /* We can find a pointer to the dll's pathname within the descriptor. */
1173 dll_pathname_address = (CORE_ADDR) dll_descriptor.name;
c5aa993b 1174
c906108c
SS
1175 /* Read the pathname, one byte at a time. */
1176 p = dll_pathname;
1177 for (;;)
1178 {
c5aa993b 1179 char b;
c906108c
SS
1180 read_memory (dll_pathname_address++, (char *) &b, 1);
1181 *p++ = b;
1182 if (b == '\0')
c5aa993b 1183 break;
c906108c
SS
1184 }
1185
1186 return dll_pathname;
1187}
1188
1189char *
fba45db2 1190som_solib_loaded_library_pathname (int pid)
c906108c 1191{
c5aa993b 1192 if (!som_solib_have_load_event (pid))
c906108c
SS
1193 error ("Must have a load event to use this query");
1194
1195 return som_solib_library_pathname (pid);
1196}
1197
1198char *
fba45db2 1199som_solib_unloaded_library_pathname (int pid)
c906108c 1200{
c5aa993b 1201 if (!som_solib_have_unload_event (pid))
c906108c
SS
1202 error ("Must have an unload event to use this query");
1203
1204 return som_solib_library_pathname (pid);
1205}
1206
1207static void
fba45db2 1208som_solib_desire_dynamic_linker_symbols (void)
c906108c
SS
1209{
1210 struct objfile *objfile;
1211 struct unwind_table_entry *u;
c5aa993b 1212 struct minimal_symbol *dld_msymbol;
c906108c
SS
1213
1214 /* Do we already know the value of these symbols? If so, then
1215 we've no work to do.
1216
1217 (If you add clauses to this test, be sure to likewise update the
1218 test within the loop.)
c5aa993b 1219 */
c906108c
SS
1220 if (dld_cache.is_valid)
1221 return;
1222
1223 ALL_OBJFILES (objfile)
c5aa993b
JM
1224 {
1225 dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
1226 if (dld_msymbol != NULL)
1227 {
1228 dld_cache.load.address = SYMBOL_VALUE (dld_msymbol);
1229 dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
1230 }
1231
1232 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
c5aa993b
JM
1233 objfile);
1234 if (dld_msymbol != NULL)
1235 {
1236 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1237 {
1238 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1239 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1240 {
1241 dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol);
1242 dld_cache.load_stub.unwind = u;
1243 }
1244 }
1245 }
1246
1247 dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
1248 if (dld_msymbol != NULL)
1249 {
1250 dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol);
1251 dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);
1252
1253 /* ??rehrauer: I'm not sure exactly what this is, but it appears
1254 that on some HPUX 10.x versions, there's two unwind regions to
1255 cover the body of "shl_unload", the second being 4 bytes past
1256 the end of the first. This is a large hack to handle that
1257 case, but since I don't seem to have any legitimate way to
1258 look for this thing via the symbol table...
1259 */
1260 if (dld_cache.unload.unwind != NULL)
1261 {
1262 u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
1263 if (u != NULL)
1264 {
1265 dld_cache.unload2.address = u->region_start;
1266 dld_cache.unload2.unwind = u;
1267 }
1268 }
1269 }
1270
1271 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
c5aa993b
JM
1272 objfile);
1273 if (dld_msymbol != NULL)
1274 {
1275 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1276 {
1277 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1278 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1279 {
1280 dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol);
1281 dld_cache.unload_stub.unwind = u;
1282 }
1283 }
1284 }
1285
1286 /* Did we find everything we were looking for? If so, stop. */
8f236061
AC
1287 if ((dld_cache.load.address != 0)
1288 && (dld_cache.load_stub.address != 0)
1289 && (dld_cache.unload.address != 0)
1290 && (dld_cache.unload_stub.address != 0))
c5aa993b
JM
1291 {
1292 dld_cache.is_valid = 1;
1293 break;
1294 }
1295 }
c906108c
SS
1296
1297 dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
1298 dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);
1299
1300 /* We're prepared not to find some of these symbols, which is why
1301 this function is a "desire" operation, and not a "require".
c5aa993b 1302 */
c906108c
SS
1303}
1304
1305int
fba45db2 1306som_solib_in_dynamic_linker (int pid, CORE_ADDR pc)
c906108c 1307{
c5aa993b 1308 struct unwind_table_entry *u_pc;
c906108c
SS
1309
1310 /* Are we in the dld itself?
1311
1312 ??rehrauer: Large hack -- We'll assume that any address in a
1313 shared text region is the dld's text. This would obviously
1314 fall down if the user attached to a process, whose shlibs
1315 weren't mapped to a (writeable) private region. However, in
1316 that case the debugger probably isn't able to set the fundamental
1317 breakpoint in the dld callback anyways, so this hack should be
1318 safe.
c5aa993b 1319 */
c906108c
SS
1320 if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
1321 return 1;
1322
1323 /* Cache the address of some symbols that are part of the dynamic
1324 linker, if not already known.
c5aa993b 1325 */
c906108c
SS
1326 som_solib_desire_dynamic_linker_symbols ();
1327
1328 /* Are we in the dld callback? Or its export stub? */
1329 u_pc = find_unwind_entry (pc);
1330 if (u_pc == NULL)
1331 return 0;
1332
1333 if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
1334 return 1;
1335
1336 /* Or the interface of the dld (i.e., "shl_load" or friends)? */
1337 if ((u_pc == dld_cache.load.unwind)
1338 || (u_pc == dld_cache.unload.unwind)
1339 || (u_pc == dld_cache.unload2.unwind)
1340 || (u_pc == dld_cache.load_stub.unwind)
1341 || (u_pc == dld_cache.unload_stub.unwind))
1342 return 1;
1343
1344 /* Apparently this address isn't part of the dld's text. */
1345 return 0;
1346}
1347
1348
1349/* Return the GOT value for the shared library in which ADDR belongs. If
1350 ADDR isn't in any known shared library, return zero. */
1351
1352CORE_ADDR
fba45db2 1353som_solib_get_got_by_pc (CORE_ADDR addr)
c906108c
SS
1354{
1355 struct so_list *so_list = so_list_head;
1356 CORE_ADDR got_value = 0;
1357
1358 while (so_list)
1359 {
1360 if (so_list->som_solib.text_addr <= addr
1361 && so_list->som_solib.text_end > addr)
1362 {
1363 got_value = so_list->som_solib.got_value;
1364 break;
1365 }
1366 so_list = so_list->next;
1367 }
1368 return got_value;
1369}
1370
1371/* elz:
1372 Return the address of the handle of the shared library
1373 in which ADDR belongs. If
1374 ADDR isn't in any known shared library, return zero. */
c5aa993b 1375/* this function is used in hppa_fix_call_dummy in hppa-tdep.c */
c906108c
SS
1376
1377CORE_ADDR
fba45db2 1378som_solib_get_solib_by_pc (CORE_ADDR addr)
c906108c
SS
1379{
1380 struct so_list *so_list = so_list_head;
1381
1382 while (so_list)
1383 {
1384 if (so_list->som_solib.text_addr <= addr
1385 && so_list->som_solib.text_end > addr)
c5aa993b 1386 {
c906108c 1387 break;
c5aa993b 1388 }
c906108c
SS
1389 so_list = so_list->next;
1390 }
1391 if (so_list)
c5aa993b 1392 return so_list->solib_addr;
c906108c 1393 else
c5aa993b 1394 return 0;
c906108c
SS
1395}
1396
1397
1398int
fba45db2
KB
1399som_solib_section_offsets (struct objfile *objfile,
1400 struct section_offsets *offsets)
c906108c
SS
1401{
1402 struct so_list *so_list = so_list_head;
1403
1404 while (so_list)
1405 {
1406 /* Oh what a pain! We need the offsets before so_list->objfile
c5aa993b 1407 is valid. The BFDs will never match. Make a best guess. */
c906108c
SS
1408 if (strstr (objfile->name, so_list->som_solib.name))
1409 {
1410 asection *private_section;
1411
1412 /* The text offset is easy. */
f0a58b0b 1413 offsets->offsets[SECT_OFF_TEXT (objfile)]
c906108c
SS
1414 = (so_list->som_solib.text_addr
1415 - so_list->som_solib.text_link_addr);
f0a58b0b 1416 offsets->offsets[SECT_OFF_RODATA (objfile)]
b8fbeb18 1417 = ANOFFSET (offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1418
1419 /* We should look at presumed_dp in the SOM header, but
1420 that's not easily available. This should be OK though. */
1421 private_section = bfd_get_section_by_name (objfile->obfd,
1422 "$PRIVATE$");
1423 if (!private_section)
1424 {
1425 warning ("Unable to find $PRIVATE$ in shared library!");
f0a58b0b
EZ
1426 offsets->offsets[SECT_OFF_DATA (objfile)] = 0;
1427 offsets->offsets[SECT_OFF_BSS (objfile)] = 0;
c906108c
SS
1428 return 1;
1429 }
f0a58b0b 1430 offsets->offsets[SECT_OFF_DATA (objfile)]
c906108c 1431 = (so_list->som_solib.data_start - private_section->vma);
f0a58b0b 1432 offsets->offsets[SECT_OFF_BSS (objfile)]
b8fbeb18 1433 = ANOFFSET (offsets, SECT_OFF_DATA (objfile));
c906108c
SS
1434 return 1;
1435 }
1436 so_list = so_list->next;
1437 }
1438 return 0;
1439}
1440
1441/* Dump information about all the currently loaded shared libraries. */
1442
1443static void
fba45db2 1444som_sharedlibrary_info_command (char *ignore, int from_tty)
c906108c
SS
1445{
1446 struct so_list *so_list = so_list_head;
1447
1448 if (exec_bfd == NULL)
1449 {
4ce44c66 1450 printf_unfiltered ("No executable file.\n");
c906108c
SS
1451 return;
1452 }
1453
1454 if (so_list == NULL)
1455 {
1456 printf_unfiltered ("No shared libraries loaded at this time.\n");
1457 return;
1458 }
1459
1460 printf_unfiltered ("Shared Object Libraries\n");
1461 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
c5aa993b 1462 " flags", " tstart", " tend", " dstart", " dend", " dlt");
c906108c
SS
1463 while (so_list)
1464 {
1465 unsigned int flags;
1466
1467 flags = so_list->som_solib.struct_version << 24;
1468 flags |= so_list->som_solib.bind_mode << 16;
1469 flags |= so_list->som_solib.library_version;
1470 printf_unfiltered ("%s", so_list->som_solib.name);
1471 if (so_list->objfile == NULL)
c5aa993b 1472 printf_unfiltered (" (symbols not loaded)");
c906108c 1473 printf_unfiltered ("\n");
bb599908 1474 printf_unfiltered (" %-12s", hex_string_custom (flags, 8));
c906108c 1475 printf_unfiltered ("%-12s",
bb599908 1476 hex_string_custom (so_list->som_solib.text_addr, 8));
c906108c 1477 printf_unfiltered ("%-12s",
bb599908 1478 hex_string_custom (so_list->som_solib.text_end, 8));
c906108c 1479 printf_unfiltered ("%-12s",
bb599908 1480 hex_string_custom (so_list->som_solib.data_start, 8));
c906108c 1481 printf_unfiltered ("%-12s",
bb599908 1482 hex_string_custom (so_list->som_solib.data_end, 8));
c906108c 1483 printf_unfiltered ("%-12s\n",
bb599908 1484 hex_string_custom (so_list->som_solib.got_value, 8));
c906108c
SS
1485 so_list = so_list->next;
1486 }
1487}
1488
1489static void
fba45db2 1490som_solib_sharedlibrary_command (char *args, int from_tty)
c906108c
SS
1491{
1492 dont_repeat ();
990f9fe3 1493 som_solib_add (args, from_tty, (struct target_ops *) 0, 1);
c906108c
SS
1494}
1495
1496
1497
1498char *
fba45db2 1499som_solib_address (CORE_ADDR addr)
c906108c 1500{
c5aa993b 1501 struct so_list *so = so_list_head;
c906108c
SS
1502
1503 while (so)
1504 {
1505 /* Is this address within this shlib's text range? If so,
1506 return the shlib's name.
c5aa993b 1507 */
c906108c 1508 if ((addr >= so->som_solib.text_addr) && (addr <= so->som_solib.text_end))
c5aa993b 1509 return so->som_solib.name;
c906108c
SS
1510
1511 /* Nope, keep looking... */
1512 so = so->next;
1513 }
1514
1515 /* No, we couldn't prove that the address is within a shlib. */
1516 return NULL;
1517}
1518
1519
1520void
fba45db2 1521som_solib_restart (void)
c906108c 1522{
c5aa993b 1523 struct so_list *sl = so_list_head;
c906108c
SS
1524
1525 /* Before the shlib info vanishes, use it to disable any breakpoints
1526 that may still be active in those shlibs.
c5aa993b 1527 */
c906108c
SS
1528 disable_breakpoints_in_shlibs (0);
1529
1530 /* Discard all the shlib descriptors.
c5aa993b 1531 */
c906108c
SS
1532 while (sl)
1533 {
c5aa993b 1534 struct so_list *next_sl = sl->next;
b8c9b27d 1535 xfree (sl);
c906108c
SS
1536 sl = next_sl;
1537 }
1538 so_list_head = NULL;
1539
1540 som_solib_total_st_size = (LONGEST) 0;
1541 som_solib_st_size_threshold_exceeded = 0;
1542
1543 dld_cache.is_valid = 0;
1544
1545 dld_cache.hook.address = 0;
1546 dld_cache.hook.unwind = NULL;
1547
1548 dld_cache.hook_stub.address = 0;
1549 dld_cache.hook_stub.unwind = NULL;
1550
1551 dld_cache.load.address = 0;
1552 dld_cache.load.unwind = NULL;
1553
1554 dld_cache.load_stub.address = 0;
1555 dld_cache.load_stub.unwind = NULL;
1556
1557 dld_cache.unload.address = 0;
1558 dld_cache.unload.unwind = NULL;
1559
1560 dld_cache.unload2.address = 0;
1561 dld_cache.unload2.unwind = NULL;
1562
1563 dld_cache.unload_stub.address = 0;
1564 dld_cache.unload_stub.unwind = NULL;
1565}
1566
1567
c906108c 1568void
fba45db2 1569_initialize_som_solib (void)
c906108c
SS
1570{
1571 add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
c5aa993b 1572 "Load shared object library symbols for files matching REGEXP.");
c906108c
SS
1573 add_info ("sharedlibrary", som_sharedlibrary_info_command,
1574 "Status of loaded shared object libraries.");
b7209cb4 1575
cb1a6d5f 1576 deprecated_add_show_from_set
b7209cb4 1577 (add_set_cmd ("auto-solib-add", class_support, var_boolean,
c906108c 1578 (char *) &auto_solib_add,
b7209cb4
FF
1579 "Set autoloading of shared library symbols.\n\
1580If \"on\", symbols from all shared object libraries will be loaded\n\
1581automatically when the inferior begins execution, when the dynamic linker\n\
1582informs gdb that a new library has been loaded, or when attaching to the\n\
1583inferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
c906108c
SS
1584 &setlist),
1585 &showlist);
1586
cb1a6d5f 1587 deprecated_add_show_from_set
b7209cb4
FF
1588 (add_set_cmd ("auto-solib-limit", class_support, var_zinteger,
1589 (char *) &auto_solib_limit,
1590 "Set threshold (in Mb) for autoloading shared library symbols.\n\
1591When shared library autoloading is enabled, new libraries will be loaded\n\
1592only until the total size of shared library symbols exceeds this\n\
1593threshold in megabytes. Is ignored when using `sharedlibrary'.",
1594 &setlist),
1595 &showlist);
1596
1597 /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how
1598 much data space a process can use. We ought to be reading
1599 MAXDSIZ and setting auto_solib_limit to some large fraction of
1600 that value. If not that, we maybe ought to be setting it smaller
1601 than the default for MAXDSIZ (that being 64Mb, I believe).
1602 However, [1] this threshold is only crudely approximated rather
1603 than actually measured, and [2] 50 Mbytes is too small for
1604 debugging gdb itself. Thus, the arbitrary 100 figure. */
1605 auto_solib_limit = 100; /* Megabytes */
c906108c
SS
1606
1607 som_solib_restart ();
1608}
1609
1610/* Get some HPUX-specific data from a shared lib.
1611 */
1612CORE_ADDR
89676665 1613som_solib_thread_start_addr (struct so_list *so)
c906108c 1614{
c5aa993b 1615 return so->som_solib.tsd_start_addr;
c906108c 1616}
This page took 0.633218 seconds and 4 git commands to generate.