61dea49e2d9a9838d0c1d5042a00c26950c0b007
[platform/upstream/binutils.git] / gdb / exec.c
1 /* Work with executable files, for GDB. 
2
3    Copyright (C) 1988-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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 #include "filenames.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "completer.h"
30 #include "value.h"
31 #include "exec.h"
32 #include "observer.h"
33 #include "arch-utils.h"
34 #include "gdbthread.h"
35 #include "progspace.h"
36 #include "gdb_bfd.h"
37
38 #include <fcntl.h>
39 #include "readline/readline.h"
40 #include <string.h>
41
42 #include "gdbcore.h"
43
44 #include <ctype.h>
45 #include <sys/stat.h>
46
47 void (*deprecated_file_changed_hook) (char *);
48
49 /* Prototypes for local functions */
50
51 static void file_command (char *, int);
52
53 static void set_section_command (char *, int);
54
55 static void exec_files_info (struct target_ops *);
56
57 static void init_exec_ops (void);
58
59 void _initialize_exec (void);
60
61 /* The target vector for executable files.  */
62
63 struct target_ops exec_ops;
64
65 /* Function used to implement to_find_memory_regions.  */
66
67 static int (*exec_do_find_memory_regions) (find_memory_region_ftype, void *);
68
69 /* True if the exec target is pushed on the stack.  */
70 static int using_exec_ops;
71
72 /* Whether to open exec and core files read-only or read-write.  */
73
74 int write_files = 0;
75 static void
76 show_write_files (struct ui_file *file, int from_tty,
77                   struct cmd_list_element *c, const char *value)
78 {
79   fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
80                     value);
81 }
82
83
84 static void
85 exec_open (char *args, int from_tty)
86 {
87   target_preopen (from_tty);
88   exec_file_attach (args, from_tty);
89 }
90
91 /* Close and clear exec_bfd.  If we end up with no target sections to
92    read memory from, this unpushes the exec_ops target.  */
93
94 void
95 exec_close (void)
96 {
97   if (exec_bfd)
98     {
99       bfd *abfd = exec_bfd;
100
101       gdb_bfd_unref (abfd);
102
103       /* Removing target sections may close the exec_ops target.
104          Clear exec_bfd before doing so to prevent recursion.  */
105       exec_bfd = NULL;
106       exec_bfd_mtime = 0;
107
108       remove_target_sections (&exec_bfd);
109
110       xfree (exec_filename);
111       exec_filename = NULL;
112     }
113 }
114
115 /* This is the target_close implementation.  Clears all target
116    sections and closes all executable bfds from all program spaces.  */
117
118 static void
119 exec_close_1 (struct target_ops *self)
120 {
121   using_exec_ops = 0;
122
123   {
124     struct program_space *ss;
125     struct cleanup *old_chain;
126
127     old_chain = save_current_program_space ();
128     ALL_PSPACES (ss)
129     {
130       set_current_program_space (ss);
131
132       /* Delete all target sections.  */
133       resize_section_table
134         (current_target_sections,
135          -resize_section_table (current_target_sections, 0));
136
137       exec_close ();
138     }
139
140     do_cleanups (old_chain);
141   }
142 }
143
144 void
145 exec_file_clear (int from_tty)
146 {
147   /* Remove exec file.  */
148   exec_close ();
149
150   if (from_tty)
151     printf_unfiltered (_("No executable file now.\n"));
152 }
153
154 /* Set FILENAME as the new exec file.
155
156    This function is intended to be behave essentially the same
157    as exec_file_command, except that the latter will detect when
158    a target is being debugged, and will ask the user whether it
159    should be shut down first.  (If the answer is "no", then the
160    new file is ignored.)
161
162    This file is used by exec_file_command, to do the work of opening
163    and processing the exec file after any prompting has happened.
164
165    And, it is used by child_attach, when the attach command was
166    given a pid but not a exec pathname, and the attach command could
167    figure out the pathname from the pid.  (In this case, we shouldn't
168    ask the user whether the current target should be shut down --
169    we're supplying the exec pathname late for good reason.)  */
170
171 void
172 exec_file_attach (char *filename, int from_tty)
173 {
174   /* Remove any previous exec file.  */
175   exec_close ();
176
177   /* Now open and digest the file the user requested, if any.  */
178
179   if (!filename)
180     {
181       if (from_tty)
182         printf_unfiltered (_("No executable file now.\n"));
183
184       set_gdbarch_from_file (NULL);
185     }
186   else
187     {
188       struct cleanup *cleanups;
189       char *scratch_pathname, *canonical_pathname;
190       int scratch_chan;
191       struct target_section *sections = NULL, *sections_end = NULL;
192       char **matching;
193
194       scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
195                    write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
196                             &scratch_pathname);
197 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
198       if (scratch_chan < 0)
199         {
200           char *exename = alloca (strlen (filename) + 5);
201
202           strcat (strcpy (exename, filename), ".exe");
203           scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
204              write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
205              &scratch_pathname);
206         }
207 #endif
208       if (scratch_chan < 0)
209         perror_with_name (filename);
210
211       cleanups = make_cleanup (xfree, scratch_pathname);
212
213       /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
214          better BFD caching.  */
215       canonical_pathname = gdb_realpath (scratch_pathname);
216       make_cleanup (xfree, canonical_pathname);
217
218       if (write_files)
219         exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
220                                   FOPEN_RUB, scratch_chan);
221       else
222         exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
223
224       if (!exec_bfd)
225         {
226           error (_("\"%s\": could not open as an executable file: %s"),
227                  scratch_pathname, bfd_errmsg (bfd_get_error ()));
228         }
229
230       gdb_assert (exec_filename == NULL);
231       exec_filename = gdb_realpath_keepfile (scratch_pathname);
232
233       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
234         {
235           /* Make sure to close exec_bfd, or else "run" might try to use
236              it.  */
237           exec_close ();
238           error (_("\"%s\": not in executable format: %s"),
239                  scratch_pathname,
240                  gdb_bfd_errmsg (bfd_get_error (), matching));
241         }
242
243       if (build_section_table (exec_bfd, &sections, &sections_end))
244         {
245           /* Make sure to close exec_bfd, or else "run" might try to use
246              it.  */
247           exec_close ();
248           error (_("\"%s\": can't find the file sections: %s"),
249                  scratch_pathname, bfd_errmsg (bfd_get_error ()));
250         }
251
252       exec_bfd_mtime = bfd_get_mtime (exec_bfd);
253
254       validate_files ();
255
256       set_gdbarch_from_file (exec_bfd);
257
258       /* Add the executable's sections to the current address spaces'
259          list of sections.  This possibly pushes the exec_ops
260          target.  */
261       add_target_sections (&exec_bfd, sections, sections_end);
262       xfree (sections);
263
264       /* Tell display code (if any) about the changed file name.  */
265       if (deprecated_exec_file_display_hook)
266         (*deprecated_exec_file_display_hook) (filename);
267
268       do_cleanups (cleanups);
269     }
270   bfd_cache_close_all ();
271   observer_notify_executable_changed ();
272 }
273
274 /*  Process the first arg in ARGS as the new exec file.
275
276    Note that we have to explicitly ignore additional args, since we can
277    be called from file_command(), which also calls symbol_file_command()
278    which can take multiple args.
279    
280    If ARGS is NULL, we just want to close the exec file.  */
281
282 static void
283 exec_file_command (char *args, int from_tty)
284 {
285   char **argv;
286   char *filename;
287
288   if (from_tty && target_has_execution
289       && !query (_("A program is being debugged already.\n"
290                    "Are you sure you want to change the file? ")))
291     error (_("File not changed."));
292
293   if (args)
294     {
295       struct cleanup *cleanups;
296
297       /* Scan through the args and pick up the first non option arg
298          as the filename.  */
299
300       argv = gdb_buildargv (args);
301       cleanups = make_cleanup_freeargv (argv);
302
303       for (; (*argv != NULL) && (**argv == '-'); argv++)
304         {;
305         }
306       if (*argv == NULL)
307         error (_("No executable file name was specified"));
308
309       filename = tilde_expand (*argv);
310       make_cleanup (xfree, filename);
311       exec_file_attach (filename, from_tty);
312
313       do_cleanups (cleanups);
314     }
315   else
316     exec_file_attach (NULL, from_tty);
317 }
318
319 /* Set both the exec file and the symbol file, in one command.
320    What a novelty.  Why did GDB go through four major releases before this
321    command was added?  */
322
323 static void
324 file_command (char *arg, int from_tty)
325 {
326   /* FIXME, if we lose on reading the symbol file, we should revert
327      the exec file, but that's rough.  */
328   exec_file_command (arg, from_tty);
329   symbol_file_command (arg, from_tty);
330   if (deprecated_file_changed_hook)
331     deprecated_file_changed_hook (arg);
332 }
333 \f
334
335 /* Locate all mappable sections of a BFD file.
336    table_pp_char is a char * to get it through bfd_map_over_sections;
337    we cast it back to its proper type.  */
338
339 static void
340 add_to_section_table (bfd *abfd, struct bfd_section *asect,
341                       void *table_pp_char)
342 {
343   struct target_section **table_pp = (struct target_section **) table_pp_char;
344   flagword aflag;
345
346   gdb_assert (abfd == asect->owner);
347
348   /* Check the section flags, but do not discard zero-length sections, since
349      some symbols may still be attached to this section.  For instance, we
350      encountered on sparc-solaris 2.10 a shared library with an empty .bss
351      section to which a symbol named "_end" was attached.  The address
352      of this symbol still needs to be relocated.  */
353   aflag = bfd_get_section_flags (abfd, asect);
354   if (!(aflag & SEC_ALLOC))
355     return;
356
357   (*table_pp)->owner = NULL;
358   (*table_pp)->the_bfd_section = asect;
359   (*table_pp)->addr = bfd_section_vma (abfd, asect);
360   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
361   (*table_pp)++;
362 }
363
364 int
365 resize_section_table (struct target_section_table *table, int num_added)
366 {
367   int old_count;
368   int new_count;
369
370   old_count = table->sections_end - table->sections;
371
372   new_count = num_added + old_count;
373
374   if (new_count)
375     {
376       table->sections = xrealloc (table->sections,
377                                   sizeof (struct target_section) * new_count);
378       table->sections_end = table->sections + new_count;
379     }
380   else
381     {
382       xfree (table->sections);
383       table->sections = table->sections_end = NULL;
384     }
385
386   return old_count;
387 }
388
389 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
390    Returns 0 if OK, 1 on error.  */
391
392 int
393 build_section_table (struct bfd *some_bfd, struct target_section **start,
394                      struct target_section **end)
395 {
396   unsigned count;
397
398   count = bfd_count_sections (some_bfd);
399   if (*start)
400     xfree (* start);
401   *start = (struct target_section *) xmalloc (count * sizeof (**start));
402   *end = *start;
403   bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
404   if (*end > *start + count)
405     internal_error (__FILE__, __LINE__,
406                     _("failed internal consistency check"));
407   /* We could realloc the table, but it probably loses for most files.  */
408   return 0;
409 }
410
411 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
412    current set of target sections.  */
413
414 void
415 add_target_sections (void *owner,
416                      struct target_section *sections,
417                      struct target_section *sections_end)
418 {
419   int count;
420   struct target_section_table *table = current_target_sections;
421
422   count = sections_end - sections;
423
424   if (count > 0)
425     {
426       int space = resize_section_table (table, count);
427       int i;
428
429       for (i = 0; i < count; ++i)
430         {
431           table->sections[space + i] = sections[i];
432           table->sections[space + i].owner = owner;
433         }
434
435       /* If these are the first file sections we can provide memory
436          from, push the file_stratum target.  */
437       if (!using_exec_ops)
438         {
439           using_exec_ops = 1;
440           push_target (&exec_ops);
441         }
442     }
443 }
444
445 /* Add the sections of OBJFILE to the current set of target sections.  */
446
447 void
448 add_target_sections_of_objfile (struct objfile *objfile)
449 {
450   struct target_section_table *table = current_target_sections;
451   struct obj_section *osect;
452   int space;
453   unsigned count = 0;
454   struct target_section *ts;
455
456   if (objfile == NULL)
457     return;
458
459   /* Compute the number of sections to add.  */
460   ALL_OBJFILE_OSECTIONS (objfile, osect)
461     {
462       if (bfd_get_section_size (osect->the_bfd_section) == 0)
463         continue;
464       count++;
465     }
466
467   if (count == 0)
468     return;
469
470   space = resize_section_table (table, count);
471
472   ts = table->sections + space;
473
474   ALL_OBJFILE_OSECTIONS (objfile, osect)
475     {
476       if (bfd_get_section_size (osect->the_bfd_section) == 0)
477         continue;
478
479       gdb_assert (ts < table->sections + space + count);
480
481       ts->addr = obj_section_addr (osect);
482       ts->endaddr = obj_section_endaddr (osect);
483       ts->the_bfd_section = osect->the_bfd_section;
484       ts->owner = (void *) objfile;
485
486       ts++;
487     }
488 }
489
490 /* Remove all target sections owned by OWNER.
491    OWNER must be the same value passed to add_target_sections.  */
492
493 void
494 remove_target_sections (void *owner)
495 {
496   struct target_section *src, *dest;
497   struct target_section_table *table = current_target_sections;
498
499   gdb_assert (owner != NULL);
500
501   dest = table->sections;
502   for (src = table->sections; src < table->sections_end; src++)
503     if (src->owner != owner)
504       {
505         /* Keep this section.  */
506         if (dest < src)
507           *dest = *src;
508         dest++;
509       }
510
511   /* If we've dropped any sections, resize the section table.  */
512   if (dest < src)
513     {
514       int old_count;
515
516       old_count = resize_section_table (table, dest - src);
517
518       /* If we don't have any more sections to read memory from,
519          remove the file_stratum target from the stack.  */
520       if (old_count + (dest - src) == 0)
521         {
522           struct program_space *pspace;
523
524           ALL_PSPACES (pspace)
525             if (pspace->target_sections.sections
526                 != pspace->target_sections.sections_end)
527               return;
528
529           unpush_target (&exec_ops);
530         }
531     }
532 }
533
534 \f
535
536 VEC(mem_range_s) *
537 section_table_available_memory (VEC(mem_range_s) *memory,
538                                 CORE_ADDR memaddr, ULONGEST len,
539                                 struct target_section *sections,
540                                 struct target_section *sections_end)
541 {
542   struct target_section *p;
543
544   for (p = sections; p < sections_end; p++)
545     {
546       if ((bfd_get_section_flags (p->the_bfd_section->owner,
547                                   p->the_bfd_section)
548            & SEC_READONLY) == 0)
549         continue;
550
551       /* Copy the meta-data, adjusted.  */
552       if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
553         {
554           ULONGEST lo1, hi1, lo2, hi2;
555           struct mem_range *r;
556
557           lo1 = memaddr;
558           hi1 = memaddr + len;
559
560           lo2 = p->addr;
561           hi2 = p->endaddr;
562
563           r = VEC_safe_push (mem_range_s, memory, NULL);
564
565           r->start = max (lo1, lo2);
566           r->length = min (hi1, hi2) - r->start;
567         }
568     }
569
570   return memory;
571 }
572
573 enum target_xfer_status
574 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
575                                    ULONGEST offset, ULONGEST len,
576                                    ULONGEST *xfered_len,
577                                    struct target_section *sections,
578                                    struct target_section *sections_end,
579                                    const char *section_name)
580 {
581   int res;
582   struct target_section *p;
583   ULONGEST memaddr = offset;
584   ULONGEST memend = memaddr + len;
585
586   if (len == 0)
587     internal_error (__FILE__, __LINE__,
588                     _("failed internal consistency check"));
589
590   for (p = sections; p < sections_end; p++)
591     {
592       struct bfd_section *asect = p->the_bfd_section;
593       bfd *abfd = asect->owner;
594
595       if (section_name && strcmp (section_name, asect->name) != 0)
596         continue;               /* not the section we need.  */
597       if (memaddr >= p->addr)
598         {
599           if (memend <= p->endaddr)
600             {
601               /* Entire transfer is within this section.  */
602               if (writebuf)
603                 res = bfd_set_section_contents (abfd, asect,
604                                                 writebuf, memaddr - p->addr,
605                                                 len);
606               else
607                 res = bfd_get_section_contents (abfd, asect,
608                                                 readbuf, memaddr - p->addr,
609                                                 len);
610
611               if (res != 0)
612                 {
613                   *xfered_len = len;
614                   return TARGET_XFER_OK;
615                 }
616               else
617                 return TARGET_XFER_EOF;
618             }
619           else if (memaddr >= p->endaddr)
620             {
621               /* This section ends before the transfer starts.  */
622               continue;
623             }
624           else
625             {
626               /* This section overlaps the transfer.  Just do half.  */
627               len = p->endaddr - memaddr;
628               if (writebuf)
629                 res = bfd_set_section_contents (abfd, asect,
630                                                 writebuf, memaddr - p->addr,
631                                                 len);
632               else
633                 res = bfd_get_section_contents (abfd, asect,
634                                                 readbuf, memaddr - p->addr,
635                                                 len);
636               if (res != 0)
637                 {
638                   *xfered_len = len;
639                   return TARGET_XFER_OK;
640                 }
641               else
642                 return TARGET_XFER_EOF;
643             }
644         }
645     }
646
647   return TARGET_XFER_EOF;               /* We can't help.  */
648 }
649
650 static struct target_section_table *
651 exec_get_section_table (struct target_ops *ops)
652 {
653   return current_target_sections;
654 }
655
656 static enum target_xfer_status
657 exec_xfer_partial (struct target_ops *ops, enum target_object object,
658                    const char *annex, gdb_byte *readbuf,
659                    const gdb_byte *writebuf,
660                    ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
661 {
662   struct target_section_table *table = target_get_section_table (ops);
663
664   if (object == TARGET_OBJECT_MEMORY)
665     return section_table_xfer_memory_partial (readbuf, writebuf,
666                                               offset, len, xfered_len,
667                                               table->sections,
668                                               table->sections_end,
669                                               NULL);
670   else
671     return TARGET_XFER_E_IO;
672 }
673 \f
674
675 void
676 print_section_info (struct target_section_table *t, bfd *abfd)
677 {
678   struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
679   struct target_section *p;
680   /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64.  */
681   int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
682
683   printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
684   wrap_here ("        ");
685   printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
686   if (abfd == exec_bfd)
687     {
688       /* gcc-3.4 does not like the initialization in
689          <p == t->sections_end>.  */
690       bfd_vma displacement = 0;
691       bfd_vma entry_point;
692
693       for (p = t->sections; p < t->sections_end; p++)
694         {
695           struct bfd_section *psect = p->the_bfd_section;
696           bfd *pbfd = psect->owner;
697
698           if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
699               != (SEC_ALLOC | SEC_LOAD))
700             continue;
701
702           if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address
703               && abfd->start_address < (bfd_get_section_vma (pbfd, psect)
704                                         + bfd_get_section_size (psect)))
705             {
706               displacement = p->addr - bfd_get_section_vma (pbfd, psect);
707               break;
708             }
709         }
710       if (p == t->sections_end)
711         warning (_("Cannot find section for the entry point of %s."),
712                  bfd_get_filename (abfd));
713
714       entry_point = gdbarch_addr_bits_remove (gdbarch, 
715                                               bfd_get_start_address (abfd) 
716                                                 + displacement);
717       printf_filtered (_("\tEntry point: %s\n"),
718                        paddress (gdbarch, entry_point));
719     }
720   for (p = t->sections; p < t->sections_end; p++)
721     {
722       struct bfd_section *psect = p->the_bfd_section;
723       bfd *pbfd = psect->owner;
724
725       printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
726       printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
727
728       /* FIXME: A format of "08l" is not wide enough for file offsets
729          larger than 4GB.  OTOH, making it "016l" isn't desirable either
730          since most output will then be much wider than necessary.  It
731          may make sense to test the size of the file and choose the
732          format string accordingly.  */
733       /* FIXME: i18n: Need to rewrite this sentence.  */
734       if (info_verbose)
735         printf_filtered (" @ %s",
736                          hex_string_custom (psect->filepos, 8));
737       printf_filtered (" is %s", bfd_section_name (pbfd, psect));
738       if (pbfd != abfd)
739         printf_filtered (" in %s", bfd_get_filename (pbfd));
740       printf_filtered ("\n");
741     }
742 }
743
744 static void
745 exec_files_info (struct target_ops *t)
746 {
747   if (exec_bfd)
748     print_section_info (current_target_sections, exec_bfd);
749   else
750     puts_filtered (_("\t<no file loaded>\n"));
751 }
752
753 static void
754 set_section_command (char *args, int from_tty)
755 {
756   struct target_section *p;
757   char *secname;
758   unsigned seclen;
759   unsigned long secaddr;
760   char secprint[100];
761   long offset;
762   struct target_section_table *table;
763
764   if (args == 0)
765     error (_("Must specify section name and its virtual address"));
766
767   /* Parse out section name.  */
768   for (secname = args; !isspace (*args); args++);
769   seclen = args - secname;
770
771   /* Parse out new virtual address.  */
772   secaddr = parse_and_eval_address (args);
773
774   table = current_target_sections;
775   for (p = table->sections; p < table->sections_end; p++)
776     {
777       if (!strncmp (secname, bfd_section_name (p->bfd,
778                                                p->the_bfd_section), seclen)
779           && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
780         {
781           offset = secaddr - p->addr;
782           p->addr += offset;
783           p->endaddr += offset;
784           if (from_tty)
785             exec_files_info (&exec_ops);
786           return;
787         }
788     }
789   if (seclen >= sizeof (secprint))
790     seclen = sizeof (secprint) - 1;
791   strncpy (secprint, secname, seclen);
792   secprint[seclen] = '\0';
793   error (_("Section %s not found"), secprint);
794 }
795
796 /* If we can find a section in FILENAME with BFD index INDEX, adjust
797    it to ADDRESS.  */
798
799 void
800 exec_set_section_address (const char *filename, int index, CORE_ADDR address)
801 {
802   struct target_section *p;
803   struct target_section_table *table;
804
805   table = current_target_sections;
806   for (p = table->sections; p < table->sections_end; p++)
807     {
808       if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
809           && index == p->the_bfd_section->index)
810         {
811           p->endaddr += address - p->addr;
812           p->addr = address;
813         }
814     }
815 }
816
817 /* If mourn is being called in all the right places, this could be say
818    `gdb internal error' (since generic_mourn calls
819    breakpoint_init_inferior).  */
820
821 static int
822 ignore (struct target_ops *ops, struct gdbarch *gdbarch,
823         struct bp_target_info *bp_tgt)
824 {
825   return 0;
826 }
827
828 static int
829 exec_has_memory (struct target_ops *ops)
830 {
831   /* We can provide memory if we have any file/target sections to read
832      from.  */
833   return (current_target_sections->sections
834           != current_target_sections->sections_end);
835 }
836
837 /* Find mapped memory.  */
838
839 extern void
840 exec_set_find_memory_regions (int (*func) (find_memory_region_ftype, void *))
841 {
842   exec_do_find_memory_regions = func;
843 }
844
845 static int
846 exec_find_memory_regions (struct target_ops *self,
847                           find_memory_region_ftype func, void *data)
848 {
849   return exec_do_find_memory_regions (func, data);
850 }
851
852 static char *exec_make_note_section (struct target_ops *self, bfd *, int *);
853
854 /* Fill in the exec file target vector.  Very few entries need to be
855    defined.  */
856
857 static void
858 init_exec_ops (void)
859 {
860   exec_ops.to_shortname = "exec";
861   exec_ops.to_longname = "Local exec file";
862   exec_ops.to_doc = "Use an executable file as a target.\n\
863 Specify the filename of the executable file.";
864   exec_ops.to_open = exec_open;
865   exec_ops.to_close = exec_close_1;
866   exec_ops.to_attach = find_default_attach;
867   exec_ops.to_xfer_partial = exec_xfer_partial;
868   exec_ops.to_get_section_table = exec_get_section_table;
869   exec_ops.to_files_info = exec_files_info;
870   exec_ops.to_insert_breakpoint = ignore;
871   exec_ops.to_remove_breakpoint = ignore;
872   exec_ops.to_create_inferior = find_default_create_inferior;
873   exec_ops.to_stratum = file_stratum;
874   exec_ops.to_has_memory = exec_has_memory;
875   exec_ops.to_make_corefile_notes = exec_make_note_section;
876   exec_ops.to_find_memory_regions = exec_find_memory_regions;
877   exec_ops.to_magic = OPS_MAGIC;
878 }
879
880 void
881 _initialize_exec (void)
882 {
883   struct cmd_list_element *c;
884
885   init_exec_ops ();
886
887   if (!dbx_commands)
888     {
889       c = add_cmd ("file", class_files, file_command, _("\
890 Use FILE as program to be debugged.\n\
891 It is read for its symbols, for getting the contents of pure memory,\n\
892 and it is the program executed when you use the `run' command.\n\
893 If FILE cannot be found as specified, your execution directory path\n\
894 ($PATH) is searched for a command of that name.\n\
895 No arg means to have no executable file and no symbols."), &cmdlist);
896       set_cmd_completer (c, filename_completer);
897     }
898
899   c = add_cmd ("exec-file", class_files, exec_file_command, _("\
900 Use FILE as program for getting contents of pure memory.\n\
901 If FILE cannot be found as specified, your execution directory path\n\
902 is searched for a command of that name.\n\
903 No arg means have no executable file."), &cmdlist);
904   set_cmd_completer (c, filename_completer);
905
906   add_com ("section", class_files, set_section_command, _("\
907 Change the base address of section SECTION of the exec file to ADDR.\n\
908 This can be used if the exec file does not contain section addresses,\n\
909 (such as in the a.out format), or when the addresses specified in the\n\
910 file itself are wrong.  Each section must be changed separately.  The\n\
911 ``info files'' command lists all the sections and their addresses."));
912
913   add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
914 Set writing into executable and core files."), _("\
915 Show writing into executable and core files."), NULL,
916                            NULL,
917                            show_write_files,
918                            &setlist, &showlist);
919
920   add_target_with_completer (&exec_ops, filename_completer);
921 }
922
923 static char *
924 exec_make_note_section (struct target_ops *self, bfd *obfd, int *note_size)
925 {
926   error (_("Can't create a corefile"));
927 }