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