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