]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/somsolib.c
2005-02-15 Andrew Cagney <cagney@gnu.org>
[thirdparty/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);
8a3fe4f8 240 error (_("\"%s\": can't open to read symbols: %s."), filename,
c906108c
SS
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);
8a3fe4f8 248 error (_("\"%s\": can't read symbols: %s."), filename,
c906108c
SS
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 {
8a3fe4f8 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 363 {
8a3fe4f8 364 error (_("Unable to build section table for shared library\n."));
c906108c
SS
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 444 {
8a3fe4f8 445 error (_("Invalid regexp: %s"), re_err);
c906108c
SS
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)
8a3fe4f8 472 error (_("Unable to find __dld_flags symbol in object file."));
c906108c
SS
473
474 addr = SYMBOL_VALUE_ADDRESS (msymbol);
475 /* Read the current contents. */
476 status = target_read_memory (addr, buf, 4);
477 if (status != 0)
8a3fe4f8 478 error (_("Unable to read __dld_flags."));
c906108c
SS
479 dld_flags = extract_unsigned_integer (buf, 4);
480
481 /* __dld_list may not be valid. If not, then we punt, warning the user if
482 we were called as a result of the add-symfile command.
c5aa993b 483 */
c906108c
SS
484 if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
485 {
486 if (from_tty)
8a3fe4f8 487 error (_("__dld_list is not valid according to __dld_flags."));
c906108c
SS
488 return;
489 }
490
491 /* If the libraries were not mapped private, warn the user. */
492 if ((dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
8a3fe4f8
AC
493 warning (_("\
494The shared libraries were not privately mapped; setting a breakpoint\n\
495in a shared library will not work until you rerun the program."));
c906108c
SS
496
497 msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL);
498 if (!msymbol)
499 {
500 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
c5aa993b 501 but the data is still available if you know where to look. */
c906108c
SS
502 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
503 if (!msymbol)
8a3fe4f8 504 error (_("Unable to find dynamic library list."));
c906108c
SS
505 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
506 }
507 else
508 addr = SYMBOL_VALUE_ADDRESS (msymbol);
509
510 status = target_read_memory (addr, buf, 4);
511 if (status != 0)
8a3fe4f8 512 error (_("Unable to find dynamic library list."));
c906108c
SS
513
514 addr = extract_unsigned_integer (buf, 4);
515
516 /* If addr is zero, then we're using an old dynamic loader which
517 doesn't maintain __dld_list. We'll have to use a completely
518 different approach to get shared library information. */
519 if (addr == 0)
520 goto old_dld;
521
522 /* Using the information in __dld_list is the preferred method
523 to get at shared library information. It doesn't depend on
524 any functions in /opt/langtools/lib/end.o and has a chance of working
525 with hpux10 when it is released. */
526 status = target_read_memory (addr, buf, 4);
527 if (status != 0)
8a3fe4f8 528 error (_("Unable to find dynamic library list."));
c906108c
SS
529
530 /* addr now holds the address of the first entry in the dynamic
531 library list. */
532 addr = extract_unsigned_integer (buf, 4);
533
534 /* Now that we have a pointer to the dynamic library list, walk
535 through it and add the symbols for each library. */
536
537 so_list_tail = so_list_head;
538 /* Find the end of the list of shared objects. */
539 while (so_list_tail && so_list_tail->next)
540 so_list_tail = so_list_tail->next;
541
542#ifdef SOLIB_DEBUG
c5aa993b 543 printf ("--About to read shared library list data\n");
c906108c
SS
544#endif
545
546 /* "addr" will always point to the base of the
547 * current data entry describing the current
548 * shared library.
549 */
550 while (1)
551 {
552 CORE_ADDR name_addr, text_addr;
553 unsigned int name_len;
554 char *name;
555 struct so_list *new_so;
556 struct so_list *so_list = so_list_head;
557 struct stat statbuf;
c5aa993b
JM
558 LONGEST st_size;
559 int is_main_program;
c906108c
SS
560
561 if (addr == 0)
562 break;
563
564 /* Get a pointer to the name of this library. */
565 status = target_read_memory (addr, buf, 4);
566 if (status != 0)
567 goto err;
568
569 name_addr = extract_unsigned_integer (buf, 4);
570 name_len = 0;
571 while (1)
572 {
573 target_read_memory (name_addr + name_len, buf, 1);
574 if (status != 0)
575 goto err;
576
577 name_len++;
578 if (*buf == '\0')
579 break;
580 }
581 name = alloca (name_len);
582 status = target_read_memory (name_addr, name, name_len);
583 if (status != 0)
584 goto err;
585
586 /* See if we've already loaded something with this name. */
587 while (so_list)
588 {
589 if (!strcmp (so_list->som_solib.name, name))
590 break;
591 so_list = so_list->next;
592 }
593
594 /* See if the file exists. If not, give a warning, but don't
c5aa993b 595 die. */
c906108c
SS
596 status = stat (name, &statbuf);
597 if (status == -1)
598 {
8a3fe4f8 599 warning (_("Can't find file %s referenced in dld_list."), name);
c906108c
SS
600
601 status = target_read_memory (addr + 36, buf, 4);
602 if (status != 0)
603 goto err;
604
c5aa993b 605 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
c906108c
SS
606 continue;
607 }
608
609 /* If we've already loaded this one or it's the main program, skip it. */
610 is_main_program = (strcmp (name, symfile_objfile->name) == 0);
611 if (so_list || is_main_program)
612 {
c5aa993b
JM
613 /* This is the "next" pointer in the strcuture.
614 */
c906108c
SS
615 status = target_read_memory (addr + 36, buf, 4);
616 if (status != 0)
617 goto err;
618
619 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
620
c5aa993b
JM
621 /* Record the main program's symbol table size. */
622 if (is_main_program && !so_list)
623 {
624 st_size = som_solib_sizeof_symbol_table (name);
625 som_solib_total_st_size += st_size;
626 }
627
628 /* Was this a shlib that we noted but didn't load the symbols for?
629 If so, were we invoked this time from the command-line, via
630 a 'sharedlibrary' or 'add-symbol-file' command? If yes to
631 both, we'd better load the symbols this time.
632 */
633 if (from_tty && so_list && !is_main_program && (so_list->objfile == NULL))
634 som_solib_load_symbols (so_list,
635 name,
636 from_tty,
637 so_list->som_solib.text_addr,
638 target);
c906108c
SS
639
640 continue;
641 }
642
643 name = obsavestring (name, name_len - 1,
4a146b47 644 &symfile_objfile->objfile_obstack);
c906108c
SS
645
646 status = target_read_memory (addr + 8, buf, 4);
647 if (status != 0)
648 goto err;
649
650 text_addr = extract_unsigned_integer (buf, 4);
651
652 new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
c5aa993b 653 memset ((char *) new_so, 0, sizeof (struct so_list));
c906108c
SS
654 if (so_list_head == NULL)
655 {
656 so_list_head = new_so;
657 so_list_tail = new_so;
658 }
659 else
660 {
661 so_list_tail->next = new_so;
662 so_list_tail = new_so;
663 }
664
665 /* Fill in all the entries in GDB's shared library list.
666 */
c5aa993b 667
c906108c
SS
668 new_so->solib_addr = addr;
669 new_so->som_solib.name = name;
670 status = target_read_memory (addr + 4, buf, 4);
671 if (status != 0)
672 goto err;
673
674 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
675 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
c5aa993b
JM
676 /* Following is "high water mark", highest version number
677 * seen, rather than plain version number.
678 */
c906108c
SS
679 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
680 new_so->som_solib.text_addr = text_addr;
681
c5aa993b
JM
682 /* Q: What about longword at "addr + 8"?
683 * A: It's read above, out of order, into "text_addr".
684 */
c906108c
SS
685
686 status = target_read_memory (addr + 12, buf, 4);
687 if (status != 0)
688 goto err;
689
690 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
691
692 status = target_read_memory (addr + 16, buf, 4);
693 if (status != 0)
694 goto err;
695
696 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
697
698 status = target_read_memory (addr + 20, buf, 4);
699 if (status != 0)
700 goto err;
701
702 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
703
704 status = target_read_memory (addr + 24, buf, 4);
705 if (status != 0)
706 goto err;
707
708 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
709
710 status = target_read_memory (addr + 28, buf, 4);
711 if (status != 0)
712 goto err;
713
714 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
715
716 status = target_read_memory (addr + 32, buf, 4);
717 if (status != 0)
718 goto err;
719
720 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
721
722 status = target_read_memory (addr + 36, buf, 4);
723 if (status != 0)
724 goto err;
725
9a043c1d
AC
726 /* FIXME: cagney/2003-02-01: I think som_solib.next should be a
727 CORE_ADDR. */
8f236061 728 new_so->som_solib.next =
9a043c1d 729 hpux_address_to_host_pointer_hack (extract_unsigned_integer (buf, 4));
c906108c
SS
730
731 /* Note that we don't re-set "addr" to the next pointer
732 * until after we've read the trailing data.
733 */
734
735 status = target_read_memory (addr + 40, buf, 4);
736 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
737 if (status != 0)
c5aa993b 738 goto err;
c906108c
SS
739
740 /* Now indirect via that value!
741 */
742 status = target_read_memory (new_so->som_solib.tsd_start_addr, buf, 4);
743 new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
744 if (status != 0)
c5aa993b 745 goto err;
c906108c 746#ifdef SOLIB_DEBUG
c5aa993b
JM
747 printf ("\n+ library \"%s\" is described at 0x%x\n", name, addr);
748 printf (" 'version' is %d\n", new_so->som_solib.struct_version);
749 printf (" 'bind_mode' is %d\n", new_so->som_solib.bind_mode);
750 printf (" 'library_version' is %d\n", new_so->som_solib.library_version);
751 printf (" 'text_addr' is 0x%x\n", new_so->som_solib.text_addr);
752 printf (" 'text_link_addr' is 0x%x\n", new_so->som_solib.text_link_addr);
753 printf (" 'text_end' is 0x%x\n", new_so->som_solib.text_end);
754 printf (" 'data_start' is 0x%x\n", new_so->som_solib.data_start);
755 printf (" 'bss_start' is 0x%x\n", new_so->som_solib.bss_start);
756 printf (" 'data_end' is 0x%x\n", new_so->som_solib.data_end);
757 printf (" 'got_value' is %x\n", new_so->som_solib.got_value);
758 printf (" 'next' is 0x%x\n", new_so->som_solib.next);
759 printf (" 'tsd_start_addr' is 0x%x\n", new_so->som_solib.tsd_start_addr);
c906108c
SS
760#endif
761
762 /* Go on to the next shared library descriptor.
763 */
c5aa993b 764 addr = (CORE_ADDR) new_so->som_solib.next;
c906108c
SS
765
766
767
768 /* At this point, we have essentially hooked the shlib into the
769 "info share" command. However, we haven't yet loaded its
770 symbol table. We must now decide whether we ought to, i.e.,
771 whether doing so would exceed the symbol table size threshold.
772
773 If the threshold has just now been exceeded, then we'll issue
774 a warning message (which explains how to load symbols manually,
775 if the user so desires).
776
777 If the threshold has just now or previously been exceeded,
778 we'll just add the shlib to the list of object files, but won't
779 actually load its symbols. (This is more useful than it might
780 sound, for it allows us to e.g., still load and use the shlibs'
781 unwind information for stack tracebacks.)
c5aa993b 782 */
c906108c
SS
783
784 /* Note that we DON'T want to preclude the user from using the
785 add-symbol-file command! Thus, we only worry about the threshold
786 when we're invoked for other reasons.
c5aa993b 787 */
c906108c
SS
788 st_size = som_solib_sizeof_symbol_table (name);
789 som_solib_st_size_threshold_exceeded =
c5aa993b 790 !from_tty &&
9b3128c4 791 auto_solib_limit > 0 &&
990f9fe3 792 readsyms &&
c3c057c8 793 ((st_size + som_solib_total_st_size) > (auto_solib_limit * (LONGEST) (1024 * 1024)));
c906108c
SS
794
795 if (som_solib_st_size_threshold_exceeded)
c5aa993b
JM
796 {
797 if (!threshold_warning_given)
8a3fe4f8
AC
798 warning (_("\
799Symbols for some libraries have not been loaded, because doing so would\n\
800exceed the size threshold specified by auto-solib-limit.\n\
801To manually load symbols, use the 'sharedlibrary' command.\n\
802To raise the threshold, set auto-solib-limit to a larger value and rerun\n\
803the program."));
c5aa993b
JM
804 threshold_warning_given = 1;
805
806 /* We'll still make note of this shlib, even if we don't
807 read its symbols. This allows us to use its unwind
808 information well enough to know how to e.g., correctly
809 do a traceback from a PC within the shlib, even if we
810 can't symbolize those PCs...
811 */
812 som_solib_add_solib_objfile (new_so, name, from_tty, text_addr);
813 continue;
814 }
c906108c
SS
815
816 som_solib_total_st_size += st_size;
817
818 /* This fills in new_so->objfile, among others. */
819 som_solib_load_symbols (new_so, name, from_tty, text_addr, target);
820 }
821
822#ifdef SOLIB_DEBUG
c5aa993b 823 printf ("--Done reading shared library data\n");
c906108c
SS
824#endif
825
826 /* Getting new symbols may change our opinion about what is
827 frameless. */
828 reinit_frame_cache ();
829 return;
830
831old_dld:
a3f17187 832 error (_("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported."));
c906108c
SS
833
834err:
8a3fe4f8 835 error (_("Error while reading dynamic library list."));
c906108c
SS
836}
837
838
839/* This hook gets called just before the first instruction in the
840 inferior process is executed.
841
842 This is our opportunity to set magic flags in the inferior so
843 that GDB can be notified when a shared library is mapped in and
844 to tell the dynamic linker that a private copy of the library is
845 needed (so GDB can set breakpoints in the library).
846
847 __dld_flags is the location of the magic flags; as of this implementation
848 there are 3 flags of interest:
849
850 bit 0 when set indicates that private copies of the libraries are needed
851 bit 1 when set indicates that the callback hook routine is valid
852 bit 2 when set indicates that the dynamic linker should maintain the
c5aa993b 853 __dld_list structure when loading/unloading libraries.
c906108c
SS
854
855 Note that shared libraries are not mapped in at this time, so we have
856 run the inferior until the libraries are mapped in. Typically this
857 means running until the "_start" is called. */
858
859void
fba45db2 860som_solib_create_inferior_hook (void)
c906108c
SS
861{
862 struct minimal_symbol *msymbol;
863 unsigned int dld_flags, status, have_endo;
864 asection *shlib_info;
865 char buf[4];
866 struct objfile *objfile;
867 CORE_ADDR anaddr;
868
869 /* First, remove all the solib event breakpoints. Their addresses
870 may have changed since the last time we ran the program. */
871 remove_solib_event_breakpoints ();
872
873 if (symfile_objfile == NULL)
c5aa993b 874 return;
c906108c
SS
875
876 /* First see if the objfile was dynamically linked. */
877 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
878 if (!shlib_info)
879 return;
880
881 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
882 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
883 return;
884
885 have_endo = 0;
9b3128c4
JL
886 /* Slam the pid of the process into __d_pid.
887
888 We used to warn when this failed, but that warning is only useful
889 on very old HP systems (hpux9 and older). The warnings are an
890 annoyance to users of modern systems and foul up the testsuite as
891 well. As a result, the warnings have been disabled. */
c906108c
SS
892 msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
893 if (msymbol == NULL)
9b3128c4 894 goto keep_going;
c906108c
SS
895
896 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
39f77062 897 store_unsigned_integer (buf, 4, PIDGET (inferior_ptid));
c906108c
SS
898 status = target_write_memory (anaddr, buf, 4);
899 if (status != 0)
900 {
8a3fe4f8
AC
901 warning (_("Unable to write __d_pid"));
902 warning (_("Suggest linking with /opt/langtools/lib/end.o."));
903 warning (_("GDB will be unable to track shl_load/shl_unload calls"));
c906108c
SS
904 goto keep_going;
905 }
906
907 /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
908 This will force the dynamic linker to call __d_trap when significant
909 events occur.
910
911 Note that the above is the pre-HP-UX 9.0 behaviour. At 9.0 and above,
912 the dld provides an export stub named "__d_trap" as well as the
913 function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
914 We'll look first for the old flavor and then the new.
c5aa993b 915 */
c906108c
SS
916 msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
917 if (msymbol == NULL)
c5aa993b 918 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
c906108c
SS
919 if (msymbol == NULL)
920 {
8a3fe4f8
AC
921 warning (_("Unable to find _DLD_HOOK symbol in object file."));
922 warning (_("Suggest linking with /opt/langtools/lib/end.o."));
923 warning (_("GDB will be unable to track shl_load/shl_unload calls"));
c906108c
SS
924 goto keep_going;
925 }
926 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
927 dld_cache.hook.address = anaddr;
928
929 /* Grrr, this might not be an export symbol! We have to find the
930 export stub. */
931 ALL_OBJFILES (objfile)
c5aa993b
JM
932 {
933 struct unwind_table_entry *u;
934 struct minimal_symbol *msymbol2;
935
936 /* What a crock. */
22abf04a 937 msymbol2 = lookup_minimal_symbol_solib_trampoline (DEPRECATED_SYMBOL_NAME (msymbol),
40324f1b 938 objfile);
c5aa993b
JM
939 /* Found a symbol with the right name. */
940 if (msymbol2)
941 {
942 struct unwind_table_entry *u;
943 /* It must be a shared library trampoline. */
944 if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
945 continue;
946
947 /* It must also be an export stub. */
948 u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
949 if (!u || u->stub_unwind.stub_type != EXPORT)
950 continue;
951
952 /* OK. Looks like the correct import stub. */
953 anaddr = SYMBOL_VALUE (msymbol2);
954 dld_cache.hook_stub.address = anaddr;
955 }
956 }
c906108c
SS
957 store_unsigned_integer (buf, 4, anaddr);
958
959 msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
960 if (msymbol == NULL)
961 {
8a3fe4f8
AC
962 warning (_("Unable to find __dld_hook symbol in object file."));
963 warning (_("Suggest linking with /opt/langtools/lib/end.o."));
964 warning (_("GDB will be unable to track shl_load/shl_unload calls"));
c906108c
SS
965 goto keep_going;
966 }
967 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
968 status = target_write_memory (anaddr, buf, 4);
c5aa993b 969
c906108c
SS
970 /* Now set a shlib_event breakpoint at __d_trap so we can track
971 significant shared library events. */
972 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
973 if (msymbol == NULL)
974 {
8a3fe4f8
AC
975 warning (_("Unable to find __dld_d_trap symbol in object file."));
976 warning (_("Suggest linking with /opt/langtools/lib/end.o."));
977 warning (_("GDB will be unable to track shl_load/shl_unload calls"));
c906108c
SS
978 goto keep_going;
979 }
980 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
981
982 /* We have all the support usually found in end.o, so we can track
983 shl_load and shl_unload calls. */
984 have_endo = 1;
985
986keep_going:
987
988 /* Get the address of __dld_flags, if no such symbol exists, then we can
989 not debug the shared code. */
990 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
991 if (msymbol == NULL)
8a3fe4f8 992 error (_("Unable to find __dld_flags symbol in object file."));
c906108c
SS
993
994 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
995
996 /* Read the current contents. */
997 status = target_read_memory (anaddr, buf, 4);
998 if (status != 0)
8a3fe4f8 999 error (_("Unable to read __dld_flags."));
c906108c
SS
1000 dld_flags = extract_unsigned_integer (buf, 4);
1001
1002 /* Turn on the flags we care about. */
1003 dld_flags |= DLD_FLAGS_MAPPRIVATE;
1004 if (have_endo)
1005 dld_flags |= DLD_FLAGS_HOOKVALID;
1006 store_unsigned_integer (buf, 4, dld_flags);
1007 status = target_write_memory (anaddr, buf, 4);
1008 if (status != 0)
8a3fe4f8 1009 error (_("Unable to write __dld_flags."));
c906108c
SS
1010
1011 /* Now find the address of _start and set a breakpoint there.
1012 We still need this code for two reasons:
1013
c5aa993b
JM
1014 * Not all sites have /opt/langtools/lib/end.o, so it's not always
1015 possible to track the dynamic linker's events.
1016
1017 * At this time no events are triggered for shared libraries
1018 loaded at startup time (what a crock). */
c906108c 1019
c906108c
SS
1020 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1021 if (msymbol == NULL)
8a3fe4f8 1022 error (_("Unable to find _start symbol in object file."));
c906108c
SS
1023
1024 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
1025
1026 /* Make the breakpoint at "_start" a shared library event breakpoint. */
1027 create_solib_event_breakpoint (anaddr);
1028
1029 /* Wipe out all knowledge of old shared libraries since their
1030 mapping can change from one exec to another! */
1031 while (so_list_head)
1032 {
1033 struct so_list *temp;
1034
1035 temp = so_list_head;
b8c9b27d 1036 xfree (so_list_head);
c906108c
SS
1037 so_list_head = temp->next;
1038 }
1039 clear_symtab_users ();
1040}
1041
c906108c
SS
1042/* This operation removes the "hook" between GDB and the dynamic linker,
1043 which causes the dld to notify GDB of shared library events.
1044
1045 After this operation completes, the dld will no longer notify GDB of
1046 shared library events. To resume notifications, GDB must call
1047 som_solib_create_inferior_hook.
1048
318965d7
MS
1049 This operation does not remove any knowledge of shared libraries
1050 of which GDB may already have been notified.
c5aa993b 1051 */
c906108c 1052void
fba45db2 1053som_solib_remove_inferior_hook (int pid)
c906108c 1054{
c5aa993b
JM
1055 CORE_ADDR addr;
1056 struct minimal_symbol *msymbol;
1057 int status;
0d99fdb9 1058 char dld_flags_buffer[4];
c5aa993b 1059 unsigned int dld_flags_value;
ce696e05 1060 struct cleanup *old_cleanups = save_inferior_ptid ();
c906108c
SS
1061
1062 /* Ensure that we're really operating on the specified process. */
39f77062 1063 inferior_ptid = pid_to_ptid (pid);
c906108c
SS
1064
1065 /* We won't bother to remove the solib breakpoints from this process.
1066
1067 In fact, on PA64 the breakpoint is hard-coded into the dld callback,
1068 and thus we're not supposed to remove it.
1069
1070 Rather, we'll merely clear the dld_flags bit that enables callbacks.
c5aa993b 1071 */
c906108c
SS
1072 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
1073
1074 addr = SYMBOL_VALUE_ADDRESS (msymbol);
9ca80cf8 1075 status = target_read_memory (addr, dld_flags_buffer, 4);
c906108c 1076
9ca80cf8 1077 dld_flags_value = extract_unsigned_integer (dld_flags_buffer, 4);
c906108c
SS
1078
1079 dld_flags_value &= ~DLD_FLAGS_HOOKVALID;
9ca80cf8
RC
1080 store_unsigned_integer (dld_flags_buffer, 4, dld_flags_value);
1081 status = target_write_memory (addr, dld_flags_buffer, 4);
c906108c
SS
1082
1083 do_cleanups (old_cleanups);
1084}
1085
1086
1087/* This function creates a breakpoint on the dynamic linker hook, which
1088 is called when e.g., a shl_load or shl_unload call is made. This
1089 breakpoint will only trigger when a shl_load call is made.
1090
1091 If filename is NULL, then loads of any dll will be caught. Else,
1092 only loads of the file whose pathname is the string contained by
1093 filename will be caught.
1094
1095 Undefined behaviour is guaranteed if this function is called before
1096 som_solib_create_inferior_hook.
c5aa993b 1097 */
c906108c 1098void
fba45db2
KB
1099som_solib_create_catch_load_hook (int pid, int tempflag, char *filename,
1100 char *cond_string)
c906108c 1101{
318965d7
MS
1102 create_solib_load_event_breakpoint ("__d_trap", tempflag,
1103 filename, cond_string);
c906108c
SS
1104}
1105
1106/* This function creates a breakpoint on the dynamic linker hook, which
1107 is called when e.g., a shl_load or shl_unload call is made. This
1108 breakpoint will only trigger when a shl_unload call is made.
1109
1110 If filename is NULL, then unloads of any dll will be caught. Else,
1111 only unloads of the file whose pathname is the string contained by
1112 filename will be caught.
1113
1114 Undefined behaviour is guaranteed if this function is called before
1115 som_solib_create_inferior_hook.
c5aa993b 1116 */
c906108c 1117void
fba45db2
KB
1118som_solib_create_catch_unload_hook (int pid, int tempflag, char *filename,
1119 char *cond_string)
c906108c 1120{
318965d7
MS
1121 create_solib_unload_event_breakpoint ("__d_trap", tempflag,
1122 filename, cond_string);
c906108c
SS
1123}
1124
1125int
fba45db2 1126som_solib_have_load_event (int pid)
c906108c 1127{
c5aa993b 1128 CORE_ADDR event_kind;
c906108c 1129
34f75cc1 1130 event_kind = read_register (HPPA_ARG0_REGNUM);
c906108c
SS
1131 return (event_kind == SHL_LOAD);
1132}
1133
1134int
fba45db2 1135som_solib_have_unload_event (int pid)
c906108c 1136{
c5aa993b 1137 CORE_ADDR event_kind;
c906108c 1138
34f75cc1 1139 event_kind = read_register (HPPA_ARG0_REGNUM);
c906108c
SS
1140 return (event_kind == SHL_UNLOAD);
1141}
1142
1143static char *
fba45db2 1144som_solib_library_pathname (int pid)
c906108c 1145{
c5aa993b
JM
1146 CORE_ADDR dll_handle_address;
1147 CORE_ADDR dll_pathname_address;
1148 struct som_solib_mapped_entry dll_descriptor;
1149 char *p;
1150 static char dll_pathname[1024];
c906108c
SS
1151
1152 /* Read the descriptor of this newly-loaded library. */
34f75cc1 1153 dll_handle_address = read_register (HPPA_ARG1_REGNUM);
c906108c
SS
1154 read_memory (dll_handle_address, (char *) &dll_descriptor, sizeof (dll_descriptor));
1155
1156 /* We can find a pointer to the dll's pathname within the descriptor. */
1157 dll_pathname_address = (CORE_ADDR) dll_descriptor.name;
c5aa993b 1158
c906108c
SS
1159 /* Read the pathname, one byte at a time. */
1160 p = dll_pathname;
1161 for (;;)
1162 {
c5aa993b 1163 char b;
c906108c
SS
1164 read_memory (dll_pathname_address++, (char *) &b, 1);
1165 *p++ = b;
1166 if (b == '\0')
c5aa993b 1167 break;
c906108c
SS
1168 }
1169
1170 return dll_pathname;
1171}
1172
1173char *
fba45db2 1174som_solib_loaded_library_pathname (int pid)
c906108c 1175{
c5aa993b 1176 if (!som_solib_have_load_event (pid))
8a3fe4f8 1177 error (_("Must have a load event to use this query"));
c906108c
SS
1178
1179 return som_solib_library_pathname (pid);
1180}
1181
1182char *
fba45db2 1183som_solib_unloaded_library_pathname (int pid)
c906108c 1184{
c5aa993b 1185 if (!som_solib_have_unload_event (pid))
8a3fe4f8 1186 error (_("Must have an unload event to use this query"));
c906108c
SS
1187
1188 return som_solib_library_pathname (pid);
1189}
1190
1191static void
fba45db2 1192som_solib_desire_dynamic_linker_symbols (void)
c906108c
SS
1193{
1194 struct objfile *objfile;
1195 struct unwind_table_entry *u;
c5aa993b 1196 struct minimal_symbol *dld_msymbol;
c906108c
SS
1197
1198 /* Do we already know the value of these symbols? If so, then
1199 we've no work to do.
1200
1201 (If you add clauses to this test, be sure to likewise update the
1202 test within the loop.)
c5aa993b 1203 */
c906108c
SS
1204 if (dld_cache.is_valid)
1205 return;
1206
1207 ALL_OBJFILES (objfile)
c5aa993b
JM
1208 {
1209 dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
1210 if (dld_msymbol != NULL)
1211 {
1212 dld_cache.load.address = SYMBOL_VALUE (dld_msymbol);
1213 dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
1214 }
1215
1216 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
c5aa993b
JM
1217 objfile);
1218 if (dld_msymbol != NULL)
1219 {
1220 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1221 {
1222 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1223 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1224 {
1225 dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol);
1226 dld_cache.load_stub.unwind = u;
1227 }
1228 }
1229 }
1230
1231 dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
1232 if (dld_msymbol != NULL)
1233 {
1234 dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol);
1235 dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);
1236
1237 /* ??rehrauer: I'm not sure exactly what this is, but it appears
1238 that on some HPUX 10.x versions, there's two unwind regions to
1239 cover the body of "shl_unload", the second being 4 bytes past
1240 the end of the first. This is a large hack to handle that
1241 case, but since I don't seem to have any legitimate way to
1242 look for this thing via the symbol table...
1243 */
1244 if (dld_cache.unload.unwind != NULL)
1245 {
1246 u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
1247 if (u != NULL)
1248 {
1249 dld_cache.unload2.address = u->region_start;
1250 dld_cache.unload2.unwind = u;
1251 }
1252 }
1253 }
1254
1255 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
c5aa993b
JM
1256 objfile);
1257 if (dld_msymbol != NULL)
1258 {
1259 if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1260 {
1261 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1262 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1263 {
1264 dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol);
1265 dld_cache.unload_stub.unwind = u;
1266 }
1267 }
1268 }
1269
1270 /* Did we find everything we were looking for? If so, stop. */
8f236061
AC
1271 if ((dld_cache.load.address != 0)
1272 && (dld_cache.load_stub.address != 0)
1273 && (dld_cache.unload.address != 0)
1274 && (dld_cache.unload_stub.address != 0))
c5aa993b
JM
1275 {
1276 dld_cache.is_valid = 1;
1277 break;
1278 }
1279 }
c906108c
SS
1280
1281 dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
1282 dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);
1283
1284 /* We're prepared not to find some of these symbols, which is why
1285 this function is a "desire" operation, and not a "require".
c5aa993b 1286 */
c906108c
SS
1287}
1288
1289int
fba45db2 1290som_solib_in_dynamic_linker (int pid, CORE_ADDR pc)
c906108c 1291{
c5aa993b 1292 struct unwind_table_entry *u_pc;
c906108c
SS
1293
1294 /* Are we in the dld itself?
1295
1296 ??rehrauer: Large hack -- We'll assume that any address in a
1297 shared text region is the dld's text. This would obviously
1298 fall down if the user attached to a process, whose shlibs
1299 weren't mapped to a (writeable) private region. However, in
1300 that case the debugger probably isn't able to set the fundamental
1301 breakpoint in the dld callback anyways, so this hack should be
1302 safe.
c5aa993b 1303 */
c906108c
SS
1304 if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
1305 return 1;
1306
1307 /* Cache the address of some symbols that are part of the dynamic
1308 linker, if not already known.
c5aa993b 1309 */
c906108c
SS
1310 som_solib_desire_dynamic_linker_symbols ();
1311
1312 /* Are we in the dld callback? Or its export stub? */
1313 u_pc = find_unwind_entry (pc);
1314 if (u_pc == NULL)
1315 return 0;
1316
1317 if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
1318 return 1;
1319
1320 /* Or the interface of the dld (i.e., "shl_load" or friends)? */
1321 if ((u_pc == dld_cache.load.unwind)
1322 || (u_pc == dld_cache.unload.unwind)
1323 || (u_pc == dld_cache.unload2.unwind)
1324 || (u_pc == dld_cache.load_stub.unwind)
1325 || (u_pc == dld_cache.unload_stub.unwind))
1326 return 1;
1327
1328 /* Apparently this address isn't part of the dld's text. */
1329 return 0;
1330}
1331
1332
1333/* Return the GOT value for the shared library in which ADDR belongs. If
1334 ADDR isn't in any known shared library, return zero. */
1335
1336CORE_ADDR
fba45db2 1337som_solib_get_got_by_pc (CORE_ADDR addr)
c906108c
SS
1338{
1339 struct so_list *so_list = so_list_head;
1340 CORE_ADDR got_value = 0;
1341
1342 while (so_list)
1343 {
1344 if (so_list->som_solib.text_addr <= addr
1345 && so_list->som_solib.text_end > addr)
1346 {
1347 got_value = so_list->som_solib.got_value;
1348 break;
1349 }
1350 so_list = so_list->next;
1351 }
1352 return got_value;
1353}
1354
1355/* elz:
1356 Return the address of the handle of the shared library
1357 in which ADDR belongs. If
1358 ADDR isn't in any known shared library, return zero. */
c5aa993b 1359/* this function is used in hppa_fix_call_dummy in hppa-tdep.c */
c906108c
SS
1360
1361CORE_ADDR
fba45db2 1362som_solib_get_solib_by_pc (CORE_ADDR addr)
c906108c
SS
1363{
1364 struct so_list *so_list = so_list_head;
1365
1366 while (so_list)
1367 {
1368 if (so_list->som_solib.text_addr <= addr
1369 && so_list->som_solib.text_end > addr)
c5aa993b 1370 {
c906108c 1371 break;
c5aa993b 1372 }
c906108c
SS
1373 so_list = so_list->next;
1374 }
1375 if (so_list)
c5aa993b 1376 return so_list->solib_addr;
c906108c 1377 else
c5aa993b 1378 return 0;
c906108c
SS
1379}
1380
1381
1382int
fba45db2
KB
1383som_solib_section_offsets (struct objfile *objfile,
1384 struct section_offsets *offsets)
c906108c
SS
1385{
1386 struct so_list *so_list = so_list_head;
1387
1388 while (so_list)
1389 {
1390 /* Oh what a pain! We need the offsets before so_list->objfile
c5aa993b 1391 is valid. The BFDs will never match. Make a best guess. */
c906108c
SS
1392 if (strstr (objfile->name, so_list->som_solib.name))
1393 {
1394 asection *private_section;
1395
1396 /* The text offset is easy. */
f0a58b0b 1397 offsets->offsets[SECT_OFF_TEXT (objfile)]
c906108c
SS
1398 = (so_list->som_solib.text_addr
1399 - so_list->som_solib.text_link_addr);
f0a58b0b 1400 offsets->offsets[SECT_OFF_RODATA (objfile)]
b8fbeb18 1401 = ANOFFSET (offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1402
1403 /* We should look at presumed_dp in the SOM header, but
1404 that's not easily available. This should be OK though. */
1405 private_section = bfd_get_section_by_name (objfile->obfd,
1406 "$PRIVATE$");
1407 if (!private_section)
1408 {
8a3fe4f8 1409 warning (_("Unable to find $PRIVATE$ in shared library!"));
f0a58b0b
EZ
1410 offsets->offsets[SECT_OFF_DATA (objfile)] = 0;
1411 offsets->offsets[SECT_OFF_BSS (objfile)] = 0;
c906108c
SS
1412 return 1;
1413 }
f0a58b0b 1414 offsets->offsets[SECT_OFF_DATA (objfile)]
c906108c 1415 = (so_list->som_solib.data_start - private_section->vma);
f0a58b0b 1416 offsets->offsets[SECT_OFF_BSS (objfile)]
b8fbeb18 1417 = ANOFFSET (offsets, SECT_OFF_DATA (objfile));
c906108c
SS
1418 return 1;
1419 }
1420 so_list = so_list->next;
1421 }
1422 return 0;
1423}
1424
1425/* Dump information about all the currently loaded shared libraries. */
1426
1427static void
fba45db2 1428som_sharedlibrary_info_command (char *ignore, int from_tty)
c906108c
SS
1429{
1430 struct so_list *so_list = so_list_head;
1431
1432 if (exec_bfd == NULL)
1433 {
4ce44c66 1434 printf_unfiltered ("No executable file.\n");
c906108c
SS
1435 return;
1436 }
1437
1438 if (so_list == NULL)
1439 {
1440 printf_unfiltered ("No shared libraries loaded at this time.\n");
1441 return;
1442 }
1443
1444 printf_unfiltered ("Shared Object Libraries\n");
1445 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
c5aa993b 1446 " flags", " tstart", " tend", " dstart", " dend", " dlt");
c906108c
SS
1447 while (so_list)
1448 {
1449 unsigned int flags;
1450
1451 flags = so_list->som_solib.struct_version << 24;
1452 flags |= so_list->som_solib.bind_mode << 16;
1453 flags |= so_list->som_solib.library_version;
1454 printf_unfiltered ("%s", so_list->som_solib.name);
1455 if (so_list->objfile == NULL)
c5aa993b 1456 printf_unfiltered (" (symbols not loaded)");
c906108c 1457 printf_unfiltered ("\n");
bb599908 1458 printf_unfiltered (" %-12s", hex_string_custom (flags, 8));
c906108c 1459 printf_unfiltered ("%-12s",
bb599908 1460 hex_string_custom (so_list->som_solib.text_addr, 8));
c906108c 1461 printf_unfiltered ("%-12s",
bb599908 1462 hex_string_custom (so_list->som_solib.text_end, 8));
c906108c 1463 printf_unfiltered ("%-12s",
bb599908 1464 hex_string_custom (so_list->som_solib.data_start, 8));
c906108c 1465 printf_unfiltered ("%-12s",
bb599908 1466 hex_string_custom (so_list->som_solib.data_end, 8));
c906108c 1467 printf_unfiltered ("%-12s\n",
bb599908 1468 hex_string_custom (so_list->som_solib.got_value, 8));
c906108c
SS
1469 so_list = so_list->next;
1470 }
1471}
1472
1473static void
fba45db2 1474som_solib_sharedlibrary_command (char *args, int from_tty)
c906108c
SS
1475{
1476 dont_repeat ();
990f9fe3 1477 som_solib_add (args, from_tty, (struct target_ops *) 0, 1);
c906108c
SS
1478}
1479
1480
1481
1482char *
fba45db2 1483som_solib_address (CORE_ADDR addr)
c906108c 1484{
c5aa993b 1485 struct so_list *so = so_list_head;
c906108c
SS
1486
1487 while (so)
1488 {
1489 /* Is this address within this shlib's text range? If so,
1490 return the shlib's name.
c5aa993b 1491 */
c906108c 1492 if ((addr >= so->som_solib.text_addr) && (addr <= so->som_solib.text_end))
c5aa993b 1493 return so->som_solib.name;
c906108c
SS
1494
1495 /* Nope, keep looking... */
1496 so = so->next;
1497 }
1498
1499 /* No, we couldn't prove that the address is within a shlib. */
1500 return NULL;
1501}
1502
1503
1504void
fba45db2 1505som_solib_restart (void)
c906108c 1506{
c5aa993b 1507 struct so_list *sl = so_list_head;
c906108c
SS
1508
1509 /* Before the shlib info vanishes, use it to disable any breakpoints
1510 that may still be active in those shlibs.
c5aa993b 1511 */
c906108c
SS
1512 disable_breakpoints_in_shlibs (0);
1513
1514 /* Discard all the shlib descriptors.
c5aa993b 1515 */
c906108c
SS
1516 while (sl)
1517 {
c5aa993b 1518 struct so_list *next_sl = sl->next;
b8c9b27d 1519 xfree (sl);
c906108c
SS
1520 sl = next_sl;
1521 }
1522 so_list_head = NULL;
1523
1524 som_solib_total_st_size = (LONGEST) 0;
1525 som_solib_st_size_threshold_exceeded = 0;
1526
1527 dld_cache.is_valid = 0;
1528
1529 dld_cache.hook.address = 0;
1530 dld_cache.hook.unwind = NULL;
1531
1532 dld_cache.hook_stub.address = 0;
1533 dld_cache.hook_stub.unwind = NULL;
1534
1535 dld_cache.load.address = 0;
1536 dld_cache.load.unwind = NULL;
1537
1538 dld_cache.load_stub.address = 0;
1539 dld_cache.load_stub.unwind = NULL;
1540
1541 dld_cache.unload.address = 0;
1542 dld_cache.unload.unwind = NULL;
1543
1544 dld_cache.unload2.address = 0;
1545 dld_cache.unload2.unwind = NULL;
1546
1547 dld_cache.unload_stub.address = 0;
1548 dld_cache.unload_stub.unwind = NULL;
1549}
1550
1551
c906108c 1552void
fba45db2 1553_initialize_som_solib (void)
c906108c
SS
1554{
1555 add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
1bedd215 1556 _("Load shared object library symbols for files matching REGEXP."));
c906108c 1557 add_info ("sharedlibrary", som_sharedlibrary_info_command,
1bedd215 1558 _("Status of loaded shared object libraries."));
b7209cb4 1559
cb1a6d5f 1560 deprecated_add_show_from_set
b7209cb4 1561 (add_set_cmd ("auto-solib-add", class_support, var_boolean,
c906108c 1562 (char *) &auto_solib_add,
b7209cb4
FF
1563 "Set autoloading of shared library symbols.\n\
1564If \"on\", symbols from all shared object libraries will be loaded\n\
1565automatically when the inferior begins execution, when the dynamic linker\n\
1566informs gdb that a new library has been loaded, or when attaching to the\n\
1567inferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
c906108c
SS
1568 &setlist),
1569 &showlist);
1570
cb1a6d5f 1571 deprecated_add_show_from_set
b7209cb4
FF
1572 (add_set_cmd ("auto-solib-limit", class_support, var_zinteger,
1573 (char *) &auto_solib_limit,
1574 "Set threshold (in Mb) for autoloading shared library symbols.\n\
1575When shared library autoloading is enabled, new libraries will be loaded\n\
1576only until the total size of shared library symbols exceeds this\n\
1577threshold in megabytes. Is ignored when using `sharedlibrary'.",
1578 &setlist),
1579 &showlist);
1580
1581 /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how
1582 much data space a process can use. We ought to be reading
1583 MAXDSIZ and setting auto_solib_limit to some large fraction of
1584 that value. If not that, we maybe ought to be setting it smaller
1585 than the default for MAXDSIZ (that being 64Mb, I believe).
1586 However, [1] this threshold is only crudely approximated rather
1587 than actually measured, and [2] 50 Mbytes is too small for
1588 debugging gdb itself. Thus, the arbitrary 100 figure. */
1589 auto_solib_limit = 100; /* Megabytes */
c906108c
SS
1590
1591 som_solib_restart ();
1592}
1593
1594/* Get some HPUX-specific data from a shared lib.
1595 */
1596CORE_ADDR
89676665 1597som_solib_thread_start_addr (struct so_list *so)
c906108c 1598{
c5aa993b 1599 return so->som_solib.tsd_start_addr;
c906108c 1600}
792432a5
MK
1601
1602#ifdef PA_SOM_ONLY
1603void
1604no_shared_libraries (char *ignored, int from_tty)
1605{
1606}
1607#endif