Strip "target:" prefix in solib_find if accessing local files
[deliverable/binutils-gdb.git] / gdb / solib.c
CommitLineData
13437d4b 1/* Handle shared libraries for GDB, the GNU Debugger.
14a5e767 2
32d0add0 3 Copyright (C) 1990-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
c906108c
SS
20#include "defs.h"
21
c906108c 22#include <sys/types.h>
c906108c 23#include <fcntl.h>
c906108c
SS
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"
88987551 32#include "gdb_regex.h"
c906108c
SS
33#include "inferior.h"
34#include "environ.h"
35#include "language.h"
36#include "gdbcmd.h"
fa58ee11 37#include "completer.h"
fe4e3eb8 38#include "filenames.h" /* for DOSish file names */
4646aa9d 39#include "exec.h"
13437d4b 40#include "solist.h"
84acb35a 41#include "observer.h"
dbda9972 42#include "readline/readline.h"
f1838a98 43#include "remote.h"
2c0b251b 44#include "solib.h"
55333a84 45#include "interps.h"
ab38a727 46#include "filesystem.h"
cbb099e8 47#include "gdb_bfd.h"
614c279d 48#include "filestuff.h"
c906108c 49
66aba65d
MK
50/* Architecture-specific operations. */
51
52/* Per-architecture data key. */
53static struct gdbarch_data *solib_data;
54
55static void *
56solib_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
3641da11 65static const struct target_so_ops *
66aba65d
MK
66solib_ops (struct gdbarch *gdbarch)
67{
3641da11 68 const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
433759f7 69
66aba65d
MK
70 return *ops;
71}
7d522c90
DJ
72
73/* Set the solib operations for GDBARCH to NEW_OPS. */
74
75void
3641da11 76set_solib_ops (struct gdbarch *gdbarch, const struct target_so_ops *new_ops)
7d522c90 77{
3641da11 78 const struct target_so_ops **ops = gdbarch_data (gdbarch, solib_data);
433759f7 79
7d522c90
DJ
80 *ops = new_ops;
81}
66aba65d
MK
82\f
83
13437d4b 84/* external data declarations */
c906108c 85
7d522c90
DJ
86/* FIXME: gdbarch needs to control this variable, or else every
87 configuration needs to call set_solib_ops. */
13437d4b 88struct target_so_ops *current_target_so_ops;
23e04971 89
6c95b8df
PA
90/* List of known shared objects */
91#define so_list_head current_program_space->so_list
23e04971 92
c906108c
SS
93/* Local function prototypes */
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{
3e43a32a
MS
103 fprintf_filtered (file, _("The search path for loading non-absolute "
104 "shared library symbol files is %s.\n"),
920d2a44
AC
105 value);
106}
c906108c 107
ab38a727
PA
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
7f86f058
PA
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.
e4f7b8c8 119
f822c95b 120 Global variable GDB_SYSROOT is used as a prefix directory
e4f7b8c8 121 to search for shared libraries if they have an absolute path.
b57fbfba
GB
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.
e4f7b8c8
MS
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
b57fbfba
GB
130 shared libraries if not found in either the sysroot (if set) or
131 the local filesystem.
e4f7b8c8 132
c8c18e65 133 Search algorithm:
b57fbfba
GB
134 * If a sysroot is set and path is absolute:
135 * Search for sysroot/path.
c8c18e65
KW
136 * else
137 * Look for it literally (unmodified).
e4f7b8c8 138 * Look in SOLIB_SEARCH_PATH.
f43caff8 139 * If available, use target defined search function.
b57fbfba 140 * If NO sysroot is set, perform the following two searches:
c8c18e65
KW
141 * Look in inferior's $PATH.
142 * Look in inferior's $LD_LIBRARY_PATH.
ab38a727 143 *
c8c18e65 144 * The last check avoids doing this search when targetting remote
b57fbfba 145 * machines since a sysroot will almost always be set.
7f86f058 146*/
e4f7b8c8 147
572d275c
UW
148char *
149solib_find (char *in_pathname, int *fd)
e4f7b8c8 150{
3641da11 151 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
e4f7b8c8
MS
152 int found_file = -1;
153 char *temp_pathname = NULL;
08105857 154 const char *solib_symbols_extension
f5656ead 155 = gdbarch_solib_symbols_extension (target_gdbarch ());
ab38a727
PA
156 const char *fskind = effective_target_file_system_kind ();
157 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
b57fbfba 158 char *sysroot = gdb_sysroot;
08105857
PA
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);
433759f7 165
08105857
PA
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 }
58dc52c3 182
b57fbfba
GB
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 }
e4f7b8c8 198
b57fbfba 199 if (sysroot != NULL)
e4f7b8c8 200 {
b57fbfba 201 int prefix_len = strlen (sysroot);
f1d10cfb
AS
202
203 /* Remove trailing slashes from absolute prefix. */
204 while (prefix_len > 0
b57fbfba 205 && IS_DIR_SEPARATOR (sysroot[prefix_len - 1]))
f1d10cfb
AS
206 prefix_len--;
207
b57fbfba 208 sysroot = savestring (sysroot, prefix_len);
ab38a727
PA
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
b57fbfba 244 if (!IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname) || sysroot == NULL)
ab38a727
PA
245 temp_pathname = xstrdup (in_pathname);
246 else
247 {
248 int need_dir_separator;
249
3736004f
PA
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 |
2938e6cf
GB
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 |
3736004f
PA
262
263 IOW, we don't need to add a separator if IN_PATHNAME already
2938e6cf 264 has one, or when the the sysroot is exactly "target:".
3736004f
PA
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])
2938e6cf 268 || strcmp (TARGET_SYSROOT_PREFIX, sysroot) == 0);
ab38a727 269
f1d10cfb 270 /* Cat the prefixed pathname together. */
ab38a727
PA
271 temp_pathname = concat (sysroot,
272 need_dir_separator ? SLASH_STRING : "",
273 in_pathname, (char *) NULL);
e4f7b8c8
MS
274 }
275
2938e6cf
GB
276 /* Handle files to be accessed via the target. */
277 if (is_target_filename (temp_pathname))
f1838a98 278 {
572d275c 279 *fd = -1;
f748fb40 280 do_cleanups (old_chain);
ab38a727 281 return temp_pathname;
f1838a98
UW
282 }
283
f1d10cfb 284 /* Now see if we can open it. */
614c279d 285 found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
ab38a727
PA
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. */
f1d10cfb 293
ab38a727 294 if (found_file < 0
b57fbfba 295 && sysroot != NULL
ab38a727
PA
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
614c279d 308 found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
ab38a727
PA
309 if (found_file < 0)
310 {
ab38a727
PA
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
614c279d 323 found_file = gdb_open_cloexec (temp_pathname, O_RDONLY | O_BINARY, 0);
ab38a727
PA
324 if (found_file < 0)
325 xfree (temp_pathname);
326 }
327 }
0997b535 328
ab38a727
PA
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
f822c95b 339 /* If the search in gdb_sysroot failed, and the path name is
ba5f0d88
OF
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. */
ab38a727 343 if (found_file < 0 && IS_TARGET_ABSOLUTE_PATH (fskind, in_pathname))
ba5f0d88
OF
344 {
345 /* First, get rid of any drive letters etc. */
ab38a727
PA
346 while (!IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
347 in_pathname++;
ba5f0d88
OF
348
349 /* Next, get rid of all leading dir separators. */
ab38a727
PA
350 while (IS_TARGET_DIR_SEPARATOR (fskind, *in_pathname))
351 in_pathname++;
ba5f0d88 352 }
ab38a727 353
c8c18e65 354 /* If not found, search the solib_search_path (if any). */
e4f7b8c8 355 if (found_file < 0 && solib_search_path != NULL)
492c0ab7
JK
356 found_file = openp (solib_search_path,
357 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
fbdebf46 358 in_pathname, O_RDONLY | O_BINARY, &temp_pathname);
ab38a727 359
ba5f0d88
OF
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)
492c0ab7
JK
364 found_file = openp (solib_search_path,
365 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
ab38a727
PA
366 target_lbasename (fskind, in_pathname),
367 O_RDONLY | O_BINARY, &temp_pathname);
e4f7b8c8 368
c378eb4e 369 /* If not found, try to use target supplied solib search method. */
66aba65d 370 if (found_file < 0 && ops->find_and_open_solib)
637d6690 371 found_file = ops->find_and_open_solib (in_pathname, O_RDONLY | O_BINARY,
66aba65d 372 &temp_pathname);
2610b0bf 373
c378eb4e 374 /* If not found, next search the inferior's $PATH environment variable. */
b57fbfba 375 if (found_file < 0 && sysroot == NULL)
3f81c18a
VP
376 found_file = openp (get_in_environ (current_inferior ()->environment,
377 "PATH"),
492c0ab7
JK
378 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
379 O_RDONLY | O_BINARY, &temp_pathname);
e4f7b8c8 380
ab38a727 381 /* If not found, next search the inferior's $LD_LIBRARY_PATH
c378eb4e 382 environment variable. */
b57fbfba 383 if (found_file < 0 && sysroot == NULL)
3f81c18a
VP
384 found_file = openp (get_in_environ (current_inferior ()->environment,
385 "LD_LIBRARY_PATH"),
492c0ab7
JK
386 OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, in_pathname,
387 O_RDONLY | O_BINARY, &temp_pathname);
e4f7b8c8 388
572d275c
UW
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
a4453b7e
TT
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). */
572d275c
UW
400
401bfd *
402solib_bfd_fopen (char *pathname, int fd)
403{
2938e6cf 404 bfd *abfd = gdb_bfd_open (pathname, gnutarget, fd);
572d275c 405
2938e6cf
GB
406 if (abfd != NULL && !gdb_bfd_has_target_filename (abfd))
407 bfd_set_cacheable (abfd, 1);
f1838a98 408
f1838a98
UW
409 if (!abfd)
410 {
572d275c 411 make_cleanup (xfree, pathname);
f1838a98 412 error (_("Could not open `%s' as an executable file: %s"),
572d275c 413 pathname, bfd_errmsg (bfd_get_error ()));
f1838a98
UW
414 }
415
a4453b7e
TT
416 xfree (pathname);
417
520b0001 418 return abfd;
572d275c
UW
419}
420
421/* Find shared library PATHNAME and open a BFD for it. */
422
423bfd *
424solib_bfd_open (char *pathname)
425{
572d275c
UW
426 char *found_pathname;
427 int found_file;
428 bfd *abfd;
378d2b72 429 const struct bfd_arch_info *b;
572d275c 430
572d275c
UW
431 /* Search for shared library file. */
432 found_pathname = solib_find (pathname, &found_file);
433 if (found_pathname == NULL)
048d532d
PA
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 }
572d275c
UW
442
443 /* Open bfd for shared library. */
444 abfd = solib_bfd_fopen (found_pathname, found_file);
445
446 /* Check bfd format. */
f1838a98 447 if (!bfd_check_format (abfd, bfd_object))
0997b535 448 {
f9a062ff 449 make_cleanup_bfd_unref (abfd);
f1838a98 450 error (_("`%s': not in executable format: %s"),
a4453b7e 451 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
0997b535 452 }
f1838a98 453
378d2b72 454 /* Check bfd arch. */
f5656ead 455 b = gdbarch_bfd_arch_info (target_gdbarch ());
69e2bf17 456 if (!b->compatible (b, bfd_get_arch_info (abfd)))
378d2b72 457 warning (_("`%s': Shared library architecture %s is not compatible "
a4453b7e 458 "with target architecture %s."), bfd_get_filename (abfd),
378d2b72
HZ
459 bfd_get_arch_info (abfd)->printable_name, b->printable_name);
460
f1838a98 461 return abfd;
e4f7b8c8
MS
462}
463
7f86f058
PA
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.
e4f7b8c8 469
7f86f058
PA
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
c5aa993b
JM
472 cases where it isn't, do we really mimic the systems search
473 mechanism correctly in the below code (particularly the tilde
7f86f058 474 expansion stuff?). */
c906108c
SS
475
476static int
048d532d 477solib_map_sections (struct so_list *so)
c906108c 478{
3641da11 479 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
c906108c 480 char *filename;
0542c86d 481 struct target_section *p;
c906108c
SS
482 struct cleanup *old_chain;
483 bfd *abfd;
c5aa993b
JM
484
485 filename = tilde_expand (so->so_name);
b8c9b27d 486 old_chain = make_cleanup (xfree, filename);
831a0c44 487 abfd = ops->bfd_open (filename);
f1838a98 488 do_cleanups (old_chain);
e4f7b8c8 489
048d532d
PA
490 if (abfd == NULL)
491 return 0;
492
13437d4b 493 /* Leave bfd open, core_xfer_memory and "info files" need it. */
cbb099e8 494 so->abfd = abfd;
23e04971 495
b030cf11
JB
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
13437d4b 506 if (build_section_table (abfd, &so->sections, &so->sections_end))
23e04971 507 {
8a3fe4f8 508 error (_("Can't find the file sections in `%s': %s"),
13437d4b 509 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
23e04971 510 }
104c1213 511
13437d4b 512 for (p = so->sections; p < so->sections_end; p++)
104c1213 513 {
13437d4b
KB
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
c378eb4e 516 mapped. */
66aba65d 517 ops->relocate_section_addresses (so, p);
cfa9d6d9
DJ
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)
13437d4b 524 {
cfa9d6d9
DJ
525 so->addr_low = p->addr;
526 so->addr_high = p->endaddr;
13437d4b 527 }
104c1213
JM
528 }
529
048d532d
PA
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. */
ed9eebaf 534 add_target_sections (so, so->sections, so->sections_end);
048d532d
PA
535
536 return 1;
104c1213 537}
c906108c 538
0892cb63
DE
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.
a86988f2
PA
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
548static void
0892cb63 549clear_so (struct so_list *so)
a86988f2 550{
3641da11 551 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
0892cb63 552
a86988f2
PA
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);
0892cb63
DE
571
572 /* Do the same for target-specific data. */
573 if (ops->clear_so != NULL)
574 ops->clear_so (so);
a86988f2
PA
575}
576
7f86f058 577/* Free the storage associated with the `struct so_list' object SO.
c378eb4e 578 If we have opened a BFD for SO, close it.
c906108c 579
07cd4b97
JB
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.
c906108c 583
07cd4b97
JB
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
13437d4b 588void
07cd4b97 589free_so (struct so_list *so)
c906108c 590{
3641da11 591 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
c5aa993b 592
0892cb63 593 clear_so (so);
66aba65d 594 ops->free_so (so);
07cd4b97 595
b8c9b27d 596 xfree (so);
c906108c
SS
597}
598
07cd4b97 599
f8766ec1
KB
600/* Return address of first so_list entry in master shared object list. */
601struct so_list *
602master_so_list (void)
603{
604 return so_list_head;
605}
606
7eedccfa
PP
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
42a6e6a0
MK
609 loaded. */
610
611int
7eedccfa 612solib_read_symbols (struct so_list *so, int flags)
42a6e6a0
MK
613{
614 if (so->symbols_loaded)
615 {
a86988f2 616 /* If needed, we've already warned in our caller. */
42a6e6a0 617 }
8bb75286
DJ
618 else if (so->abfd == NULL)
619 {
048d532d
PA
620 /* We've already warned about this library, when trying to open
621 it. */
8bb75286 622 }
42a6e6a0
MK
623 else
624 {
048d532d 625
7dcd53a0
TT
626 flags |= current_inferior ()->symfile_flags;
627
492d29ea 628 TRY
048d532d
PA
629 {
630 struct section_addr_info *sap;
631
632 /* Have we already loaded this shared object? */
633 ALL_OBJFILES (so->objfile)
634 {
4262abfb 635 if (filename_cmp (objfile_name (so->objfile), so->so_name) == 0
e4f6d2ec 636 && so->objfile->addr_low == so->addr_low)
048d532d
PA
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);
24ba069a 644 so->objfile = symbol_file_add_from_bfd (so->abfd, so->so_name,
63524580
JK
645 flags, sap, OBJF_SHARED,
646 NULL);
e4f6d2ec 647 so->objfile->addr_low = so->addr_low;
048d532d 648 free_section_addr_info (sap);
492d29ea
PA
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);
048d532d 657 }
492d29ea 658 END_CATCH
048d532d 659
7eedccfa 660 return 1;
42a6e6a0
MK
661 }
662
663 return 0;
664}
c906108c 665
59145f8c
AR
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
669static int
670solib_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
7f86f058 680/* Synchronize GDB's shared object list with inferior's.
c906108c 681
07cd4b97 682 Extract the list of currently loaded shared objects from the
105b175f
JB
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.
c906108c 686
105b175f
JB
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.
07cd4b97
JB
692
693 If FROM_TTY is non-null, feel free to print messages about what
694 we're doing.
c906108c 695
07cd4b97
JB
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. */
c906108c 702
a78f21af 703static void
105b175f 704update_solib_list (int from_tty, struct target_ops *target)
07cd4b97 705{
3641da11 706 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
66aba65d 707 struct so_list *inferior = ops->current_sos();
07cd4b97
JB
708 struct so_list *gdb, **gdb_link;
709
181e7f93
PA
710 /* We can reach here due to changing solib-search-path or the
711 sysroot, before having any inferior. */
50c71eaf 712 if (target_has_execution && !ptid_equal (inferior_ptid, null_ptid))
181e7f93
PA
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 }
104c1213 724
07cd4b97
JB
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
105b175f
JB
731 knows about it already, and it's still loaded in the inferior.
732 Nothing needs to happen.
07cd4b97
JB
733
734 - A shared object appears only on GDB's list. This means that
105b175f
JB
735 the inferior has unloaded it. We should remove the shared
736 object from GDB's tables.
07cd4b97
JB
737
738 - A shared object appears only on the inferior's list. This
105b175f
JB
739 means that it's just been loaded. We should add it to GDB's
740 tables.
07cd4b97
JB
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)
c906108c 752 {
07cd4b97
JB
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)
c906108c 759 {
a7c02bc8
VP
760 if (ops->same)
761 {
762 if (ops->same (gdb, i))
763 break;
764 }
765 else
766 {
0ba1096a 767 if (! filename_cmp (gdb->so_original_name, i->so_original_name))
a7c02bc8
VP
768 break;
769 }
07cd4b97
JB
770
771 i_link = &i->next;
772 i = *i_link;
c906108c 773 }
c5aa993b 774
07cd4b97
JB
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)
c906108c 779 {
07cd4b97 780 *i_link = i->next;
07cd4b97
JB
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 {
42a6e6a0
MK
789 /* Notify any observer that the shared object has been
790 unloaded before we remove it from GDB's tables. */
84acb35a
JJ
791 observer_notify_solib_unloaded (gdb);
792
edcc5120
TT
793 VEC_safe_push (char_ptr, current_program_space->deleted_solibs,
794 xstrdup (gdb->so_name));
795
07cd4b97 796 *gdb_link = gdb->next;
07cd4b97
JB
797
798 /* Unless the user loaded it explicitly, free SO's objfile. */
59145f8c
AR
799 if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)
800 && !solib_used (gdb))
07cd4b97
JB
801 free_objfile (gdb->objfile);
802
803 /* Some targets' section tables might be referring to
804 sections from so->abfd; remove them. */
046ac79f 805 remove_target_sections (gdb);
07cd4b97
JB
806
807 free_so (gdb);
808 gdb = *gdb_link;
c906108c
SS
809 }
810 }
c5aa993b 811
07cd4b97
JB
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
e8930304 814 to GDB's shared object list. */
07cd4b97 815 if (inferior)
c906108c 816 {
048d532d
PA
817 int not_found = 0;
818 const char *not_found_filename = NULL;
819
07cd4b97
JB
820 struct so_list *i;
821
822 /* Add the new shared objects to GDB's list. */
823 *gdb_link = inferior;
824
e8930304 825 /* Fill in the rest of each of the `struct so_list' nodes. */
07cd4b97 826 for (i = inferior; i; i = i->next)
c906108c 827 {
048d532d 828
6c95b8df 829 i->pspace = current_program_space;
edcc5120 830 VEC_safe_push (so_list_ptr, current_program_space->added_solibs, i);
07cd4b97 831
492d29ea 832 TRY
048d532d
PA
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 }
07cd4b97 842
492d29ea
PA
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
42a6e6a0
MK
850
851 /* Notify any observer that the shared object has been
048d532d 852 loaded now that we've added it to GDB's tables. */
42a6e6a0 853 observer_notify_solib_loaded (i);
c906108c 854 }
048d532d
PA
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)
3e43a32a
MS
864 warning (_("Could not load shared library symbols for %s.\n"
865 "Do you need \"set solib-search-path\" "
866 "or \"set sysroot\"?"),
048d532d
PA
867 not_found_filename);
868 else if (not_found > 1)
869 warning (_("\
870Could not load shared library symbols for %d libraries, e.g. %s.\n\
871Use the \"info sharedlibrary\" command to see the complete listing.\n\
872Do you need \"set solib-search-path\" or \"set sysroot\"?"),
873 not_found, not_found_filename);
e8930304 874 }
105b175f
JB
875}
876
17a37d48
PP
877
878/* Return non-zero if NAME is the libpthread shared library.
6612ad7f
JB
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
17a37d48
PP
884int
885libpthread_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
6612ad7f
JB
892static int
893libpthread_solib_p (struct so_list *so)
894{
17a37d48 895 return libpthread_name_p (so->so_name);
6612ad7f 896}
105b175f 897
7f86f058 898/* Read in symbolic information for any shared objects whose names
105b175f
JB
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
990f9fe3
FF
902 If READSYMS is 0, defer reading symbolic information until later
903 but still do any needed low level processing.
904
105b175f
JB
905 FROM_TTY and TARGET are as described for update_solib_list, above. */
906
907void
414842dc 908solib_add (const char *pattern, int from_tty,
3e43a32a 909 struct target_ops *target, int readsyms)
105b175f
JB
910{
911 struct so_list *gdb;
912
770e7fc7
DE
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
2eff07b3
PP
924 current_program_space->solib_add_generation++;
925
105b175f
JB
926 if (pattern)
927 {
928 char *re_err = re_comp (pattern);
929
930 if (re_err)
8a3fe4f8 931 error (_("Invalid regexp: %s"), re_err);
105b175f
JB
932 }
933
934 update_solib_list (from_tty, target);
c906108c 935
105b175f
JB
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. */
e8930304
JB
939 {
940 int any_matches = 0;
941 int loaded_any_symbols = 0;
7eedccfa
PP
942 const int flags =
943 SYMFILE_DEFER_BP_RESET | (from_tty ? SYMFILE_VERBOSE : 0);
c906108c 944
e8930304
JB
945 for (gdb = so_list_head; gdb; gdb = gdb->next)
946 if (! pattern || re_exec (gdb->so_name))
947 {
6612ad7f
JB
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
e8930304 956 any_matches = 1;
a86988f2
PA
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 }
e8930304
JB
970 }
971
7eedccfa
PP
972 if (loaded_any_symbols)
973 breakpoint_re_set ();
974
e8930304
JB
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 {
3641da11 981 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
66aba65d 982
e8930304
JB
983 /* Getting new symbols may change our opinion about what is
984 frameless. */
985 reinit_frame_cache ();
986
66aba65d 987 ops->special_symbol_handling ();
e8930304
JB
988 }
989 }
c906108c
SS
990}
991
7f86f058
PA
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. */
c906108c
SS
996
997static void
55333a84 998info_sharedlibrary_command (char *pattern, int from_tty)
c906108c 999{
52f0bd74 1000 struct so_list *so = NULL; /* link map state variable */
55333a84 1001 int so_missing_debug_info = 0;
c906108c 1002 int addr_width;
55333a84
DE
1003 int nr_libs;
1004 struct cleanup *table_cleanup;
f5656ead 1005 struct gdbarch *gdbarch = target_gdbarch ();
79a45e25 1006 struct ui_out *uiout = current_uiout;
55333a84
DE
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 }
c906108c 1015
84eb3c4f 1016 /* "0x", a little whitespace, and two hex digits per byte of pointers. */
55333a84 1017 addr_width = 4 + (gdbarch_ptr_bit (gdbarch) / 4);
c906108c 1018
105b175f 1019 update_solib_list (from_tty, 0);
07cd4b97 1020
55333a84
DE
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)
c906108c 1025 {
c5aa993b 1026 if (so->so_name[0])
c906108c 1027 {
55333a84
DE
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;
c906108c 1050
55333a84
DE
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");
c906108c 1067 }
55333a84
DE
1068
1069 if (! ui_out_is_mi_like_p (interp_ui_out (top_level_interpreter ()))
1070 && so->symbols_loaded
e0ae4240 1071 && !objfile_has_symbols (so->objfile))
55333a84
DE
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);
c906108c 1085 }
55333a84
DE
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
c906108c 1099 {
55333a84
DE
1100 if (so_missing_debug_info)
1101 ui_out_message (uiout, 0,
3e43a32a
MS
1102 _("(*): Shared library is missing "
1103 "debugging information.\n"));
c906108c
SS
1104 }
1105}
1106
5fd1a349
PP
1107/* Return 1 if ADDRESS lies within SOLIB. */
1108
1109int
1110solib_contains_address_p (const struct so_list *const solib,
1111 CORE_ADDR address)
1112{
0542c86d 1113 struct target_section *p;
5fd1a349
PP
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
7f86f058
PA
1122/* If ADDRESS is in a shared lib in program space PSPACE, return its
1123 name.
c906108c 1124
7f86f058
PA
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.
c906108c 1128
c5aa993b
JM
1129 For example, this routine is called at one point to disable
1130 breakpoints which are in shared libraries that are not currently
7f86f058 1131 mapped in. */
c906108c
SS
1132
1133char *
6c95b8df 1134solib_name_from_address (struct program_space *pspace, CORE_ADDR address)
c906108c 1135{
6c95b8df 1136 struct so_list *so = NULL;
c5aa993b 1137
6c95b8df 1138 for (so = pspace->so_list; so; so = so->next)
5fd1a349
PP
1139 if (solib_contains_address_p (so, address))
1140 return (so->so_name);
07cd4b97 1141
c906108c
SS
1142 return (0);
1143}
1144
de18c1d8
JM
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
1151int
1152solib_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
1153{
3641da11 1154 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
de18c1d8
JM
1155
1156 if (ops->keep_data_in_core)
1157 return ops->keep_data_in_core (vaddr, size);
1158 else
1159 return 0;
1160}
1161
c906108c
SS
1162/* Called by free_all_symtabs */
1163
c5aa993b 1164void
fba45db2 1165clear_solib (void)
c906108c 1166{
3641da11 1167 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
66aba65d 1168
085dd6e6
JM
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)
cb851954 1191 disable_breakpoints_in_shlibs ();
085dd6e6 1192
c906108c
SS
1193 while (so_list_head)
1194 {
07cd4b97 1195 struct so_list *so = so_list_head;
433759f7 1196
07cd4b97 1197 so_list_head = so->next;
c86cf029 1198 observer_notify_solib_unloaded (so);
046ac79f 1199 remove_target_sections (so);
07cd4b97 1200 free_so (so);
c906108c 1201 }
07cd4b97 1202
66aba65d 1203 ops->clear_solib ();
c906108c
SS
1204}
1205
7f86f058
PA
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. */
c5aa993b
JM
1210
1211void
268a4a75 1212solib_create_inferior_hook (int from_tty)
c906108c 1213{
3641da11 1214 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
433759f7 1215
268a4a75 1216 ops->solib_create_inferior_hook (from_tty);
c906108c
SS
1217}
1218
7f86f058
PA
1219/* Check to see if an address is in the dynamic loader's dynamic
1220 symbol resolution code. Return 1 if so, 0 otherwise. */
d7fa2ae2
KB
1221
1222int
1223in_solib_dynsym_resolve_code (CORE_ADDR pc)
1224{
3641da11 1225 const struct target_so_ops *ops = solib_ops (target_gdbarch ());
433759f7 1226
66aba65d 1227 return ops->in_dynsym_resolve_code (pc);
d7fa2ae2 1228}
c906108c 1229
7f86f058 1230/* Implements the "sharedlibrary" command. */
c906108c
SS
1231
1232static void
fba45db2 1233sharedlibrary_command (char *args, int from_tty)
c906108c
SS
1234{
1235 dont_repeat ();
990f9fe3 1236 solib_add (args, from_tty, (struct target_ops *) 0, 1);
c906108c
SS
1237}
1238
7f86f058 1239/* Implements the command "nosharedlibrary", which discards symbols
cb0ba49e
MS
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
c60a7562
MS
1244void
1245no_shared_libraries (char *ignored, int from_tty)
1246{
a3247a22
PP
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
615b9dba 1252 clear_solib ();
a3247a22 1253 objfile_purge_solibs ();
c60a7562 1254}
c906108c 1255
f9e14852
GB
1256/* See solib.h. */
1257
1258void
1259update_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
1269void
1270handle_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
a86988f2
PA
1287/* Reload shared libraries, but avoid reloading the same symbol file
1288 we already have loaded. */
1289
1290static void
1291reload_shared_libraries_1 (int from_tty)
1292{
1293 struct so_list *so;
1294 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
1295
770e7fc7
DE
1296 if (print_symbol_loading_p (from_tty, 0, 0))
1297 printf_unfiltered (_("Loading symbols for shared libraries.\n"));
1298
a86988f2
PA
1299 for (so = so_list_head; so != NULL; so = so->next)
1300 {
1301 char *filename, *found_pathname = NULL;
1302 bfd *abfd;
a86988f2
PA
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);
42b1321c 1308 make_cleanup (xfree, filename);
a86988f2
PA
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);
cbb099e8 1314 gdb_bfd_unref (abfd);
a86988f2
PA
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
0ba1096a 1321 && filename_cmp (found_pathname, so->so_name) != 0))
a86988f2 1322 {
59145f8c
AR
1323 if (so->objfile && ! (so->objfile->flags & OBJF_USERLOADED)
1324 && !solib_used (so))
a86988f2 1325 free_objfile (so->objfile);
046ac79f 1326 remove_target_sections (so);
0892cb63 1327 clear_so (so);
a86988f2
PA
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
0ba1096a 1334 || filename_cmp (found_pathname, so->so_name) != 0))
a86988f2 1335 {
492d29ea 1336 int got_error = 0;
a86988f2 1337
492d29ea
PA
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
a86988f2 1351
492d29ea
PA
1352 if (!got_error
1353 && (auto_solib_add || was_loaded || libpthread_solib_p (so)))
1354 solib_read_symbols (so, flags);
a86988f2
PA
1355 }
1356 }
1357
1358 do_cleanups (old_chain);
1359}
1360
cf466558 1361static void
f397e303
AC
1362reload_shared_libraries (char *ignored, int from_tty,
1363 struct cmd_list_element *e)
cf466558 1364{
3641da11 1365 const struct target_so_ops *ops;
a86988f2
PA
1366
1367 reload_shared_libraries_1 (from_tty);
1368
f5656ead 1369 ops = solib_ops (target_gdbarch ());
a86988f2 1370
c378eb4e 1371 /* Creating inferior hooks here has two purposes. First, if we reload
c8fa6cdd
VP
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 {
a86988f2
PA
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
268a4a75 1391 solib_create_inferior_hook (from_tty);
c8fa6cdd 1392 }
268a4a75
JK
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
a86988f2
PA
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. */
c8fa6cdd 1410 reinit_frame_cache ();
a86988f2
PA
1411
1412 ops->special_symbol_handling ();
cf466558
KB
1413}
1414
2938e6cf
GB
1415/* Wrapper for reload_shared_libraries that replaces "remote:"
1416 at the start of gdb_sysroot with "target:". */
1417
1418static void
1419gdb_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
920d2a44
AC
1445static void
1446show_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
3a40aaa0
UW
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
1457struct symbol *
efad9b6a 1458solib_global_lookup (struct objfile *objfile,
3a40aaa0 1459 const char *name,
21b556f4 1460 const domain_enum domain)
3a40aaa0 1461{
9c1877ea 1462 const struct target_so_ops *ops = solib_ops (get_objfile_arch (objfile));
3a40aaa0 1463
e8a92f7b 1464 if (ops->lookup_lib_global_symbol != NULL)
94af9270 1465 return ops->lookup_lib_global_symbol (objfile, name, domain);
3a40aaa0
UW
1466 return NULL;
1467}
1468
cb457ae2
YQ
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
1474CORE_ADDR
1475gdb_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 {
3e29f34a
MR
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
cb457ae2 1517 /* BFD symbols are section relative. */
3e29f34a 1518 symaddr += sym->section->vma;
cb457ae2
YQ
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
1533static CORE_ADDR
1534bfd_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
1571CORE_ADDR
1572gdb_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}
3a40aaa0 1585
63644780
NB
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
1590static void
1591remove_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
a78f21af
AC
1603extern initialize_file_ftype _initialize_solib; /* -Wmissing-prototypes */
1604
c906108c 1605void
fba45db2 1606_initialize_solib (void)
c906108c 1607{
66aba65d
MK
1608 solib_data = gdbarch_data_register_pre_init (solib_init);
1609
63644780
NB
1610 observer_attach_free_objfile (remove_user_added_objfile);
1611
c906108c 1612 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1bedd215 1613 _("Load shared object library symbols for files matching REGEXP."));
c5aa993b 1614 add_info ("sharedlibrary", info_sharedlibrary_command,
1bedd215 1615 _("Status of loaded shared object libraries."));
c60a7562 1616 add_com ("nosharedlibrary", class_files, no_shared_libraries,
1bedd215 1617 _("Unload all shared object library symbols."));
c906108c 1618
5bf193a2
AC
1619 add_setshow_boolean_cmd ("auto-solib-add", class_support,
1620 &auto_solib_add, _("\
1621Set autoloading of shared library symbols."), _("\
1622Show autoloading of shared library symbols."), _("\
b7209cb4
FF
1623If \"on\", symbols from all shared object libraries will be loaded\n\
1624automatically when the inferior begins execution, when the dynamic linker\n\
1625informs gdb that a new library has been loaded, or when attaching to the\n\
3e43a32a
MS
1626inferior. Otherwise, symbols must be loaded manually, using \
1627`sharedlibrary'."),
5bf193a2 1628 NULL,
920d2a44 1629 show_auto_solib_add,
5bf193a2 1630 &setlist, &showlist);
c906108c 1631
811a659a
GB
1632 add_setshow_optional_filename_cmd ("sysroot", class_support,
1633 &gdb_sysroot, _("\
f822c95b
DJ
1634Set an alternate system root."), _("\
1635Show the current system root."), _("\
1636The system root is used to load absolute shared library symbol files.\n\
1637For other (relative) files, you can add directories using\n\
1638`set solib-search-path'."),
2938e6cf 1639 gdb_sysroot_changed,
811a659a
GB
1640 NULL,
1641 &setlist, &showlist);
c906108c 1642
f822c95b
DJ
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);
030292b7 1647
525226b5
AC
1648 add_setshow_optional_filename_cmd ("solib-search-path", class_support,
1649 &solib_search_path, _("\
3e43a32a
MS
1650Set the search path for loading non-absolute shared library symbol files."),
1651 _("\
1652Show the search path for loading non-absolute shared library symbol files."),
1653 _("\
1654This takes precedence over the environment variables \
1655PATH and LD_LIBRARY_PATH."),
525226b5 1656 reload_shared_libraries,
920d2a44 1657 show_solib_search_path,
525226b5 1658 &setlist, &showlist);
c906108c 1659}
This page took 1.495482 seconds and 4 git commands to generate.