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