OSDN Git Service

2004-12-07 Randolph Chung <tausq@debian.org>
[pf3gnuchains/sourceware.git] / gdb / solib-som.c
1 /* Handle SOM shared libraries for GDB, the GNU Debugger.
2
3    Copyright 2004 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "som.h"
24 #include "symtab.h"
25 #include "bfd.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "gdbcore.h"
29 #include "target.h"
30 #include "inferior.h"
31
32 #include "hppa-tdep.h"
33 #include "solist.h"
34
35 #undef SOLIB_SOM_DBG 
36
37 /* These ought to be defined in some public interface, but aren't.  They
38    define the meaning of the various bits in the distinguished __dld_flags
39    variable that is declared in every debuggable a.out on HP-UX, and that
40    is shared between the debugger and the dynamic linker.
41  */
42 #define DLD_FLAGS_MAPPRIVATE    0x1
43 #define DLD_FLAGS_HOOKVALID     0x2
44 #define DLD_FLAGS_LISTVALID     0x4
45 #define DLD_FLAGS_BOR_ENABLE    0x8
46
47 struct lm_info
48   {
49     /* Version of this structure (it is expected to change again in hpux10).  */
50     unsigned char struct_version;
51
52     /* Binding mode for this library.  */
53     unsigned char bind_mode;
54
55     /* Version of this library.  */
56     short library_version;
57
58     /* Start of text address,
59        link-time text location (length of text area),
60        end of text address.  */
61     CORE_ADDR text_addr;
62     CORE_ADDR text_link_addr;
63     CORE_ADDR text_end;
64
65     /* Start of data, start of bss and end of data.  */
66     CORE_ADDR data_start;
67     CORE_ADDR bss_start;
68     CORE_ADDR data_end;
69
70     /* Value of linkage pointer (%r19).  */
71     CORE_ADDR got_value;
72
73     /* Address in target of offset from thread-local register of
74        start of this thread's data.  I.e., the first thread-local
75        variable in this shared library starts at *(tsd_start_addr)
76        from that area pointed to by cr27 (mpsfu_hi).
77       
78        We do the indirection as soon as we read it, so from then
79        on it's the offset itself.  */
80     CORE_ADDR tsd_start_addr;
81
82     /* Address of the link map entry in the loader.  */
83     CORE_ADDR lm_addr;
84   };
85
86 /* These addresses should be filled in by som_solib_create_inferior_hook.
87    They are also used elsewhere in this module.
88  */
89 typedef struct
90   {
91     CORE_ADDR address;
92     struct unwind_table_entry *unwind;
93   }
94 addr_and_unwind_t;
95
96 /* When adding fields, be sure to clear them in _initialize_som_solib. */
97 static struct
98   {
99     int is_valid;
100     addr_and_unwind_t hook;
101     addr_and_unwind_t hook_stub;
102     addr_and_unwind_t load;
103     addr_and_unwind_t load_stub;
104     addr_and_unwind_t unload;
105     addr_and_unwind_t unload2;
106     addr_and_unwind_t unload_stub;
107   }
108 dld_cache;
109
110 static void
111 som_relocate_section_addresses (struct so_list *so,
112                                 struct section_table *sec)
113 {
114   flagword aflag = bfd_get_section_flags(so->abfd, sec->the_bfd_section);
115
116   /* solib.c does something similar, but it only recognizes ".text", SOM calls
117      the text section "$CODE$".  */
118   if (strcmp (sec->the_bfd_section->name, "$CODE$") == 0)
119     {
120       so->textsection = sec;
121     }
122
123   if (aflag & SEC_CODE)
124     {
125       sec->addr    += so->lm_info->text_addr - so->lm_info->text_link_addr; 
126       sec->endaddr += so->lm_info->text_addr - so->lm_info->text_link_addr;
127     }
128   else if (aflag & SEC_DATA)
129     {
130       sec->addr    += so->lm_info->data_start; 
131       sec->endaddr += so->lm_info->data_start;
132     }
133   else
134     ;
135 }
136
137 /* This hook gets called just before the first instruction in the
138    inferior process is executed.
139
140    This is our opportunity to set magic flags in the inferior so
141    that GDB can be notified when a shared library is mapped in and
142    to tell the dynamic linker that a private copy of the library is
143    needed (so GDB can set breakpoints in the library).
144
145    __dld_flags is the location of the magic flags; as of this implementation
146    there are 3 flags of interest:
147
148    bit 0 when set indicates that private copies of the libraries are needed
149    bit 1 when set indicates that the callback hook routine is valid
150    bit 2 when set indicates that the dynamic linker should maintain the
151    __dld_list structure when loading/unloading libraries.
152
153    Note that shared libraries are not mapped in at this time, so we have
154    run the inferior until the libraries are mapped in.  Typically this
155    means running until the "_start" is called.  */
156
157 static void
158 som_solib_create_inferior_hook (void)
159 {
160   struct minimal_symbol *msymbol;
161   unsigned int dld_flags, status, have_endo;
162   asection *shlib_info;
163   char buf[4];
164   struct objfile *objfile;
165   CORE_ADDR anaddr;
166
167   /* First, remove all the solib event breakpoints.  Their addresses
168      may have changed since the last time we ran the program.  */
169   remove_solib_event_breakpoints ();
170
171   if (symfile_objfile == NULL)
172     return;
173
174   /* First see if the objfile was dynamically linked.  */
175   shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
176   if (!shlib_info)
177     return;
178
179   /* It's got a $SHLIB_INFO$ section, make sure it's not empty.  */
180   if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
181     return;
182
183   have_endo = 0;
184   /* Slam the pid of the process into __d_pid.
185
186      We used to warn when this failed, but that warning is only useful
187      on very old HP systems (hpux9 and older).  The warnings are an
188      annoyance to users of modern systems and foul up the testsuite as
189      well.  As a result, the warnings have been disabled.  */
190   msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
191   if (msymbol == NULL)
192     goto keep_going;
193
194   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
195   store_unsigned_integer (buf, 4, PIDGET (inferior_ptid));
196   status = target_write_memory (anaddr, buf, 4);
197   if (status != 0)
198     {
199       warning ("Unable to write __d_pid");
200       warning ("Suggest linking with /opt/langtools/lib/end.o.");
201       warning ("GDB will be unable to track shl_load/shl_unload calls");
202       goto keep_going;
203     }
204
205   /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
206      This will force the dynamic linker to call __d_trap when significant
207      events occur.
208
209      Note that the above is the pre-HP-UX 9.0 behaviour.  At 9.0 and above,
210      the dld provides an export stub named "__d_trap" as well as the
211      function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
212      We'll look first for the old flavor and then the new.
213    */
214   msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
215   if (msymbol == NULL)
216     msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
217   if (msymbol == NULL)
218     {
219       warning ("Unable to find _DLD_HOOK symbol in object file.");
220       warning ("Suggest linking with /opt/langtools/lib/end.o.");
221       warning ("GDB will be unable to track shl_load/shl_unload calls");
222       goto keep_going;
223     }
224   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
225   dld_cache.hook.address = anaddr;
226
227   /* Grrr, this might not be an export symbol!  We have to find the
228      export stub.  */
229   ALL_OBJFILES (objfile)
230   {
231     struct unwind_table_entry *u;
232     struct minimal_symbol *msymbol2;
233
234     /* What a crock.  */
235     msymbol2 =
236       lookup_minimal_symbol_solib_trampoline (SYMBOL_LINKAGE_NAME (msymbol),
237                                               objfile);
238     /* Found a symbol with the right name.  */
239     if (msymbol2)
240       {
241         struct unwind_table_entry *u;
242         /* It must be a shared library trampoline.  */
243         if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
244           continue;
245
246         /* It must also be an export stub.  */
247         u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
248         if (!u || u->stub_unwind.stub_type != EXPORT)
249           continue;
250
251         /* OK.  Looks like the correct import stub.  */
252         anaddr = SYMBOL_VALUE (msymbol2);
253         dld_cache.hook_stub.address = anaddr;
254       }
255   }
256   store_unsigned_integer (buf, 4, anaddr);
257
258   msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
259   if (msymbol == NULL)
260     {
261       warning ("Unable to find __dld_hook symbol in object file.");
262       warning ("Suggest linking with /opt/langtools/lib/end.o.");
263       warning ("GDB will be unable to track shl_load/shl_unload calls");
264       goto keep_going;
265     }
266   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
267   status = target_write_memory (anaddr, buf, 4);
268
269   /* Now set a shlib_event breakpoint at __d_trap so we can track
270      significant shared library events.  */
271   msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
272   if (msymbol == NULL)
273     {
274       warning ("Unable to find __dld_d_trap symbol in object file.");
275       warning ("Suggest linking with /opt/langtools/lib/end.o.");
276       warning ("GDB will be unable to track shl_load/shl_unload calls");
277       goto keep_going;
278     }
279   create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
280
281   /* We have all the support usually found in end.o, so we can track
282      shl_load and shl_unload calls.  */
283   have_endo = 1;
284
285 keep_going:
286
287   /* Get the address of __dld_flags, if no such symbol exists, then we can
288      not debug the shared code.  */
289   msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
290   if (msymbol == NULL)
291     {
292       error ("Unable to find __dld_flags symbol in object file.\n");
293     }
294
295   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
296
297   /* Read the current contents.  */
298   status = target_read_memory (anaddr, buf, 4);
299   if (status != 0)
300     {
301       error ("Unable to read __dld_flags\n");
302     }
303   dld_flags = extract_unsigned_integer (buf, 4);
304
305   /* Turn on the flags we care about.  */
306   dld_flags |= DLD_FLAGS_MAPPRIVATE;
307   if (have_endo)
308     dld_flags |= DLD_FLAGS_HOOKVALID;
309   store_unsigned_integer (buf, 4, dld_flags);
310   status = target_write_memory (anaddr, buf, 4);
311   if (status != 0)
312     {
313       error ("Unable to write __dld_flags\n");
314     }
315
316   /* Now find the address of _start and set a breakpoint there. 
317      We still need this code for two reasons:
318
319      * Not all sites have /opt/langtools/lib/end.o, so it's not always
320      possible to track the dynamic linker's events.
321
322      * At this time no events are triggered for shared libraries
323      loaded at startup time (what a crock).  */
324
325   msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
326   if (msymbol == NULL)
327     {
328       error ("Unable to find _start symbol in object file.\n");
329     }
330
331   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
332
333   /* Make the breakpoint at "_start" a shared library event breakpoint.  */
334   create_solib_event_breakpoint (anaddr);
335
336   clear_symtab_users ();
337 }
338
339 /* This operation removes the "hook" between GDB and the dynamic linker,
340    which causes the dld to notify GDB of shared library events.
341
342    After this operation completes, the dld will no longer notify GDB of
343    shared library events.  To resume notifications, GDB must call
344    som_solib_create_inferior_hook.
345
346    This operation does not remove any knowledge of shared libraries which
347    GDB may already have been notified of.
348  */
349 static void
350 som_solib_remove_inferior_hook (int pid)
351 {
352   CORE_ADDR addr;
353   struct minimal_symbol *msymbol;
354   int status;
355   char dld_flags_buffer[4];
356   unsigned int dld_flags_value;
357   struct cleanup *old_cleanups = save_inferior_ptid ();
358
359   /* Ensure that we're really operating on the specified process. */
360   inferior_ptid = pid_to_ptid (pid);
361
362   /* We won't bother to remove the solib breakpoints from this process.
363
364      In fact, on PA64 the breakpoint is hard-coded into the dld callback,
365      and thus we're not supposed to remove it.
366
367      Rather, we'll merely clear the dld_flags bit that enables callbacks.
368    */
369   msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
370
371   addr = SYMBOL_VALUE_ADDRESS (msymbol);
372   status = target_read_memory (addr, dld_flags_buffer, 4);
373
374   dld_flags_value = extract_unsigned_integer (dld_flags_buffer, 4);
375
376   dld_flags_value &= ~DLD_FLAGS_HOOKVALID;
377   store_unsigned_integer (dld_flags_buffer, 4, dld_flags_value);
378   status = target_write_memory (addr, dld_flags_buffer, 4);
379
380   do_cleanups (old_cleanups);
381 }
382
383 static void
384 som_special_symbol_handling (void)
385 {
386 }
387
388 static void
389 som_solib_desire_dynamic_linker_symbols (void)
390 {
391   struct objfile *objfile;
392   struct unwind_table_entry *u;
393   struct minimal_symbol *dld_msymbol;
394
395   /* Do we already know the value of these symbols?  If so, then
396      we've no work to do.
397
398      (If you add clauses to this test, be sure to likewise update the
399      test within the loop.)
400    */
401   if (dld_cache.is_valid)
402     return;
403
404   ALL_OBJFILES (objfile)
405   {
406     dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
407     if (dld_msymbol != NULL)
408       {
409         dld_cache.load.address = SYMBOL_VALUE (dld_msymbol);
410         dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
411       }
412
413     dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
414                                                           objfile);
415     if (dld_msymbol != NULL)
416       {
417         if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
418           {
419             u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
420             if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
421               {
422                 dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol);
423                 dld_cache.load_stub.unwind = u;
424               }
425           }
426       }
427
428     dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
429     if (dld_msymbol != NULL)
430       {
431         dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol);
432         dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);
433
434         /* ??rehrauer: I'm not sure exactly what this is, but it appears
435            that on some HPUX 10.x versions, there's two unwind regions to
436            cover the body of "shl_unload", the second being 4 bytes past
437            the end of the first.  This is a large hack to handle that
438            case, but since I don't seem to have any legitimate way to
439            look for this thing via the symbol table...
440          */
441         if (dld_cache.unload.unwind != NULL)
442           {
443             u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
444             if (u != NULL)
445               {
446                 dld_cache.unload2.address = u->region_start;
447                 dld_cache.unload2.unwind = u;
448               }
449           }
450       }
451
452     dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
453                                                           objfile);
454     if (dld_msymbol != NULL)
455       {
456         if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
457           {
458             u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
459             if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
460               {
461                 dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol);
462                 dld_cache.unload_stub.unwind = u;
463               }
464           }
465       }
466
467     /* Did we find everything we were looking for?  If so, stop. */
468     if ((dld_cache.load.address != 0)
469         && (dld_cache.load_stub.address != 0)
470         && (dld_cache.unload.address != 0)
471         && (dld_cache.unload_stub.address != 0))
472       {
473         dld_cache.is_valid = 1;
474         break;
475       }
476   }
477
478   dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
479   dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);
480
481   /* We're prepared not to find some of these symbols, which is why
482      this function is a "desire" operation, and not a "require".
483    */
484 }
485
486 static int
487 som_in_dynsym_resolve_code (CORE_ADDR pc)
488 {
489   struct unwind_table_entry *u_pc;
490
491   /* Are we in the dld itself?
492
493      ??rehrauer: Large hack -- We'll assume that any address in a
494      shared text region is the dld's text.  This would obviously
495      fall down if the user attached to a process, whose shlibs
496      weren't mapped to a (writeable) private region.  However, in
497      that case the debugger probably isn't able to set the fundamental
498      breakpoint in the dld callback anyways, so this hack should be
499      safe.
500    */
501   if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
502     return 1;
503
504   /* Cache the address of some symbols that are part of the dynamic
505      linker, if not already known.
506    */
507   som_solib_desire_dynamic_linker_symbols ();
508
509   /* Are we in the dld callback?  Or its export stub? */
510   u_pc = find_unwind_entry (pc);
511   if (u_pc == NULL)
512     return 0;
513
514   if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
515     return 1;
516
517   /* Or the interface of the dld (i.e., "shl_load" or friends)? */
518   if ((u_pc == dld_cache.load.unwind)
519       || (u_pc == dld_cache.unload.unwind)
520       || (u_pc == dld_cache.unload2.unwind)
521       || (u_pc == dld_cache.load_stub.unwind)
522       || (u_pc == dld_cache.unload_stub.unwind))
523     return 1;
524
525   /* Apparently this address isn't part of the dld's text. */
526   return 0;
527 }
528
529 static void
530 som_clear_solib (void)
531 {
532 }
533
534 struct dld_list {
535   char name[4];
536   char info[4];
537   char text_addr[4];
538   char text_link_addr[4];
539   char text_end[4];
540   char data_start[4];
541   char bss_start[4];
542   char data_end[4];
543   char got_value[4];
544   char next[4];
545   char tsd_start_addr_ptr[4];
546 };
547
548 static CORE_ADDR
549 link_map_start (void)
550 {
551   struct minimal_symbol *sym;
552   CORE_ADDR addr;
553   char buf[4];
554   unsigned int dld_flags;
555
556   sym = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
557   if (!sym)
558     {
559       error ("Unable to find __dld_flags symbol in object file.\n");
560       return 0;
561     }
562   addr = SYMBOL_VALUE_ADDRESS (sym);
563   read_memory (addr, buf, 4);
564   dld_flags = extract_unsigned_integer (buf, 4);
565   if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
566     {
567       error ("__dld_list is not valid according to __dld_flags.\n");
568       return 0;
569     }
570
571   /* If the libraries were not mapped private, warn the user.  */
572   if ((dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
573     warning ("The shared libraries were not privately mapped; setting a\n"
574              "breakpoint in a shared library will not work until you rerun the "
575              "program.\n");
576
577   sym = lookup_minimal_symbol ("__dld_list", NULL, NULL);
578   if (!sym)
579     {
580       /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
581          but the data is still available if you know where to look.  */
582       sym = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
583       if (!sym)
584         {
585           error ("Unable to find dynamic library list.\n");
586           return 0;
587         }
588       addr = SYMBOL_VALUE_ADDRESS (sym) - 8;
589     }
590   else
591     addr = SYMBOL_VALUE_ADDRESS (sym);
592
593   read_memory (addr, buf, 4);
594   addr = extract_unsigned_integer (buf, 4);
595   if (addr == 0)
596     {
597       error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
598       return 0;
599     }
600
601   read_memory (addr, buf, 4);
602   return extract_unsigned_integer (buf, 4);
603 }
604
605 /* Does this so's name match the main binary? */
606 static int
607 match_main (const char *name)
608 {
609   return strcmp (name, symfile_objfile->name) == 0;
610 }
611
612 static struct so_list *
613 som_current_sos (void)
614 {
615   CORE_ADDR lm;
616   struct so_list *head = 0;
617   struct so_list **link_ptr = &head;
618
619   for (lm = link_map_start (); lm; )
620     {
621       char *namebuf;
622       CORE_ADDR addr;
623       struct so_list *new;
624       struct cleanup *old_chain;
625       int errcode;
626       struct dld_list dbuf;
627       char tsdbuf[4];
628
629       new = (struct so_list *) xmalloc (sizeof (struct so_list));
630       old_chain = make_cleanup (xfree, new);
631
632       memset (new, 0, sizeof (*new));
633       new->lm_info = xmalloc (sizeof (struct lm_info));
634       make_cleanup (xfree, new->lm_info);
635
636       read_memory (lm, (char *)&dbuf, sizeof (struct dld_list));
637
638       addr = extract_unsigned_integer (&dbuf.name, sizeof (dbuf.name));
639       target_read_string (addr, &namebuf, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
640       if (errcode != 0)
641         {
642           warning ("current_sos: Can't read pathname for load map: %s\n",
643                    safe_strerror (errcode));
644         }
645       else
646         {
647           strncpy (new->so_name, namebuf, SO_NAME_MAX_PATH_SIZE - 1);
648           new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
649           xfree (namebuf);
650           strcpy (new->so_original_name, new->so_name);
651         }
652
653         if (new->so_name[0] && !match_main (new->so_name))
654           {
655             struct lm_info *lmi = new->lm_info;
656             unsigned int tmp;
657
658             lmi->lm_addr = lm;
659
660 #define EXTRACT(_fld) \
661   extract_unsigned_integer (&dbuf._fld, sizeof (dbuf._fld));
662
663             lmi->text_addr = EXTRACT (text_addr);
664             tmp = EXTRACT (info);
665             lmi->library_version = (tmp >> 16) & 0xffff;
666             lmi->bind_mode = (tmp >> 8) & 0xff;
667             lmi->struct_version = tmp & 0xff;
668             lmi->text_link_addr = EXTRACT (text_link_addr);
669             lmi->text_end = EXTRACT (text_end);
670             lmi->data_start = EXTRACT (data_start);
671             lmi->bss_start = EXTRACT (bss_start);
672             lmi->data_end = EXTRACT (data_end);
673             lmi->got_value = EXTRACT (got_value);
674             tmp = EXTRACT (tsd_start_addr_ptr);
675             read_memory (tmp, tsdbuf, 4);
676             lmi->tsd_start_addr = extract_unsigned_integer (tsdbuf, 4);
677
678 #ifdef SOLIB_SOM_DBG
679             printf ("\n+ library \"%s\" is described at 0x%s\n", new->so_name, 
680                     paddr_nz (lm));
681             printf ("  'version' is %d\n", new->lm_info->struct_version);
682             printf ("  'bind_mode' is %d\n", new->lm_info->bind_mode);
683             printf ("  'library_version' is %d\n", 
684                     new->lm_info->library_version);
685             printf ("  'text_addr' is 0x%s\n", 
686                     paddr_nz (new->lm_info->text_addr));
687             printf ("  'text_link_addr' is 0x%s\n", 
688                     paddr_nz (new->lm_info->text_link_addr));
689             printf ("  'text_end' is 0x%s\n", 
690                     paddr_nz (new->lm_info->text_end));
691             printf ("  'data_start' is 0x%s\n", 
692                     paddr_nz (new->lm_info->data_start));
693             printf ("  'bss_start' is 0x%s\n", 
694                     paddr_nz (new->lm_info->bss_start));
695             printf ("  'data_end' is 0x%s\n", 
696                     paddr_nz (new->lm_info->data_end));
697             printf ("  'got_value' is %s\n", 
698                     paddr_nz (new->lm_info->got_value));
699             printf ("  'tsd_start_addr' is 0x%s\n", 
700                     paddr_nz (new->lm_info->tsd_start_addr));
701 #endif
702
703             /* Link the new object onto the list.  */
704             new->next = NULL;
705             *link_ptr = new;
706             link_ptr = &new->next;
707           }
708         else
709           {
710             free_so (new);
711           }
712
713       lm = EXTRACT (next);
714       discard_cleanups (old_chain);
715 #undef EXTRACT
716     }
717
718   /* TODO: The original somsolib code has logic to detect and eliminate 
719      duplicate entries.  Do we need that?  */
720
721   return head;
722 }
723
724 static int
725 som_open_symbol_file_object (void *from_ttyp)
726 {
727   CORE_ADDR lm, l_name;
728   char *filename;
729   int errcode;
730   int from_tty = *(int *)from_ttyp;
731   char buf[4];
732
733   if (symfile_objfile)
734     if (!query ("Attempt to reload symbols from process? "))
735       return 0;
736
737   /* First link map member should be the executable.  */
738   if ((lm = link_map_start ()) == 0)
739     return 0;   /* failed somehow... */
740
741   /* Read address of name from target memory to GDB.  */
742   read_memory (lm + offsetof (struct dld_list, name), buf, 4);
743
744   /* Convert the address to host format.  Assume that the address is
745      unsigned.  */
746   l_name = extract_unsigned_integer (buf, 4);
747
748   if (l_name == 0)
749     return 0;           /* No filename.  */
750
751   /* Now fetch the filename from target memory.  */
752   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
753
754   if (errcode)
755     {
756       warning ("failed to read exec filename from attached file: %s",
757                safe_strerror (errcode));
758       return 0;
759     }
760
761   make_cleanup (xfree, filename);
762   /* Have a pathname: read the symbol file.  */
763   symbol_file_add_main (filename, from_tty);
764
765   return 1;
766 }
767
768 static void
769 som_free_so (struct so_list *so)
770 {
771   xfree (so->lm_info);
772 }
773
774 static CORE_ADDR
775 som_solib_thread_start_addr (struct so_list *so)
776 {
777   return so->lm_info->tsd_start_addr;
778 }
779
780 /* Return the GOT value for the shared library in which ADDR belongs.  If
781    ADDR isn't in any known shared library, return zero.  */
782
783 static CORE_ADDR
784 som_solib_get_got_by_pc (CORE_ADDR addr)
785 {
786   struct so_list *so_list = master_so_list ();
787   CORE_ADDR got_value = 0;
788
789   while (so_list)
790     {
791       if (so_list->lm_info->text_addr <= addr
792           && so_list->lm_info->text_end > addr)
793         {
794           got_value = so_list->lm_info->got_value;
795           break;
796         }
797       so_list = so_list->next;
798     }
799   return got_value;
800 }
801
802 /* Return the address of the handle of the shared library in which ADDR belongs.
803    If ADDR isn't in any known shared library, return zero.  */
804 /* this function is used in initialize_hp_cxx_exception_support in 
805    hppa-hpux-tdep.c  */
806
807 static CORE_ADDR
808 som_solib_get_solib_by_pc (CORE_ADDR addr)
809 {
810   struct so_list *so_list = master_so_list ();
811
812   while (so_list)
813     {
814       if (so_list->lm_info->text_addr <= addr
815           && so_list->lm_info->text_end > addr)
816         {
817           break;
818         }
819       so_list = so_list->next;
820     }
821   if (so_list)
822     return so_list->lm_info->lm_addr;
823   else
824     return 0;
825 }
826
827
828 static struct target_so_ops som_so_ops;
829
830 extern initialize_file_ftype _initialize_som_solib; /* -Wmissing-prototypes */
831
832 void
833 _initialize_som_solib (void)
834 {
835   som_so_ops.relocate_section_addresses = som_relocate_section_addresses;
836   som_so_ops.free_so = som_free_so;
837   som_so_ops.clear_solib = som_clear_solib;
838   som_so_ops.solib_create_inferior_hook = som_solib_create_inferior_hook;
839   som_so_ops.special_symbol_handling = som_special_symbol_handling;
840   som_so_ops.current_sos = som_current_sos;
841   som_so_ops.open_symbol_file_object = som_open_symbol_file_object;
842   som_so_ops.in_dynsym_resolve_code = som_in_dynsym_resolve_code;
843 }
844
845 void som_solib_select (struct gdbarch_tdep *tdep)
846 {
847   current_target_so_ops = &som_so_ops;
848
849   tdep->solib_thread_start_addr = som_solib_thread_start_addr;
850   tdep->solib_get_got_by_pc = som_solib_get_got_by_pc;
851   tdep->solib_get_solib_by_pc = som_solib_get_solib_by_pc;
852 }
853
854 /* The rest of these functions are not part of the solib interface; they 
855    are used by somread.c or hppa-hpux-tdep.c */
856
857 int
858 som_solib_section_offsets (struct objfile *objfile,
859                            struct section_offsets *offsets)
860 {
861   struct so_list *so_list = master_so_list ();
862
863   while (so_list)
864     {
865       /* Oh what a pain!  We need the offsets before so_list->objfile
866          is valid.  The BFDs will never match.  Make a best guess.  */
867       if (strstr (objfile->name, so_list->so_name))
868         {
869           asection *private_section;
870
871           /* The text offset is easy.  */
872           offsets->offsets[SECT_OFF_TEXT (objfile)]
873             = (so_list->lm_info->text_addr
874                - so_list->lm_info->text_link_addr);
875           offsets->offsets[SECT_OFF_RODATA (objfile)]
876             = ANOFFSET (offsets, SECT_OFF_TEXT (objfile));
877
878           /* We should look at presumed_dp in the SOM header, but
879              that's not easily available.  This should be OK though.  */
880           private_section = bfd_get_section_by_name (objfile->obfd,
881                                                      "$PRIVATE$");
882           if (!private_section)
883             {
884               warning ("Unable to find $PRIVATE$ in shared library!");
885               offsets->offsets[SECT_OFF_DATA (objfile)] = 0;
886               offsets->offsets[SECT_OFF_BSS (objfile)] = 0;
887               return 1;
888             }
889           offsets->offsets[SECT_OFF_DATA (objfile)]
890             = (so_list->lm_info->data_start - private_section->vma);
891           offsets->offsets[SECT_OFF_BSS (objfile)]
892             = ANOFFSET (offsets, SECT_OFF_DATA (objfile));
893           return 1;
894         }
895       so_list = so_list->next;
896     }
897   return 0;
898 }