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