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