gdb/solib-som.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Handle SOM shared libraries.

  2.    Copyright (C) 2004-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "bfd.h"
  17. #include "symfile.h"
  18. #include "objfiles.h"
  19. #include "gdbcore.h"
  20. #include "target.h"
  21. #include "inferior.h"

  22. #include "hppa-tdep.h"
  23. #include "solist.h"
  24. #include "solib.h"
  25. #include "solib-som.h"

  26. #undef SOLIB_SOM_DBG

  27. /* These ought to be defined in some public interface, but aren't.  They
  28.    define the meaning of the various bits in the distinguished __dld_flags
  29.    variable that is declared in every debuggable a.out on HP-UX, and that
  30.    is shared between the debugger and the dynamic linker.  */

  31. #define DLD_FLAGS_MAPPRIVATE    0x1
  32. #define DLD_FLAGS_HOOKVALID     0x2
  33. #define DLD_FLAGS_LISTVALID     0x4
  34. #define DLD_FLAGS_BOR_ENABLE    0x8

  35. struct lm_info
  36.   {
  37.     /* Version of this structure (it is expected to change again in
  38.        hpux10).  */
  39.     unsigned char struct_version;

  40.     /* Binding mode for this library.  */
  41.     unsigned char bind_mode;

  42.     /* Version of this library.  */
  43.     short library_version;

  44.     /* Start of text address,
  45.        link-time text location (length of text area),
  46.        end of text address.  */
  47.     CORE_ADDR text_addr;
  48.     CORE_ADDR text_link_addr;
  49.     CORE_ADDR text_end;

  50.     /* Start of data, start of bss and end of data.  */
  51.     CORE_ADDR data_start;
  52.     CORE_ADDR bss_start;
  53.     CORE_ADDR data_end;

  54.     /* Value of linkage pointer (%r19).  */
  55.     CORE_ADDR got_value;

  56.     /* Address in target of offset from thread-local register of
  57.        start of this thread's dataI.e., the first thread-local
  58.        variable in this shared library starts at *(tsd_start_addr)
  59.        from that area pointed to by cr27 (mpsfu_hi).

  60.        We do the indirection as soon as we read it, so from then
  61.        on it's the offset itself.  */
  62.     CORE_ADDR tsd_start_addr;

  63.     /* Address of the link map entry in the loader.  */
  64.     CORE_ADDR lm_addr;
  65.   };

  66. /* These addresses should be filled in by som_solib_create_inferior_hook.
  67.    They are also used elsewhere in this module.  */

  68. typedef struct
  69.   {
  70.     CORE_ADDR address;
  71.     struct unwind_table_entry *unwind;
  72.   }
  73. addr_and_unwind_t;

  74. /* When adding fields, be sure to clear them in _initialize_som_solib.  */
  75. static struct
  76.   {
  77.     int is_valid;
  78.     addr_and_unwind_t hook;
  79.     addr_and_unwind_t hook_stub;
  80.     addr_and_unwind_t load;
  81.     addr_and_unwind_t load_stub;
  82.     addr_and_unwind_t unload;
  83.     addr_and_unwind_t unload2;
  84.     addr_and_unwind_t unload_stub;
  85.   }
  86. dld_cache;

  87. static void
  88. som_relocate_section_addresses (struct so_list *so,
  89.                                 struct target_section *sec)
  90. {
  91.   flagword aflag = bfd_get_section_flags(so->abfd, sec->the_bfd_section);

  92.   if (aflag & SEC_CODE)
  93.     {
  94.       sec->addr    += so->lm_info->text_addr - so->lm_info->text_link_addr;
  95.       sec->endaddr += so->lm_info->text_addr - so->lm_info->text_link_addr;
  96.     }
  97.   else if (aflag & SEC_DATA)
  98.     {
  99.       sec->addr    += so->lm_info->data_start;
  100.       sec->endaddr += so->lm_info->data_start;
  101.     }
  102.   else
  103.     {
  104.       /* Nothing.  */
  105.     }
  106. }


  107. /* Variable storing HP-UX major release number.

  108.    On non-native system, simply assume that the major release number
  109.    is 11.  On native systems, hppa-hpux-nat.c initialization code
  110.    sets this number to the real one on startup.

  111.    We cannot compute this value here, because we need to make a native
  112.    call to "uname".  We are are not allowed to do that from here, as
  113.    this file is used for both native and cross debugging.  */

  114. #define DEFAULT_HPUX_MAJOR_RELEASE 11
  115. int hpux_major_release = DEFAULT_HPUX_MAJOR_RELEASE;

  116. static int
  117. get_hpux_major_release (void)
  118. {
  119.   return hpux_major_release;
  120. }

  121. /* DL header flag defines.  */
  122. #define SHLIB_TEXT_PRIVATE_ENABLE 0x4000

  123. /* The DL header is documented in <shl.h>.  We are only interested
  124.    in the flags field to determine whether the executable wants shared
  125.    libraries mapped private.  */
  126. struct {
  127.     short junk[37];
  128.     short flags;
  129. } dl_header;

  130. /* This hook gets called just before the first instruction in the
  131.    inferior process is executed.

  132.    This is our opportunity to set magic flags in the inferior so
  133.    that GDB can be notified when a shared library is mapped in and
  134.    to tell the dynamic linker that a private copy of the library is
  135.    needed (so GDB can set breakpoints in the library).

  136.    __dld_flags is the location of the magic flags; as of this implementation
  137.    there are 3 flags of interest:

  138.    bit 0 when set indicates that private copies of the libraries are needed
  139.    bit 1 when set indicates that the callback hook routine is valid
  140.    bit 2 when set indicates that the dynamic linker should maintain the
  141.    __dld_list structure when loading/unloading libraries.

  142.    Note that shared libraries are not mapped in at this time, so we have
  143.    run the inferior until the libraries are mapped in.  Typically this
  144.    means running until the "_start" is called.  */

  145. static void
  146. som_solib_create_inferior_hook (int from_tty)
  147. {
  148.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  149.   struct bound_minimal_symbol msymbol;
  150.   unsigned int dld_flags, status, have_endo;
  151.   asection *shlib_info;
  152.   gdb_byte buf[4];
  153.   CORE_ADDR anaddr;

  154.   if (symfile_objfile == NULL)
  155.     return;

  156.   /* First see if the objfile was dynamically linked.  */
  157.   shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
  158.   if (!shlib_info)
  159.     return;

  160.   /* It's got a $SHLIB_INFO$ section, make sure it's not empty.  */
  161.   if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
  162.     return;

  163.   /* Read the DL header.  */
  164.   bfd_get_section_contents (symfile_objfile->obfd, shlib_info,
  165.                             (char *) &dl_header, 0, sizeof (dl_header));

  166.   have_endo = 0;
  167.   /* Slam the pid of the process into __d_pid.

  168.      We used to warn when this failed, but that warning is only useful
  169.      on very old HP systems (hpux9 and older).  The warnings are an
  170.      annoyance to users of modern systems and foul up the testsuite as
  171.      well.  As a result, the warnings have been disabled.  */
  172.   msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
  173.   if (msymbol.minsym == NULL)
  174.     goto keep_going;

  175.   anaddr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  176.   store_unsigned_integer (buf, 4, byte_order, ptid_get_pid (inferior_ptid));
  177.   status = target_write_memory (anaddr, buf, 4);
  178.   if (status != 0)
  179.     {
  180.       warning (_("\
  181. Unable to write __d_pid.\n\
  182. Suggest linking with /opt/langtools/lib/end.o.\n\
  183. GDB will be unable to track shl_load/shl_unload calls"));
  184.       goto keep_going;
  185.     }

  186.   /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
  187.      This will force the dynamic linker to call __d_trap when significant
  188.      events occur.

  189.      Note that the above is the pre-HP-UX 9.0 behaviour.  At 9.0 and above,
  190.      the dld provides an export stub named "__d_trap" as well as the
  191.      function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
  192.      We'll look first for the old flavor and then the new.  */

  193.   msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
  194.   if (msymbol.minsym == NULL)
  195.     msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
  196.   if (msymbol.minsym == NULL)
  197.     {
  198.       warning (_("\
  199. Unable to find _DLD_HOOK symbol in object file.\n\
  200. Suggest linking with /opt/langtools/lib/end.o.\n\
  201. GDB will be unable to track shl_load/shl_unload calls"));
  202.       goto keep_going;
  203.     }
  204.   anaddr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  205.   dld_cache.hook.address = anaddr;

  206.   /* Grrr, this might not be an export symbol!  We have to find the
  207.      export stub.  */
  208.   msymbol
  209.     = hppa_lookup_stub_minimal_symbol (MSYMBOL_LINKAGE_NAME (msymbol.minsym),
  210.                                        EXPORT);
  211.   if (msymbol.minsym != NULL)
  212.     {
  213.       anaddr = MSYMBOL_VALUE (msymbol.minsym);
  214.       dld_cache.hook_stub.address = anaddr;
  215.     }
  216.   store_unsigned_integer (buf, 4, byte_order, anaddr);

  217.   msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
  218.   if (msymbol.minsym == NULL)
  219.     {
  220.       warning (_("\
  221. Unable to find __dld_hook symbol in object file.\n\
  222. Suggest linking with /opt/langtools/lib/end.o.\n\
  223. GDB will be unable to track shl_load/shl_unload calls"));
  224.       goto keep_going;
  225.     }
  226.   anaddr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  227.   status = target_write_memory (anaddr, buf, 4);

  228.   /* Now set a shlib_event breakpoint at __d_trap so we can track
  229.      significant shared library events.  */
  230.   msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
  231.   if (msymbol.minsym == NULL)
  232.     {
  233.       warning (_("\
  234. Unable to find __dld_d_trap symbol in object file.\n\
  235. Suggest linking with /opt/langtools/lib/end.o.\n\
  236. GDB will be unable to track shl_load/shl_unload calls"));
  237.       goto keep_going;
  238.     }
  239.   create_solib_event_breakpoint (target_gdbarch (),
  240.                                  BMSYMBOL_VALUE_ADDRESS (msymbol));

  241.   /* We have all the support usually found in end.o, so we can track
  242.      shl_load and shl_unload calls.  */
  243.   have_endo = 1;

  244. keep_going:

  245.   /* Get the address of __dld_flags, if no such symbol exists, then we can
  246.      not debug the shared code.  */
  247.   msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
  248.   if (msymbol.minsym == NULL)
  249.     {
  250.       error (_("Unable to find __dld_flags symbol in object file."));
  251.     }

  252.   anaddr = BMSYMBOL_VALUE_ADDRESS (msymbol);

  253.   /* Read the current contents.  */
  254.   status = target_read_memory (anaddr, buf, 4);
  255.   if (status != 0)
  256.     error (_("Unable to read __dld_flags."));
  257.   dld_flags = extract_unsigned_integer (buf, 4, byte_order);

  258.   /* If the libraries were not mapped private on HP-UX 11 and later, warn
  259.      the user.  On HP-UX 10 and earlier, there is no easy way to specify
  260.      that shared libraries should be privately mapped.  So, we just force
  261.      private mapping.  */
  262.   if (get_hpux_major_release () >= 11
  263.       && (dl_header.flags & SHLIB_TEXT_PRIVATE_ENABLE) == 0
  264.       && (dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
  265.     warning
  266.       (_("\
  267. Private mapping of shared library text was not specified\n\
  268. by the executable; setting a breakpoint in a shared library which\n\
  269. is not privately mapped will not work.  See the HP-UX 11i v3 chatr\n\
  270. manpage for methods to privately map shared library text."));

  271.   /* Turn on the flags we care about.  */
  272.   if (get_hpux_major_release () < 11)
  273.     dld_flags |= DLD_FLAGS_MAPPRIVATE;
  274.   if (have_endo)
  275.     dld_flags |= DLD_FLAGS_HOOKVALID;
  276.   store_unsigned_integer (buf, 4, byte_order, dld_flags);
  277.   status = target_write_memory (anaddr, buf, 4);
  278.   if (status != 0)
  279.     error (_("Unable to write __dld_flags."));

  280.   /* Now find the address of _start and set a breakpoint there.
  281.      We still need this code for two reasons:

  282.      * Not all sites have /opt/langtools/lib/end.o, so it's not always
  283.      possible to track the dynamic linker's events.

  284.      * At this time no events are triggered for shared libraries
  285.      loaded at startup time (what a crock).  */

  286.   msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
  287.   if (msymbol.minsym == NULL)
  288.     error (_("Unable to find _start symbol in object file."));

  289.   anaddr = BMSYMBOL_VALUE_ADDRESS (msymbol);

  290.   /* Make the breakpoint at "_start" a shared library event breakpoint.  */
  291.   create_solib_event_breakpoint (target_gdbarch (), anaddr);

  292.   clear_symtab_users (0);
  293. }

  294. static void
  295. som_special_symbol_handling (void)
  296. {
  297. }

  298. static void
  299. som_solib_desire_dynamic_linker_symbols (void)
  300. {
  301.   struct objfile *objfile;
  302.   struct unwind_table_entry *u;
  303.   struct bound_minimal_symbol dld_msymbol;

  304.   /* Do we already know the value of these symbols?  If so, then
  305.      we've no work to do.

  306.      (If you add clauses to this test, be sure to likewise update the
  307.      test within the loop.)  */

  308.   if (dld_cache.is_valid)
  309.     return;

  310.   ALL_OBJFILES (objfile)
  311.   {
  312.     dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
  313.     if (dld_msymbol.minsym != NULL)
  314.       {
  315.         dld_cache.load.address = MSYMBOL_VALUE (dld_msymbol.minsym);
  316.         dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
  317.       }

  318.     dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
  319.                                                           objfile);
  320.     if (dld_msymbol.minsym != NULL)
  321.       {
  322.         if (MSYMBOL_TYPE (dld_msymbol.minsym) == mst_solib_trampoline)
  323.           {
  324.             u = find_unwind_entry (MSYMBOL_VALUE (dld_msymbol.minsym));
  325.             if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
  326.               {
  327.                 dld_cache.load_stub.address
  328.                   = MSYMBOL_VALUE (dld_msymbol.minsym);
  329.                 dld_cache.load_stub.unwind = u;
  330.               }
  331.           }
  332.       }

  333.     dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
  334.     if (dld_msymbol.minsym != NULL)
  335.       {
  336.         dld_cache.unload.address = MSYMBOL_VALUE (dld_msymbol.minsym);
  337.         dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);

  338.         /* ??rehrauer: I'm not sure exactly what this is, but it appears
  339.            that on some HPUX 10.x versions, there's two unwind regions to
  340.            cover the body of "shl_unload", the second being 4 bytes past
  341.            the end of the first.  This is a large hack to handle that
  342.            case, but since I don't seem to have any legitimate way to
  343.            look for this thing via the symbol table...  */

  344.         if (dld_cache.unload.unwind != NULL)
  345.           {
  346.             u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
  347.             if (u != NULL)
  348.               {
  349.                 dld_cache.unload2.address = u->region_start;
  350.                 dld_cache.unload2.unwind = u;
  351.               }
  352.           }
  353.       }

  354.     dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
  355.                                                           objfile);
  356.     if (dld_msymbol.minsym != NULL)
  357.       {
  358.         if (MSYMBOL_TYPE (dld_msymbol.minsym) == mst_solib_trampoline)
  359.           {
  360.             u = find_unwind_entry (MSYMBOL_VALUE (dld_msymbol.minsym));
  361.             if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
  362.               {
  363.                 dld_cache.unload_stub.address
  364.                   = MSYMBOL_VALUE (dld_msymbol.minsym);
  365.                 dld_cache.unload_stub.unwind = u;
  366.               }
  367.           }
  368.       }

  369.     /* Did we find everything we were looking for?  If so, stop.  */
  370.     if ((dld_cache.load.address != 0)
  371.         && (dld_cache.load_stub.address != 0)
  372.         && (dld_cache.unload.address != 0)
  373.         && (dld_cache.unload_stub.address != 0))
  374.       {
  375.         dld_cache.is_valid = 1;
  376.         break;
  377.       }
  378.   }

  379.   dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
  380.   dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);

  381.   /* We're prepared not to find some of these symbols, which is why
  382.      this function is a "desire" operation, and not a "require".  */
  383. }

  384. static int
  385. som_in_dynsym_resolve_code (CORE_ADDR pc)
  386. {
  387.   struct unwind_table_entry *u_pc;

  388.   /* Are we in the dld itself?

  389.      ??rehrauer: Large hack -- We'll assume that any address in a
  390.      shared text region is the dld's text.  This would obviously
  391.      fall down if the user attached to a process, whose shlibs
  392.      weren't mapped to a (writeable) private region.  However, in
  393.      that case the debugger probably isn't able to set the fundamental
  394.      breakpoint in the dld callback anyways, so this hack should be
  395.      safe.  */

  396.   if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
  397.     return 1;

  398.   /* Cache the address of some symbols that are part of the dynamic
  399.      linker, if not already known.  */

  400.   som_solib_desire_dynamic_linker_symbols ();

  401.   /* Are we in the dld callback?  Or its export stub?  */
  402.   u_pc = find_unwind_entry (pc);
  403.   if (u_pc == NULL)
  404.     return 0;

  405.   if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
  406.     return 1;

  407.   /* Or the interface of the dld (i.e., "shl_load" or friends)?  */
  408.   if ((u_pc == dld_cache.load.unwind)
  409.       || (u_pc == dld_cache.unload.unwind)
  410.       || (u_pc == dld_cache.unload2.unwind)
  411.       || (u_pc == dld_cache.load_stub.unwind)
  412.       || (u_pc == dld_cache.unload_stub.unwind))
  413.     return 1;

  414.   /* Apparently this address isn't part of the dld's text.  */
  415.   return 0;
  416. }

  417. static void
  418. som_clear_solib (void)
  419. {
  420. }

  421. struct dld_list {
  422.   char name[4];
  423.   char info[4];
  424.   char text_addr[4];
  425.   char text_link_addr[4];
  426.   char text_end[4];
  427.   char data_start[4];
  428.   char bss_start[4];
  429.   char data_end[4];
  430.   char got_value[4];
  431.   char next[4];
  432.   char tsd_start_addr_ptr[4];
  433. };

  434. static CORE_ADDR
  435. link_map_start (void)
  436. {
  437.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  438.   struct bound_minimal_symbol sym;
  439.   CORE_ADDR addr;
  440.   gdb_byte buf[4];
  441.   unsigned int dld_flags;

  442.   sym = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
  443.   if (!sym.minsym)
  444.     error (_("Unable to find __dld_flags symbol in object file."));
  445.   addr = BMSYMBOL_VALUE_ADDRESS (sym);
  446.   read_memory (addr, buf, 4);
  447.   dld_flags = extract_unsigned_integer (buf, 4, byte_order);
  448.   if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
  449.     error (_("__dld_list is not valid according to __dld_flags."));

  450.   sym = lookup_minimal_symbol ("__dld_list", NULL, NULL);
  451.   if (!sym.minsym)
  452.     {
  453.       /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
  454.          but the data is still available if you know where to look.  */
  455.       sym = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
  456.       if (!sym.minsym)
  457.         {
  458.           error (_("Unable to find dynamic library list."));
  459.           return 0;
  460.         }
  461.       addr = BMSYMBOL_VALUE_ADDRESS (sym) - 8;
  462.     }
  463.   else
  464.     addr = BMSYMBOL_VALUE_ADDRESS (sym);

  465.   read_memory (addr, buf, 4);
  466.   addr = extract_unsigned_integer (buf, 4, byte_order);
  467.   if (addr == 0)
  468.     return 0;

  469.   read_memory (addr, buf, 4);
  470.   return extract_unsigned_integer (buf, 4, byte_order);
  471. }

  472. /* Does this so's name match the main binary?  */
  473. static int
  474. match_main (const char *name)
  475. {
  476.   return strcmp (name, objfile_name (symfile_objfile)) == 0;
  477. }

  478. static struct so_list *
  479. som_current_sos (void)
  480. {
  481.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  482.   CORE_ADDR lm;
  483.   struct so_list *head = 0;
  484.   struct so_list **link_ptr = &head;

  485.   for (lm = link_map_start (); lm; )
  486.     {
  487.       char *namebuf;
  488.       CORE_ADDR addr;
  489.       struct so_list *new;
  490.       struct cleanup *old_chain;
  491.       int errcode;
  492.       struct dld_list dbuf;
  493.       gdb_byte tsdbuf[4];

  494.       new = (struct so_list *) xmalloc (sizeof (struct so_list));
  495.       old_chain = make_cleanup (xfree, new);

  496.       memset (new, 0, sizeof (*new));
  497.       new->lm_info = xmalloc (sizeof (struct lm_info));
  498.       make_cleanup (xfree, new->lm_info);

  499.       read_memory (lm, (gdb_byte *)&dbuf, sizeof (struct dld_list));

  500.       addr = extract_unsigned_integer ((gdb_byte *)&dbuf.name,
  501.                                        sizeof (dbuf.name), byte_order);
  502.       target_read_string (addr, &namebuf, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
  503.       if (errcode != 0)
  504.         warning (_("Can't read pathname for load map: %s."),
  505.                  safe_strerror (errcode));
  506.       else
  507.         {
  508.           strncpy (new->so_name, namebuf, SO_NAME_MAX_PATH_SIZE - 1);
  509.           new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  510.           xfree (namebuf);
  511.           strcpy (new->so_original_name, new->so_name);
  512.         }

  513.         if (new->so_name[0] && !match_main (new->so_name))
  514.           {
  515.             struct lm_info *lmi = new->lm_info;
  516.             unsigned int tmp;

  517.             lmi->lm_addr = lm;

  518. #define EXTRACT(_fld) \
  519.   extract_unsigned_integer ((gdb_byte *)&dbuf._fld, \
  520.                             sizeof (dbuf._fld), byte_order);

  521.             lmi->text_addr = EXTRACT (text_addr);
  522.             tmp = EXTRACT (info);
  523.             lmi->library_version = (tmp >> 16) & 0xffff;
  524.             lmi->bind_mode = (tmp >> 8) & 0xff;
  525.             lmi->struct_version = tmp & 0xff;
  526.             lmi->text_link_addr = EXTRACT (text_link_addr);
  527.             lmi->text_end = EXTRACT (text_end);
  528.             lmi->data_start = EXTRACT (data_start);
  529.             lmi->bss_start = EXTRACT (bss_start);
  530.             lmi->data_end = EXTRACT (data_end);
  531.             lmi->got_value = EXTRACT (got_value);
  532.             tmp = EXTRACT (tsd_start_addr_ptr);
  533.             read_memory (tmp, tsdbuf, 4);
  534.             lmi->tsd_start_addr
  535.               = extract_unsigned_integer (tsdbuf, 4, byte_order);

  536. #ifdef SOLIB_SOM_DBG
  537.             printf ("\n+ library \"%s\" is described at %s\n", new->so_name,
  538.                         paddress (target_gdbarch (), lm));
  539.             printf ("  'version' is %d\n", new->lm_info->struct_version);
  540.             printf ("  'bind_mode' is %d\n", new->lm_info->bind_mode);
  541.             printf ("  'library_version' is %d\n",
  542.                         new->lm_info->library_version);
  543.             printf ("  'text_addr' is %s\n",
  544.                         paddress (target_gdbarch (), new->lm_info->text_addr));
  545.             printf ("  'text_link_addr' is %s\n",
  546.                         paddress (target_gdbarch (), new->lm_info->text_link_addr));
  547.             printf ("  'text_end' is %s\n",
  548.                         paddress (target_gdbarch (), new->lm_info->text_end));
  549.             printf ("  'data_start' is %s\n",
  550.                         paddress (target_gdbarch (), new->lm_info->data_start));
  551.             printf ("  'bss_start' is %s\n",
  552.                         paddress (target_gdbarch (), new->lm_info->bss_start));
  553.             printf ("  'data_end' is %s\n",
  554.                         paddress (target_gdbarch (), new->lm_info->data_end));
  555.             printf ("  'got_value' is %s\n",
  556.                         paddress (target_gdbarch (), new->lm_info->got_value));
  557.             printf ("  'tsd_start_addr' is %s\n",
  558.                         paddress (target_gdbarch (), new->lm_info->tsd_start_addr));
  559. #endif

  560.             new->addr_low = lmi->text_addr;
  561.             new->addr_high = lmi->text_end;

  562.             /* Link the new object onto the list.  */
  563.             new->next = NULL;
  564.             *link_ptr = new;
  565.             link_ptr = &new->next;
  566.           }
  567.          else
  568.           {
  569.             free_so (new);
  570.           }

  571.       lm = EXTRACT (next);
  572.       discard_cleanups (old_chain);
  573. #undef EXTRACT
  574.     }

  575.   /* TODO: The original somsolib code has logic to detect and eliminate
  576.      duplicate entries.  Do we need that?  */

  577.   return head;
  578. }

  579. static int
  580. som_open_symbol_file_object (void *from_ttyp)
  581. {
  582.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  583.   CORE_ADDR lm, l_name;
  584.   char *filename;
  585.   int errcode;
  586.   int from_tty = *(int *)from_ttyp;
  587.   gdb_byte buf[4];
  588.   struct cleanup *cleanup;

  589.   if (symfile_objfile)
  590.     if (!query (_("Attempt to reload symbols from process? ")))
  591.       return 0;

  592.   /* First link map member should be the executable.  */
  593.   if ((lm = link_map_start ()) == 0)
  594.     return 0;        /* failed somehow...  */

  595.   /* Read address of name from target memory to GDB.  */
  596.   read_memory (lm + offsetof (struct dld_list, name), buf, 4);

  597.   /* Convert the address to host format.  Assume that the address is
  598.      unsigned.  */
  599.   l_name = extract_unsigned_integer (buf, 4, byte_order);

  600.   if (l_name == 0)
  601.     return 0;                /* No filename.  */

  602.   /* Now fetch the filename from target memory.  */
  603.   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);

  604.   if (errcode)
  605.     {
  606.       warning (_("failed to read exec filename from attached file: %s"),
  607.                safe_strerror (errcode));
  608.       return 0;
  609.     }

  610.   cleanup = make_cleanup (xfree, filename);
  611.   /* Have a pathname: read the symbol file.  */
  612.   symbol_file_add_main (filename, from_tty);

  613.   do_cleanups (cleanup);
  614.   return 1;
  615. }

  616. static void
  617. som_free_so (struct so_list *so)
  618. {
  619.   xfree (so->lm_info);
  620. }

  621. static CORE_ADDR
  622. som_solib_thread_start_addr (struct so_list *so)
  623. {
  624.   return so->lm_info->tsd_start_addr;
  625. }

  626. /* Return the GOT value for the shared library in which ADDR belongs.  If
  627.    ADDR isn't in any known shared library, return zero.  */

  628. static CORE_ADDR
  629. som_solib_get_got_by_pc (CORE_ADDR addr)
  630. {
  631.   struct so_list *so_list = master_so_list ();
  632.   CORE_ADDR got_value = 0;

  633.   while (so_list)
  634.     {
  635.       if (so_list->lm_info->text_addr <= addr
  636.           && so_list->lm_info->text_end > addr)
  637.         {
  638.           got_value = so_list->lm_info->got_value;
  639.           break;
  640.         }
  641.       so_list = so_list->next;
  642.     }
  643.   return got_value;
  644. }

  645. /* Return the address of the handle of the shared library in which
  646.    ADDR belongs.  If ADDR isn't in any known shared library, return
  647.    zero.  */
  648. /* This function is used in initialize_hp_cxx_exception_support in
  649.    hppa-hpux-tdep.c.  */

  650. static CORE_ADDR
  651. som_solib_get_solib_by_pc (CORE_ADDR addr)
  652. {
  653.   struct so_list *so_list = master_so_list ();

  654.   while (so_list)
  655.     {
  656.       if (so_list->lm_info->text_addr <= addr
  657.           && so_list->lm_info->text_end > addr)
  658.         {
  659.           break;
  660.         }
  661.       so_list = so_list->next;
  662.     }
  663.   if (so_list)
  664.     return so_list->lm_info->lm_addr;
  665.   else
  666.     return 0;
  667. }


  668. static struct target_so_ops som_so_ops;

  669. extern initialize_file_ftype _initialize_som_solib; /* -Wmissing-prototypes */

  670. void
  671. _initialize_som_solib (void)
  672. {
  673.   som_so_ops.relocate_section_addresses = som_relocate_section_addresses;
  674.   som_so_ops.free_so = som_free_so;
  675.   som_so_ops.clear_solib = som_clear_solib;
  676.   som_so_ops.solib_create_inferior_hook = som_solib_create_inferior_hook;
  677.   som_so_ops.special_symbol_handling = som_special_symbol_handling;
  678.   som_so_ops.current_sos = som_current_sos;
  679.   som_so_ops.open_symbol_file_object = som_open_symbol_file_object;
  680.   som_so_ops.in_dynsym_resolve_code = som_in_dynsym_resolve_code;
  681.   som_so_ops.bfd_open = solib_bfd_open;
  682. }

  683. void
  684. som_solib_select (struct gdbarch *gdbarch)
  685. {
  686.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  687.   set_solib_ops (gdbarch, &som_so_ops);
  688.   tdep->solib_thread_start_addr = som_solib_thread_start_addr;
  689.   tdep->solib_get_got_by_pc = som_solib_get_got_by_pc;
  690.   tdep->solib_get_solib_by_pc = som_solib_get_solib_by_pc;
  691. }

  692. /* The rest of these functions are not part of the solib interface; they
  693.    are used by somread.c or hppa-hpux-tdep.c.  */

  694. int
  695. som_solib_section_offsets (struct objfile *objfile,
  696.                            struct section_offsets *offsets)
  697. {
  698.   struct so_list *so_list = master_so_list ();

  699.   while (so_list)
  700.     {
  701.       /* Oh what a pain!  We need the offsets before so_list->objfile
  702.          is valid.  The BFDs will never match.  Make a best guess.  */
  703.       if (strstr (objfile_name (objfile), so_list->so_name))
  704.         {
  705.           asection *private_section;
  706.           struct obj_section *sect;

  707.           /* The text offset is easy.  */
  708.           offsets->offsets[SECT_OFF_TEXT (objfile)]
  709.             = (so_list->lm_info->text_addr
  710.                - so_list->lm_info->text_link_addr);

  711.           /* We should look at presumed_dp in the SOM header, but
  712.              that's not easily available.  This should be OK though.  */
  713.           private_section = bfd_get_section_by_name (objfile->obfd,
  714.                                                      "$PRIVATE$");
  715.           if (!private_section)
  716.             {
  717.               warning (_("Unable to find $PRIVATE$ in shared library!"));
  718.               offsets->offsets[SECT_OFF_DATA (objfile)] = 0;
  719.               offsets->offsets[SECT_OFF_BSS (objfile)] = 0;
  720.               return 1;
  721.             }
  722.           if (objfile->sect_index_data != -1)
  723.             {
  724.               offsets->offsets[SECT_OFF_DATA (objfile)]
  725.                 = (so_list->lm_info->data_start - private_section->vma);
  726.               if (objfile->sect_index_bss != -1)
  727.                 offsets->offsets[SECT_OFF_BSS (objfile)]
  728.                   = ANOFFSET (offsets, SECT_OFF_DATA (objfile));
  729.             }

  730.           ALL_OBJFILE_OSECTIONS (objfile, sect)
  731.             {
  732.               flagword flags = bfd_get_section_flags (objfile->obfd,
  733.                                                       sect->the_bfd_section);

  734.               if ((flags & SEC_CODE) != 0)
  735.                 offsets->offsets[sect->the_bfd_section->index]
  736.                   = offsets->offsets[SECT_OFF_TEXT (objfile)];
  737.               else
  738.                 offsets->offsets[sect->the_bfd_section->index]
  739.                   = offsets->offsets[SECT_OFF_DATA (objfile)];
  740.             }

  741.           return 1;
  742.         }
  743.       so_list = so_list->next;
  744.     }
  745.   return 0;
  746. }