GDB (API)
/home/stan/gdb/src/gdb/exec.c
Go to the documentation of this file.
00001 /* Work with executable files, for GDB. 
00002 
00003    Copyright (C) 1988-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "frame.h"
00022 #include "inferior.h"
00023 #include "target.h"
00024 #include "gdbcmd.h"
00025 #include "language.h"
00026 #include "filenames.h"
00027 #include "symfile.h"
00028 #include "objfiles.h"
00029 #include "completer.h"
00030 #include "value.h"
00031 #include "exec.h"
00032 #include "observer.h"
00033 #include "arch-utils.h"
00034 #include "gdbthread.h"
00035 #include "progspace.h"
00036 #include "gdb_bfd.h"
00037 
00038 #include <fcntl.h>
00039 #include "readline/readline.h"
00040 #include "gdb_string.h"
00041 
00042 #include "gdbcore.h"
00043 
00044 #include <ctype.h>
00045 #include "gdb_stat.h"
00046 
00047 void (*deprecated_file_changed_hook) (char *);
00048 
00049 /* Prototypes for local functions */
00050 
00051 static void file_command (char *, int);
00052 
00053 static void set_section_command (char *, int);
00054 
00055 static void exec_files_info (struct target_ops *);
00056 
00057 static void init_exec_ops (void);
00058 
00059 void _initialize_exec (void);
00060 
00061 /* The target vector for executable files.  */
00062 
00063 struct target_ops exec_ops;
00064 
00065 /* True if the exec target is pushed on the stack.  */
00066 static int using_exec_ops;
00067 
00068 /* Whether to open exec and core files read-only or read-write.  */
00069 
00070 int write_files = 0;
00071 static void
00072 show_write_files (struct ui_file *file, int from_tty,
00073                   struct cmd_list_element *c, const char *value)
00074 {
00075   fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
00076                     value);
00077 }
00078 
00079 
00080 static void
00081 exec_open (char *args, int from_tty)
00082 {
00083   target_preopen (from_tty);
00084   exec_file_attach (args, from_tty);
00085 }
00086 
00087 /* Close and clear exec_bfd.  If we end up with no target sections to
00088    read memory from, this unpushes the exec_ops target.  */
00089 
00090 void
00091 exec_close (void)
00092 {
00093   if (exec_bfd)
00094     {
00095       bfd *abfd = exec_bfd;
00096 
00097       gdb_bfd_unref (abfd);
00098 
00099       /* Removing target sections may close the exec_ops target.
00100          Clear exec_bfd before doing so to prevent recursion.  */
00101       exec_bfd = NULL;
00102       exec_bfd_mtime = 0;
00103 
00104       remove_target_sections (&exec_bfd);
00105 
00106       xfree (exec_filename);
00107       exec_filename = NULL;
00108     }
00109 }
00110 
00111 /* This is the target_close implementation.  Clears all target
00112    sections and closes all executable bfds from all program spaces.  */
00113 
00114 static void
00115 exec_close_1 (void)
00116 {
00117   using_exec_ops = 0;
00118 
00119   {
00120     struct program_space *ss;
00121     struct cleanup *old_chain;
00122 
00123     old_chain = save_current_program_space ();
00124     ALL_PSPACES (ss)
00125     {
00126       set_current_program_space (ss);
00127 
00128       /* Delete all target sections.  */
00129       resize_section_table
00130         (current_target_sections,
00131          -resize_section_table (current_target_sections, 0));
00132 
00133       exec_close ();
00134     }
00135 
00136     do_cleanups (old_chain);
00137   }
00138 }
00139 
00140 void
00141 exec_file_clear (int from_tty)
00142 {
00143   /* Remove exec file.  */
00144   exec_close ();
00145 
00146   if (from_tty)
00147     printf_unfiltered (_("No executable file now.\n"));
00148 }
00149 
00150 /* Set FILENAME as the new exec file.
00151 
00152    This function is intended to be behave essentially the same
00153    as exec_file_command, except that the latter will detect when
00154    a target is being debugged, and will ask the user whether it
00155    should be shut down first.  (If the answer is "no", then the
00156    new file is ignored.)
00157 
00158    This file is used by exec_file_command, to do the work of opening
00159    and processing the exec file after any prompting has happened.
00160 
00161    And, it is used by child_attach, when the attach command was
00162    given a pid but not a exec pathname, and the attach command could
00163    figure out the pathname from the pid.  (In this case, we shouldn't
00164    ask the user whether the current target should be shut down --
00165    we're supplying the exec pathname late for good reason.)  */
00166 
00167 void
00168 exec_file_attach (char *filename, int from_tty)
00169 {
00170   /* Remove any previous exec file.  */
00171   exec_close ();
00172 
00173   /* Now open and digest the file the user requested, if any.  */
00174 
00175   if (!filename)
00176     {
00177       if (from_tty)
00178         printf_unfiltered (_("No executable file now.\n"));
00179 
00180       set_gdbarch_from_file (NULL);
00181     }
00182   else
00183     {
00184       struct cleanup *cleanups;
00185       char *scratch_pathname, *canonical_pathname;
00186       int scratch_chan;
00187       struct target_section *sections = NULL, *sections_end = NULL;
00188       char **matching;
00189 
00190       scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
00191                    write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
00192                             &scratch_pathname);
00193 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
00194       if (scratch_chan < 0)
00195         {
00196           char *exename = alloca (strlen (filename) + 5);
00197 
00198           strcat (strcpy (exename, filename), ".exe");
00199           scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
00200              write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
00201              &scratch_pathname);
00202         }
00203 #endif
00204       if (scratch_chan < 0)
00205         perror_with_name (filename);
00206 
00207       cleanups = make_cleanup (xfree, scratch_pathname);
00208 
00209       /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
00210          better BFD caching.  */
00211       canonical_pathname = gdb_realpath (scratch_pathname);
00212       make_cleanup (xfree, canonical_pathname);
00213 
00214       if (write_files)
00215         exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
00216                                   FOPEN_RUB, scratch_chan);
00217       else
00218         exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
00219 
00220       if (!exec_bfd)
00221         {
00222           error (_("\"%s\": could not open as an executable file: %s"),
00223                  scratch_pathname, bfd_errmsg (bfd_get_error ()));
00224         }
00225 
00226       gdb_assert (exec_filename == NULL);
00227       exec_filename = xstrdup (scratch_pathname);
00228 
00229       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
00230         {
00231           /* Make sure to close exec_bfd, or else "run" might try to use
00232              it.  */
00233           exec_close ();
00234           error (_("\"%s\": not in executable format: %s"),
00235                  scratch_pathname,
00236                  gdb_bfd_errmsg (bfd_get_error (), matching));
00237         }
00238 
00239       if (build_section_table (exec_bfd, &sections, &sections_end))
00240         {
00241           /* Make sure to close exec_bfd, or else "run" might try to use
00242              it.  */
00243           exec_close ();
00244           error (_("\"%s\": can't find the file sections: %s"),
00245                  scratch_pathname, bfd_errmsg (bfd_get_error ()));
00246         }
00247 
00248       exec_bfd_mtime = bfd_get_mtime (exec_bfd);
00249 
00250       validate_files ();
00251 
00252       set_gdbarch_from_file (exec_bfd);
00253 
00254       /* Add the executable's sections to the current address spaces'
00255          list of sections.  This possibly pushes the exec_ops
00256          target.  */
00257       add_target_sections (&exec_bfd, sections, sections_end);
00258       xfree (sections);
00259 
00260       /* Tell display code (if any) about the changed file name.  */
00261       if (deprecated_exec_file_display_hook)
00262         (*deprecated_exec_file_display_hook) (filename);
00263 
00264       do_cleanups (cleanups);
00265     }
00266   bfd_cache_close_all ();
00267   observer_notify_executable_changed ();
00268 }
00269 
00270 /*  Process the first arg in ARGS as the new exec file.
00271 
00272    Note that we have to explicitly ignore additional args, since we can
00273    be called from file_command(), which also calls symbol_file_command()
00274    which can take multiple args.
00275    
00276    If ARGS is NULL, we just want to close the exec file.  */
00277 
00278 static void
00279 exec_file_command (char *args, int from_tty)
00280 {
00281   char **argv;
00282   char *filename;
00283 
00284   if (from_tty && target_has_execution
00285       && !query (_("A program is being debugged already.\n"
00286                    "Are you sure you want to change the file? ")))
00287     error (_("File not changed."));
00288 
00289   if (args)
00290     {
00291       struct cleanup *cleanups;
00292 
00293       /* Scan through the args and pick up the first non option arg
00294          as the filename.  */
00295 
00296       argv = gdb_buildargv (args);
00297       cleanups = make_cleanup_freeargv (argv);
00298 
00299       for (; (*argv != NULL) && (**argv == '-'); argv++)
00300         {;
00301         }
00302       if (*argv == NULL)
00303         error (_("No executable file name was specified"));
00304 
00305       filename = tilde_expand (*argv);
00306       make_cleanup (xfree, filename);
00307       exec_file_attach (filename, from_tty);
00308 
00309       do_cleanups (cleanups);
00310     }
00311   else
00312     exec_file_attach (NULL, from_tty);
00313 }
00314 
00315 /* Set both the exec file and the symbol file, in one command.
00316    What a novelty.  Why did GDB go through four major releases before this
00317    command was added?  */
00318 
00319 static void
00320 file_command (char *arg, int from_tty)
00321 {
00322   /* FIXME, if we lose on reading the symbol file, we should revert
00323      the exec file, but that's rough.  */
00324   exec_file_command (arg, from_tty);
00325   symbol_file_command (arg, from_tty);
00326   if (deprecated_file_changed_hook)
00327     deprecated_file_changed_hook (arg);
00328 }
00329 
00330 
00331 /* Locate all mappable sections of a BFD file.
00332    table_pp_char is a char * to get it through bfd_map_over_sections;
00333    we cast it back to its proper type.  */
00334 
00335 static void
00336 add_to_section_table (bfd *abfd, struct bfd_section *asect,
00337                       void *table_pp_char)
00338 {
00339   struct target_section **table_pp = (struct target_section **) table_pp_char;
00340   flagword aflag;
00341 
00342   gdb_assert (abfd == asect->owner);
00343 
00344   /* Check the section flags, but do not discard zero-length sections, since
00345      some symbols may still be attached to this section.  For instance, we
00346      encountered on sparc-solaris 2.10 a shared library with an empty .bss
00347      section to which a symbol named "_end" was attached.  The address
00348      of this symbol still needs to be relocated.  */
00349   aflag = bfd_get_section_flags (abfd, asect);
00350   if (!(aflag & SEC_ALLOC))
00351     return;
00352 
00353   (*table_pp)->owner = NULL;
00354   (*table_pp)->the_bfd_section = asect;
00355   (*table_pp)->addr = bfd_section_vma (abfd, asect);
00356   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
00357   (*table_pp)++;
00358 }
00359 
00360 int
00361 resize_section_table (struct target_section_table *table, int num_added)
00362 {
00363   int old_count;
00364   int new_count;
00365 
00366   old_count = table->sections_end - table->sections;
00367 
00368   new_count = num_added + old_count;
00369 
00370   if (new_count)
00371     {
00372       table->sections = xrealloc (table->sections,
00373                                   sizeof (struct target_section) * new_count);
00374       table->sections_end = table->sections + new_count;
00375     }
00376   else
00377     {
00378       xfree (table->sections);
00379       table->sections = table->sections_end = NULL;
00380     }
00381 
00382   return old_count;
00383 }
00384 
00385 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
00386    Returns 0 if OK, 1 on error.  */
00387 
00388 int
00389 build_section_table (struct bfd *some_bfd, struct target_section **start,
00390                      struct target_section **end)
00391 {
00392   unsigned count;
00393 
00394   count = bfd_count_sections (some_bfd);
00395   if (*start)
00396     xfree (* start);
00397   *start = (struct target_section *) xmalloc (count * sizeof (**start));
00398   *end = *start;
00399   bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
00400   if (*end > *start + count)
00401     internal_error (__FILE__, __LINE__,
00402                     _("failed internal consistency check"));
00403   /* We could realloc the table, but it probably loses for most files.  */
00404   return 0;
00405 }
00406 
00407 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
00408    current set of target sections.  */
00409 
00410 void
00411 add_target_sections (void *owner,
00412                      struct target_section *sections,
00413                      struct target_section *sections_end)
00414 {
00415   int count;
00416   struct target_section_table *table = current_target_sections;
00417 
00418   count = sections_end - sections;
00419 
00420   if (count > 0)
00421     {
00422       int space = resize_section_table (table, count);
00423       int i;
00424 
00425       for (i = 0; i < count; ++i)
00426         {
00427           table->sections[space + i] = sections[i];
00428           table->sections[space + i].owner = owner;
00429         }
00430 
00431       /* If these are the first file sections we can provide memory
00432          from, push the file_stratum target.  */
00433       if (!using_exec_ops)
00434         {
00435           using_exec_ops = 1;
00436           push_target (&exec_ops);
00437         }
00438     }
00439 }
00440 
00441 /* Remove all target sections owned by OWNER.
00442    OWNER must be the same value passed to add_target_sections.  */
00443 
00444 void
00445 remove_target_sections (void *owner)
00446 {
00447   struct target_section *src, *dest;
00448   struct target_section_table *table = current_target_sections;
00449 
00450   gdb_assert (owner != NULL);
00451 
00452   dest = table->sections;
00453   for (src = table->sections; src < table->sections_end; src++)
00454     if (src->owner != owner)
00455       {
00456         /* Keep this section.  */
00457         if (dest < src)
00458           *dest = *src;
00459         dest++;
00460       }
00461 
00462   /* If we've dropped any sections, resize the section table.  */
00463   if (dest < src)
00464     {
00465       int old_count;
00466 
00467       old_count = resize_section_table (table, dest - src);
00468 
00469       /* If we don't have any more sections to read memory from,
00470          remove the file_stratum target from the stack.  */
00471       if (old_count + (dest - src) == 0)
00472         {
00473           struct program_space *pspace;
00474 
00475           ALL_PSPACES (pspace)
00476             if (pspace->target_sections.sections
00477                 != pspace->target_sections.sections_end)
00478               return;
00479 
00480           unpush_target (&exec_ops);
00481         }
00482     }
00483 }
00484 
00485 
00486 
00487 VEC(mem_range_s) *
00488 section_table_available_memory (VEC(mem_range_s) *memory,
00489                                 CORE_ADDR memaddr, ULONGEST len,
00490                                 struct target_section *sections,
00491                                 struct target_section *sections_end)
00492 {
00493   struct target_section *p;
00494 
00495   for (p = sections; p < sections_end; p++)
00496     {
00497       if ((bfd_get_section_flags (p->the_bfd_section->owner,
00498                                   p->the_bfd_section)
00499            & SEC_READONLY) == 0)
00500         continue;
00501 
00502       /* Copy the meta-data, adjusted.  */
00503       if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
00504         {
00505           ULONGEST lo1, hi1, lo2, hi2;
00506           struct mem_range *r;
00507 
00508           lo1 = memaddr;
00509           hi1 = memaddr + len;
00510 
00511           lo2 = p->addr;
00512           hi2 = p->endaddr;
00513 
00514           r = VEC_safe_push (mem_range_s, memory, NULL);
00515 
00516           r->start = max (lo1, lo2);
00517           r->length = min (hi1, hi2) - r->start;
00518         }
00519     }
00520 
00521   return memory;
00522 }
00523 
00524 int
00525 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
00526                                    ULONGEST offset, LONGEST len,
00527                                    struct target_section *sections,
00528                                    struct target_section *sections_end,
00529                                    const char *section_name)
00530 {
00531   int res;
00532   struct target_section *p;
00533   ULONGEST memaddr = offset;
00534   ULONGEST memend = memaddr + len;
00535 
00536   if (len <= 0)
00537     internal_error (__FILE__, __LINE__,
00538                     _("failed internal consistency check"));
00539 
00540   for (p = sections; p < sections_end; p++)
00541     {
00542       struct bfd_section *asect = p->the_bfd_section;
00543       bfd *abfd = asect->owner;
00544 
00545       if (section_name && strcmp (section_name, asect->name) != 0)
00546         continue;               /* not the section we need.  */
00547       if (memaddr >= p->addr)
00548         {
00549           if (memend <= p->endaddr)
00550             {
00551               /* Entire transfer is within this section.  */
00552               if (writebuf)
00553                 res = bfd_set_section_contents (abfd, asect,
00554                                                 writebuf, memaddr - p->addr,
00555                                                 len);
00556               else
00557                 res = bfd_get_section_contents (abfd, asect,
00558                                                 readbuf, memaddr - p->addr,
00559                                                 len);
00560               return (res != 0) ? len : 0;
00561             }
00562           else if (memaddr >= p->endaddr)
00563             {
00564               /* This section ends before the transfer starts.  */
00565               continue;
00566             }
00567           else
00568             {
00569               /* This section overlaps the transfer.  Just do half.  */
00570               len = p->endaddr - memaddr;
00571               if (writebuf)
00572                 res = bfd_set_section_contents (abfd, asect,
00573                                                 writebuf, memaddr - p->addr,
00574                                                 len);
00575               else
00576                 res = bfd_get_section_contents (abfd, asect,
00577                                                 readbuf, memaddr - p->addr,
00578                                                 len);
00579               return (res != 0) ? len : 0;
00580             }
00581         }
00582     }
00583 
00584   return 0;                     /* We can't help.  */
00585 }
00586 
00587 static struct target_section_table *
00588 exec_get_section_table (struct target_ops *ops)
00589 {
00590   return current_target_sections;
00591 }
00592 
00593 static LONGEST
00594 exec_xfer_partial (struct target_ops *ops, enum target_object object,
00595                    const char *annex, gdb_byte *readbuf,
00596                    const gdb_byte *writebuf,
00597                    ULONGEST offset, LONGEST len)
00598 {
00599   struct target_section_table *table = target_get_section_table (ops);
00600 
00601   if (object == TARGET_OBJECT_MEMORY)
00602     return section_table_xfer_memory_partial (readbuf, writebuf,
00603                                               offset, len,
00604                                               table->sections,
00605                                               table->sections_end,
00606                                               NULL);
00607   else
00608     return -1;
00609 }
00610 
00611 
00612 void
00613 print_section_info (struct target_section_table *t, bfd *abfd)
00614 {
00615   struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
00616   struct target_section *p;
00617   /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64.  */
00618   int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
00619 
00620   printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
00621   wrap_here ("        ");
00622   printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
00623   if (abfd == exec_bfd)
00624     {
00625       /* gcc-3.4 does not like the initialization in
00626          <p == t->sections_end>.  */
00627       bfd_vma displacement = 0;
00628       bfd_vma entry_point;
00629 
00630       for (p = t->sections; p < t->sections_end; p++)
00631         {
00632           struct bfd_section *psect = p->the_bfd_section;
00633           bfd *pbfd = psect->owner;
00634 
00635           if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
00636               != (SEC_ALLOC | SEC_LOAD))
00637             continue;
00638 
00639           if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address
00640               && abfd->start_address < (bfd_get_section_vma (pbfd, psect)
00641                                         + bfd_get_section_size (psect)))
00642             {
00643               displacement = p->addr - bfd_get_section_vma (pbfd, psect);
00644               break;
00645             }
00646         }
00647       if (p == t->sections_end)
00648         warning (_("Cannot find section for the entry point of %s."),
00649                  bfd_get_filename (abfd));
00650 
00651       entry_point = gdbarch_addr_bits_remove (gdbarch, 
00652                                               bfd_get_start_address (abfd) 
00653                                                 + displacement);
00654       printf_filtered (_("\tEntry point: %s\n"),
00655                        paddress (gdbarch, entry_point));
00656     }
00657   for (p = t->sections; p < t->sections_end; p++)
00658     {
00659       struct bfd_section *psect = p->the_bfd_section;
00660       bfd *pbfd = psect->owner;
00661 
00662       printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
00663       printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
00664 
00665       /* FIXME: A format of "08l" is not wide enough for file offsets
00666          larger than 4GB.  OTOH, making it "016l" isn't desirable either
00667          since most output will then be much wider than necessary.  It
00668          may make sense to test the size of the file and choose the
00669          format string accordingly.  */
00670       /* FIXME: i18n: Need to rewrite this sentence.  */
00671       if (info_verbose)
00672         printf_filtered (" @ %s",
00673                          hex_string_custom (psect->filepos, 8));
00674       printf_filtered (" is %s", bfd_section_name (pbfd, psect));
00675       if (pbfd != abfd)
00676         printf_filtered (" in %s", bfd_get_filename (pbfd));
00677       printf_filtered ("\n");
00678     }
00679 }
00680 
00681 static void
00682 exec_files_info (struct target_ops *t)
00683 {
00684   if (exec_bfd)
00685     print_section_info (current_target_sections, exec_bfd);
00686   else
00687     puts_filtered (_("\t<no file loaded>\n"));
00688 }
00689 
00690 static void
00691 set_section_command (char *args, int from_tty)
00692 {
00693   struct target_section *p;
00694   char *secname;
00695   unsigned seclen;
00696   unsigned long secaddr;
00697   char secprint[100];
00698   long offset;
00699   struct target_section_table *table;
00700 
00701   if (args == 0)
00702     error (_("Must specify section name and its virtual address"));
00703 
00704   /* Parse out section name.  */
00705   for (secname = args; !isspace (*args); args++);
00706   seclen = args - secname;
00707 
00708   /* Parse out new virtual address.  */
00709   secaddr = parse_and_eval_address (args);
00710 
00711   table = current_target_sections;
00712   for (p = table->sections; p < table->sections_end; p++)
00713     {
00714       if (!strncmp (secname, bfd_section_name (p->bfd,
00715                                                p->the_bfd_section), seclen)
00716           && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
00717         {
00718           offset = secaddr - p->addr;
00719           p->addr += offset;
00720           p->endaddr += offset;
00721           if (from_tty)
00722             exec_files_info (&exec_ops);
00723           return;
00724         }
00725     }
00726   if (seclen >= sizeof (secprint))
00727     seclen = sizeof (secprint) - 1;
00728   strncpy (secprint, secname, seclen);
00729   secprint[seclen] = '\0';
00730   error (_("Section %s not found"), secprint);
00731 }
00732 
00733 /* If we can find a section in FILENAME with BFD index INDEX, adjust
00734    it to ADDRESS.  */
00735 
00736 void
00737 exec_set_section_address (const char *filename, int index, CORE_ADDR address)
00738 {
00739   struct target_section *p;
00740   struct target_section_table *table;
00741 
00742   table = current_target_sections;
00743   for (p = table->sections; p < table->sections_end; p++)
00744     {
00745       if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
00746           && index == p->the_bfd_section->index)
00747         {
00748           p->endaddr += address - p->addr;
00749           p->addr = address;
00750         }
00751     }
00752 }
00753 
00754 /* If mourn is being called in all the right places, this could be say
00755    `gdb internal error' (since generic_mourn calls
00756    breakpoint_init_inferior).  */
00757 
00758 static int
00759 ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
00760 {
00761   return 0;
00762 }
00763 
00764 static int
00765 exec_has_memory (struct target_ops *ops)
00766 {
00767   /* We can provide memory if we have any file/target sections to read
00768      from.  */
00769   return (current_target_sections->sections
00770           != current_target_sections->sections_end);
00771 }
00772 
00773 /* Find mapped memory.  */
00774 
00775 extern void
00776 exec_set_find_memory_regions (int (*func) (find_memory_region_ftype, void *))
00777 {
00778   exec_ops.to_find_memory_regions = func;
00779 }
00780 
00781 static char *exec_make_note_section (bfd *, int *);
00782 
00783 /* Fill in the exec file target vector.  Very few entries need to be
00784    defined.  */
00785 
00786 static void
00787 init_exec_ops (void)
00788 {
00789   exec_ops.to_shortname = "exec";
00790   exec_ops.to_longname = "Local exec file";
00791   exec_ops.to_doc = "Use an executable file as a target.\n\
00792 Specify the filename of the executable file.";
00793   exec_ops.to_open = exec_open;
00794   exec_ops.to_close = exec_close_1;
00795   exec_ops.to_attach = find_default_attach;
00796   exec_ops.to_xfer_partial = exec_xfer_partial;
00797   exec_ops.to_get_section_table = exec_get_section_table;
00798   exec_ops.to_files_info = exec_files_info;
00799   exec_ops.to_insert_breakpoint = ignore;
00800   exec_ops.to_remove_breakpoint = ignore;
00801   exec_ops.to_create_inferior = find_default_create_inferior;
00802   exec_ops.to_stratum = file_stratum;
00803   exec_ops.to_has_memory = exec_has_memory;
00804   exec_ops.to_make_corefile_notes = exec_make_note_section;
00805   exec_ops.to_magic = OPS_MAGIC;
00806 }
00807 
00808 void
00809 _initialize_exec (void)
00810 {
00811   struct cmd_list_element *c;
00812 
00813   init_exec_ops ();
00814 
00815   if (!dbx_commands)
00816     {
00817       c = add_cmd ("file", class_files, file_command, _("\
00818 Use FILE as program to be debugged.\n\
00819 It is read for its symbols, for getting the contents of pure memory,\n\
00820 and it is the program executed when you use the `run' command.\n\
00821 If FILE cannot be found as specified, your execution directory path\n\
00822 ($PATH) is searched for a command of that name.\n\
00823 No arg means to have no executable file and no symbols."), &cmdlist);
00824       set_cmd_completer (c, filename_completer);
00825     }
00826 
00827   c = add_cmd ("exec-file", class_files, exec_file_command, _("\
00828 Use FILE as program for getting contents of pure memory.\n\
00829 If FILE cannot be found as specified, your execution directory path\n\
00830 is searched for a command of that name.\n\
00831 No arg means have no executable file."), &cmdlist);
00832   set_cmd_completer (c, filename_completer);
00833 
00834   add_com ("section", class_files, set_section_command, _("\
00835 Change the base address of section SECTION of the exec file to ADDR.\n\
00836 This can be used if the exec file does not contain section addresses,\n\
00837 (such as in the a.out format), or when the addresses specified in the\n\
00838 file itself are wrong.  Each section must be changed separately.  The\n\
00839 ``info files'' command lists all the sections and their addresses."));
00840 
00841   add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
00842 Set writing into executable and core files."), _("\
00843 Show writing into executable and core files."), NULL,
00844                            NULL,
00845                            show_write_files,
00846                            &setlist, &showlist);
00847 
00848   add_target_with_completer (&exec_ops, filename_completer);
00849 }
00850 
00851 static char *
00852 exec_make_note_section (bfd *obfd, int *note_size)
00853 {
00854   error (_("Can't create a corefile"));
00855 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines