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 / xcoffexec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-29  |  16.1 KB  |  589 lines

  1. /* Execute AIXcoff files, for GDB.
  2.    Copyright 1988, 1989, 1991, 1992, 1994 Free Software Foundation, Inc.
  3.    Derived from exec.c.  Modified by IBM Corporation.
  4.    Donated by IBM Corporation and Cygnus Support.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* xcoff-exec -    deal with executing XCOFF files.  */
  23.   
  24. #include "defs.h"
  25.  
  26. #include <sys/types.h>
  27. #include <sys/param.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <sys/stat.h>
  32.  
  33. #include "frame.h"
  34. #include "inferior.h"
  35. #include "target.h"
  36. #include "gdbcmd.h"
  37. #include "gdbcore.h"
  38. #include "language.h"
  39. #include "symfile.h"
  40. #include "objfiles.h"
  41.  
  42. #include "bfd.h"
  43. #include "xcoffsolib.h"
  44.  
  45. /* Prototypes for local functions */
  46.  
  47. static void
  48. file_command PARAMS ((char *, int));
  49.  
  50. static void
  51. exec_close PARAMS ((int));
  52.  
  53. struct vmap *
  54. map_vmap PARAMS ((bfd *, bfd *));
  55.  
  56. struct section_table *exec_sections, *exec_sections_end;
  57.  
  58. /* Whether to open exec and core files read-only or read-write.  */
  59.  
  60. int write_files = 0;
  61.  
  62. extern int info_verbose;
  63.  
  64. bfd *exec_bfd;            /* needed by core.c    */
  65.  
  66. extern char *getenv();
  67. extern void add_syms_addr_command ();
  68. extern void symbol_file_command ();
  69. static void exec_files_info();
  70.  
  71. struct vmap *vmap;        /* current vmap */
  72.  
  73. extern struct target_ops exec_ops;
  74.  
  75. /* exec_close -    done with exec file, clean up all resources. */
  76.  
  77. static void
  78. exec_close (quitting)
  79. int quitting;
  80. {
  81.   register struct vmap *vp, *nxt;
  82.   int need_symtab_cleanup = 0;
  83.   
  84.   for (nxt = vmap; nxt; )
  85.     {
  86.       vp = nxt;
  87.       nxt = vp->nxt;
  88.  
  89.       /* if there is an objfile associated with this bfd,
  90.      free_objfile() will do proper cleanup of objfile *and* bfd. */
  91.            
  92.       if (vp->objfile)
  93.     {
  94.       free_objfile (vp->objfile);
  95.       need_symtab_cleanup = 1;
  96.     }
  97.       else
  98.     bfd_close (vp->bfd);
  99.  
  100.       /* FIXME: This routine is #if 0'd in symfile.c.  What should we
  101.      be doing here?  Should we just free everything in
  102.      vp->objfile->symtabs?  Should free_objfile do that?  */
  103.       free_named_symtabs (vp->name);
  104.       free (vp);
  105.     }
  106.   
  107.   vmap = 0;
  108.  
  109.   /* exec_bfd was already closed (the exec file has a vmap entry).  */
  110.   exec_bfd = NULL;
  111.  
  112.   if (exec_ops.to_sections)
  113.     {
  114.       free (exec_ops.to_sections);
  115.       exec_ops.to_sections = NULL;
  116.       exec_ops.to_sections_end = NULL;
  117.     }
  118.  
  119.   if (need_symtab_cleanup)
  120.     clear_symtab_users ();
  121. }
  122.  
  123. /*  Process the first arg in ARGS as the new exec file.
  124.  
  125.     Note that we have to explicitly ignore additional args, since we can
  126.     be called from file_command(), which also calls symbol_file_command()
  127.     which can take multiple args. */
  128.  
  129. void
  130. exec_file_command (filename, from_tty)
  131.      char *filename;
  132.      int from_tty;
  133. {
  134.   target_preopen (from_tty);
  135.  
  136.   /* Remove any previous exec file.  */
  137.   unpush_target (&exec_ops);
  138.  
  139.   /* Now open and digest the file the user requested, if any. */
  140.  
  141.   if (filename)
  142.     {
  143.       char *scratch_pathname;
  144.       int scratch_chan;
  145.       
  146.       filename = tilde_expand (filename);
  147.       make_cleanup (free, filename);
  148.       
  149.       scratch_chan = openp (getenv ("PATH"), 1, filename,
  150.                 write_files? O_RDWR: O_RDONLY, 0,
  151.                 &scratch_pathname);
  152.       if (scratch_chan < 0)
  153.     perror_with_name (filename);
  154.  
  155.       exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
  156.       if (!exec_bfd)
  157.     error ("Could not open `%s' as an executable file: %s",
  158.            scratch_pathname, bfd_errmsg(bfd_get_error ()));
  159.  
  160.       /* make sure we have an object file */
  161.  
  162.       if (!bfd_check_format (exec_bfd, bfd_object))
  163.     error ("\"%s\": not in executable format: %s.", scratch_pathname,
  164.            bfd_errmsg (bfd_get_error ()));
  165.  
  166.       /* setup initial vmap */
  167.  
  168.       map_vmap (exec_bfd, 0);
  169.       if (!vmap)
  170.     error ("Can't find the file sections in `%s': %s", exec_bfd->filename,
  171.            bfd_errmsg(bfd_get_error ()));
  172.  
  173.       if (build_section_table (exec_bfd, &exec_ops.to_sections,
  174.                    &exec_ops.to_sections_end))
  175.     error ("Can't find the file sections in `%s': %s", exec_bfd->filename,
  176.            bfd_errmsg (bfd_get_error ()));
  177.  
  178.       /* make sure core, if present, matches */
  179.       validate_files ();
  180.  
  181.       push_target(&exec_ops);
  182.  
  183.       /* Tell display code (if any) about the changed file name. */
  184.       if (exec_file_display_hook)
  185.     (*exec_file_display_hook) (filename);
  186.     } 
  187.   else
  188.     {
  189.       exec_close (0);        /* just in case    */
  190.       if (from_tty)
  191.     printf_unfiltered ("No exec file now.\n");
  192.     }
  193. }
  194.  
  195. /* Set both the exec file and the symbol file, in one command.  What a
  196.    novelty.  Why did GDB go through four major releases before this
  197.    command was added?  */
  198.  
  199. static void
  200. file_command (arg, from_tty)
  201.      char *arg;
  202.      int from_tty;
  203. {
  204.   /* FIXME, if we lose on reading the symbol file, we should revert
  205.      the exec file, but that's rough.  */
  206.   exec_file_command (arg, from_tty);
  207.   symbol_file_command (arg, from_tty);
  208. }
  209.  
  210. /* Locate all mappable sections of a BFD file. 
  211.    table_pp_char is a char * to get it through bfd_map_over_sections;
  212.    we cast it back to its proper type.  */
  213.  
  214. static void
  215. add_to_section_table (abfd, asect, table_pp_char)
  216.      bfd *abfd;
  217.      sec_ptr asect;
  218.      char *table_pp_char;
  219. {
  220.   struct section_table **table_pp = (struct section_table **)table_pp_char;
  221.   flagword aflag;
  222.  
  223.   aflag = bfd_get_section_flags (abfd, asect);
  224.   /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
  225.   if (!(aflag & SEC_LOAD))
  226.     return;
  227.   if (0 == bfd_section_size (abfd, asect))
  228.     return;
  229.   (*table_pp)->bfd = abfd;
  230.   (*table_pp)->the_bfd_section = asect;
  231.   (*table_pp)->addr = bfd_section_vma (abfd, asect);
  232.   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
  233.   (*table_pp)++;
  234. }
  235.  
  236. int
  237. build_section_table (some_bfd, start, end)
  238.      bfd *some_bfd;
  239.      struct section_table **start, **end;
  240. {
  241.   unsigned count;
  242.  
  243.   count = bfd_count_sections (some_bfd);
  244.   if (count == 0)
  245.     fatal ("aborting");    /* return 1? */
  246.   if (*start)
  247.     free (*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.     fatal ("aborting");
  253.   /* We could realloc the table, but it probably loses for most files.  */
  254.   return 0;
  255. }
  256.  
  257. static void
  258. bfdsec_to_vmap(bf, sect, arg3) 
  259.      bfd *bf;
  260.      sec_ptr sect;
  261.      PTR arg3;
  262. {
  263.   struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
  264.   register struct vmap *vp;
  265.   vp = vmap_bfd->pvmap;
  266.  
  267.   if ((bfd_get_section_flags (bf, sect) & SEC_LOAD) == 0)
  268.     return;
  269.  
  270.   if (STREQ(bfd_section_name (bf, sect), ".text"))
  271.     {
  272.       vp->tstart = 0;
  273.       vp->tend = vp->tstart + bfd_section_size (bf, sect);
  274.  
  275.       /* When it comes to this adjustment value, in contrast to our previous
  276.      belief shared objects should behave the same as the main load segment.
  277.      This is the offset from the beginning of text section to the first
  278.      real instruction. */
  279.  
  280.       vp->tadj = sect->filepos - bfd_section_vma (bf, sect);
  281.     }
  282.   else if (STREQ(bfd_section_name (bf, sect), ".data"))
  283.     {
  284.       vp->dstart = 0;
  285.       vp->dend   = vp->dstart + bfd_section_size (bf, sect);
  286.     }
  287.   else if (STREQ(bfd_section_name(bf, sect), ".bss"))    /* FIXMEmgo */
  288.     printf_unfiltered ("bss section in exec! Don't know what the heck to do!\n");
  289. }
  290.  
  291. /* Make a vmap for the BFD "bf", which might be a member of the archive
  292.    BFD "arch".  Return the new vmap.  */
  293.  
  294. struct vmap *
  295. map_vmap (bf, arch)
  296.      bfd *bf;
  297.      bfd *arch;
  298. {
  299.   struct vmap_and_bfd vmap_bfd;
  300.   struct vmap *vp, **vpp;
  301.  
  302.   vp = (PTR) xmalloc (sizeof (*vp));
  303.   memset (vp, '\0', sizeof (*vp));
  304.   vp->nxt = 0;
  305.   vp->bfd = bf;
  306.   vp->name = bfd_get_filename (arch ? arch : bf);
  307.   vp->member = arch ? bfd_get_filename (bf) : "";
  308.   
  309.   vmap_bfd.pbfd = arch;
  310.   vmap_bfd.pvmap = vp;
  311.   bfd_map_over_sections (bf, bfdsec_to_vmap, &vmap_bfd);
  312.  
  313.   /* find the end of the list, and append. */
  314.   for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
  315.   ;
  316.   *vpp = vp;
  317.  
  318.   return vp;
  319. }
  320.  
  321. /* Read or write the exec file.
  322.  
  323.    Args are address within exec file, address within gdb address-space,
  324.    length, and a flag indicating whether to read or write.
  325.  
  326.    Result is a length:
  327.  
  328.     0:    We cannot handle this address and length.
  329.     > 0:  We have handled N bytes starting at this address.
  330.           (If N == length, we did it all.)  We might be able
  331.           to handle more bytes beyond this length, but no
  332.           promises.
  333.     < 0:  We cannot handle this address, but if somebody
  334.           else handles (-N) bytes, we can start from there.
  335.  
  336.     The same routine is used to handle both core and exec files;
  337.     we just tail-call it with more arguments to select between them.  */
  338.  
  339. int
  340. xfer_memory (memaddr, myaddr, len, write, target)
  341.      CORE_ADDR memaddr;
  342.      char *myaddr;
  343.      int len;
  344.      int write;
  345.      struct target_ops *target;
  346. {
  347.   boolean res;
  348.   struct section_table *p;
  349.   CORE_ADDR nextsectaddr, memend;
  350.   boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
  351.  
  352.   if (len <= 0)
  353.     fatal ("aborting");
  354.  
  355.   memend = memaddr + len;
  356.   xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
  357.   nextsectaddr = memend;
  358.  
  359.   for (p = target->to_sections; p < target->to_sections_end; p++)
  360.     {
  361.       if (p->addr <= memaddr)
  362.     if (p->endaddr >= memend)
  363.       {
  364.         /* Entire transfer is within this section.  */
  365.         res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
  366.         return (res != 0) ? len : 0;
  367.       }
  368.     else if (p->endaddr <= memaddr)
  369.       {
  370.         /* This section ends before the transfer starts.  */
  371.         continue;
  372.       }
  373.     else 
  374.       {
  375.         /* This section overlaps the transfer.  Just do half.  */
  376.         len = p->endaddr - memaddr;
  377.         res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
  378.         return (res != 0) ? len : 0;
  379.       }
  380.       else if (p->addr < nextsectaddr)
  381.     nextsectaddr = p->addr;
  382.     }
  383.  
  384.   if (nextsectaddr >= memend)
  385.     return 0;                /* We can't help */
  386.   else
  387.     return - (nextsectaddr - memaddr);    /* Next boundary where we can help */
  388. }
  389.  
  390. void
  391. print_section_info (t, abfd)
  392.   struct target_ops *t;
  393.   bfd *abfd;
  394. {
  395.   struct section_table *p;
  396.  
  397.   /* FIXME-32x64: Need a version of print_address_numeric with field width.  */
  398.   printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
  399.   wrap_here ("        ");
  400.   printf_filtered ("file type %s.\n", bfd_get_target(abfd));
  401.  
  402.   for (p = t->to_sections; p < t->to_sections_end; p++) {
  403.     printf_filtered ("\t%s",
  404.              local_hex_string_custom ((unsigned long) p->addr, "08l"));
  405.     printf_filtered (" - %s",
  406.              local_hex_string_custom ((unsigned long) p->endaddr, "08l"));
  407.     if (info_verbose)
  408.       printf_filtered (" @ %s",
  409.                local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l"));
  410.     printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
  411.     if (p->bfd != abfd) {
  412.       printf_filtered (" in %s", bfd_get_filename (p->bfd));
  413.     }
  414.     printf_filtered ("\n");
  415.   }
  416. }
  417.  
  418.  
  419. static void
  420. exec_files_info (t)
  421.      struct target_ops *t;
  422. {
  423.   register struct vmap *vp = vmap;
  424.  
  425.   print_section_info (t, exec_bfd);
  426.  
  427.   if (!vp)
  428.     return;
  429.  
  430.   printf_unfiltered ("\tMapping info for file `%s'.\n", vp->name);
  431.  
  432.   printf_unfiltered ("\t  %8.8s   %8.8s   %8.8s   %8.8s %8.8s %s\n",
  433.     "tstart", "tend", "dstart", "dend", "section", "file(member)");
  434.  
  435.   for (; vp; vp = vp->nxt)
  436.     printf_unfiltered ("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
  437.                vp->tstart,
  438.                vp->tend,
  439.                vp->dstart,
  440.                vp->dend,
  441.                vp->name,
  442.                *vp->member ? "(" : "",
  443.                vp->member,
  444.                *vp->member ? ")" : "");
  445. }
  446.  
  447. #ifdef DAMON
  448. /*  Damon's implementation of set_section_command! It is based on the sex member
  449.   (which is a section pointer from vmap) of vmap.
  450.   We will not have multiple vmap entries (one for each section), rather transmit
  451.   text and data base offsets and fix them at the same time. Elimination of sex
  452.   entry in vmap make this function obsolute, use the one from exec.c. 
  453.   Need further testing!!    FIXMEmgo.  */
  454.  
  455. static void
  456. set_section_command(args, from_tty)
  457. char *args; 
  458. {
  459.     register struct vmap *vp = vmap;
  460.     char *secname;
  461.     unsigned seclen;
  462.     unsigned long secaddr;
  463.     char secprint[100];
  464.     long offset;
  465.  
  466.     if (args == 0)
  467.         error("Must specify section name and its virtual address");
  468.  
  469.     /* Parse out section name */
  470.     for (secname = args; !isspace(*args); args++)
  471.         ;
  472.     seclen = args - secname;
  473.  
  474.     /* Parse out new virtual address */
  475.     secaddr = parse_and_eval_address(args);
  476.  
  477.     for (vp = vmap; vp; vp = vp->nxt) {
  478.         if (!strncmp(secname
  479.                  , bfd_section_name(vp->bfd, vp->sex), seclen)
  480.             && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
  481.             offset = secaddr - vp->tstart;
  482.             vp->tstart += offset;
  483.             vp->tend   += offset;
  484.             exec_files_info();
  485.             return;
  486.         }
  487.     } 
  488.  
  489.     if (seclen >= sizeof(secprint))
  490.         seclen = sizeof(secprint) - 1;
  491.     strncpy(secprint, secname, seclen);
  492.     secprint[seclen] = '\0';
  493.     error("Section %s not found", secprint);
  494. }
  495. #else
  496. static void
  497. set_section_command (args, from_tty)
  498.      char *args;
  499.      int from_tty;
  500. {
  501.   struct section_table *p;
  502.   char *secname;
  503.   unsigned seclen;
  504.   unsigned long secaddr;
  505.   char secprint[100];
  506.   long offset;
  507.  
  508.   if (args == 0)
  509.     error ("Must specify section name and its virtual address");
  510.  
  511.   /* Parse out section name */
  512.   for (secname = args; !isspace(*args); args++) ;
  513.   seclen = args - secname;
  514.  
  515.   /* Parse out new virtual address */
  516.   secaddr = parse_and_eval_address (args);
  517.  
  518.   for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
  519.     if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
  520.     && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0')
  521.       {
  522.     offset = secaddr - p->addr;
  523.     p->addr += offset;
  524.     p->endaddr += offset;
  525.     if (from_tty)
  526.       exec_files_info (&exec_ops);
  527.     return;
  528.       }
  529.  
  530.   if (seclen >= sizeof (secprint))
  531.     seclen = sizeof (secprint) - 1;
  532.   strncpy (secprint, secname, seclen);
  533.   secprint[seclen] = '\0';
  534.   error ("Section %s not found", secprint);
  535. }
  536.  
  537. #endif /* !DAMON */
  538.  
  539. struct target_ops exec_ops = {
  540.     "exec", "Local exec file",
  541.     "Use an executable file as a target.\n\
  542. Specify the filename of the executable file.",
  543.     exec_file_command, exec_close, /* open, close */
  544.     find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
  545.     0, 0, /* fetch_registers, store_registers, */
  546.     0, /* prepare_to_store */
  547.     xfer_memory, exec_files_info,
  548.     0, 0, /* insert_breakpoint, remove_breakpoint, */
  549.     0, 0, 0, 0, 0, /* terminal stuff */
  550.     0, 0, /* kill, load */
  551.     0, /* lookup sym */
  552.     find_default_create_inferior,
  553.     0, /* mourn_inferior */
  554.     0, /* can_run */
  555.     0, /* notice_signals */
  556.     file_stratum, 0, /* next */
  557.     0, 1, 0, 0, 0,    /* all mem, mem, stack, regs, exec */
  558.     0, 0,            /* section pointers */
  559.     OPS_MAGIC,        /* Always the last thing */
  560. };
  561.  
  562. void
  563. _initialize_exec()
  564. {
  565.  
  566.   add_com("file", class_files, file_command,
  567.        "Use FILE as program to be debugged.\n\
  568. It is read for its symbols, for getting the contents of pure memory,\n\
  569. and it is the program executed when you use the `run' command.\n\
  570. If FILE cannot be found as specified, your execution directory path\n\
  571. ($PATH) is searched for a command of that name.\n\
  572. No arg means to have no executable file and no symbols.");
  573.  
  574.   add_com("exec-file", class_files, exec_file_command,
  575.        "Use FILE as program for getting contents of pure memory.\n\
  576. If FILE cannot be found as specified, your execution directory path\n\
  577. is searched for a command of that name.\n\
  578. No arg means have no executable file.");
  579.  
  580.   add_com("section", class_files, set_section_command,
  581.    "Change the base address of section SECTION of the exec file to ADDR.\n\
  582. This can be used if the exec file does not contain section addresses,\n\
  583. (such as in the a.out format), or when the addresses specified in the\n\
  584. file itself are wrong.  Each section must be changed separately.  The\n\
  585. ``info files'' command lists all the sections and their addresses.");
  586.  
  587.   add_target(&exec_ops);
  588. }
  589.