]>
Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Work with executable files, for GDB. |
be772100 | 2 | Copyright 1988, 1989, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
bdbd5f50 | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
bdbd5f50 JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
bdbd5f50 | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
bdbd5f50 JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 19 | |
bd5635a1 | 20 | #include "defs.h" |
bd5635a1 RP |
21 | #include "frame.h" |
22 | #include "inferior.h" | |
23 | #include "target.h" | |
bdbd5f50 | 24 | #include "gdbcmd.h" |
bd5635a1 RP |
25 | |
26 | #ifdef USG | |
27 | #include <sys/types.h> | |
28 | #endif | |
29 | ||
30 | #include <sys/param.h> | |
31 | #include <fcntl.h> | |
bdbd5f50 | 32 | #include <string.h> |
bd5635a1 RP |
33 | |
34 | #include "gdbcore.h" | |
35 | ||
f2ebc25f | 36 | #include <ctype.h> |
bd5635a1 RP |
37 | #include <sys/stat.h> |
38 | ||
be772100 JG |
39 | /* Prototypes for local functions */ |
40 | ||
41 | static void | |
42 | add_to_section_table PARAMS ((bfd *, sec_ptr, PTR)); | |
43 | ||
44 | static void | |
45 | exec_close PARAMS ((int)); | |
46 | ||
47 | static void | |
48 | file_command PARAMS ((char *, int)); | |
49 | ||
50 | static void | |
51 | set_section_command PARAMS ((char *, int)); | |
52 | ||
53 | static void | |
54 | exec_files_info PARAMS ((struct target_ops *)); | |
55 | ||
56 | extern int info_verbose; | |
bd5635a1 RP |
57 | |
58 | /* The Binary File Descriptor handle for the executable file. */ | |
59 | ||
60 | bfd *exec_bfd = NULL; | |
61 | ||
bdbd5f50 | 62 | /* Whether to open exec and core files read-only or read-write. */ |
bd5635a1 | 63 | |
bdbd5f50 | 64 | int write_files = 0; |
bd5635a1 | 65 | |
7730bd5a JG |
66 | /* Text start and end addresses (KLUDGE) if needed */ |
67 | ||
dad0e12d | 68 | #ifdef NEED_TEXT_START_END |
7730bd5a JG |
69 | CORE_ADDR text_start = 0; |
70 | CORE_ADDR text_end = 0; | |
71 | #endif | |
72 | ||
bd5635a1 RP |
73 | /* Forward decl */ |
74 | ||
75 | extern struct target_ops exec_ops; | |
76 | ||
bdbd5f50 | 77 | /* ARGSUSED */ |
be772100 | 78 | static void |
bd5635a1 RP |
79 | exec_close (quitting) |
80 | int quitting; | |
81 | { | |
82 | if (exec_bfd) { | |
83 | bfd_close (exec_bfd); | |
84 | exec_bfd = NULL; | |
85 | } | |
be772100 JG |
86 | if (exec_ops.to_sections) { |
87 | free ((PTR)exec_ops.to_sections); | |
88 | exec_ops.to_sections = NULL; | |
89 | exec_ops.to_sections_end = NULL; | |
bdbd5f50 | 90 | } |
bd5635a1 RP |
91 | } |
92 | ||
be772100 JG |
93 | /* Process the first arg in ARGS as the new exec file. |
94 | ||
95 | Note that we have to explicitly ignore additional args, since we can | |
96 | be called from file_command(), which also calls symbol_file_command() | |
97 | which can take multiple args. */ | |
98 | ||
bd5635a1 | 99 | void |
be772100 JG |
100 | exec_file_command (args, from_tty) |
101 | char *args; | |
bd5635a1 RP |
102 | int from_tty; |
103 | { | |
be772100 JG |
104 | char **argv; |
105 | char *filename; | |
106 | ||
f2fc6e7a | 107 | target_preopen (from_tty); |
bd5635a1 RP |
108 | |
109 | /* Remove any previous exec file. */ | |
110 | unpush_target (&exec_ops); | |
111 | ||
112 | /* Now open and digest the file the user requested, if any. */ | |
113 | ||
be772100 | 114 | if (args) |
bd5635a1 RP |
115 | { |
116 | char *scratch_pathname; | |
117 | int scratch_chan; | |
118 | ||
be772100 JG |
119 | /* Scan through the args and pick up the first non option arg |
120 | as the filename. */ | |
121 | ||
122 | if ((argv = buildargv (args)) == NULL) | |
123 | { | |
124 | nomem (0); | |
125 | } | |
126 | make_cleanup (freeargv, (char *) argv); | |
127 | ||
128 | for (; (*argv != NULL) && (**argv == '-'); argv++) {;} | |
129 | if (*argv == NULL) | |
130 | { | |
131 | error ("no exec file name was specified"); | |
132 | } | |
133 | ||
134 | filename = tilde_expand (*argv); | |
bd5635a1 RP |
135 | make_cleanup (free, filename); |
136 | ||
bdbd5f50 JG |
137 | scratch_chan = openp (getenv ("PATH"), 1, filename, |
138 | write_files? O_RDWR: O_RDONLY, 0, | |
bd5635a1 RP |
139 | &scratch_pathname); |
140 | if (scratch_chan < 0) | |
141 | perror_with_name (filename); | |
142 | ||
143 | exec_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan); | |
144 | if (!exec_bfd) | |
145 | error ("Could not open `%s' as an executable file: %s", | |
146 | scratch_pathname, bfd_errmsg (bfd_error)); | |
147 | if (!bfd_check_format (exec_bfd, bfd_object)) | |
148 | error ("\"%s\": not in executable format: %s.", | |
149 | scratch_pathname, bfd_errmsg (bfd_error)); | |
150 | ||
be772100 JG |
151 | if (build_section_table (exec_bfd, &exec_ops.to_sections, |
152 | &exec_ops.to_sections_end)) | |
bd5635a1 RP |
153 | error ("Can't find the file sections in `%s': %s", |
154 | exec_bfd->filename, bfd_errmsg (bfd_error)); | |
155 | ||
dad0e12d | 156 | #ifdef NEED_TEXT_START_END |
7730bd5a JG |
157 | /* This is a KLUDGE (FIXME) because a few places in a few ports |
158 | (29K springs to mind) need this info for now. */ | |
159 | { | |
160 | struct section_table *p; | |
be772100 | 161 | for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) |
dad0e12d JG |
162 | if (!strcmp (".text", bfd_section_name (p->bfd, p->sec_ptr))) |
163 | { | |
164 | text_start = p->addr; | |
165 | text_end = p->endaddr; | |
166 | break; | |
167 | } | |
7730bd5a JG |
168 | } |
169 | #endif | |
170 | ||
bd5635a1 RP |
171 | validate_files (); |
172 | ||
173 | push_target (&exec_ops); | |
174 | ||
175 | /* Tell display code (if any) about the changed file name. */ | |
176 | if (exec_file_display_hook) | |
177 | (*exec_file_display_hook) (filename); | |
178 | } | |
179 | else if (from_tty) | |
180 | printf ("No exec file now.\n"); | |
181 | } | |
182 | ||
183 | /* Set both the exec file and the symbol file, in one command. | |
184 | What a novelty. Why did GDB go through four major releases before this | |
185 | command was added? */ | |
186 | ||
be772100 | 187 | static void |
bd5635a1 RP |
188 | file_command (arg, from_tty) |
189 | char *arg; | |
190 | int from_tty; | |
191 | { | |
192 | /* FIXME, if we lose on reading the symbol file, we should revert | |
193 | the exec file, but that's rough. */ | |
194 | exec_file_command (arg, from_tty); | |
195 | symbol_file_command (arg, from_tty); | |
196 | } | |
197 | ||
198 | \f | |
bdbd5f50 JG |
199 | /* Locate all mappable sections of a BFD file. |
200 | table_pp_char is a char * to get it through bfd_map_over_sections; | |
201 | we cast it back to its proper type. */ | |
bd5635a1 | 202 | |
be772100 | 203 | static void |
bdbd5f50 | 204 | add_to_section_table (abfd, asect, table_pp_char) |
bd5635a1 RP |
205 | bfd *abfd; |
206 | sec_ptr asect; | |
be772100 | 207 | PTR table_pp_char; |
bd5635a1 | 208 | { |
bdbd5f50 | 209 | struct section_table **table_pp = (struct section_table **)table_pp_char; |
bd5635a1 RP |
210 | flagword aflag; |
211 | ||
212 | aflag = bfd_get_section_flags (abfd, asect); | |
213 | /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */ | |
214 | if (!(aflag & SEC_LOAD)) | |
215 | return; | |
dad0e12d JG |
216 | if (0 == bfd_section_size (abfd, asect)) |
217 | return; | |
bdbd5f50 | 218 | (*table_pp)->bfd = abfd; |
bd5635a1 RP |
219 | (*table_pp)->sec_ptr = asect; |
220 | (*table_pp)->addr = bfd_section_vma (abfd, asect); | |
221 | (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); | |
222 | (*table_pp)++; | |
223 | } | |
224 | ||
be772100 JG |
225 | /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. |
226 | Returns 0 if OK, 1 on error. */ | |
227 | ||
bd5635a1 RP |
228 | int |
229 | build_section_table (some_bfd, start, end) | |
230 | bfd *some_bfd; | |
231 | struct section_table **start, **end; | |
232 | { | |
233 | unsigned count; | |
234 | ||
235 | count = bfd_count_sections (some_bfd); | |
236 | if (count == 0) | |
237 | abort(); /* return 1? */ | |
777bef06 | 238 | if (*start) |
be772100 | 239 | free ((PTR)*start); |
bd5635a1 RP |
240 | *start = (struct section_table *) xmalloc (count * sizeof (**start)); |
241 | *end = *start; | |
bdbd5f50 | 242 | bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end); |
bd5635a1 RP |
243 | if (*end > *start + count) |
244 | abort(); | |
245 | /* We could realloc the table, but it probably loses for most files. */ | |
246 | return 0; | |
247 | } | |
248 | \f | |
249 | /* Read or write the exec file. | |
250 | ||
bdbd5f50 | 251 | Args are address within a BFD file, address within gdb address-space, |
bd5635a1 RP |
252 | length, and a flag indicating whether to read or write. |
253 | ||
254 | Result is a length: | |
255 | ||
256 | 0: We cannot handle this address and length. | |
257 | > 0: We have handled N bytes starting at this address. | |
258 | (If N == length, we did it all.) We might be able | |
259 | to handle more bytes beyond this length, but no | |
260 | promises. | |
261 | < 0: We cannot handle this address, but if somebody | |
262 | else handles (-N) bytes, we can start from there. | |
263 | ||
264 | The same routine is used to handle both core and exec files; | |
265 | we just tail-call it with more arguments to select between them. */ | |
266 | ||
267 | int | |
bdbd5f50 | 268 | xfer_memory (memaddr, myaddr, len, write, target) |
bd5635a1 RP |
269 | CORE_ADDR memaddr; |
270 | char *myaddr; | |
271 | int len; | |
272 | int write; | |
bdbd5f50 | 273 | struct target_ops *target; |
bd5635a1 RP |
274 | { |
275 | boolean res; | |
276 | struct section_table *p; | |
277 | CORE_ADDR nextsectaddr, memend; | |
be772100 | 278 | boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); |
bd5635a1 RP |
279 | |
280 | if (len <= 0) | |
281 | abort(); | |
282 | ||
283 | memend = memaddr + len; | |
284 | xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents; | |
285 | nextsectaddr = memend; | |
286 | ||
be772100 | 287 | for (p = target->to_sections; p < target->to_sections_end; p++) |
bd5635a1 RP |
288 | { |
289 | if (p->addr <= memaddr) | |
290 | if (p->endaddr >= memend) | |
291 | { | |
292 | /* Entire transfer is within this section. */ | |
bdbd5f50 | 293 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
bd5635a1 RP |
294 | return (res != false)? len: 0; |
295 | } | |
296 | else if (p->endaddr <= memaddr) | |
297 | { | |
298 | /* This section ends before the transfer starts. */ | |
299 | continue; | |
300 | } | |
301 | else | |
302 | { | |
303 | /* This section overlaps the transfer. Just do half. */ | |
304 | len = p->endaddr - memaddr; | |
bdbd5f50 | 305 | res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); |
bd5635a1 RP |
306 | return (res != false)? len: 0; |
307 | } | |
308 | else if (p->addr < nextsectaddr) | |
309 | nextsectaddr = p->addr; | |
310 | } | |
311 | ||
312 | if (nextsectaddr >= memend) | |
313 | return 0; /* We can't help */ | |
314 | else | |
315 | return - (nextsectaddr - memaddr); /* Next boundary where we can help */ | |
316 | } | |
317 | ||
bd5635a1 RP |
318 | #ifdef FIXME |
319 | #ifdef REG_STACK_SEGMENT | |
320 | /* MOVE TO BFD... */ | |
321 | /* Pyramids and AM29000s have an extra segment in the virtual address space | |
322 | for the (control) stack of register-window frames. The AM29000 folk | |
323 | call it the "register stack" rather than the "memory stack". */ | |
324 | else if (memaddr >= reg_stack_start && memaddr < reg_stack_end) | |
325 | { | |
326 | i = min (len, reg_stack_end - memaddr); | |
327 | fileptr = memaddr - reg_stack_start + reg_stack_offset; | |
328 | wanna_xfer = coredata; | |
329 | } | |
330 | #endif /* REG_STACK_SEGMENT */ | |
331 | #endif FIXME | |
332 | \f | |
be772100 JG |
333 | void |
334 | print_section_info (t, abfd) | |
335 | struct target_ops *t; | |
336 | bfd *abfd; | |
bd5635a1 RP |
337 | { |
338 | struct section_table *p; | |
339 | ||
be772100 | 340 | printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); |
7d9884b9 | 341 | wrap_here (" "); |
be772100 | 342 | printf_filtered ("file type %s.\n", bfd_get_target(abfd)); |
bd5635a1 | 343 | |
be772100 | 344 | for (p = t->to_sections; p < t->to_sections_end; p++) { |
7d9884b9 | 345 | printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08")); |
be772100 JG |
346 | printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08")); |
347 | if (info_verbose) | |
348 | printf_filtered (" @ %s", | |
349 | local_hex_string_custom (p->sec_ptr->filepos, "08")); | |
350 | printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr)); | |
351 | if (p->bfd != abfd) { | |
352 | printf_filtered (" in %s", bfd_get_filename (p->bfd)); | |
353 | } | |
354 | printf_filtered ("\n"); | |
dad0e12d | 355 | } |
bd5635a1 RP |
356 | } |
357 | ||
be772100 JG |
358 | static void |
359 | exec_files_info (t) | |
360 | struct target_ops *t; | |
361 | { | |
362 | print_section_info (t, exec_bfd); | |
363 | } | |
364 | ||
f2fc6e7a JK |
365 | static void |
366 | set_section_command (args, from_tty) | |
367 | char *args; | |
368 | int from_tty; | |
369 | { | |
370 | struct section_table *p; | |
371 | char *secname; | |
372 | unsigned seclen; | |
373 | unsigned long secaddr; | |
374 | char secprint[100]; | |
375 | long offset; | |
376 | ||
377 | if (args == 0) | |
378 | error ("Must specify section name and its virtual address"); | |
379 | ||
380 | /* Parse out section name */ | |
381 | for (secname = args; !isspace(*args); args++) ; | |
382 | seclen = args - secname; | |
383 | ||
384 | /* Parse out new virtual address */ | |
385 | secaddr = parse_and_eval_address (args); | |
386 | ||
be772100 | 387 | for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { |
f2fc6e7a JK |
388 | if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen) |
389 | && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') { | |
390 | offset = secaddr - p->addr; | |
391 | p->addr += offset; | |
392 | p->endaddr += offset; | |
be772100 JG |
393 | if (from_tty) |
394 | exec_files_info(&exec_ops); | |
f2fc6e7a JK |
395 | return; |
396 | } | |
397 | } | |
398 | if (seclen >= sizeof (secprint)) | |
399 | seclen = sizeof (secprint) - 1; | |
400 | strncpy (secprint, secname, seclen); | |
401 | secprint[seclen] = '\0'; | |
402 | error ("Section %s not found", secprint); | |
403 | } | |
404 | ||
bd5635a1 RP |
405 | struct target_ops exec_ops = { |
406 | "exec", "Local exec file", | |
f2fc6e7a JK |
407 | "Use an executable file as a target.\n\ |
408 | Specify the filename of the executable file.", | |
bd5635a1 RP |
409 | exec_file_command, exec_close, /* open, close */ |
410 | child_attach, 0, 0, 0, /* attach, detach, resume, wait, */ | |
411 | 0, 0, /* fetch_registers, store_registers, */ | |
412 | 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */ | |
bdbd5f50 | 413 | xfer_memory, exec_files_info, |
bd5635a1 RP |
414 | 0, 0, /* insert_breakpoint, remove_breakpoint, */ |
415 | 0, 0, 0, 0, 0, /* terminal stuff */ | |
64e52224 | 416 | 0, 0, /* kill, load */ |
be772100 | 417 | 0, /* lookup sym */ |
bd5635a1 RP |
418 | child_create_inferior, |
419 | 0, /* mourn_inferior */ | |
420 | file_stratum, 0, /* next */ | |
421 | 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */ | |
bdbd5f50 | 422 | 0, 0, /* section pointers */ |
bd5635a1 RP |
423 | OPS_MAGIC, /* Always the last thing */ |
424 | }; | |
425 | ||
426 | void | |
427 | _initialize_exec() | |
428 | { | |
429 | ||
430 | add_com ("file", class_files, file_command, | |
431 | "Use FILE as program to be debugged.\n\ | |
432 | It is read for its symbols, for getting the contents of pure memory,\n\ | |
433 | and it is the program executed when you use the `run' command.\n\ | |
434 | If FILE cannot be found as specified, your execution directory path\n\ | |
435 | ($PATH) is searched for a command of that name.\n\ | |
436 | No arg means to have no executable file and no symbols."); | |
437 | ||
438 | add_com ("exec-file", class_files, exec_file_command, | |
439 | "Use FILE as program for getting contents of pure memory.\n\ | |
440 | If FILE cannot be found as specified, your execution directory path\n\ | |
441 | is searched for a command of that name.\n\ | |
442 | No arg means have no executable file."); | |
443 | ||
f2fc6e7a JK |
444 | add_com ("section", class_files, set_section_command, |
445 | "Change the base address of section SECTION of the exec file to ADDR.\n\ | |
446 | This can be used if the exec file does not contain section addresses,\n\ | |
447 | (such as in the a.out format), or when the addresses specified in the\n\ | |
448 | file itself are wrong. Each section must be changed separately. The\n\ | |
449 | ``info files'' command lists all the sections and their addresses."); | |
450 | ||
bdbd5f50 JG |
451 | add_show_from_set |
452 | (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files, | |
453 | "Set writing into executable and core files.", | |
454 | &setlist), | |
455 | &showlist); | |
456 | ||
bd5635a1 RP |
457 | add_target (&exec_ops); |
458 | } |