* externalize a function
[binutils-gdb.git] / gdb / exec.c
blob511977f57031deedf2fa448fd760ce68cf05a005
1 /* Work with executable files, for GDB.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "defs.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "target.h"
27 #include "gdbcmd.h"
28 #include "language.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "completer.h"
33 #ifdef USG
34 #include <sys/types.h>
35 #endif
37 #include <fcntl.h>
38 #include "gdb_string.h"
40 #include "gdbcore.h"
42 #include <ctype.h>
43 #include "gdb_stat.h"
44 #ifndef O_BINARY
45 #define O_BINARY 0
46 #endif
48 #include "xcoffsolib.h"
50 struct vmap *map_vmap (bfd *, bfd *);
52 void (*file_changed_hook) (char *);
54 /* Prototypes for local functions */
56 static void add_to_section_table (bfd *, sec_ptr, PTR);
58 static void exec_close (int);
60 static void file_command (char *, int);
62 static void set_section_command (char *, int);
64 static void exec_files_info (struct target_ops *);
66 static void bfdsec_to_vmap (bfd *, sec_ptr, PTR);
68 static int ignore (CORE_ADDR, char *);
70 static void init_exec_ops (void);
72 void _initialize_exec (void);
74 extern int info_verbose;
76 /* The target vector for executable files. */
78 struct target_ops exec_ops;
80 /* The Binary File Descriptor handle for the executable file. */
82 bfd *exec_bfd = NULL;
84 /* Whether to open exec and core files read-only or read-write. */
86 int write_files = 0;
88 /* Text start and end addresses (KLUDGE) if needed */
90 #ifndef NEED_TEXT_START_END
91 #define NEED_TEXT_START_END (0)
92 #endif
93 CORE_ADDR text_start = 0;
94 CORE_ADDR text_end = 0;
96 struct vmap *vmap;
98 void
99 exec_open (char *args, int from_tty)
101 target_preopen (from_tty);
102 exec_file_attach (args, from_tty);
105 /* ARGSUSED */
106 static void
107 exec_close (int quitting)
109 int need_symtab_cleanup = 0;
110 struct vmap *vp, *nxt;
112 for (nxt = vmap; nxt != NULL;)
114 vp = nxt;
115 nxt = vp->nxt;
117 /* if there is an objfile associated with this bfd,
118 free_objfile() will do proper cleanup of objfile *and* bfd. */
120 if (vp->objfile)
122 free_objfile (vp->objfile);
123 need_symtab_cleanup = 1;
125 else if (vp->bfd != exec_bfd)
126 /* FIXME-leak: We should be freeing vp->name too, I think. */
127 if (!bfd_close (vp->bfd))
128 warning ("cannot close \"%s\": %s",
129 vp->name, bfd_errmsg (bfd_get_error ()));
131 /* FIXME: This routine is #if 0'd in symfile.c. What should we
132 be doing here? Should we just free everything in
133 vp->objfile->symtabs? Should free_objfile do that?
134 FIXME-as-well: free_objfile already free'd vp->name, so it isn't
135 valid here. */
136 free_named_symtabs (vp->name);
137 xfree (vp);
140 vmap = NULL;
142 if (exec_bfd)
144 char *name = bfd_get_filename (exec_bfd);
146 if (!bfd_close (exec_bfd))
147 warning ("cannot close \"%s\": %s",
148 name, bfd_errmsg (bfd_get_error ()));
149 xfree (name);
150 exec_bfd = NULL;
153 if (exec_ops.to_sections)
155 xfree (exec_ops.to_sections);
156 exec_ops.to_sections = NULL;
157 exec_ops.to_sections_end = NULL;
161 void
162 exec_file_clear (int from_tty)
164 /* Remove exec file. */
165 unpush_target (&exec_ops);
167 if (from_tty)
168 printf_unfiltered ("No executable file now.\n");
171 /* Process the first arg in ARGS as the new exec file.
173 This function is intended to be behave essentially the same
174 as exec_file_command, except that the latter will detect when
175 a target is being debugged, and will ask the user whether it
176 should be shut down first. (If the answer is "no", then the
177 new file is ignored.)
179 This file is used by exec_file_command, to do the work of opening
180 and processing the exec file after any prompting has happened.
182 And, it is used by child_attach, when the attach command was
183 given a pid but not a exec pathname, and the attach command could
184 figure out the pathname from the pid. (In this case, we shouldn't
185 ask the user whether the current target should be shut down --
186 we're supplying the exec pathname late for good reason.)
188 ARGS is assumed to be the filename. */
190 void
191 exec_file_attach (char *filename, int from_tty)
193 /* Remove any previous exec file. */
194 unpush_target (&exec_ops);
196 /* Now open and digest the file the user requested, if any. */
198 if (!filename)
200 if (from_tty)
201 printf_unfiltered ("No executable file now.\n");
203 else
205 char *scratch_pathname;
206 int scratch_chan;
208 scratch_chan = openp (getenv ("PATH"), 1, filename,
209 write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY, 0,
210 &scratch_pathname);
211 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
212 if (scratch_chan < 0)
214 char *exename = alloca (strlen (filename) + 5);
215 strcat (strcpy (exename, filename), ".exe");
216 scratch_chan = openp (getenv ("PATH"), 1, exename, write_files ?
217 O_RDWR | O_BINARY : O_RDONLY | O_BINARY, 0, &scratch_pathname);
219 #endif
220 if (scratch_chan < 0)
221 perror_with_name (filename);
222 exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
224 if (!exec_bfd)
225 error ("\"%s\": could not open as an executable file: %s",
226 scratch_pathname, bfd_errmsg (bfd_get_error ()));
228 /* At this point, scratch_pathname and exec_bfd->name both point to the
229 same malloc'd string. However exec_close() will attempt to free it
230 via the exec_bfd->name pointer, so we need to make another copy and
231 leave exec_bfd as the new owner of the original copy. */
232 scratch_pathname = xstrdup (scratch_pathname);
233 make_cleanup (xfree, scratch_pathname);
235 if (!bfd_check_format (exec_bfd, bfd_object))
237 /* Make sure to close exec_bfd, or else "run" might try to use
238 it. */
239 exec_close (0);
240 error ("\"%s\": not in executable format: %s",
241 scratch_pathname, bfd_errmsg (bfd_get_error ()));
244 /* FIXME - This should only be run for RS6000, but the ifdef is a poor
245 way to accomplish. */
246 #ifdef IBM6000_TARGET
247 /* Setup initial vmap. */
249 map_vmap (exec_bfd, 0);
250 if (vmap == NULL)
252 /* Make sure to close exec_bfd, or else "run" might try to use
253 it. */
254 exec_close (0);
255 error ("\"%s\": can't find the file sections: %s",
256 scratch_pathname, bfd_errmsg (bfd_get_error ()));
258 #endif /* IBM6000_TARGET */
260 if (build_section_table (exec_bfd, &exec_ops.to_sections,
261 &exec_ops.to_sections_end))
263 /* Make sure to close exec_bfd, or else "run" might try to use
264 it. */
265 exec_close (0);
266 error ("\"%s\": can't find the file sections: %s",
267 scratch_pathname, bfd_errmsg (bfd_get_error ()));
270 /* text_end is sometimes used for where to put call dummies. A
271 few ports use these for other purposes too. */
272 if (NEED_TEXT_START_END)
274 struct section_table *p;
276 /* Set text_start to the lowest address of the start of any
277 readonly code section and set text_end to the highest
278 address of the end of any readonly code section. */
279 /* FIXME: The comment above does not match the code. The
280 code checks for sections with are either code *or*
281 readonly. */
282 text_start = ~(CORE_ADDR) 0;
283 text_end = (CORE_ADDR) 0;
284 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
285 if (bfd_get_section_flags (p->bfd, p->the_bfd_section)
286 & (SEC_CODE | SEC_READONLY))
288 if (text_start > p->addr)
289 text_start = p->addr;
290 if (text_end < p->endaddr)
291 text_end = p->endaddr;
295 validate_files ();
297 set_gdbarch_from_file (exec_bfd);
299 push_target (&exec_ops);
301 /* Tell display code (if any) about the changed file name. */
302 if (exec_file_display_hook)
303 (*exec_file_display_hook) (filename);
307 /* Process the first arg in ARGS as the new exec file.
309 Note that we have to explicitly ignore additional args, since we can
310 be called from file_command(), which also calls symbol_file_command()
311 which can take multiple args.
313 If ARGS is NULL, we just want to close the exec file. */
315 static void
316 exec_file_command (char *args, int from_tty)
318 char **argv;
319 char *filename;
321 target_preopen (from_tty);
323 if (args)
325 /* Scan through the args and pick up the first non option arg
326 as the filename. */
328 argv = buildargv (args);
329 if (argv == NULL)
330 nomem (0);
332 make_cleanup_freeargv (argv);
334 for (; (*argv != NULL) && (**argv == '-'); argv++)
337 if (*argv == NULL)
338 error ("No executable file name was specified");
340 filename = tilde_expand (*argv);
341 make_cleanup (xfree, filename);
342 exec_file_attach (filename, from_tty);
344 else
345 exec_file_attach (NULL, from_tty);
348 /* Set both the exec file and the symbol file, in one command.
349 What a novelty. Why did GDB go through four major releases before this
350 command was added? */
352 static void
353 file_command (char *arg, int from_tty)
355 /* FIXME, if we lose on reading the symbol file, we should revert
356 the exec file, but that's rough. */
357 exec_file_command (arg, from_tty);
358 symbol_file_command (arg, from_tty);
359 if (file_changed_hook)
360 file_changed_hook (arg);
364 /* Locate all mappable sections of a BFD file.
365 table_pp_char is a char * to get it through bfd_map_over_sections;
366 we cast it back to its proper type. */
368 static void
369 add_to_section_table (bfd *abfd, sec_ptr asect, PTR table_pp_char)
371 struct section_table **table_pp = (struct section_table **) table_pp_char;
372 flagword aflag;
374 aflag = bfd_get_section_flags (abfd, asect);
375 if (!(aflag & SEC_ALLOC))
376 return;
377 if (0 == bfd_section_size (abfd, asect))
378 return;
379 (*table_pp)->bfd = abfd;
380 (*table_pp)->the_bfd_section = asect;
381 (*table_pp)->addr = bfd_section_vma (abfd, asect);
382 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
383 (*table_pp)++;
386 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
387 Returns 0 if OK, 1 on error. */
390 build_section_table (bfd *some_bfd, struct section_table **start,
391 struct section_table **end)
393 unsigned count;
395 count = bfd_count_sections (some_bfd);
396 if (*start)
397 xfree (* start);
398 *start = (struct section_table *) xmalloc (count * sizeof (**start));
399 *end = *start;
400 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
401 if (*end > *start + count)
402 internal_error (__FILE__, __LINE__, "failed internal consistency check");
403 /* We could realloc the table, but it probably loses for most files. */
404 return 0;
407 static void
408 bfdsec_to_vmap (bfd *abfd, sec_ptr sect, PTR arg3)
410 struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
411 struct vmap *vp;
413 vp = vmap_bfd->pvmap;
415 if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0)
416 return;
418 if (STREQ (bfd_section_name (abfd, sect), ".text"))
420 vp->tstart = bfd_section_vma (abfd, sect);
421 vp->tend = vp->tstart + bfd_section_size (abfd, sect);
422 vp->tvma = bfd_section_vma (abfd, sect);
423 vp->toffs = sect->filepos;
425 else if (STREQ (bfd_section_name (abfd, sect), ".data"))
427 vp->dstart = bfd_section_vma (abfd, sect);
428 vp->dend = vp->dstart + bfd_section_size (abfd, sect);
429 vp->dvma = bfd_section_vma (abfd, sect);
431 /* Silently ignore other types of sections. (FIXME?) */
434 /* Make a vmap for ABFD which might be a member of the archive ARCH.
435 Return the new vmap. */
437 struct vmap *
438 map_vmap (bfd *abfd, bfd *arch)
440 struct vmap_and_bfd vmap_bfd;
441 struct vmap *vp, **vpp;
443 vp = (struct vmap *) xmalloc (sizeof (*vp));
444 memset ((char *) vp, '\0', sizeof (*vp));
445 vp->nxt = 0;
446 vp->bfd = abfd;
447 vp->name = bfd_get_filename (arch ? arch : abfd);
448 vp->member = arch ? bfd_get_filename (abfd) : "";
450 vmap_bfd.pbfd = arch;
451 vmap_bfd.pvmap = vp;
452 bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd);
454 /* Find the end of the list and append. */
455 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
457 *vpp = vp;
459 return vp;
462 /* Read or write the exec file.
464 Args are address within a BFD file, address within gdb address-space,
465 length, and a flag indicating whether to read or write.
467 Result is a length:
469 0: We cannot handle this address and length.
470 > 0: We have handled N bytes starting at this address.
471 (If N == length, we did it all.) We might be able
472 to handle more bytes beyond this length, but no
473 promises.
474 < 0: We cannot handle this address, but if somebody
475 else handles (-N) bytes, we can start from there.
477 The same routine is used to handle both core and exec files;
478 we just tail-call it with more arguments to select between them. */
481 xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
482 struct mem_attrib *attrib,
483 struct target_ops *target)
485 boolean res;
486 struct section_table *p;
487 CORE_ADDR nextsectaddr, memend;
488 boolean (*xfer_fn) (bfd *, sec_ptr, PTR, file_ptr, bfd_size_type);
489 asection *section = NULL;
491 if (len <= 0)
492 internal_error (__FILE__, __LINE__, "failed internal consistency check");
494 if (overlay_debugging)
496 section = find_pc_overlay (memaddr);
497 if (pc_in_unmapped_range (memaddr, section))
498 memaddr = overlay_mapped_address (memaddr, section);
501 memend = memaddr + len;
502 xfer_fn = write ? bfd_set_section_contents : bfd_get_section_contents;
503 nextsectaddr = memend;
505 for (p = target->to_sections; p < target->to_sections_end; p++)
507 if (overlay_debugging && section && p->the_bfd_section &&
508 strcmp (section->name, p->the_bfd_section->name) != 0)
509 continue; /* not the section we need */
510 if (memaddr >= p->addr)
511 if (memend <= p->endaddr)
513 /* Entire transfer is within this section. */
514 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr,
515 memaddr - p->addr, len);
516 return (res != 0) ? len : 0;
518 else if (memaddr >= p->endaddr)
520 /* This section ends before the transfer starts. */
521 continue;
523 else
525 /* This section overlaps the transfer. Just do half. */
526 len = p->endaddr - memaddr;
527 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr,
528 memaddr - p->addr, len);
529 return (res != 0) ? len : 0;
531 else
532 nextsectaddr = min (nextsectaddr, p->addr);
535 if (nextsectaddr >= memend)
536 return 0; /* We can't help */
537 else
538 return -(nextsectaddr - memaddr); /* Next boundary where we can help */
542 void
543 print_section_info (struct target_ops *t, bfd *abfd)
545 struct section_table *p;
547 printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
548 wrap_here (" ");
549 printf_filtered ("file type %s.\n", bfd_get_target (abfd));
550 if (abfd == exec_bfd)
552 printf_filtered ("\tEntry point: ");
553 print_address_numeric (bfd_get_start_address (abfd), 1, gdb_stdout);
554 printf_filtered ("\n");
556 for (p = t->to_sections; p < t->to_sections_end; p++)
558 /* FIXME-32x64 need a print_address_numeric with field width */
559 printf_filtered ("\t%s", local_hex_string_custom ((unsigned long) p->addr, "08l"));
560 printf_filtered (" - %s", local_hex_string_custom ((unsigned long) p->endaddr, "08l"));
561 if (info_verbose)
562 printf_filtered (" @ %s",
563 local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l"));
564 printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
565 if (p->bfd != abfd)
567 printf_filtered (" in %s", bfd_get_filename (p->bfd));
569 printf_filtered ("\n");
573 static void
574 exec_files_info (struct target_ops *t)
576 print_section_info (t, exec_bfd);
578 if (vmap)
580 struct vmap *vp;
582 printf_unfiltered ("\tMapping info for file `%s'.\n", vmap->name);
583 printf_unfiltered ("\t %*s %*s %*s %*s %8.8s %s\n",
584 strlen_paddr (), "tstart",
585 strlen_paddr (), "tend",
586 strlen_paddr (), "dstart",
587 strlen_paddr (), "dend",
588 "section",
589 "file(member)");
591 for (vp = vmap; vp; vp = vp->nxt)
592 printf_unfiltered ("\t0x%s 0x%s 0x%s 0x%s %s%s%s%s\n",
593 paddr (vp->tstart),
594 paddr (vp->tend),
595 paddr (vp->dstart),
596 paddr (vp->dend),
597 vp->name,
598 *vp->member ? "(" : "", vp->member,
599 *vp->member ? ")" : "");
603 /* msnyder 5/21/99:
604 exec_set_section_offsets sets the offsets of all the sections
605 in the exec objfile. */
607 void
608 exec_set_section_offsets (bfd_signed_vma text_off, bfd_signed_vma data_off,
609 bfd_signed_vma bss_off)
611 struct section_table *sect;
613 for (sect = exec_ops.to_sections;
614 sect < exec_ops.to_sections_end;
615 sect++)
617 flagword flags;
619 flags = bfd_get_section_flags (exec_bfd, sect->the_bfd_section);
621 if (flags & SEC_CODE)
623 sect->addr += text_off;
624 sect->endaddr += text_off;
626 else if (flags & (SEC_DATA | SEC_LOAD))
628 sect->addr += data_off;
629 sect->endaddr += data_off;
631 else if (flags & SEC_ALLOC)
633 sect->addr += bss_off;
634 sect->endaddr += bss_off;
639 static void
640 set_section_command (char *args, int from_tty)
642 struct section_table *p;
643 char *secname;
644 unsigned seclen;
645 unsigned long secaddr;
646 char secprint[100];
647 long offset;
649 if (args == 0)
650 error ("Must specify section name and its virtual address");
652 /* Parse out section name */
653 for (secname = args; !isspace (*args); args++);
654 seclen = args - secname;
656 /* Parse out new virtual address */
657 secaddr = parse_and_eval_address (args);
659 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
661 if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
662 && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0')
664 offset = secaddr - p->addr;
665 p->addr += offset;
666 p->endaddr += offset;
667 if (from_tty)
668 exec_files_info (&exec_ops);
669 return;
672 if (seclen >= sizeof (secprint))
673 seclen = sizeof (secprint) - 1;
674 strncpy (secprint, secname, seclen);
675 secprint[seclen] = '\0';
676 error ("Section %s not found", secprint);
679 /* If mourn is being called in all the right places, this could be say
680 `gdb internal error' (since generic_mourn calls
681 breakpoint_init_inferior). */
683 static int
684 ignore (CORE_ADDR addr, char *contents)
686 return 0;
689 /* Fill in the exec file target vector. Very few entries need to be
690 defined. */
692 void
693 init_exec_ops (void)
695 exec_ops.to_shortname = "exec";
696 exec_ops.to_longname = "Local exec file";
697 exec_ops.to_doc = "Use an executable file as a target.\n\
698 Specify the filename of the executable file.";
699 exec_ops.to_open = exec_open;
700 exec_ops.to_close = exec_close;
701 exec_ops.to_attach = find_default_attach;
702 exec_ops.to_require_attach = find_default_require_attach;
703 exec_ops.to_require_detach = find_default_require_detach;
704 exec_ops.to_xfer_memory = xfer_memory;
705 exec_ops.to_files_info = exec_files_info;
706 exec_ops.to_insert_breakpoint = ignore;
707 exec_ops.to_remove_breakpoint = ignore;
708 exec_ops.to_create_inferior = find_default_create_inferior;
709 exec_ops.to_clone_and_follow_inferior = find_default_clone_and_follow_inferior;
710 exec_ops.to_stratum = file_stratum;
711 exec_ops.to_has_memory = 1;
712 exec_ops.to_magic = OPS_MAGIC;
715 void
716 _initialize_exec (void)
718 struct cmd_list_element *c;
720 init_exec_ops ();
722 if (!dbx_commands)
724 c = add_cmd ("file", class_files, file_command,
725 "Use FILE as program to be debugged.\n\
726 It is read for its symbols, for getting the contents of pure memory,\n\
727 and it is the program executed when you use the `run' command.\n\
728 If FILE cannot be found as specified, your execution directory path\n\
729 ($PATH) is searched for a command of that name.\n\
730 No arg means to have no executable file and no symbols.", &cmdlist);
731 c->completer = filename_completer;
734 c = add_cmd ("exec-file", class_files, exec_file_command,
735 "Use FILE as program for getting contents of pure memory.\n\
736 If FILE cannot be found as specified, your execution directory path\n\
737 is searched for a command of that name.\n\
738 No arg means have no executable file.", &cmdlist);
739 c->completer = filename_completer;
741 add_com ("section", class_files, set_section_command,
742 "Change the base address of section SECTION of the exec file to ADDR.\n\
743 This can be used if the exec file does not contain section addresses,\n\
744 (such as in the a.out format), or when the addresses specified in the\n\
745 file itself are wrong. Each section must be changed separately. The\n\
746 ``info files'' command lists all the sections and their addresses.");
748 add_show_from_set
749 (add_set_cmd ("write", class_support, var_boolean, (char *) &write_files,
750 "Set writing into executable and core files.",
751 &setlist),
752 &showlist);
754 add_target (&exec_ops);