Update checked-in copy for convenience. (FSF releases build their own.)
[deliverable/binutils-gdb.git] / gdb / exec.c
CommitLineData
bd5635a1
RP
1/* Work with executable files, for GDB.
2 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
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
RP
19
20#include <stdio.h>
21#include "defs.h"
22#include "param.h"
23#include "frame.h"
24#include "inferior.h"
25#include "target.h"
bdbd5f50 26#include "gdbcmd.h"
bd5635a1
RP
27
28#ifdef USG
29#include <sys/types.h>
30#endif
31
32#include <sys/param.h>
33#include <fcntl.h>
bdbd5f50 34#include <string.h>
bd5635a1
RP
35
36#include "gdbcore.h"
37
f2ebc25f 38#include <ctype.h>
bd5635a1
RP
39#include <sys/stat.h>
40
41extern char *getenv();
42extern void child_create_inferior (), child_attach ();
43extern void symbol_file_command ();
44
45/* The Binary File Descriptor handle for the executable file. */
46
47bfd *exec_bfd = NULL;
48
bdbd5f50 49/* Whether to open exec and core files read-only or read-write. */
bd5635a1 50
bdbd5f50 51int write_files = 0;
bd5635a1 52
7730bd5a
JG
53/* Text start and end addresses (KLUDGE) if needed */
54
dad0e12d 55#ifdef NEED_TEXT_START_END
7730bd5a
JG
56CORE_ADDR text_start = 0;
57CORE_ADDR text_end = 0;
58#endif
59
bd5635a1
RP
60/* Forward decl */
61
62extern struct target_ops exec_ops;
63
bdbd5f50 64/* ARGSUSED */
bd5635a1
RP
65void
66exec_close (quitting)
67 int quitting;
68{
69 if (exec_bfd) {
70 bfd_close (exec_bfd);
71 exec_bfd = NULL;
72 }
bdbd5f50
JG
73 if (exec_ops.sections) {
74 free (exec_ops.sections);
75 exec_ops.sections = NULL;
76 exec_ops.sections_end = NULL;
77 }
bd5635a1
RP
78}
79
80void
81exec_file_command (filename, from_tty)
82 char *filename;
83 int from_tty;
84{
f2fc6e7a 85 target_preopen (from_tty);
bd5635a1
RP
86
87 /* Remove any previous exec file. */
88 unpush_target (&exec_ops);
89
90 /* Now open and digest the file the user requested, if any. */
91
92 if (filename)
93 {
94 char *scratch_pathname;
95 int scratch_chan;
96
97 filename = tilde_expand (filename);
98 make_cleanup (free, filename);
99
bdbd5f50
JG
100 scratch_chan = openp (getenv ("PATH"), 1, filename,
101 write_files? O_RDWR: O_RDONLY, 0,
bd5635a1
RP
102 &scratch_pathname);
103 if (scratch_chan < 0)
104 perror_with_name (filename);
105
106 exec_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
107 if (!exec_bfd)
108 error ("Could not open `%s' as an executable file: %s",
109 scratch_pathname, bfd_errmsg (bfd_error));
110 if (!bfd_check_format (exec_bfd, bfd_object))
111 error ("\"%s\": not in executable format: %s.",
112 scratch_pathname, bfd_errmsg (bfd_error));
113
114#if FIXME
115/* This code needs to be incorporated into BFD */
116#ifdef COFF_ENCAPSULATE
117 /* If we have a coff header, it can give us better values for
118 text_start and exec_data_start. This is particularly useful
119 for remote debugging of embedded systems. */
120 if (N_FLAGS(exec_aouthdr) & N_FLAGS_COFF_ENCAPSULATE)
121 {
122 struct coffheader ch;
123 int val;
124 val = lseek (execchan, -(sizeof (AOUTHDR) + sizeof (ch)), 1);
125 if (val == -1)
126 perror_with_name (filename);
127 val = myread (execchan, &ch, sizeof (ch));
128 if (val < 0)
129 perror_with_name (filename);
130 text_start = ch.text_start;
131 exec_data_start = ch.data_start;
132 } else
133#endif
134 {
135 text_start =
136 IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
137 exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
138 ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
139 }
140#endif FIXME
141
bdbd5f50
JG
142 if (build_section_table (exec_bfd, &exec_ops.sections,
143 &exec_ops.sections_end))
bd5635a1
RP
144 error ("Can't find the file sections in `%s': %s",
145 exec_bfd->filename, bfd_errmsg (bfd_error));
146
dad0e12d 147#ifdef NEED_TEXT_START_END
7730bd5a
JG
148 /* This is a KLUDGE (FIXME) because a few places in a few ports
149 (29K springs to mind) need this info for now. */
150 {
151 struct section_table *p;
152 for (p = exec_ops.sections; p < exec_ops.sections_end; p++)
dad0e12d
JG
153 if (!strcmp (".text", bfd_section_name (p->bfd, p->sec_ptr)))
154 {
155 text_start = p->addr;
156 text_end = p->endaddr;
157 break;
158 }
7730bd5a
JG
159 }
160#endif
161
bd5635a1
RP
162 validate_files ();
163
164 push_target (&exec_ops);
165
166 /* Tell display code (if any) about the changed file name. */
167 if (exec_file_display_hook)
168 (*exec_file_display_hook) (filename);
169 }
170 else if (from_tty)
171 printf ("No exec file now.\n");
172}
173
174/* Set both the exec file and the symbol file, in one command.
175 What a novelty. Why did GDB go through four major releases before this
176 command was added? */
177
178void
179file_command (arg, from_tty)
180 char *arg;
181 int from_tty;
182{
183 /* FIXME, if we lose on reading the symbol file, we should revert
184 the exec file, but that's rough. */
185 exec_file_command (arg, from_tty);
186 symbol_file_command (arg, from_tty);
187}
188
189\f
bdbd5f50
JG
190/* Locate all mappable sections of a BFD file.
191 table_pp_char is a char * to get it through bfd_map_over_sections;
192 we cast it back to its proper type. */
bd5635a1
RP
193
194void
bdbd5f50 195add_to_section_table (abfd, asect, table_pp_char)
bd5635a1
RP
196 bfd *abfd;
197 sec_ptr asect;
bdbd5f50 198 char *table_pp_char;
bd5635a1 199{
bdbd5f50 200 struct section_table **table_pp = (struct section_table **)table_pp_char;
bd5635a1
RP
201 flagword aflag;
202
203 aflag = bfd_get_section_flags (abfd, asect);
204 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
205 if (!(aflag & SEC_LOAD))
206 return;
dad0e12d
JG
207 if (0 == bfd_section_size (abfd, asect))
208 return;
bdbd5f50 209 (*table_pp)->bfd = abfd;
bd5635a1
RP
210 (*table_pp)->sec_ptr = asect;
211 (*table_pp)->addr = bfd_section_vma (abfd, asect);
212 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
213 (*table_pp)++;
214}
215
216int
217build_section_table (some_bfd, start, end)
218 bfd *some_bfd;
219 struct section_table **start, **end;
220{
221 unsigned count;
222
223 count = bfd_count_sections (some_bfd);
224 if (count == 0)
225 abort(); /* return 1? */
777bef06
JK
226 if (*start)
227 free (*start);
bd5635a1
RP
228 *start = (struct section_table *) xmalloc (count * sizeof (**start));
229 *end = *start;
bdbd5f50 230 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
bd5635a1
RP
231 if (*end > *start + count)
232 abort();
233 /* We could realloc the table, but it probably loses for most files. */
234 return 0;
235}
236\f
237/* Read or write the exec file.
238
bdbd5f50 239 Args are address within a BFD file, address within gdb address-space,
bd5635a1
RP
240 length, and a flag indicating whether to read or write.
241
242 Result is a length:
243
244 0: We cannot handle this address and length.
245 > 0: We have handled N bytes starting at this address.
246 (If N == length, we did it all.) We might be able
247 to handle more bytes beyond this length, but no
248 promises.
249 < 0: We cannot handle this address, but if somebody
250 else handles (-N) bytes, we can start from there.
251
252 The same routine is used to handle both core and exec files;
253 we just tail-call it with more arguments to select between them. */
254
255int
bdbd5f50 256xfer_memory (memaddr, myaddr, len, write, target)
bd5635a1
RP
257 CORE_ADDR memaddr;
258 char *myaddr;
259 int len;
260 int write;
bdbd5f50 261 struct target_ops *target;
bd5635a1
RP
262{
263 boolean res;
264 struct section_table *p;
265 CORE_ADDR nextsectaddr, memend;
266 boolean (*xfer_fn) ();
267
268 if (len <= 0)
269 abort();
270
271 memend = memaddr + len;
272 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
273 nextsectaddr = memend;
274
bdbd5f50 275 for (p = target->sections; p < target->sections_end; p++)
bd5635a1
RP
276 {
277 if (p->addr <= memaddr)
278 if (p->endaddr >= memend)
279 {
280 /* Entire transfer is within this section. */
bdbd5f50 281 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
bd5635a1
RP
282 return (res != false)? len: 0;
283 }
284 else if (p->endaddr <= memaddr)
285 {
286 /* This section ends before the transfer starts. */
287 continue;
288 }
289 else
290 {
291 /* This section overlaps the transfer. Just do half. */
292 len = p->endaddr - memaddr;
bdbd5f50 293 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
bd5635a1
RP
294 return (res != false)? len: 0;
295 }
296 else if (p->addr < nextsectaddr)
297 nextsectaddr = p->addr;
298 }
299
300 if (nextsectaddr >= memend)
301 return 0; /* We can't help */
302 else
303 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
304}
305
bd5635a1
RP
306#ifdef FIXME
307#ifdef REG_STACK_SEGMENT
308/* MOVE TO BFD... */
309 /* Pyramids and AM29000s have an extra segment in the virtual address space
310 for the (control) stack of register-window frames. The AM29000 folk
311 call it the "register stack" rather than the "memory stack". */
312 else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
313 {
314 i = min (len, reg_stack_end - memaddr);
315 fileptr = memaddr - reg_stack_start + reg_stack_offset;
316 wanna_xfer = coredata;
317 }
318#endif /* REG_STACK_SEGMENT */
319#endif FIXME
320\f
321static void
322exec_files_info ()
323{
324 struct section_table *p;
325
326 printf ("\tExecutable file `%s'.\n", bfd_get_filename(exec_bfd));
327
dad0e12d
JG
328 for (p = exec_ops.sections; p < exec_ops.sections_end; p++) {
329 printf("\t%s", local_hex_string_custom (p->addr, "08"));
330 printf(" - %s is %s\n", local_hex_string_custom (p->endaddr, "08"),
bd5635a1 331 bfd_section_name (exec_bfd, p->sec_ptr));
dad0e12d 332 }
bd5635a1
RP
333}
334
f2fc6e7a
JK
335static void
336set_section_command (args, from_tty)
337 char *args;
338 int from_tty;
339{
340 struct section_table *p;
341 char *secname;
342 unsigned seclen;
343 unsigned long secaddr;
344 char secprint[100];
345 long offset;
346
347 if (args == 0)
348 error ("Must specify section name and its virtual address");
349
350 /* Parse out section name */
351 for (secname = args; !isspace(*args); args++) ;
352 seclen = args - secname;
353
354 /* Parse out new virtual address */
355 secaddr = parse_and_eval_address (args);
356
bdbd5f50 357 for (p = exec_ops.sections; p < exec_ops.sections_end; p++) {
f2fc6e7a
JK
358 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
359 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
360 offset = secaddr - p->addr;
361 p->addr += offset;
362 p->endaddr += offset;
363 exec_files_info();
364 return;
365 }
366 }
367 if (seclen >= sizeof (secprint))
368 seclen = sizeof (secprint) - 1;
369 strncpy (secprint, secname, seclen);
370 secprint[seclen] = '\0';
371 error ("Section %s not found", secprint);
372}
373
bd5635a1
RP
374struct target_ops exec_ops = {
375 "exec", "Local exec file",
f2fc6e7a
JK
376 "Use an executable file as a target.\n\
377Specify the filename of the executable file.",
bd5635a1
RP
378 exec_file_command, exec_close, /* open, close */
379 child_attach, 0, 0, 0, /* attach, detach, resume, wait, */
380 0, 0, /* fetch_registers, store_registers, */
381 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */
bdbd5f50 382 xfer_memory, exec_files_info,
bd5635a1
RP
383 0, 0, /* insert_breakpoint, remove_breakpoint, */
384 0, 0, 0, 0, 0, /* terminal stuff */
64e52224 385 0, 0, /* kill, load */
64e52224 386 0, 0, /* call fn, lookup sym */
bd5635a1
RP
387 child_create_inferior,
388 0, /* mourn_inferior */
389 file_stratum, 0, /* next */
390 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
bdbd5f50 391 0, 0, /* section pointers */
bd5635a1
RP
392 OPS_MAGIC, /* Always the last thing */
393};
394
395void
396_initialize_exec()
397{
398
399 add_com ("file", class_files, file_command,
400 "Use FILE as program to be debugged.\n\
401It is read for its symbols, for getting the contents of pure memory,\n\
402and it is the program executed when you use the `run' command.\n\
403If FILE cannot be found as specified, your execution directory path\n\
404($PATH) is searched for a command of that name.\n\
405No arg means to have no executable file and no symbols.");
406
407 add_com ("exec-file", class_files, exec_file_command,
408 "Use FILE as program for getting contents of pure memory.\n\
409If FILE cannot be found as specified, your execution directory path\n\
410is searched for a command of that name.\n\
411No arg means have no executable file.");
412
f2fc6e7a
JK
413 add_com ("section", class_files, set_section_command,
414 "Change the base address of section SECTION of the exec file to ADDR.\n\
415This can be used if the exec file does not contain section addresses,\n\
416(such as in the a.out format), or when the addresses specified in the\n\
417file itself are wrong. Each section must be changed separately. The\n\
418``info files'' command lists all the sections and their addresses.");
419
bdbd5f50
JG
420 add_show_from_set
421 (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files,
422 "Set writing into executable and core files.",
423 &setlist),
424 &showlist);
425
bd5635a1
RP
426 add_target (&exec_ops);
427}
This page took 0.059552 seconds and 4 git commands to generate.