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