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