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