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