Fix whitespace problem in my most recent entry.
[deliverable/binutils-gdb.git] / gdb / osfsolib.c
CommitLineData
c906108c
SS
1/* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993, 94, 95, 96, 98, 1999 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21/* FIXME: Most of this code could be merged with solib.c by using
22 next_link_map_member and xfer_link_map_member in solib.c. */
23
24#include "defs.h"
25
26#include <sys/types.h>
27#include <signal.h>
28#include "gdb_string.h"
29#include <fcntl.h>
30
31#include "symtab.h"
32#include "bfd.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "gdbcore.h"
36#include "command.h"
37#include "target.h"
38#include "frame.h"
88987551 39#include "gdb_regex.h"
c906108c
SS
40#include "inferior.h"
41#include "language.h"
42#include "gdbcmd.h"
43
c5aa993b 44#define MAX_PATH_SIZE 1024 /* FIXME: Should be dynamic */
c906108c
SS
45
46/* When handling shared libraries, GDB has to find out the pathnames
47 of all shared libraries that are currently loaded (to read in their
48 symbols) and where the shared libraries are loaded in memory
49 (to relocate them properly from their prelinked addresses to the
50 current load address).
51
52 Under OSF/1 there are two possibilities to get at this information:
53 1) Peek around in the runtime loader structures.
c5aa993b
JM
54 These are not documented, and they are not defined in the system
55 header files. The definitions below were obtained by experimentation,
56 but they seem stable enough.
c906108c 57 2) Use the undocumented libxproc.a library, which contains the
c5aa993b
JM
58 equivalent ldr_* routines.
59 This approach is somewhat cleaner, but it requires that the GDB
60 executable is dynamically linked. In addition it requires a
61 NAT_CLIBS= -lxproc -Wl,-expect_unresolved,ldr_process_context
62 linker specification for GDB and all applications that are using
63 libgdb.
c906108c
SS
64 We will use the peeking approach until it becomes unwieldy. */
65
66#ifndef USE_LDR_ROUTINES
67
68/* Definition of runtime loader structures, found by experimentation. */
69#define RLD_CONTEXT_ADDRESS 0x3ffc0000000
70
71typedef struct
c5aa993b
JM
72 {
73 CORE_ADDR next;
74 CORE_ADDR previous;
75 CORE_ADDR unknown1;
76 char *module_name;
77 CORE_ADDR modinfo_addr;
78 long module_id;
79 CORE_ADDR unknown2;
80 CORE_ADDR unknown3;
81 long region_count;
82 CORE_ADDR regioninfo_addr;
83 }
84ldr_module_info_t;
c906108c
SS
85
86typedef struct
c5aa993b
JM
87 {
88 long unknown1;
89 CORE_ADDR regionname_addr;
90 long protection;
91 CORE_ADDR vaddr;
92 CORE_ADDR mapaddr;
93 long size;
94 long unknown2[5];
95 }
96ldr_region_info_t;
c906108c
SS
97
98typedef struct
c5aa993b
JM
99 {
100 CORE_ADDR unknown1;
101 CORE_ADDR unknown2;
102 CORE_ADDR head;
103 CORE_ADDR tail;
104 }
105ldr_context_t;
c906108c
SS
106
107static ldr_context_t ldr_context;
108
109#else
110
111#include <loader.h>
112static ldr_process_t fake_ldr_process;
113
114/* Called by ldr_* routines to read memory from the current target. */
115
a14ed312 116static int ldr_read_memory (CORE_ADDR, char *, int, int);
c906108c
SS
117
118static int
fba45db2 119ldr_read_memory (CORE_ADDR memaddr, char *myaddr, int len, int readstring)
c906108c
SS
120{
121 int result;
122 char *buffer;
123
124 if (readstring)
125 {
126 target_read_string (memaddr, &buffer, len, &result);
127 if (result == 0)
c5aa993b 128 strcpy (myaddr, buffer);
b8c9b27d 129 xfree (buffer);
c906108c
SS
130 }
131 else
132 result = target_read_memory (memaddr, myaddr, len);
133
134 if (result != 0)
135 result = -result;
136 return result;
137}
138
139#endif
140
141/* Define our own link_map structure.
142 This will help to share code with solib.c. */
143
c5aa993b
JM
144struct link_map
145{
146 CORE_ADDR l_offset; /* prelink to load address offset */
147 char *l_name; /* full name of loaded object */
c906108c
SS
148 ldr_module_info_t module_info; /* corresponding module info */
149};
150
151#define LM_OFFSET(so) ((so) -> lm.l_offset)
152#define LM_NAME(so) ((so) -> lm.l_name)
153
c5aa993b
JM
154struct so_list
155 {
156 struct so_list *next; /* next structure in linked list */
157 struct link_map lm; /* copy of link map from inferior */
158 struct link_map *lmaddr; /* addr in inferior lm was read from */
159 CORE_ADDR lmend; /* upper addr bound of mapped object */
160 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
161 char symbols_loaded; /* flag: symbols read in yet? */
162 char from_tty; /* flag: print msgs? */
163 struct objfile *objfile; /* objfile for loaded lib */
164 struct section_table *sections;
165 struct section_table *sections_end;
166 struct section_table *textsection;
167 bfd *abfd;
168 };
c906108c
SS
169
170static struct so_list *so_list_head; /* List of known shared objects */
171
a14ed312 172extern int fdmatch (int, int); /* In libiberty */
c906108c
SS
173
174/* Local function prototypes */
175
a14ed312 176static void sharedlibrary_command (char *, int);
c906108c 177
a14ed312 178static void info_sharedlibrary_command (char *, int);
c906108c 179
a14ed312 180static int symbol_add_stub (char *);
c906108c 181
a14ed312 182static struct so_list *find_solib (struct so_list *);
c906108c 183
a14ed312 184static struct link_map *first_link_map_member (void);
c906108c 185
a14ed312 186static struct link_map *next_link_map_member (struct so_list *);
c906108c 187
a14ed312 188static void xfer_link_map_member (struct so_list *, struct link_map *);
c906108c 189
a14ed312 190static int solib_map_sections (char *);
c906108c
SS
191
192/*
193
c5aa993b 194 LOCAL FUNCTION
c906108c 195
c5aa993b 196 solib_map_sections -- open bfd and build sections for shared lib
c906108c 197
c5aa993b 198 SYNOPSIS
c906108c 199
c5aa993b 200 static int solib_map_sections (struct so_list *so)
c906108c 201
c5aa993b 202 DESCRIPTION
c906108c 203
c5aa993b
JM
204 Given a pointer to one of the shared objects in our list
205 of mapped objects, use the recorded name to open a bfd
206 descriptor for the object, build a section table, and then
207 relocate all the section addresses by the base address at
208 which the shared object was mapped.
c906108c 209
c5aa993b 210 FIXMES
c906108c 211
c5aa993b
JM
212 In most (all?) cases the shared object file name recorded in the
213 dynamic linkage tables will be a fully qualified pathname. For
214 cases where it isn't, do we really mimic the systems search
215 mechanism correctly in the below code (particularly the tilde
216 expansion stuff?).
c906108c
SS
217 */
218
219static int
fba45db2 220solib_map_sections (char *arg)
c906108c
SS
221{
222 struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */
223 char *filename;
224 char *scratch_pathname;
225 int scratch_chan;
226 struct section_table *p;
227 struct cleanup *old_chain;
228 bfd *abfd;
c5aa993b
JM
229
230 filename = tilde_expand (so->so_name);
b8c9b27d 231 old_chain = make_cleanup (xfree, filename);
c5aa993b 232
c906108c
SS
233 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
234 &scratch_pathname);
235 if (scratch_chan < 0)
236 {
237 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
238 O_RDONLY, 0, &scratch_pathname);
239 }
240 if (scratch_chan < 0)
241 {
242 perror_with_name (filename);
243 }
244 /* Leave scratch_pathname allocated. bfd->name will point to it. */
245
246 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
247 if (!abfd)
248 {
249 close (scratch_chan);
250 error ("Could not open `%s' as an executable file: %s",
251 scratch_pathname, bfd_errmsg (bfd_get_error ()));
252 }
253 /* Leave bfd open, core_xfer_memory and "info files" need it. */
c5aa993b
JM
254 so->abfd = abfd;
255 abfd->cacheable = true;
c906108c
SS
256
257 if (!bfd_check_format (abfd, bfd_object))
258 {
259 error ("\"%s\": not in executable format: %s.",
260 scratch_pathname, bfd_errmsg (bfd_get_error ()));
261 }
c5aa993b 262 if (build_section_table (abfd, &so->sections, &so->sections_end))
c906108c 263 {
c5aa993b 264 error ("Can't find the file sections in `%s': %s",
c906108c
SS
265 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
266 }
267
c5aa993b 268 for (p = so->sections; p < so->sections_end; p++)
c906108c
SS
269 {
270 /* Relocate the section binding addresses as recorded in the shared
c5aa993b
JM
271 object's file by the offset to get the address to which the
272 object was actually mapped. */
273 p->addr += LM_OFFSET (so);
274 p->endaddr += LM_OFFSET (so);
275 so->lmend = (CORE_ADDR) max (p->endaddr, so->lmend);
276 if (STREQ (p->the_bfd_section->name, ".text"))
c906108c 277 {
c5aa993b 278 so->textsection = p;
c906108c
SS
279 }
280 }
281
282 /* Free the file names, close the file now. */
283 do_cleanups (old_chain);
284
285 return (1);
286}
287
288/*
289
c5aa993b 290 LOCAL FUNCTION
c906108c 291
c5aa993b 292 first_link_map_member -- locate first member in dynamic linker's map
c906108c 293
c5aa993b 294 SYNOPSIS
c906108c 295
c5aa993b 296 static struct link_map *first_link_map_member (void)
c906108c 297
c5aa993b 298 DESCRIPTION
c906108c 299
c5aa993b
JM
300 Read in a copy of the first member in the inferior's dynamic
301 link map from the inferior's dynamic linker structures, and return
302 a pointer to the copy in our address space.
303 */
c906108c
SS
304
305static struct link_map *
fba45db2 306first_link_map_member (void)
c906108c
SS
307{
308 struct link_map *lm = NULL;
309 static struct link_map first_lm;
310
311#ifdef USE_LDR_ROUTINES
312 ldr_module_t mod_id = LDR_NULL_MODULE;
313 size_t retsize;
314
315 fake_ldr_process = ldr_core_process ();
316 ldr_set_core_reader (ldr_read_memory);
317 ldr_xdetach (fake_ldr_process);
318 if (ldr_xattach (fake_ldr_process) != 0
c5aa993b 319 || ldr_next_module (fake_ldr_process, &mod_id) != 0
c906108c 320 || mod_id == LDR_NULL_MODULE
c5aa993b
JM
321 || ldr_inq_module (fake_ldr_process, mod_id,
322 &first_lm.module_info, sizeof (ldr_module_info_t),
323 &retsize) != 0)
c906108c
SS
324 return lm;
325#else
326 CORE_ADDR ldr_context_addr;
327
328 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
329 (char *) &ldr_context_addr,
330 sizeof (CORE_ADDR)) != 0
331 || target_read_memory (ldr_context_addr,
332 (char *) &ldr_context,
333 sizeof (ldr_context_t)) != 0
334 || target_read_memory ((CORE_ADDR) ldr_context.head,
335 (char *) &first_lm.module_info,
336 sizeof (ldr_module_info_t)) != 0)
337 return lm;
338#endif
339
340 lm = &first_lm;
341
342 /* The first entry is for the main program and should be skipped. */
343 lm->l_name = NULL;
344
345 return lm;
346}
347
348static struct link_map *
fba45db2 349next_link_map_member (struct so_list *so_list_ptr)
c906108c
SS
350{
351 struct link_map *lm = NULL;
352 static struct link_map next_lm;
353#ifdef USE_LDR_ROUTINES
354 ldr_module_t mod_id = so_list_ptr->lm.module_info.lmi_modid;
355 size_t retsize;
356
c5aa993b 357 if (ldr_next_module (fake_ldr_process, &mod_id) != 0
c906108c 358 || mod_id == LDR_NULL_MODULE
c5aa993b
JM
359 || ldr_inq_module (fake_ldr_process, mod_id,
360 &next_lm.module_info, sizeof (ldr_module_info_t),
361 &retsize) != 0)
c906108c
SS
362 return lm;
363
364 lm = &next_lm;
365 lm->l_name = lm->module_info.lmi_name;
366#else
367 CORE_ADDR ldr_context_addr;
368
369 /* Reread context in case ldr_context.tail was updated. */
370
371 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
372 (char *) &ldr_context_addr,
373 sizeof (CORE_ADDR)) != 0
374 || target_read_memory (ldr_context_addr,
375 (char *) &ldr_context,
376 sizeof (ldr_context_t)) != 0
377 || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
378 || target_read_memory (so_list_ptr->lm.module_info.next,
379 (char *) &next_lm.module_info,
380 sizeof (ldr_module_info_t)) != 0)
381 return lm;
382
383 lm = &next_lm;
384 lm->l_name = lm->module_info.module_name;
385#endif
386 return lm;
387}
388
389static void
fba45db2 390xfer_link_map_member (struct so_list *so_list_ptr, struct link_map *lm)
c906108c
SS
391{
392 int i;
393 so_list_ptr->lm = *lm;
394
395 /* OSF/1 shared libraries are pre-linked to particular addresses,
396 but the runtime loader may have to relocate them if the
397 address ranges of the libraries used by the target executable clash,
398 or if the target executable is linked with the -taso option.
399 The offset is the difference between the address where the shared
400 library is mapped and the pre-linked address of the shared library.
401
402 FIXME: GDB is currently unable to relocate the shared library
403 sections by different offsets. If sections are relocated by
404 different offsets, put out a warning and use the offset of the
405 first section for all remaining sections. */
406 LM_OFFSET (so_list_ptr) = 0;
407
408 /* There is one entry that has no name (for the inferior executable)
409 since it is not a shared object. */
410 if (LM_NAME (so_list_ptr) != 0)
411 {
412
413#ifdef USE_LDR_ROUTINES
414 int len = strlen (LM_NAME (so_list_ptr) + 1);
415
416 if (len > MAX_PATH_SIZE)
417 len = MAX_PATH_SIZE;
418 strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
419 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
420
421 for (i = 0; i < lm->module_info.lmi_nregion; i++)
422 {
423 ldr_region_info_t region_info;
424 size_t retsize;
425 CORE_ADDR region_offset;
426
427 if (ldr_inq_region (fake_ldr_process, lm->module_info.lmi_modid,
428 i, &region_info, sizeof (region_info),
429 &retsize) != 0)
430 break;
431 region_offset = (CORE_ADDR) region_info.lri_mapaddr
c5aa993b 432 - (CORE_ADDR) region_info.lri_vaddr;
c906108c
SS
433 if (i == 0)
434 LM_OFFSET (so_list_ptr) = region_offset;
435 else if (LM_OFFSET (so_list_ptr) != region_offset)
436 warning ("cannot handle shared library relocation for %s (%s)",
437 so_list_ptr->so_name, region_info.lri_name);
438 }
439#else
440 int errcode;
441 char *buffer;
442 target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
443 MAX_PATH_SIZE - 1, &errcode);
444 if (errcode != 0)
445 error ("xfer_link_map_member: Can't read pathname for load map: %s\n",
446 safe_strerror (errcode));
447 strncpy (so_list_ptr->so_name, buffer, MAX_PATH_SIZE - 1);
b8c9b27d 448 xfree (buffer);
c906108c
SS
449 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
450
451 for (i = 0; i < lm->module_info.region_count; i++)
452 {
453 ldr_region_info_t region_info;
454 CORE_ADDR region_offset;
455
456 if (target_read_memory (lm->module_info.regioninfo_addr
c5aa993b 457 + i * sizeof (region_info),
c906108c
SS
458 (char *) &region_info,
459 sizeof (region_info)) != 0)
460 break;
461 region_offset = region_info.mapaddr - region_info.vaddr;
462 if (i == 0)
463 LM_OFFSET (so_list_ptr) = region_offset;
464 else if (LM_OFFSET (so_list_ptr) != region_offset)
465 {
466 char *region_name;
467 target_read_string (region_info.regionname_addr, &buffer,
468 MAX_PATH_SIZE - 1, &errcode);
469 if (errcode == 0)
470 region_name = buffer;
471 else
472 region_name = "??";
473 warning ("cannot handle shared library relocation for %s (%s)",
c5aa993b 474 so_list_ptr->so_name, region_name);
b8c9b27d 475 xfree (buffer);
c906108c
SS
476 }
477 }
478#endif
479
480 catch_errors (solib_map_sections, (char *) so_list_ptr,
481 "Error while mapping shared library sections:\n",
482 RETURN_MASK_ALL);
483 }
484}
485
486/*
487
c5aa993b 488 LOCAL FUNCTION
c906108c 489
c5aa993b 490 find_solib -- step through list of shared objects
c906108c 491
c5aa993b 492 SYNOPSIS
c906108c 493
c5aa993b 494 struct so_list *find_solib (struct so_list *so_list_ptr)
c906108c 495
c5aa993b 496 DESCRIPTION
c906108c 497
c5aa993b
JM
498 This module contains the routine which finds the names of any
499 loaded "images" in the current process. The argument in must be
500 NULL on the first call, and then the returned value must be passed
501 in on subsequent calls. This provides the capability to "step" down
502 the list of loaded objects. On the last object, a NULL value is
503 returned.
c906108c 504
c5aa993b
JM
505 The arg and return value are "struct link_map" pointers, as defined
506 in <link.h>.
c906108c
SS
507 */
508
509static struct so_list *
fa6b9313 510find_solib (struct so_list *so_list_ptr)
c906108c
SS
511{
512 struct so_list *so_list_next = NULL;
513 struct link_map *lm = NULL;
514 struct so_list *new;
c5aa993b 515
c906108c
SS
516 if (so_list_ptr == NULL)
517 {
518 /* We are setting up for a new scan through the loaded images. */
519 if ((so_list_next = so_list_head) == NULL)
520 {
521 /* Find the first link map list member. */
522 lm = first_link_map_member ();
523 }
524 }
525 else
526 {
527 /* We have been called before, and are in the process of walking
c5aa993b 528 the shared library list. Advance to the next shared object. */
c906108c 529 lm = next_link_map_member (so_list_ptr);
c5aa993b 530 so_list_next = so_list_ptr->next;
c906108c
SS
531 }
532 if ((so_list_next == NULL) && (lm != NULL))
533 {
534 /* Get next link map structure from inferior image and build a local
c5aa993b 535 abbreviated load_map structure */
c906108c
SS
536 new = (struct so_list *) xmalloc (sizeof (struct so_list));
537 memset ((char *) new, 0, sizeof (struct so_list));
c5aa993b 538 new->lmaddr = lm;
c906108c 539 /* Add the new node as the next node in the list, or as the root
c5aa993b 540 node if this is the first one. */
c906108c
SS
541 if (so_list_ptr != NULL)
542 {
c5aa993b 543 so_list_ptr->next = new;
c906108c
SS
544 }
545 else
546 {
547 so_list_head = new;
c5aa993b 548 }
c906108c
SS
549 so_list_next = new;
550 xfer_link_map_member (new, lm);
551 }
552 return (so_list_next);
553}
554
555/* A small stub to get us past the arg-passing pinhole of catch_errors. */
556
557static int
fba45db2 558symbol_add_stub (char *arg)
c906108c 559{
c5aa993b 560 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
c906108c 561 CORE_ADDR text_addr = 0;
2acceee2 562 struct section_addr_info section_addrs;
c906108c 563
2acceee2 564 memset (&section_addrs, 0, sizeof (section_addrs));
c5aa993b
JM
565 if (so->textsection)
566 text_addr = so->textsection->addr;
567 else if (so->abfd != NULL)
c906108c
SS
568 {
569 asection *lowest_sect;
570
571 /* If we didn't find a mapped non zero sized .text section, set up
c5aa993b 572 text_addr so that the relocation in symbol_file_add does no harm. */
c906108c 573
c5aa993b 574 lowest_sect = bfd_get_section_by_name (so->abfd, ".text");
c906108c 575 if (lowest_sect == NULL)
c5aa993b 576 bfd_map_over_sections (so->abfd, find_lowest_section,
96baa820 577 (PTR) &lowest_sect);
c906108c 578 if (lowest_sect)
c5aa993b 579 text_addr = bfd_section_vma (so->abfd, lowest_sect) + LM_OFFSET (so);
c906108c 580 }
c5aa993b 581
a034fba4
EZ
582 section_addrs.other[0].addr = text_addr;
583 section_addrs.other[0].name = ".text";
c5aa993b 584 so->objfile = symbol_file_add (so->so_name, so->from_tty,
2df3850c 585 &section_addrs, 0, OBJF_SHARED);
c906108c
SS
586 return (1);
587}
588
589/*
590
c5aa993b 591 GLOBAL FUNCTION
c906108c 592
c5aa993b 593 solib_add -- add a shared library file to the symtab and section list
c906108c 594
c5aa993b 595 SYNOPSIS
c906108c 596
c5aa993b
JM
597 void solib_add (char *arg_string, int from_tty,
598 struct target_ops *target)
c906108c 599
c5aa993b 600 DESCRIPTION
c906108c 601
c5aa993b 602 */
c906108c
SS
603
604void
fba45db2 605solib_add (char *arg_string, int from_tty, struct target_ops *target)
c5aa993b
JM
606{
607 register struct so_list *so = NULL; /* link map state variable */
c906108c
SS
608
609 /* Last shared library that we read. */
610 struct so_list *so_last = NULL;
611
612 char *re_err;
613 int count;
614 int old;
c5aa993b 615
c906108c
SS
616 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
617 {
618 error ("Invalid regexp: %s", re_err);
619 }
c5aa993b
JM
620
621
c906108c
SS
622 /* Add the shared library sections to the section table of the
623 specified target, if any. */
624 if (target)
625 {
626 /* Count how many new section_table entries there are. */
627 so = NULL;
628 count = 0;
629 while ((so = find_solib (so)) != NULL)
630 {
c5aa993b 631 if (so->so_name[0])
c906108c 632 {
c5aa993b 633 count += so->sections_end - so->sections;
c906108c
SS
634 }
635 }
c5aa993b 636
c906108c
SS
637 if (count)
638 {
c906108c 639 /* Add these section table entries to the target's table. */
6426a772
JM
640
641 old = target_resize_to_sections (target, count);
642
c906108c
SS
643 while ((so = find_solib (so)) != NULL)
644 {
c5aa993b 645 if (so->so_name[0])
c906108c 646 {
c5aa993b
JM
647 count = so->sections_end - so->sections;
648 memcpy ((char *) (target->to_sections + old),
649 so->sections,
c906108c
SS
650 (sizeof (struct section_table)) * count);
651 old += count;
652 }
653 }
654 }
655 }
c5aa993b 656
c906108c
SS
657 /* Now add the symbol files. */
658 so = NULL;
659 while ((so = find_solib (so)) != NULL)
660 {
c5aa993b 661 if (so->so_name[0] && re_exec (so->so_name))
c906108c 662 {
c5aa993b
JM
663 so->from_tty = from_tty;
664 if (so->symbols_loaded)
c906108c
SS
665 {
666 if (from_tty)
667 {
c5aa993b 668 printf_unfiltered ("Symbols already loaded for %s\n", so->so_name);
c906108c
SS
669 }
670 }
671 else if (catch_errors
672 (symbol_add_stub, (char *) so,
673 "Error while reading shared library symbols:\n",
674 RETURN_MASK_ALL))
675 {
676 so_last = so;
c5aa993b 677 so->symbols_loaded = 1;
c906108c
SS
678 }
679 }
680 }
681
682 /* Getting new symbols may change our opinion about what is
683 frameless. */
684 if (so_last)
685 reinit_frame_cache ();
686}
687
688/*
689
c5aa993b 690 LOCAL FUNCTION
c906108c 691
c5aa993b 692 info_sharedlibrary_command -- code for "info sharedlibrary"
c906108c 693
c5aa993b 694 SYNOPSIS
c906108c 695
c5aa993b 696 static void info_sharedlibrary_command ()
c906108c 697
c5aa993b 698 DESCRIPTION
c906108c 699
c5aa993b
JM
700 Walk through the shared library list and print information
701 about each attached library.
702 */
c906108c
SS
703
704static void
fba45db2 705info_sharedlibrary_command (char *ignore, int from_tty)
c906108c 706{
c5aa993b 707 register struct so_list *so = NULL; /* link map state variable */
c906108c 708 int header_done = 0;
c5aa993b 709
c906108c
SS
710 if (exec_bfd == NULL)
711 {
4ce44c66 712 printf_unfiltered ("No executable file.\n");
c906108c
SS
713 return;
714 }
715 while ((so = find_solib (so)) != NULL)
716 {
c5aa993b 717 if (so->so_name[0])
c906108c
SS
718 {
719 unsigned long txt_start = 0;
720 unsigned long txt_end = 0;
721
722 if (!header_done)
723 {
c5aa993b
JM
724 printf_unfiltered ("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
725 "Shared Object Library");
c906108c
SS
726 header_done++;
727 }
c5aa993b 728 if (so->textsection)
c906108c 729 {
c5aa993b
JM
730 txt_start = (unsigned long) so->textsection->addr;
731 txt_end = (unsigned long) so->textsection->endaddr;
c906108c
SS
732 }
733 printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
734 printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
c5aa993b
JM
735 printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No");
736 printf_unfiltered ("%s\n", so->so_name);
c906108c
SS
737 }
738 }
739 if (so_list_head == NULL)
740 {
c5aa993b 741 printf_unfiltered ("No shared libraries loaded at this time.\n");
c906108c
SS
742 }
743}
744
745/*
746
c5aa993b 747 GLOBAL FUNCTION
c906108c 748
c5aa993b 749 solib_address -- check to see if an address is in a shared lib
c906108c 750
c5aa993b 751 SYNOPSIS
c906108c 752
c5aa993b 753 char *solib_address (CORE_ADDR address)
c906108c 754
c5aa993b 755 DESCRIPTION
c906108c 756
c5aa993b
JM
757 Provides a hook for other gdb routines to discover whether or
758 not a particular address is within the mapped address space of
759 a shared library. Any address between the base mapping address
760 and the first address beyond the end of the last mapping, is
761 considered to be within the shared library address space, for
762 our purposes.
c906108c 763
c5aa993b
JM
764 For example, this routine is called at one point to disable
765 breakpoints which are in shared libraries that are not currently
766 mapped in.
c906108c
SS
767 */
768
769char *
fba45db2 770solib_address (CORE_ADDR address)
c906108c 771{
c5aa993b
JM
772 register struct so_list *so = 0; /* link map state variable */
773
c906108c
SS
774 while ((so = find_solib (so)) != NULL)
775 {
c5aa993b 776 if (so->so_name[0] && so->textsection)
c906108c 777 {
c5aa993b
JM
778 if ((address >= (CORE_ADDR) so->textsection->addr) &&
779 (address < (CORE_ADDR) so->textsection->endaddr))
c906108c
SS
780 return (so->so_name);
781 }
782 }
783 return (0);
784}
785
786/* Called by free_all_symtabs */
787
c5aa993b 788void
fba45db2 789clear_solib (void)
c906108c
SS
790{
791 struct so_list *next;
792 char *bfd_filename;
c5aa993b 793
c906108c
SS
794 disable_breakpoints_in_shlibs (1);
795
796 while (so_list_head)
797 {
c5aa993b 798 if (so_list_head->sections)
c906108c 799 {
b8c9b27d 800 xfree (so_list_head->sections);
c906108c 801 }
c5aa993b 802 if (so_list_head->abfd)
c906108c 803 {
c5aa993b
JM
804 bfd_filename = bfd_get_filename (so_list_head->abfd);
805 if (!bfd_close (so_list_head->abfd))
c906108c
SS
806 warning ("cannot close \"%s\": %s",
807 bfd_filename, bfd_errmsg (bfd_get_error ()));
808 }
809 else
810 /* This happens for the executable on SVR4. */
811 bfd_filename = NULL;
812
c5aa993b 813 next = so_list_head->next;
c906108c 814 if (bfd_filename)
b8c9b27d
KB
815 xfree (bfd_filename);
816 xfree (so_list_head);
c906108c
SS
817 so_list_head = next;
818 }
819}
c5aa993b 820
c906108c 821/*
c5aa993b
JM
822
823 GLOBAL FUNCTION
824
825 solib_create_inferior_hook -- shared library startup support
826
827 SYNOPSIS
828
829 void solib_create_inferior_hook()
830
831 DESCRIPTION
832
833 When gdb starts up the inferior, it nurses it along (through the
834 shell) until it is ready to execute it's first instruction. At this
835 point, this function gets called via expansion of the macro
836 SOLIB_CREATE_INFERIOR_HOOK.
837 For a statically bound executable, this first instruction is the
838 one at "_start", or a similar text label. No further processing is
839 needed in that case.
840 For a dynamically bound executable, this first instruction is somewhere
841 in the rld, and the actual user executable is not yet mapped in.
842 We continue the inferior again, rld then maps in the actual user
843 executable and any needed shared libraries and then sends
844 itself a SIGTRAP.
845 At that point we discover the names of all shared libraries and
846 read their symbols in.
847
848 FIXME
849
850 This code does not properly handle hitting breakpoints which the
851 user might have set in the rld itself. Proper handling would have
852 to check if the SIGTRAP happened due to a kill call.
853
854 Also, what if child has exit()ed? Must exit loop somehow.
855 */
c906108c
SS
856
857void
fba45db2 858solib_create_inferior_hook (void)
c906108c
SS
859{
860
861 /* Nothing to do for statically bound executables. */
862
863 if (symfile_objfile == NULL
864 || symfile_objfile->obfd == NULL
865 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
866 return;
867
868 /* Now run the target. It will eventually get a SIGTRAP, at
869 which point all of the libraries will have been mapped in and we
870 can go groveling around in the rld structures to find
871 out what we need to know about them. */
c5aa993b 872
c906108c
SS
873 clear_proceed_status ();
874 stop_soon_quietly = 1;
875 stop_signal = TARGET_SIGNAL_0;
876 do
877 {
878 target_resume (-1, 0, stop_signal);
879 wait_for_inferior ();
880 }
881 while (stop_signal != TARGET_SIGNAL_TRAP);
882
883 /* solib_add will call reinit_frame_cache.
c5aa993b
JM
884 But we are stopped in the runtime loader and we do not have symbols
885 for the runtime loader. So heuristic_proc_start will be called
886 and will put out an annoying warning.
887 Delaying the resetting of stop_soon_quietly until after symbol loading
888 suppresses the warning. */
c906108c
SS
889 if (auto_solib_add)
890 solib_add ((char *) 0, 0, (struct target_ops *) 0);
891 stop_soon_quietly = 0;
892}
893
894
895/*
896
c5aa993b 897 LOCAL FUNCTION
c906108c 898
c5aa993b 899 sharedlibrary_command -- handle command to explicitly add library
c906108c 900
c5aa993b 901 SYNOPSIS
c906108c 902
c5aa993b 903 static void sharedlibrary_command (char *args, int from_tty)
c906108c 904
c5aa993b 905 DESCRIPTION
c906108c 906
c5aa993b 907 */
c906108c
SS
908
909static void
fba45db2 910sharedlibrary_command (char *args, int from_tty)
c906108c
SS
911{
912 dont_repeat ();
913 solib_add (args, from_tty, (struct target_ops *) 0);
914}
915
916void
fba45db2 917_initialize_solib (void)
c906108c
SS
918{
919 add_com ("sharedlibrary", class_files, sharedlibrary_command,
920 "Load shared object library symbols for files matching REGEXP.");
c5aa993b 921 add_info ("sharedlibrary", info_sharedlibrary_command,
c906108c
SS
922 "Status of loaded shared object libraries.");
923
924 add_show_from_set
925 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
926 (char *) &auto_solib_add,
927 "Set autoloading of shared library symbols.\n\
928If nonzero, symbols from all shared object libraries will be loaded\n\
929automatically when the inferior begins execution or when the dynamic linker\n\
930informs gdb that a new library has been loaded. Otherwise, symbols\n\
931must be loaded manually, using `sharedlibrary'.",
932 &setlist),
933 &showlist);
934}
This page took 0.134561 seconds and 4 git commands to generate.