Commit | Line | Data |
---|---|---|
ab31aa69 KB |
1 | /* Handle SunOS shared libraries for GDB, the GNU Debugger. |
2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, | |
3 | 2001 | |
4 | Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
24 | ||
25 | #include <sys/types.h> | |
26 | #include <signal.h> | |
27 | #include "gdb_string.h" | |
28 | #include <sys/param.h> | |
29 | #include <fcntl.h> | |
30 | ||
31 | /* SunOS shared libs need the nlist structure. */ | |
32 | #include <a.out.h> | |
33 | #include <link.h> | |
34 | ||
35 | #include "symtab.h" | |
36 | #include "bfd.h" | |
37 | #include "symfile.h" | |
38 | #include "objfiles.h" | |
39 | #include "gdbcore.h" | |
40 | #include "inferior.h" | |
41 | #include "solist.h" | |
42 | ||
43 | /* Link map info to include in an allocated so_list entry */ | |
44 | ||
45 | struct lm_info | |
46 | { | |
47 | /* Pointer to copy of link map from inferior. The type is char * | |
48 | rather than void *, so that we may use byte offsets to find the | |
49 | various fields without the need for a cast. */ | |
50 | char *lm; | |
51 | }; | |
52 | ||
53 | ||
54 | /* Symbols which are used to locate the base of the link map structures. */ | |
55 | ||
56 | static char *debug_base_symbols[] = | |
57 | { | |
58 | "_DYNAMIC", | |
59 | "_DYNAMIC__MGC", | |
60 | NULL | |
61 | }; | |
62 | ||
63 | static char *main_name_list[] = | |
64 | { | |
65 | "main_$main", | |
66 | NULL | |
67 | }; | |
68 | ||
69 | /* Macro to extract an address from a solib structure. | |
70 | When GDB is configured for some 32-bit targets (e.g. Solaris 2.7 | |
71 | sparc), BFD is configured to handle 64-bit targets, so CORE_ADDR is | |
72 | 64 bits. We have to extract only the significant bits of addresses | |
73 | to get the right address when accessing the core file BFD. */ | |
74 | ||
75 | #define SOLIB_EXTRACT_ADDRESS(MEMBER) \ | |
76 | extract_address (&(MEMBER), sizeof (MEMBER)) | |
77 | ||
78 | /* local data declarations */ | |
79 | ||
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 | ||
86 | #ifndef offsetof | |
87 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
88 | #endif | |
89 | #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER)) | |
90 | ||
91 | /* link map access functions */ | |
92 | ||
93 | static CORE_ADDR | |
94 | LM_ADDR (struct so_list *so) | |
95 | { | |
96 | int lm_addr_offset = offsetof (struct link_map, lm_addr); | |
97 | int lm_addr_size = fieldsize (struct link_map, lm_addr); | |
98 | ||
99 | return (CORE_ADDR) extract_signed_integer (so->lm_info->lm + lm_addr_offset, | |
100 | lm_addr_size); | |
101 | } | |
102 | ||
103 | static CORE_ADDR | |
104 | LM_NEXT (struct so_list *so) | |
105 | { | |
106 | int lm_next_offset = offsetof (struct link_map, lm_next); | |
107 | int lm_next_size = fieldsize (struct link_map, lm_next); | |
108 | ||
109 | return extract_address (so->lm_info->lm + lm_next_offset, lm_next_size); | |
110 | } | |
111 | ||
112 | static CORE_ADDR | |
113 | LM_NAME (struct so_list *so) | |
114 | { | |
115 | int lm_name_offset = offsetof (struct link_map, lm_name); | |
116 | int lm_name_size = fieldsize (struct link_map, lm_name); | |
117 | ||
118 | return extract_address (so->lm_info->lm + lm_name_offset, lm_name_size); | |
119 | } | |
120 | ||
121 | static CORE_ADDR debug_base; /* Base of dynamic linker structures */ | |
122 | ||
123 | /* Local function prototypes */ | |
124 | ||
125 | static int match_main (char *); | |
126 | ||
127 | /* Allocate the runtime common object file. */ | |
128 | ||
129 | static void | |
130 | allocate_rt_common_objfile (void) | |
131 | { | |
132 | struct objfile *objfile; | |
133 | struct objfile *last_one; | |
134 | ||
135 | objfile = (struct objfile *) xmalloc (sizeof (struct objfile)); | |
136 | memset (objfile, 0, sizeof (struct objfile)); | |
137 | objfile->md = NULL; | |
138 | obstack_specify_allocation (&objfile->psymbol_cache.cache, 0, 0, | |
139 | xmalloc, xfree); | |
140 | obstack_specify_allocation (&objfile->psymbol_obstack, 0, 0, xmalloc, | |
141 | xfree); | |
142 | obstack_specify_allocation (&objfile->symbol_obstack, 0, 0, xmalloc, | |
143 | xfree); | |
144 | obstack_specify_allocation (&objfile->type_obstack, 0, 0, xmalloc, | |
145 | xfree); | |
146 | objfile->name = mstrsave (objfile->md, "rt_common"); | |
147 | ||
148 | /* Add this file onto the tail of the linked list of other such files. */ | |
149 | ||
150 | objfile->next = NULL; | |
151 | if (object_files == NULL) | |
152 | object_files = objfile; | |
153 | else | |
154 | { | |
155 | for (last_one = object_files; | |
156 | last_one->next; | |
157 | last_one = last_one->next); | |
158 | last_one->next = objfile; | |
159 | } | |
160 | ||
161 | rt_common_objfile = objfile; | |
162 | } | |
163 | ||
164 | /* Read all dynamically loaded common symbol definitions from the inferior | |
165 | and put them into the minimal symbol table for the runtime common | |
166 | objfile. */ | |
167 | ||
168 | static void | |
169 | solib_add_common_symbols (CORE_ADDR rtc_symp) | |
170 | { | |
171 | struct rtc_symb inferior_rtc_symb; | |
172 | struct nlist inferior_rtc_nlist; | |
173 | int len; | |
174 | char *name; | |
175 | ||
176 | /* Remove any runtime common symbols from previous runs. */ | |
177 | ||
178 | if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count) | |
179 | { | |
180 | obstack_free (&rt_common_objfile->symbol_obstack, 0); | |
181 | obstack_specify_allocation (&rt_common_objfile->symbol_obstack, 0, 0, | |
182 | xmalloc, xfree); | |
183 | rt_common_objfile->minimal_symbol_count = 0; | |
184 | rt_common_objfile->msymbols = NULL; | |
185 | } | |
186 | ||
187 | init_minimal_symbol_collection (); | |
188 | make_cleanup_discard_minimal_symbols (); | |
189 | ||
190 | while (rtc_symp) | |
191 | { | |
192 | read_memory (rtc_symp, | |
193 | (char *) &inferior_rtc_symb, | |
194 | sizeof (inferior_rtc_symb)); | |
195 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp), | |
196 | (char *) &inferior_rtc_nlist, | |
197 | sizeof (inferior_rtc_nlist)); | |
198 | if (inferior_rtc_nlist.n_type == N_COMM) | |
199 | { | |
200 | /* FIXME: The length of the symbol name is not available, but in the | |
201 | current implementation the common symbol is allocated immediately | |
202 | behind the name of the symbol. */ | |
203 | len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx; | |
204 | ||
205 | name = xmalloc (len); | |
206 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name), | |
207 | name, len); | |
208 | ||
209 | /* Allocate the runtime common objfile if necessary. */ | |
210 | if (rt_common_objfile == NULL) | |
211 | allocate_rt_common_objfile (); | |
212 | ||
213 | prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value, | |
214 | mst_bss, rt_common_objfile); | |
215 | xfree (name); | |
216 | } | |
217 | rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next); | |
218 | } | |
219 | ||
220 | /* Install any minimal symbols that have been collected as the current | |
221 | minimal symbols for the runtime common objfile. */ | |
222 | ||
223 | install_minimal_symbols (rt_common_objfile); | |
224 | } | |
225 | ||
226 | ||
227 | /* | |
228 | ||
229 | LOCAL FUNCTION | |
230 | ||
231 | locate_base -- locate the base address of dynamic linker structs | |
232 | ||
233 | SYNOPSIS | |
234 | ||
235 | CORE_ADDR locate_base (void) | |
236 | ||
237 | DESCRIPTION | |
238 | ||
239 | For both the SunOS and SVR4 shared library implementations, if the | |
240 | inferior executable has been linked dynamically, there is a single | |
241 | address somewhere in the inferior's data space which is the key to | |
242 | locating all of the dynamic linker's runtime structures. This | |
243 | address is the value of the debug base symbol. The job of this | |
244 | function is to find and return that address, or to return 0 if there | |
245 | is no such address (the executable is statically linked for example). | |
246 | ||
247 | For SunOS, the job is almost trivial, since the dynamic linker and | |
248 | all of it's structures are statically linked to the executable at | |
249 | link time. Thus the symbol for the address we are looking for has | |
250 | already been added to the minimal symbol table for the executable's | |
251 | objfile at the time the symbol file's symbols were read, and all we | |
252 | have to do is look it up there. Note that we explicitly do NOT want | |
253 | to find the copies in the shared library. | |
254 | ||
255 | The SVR4 version is a bit more complicated because the address | |
256 | is contained somewhere in the dynamic info section. We have to go | |
257 | to a lot more work to discover the address of the debug base symbol. | |
258 | Because of this complexity, we cache the value we find and return that | |
259 | value on subsequent invocations. Note there is no copy in the | |
260 | executable symbol tables. | |
261 | ||
262 | */ | |
263 | ||
264 | static CORE_ADDR | |
265 | locate_base (void) | |
266 | { | |
267 | struct minimal_symbol *msymbol; | |
268 | CORE_ADDR address = 0; | |
269 | char **symbolp; | |
270 | ||
271 | /* For SunOS, we want to limit the search for the debug base symbol to the | |
272 | executable being debugged, since there is a duplicate named symbol in the | |
273 | shared library. We don't want the shared library versions. */ | |
274 | ||
275 | for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++) | |
276 | { | |
277 | msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile); | |
278 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) | |
279 | { | |
280 | address = SYMBOL_VALUE_ADDRESS (msymbol); | |
281 | return (address); | |
282 | } | |
283 | } | |
284 | return (0); | |
285 | } | |
286 | ||
287 | /* | |
288 | ||
289 | LOCAL FUNCTION | |
290 | ||
291 | first_link_map_member -- locate first member in dynamic linker's map | |
292 | ||
293 | SYNOPSIS | |
294 | ||
295 | static CORE_ADDR first_link_map_member (void) | |
296 | ||
297 | DESCRIPTION | |
298 | ||
299 | Find the first element in the inferior's dynamic link map, and | |
300 | return its address in the inferior. This function doesn't copy the | |
301 | link map entry itself into our address space; current_sos actually | |
302 | does the reading. */ | |
303 | ||
304 | static CORE_ADDR | |
305 | first_link_map_member (void) | |
306 | { | |
307 | CORE_ADDR lm = 0; | |
308 | ||
309 | read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy)); | |
310 | if (dynamic_copy.ld_version >= 2) | |
311 | { | |
312 | /* It is a version that we can deal with, so read in the secondary | |
313 | structure and find the address of the link map list from it. */ | |
314 | read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2), | |
315 | (char *) &ld_2_copy, sizeof (struct link_dynamic_2)); | |
316 | lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded); | |
317 | } | |
318 | return (lm); | |
319 | } | |
320 | ||
321 | static int | |
322 | open_symbol_file_object (void *from_ttyp) | |
323 | { | |
324 | return 1; | |
325 | } | |
326 | ||
327 | ||
328 | /* LOCAL FUNCTION | |
329 | ||
330 | current_sos -- build a list of currently loaded shared objects | |
331 | ||
332 | SYNOPSIS | |
333 | ||
334 | struct so_list *current_sos () | |
335 | ||
336 | DESCRIPTION | |
337 | ||
338 | Build a list of `struct so_list' objects describing the shared | |
339 | objects currently loaded in the inferior. This list does not | |
340 | include an entry for the main executable file. | |
341 | ||
342 | Note that we only gather information directly available from the | |
343 | inferior --- we don't examine any of the shared library files | |
344 | themselves. The declaration of `struct so_list' says which fields | |
345 | we provide values for. */ | |
346 | ||
347 | static struct so_list * | |
348 | sunos_current_sos (void) | |
349 | { | |
350 | CORE_ADDR lm; | |
351 | struct so_list *head = 0; | |
352 | struct so_list **link_ptr = &head; | |
353 | int errcode; | |
354 | char *buffer; | |
355 | ||
356 | /* Make sure we've looked up the inferior's dynamic linker's base | |
357 | structure. */ | |
358 | if (! debug_base) | |
359 | { | |
360 | debug_base = locate_base (); | |
361 | ||
362 | /* If we can't find the dynamic linker's base structure, this | |
363 | must not be a dynamically linked executable. Hmm. */ | |
364 | if (! debug_base) | |
365 | return 0; | |
366 | } | |
367 | ||
368 | /* Walk the inferior's link map list, and build our list of | |
369 | `struct so_list' nodes. */ | |
370 | lm = first_link_map_member (); | |
371 | while (lm) | |
372 | { | |
373 | struct so_list *new | |
374 | = (struct so_list *) xmalloc (sizeof (struct so_list)); | |
375 | struct cleanup *old_chain = make_cleanup (xfree, new); | |
376 | ||
377 | memset (new, 0, sizeof (*new)); | |
378 | ||
379 | new->lm_info = xmalloc (sizeof (struct lm_info)); | |
380 | make_cleanup (xfree, new->lm_info); | |
381 | ||
382 | new->lm_info->lm = xmalloc (sizeof (struct link_map)); | |
383 | make_cleanup (xfree, new->lm_info->lm); | |
384 | memset (new->lm_info->lm, 0, sizeof (struct link_map)); | |
385 | ||
386 | read_memory (lm, new->lm_info->lm, sizeof (struct link_map)); | |
387 | ||
388 | lm = LM_NEXT (new); | |
389 | ||
390 | /* Extract this shared object's name. */ | |
391 | target_read_string (LM_NAME (new), &buffer, | |
392 | SO_NAME_MAX_PATH_SIZE - 1, &errcode); | |
393 | if (errcode != 0) | |
394 | { | |
395 | warning ("current_sos: Can't read pathname for load map: %s\n", | |
396 | safe_strerror (errcode)); | |
397 | } | |
398 | else | |
399 | { | |
400 | strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1); | |
401 | new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; | |
402 | xfree (buffer); | |
403 | strcpy (new->so_original_name, new->so_name); | |
404 | } | |
405 | ||
406 | /* If this entry has no name, or its name matches the name | |
407 | for the main executable, don't include it in the list. */ | |
408 | if (! new->so_name[0] | |
409 | || match_main (new->so_name)) | |
410 | free_so (new); | |
411 | else | |
412 | { | |
413 | new->next = 0; | |
414 | *link_ptr = new; | |
415 | link_ptr = &new->next; | |
416 | } | |
417 | ||
418 | discard_cleanups (old_chain); | |
419 | } | |
420 | ||
421 | return head; | |
422 | } | |
423 | ||
424 | ||
425 | /* On some systems, the only way to recognize the link map entry for | |
426 | the main executable file is by looking at its name. Return | |
427 | non-zero iff SONAME matches one of the known main executable names. */ | |
428 | ||
429 | static int | |
430 | match_main (char *soname) | |
431 | { | |
432 | char **mainp; | |
433 | ||
434 | for (mainp = main_name_list; *mainp != NULL; mainp++) | |
435 | { | |
436 | if (strcmp (soname, *mainp) == 0) | |
437 | return (1); | |
438 | } | |
439 | ||
440 | return (0); | |
441 | } | |
442 | ||
443 | ||
444 | static int | |
445 | sunos_in_dynsym_resolve_code (CORE_ADDR pc) | |
446 | { | |
447 | return 0; | |
448 | } | |
449 | ||
450 | /* | |
451 | ||
452 | LOCAL FUNCTION | |
453 | ||
454 | disable_break -- remove the "mapping changed" breakpoint | |
455 | ||
456 | SYNOPSIS | |
457 | ||
458 | static int disable_break () | |
459 | ||
460 | DESCRIPTION | |
461 | ||
462 | Removes the breakpoint that gets hit when the dynamic linker | |
463 | completes a mapping change. | |
464 | ||
465 | */ | |
466 | ||
467 | static int | |
468 | disable_break (void) | |
469 | { | |
470 | CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */ | |
471 | ||
472 | int in_debugger = 0; | |
473 | ||
474 | /* Read the debugger structure from the inferior to retrieve the | |
475 | address of the breakpoint and the original contents of the | |
476 | breakpoint address. Remove the breakpoint by writing the original | |
477 | contents back. */ | |
478 | ||
479 | read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy)); | |
480 | ||
481 | /* Set `in_debugger' to zero now. */ | |
482 | ||
483 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); | |
484 | ||
485 | breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr); | |
486 | write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst, | |
487 | sizeof (debug_copy.ldd_bp_inst)); | |
488 | ||
489 | /* For the SVR4 version, we always know the breakpoint address. For the | |
490 | SunOS version we don't know it until the above code is executed. | |
491 | Grumble if we are stopped anywhere besides the breakpoint address. */ | |
492 | ||
493 | if (stop_pc != breakpoint_addr) | |
494 | { | |
495 | warning ("stopped at unknown breakpoint while handling shared libraries"); | |
496 | } | |
497 | ||
498 | return 1; | |
499 | } | |
500 | ||
501 | ||
502 | /* | |
503 | ||
504 | LOCAL FUNCTION | |
505 | ||
506 | enable_break -- arrange for dynamic linker to hit breakpoint | |
507 | ||
508 | SYNOPSIS | |
509 | ||
510 | int enable_break (void) | |
511 | ||
512 | DESCRIPTION | |
513 | ||
514 | Both the SunOS and the SVR4 dynamic linkers have, as part of their | |
515 | debugger interface, support for arranging for the inferior to hit | |
516 | a breakpoint after mapping in the shared libraries. This function | |
517 | enables that breakpoint. | |
518 | ||
519 | For SunOS, there is a special flag location (in_debugger) which we | |
520 | set to 1. When the dynamic linker sees this flag set, it will set | |
521 | a breakpoint at a location known only to itself, after saving the | |
522 | original contents of that place and the breakpoint address itself, | |
523 | in it's own internal structures. When we resume the inferior, it | |
524 | will eventually take a SIGTRAP when it runs into the breakpoint. | |
525 | We handle this (in a different place) by restoring the contents of | |
526 | the breakpointed location (which is only known after it stops), | |
527 | chasing around to locate the shared libraries that have been | |
528 | loaded, then resuming. | |
529 | ||
530 | For SVR4, the debugger interface structure contains a member (r_brk) | |
531 | which is statically initialized at the time the shared library is | |
532 | built, to the offset of a function (_r_debug_state) which is guaran- | |
533 | teed to be called once before mapping in a library, and again when | |
534 | the mapping is complete. At the time we are examining this member, | |
535 | it contains only the unrelocated offset of the function, so we have | |
536 | to do our own relocation. Later, when the dynamic linker actually | |
537 | runs, it relocates r_brk to be the actual address of _r_debug_state(). | |
538 | ||
539 | The debugger interface structure also contains an enumeration which | |
540 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, | |
541 | depending upon whether or not the library is being mapped or unmapped, | |
542 | and then set to RT_CONSISTENT after the library is mapped/unmapped. | |
543 | */ | |
544 | ||
545 | static int | |
546 | enable_break (void) | |
547 | { | |
548 | int success = 0; | |
549 | int j; | |
550 | int in_debugger; | |
551 | ||
552 | /* Get link_dynamic structure */ | |
553 | ||
554 | j = target_read_memory (debug_base, (char *) &dynamic_copy, | |
555 | sizeof (dynamic_copy)); | |
556 | if (j) | |
557 | { | |
558 | /* unreadable */ | |
559 | return (0); | |
560 | } | |
561 | ||
562 | /* Calc address of debugger interface structure */ | |
563 | ||
564 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); | |
565 | ||
566 | /* Calc address of `in_debugger' member of debugger interface structure */ | |
567 | ||
568 | flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger - | |
569 | (char *) &debug_copy); | |
570 | ||
571 | /* Write a value of 1 to this member. */ | |
572 | ||
573 | in_debugger = 1; | |
574 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); | |
575 | success = 1; | |
576 | ||
577 | return (success); | |
578 | } | |
579 | ||
580 | /* | |
581 | ||
582 | LOCAL FUNCTION | |
583 | ||
584 | special_symbol_handling -- additional shared library symbol handling | |
585 | ||
586 | SYNOPSIS | |
587 | ||
588 | void special_symbol_handling () | |
589 | ||
590 | DESCRIPTION | |
591 | ||
592 | Once the symbols from a shared object have been loaded in the usual | |
593 | way, we are called to do any system specific symbol handling that | |
594 | is needed. | |
595 | ||
596 | For SunOS4, this consists of grunging around in the dynamic | |
597 | linkers structures to find symbol definitions for "common" symbols | |
598 | and adding them to the minimal symbol table for the runtime common | |
599 | objfile. | |
600 | ||
601 | */ | |
602 | ||
603 | static void | |
604 | sunos_special_symbol_handling (void) | |
605 | { | |
606 | int j; | |
607 | ||
608 | if (debug_addr == 0) | |
609 | { | |
610 | /* Get link_dynamic structure */ | |
611 | ||
612 | j = target_read_memory (debug_base, (char *) &dynamic_copy, | |
613 | sizeof (dynamic_copy)); | |
614 | if (j) | |
615 | { | |
616 | /* unreadable */ | |
617 | return; | |
618 | } | |
619 | ||
620 | /* Calc address of debugger interface structure */ | |
621 | /* FIXME, this needs work for cross-debugging of core files | |
622 | (byteorder, size, alignment, etc). */ | |
623 | ||
624 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); | |
625 | } | |
626 | ||
627 | /* Read the debugger structure from the inferior, just to make sure | |
628 | we have a current copy. */ | |
629 | ||
630 | j = target_read_memory (debug_addr, (char *) &debug_copy, | |
631 | sizeof (debug_copy)); | |
632 | if (j) | |
633 | return; /* unreadable */ | |
634 | ||
635 | /* Get common symbol definitions for the loaded object. */ | |
636 | ||
637 | if (debug_copy.ldd_cp) | |
638 | { | |
639 | solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp)); | |
640 | } | |
641 | } | |
642 | ||
643 | /* Relocate the main executable. This function should be called upon | |
644 | stopping the inferior process at the entry point to the program. | |
645 | The entry point from BFD is compared to the PC and if they are | |
646 | different, the main executable is relocated by the proper amount. | |
647 | ||
648 | As written it will only attempt to relocate executables which | |
649 | lack interpreter sections. It seems likely that only dynamic | |
650 | linker executables will get relocated, though it should work | |
651 | properly for a position-independent static executable as well. */ | |
652 | ||
653 | static void | |
654 | sunos_relocate_main_executable (void) | |
655 | { | |
656 | asection *interp_sect; | |
657 | CORE_ADDR pc = read_pc (); | |
658 | ||
659 | /* Decide if the objfile needs to be relocated. As indicated above, | |
660 | we will only be here when execution is stopped at the beginning | |
661 | of the program. Relocation is necessary if the address at which | |
662 | we are presently stopped differs from the start address stored in | |
663 | the executable AND there's no interpreter section. The condition | |
664 | regarding the interpreter section is very important because if | |
665 | there *is* an interpreter section, execution will begin there | |
666 | instead. When there is an interpreter section, the start address | |
667 | is (presumably) used by the interpreter at some point to start | |
668 | execution of the program. | |
669 | ||
670 | If there is an interpreter, it is normal for it to be set to an | |
671 | arbitrary address at the outset. The job of finding it is | |
672 | handled in enable_break(). | |
673 | ||
674 | So, to summarize, relocations are necessary when there is no | |
675 | interpreter section and the start address obtained from the | |
676 | executable is different from the address at which GDB is | |
677 | currently stopped. | |
678 | ||
679 | [ The astute reader will note that we also test to make sure that | |
680 | the executable in question has the DYNAMIC flag set. It is my | |
681 | opinion that this test is unnecessary (undesirable even). It | |
682 | was added to avoid inadvertent relocation of an executable | |
683 | whose e_type member in the ELF header is not ET_DYN. There may | |
684 | be a time in the future when it is desirable to do relocations | |
685 | on other types of files as well in which case this condition | |
686 | should either be removed or modified to accomodate the new file | |
687 | type. (E.g, an ET_EXEC executable which has been built to be | |
688 | position-independent could safely be relocated by the OS if | |
689 | desired. It is true that this violates the ABI, but the ABI | |
690 | has been known to be bent from time to time.) - Kevin, Nov 2000. ] | |
691 | */ | |
692 | ||
693 | interp_sect = bfd_get_section_by_name (exec_bfd, ".interp"); | |
694 | if (interp_sect == NULL | |
695 | && (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0 | |
696 | && bfd_get_start_address (exec_bfd) != pc) | |
697 | { | |
698 | struct cleanup *old_chain; | |
699 | struct section_offsets *new_offsets; | |
700 | int i, changed; | |
701 | CORE_ADDR displacement; | |
702 | ||
703 | /* It is necessary to relocate the objfile. The amount to | |
704 | relocate by is simply the address at which we are stopped | |
705 | minus the starting address from the executable. | |
706 | ||
707 | We relocate all of the sections by the same amount. This | |
708 | behavior is mandated by recent editions of the System V ABI. | |
709 | According to the System V Application Binary Interface, | |
710 | Edition 4.1, page 5-5: | |
711 | ||
712 | ... Though the system chooses virtual addresses for | |
713 | individual processes, it maintains the segments' relative | |
714 | positions. Because position-independent code uses relative | |
715 | addressesing between segments, the difference between | |
716 | virtual addresses in memory must match the difference | |
717 | between virtual addresses in the file. The difference | |
718 | between the virtual address of any segment in memory and | |
719 | the corresponding virtual address in the file is thus a | |
720 | single constant value for any one executable or shared | |
721 | object in a given process. This difference is the base | |
722 | address. One use of the base address is to relocate the | |
723 | memory image of the program during dynamic linking. | |
724 | ||
725 | The same language also appears in Edition 4.0 of the System V | |
726 | ABI and is left unspecified in some of the earlier editions. */ | |
727 | ||
728 | displacement = pc - bfd_get_start_address (exec_bfd); | |
729 | changed = 0; | |
730 | ||
731 | new_offsets = xcalloc (sizeof (struct section_offsets), | |
732 | symfile_objfile->num_sections); | |
733 | old_chain = make_cleanup (xfree, new_offsets); | |
734 | ||
735 | for (i = 0; i < symfile_objfile->num_sections; i++) | |
736 | { | |
737 | if (displacement != ANOFFSET (symfile_objfile->section_offsets, i)) | |
738 | changed = 1; | |
739 | new_offsets->offsets[i] = displacement; | |
740 | } | |
741 | ||
742 | if (changed) | |
743 | objfile_relocate (symfile_objfile, new_offsets); | |
744 | ||
745 | do_cleanups (old_chain); | |
746 | } | |
747 | } | |
748 | ||
749 | /* | |
750 | ||
751 | GLOBAL FUNCTION | |
752 | ||
753 | sunos_solib_create_inferior_hook -- shared library startup support | |
754 | ||
755 | SYNOPSIS | |
756 | ||
757 | void sunos_solib_create_inferior_hook() | |
758 | ||
759 | DESCRIPTION | |
760 | ||
761 | When gdb starts up the inferior, it nurses it along (through the | |
762 | shell) until it is ready to execute it's first instruction. At this | |
763 | point, this function gets called via expansion of the macro | |
764 | SOLIB_CREATE_INFERIOR_HOOK. | |
765 | ||
766 | For SunOS executables, this first instruction is typically the | |
767 | one at "_start", or a similar text label, regardless of whether | |
768 | the executable is statically or dynamically linked. The runtime | |
769 | startup code takes care of dynamically linking in any shared | |
770 | libraries, once gdb allows the inferior to continue. | |
771 | ||
772 | For SVR4 executables, this first instruction is either the first | |
773 | instruction in the dynamic linker (for dynamically linked | |
774 | executables) or the instruction at "start" for statically linked | |
775 | executables. For dynamically linked executables, the system | |
776 | first exec's /lib/libc.so.N, which contains the dynamic linker, | |
777 | and starts it running. The dynamic linker maps in any needed | |
778 | shared libraries, maps in the actual user executable, and then | |
779 | jumps to "start" in the user executable. | |
780 | ||
781 | For both SunOS shared libraries, and SVR4 shared libraries, we | |
782 | can arrange to cooperate with the dynamic linker to discover the | |
783 | names of shared libraries that are dynamically linked, and the | |
784 | base addresses to which they are linked. | |
785 | ||
786 | This function is responsible for discovering those names and | |
787 | addresses, and saving sufficient information about them to allow | |
788 | their symbols to be read at a later time. | |
789 | ||
790 | FIXME | |
791 | ||
792 | Between enable_break() and disable_break(), this code does not | |
793 | properly handle hitting breakpoints which the user might have | |
794 | set in the startup code or in the dynamic linker itself. Proper | |
795 | handling will probably have to wait until the implementation is | |
796 | changed to use the "breakpoint handler function" method. | |
797 | ||
798 | Also, what if child has exit()ed? Must exit loop somehow. | |
799 | */ | |
800 | ||
801 | static void | |
802 | sunos_solib_create_inferior_hook (void) | |
803 | { | |
804 | /* Relocate the main executable if necessary. */ | |
805 | sunos_relocate_main_executable (); | |
806 | ||
807 | if ((debug_base = locate_base ()) == 0) | |
808 | { | |
809 | /* Can't find the symbol or the executable is statically linked. */ | |
810 | return; | |
811 | } | |
812 | ||
813 | if (!enable_break ()) | |
814 | { | |
815 | warning ("shared library handler failed to enable breakpoint"); | |
816 | return; | |
817 | } | |
818 | ||
819 | /* SCO and SunOS need the loop below, other systems should be using the | |
820 | special shared library breakpoints and the shared library breakpoint | |
821 | service routine. | |
822 | ||
823 | Now run the target. It will eventually hit the breakpoint, at | |
824 | which point all of the libraries will have been mapped in and we | |
825 | can go groveling around in the dynamic linker structures to find | |
826 | out what we need to know about them. */ | |
827 | ||
828 | clear_proceed_status (); | |
829 | stop_soon_quietly = 1; | |
830 | stop_signal = TARGET_SIGNAL_0; | |
831 | do | |
832 | { | |
833 | target_resume (pid_to_ptid (-1), 0, stop_signal); | |
834 | wait_for_inferior (); | |
835 | } | |
836 | while (stop_signal != TARGET_SIGNAL_TRAP); | |
837 | stop_soon_quietly = 0; | |
838 | ||
839 | /* We are now either at the "mapping complete" breakpoint (or somewhere | |
840 | else, a condition we aren't prepared to deal with anyway), so adjust | |
841 | the PC as necessary after a breakpoint, disable the breakpoint, and | |
842 | add any shared libraries that were mapped in. */ | |
843 | ||
844 | if (DECR_PC_AFTER_BREAK) | |
845 | { | |
846 | stop_pc -= DECR_PC_AFTER_BREAK; | |
847 | write_register (PC_REGNUM, stop_pc); | |
848 | } | |
849 | ||
850 | if (!disable_break ()) | |
851 | { | |
852 | warning ("shared library handler failed to disable breakpoint"); | |
853 | } | |
854 | ||
990f9fe3 | 855 | solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add); |
ab31aa69 KB |
856 | } |
857 | ||
858 | static void | |
859 | sunos_clear_solib (void) | |
860 | { | |
861 | debug_base = 0; | |
862 | } | |
863 | ||
864 | static void | |
865 | sunos_free_so (struct so_list *so) | |
866 | { | |
867 | xfree (so->lm_info->lm); | |
868 | xfree (so->lm_info); | |
869 | } | |
870 | ||
871 | static void | |
872 | sunos_relocate_section_addresses (struct so_list *so, | |
873 | struct section_table *sec) | |
874 | { | |
875 | sec->addr += LM_ADDR (so); | |
876 | sec->endaddr += LM_ADDR (so); | |
877 | } | |
878 | ||
879 | static struct target_so_ops sunos_so_ops; | |
880 | ||
881 | void | |
882 | _initialize_sunos_solib (void) | |
883 | { | |
884 | sunos_so_ops.relocate_section_addresses = sunos_relocate_section_addresses; | |
885 | sunos_so_ops.free_so = sunos_free_so; | |
886 | sunos_so_ops.clear_solib = sunos_clear_solib; | |
887 | sunos_so_ops.solib_create_inferior_hook = sunos_solib_create_inferior_hook; | |
888 | sunos_so_ops.special_symbol_handling = sunos_special_symbol_handling; | |
889 | sunos_so_ops.current_sos = sunos_current_sos; | |
890 | sunos_so_ops.open_symbol_file_object = open_symbol_file_object; | |
891 | sunos_so_ops.in_dynsym_resolve_code = sunos_in_dynsym_resolve_code; | |
892 | ||
893 | /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */ | |
894 | current_target_so_ops = &sunos_so_ops; | |
895 | } |