* breakpoint.c, breakpoint.h (breakpoint_init_inferior): New function
[deliverable/binutils-gdb.git] / gdb / exec.c
CommitLineData
bd5635a1 1/* Work with executable files, for GDB.
be772100 2 Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
bdbd5f50 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
bdbd5f50
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
bdbd5f50 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
bdbd5f50
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
bd5635a1
RP
21#include "frame.h"
22#include "inferior.h"
23#include "target.h"
bdbd5f50 24#include "gdbcmd.h"
bd5635a1
RP
25
26#ifdef USG
27#include <sys/types.h>
28#endif
29
30#include <sys/param.h>
31#include <fcntl.h>
bdbd5f50 32#include <string.h>
bd5635a1
RP
33
34#include "gdbcore.h"
35
f2ebc25f 36#include <ctype.h>
bd5635a1 37#include <sys/stat.h>
a8e033f2
SG
38#ifndef O_BINARY
39#define O_BINARY 0
40#endif
bd5635a1 41
be772100
JG
42/* Prototypes for local functions */
43
44static void
45add_to_section_table PARAMS ((bfd *, sec_ptr, PTR));
46
47static void
48exec_close PARAMS ((int));
49
50static void
51file_command PARAMS ((char *, int));
52
53static void
54set_section_command PARAMS ((char *, int));
55
56static void
57exec_files_info PARAMS ((struct target_ops *));
58
59extern int info_verbose;
bd5635a1
RP
60
61/* The Binary File Descriptor handle for the executable file. */
62
63bfd *exec_bfd = NULL;
64
bdbd5f50 65/* Whether to open exec and core files read-only or read-write. */
bd5635a1 66
bdbd5f50 67int write_files = 0;
bd5635a1 68
7730bd5a
JG
69/* Text start and end addresses (KLUDGE) if needed */
70
dad0e12d 71#ifdef NEED_TEXT_START_END
7730bd5a
JG
72CORE_ADDR text_start = 0;
73CORE_ADDR text_end = 0;
74#endif
75
bd5635a1
RP
76/* Forward decl */
77
78extern struct target_ops exec_ops;
79
bdbd5f50 80/* ARGSUSED */
be772100 81static void
bd5635a1
RP
82exec_close (quitting)
83 int quitting;
84{
85 if (exec_bfd) {
b1eaba9a 86 char *name = bfd_get_filename (exec_bfd);
bd5635a1 87 bfd_close (exec_bfd);
b1eaba9a 88 free (name);
bd5635a1
RP
89 exec_bfd = NULL;
90 }
be772100
JG
91 if (exec_ops.to_sections) {
92 free ((PTR)exec_ops.to_sections);
93 exec_ops.to_sections = NULL;
94 exec_ops.to_sections_end = NULL;
bdbd5f50 95 }
bd5635a1
RP
96}
97
be772100
JG
98/* Process the first arg in ARGS as the new exec file.
99
100 Note that we have to explicitly ignore additional args, since we can
101 be called from file_command(), which also calls symbol_file_command()
102 which can take multiple args. */
103
bd5635a1 104void
be772100
JG
105exec_file_command (args, from_tty)
106 char *args;
bd5635a1
RP
107 int from_tty;
108{
be772100
JG
109 char **argv;
110 char *filename;
111
f2fc6e7a 112 target_preopen (from_tty);
bd5635a1
RP
113
114 /* Remove any previous exec file. */
115 unpush_target (&exec_ops);
116
117 /* Now open and digest the file the user requested, if any. */
118
be772100 119 if (args)
bd5635a1
RP
120 {
121 char *scratch_pathname;
122 int scratch_chan;
123
be772100
JG
124 /* Scan through the args and pick up the first non option arg
125 as the filename. */
126
127 if ((argv = buildargv (args)) == NULL)
128 {
129 nomem (0);
130 }
131 make_cleanup (freeargv, (char *) argv);
132
133 for (; (*argv != NULL) && (**argv == '-'); argv++) {;}
134 if (*argv == NULL)
135 {
136 error ("no exec file name was specified");
137 }
138
139 filename = tilde_expand (*argv);
bd5635a1
RP
140 make_cleanup (free, filename);
141
bdbd5f50 142 scratch_chan = openp (getenv ("PATH"), 1, filename,
a8e033f2 143 write_files? O_RDWR|O_BINARY: O_RDONLY|O_BINARY, 0,
bd5635a1
RP
144 &scratch_pathname);
145 if (scratch_chan < 0)
146 perror_with_name (filename);
147
b1eaba9a 148 exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
bd5635a1
RP
149 if (!exec_bfd)
150 error ("Could not open `%s' as an executable file: %s",
151 scratch_pathname, bfd_errmsg (bfd_error));
152 if (!bfd_check_format (exec_bfd, bfd_object))
153 error ("\"%s\": not in executable format: %s.",
154 scratch_pathname, bfd_errmsg (bfd_error));
155
be772100
JG
156 if (build_section_table (exec_bfd, &exec_ops.to_sections,
157 &exec_ops.to_sections_end))
bd5635a1
RP
158 error ("Can't find the file sections in `%s': %s",
159 exec_bfd->filename, bfd_errmsg (bfd_error));
160
dad0e12d 161#ifdef NEED_TEXT_START_END
7730bd5a
JG
162 /* This is a KLUDGE (FIXME) because a few places in a few ports
163 (29K springs to mind) need this info for now. */
164 {
165 struct section_table *p;
be772100 166 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
b1eaba9a 167 if (STREQ (".text", bfd_section_name (p->bfd, p->sec_ptr)))
dad0e12d
JG
168 {
169 text_start = p->addr;
170 text_end = p->endaddr;
171 break;
172 }
7730bd5a
JG
173 }
174#endif
175
bd5635a1
RP
176 validate_files ();
177
178 push_target (&exec_ops);
179
180 /* Tell display code (if any) about the changed file name. */
181 if (exec_file_display_hook)
182 (*exec_file_display_hook) (filename);
183 }
184 else if (from_tty)
117f631e 185 printf ("No exec file now.\n");
bd5635a1
RP
186}
187
188/* Set both the exec file and the symbol file, in one command.
189 What a novelty. Why did GDB go through four major releases before this
190 command was added? */
191
be772100 192static void
bd5635a1
RP
193file_command (arg, from_tty)
194 char *arg;
195 int from_tty;
196{
197 /* FIXME, if we lose on reading the symbol file, we should revert
198 the exec file, but that's rough. */
199 exec_file_command (arg, from_tty);
200 symbol_file_command (arg, from_tty);
201}
202
203\f
bdbd5f50
JG
204/* Locate all mappable sections of a BFD file.
205 table_pp_char is a char * to get it through bfd_map_over_sections;
206 we cast it back to its proper type. */
bd5635a1 207
be772100 208static void
bdbd5f50 209add_to_section_table (abfd, asect, table_pp_char)
bd5635a1
RP
210 bfd *abfd;
211 sec_ptr asect;
be772100 212 PTR table_pp_char;
bd5635a1 213{
bdbd5f50 214 struct section_table **table_pp = (struct section_table **)table_pp_char;
bd5635a1
RP
215 flagword aflag;
216
217 aflag = bfd_get_section_flags (abfd, asect);
218 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
219 if (!(aflag & SEC_LOAD))
220 return;
dad0e12d
JG
221 if (0 == bfd_section_size (abfd, asect))
222 return;
bdbd5f50 223 (*table_pp)->bfd = abfd;
bd5635a1
RP
224 (*table_pp)->sec_ptr = asect;
225 (*table_pp)->addr = bfd_section_vma (abfd, asect);
226 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
227 (*table_pp)++;
228}
229
be772100
JG
230/* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
231 Returns 0 if OK, 1 on error. */
232
bd5635a1
RP
233int
234build_section_table (some_bfd, start, end)
235 bfd *some_bfd;
236 struct section_table **start, **end;
237{
238 unsigned count;
239
240 count = bfd_count_sections (some_bfd);
777bef06 241 if (*start)
be772100 242 free ((PTR)*start);
bd5635a1
RP
243 *start = (struct section_table *) xmalloc (count * sizeof (**start));
244 *end = *start;
bdbd5f50 245 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
bd5635a1
RP
246 if (*end > *start + count)
247 abort();
248 /* We could realloc the table, but it probably loses for most files. */
249 return 0;
250}
251\f
252/* Read or write the exec file.
253
bdbd5f50 254 Args are address within a BFD file, address within gdb address-space,
bd5635a1
RP
255 length, and a flag indicating whether to read or write.
256
257 Result is a length:
258
259 0: We cannot handle this address and length.
260 > 0: We have handled N bytes starting at this address.
261 (If N == length, we did it all.) We might be able
262 to handle more bytes beyond this length, but no
263 promises.
264 < 0: We cannot handle this address, but if somebody
265 else handles (-N) bytes, we can start from there.
266
267 The same routine is used to handle both core and exec files;
268 we just tail-call it with more arguments to select between them. */
269
270int
bdbd5f50 271xfer_memory (memaddr, myaddr, len, write, target)
bd5635a1
RP
272 CORE_ADDR memaddr;
273 char *myaddr;
274 int len;
275 int write;
bdbd5f50 276 struct target_ops *target;
bd5635a1
RP
277{
278 boolean res;
279 struct section_table *p;
280 CORE_ADDR nextsectaddr, memend;
be772100 281 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
bd5635a1
RP
282
283 if (len <= 0)
284 abort();
285
286 memend = memaddr + len;
287 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
288 nextsectaddr = memend;
289
be772100 290 for (p = target->to_sections; p < target->to_sections_end; p++)
bd5635a1
RP
291 {
292 if (p->addr <= memaddr)
293 if (p->endaddr >= memend)
294 {
295 /* Entire transfer is within this section. */
bdbd5f50 296 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
bd5635a1
RP
297 return (res != false)? len: 0;
298 }
299 else if (p->endaddr <= memaddr)
300 {
301 /* This section ends before the transfer starts. */
302 continue;
303 }
304 else
305 {
306 /* This section overlaps the transfer. Just do half. */
307 len = p->endaddr - memaddr;
bdbd5f50 308 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
bd5635a1
RP
309 return (res != false)? len: 0;
310 }
311 else if (p->addr < nextsectaddr)
312 nextsectaddr = p->addr;
313 }
314
315 if (nextsectaddr >= memend)
316 return 0; /* We can't help */
317 else
318 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
319}
320
bd5635a1
RP
321#ifdef FIXME
322#ifdef REG_STACK_SEGMENT
323/* MOVE TO BFD... */
324 /* Pyramids and AM29000s have an extra segment in the virtual address space
325 for the (control) stack of register-window frames. The AM29000 folk
326 call it the "register stack" rather than the "memory stack". */
327 else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
328 {
329 i = min (len, reg_stack_end - memaddr);
330 fileptr = memaddr - reg_stack_start + reg_stack_offset;
331 wanna_xfer = coredata;
332 }
333#endif /* REG_STACK_SEGMENT */
117f631e 334#endif /* FIXME */
bd5635a1 335\f
be772100
JG
336void
337print_section_info (t, abfd)
338 struct target_ops *t;
339 bfd *abfd;
bd5635a1
RP
340{
341 struct section_table *p;
342
be772100 343 printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
7d9884b9 344 wrap_here (" ");
be772100 345 printf_filtered ("file type %s.\n", bfd_get_target(abfd));
b1eaba9a
SG
346 printf_filtered ("\tEntry point: %s\n",
347 local_hex_string (bfd_get_start_address (exec_bfd)));
be772100 348 for (p = t->to_sections; p < t->to_sections_end; p++) {
7d9884b9 349 printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08"));
be772100
JG
350 printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08"));
351 if (info_verbose)
352 printf_filtered (" @ %s",
353 local_hex_string_custom (p->sec_ptr->filepos, "08"));
354 printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr));
355 if (p->bfd != abfd) {
356 printf_filtered (" in %s", bfd_get_filename (p->bfd));
357 }
358 printf_filtered ("\n");
dad0e12d 359 }
bd5635a1
RP
360}
361
be772100
JG
362static void
363exec_files_info (t)
364 struct target_ops *t;
365{
366 print_section_info (t, exec_bfd);
367}
368
f2fc6e7a
JK
369static void
370set_section_command (args, from_tty)
371 char *args;
372 int from_tty;
373{
374 struct section_table *p;
375 char *secname;
376 unsigned seclen;
377 unsigned long secaddr;
378 char secprint[100];
379 long offset;
380
381 if (args == 0)
382 error ("Must specify section name and its virtual address");
383
384 /* Parse out section name */
385 for (secname = args; !isspace(*args); args++) ;
386 seclen = args - secname;
387
388 /* Parse out new virtual address */
389 secaddr = parse_and_eval_address (args);
390
be772100 391 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) {
f2fc6e7a
JK
392 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
393 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
394 offset = secaddr - p->addr;
395 p->addr += offset;
396 p->endaddr += offset;
be772100
JG
397 if (from_tty)
398 exec_files_info(&exec_ops);
f2fc6e7a
JK
399 return;
400 }
401 }
402 if (seclen >= sizeof (secprint))
403 seclen = sizeof (secprint) - 1;
404 strncpy (secprint, secname, seclen);
405 secprint[seclen] = '\0';
406 error ("Section %s not found", secprint);
407}
408
b1eaba9a
SG
409/* If mourn is being called in all the right places, this could be say
410 `gdb internal error' (since generic_mourn calls mark_breakpoints_out). */
411
412static int
413ignore (addr, contents)
414 CORE_ADDR addr;
415 char *contents;
416{
417 return 0;
418}
419
bd5635a1
RP
420struct target_ops exec_ops = {
421 "exec", "Local exec file",
f2fc6e7a
JK
422 "Use an executable file as a target.\n\
423Specify the filename of the executable file.",
bd5635a1 424 exec_file_command, exec_close, /* open, close */
117f631e 425 find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
bd5635a1 426 0, 0, /* fetch_registers, store_registers, */
a03d4f8e 427 0, /* prepare_to_store, */
bdbd5f50 428 xfer_memory, exec_files_info,
b1eaba9a 429 ignore, ignore, /* insert_breakpoint, remove_breakpoint, */
bd5635a1 430 0, 0, 0, 0, 0, /* terminal stuff */
64e52224 431 0, 0, /* kill, load */
be772100 432 0, /* lookup sym */
117f631e 433 find_default_create_inferior,
bd5635a1 434 0, /* mourn_inferior */
117f631e
ILT
435 0, /* can_run */
436 0, /* notice_signals */
bd5635a1
RP
437 file_stratum, 0, /* next */
438 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
bdbd5f50 439 0, 0, /* section pointers */
bd5635a1
RP
440 OPS_MAGIC, /* Always the last thing */
441};
442
443void
444_initialize_exec()
445{
b1eaba9a 446 struct cmd_list_element *c;
bd5635a1 447
b1eaba9a
SG
448 c = add_cmd ("file", class_files, file_command,
449 "Use FILE as program to be debugged.\n\
bd5635a1
RP
450It is read for its symbols, for getting the contents of pure memory,\n\
451and it is the program executed when you use the `run' command.\n\
452If FILE cannot be found as specified, your execution directory path\n\
453($PATH) is searched for a command of that name.\n\
b1eaba9a
SG
454No arg means to have no executable file and no symbols.", &cmdlist);
455 c->completer = filename_completer;
bd5635a1 456
b1eaba9a 457 c = add_cmd ("exec-file", class_files, exec_file_command,
bd5635a1
RP
458 "Use FILE as program for getting contents of pure memory.\n\
459If FILE cannot be found as specified, your execution directory path\n\
460is searched for a command of that name.\n\
b1eaba9a
SG
461No arg means have no executable file.", &cmdlist);
462 c->completer = filename_completer;
bd5635a1 463
f2fc6e7a
JK
464 add_com ("section", class_files, set_section_command,
465 "Change the base address of section SECTION of the exec file to ADDR.\n\
466This can be used if the exec file does not contain section addresses,\n\
467(such as in the a.out format), or when the addresses specified in the\n\
468file itself are wrong. Each section must be changed separately. The\n\
469``info files'' command lists all the sections and their addresses.");
470
bdbd5f50
JG
471 add_show_from_set
472 (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files,
473 "Set writing into executable and core files.",
474 &setlist),
475 &showlist);
476
bd5635a1
RP
477 add_target (&exec_ops);
478}
This page took 0.131684 seconds and 4 git commands to generate.