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