gdb/solib-frv.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Handle FR-V (FDPIC) shared libraries for GDB, the GNU Debugger.
  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 "inferior.h"
  16. #include "gdbcore.h"
  17. #include "solib.h"
  18. #include "solist.h"
  19. #include "frv-tdep.h"
  20. #include "objfiles.h"
  21. #include "symtab.h"
  22. #include "language.h"
  23. #include "command.h"
  24. #include "gdbcmd.h"
  25. #include "elf/frv.h"
  26. #include "gdb_bfd.h"

  27. /* Flag which indicates whether internal debug messages should be printed.  */
  28. static unsigned int solib_frv_debug;

  29. /* FR-V pointers are four bytes wide.  */
  30. enum { FRV_PTR_SIZE = 4 };

  31. /* Representation of loadmap and related structs for the FR-V FDPIC ABI.  */

  32. /* External versions; the size and alignment of the fields should be
  33.    the same as those on the target.  When loaded, the placement of
  34.    the bits in each field will be the same as on the target.  */
  35. typedef gdb_byte ext_Elf32_Half[2];
  36. typedef gdb_byte ext_Elf32_Addr[4];
  37. typedef gdb_byte ext_Elf32_Word[4];

  38. struct ext_elf32_fdpic_loadseg
  39. {
  40.   /* Core address to which the segment is mapped.  */
  41.   ext_Elf32_Addr addr;
  42.   /* VMA recorded in the program header.  */
  43.   ext_Elf32_Addr p_vaddr;
  44.   /* Size of this segment in memory.  */
  45.   ext_Elf32_Word p_memsz;
  46. };

  47. struct ext_elf32_fdpic_loadmap {
  48.   /* Protocol version number, must be zero.  */
  49.   ext_Elf32_Half version;
  50.   /* Number of segments in this map.  */
  51.   ext_Elf32_Half nsegs;
  52.   /* The actual memory map.  */
  53.   struct ext_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
  54. };

  55. /* Internal versions; the types are GDB types and the data in each
  56.    of the fields is (or will be) decoded from the external struct
  57.    for ease of consumption.  */
  58. struct int_elf32_fdpic_loadseg
  59. {
  60.   /* Core address to which the segment is mapped.  */
  61.   CORE_ADDR addr;
  62.   /* VMA recorded in the program header.  */
  63.   CORE_ADDR p_vaddr;
  64.   /* Size of this segment in memory.  */
  65.   long p_memsz;
  66. };

  67. struct int_elf32_fdpic_loadmap {
  68.   /* Protocol version number, must be zero.  */
  69.   int version;
  70.   /* Number of segments in this map.  */
  71.   int nsegs;
  72.   /* The actual memory map.  */
  73.   struct int_elf32_fdpic_loadseg segs[1 /* nsegs, actually */];
  74. };

  75. /* Given address LDMADDR, fetch and decode the loadmap at that address.
  76.    Return NULL if there is a problem reading the target memory or if
  77.    there doesn't appear to be a loadmap at the given address.  The
  78.    allocated space (representing the loadmap) returned by this
  79.    function may be freed via a single call to xfree().  */

  80. static struct int_elf32_fdpic_loadmap *
  81. fetch_loadmap (CORE_ADDR ldmaddr)
  82. {
  83.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  84.   struct ext_elf32_fdpic_loadmap ext_ldmbuf_partial;
  85.   struct ext_elf32_fdpic_loadmap *ext_ldmbuf;
  86.   struct int_elf32_fdpic_loadmap *int_ldmbuf;
  87.   int ext_ldmbuf_size, int_ldmbuf_size;
  88.   int version, seg, nsegs;

  89.   /* Fetch initial portion of the loadmap.  */
  90.   if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
  91.                           sizeof ext_ldmbuf_partial))
  92.     {
  93.       /* Problem reading the target's memory.  */
  94.       return NULL;
  95.     }

  96.   /* Extract the version.  */
  97.   version = extract_unsigned_integer (ext_ldmbuf_partial.version,
  98.                                       sizeof ext_ldmbuf_partial.version,
  99.                                       byte_order);
  100.   if (version != 0)
  101.     {
  102.       /* We only handle version 0.  */
  103.       return NULL;
  104.     }

  105.   /* Extract the number of segments.  */
  106.   nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
  107.                                     sizeof ext_ldmbuf_partial.nsegs,
  108.                                     byte_order);

  109.   if (nsegs <= 0)
  110.     return NULL;

  111.   /* Allocate space for the complete (external) loadmap.  */
  112.   ext_ldmbuf_size = sizeof (struct ext_elf32_fdpic_loadmap)
  113.                + (nsegs - 1) * sizeof (struct ext_elf32_fdpic_loadseg);
  114.   ext_ldmbuf = xmalloc (ext_ldmbuf_size);

  115.   /* Copy over the portion of the loadmap that's already been read.  */
  116.   memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);

  117.   /* Read the rest of the loadmap from the target.  */
  118.   if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
  119.                           (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
  120.                           ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
  121.     {
  122.       /* Couldn't read rest of the loadmap.  */
  123.       xfree (ext_ldmbuf);
  124.       return NULL;
  125.     }

  126.   /* Allocate space into which to put information extract from the
  127.      external loadsegs.  I.e, allocate the internal loadsegs.  */
  128.   int_ldmbuf_size = sizeof (struct int_elf32_fdpic_loadmap)
  129.                + (nsegs - 1) * sizeof (struct int_elf32_fdpic_loadseg);
  130.   int_ldmbuf = xmalloc (int_ldmbuf_size);

  131.   /* Place extracted information in internal structs.  */
  132.   int_ldmbuf->version = version;
  133.   int_ldmbuf->nsegs = nsegs;
  134.   for (seg = 0; seg < nsegs; seg++)
  135.     {
  136.       int_ldmbuf->segs[seg].addr
  137.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
  138.                                     sizeof (ext_ldmbuf->segs[seg].addr),
  139.                                     byte_order);
  140.       int_ldmbuf->segs[seg].p_vaddr
  141.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
  142.                                     sizeof (ext_ldmbuf->segs[seg].p_vaddr),
  143.                                     byte_order);
  144.       int_ldmbuf->segs[seg].p_memsz
  145.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
  146.                                     sizeof (ext_ldmbuf->segs[seg].p_memsz),
  147.                                     byte_order);
  148.     }

  149.   xfree (ext_ldmbuf);
  150.   return int_ldmbuf;
  151. }

  152. /* External link_map and elf32_fdpic_loadaddr struct definitions.  */

  153. typedef gdb_byte ext_ptr[4];

  154. struct ext_elf32_fdpic_loadaddr
  155. {
  156.   ext_ptr map;                        /* struct elf32_fdpic_loadmap *map; */
  157.   ext_ptr got_value;                /* void *got_value; */
  158. };

  159. struct ext_link_map
  160. {
  161.   struct ext_elf32_fdpic_loadaddr l_addr;

  162.   /* Absolute file name object was found in.  */
  163.   ext_ptr l_name;                /* char *l_name; */

  164.   /* Dynamic section of the shared object.  */
  165.   ext_ptr l_ld;                        /* ElfW(Dyn) *l_ld; */

  166.   /* Chain of loaded objects.  */
  167.   ext_ptr l_next, l_prev;        /* struct link_map *l_next, *l_prev; */
  168. };

  169. /* Link map info to include in an allocated so_list entry.  */

  170. struct lm_info
  171.   {
  172.     /* The loadmap, digested into an easier to use form.  */
  173.     struct int_elf32_fdpic_loadmap *map;
  174.     /* The GOT address for this link map entry.  */
  175.     CORE_ADDR got_value;
  176.     /* The link map address, needed for frv_fetch_objfile_link_map().  */
  177.     CORE_ADDR lm_addr;

  178.     /* Cached dynamic symbol table and dynamic relocs initialized and
  179.        used only by find_canonical_descriptor_in_load_object().

  180.        Note: kevinb/2004-02-26: It appears that calls to
  181.        bfd_canonicalize_dynamic_reloc() will use the same symbols as
  182.        those supplied to the first call to this function.  Therefore,
  183.        it's important to NOT free the asymbol ** data structure
  184.        supplied to the first call.  Thus the caching of the dynamic
  185.        symbols (dyn_syms) is critical for correct operation.  The
  186.        caching of the dynamic relocations could be dispensed with.  */
  187.     asymbol **dyn_syms;
  188.     arelent **dyn_relocs;
  189.     int dyn_reloc_count;        /* Number of dynamic relocs.  */

  190.   };

  191. /* The load map, got value, etc. are not available from the chain
  192.    of loaded shared objects.  ``main_executable_lm_info'' provides
  193.    a way to get at this information so that it doesn't need to be
  194.    frequently recomputed.  Initialized by frv_relocate_main_executable().  */
  195. static struct lm_info *main_executable_lm_info;

  196. static void frv_relocate_main_executable (void);
  197. static CORE_ADDR main_got (void);
  198. static int enable_break2 (void);

  199. /* Implement the "open_symbol_file_object" target_so_ops method.  */

  200. static int
  201. open_symbol_file_object (void *from_ttyp)
  202. {
  203.   /* Unimplemented.  */
  204.   return 0;
  205. }

  206. /* Cached value for lm_base(), below.  */
  207. static CORE_ADDR lm_base_cache = 0;

  208. /* Link map address for main module.  */
  209. static CORE_ADDR main_lm_addr = 0;

  210. /* Return the address from which the link map chain may be found.  On
  211.    the FR-V, this may be found in a number of ways.  Assuming that the
  212.    main executable has already been relocated, the easiest way to find
  213.    this value is to look up the address of _GLOBAL_OFFSET_TABLE_.  A
  214.    pointer to the start of the link map will be located at the word found
  215.    at _GLOBAL_OFFSET_TABLE_ + 8.  (This is part of the dynamic linker
  216.    reserve area mandated by the ABI.)  */

  217. static CORE_ADDR
  218. lm_base (void)
  219. {
  220.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  221.   struct bound_minimal_symbol got_sym;
  222.   CORE_ADDR addr;
  223.   gdb_byte buf[FRV_PTR_SIZE];

  224.   /* One of our assumptions is that the main executable has been relocated.
  225.      Bail out if this has not happened.  (Note that post_create_inferior()
  226.      in infcmd.c will call solib_add prior to solib_create_inferior_hook().
  227.      If we allow this to happen, lm_base_cache will be initialized with
  228.      a bogus value.  */
  229.   if (main_executable_lm_info == 0)
  230.     return 0;

  231.   /* If we already have a cached value, return it.  */
  232.   if (lm_base_cache)
  233.     return lm_base_cache;

  234.   got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
  235.                                    symfile_objfile);
  236.   if (got_sym.minsym == 0)
  237.     {
  238.       if (solib_frv_debug)
  239.         fprintf_unfiltered (gdb_stdlog,
  240.                             "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
  241.       return 0;
  242.     }

  243.   addr = BMSYMBOL_VALUE_ADDRESS (got_sym) + 8;

  244.   if (solib_frv_debug)
  245.     fprintf_unfiltered (gdb_stdlog,
  246.                         "lm_base: _GLOBAL_OFFSET_TABLE_ + 8 = %s\n",
  247.                         hex_string_custom (addr, 8));

  248.   if (target_read_memory (addr, buf, sizeof buf) != 0)
  249.     return 0;
  250.   lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);

  251.   if (solib_frv_debug)
  252.     fprintf_unfiltered (gdb_stdlog,
  253.                         "lm_base: lm_base_cache = %s\n",
  254.                         hex_string_custom (lm_base_cache, 8));

  255.   return lm_base_cache;
  256. }


  257. /* Implement the "current_sos" target_so_ops method.  */

  258. static struct so_list *
  259. frv_current_sos (void)
  260. {
  261.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  262.   CORE_ADDR lm_addr, mgot;
  263.   struct so_list *sos_head = NULL;
  264.   struct so_list **sos_next_ptr = &sos_head;

  265.   /* Make sure that the main executable has been relocated.  This is
  266.      required in order to find the address of the global offset table,
  267.      which in turn is used to find the link map info.  (See lm_base()
  268.      for details.)

  269.      Note that the relocation of the main executable is also performed
  270.      by solib_create_inferior_hook(), however, in the case of core
  271.      files, this hook is called too late in order to be of benefit to
  272.      solib_addsolib_add eventually calls this this function,
  273.      frv_current_sos, and also precedes the call to
  274.      solib_create_inferior_hook().   (See post_create_inferior() in
  275.      infcmd.c.)  */
  276.   if (main_executable_lm_info == 0 && core_bfd != NULL)
  277.     frv_relocate_main_executable ();

  278.   /* Fetch the GOT corresponding to the main executable.  */
  279.   mgot = main_got ();

  280.   /* Locate the address of the first link map struct.  */
  281.   lm_addr = lm_base ();

  282.   /* We have at least one link map entry.  Fetch the lot of them,
  283.      building the solist chain.  */
  284.   while (lm_addr)
  285.     {
  286.       struct ext_link_map lm_buf;
  287.       CORE_ADDR got_addr;

  288.       if (solib_frv_debug)
  289.         fprintf_unfiltered (gdb_stdlog,
  290.                             "current_sos: reading link_map entry at %s\n",
  291.                             hex_string_custom (lm_addr, 8));

  292.       if (target_read_memory (lm_addr, (gdb_byte *) &lm_buf,
  293.                               sizeof (lm_buf)) != 0)
  294.         {
  295.           warning (_("frv_current_sos: Unable to read link map entry.  "
  296.                      "Shared object chain may be incomplete."));
  297.           break;
  298.         }

  299.       got_addr
  300.         = extract_unsigned_integer (lm_buf.l_addr.got_value,
  301.                                     sizeof (lm_buf.l_addr.got_value),
  302.                                     byte_order);
  303.       /* If the got_addr is the same as mgotr, then we're looking at the
  304.          entry for the main executable.  By convention, we don't include
  305.          this in the list of shared objects.  */
  306.       if (got_addr != mgot)
  307.         {
  308.           int errcode;
  309.           char *name_buf;
  310.           struct int_elf32_fdpic_loadmap *loadmap;
  311.           struct so_list *sop;
  312.           CORE_ADDR addr;

  313.           /* Fetch the load map address.  */
  314.           addr = extract_unsigned_integer (lm_buf.l_addr.map,
  315.                                            sizeof lm_buf.l_addr.map,
  316.                                            byte_order);
  317.           loadmap = fetch_loadmap (addr);
  318.           if (loadmap == NULL)
  319.             {
  320.               warning (_("frv_current_sos: Unable to fetch load map.  "
  321.                          "Shared object chain may be incomplete."));
  322.               break;
  323.             }

  324.           sop = xcalloc (1, sizeof (struct so_list));
  325.           sop->lm_info = xcalloc (1, sizeof (struct lm_info));
  326.           sop->lm_info->map = loadmap;
  327.           sop->lm_info->got_value = got_addr;
  328.           sop->lm_info->lm_addr = lm_addr;
  329.           /* Fetch the name.  */
  330.           addr = extract_unsigned_integer (lm_buf.l_name,
  331.                                            sizeof (lm_buf.l_name),
  332.                                            byte_order);
  333.           target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
  334.                               &errcode);

  335.           if (solib_frv_debug)
  336.             fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
  337.                                 name_buf);

  338.           if (errcode != 0)
  339.             warning (_("Can't read pathname for link map entry: %s."),
  340.                      safe_strerror (errcode));
  341.           else
  342.             {
  343.               strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
  344.               sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  345.               xfree (name_buf);
  346.               strcpy (sop->so_original_name, sop->so_name);
  347.             }

  348.           *sos_next_ptr = sop;
  349.           sos_next_ptr = &sop->next;
  350.         }
  351.       else
  352.         {
  353.           main_lm_addr = lm_addr;
  354.         }

  355.       lm_addr = extract_unsigned_integer (lm_buf.l_next,
  356.                                           sizeof (lm_buf.l_next), byte_order);
  357.     }

  358.   enable_break2 ();

  359.   return sos_head;
  360. }


  361. /* Return 1 if PC lies in the dynamic symbol resolution code of the
  362.    run time loader.  */

  363. static CORE_ADDR interp_text_sect_low;
  364. static CORE_ADDR interp_text_sect_high;
  365. static CORE_ADDR interp_plt_sect_low;
  366. static CORE_ADDR interp_plt_sect_high;

  367. static int
  368. frv_in_dynsym_resolve_code (CORE_ADDR pc)
  369. {
  370.   return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
  371.           || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
  372.           || in_plt_section (pc));
  373. }

  374. /* Given a loadmap and an address, return the displacement needed
  375.    to relocate the address.  */

  376. static CORE_ADDR
  377. displacement_from_map (struct int_elf32_fdpic_loadmap *map,
  378.                        CORE_ADDR addr)
  379. {
  380.   int seg;

  381.   for (seg = 0; seg < map->nsegs; seg++)
  382.     {
  383.       if (map->segs[seg].p_vaddr <= addr
  384.           && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
  385.         {
  386.           return map->segs[seg].addr - map->segs[seg].p_vaddr;
  387.         }
  388.     }

  389.   return 0;
  390. }

  391. /* Print a warning about being unable to set the dynamic linker
  392.    breakpoint.  */

  393. static void
  394. enable_break_failure_warning (void)
  395. {
  396.   warning (_("Unable to find dynamic linker breakpoint function.\n"
  397.            "GDB will be unable to debug shared library initializers\n"
  398.            "and track explicitly loaded dynamic code."));
  399. }

  400. /* Helper function for gdb_bfd_lookup_symbol.  */

  401. static int
  402. cmp_name (asymbol *sym, void *data)
  403. {
  404.   return (strcmp (sym->name, (const char *) data) == 0);
  405. }

  406. /* Arrange for dynamic linker to hit breakpoint.

  407.    The dynamic linkers has, as part of its debugger interface, support
  408.    for arranging for the inferior to hit a breakpoint after mapping in
  409.    the shared libraries.  This function enables that breakpoint.

  410.    On the FR-V, using the shared library (FDPIC) ABI, the symbol
  411.    _dl_debug_addr points to the r_debug struct which contains
  412.    a field called r_brk.  r_brk is the address of the function
  413.    descriptor upon which a breakpoint must be placed.  Being a
  414.    function descriptor, we must extract the entry point in order
  415.    to set the breakpoint.

  416.    Our strategy will be to get the .interp section from the
  417.    executable.  This section will provide us with the name of the
  418.    interpreter.  We'll open the interpreter and then look up
  419.    the address of _dl_debug_addr.  We then relocate this address
  420.    using the interpreter's loadmap.  Once the relocated address
  421.    is known, we fetch the value (address) corresponding to r_brk
  422.    and then use that value to fetch the entry point of the function
  423.    we're interested in.  */

  424. static int enable_break2_done = 0;

  425. static int
  426. enable_break2 (void)
  427. {
  428.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  429.   int success = 0;
  430.   char **bkpt_namep;
  431.   asection *interp_sect;

  432.   if (enable_break2_done)
  433.     return 1;

  434.   interp_text_sect_low = interp_text_sect_high = 0;
  435.   interp_plt_sect_low = interp_plt_sect_high = 0;

  436.   /* Find the .interp section; if not found, warn the user and drop
  437.      into the old breakpoint at symbol code.  */
  438.   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
  439.   if (interp_sect)
  440.     {
  441.       unsigned int interp_sect_size;
  442.       char *buf;
  443.       bfd *tmp_bfd = NULL;
  444.       int status;
  445.       CORE_ADDR addr, interp_loadmap_addr;
  446.       gdb_byte addr_buf[FRV_PTR_SIZE];
  447.       struct int_elf32_fdpic_loadmap *ldm;
  448.       volatile struct gdb_exception ex;

  449.       /* Read the contents of the .interp section into a local buffer;
  450.          the contents specify the dynamic linker this program uses.  */
  451.       interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
  452.       buf = alloca (interp_sect_size);
  453.       bfd_get_section_contents (exec_bfd, interp_sect,
  454.                                 buf, 0, interp_sect_size);

  455.       /* Now we need to figure out where the dynamic linker was
  456.          loaded so that we can load its symbols and place a breakpoint
  457.          in the dynamic linker itself.

  458.          This address is stored on the stack.  However, I've been unable
  459.          to find any magic formula to find it for Solaris (appears to
  460.          be trivial on GNU/Linux).  Therefore, we have to try an alternate
  461.          mechanism to find the dynamic linker's base address.  */

  462.       TRY_CATCH (ex, RETURN_MASK_ALL)
  463.         {
  464.           tmp_bfd = solib_bfd_open (buf);
  465.         }
  466.       if (tmp_bfd == NULL)
  467.         {
  468.           enable_break_failure_warning ();
  469.           return 0;
  470.         }

  471.       status = frv_fdpic_loadmap_addresses (target_gdbarch (),
  472.                                             &interp_loadmap_addr, 0);
  473.       if (status < 0)
  474.         {
  475.           warning (_("Unable to determine dynamic linker loadmap address."));
  476.           enable_break_failure_warning ();
  477.           gdb_bfd_unref (tmp_bfd);
  478.           return 0;
  479.         }

  480.       if (solib_frv_debug)
  481.         fprintf_unfiltered (gdb_stdlog,
  482.                             "enable_break: interp_loadmap_addr = %s\n",
  483.                             hex_string_custom (interp_loadmap_addr, 8));

  484.       ldm = fetch_loadmap (interp_loadmap_addr);
  485.       if (ldm == NULL)
  486.         {
  487.           warning (_("Unable to load dynamic linker loadmap at address %s."),
  488.                    hex_string_custom (interp_loadmap_addr, 8));
  489.           enable_break_failure_warning ();
  490.           gdb_bfd_unref (tmp_bfd);
  491.           return 0;
  492.         }

  493.       /* Record the relocated start and end address of the dynamic linker
  494.          text and plt section for svr4_in_dynsym_resolve_code.  */
  495.       interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
  496.       if (interp_sect)
  497.         {
  498.           interp_text_sect_low
  499.             = bfd_section_vma (tmp_bfd, interp_sect);
  500.           interp_text_sect_low
  501.             += displacement_from_map (ldm, interp_text_sect_low);
  502.           interp_text_sect_high
  503.             = interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
  504.         }
  505.       interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
  506.       if (interp_sect)
  507.         {
  508.           interp_plt_sect_low =
  509.             bfd_section_vma (tmp_bfd, interp_sect);
  510.           interp_plt_sect_low
  511.             += displacement_from_map (ldm, interp_plt_sect_low);
  512.           interp_plt_sect_high =
  513.             interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
  514.         }

  515.       addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_addr");

  516.       if (addr == 0)
  517.         {
  518.           warning (_("Could not find symbol _dl_debug_addr "
  519.                      "in dynamic linker"));
  520.           enable_break_failure_warning ();
  521.           gdb_bfd_unref (tmp_bfd);
  522.           return 0;
  523.         }

  524.       if (solib_frv_debug)
  525.         fprintf_unfiltered (gdb_stdlog,
  526.                             "enable_break: _dl_debug_addr "
  527.                             "(prior to relocation) = %s\n",
  528.                             hex_string_custom (addr, 8));

  529.       addr += displacement_from_map (ldm, addr);

  530.       if (solib_frv_debug)
  531.         fprintf_unfiltered (gdb_stdlog,
  532.                             "enable_break: _dl_debug_addr "
  533.                             "(after relocation) = %s\n",
  534.                             hex_string_custom (addr, 8));

  535.       /* Fetch the address of the r_debug struct.  */
  536.       if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
  537.         {
  538.           warning (_("Unable to fetch contents of _dl_debug_addr "
  539.                      "(at address %s) from dynamic linker"),
  540.                    hex_string_custom (addr, 8));
  541.         }
  542.       addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);

  543.       if (solib_frv_debug)
  544.         fprintf_unfiltered (gdb_stdlog,
  545.                             "enable_break: _dl_debug_addr[0..3] = %s\n",
  546.                             hex_string_custom (addr, 8));

  547.       /* If it's zero, then the ldso hasn't initialized yet, and so
  548.          there are no shared libs yet loaded.  */
  549.       if (addr == 0)
  550.         {
  551.           if (solib_frv_debug)
  552.             fprintf_unfiltered (gdb_stdlog,
  553.                                 "enable_break: ldso not yet initialized\n");
  554.           /* Do not warn, but mark to run again.  */
  555.           return 0;
  556.         }

  557.       /* Fetch the r_brk field.  It's 8 bytes from the start of
  558.          _dl_debug_addr.  */
  559.       if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
  560.         {
  561.           warning (_("Unable to fetch _dl_debug_addr->r_brk "
  562.                      "(at address %s) from dynamic linker"),
  563.                    hex_string_custom (addr + 8, 8));
  564.           enable_break_failure_warning ();
  565.           gdb_bfd_unref (tmp_bfd);
  566.           return 0;
  567.         }
  568.       addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);

  569.       /* Now fetch the function entry point.  */
  570.       if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
  571.         {
  572.           warning (_("Unable to fetch _dl_debug_addr->.r_brk entry point "
  573.                      "(at address %s) from dynamic linker"),
  574.                    hex_string_custom (addr, 8));
  575.           enable_break_failure_warning ();
  576.           gdb_bfd_unref (tmp_bfd);
  577.           return 0;
  578.         }
  579.       addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);

  580.       /* We're done with the temporary bfd.  */
  581.       gdb_bfd_unref (tmp_bfd);

  582.       /* We're also done with the loadmap.  */
  583.       xfree (ldm);

  584.       /* Remove all the solib event breakpoints.  Their addresses
  585.          may have changed since the last time we ran the program.  */
  586.       remove_solib_event_breakpoints ();

  587.       /* Now (finally!) create the solib breakpoint.  */
  588.       create_solib_event_breakpoint (target_gdbarch (), addr);

  589.       enable_break2_done = 1;

  590.       return 1;
  591.     }

  592.   /* Tell the user we couldn't set a dynamic linker breakpoint.  */
  593.   enable_break_failure_warning ();

  594.   /* Failure return.  */
  595.   return 0;
  596. }

  597. static int
  598. enable_break (void)
  599. {
  600.   asection *interp_sect;
  601.   CORE_ADDR entry_point;

  602.   if (symfile_objfile == NULL)
  603.     {
  604.       if (solib_frv_debug)
  605.         fprintf_unfiltered (gdb_stdlog,
  606.                             "enable_break: No symbol file found.\n");
  607.       return 0;
  608.     }

  609.   if (!entry_point_address_query (&entry_point))
  610.     {
  611.       if (solib_frv_debug)
  612.         fprintf_unfiltered (gdb_stdlog,
  613.                             "enable_break: Symbol file has no entry point.\n");
  614.       return 0;
  615.     }

  616.   /* Check for the presence of a .interp section.  If there is no
  617.      such section, the executable is statically linked.  */

  618.   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");

  619.   if (interp_sect == NULL)
  620.     {
  621.       if (solib_frv_debug)
  622.         fprintf_unfiltered (gdb_stdlog,
  623.                             "enable_break: No .interp section found.\n");
  624.       return 0;
  625.     }

  626.   create_solib_event_breakpoint (target_gdbarch (), entry_point);

  627.   if (solib_frv_debug)
  628.     fprintf_unfiltered (gdb_stdlog,
  629.                         "enable_break: solib event breakpoint "
  630.                         "placed at entry point: %s\n",
  631.                         hex_string_custom (entry_point, 8));
  632.   return 1;
  633. }

  634. /* Implement the "special_symbol_handling" target_so_ops method.  */

  635. static void
  636. frv_special_symbol_handling (void)
  637. {
  638.   /* Nothing needed for FRV.  */
  639. }

  640. static void
  641. frv_relocate_main_executable (void)
  642. {
  643.   int status;
  644.   CORE_ADDR exec_addr, interp_addr;
  645.   struct int_elf32_fdpic_loadmap *ldm;
  646.   struct cleanup *old_chain;
  647.   struct section_offsets *new_offsets;
  648.   int changed;
  649.   struct obj_section *osect;

  650.   status = frv_fdpic_loadmap_addresses (target_gdbarch (),
  651.                                         &interp_addr, &exec_addr);

  652.   if (status < 0 || (exec_addr == 0 && interp_addr == 0))
  653.     {
  654.       /* Not using FDPIC ABI, so do nothing.  */
  655.       return;
  656.     }

  657.   /* Fetch the loadmap located at ``exec_addr''.  */
  658.   ldm = fetch_loadmap (exec_addr);
  659.   if (ldm == NULL)
  660.     error (_("Unable to load the executable's loadmap."));

  661.   if (main_executable_lm_info)
  662.     xfree (main_executable_lm_info);
  663.   main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
  664.   main_executable_lm_info->map = ldm;

  665.   new_offsets = xcalloc (symfile_objfile->num_sections,
  666.                          sizeof (struct section_offsets));
  667.   old_chain = make_cleanup (xfree, new_offsets);
  668.   changed = 0;

  669.   ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
  670.     {
  671.       CORE_ADDR orig_addr, addr, offset;
  672.       int osect_idx;
  673.       int seg;

  674.       osect_idx = osect - symfile_objfile->sections;

  675.       /* Current address of section.  */
  676.       addr = obj_section_addr (osect);
  677.       /* Offset from where this section started.  */
  678.       offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
  679.       /* Original address prior to any past relocations.  */
  680.       orig_addr = addr - offset;

  681.       for (seg = 0; seg < ldm->nsegs; seg++)
  682.         {
  683.           if (ldm->segs[seg].p_vaddr <= orig_addr
  684.               && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
  685.             {
  686.               new_offsets->offsets[osect_idx]
  687.                 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;

  688.               if (new_offsets->offsets[osect_idx] != offset)
  689.                 changed = 1;
  690.               break;
  691.             }
  692.         }
  693.     }

  694.   if (changed)
  695.     objfile_relocate (symfile_objfile, new_offsets);

  696.   do_cleanups (old_chain);

  697.   /* Now that symfile_objfile has been relocated, we can compute the
  698.      GOT value and stash it away.  */
  699.   main_executable_lm_info->got_value = main_got ();
  700. }

  701. /* Implement the "create_inferior_hook" target_solib_ops method.

  702.    For the FR-V shared library ABI (FDPIC), the main executable needs
  703.    to be relocated.  The shared library breakpoints also need to be
  704.    enabled.  */

  705. static void
  706. frv_solib_create_inferior_hook (int from_tty)
  707. {
  708.   /* Relocate main executable.  */
  709.   frv_relocate_main_executable ();

  710.   /* Enable shared library breakpoints.  */
  711.   if (!enable_break ())
  712.     {
  713.       warning (_("shared library handler failed to enable breakpoint"));
  714.       return;
  715.     }
  716. }

  717. static void
  718. frv_clear_solib (void)
  719. {
  720.   lm_base_cache = 0;
  721.   enable_break2_done = 0;
  722.   main_lm_addr = 0;
  723.   if (main_executable_lm_info != 0)
  724.     {
  725.       xfree (main_executable_lm_info->map);
  726.       xfree (main_executable_lm_info->dyn_syms);
  727.       xfree (main_executable_lm_info->dyn_relocs);
  728.       xfree (main_executable_lm_info);
  729.       main_executable_lm_info = 0;
  730.     }
  731. }

  732. static void
  733. frv_free_so (struct so_list *so)
  734. {
  735.   xfree (so->lm_info->map);
  736.   xfree (so->lm_info->dyn_syms);
  737.   xfree (so->lm_info->dyn_relocs);
  738.   xfree (so->lm_info);
  739. }

  740. static void
  741. frv_relocate_section_addresses (struct so_list *so,
  742.                                  struct target_section *sec)
  743. {
  744.   int seg;
  745.   struct int_elf32_fdpic_loadmap *map;

  746.   map = so->lm_info->map;

  747.   for (seg = 0; seg < map->nsegs; seg++)
  748.     {
  749.       if (map->segs[seg].p_vaddr <= sec->addr
  750.           && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
  751.         {
  752.           CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;

  753.           sec->addr += displ;
  754.           sec->endaddr += displ;
  755.           break;
  756.         }
  757.     }
  758. }

  759. /* Return the GOT address associated with the main executable.  Return
  760.    0 if it can't be found.  */

  761. static CORE_ADDR
  762. main_got (void)
  763. {
  764.   struct bound_minimal_symbol got_sym;

  765.   got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_",
  766.                                    NULL, symfile_objfile);
  767.   if (got_sym.minsym == 0)
  768.     return 0;

  769.   return BMSYMBOL_VALUE_ADDRESS (got_sym);
  770. }

  771. /* Find the global pointer for the given function address ADDR.  */

  772. CORE_ADDR
  773. frv_fdpic_find_global_pointer (CORE_ADDR addr)
  774. {
  775.   struct so_list *so;

  776.   so = master_so_list ();
  777.   while (so)
  778.     {
  779.       int seg;
  780.       struct int_elf32_fdpic_loadmap *map;

  781.       map = so->lm_info->map;

  782.       for (seg = 0; seg < map->nsegs; seg++)
  783.         {
  784.           if (map->segs[seg].addr <= addr
  785.               && addr < map->segs[seg].addr + map->segs[seg].p_memsz)
  786.             return so->lm_info->got_value;
  787.         }

  788.       so = so->next;
  789.     }

  790.   /* Didn't find it in any of the shared objects.  So assume it's in the
  791.      main executable.  */
  792.   return main_got ();
  793. }

  794. /* Forward declarations for frv_fdpic_find_canonical_descriptor().  */
  795. static CORE_ADDR find_canonical_descriptor_in_load_object
  796.   (CORE_ADDR, CORE_ADDR, const char *, bfd *, struct lm_info *);

  797. /* Given a function entry point, attempt to find the canonical descriptor
  798.    associated with that entry point.  Return 0 if no canonical descriptor
  799.    could be found.  */

  800. CORE_ADDR
  801. frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
  802. {
  803.   const char *name;
  804.   CORE_ADDR addr;
  805.   CORE_ADDR got_value;
  806.   struct int_elf32_fdpic_loadmap *ldm = 0;
  807.   struct symbol *sym;

  808.   /* Fetch the corresponding global pointer for the entry point.  */
  809.   got_value = frv_fdpic_find_global_pointer (entry_point);

  810.   /* Attempt to find the name of the function.  If the name is available,
  811.      it'll be used as an aid in finding matching functions in the dynamic
  812.      symbol table.  */
  813.   sym = find_pc_function (entry_point);
  814.   if (sym == 0)
  815.     name = 0;
  816.   else
  817.     name = SYMBOL_LINKAGE_NAME (sym);

  818.   /* Check the main executable.  */
  819.   addr = find_canonical_descriptor_in_load_object
  820.            (entry_point, got_value, name, symfile_objfile->obfd,
  821.             main_executable_lm_info);

  822.   /* If descriptor not found via main executable, check each load object
  823.      in list of shared objects.  */
  824.   if (addr == 0)
  825.     {
  826.       struct so_list *so;

  827.       so = master_so_list ();
  828.       while (so)
  829.         {
  830.           addr = find_canonical_descriptor_in_load_object
  831.                    (entry_point, got_value, name, so->abfd, so->lm_info);

  832.           if (addr != 0)
  833.             break;

  834.           so = so->next;
  835.         }
  836.     }

  837.   return addr;
  838. }

  839. static CORE_ADDR
  840. find_canonical_descriptor_in_load_object
  841.   (CORE_ADDR entry_point, CORE_ADDR got_value, const char *name, bfd *abfd,
  842.    struct lm_info *lm)
  843. {
  844.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  845.   arelent *rel;
  846.   unsigned int i;
  847.   CORE_ADDR addr = 0;

  848.   /* Nothing to do if no bfd.  */
  849.   if (abfd == 0)
  850.     return 0;

  851.   /* Nothing to do if no link map.  */
  852.   if (lm == 0)
  853.     return 0;

  854.   /* We want to scan the dynamic relocs for R_FRV_FUNCDESC relocations.
  855.      (More about this later.)  But in order to fetch the relocs, we
  856.      need to first fetch the dynamic symbols.  These symbols need to
  857.      be cached due to the way that bfd_canonicalize_dynamic_reloc()
  858.      works.  (See the comments in the declaration of struct lm_info
  859.      for more information.)  */
  860.   if (lm->dyn_syms == NULL)
  861.     {
  862.       long storage_needed;
  863.       unsigned int number_of_symbols;

  864.       /* Determine amount of space needed to hold the dynamic symbol table.  */
  865.       storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);

  866.       /* If there are no dynamic symbols, there's nothing to do.  */
  867.       if (storage_needed <= 0)
  868.         return 0;

  869.       /* Allocate space for the dynamic symbol table.  */
  870.       lm->dyn_syms = (asymbol **) xmalloc (storage_needed);

  871.       /* Fetch the dynamic symbol table.  */
  872.       number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, lm->dyn_syms);

  873.       if (number_of_symbols == 0)
  874.         return 0;
  875.     }

  876.   /* Fetch the dynamic relocations if not already cached.  */
  877.   if (lm->dyn_relocs == NULL)
  878.     {
  879.       long storage_needed;

  880.       /* Determine amount of space needed to hold the dynamic relocs.  */
  881.       storage_needed = bfd_get_dynamic_reloc_upper_bound (abfd);

  882.       /* Bail out if there are no dynamic relocs.  */
  883.       if (storage_needed <= 0)
  884.         return 0;

  885.       /* Allocate space for the relocs.  */
  886.       lm->dyn_relocs = (arelent **) xmalloc (storage_needed);

  887.       /* Fetch the dynamic relocs.  */
  888.       lm->dyn_reloc_count
  889.         = bfd_canonicalize_dynamic_reloc (abfd, lm->dyn_relocs, lm->dyn_syms);
  890.     }

  891.   /* Search the dynamic relocs.  */
  892.   for (i = 0; i < lm->dyn_reloc_count; i++)
  893.     {
  894.       rel = lm->dyn_relocs[i];

  895.       /* Relocs of interest are those which meet the following
  896.          criteria:

  897.            - the names match (assuming the caller could provide
  898.              a name which matches ``entry_point'').
  899.            - the relocation type must be R_FRV_FUNCDESC.  Relocs
  900.              of this type are used (by the dynamic linker) to
  901.              look up the address of a canonical descriptor (allocating
  902.              it if need be) and initializing the GOT entry referred
  903.              to by the offset to the address of the descriptor.

  904.          These relocs of interest may be used to obtain a
  905.          candidate descriptor by first adjusting the reloc's
  906.          address according to the link map and then dereferencing
  907.          this address (which is a GOT entry) to obtain a descriptor
  908.          address.  */
  909.       if ((name == 0 || strcmp (name, (*rel->sym_ptr_ptr)->name) == 0)
  910.           && rel->howto->type == R_FRV_FUNCDESC)
  911.         {
  912.           gdb_byte buf [FRV_PTR_SIZE];

  913.           /* Compute address of address of candidate descriptor.  */
  914.           addr = rel->address + displacement_from_map (lm->map, rel->address);

  915.           /* Fetch address of candidate descriptor.  */
  916.           if (target_read_memory (addr, buf, sizeof buf) != 0)
  917.             continue;
  918.           addr = extract_unsigned_integer (buf, sizeof buf, byte_order);

  919.           /* Check for matching entry point.  */
  920.           if (target_read_memory (addr, buf, sizeof buf) != 0)
  921.             continue;
  922.           if (extract_unsigned_integer (buf, sizeof buf, byte_order)
  923.               != entry_point)
  924.             continue;

  925.           /* Check for matching got value.  */
  926.           if (target_read_memory (addr + 4, buf, sizeof buf) != 0)
  927.             continue;
  928.           if (extract_unsigned_integer (buf, sizeof buf, byte_order)
  929.               != got_value)
  930.             continue;

  931.           /* Match was successful!  Exit loop.  */
  932.           break;
  933.         }
  934.     }

  935.   return addr;
  936. }

  937. /* Given an objfile, return the address of its link map.  This value is
  938.    needed for TLS support.  */
  939. CORE_ADDR
  940. frv_fetch_objfile_link_map (struct objfile *objfile)
  941. {
  942.   struct so_list *so;

  943.   /* Cause frv_current_sos() to be run if it hasn't been already.  */
  944.   if (main_lm_addr == 0)
  945.     solib_add (0, 0, 0, 1);

  946.   /* frv_current_sos() will set main_lm_addr for the main executable.  */
  947.   if (objfile == symfile_objfile)
  948.     return main_lm_addr;

  949.   /* The other link map addresses may be found by examining the list
  950.      of shared libraries.  */
  951.   for (so = master_so_list (); so; so = so->next)
  952.     {
  953.       if (so->objfile == objfile)
  954.         return so->lm_info->lm_addr;
  955.     }

  956.   /* Not found!  */
  957.   return 0;
  958. }

  959. struct target_so_ops frv_so_ops;

  960. /* Provide a prototype to silence -Wmissing-prototypes.  */
  961. extern initialize_file_ftype _initialize_frv_solib;

  962. void
  963. _initialize_frv_solib (void)
  964. {
  965.   frv_so_ops.relocate_section_addresses = frv_relocate_section_addresses;
  966.   frv_so_ops.free_so = frv_free_so;
  967.   frv_so_ops.clear_solib = frv_clear_solib;
  968.   frv_so_ops.solib_create_inferior_hook = frv_solib_create_inferior_hook;
  969.   frv_so_ops.special_symbol_handling = frv_special_symbol_handling;
  970.   frv_so_ops.current_sos = frv_current_sos;
  971.   frv_so_ops.open_symbol_file_object = open_symbol_file_object;
  972.   frv_so_ops.in_dynsym_resolve_code = frv_in_dynsym_resolve_code;
  973.   frv_so_ops.bfd_open = solib_bfd_open;

  974.   /* Debug this file's internals.  */
  975.   add_setshow_zuinteger_cmd ("solib-frv", class_maintenance,
  976.                              &solib_frv_debug, _("\
  977. Set internal debugging of shared library code for FR-V."), _("\
  978. Show internal debugging of shared library code for FR-V."), _("\
  979. When non-zero, FR-V solib specific internal debugging is enabled."),
  980.                              NULL,
  981.                              NULL, /* FIXME: i18n: */
  982.                              &setdebuglist, &showdebuglist);
  983. }