OSDN Git Service

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