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