home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-04  |  14.0 KB  |  507 lines

  1. /* Work with executable files, for GDB. 
  2.    Copyright 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "frame.h"
  22. #include "inferior.h"
  23. #include "target.h"
  24. #include "gdbcmd.h"
  25. #include "language.h"
  26.  
  27. #ifdef USG
  28. #include <sys/types.h>
  29. #endif
  30.  
  31. #include <sys/param.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34.  
  35. #include "gdbcore.h"
  36.  
  37. #include <ctype.h>
  38. #include <sys/stat.h>
  39. #ifndef O_BINARY
  40. #define O_BINARY 0
  41. #endif
  42.  
  43. /* Prototypes for local functions */
  44.  
  45. static void
  46. add_to_section_table PARAMS ((bfd *, sec_ptr, PTR));
  47.  
  48. static void
  49. exec_close PARAMS ((int));
  50.  
  51. static void
  52. file_command PARAMS ((char *, int));
  53.  
  54. static void
  55. set_section_command PARAMS ((char *, int));
  56.  
  57. static void
  58. exec_files_info PARAMS ((struct target_ops *));
  59.  
  60. extern int info_verbose;
  61.  
  62. /* The Binary File Descriptor handle for the executable file.  */
  63.  
  64. bfd *exec_bfd = NULL;
  65.  
  66. /* Whether to open exec and core files read-only or read-write.  */
  67.  
  68. int write_files = 0;
  69.  
  70. /* Text start and end addresses (KLUDGE) if needed */
  71.  
  72. #ifdef NEED_TEXT_START_END
  73. CORE_ADDR text_start = 0;
  74. CORE_ADDR text_end   = 0;
  75. #endif
  76.  
  77. /* Forward decl */
  78.  
  79. extern struct target_ops exec_ops;
  80.  
  81. /* ARGSUSED */
  82. static void
  83. exec_close (quitting)
  84.      int quitting;
  85. {
  86.   if (exec_bfd) {
  87.     char *name = bfd_get_filename (exec_bfd);
  88.     bfd_close (exec_bfd);
  89.     free (name);
  90.     exec_bfd = NULL;
  91.   }
  92.   if (exec_ops.to_sections) {
  93.     free ((PTR)exec_ops.to_sections);
  94.     exec_ops.to_sections = NULL;
  95.     exec_ops.to_sections_end = NULL;
  96.   }
  97. }
  98.  
  99. /*  Process the first arg in ARGS as the new exec file.
  100.  
  101.     Note that we have to explicitly ignore additional args, since we can
  102.     be called from file_command(), which also calls symbol_file_command()
  103.     which can take multiple args. */
  104.  
  105. void
  106. exec_file_command (args, from_tty)
  107.      char *args;
  108.      int from_tty;
  109. {
  110.   char **argv;
  111.   char *filename;
  112.  
  113.   target_preopen (from_tty);
  114.  
  115.   /* Remove any previous exec file.  */
  116.   unpush_target (&exec_ops);
  117.  
  118.   /* Now open and digest the file the user requested, if any.  */
  119.  
  120.   if (args)
  121.     {
  122.       char *scratch_pathname;
  123.       int scratch_chan;
  124.       
  125.       /* Scan through the args and pick up the first non option arg
  126.      as the filename. */
  127.  
  128.       if ((argv = buildargv (args)) == NULL)
  129.     {
  130.       nomem (0);
  131.     }
  132.       make_cleanup (freeargv, (char *) argv);
  133.  
  134.       for (; (*argv != NULL) && (**argv == '-'); argv++) {;}
  135.       if (*argv == NULL)
  136.     {
  137.       error ("no exec file name was specified");
  138.     }
  139.  
  140.       filename = tilde_expand (*argv);
  141.       make_cleanup (free, filename);
  142.       
  143.       scratch_chan = openp (getenv ("PATH"), 1, filename, 
  144.                 write_files? O_RDWR|O_BINARY: O_RDONLY|O_BINARY, 0,
  145.                 &scratch_pathname);
  146.       if (scratch_chan < 0)
  147.     perror_with_name (filename);
  148.  
  149.       exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
  150.       if (!exec_bfd)
  151.     error ("Could not open `%s' as an executable file: %s",
  152.            scratch_pathname, bfd_errmsg (bfd_get_error ()));
  153.       if (!bfd_check_format (exec_bfd, bfd_object))
  154.     {
  155.       /* Make sure to close exec_bfd, or else "run" might try to use
  156.          it.  */
  157.       exec_close (0);
  158.       error ("\"%s\": not in executable format: %s.",
  159.          scratch_pathname, bfd_errmsg (bfd_get_error ()));
  160.     }
  161.  
  162.       if (build_section_table (exec_bfd, &exec_ops.to_sections,
  163.                 &exec_ops.to_sections_end))
  164.     {
  165.       /* Make sure to close exec_bfd, or else "run" might try to use
  166.          it.  */
  167.       exec_close (0);
  168.       error ("Can't find the file sections in `%s': %s", 
  169.          exec_bfd->filename, bfd_errmsg (bfd_get_error ()));
  170.     }
  171.  
  172. #ifdef NEED_TEXT_START_END
  173.  
  174.       /* text_end is sometimes used for where to put call dummies.  A
  175.      few ports use these for other purposes too.  */
  176.  
  177.       {
  178.     struct section_table *p;
  179.  
  180.     /* Set text_start to the lowest address of the start of any
  181.        readonly code section and set text_end to the highest
  182.        address of the end of any readonly code section.  */
  183.  
  184.     text_start = ~(CORE_ADDR)0;
  185.     text_end = (CORE_ADDR)0;
  186.     for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
  187.       if (bfd_get_section_flags (p->bfd, p->the_bfd_section)
  188.           & (SEC_CODE | SEC_READONLY))
  189.         {
  190.           if (text_start > p->addr) 
  191.         text_start = p->addr;
  192.           if (text_end < p->endaddr)
  193.         text_end = p->endaddr;
  194.         }
  195.       }
  196. #endif
  197.  
  198.       validate_files ();
  199.  
  200.       push_target (&exec_ops);
  201.  
  202.       /* Tell display code (if any) about the changed file name.  */
  203.       if (exec_file_display_hook)
  204.     (*exec_file_display_hook) (filename);
  205.     }
  206.   else if (from_tty)
  207.     printf_unfiltered ("No exec file now.\n");
  208. }
  209.  
  210. /* Set both the exec file and the symbol file, in one command.  
  211.    What a novelty.  Why did GDB go through four major releases before this
  212.    command was added?  */
  213.  
  214. static void
  215. file_command (arg, from_tty)
  216.      char *arg;
  217.      int from_tty;
  218. {
  219.   /* FIXME, if we lose on reading the symbol file, we should revert
  220.      the exec file, but that's rough.  */
  221.   exec_file_command (arg, from_tty);
  222.   symbol_file_command (arg, from_tty);
  223. }
  224.  
  225.  
  226. /* Locate all mappable sections of a BFD file. 
  227.    table_pp_char is a char * to get it through bfd_map_over_sections;
  228.    we cast it back to its proper type.  */
  229.  
  230. static void
  231. add_to_section_table (abfd, asect, table_pp_char)
  232.      bfd *abfd;
  233.      sec_ptr asect;
  234.      PTR table_pp_char;
  235. {
  236.   struct section_table **table_pp = (struct section_table **)table_pp_char;
  237.   flagword aflag;
  238.  
  239.   aflag = bfd_get_section_flags (abfd, asect);
  240.   if (!(aflag & SEC_ALLOC))
  241.     return;
  242.   if (0 == bfd_section_size (abfd, asect))
  243.     return;
  244.   (*table_pp)->bfd = abfd;
  245.   (*table_pp)->the_bfd_section = asect;
  246.   (*table_pp)->addr = bfd_section_vma (abfd, asect);
  247.   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
  248.   (*table_pp)++;
  249. }
  250.  
  251. /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
  252.    Returns 0 if OK, 1 on error.  */
  253.  
  254. int
  255. build_section_table (some_bfd, start, end)
  256.      bfd *some_bfd;
  257.      struct section_table **start, **end;
  258. {
  259.   unsigned count;
  260.  
  261.   count = bfd_count_sections (some_bfd);
  262.   if (*start)
  263.     free ((PTR)*start);
  264.   *start = (struct section_table *) xmalloc (count * sizeof (**start));
  265.   *end = *start;
  266.   bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
  267.   if (*end > *start + count)
  268.     abort();
  269.   /* We could realloc the table, but it probably loses for most files.  */
  270.   return 0;
  271. }
  272.  
  273. /* Read or write the exec file.
  274.  
  275.    Args are address within a BFD file, address within gdb address-space,
  276.    length, and a flag indicating whether to read or write.
  277.  
  278.    Result is a length:
  279.  
  280.     0:    We cannot handle this address and length.
  281.     > 0:  We have handled N bytes starting at this address.
  282.           (If N == length, we did it all.)  We might be able
  283.           to handle more bytes beyond this length, but no
  284.           promises.
  285.     < 0:  We cannot handle this address, but if somebody
  286.           else handles (-N) bytes, we can start from there.
  287.  
  288.     The same routine is used to handle both core and exec files;
  289.     we just tail-call it with more arguments to select between them.  */
  290.  
  291. int
  292. xfer_memory (memaddr, myaddr, len, write, target)
  293.      CORE_ADDR memaddr;
  294.      char *myaddr;
  295.      int len;
  296.      int write;
  297.      struct target_ops *target;
  298. {
  299.   boolean res;
  300.   struct section_table *p;
  301.   CORE_ADDR nextsectaddr, memend;
  302.   boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
  303.  
  304.   if (len <= 0)
  305.     abort();
  306.  
  307.   memend = memaddr + len;
  308.   xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
  309.   nextsectaddr = memend;
  310.  
  311.   for (p = target->to_sections; p < target->to_sections_end; p++)
  312.     {
  313.       if (p->addr <= memaddr)
  314.     if (p->endaddr >= memend)
  315.       {
  316.         /* Entire transfer is within this section.  */
  317.         res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
  318.         return (res != 0) ? len : 0;
  319.       }
  320.     else if (p->endaddr <= memaddr)
  321.       {
  322.         /* This section ends before the transfer starts.  */
  323.         continue;
  324.       }
  325.     else 
  326.       {
  327.         /* This section overlaps the transfer.  Just do half.  */
  328.         len = p->endaddr - memaddr;
  329.         res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
  330.         return (res != 0) ? len : 0;
  331.       }
  332.       else if (p->addr < nextsectaddr)
  333.     nextsectaddr = p->addr;
  334.     }
  335.  
  336.   if (nextsectaddr >= memend)
  337.     return 0;                /* We can't help */
  338.   else
  339.     return - (nextsectaddr - memaddr);    /* Next boundary where we can help */
  340. }
  341.  
  342. #ifdef FIXME
  343. #ifdef REG_STACK_SEGMENT
  344. /* MOVE TO BFD... */
  345.     /* Pyramids and AM29000s have an extra segment in the virtual address space
  346.        for the (control) stack of register-window frames.  The AM29000 folk
  347.        call it the "register stack" rather than the "memory stack".  */
  348.     else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
  349.       {
  350.     i = min (len, reg_stack_end - memaddr);
  351.     fileptr = memaddr - reg_stack_start + reg_stack_offset;
  352.     wanna_xfer = coredata;
  353.       }
  354. #endif                /* REG_STACK_SEGMENT */
  355. #endif /* FIXME */
  356.  
  357. void
  358. print_section_info (t, abfd)
  359.   struct target_ops *t;
  360.   bfd *abfd;
  361. {
  362.   struct section_table *p;
  363.  
  364.   printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
  365.   wrap_here ("        ");
  366.   printf_filtered ("file type %s.\n", bfd_get_target(abfd));
  367.   if (abfd == exec_bfd)
  368.     {
  369.       printf_filtered ("\tEntry point: ");
  370.       print_address_numeric (bfd_get_start_address (abfd), 1, gdb_stdout);
  371.       printf_filtered ("\n");
  372.     }
  373.   for (p = t->to_sections; p < t->to_sections_end; p++)
  374.     {
  375.       /* FIXME-32x64 need a print_address_numeric with field width */
  376.       printf_filtered ("\t%s", local_hex_string_custom ((unsigned long) p->addr, "08l"));
  377.       printf_filtered (" - %s", local_hex_string_custom ((unsigned long) p->endaddr, "08l"));
  378.       if (info_verbose)
  379.     printf_filtered (" @ %s",
  380.              local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l"));
  381.       printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
  382.       if (p->bfd != abfd)
  383.     {
  384.       printf_filtered (" in %s", bfd_get_filename (p->bfd));
  385.     }
  386.       printf_filtered ("\n");
  387.     }
  388. }
  389.  
  390. static void
  391. exec_files_info (t)
  392.   struct target_ops *t;
  393. {
  394.   print_section_info (t, exec_bfd);
  395. }
  396.  
  397. static void
  398. set_section_command (args, from_tty)
  399.      char *args;
  400.      int from_tty;
  401. {
  402.   struct section_table *p;
  403.   char *secname;
  404.   unsigned seclen;
  405.   unsigned long secaddr;
  406.   char secprint[100];
  407.   long offset;
  408.  
  409.   if (args == 0)
  410.     error ("Must specify section name and its virtual address");
  411.  
  412.   /* Parse out section name */
  413.   for (secname = args; !isspace(*args); args++) ;
  414.   seclen = args - secname;
  415.  
  416.   /* Parse out new virtual address */
  417.   secaddr = parse_and_eval_address (args);
  418.  
  419.   for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) {
  420.     if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
  421.     && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0') {
  422.       offset = secaddr - p->addr;
  423.       p->addr += offset;
  424.       p->endaddr += offset;
  425.       if (from_tty)
  426.     exec_files_info(&exec_ops);
  427.       return;
  428.     }
  429.   } 
  430.   if (seclen >= sizeof (secprint))
  431.     seclen = sizeof (secprint) - 1;
  432.   strncpy (secprint, secname, seclen);
  433.   secprint[seclen] = '\0';
  434.   error ("Section %s not found", secprint);
  435. }
  436.  
  437. /* If mourn is being called in all the right places, this could be say
  438.    `gdb internal error' (since generic_mourn calls breakpoint_init_inferior).  */
  439.  
  440. static int
  441. ignore (addr, contents)
  442.      CORE_ADDR addr;
  443.      char *contents;
  444. {
  445.   return 0;
  446. }
  447.  
  448. struct target_ops exec_ops = {
  449.     "exec", "Local exec file",
  450.     "Use an executable file as a target.\n\
  451. Specify the filename of the executable file.",
  452.     exec_file_command, exec_close, /* open, close */
  453.     find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
  454.     0, 0, /* fetch_registers, store_registers, */
  455.     0, /* prepare_to_store, */
  456.     xfer_memory, exec_files_info,
  457.     ignore, ignore, /* insert_breakpoint, remove_breakpoint, */
  458.     0, 0, 0, 0, 0, /* terminal stuff */
  459.     0, 0, /* kill, load */
  460.     0, /* lookup sym */
  461.     find_default_create_inferior,
  462.     0, /* mourn_inferior */
  463.     0, /* can_run */
  464.     0, /* notice_signals */
  465.     file_stratum, 0, /* next */
  466.     0, 1, 0, 0, 0,    /* all mem, mem, stack, regs, exec */
  467.     0, 0,            /* section pointers */
  468.     OPS_MAGIC,        /* Always the last thing */
  469. };
  470.  
  471. void
  472. _initialize_exec()
  473. {
  474.   struct cmd_list_element *c;
  475.  
  476.   c = add_cmd ("file", class_files, file_command,
  477.            "Use FILE as program to be debugged.\n\
  478. It is read for its symbols, for getting the contents of pure memory,\n\
  479. and it is the program executed when you use the `run' command.\n\
  480. If FILE cannot be found as specified, your execution directory path\n\
  481. ($PATH) is searched for a command of that name.\n\
  482. No arg means to have no executable file and no symbols.", &cmdlist);
  483.   c->completer = filename_completer;
  484.  
  485.   c = add_cmd ("exec-file", class_files, exec_file_command,
  486.        "Use FILE as program for getting contents of pure memory.\n\
  487. If FILE cannot be found as specified, your execution directory path\n\
  488. is searched for a command of that name.\n\
  489. No arg means have no executable file.", &cmdlist);
  490.   c->completer = filename_completer;
  491.  
  492.   add_com ("section", class_files, set_section_command,
  493.    "Change the base address of section SECTION of the exec file to ADDR.\n\
  494. This can be used if the exec file does not contain section addresses,\n\
  495. (such as in the a.out format), or when the addresses specified in the\n\
  496. file itself are wrong.  Each section must be changed separately.  The\n\
  497. ``info files'' command lists all the sections and their addresses.");
  498.  
  499.   add_show_from_set
  500.     (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files,
  501.           "Set writing into executable and core files.",
  502.           &setlist),
  503.      &showlist);
  504.   
  505.   add_target (&exec_ops);
  506. }
  507.