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