1 /* Work with executable files, for GDB.
2 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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.
16 You should have received a copy of the GNU General Public License
17 along with GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
31 #include <sys/param.h>
36 #ifdef STILL_NEEDED_FOR_DECSTATION
37 #include <sys/dir.h> /* For DECstations */
38 #include <sys/user.h> /* After a.out.h */
45 extern char *getenv();
46 extern void child_create_inferior (), child_attach ();
47 extern void add_syms_addr_command ();
48 extern void symbol_file_command ();
50 /* The Binary File Descriptor handle for the executable file. */
54 /* The base and bounds of the table of the exec file's sections. */
56 struct section_table *exec_sections, *exec_sections_end;
60 extern struct target_ops exec_ops;
73 exec_file_command (filename, from_tty)
77 target_preopen (from_tty);
79 /* Remove any previous exec file. */
80 unpush_target (&exec_ops);
82 /* Now open and digest the file the user requested, if any. */
86 char *scratch_pathname;
89 filename = tilde_expand (filename);
90 make_cleanup (free, filename);
92 /* FIXME, if writeable is set, open for read/write. */
93 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
96 perror_with_name (filename);
98 exec_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
100 error ("Could not open `%s' as an executable file: %s",
101 scratch_pathname, bfd_errmsg (bfd_error));
102 if (!bfd_check_format (exec_bfd, bfd_object))
103 error ("\"%s\": not in executable format: %s.",
104 scratch_pathname, bfd_errmsg (bfd_error));
107 /* This code needs to be incorporated into BFD */
108 #ifdef COFF_ENCAPSULATE
109 /* If we have a coff header, it can give us better values for
110 text_start and exec_data_start. This is particularly useful
111 for remote debugging of embedded systems. */
112 if (N_FLAGS(exec_aouthdr) & N_FLAGS_COFF_ENCAPSULATE)
114 struct coffheader ch;
116 val = lseek (execchan, -(sizeof (AOUTHDR) + sizeof (ch)), 1);
118 perror_with_name (filename);
119 val = myread (execchan, &ch, sizeof (ch));
121 perror_with_name (filename);
122 text_start = ch.text_start;
123 exec_data_start = ch.data_start;
128 IS_OBJECT_FILE (exec_aouthdr) ? 0 : N_TXTADDR (exec_aouthdr);
129 exec_data_start = IS_OBJECT_FILE (exec_aouthdr)
130 ? exec_aouthdr.a_text : N_DATADDR (exec_aouthdr);
134 if (build_section_table (exec_bfd, &exec_sections, &exec_sections_end))
135 error ("Can't find the file sections in `%s': %s",
136 exec_bfd->filename, bfd_errmsg (bfd_error));
140 push_target (&exec_ops);
142 /* Tell display code (if any) about the changed file name. */
143 if (exec_file_display_hook)
144 (*exec_file_display_hook) (filename);
147 printf ("No exec file now.\n");
150 /* Set both the exec file and the symbol file, in one command.
151 What a novelty. Why did GDB go through four major releases before this
152 command was added? */
155 file_command (arg, from_tty)
159 /* FIXME, if we lose on reading the symbol file, we should revert
160 the exec file, but that's rough. */
161 exec_file_command (arg, from_tty);
162 symbol_file_command (arg, from_tty);
166 /* Locate all mappable sections of a BFD file. */
169 add_to_section_table (abfd, asect, table_pp)
172 struct section_table **table_pp;
176 aflag = bfd_get_section_flags (abfd, asect);
177 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
178 if (!(aflag & SEC_LOAD))
180 (*table_pp)->sec_ptr = asect;
181 (*table_pp)->addr = bfd_section_vma (abfd, asect);
182 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
187 build_section_table (some_bfd, start, end)
189 struct section_table **start, **end;
193 count = bfd_count_sections (some_bfd);
195 abort(); /* return 1? */
196 *start = (struct section_table *) xmalloc (count * sizeof (**start));
198 bfd_map_over_sections (some_bfd, add_to_section_table, end);
199 if (*end > *start + count)
201 /* We could realloc the table, but it probably loses for most files. */
205 /* Read or write the exec file.
207 Args are address within exec file, address within gdb address-space,
208 length, and a flag indicating whether to read or write.
212 0: We cannot handle this address and length.
213 > 0: We have handled N bytes starting at this address.
214 (If N == length, we did it all.) We might be able
215 to handle more bytes beyond this length, but no
217 < 0: We cannot handle this address, but if somebody
218 else handles (-N) bytes, we can start from there.
220 The same routine is used to handle both core and exec files;
221 we just tail-call it with more arguments to select between them. */
224 xfer_memory (memaddr, myaddr, len, write, abfd, sections, sections_end)
230 struct section_table *sections, *sections_end;
233 struct section_table *p;
234 CORE_ADDR nextsectaddr, memend;
235 boolean (*xfer_fn) ();
240 memend = memaddr + len;
241 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
242 nextsectaddr = memend;
244 for (p = sections; p < sections_end; p++)
246 if (p->addr <= memaddr)
247 if (p->endaddr >= memend)
249 /* Entire transfer is within this section. */
250 res = xfer_fn (abfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
251 return (res != false)? len: 0;
253 else if (p->endaddr <= memaddr)
255 /* This section ends before the transfer starts. */
260 /* This section overlaps the transfer. Just do half. */
261 len = p->endaddr - memaddr;
262 res = xfer_fn (abfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
263 return (res != false)? len: 0;
265 else if (p->addr < nextsectaddr)
266 nextsectaddr = p->addr;
269 if (nextsectaddr >= memend)
270 return 0; /* We can't help */
272 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
275 /* The function called by target_xfer_memory via our target_ops */
278 exec_xfer_memory (memaddr, myaddr, len, write)
284 return xfer_memory (memaddr, myaddr, len, write,
285 exec_bfd, exec_sections, exec_sections_end);
290 #ifdef REG_STACK_SEGMENT
292 /* Pyramids and AM29000s have an extra segment in the virtual address space
293 for the (control) stack of register-window frames. The AM29000 folk
294 call it the "register stack" rather than the "memory stack". */
295 else if (memaddr >= reg_stack_start && memaddr < reg_stack_end)
297 i = min (len, reg_stack_end - memaddr);
298 fileptr = memaddr - reg_stack_start + reg_stack_offset;
299 wanna_xfer = coredata;
301 #endif /* REG_STACK_SEGMENT */
307 struct section_table *p;
309 printf ("\tExecutable file `%s'.\n", bfd_get_filename(exec_bfd));
311 for (p = exec_sections; p < exec_sections_end; p++)
312 printf("\texecutable from 0x%08x to 0x%08x is %s\n",
314 bfd_section_name (exec_bfd, p->sec_ptr));
318 set_section_command (args, from_tty)
322 struct section_table *p;
325 unsigned long secaddr;
330 error ("Must specify section name and its virtual address");
332 /* Parse out section name */
333 for (secname = args; !isspace(*args); args++) ;
334 seclen = args - secname;
336 /* Parse out new virtual address */
337 secaddr = parse_and_eval_address (args);
339 for (p = exec_sections; p < exec_sections_end; p++) {
340 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
341 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
342 offset = secaddr - p->addr;
344 p->endaddr += offset;
349 if (seclen >= sizeof (secprint))
350 seclen = sizeof (secprint) - 1;
351 strncpy (secprint, secname, seclen);
352 secprint[seclen] = '\0';
353 error ("Section %s not found", secprint);
356 struct target_ops exec_ops = {
357 "exec", "Local exec file",
358 "Use an executable file as a target.\n\
359 Specify the filename of the executable file.",
360 exec_file_command, exec_close, /* open, close */
361 child_attach, 0, 0, 0, /* attach, detach, resume, wait, */
362 0, 0, /* fetch_registers, store_registers, */
363 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */
364 exec_xfer_memory, exec_files_info,
365 0, 0, /* insert_breakpoint, remove_breakpoint, */
366 0, 0, 0, 0, 0, /* terminal stuff */
367 0, 0, /* kill, load */
368 add_syms_addr_command,
369 0, 0, /* call fn, lookup sym */
370 child_create_inferior,
371 0, /* mourn_inferior */
372 file_stratum, 0, /* next */
373 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
374 OPS_MAGIC, /* Always the last thing */
381 add_com ("file", class_files, file_command,
382 "Use FILE as program to be debugged.\n\
383 It is read for its symbols, for getting the contents of pure memory,\n\
384 and it is the program executed when you use the `run' command.\n\
385 If FILE cannot be found as specified, your execution directory path\n\
386 ($PATH) is searched for a command of that name.\n\
387 No arg means to have no executable file and no symbols.");
389 add_com ("exec-file", class_files, exec_file_command,
390 "Use FILE as program for getting contents of pure memory.\n\
391 If FILE cannot be found as specified, your execution directory path\n\
392 is searched for a command of that name.\n\
393 No arg means have no executable file.");
395 add_com ("section", class_files, set_section_command,
396 "Change the base address of section SECTION of the exec file to ADDR.\n\
397 This can be used if the exec file does not contain section addresses,\n\
398 (such as in the a.out format), or when the addresses specified in the\n\
399 file itself are wrong. Each section must be changed separately. The\n\
400 ``info files'' command lists all the sections and their addresses.");
402 add_target (&exec_ops);