1 /* Work with executable files, for GDB.
2 Copyright 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
31 #include <sys/param.h>
43 /* Prototypes for local functions */
46 add_to_section_table
PARAMS ((bfd
*, sec_ptr
, PTR
));
49 exec_close
PARAMS ((int));
52 file_command
PARAMS ((char *, int));
55 set_section_command
PARAMS ((char *, int));
58 exec_files_info
PARAMS ((struct target_ops
*));
60 extern int info_verbose
;
62 /* The Binary File Descriptor handle for the executable file. */
66 /* Whether to open exec and core files read-only or read-write. */
70 /* Text start and end addresses (KLUDGE) if needed */
72 #ifdef NEED_TEXT_START_END
73 CORE_ADDR text_start
= 0;
74 CORE_ADDR text_end
= 0;
79 extern struct target_ops exec_ops
;
87 char *name
= bfd_get_filename (exec_bfd
);
92 if (exec_ops
.to_sections
) {
93 free ((PTR
)exec_ops
.to_sections
);
94 exec_ops
.to_sections
= NULL
;
95 exec_ops
.to_sections_end
= NULL
;
99 /* Process the first arg in ARGS as the new exec file.
101 Note that we have to explicitly ignore additional args, since we can
102 be called from file_command(), which also calls symbol_file_command()
103 which can take multiple args. */
106 exec_file_command (args
, from_tty
)
113 target_preopen (from_tty
);
115 /* Remove any previous exec file. */
116 unpush_target (&exec_ops
);
118 /* Now open and digest the file the user requested, if any. */
122 char *scratch_pathname
;
125 /* Scan through the args and pick up the first non option arg
128 if ((argv
= buildargv (args
)) == NULL
)
132 make_cleanup (freeargv
, (char *) argv
);
134 for (; (*argv
!= NULL
) && (**argv
== '-'); argv
++) {;}
137 error ("no exec file name was specified");
140 filename
= tilde_expand (*argv
);
141 make_cleanup (free
, filename
);
143 scratch_chan
= openp (getenv ("PATH"), 1, filename
,
144 write_files
? O_RDWR
|O_BINARY
: O_RDONLY
|O_BINARY
, 0,
146 if (scratch_chan
< 0)
147 perror_with_name (filename
);
149 exec_bfd
= bfd_fdopenr (scratch_pathname
, gnutarget
, scratch_chan
);
151 error ("Could not open `%s' as an executable file: %s",
152 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
153 if (!bfd_check_format (exec_bfd
, bfd_object
))
155 /* Make sure to close exec_bfd, or else "run" might try to use
158 error ("\"%s\": not in executable format: %s.",
159 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
162 if (build_section_table (exec_bfd
, &exec_ops
.to_sections
,
163 &exec_ops
.to_sections_end
))
165 /* Make sure to close exec_bfd, or else "run" might try to use
168 error ("Can't find the file sections in `%s': %s",
169 exec_bfd
->filename
, bfd_errmsg (bfd_get_error ()));
172 #ifdef NEED_TEXT_START_END
174 /* text_end is sometimes used for where to put call dummies. A
175 few ports use these for other purposes too. */
178 struct section_table
*p
;
180 /* Set text_start to the lowest address of the start of any
181 readonly code section and set text_end to the highest
182 address of the end of any readonly code section. */
184 text_start
= ~(CORE_ADDR
)0;
185 text_end
= (CORE_ADDR
)0;
186 for (p
= exec_ops
.to_sections
; p
< exec_ops
.to_sections_end
; p
++)
187 if (bfd_get_section_flags (p
->bfd
, p
->the_bfd_section
)
188 & (SEC_CODE
| SEC_READONLY
))
190 if (text_start
> p
->addr
)
191 text_start
= p
->addr
;
192 if (text_end
< p
->endaddr
)
193 text_end
= p
->endaddr
;
200 set_endian_from_file (exec_bfd
);
202 push_target (&exec_ops
);
204 /* Tell display code (if any) about the changed file name. */
205 if (exec_file_display_hook
)
206 (*exec_file_display_hook
) (filename
);
209 printf_unfiltered ("No exec file now.\n");
212 /* Set both the exec file and the symbol file, in one command.
213 What a novelty. Why did GDB go through four major releases before this
214 command was added? */
217 file_command (arg
, from_tty
)
221 /* FIXME, if we lose on reading the symbol file, we should revert
222 the exec file, but that's rough. */
223 exec_file_command (arg
, from_tty
);
224 symbol_file_command (arg
, from_tty
);
228 /* Locate all mappable sections of a BFD file.
229 table_pp_char is a char * to get it through bfd_map_over_sections;
230 we cast it back to its proper type. */
233 add_to_section_table (abfd
, asect
, table_pp_char
)
238 struct section_table
**table_pp
= (struct section_table
**)table_pp_char
;
241 aflag
= bfd_get_section_flags (abfd
, asect
);
242 if (!(aflag
& SEC_ALLOC
))
244 if (0 == bfd_section_size (abfd
, asect
))
246 (*table_pp
)->bfd
= abfd
;
247 (*table_pp
)->the_bfd_section
= asect
;
248 (*table_pp
)->addr
= bfd_section_vma (abfd
, asect
);
249 (*table_pp
)->endaddr
= (*table_pp
)->addr
+ bfd_section_size (abfd
, asect
);
253 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
254 Returns 0 if OK, 1 on error. */
257 build_section_table (some_bfd
, start
, end
)
259 struct section_table
**start
, **end
;
263 count
= bfd_count_sections (some_bfd
);
266 *start
= (struct section_table
*) xmalloc (count
* sizeof (**start
));
268 bfd_map_over_sections (some_bfd
, add_to_section_table
, (char *)end
);
269 if (*end
> *start
+ count
)
271 /* We could realloc the table, but it probably loses for most files. */
275 /* Read or write the exec file.
277 Args are address within a BFD file, address within gdb address-space,
278 length, and a flag indicating whether to read or write.
282 0: We cannot handle this address and length.
283 > 0: We have handled N bytes starting at this address.
284 (If N == length, we did it all.) We might be able
285 to handle more bytes beyond this length, but no
287 < 0: We cannot handle this address, but if somebody
288 else handles (-N) bytes, we can start from there.
290 The same routine is used to handle both core and exec files;
291 we just tail-call it with more arguments to select between them. */
294 xfer_memory (memaddr
, myaddr
, len
, write
, target
)
299 struct target_ops
*target
;
302 struct section_table
*p
;
303 CORE_ADDR nextsectaddr
, memend
;
304 boolean (*xfer_fn
) PARAMS ((bfd
*, sec_ptr
, PTR
, file_ptr
, bfd_size_type
));
309 memend
= memaddr
+ len
;
310 xfer_fn
= write
? bfd_set_section_contents
: bfd_get_section_contents
;
311 nextsectaddr
= memend
;
313 for (p
= target
->to_sections
; p
< target
->to_sections_end
; p
++)
315 if (p
->addr
<= memaddr
)
316 if (p
->endaddr
>= memend
)
318 /* Entire transfer is within this section. */
319 res
= xfer_fn (p
->bfd
, p
->the_bfd_section
, myaddr
, memaddr
- p
->addr
, len
);
320 return (res
!= 0) ? len
: 0;
322 else if (p
->endaddr
<= memaddr
)
324 /* This section ends before the transfer starts. */
329 /* This section overlaps the transfer. Just do half. */
330 len
= p
->endaddr
- memaddr
;
331 res
= xfer_fn (p
->bfd
, p
->the_bfd_section
, myaddr
, memaddr
- p
->addr
, len
);
332 return (res
!= 0) ? len
: 0;
334 else if (p
->addr
< nextsectaddr
)
335 nextsectaddr
= p
->addr
;
338 if (nextsectaddr
>= memend
)
339 return 0; /* We can't help */
341 return - (nextsectaddr
- memaddr
); /* Next boundary where we can help */
345 #ifdef REG_STACK_SEGMENT
347 /* Pyramids and AM29000s have an extra segment in the virtual address space
348 for the (control) stack of register-window frames. The AM29000 folk
349 call it the "register stack" rather than the "memory stack". */
350 else if (memaddr
>= reg_stack_start
&& memaddr
< reg_stack_end
)
352 i
= min (len
, reg_stack_end
- memaddr
);
353 fileptr
= memaddr
- reg_stack_start
+ reg_stack_offset
;
354 wanna_xfer
= coredata
;
356 #endif /* REG_STACK_SEGMENT */
360 print_section_info (t
, abfd
)
361 struct target_ops
*t
;
364 struct section_table
*p
;
366 printf_filtered ("\t`%s', ", bfd_get_filename(abfd
));
368 printf_filtered ("file type %s.\n", bfd_get_target(abfd
));
369 if (abfd
== exec_bfd
)
371 printf_filtered ("\tEntry point: ");
372 print_address_numeric (bfd_get_start_address (abfd
), 1, gdb_stdout
);
373 printf_filtered ("\n");
375 for (p
= t
->to_sections
; p
< t
->to_sections_end
; p
++)
377 /* FIXME-32x64 need a print_address_numeric with field width */
378 printf_filtered ("\t%s", local_hex_string_custom ((unsigned long) p
->addr
, "08l"));
379 printf_filtered (" - %s", local_hex_string_custom ((unsigned long) p
->endaddr
, "08l"));
381 printf_filtered (" @ %s",
382 local_hex_string_custom ((unsigned long) p
->the_bfd_section
->filepos
, "08l"));
383 printf_filtered (" is %s", bfd_section_name (p
->bfd
, p
->the_bfd_section
));
386 printf_filtered (" in %s", bfd_get_filename (p
->bfd
));
388 printf_filtered ("\n");
394 struct target_ops
*t
;
396 print_section_info (t
, exec_bfd
);
400 set_section_command (args
, from_tty
)
404 struct section_table
*p
;
407 unsigned long secaddr
;
412 error ("Must specify section name and its virtual address");
414 /* Parse out section name */
415 for (secname
= args
; !isspace(*args
); args
++) ;
416 seclen
= args
- secname
;
418 /* Parse out new virtual address */
419 secaddr
= parse_and_eval_address (args
);
421 for (p
= exec_ops
.to_sections
; p
< exec_ops
.to_sections_end
; p
++) {
422 if (!strncmp (secname
, bfd_section_name (exec_bfd
, p
->the_bfd_section
), seclen
)
423 && bfd_section_name (exec_bfd
, p
->the_bfd_section
)[seclen
] == '\0') {
424 offset
= secaddr
- p
->addr
;
426 p
->endaddr
+= offset
;
428 exec_files_info(&exec_ops
);
432 if (seclen
>= sizeof (secprint
))
433 seclen
= sizeof (secprint
) - 1;
434 strncpy (secprint
, secname
, seclen
);
435 secprint
[seclen
] = '\0';
436 error ("Section %s not found", secprint
);
439 /* If mourn is being called in all the right places, this could be say
440 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */
443 ignore (addr
, contents
)
450 struct target_ops exec_ops
= {
451 "exec", "Local exec file",
452 "Use an executable file as a target.\n\
453 Specify the filename of the executable file.",
454 exec_file_command
, exec_close
, /* open, close */
455 find_default_attach
, 0, 0, 0, /* attach, detach, resume, wait, */
456 0, 0, /* fetch_registers, store_registers, */
457 0, /* prepare_to_store, */
458 xfer_memory
, exec_files_info
,
459 ignore
, ignore
, /* insert_breakpoint, remove_breakpoint, */
460 0, 0, 0, 0, 0, /* terminal stuff */
461 0, 0, /* kill, load */
463 find_default_create_inferior
,
464 0, /* mourn_inferior */
466 0, /* notice_signals */
467 file_stratum
, 0, /* next */
468 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
469 0, 0, /* section pointers */
470 OPS_MAGIC
, /* Always the last thing */
476 struct cmd_list_element
*c
;
478 c
= add_cmd ("file", class_files
, file_command
,
479 "Use FILE as program to be debugged.\n\
480 It is read for its symbols, for getting the contents of pure memory,\n\
481 and it is the program executed when you use the `run' command.\n\
482 If FILE cannot be found as specified, your execution directory path\n\
483 ($PATH) is searched for a command of that name.\n\
484 No arg means to have no executable file and no symbols.", &cmdlist
);
485 c
->completer
= filename_completer
;
487 c
= add_cmd ("exec-file", class_files
, exec_file_command
,
488 "Use FILE as program for getting contents of pure memory.\n\
489 If FILE cannot be found as specified, your execution directory path\n\
490 is searched for a command of that name.\n\
491 No arg means have no executable file.", &cmdlist
);
492 c
->completer
= filename_completer
;
494 add_com ("section", class_files
, set_section_command
,
495 "Change the base address of section SECTION of the exec file to ADDR.\n\
496 This can be used if the exec file does not contain section addresses,\n\
497 (such as in the a.out format), or when the addresses specified in the\n\
498 file itself are wrong. Each section must be changed separately. The\n\
499 ``info files'' command lists all the sections and their addresses.");
502 (add_set_cmd ("write", class_support
, var_boolean
, (char *)&write_files
,
503 "Set writing into executable and core files.",
507 add_target (&exec_ops
);