gdb/exec.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Work with executable files, for GDB.

  2.    Copyright (C) 1988-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 "frame.h"
  16. #include "inferior.h"
  17. #include "target.h"
  18. #include "gdbcmd.h"
  19. #include "language.h"
  20. #include "filenames.h"
  21. #include "symfile.h"
  22. #include "objfiles.h"
  23. #include "completer.h"
  24. #include "value.h"
  25. #include "exec.h"
  26. #include "observer.h"
  27. #include "arch-utils.h"
  28. #include "gdbthread.h"
  29. #include "progspace.h"
  30. #include "gdb_bfd.h"
  31. #include "gcore.h"

  32. #include <fcntl.h>
  33. #include "readline/readline.h"
  34. #include "gdbcore.h"

  35. #include <ctype.h>
  36. #include <sys/stat.h>

  37. void (*deprecated_file_changed_hook) (char *);

  38. /* Prototypes for local functions */

  39. static void file_command (char *, int);

  40. static void set_section_command (char *, int);

  41. static void exec_files_info (struct target_ops *);

  42. static void init_exec_ops (void);

  43. void _initialize_exec (void);

  44. /* The target vector for executable files.  */

  45. static struct target_ops exec_ops;

  46. /* Whether to open exec and core files read-only or read-write.  */

  47. int write_files = 0;
  48. static void
  49. show_write_files (struct ui_file *file, int from_tty,
  50.                   struct cmd_list_element *c, const char *value)
  51. {
  52.   fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
  53.                     value);
  54. }


  55. static void
  56. exec_open (const char *args, int from_tty)
  57. {
  58.   target_preopen (from_tty);
  59.   exec_file_attach (args, from_tty);
  60. }

  61. /* Close and clear exec_bfd.  If we end up with no target sections to
  62.    read memory from, this unpushes the exec_ops target.  */

  63. void
  64. exec_close (void)
  65. {
  66.   if (exec_bfd)
  67.     {
  68.       bfd *abfd = exec_bfd;

  69.       gdb_bfd_unref (abfd);

  70.       /* Removing target sections may close the exec_ops target.
  71.          Clear exec_bfd before doing so to prevent recursion.  */
  72.       exec_bfd = NULL;
  73.       exec_bfd_mtime = 0;

  74.       remove_target_sections (&exec_bfd);

  75.       xfree (exec_filename);
  76.       exec_filename = NULL;
  77.     }
  78. }

  79. /* This is the target_close implementation.  Clears all target
  80.    sections and closes all executable bfds from all program spaces.  */

  81. static void
  82. exec_close_1 (struct target_ops *self)
  83. {
  84.   struct program_space *ss;
  85.   struct cleanup *old_chain;

  86.   old_chain = save_current_program_space ();
  87.   ALL_PSPACES (ss)
  88.   {
  89.     set_current_program_space (ss);
  90.     clear_section_table (current_target_sections);
  91.     exec_close ();
  92.   }

  93.   do_cleanups (old_chain);
  94. }

  95. void
  96. exec_file_clear (int from_tty)
  97. {
  98.   /* Remove exec file.  */
  99.   exec_close ();

  100.   if (from_tty)
  101.     printf_unfiltered (_("No executable file now.\n"));
  102. }

  103. /* Set FILENAME as the new exec file.

  104.    This function is intended to be behave essentially the same
  105.    as exec_file_command, except that the latter will detect when
  106.    a target is being debugged, and will ask the user whether it
  107.    should be shut down first.  (If the answer is "no", then the
  108.    new file is ignored.)

  109.    This file is used by exec_file_command, to do the work of opening
  110.    and processing the exec file after any prompting has happened.

  111.    And, it is used by child_attach, when the attach command was
  112.    given a pid but not a exec pathname, and the attach command could
  113.    figure out the pathname from the pid.  (In this case, we shouldn't
  114.    ask the user whether the current target should be shut down --
  115.    we're supplying the exec pathname late for good reason.)  */

  116. void
  117. exec_file_attach (const char *filename, int from_tty)
  118. {
  119.   struct cleanup *cleanups;

  120.   /* First, acquire a reference to the current exec_bfd.  We release
  121.      this at the end of the function; but acquiring it now lets the
  122.      BFD cache return it if this call refers to the same file.  */
  123.   gdb_bfd_ref (exec_bfd);
  124.   cleanups = make_cleanup_bfd_unref (exec_bfd);

  125.   /* Remove any previous exec file.  */
  126.   exec_close ();

  127.   /* Now open and digest the file the user requested, if any.  */

  128.   if (!filename)
  129.     {
  130.       if (from_tty)
  131.         printf_unfiltered (_("No executable file now.\n"));

  132.       set_gdbarch_from_file (NULL);
  133.     }
  134.   else
  135.     {
  136.       char *scratch_pathname, *canonical_pathname;
  137.       int scratch_chan;
  138.       struct target_section *sections = NULL, *sections_end = NULL;
  139.       char **matching;

  140.       scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
  141.                    write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
  142.                             &scratch_pathname);
  143. #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
  144.       if (scratch_chan < 0)
  145.         {
  146.           char *exename = alloca (strlen (filename) + 5);

  147.           strcat (strcpy (exename, filename), ".exe");
  148.           scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
  149.              write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
  150.              &scratch_pathname);
  151.         }
  152. #endif
  153.       if (scratch_chan < 0)
  154.         perror_with_name (filename);

  155.       make_cleanup (xfree, scratch_pathname);

  156.       /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
  157.          better BFD caching.  */
  158.       canonical_pathname = gdb_realpath (scratch_pathname);
  159.       make_cleanup (xfree, canonical_pathname);

  160.       if (write_files)
  161.         exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
  162.                                   FOPEN_RUB, scratch_chan);
  163.       else
  164.         exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);

  165.       if (!exec_bfd)
  166.         {
  167.           error (_("\"%s\": could not open as an executable file: %s"),
  168.                  scratch_pathname, bfd_errmsg (bfd_get_error ()));
  169.         }

  170.       gdb_assert (exec_filename == NULL);
  171.       exec_filename = gdb_realpath_keepfile (scratch_pathname);

  172.       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
  173.         {
  174.           /* Make sure to close exec_bfd, or else "run" might try to use
  175.              it.  */
  176.           exec_close ();
  177.           error (_("\"%s\": not in executable format: %s"),
  178.                  scratch_pathname,
  179.                  gdb_bfd_errmsg (bfd_get_error (), matching));
  180.         }

  181.       if (build_section_table (exec_bfd, &sections, &sections_end))
  182.         {
  183.           /* Make sure to close exec_bfd, or else "run" might try to use
  184.              it.  */
  185.           exec_close ();
  186.           error (_("\"%s\": can't find the file sections: %s"),
  187.                  scratch_pathname, bfd_errmsg (bfd_get_error ()));
  188.         }

  189.       exec_bfd_mtime = bfd_get_mtime (exec_bfd);

  190.       validate_files ();

  191.       set_gdbarch_from_file (exec_bfd);

  192.       /* Add the executable's sections to the current address spaces'
  193.          list of sections.  This possibly pushes the exec_ops
  194.          target.  */
  195.       add_target_sections (&exec_bfd, sections, sections_end);
  196.       xfree (sections);

  197.       /* Tell display code (if any) about the changed file name.  */
  198.       if (deprecated_exec_file_display_hook)
  199.         (*deprecated_exec_file_display_hook) (filename);
  200.     }

  201.   do_cleanups (cleanups);

  202.   bfd_cache_close_all ();
  203.   observer_notify_executable_changed ();
  204. }

  205. /*  Process the first arg in ARGS as the new exec file.

  206.    Note that we have to explicitly ignore additional args, since we can
  207.    be called from file_command(), which also calls symbol_file_command()
  208.    which can take multiple args.

  209.    If ARGS is NULL, we just want to close the exec file.  */

  210. static void
  211. exec_file_command (char *args, int from_tty)
  212. {
  213.   char **argv;
  214.   char *filename;

  215.   if (from_tty && target_has_execution
  216.       && !query (_("A program is being debugged already.\n"
  217.                    "Are you sure you want to change the file? ")))
  218.     error (_("File not changed."));

  219.   if (args)
  220.     {
  221.       struct cleanup *cleanups;

  222.       /* Scan through the args and pick up the first non option arg
  223.          as the filename.  */

  224.       argv = gdb_buildargv (args);
  225.       cleanups = make_cleanup_freeargv (argv);

  226.       for (; (*argv != NULL) && (**argv == '-'); argv++)
  227.         {;
  228.         }
  229.       if (*argv == NULL)
  230.         error (_("No executable file name was specified"));

  231.       filename = tilde_expand (*argv);
  232.       make_cleanup (xfree, filename);
  233.       exec_file_attach (filename, from_tty);

  234.       do_cleanups (cleanups);
  235.     }
  236.   else
  237.     exec_file_attach (NULL, from_tty);
  238. }

  239. /* Set both the exec file and the symbol file, in one command.
  240.    What a novelty.  Why did GDB go through four major releases before this
  241.    command was added?  */

  242. static void
  243. file_command (char *arg, int from_tty)
  244. {
  245.   /* FIXME, if we lose on reading the symbol file, we should revert
  246.      the exec file, but that's rough.  */
  247.   exec_file_command (arg, from_tty);
  248.   symbol_file_command (arg, from_tty);
  249.   if (deprecated_file_changed_hook)
  250.     deprecated_file_changed_hook (arg);
  251. }


  252. /* Locate all mappable sections of a BFD file.
  253.    table_pp_char is a char * to get it through bfd_map_over_sections;
  254.    we cast it back to its proper type.  */

  255. static void
  256. add_to_section_table (bfd *abfd, struct bfd_section *asect,
  257.                       void *table_pp_char)
  258. {
  259.   struct target_section **table_pp = (struct target_section **) table_pp_char;
  260.   flagword aflag;

  261.   gdb_assert (abfd == asect->owner);

  262.   /* Check the section flags, but do not discard zero-length sections, since
  263.      some symbols may still be attached to this section.  For instance, we
  264.      encountered on sparc-solaris 2.10 a shared library with an empty .bss
  265.      section to which a symbol named "_end" was attached.  The address
  266.      of this symbol still needs to be relocated.  */
  267.   aflag = bfd_get_section_flags (abfd, asect);
  268.   if (!(aflag & SEC_ALLOC))
  269.     return;

  270.   (*table_pp)->owner = NULL;
  271.   (*table_pp)->the_bfd_section = asect;
  272.   (*table_pp)->addr = bfd_section_vma (abfd, asect);
  273.   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
  274.   (*table_pp)++;
  275. }

  276. /* See exec.h.  */

  277. void
  278. clear_section_table (struct target_section_table *table)
  279. {
  280.   xfree (table->sections);
  281.   table->sections = table->sections_end = NULL;
  282. }

  283. /* Resize section table TABLE by ADJUSTMENT.
  284.    ADJUSTMENT may be negative, in which case the caller must have already
  285.    removed the sections being deleted.
  286.    Returns the old size.  */

  287. static int
  288. resize_section_table (struct target_section_table *table, int adjustment)
  289. {
  290.   int old_count;
  291.   int new_count;

  292.   old_count = table->sections_end - table->sections;

  293.   new_count = adjustment + old_count;

  294.   if (new_count)
  295.     {
  296.       table->sections = xrealloc (table->sections,
  297.                                   sizeof (struct target_section) * new_count);
  298.       table->sections_end = table->sections + new_count;
  299.     }
  300.   else
  301.     clear_section_table (table);

  302.   return old_count;
  303. }

  304. /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
  305.    Returns 0 if OK, 1 on error.  */

  306. int
  307. build_section_table (struct bfd *some_bfd, struct target_section **start,
  308.                      struct target_section **end)
  309. {
  310.   unsigned count;

  311.   count = bfd_count_sections (some_bfd);
  312.   if (*start)
  313.     xfree (* start);
  314.   *start = (struct target_section *) xmalloc (count * sizeof (**start));
  315.   *end = *start;
  316.   bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
  317.   if (*end > *start + count)
  318.     internal_error (__FILE__, __LINE__,
  319.                     _("failed internal consistency check"));
  320.   /* We could realloc the table, but it probably loses for most files.  */
  321.   return 0;
  322. }

  323. /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
  324.    current set of target sections.  */

  325. void
  326. add_target_sections (void *owner,
  327.                      struct target_section *sections,
  328.                      struct target_section *sections_end)
  329. {
  330.   int count;
  331.   struct target_section_table *table = current_target_sections;

  332.   count = sections_end - sections;

  333.   if (count > 0)
  334.     {
  335.       int space = resize_section_table (table, count);
  336.       int i;

  337.       for (i = 0; i < count; ++i)
  338.         {
  339.           table->sections[space + i] = sections[i];
  340.           table->sections[space + i].owner = owner;
  341.         }

  342.       /* If these are the first file sections we can provide memory
  343.          from, push the file_stratum target.  */
  344.       if (!target_is_pushed (&exec_ops))
  345.         push_target (&exec_ops);
  346.     }
  347. }

  348. /* Add the sections of OBJFILE to the current set of target sections.  */

  349. void
  350. add_target_sections_of_objfile (struct objfile *objfile)
  351. {
  352.   struct target_section_table *table = current_target_sections;
  353.   struct obj_section *osect;
  354.   int space;
  355.   unsigned count = 0;
  356.   struct target_section *ts;

  357.   if (objfile == NULL)
  358.     return;

  359.   /* Compute the number of sections to add.  */
  360.   ALL_OBJFILE_OSECTIONS (objfile, osect)
  361.     {
  362.       if (bfd_get_section_size (osect->the_bfd_section) == 0)
  363.         continue;
  364.       count++;
  365.     }

  366.   if (count == 0)
  367.     return;

  368.   space = resize_section_table (table, count);

  369.   ts = table->sections + space;

  370.   ALL_OBJFILE_OSECTIONS (objfile, osect)
  371.     {
  372.       if (bfd_get_section_size (osect->the_bfd_section) == 0)
  373.         continue;

  374.       gdb_assert (ts < table->sections + space + count);

  375.       ts->addr = obj_section_addr (osect);
  376.       ts->endaddr = obj_section_endaddr (osect);
  377.       ts->the_bfd_section = osect->the_bfd_section;
  378.       ts->owner = (void *) objfile;

  379.       ts++;
  380.     }
  381. }

  382. /* Remove all target sections owned by OWNER.
  383.    OWNER must be the same value passed to add_target_sections.  */

  384. void
  385. remove_target_sections (void *owner)
  386. {
  387.   struct target_section *src, *dest;
  388.   struct target_section_table *table = current_target_sections;

  389.   gdb_assert (owner != NULL);

  390.   dest = table->sections;
  391.   for (src = table->sections; src < table->sections_end; src++)
  392.     if (src->owner != owner)
  393.       {
  394.         /* Keep this section.  */
  395.         if (dest < src)
  396.           *dest = *src;
  397.         dest++;
  398.       }

  399.   /* If we've dropped any sections, resize the section table.  */
  400.   if (dest < src)
  401.     {
  402.       int old_count;

  403.       old_count = resize_section_table (table, dest - src);

  404.       /* If we don't have any more sections to read memory from,
  405.          remove the file_stratum target from the stack.  */
  406.       if (old_count + (dest - src) == 0)
  407.         {
  408.           struct program_space *pspace;

  409.           ALL_PSPACES (pspace)
  410.             if (pspace->target_sections.sections
  411.                 != pspace->target_sections.sections_end)
  412.               return;

  413.           unpush_target (&exec_ops);
  414.         }
  415.     }
  416. }



  417. enum target_xfer_status
  418. exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
  419.                              ULONGEST len, ULONGEST *xfered_len)
  420. {
  421.   /* It's unduly pedantic to refuse to look at the executable for
  422.      read-only pieces; so do the equivalent of readonly regions aka
  423.      QTro packet.  */
  424.   if (exec_bfd != NULL)
  425.     {
  426.       asection *s;
  427.       bfd_size_type size;
  428.       bfd_vma vma;

  429.       for (s = exec_bfd->sections; s; s = s->next)
  430.         {
  431.           if ((s->flags & SEC_LOAD) == 0
  432.               || (s->flags & SEC_READONLY) == 0)
  433.             continue;

  434.           vma = s->vma;
  435.           size = bfd_get_section_size (s);
  436.           if (vma <= offset && offset < (vma + size))
  437.             {
  438.               ULONGEST amt;

  439.               amt = (vma + size) - offset;
  440.               if (amt > len)
  441.                 amt = len;

  442.               amt = bfd_get_section_contents (exec_bfd, s,
  443.                                               readbuf, offset - vma, amt);

  444.               if (amt == 0)
  445.                 return TARGET_XFER_EOF;
  446.               else
  447.                 {
  448.                   *xfered_len = amt;
  449.                   return TARGET_XFER_OK;
  450.                 }
  451.             }
  452.         }
  453.     }

  454.   /* Indicate failure to find the requested memory block.  */
  455.   return TARGET_XFER_E_IO;
  456. }

  457. /* Appends all read-only memory ranges found in the target section
  458.    table defined by SECTIONS and SECTIONS_END, starting at (and
  459.    intersected with) MEMADDR for LEN bytes.  Returns the augmented
  460.    VEC.  */

  461. static VEC(mem_range_s) *
  462. section_table_available_memory (VEC(mem_range_s) *memory,
  463.                                 CORE_ADDR memaddr, ULONGEST len,
  464.                                 struct target_section *sections,
  465.                                 struct target_section *sections_end)
  466. {
  467.   struct target_section *p;

  468.   for (p = sections; p < sections_end; p++)
  469.     {
  470.       if ((bfd_get_section_flags (p->the_bfd_section->owner,
  471.                                   p->the_bfd_section)
  472.            & SEC_READONLY) == 0)
  473.         continue;

  474.       /* Copy the meta-data, adjusted.  */
  475.       if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
  476.         {
  477.           ULONGEST lo1, hi1, lo2, hi2;
  478.           struct mem_range *r;

  479.           lo1 = memaddr;
  480.           hi1 = memaddr + len;

  481.           lo2 = p->addr;
  482.           hi2 = p->endaddr;

  483.           r = VEC_safe_push (mem_range_s, memory, NULL);

  484.           r->start = max (lo1, lo2);
  485.           r->length = min (hi1, hi2) - r->start;
  486.         }
  487.     }

  488.   return memory;
  489. }

  490. enum target_xfer_status
  491. section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
  492.                                      ULONGEST len, ULONGEST *xfered_len)
  493. {
  494.   VEC(mem_range_s) *available_memory = NULL;
  495.   struct target_section_table *table;
  496.   struct cleanup *old_chain;
  497.   mem_range_s *r;
  498.   int i;

  499.   table = target_get_section_table (&exec_ops);
  500.   available_memory = section_table_available_memory (available_memory,
  501.                                                      offset, len,
  502.                                                      table->sections,
  503.                                                      table->sections_end);

  504.   old_chain = make_cleanup (VEC_cleanup(mem_range_s),
  505.                             &available_memory);

  506.   normalize_mem_ranges (available_memory);

  507.   for (i = 0;
  508.        VEC_iterate (mem_range_s, available_memory, i, r);
  509.        i++)
  510.     {
  511.       if (mem_ranges_overlap (r->start, r->length, offset, len))
  512.         {
  513.           CORE_ADDR end;
  514.           enum target_xfer_status status;

  515.           /* Get the intersection window.  */
  516.           end = min (offset + len, r->start + r->length);

  517.           gdb_assert (end - offset <= len);

  518.           if (offset >= r->start)
  519.             status = exec_read_partial_read_only (readbuf, offset,
  520.                                                   end - offset,
  521.                                                   xfered_len);
  522.           else
  523.             {
  524.               *xfered_len = r->start - offset;
  525.               status = TARGET_XFER_UNAVAILABLE;
  526.             }
  527.           do_cleanups (old_chain);
  528.           return status;
  529.         }
  530.     }
  531.   do_cleanups (old_chain);

  532.   *xfered_len = len;
  533.   return TARGET_XFER_UNAVAILABLE;
  534. }

  535. enum target_xfer_status
  536. section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
  537.                                    ULONGEST offset, ULONGEST len,
  538.                                    ULONGEST *xfered_len,
  539.                                    struct target_section *sections,
  540.                                    struct target_section *sections_end,
  541.                                    const char *section_name)
  542. {
  543.   int res;
  544.   struct target_section *p;
  545.   ULONGEST memaddr = offset;
  546.   ULONGEST memend = memaddr + len;

  547.   if (len == 0)
  548.     internal_error (__FILE__, __LINE__,
  549.                     _("failed internal consistency check"));

  550.   for (p = sections; p < sections_end; p++)
  551.     {
  552.       struct bfd_section *asect = p->the_bfd_section;
  553.       bfd *abfd = asect->owner;

  554.       if (section_name && strcmp (section_name, asect->name) != 0)
  555.         continue;                /* not the section we need.  */
  556.       if (memaddr >= p->addr)
  557.         {
  558.           if (memend <= p->endaddr)
  559.             {
  560.               /* Entire transfer is within this section.  */
  561.               if (writebuf)
  562.                 res = bfd_set_section_contents (abfd, asect,
  563.                                                 writebuf, memaddr - p->addr,
  564.                                                 len);
  565.               else
  566.                 res = bfd_get_section_contents (abfd, asect,
  567.                                                 readbuf, memaddr - p->addr,
  568.                                                 len);

  569.               if (res != 0)
  570.                 {
  571.                   *xfered_len = len;
  572.                   return TARGET_XFER_OK;
  573.                 }
  574.               else
  575.                 return TARGET_XFER_EOF;
  576.             }
  577.           else if (memaddr >= p->endaddr)
  578.             {
  579.               /* This section ends before the transfer starts.  */
  580.               continue;
  581.             }
  582.           else
  583.             {
  584.               /* This section overlaps the transfer.  Just do half.  */
  585.               len = p->endaddr - memaddr;
  586.               if (writebuf)
  587.                 res = bfd_set_section_contents (abfd, asect,
  588.                                                 writebuf, memaddr - p->addr,
  589.                                                 len);
  590.               else
  591.                 res = bfd_get_section_contents (abfd, asect,
  592.                                                 readbuf, memaddr - p->addr,
  593.                                                 len);
  594.               if (res != 0)
  595.                 {
  596.                   *xfered_len = len;
  597.                   return TARGET_XFER_OK;
  598.                 }
  599.               else
  600.                 return TARGET_XFER_EOF;
  601.             }
  602.         }
  603.     }

  604.   return TARGET_XFER_EOF;                /* We can't help.  */
  605. }

  606. static struct target_section_table *
  607. exec_get_section_table (struct target_ops *ops)
  608. {
  609.   return current_target_sections;
  610. }

  611. static enum target_xfer_status
  612. exec_xfer_partial (struct target_ops *ops, enum target_object object,
  613.                    const char *annex, gdb_byte *readbuf,
  614.                    const gdb_byte *writebuf,
  615.                    ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
  616. {
  617.   struct target_section_table *table = target_get_section_table (ops);

  618.   if (object == TARGET_OBJECT_MEMORY)
  619.     return section_table_xfer_memory_partial (readbuf, writebuf,
  620.                                               offset, len, xfered_len,
  621.                                               table->sections,
  622.                                               table->sections_end,
  623.                                               NULL);
  624.   else
  625.     return TARGET_XFER_E_IO;
  626. }


  627. void
  628. print_section_info (struct target_section_table *t, bfd *abfd)
  629. {
  630.   struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
  631.   struct target_section *p;
  632.   /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64.  */
  633.   int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;

  634.   printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
  635.   wrap_here ("        ");
  636.   printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
  637.   if (abfd == exec_bfd)
  638.     {
  639.       /* gcc-3.4 does not like the initialization in
  640.          <p == t->sections_end>.  */
  641.       bfd_vma displacement = 0;
  642.       bfd_vma entry_point;

  643.       for (p = t->sections; p < t->sections_end; p++)
  644.         {
  645.           struct bfd_section *psect = p->the_bfd_section;
  646.           bfd *pbfd = psect->owner;

  647.           if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
  648.               != (SEC_ALLOC | SEC_LOAD))
  649.             continue;

  650.           if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address
  651.               && abfd->start_address < (bfd_get_section_vma (pbfd, psect)
  652.                                         + bfd_get_section_size (psect)))
  653.             {
  654.               displacement = p->addr - bfd_get_section_vma (pbfd, psect);
  655.               break;
  656.             }
  657.         }
  658.       if (p == t->sections_end)
  659.         warning (_("Cannot find section for the entry point of %s."),
  660.                  bfd_get_filename (abfd));

  661.       entry_point = gdbarch_addr_bits_remove (gdbarch,
  662.                                               bfd_get_start_address (abfd)
  663.                                                 + displacement);
  664.       printf_filtered (_("\tEntry point: %s\n"),
  665.                        paddress (gdbarch, entry_point));
  666.     }
  667.   for (p = t->sections; p < t->sections_end; p++)
  668.     {
  669.       struct bfd_section *psect = p->the_bfd_section;
  670.       bfd *pbfd = psect->owner;

  671.       printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
  672.       printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));

  673.       /* FIXME: A format of "08l" is not wide enough for file offsets
  674.          larger than 4GB.  OTOH, making it "016l" isn't desirable either
  675.          since most output will then be much wider than necessary.  It
  676.          may make sense to test the size of the file and choose the
  677.          format string accordingly.  */
  678.       /* FIXME: i18n: Need to rewrite this sentence.  */
  679.       if (info_verbose)
  680.         printf_filtered (" @ %s",
  681.                          hex_string_custom (psect->filepos, 8));
  682.       printf_filtered (" is %s", bfd_section_name (pbfd, psect));
  683.       if (pbfd != abfd)
  684.         printf_filtered (" in %s", bfd_get_filename (pbfd));
  685.       printf_filtered ("\n");
  686.     }
  687. }

  688. static void
  689. exec_files_info (struct target_ops *t)
  690. {
  691.   if (exec_bfd)
  692.     print_section_info (current_target_sections, exec_bfd);
  693.   else
  694.     puts_filtered (_("\t<no file loaded>\n"));
  695. }

  696. static void
  697. set_section_command (char *args, int from_tty)
  698. {
  699.   struct target_section *p;
  700.   char *secname;
  701.   unsigned seclen;
  702.   unsigned long secaddr;
  703.   char secprint[100];
  704.   long offset;
  705.   struct target_section_table *table;

  706.   if (args == 0)
  707.     error (_("Must specify section name and its virtual address"));

  708.   /* Parse out section name.  */
  709.   for (secname = args; !isspace (*args); args++);
  710.   seclen = args - secname;

  711.   /* Parse out new virtual address.  */
  712.   secaddr = parse_and_eval_address (args);

  713.   table = current_target_sections;
  714.   for (p = table->sections; p < table->sections_end; p++)
  715.     {
  716.       if (!strncmp (secname, bfd_section_name (p->bfd,
  717.                                                p->the_bfd_section), seclen)
  718.           && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
  719.         {
  720.           offset = secaddr - p->addr;
  721.           p->addr += offset;
  722.           p->endaddr += offset;
  723.           if (from_tty)
  724.             exec_files_info (&exec_ops);
  725.           return;
  726.         }
  727.     }
  728.   if (seclen >= sizeof (secprint))
  729.     seclen = sizeof (secprint) - 1;
  730.   strncpy (secprint, secname, seclen);
  731.   secprint[seclen] = '\0';
  732.   error (_("Section %s not found"), secprint);
  733. }

  734. /* If we can find a section in FILENAME with BFD index INDEX, adjust
  735.    it to ADDRESS.  */

  736. void
  737. exec_set_section_address (const char *filename, int index, CORE_ADDR address)
  738. {
  739.   struct target_section *p;
  740.   struct target_section_table *table;

  741.   table = current_target_sections;
  742.   for (p = table->sections; p < table->sections_end; p++)
  743.     {
  744.       if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
  745.           && index == p->the_bfd_section->index)
  746.         {
  747.           p->endaddr += address - p->addr;
  748.           p->addr = address;
  749.         }
  750.     }
  751. }

  752. /* If mourn is being called in all the right places, this could be say
  753.    `gdb internal error' (since generic_mourn calls
  754.    breakpoint_init_inferior).  */

  755. static int
  756. ignore (struct target_ops *ops, struct gdbarch *gdbarch,
  757.         struct bp_target_info *bp_tgt)
  758. {
  759.   return 0;
  760. }

  761. static int
  762. exec_has_memory (struct target_ops *ops)
  763. {
  764.   /* We can provide memory if we have any file/target sections to read
  765.      from.  */
  766.   return (current_target_sections->sections
  767.           != current_target_sections->sections_end);
  768. }

  769. static char *
  770. exec_make_note_section (struct target_ops *self, bfd *obfd, int *note_size)
  771. {
  772.   error (_("Can't create a corefile"));
  773. }

  774. /* Fill in the exec file target vector.  Very few entries need to be
  775.    defined.  */

  776. static void
  777. init_exec_ops (void)
  778. {
  779.   exec_ops.to_shortname = "exec";
  780.   exec_ops.to_longname = "Local exec file";
  781.   exec_ops.to_doc = "Use an executable file as a target.\n\
  782. Specify the filename of the executable file.";
  783.   exec_ops.to_open = exec_open;
  784.   exec_ops.to_close = exec_close_1;
  785.   exec_ops.to_xfer_partial = exec_xfer_partial;
  786.   exec_ops.to_get_section_table = exec_get_section_table;
  787.   exec_ops.to_files_info = exec_files_info;
  788.   exec_ops.to_insert_breakpoint = ignore;
  789.   exec_ops.to_remove_breakpoint = ignore;
  790.   exec_ops.to_stratum = file_stratum;
  791.   exec_ops.to_has_memory = exec_has_memory;
  792.   exec_ops.to_make_corefile_notes = exec_make_note_section;
  793.   exec_ops.to_find_memory_regions = objfile_find_memory_regions;
  794.   exec_ops.to_magic = OPS_MAGIC;
  795. }

  796. void
  797. _initialize_exec (void)
  798. {
  799.   struct cmd_list_element *c;

  800.   init_exec_ops ();

  801.   if (!dbx_commands)
  802.     {
  803.       c = add_cmd ("file", class_files, file_command, _("\
  804. Use FILE as program to be debugged.\n\
  805. It is read for its symbols, for getting the contents of pure memory,\n\
  806. and it is the program executed when you use the `run' command.\n\
  807. If FILE cannot be found as specified, your execution directory path\n\
  808. ($PATH) is searched for a command of that name.\n\
  809. No arg means to have no executable file and no symbols."), &cmdlist);
  810.       set_cmd_completer (c, filename_completer);
  811.     }

  812.   c = add_cmd ("exec-file", class_files, exec_file_command, _("\
  813. Use FILE as program for getting contents of pure memory.\n\
  814. If FILE cannot be found as specified, your execution directory path\n\
  815. is searched for a command of that name.\n\
  816. No arg means have no executable file."), &cmdlist);
  817.   set_cmd_completer (c, filename_completer);

  818.   add_com ("section", class_files, set_section_command, _("\
  819. Change the base address of section SECTION of the exec file to ADDR.\n\
  820. This can be used if the exec file does not contain section addresses,\n\
  821. (such as in the a.out format), or when the addresses specified in the\n\
  822. file itself are wrong.  Each section must be changed separately.  The\n\
  823. ``info files'' command lists all the sections and their addresses."));

  824.   add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
  825. Set writing into executable and core files."), _("\
  826. Show writing into executable and core files."), NULL,
  827.                            NULL,
  828.                            show_write_files,
  829.                            &setlist, &showlist);

  830.   add_target_with_completer (&exec_ops, filename_completer);
  831. }