1 /* Work with executable files, for GDB.
2 Copyright 1988, 1989, 1991, 1992 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. */
27 #include <sys/types.h>
30 #include <sys/param.h>
42 /* Prototypes for local functions */
45 add_to_section_table PARAMS ((bfd *, sec_ptr, PTR));
48 exec_close PARAMS ((int));
51 file_command PARAMS ((char *, int));
54 set_section_command PARAMS ((char *, int));
57 exec_files_info PARAMS ((struct target_ops *));
59 extern int info_verbose;
61 /* The Binary File Descriptor handle for the executable file. */
65 /* Whether to open exec and core files read-only or read-write. */
69 /* Text start and end addresses (KLUDGE) if needed */
71 #ifdef NEED_TEXT_START_END
72 CORE_ADDR text_start = 0;
73 CORE_ADDR text_end = 0;
78 extern struct target_ops exec_ops;
89 if (exec_ops.to_sections) {
90 free ((PTR)exec_ops.to_sections);
91 exec_ops.to_sections = NULL;
92 exec_ops.to_sections_end = NULL;
96 /* Process the first arg in ARGS as the new exec file.
98 Note that we have to explicitly ignore additional args, since we can
99 be called from file_command(), which also calls symbol_file_command()
100 which can take multiple args. */
103 exec_file_command (args, from_tty)
110 target_preopen (from_tty);
112 /* Remove any previous exec file. */
113 unpush_target (&exec_ops);
115 /* Now open and digest the file the user requested, if any. */
119 char *scratch_pathname;
122 /* Scan through the args and pick up the first non option arg
125 if ((argv = buildargv (args)) == NULL)
129 make_cleanup (freeargv, (char *) argv);
131 for (; (*argv != NULL) && (**argv == '-'); argv++) {;}
134 error ("no exec file name was specified");
137 filename = tilde_expand (*argv);
138 make_cleanup (free, filename);
140 scratch_chan = openp (getenv ("PATH"), 1, filename,
141 write_files? O_RDWR|O_BINARY: O_RDONLY|O_BINARY, 0,
143 if (scratch_chan < 0)
144 perror_with_name (filename);
146 exec_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
148 error ("Could not open `%s' as an executable file: %s",
149 scratch_pathname, bfd_errmsg (bfd_error));
150 if (!bfd_check_format (exec_bfd, bfd_object))
151 error ("\"%s\": not in executable format: %s.",
152 scratch_pathname, bfd_errmsg (bfd_error));
154 if (build_section_table (exec_bfd, &exec_ops.to_sections,
155 &exec_ops.to_sections_end))
156 error ("Can't find the file sections in `%s': %s",
157 exec_bfd->filename, bfd_errmsg (bfd_error));
159 #ifdef NEED_TEXT_START_END
160 /* This is a KLUDGE (FIXME) because a few places in a few ports
161 (29K springs to mind) need this info for now. */
163 struct section_table *p;
164 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
165 if (!strcmp (".text", bfd_section_name (p->bfd, p->sec_ptr)))
167 text_start = p->addr;
168 text_end = p->endaddr;
176 push_target (&exec_ops);
178 /* Tell display code (if any) about the changed file name. */
179 if (exec_file_display_hook)
180 (*exec_file_display_hook) (filename);
183 printf_filtered ("No exec file now.\n");
186 /* Set both the exec file and the symbol file, in one command.
187 What a novelty. Why did GDB go through four major releases before this
188 command was added? */
191 file_command (arg, from_tty)
195 /* FIXME, if we lose on reading the symbol file, we should revert
196 the exec file, but that's rough. */
197 exec_file_command (arg, from_tty);
198 symbol_file_command (arg, from_tty);
202 /* Locate all mappable sections of a BFD file.
203 table_pp_char is a char * to get it through bfd_map_over_sections;
204 we cast it back to its proper type. */
207 add_to_section_table (abfd, asect, table_pp_char)
212 struct section_table **table_pp = (struct section_table **)table_pp_char;
215 aflag = bfd_get_section_flags (abfd, asect);
216 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
217 if (!(aflag & SEC_LOAD))
219 if (0 == bfd_section_size (abfd, asect))
221 (*table_pp)->bfd = abfd;
222 (*table_pp)->sec_ptr = asect;
223 (*table_pp)->addr = bfd_section_vma (abfd, asect);
224 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
228 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
229 Returns 0 if OK, 1 on error. */
232 build_section_table (some_bfd, start, end)
234 struct section_table **start, **end;
238 count = bfd_count_sections (some_bfd);
241 *start = (struct section_table *) xmalloc (count * sizeof (**start));
243 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
244 if (*end > *start + count)
246 /* We could realloc the table, but it probably loses for most files. */
250 /* Read or write the exec file.
252 Args are address within a BFD file, address within gdb address-space,
253 length, and a flag indicating whether to read or write.
257 0: We cannot handle this address and length.
258 > 0: We have handled N bytes starting at this address.
259 (If N == length, we did it all.) We might be able
260 to handle more bytes beyond this length, but no
262 < 0: We cannot handle this address, but if somebody
263 else handles (-N) bytes, we can start from there.
265 The same routine is used to handle both core and exec files;
266 we just tail-call it with more arguments to select between them. */
269 xfer_memory (memaddr, myaddr, len, write, target)
274 struct target_ops *target;
277 struct section_table *p;
278 CORE_ADDR nextsectaddr, memend;
279 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
284 memend = memaddr + len;
285 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
286 nextsectaddr = memend;
288 for (p = target->to_sections; p < target->to_sections_end; p++)
290 if (p->addr <= memaddr)
291 if (p->endaddr >= memend)
293 /* Entire transfer is within this section. */
294 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
295 return (res != false)? len: 0;
297 else if (p->endaddr <= memaddr)
299 /* This section ends before the transfer starts. */
304 /* This section overlaps the transfer. Just do half. */
305 len = p->endaddr - memaddr;
306 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
307 return (res != false)? len: 0;
309 else if (p->addr < nextsectaddr)
310 nextsectaddr = p->addr;
313 if (nextsectaddr >= memend)
314 return 0; /* We can't help */
316 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
320 #ifdef REG_STACK_SEGMENT
322 /* Pyramids and AM29000s have an extra segment in the virtual address space
323 for the (control) stack of register-window frames. The AM29000 folk
324 call it the "register stack" rather than the "memory stack". */
325 else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
327 i = min (len, reg_stack_end - memaddr);
328 fileptr = memaddr - reg_stack_start + reg_stack_offset;
329 wanna_xfer = coredata;
331 #endif /* REG_STACK_SEGMENT */
335 print_section_info (t, abfd)
336 struct target_ops *t;
339 struct section_table *p;
341 printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
343 printf_filtered ("file type %s.\n", bfd_get_target(abfd));
345 for (p = t->to_sections; p < t->to_sections_end; p++) {
346 printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08"));
347 printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08"));
349 printf_filtered (" @ %s",
350 local_hex_string_custom (p->sec_ptr->filepos, "08"));
351 printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr));
352 if (p->bfd != abfd) {
353 printf_filtered (" in %s", bfd_get_filename (p->bfd));
355 printf_filtered ("\n");
361 struct target_ops *t;
363 print_section_info (t, exec_bfd);
367 set_section_command (args, from_tty)
371 struct section_table *p;
374 unsigned long secaddr;
379 error ("Must specify section name and its virtual address");
381 /* Parse out section name */
382 for (secname = args; !isspace(*args); args++) ;
383 seclen = args - secname;
385 /* Parse out new virtual address */
386 secaddr = parse_and_eval_address (args);
388 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) {
389 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
390 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
391 offset = secaddr - p->addr;
393 p->endaddr += offset;
395 exec_files_info(&exec_ops);
399 if (seclen >= sizeof (secprint))
400 seclen = sizeof (secprint) - 1;
401 strncpy (secprint, secname, seclen);
402 secprint[seclen] = '\0';
403 error ("Section %s not found", secprint);
406 struct target_ops exec_ops = {
407 "exec", "Local exec file",
408 "Use an executable file as a target.\n\
409 Specify the filename of the executable file.",
410 exec_file_command, exec_close, /* open, close */
411 child_attach, 0, 0, 0, /* attach, detach, resume, wait, */
412 0, 0, /* fetch_registers, store_registers, */
413 0, /* prepare_to_store, */
414 xfer_memory, exec_files_info,
415 0, 0, /* insert_breakpoint, remove_breakpoint, */
416 0, 0, 0, 0, 0, /* terminal stuff */
417 0, 0, /* kill, load */
419 child_create_inferior,
420 0, /* mourn_inferior */
421 file_stratum, 0, /* next */
422 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
423 0, 0, /* section pointers */
424 OPS_MAGIC, /* Always the last thing */
431 add_com ("file", class_files, file_command,
432 "Use FILE as program to be debugged.\n\
433 It is read for its symbols, for getting the contents of pure memory,\n\
434 and it is the program executed when you use the `run' command.\n\
435 If FILE cannot be found as specified, your execution directory path\n\
436 ($PATH) is searched for a command of that name.\n\
437 No arg means to have no executable file and no symbols.");
439 add_com ("exec-file", class_files, exec_file_command,
440 "Use FILE as program for getting contents of pure memory.\n\
441 If FILE cannot be found as specified, your execution directory path\n\
442 is searched for a command of that name.\n\
443 No arg means have no executable file.");
445 add_com ("section", class_files, set_section_command,
446 "Change the base address of section SECTION of the exec file to ADDR.\n\
447 This can be used if the exec file does not contain section addresses,\n\
448 (such as in the a.out format), or when the addresses specified in the\n\
449 file itself are wrong. Each section must be changed separately. The\n\
450 ``info files'' command lists all the sections and their addresses.");
453 (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files,
454 "Set writing into executable and core files.",
458 add_target (&exec_ops);