OSDN Git Service

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