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