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