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