1 /* Work with executable files, for GDB.
2 Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1997, 1998
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 #include <sys/types.h>
35 #include "gdb_string.h"
45 #include "xcoffsolib.h"
47 struct vmap
*map_vmap
PARAMS ((bfd
*, bfd
*));
49 void (*file_changed_hook
) PARAMS ((char *));
51 /* Prototypes for local functions */
53 static void add_to_section_table
PARAMS ((bfd
*, sec_ptr
, PTR
));
55 static void exec_close
PARAMS ((int));
57 static void file_command
PARAMS ((char *, int));
59 static void set_section_command
PARAMS ((char *, int));
61 static void exec_files_info
PARAMS ((struct target_ops
*));
63 static void bfdsec_to_vmap
PARAMS ((bfd
*, sec_ptr
, PTR
));
65 static int ignore
PARAMS ((CORE_ADDR
, char *));
67 extern int info_verbose
;
69 /* The Binary File Descriptor handle for the executable file. */
73 /* Whether to open exec and core files read-only or read-write. */
77 /* Text start and end addresses (KLUDGE) if needed */
79 #ifndef NEED_TEXT_START_END
80 #define NEED_TEXT_START_END (0)
82 CORE_ADDR text_start
= 0;
83 CORE_ADDR text_end
= 0;
89 extern struct target_ops exec_ops
;
96 int need_symtab_cleanup
= 0;
97 struct vmap
*vp
, *nxt
;
99 for (nxt
= vmap
; nxt
!= NULL
; )
104 /* if there is an objfile associated with this bfd,
105 free_objfile() will do proper cleanup of objfile *and* bfd. */
109 free_objfile (vp
->objfile
);
110 need_symtab_cleanup
= 1;
112 else if (vp
->bfd
!= exec_bfd
)
113 /* FIXME-leak: We should be freeing vp->name too, I think. */
114 if (!bfd_close (vp
->bfd
))
115 warning ("cannot close \"%s\": %s",
116 vp
->name
, bfd_errmsg (bfd_get_error ()));
118 /* FIXME: This routine is #if 0'd in symfile.c. What should we
119 be doing here? Should we just free everything in
120 vp->objfile->symtabs? Should free_objfile do that?
121 FIXME-as-well: free_objfile already free'd vp->name, so it isn't
123 free_named_symtabs (vp
->name
);
131 char *name
= bfd_get_filename (exec_bfd
);
133 if (!bfd_close (exec_bfd
))
134 warning ("cannot close \"%s\": %s",
135 name
, bfd_errmsg (bfd_get_error ()));
140 if (exec_ops
.to_sections
)
142 free ((PTR
)exec_ops
.to_sections
);
143 exec_ops
.to_sections
= NULL
;
144 exec_ops
.to_sections_end
= NULL
;
148 /* Process the first arg in ARGS as the new exec file.
150 This function is intended to be behave essentially the same
151 as exec_file_command, except that the latter will detect when
152 a target is being debugged, and will ask the user whether it
153 should be shut down first. (If the answer is "no", then the
154 new file is ignored.)
156 This file is used by exec_file_command, to do the work of opening
157 and processing the exec file after any prompting has happened.
159 And, it is used by child_attach, when the attach command was
160 given a pid but not a exec pathname, and the attach command could
161 figure out the pathname from the pid. (In this case, we shouldn't
162 ask the user whether the current target should be shut down --
163 we're supplying the exec pathname late for good reason.) */
166 exec_file_attach (args
, from_tty
)
173 /* Remove any previous exec file. */
174 unpush_target (&exec_ops
);
176 /* Now open and digest the file the user requested, if any. */
180 char *scratch_pathname
;
183 /* Scan through the args and pick up the first non option arg
186 argv
= buildargv (args
);
190 make_cleanup ((make_cleanup_func
) freeargv
, (char *) argv
);
192 for (; (*argv
!= NULL
) && (**argv
== '-'); argv
++) {;}
194 error ("no exec file name was specified");
196 filename
= tilde_expand (*argv
);
197 make_cleanup (free
, filename
);
199 scratch_chan
= openp (getenv ("PATH"), 1, filename
,
200 write_files
? O_RDWR
|O_BINARY
: O_RDONLY
|O_BINARY
, 0,
202 #if defined(__GO32__) || defined(_WIN32)
203 if (scratch_chan
< 0)
205 char *exename
= alloca (strlen (filename
) + 5);
206 strcat (strcpy (exename
, filename
), ".exe");
207 scratch_chan
= openp (getenv ("PATH"), 1, exename
, write_files
?
208 O_RDWR
|O_BINARY
: O_RDONLY
|O_BINARY
, 0, &scratch_pathname
);
211 if (scratch_chan
< 0)
212 perror_with_name (filename
);
213 exec_bfd
= bfd_fdopenr (scratch_pathname
, gnutarget
, scratch_chan
);
216 error ("\"%s\": could not open as an executable file: %s",
217 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
219 /* At this point, scratch_pathname and exec_bfd->name both point to the
220 same malloc'd string. However exec_close() will attempt to free it
221 via the exec_bfd->name pointer, so we need to make another copy and
222 leave exec_bfd as the new owner of the original copy. */
223 scratch_pathname
= strdup (scratch_pathname
);
224 make_cleanup (free
, scratch_pathname
);
226 if (!bfd_check_format (exec_bfd
, bfd_object
))
228 /* Make sure to close exec_bfd, or else "run" might try to use
231 error ("\"%s\": not in executable format: %s",
232 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
235 /* FIXME - This should only be run for RS6000, but the ifdef is a poor
236 way to accomplish. */
237 #ifdef IBM6000_TARGET
238 /* Setup initial vmap. */
240 map_vmap (exec_bfd
, 0);
243 /* Make sure to close exec_bfd, or else "run" might try to use
246 error ("\"%s\": can't find the file sections: %s",
247 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
249 #endif /* IBM6000_TARGET */
251 if (build_section_table (exec_bfd
, &exec_ops
.to_sections
,
252 &exec_ops
.to_sections_end
))
254 /* Make sure to close exec_bfd, or else "run" might try to use
257 error ("\"%s\": can't find the file sections: %s",
258 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
261 /* text_end is sometimes used for where to put call dummies. A
262 few ports use these for other purposes too. */
263 if (NEED_TEXT_START_END
)
265 struct section_table
*p
;
267 /* Set text_start to the lowest address of the start of any
268 readonly code section and set text_end to the highest
269 address of the end of any readonly code section. */
270 /* FIXME: The comment above does not match the code. The
271 code checks for sections with are either code *or*
273 text_start
= ~(CORE_ADDR
)0;
274 text_end
= (CORE_ADDR
)0;
275 for (p
= exec_ops
.to_sections
; p
< exec_ops
.to_sections_end
; p
++)
276 if (bfd_get_section_flags (p
->bfd
, p
->the_bfd_section
)
277 & (SEC_CODE
| SEC_READONLY
))
279 if (text_start
> p
->addr
)
280 text_start
= p
->addr
;
281 if (text_end
< p
->endaddr
)
282 text_end
= p
->endaddr
;
288 set_gdbarch_from_file (exec_bfd
);
290 push_target (&exec_ops
);
292 /* Tell display code (if any) about the changed file name. */
293 if (exec_file_display_hook
)
294 (*exec_file_display_hook
) (filename
);
297 printf_unfiltered ("No executable file now.\n");
300 /* Process the first arg in ARGS as the new exec file.
302 Note that we have to explicitly ignore additional args, since we can
303 be called from file_command(), which also calls symbol_file_command()
304 which can take multiple args. */
307 exec_file_command (args
, from_tty
)
314 target_preopen (from_tty
);
316 exec_file_attach (args
, from_tty
);
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? */
324 file_command (arg
, from_tty
)
328 /* FIXME, if we lose on reading the symbol file, we should revert
329 the exec file, but that's rough. */
330 exec_file_command (arg
, from_tty
);
331 symbol_file_command (arg
, from_tty
);
332 if (file_changed_hook
)
333 file_changed_hook (arg
);
337 /* Locate all mappable sections of a BFD file.
338 table_pp_char is a char * to get it through bfd_map_over_sections;
339 we cast it back to its proper type. */
342 add_to_section_table (abfd
, asect
, table_pp_char
)
347 struct section_table
**table_pp
= (struct section_table
**)table_pp_char
;
350 aflag
= bfd_get_section_flags (abfd
, asect
);
351 if (!(aflag
& SEC_ALLOC
))
353 if (0 == bfd_section_size (abfd
, asect
))
355 (*table_pp
)->bfd
= abfd
;
356 (*table_pp
)->the_bfd_section
= asect
;
357 (*table_pp
)->addr
= bfd_section_vma (abfd
, asect
);
358 (*table_pp
)->endaddr
= (*table_pp
)->addr
+ bfd_section_size (abfd
, asect
);
362 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
363 Returns 0 if OK, 1 on error. */
366 build_section_table (some_bfd
, start
, end
)
368 struct section_table
**start
, **end
;
372 count
= bfd_count_sections (some_bfd
);
375 *start
= (struct section_table
*) xmalloc (count
* sizeof (**start
));
377 bfd_map_over_sections (some_bfd
, add_to_section_table
, (char *)end
);
378 if (*end
> *start
+ count
)
380 /* We could realloc the table, but it probably loses for most files. */
385 bfdsec_to_vmap(abfd
, sect
, arg3
)
390 struct vmap_and_bfd
*vmap_bfd
= (struct vmap_and_bfd
*) arg3
;
393 vp
= vmap_bfd
->pvmap
;
395 if ((bfd_get_section_flags (abfd
, sect
) & SEC_LOAD
) == 0)
398 if (STREQ (bfd_section_name (abfd
, sect
), ".text"))
400 vp
->tstart
= bfd_section_vma (abfd
, sect
);
401 vp
->tend
= vp
->tstart
+ bfd_section_size (abfd
, sect
);
402 vp
->tvma
= bfd_section_vma (abfd
, sect
);
403 vp
->toffs
= sect
->filepos
;
405 else if (STREQ (bfd_section_name (abfd
, sect
), ".data"))
407 vp
->dstart
= bfd_section_vma (abfd
, sect
);
408 vp
->dend
= vp
->dstart
+ bfd_section_size (abfd
, sect
);
409 vp
->dvma
= bfd_section_vma (abfd
, sect
);
411 /* Silently ignore other types of sections. (FIXME?) */
414 /* Make a vmap for ABFD which might be a member of the archive ARCH.
415 Return the new vmap. */
418 map_vmap (abfd
, arch
)
422 struct vmap_and_bfd vmap_bfd
;
423 struct vmap
*vp
, **vpp
;
425 vp
= (struct vmap
*) xmalloc (sizeof (*vp
));
426 memset ((char *) vp
, '\0', sizeof (*vp
));
429 vp
->name
= bfd_get_filename (arch
? arch
: abfd
);
430 vp
->member
= arch
? bfd_get_filename (abfd
) : "";
432 vmap_bfd
.pbfd
= arch
;
434 bfd_map_over_sections (abfd
, bfdsec_to_vmap
, &vmap_bfd
);
436 /* Find the end of the list and append. */
437 for (vpp
= &vmap
; *vpp
; vpp
= &(*vpp
)->nxt
)
444 /* Read or write the exec file.
446 Args are address within a BFD file, address within gdb address-space,
447 length, and a flag indicating whether to read or write.
451 0: We cannot handle this address and length.
452 > 0: We have handled N bytes starting at this address.
453 (If N == length, we did it all.) We might be able
454 to handle more bytes beyond this length, but no
456 < 0: We cannot handle this address, but if somebody
457 else handles (-N) bytes, we can start from there.
459 The same routine is used to handle both core and exec files;
460 we just tail-call it with more arguments to select between them. */
463 xfer_memory (memaddr
, myaddr
, len
, write
, target
)
468 struct target_ops
*target
;
471 struct section_table
*p
;
472 CORE_ADDR nextsectaddr
, memend
;
473 boolean (*xfer_fn
) PARAMS ((bfd
*, sec_ptr
, PTR
, file_ptr
, bfd_size_type
));
479 if (overlay_debugging
)
481 section
= find_pc_overlay (memaddr
);
482 if (pc_in_unmapped_range (memaddr
, section
))
483 memaddr
= overlay_mapped_address (memaddr
, section
);
486 memend
= memaddr
+ len
;
487 xfer_fn
= write
? bfd_set_section_contents
: bfd_get_section_contents
;
488 nextsectaddr
= memend
;
490 #if 0 /* Stu's implementation */
491 /* If a section has been specified, try to use it. Note that we cannot use the
492 specified section directly. This is because it usually comes from the
493 symbol file, which may be different from the exec or core file. Instead, we
494 have to lookup the specified section by name in the bfd associated with
497 if (target_memory_bfd_section
)
501 asection
*target_section
;
504 s
= target_memory_bfd_section
;
507 target_bfd
= target
->to_sections
->bfd
;
508 target_section
= bfd_get_section_by_name (target_bfd
, bfd_section_name (abfd
, s
));
513 bfd_size_type sec_size
;
515 sec_addr
= bfd_section_vma (target_bfd
, target_section
);
516 sec_size
= target_section
->_raw_size
;
518 /* Make sure the requested memory starts inside the section. */
520 if (memaddr
>= sec_addr
521 && memaddr
< sec_addr
+ sec_size
)
523 /* Cut back length in case request overflows the end of the section. */
524 len
= min (len
, sec_addr
+ sec_size
- memaddr
);
526 res
= xfer_fn (target_bfd
, target_section
, myaddr
, memaddr
- sec_addr
, len
);
528 return res
? len
: 0;
532 #endif /* 0, Stu's implementation */
533 for (p
= target
->to_sections
; p
< target
->to_sections_end
; p
++)
535 if (overlay_debugging
&& section
&& p
->the_bfd_section
&&
536 strcmp (section
->name
, p
->the_bfd_section
->name
) != 0)
537 continue; /* not the section we need */
538 if (memaddr
>= p
->addr
)
539 if (memend
<= p
->endaddr
)
541 /* Entire transfer is within this section. */
542 res
= xfer_fn (p
->bfd
, p
->the_bfd_section
, myaddr
,
543 memaddr
- p
->addr
, len
);
544 return (res
!= 0) ? len
: 0;
546 else if (memaddr
>= p
->endaddr
)
548 /* This section ends before the transfer starts. */
553 /* This section overlaps the transfer. Just do half. */
554 len
= p
->endaddr
- memaddr
;
555 res
= xfer_fn (p
->bfd
, p
->the_bfd_section
, myaddr
,
556 memaddr
- p
->addr
, len
);
557 return (res
!= 0) ? len
: 0;
560 nextsectaddr
= min (nextsectaddr
, p
->addr
);
563 if (nextsectaddr
>= memend
)
564 return 0; /* We can't help */
566 return - (nextsectaddr
- memaddr
); /* Next boundary where we can help */
570 #ifdef REG_STACK_SEGMENT
572 /* Pyramids and AM29000s have an extra segment in the virtual address space
573 for the (control) stack of register-window frames. The AM29000 folk
574 call it the "register stack" rather than the "memory stack". */
575 else if (memaddr
>= reg_stack_start
&& memaddr
< reg_stack_end
)
577 i
= min (len
, reg_stack_end
- memaddr
);
578 fileptr
= memaddr
- reg_stack_start
+ reg_stack_offset
;
579 wanna_xfer
= coredata
;
581 #endif /* REG_STACK_SEGMENT */
585 print_section_info (t
, abfd
)
586 struct target_ops
*t
;
589 struct section_table
*p
;
591 printf_filtered ("\t`%s', ", bfd_get_filename(abfd
));
593 printf_filtered ("file type %s.\n", bfd_get_target(abfd
));
594 if (abfd
== exec_bfd
)
596 printf_filtered ("\tEntry point: ");
597 print_address_numeric (bfd_get_start_address (abfd
), 1, gdb_stdout
);
598 printf_filtered ("\n");
600 for (p
= t
->to_sections
; p
< t
->to_sections_end
; p
++)
602 /* FIXME-32x64 need a print_address_numeric with field width */
603 printf_filtered ("\t%s", local_hex_string_custom ((unsigned long) p
->addr
, "08l"));
604 printf_filtered (" - %s", local_hex_string_custom ((unsigned long) p
->endaddr
, "08l"));
606 printf_filtered (" @ %s",
607 local_hex_string_custom ((unsigned long) p
->the_bfd_section
->filepos
, "08l"));
608 printf_filtered (" is %s", bfd_section_name (p
->bfd
, p
->the_bfd_section
));
611 printf_filtered (" in %s", bfd_get_filename (p
->bfd
));
613 printf_filtered ("\n");
619 struct target_ops
*t
;
621 print_section_info (t
, exec_bfd
);
627 printf_unfiltered ("\tMapping info for file `%s'.\n", vmap
->name
);
628 printf_unfiltered ("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n",
629 "tstart", "tend", "dstart", "dend", "section",
632 for (vp
= vmap
; vp
; vp
= vp
->nxt
)
633 printf_unfiltered ("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
634 vp
->tstart
, vp
->tend
, vp
->dstart
, vp
->dend
, vp
->name
,
635 *vp
->member
? "(" : "", vp
->member
,
636 *vp
->member
? ")" : "");
641 set_section_command (args
, from_tty
)
645 struct section_table
*p
;
648 unsigned long secaddr
;
653 error ("Must specify section name and its virtual address");
655 /* Parse out section name */
656 for (secname
= args
; !isspace(*args
); args
++) ;
657 seclen
= args
- secname
;
659 /* Parse out new virtual address */
660 secaddr
= parse_and_eval_address (args
);
662 for (p
= exec_ops
.to_sections
; p
< exec_ops
.to_sections_end
; p
++) {
663 if (!strncmp (secname
, bfd_section_name (exec_bfd
, p
->the_bfd_section
), seclen
)
664 && bfd_section_name (exec_bfd
, p
->the_bfd_section
)[seclen
] == '\0') {
665 offset
= secaddr
- p
->addr
;
667 p
->endaddr
+= offset
;
669 exec_files_info(&exec_ops
);
673 if (seclen
>= sizeof (secprint
))
674 seclen
= sizeof (secprint
) - 1;
675 strncpy (secprint
, secname
, seclen
);
676 secprint
[seclen
] = '\0';
677 error ("Section %s not found", secprint
);
680 /* If mourn is being called in all the right places, this could be say
681 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
684 ignore (addr
, contents
)
691 struct target_ops exec_ops
= {
692 "exec", /* to_shortname */
693 "Local exec file", /* to_longname */
694 "Use an executable file as a target.\n\
695 Specify the filename of the executable file.", /* to_doc */
696 exec_file_command
, /* to_open */
697 exec_close
, /* to_close */
698 find_default_attach
, /* to_attach */
699 NULL
, /* to_post_attach */
700 find_default_require_attach
, /* to_require_attach */
702 find_default_require_detach
, /* to_require_detach */
705 NULL
, /* to_post_wait */
706 0, /* to_fetch_registers */
707 0, /* to_store_registers */
708 0, /* to_prepare_to_store */
709 xfer_memory
, /* to_xfer_memory */
710 exec_files_info
, /* to_files_info */
711 ignore
, /* to_insert_breakpoint */
712 ignore
, /* to_remove_breakpoint */
713 0, /* to_terminal_init */
714 0, /* to_terminal_inferior */
715 0, /* to_terminal_ours_for_output */
716 0, /* to_terminal_ours */
717 0, /* to_terminal_info */
720 0, /* to_lookup_symbol */
721 find_default_create_inferior
, /* to_create_inferior */
722 NULL
, /* to_post_startup_inferior */
723 NULL
, /* to_acknowledge_created_inferior */
724 find_default_clone_and_follow_inferior
, /* to_clone_and_follow_inferior */
725 NULL
, /* to_post_follow_inferior_by_clone */
726 NULL
, /* to_insert_fork_catchpoint */
727 NULL
, /* to_remove_fork_catchpoint */
728 NULL
, /* to_insert_vfork_catchpoint */
729 NULL
, /* to_remove_vfork_catchpoint */
730 NULL
, /* to_has_forked */
731 NULL
, /* to_has_vforked */
732 NULL
, /* to_can_follow_vfork_prior_to_exec */
733 NULL
, /* to_post_follow_vfork */
734 NULL
, /* to_insert_exec_catchpoint */
735 NULL
, /* to_remove_exec_catchpoint */
736 NULL
, /* to_has_execd */
737 NULL
, /* to_reported_exec_events_per_exec_call */
738 NULL
, /* to_has_syscall_event */
739 NULL
, /* to_has_exited */
740 0, /* to_mourn_inferior */
742 0, /* to_notice_signals */
743 0, /* to_thread_alive */
745 NULL
, /* to_enable_exception_callback */
746 NULL
, /* to_get_current_exception_event */
747 NULL
, /* to_pid_to_exec_file */
748 NULL
, /* to_core_file_to_sym_file */
749 file_stratum
, /* to_stratum */
751 0, /* to_has_all_memory */
752 1, /* to_has_memory */
753 0, /* to_has_stack */
754 0, /* to_has_registers */
755 0, /* to_has_execution */
757 0, /* to_sections_end */
758 OPS_MAGIC
, /* to_magic */
764 struct cmd_list_element
*c
;
768 c
= add_cmd ("file", class_files
, file_command
,
769 "Use FILE as program to be debugged.\n\
770 It is read for its symbols, for getting the contents of pure memory,\n\
771 and it is the program executed when you use the `run' command.\n\
772 If FILE cannot be found as specified, your execution directory path\n\
773 ($PATH) is searched for a command of that name.\n\
774 No arg means to have no executable file and no symbols.", &cmdlist
);
775 c
->completer
= filename_completer
;
778 c
= add_cmd ("exec-file", class_files
, exec_file_command
,
779 "Use FILE as program for getting contents of pure memory.\n\
780 If FILE cannot be found as specified, your execution directory path\n\
781 is searched for a command of that name.\n\
782 No arg means have no executable file.", &cmdlist
);
783 c
->completer
= filename_completer
;
785 add_com ("section", class_files
, set_section_command
,
786 "Change the base address of section SECTION of the exec file to ADDR.\n\
787 This can be used if the exec file does not contain section addresses,\n\
788 (such as in the a.out format), or when the addresses specified in the\n\
789 file itself are wrong. Each section must be changed separately. The\n\
790 ``info files'' command lists all the sections and their addresses.");
793 (add_set_cmd ("write", class_support
, var_boolean
, (char *)&write_files
,
794 "Set writing into executable and core files.",
798 add_target (&exec_ops
);