Commit | Line | Data |
---|---|---|
f8b76e70 | 1 | /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger. |
ee0613d1 | 2 | Copyright 1990, 1991, 1992 Free Software Foundation, Inc. |
f8b76e70 | 3 | |
bd5635a1 RP |
4 | This file is part of GDB. |
5 | ||
bdbd5f50 | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
bdbd5f50 JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
bdbd5f50 | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
bdbd5f50 JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 19 | |
f8b76e70 | 20 | |
bd5635a1 | 21 | #include <sys/types.h> |
f8b76e70 | 22 | #include <signal.h> |
bd5635a1 RP |
23 | #include <string.h> |
24 | #include <link.h> | |
d0237a54 JK |
25 | #include <sys/param.h> |
26 | #include <fcntl.h> | |
27 | #include <stdio.h> | |
d261ece7 | 28 | #include <a.out.h> |
f8b76e70 | 29 | |
bd5635a1 | 30 | #include "defs.h" |
bd5635a1 RP |
31 | #include "symtab.h" |
32 | #include "gdbcore.h" | |
33 | #include "command.h" | |
b3fdaf3d | 34 | #include "target.h" |
2403f49b | 35 | #include "frame.h" |
bdbd5f50 JG |
36 | #include "regex.h" |
37 | #include "inferior.h" | |
38 | ||
f8b76e70 FF |
39 | extern char *getenv (); |
40 | extern char *elf_interpreter (); /* Interpreter name from exec file */ | |
41 | extern char *re_comp (); | |
42 | ||
43 | #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */ | |
44 | ||
45 | /* On SVR4 systems, for the initial implementation, use main() as the | |
46 | "startup mapping complete" breakpoint address. The models for SunOS | |
47 | and SVR4 dynamic linking debugger support are different in that SunOS | |
48 | hits one breakpoint when all mapping is complete while using the SVR4 | |
49 | debugger support takes two breakpoint hits for each file mapped, and | |
50 | there is no way to know when the "last" one is hit. Both these | |
51 | mechanisms should be tied to a "breakpoint service routine" that | |
52 | gets automatically executed whenever one of the breakpoints indicating | |
53 | a change in mapping is hit. This is a future enhancement. (FIXME) */ | |
54 | ||
55 | #define BKPT_AT_MAIN 1 | |
56 | ||
57 | /* local data declarations */ | |
58 | ||
d261ece7 | 59 | #ifndef SVR4_SHARED_LIBS |
f8b76e70 FF |
60 | |
61 | #define DEBUG_BASE "_DYNAMIC" | |
62 | #define LM_ADDR(so) ((so) -> lm.lm_addr) | |
63 | #define LM_NEXT(so) ((so) -> lm.lm_next) | |
64 | #define LM_NAME(so) ((so) -> lm.lm_name) | |
65 | static struct link_dynamic dynamic_copy; | |
66 | static struct link_dynamic_2 ld_2_copy; | |
67 | static struct ld_debug debug_copy; | |
68 | static CORE_ADDR debug_addr; | |
69 | static CORE_ADDR flag_addr; | |
70 | ||
d261ece7 | 71 | #else /* SVR4_SHARED_LIBS */ |
f8b76e70 FF |
72 | |
73 | #define DEBUG_BASE "_r_debug" | |
74 | #define LM_ADDR(so) ((so) -> lm.l_addr) | |
75 | #define LM_NEXT(so) ((so) -> lm.l_next) | |
76 | #define LM_NAME(so) ((so) -> lm.l_name) | |
77 | static struct r_debug debug_copy; | |
f8b76e70 FF |
78 | char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */ |
79 | extern CORE_ADDR proc_base_address (); | |
80 | extern int proc_address_to_fd (); | |
81 | ||
d261ece7 | 82 | #endif /* !SVR4_SHARED_LIBS */ |
bd5635a1 | 83 | |
bd5635a1 | 84 | struct so_list { |
f8b76e70 FF |
85 | struct so_list *next; /* next structure in linked list */ |
86 | struct link_map lm; /* copy of link map from inferior */ | |
87 | struct link_map *lmaddr; /* addr in inferior lm was read from */ | |
88 | CORE_ADDR lmend; /* upper addr bound of mapped object */ | |
89 | char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */ | |
90 | char symbols_loaded; /* flag: symbols read in yet? */ | |
91 | char from_tty; /* flag: print msgs? */ | |
92 | bfd *so_bfd; /* bfd for so_name */ | |
93 | struct section_table *sections; | |
94 | struct section_table *sections_end; | |
bd5635a1 RP |
95 | }; |
96 | ||
f8b76e70 FF |
97 | static struct so_list *so_list_head; /* List of known shared objects */ |
98 | static CORE_ADDR debug_base; /* Base of dynamic linker structures */ | |
99 | static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */ | |
100 | ||
bd5635a1 | 101 | |
d0237a54 | 102 | /* |
f8b76e70 FF |
103 | |
104 | LOCAL FUNCTION | |
105 | ||
106 | solib_map_sections -- open bfd and build sections for shared lib | |
107 | ||
108 | SYNOPSIS | |
109 | ||
110 | static void solib_map_sections (struct so_list *so) | |
111 | ||
112 | DESCRIPTION | |
113 | ||
114 | Given a pointer to one of the shared objects in our list | |
115 | of mapped objects, use the recorded name to open a bfd | |
116 | descriptor for the object, build a section table, and then | |
117 | relocate all the section addresses by the base address at | |
118 | which the shared object was mapped. | |
119 | ||
120 | FIXMES | |
121 | ||
122 | In most (all?) cases the shared object file name recorded in the | |
123 | dynamic linkage tables will be a fully qualified pathname. For | |
124 | cases where it isn't, do we really mimic the systems search | |
125 | mechanism correctly in the below code (particularly the tilde | |
126 | expansion stuff?). | |
127 | */ | |
128 | ||
d0237a54 | 129 | static void |
f8b76e70 FF |
130 | solib_map_sections (so) |
131 | struct so_list *so; | |
d0237a54 JK |
132 | { |
133 | char *filename; | |
134 | char *scratch_pathname; | |
135 | int scratch_chan; | |
136 | struct section_table *p; | |
137 | ||
f8b76e70 | 138 | filename = tilde_expand (so -> so_name); |
d0237a54 JK |
139 | make_cleanup (free, filename); |
140 | ||
141 | scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0, | |
f8b76e70 | 142 | &scratch_pathname); |
d0237a54 | 143 | if (scratch_chan < 0) |
f8b76e70 FF |
144 | { |
145 | scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename, | |
146 | O_RDONLY, 0, &scratch_pathname); | |
147 | } | |
d0237a54 | 148 | if (scratch_chan < 0) |
f8b76e70 FF |
149 | { |
150 | perror_with_name (filename); | |
151 | } | |
152 | ||
153 | so -> so_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan); | |
154 | if (!so -> so_bfd) | |
155 | { | |
156 | error ("Could not open `%s' as an executable file: %s", | |
157 | scratch_pathname, bfd_errmsg (bfd_error)); | |
158 | } | |
159 | if (!bfd_check_format (so -> so_bfd, bfd_object)) | |
160 | { | |
161 | error ("\"%s\": not in executable format: %s.", | |
162 | scratch_pathname, bfd_errmsg (bfd_error)); | |
163 | } | |
164 | if (build_section_table (so -> so_bfd, &so -> sections, &so -> sections_end)) | |
165 | { | |
166 | error ("Can't find the file sections in `%s': %s", | |
167 | exec_bfd -> filename, bfd_errmsg (bfd_error)); | |
168 | } | |
169 | ||
170 | for (p = so -> sections; p < so -> sections_end; p++) | |
171 | { | |
172 | /* Relocate the section binding addresses as recorded in the shared | |
173 | object's file by the base address to which the object was actually | |
174 | mapped. */ | |
175 | p -> addr += (CORE_ADDR) LM_ADDR (so); | |
176 | p -> endaddr += (CORE_ADDR) LM_ADDR (so); | |
177 | so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend); | |
178 | } | |
179 | } | |
180 | ||
d261ece7 SG |
181 | /* Read all dynamically loaded common symbol definitions from the inferior |
182 | and add them to the misc_function_vector. */ | |
183 | ||
7f435241 FF |
184 | #ifndef SVR4_SHARED_LIBS |
185 | ||
d261ece7 SG |
186 | static void |
187 | solib_add_common_symbols (rtc_symp) | |
188 | struct rtc_symb *rtc_symp; | |
189 | { | |
190 | struct rtc_symb inferior_rtc_symb; | |
191 | struct nlist inferior_rtc_nlist; | |
192 | extern void discard_misc_bunches(); | |
193 | ||
194 | init_misc_bunches (); | |
195 | make_cleanup (discard_misc_bunches, 0); | |
196 | ||
197 | while (rtc_symp) | |
198 | { | |
199 | read_memory((CORE_ADDR)rtc_symp, | |
200 | &inferior_rtc_symb, | |
201 | sizeof(inferior_rtc_symb)); | |
202 | read_memory((CORE_ADDR)inferior_rtc_symb.rtc_sp, | |
203 | &inferior_rtc_nlist, | |
204 | sizeof(inferior_rtc_nlist)); | |
205 | if (inferior_rtc_nlist.n_type == N_COMM) | |
206 | { | |
207 | /* FIXME: The length of the symbol name is not available, but in the | |
208 | current implementation the common symbol is allocated immediately | |
209 | behind the name of the symbol. */ | |
210 | int len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx; | |
211 | char *name, *origname; | |
212 | ||
213 | origname = name = xmalloc (len); | |
214 | read_memory((CORE_ADDR)inferior_rtc_nlist.n_un.n_name, name, len); | |
215 | ||
216 | /* Don't enter the symbol twice if the target is re-run. */ | |
217 | ||
218 | #ifdef NAMES_HAVE_UNDERSCORE | |
219 | if (*name == '_') | |
220 | name++; | |
221 | #endif | |
222 | if (lookup_misc_func (name) < 0) | |
223 | prim_record_misc_function (obsavestring (name, strlen (name)), | |
224 | inferior_rtc_nlist.n_value, | |
225 | mf_bss); | |
226 | free (origname); | |
227 | } | |
228 | rtc_symp = inferior_rtc_symb.rtc_next; | |
229 | } | |
230 | ||
231 | condense_misc_bunches (1); | |
232 | } | |
233 | ||
7f435241 FF |
234 | #endif /* SVR4_SHARED_LIBS */ |
235 | ||
f8b76e70 FF |
236 | /* |
237 | ||
238 | LOCAL FUNCTION | |
239 | ||
240 | bfd_lookup_symbol -- lookup the value for a specific symbol | |
241 | ||
242 | SYNOPSIS | |
243 | ||
244 | CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname) | |
245 | ||
246 | DESCRIPTION | |
247 | ||
248 | An expensive way to lookup the value of a single symbol for | |
249 | bfd's that are only temporary anyway. This is used by the | |
250 | shared library support to find the address of the debugger | |
251 | interface structures in the shared library. | |
252 | ||
253 | Note that 0 is specifically allowed as an error return (no | |
254 | such symbol). | |
255 | ||
256 | FIXME: See if there is a less "expensive" way of doing this. | |
257 | Also see if there is already another bfd or gdb function | |
258 | that specifically does this, and if so, use it. | |
259 | */ | |
260 | ||
261 | static CORE_ADDR | |
262 | DEFUN (bfd_lookup_symbol, (abfd, symname), | |
263 | bfd *abfd AND | |
264 | char *symname) | |
265 | { | |
266 | unsigned int storage_needed; | |
267 | asymbol *sym; | |
268 | asymbol **symbol_table; | |
269 | unsigned int number_of_symbols; | |
270 | unsigned int i; | |
271 | struct cleanup *back_to; | |
272 | CORE_ADDR symaddr = 0; | |
273 | enum misc_function_type mf_type; | |
274 | ||
275 | storage_needed = get_symtab_upper_bound (abfd); | |
276 | ||
277 | if (storage_needed > 0) | |
278 | { | |
279 | symbol_table = (asymbol **) bfd_xmalloc (storage_needed); | |
280 | back_to = make_cleanup (free, symbol_table); | |
281 | number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); | |
282 | ||
283 | for (i = 0; i < number_of_symbols; i++) | |
d0237a54 | 284 | { |
f8b76e70 FF |
285 | sym = *symbol_table++; |
286 | if (strcmp (sym -> name, symname) == 0) | |
287 | { | |
288 | symaddr = sym -> value; | |
289 | break; | |
290 | } | |
d0237a54 | 291 | } |
f8b76e70 | 292 | do_cleanups (back_to); |
d0237a54 | 293 | } |
f8b76e70 | 294 | return (symaddr); |
d0237a54 JK |
295 | } |
296 | ||
f8b76e70 FF |
297 | /* |
298 | ||
d261ece7 SG |
299 | LOCAL FUNCTION |
300 | ||
301 | look_for_base -- examine file for each mapped address segment | |
302 | ||
303 | SYNOPSYS | |
304 | ||
305 | static int look_for_base (int fd, CORE_ADDR baseaddr) | |
306 | ||
307 | DESCRIPTION | |
308 | ||
309 | This function is passed to proc_iterate_over_mappings, which | |
310 | causes it to get called once for each mapped address space, with | |
311 | an open file descriptor for the file mapped to that space, and the | |
312 | base address of that mapped space. | |
313 | ||
314 | Our job is to find the symbol DEBUG_BASE in the file that this | |
315 | fd is open on, if it exists, and if so, initialize the dynamic | |
316 | linker structure base address debug_base. | |
317 | ||
318 | Note that this is a computationally expensive proposition, since | |
319 | we basically have to open a bfd on every call, so we specifically | |
320 | avoid opening the exec file. | |
321 | */ | |
322 | ||
323 | static int | |
324 | DEFUN (look_for_base, (fd, baseaddr), | |
325 | int fd AND | |
326 | CORE_ADDR baseaddr) | |
327 | { | |
328 | bfd *interp_bfd; | |
329 | CORE_ADDR address; | |
330 | ||
331 | /* If the fd is -1, then there is no file that corresponds to this | |
332 | mapped memory segment, so skip it. Also, if the fd corresponds | |
333 | to the exec file, skip it as well. */ | |
334 | ||
335 | if ((fd == -1) || fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd)) | |
336 | { | |
337 | return (0); | |
338 | } | |
339 | ||
340 | /* Try to open whatever random file this fd corresponds to. Note that | |
341 | we have no way currently to find the filename. Don't gripe about | |
342 | any problems we might have, just fail. */ | |
343 | ||
344 | if ((interp_bfd = bfd_fdopenr ("unnamed", NULL, fd)) == NULL) | |
345 | { | |
346 | return (0); | |
347 | } | |
348 | if (!bfd_check_format (interp_bfd, bfd_object)) | |
349 | { | |
350 | bfd_close (interp_bfd); | |
351 | return (0); | |
352 | } | |
353 | ||
354 | /* Now try to find our DEBUG_BASE symbol in this file, which we at | |
355 | least know to be a valid ELF executable or shared library. */ | |
356 | ||
357 | if ((address = bfd_lookup_symbol (interp_bfd, DEBUG_BASE)) == 0) | |
358 | { | |
359 | bfd_close (interp_bfd); | |
360 | return (0); | |
361 | } | |
362 | ||
363 | /* Eureka! We found the symbol. But now we may need to relocate it | |
364 | by the base address. If the symbol's value is less than the base | |
365 | address of the shared library, then it hasn't yet been relocated | |
366 | by the dynamic linker, and we have to do it ourself. FIXME: Note | |
367 | that we make the assumption that the first segment that corresponds | |
368 | to the shared library has the base address to which the library | |
369 | was relocated. */ | |
370 | ||
371 | if (address < baseaddr) | |
372 | { | |
373 | address += baseaddr; | |
374 | } | |
375 | debug_base = address; | |
376 | bfd_close (interp_bfd); | |
377 | return (1); | |
378 | } | |
379 | ||
380 | /* | |
381 | ||
f8b76e70 FF |
382 | LOCAL FUNCTION |
383 | ||
384 | locate_base -- locate the base address of dynamic linker structs | |
385 | ||
386 | SYNOPSIS | |
387 | ||
388 | CORE_ADDR locate_base (void) | |
389 | ||
390 | DESCRIPTION | |
391 | ||
392 | For both the SunOS and SVR4 shared library implementations, if the | |
393 | inferior executable has been linked dynamically, there is a single | |
394 | address somewhere in the inferior's data space which is the key to | |
d261ece7 | 395 | locating all of the dynamic linker's runtime structures. This |
f8b76e70 FF |
396 | address is the value of the symbol defined by the macro DEBUG_BASE. |
397 | The job of this function is to find and return that address, or to | |
398 | return 0 if there is no such address (the executable is statically | |
399 | linked for example). | |
400 | ||
401 | For SunOS, the job is almost trivial, since the dynamic linker and | |
402 | all of it's structures are statically linked to the executable at | |
403 | link time. Thus the symbol for the address we are looking for has | |
404 | already been added to the misc function vector at the time the symbol | |
d261ece7 | 405 | file's symbols were read, and all we have to do is look it up there. |
f8b76e70 FF |
406 | |
407 | The SVR4 version is much more complicated because the dynamic linker | |
d261ece7 SG |
408 | and it's structures are located in the shared C library, which gets |
409 | run as the executable's "interpreter" by the kernel. We have to go | |
410 | to a lot more work to discover the address of DEBUG_BASE. Because | |
f8b76e70 | 411 | of this complexity, we cache the value we find and return that value |
d261ece7 | 412 | on subsequent invocations. |
f8b76e70 | 413 | |
d261ece7 SG |
414 | Note that we can assume nothing about the process state at the time |
415 | we need to find this address. We may be stopped on the first instruc- | |
416 | tion of the interpreter (C shared library), the first instruction of | |
417 | the executable itself, or somewhere else entirely (if we attached | |
418 | to the process for example). | |
f8b76e70 FF |
419 | |
420 | */ | |
421 | ||
422 | static CORE_ADDR | |
423 | locate_base () | |
424 | { | |
f8b76e70 | 425 | |
d261ece7 | 426 | #ifndef SVR4_SHARED_LIBS |
f8b76e70 FF |
427 | |
428 | int i; | |
d261ece7 | 429 | CORE_ADDR address = 0; |
f8b76e70 FF |
430 | |
431 | i = lookup_misc_func (DEBUG_BASE); | |
432 | if (i >= 0 && misc_function_vector[i].address != 0) | |
433 | { | |
434 | address = misc_function_vector[i].address; | |
435 | } | |
d261ece7 | 436 | return (address); |
f8b76e70 | 437 | |
d261ece7 | 438 | #else /* SVR4_SHARED_LIBS */ |
f8b76e70 | 439 | |
d261ece7 SG |
440 | /* Check to see if we have a currently valid address, and if so, avoid |
441 | doing all this work again and just return the cached address. If | |
442 | we have no cached address, ask the /proc support interface to iterate | |
443 | over the list of mapped address segments, calling look_for_base() for | |
444 | each segment. When we are done, we will have either found the base | |
445 | address or not. */ | |
f8b76e70 | 446 | |
d261ece7 | 447 | if (debug_base == 0) |
f8b76e70 | 448 | { |
d261ece7 | 449 | proc_iterate_over_mappings (look_for_base); |
f8b76e70 | 450 | } |
d261ece7 | 451 | return (debug_base); |
f8b76e70 | 452 | |
d261ece7 | 453 | #endif /* !SVR4_SHARED_LIBS */ |
f8b76e70 FF |
454 | |
455 | } | |
bd5635a1 | 456 | |
f8b76e70 FF |
457 | static struct link_map * |
458 | first_link_map_member () | |
bd5635a1 | 459 | { |
f8b76e70 FF |
460 | struct link_map *lm = NULL; |
461 | ||
d261ece7 | 462 | #ifndef SVR4_SHARED_LIBS |
f8b76e70 FF |
463 | |
464 | read_memory (debug_base, &dynamic_copy, sizeof (dynamic_copy)); | |
465 | if (dynamic_copy.ld_version >= 2) | |
466 | { | |
467 | /* It is a version that we can deal with, so read in the secondary | |
468 | structure and find the address of the link map list from it. */ | |
469 | read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, &ld_2_copy, | |
470 | sizeof (struct link_dynamic_2)); | |
471 | lm = ld_2_copy.ld_loaded; | |
472 | } | |
473 | ||
d261ece7 | 474 | #else /* SVR4_SHARED_LIBS */ |
f8b76e70 FF |
475 | |
476 | read_memory (debug_base, &debug_copy, sizeof (struct r_debug)); | |
477 | lm = debug_copy.r_map; | |
478 | ||
d261ece7 | 479 | #endif /* !SVR4_SHARED_LIBS */ |
d0237a54 | 480 | |
f8b76e70 FF |
481 | return (lm); |
482 | } | |
483 | ||
484 | /* | |
485 | ||
486 | GLOBAL FUNCTION | |
487 | ||
488 | find_solib -- step through list of shared objects | |
489 | ||
490 | SYNOPSIS | |
491 | ||
492 | struct so_list *find_solib (struct so_list *so_list_ptr) | |
493 | ||
494 | DESCRIPTION | |
495 | ||
496 | This module contains the routine which finds the names of any | |
497 | loaded "images" in the current process. The argument in must be | |
498 | NULL on the first call, and then the returned value must be passed | |
499 | in on subsequent calls. This provides the capability to "step" down | |
500 | the list of loaded objects. On the last object, a NULL value is | |
501 | returned. | |
d0237a54 | 502 | |
f8b76e70 FF |
503 | The arg and return value are "struct link_map" pointers, as defined |
504 | in <link.h>. | |
505 | */ | |
d0237a54 | 506 | |
f8b76e70 FF |
507 | struct so_list * |
508 | find_solib (so_list_ptr) | |
509 | struct so_list *so_list_ptr; /* Last lm or NULL for first one */ | |
510 | { | |
511 | struct so_list *so_list_next = NULL; | |
512 | struct link_map *lm = NULL; | |
513 | struct so_list *new; | |
514 | ||
515 | if (so_list_ptr == NULL) | |
516 | { | |
517 | /* We are setting up for a new scan through the loaded images. */ | |
518 | if ((so_list_next = so_list_head) == NULL) | |
519 | { | |
520 | /* We have not already read in the dynamic linking structures | |
521 | from the inferior, lookup the address of the base structure. */ | |
522 | debug_base = locate_base (); | |
523 | if (debug_base > 0) | |
524 | { | |
525 | /* Read the base structure in and find the address of the first | |
526 | link map list member. */ | |
527 | lm = first_link_map_member (); | |
528 | } | |
529 | } | |
530 | } | |
531 | else | |
532 | { | |
533 | /* We have been called before, and are in the process of walking | |
534 | the shared library list. Advance to the next shared object. */ | |
535 | if ((lm = LM_NEXT (so_list_ptr)) == NULL) | |
536 | { | |
537 | /* We have hit the end of the list, so check to see if any were | |
538 | added, but be quiet if we can't read from the target any more. */ | |
539 | int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr, | |
540 | (char *) &(so_list_ptr -> lm), | |
541 | sizeof (struct link_map)); | |
542 | if (status == 0) | |
543 | { | |
544 | lm = LM_NEXT (so_list_ptr); | |
545 | } | |
546 | else | |
547 | { | |
548 | lm = NULL; | |
549 | } | |
550 | } | |
551 | so_list_next = so_list_ptr -> next; | |
552 | } | |
553 | if ((so_list_next == NULL) && (lm != NULL)) | |
554 | { | |
555 | /* Get next link map structure from inferior image and build a local | |
556 | abbreviated load_map structure */ | |
557 | new = (struct so_list *) xmalloc (sizeof (struct so_list)); | |
558 | (void) memset ((char *) new, 0, sizeof (struct so_list)); | |
559 | new -> lmaddr = lm; | |
560 | /* Add the new node as the next node in the list, or as the root | |
561 | node if this is the first one. */ | |
562 | if (so_list_ptr != NULL) | |
563 | { | |
564 | so_list_ptr -> next = new; | |
565 | } | |
566 | else | |
567 | { | |
568 | so_list_head = new; | |
569 | } | |
570 | so_list_next = new; | |
571 | read_memory ((CORE_ADDR) lm, &(new -> lm), sizeof (struct link_map)); | |
572 | /* For the SVR4 version, there is one entry that has no name | |
573 | (for the inferior executable) since it is not a shared object. */ | |
574 | if (LM_NAME (new) != 0) | |
575 | { | |
ee0613d1 JG |
576 | if (!target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name, |
577 | MAX_PATH_SIZE - 1)) | |
578 | error ("find_solib: Can't read pathname for load map\n"); | |
f8b76e70 FF |
579 | new -> so_name[MAX_PATH_SIZE - 1] = 0; |
580 | solib_map_sections (new); | |
581 | } | |
582 | } | |
583 | return (so_list_next); | |
bd5635a1 | 584 | } |
d0237a54 | 585 | |
bdbd5f50 JG |
586 | /* A small stub to get us past the arg-passing pinhole of catch_errors. */ |
587 | ||
588 | static int | |
589 | symbol_add_stub (arg) | |
590 | char *arg; | |
d0237a54 | 591 | { |
f8b76e70 FF |
592 | register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */ |
593 | ||
594 | symbol_file_add (so -> so_name, so -> from_tty, | |
595 | (unsigned int) LM_ADDR (so), 0); | |
596 | return (1); | |
d0237a54 | 597 | } |
bd5635a1 | 598 | |
f8b76e70 FF |
599 | /* |
600 | ||
601 | GLOBAL FUNCTION | |
602 | ||
603 | solib_add -- add a shared library file to the symtab and section list | |
604 | ||
605 | SYNOPSIS | |
606 | ||
607 | void solib_add (char *arg_string, int from_tty, | |
608 | struct target_ops *target) | |
609 | ||
610 | DESCRIPTION | |
611 | ||
612 | */ | |
bdbd5f50 JG |
613 | |
614 | void | |
615 | solib_add (arg_string, from_tty, target) | |
616 | char *arg_string; | |
617 | int from_tty; | |
618 | struct target_ops *target; | |
bd5635a1 | 619 | { |
f8b76e70 FF |
620 | register struct so_list *so = NULL; /* link map state variable */ |
621 | char *re_err; | |
622 | int count; | |
623 | int old; | |
624 | ||
625 | if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL) | |
626 | { | |
627 | error ("Invalid regexp: %s", re_err); | |
628 | } | |
629 | ||
bdbd5f50 JG |
630 | /* Getting new symbols may change our opinion about what is |
631 | frameless. */ | |
632 | reinit_frame_cache (); | |
bdbd5f50 | 633 | |
f8b76e70 FF |
634 | while ((so = find_solib (so)) != NULL) |
635 | { | |
636 | if (so -> so_name[0] && re_exec (so -> so_name)) | |
637 | { | |
638 | if (so -> symbols_loaded) | |
639 | { | |
bdbd5f50 | 640 | if (from_tty) |
f8b76e70 FF |
641 | { |
642 | printf ("Symbols already loaded for %s\n", so -> so_name); | |
643 | } | |
644 | } | |
645 | else | |
646 | { | |
647 | so -> symbols_loaded = 1; | |
648 | so -> from_tty = from_tty; | |
649 | catch_errors (symbol_add_stub, (char *) so, | |
650 | "Error while reading shared library symbols:\n"); | |
651 | } | |
652 | } | |
653 | } | |
654 | ||
bdbd5f50 JG |
655 | /* Now add the shared library sections to the section table of the |
656 | specified target, if any. */ | |
f8b76e70 FF |
657 | if (target) |
658 | { | |
659 | /* Count how many new section_table entries there are. */ | |
660 | so = NULL; | |
661 | count = 0; | |
662 | while ((so = find_solib (so)) != NULL) | |
663 | { | |
664 | if (so -> so_name[0]) | |
665 | { | |
666 | count += so -> sections_end - so -> sections; | |
667 | } | |
668 | } | |
669 | ||
670 | if (count) | |
671 | { | |
672 | /* Reallocate the target's section table including the new size. */ | |
ee0613d1 | 673 | if (target -> to_sections) |
f8b76e70 | 674 | { |
ee0613d1 JG |
675 | old = target -> to_sections_end - target -> to_sections; |
676 | target -> to_sections = (struct section_table *) | |
677 | realloc ((char *)target -> to_sections, | |
f8b76e70 FF |
678 | (sizeof (struct section_table)) * (count + old)); |
679 | } | |
680 | else | |
681 | { | |
682 | old = 0; | |
ee0613d1 | 683 | target -> to_sections = (struct section_table *) |
f8b76e70 FF |
684 | malloc ((sizeof (struct section_table)) * count); |
685 | } | |
ee0613d1 | 686 | target -> to_sections_end = target -> to_sections + (count + old); |
f8b76e70 FF |
687 | |
688 | /* Add these section table entries to the target's table. */ | |
689 | while ((so = find_solib (so)) != NULL) | |
690 | { | |
691 | if (so -> so_name[0]) | |
692 | { | |
693 | count = so -> sections_end - so -> sections; | |
ee0613d1 | 694 | bcopy (so -> sections, (char *)(target -> to_sections + old), |
f8b76e70 FF |
695 | (sizeof (struct section_table)) * count); |
696 | old += count; | |
697 | } | |
698 | } | |
699 | } | |
700 | } | |
bd5635a1 | 701 | } |
bdbd5f50 | 702 | |
f8b76e70 | 703 | /* |
bd5635a1 | 704 | |
f8b76e70 FF |
705 | LOCAL FUNCTION |
706 | ||
707 | info_sharedlibrary_command -- code for "info sharedlibrary" | |
708 | ||
709 | SYNOPSIS | |
710 | ||
711 | static void info_sharedlibrary_command () | |
712 | ||
713 | DESCRIPTION | |
bd5635a1 | 714 | |
f8b76e70 FF |
715 | Walk through the shared library list and print information |
716 | about each attached library. | |
717 | */ | |
718 | ||
719 | static void | |
720 | info_sharedlibrary_command () | |
721 | { | |
722 | register struct so_list *so = NULL; /* link map state variable */ | |
723 | int header_done = 0; | |
724 | ||
725 | if (exec_bfd == NULL) | |
726 | { | |
727 | printf ("No exec file.\n"); | |
728 | return; | |
729 | } | |
730 | while ((so = find_solib (so)) != NULL) | |
731 | { | |
732 | if (so -> so_name[0]) | |
733 | { | |
734 | if (!header_done) | |
735 | { | |
736 | printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read", | |
737 | "Shared Object Library"); | |
738 | header_done++; | |
739 | } | |
740 | printf ("%-12s", local_hex_string_custom (LM_ADDR (so), "08")); | |
741 | printf ("%-12s", local_hex_string_custom (so -> lmend, "08")); | |
742 | printf ("%-12s", so -> symbols_loaded ? "Yes" : "No"); | |
743 | printf ("%s\n", so -> so_name); | |
bd5635a1 | 744 | } |
bd5635a1 | 745 | } |
f8b76e70 FF |
746 | if (so_list_head == NULL) |
747 | { | |
748 | printf ("No shared libraries loaded at this time.\n"); | |
bd5635a1 RP |
749 | } |
750 | } | |
751 | ||
752 | /* | |
f8b76e70 FF |
753 | |
754 | GLOBAL FUNCTION | |
755 | ||
756 | solib_address -- check to see if an address is in a shared lib | |
757 | ||
758 | SYNOPSIS | |
759 | ||
760 | int solib_address (CORE_ADDR address) | |
761 | ||
762 | DESCRIPTION | |
763 | ||
764 | Provides a hook for other gdb routines to discover whether or | |
765 | not a particular address is within the mapped address space of | |
766 | a shared library. Any address between the base mapping address | |
767 | and the first address beyond the end of the last mapping, is | |
768 | considered to be within the shared library address space, for | |
769 | our purposes. | |
770 | ||
771 | For example, this routine is called at one point to disable | |
772 | breakpoints which are in shared libraries that are not currently | |
773 | mapped in. | |
774 | */ | |
775 | ||
bd5635a1 | 776 | int |
f8b76e70 | 777 | solib_address (address) |
bd5635a1 RP |
778 | CORE_ADDR address; |
779 | { | |
f8b76e70 FF |
780 | register struct so_list *so = 0; /* link map state variable */ |
781 | ||
782 | while ((so = find_solib (so)) != NULL) | |
783 | { | |
784 | if (so -> so_name[0]) | |
785 | { | |
786 | if ((address >= (CORE_ADDR) LM_ADDR (so)) && | |
787 | (address < (CORE_ADDR) so -> lmend)) | |
788 | { | |
789 | return (1); | |
790 | } | |
791 | } | |
792 | } | |
793 | return (0); | |
794 | } | |
795 | ||
796 | /* Called by free_all_symtabs */ | |
bd5635a1 | 797 | |
f8b76e70 FF |
798 | void |
799 | clear_solib() | |
800 | { | |
801 | struct so_list *next; | |
802 | ||
803 | while (so_list_head) | |
804 | { | |
805 | if (so_list_head -> sections) | |
806 | { | |
807 | free (so_list_head -> sections); | |
808 | } | |
809 | if (so_list_head -> so_bfd) | |
810 | { | |
811 | bfd_close (so_list_head -> so_bfd); | |
812 | } | |
813 | next = so_list_head -> next; | |
814 | free(so_list_head); | |
815 | so_list_head = next; | |
bd5635a1 | 816 | } |
f8b76e70 | 817 | debug_base = 0; |
bd5635a1 RP |
818 | } |
819 | ||
820 | /* | |
f8b76e70 FF |
821 | |
822 | LOCAL FUNCTION | |
823 | ||
824 | disable_break -- remove the "mapping changed" breakpoint | |
825 | ||
826 | SYNOPSIS | |
827 | ||
828 | static int disable_break () | |
829 | ||
830 | DESCRIPTION | |
831 | ||
832 | Removes the breakpoint that gets hit when the dynamic linker | |
833 | completes a mapping change. | |
834 | ||
bd5635a1 | 835 | */ |
f8b76e70 FF |
836 | |
837 | static int | |
838 | disable_break () | |
bd5635a1 | 839 | { |
f8b76e70 FF |
840 | int status = 1; |
841 | ||
d261ece7 | 842 | #ifndef SVR4_SHARED_LIBS |
f8b76e70 FF |
843 | |
844 | int in_debugger = 0; | |
845 | ||
f8b76e70 FF |
846 | /* Read the debugger structure from the inferior to retrieve the |
847 | address of the breakpoint and the original contents of the | |
848 | breakpoint address. Remove the breakpoint by writing the original | |
849 | contents back. */ | |
850 | ||
851 | read_memory (debug_addr, &debug_copy, sizeof (debug_copy)); | |
d261ece7 SG |
852 | |
853 | /* Get common symbol definitions for the loaded object. */ | |
854 | if (debug_copy.ldd_cp) | |
855 | solib_add_common_symbols (debug_copy.ldd_cp); | |
856 | ||
857 | /* Set `in_debugger' to zero now. */ | |
858 | ||
859 | write_memory (flag_addr, &in_debugger, sizeof (in_debugger)); | |
860 | ||
f8b76e70 FF |
861 | breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr; |
862 | write_memory (breakpoint_addr, &debug_copy.ldd_bp_inst, | |
863 | sizeof (debug_copy.ldd_bp_inst)); | |
864 | ||
d261ece7 | 865 | #else /* SVR4_SHARED_LIBS */ |
f8b76e70 FF |
866 | |
867 | /* Note that breakpoint address and original contents are in our address | |
868 | space, so we just need to write the original contents back. */ | |
869 | ||
870 | if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0) | |
871 | { | |
872 | status = 0; | |
873 | } | |
874 | ||
d261ece7 | 875 | #endif /* !SVR4_SHARED_LIBS */ |
f8b76e70 FF |
876 | |
877 | /* For the SVR4 version, we always know the breakpoint address. For the | |
878 | SunOS version we don't know it until the above code is executed. | |
879 | Grumble if we are stopped anywhere besides the breakpoint address. */ | |
880 | ||
881 | if (stop_pc != breakpoint_addr) | |
882 | { | |
883 | warning ("stopped at unknown breakpoint while handling shared libraries"); | |
884 | } | |
885 | ||
886 | return (status); | |
bdbd5f50 JG |
887 | } |
888 | ||
f8b76e70 | 889 | /* |
bdbd5f50 | 890 | |
f8b76e70 FF |
891 | LOCAL FUNCTION |
892 | ||
893 | enable_break -- arrange for dynamic linker to hit breakpoint | |
894 | ||
895 | SYNOPSIS | |
896 | ||
897 | int enable_break (void) | |
898 | ||
899 | DESCRIPTION | |
900 | ||
901 | Both the SunOS and the SVR4 dynamic linkers have, as part of their | |
902 | debugger interface, support for arranging for the inferior to hit | |
903 | a breakpoint after mapping in the shared libraries. This function | |
904 | enables that breakpoint. | |
905 | ||
906 | For SunOS, there is a special flag location (in_debugger) which we | |
907 | set to 1. When the dynamic linker sees this flag set, it will set | |
908 | a breakpoint at a location known only to itself, after saving the | |
909 | original contents of that place and the breakpoint address itself, | |
910 | in it's own internal structures. When we resume the inferior, it | |
911 | will eventually take a SIGTRAP when it runs into the breakpoint. | |
912 | We handle this (in a different place) by restoring the contents of | |
913 | the breakpointed location (which is only known after it stops), | |
914 | chasing around to locate the shared libraries that have been | |
915 | loaded, then resuming. | |
916 | ||
917 | For SVR4, the debugger interface structure contains a member (r_brk) | |
918 | which is statically initialized at the time the shared library is | |
919 | built, to the offset of a function (_r_debug_state) which is guaran- | |
920 | teed to be called once before mapping in a library, and again when | |
921 | the mapping is complete. At the time we are examining this member, | |
922 | it contains only the unrelocated offset of the function, so we have | |
923 | to do our own relocation. Later, when the dynamic linker actually | |
924 | runs, it relocates r_brk to be the actual address of _r_debug_state(). | |
925 | ||
926 | The debugger interface structure also contains an enumeration which | |
927 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, | |
928 | depending upon whether or not the library is being mapped or unmapped, | |
929 | and then set to RT_CONSISTENT after the library is mapped/unmapped. | |
930 | */ | |
931 | ||
932 | static int | |
933 | enable_break () | |
bdbd5f50 | 934 | { |
bdbd5f50 | 935 | |
f8b76e70 FF |
936 | int j; |
937 | ||
d261ece7 | 938 | #ifndef SVR4_SHARED_LIBS |
bdbd5f50 | 939 | |
f8b76e70 FF |
940 | int in_debugger; |
941 | ||
bdbd5f50 | 942 | /* Get link_dynamic structure */ |
f8b76e70 FF |
943 | |
944 | j = target_read_memory (debug_base, (char *) &dynamic_copy, | |
945 | sizeof (dynamic_copy)); | |
946 | if (j) | |
947 | { | |
948 | /* unreadable */ | |
949 | return (0); | |
950 | } | |
06b6c733 | 951 | |
bdbd5f50 | 952 | /* Calc address of debugger interface structure */ |
f8b76e70 FF |
953 | |
954 | debug_addr = (CORE_ADDR) dynamic_copy.ldd; | |
955 | ||
bdbd5f50 | 956 | /* Calc address of `in_debugger' member of debugger interface structure */ |
f8b76e70 FF |
957 | |
958 | flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger - | |
959 | (char *) &debug_copy); | |
960 | ||
bdbd5f50 | 961 | /* Write a value of 1 to this member. */ |
f8b76e70 | 962 | |
bdbd5f50 | 963 | in_debugger = 1; |
bdbd5f50 | 964 | |
f8b76e70 FF |
965 | write_memory (flag_addr, &in_debugger, sizeof (in_debugger)); |
966 | ||
d261ece7 | 967 | #else /* SVR4_SHARED_LIBS */ |
f8b76e70 FF |
968 | |
969 | #ifdef BKPT_AT_MAIN | |
970 | ||
971 | int i; | |
972 | ||
973 | i = lookup_misc_func ("main"); | |
974 | if (i >= 0 && misc_function_vector[i].address != 0) | |
975 | { | |
976 | breakpoint_addr = misc_function_vector[i].address; | |
977 | } | |
978 | else | |
979 | { | |
980 | return (0); | |
981 | } | |
982 | ||
983 | if (target_insert_breakpoint (breakpoint_addr, shadow_contents) != 0) | |
984 | { | |
985 | return (0); | |
986 | } | |
987 | ||
988 | #else /* !BKPT_AT_MAIN */ | |
989 | ||
990 | struct symtab_and_line sal; | |
991 | ||
992 | /* Read the debugger interface structure directly. */ | |
993 | ||
994 | read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy)); | |
995 | ||
996 | /* Set breakpoint at the debugger interface stub routine that will | |
997 | be called just prior to each mapping change and again after the | |
998 | mapping change is complete. Set up the (nonexistent) handler to | |
999 | deal with hitting these breakpoints. (FIXME). */ | |
1000 | ||
1001 | warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__); | |
1002 | ||
1003 | #endif /* BKPT_AT_MAIN */ | |
1004 | ||
d261ece7 | 1005 | #endif /* !SVR4_SHARED_LIBS */ |
f8b76e70 FF |
1006 | |
1007 | return (1); | |
1008 | } | |
1009 | ||
1010 | /* | |
1011 | ||
1012 | GLOBAL FUNCTION | |
1013 | ||
1014 | solib_create_inferior_hook -- shared library startup support | |
1015 | ||
1016 | SYNOPSIS | |
1017 | ||
1018 | void solib_create_inferior_hook() | |
1019 | ||
1020 | DESCRIPTION | |
1021 | ||
1022 | When gdb starts up the inferior, it nurses it along (through the | |
1023 | shell) until it is ready to execute it's first instruction. At this | |
1024 | point, this function gets called via expansion of the macro | |
1025 | SOLIB_CREATE_INFERIOR_HOOK. | |
1026 | ||
1027 | For both SunOS shared libraries, and SVR4 shared libraries, we | |
1028 | can arrange to cooperate with the dynamic linker to discover the | |
1029 | names of shared libraries that are dynamically linked, and the | |
1030 | base addresses to which they are linked. | |
1031 | ||
1032 | This function is responsible for discovering those names and | |
1033 | addresses, and saving sufficient information about them to allow | |
1034 | their symbols to be read at a later time. | |
1035 | ||
1036 | FIXME | |
1037 | ||
1038 | Between enable_break() and disable_break(), this code does not | |
1039 | properly handle hitting breakpoints which the user might have | |
1040 | set in the startup code or in the dynamic linker itself. Proper | |
1041 | handling will probably have to wait until the implementation is | |
1042 | changed to use the "breakpoint handler function" method. | |
1043 | ||
1044 | Also, what if child has exit()ed? Must exit loop somehow. | |
1045 | */ | |
1046 | ||
1047 | void | |
1048 | solib_create_inferior_hook() | |
1049 | { | |
1050 | CORE_ADDR debug_addr; | |
1051 | int in_debugger; | |
1052 | CORE_ADDR in_debugger_addr; | |
1053 | CORE_ADDR breakpoint_addr; | |
1054 | int i, j; | |
1055 | ||
1056 | if ((debug_base = locate_base ()) == 0) | |
1057 | { | |
1058 | /* Can't find the symbol or the executable is statically linked. */ | |
1059 | return; | |
1060 | } | |
1061 | ||
1062 | if (!enable_break ()) | |
1063 | { | |
1064 | warning ("shared library handler failed to enable breakpoint"); | |
1065 | return; | |
1066 | } | |
1067 | ||
1068 | /* Now run the target. It will eventually hit the breakpoint, at | |
1069 | which point all of the libraries will have been mapped in and we | |
1070 | can go groveling around in the dynamic linker structures to find | |
1071 | out what we need to know about them. */ | |
bdbd5f50 JG |
1072 | |
1073 | clear_proceed_status (); | |
1074 | stop_soon_quietly = 1; | |
f8b76e70 FF |
1075 | stop_signal = 0; |
1076 | do | |
bdbd5f50 | 1077 | { |
bdbd5f50 JG |
1078 | target_resume (0, stop_signal); |
1079 | wait_for_inferior (); | |
1080 | } | |
f8b76e70 | 1081 | while (stop_signal != SIGTRAP); |
bdbd5f50 | 1082 | stop_soon_quietly = 0; |
f8b76e70 FF |
1083 | |
1084 | /* We are now either at the "mapping complete" breakpoint (or somewhere | |
1085 | else, a condition we aren't prepared to deal with anyway), so adjust | |
1086 | the PC as necessary after a breakpoint, disable the breakpoint, and | |
1087 | add any shared libraries that were mapped in. */ | |
bdbd5f50 | 1088 | |
f8b76e70 FF |
1089 | if (DECR_PC_AFTER_BREAK) |
1090 | { | |
1091 | stop_pc -= DECR_PC_AFTER_BREAK; | |
1092 | write_register (PC_REGNUM, stop_pc); | |
1093 | } | |
1094 | ||
1095 | if (!disable_break ()) | |
1096 | { | |
1097 | warning ("shared library handler failed to disable breakpoint"); | |
1098 | } | |
1099 | ||
1100 | solib_add ((char *) 0, 0, (struct target_ops *) 0); | |
bdbd5f50 JG |
1101 | } |
1102 | ||
f8b76e70 FF |
1103 | /* |
1104 | ||
1105 | GLOBAL FUNCTION | |
1106 | ||
1107 | sharedlibrary_command -- handle command to explicitly add library | |
1108 | ||
1109 | SYNOPSIS | |
1110 | ||
1111 | void sharedlibrary_command (char *args, int from_tty) | |
1112 | ||
1113 | DESCRIPTION | |
1114 | ||
1115 | */ | |
1116 | ||
bdbd5f50 JG |
1117 | void |
1118 | sharedlibrary_command (args, from_tty) | |
f8b76e70 FF |
1119 | char *args; |
1120 | int from_tty; | |
bdbd5f50 | 1121 | { |
f8b76e70 FF |
1122 | dont_repeat (); |
1123 | solib_add (args, from_tty, (struct target_ops *) 0); | |
bd5635a1 RP |
1124 | } |
1125 | ||
1126 | void | |
1127 | _initialize_solib() | |
1128 | { | |
f8b76e70 FF |
1129 | |
1130 | add_com ("sharedlibrary", class_files, sharedlibrary_command, | |
bd5635a1 | 1131 | "Load shared object library symbols for files matching REGEXP."); |
f8b76e70 FF |
1132 | add_info ("sharedlibrary", info_sharedlibrary_command, |
1133 | "Status of loaded shared object libraries."); | |
bd5635a1 | 1134 | } |