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