gdb/solib-dsbt.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
  2.    Copyright (C) 2010-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 "objfiles.h"
  20. #include "symtab.h"
  21. #include "language.h"
  22. #include "command.h"
  23. #include "gdbcmd.h"
  24. #include "elf-bfd.h"
  25. #include "gdb_bfd.h"

  26. #define GOT_MODULE_OFFSET 4

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

  29. /* TIC6X pointers are four bytes wide.  */
  30. enum { TIC6X_PTR_SIZE = 4 };

  31. /* Representation of loadmap and related structs for the TIC6X DSBT.  */

  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_dsbt_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_dsbt_loadmap {
  48.   /* Protocol version number, must be zero.  */
  49.   ext_Elf32_Word version;
  50.   /* A pointer to the DSBT table; the DSBT size and the index of this
  51.      module.  */
  52.   ext_Elf32_Word dsbt_table_ptr;
  53.   ext_Elf32_Word dsbt_size;
  54.   ext_Elf32_Word dsbt_index;
  55.   /* Number of segments in this map.  */
  56.   ext_Elf32_Word nsegs;
  57.   /* The actual memory map.  */
  58.   struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
  59. };

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

  72. struct int_elf32_dsbt_loadmap
  73. {
  74.   /* Protocol version number, must be zero.  */
  75.   int version;
  76.   CORE_ADDR dsbt_table_ptr;
  77.   /* A pointer to the DSBT table; the DSBT size and the index of this
  78.      module.  */
  79.   int dsbt_size, dsbt_index;
  80.   /* Number of segments in this map.  */
  81.   int nsegs;
  82.   /* The actual memory map.  */
  83.   struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
  84. };

  85. /* External link_map and elf32_dsbt_loadaddr struct definitions.  */

  86. typedef gdb_byte ext_ptr[4];

  87. struct ext_elf32_dsbt_loadaddr
  88. {
  89.   ext_ptr map;                        /* struct elf32_dsbt_loadmap *map; */
  90. };

  91. struct ext_link_map
  92. {
  93.   struct ext_elf32_dsbt_loadaddr l_addr;

  94.   /* Absolute file name object was found in.  */
  95.   ext_ptr l_name;                /* char *l_name; */

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

  98.   /* Chain of loaded objects.  */
  99.   ext_ptr l_next, l_prev;        /* struct link_map *l_next, *l_prev; */
  100. };

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

  102. struct lm_info
  103. {
  104.   /* The loadmap, digested into an easier to use form.  */
  105.   struct int_elf32_dsbt_loadmap *map;
  106. };

  107. /* Per pspace dsbt specific data.  */

  108. struct dsbt_info
  109. {
  110.   /* The load map, got value, etc. are not available from the chain
  111.      of loaded shared objects.  ``main_executable_lm_info'' provides
  112.      a way to get at this information so that it doesn't need to be
  113.      frequently recomputed.  Initialized by dsbt_relocate_main_executable.  */
  114.   struct lm_info *main_executable_lm_info;

  115.   /* Load maps for the main executable and the interpreter.  These are obtained
  116.      from ptrace.  They are the starting point for getting into the program,
  117.      and are required to find the solib list with the individual load maps for
  118.      each module.  */
  119.   struct int_elf32_dsbt_loadmap *exec_loadmap;
  120.   struct int_elf32_dsbt_loadmap *interp_loadmap;

  121.   /* Cached value for lm_base, below.  */
  122.   CORE_ADDR lm_base_cache;

  123.   /* Link map address for main module.  */
  124.   CORE_ADDR main_lm_addr;

  125.   CORE_ADDR interp_text_sect_low;
  126.   CORE_ADDR interp_text_sect_high;
  127.   CORE_ADDR interp_plt_sect_low;
  128.   CORE_ADDR interp_plt_sect_high;
  129. };

  130. /* Per-program-space data key.  */
  131. static const struct program_space_data *solib_dsbt_pspace_data;

  132. static void
  133. dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
  134. {
  135.   xfree (arg);
  136. }

  137. /* Get the current dsbt data.  If none is found yet, add it now.  This
  138.    function always returns a valid object.  */

  139. static struct dsbt_info *
  140. get_dsbt_info (void)
  141. {
  142.   struct dsbt_info *info;

  143.   info = program_space_data (current_program_space, solib_dsbt_pspace_data);
  144.   if (info != NULL)
  145.     return info;

  146.   info = XCNEW (struct dsbt_info);
  147.   set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);

  148.   info->lm_base_cache = 0;
  149.   info->main_lm_addr = 0;

  150.   return info;
  151. }


  152. static void
  153. dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
  154. {
  155.   int i;

  156.   if (map == NULL)
  157.     printf_filtered ("(null)\n");
  158.   else if (map->version != 0)
  159.     printf_filtered (_("Unsupported map version: %d\n"), map->version);
  160.   else
  161.     {
  162.       printf_filtered ("version %d\n", map->version);

  163.       for (i = 0; i < map->nsegs; i++)
  164.         printf_filtered ("%s:%s -> %s:%s\n",
  165.                          print_core_address (target_gdbarch (),
  166.                                              map->segs[i].p_vaddr),
  167.                          print_core_address (target_gdbarch (),
  168.                                              map->segs[i].p_vaddr
  169.                                              + map->segs[i].p_memsz),
  170.                          print_core_address (target_gdbarch (), map->segs[i].addr),
  171.                          print_core_address (target_gdbarch (), map->segs[i].addr
  172.                                              + map->segs[i].p_memsz));
  173.     }
  174. }

  175. /* Decode int_elf32_dsbt_loadmap from BUF.  */

  176. static struct int_elf32_dsbt_loadmap *
  177. decode_loadmap (gdb_byte *buf)
  178. {
  179.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  180.   struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
  181.   struct int_elf32_dsbt_loadmap *int_ldmbuf;

  182.   int version, seg, nsegs;
  183.   int int_ldmbuf_size;

  184.   ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;

  185.   /* Extract the version.  */
  186.   version = extract_unsigned_integer (ext_ldmbuf->version,
  187.                                       sizeof ext_ldmbuf->version,
  188.                                       byte_order);
  189.   if (version != 0)
  190.     {
  191.       /* We only handle version 0.  */
  192.       return NULL;
  193.     }

  194.   /* Extract the number of segments.  */
  195.   nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
  196.                                     sizeof ext_ldmbuf->nsegs,
  197.                                     byte_order);

  198.   if (nsegs <= 0)
  199.     return NULL;

  200.   /* Allocate space into which to put information extract from the
  201.      external loadsegs.  I.e, allocate the internal loadsegs.  */
  202.   int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
  203.                      + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
  204.   int_ldmbuf = xmalloc (int_ldmbuf_size);

  205.   /* Place extracted information in internal structs.  */
  206.   int_ldmbuf->version = version;
  207.   int_ldmbuf->nsegs = nsegs;
  208.   for (seg = 0; seg < nsegs; seg++)
  209.     {
  210.       int_ldmbuf->segs[seg].addr
  211.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
  212.                                     sizeof (ext_ldmbuf->segs[seg].addr),
  213.                                     byte_order);
  214.       int_ldmbuf->segs[seg].p_vaddr
  215.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
  216.                                     sizeof (ext_ldmbuf->segs[seg].p_vaddr),
  217.                                     byte_order);
  218.       int_ldmbuf->segs[seg].p_memsz
  219.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
  220.                                     sizeof (ext_ldmbuf->segs[seg].p_memsz),
  221.                                     byte_order);
  222.     }

  223.   xfree (ext_ldmbuf);
  224.   return int_ldmbuf;
  225. }


  226. static struct dsbt_info *get_dsbt_info (void);

  227. /* Interrogate the Linux kernel to find out where the program was loaded.
  228.    There are two load maps; one for the executable and one for the
  229.    interpreter (only in the case of a dynamically linked executable).  */

  230. static void
  231. dsbt_get_initial_loadmaps (void)
  232. {
  233.   gdb_byte *buf;
  234.   struct dsbt_info *info = get_dsbt_info ();

  235.   if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
  236.                               "exec", &buf))
  237.     {
  238.       info->exec_loadmap = NULL;
  239.       error (_("Error reading DSBT exec loadmap"));
  240.     }
  241.   info->exec_loadmap = decode_loadmap (buf);
  242.   if (solib_dsbt_debug)
  243.     dsbt_print_loadmap (info->exec_loadmap);

  244.   if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
  245.                               "interp", &buf))
  246.     {
  247.       info->interp_loadmap = NULL;
  248.       error (_("Error reading DSBT interp loadmap"));
  249.     }
  250.   info->interp_loadmap = decode_loadmap (buf);
  251.   if (solib_dsbt_debug)
  252.     dsbt_print_loadmap (info->interp_loadmap);
  253. }

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

  259. static struct int_elf32_dsbt_loadmap *
  260. fetch_loadmap (CORE_ADDR ldmaddr)
  261. {
  262.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  263.   struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
  264.   struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
  265.   struct int_elf32_dsbt_loadmap *int_ldmbuf;
  266.   int ext_ldmbuf_size, int_ldmbuf_size;
  267.   int version, seg, nsegs;

  268.   /* Fetch initial portion of the loadmap.  */
  269.   if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
  270.                           sizeof ext_ldmbuf_partial))
  271.     {
  272.       /* Problem reading the target's memory.  */
  273.       return NULL;
  274.     }

  275.   /* Extract the version.  */
  276.   version = extract_unsigned_integer (ext_ldmbuf_partial.version,
  277.                                       sizeof ext_ldmbuf_partial.version,
  278.                                       byte_order);
  279.   if (version != 0)
  280.     {
  281.       /* We only handle version 0.  */
  282.       return NULL;
  283.     }

  284.   /* Extract the number of segments.  */
  285.   nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
  286.                                     sizeof ext_ldmbuf_partial.nsegs,
  287.                                     byte_order);

  288.   if (nsegs <= 0)
  289.     return NULL;

  290.   /* Allocate space for the complete (external) loadmap.  */
  291.   ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
  292.     + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
  293.   ext_ldmbuf = xmalloc (ext_ldmbuf_size);

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

  296.   /* Read the rest of the loadmap from the target.  */
  297.   if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
  298.                           (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
  299.                           ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
  300.     {
  301.       /* Couldn't read rest of the loadmap.  */
  302.       xfree (ext_ldmbuf);
  303.       return NULL;
  304.     }

  305.   /* Allocate space into which to put information extract from the
  306.      external loadsegs.  I.e, allocate the internal loadsegs.  */
  307.   int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
  308.     + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
  309.   int_ldmbuf = xmalloc (int_ldmbuf_size);

  310.   /* Place extracted information in internal structs.  */
  311.   int_ldmbuf->version = version;
  312.   int_ldmbuf->nsegs = nsegs;
  313.   for (seg = 0; seg < nsegs; seg++)
  314.     {
  315.       int_ldmbuf->segs[seg].addr
  316.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
  317.                                     sizeof (ext_ldmbuf->segs[seg].addr),
  318.                                     byte_order);
  319.       int_ldmbuf->segs[seg].p_vaddr
  320.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
  321.                                     sizeof (ext_ldmbuf->segs[seg].p_vaddr),
  322.                                     byte_order);
  323.       int_ldmbuf->segs[seg].p_memsz
  324.         = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
  325.                                     sizeof (ext_ldmbuf->segs[seg].p_memsz),
  326.                                     byte_order);
  327.     }

  328.   xfree (ext_ldmbuf);
  329.   return int_ldmbuf;
  330. }

  331. static void dsbt_relocate_main_executable (void);
  332. static int enable_break (void);

  333. /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
  334.    returned and the corresponding PTR is set.  */

  335. static int
  336. scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
  337. {
  338.   int arch_size, step, sect_size;
  339.   long dyn_tag;
  340.   CORE_ADDR dyn_ptr, dyn_addr;
  341.   gdb_byte *bufend, *bufstart, *buf;
  342.   Elf32_External_Dyn *x_dynp_32;
  343.   Elf64_External_Dyn *x_dynp_64;
  344.   struct bfd_section *sect;
  345.   struct target_section *target_section;

  346.   if (abfd == NULL)
  347.     return 0;

  348.   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  349.     return 0;

  350.   arch_size = bfd_get_arch_size (abfd);
  351.   if (arch_size == -1)
  352.     return 0;

  353.   /* Find the start address of the .dynamic section.  */
  354.   sect = bfd_get_section_by_name (abfd, ".dynamic");
  355.   if (sect == NULL)
  356.     return 0;

  357.   for (target_section = current_target_sections->sections;
  358.        target_section < current_target_sections->sections_end;
  359.        target_section++)
  360.     if (sect == target_section->the_bfd_section)
  361.       break;
  362.   if (target_section < current_target_sections->sections_end)
  363.     dyn_addr = target_section->addr;
  364.   else
  365.     {
  366.       /* ABFD may come from OBJFILE acting only as a symbol file without being
  367.          loaded into the target (see add_symbol_file_command).  This case is
  368.          such fallback to the file VMA address without the possibility of
  369.          having the section relocated to its actual in-memory address.  */

  370.       dyn_addr = bfd_section_vma (abfd, sect);
  371.     }

  372.   /* Read in .dynamic from the BFD.  We will get the actual value
  373.      from memory later.  */
  374.   sect_size = bfd_section_size (abfd, sect);
  375.   buf = bufstart = alloca (sect_size);
  376.   if (!bfd_get_section_contents (abfd, sect,
  377.                                  buf, 0, sect_size))
  378.     return 0;

  379.   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
  380.   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
  381.                            : sizeof (Elf64_External_Dyn);
  382.   for (bufend = buf + sect_size;
  383.        buf < bufend;
  384.        buf += step)
  385.   {
  386.     if (arch_size == 32)
  387.       {
  388.         x_dynp_32 = (Elf32_External_Dyn *) buf;
  389.         dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
  390.         dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
  391.       }
  392.     else
  393.       {
  394.         x_dynp_64 = (Elf64_External_Dyn *) buf;
  395.         dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
  396.         dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
  397.       }
  398.      if (dyn_tag == DT_NULL)
  399.        return 0;
  400.      if (dyn_tag == dyntag)
  401.        {
  402.          /* If requested, try to read the runtime value of this .dynamic
  403.             entry.  */
  404.          if (ptr)
  405.            {
  406.              struct type *ptr_type;
  407.              gdb_byte ptr_buf[8];
  408.              CORE_ADDR ptr_addr;

  409.              ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  410.              ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
  411.              if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
  412.                dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
  413.              *ptr = dyn_ptr;
  414.            }
  415.          return 1;
  416.        }
  417.   }

  418.   return 0;
  419. }

  420. /* If no open symbol file, attempt to locate and open the main symbol
  421.    file.

  422.    If FROM_TTYP dereferences to a non-zero integer, allow messages to
  423.    be printed.  This parameter is a pointer rather than an int because
  424.    open_symbol_file_object is called via catch_errors and
  425.    catch_errors requires a pointer argument. */

  426. static int
  427. open_symbol_file_object (void *from_ttyp)
  428. {
  429.   /* Unimplemented.  */
  430.   return 0;
  431. }

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

  434. static CORE_ADDR
  435. displacement_from_map (struct int_elf32_dsbt_loadmap *map,
  436.                        CORE_ADDR addr)
  437. {
  438.   int seg;

  439.   for (seg = 0; seg < map->nsegs; seg++)
  440.     if (map->segs[seg].p_vaddr <= addr
  441.         && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
  442.       return map->segs[seg].addr - map->segs[seg].p_vaddr;

  443.   return 0;
  444. }

  445. /* Return the address from which the link map chain may be found.  On
  446.    DSBT, a pointer to the start of the link map will be located at the
  447.    word found at base of GOT + GOT_MODULE_OFFSET.

  448.    The base of GOT may be found in a number of ways.  Assuming that the
  449.    main executable has already been relocated,
  450.    1 The easiest way to find this value is to look up the address of
  451.    _GLOBAL_OFFSET_TABLE_.
  452.    2 The other way is to look for tag DT_PLTGOT, which contains the virtual
  453.    address of Global Offset Table.  .*/

  454. static CORE_ADDR
  455. lm_base (void)
  456. {
  457.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  458.   struct bound_minimal_symbol got_sym;
  459.   CORE_ADDR addr;
  460.   gdb_byte buf[TIC6X_PTR_SIZE];
  461.   struct dsbt_info *info = get_dsbt_info ();

  462.   /* One of our assumptions is that the main executable has been relocated.
  463.      Bail out if this has not happened.  (Note that post_create_inferior
  464.      in infcmd.c will call solib_add prior to solib_create_inferior_hook.
  465.      If we allow this to happen, lm_base_cache will be initialized with
  466.      a bogus value.  */
  467.   if (info->main_executable_lm_info == 0)
  468.     return 0;

  469.   /* If we already have a cached value, return it.  */
  470.   if (info->lm_base_cache)
  471.     return info->lm_base_cache;

  472.   got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
  473.                                    symfile_objfile);

  474.   if (got_sym.minsym != 0)
  475.     {
  476.       addr = BMSYMBOL_VALUE_ADDRESS (got_sym);
  477.       if (solib_dsbt_debug)
  478.         fprintf_unfiltered (gdb_stdlog,
  479.                             "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
  480.                             (unsigned int) addr);
  481.     }
  482.   else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
  483.     {
  484.       struct int_elf32_dsbt_loadmap *ldm;

  485.       dsbt_get_initial_loadmaps ();
  486.       ldm = info->exec_loadmap;
  487.       addr += displacement_from_map (ldm, addr);
  488.       if (solib_dsbt_debug)
  489.         fprintf_unfiltered (gdb_stdlog,
  490.                             "lm_base: get addr %x by DT_PLTGOT.\n",
  491.                             (unsigned int) addr);
  492.     }
  493.   else
  494.     {
  495.       if (solib_dsbt_debug)
  496.         fprintf_unfiltered (gdb_stdlog,
  497.                             "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
  498.       return 0;
  499.     }
  500.   addr += GOT_MODULE_OFFSET;

  501.   if (solib_dsbt_debug)
  502.     fprintf_unfiltered (gdb_stdlog,
  503.                         "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
  504.                         GOT_MODULE_OFFSET, hex_string_custom (addr, 8));

  505.   if (target_read_memory (addr, buf, sizeof buf) != 0)
  506.     return 0;
  507.   info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);

  508.   if (solib_dsbt_debug)
  509.     fprintf_unfiltered (gdb_stdlog,
  510.                         "lm_base: lm_base_cache = %s\n",
  511.                         hex_string_custom (info->lm_base_cache, 8));

  512.   return info->lm_base_cache;
  513. }


  514. /* Build a list of `struct so_list' objects describing the shared
  515.    objects currently loaded in the inferior.  This list does not
  516.    include an entry for the main executable file.

  517.    Note that we only gather information directly available from the
  518.    inferior --- we don't examine any of the shared library files
  519.    themselves.  The declaration of `struct so_list' says which fields
  520.    we provide values for.  */

  521. static struct so_list *
  522. dsbt_current_sos (void)
  523. {
  524.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  525.   CORE_ADDR lm_addr;
  526.   struct so_list *sos_head = NULL;
  527.   struct so_list **sos_next_ptr = &sos_head;
  528.   struct dsbt_info *info = get_dsbt_info ();

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

  533.      Note that the relocation of the main executable is also performed
  534.      by solib_create_inferior_hook, however, in the case of core
  535.      files, this hook is called too late in order to be of benefit to
  536.      solib_addsolib_add eventually calls this function,
  537.      dsbt_current_sos, and also precedes the call to
  538.      solib_create_inferior_hook.   (See post_create_inferior in
  539.      infcmd.c.)  */
  540.   if (info->main_executable_lm_info == 0 && core_bfd != NULL)
  541.     dsbt_relocate_main_executable ();

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

  544.   /* We have at least one link map entry.  Fetch the the lot of them,
  545.      building the solist chain.  */
  546.   while (lm_addr)
  547.     {
  548.       struct ext_link_map lm_buf;
  549.       ext_Elf32_Word indexword;
  550.       CORE_ADDR map_addr;
  551.       int dsbt_index;
  552.       int ret;

  553.       if (solib_dsbt_debug)
  554.         fprintf_unfiltered (gdb_stdlog,
  555.                             "current_sos: reading link_map entry at %s\n",
  556.                             hex_string_custom (lm_addr, 8));

  557.       ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
  558.       if (ret)
  559.         {
  560.           warning (_("dsbt_current_sos: Unable to read link map entry."
  561.                      "  Shared object chain may be incomplete."));
  562.           break;
  563.         }

  564.       /* Fetch the load map address.  */
  565.       map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
  566.                                            sizeof lm_buf.l_addr.map,
  567.                                            byte_order);

  568.       ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
  569.                                 sizeof indexword);
  570.       if (ret)
  571.         {
  572.           warning (_("dsbt_current_sos: Unable to read dsbt index."
  573.                      "  Shared object chain may be incomplete."));
  574.           break;
  575.         }
  576.       dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
  577.                                              byte_order);

  578.       /* If the DSBT index is zero, then we're looking at the entry
  579.          for the main executable.  By convention, we don't include
  580.          this in the list of shared objects.  */
  581.       if (dsbt_index != 0)
  582.         {
  583.           int errcode;
  584.           char *name_buf;
  585.           struct int_elf32_dsbt_loadmap *loadmap;
  586.           struct so_list *sop;
  587.           CORE_ADDR addr;

  588.           loadmap = fetch_loadmap (map_addr);
  589.           if (loadmap == NULL)
  590.             {
  591.               warning (_("dsbt_current_sos: Unable to fetch load map."
  592.                          "  Shared object chain may be incomplete."));
  593.               break;
  594.             }

  595.           sop = xcalloc (1, sizeof (struct so_list));
  596.           sop->lm_info = xcalloc (1, sizeof (struct lm_info));
  597.           sop->lm_info->map = loadmap;
  598.           /* Fetch the name.  */
  599.           addr = extract_unsigned_integer (lm_buf.l_name,
  600.                                            sizeof (lm_buf.l_name),
  601.                                            byte_order);
  602.           target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
  603.                               &errcode);

  604.           if (errcode != 0)
  605.             warning (_("Can't read pathname for link map entry: %s."),
  606.                      safe_strerror (errcode));
  607.           else
  608.             {
  609.               if (solib_dsbt_debug)
  610.                 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
  611.                                     name_buf);

  612.               strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
  613.               sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  614.               xfree (name_buf);
  615.               strcpy (sop->so_original_name, sop->so_name);
  616.             }

  617.           *sos_next_ptr = sop;
  618.           sos_next_ptr = &sop->next;
  619.         }
  620.       else
  621.         {
  622.           info->main_lm_addr = lm_addr;
  623.         }

  624.       lm_addr = extract_unsigned_integer (lm_buf.l_next,
  625.                                           sizeof (lm_buf.l_next), byte_order);
  626.     }

  627.   return sos_head;
  628. }

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

  631. static int
  632. dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
  633. {
  634.   struct dsbt_info *info = get_dsbt_info ();

  635.   return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
  636.           || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
  637.           || in_plt_section (pc));
  638. }

  639. /* Print a warning about being unable to set the dynamic linker
  640.    breakpoint.  */

  641. static void
  642. enable_break_failure_warning (void)
  643. {
  644.   warning (_("Unable to find dynamic linker breakpoint function.\n"
  645.              "GDB will be unable to debug shared library initializers\n"
  646.              "and track explicitly loaded dynamic code."));
  647. }

  648. /* Helper function for gdb_bfd_lookup_symbol.  */

  649. static int
  650. cmp_name (asymbol *sym, void *data)
  651. {
  652.   return (strcmp (sym->name, (const char *) data) == 0);
  653. }

  654. /* The dynamic linkers has, as part of its debugger interface, support
  655.    for arranging for the inferior to hit a breakpoint after mapping in
  656.    the shared libraries.  This function enables that breakpoint.

  657.    On the TIC6X, using the shared library (DSBT), GDB can try to place
  658.    a breakpoint on '_dl_debug_state' to monitor the shared library
  659.    event.  */

  660. static int
  661. enable_break (void)
  662. {
  663.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  664.   asection *interp_sect;
  665.   struct dsbt_info *info;

  666.   if (exec_bfd == NULL)
  667.     return 0;

  668.   if (!target_has_execution)
  669.     return 0;

  670.   info = get_dsbt_info ();

  671.   info->interp_text_sect_low = 0;
  672.   info->interp_text_sect_high = 0;
  673.   info->interp_plt_sect_low = 0;
  674.   info->interp_plt_sect_high = 0;

  675.   /* Find the .interp section; if not found, warn the user and drop
  676.      into the old breakpoint at symbol code.  */
  677.   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
  678.   if (interp_sect)
  679.     {
  680.       unsigned int interp_sect_size;
  681.       char *buf;
  682.       bfd *tmp_bfd = NULL;
  683.       CORE_ADDR addr;
  684.       gdb_byte addr_buf[TIC6X_PTR_SIZE];
  685.       struct int_elf32_dsbt_loadmap *ldm;
  686.       volatile struct gdb_exception ex;
  687.       int ret;

  688.       /* Read the contents of the .interp section into a local buffer;
  689.          the contents specify the dynamic linker this program uses.  */
  690.       interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
  691.       buf = alloca (interp_sect_size);
  692.       bfd_get_section_contents (exec_bfd, interp_sect,
  693.                                 buf, 0, interp_sect_size);

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

  697.       TRY_CATCH (ex, RETURN_MASK_ALL)
  698.         {
  699.           tmp_bfd = solib_bfd_open (buf);
  700.         }
  701.       if (tmp_bfd == NULL)
  702.         {
  703.           enable_break_failure_warning ();
  704.           return 0;
  705.         }

  706.       dsbt_get_initial_loadmaps ();
  707.       ldm = info->interp_loadmap;

  708.       /* Record the relocated start and end address of the dynamic linker
  709.          text and plt section for dsbt_in_dynsym_resolve_code.  */
  710.       interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
  711.       if (interp_sect)
  712.         {
  713.           info->interp_text_sect_low
  714.             = bfd_section_vma (tmp_bfd, interp_sect);
  715.           info->interp_text_sect_low
  716.             += displacement_from_map (ldm, info->interp_text_sect_low);
  717.           info->interp_text_sect_high
  718.             = info->interp_text_sect_low
  719.             + bfd_section_size (tmp_bfd, interp_sect);
  720.         }
  721.       interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
  722.       if (interp_sect)
  723.         {
  724.           info->interp_plt_sect_low =
  725.             bfd_section_vma (tmp_bfd, interp_sect);
  726.           info->interp_plt_sect_low
  727.             += displacement_from_map (ldm, info->interp_plt_sect_low);
  728.           info->interp_plt_sect_high =
  729.             info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
  730.         }

  731.       addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_state");
  732.       if (addr != 0)
  733.         {
  734.           if (solib_dsbt_debug)
  735.             fprintf_unfiltered (gdb_stdlog,
  736.                                 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
  737.                                 hex_string_custom (addr, 8));
  738.           addr += displacement_from_map (ldm, addr);

  739.           if (solib_dsbt_debug)
  740.             fprintf_unfiltered (gdb_stdlog,
  741.                                 "enable_break: _dl_debug_state (after relocation) = %s\n",
  742.                                 hex_string_custom (addr, 8));

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

  745.           ret = 1;
  746.         }
  747.       else
  748.         {
  749.           if (solib_dsbt_debug)
  750.             fprintf_unfiltered (gdb_stdlog,
  751.                                 "enable_break: _dl_debug_state is not found\n");
  752.           ret = 0;
  753.         }

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

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

  758.       return ret;
  759.     }

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

  762.   /* Failure return.  */
  763.   return 0;
  764. }

  765. /* Once the symbols from a shared object have been loaded in the usual
  766.    way, we are called to do any system specific symbol handling that
  767.    is needed.  */

  768. static void
  769. dsbt_special_symbol_handling (void)
  770. {
  771. }

  772. static void
  773. dsbt_relocate_main_executable (void)
  774. {
  775.   struct int_elf32_dsbt_loadmap *ldm;
  776.   struct cleanup *old_chain;
  777.   struct section_offsets *new_offsets;
  778.   int changed;
  779.   struct obj_section *osect;
  780.   struct dsbt_info *info = get_dsbt_info ();

  781.   dsbt_get_initial_loadmaps ();
  782.   ldm = info->exec_loadmap;

  783.   xfree (info->main_executable_lm_info);
  784.   info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
  785.   info->main_executable_lm_info->map = ldm;

  786.   new_offsets = xcalloc (symfile_objfile->num_sections,
  787.                          sizeof (struct section_offsets));
  788.   old_chain = make_cleanup (xfree, new_offsets);
  789.   changed = 0;

  790.   ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
  791.     {
  792.       CORE_ADDR orig_addr, addr, offset;
  793.       int osect_idx;
  794.       int seg;

  795.       osect_idx = osect - symfile_objfile->sections;

  796.       /* Current address of section.  */
  797.       addr = obj_section_addr (osect);
  798.       /* Offset from where this section started.  */
  799.       offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
  800.       /* Original address prior to any past relocations.  */
  801.       orig_addr = addr - offset;

  802.       for (seg = 0; seg < ldm->nsegs; seg++)
  803.         {
  804.           if (ldm->segs[seg].p_vaddr <= orig_addr
  805.               && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
  806.             {
  807.               new_offsets->offsets[osect_idx]
  808.                 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;

  809.               if (new_offsets->offsets[osect_idx] != offset)
  810.                 changed = 1;
  811.               break;
  812.             }
  813.         }
  814.     }

  815.   if (changed)
  816.     objfile_relocate (symfile_objfile, new_offsets);

  817.   do_cleanups (old_chain);

  818.   /* Now that symfile_objfile has been relocated, we can compute the
  819.      GOT value and stash it away.  */
  820. }

  821. /* When gdb starts up the inferior, it nurses it along (through the
  822.    shell) until it is ready to execute it's first instruction.  At this
  823.    point, this function gets called via solib_create_inferior_hook.

  824.    For the DSBT shared library, the main executable needs to be relocated.
  825.    The shared library breakpoints also need to be enabled.  */

  826. static void
  827. dsbt_solib_create_inferior_hook (int from_tty)
  828. {
  829.   /* Relocate main executable.  */
  830.   dsbt_relocate_main_executable ();

  831.   /* Enable shared library breakpoints.  */
  832.   if (!enable_break ())
  833.     {
  834.       warning (_("shared library handler failed to enable breakpoint"));
  835.       return;
  836.     }
  837. }

  838. static void
  839. dsbt_clear_solib (void)
  840. {
  841.   struct dsbt_info *info = get_dsbt_info ();

  842.   info->lm_base_cache = 0;
  843.   info->main_lm_addr = 0;
  844.   if (info->main_executable_lm_info != 0)
  845.     {
  846.       xfree (info->main_executable_lm_info->map);
  847.       xfree (info->main_executable_lm_info);
  848.       info->main_executable_lm_info = 0;
  849.     }
  850. }

  851. static void
  852. dsbt_free_so (struct so_list *so)
  853. {
  854.   xfree (so->lm_info->map);
  855.   xfree (so->lm_info);
  856. }

  857. static void
  858. dsbt_relocate_section_addresses (struct so_list *so,
  859.                                  struct target_section *sec)
  860. {
  861.   int seg;
  862.   struct int_elf32_dsbt_loadmap *map;

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

  864.   for (seg = 0; seg < map->nsegs; seg++)
  865.     {
  866.       if (map->segs[seg].p_vaddr <= sec->addr
  867.           && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
  868.         {
  869.           CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;

  870.           sec->addr += displ;
  871.           sec->endaddr += displ;
  872.           break;
  873.         }
  874.     }
  875. }
  876. static void
  877. show_dsbt_debug (struct ui_file *file, int from_tty,
  878.                  struct cmd_list_element *c, const char *value)
  879. {
  880.   fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
  881. }

  882. struct target_so_ops dsbt_so_ops;

  883. /* Provide a prototype to silence -Wmissing-prototypes.  */
  884. extern initialize_file_ftype _initialize_dsbt_solib;

  885. void
  886. _initialize_dsbt_solib (void)
  887. {
  888.   solib_dsbt_pspace_data
  889.     = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);

  890.   dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
  891.   dsbt_so_ops.free_so = dsbt_free_so;
  892.   dsbt_so_ops.clear_solib = dsbt_clear_solib;
  893.   dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
  894.   dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
  895.   dsbt_so_ops.current_sos = dsbt_current_sos;
  896.   dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
  897.   dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
  898.   dsbt_so_ops.bfd_open = solib_bfd_open;

  899.   /* Debug this file's internals.  */
  900.   add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
  901.                              &solib_dsbt_debug, _("\
  902. Set internal debugging of shared library code for DSBT ELF."), _("\
  903. Show internal debugging of shared library code for DSBT ELF."), _("\
  904. When non-zero, DSBT solib specific internal debugging is enabled."),
  905.                              NULL,
  906.                              show_dsbt_debug,
  907.                              &setdebuglist, &showdebuglist);
  908. }