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