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