Strip "target:" prefix in solib_find if accessing local files
[deliverable/binutils-gdb.git] / gdb / solib.c
1 /* Handle shared libraries for GDB, the GNU Debugger.
2
3 Copyright (C) 1990-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include "symtab.h"
25 #include "bfd.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "gdbcore.h"
29 #include "command.h"
30 #include "target.h"
31 #include "frame.h"
32 #include "gdb_regex.h"
33 #include "inferior.h"
34 #include "environ.h"
35 #include "language.h"
36 #include "gdbcmd.h"
37 #include "completer.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "exec.h"
40 #include "solist.h"
41 #include "observer.h"
42 #include "readline/readline.h"
43 #include "remote.h"
44 #include "solib.h"
45 #include "interps.h"
46 #include "filesystem.h"
47 #include "gdb_bfd.h"
48 #include "filestuff.h"
49
50 /* Architecture-specific operations. */
51
52 /* Per-architecture data key. */
53 static struct gdbarch_data *solib_data;
54
55 static void *
56 solib_init (struct obstack *obstack)
57 {
58 struct target_so_ops **ops;
59
60 ops = OBSTACK_ZALLOC (obstack, struct target_so_ops *);
61 *ops = current_target_so_ops;
62 return ops;
63 }
64
65 static const struct target_so_ops *
66 solib_ops (struct gdbarch *gdbarch)
67 {
68 const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
69
70 return *ops;
71 }
72
73 /* Set the solib operations for GDBARCH to NEW_OPS. */
74
75 void
76 set_solib_ops (struct gdbarch *gdbarch, const struct target_so_ops *new_ops)
77 {
78 const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
79
80 *ops = new_ops;
81 }
82 \f
83
84 /* external data declarations */
85
86 /* FIXME: gdbarch needs to control this variable, or else every
87 configuration needs to call set_solib_ops. */
88 struct target_so_ops *current_target_so_ops;
89
90 /* List of known shared objects */
91 #define so_list_head current_program_space->so_list
92
93 /* Local function prototypes */
94
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. */
98 static char *solib_search_path = NULL;
99 static void
100 show_solib_search_path (struct ui_file *file, int from_tty,
101 struct cmd_list_element *c, const char *value)
102 {
103 fprintf_filtered (file, _("The search path for loading non-absolute "
104 "shared library symbol files is %s.\n"),
105 value);
106 }
107
108 /* Same as HAVE_DOS_BASED_FILE_SYSTEM, but useable as an rvalue. */
109 #if (HAVE_DOS_BASED_FILE_SYSTEM)
110 # define DOS_BASED_FILE_SYSTEM 1
111 #else
112 # define DOS_BASED_FILE_SYSTEM 0
113 #endif
114
115 /* Returns the full pathname of the shared library file, or NULL if
116 not found. (The pathname is malloc'ed; it needs to be freed by the
117 caller.) *FD is set to either -1 or an open file handle for the
118 library.
119
120 Global variable GDB_SYSROOT is used as a prefix directory
121 to search for shared libraries if they have an absolute path.
122 If GDB_SYSROOT starts with "target:" and target filesystem
123 is the local filesystem then the "target:" prefix will be
124 stripped before the search starts. This ensures that the
125 same search algorithm is used for local files regardless of
126 whether a "target:" prefix was used.
127
128 Global variable SOLIB_SEARCH_PATH is used as a prefix directory
129 (or set of directories, as in LD_LIBRARY_PATH) to search for all
130 shared libraries if not found in either the sysroot (if set) or
131 the local filesystem.
132
133 Search algorithm:
134 * If a sysroot is set and path is absolute:
135 * Search for sysroot/path.
136 * else
137 * Look for it literally (unmodified).
138 * Look in SOLIB_SEARCH_PATH.
139 * If available, use target defined search function.
140 * If NO sysroot is set, perform the following two searches:
141 * Look in inferior's $PATH.
142 * Look in inferior's $LD_LIBRARY_PATH.
143 *
144 * The last check avoids doing this search when targetting remote
145 * machines since a sysroot will almost always be set.
146 */
147
148 char *
149 solib_find (char *in_pathname, int *fd)
150 {
151 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
152 int found_file = -1;
153 char *temp_pathname = NULL;
154 const char *solib_symbols_extension
155 = gdbarch_solib_symbols_extension (target_gdbarch ());
156 const char *fskind = effective_target_file_system_kind ();
157 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
158 char *sysroot = gdb_sysroot;
159
160 /* If solib_symbols_extension is set, replace the file's
161 extension. */
162 if (solib_symbols_extension)
163 {
164 char *p = in_pathname + strlen (in_pathname);
165
166 while (p > in_pathname && *p != '.')
167 p--;
168
169 if (*p == '.')
170 {
171 char *new_pathname;
172
173 new_pathname = alloca (p - in_pathname + 1
174 + strlen (solib_symbols_extension) + 1);
175 memcpy (new_pathname, in_pathname, p - in_pathname + 1);
176 strcpy (new_pathname + (p - in_pathname) + 1,
177 solib_symbols_extension);
178
179 in_pathname = new_pathname;
180 }
181 }
182
183 if (sysroot != NULL)
184 {
185 /* If the absolute prefix starts with "target:" but the
186 filesystem accessed by the target_fileio_* methods
187 is the local filesystem then we strip the "target:"
188 prefix now and work with the local filesystem. This
189 ensures that the same search algorithm is used for
190 all local files regardless of whether a "target:"
191 prefix was used. */
192 if (is_target_filename (sysroot) && target_filesystem_is_local ())
193 sysroot += strlen (TARGET_SYSROOT_PREFIX);
194
195 if (*sysroot == '\0')
196 sysroot = NULL;
197 }
198
199 if (sysroot != NULL)
200 {
201 int prefix_len = strlen (sysroot);
202
203 /* Remove trailing slashes from absolute prefix. */
204 while (prefix_len > 0
205 && IS_DIR_SEPARATOR (sysroot[prefix_len - 1]))
206 prefix_len--;
207
208 sysroot = savestring (sysroot, prefix_len);
209 make_cleanup (xfree, sysroot);
210 }
211
212 /* If we're on a non-DOS-based system, backslashes won't be
213 understood as directory separator, so, convert them to forward
214 slashes, iff we're supposed to handle DOS-based file system
215 semantics for target paths. */
216 if (!DOS_BASED_FILE_SYSTEM && fskind == file_system_kind_dos_based)
217 {
218 char *p;
219
220 /* Avoid clobbering our input. */
221 p = alloca (strlen (in_pathname) + 1);
222 strcpy (p, in_pathname);
223 in_pathname = p;
224
225 for (; *p; p++)
226 {
227 if (*p == '\\')
228 *p = '/';
229 }
230 }
231
232 /* Note, we're interested in IS_TARGET_ABSOLUTE_PATH, not
233 IS_ABSOLUTE_PATH. The latter is for host paths only, while
234 IN_PATHNAME is a target path. For example, if we're supposed to
235 be handling DOS-like semantics we want to consider a
236 'c:/foo/bar.dll' path as an absolute path, even on a Unix box.
237 With such a path, before giving up on the sysroot, we'll try:
238
239 1st attempt, c:/foo/bar.dll ==> /sysroot/c:/foo/bar.dll
240 2nd attempt, c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll
241 3rd attempt, c:/foo/bar.dll ==> /sysroot/foo/bar.dll
242 */
243
244 if (!IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname) || sysroot == NULL)
245 temp_pathname = xstrdup (in_pathname);
246 else
247 {
248 int need_dir_separator;
249
250 /* Concatenate the sysroot and the target reported filename. We
251 may need to glue them with a directory separator. Cases to
252 consider:
253
254 | sysroot | separator | in_pathname |
255 |-----------------+-----------+----------------|
256 | /some/dir | / | c:/foo/bar.dll |
257 | /some/dir | | /foo/bar.dll |
258 | target: | | c:/foo/bar.dll |
259 | target: | | /foo/bar.dll |
260 | target:some/dir | / | c:/foo/bar.dll |
261 | target:some/dir | | /foo/bar.dll |
262
263 IOW, we don't need to add a separator if IN_PATHNAME already
264 has one, or when the the sysroot is exactly "target:".
265 There's no need to check for drive spec explicitly, as we only
266 get here if IN_PATHNAME is considered an absolute path. */
267 need_dir_separator = !(IS_DIR_SEPARATOR (in_pathname[0])
268 || strcmp (TARGET_SYSROOT_PREFIX, sysroot) == 0);
269
270 /* Cat the prefixed pathname together. */
271 temp_pathname = concat (sysroot,
272 need_dir_separator ? SLASH_STRING : "",
273 in_pathname, (char *) NULL);
274 }
275
276 /* Handle files to be accessed via the target. */
277 if (is_target_filename (temp_pathname))
278 {
279 *fd = -1;
280 do_cleanups (old_chain);
281 return temp_pathname;
282 }
283
284 /* Now see if we can open it. */
285 found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
286 if (found_file < 0)
287 xfree (temp_pathname);
288
289 /* If the search in gdb_sysroot failed, and the path name has a
290 drive spec (e.g, c:/foo), try stripping ':' from the drive spec,
291 and retrying in the sysroot:
292 c:/foo/bar.dll ==> /sysroot/c/foo/bar.dll. */
293
294 if (found_file < 0
295 && sysroot != NULL
296 && HAS_TARGET_DRIVE_SPEC (fskind, in_pathname))
297 {
298 int need_dir_separator = !IS_DIR_SEPARATOR (in_pathname[2]);
299 char *drive = savestring (in_pathname, 1);
300
301 temp_pathname = concat (sysroot,
302 SLASH_STRING,
303 drive,
304 need_dir_separator ? SLASH_STRING : "",
305 in_pathname + 2, (char *) NULL);
306 xfree (drive);
307
308 found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
309 if (found_file < 0)
310 {
311 xfree (temp_pathname);
312
313 /* If the search in gdb_sysroot still failed, try fully
314 stripping the drive spec, and trying once more in the
315 sysroot before giving up.
316
317 c:/foo/bar.dll ==> /sysroot/foo/bar.dll. */
318
319 temp_pathname = concat (sysroot,
320 need_dir_separator ? SLASH_STRING : "",
321 in_pathname + 2, (char *) NULL);
322
323 found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
324 if (found_file < 0)
325 xfree (temp_pathname);
326 }
327 }
328
329 do_cleanups (old_chain);
330
331 /* We try to find the library in various ways. After each attempt,
332 either found_file >= 0 and temp_pathname is a malloc'd string, or
333 found_file < 0 and temp_pathname does not point to storage that
334 needs to be freed. */
335
336 if (found_file < 0)
337 temp_pathname = NULL;
338
339 /* If the search in gdb_sysroot failed, and the path name is
340 absolute at this point, make it relative. (openp will try and open the
341 file according to its absolute path otherwise, which is not what we want.)
342 Affects subsequent searches for this solib. */
343 if (found_file < 0 && IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname))
344 {
345 /* First, get rid of any drive letters etc. */
346 while (!IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
347 in_pathname++;
348
349 /* Next, get rid of all leading dir separators. */
350 while (IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
351 in_pathname++;
352 }
353
354 /* If not found, search the solib_search_path (if any). */
355 if (found_file < 0 && solib_search_path != NULL)
356 found_file = openp (solib_search_path,
357 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
358 in_pathname, O_RDONLY | O_BINARY, &temp_pathname);
359
360 /* If not found, next search the solib_search_path (if any) for the basename
361 only (ignoring the path). This is to allow reading solibs from a path
362 that differs from the opened path. */
363 if (found_file < 0 && solib_search_path != NULL)
364 found_file = openp (solib_search_path,
365 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
366 target_lbasename (fskind, in_pathname),
367 O_RDONLY | O_BINARY, &temp_pathname);
368
369 /* If not found, try to use target supplied solib search method. */
370 if (found_file < 0 && ops->find_and_open_solib)
371 found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
372 &temp_pathname);
373
374 /* If not found, next search the inferior's $PATH environment variable. */
375 if (found_file < 0 && sysroot == NULL)
376 found_file = openp (get_in_environ (current_inferior ()->environment,
377 "PATH"),
378 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
379 O_RDONLY | O_BINARY, &temp_pathname);
380
381 /* If not found, next search the inferior's $LD_LIBRARY_PATH
382 environment variable. */
383 if (found_file < 0 && sysroot == NULL)
384 found_file = openp (get_in_environ (current_inferior ()->environment,
385 "LD_LIBRARY_PATH"),
386 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
387 O_RDONLY | O_BINARY, &temp_pathname);
388
389 *fd = found_file;
390 return temp_pathname;
391 }
392
393 /* Open and return a BFD for the shared library PATHNAME. If FD is not -1,
394 it is used as file handle to open the file. Throws an error if the file
395 could not be opened. Handles both local and remote file access.
396
397 PATHNAME must be malloc'ed by the caller. It will be freed by this
398 function. If unsuccessful, the FD will be closed (unless FD was
399 -1). */
400
401 bfd *
402 solib_bfd_fopen (char *pathname, int fd)
403 {
404 bfd *abfd = gdb_bfd_open (pathname, gnutarget, fd);
405
406 if (abfd != NULL && !gdb_bfd_has_target_filename (abfd))
407 bfd_set_cacheable (abfd, 1);
408
409 if (!abfd)
410 {
411 make_cleanup (xfree, pathname);
412 error (_("Could not open `%s' as an executable file: %s"),
413 pathname, bfd_errmsg (bfd_get_error ()));
414 }
415
416 xfree (pathname);
417
418 return abfd;
419 }
420
421 /* Find shared library PATHNAME and open a BFD for it. */
422
423 bfd *
424 solib_bfd_open (char *pathname)
425 {
426 char *found_pathname;
427 int found_file;
428 bfd *abfd;
429 const struct bfd_arch_info *b;
430
431 /* Search for shared library file. */
432 found_pathname = solib_find (pathname, &found_file);
433 if (found_pathname == NULL)
434 {
435 /* Return failure if the file could not be found, so that we can
436 accumulate messages about missing libraries. */
437 if (errno == ENOENT)
438 return NULL;
439
440 perror_with_name (pathname);
441 }
442
443 /* Open bfd for shared library. */
444 abfd = solib_bfd_fopen (found_pathname, found_file);
445
446 /* Check bfd format. */
447 if (!bfd_check_format (abfd, bfd_object))
448 {
449 make_cleanup_bfd_unref (abfd);
450 error (_("`%s': not in executable format: %s"),
451 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
452 }
453
454 /* Check bfd arch. */
455 b = gdbarch_bfd_arch_info (target_gdbarch ());
456 if (!b->compatible (b, bfd_get_arch_info (abfd)))
457 warning (_("`%s': Shared library architecture %s is not compatible "
458 "with target architecture %s."), bfd_get_filename (abfd),
459 bfd_get_arch_info (abfd)->printable_name, b->printable_name);
460
461 return abfd;
462 }
463
464 /* Given a pointer to one of the shared objects in our list of mapped
465 objects, use the recorded name to open a bfd descriptor for the
466 object, build a section table, relocate all the section addresses
467 by the base address at which the shared object was mapped, and then
468 add the sections to the target's section table.
469
470 FIXME: In most (all?) cases the shared object file name recorded in
471 the dynamic linkage tables will be a fully qualified pathname. For
472 cases where it isn't, do we really mimic the systems search
473 mechanism correctly in the below code (particularly the tilde
474 expansion stuff?). */
475
476 static int
477 solib_map_sections (struct so_list *so)
478 {
479 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
480 char *filename;
481 struct target_section *p;
482 struct cleanup *old_chain;
483 bfd *abfd;
484
485 filename = tilde_expand (so->so_name);
486 old_chain = make_cleanup (xfree, filename);
487 abfd = ops->bfd_open (filename);
488 do_cleanups (old_chain);
489
490 if (abfd == NULL)
491 return 0;
492
493 /* Leave bfd open, core_xfer_memory and "info files" need it. */
494 so->abfd = abfd;
495
496 /* Copy the full path name into so_name, allowing symbol_file_add
497 to find it later. This also affects the =library-loaded GDB/MI
498 event, and in particular the part of that notification providing
499 the library's host-side path. If we let the target dictate
500 that objfile's path, and the target is different from the host,
501 GDB/MI will not provide the correct host-side path. */
502 if (strlen (bfd_get_filename (abfd)) >= SO_NAME_MAX_PATH_SIZE)
503 error (_("Shared library file name is too long."));
504 strcpy (so->so_name, bfd_get_filename (abfd));
505
506 if (build_section_table (abfd, &so->sections, &so->sections_end))
507 {
508 error (_("Can't find the file sections in `%s': %s"),
509 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
510 }
511
512 for (p = so->sections; p < so->sections_end; p++)
513 {
514 /* Relocate the section binding addresses as recorded in the shared
515 object's file by the base address to which the object was actually
516 mapped. */
517 ops->relocate_section_addresses (so, p);
518
519 /* If the target didn't provide information about the address
520 range of the shared object, assume we want the location of
521 the .text section. */
522 if (so->addr_low == 0 && so->addr_high == 0
523 && strcmp (p->the_bfd_section->name, ".text") == 0)
524 {
525 so->addr_low = p->addr;
526 so->addr_high = p->endaddr;
527 }
528 }
529
530 /* Add the shared object's sections to the current set of file
531 section tables. Do this immediately after mapping the object so
532 that later nodes in the list can query this object, as is needed
533 in solib-osf.c. */
534 add_target_sections (so, so->sections, so->sections_end);
535
536 return 1;
537 }
538
539 /* Free symbol-file related contents of SO and reset for possible reloading
540 of SO. If we have opened a BFD for SO, close it. If we have placed SO's
541 sections in some target's section table, the caller is responsible for
542 removing them.
543
544 This function doesn't mess with objfiles at all. If there is an
545 objfile associated with SO that needs to be removed, the caller is
546 responsible for taking care of that. */
547
548 static void
549 clear_so (struct so_list *so)
550 {
551 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
552
553 if (so->sections)
554 {
555 xfree (so->sections);
556 so->sections = so->sections_end = NULL;
557 }
558
559 gdb_bfd_unref (so->abfd);
560 so->abfd = NULL;
561
562 /* Our caller closed the objfile, possibly via objfile_purge_solibs. */
563 so->symbols_loaded = 0;
564 so->objfile = NULL;
565
566 so->addr_low = so->addr_high = 0;
567
568 /* Restore the target-supplied file name. SO_NAME may be the path
569 of the symbol file. */
570 strcpy (so->so_name, so->so_original_name);
571
572 /* Do the same for target-specific data. */
573 if (ops->clear_so != NULL)
574 ops->clear_so (so);
575 }
576
577 /* Free the storage associated with the `struct so_list' object SO.
578 If we have opened a BFD for SO, close it.
579
580 The caller is responsible for removing SO from whatever list it is
581 a member of. If we have placed SO's sections in some target's
582 section table, the caller is responsible for removing them.
583
584 This function doesn't mess with objfiles at all. If there is an
585 objfile associated with SO that needs to be removed, the caller is
586 responsible for taking care of that. */
587
588 void
589 free_so (struct so_list *so)
590 {
591 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
592
593 clear_so (so);
594 ops->free_so (so);
595
596 xfree (so);
597 }
598
599
600 /* Return address of first so_list entry in master shared object list. */
601 struct so_list *
602 master_so_list (void)
603 {
604 return so_list_head;
605 }
606
607 /* Read in symbols for shared object SO. If SYMFILE_VERBOSE is set in FLAGS,
608 be chatty about it. Return non-zero if any symbols were actually
609 loaded. */
610
611 int
612 solib_read_symbols (struct so_list *so, int flags)
613 {
614 if (so->symbols_loaded)
615 {
616 /* If needed, we've already warned in our caller. */
617 }
618 else if (so->abfd == NULL)
619 {
620 /* We've already warned about this library, when trying to open
621 it. */
622 }
623 else
624 {
625
626 flags |= current_inferior ()->symfile_flags;
627
628 TRY
629 {
630 struct section_addr_info *sap;
631
632 /* Have we already loaded this shared object? */
633 ALL_OBJFILES (so->objfile)
634 {
635 if (filename_cmp (objfile_name (so->objfile), so->so_name) == 0
636 && so->objfile->addr_low == so->addr_low)
637 break;
638 }
639 if (so->objfile != NULL)
640 break;
641
642 sap = build_section_addr_info_from_section_table (so->sections,
643 so->sections_end);
644 so->objfile = symbol_file_add_from_bfd (so->abfd, so->so_name,
645 flags, sap, OBJF_SHARED,
646 NULL);
647 so->objfile->addr_low = so->addr_low;
648 free_section_addr_info (sap);
649
650 so->symbols_loaded = 1;
651 }
652 CATCH (e, RETURN_MASK_ERROR)
653 {
654 exception_fprintf (gdb_stderr, e, _("Error while reading shared"
655 " library symbols for %s:\n"),
656 so->so_name);
657 }
658 END_CATCH
659
660 return 1;
661 }
662
663 return 0;
664 }
665
666 /* Return 1 if KNOWN->objfile is used by any other so_list object in the
667 SO_LIST_HEAD list. Return 0 otherwise. */
668
669 static int
670 solib_used (const struct so_list *const known)
671 {
672 const struct so_list *pivot;
673
674 for (pivot = so_list_head; pivot != NULL; pivot = pivot->next)
675 if (pivot != known && pivot->objfile == known->objfile)
676 return 1;
677 return 0;
678 }
679
680 /* Synchronize GDB's shared object list with inferior's.
681
682 Extract the list of currently loaded shared objects from the
683 inferior, and compare it with the list of shared objects currently
684 in GDB's so_list_head list. Edit so_list_head to bring it in sync
685 with the inferior's new list.
686
687 If we notice that the inferior has unloaded some shared objects,
688 free any symbolic info GDB had read about those shared objects.
689
690 Don't load symbolic info for any new shared objects; just add them
691 to the list, and leave their symbols_loaded flag clear.
692
693 If FROM_TTY is non-null, feel free to print messages about what
694 we're doing.
695
696 If TARGET is non-null, add the sections of all new shared objects
697 to TARGET's section table. Note that this doesn't remove any
698 sections for shared objects that have been unloaded, and it
699 doesn't check to see if the new shared objects are already present in
700 the section table. But we only use this for core files and
701 processes we've just attached to, so that's okay. */
702
703 static void
704 update_solib_list (int from_tty, struct target_ops *target)
705 {
706 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
707 struct so_list *inferior = ops->current_sos();
708 struct so_list *gdb, **gdb_link;
709
710 /* We can reach here due to changing solib-search-path or the
711 sysroot, before having any inferior. */
712 if (target_has_execution && !ptid_equal (inferior_ptid, null_ptid))
713 {
714 struct inferior *inf = current_inferior ();
715
716 /* If we are attaching to a running process for which we
717 have not opened a symbol file, we may be able to get its
718 symbols now! */
719 if (inf->attach_flag && symfile_objfile == NULL)
720 catch_errors (ops->open_symbol_file_object, &from_tty,
721 "Error reading attached process's symbol file.\n",
722 RETURN_MASK_ALL);
723 }
724
725 /* GDB and the inferior's dynamic linker each maintain their own
726 list of currently loaded shared objects; we want to bring the
727 former in sync with the latter. Scan both lists, seeing which
728 shared objects appear where. There are three cases:
729
730 - A shared object appears on both lists. This means that GDB
731 knows about it already, and it's still loaded in the inferior.
732 Nothing needs to happen.
733
734 - A shared object appears only on GDB's list. This means that
735 the inferior has unloaded it. We should remove the shared
736 object from GDB's tables.
737
738 - A shared object appears only on the inferior's list. This
739 means that it's just been loaded. We should add it to GDB's
740 tables.
741
742 So we walk GDB's list, checking each entry to see if it appears
743 in the inferior's list too. If it does, no action is needed, and
744 we remove it from the inferior's list. If it doesn't, the
745 inferior has unloaded it, and we remove it from GDB's list. By
746 the time we're done walking GDB's list, the inferior's list
747 contains only the new shared objects, which we then add. */
748
749 gdb = so_list_head;
750 gdb_link = &so_list_head;
751 while (gdb)
752 {
753 struct so_list *i = inferior;
754 struct so_list **i_link = &inferior;
755
756 /* Check to see whether the shared object *gdb also appears in
757 the inferior's current list. */
758 while (i)
759 {
760 if (ops->same)
761 {
762 if (ops->same (gdb, i))
763 break;
764 }
765 else
766 {
767 if (! filename_cmp (gdb->so_original_name, i->so_original_name))
768 break;
769 }
770
771 i_link = &i->next;
772 i = *i_link;
773 }
774
775 /* If the shared object appears on the inferior's list too, then
776 it's still loaded, so we don't need to do anything. Delete
777 it from the inferior's list, and leave it on GDB's list. */
778 if (i)
779 {
780 *i_link = i->next;
781 free_so (i);
782 gdb_link = &gdb->next;
783 gdb = *gdb_link;
784 }
785
786 /* If it's not on the inferior's list, remove it from GDB's tables. */
787 else
788 {
789 /* Notify any observer that the shared object has been
790 unloaded before we remove it from GDB's tables. */
791 observer_notify_solib_unloaded (gdb);
792
793 VEC_safe_push (char_ptr, current_program_space->deleted_solibs,
794 xstrdup (gdb->so_name));
795
796 *gdb_link = gdb->next;
797
798 /* Unless the user loaded it explicitly, free SO's objfile. */
799 if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
800 && !solib_used (gdb))
801 free_objfile (gdb->objfile);
802
803 /* Some targets' section tables might be referring to
804 sections from so->abfd; remove them. */
805 remove_target_sections (gdb);
806
807 free_so (gdb);
808 gdb = *gdb_link;
809 }
810 }
811
812 /* Now the inferior's list contains only shared objects that don't
813 appear in GDB's list --- those that are newly loaded. Add them
814 to GDB's shared object list. */
815 if (inferior)
816 {
817 int not_found = 0;
818 const char *not_found_filename = NULL;
819
820 struct so_list *i;
821
822 /* Add the new shared objects to GDB's list. */
823 *gdb_link = inferior;
824
825 /* Fill in the rest of each of the `struct so_list' nodes. */
826 for (i = inferior; i; i = i->next)
827 {
828
829 i->pspace = current_program_space;
830 VEC_safe_push (so_list_ptr, current_program_space->added_solibs, i);
831
832 TRY
833 {
834 /* Fill in the rest of the `struct so_list' node. */
835 if (!solib_map_sections (i))
836 {
837 not_found++;
838 if (not_found_filename == NULL)
839 not_found_filename = i->so_original_name;
840 }
841 }
842
843 CATCH (e, RETURN_MASK_ERROR)
844 {
845 exception_fprintf (gdb_stderr, e,
846 _("Error while mapping shared "
847 "library sections:\n"));
848 }
849 END_CATCH
850
851 /* Notify any observer that the shared object has been
852 loaded now that we've added it to GDB's tables. */
853 observer_notify_solib_loaded (i);
854 }
855
856 /* If a library was not found, issue an appropriate warning
857 message. We have to use a single call to warning in case the
858 front end does something special with warnings, e.g., pop up
859 a dialog box. It Would Be Nice if we could get a "warning: "
860 prefix on each line in the CLI front end, though - it doesn't
861 stand out well. */
862
863 if (not_found == 1)
864 warning (_("Could not load shared library symbols for %s.\n"
865 "Do you need \"set solib-search-path\" "
866 "or \"set sysroot\"?"),
867 not_found_filename);
868 else if (not_found > 1)
869 warning (_("\
870 Could not load shared library symbols for %d libraries, e.g. %s.\n\
871 Use the \"info sharedlibrary\" command to see the complete listing.\n\
872 Do you need \"set solib-search-path\" or \"set sysroot\"?"),
873 not_found, not_found_filename);
874 }
875 }
876
877
878 /* Return non-zero if NAME is the libpthread shared library.
879
880 Uses a fairly simplistic heuristic approach where we check
881 the file name against "/libpthread". This can lead to false
882 positives, but this should be good enough in practice. */
883
884 int
885 libpthread_name_p (const char *name)
886 {
887 return (strstr (name, "/libpthread") != NULL);
888 }
889
890 /* Return non-zero if SO is the libpthread shared library. */
891
892 static int
893 libpthread_solib_p (struct so_list *so)
894 {
895 return libpthread_name_p (so->so_name);
896 }
897
898 /* Read in symbolic information for any shared objects whose names
899 match PATTERN. (If we've already read a shared object's symbol
900 info, leave it alone.) If PATTERN is zero, read them all.
901
902 If READSYMS is 0, defer reading symbolic information until later
903 but still do any needed low level processing.
904
905 FROM_TTY and TARGET are as described for update_solib_list, above. */
906
907 void
908 solib_add (const char *pattern, int from_tty,
909 struct target_ops *target, int readsyms)
910 {
911 struct so_list *gdb;
912
913 if (print_symbol_loading_p (from_tty, 0, 0))
914 {
915 if (pattern != NULL)
916 {
917 printf_unfiltered (_("Loading symbols for shared libraries: %s\n"),
918 pattern);
919 }
920 else
921 printf_unfiltered (_("Loading symbols for shared libraries.\n"));
922 }
923
924 current_program_space->solib_add_generation++;
925
926 if (pattern)
927 {
928 char *re_err = re_comp (pattern);
929
930 if (re_err)
931 error (_("Invalid regexp: %s"), re_err);
932 }
933
934 update_solib_list (from_tty, target);
935
936 /* Walk the list of currently loaded shared libraries, and read
937 symbols for any that match the pattern --- or any whose symbols
938 aren't already loaded, if no pattern was given. */
939 {
940 int any_matches = 0;
941 int loaded_any_symbols = 0;
942 const int flags =
943 SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);
944
945 for (gdb = so_list_head; gdb; gdb = gdb->next)
946 if (! pattern || re_exec (gdb->so_name))
947 {
948 /* Normally, we would read the symbols from that library
949 only if READSYMS is set. However, we're making a small
950 exception for the pthread library, because we sometimes
951 need the library symbols to be loaded in order to provide
952 thread support (x86-linux for instance). */
953 const int add_this_solib =
954 (readsyms || libpthread_solib_p (gdb));
955
956 any_matches = 1;
957 if (add_this_solib)
958 {
959 if (gdb->symbols_loaded)
960 {
961 /* If no pattern was given, be quiet for shared
962 libraries we have already loaded. */
963 if (pattern && (from_tty || info_verbose))
964 printf_unfiltered (_("Symbols already loaded for %s\n"),
965 gdb->so_name);
966 }
967 else if (solib_read_symbols (gdb, flags))
968 loaded_any_symbols = 1;
969 }
970 }
971
972 if (loaded_any_symbols)
973 breakpoint_re_set ();
974
975 if (from_tty && pattern && ! any_matches)
976 printf_unfiltered
977 ("No loaded shared libraries match the pattern `%s'.\n", pattern);
978
979 if (loaded_any_symbols)
980 {
981 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
982
983 /* Getting new symbols may change our opinion about what is
984 frameless. */
985 reinit_frame_cache ();
986
987 ops->special_symbol_handling ();
988 }
989 }
990 }
991
992 /* Implement the "info sharedlibrary" command. Walk through the
993 shared library list and print information about each attached
994 library matching PATTERN. If PATTERN is elided, print them
995 all. */
996
997 static void
998 info_sharedlibrary_command (char *pattern, int from_tty)
999 {
1000 struct so_list *so = NULL; /* link map state variable */
1001 int so_missing_debug_info = 0;
1002 int addr_width;
1003 int nr_libs;
1004 struct cleanup *table_cleanup;
1005 struct gdbarch *gdbarch = target_gdbarch ();
1006 struct ui_out *uiout = current_uiout;
1007
1008 if (pattern)
1009 {
1010 char *re_err = re_comp (pattern);
1011
1012 if (re_err)
1013 error (_("Invalid regexp: %s"), re_err);
1014 }
1015
1016 /* "0x", a little whitespace, and two hex digits per byte of pointers. */
1017 addr_width = 4 + (gdbarch_ptr_bit (gdbarch) / 4);
1018
1019 update_solib_list (from_tty, 0);
1020
1021 /* make_cleanup_ui_out_table_begin_end needs to know the number of
1022 rows, so we need to make two passes over the libs. */
1023
1024 for (nr_libs = 0, so = so_list_head; so; so = so->next)
1025 {
1026 if (so->so_name[0])
1027 {
1028 if (pattern && ! re_exec (so->so_name))
1029 continue;
1030 ++nr_libs;
1031 }
1032 }
1033
1034 table_cleanup =
1035 make_cleanup_ui_out_table_begin_end (uiout, 4, nr_libs,
1036 "SharedLibraryTable");
1037
1038 /* The "- 1" is because ui_out adds one space between columns. */
1039 ui_out_table_header (uiout, addr_width - 1, ui_left, "from", "From");
1040 ui_out_table_header (uiout, addr_width - 1, ui_left, "to", "To");
1041 ui_out_table_header (uiout, 12 - 1, ui_left, "syms-read", "Syms Read");
1042 ui_out_table_header (uiout, 0, ui_noalign,
1043 "name", "Shared Object Library");
1044
1045 ui_out_table_body (uiout);
1046
1047 for (so = so_list_head; so; so = so->next)
1048 {
1049 struct cleanup *lib_cleanup;
1050
1051 if (! so->so_name[0])
1052 continue;
1053 if (pattern && ! re_exec (so->so_name))
1054 continue;
1055
1056 lib_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "lib");
1057
1058 if (so->addr_high != 0)
1059 {
1060 ui_out_field_core_addr (uiout, "from", gdbarch, so->addr_low);
1061 ui_out_field_core_addr (uiout, "to", gdbarch, so->addr_high);
1062 }
1063 else
1064 {
1065 ui_out_field_skip (uiout, "from");
1066 ui_out_field_skip (uiout, "to");
1067 }
1068
1069 if (! ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1070 && so->symbols_loaded
1071 && !objfile_has_symbols (so->objfile))
1072 {
1073 so_missing_debug_info = 1;
1074 ui_out_field_string (uiout, "syms-read", "Yes (*)");
1075 }
1076 else
1077 ui_out_field_string (uiout, "syms-read",
1078 so->symbols_loaded ? "Yes" : "No");
1079
1080 ui_out_field_string (uiout, "name", so->so_name);
1081
1082 ui_out_text (uiout, "\n");
1083
1084 do_cleanups (lib_cleanup);
1085 }
1086
1087 do_cleanups (table_cleanup);
1088
1089 if (nr_libs == 0)
1090 {
1091 if (pattern)
1092 ui_out_message (uiout, 0,
1093 _("No shared libraries matched.\n"));
1094 else
1095 ui_out_message (uiout, 0,
1096 _("No shared libraries loaded at this time.\n"));
1097 }
1098 else
1099 {
1100 if (so_missing_debug_info)
1101 ui_out_message (uiout, 0,
1102 _("(*): Shared library is missing "
1103 "debugging information.\n"));
1104 }
1105 }
1106
1107 /* Return 1 if ADDRESS lies within SOLIB. */
1108
1109 int
1110 solib_contains_address_p (const struct so_list *const solib,
1111 CORE_ADDR address)
1112 {
1113 struct target_section *p;
1114
1115 for (p = solib->sections; p < solib->sections_end; p++)
1116 if (p->addr <= address && address < p->endaddr)
1117 return 1;
1118
1119 return 0;
1120 }
1121
1122 /* If ADDRESS is in a shared lib in program space PSPACE, return its
1123 name.
1124
1125 Provides a hook for other gdb routines to discover whether or not a
1126 particular address is within the mapped address space of a shared
1127 library.
1128
1129 For example, this routine is called at one point to disable
1130 breakpoints which are in shared libraries that are not currently
1131 mapped in. */
1132
1133 char *
1134 solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
1135 {
1136 struct so_list *so = NULL;
1137
1138 for (so = pspace->so_list; so; so = so->next)
1139 if (solib_contains_address_p (so, address))
1140 return (so->so_name);
1141
1142 return (0);
1143 }
1144
1145 /* Return whether the data starting at VADDR, size SIZE, must be kept
1146 in a core file for shared libraries loaded before "gcore" is used
1147 to be handled correctly when the core file is loaded. This only
1148 applies when the section would otherwise not be kept in the core
1149 file (in particular, for readonly sections). */
1150
1151 int
1152 solib_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
1153 {
1154 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
1155
1156 if (ops->keep_data_in_core)
1157 return ops->keep_data_in_core (vaddr, size);
1158 else
1159 return 0;
1160 }
1161
1162 /* Called by free_all_symtabs */
1163
1164 void
1165 clear_solib (void)
1166 {
1167 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
1168
1169 /* This function is expected to handle ELF shared libraries. It is
1170 also used on Solaris, which can run either ELF or a.out binaries
1171 (for compatibility with SunOS 4), both of which can use shared
1172 libraries. So we don't know whether we have an ELF executable or
1173 an a.out executable until the user chooses an executable file.
1174
1175 ELF shared libraries don't get mapped into the address space
1176 until after the program starts, so we'd better not try to insert
1177 breakpoints in them immediately. We have to wait until the
1178 dynamic linker has loaded them; we'll hit a bp_shlib_event
1179 breakpoint (look for calls to create_solib_event_breakpoint) when
1180 it's ready.
1181
1182 SunOS shared libraries seem to be different --- they're present
1183 as soon as the process begins execution, so there's no need to
1184 put off inserting breakpoints. There's also nowhere to put a
1185 bp_shlib_event breakpoint, so if we put it off, we'll never get
1186 around to it.
1187
1188 So: disable breakpoints only if we're using ELF shared libs. */
1189 if (exec_bfd != NULL
1190 && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour)
1191 disable_breakpoints_in_shlibs ();
1192
1193 while (so_list_head)
1194 {
1195 struct so_list *so = so_list_head;
1196
1197 so_list_head = so->next;
1198 observer_notify_solib_unloaded (so);
1199 remove_target_sections (so);
1200 free_so (so);
1201 }
1202
1203 ops->clear_solib ();
1204 }
1205
1206 /* Shared library startup support. When GDB starts up the inferior,
1207 it nurses it along (through the shell) until it is ready to execute
1208 its first instruction. At this point, this function gets
1209 called. */
1210
1211 void
1212 solib_create_inferior_hook (int from_tty)
1213 {
1214 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
1215
1216 ops->solib_create_inferior_hook (from_tty);
1217 }
1218
1219 /* Check to see if an address is in the dynamic loader's dynamic
1220 symbol resolution code. Return 1 if so, 0 otherwise. */
1221
1222 int
1223 in_solib_dynsym_resolve_code (CORE_ADDR pc)
1224 {
1225 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
1226
1227 return ops->in_dynsym_resolve_code (pc);
1228 }
1229
1230 /* Implements the "sharedlibrary" command. */
1231
1232 static void
1233 sharedlibrary_command (char *args, int from_tty)
1234 {
1235 dont_repeat ();
1236 solib_add (args, from_tty, (struct target_ops *) 0, 1);
1237 }
1238
1239 /* Implements the command "nosharedlibrary", which discards symbols
1240 that have been auto-loaded from shared libraries. Symbols from
1241 shared libraries that were added by explicit request of the user
1242 are not discarded. Also called from remote.c. */
1243
1244 void
1245 no_shared_libraries (char *ignored, int from_tty)
1246 {
1247 /* The order of the two routines below is important: clear_solib notifies
1248 the solib_unloaded observers, and some of these observers might need
1249 access to their associated objfiles. Therefore, we can not purge the
1250 solibs' objfiles before clear_solib has been called. */
1251
1252 clear_solib ();
1253 objfile_purge_solibs ();
1254 }
1255
1256 /* See solib.h. */
1257
1258 void
1259 update_solib_breakpoints (void)
1260 {
1261 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
1262
1263 if (ops->update_breakpoints != NULL)
1264 ops->update_breakpoints ();
1265 }
1266
1267 /* See solib.h. */
1268
1269 void
1270 handle_solib_event (void)
1271 {
1272 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
1273
1274 if (ops->handle_event != NULL)
1275 ops->handle_event ();
1276
1277 clear_program_space_solib_cache (current_inferior ()->pspace);
1278
1279 /* Check for any newly added shared libraries if we're supposed to
1280 be adding them automatically. Switch terminal for any messages
1281 produced by breakpoint_re_set. */
1282 target_terminal_ours_for_output ();
1283 solib_add (NULL, 0, &current_target, auto_solib_add);
1284 target_terminal_inferior ();
1285 }
1286
1287 /* Reload shared libraries, but avoid reloading the same symbol file
1288 we already have loaded. */
1289
1290 static void
1291 reload_shared_libraries_1 (int from_tty)
1292 {
1293 struct so_list *so;
1294 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
1295
1296 if (print_symbol_loading_p (from_tty, 0, 0))
1297 printf_unfiltered (_("Loading symbols for shared libraries.\n"));
1298
1299 for (so = so_list_head; so != NULL; so = so->next)
1300 {
1301 char *filename, *found_pathname = NULL;
1302 bfd *abfd;
1303 int was_loaded = so->symbols_loaded;
1304 const int flags =
1305 SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);
1306
1307 filename = tilde_expand (so->so_original_name);
1308 make_cleanup (xfree, filename);
1309 abfd = solib_bfd_open (filename);
1310 if (abfd != NULL)
1311 {
1312 found_pathname = xstrdup (bfd_get_filename (abfd));
1313 make_cleanup (xfree, found_pathname);
1314 gdb_bfd_unref (abfd);
1315 }
1316
1317 /* If this shared library is no longer associated with its previous
1318 symbol file, close that. */
1319 if ((found_pathname == NULL && was_loaded)
1320 || (found_pathname != NULL
1321 && filename_cmp (found_pathname, so->so_name) != 0))
1322 {
1323 if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
1324 && !solib_used (so))
1325 free_objfile (so->objfile);
1326 remove_target_sections (so);
1327 clear_so (so);
1328 }
1329
1330 /* If this shared library is now associated with a new symbol
1331 file, open it. */
1332 if (found_pathname != NULL
1333 && (!was_loaded
1334 || filename_cmp (found_pathname, so->so_name) != 0))
1335 {
1336 int got_error = 0;
1337
1338 TRY
1339 {
1340 solib_map_sections (so);
1341 }
1342
1343 CATCH (e, RETURN_MASK_ERROR)
1344 {
1345 exception_fprintf (gdb_stderr, e,
1346 _("Error while mapping "
1347 "shared library sections:\n"));
1348 got_error = 1;
1349 }
1350 END_CATCH
1351
1352 if (!got_error
1353 && (auto_solib_add || was_loaded || libpthread_solib_p (so)))
1354 solib_read_symbols (so, flags);
1355 }
1356 }
1357
1358 do_cleanups (old_chain);
1359 }
1360
1361 static void
1362 reload_shared_libraries (char *ignored, int from_tty,
1363 struct cmd_list_element *e)
1364 {
1365 const struct target_so_ops *ops;
1366
1367 reload_shared_libraries_1 (from_tty);
1368
1369 ops = solib_ops (target_gdbarch ());
1370
1371 /* Creating inferior hooks here has two purposes. First, if we reload
1372 shared libraries then the address of solib breakpoint we've computed
1373 previously might be no longer valid. For example, if we forgot to set
1374 solib-absolute-prefix and are setting it right now, then the previous
1375 breakpoint address is plain wrong. Second, installing solib hooks
1376 also implicitly figures were ld.so is and loads symbols for it.
1377 Absent this call, if we've just connected to a target and set
1378 solib-absolute-prefix or solib-search-path, we'll lose all information
1379 about ld.so. */
1380 if (target_has_execution)
1381 {
1382 /* Reset or free private data structures not associated with
1383 so_list entries. */
1384 ops->clear_solib ();
1385
1386 /* Remove any previous solib event breakpoint. This is usually
1387 done in common code, at breakpoint_init_inferior time, but
1388 we're not really starting up the inferior here. */
1389 remove_solib_event_breakpoints ();
1390
1391 solib_create_inferior_hook (from_tty);
1392 }
1393
1394 /* Sometimes the platform-specific hook loads initial shared
1395 libraries, and sometimes it doesn't. If it doesn't FROM_TTY will be
1396 incorrectly 0 but such solib targets should be fixed anyway. If we
1397 made all the inferior hook methods consistent, this call could be
1398 removed. Call it only after the solib target has been initialized by
1399 solib_create_inferior_hook. */
1400
1401 solib_add (NULL, 0, NULL, auto_solib_add);
1402
1403 breakpoint_re_set ();
1404
1405 /* We may have loaded or unloaded debug info for some (or all)
1406 shared libraries. However, frames may still reference them. For
1407 example, a frame's unwinder might still point at DWARF FDE
1408 structures that are now freed. Also, getting new symbols may
1409 change our opinion about what is frameless. */
1410 reinit_frame_cache ();
1411
1412 ops->special_symbol_handling ();
1413 }
1414
1415 /* Wrapper for reload_shared_libraries that replaces "remote:"
1416 at the start of gdb_sysroot with "target:". */
1417
1418 static void
1419 gdb_sysroot_changed (char *ignored, int from_tty,
1420 struct cmd_list_element *e)
1421 {
1422 const char *old_prefix = "remote:";
1423 const char *new_prefix = TARGET_SYSROOT_PREFIX;
1424
1425 if (startswith (gdb_sysroot, old_prefix))
1426 {
1427 static int warning_issued = 0;
1428
1429 gdb_assert (strlen (old_prefix) == strlen (new_prefix));
1430 memcpy (gdb_sysroot, new_prefix, strlen (new_prefix));
1431
1432 if (!warning_issued)
1433 {
1434 warning (_("\"%s\" is deprecated, use \"%s\" instead."),
1435 old_prefix, new_prefix);
1436 warning (_("sysroot set to \"%s\"."), gdb_sysroot);
1437
1438 warning_issued = 1;
1439 }
1440 }
1441
1442 reload_shared_libraries (ignored, from_tty, e);
1443 }
1444
1445 static void
1446 show_auto_solib_add (struct ui_file *file, int from_tty,
1447 struct cmd_list_element *c, const char *value)
1448 {
1449 fprintf_filtered (file, _("Autoloading of shared library symbols is %s.\n"),
1450 value);
1451 }
1452
1453
1454 /* Handler for library-specific lookup of global symbol NAME in OBJFILE. Call
1455 the library-specific handler if it is installed for the current target. */
1456
1457 struct symbol *
1458 solib_global_lookup (struct objfile *objfile,
1459 const char *name,
1460 const domain_enum domain)
1461 {
1462 const struct target_so_ops *ops = solib_ops (get_objfile_arch (objfile));
1463
1464 if (ops->lookup_lib_global_symbol != NULL)
1465 return ops->lookup_lib_global_symbol (objfile, name, domain);
1466 return NULL;
1467 }
1468
1469 /* Lookup the value for a specific symbol from dynamic symbol table. Look
1470 up symbol from ABFD. MATCH_SYM is a callback function to determine
1471 whether to pick up a symbol. DATA is the input of this callback
1472 function. Return NULL if symbol is not found. */
1473
1474 CORE_ADDR
1475 gdb_bfd_lookup_symbol_from_symtab (bfd *abfd,
1476 int (*match_sym) (asymbol *, void *),
1477 void *data)
1478 {
1479 long storage_needed = bfd_get_symtab_upper_bound (abfd);
1480 CORE_ADDR symaddr = 0;
1481
1482 if (storage_needed > 0)
1483 {
1484 unsigned int i;
1485
1486 asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
1487 struct cleanup *back_to = make_cleanup (xfree, symbol_table);
1488 unsigned int number_of_symbols =
1489 bfd_canonicalize_symtab (abfd, symbol_table);
1490
1491 for (i = 0; i < number_of_symbols; i++)
1492 {
1493 asymbol *sym = *symbol_table++;
1494
1495 if (match_sym (sym, data))
1496 {
1497 struct gdbarch *gdbarch = target_gdbarch ();
1498 symaddr = sym->value;
1499
1500 /* Some ELF targets fiddle with addresses of symbols they
1501 consider special. They use minimal symbols to do that
1502 and this is needed for correct breakpoint placement,
1503 but we do not have full data here to build a complete
1504 minimal symbol, so just set the address and let the
1505 targets cope with that. */
1506 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1507 && gdbarch_elf_make_msymbol_special_p (gdbarch))
1508 {
1509 struct minimal_symbol msym;
1510
1511 memset (&msym, 0, sizeof (msym));
1512 SET_MSYMBOL_VALUE_ADDRESS (&msym, symaddr);
1513 gdbarch_elf_make_msymbol_special (gdbarch, sym, &msym);
1514 symaddr = MSYMBOL_VALUE_RAW_ADDRESS (&msym);
1515 }
1516
1517 /* BFD symbols are section relative. */
1518 symaddr += sym->section->vma;
1519 break;
1520 }
1521 }
1522 do_cleanups (back_to);
1523 }
1524
1525 return symaddr;
1526 }
1527
1528 /* Lookup the value for a specific symbol from symbol table. Look up symbol
1529 from ABFD. MATCH_SYM is a callback function to determine whether to pick
1530 up a symbol. DATA is the input of this callback function. Return NULL
1531 if symbol is not found. */
1532
1533 static CORE_ADDR
1534 bfd_lookup_symbol_from_dyn_symtab (bfd *abfd,
1535 int (*match_sym) (asymbol *, void *),
1536 void *data)
1537 {
1538 long storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
1539 CORE_ADDR symaddr = 0;
1540
1541 if (storage_needed > 0)
1542 {
1543 unsigned int i;
1544 asymbol **symbol_table = (asymbol **) xmalloc (storage_needed);
1545 struct cleanup *back_to = make_cleanup (xfree, symbol_table);
1546 unsigned int number_of_symbols =
1547 bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
1548
1549 for (i = 0; i < number_of_symbols; i++)
1550 {
1551 asymbol *sym = *symbol_table++;
1552
1553 if (match_sym (sym, data))
1554 {
1555 /* BFD symbols are section relative. */
1556 symaddr = sym->value + sym->section->vma;
1557 break;
1558 }
1559 }
1560 do_cleanups (back_to);
1561 }
1562 return symaddr;
1563 }
1564
1565 /* Lookup the value for a specific symbol from symbol table and dynamic
1566 symbol table. Look up symbol from ABFD. MATCH_SYM is a callback
1567 function to determine whether to pick up a symbol. DATA is the
1568 input of this callback function. Return NULL if symbol is not
1569 found. */
1570
1571 CORE_ADDR
1572 gdb_bfd_lookup_symbol (bfd *abfd,
1573 int (*match_sym) (asymbol *, void *),
1574 void *data)
1575 {
1576 CORE_ADDR symaddr = gdb_bfd_lookup_symbol_from_symtab (abfd, match_sym, data);
1577
1578 /* On FreeBSD, the dynamic linker is stripped by default. So we'll
1579 have to check the dynamic string table too. */
1580 if (symaddr == 0)
1581 symaddr = bfd_lookup_symbol_from_dyn_symtab (abfd, match_sym, data);
1582
1583 return symaddr;
1584 }
1585
1586 /* SO_LIST_HEAD may contain user-loaded object files that can be removed
1587 out-of-band by the user. So upon notification of free_objfile remove
1588 all references to any user-loaded file that is about to be freed. */
1589
1590 static void
1591 remove_user_added_objfile (struct objfile *objfile)
1592 {
1593 struct so_list *so;
1594
1595 if (objfile != 0 && objfile->flags & OBJF_USERLOADED)
1596 {
1597 for (so = so_list_head; so != NULL; so = so->next)
1598 if (so->objfile == objfile)
1599 so->objfile = NULL;
1600 }
1601 }
1602
1603 extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
1604
1605 void
1606 _initialize_solib (void)
1607 {
1608 solib_data = gdbarch_data_register_pre_init (solib_init);
1609
1610 observer_attach_free_objfile (remove_user_added_objfile);
1611
1612 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1613 _("Load shared object library symbols for files matching REGEXP."));
1614 add_info ("sharedlibrary", info_sharedlibrary_command,
1615 _("Status of loaded shared object libraries."));
1616 add_com ("nosharedlibrary", class_files, no_shared_libraries,
1617 _("Unload all shared object library symbols."));
1618
1619 add_setshow_boolean_cmd ("auto-solib-add", class_support,
1620 &auto_solib_add, _("\
1621 Set autoloading of shared library symbols."), _("\
1622 Show autoloading of shared library symbols."), _("\
1623 If \"on\", symbols from all shared object libraries will be loaded\n\
1624 automatically when the inferior begins execution, when the dynamic linker\n\
1625 informs gdb that a new library has been loaded, or when attaching to the\n\
1626 inferior. Otherwise, symbols must be loaded manually, using \
1627 `sharedlibrary'."),
1628 NULL,
1629 show_auto_solib_add,
1630 &setlist, &showlist);
1631
1632 add_setshow_optional_filename_cmd ("sysroot", class_support,
1633 &gdb_sysroot, _("\
1634 Set an alternate system root."), _("\
1635 Show the current system root."), _("\
1636 The system root is used to load absolute shared library symbol files.\n\
1637 For other (relative) files, you can add directories using\n\
1638 `set solib-search-path'."),
1639 gdb_sysroot_changed,
1640 NULL,
1641 &setlist, &showlist);
1642
1643 add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1644 &setlist);
1645 add_alias_cmd ("solib-absolute-prefix", "sysroot", class_support, 0,
1646 &showlist);
1647
1648 add_setshow_optional_filename_cmd ("solib-search-path", class_support,
1649 &solib_search_path, _("\
1650 Set the search path for loading non-absolute shared library symbol files."),
1651 _("\
1652 Show the search path for loading non-absolute shared library symbol files."),
1653 _("\
1654 This takes precedence over the environment variables \
1655 PATH and LD_LIBRARY_PATH."),
1656 reload_shared_libraries,
1657 show_solib_search_path,
1658 &setlist, &showlist);
1659 }
This page took 0.064905 seconds and 4 git commands to generate.