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