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