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