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