home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  12.9 KB  |  469 lines

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