]>
Commit | Line | Data |
---|---|---|
bd5635a1 RP |
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 | ||
f2ebc25f | 42 | #include <ctype.h> |
bd5635a1 RP |
43 | #include <sys/stat.h> |
44 | ||
45 | extern char *getenv(); | |
46 | extern void child_create_inferior (), child_attach (); | |
be2c3f35 | 47 | extern void add_syms_addr_command (); |
bd5635a1 RP |
48 | extern void symbol_file_command (); |
49 | ||
50 | /* The Binary File Descriptor handle for the executable file. */ | |
51 | ||
52 | bfd *exec_bfd = NULL; | |
53 | ||
54 | /* The base and bounds of the table of the exec file's sections. */ | |
55 | ||
56 | struct section_table *exec_sections, *exec_sections_end; | |
57 | ||
58 | /* Forward decl */ | |
59 | ||
60 | extern struct target_ops exec_ops; | |
61 | ||
62 | void | |
63 | exec_close (quitting) | |
64 | int quitting; | |
65 | { | |
66 | if (exec_bfd) { | |
67 | bfd_close (exec_bfd); | |
68 | exec_bfd = NULL; | |
69 | } | |
70 | } | |
71 | ||
72 | void | |
73 | exec_file_command (filename, from_tty) | |
74 | char *filename; | |
75 | int from_tty; | |
76 | { | |
f2fc6e7a | 77 | target_preopen (from_tty); |
bd5635a1 RP |
78 | |
79 | /* Remove any previous exec file. */ | |
80 | unpush_target (&exec_ops); | |
81 | ||
82 | /* Now open and digest the file the user requested, if any. */ | |
83 | ||
84 | if (filename) | |
85 | { | |
86 | char *scratch_pathname; | |
87 | int scratch_chan; | |
88 | ||
89 | filename = tilde_expand (filename); | |
90 | make_cleanup (free, filename); | |
91 | ||
92 | /* FIXME, if writeable is set, open for read/write. */ | |
93 | scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0, | |
94 | &scratch_pathname); | |
95 | if (scratch_chan < 0) | |
96 | perror_with_name (filename); | |
97 | ||
98 | exec_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan); | |
99 | if (!exec_bfd) | |
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)); | |
105 | ||
106 | #if FIXME | |
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) | |
113 | { | |
114 | struct coffheader ch; | |
115 | int val; | |
116 | val = lseek (execchan, -(sizeof (AOUTHDR) + sizeof (ch)), 1); | |
117 | if (val == -1) | |
118 | perror_with_name (filename); | |
119 | val = myread (execchan, &ch, sizeof (ch)); | |
120 | if (val < 0) | |
121 | perror_with_name (filename); | |
122 | text_start = ch.text_start; | |
123 | exec_data_start = ch.data_start; | |
124 | } else | |
125 | #endif | |
126 | { | |
127 | text_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); | |
131 | } | |
132 | #endif FIXME | |
133 | ||
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)); | |
137 | ||
138 | validate_files (); | |
139 | ||
140 | push_target (&exec_ops); | |
141 | ||
142 | /* Tell display code (if any) about the changed file name. */ | |
143 | if (exec_file_display_hook) | |
144 | (*exec_file_display_hook) (filename); | |
145 | } | |
146 | else if (from_tty) | |
147 | printf ("No exec file now.\n"); | |
148 | } | |
149 | ||
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? */ | |
153 | ||
154 | void | |
155 | file_command (arg, from_tty) | |
156 | char *arg; | |
157 | int from_tty; | |
158 | { | |
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); | |
163 | } | |
164 | ||
165 | \f | |
166 | /* Locate all mappable sections of a BFD file. */ | |
167 | ||
168 | void | |
169 | add_to_section_table (abfd, asect, table_pp) | |
170 | bfd *abfd; | |
171 | sec_ptr asect; | |
172 | struct section_table **table_pp; | |
173 | { | |
174 | flagword aflag; | |
175 | ||
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)) | |
179 | return; | |
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); | |
183 | (*table_pp)++; | |
184 | } | |
185 | ||
186 | int | |
187 | build_section_table (some_bfd, start, end) | |
188 | bfd *some_bfd; | |
189 | struct section_table **start, **end; | |
190 | { | |
191 | unsigned count; | |
192 | ||
193 | count = bfd_count_sections (some_bfd); | |
194 | if (count == 0) | |
195 | abort(); /* return 1? */ | |
777bef06 JK |
196 | if (*start) |
197 | free (*start); | |
bd5635a1 RP |
198 | *start = (struct section_table *) xmalloc (count * sizeof (**start)); |
199 | *end = *start; | |
200 | bfd_map_over_sections (some_bfd, add_to_section_table, end); | |
201 | if (*end > *start + count) | |
202 | abort(); | |
203 | /* We could realloc the table, but it probably loses for most files. */ | |
204 | return 0; | |
205 | } | |
206 | \f | |
207 | /* Read or write the exec file. | |
208 | ||
209 | Args are address within exec file, address within gdb address-space, | |
210 | length, and a flag indicating whether to read or write. | |
211 | ||
212 | Result is a length: | |
213 | ||
214 | 0: We cannot handle this address and length. | |
215 | > 0: We have handled N bytes starting at this address. | |
216 | (If N == length, we did it all.) We might be able | |
217 | to handle more bytes beyond this length, but no | |
218 | promises. | |
219 | < 0: We cannot handle this address, but if somebody | |
220 | else handles (-N) bytes, we can start from there. | |
221 | ||
222 | The same routine is used to handle both core and exec files; | |
223 | we just tail-call it with more arguments to select between them. */ | |
224 | ||
225 | int | |
226 | xfer_memory (memaddr, myaddr, len, write, abfd, sections, sections_end) | |
227 | CORE_ADDR memaddr; | |
228 | char *myaddr; | |
229 | int len; | |
230 | int write; | |
231 | bfd *abfd; | |
232 | struct section_table *sections, *sections_end; | |
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 = sections; p < 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 (abfd, 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 (abfd, 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 | /* The function called by target_xfer_memory via our target_ops */ | |
278 | ||
279 | int | |
280 | exec_xfer_memory (memaddr, myaddr, len, write) | |
281 | CORE_ADDR memaddr; | |
282 | char *myaddr; | |
283 | int len; | |
284 | int write; | |
285 | { | |
286 | return xfer_memory (memaddr, myaddr, len, write, | |
287 | exec_bfd, exec_sections, exec_sections_end); | |
288 | } | |
289 | ||
290 | ||
291 | #ifdef FIXME | |
292 | #ifdef REG_STACK_SEGMENT | |
293 | /* MOVE TO BFD... */ | |
294 | /* Pyramids and AM29000s have an extra segment in the virtual address space | |
295 | for the (control) stack of register-window frames. The AM29000 folk | |
296 | call it the "register stack" rather than the "memory stack". */ | |
297 | else if (memaddr >= reg_stack_start && memaddr < reg_stack_end) | |
298 | { | |
299 | i = min (len, reg_stack_end - memaddr); | |
300 | fileptr = memaddr - reg_stack_start + reg_stack_offset; | |
301 | wanna_xfer = coredata; | |
302 | } | |
303 | #endif /* REG_STACK_SEGMENT */ | |
304 | #endif FIXME | |
305 | \f | |
306 | static void | |
307 | exec_files_info () | |
308 | { | |
309 | struct section_table *p; | |
310 | ||
311 | printf ("\tExecutable file `%s'.\n", bfd_get_filename(exec_bfd)); | |
312 | ||
313 | for (p = exec_sections; p < exec_sections_end; p++) | |
314 | printf("\texecutable from 0x%08x to 0x%08x is %s\n", | |
315 | p->addr, p->endaddr, | |
316 | bfd_section_name (exec_bfd, p->sec_ptr)); | |
317 | } | |
318 | ||
f2fc6e7a JK |
319 | static void |
320 | set_section_command (args, from_tty) | |
321 | char *args; | |
322 | int from_tty; | |
323 | { | |
324 | struct section_table *p; | |
325 | char *secname; | |
326 | unsigned seclen; | |
327 | unsigned long secaddr; | |
328 | char secprint[100]; | |
329 | long offset; | |
330 | ||
331 | if (args == 0) | |
332 | error ("Must specify section name and its virtual address"); | |
333 | ||
334 | /* Parse out section name */ | |
335 | for (secname = args; !isspace(*args); args++) ; | |
336 | seclen = args - secname; | |
337 | ||
338 | /* Parse out new virtual address */ | |
339 | secaddr = parse_and_eval_address (args); | |
340 | ||
341 | for (p = exec_sections; p < exec_sections_end; p++) { | |
342 | if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen) | |
343 | && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') { | |
344 | offset = secaddr - p->addr; | |
345 | p->addr += offset; | |
346 | p->endaddr += offset; | |
347 | exec_files_info(); | |
348 | return; | |
349 | } | |
350 | } | |
351 | if (seclen >= sizeof (secprint)) | |
352 | seclen = sizeof (secprint) - 1; | |
353 | strncpy (secprint, secname, seclen); | |
354 | secprint[seclen] = '\0'; | |
355 | error ("Section %s not found", secprint); | |
356 | } | |
357 | ||
bd5635a1 RP |
358 | struct target_ops exec_ops = { |
359 | "exec", "Local exec file", | |
f2fc6e7a JK |
360 | "Use an executable file as a target.\n\ |
361 | Specify the filename of the executable file.", | |
bd5635a1 RP |
362 | exec_file_command, exec_close, /* open, close */ |
363 | child_attach, 0, 0, 0, /* attach, detach, resume, wait, */ | |
364 | 0, 0, /* fetch_registers, store_registers, */ | |
365 | 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */ | |
366 | exec_xfer_memory, exec_files_info, | |
367 | 0, 0, /* insert_breakpoint, remove_breakpoint, */ | |
368 | 0, 0, 0, 0, 0, /* terminal stuff */ | |
64e52224 JG |
369 | 0, 0, /* kill, load */ |
370 | add_syms_addr_command, | |
371 | 0, 0, /* call fn, lookup sym */ | |
bd5635a1 RP |
372 | child_create_inferior, |
373 | 0, /* mourn_inferior */ | |
374 | file_stratum, 0, /* next */ | |
375 | 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */ | |
376 | OPS_MAGIC, /* Always the last thing */ | |
377 | }; | |
378 | ||
379 | void | |
380 | _initialize_exec() | |
381 | { | |
382 | ||
383 | add_com ("file", class_files, file_command, | |
384 | "Use FILE as program to be debugged.\n\ | |
385 | It is read for its symbols, for getting the contents of pure memory,\n\ | |
386 | and it is the program executed when you use the `run' command.\n\ | |
387 | If FILE cannot be found as specified, your execution directory path\n\ | |
388 | ($PATH) is searched for a command of that name.\n\ | |
389 | No arg means to have no executable file and no symbols."); | |
390 | ||
391 | add_com ("exec-file", class_files, exec_file_command, | |
392 | "Use FILE as program for getting contents of pure memory.\n\ | |
393 | If FILE cannot be found as specified, your execution directory path\n\ | |
394 | is searched for a command of that name.\n\ | |
395 | No arg means have no executable file."); | |
396 | ||
f2fc6e7a JK |
397 | add_com ("section", class_files, set_section_command, |
398 | "Change the base address of section SECTION of the exec file to ADDR.\n\ | |
399 | This can be used if the exec file does not contain section addresses,\n\ | |
400 | (such as in the a.out format), or when the addresses specified in the\n\ | |
401 | file itself are wrong. Each section must be changed separately. The\n\ | |
402 | ``info files'' command lists all the sections and their addresses."); | |
403 | ||
bd5635a1 RP |
404 | add_target (&exec_ops); |
405 | } |