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