*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / solib.c
CommitLineData
13437d4b 1/* Handle shared libraries for GDB, the GNU Debugger.
14a5e767 2
6aba47ca
DJ
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2005, 2006, 2007 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
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 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.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 20
c906108c
SS
21#include "defs.h"
22
c906108c 23#include <sys/types.h>
c906108c 24#include <fcntl.h>
13437d4b 25#include "gdb_string.h"
c906108c
SS
26#include "symtab.h"
27#include "bfd.h"
28#include "symfile.h"
29#include "objfiles.h"
60250e8b 30#include "exceptions.h"
c906108c
SS
31#include "gdbcore.h"
32#include "command.h"
33#include "target.h"
34#include "frame.h"
88987551 35#include "gdb_regex.h"
c906108c
SS
36#include "inferior.h"
37#include "environ.h"
38#include "language.h"
39#include "gdbcmd.h"
fa58ee11 40#include "completer.h"
fe4e3eb8 41#include "filenames.h" /* for DOSish file names */
4646aa9d 42#include "exec.h"
13437d4b 43#include "solist.h"
84acb35a 44#include "observer.h"
dbda9972 45#include "readline/readline.h"
c906108c 46
66aba65d
MK
47/* Architecture-specific operations. */
48
49/* Per-architecture data key. */
50static struct gdbarch_data *solib_data;
51
52static void *
53solib_init (struct obstack *obstack)
54{
55 struct target_so_ops **ops;
56
57 ops = OBSTACK_ZALLOC (obstack, struct target_so_ops *);
58 *ops = current_target_so_ops;
59 return ops;
60}
61
62static struct target_so_ops *
63solib_ops (struct gdbarch *gdbarch)
64{
65 struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
66 return *ops;
67}
7d522c90
DJ
68
69/* Set the solib operations for GDBARCH to NEW_OPS. */
70
71void
72set_solib_ops (struct gdbarch *gdbarch, struct target_so_ops *new_ops)
73{
74 struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
75 *ops = new_ops;
76}
66aba65d
MK
77\f
78
13437d4b 79/* external data declarations */
c906108c 80
7d522c90
DJ
81/* FIXME: gdbarch needs to control this variable, or else every
82 configuration needs to call set_solib_ops. */
13437d4b 83struct target_so_ops *current_target_so_ops;
23e04971
MS
84
85/* local data declarations */
07cd4b97 86
c906108c 87static struct so_list *so_list_head; /* List of known shared objects */
23e04971 88
c906108c
SS
89/* Local function prototypes */
90
c906108c
SS
91/* If non-empty, this is a search path for loading non-absolute shared library
92 symbol files. This takes precedence over the environment variables PATH
93 and LD_LIBRARY_PATH. */
94static char *solib_search_path = NULL;
920d2a44
AC
95static void
96show_solib_search_path (struct ui_file *file, int from_tty,
97 struct cmd_list_element *c, const char *value)
98{
99 fprintf_filtered (file, _("\
100The search path for loading non-absolute shared library symbol files is %s.\n"),
101 value);
102}
c906108c 103
e4f7b8c8
MS
104/*
105
106 GLOBAL FUNCTION
107
108 solib_open -- Find a shared library file and open it.
109
110 SYNOPSIS
111
112 int solib_open (char *in_patname, char **found_pathname);
113
114 DESCRIPTION
115
f822c95b 116 Global variable GDB_SYSROOT is used as a prefix directory
e4f7b8c8
MS
117 to search for shared libraries if they have an absolute path.
118
119 Global variable SOLIB_SEARCH_PATH is used as a prefix directory
120 (or set of directories, as in LD_LIBRARY_PATH) to search for all
f822c95b 121 shared libraries if not found in GDB_SYSROOT.
e4f7b8c8 122
c8c18e65 123 Search algorithm:
f822c95b
DJ
124 * If there is a gdb_sysroot and path is absolute:
125 * Search for gdb_sysroot/path.
c8c18e65
KW
126 * else
127 * Look for it literally (unmodified).
e4f7b8c8 128 * Look in SOLIB_SEARCH_PATH.
f43caff8 129 * If available, use target defined search function.
f822c95b 130 * If gdb_sysroot is NOT set, perform the following two searches:
c8c18e65
KW
131 * Look in inferior's $PATH.
132 * Look in inferior's $LD_LIBRARY_PATH.
133 *
134 * The last check avoids doing this search when targetting remote
f822c95b 135 * machines since gdb_sysroot will almost always be set.
e4f7b8c8
MS
136
137 RETURNS
b21f0843 138
e4f7b8c8
MS
139 file handle for opened solib, or -1 for failure. */
140
141int
142solib_open (char *in_pathname, char **found_pathname)
143{
66aba65d 144 struct target_so_ops *ops = solib_ops (current_gdbarch);
e4f7b8c8
MS
145 int found_file = -1;
146 char *temp_pathname = NULL;
fe4e3eb8 147 char *p = in_pathname;
f822c95b 148 int gdb_sysroot_is_empty;
58dc52c3 149
f822c95b 150 gdb_sysroot_is_empty = (gdb_sysroot == NULL || *gdb_sysroot == 0);
e4f7b8c8 151
f822c95b 152 if (! IS_ABSOLUTE_PATH (in_pathname) || gdb_sysroot_is_empty)
f1d10cfb
AS
153 temp_pathname = in_pathname;
154 else
e4f7b8c8 155 {
f822c95b 156 int prefix_len = strlen (gdb_sysroot);
f1d10cfb
AS
157
158 /* Remove trailing slashes from absolute prefix. */
159 while (prefix_len > 0
f822c95b 160 && IS_DIR_SEPARATOR (gdb_sysroot[prefix_len - 1]))
f1d10cfb
AS
161 prefix_len--;
162
163 /* Cat the prefixed pathname together. */
164 temp_pathname = alloca (prefix_len + strlen (in_pathname) + 1);
f822c95b 165 strncpy (temp_pathname, gdb_sysroot, prefix_len);
f1d10cfb
AS
166 temp_pathname[prefix_len] = '\0';
167 strcat (temp_pathname, in_pathname);
e4f7b8c8
MS
168 }
169
f1d10cfb
AS
170 /* Now see if we can open it. */
171 found_file = open (temp_pathname, O_RDONLY | O_BINARY, 0);
172
0997b535
MS
173 /* We try to find the library in various ways. After each attempt
174 (except for the one above), either found_file >= 0 and
175 temp_pathname is a malloc'd string, or found_file < 0 and
176 temp_pathname does not point to storage that needs to be
177 freed. */
178
179 if (found_file < 0)
180 temp_pathname = NULL;
181 else
182 temp_pathname = xstrdup (temp_pathname);
183
f822c95b 184 /* If the search in gdb_sysroot failed, and the path name is
ba5f0d88
OF
185 absolute at this point, make it relative. (openp will try and open the
186 file according to its absolute path otherwise, which is not what we want.)
187 Affects subsequent searches for this solib. */
188 if (found_file < 0 && IS_ABSOLUTE_PATH (in_pathname))
189 {
190 /* First, get rid of any drive letters etc. */
191 while (!IS_DIR_SEPARATOR (*in_pathname))
192 in_pathname++;
193
194 /* Next, get rid of all leading dir separators. */
195 while (IS_DIR_SEPARATOR (*in_pathname))
196 in_pathname++;
197 }
198
c8c18e65 199 /* If not found, search the solib_search_path (if any). */
e4f7b8c8 200 if (found_file < 0 && solib_search_path != NULL)
014d698b 201 found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
637d6690 202 in_pathname, O_RDONLY | O_BINARY, 0, &temp_pathname);
ba5f0d88
OF
203
204 /* If not found, next search the solib_search_path (if any) for the basename
205 only (ignoring the path). This is to allow reading solibs from a path
206 that differs from the opened path. */
207 if (found_file < 0 && solib_search_path != NULL)
014d698b 208 found_file = openp (solib_search_path, OPF_TRY_CWD_FIRST,
637d6690 209 lbasename (in_pathname), O_RDONLY | O_BINARY, 0,
ba5f0d88 210 &temp_pathname);
e4f7b8c8 211
2610b0bf 212 /* If not found, try to use target supplied solib search method */
66aba65d 213 if (found_file < 0 && ops->find_and_open_solib)
637d6690 214 found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
66aba65d 215 &temp_pathname);
2610b0bf 216
e4f7b8c8 217 /* If not found, next search the inferior's $PATH environment variable. */
f822c95b 218 if (found_file < 0 && gdb_sysroot_is_empty)
e4f7b8c8 219 found_file = openp (get_in_environ (inferior_environ, "PATH"),
637d6690 220 OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY, 0,
014d698b 221 &temp_pathname);
e4f7b8c8
MS
222
223 /* If not found, next search the inferior's $LD_LIBRARY_PATH
224 environment variable. */
f822c95b 225 if (found_file < 0 && gdb_sysroot_is_empty)
e4f7b8c8 226 found_file = openp (get_in_environ (inferior_environ, "LD_LIBRARY_PATH"),
637d6690 227 OPF_TRY_CWD_FIRST, in_pathname, O_RDONLY | O_BINARY, 0,
014d698b 228 &temp_pathname);
e4f7b8c8
MS
229
230 /* Done. If not found, tough luck. Return found_file and
231 (optionally) found_pathname. */
0997b535
MS
232 if (temp_pathname)
233 {
234 if (found_pathname != NULL)
235 *found_pathname = temp_pathname;
236 else
237 xfree (temp_pathname);
238 }
e4f7b8c8
MS
239 return found_file;
240}
241
242
c906108c
SS
243/*
244
c5aa993b 245 LOCAL FUNCTION
c906108c 246
c5aa993b 247 solib_map_sections -- open bfd and build sections for shared lib
c906108c 248
c5aa993b 249 SYNOPSIS
c906108c 250
c5aa993b 251 static int solib_map_sections (struct so_list *so)
c906108c 252
c5aa993b 253 DESCRIPTION
c906108c 254
c5aa993b
JM
255 Given a pointer to one of the shared objects in our list
256 of mapped objects, use the recorded name to open a bfd
257 descriptor for the object, build a section table, and then
258 relocate all the section addresses by the base address at
259 which the shared object was mapped.
c906108c 260
c5aa993b 261 FIXMES
c906108c 262
c5aa993b
JM
263 In most (all?) cases the shared object file name recorded in the
264 dynamic linkage tables will be a fully qualified pathname. For
265 cases where it isn't, do we really mimic the systems search
266 mechanism correctly in the below code (particularly the tilde
267 expansion stuff?).
c906108c
SS
268 */
269
270static int
4efb68b1 271solib_map_sections (void *arg)
c906108c
SS
272{
273 struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */
274 char *filename;
275 char *scratch_pathname;
276 int scratch_chan;
277 struct section_table *p;
278 struct cleanup *old_chain;
279 bfd *abfd;
c5aa993b
JM
280
281 filename = tilde_expand (so->so_name);
282
b8c9b27d 283 old_chain = make_cleanup (xfree, filename);
e4f7b8c8 284 scratch_chan = solib_open (filename, &scratch_pathname);
c906108c 285
c906108c
SS
286 if (scratch_chan < 0)
287 {
13437d4b
KB
288 perror_with_name (filename);
289 }
104c1213 290
e4f7b8c8 291 /* Leave scratch_pathname allocated. abfd->name will point to it. */
9f76c2cd 292 abfd = bfd_fopen (scratch_pathname, gnutarget, FOPEN_RB, scratch_chan);
13437d4b 293 if (!abfd)
23e04971 294 {
13437d4b 295 close (scratch_chan);
8a3fe4f8 296 error (_("Could not open `%s' as an executable file: %s"),
13437d4b
KB
297 scratch_pathname, bfd_errmsg (bfd_get_error ()));
298 }
e4f7b8c8 299
13437d4b
KB
300 /* Leave bfd open, core_xfer_memory and "info files" need it. */
301 so->abfd = abfd;
549c1eea 302 bfd_set_cacheable (abfd, 1);
23e04971 303
e4f7b8c8
MS
304 /* copy full path name into so_name, so that later symbol_file_add
305 can find it */
13437d4b 306 if (strlen (scratch_pathname) >= SO_NAME_MAX_PATH_SIZE)
8a3fe4f8 307 error (_("Full path name length of shared library exceeds SO_NAME_MAX_PATH_SIZE in so_list structure."));
13437d4b 308 strcpy (so->so_name, scratch_pathname);
23e04971 309
13437d4b
KB
310 if (!bfd_check_format (abfd, bfd_object))
311 {
8a3fe4f8 312 error (_("\"%s\": not in executable format: %s."),
13437d4b 313 scratch_pathname, bfd_errmsg (bfd_get_error ()));
23e04971 314 }
13437d4b 315 if (build_section_table (abfd, &so->sections, &so->sections_end))
23e04971 316 {
8a3fe4f8 317 error (_("Can't find the file sections in `%s': %s"),
13437d4b 318 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
23e04971 319 }
104c1213 320
13437d4b 321 for (p = so->sections; p < so->sections_end; p++)
104c1213 322 {
66aba65d
MK
323 struct target_so_ops *ops = solib_ops (current_gdbarch);
324
13437d4b
KB
325 /* Relocate the section binding addresses as recorded in the shared
326 object's file by the base address to which the object was actually
327 mapped. */
66aba65d 328 ops->relocate_section_addresses (so, p);
cfa9d6d9
DJ
329
330 /* If the target didn't provide information about the address
331 range of the shared object, assume we want the location of
332 the .text section. */
333 if (so->addr_low == 0 && so->addr_high == 0
334 && strcmp (p->the_bfd_section->name, ".text") == 0)
13437d4b 335 {
cfa9d6d9
DJ
336 so->addr_low = p->addr;
337 so->addr_high = p->endaddr;
13437d4b 338 }
104c1213
JM
339 }
340
13437d4b
KB
341 /* Free the file names, close the file now. */
342 do_cleanups (old_chain);
104c1213 343
13437d4b 344 return (1);
104c1213 345}
c906108c 346
07cd4b97 347/* LOCAL FUNCTION
c906108c 348
07cd4b97 349 free_so --- free a `struct so_list' object
c906108c 350
c5aa993b 351 SYNOPSIS
c906108c 352
07cd4b97 353 void free_so (struct so_list *so)
c906108c 354
c5aa993b 355 DESCRIPTION
c906108c 356
07cd4b97
JB
357 Free the storage associated with the `struct so_list' object SO.
358 If we have opened a BFD for SO, close it.
c906108c 359
07cd4b97
JB
360 The caller is responsible for removing SO from whatever list it is
361 a member of. If we have placed SO's sections in some target's
362 section table, the caller is responsible for removing them.
c906108c 363
07cd4b97
JB
364 This function doesn't mess with objfiles at all. If there is an
365 objfile associated with SO that needs to be removed, the caller is
366 responsible for taking care of that. */
367
13437d4b 368void
07cd4b97 369free_so (struct so_list *so)
c906108c 370{
66aba65d 371 struct target_so_ops *ops = solib_ops (current_gdbarch);
07cd4b97 372 char *bfd_filename = 0;
c5aa993b 373
07cd4b97 374 if (so->sections)
b8c9b27d 375 xfree (so->sections);
07cd4b97
JB
376
377 if (so->abfd)
c906108c 378 {
07cd4b97
JB
379 bfd_filename = bfd_get_filename (so->abfd);
380 if (! bfd_close (so->abfd))
8a3fe4f8 381 warning (_("cannot close \"%s\": %s"),
07cd4b97 382 bfd_filename, bfd_errmsg (bfd_get_error ()));
c906108c 383 }
07cd4b97
JB
384
385 if (bfd_filename)
b8c9b27d 386 xfree (bfd_filename);
07cd4b97 387
66aba65d 388 ops->free_so (so);
07cd4b97 389
b8c9b27d 390 xfree (so);
c906108c
SS
391}
392
07cd4b97 393
f8766ec1
KB
394/* Return address of first so_list entry in master shared object list. */
395struct so_list *
396master_so_list (void)
397{
398 return so_list_head;
399}
400
401
c906108c
SS
402/* A small stub to get us past the arg-passing pinhole of catch_errors. */
403
404static int
4efb68b1 405symbol_add_stub (void *arg)
c906108c 406{
52f0bd74 407 struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
62557bbc 408 struct section_addr_info *sap;
c906108c 409
07cd4b97
JB
410 /* Have we already loaded this shared object? */
411 ALL_OBJFILES (so->objfile)
412 {
413 if (strcmp (so->objfile->name, so->so_name) == 0)
414 return 1;
415 }
416
62557bbc
KB
417 sap = build_section_addr_info_from_section_table (so->sections,
418 so->sections_end);
e7cf9df1 419
62557bbc
KB
420 so->objfile = symbol_file_add (so->so_name, so->from_tty,
421 sap, 0, OBJF_SHARED);
422 free_section_addr_info (sap);
c906108c 423
07cd4b97 424 return (1);
c906108c
SS
425}
426
42a6e6a0
MK
427/* Read in symbols for shared object SO. If FROM_TTY is non-zero, be
428 chatty about it. Return non-zero if any symbols were actually
429 loaded. */
430
431int
432solib_read_symbols (struct so_list *so, int from_tty)
433{
434 if (so->symbols_loaded)
435 {
436 if (from_tty)
a3f17187 437 printf_unfiltered (_("Symbols already loaded for %s\n"), so->so_name);
42a6e6a0 438 }
8bb75286
DJ
439 else if (so->abfd == NULL)
440 {
441 if (from_tty)
442 printf_unfiltered (_("Symbol file not found for %s\n"), so->so_name);
443 }
42a6e6a0
MK
444 else
445 {
446 if (catch_errors (symbol_add_stub, so,
447 "Error while reading shared library symbols:\n",
448 RETURN_MASK_ALL))
449 {
450 if (from_tty)
a3f17187 451 printf_unfiltered (_("Loaded symbols for %s\n"), so->so_name);
42a6e6a0
MK
452 so->symbols_loaded = 1;
453 return 1;
454 }
455 }
456
457 return 0;
458}
c906108c 459
07cd4b97 460/* LOCAL FUNCTION
c906108c 461
105b175f 462 update_solib_list --- synchronize GDB's shared object list with inferior's
c906108c 463
c5aa993b 464 SYNOPSIS
c906108c 465
105b175f 466 void update_solib_list (int from_tty, struct target_ops *TARGET)
c906108c 467
07cd4b97 468 Extract the list of currently loaded shared objects from the
105b175f
JB
469 inferior, and compare it with the list of shared objects currently
470 in GDB's so_list_head list. Edit so_list_head to bring it in sync
471 with the inferior's new list.
c906108c 472
105b175f
JB
473 If we notice that the inferior has unloaded some shared objects,
474 free any symbolic info GDB had read about those shared objects.
475
476 Don't load symbolic info for any new shared objects; just add them
477 to the list, and leave their symbols_loaded flag clear.
07cd4b97
JB
478
479 If FROM_TTY is non-null, feel free to print messages about what
480 we're doing.
c906108c 481
07cd4b97
JB
482 If TARGET is non-null, add the sections of all new shared objects
483 to TARGET's section table. Note that this doesn't remove any
484 sections for shared objects that have been unloaded, and it
485 doesn't check to see if the new shared objects are already present in
486 the section table. But we only use this for core files and
487 processes we've just attached to, so that's okay. */
c906108c 488
a78f21af 489static void
105b175f 490update_solib_list (int from_tty, struct target_ops *target)
07cd4b97 491{
66aba65d
MK
492 struct target_so_ops *ops = solib_ops (current_gdbarch);
493 struct so_list *inferior = ops->current_sos();
07cd4b97
JB
494 struct so_list *gdb, **gdb_link;
495
104c1213
JM
496 /* If we are attaching to a running process for which we
497 have not opened a symbol file, we may be able to get its
498 symbols now! */
499 if (attach_flag &&
500 symfile_objfile == NULL)
66aba65d 501 catch_errors (ops->open_symbol_file_object, &from_tty,
104c1213
JM
502 "Error reading attached process's symbol file.\n",
503 RETURN_MASK_ALL);
504
07cd4b97
JB
505 /* GDB and the inferior's dynamic linker each maintain their own
506 list of currently loaded shared objects; we want to bring the
507 former in sync with the latter. Scan both lists, seeing which
508 shared objects appear where. There are three cases:
509
510 - A shared object appears on both lists. This means that GDB
105b175f
JB
511 knows about it already, and it's still loaded in the inferior.
512 Nothing needs to happen.
07cd4b97
JB
513
514 - A shared object appears only on GDB's list. This means that
105b175f
JB
515 the inferior has unloaded it. We should remove the shared
516 object from GDB's tables.
07cd4b97
JB
517
518 - A shared object appears only on the inferior's list. This
105b175f
JB
519 means that it's just been loaded. We should add it to GDB's
520 tables.
07cd4b97
JB
521
522 So we walk GDB's list, checking each entry to see if it appears
523 in the inferior's list too. If it does, no action is needed, and
524 we remove it from the inferior's list. If it doesn't, the
525 inferior has unloaded it, and we remove it from GDB's list. By
526 the time we're done walking GDB's list, the inferior's list
527 contains only the new shared objects, which we then add. */
528
529 gdb = so_list_head;
530 gdb_link = &so_list_head;
531 while (gdb)
c906108c 532 {
07cd4b97
JB
533 struct so_list *i = inferior;
534 struct so_list **i_link = &inferior;
535
536 /* Check to see whether the shared object *gdb also appears in
537 the inferior's current list. */
538 while (i)
c906108c 539 {
07cd4b97
JB
540 if (! strcmp (gdb->so_original_name, i->so_original_name))
541 break;
542
543 i_link = &i->next;
544 i = *i_link;
c906108c 545 }
c5aa993b 546
07cd4b97
JB
547 /* If the shared object appears on the inferior's list too, then
548 it's still loaded, so we don't need to do anything. Delete
549 it from the inferior's list, and leave it on GDB's list. */
550 if (i)
c906108c 551 {
07cd4b97 552 *i_link = i->next;
07cd4b97
JB
553 free_so (i);
554 gdb_link = &gdb->next;
555 gdb = *gdb_link;
556 }
557
558 /* If it's not on the inferior's list, remove it from GDB's tables. */
559 else
560 {
42a6e6a0
MK
561 /* Notify any observer that the shared object has been
562 unloaded before we remove it from GDB's tables. */
84acb35a
JJ
563 observer_notify_solib_unloaded (gdb);
564
07cd4b97 565 *gdb_link = gdb->next;
07cd4b97
JB
566
567 /* Unless the user loaded it explicitly, free SO's objfile. */
e8930304 568 if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED))
07cd4b97
JB
569 free_objfile (gdb->objfile);
570
571 /* Some targets' section tables might be referring to
572 sections from so->abfd; remove them. */
573 remove_target_sections (gdb->abfd);
574
575 free_so (gdb);
576 gdb = *gdb_link;
c906108c
SS
577 }
578 }
c5aa993b 579
07cd4b97
JB
580 /* Now the inferior's list contains only shared objects that don't
581 appear in GDB's list --- those that are newly loaded. Add them
e8930304 582 to GDB's shared object list. */
07cd4b97 583 if (inferior)
c906108c 584 {
07cd4b97
JB
585 struct so_list *i;
586
587 /* Add the new shared objects to GDB's list. */
588 *gdb_link = inferior;
589
e8930304 590 /* Fill in the rest of each of the `struct so_list' nodes. */
07cd4b97 591 for (i = inferior; i; i = i->next)
c906108c 592 {
07cd4b97
JB
593 i->from_tty = from_tty;
594
595 /* Fill in the rest of the `struct so_list' node. */
596 catch_errors (solib_map_sections, i,
597 "Error while mapping shared library sections:\n",
598 RETURN_MASK_ALL);
07cd4b97 599
b41be06e
ND
600 /* If requested, add the shared object's sections to the TARGET's
601 section table. Do this immediately after mapping the object so
602 that later nodes in the list can query this object, as is needed
603 in solib-osf.c. */
604 if (target)
c906108c 605 {
b41be06e
ND
606 int count = (i->sections_end - i->sections);
607 if (count > 0)
07cd4b97 608 {
b41be06e 609 int space = target_resize_to_sections (target, count);
07cd4b97
JB
610 memcpy (target->to_sections + space,
611 i->sections,
612 count * sizeof (i->sections[0]));
07cd4b97 613 }
c906108c 614 }
42a6e6a0
MK
615
616 /* Notify any observer that the shared object has been
617 loaded now that we've added it to GDB's tables. */
618 observer_notify_solib_loaded (i);
c906108c 619 }
e8930304 620 }
105b175f
JB
621}
622
6612ad7f
JB
623/* Return non-zero if SO is the libpthread shared library.
624
625 Uses a fairly simplistic heuristic approach where we check
626 the file name against "/libpthread". This can lead to false
627 positives, but this should be good enough in practice. */
628
629static int
630libpthread_solib_p (struct so_list *so)
631{
632 return (strstr (so->so_name, "/libpthread") != NULL);
633}
105b175f
JB
634
635/* GLOBAL FUNCTION
636
637 solib_add -- read in symbol info for newly added shared libraries
638
639 SYNOPSIS
640
990f9fe3
FF
641 void solib_add (char *pattern, int from_tty, struct target_ops
642 *TARGET, int readsyms)
105b175f
JB
643
644 DESCRIPTION
645
646 Read in symbolic information for any shared objects whose names
647 match PATTERN. (If we've already read a shared object's symbol
648 info, leave it alone.) If PATTERN is zero, read them all.
649
990f9fe3
FF
650 If READSYMS is 0, defer reading symbolic information until later
651 but still do any needed low level processing.
652
105b175f
JB
653 FROM_TTY and TARGET are as described for update_solib_list, above. */
654
655void
990f9fe3 656solib_add (char *pattern, int from_tty, struct target_ops *target, int readsyms)
105b175f
JB
657{
658 struct so_list *gdb;
659
660 if (pattern)
661 {
662 char *re_err = re_comp (pattern);
663
664 if (re_err)
8a3fe4f8 665 error (_("Invalid regexp: %s"), re_err);
105b175f
JB
666 }
667
668 update_solib_list (from_tty, target);
c906108c 669
105b175f
JB
670 /* Walk the list of currently loaded shared libraries, and read
671 symbols for any that match the pattern --- or any whose symbols
672 aren't already loaded, if no pattern was given. */
e8930304
JB
673 {
674 int any_matches = 0;
675 int loaded_any_symbols = 0;
c906108c 676
e8930304
JB
677 for (gdb = so_list_head; gdb; gdb = gdb->next)
678 if (! pattern || re_exec (gdb->so_name))
679 {
6612ad7f
JB
680 /* Normally, we would read the symbols from that library
681 only if READSYMS is set. However, we're making a small
682 exception for the pthread library, because we sometimes
683 need the library symbols to be loaded in order to provide
684 thread support (x86-linux for instance). */
685 const int add_this_solib =
686 (readsyms || libpthread_solib_p (gdb));
687
e8930304 688 any_matches = 1;
6612ad7f 689 if (add_this_solib && solib_read_symbols (gdb, from_tty))
42a6e6a0 690 loaded_any_symbols = 1;
e8930304
JB
691 }
692
693 if (from_tty && pattern && ! any_matches)
694 printf_unfiltered
695 ("No loaded shared libraries match the pattern `%s'.\n", pattern);
696
697 if (loaded_any_symbols)
698 {
66aba65d
MK
699 struct target_so_ops *ops = solib_ops (current_gdbarch);
700
e8930304
JB
701 /* Getting new symbols may change our opinion about what is
702 frameless. */
703 reinit_frame_cache ();
704
66aba65d 705 ops->special_symbol_handling ();
e8930304
JB
706 }
707 }
c906108c
SS
708}
709
07cd4b97 710
c906108c
SS
711/*
712
c5aa993b 713 LOCAL FUNCTION
c906108c 714
c5aa993b 715 info_sharedlibrary_command -- code for "info sharedlibrary"
c906108c 716
c5aa993b 717 SYNOPSIS
c906108c 718
c5aa993b 719 static void info_sharedlibrary_command ()
c906108c 720
c5aa993b 721 DESCRIPTION
c906108c 722
c5aa993b
JM
723 Walk through the shared library list and print information
724 about each attached library.
725 */
c906108c
SS
726
727static void
fba45db2 728info_sharedlibrary_command (char *ignore, int from_tty)
c906108c 729{
52f0bd74 730 struct so_list *so = NULL; /* link map state variable */
c906108c
SS
731 int header_done = 0;
732 int addr_width;
c906108c 733
84eb3c4f 734 /* "0x", a little whitespace, and two hex digits per byte of pointers. */
819844ad 735 addr_width = 4 + (gdbarch_ptr_bit (current_gdbarch) / 4);
c906108c 736
105b175f 737 update_solib_list (from_tty, 0);
07cd4b97
JB
738
739 for (so = so_list_head; so; so = so->next)
c906108c 740 {
c5aa993b 741 if (so->so_name[0])
c906108c
SS
742 {
743 if (!header_done)
744 {
c5aa993b
JM
745 printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width, "From",
746 addr_width, "To", "Syms Read",
747 "Shared Object Library");
c906108c
SS
748 header_done++;
749 }
750
751 printf_unfiltered ("%-*s", addr_width,
cfa9d6d9 752 so->addr_high != 0
bb599908 753 ? hex_string_custom (
cfa9d6d9 754 (LONGEST) so->addr_low,
bb599908 755 addr_width - 4)
749499cb 756 : "");
c906108c 757 printf_unfiltered ("%-*s", addr_width,
cfa9d6d9 758 so->addr_high != 0
bb599908 759 ? hex_string_custom (
cfa9d6d9 760 (LONGEST) so->addr_high,
bb599908 761 addr_width - 4)
749499cb 762 : "");
c5aa993b
JM
763 printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No");
764 printf_unfiltered ("%s\n", so->so_name);
c906108c
SS
765 }
766 }
767 if (so_list_head == NULL)
768 {
a3f17187 769 printf_unfiltered (_("No shared libraries loaded at this time.\n"));
c906108c
SS
770 }
771}
772
773/*
774
c5aa993b 775 GLOBAL FUNCTION
c906108c 776
c5aa993b 777 solib_address -- check to see if an address is in a shared lib
c906108c 778
c5aa993b 779 SYNOPSIS
c906108c 780
c5aa993b 781 char * solib_address (CORE_ADDR address)
c906108c 782
c5aa993b 783 DESCRIPTION
c906108c 784
c5aa993b
JM
785 Provides a hook for other gdb routines to discover whether or
786 not a particular address is within the mapped address space of
749499cb 787 a shared library.
c906108c 788
c5aa993b
JM
789 For example, this routine is called at one point to disable
790 breakpoints which are in shared libraries that are not currently
791 mapped in.
c906108c
SS
792 */
793
794char *
fba45db2 795solib_address (CORE_ADDR address)
c906108c 796{
52f0bd74 797 struct so_list *so = 0; /* link map state variable */
c5aa993b 798
07cd4b97 799 for (so = so_list_head; so; so = so->next)
c906108c 800 {
749499cb
KB
801 struct section_table *p;
802
803 for (p = so->sections; p < so->sections_end; p++)
804 {
805 if (p->addr <= address && address < p->endaddr)
806 return (so->so_name);
807 }
c906108c 808 }
07cd4b97 809
c906108c
SS
810 return (0);
811}
812
813/* Called by free_all_symtabs */
814
c5aa993b 815void
fba45db2 816clear_solib (void)
c906108c 817{
66aba65d
MK
818 struct target_so_ops *ops = solib_ops (current_gdbarch);
819
085dd6e6
JM
820 /* This function is expected to handle ELF shared libraries. It is
821 also used on Solaris, which can run either ELF or a.out binaries
822 (for compatibility with SunOS 4), both of which can use shared
823 libraries. So we don't know whether we have an ELF executable or
824 an a.out executable until the user chooses an executable file.
825
826 ELF shared libraries don't get mapped into the address space
827 until after the program starts, so we'd better not try to insert
828 breakpoints in them immediately. We have to wait until the
829 dynamic linker has loaded them; we'll hit a bp_shlib_event
830 breakpoint (look for calls to create_solib_event_breakpoint) when
831 it's ready.
832
833 SunOS shared libraries seem to be different --- they're present
834 as soon as the process begins execution, so there's no need to
835 put off inserting breakpoints. There's also nowhere to put a
836 bp_shlib_event breakpoint, so if we put it off, we'll never get
837 around to it.
838
839 So: disable breakpoints only if we're using ELF shared libs. */
840 if (exec_bfd != NULL
841 && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
cb851954 842 disable_breakpoints_in_shlibs ();
085dd6e6 843
c906108c
SS
844 while (so_list_head)
845 {
07cd4b97
JB
846 struct so_list *so = so_list_head;
847 so_list_head = so->next;
2069d78d
KB
848 if (so->abfd)
849 remove_target_sections (so->abfd);
07cd4b97 850 free_so (so);
c906108c 851 }
07cd4b97 852
66aba65d 853 ops->clear_solib ();
c906108c
SS
854}
855
13437d4b 856/* GLOBAL FUNCTION
c5aa993b
JM
857
858 solib_create_inferior_hook -- shared library startup support
859
860 SYNOPSIS
861
7095b863 862 void solib_create_inferior_hook ()
c5aa993b
JM
863
864 DESCRIPTION
865
866 When gdb starts up the inferior, it nurses it along (through the
867 shell) until it is ready to execute it's first instruction. At this
868 point, this function gets called via expansion of the macro
13437d4b 869 SOLIB_CREATE_INFERIOR_HOOK. */
c5aa993b
JM
870
871void
fba45db2 872solib_create_inferior_hook (void)
c906108c 873{
66aba65d
MK
874 struct target_so_ops *ops = solib_ops (current_gdbarch);
875 ops->solib_create_inferior_hook();
c906108c
SS
876}
877
d7fa2ae2
KB
878/* GLOBAL FUNCTION
879
880 in_solib_dynsym_resolve_code -- check to see if an address is in
881 dynamic loader's dynamic symbol
882 resolution code
883
884 SYNOPSIS
885
886 int in_solib_dynsym_resolve_code (CORE_ADDR pc)
887
888 DESCRIPTION
889
890 Determine if PC is in the dynamic linker's symbol resolution
891 code. Return 1 if so, 0 otherwise.
892*/
893
894int
895in_solib_dynsym_resolve_code (CORE_ADDR pc)
896{
66aba65d
MK
897 struct target_so_ops *ops = solib_ops (current_gdbarch);
898 return ops->in_dynsym_resolve_code (pc);
d7fa2ae2 899}
c906108c
SS
900
901/*
902
c5aa993b 903 LOCAL FUNCTION
c906108c 904
c5aa993b 905 sharedlibrary_command -- handle command to explicitly add library
c906108c 906
c5aa993b 907 SYNOPSIS
c906108c 908
c5aa993b 909 static void sharedlibrary_command (char *args, int from_tty)
c906108c 910
c5aa993b 911 DESCRIPTION
c906108c 912
c5aa993b 913 */
c906108c
SS
914
915static void
fba45db2 916sharedlibrary_command (char *args, int from_tty)
c906108c
SS
917{
918 dont_repeat ();
990f9fe3 919 solib_add (args, from_tty, (struct target_ops *) 0, 1);
c906108c
SS
920}
921
cb0ba49e
MS
922/* LOCAL FUNCTION
923
924 no_shared_libraries -- handle command to explicitly discard symbols
925 from shared libraries.
926
927 DESCRIPTION
928
929 Implements the command "nosharedlibrary", which discards symbols
930 that have been auto-loaded from shared libraries. Symbols from
931 shared libraries that were added by explicit request of the user
932 are not discarded. Also called from remote.c. */
933
c60a7562
MS
934void
935no_shared_libraries (char *ignored, int from_tty)
936{
937 objfile_purge_solibs ();
615b9dba 938 clear_solib ();
c60a7562 939}
c906108c 940
cf466558 941static void
f397e303
AC
942reload_shared_libraries (char *ignored, int from_tty,
943 struct cmd_list_element *e)
cf466558
KB
944{
945 no_shared_libraries (NULL, from_tty);
946 solib_add (NULL, from_tty, NULL, auto_solib_add);
947}
948
920d2a44
AC
949static void
950show_auto_solib_add (struct ui_file *file, int from_tty,
951 struct cmd_list_element *c, const char *value)
952{
953 fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
954 value);
955}
956
957
3a40aaa0
UW
958/* Handler for library-specific lookup of global symbol NAME in OBJFILE. Call
959 the library-specific handler if it is installed for the current target. */
960
961struct symbol *
962solib_global_lookup (const struct objfile *objfile,
963 const char *name,
964 const char *linkage_name,
965 const domain_enum domain,
966 struct symtab **symtab)
967{
e8a92f7b 968 struct target_so_ops *ops = solib_ops (current_gdbarch);
3a40aaa0 969
e8a92f7b
UW
970 if (ops->lookup_lib_global_symbol != NULL)
971 return ops->lookup_lib_global_symbol (objfile, name, linkage_name,
972 domain, symtab);
3a40aaa0
UW
973 return NULL;
974}
975
976
a78f21af
AC
977extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
978
c906108c 979void
fba45db2 980_initialize_solib (void)
c906108c 981{
fa58ee11
EZ
982 struct cmd_list_element *c;
983
66aba65d
MK
984 solib_data = gdbarch_data_register_pre_init (solib_init);
985
c906108c 986 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1bedd215 987 _("Load shared object library symbols for files matching REGEXP."));
c5aa993b 988 add_info ("sharedlibrary", info_sharedlibrary_command,
1bedd215 989 _("Status of loaded shared object libraries."));
c60a7562 990 add_com ("nosharedlibrary", class_files, no_shared_libraries,
1bedd215 991 _("Unload all shared object library symbols."));
c906108c 992
5bf193a2
AC
993 add_setshow_boolean_cmd ("auto-solib-add", class_support,
994 &auto_solib_add, _("\
995Set autoloading of shared library symbols."), _("\
996Show autoloading of shared library symbols."), _("\
b7209cb4
FF
997If \"on\", symbols from all shared object libraries will be loaded\n\
998automatically when the inferior begins execution, when the dynamic linker\n\
999informs gdb that a new library has been loaded, or when attaching to the\n\
5bf193a2
AC
1000inferior. Otherwise, symbols must be loaded manually, using `sharedlibrary'."),
1001 NULL,
920d2a44 1002 show_auto_solib_add,
5bf193a2 1003 &setlist, &showlist);
c906108c 1004
f822c95b
DJ
1005 add_setshow_filename_cmd ("sysroot", class_support,
1006 &gdb_sysroot, _("\
1007Set an alternate system root."), _("\
1008Show the current system root."), _("\
1009The system root is used to load absolute shared library symbol files.\n\
1010For other (relative) files, you can add directories using\n\
1011`set solib-search-path'."),
f397e303
AC
1012 reload_shared_libraries,
1013 NULL,
1014 &setlist, &showlist);
c906108c 1015
f822c95b
DJ
1016 add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1017 &setlist);
1018 add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1019 &showlist);
030292b7 1020
525226b5
AC
1021 add_setshow_optional_filename_cmd ("solib-search-path", class_support,
1022 &solib_search_path, _("\
1023Set the search path for loading non-absolute shared library symbol files."), _("\
1024Show the search path for loading non-absolute shared library symbol files."), _("\
1025This takes precedence over the environment variables PATH and LD_LIBRARY_PATH."),
1026 reload_shared_libraries,
920d2a44 1027 show_solib_search_path,
525226b5 1028 &setlist, &showlist);
c906108c 1029}
This page took 1.188962 seconds and 4 git commands to generate.