Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Work with executable files, for GDB. |
d367c7b4 KS |
2 | Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1997 |
3 | Free Software Foundation, Inc. | |
bd5635a1 RP |
4 | |
5 | This file is part of GDB. | |
6 | ||
bdbd5f50 | 7 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 8 | it under the terms of the GNU General Public License as published by |
bdbd5f50 JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
bd5635a1 | 11 | |
bdbd5f50 | 12 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
bdbd5f50 | 18 | along with this program; if not, write to the Free Software |
3f17fed8 | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 | 20 | |
bd5635a1 | 21 | #include "defs.h" |
bd5635a1 RP |
22 | #include "frame.h" |
23 | #include "inferior.h" | |
24 | #include "target.h" | |
bdbd5f50 | 25 | #include "gdbcmd.h" |
b02fd8ca | 26 | #include "language.h" |
806f810b SS |
27 | #include "symfile.h" |
28 | #include "objfiles.h" | |
bd5635a1 RP |
29 | |
30 | #ifdef USG | |
31 | #include <sys/types.h> | |
32 | #endif | |
33 | ||
bd5635a1 | 34 | #include <fcntl.h> |
2b576293 | 35 | #include "gdb_string.h" |
bd5635a1 RP |
36 | |
37 | #include "gdbcore.h" | |
38 | ||
f2ebc25f | 39 | #include <ctype.h> |
2b576293 | 40 | #include "gdb_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 | ||
d367c7b4 KS |
49 | void (*file_changed_hook) PARAMS ((char *)); |
50 | ||
be772100 JG |
51 | /* Prototypes for local functions */ |
52 | ||
806f810b | 53 | static void add_to_section_table PARAMS ((bfd *, sec_ptr, PTR)); |
be772100 | 54 | |
806f810b | 55 | static void exec_close PARAMS ((int)); |
be772100 | 56 | |
806f810b | 57 | static void file_command PARAMS ((char *, int)); |
be772100 | 58 | |
806f810b | 59 | static void set_section_command PARAMS ((char *, int)); |
be772100 | 60 | |
806f810b | 61 | static void exec_files_info PARAMS ((struct target_ops *)); |
be772100 | 62 | |
b607efe7 FF |
63 | static void bfdsec_to_vmap PARAMS ((bfd *, sec_ptr, PTR)); |
64 | ||
65 | static int ignore PARAMS ((CORE_ADDR, char *)); | |
66 | ||
be772100 | 67 | extern int info_verbose; |
bd5635a1 RP |
68 | |
69 | /* The Binary File Descriptor handle for the executable file. */ | |
70 | ||
71 | bfd *exec_bfd = NULL; | |
72 | ||
bdbd5f50 | 73 | /* Whether to open exec and core files read-only or read-write. */ |
bd5635a1 | 74 | |
bdbd5f50 | 75 | int write_files = 0; |
bd5635a1 | 76 | |
7730bd5a JG |
77 | /* Text start and end addresses (KLUDGE) if needed */ |
78 | ||
dad0e12d | 79 | #ifdef NEED_TEXT_START_END |
7730bd5a JG |
80 | CORE_ADDR text_start = 0; |
81 | CORE_ADDR text_end = 0; | |
82 | #endif | |
83 | ||
806f810b SS |
84 | struct vmap *vmap; |
85 | ||
bd5635a1 RP |
86 | /* Forward decl */ |
87 | ||
88 | extern struct target_ops exec_ops; | |
89 | ||
bdbd5f50 | 90 | /* ARGSUSED */ |
be772100 | 91 | static void |
bd5635a1 RP |
92 | exec_close (quitting) |
93 | int quitting; | |
94 | { | |
806f810b SS |
95 | int need_symtab_cleanup = 0; |
96 | struct vmap *vp, *nxt; | |
97 | ||
98 | for (nxt = vmap; nxt != NULL; ) | |
99 | { | |
100 | vp = nxt; | |
101 | nxt = vp->nxt; | |
102 | ||
103 | /* if there is an objfile associated with this bfd, | |
104 | free_objfile() will do proper cleanup of objfile *and* bfd. */ | |
105 | ||
106 | if (vp->objfile) | |
107 | { | |
108 | free_objfile (vp->objfile); | |
109 | need_symtab_cleanup = 1; | |
110 | } | |
111 | else if (vp->bfd != exec_bfd) | |
e7b5942b JK |
112 | /* FIXME-leak: We should be freeing vp->name too, I think. */ |
113 | if (!bfd_close (vp->bfd)) | |
114 | warning ("cannot close \"%s\": %s", | |
115 | vp->name, bfd_errmsg (bfd_get_error ())); | |
806f810b SS |
116 | |
117 | /* FIXME: This routine is #if 0'd in symfile.c. What should we | |
118 | be doing here? Should we just free everything in | |
e7b5942b JK |
119 | vp->objfile->symtabs? Should free_objfile do that? |
120 | FIXME-as-well: free_objfile already free'd vp->name, so it isn't | |
121 | valid here. */ | |
806f810b SS |
122 | free_named_symtabs (vp->name); |
123 | free (vp); | |
124 | } | |
e7b5942b | 125 | |
806f810b SS |
126 | vmap = NULL; |
127 | ||
128 | if (exec_bfd) | |
129 | { | |
130 | char *name = bfd_get_filename (exec_bfd); | |
131 | ||
e7b5942b JK |
132 | if (!bfd_close (exec_bfd)) |
133 | warning ("cannot close \"%s\": %s", | |
134 | name, bfd_errmsg (bfd_get_error ())); | |
806f810b SS |
135 | free (name); |
136 | exec_bfd = NULL; | |
137 | } | |
138 | ||
139 | if (exec_ops.to_sections) | |
140 | { | |
141 | free ((PTR)exec_ops.to_sections); | |
142 | exec_ops.to_sections = NULL; | |
143 | exec_ops.to_sections_end = NULL; | |
144 | } | |
bd5635a1 RP |
145 | } |
146 | ||
be772100 JG |
147 | /* Process the first arg in ARGS as the new exec file. |
148 | ||
149 | Note that we have to explicitly ignore additional args, since we can | |
150 | be called from file_command(), which also calls symbol_file_command() | |
151 | which can take multiple args. */ | |
152 | ||
bd5635a1 | 153 | void |
be772100 JG |
154 | exec_file_command (args, from_tty) |
155 | char *args; | |
bd5635a1 RP |
156 | int from_tty; |
157 | { | |
be772100 JG |
158 | char **argv; |
159 | char *filename; | |
160 | ||
f2fc6e7a | 161 | target_preopen (from_tty); |
bd5635a1 RP |
162 | |
163 | /* Remove any previous exec file. */ | |
164 | unpush_target (&exec_ops); | |
165 | ||
166 | /* Now open and digest the file the user requested, if any. */ | |
167 | ||
be772100 | 168 | if (args) |
bd5635a1 RP |
169 | { |
170 | char *scratch_pathname; | |
171 | int scratch_chan; | |
172 | ||
be772100 | 173 | /* Scan through the args and pick up the first non option arg |
806f810b SS |
174 | as the filename. */ |
175 | ||
176 | argv = buildargv (args); | |
177 | if (argv == NULL) | |
178 | nomem (0); | |
be772100 | 179 | |
be772100 JG |
180 | make_cleanup (freeargv, (char *) argv); |
181 | ||
182 | for (; (*argv != NULL) && (**argv == '-'); argv++) {;} | |
183 | if (*argv == NULL) | |
806f810b | 184 | error ("no exec file name was specified"); |
be772100 JG |
185 | |
186 | filename = tilde_expand (*argv); | |
bd5635a1 RP |
187 | make_cleanup (free, filename); |
188 | ||
bdbd5f50 | 189 | scratch_chan = openp (getenv ("PATH"), 1, filename, |
a8e033f2 | 190 | write_files? O_RDWR|O_BINARY: O_RDONLY|O_BINARY, 0, |
bd5635a1 | 191 | &scratch_pathname); |
d367c7b4 | 192 | #if defined(__GO32__) || defined(_WIN32) |
3f17fed8 GN |
193 | if (scratch_chan < 0) |
194 | { | |
195 | char *exename = alloca (strlen (filename) + 5); | |
196 | strcat (strcpy (exename, filename), ".exe"); | |
197 | scratch_chan = openp (getenv ("PATH"), 1, exename, write_files ? | |
198 | O_RDWR|O_BINARY : O_RDONLY|O_BINARY, 0, &scratch_pathname); | |
199 | } | |
200 | #endif | |
bd5635a1 RP |
201 | if (scratch_chan < 0) |
202 | perror_with_name (filename); | |
b1eaba9a | 203 | exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan); |
3f17fed8 | 204 | |
bd5635a1 | 205 | if (!exec_bfd) |
806f810b | 206 | error ("\"%s\": could not open as an executable file: %s", |
c4a081e1 | 207 | scratch_pathname, bfd_errmsg (bfd_get_error ())); |
3f17fed8 GN |
208 | |
209 | /* At this point, scratch_pathname and exec_bfd->name both point to the | |
210 | same malloc'd string. However exec_close() will attempt to free it | |
211 | via the exec_bfd->name pointer, so we need to make another copy and | |
212 | leave exec_bfd as the new owner of the original copy. */ | |
213 | scratch_pathname = strdup (scratch_pathname); | |
214 | make_cleanup (free, scratch_pathname); | |
215 | ||
bd5635a1 | 216 | if (!bfd_check_format (exec_bfd, bfd_object)) |
b02fd8ca JK |
217 | { |
218 | /* Make sure to close exec_bfd, or else "run" might try to use | |
219 | it. */ | |
220 | exec_close (0); | |
806f810b SS |
221 | error ("\"%s\": not in executable format: %s", |
222 | scratch_pathname, bfd_errmsg (bfd_get_error ())); | |
223 | } | |
224 | ||
225 | /* FIXME - This should only be run for RS6000, but the ifdef is a poor | |
226 | way to accomplish. */ | |
227 | #ifdef IBM6000_TARGET | |
228 | /* Setup initial vmap. */ | |
229 | ||
230 | map_vmap (exec_bfd, 0); | |
231 | if (vmap == NULL) | |
232 | { | |
233 | /* Make sure to close exec_bfd, or else "run" might try to use | |
234 | it. */ | |
235 | exec_close (0); | |
236 | error ("\"%s\": can't find the file sections: %s", | |
c4a081e1 | 237 | scratch_pathname, bfd_errmsg (bfd_get_error ())); |
b02fd8ca | 238 | } |
806f810b | 239 | #endif /* IBM6000_TARGET */ |
bd5635a1 | 240 | |
be772100 JG |
241 | if (build_section_table (exec_bfd, &exec_ops.to_sections, |
242 | &exec_ops.to_sections_end)) | |
b02fd8ca JK |
243 | { |
244 | /* Make sure to close exec_bfd, or else "run" might try to use | |
245 | it. */ | |
246 | exec_close (0); | |
806f810b SS |
247 | error ("\"%s\": can't find the file sections: %s", |
248 | scratch_pathname, bfd_errmsg (bfd_get_error ())); | |
b02fd8ca | 249 | } |
bd5635a1 | 250 | |
dad0e12d | 251 | #ifdef NEED_TEXT_START_END |
b02fd8ca JK |
252 | |
253 | /* text_end is sometimes used for where to put call dummies. A | |
254 | few ports use these for other purposes too. */ | |
255 | ||
7730bd5a JG |
256 | { |
257 | struct section_table *p; | |
b02fd8ca JK |
258 | |
259 | /* Set text_start to the lowest address of the start of any | |
260 | readonly code section and set text_end to the highest | |
261 | address of the end of any readonly code section. */ | |
e7b5942b JK |
262 | /* FIXME: The comment above does not match the code. The code |
263 | checks for sections with are either code *or* readonly. */ | |
b02fd8ca JK |
264 | |
265 | text_start = ~(CORE_ADDR)0; | |
266 | text_end = (CORE_ADDR)0; | |
be772100 | 267 | for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) |
b83ed019 | 268 | if (bfd_get_section_flags (p->bfd, p->the_bfd_section) |
b02fd8ca | 269 | & (SEC_CODE | SEC_READONLY)) |
dad0e12d | 270 | { |
b02fd8ca JK |
271 | if (text_start > p->addr) |
272 | text_start = p->addr; | |
273 | if (text_end < p->endaddr) | |
274 | text_end = p->endaddr; | |
dad0e12d | 275 | } |
7730bd5a JG |
276 | } |
277 | #endif | |
278 | ||
bd5635a1 RP |
279 | validate_files (); |
280 | ||
b83ed019 | 281 | set_endian_from_file (exec_bfd); |
d367c7b4 | 282 | set_architecture_from_file (exec_bfd); |
b83ed019 | 283 | |
bd5635a1 RP |
284 | push_target (&exec_ops); |
285 | ||
286 | /* Tell display code (if any) about the changed file name. */ | |
287 | if (exec_file_display_hook) | |
288 | (*exec_file_display_hook) (filename); | |
289 | } | |
290 | else if (from_tty) | |
d367c7b4 | 291 | printf_unfiltered ("No executable file now.\n"); |
bd5635a1 RP |
292 | } |
293 | ||
294 | /* Set both the exec file and the symbol file, in one command. | |
295 | What a novelty. Why did GDB go through four major releases before this | |
296 | command was added? */ | |
297 | ||
be772100 | 298 | static void |
bd5635a1 RP |
299 | file_command (arg, from_tty) |
300 | char *arg; | |
301 | int from_tty; | |
302 | { | |
303 | /* FIXME, if we lose on reading the symbol file, we should revert | |
304 | the exec file, but that's rough. */ | |
305 | exec_file_command (arg, from_tty); | |
306 | symbol_file_command (arg, from_tty); | |
d367c7b4 KS |
307 | if (file_changed_hook) |
308 | file_changed_hook (arg); | |
bd5635a1 RP |
309 | } |
310 | ||
311 | \f | |
bdbd5f50 JG |
312 | /* Locate all mappable sections of a BFD file. |
313 | table_pp_char is a char * to get it through bfd_map_over_sections; | |
314 | we cast it back to its proper type. */ | |
bd5635a1 | 315 | |
be772100 | 316 | static void |
bdbd5f50 | 317 | add_to_section_table (abfd, asect, table_pp_char) |
bd5635a1 RP |
318 | bfd *abfd; |
319 | sec_ptr asect; | |
be772100 | 320 | PTR table_pp_char; |
bd5635a1 | 321 | { |
bdbd5f50 | 322 | struct section_table **table_pp = (struct section_table **)table_pp_char; |
bd5635a1 RP |
323 | flagword aflag; |
324 | ||
325 | aflag = bfd_get_section_flags (abfd, asect); | |
c4a081e1 | 326 | if (!(aflag & SEC_ALLOC)) |
bd5635a1 | 327 | return; |
dad0e12d JG |
328 | if (0 == bfd_section_size (abfd, asect)) |
329 | return; | |
bdbd5f50 | 330 | (*table_pp)->bfd = abfd; |
b83ed019 | 331 | (*table_pp)->the_bfd_section = asect; |
bd5635a1 RP |
332 | (*table_pp)->addr = bfd_section_vma (abfd, asect); |
333 | (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); | |
334 | (*table_pp)++; | |
335 | } | |
336 | ||
be772100 JG |
337 | /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. |
338 | Returns 0 if OK, 1 on error. */ | |
339 | ||
bd5635a1 RP |
340 | int |
341 | build_section_table (some_bfd, start, end) | |
342 | bfd *some_bfd; | |
343 | struct section_table **start, **end; | |
344 | { | |
345 | unsigned count; | |
346 | ||
347 | count = bfd_count_sections (some_bfd); | |
777bef06 | 348 | if (*start) |
be772100 | 349 | free ((PTR)*start); |
bd5635a1 RP |
350 | *start = (struct section_table *) xmalloc (count * sizeof (**start)); |
351 | *end = *start; | |
bdbd5f50 | 352 | bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end); |
bd5635a1 RP |
353 | if (*end > *start + count) |
354 | abort(); | |
355 | /* We could realloc the table, but it probably loses for most files. */ | |
356 | return 0; | |
357 | } | |
358 | \f | |
806f810b SS |
359 | static void |
360 | bfdsec_to_vmap(abfd, sect, arg3) | |
361 | bfd *abfd; | |
362 | sec_ptr sect; | |
363 | PTR arg3; | |
364 | { | |
365 | struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3; | |
366 | struct vmap *vp; | |
367 | ||
368 | vp = vmap_bfd->pvmap; | |
369 | ||
370 | if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0) | |
371 | return; | |
372 | ||
373 | if (STREQ (bfd_section_name (abfd, sect), ".text")) | |
374 | { | |
d367c7b4 | 375 | vp->tstart = bfd_section_vma (abfd, sect); |
806f810b | 376 | vp->tend = vp->tstart + bfd_section_size (abfd, sect); |
d367c7b4 KS |
377 | vp->tvma = bfd_section_vma (abfd, sect); |
378 | vp->toffs = sect->filepos; | |
806f810b SS |
379 | } |
380 | else if (STREQ (bfd_section_name (abfd, sect), ".data")) | |
381 | { | |
d367c7b4 | 382 | vp->dstart = bfd_section_vma (abfd, sect); |
806f810b | 383 | vp->dend = vp->dstart + bfd_section_size (abfd, sect); |
d367c7b4 | 384 | vp->dvma = bfd_section_vma (abfd, sect); |
806f810b SS |
385 | } |
386 | /* Silently ignore other types of sections. (FIXME?) */ | |
387 | } | |
388 | ||
389 | /* Make a vmap for ABFD which might be a member of the archive ARCH. | |
390 | Return the new vmap. */ | |
391 | ||
392 | struct vmap * | |
393 | map_vmap (abfd, arch) | |
394 | bfd *abfd; | |
395 | bfd *arch; | |
396 | { | |
397 | struct vmap_and_bfd vmap_bfd; | |
398 | struct vmap *vp, **vpp; | |
399 | ||
e7b5942b | 400 | vp = (struct vmap *) xmalloc (sizeof (*vp)); |
806f810b SS |
401 | memset ((char *) vp, '\0', sizeof (*vp)); |
402 | vp->nxt = 0; | |
403 | vp->bfd = abfd; | |
404 | vp->name = bfd_get_filename (arch ? arch : abfd); | |
405 | vp->member = arch ? bfd_get_filename (abfd) : ""; | |
406 | ||
407 | vmap_bfd.pbfd = arch; | |
408 | vmap_bfd.pvmap = vp; | |
409 | bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd); | |
410 | ||
411 | /* Find the end of the list and append. */ | |
412 | for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt) | |
413 | ; | |
414 | *vpp = vp; | |
415 | ||
416 | return vp; | |
417 | } | |
418 | \f | |
bd5635a1 RP |
419 | /* Read or write the exec file. |
420 | ||
bdbd5f50 | 421 | Args are address within a BFD file, address within gdb address-space, |
bd5635a1 RP |
422 | length, and a flag indicating whether to read or write. |
423 | ||
424 | Result is a length: | |
425 | ||
426 | 0: We cannot handle this address and length. | |
427 | > 0: We have handled N bytes starting at this address. | |
428 | (If N == length, we did it all.) We might be able | |
429 | to handle more bytes beyond this length, but no | |
430 | promises. | |
431 | < 0: We cannot handle this address, but if somebody | |
432 | else handles (-N) bytes, we can start from there. | |
433 | ||
434 | The same routine is used to handle both core and exec files; | |
435 | we just tail-call it with more arguments to select between them. */ | |
436 | ||
437 | int | |
bdbd5f50 | 438 | xfer_memory (memaddr, myaddr, len, write, target) |
bd5635a1 RP |
439 | CORE_ADDR memaddr; |
440 | char *myaddr; | |
441 | int len; | |
442 | int write; | |
bdbd5f50 | 443 | struct target_ops *target; |
bd5635a1 RP |
444 | { |
445 | boolean res; | |
446 | struct section_table *p; | |
447 | CORE_ADDR nextsectaddr, memend; | |
be772100 | 448 | boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); |
d367c7b4 | 449 | asection *section; |
bd5635a1 RP |
450 | |
451 | if (len <= 0) | |
452 | abort(); | |
453 | ||
d367c7b4 KS |
454 | if (overlay_debugging) |
455 | { | |
456 | section = find_pc_overlay (memaddr); | |
457 | if (pc_in_unmapped_range (memaddr, section)) | |
458 | memaddr = overlay_mapped_address (memaddr, section); | |
459 | } | |
460 | ||
bd5635a1 | 461 | memend = memaddr + len; |
806f810b | 462 | xfer_fn = write ? bfd_set_section_contents : bfd_get_section_contents; |
bd5635a1 RP |
463 | nextsectaddr = memend; |
464 | ||
d367c7b4 KS |
465 | #if 0 /* Stu's implementation */ |
466 | /* If a section has been specified, try to use it. Note that we cannot use the | |
467 | specified section directly. This is because it usually comes from the | |
468 | symbol file, which may be different from the exec or core file. Instead, we | |
469 | have to lookup the specified section by name in the bfd associated with | |
470 | to_sections. */ | |
471 | ||
472 | if (target_memory_bfd_section) | |
473 | { | |
474 | asection *s; | |
475 | bfd *abfd; | |
476 | asection *target_section; | |
477 | bfd *target_bfd; | |
478 | ||
479 | s = target_memory_bfd_section; | |
480 | abfd = s->owner; | |
481 | ||
482 | target_bfd = target->to_sections->bfd; | |
483 | target_section = bfd_get_section_by_name (target_bfd, bfd_section_name (abfd, s)); | |
484 | ||
485 | if (target_section) | |
486 | { | |
487 | bfd_vma sec_addr; | |
488 | bfd_size_type sec_size; | |
489 | ||
490 | sec_addr = bfd_section_vma (target_bfd, target_section); | |
491 | sec_size = target_section->_raw_size; | |
492 | ||
493 | /* Make sure the requested memory starts inside the section. */ | |
494 | ||
495 | if (memaddr >= sec_addr | |
496 | && memaddr < sec_addr + sec_size) | |
497 | { | |
498 | /* Cut back length in case request overflows the end of the section. */ | |
499 | len = min (len, sec_addr + sec_size - memaddr); | |
500 | ||
501 | res = xfer_fn (target_bfd, target_section, myaddr, memaddr - sec_addr, len); | |
502 | ||
503 | return res ? len : 0; | |
504 | } | |
505 | } | |
506 | } | |
507 | #endif /* 0, Stu's implementation */ | |
be772100 | 508 | for (p = target->to_sections; p < target->to_sections_end; p++) |
bd5635a1 | 509 | { |
d367c7b4 KS |
510 | if (overlay_debugging && section && p->the_bfd_section && |
511 | strcmp (section->name, p->the_bfd_section->name) != 0) | |
512 | continue; /* not the section we need */ | |
513 | if (memaddr >= p->addr) | |
514 | if (memend <= p->endaddr) | |
bd5635a1 RP |
515 | { |
516 | /* Entire transfer is within this section. */ | |
806f810b SS |
517 | res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, |
518 | memaddr - p->addr, len); | |
b83ed019 | 519 | return (res != 0) ? len : 0; |
bd5635a1 | 520 | } |
d367c7b4 | 521 | else if (memaddr >= p->endaddr) |
bd5635a1 RP |
522 | { |
523 | /* This section ends before the transfer starts. */ | |
524 | continue; | |
525 | } | |
526 | else | |
527 | { | |
528 | /* This section overlaps the transfer. Just do half. */ | |
529 | len = p->endaddr - memaddr; | |
806f810b SS |
530 | res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, |
531 | memaddr - p->addr, len); | |
b83ed019 | 532 | return (res != 0) ? len : 0; |
bd5635a1 | 533 | } |
d367c7b4 KS |
534 | else |
535 | nextsectaddr = min (nextsectaddr, p->addr); | |
bd5635a1 RP |
536 | } |
537 | ||
538 | if (nextsectaddr >= memend) | |
539 | return 0; /* We can't help */ | |
540 | else | |
541 | return - (nextsectaddr - memaddr); /* Next boundary where we can help */ | |
542 | } | |
543 | ||
bd5635a1 RP |
544 | #ifdef FIXME |
545 | #ifdef REG_STACK_SEGMENT | |
546 | /* MOVE TO BFD... */ | |
547 | /* Pyramids and AM29000s have an extra segment in the virtual address space | |
548 | for the (control) stack of register-window frames. The AM29000 folk | |
549 | call it the "register stack" rather than the "memory stack". */ | |
550 | else if (memaddr >= reg_stack_start && memaddr < reg_stack_end) | |
551 | { | |
552 | i = min (len, reg_stack_end - memaddr); | |
553 | fileptr = memaddr - reg_stack_start + reg_stack_offset; | |
554 | wanna_xfer = coredata; | |
555 | } | |
556 | #endif /* REG_STACK_SEGMENT */ | |
117f631e | 557 | #endif /* FIXME */ |
bd5635a1 | 558 | \f |
be772100 JG |
559 | void |
560 | print_section_info (t, abfd) | |
561 | struct target_ops *t; | |
562 | bfd *abfd; | |
bd5635a1 RP |
563 | { |
564 | struct section_table *p; | |
565 | ||
be772100 | 566 | printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); |
7d9884b9 | 567 | wrap_here (" "); |
be772100 | 568 | printf_filtered ("file type %s.\n", bfd_get_target(abfd)); |
b83ed019 ILT |
569 | if (abfd == exec_bfd) |
570 | { | |
571 | printf_filtered ("\tEntry point: "); | |
572 | print_address_numeric (bfd_get_start_address (abfd), 1, gdb_stdout); | |
573 | printf_filtered ("\n"); | |
574 | } | |
c4a081e1 DM |
575 | for (p = t->to_sections; p < t->to_sections_end; p++) |
576 | { | |
577 | /* FIXME-32x64 need a print_address_numeric with field width */ | |
578 | printf_filtered ("\t%s", local_hex_string_custom ((unsigned long) p->addr, "08l")); | |
579 | printf_filtered (" - %s", local_hex_string_custom ((unsigned long) p->endaddr, "08l")); | |
580 | if (info_verbose) | |
581 | printf_filtered (" @ %s", | |
b83ed019 ILT |
582 | local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l")); |
583 | printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section)); | |
c4a081e1 DM |
584 | if (p->bfd != abfd) |
585 | { | |
586 | printf_filtered (" in %s", bfd_get_filename (p->bfd)); | |
587 | } | |
588 | printf_filtered ("\n"); | |
be772100 | 589 | } |
bd5635a1 RP |
590 | } |
591 | ||
be772100 JG |
592 | static void |
593 | exec_files_info (t) | |
806f810b | 594 | struct target_ops *t; |
be772100 JG |
595 | { |
596 | print_section_info (t, exec_bfd); | |
806f810b SS |
597 | |
598 | if (vmap) | |
599 | { | |
600 | struct vmap *vp; | |
601 | ||
602 | printf_unfiltered ("\tMapping info for file `%s'.\n", vmap->name); | |
603 | printf_unfiltered ("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n", | |
604 | "tstart", "tend", "dstart", "dend", "section", | |
605 | "file(member)"); | |
606 | ||
607 | for (vp = vmap; vp; vp = vp->nxt) | |
608 | printf_unfiltered ("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n", | |
609 | vp->tstart, vp->tend, vp->dstart, vp->dend, vp->name, | |
610 | *vp->member ? "(" : "", vp->member, | |
611 | *vp->member ? ")" : ""); | |
612 | } | |
be772100 JG |
613 | } |
614 | ||
f2fc6e7a JK |
615 | static void |
616 | set_section_command (args, from_tty) | |
617 | char *args; | |
618 | int from_tty; | |
619 | { | |
620 | struct section_table *p; | |
621 | char *secname; | |
622 | unsigned seclen; | |
623 | unsigned long secaddr; | |
624 | char secprint[100]; | |
625 | long offset; | |
626 | ||
627 | if (args == 0) | |
628 | error ("Must specify section name and its virtual address"); | |
629 | ||
630 | /* Parse out section name */ | |
631 | for (secname = args; !isspace(*args); args++) ; | |
632 | seclen = args - secname; | |
633 | ||
634 | /* Parse out new virtual address */ | |
635 | secaddr = parse_and_eval_address (args); | |
636 | ||
be772100 | 637 | for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { |
b83ed019 ILT |
638 | if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen) |
639 | && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0') { | |
f2fc6e7a JK |
640 | offset = secaddr - p->addr; |
641 | p->addr += offset; | |
642 | p->endaddr += offset; | |
be772100 JG |
643 | if (from_tty) |
644 | exec_files_info(&exec_ops); | |
f2fc6e7a JK |
645 | return; |
646 | } | |
647 | } | |
648 | if (seclen >= sizeof (secprint)) | |
649 | seclen = sizeof (secprint) - 1; | |
650 | strncpy (secprint, secname, seclen); | |
651 | secprint[seclen] = '\0'; | |
652 | error ("Section %s not found", secprint); | |
653 | } | |
654 | ||
b1eaba9a | 655 | /* If mourn is being called in all the right places, this could be say |
b02fd8ca | 656 | `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */ |
b1eaba9a SG |
657 | |
658 | static int | |
659 | ignore (addr, contents) | |
660 | CORE_ADDR addr; | |
661 | char *contents; | |
662 | { | |
663 | return 0; | |
664 | } | |
665 | ||
bd5635a1 | 666 | struct target_ops exec_ops = { |
2b576293 C |
667 | "exec", /* to_shortname */ |
668 | "Local exec file", /* to_longname */ | |
669 | "Use an executable file as a target.\n\ | |
670 | Specify the filename of the executable file.", /* to_doc */ | |
671 | exec_file_command, /* to_open */ | |
672 | exec_close, /* to_close */ | |
673 | find_default_attach, /* to_attach */ | |
674 | 0, /* to_detach */ | |
675 | 0, /* to_resume */ | |
676 | 0, /* to_wait */ | |
677 | 0, /* to_fetch_registers */ | |
678 | 0, /* to_store_registers */ | |
679 | 0, /* to_prepare_to_store */ | |
680 | xfer_memory, /* to_xfer_memory */ | |
681 | exec_files_info, /* to_files_info */ | |
682 | ignore, /* to_insert_breakpoint */ | |
683 | ignore, /* to_remove_breakpoint */ | |
684 | 0, /* to_terminal_init */ | |
685 | 0, /* to_terminal_inferior */ | |
686 | 0, /* to_terminal_ours_for_output */ | |
687 | 0, /* to_terminal_ours */ | |
688 | 0, /* to_terminal_info */ | |
689 | 0, /* to_kill */ | |
690 | 0, /* to_load */ | |
691 | 0, /* to_lookup_symbol */ | |
692 | find_default_create_inferior, /* to_create_inferior */ | |
693 | 0, /* to_mourn_inferior */ | |
694 | 0, /* to_can_run */ | |
695 | 0, /* to_notice_signals */ | |
696 | 0, /* to_thread_alive */ | |
697 | 0, /* to_stop */ | |
698 | file_stratum, /* to_stratum */ | |
699 | 0, /* to_next */ | |
700 | 0, /* to_has_all_memory */ | |
701 | 1, /* to_has_memory */ | |
702 | 0, /* to_has_stack */ | |
703 | 0, /* to_has_registers */ | |
704 | 0, /* to_has_execution */ | |
705 | 0, /* to_sections */ | |
706 | 0, /* to_sections_end */ | |
707 | OPS_MAGIC, /* to_magic */ | |
bd5635a1 RP |
708 | }; |
709 | ||
710 | void | |
711 | _initialize_exec() | |
712 | { | |
b1eaba9a | 713 | struct cmd_list_element *c; |
bd5635a1 | 714 | |
b1eaba9a SG |
715 | c = add_cmd ("file", class_files, file_command, |
716 | "Use FILE as program to be debugged.\n\ | |
bd5635a1 RP |
717 | It is read for its symbols, for getting the contents of pure memory,\n\ |
718 | and it is the program executed when you use the `run' command.\n\ | |
719 | If FILE cannot be found as specified, your execution directory path\n\ | |
720 | ($PATH) is searched for a command of that name.\n\ | |
b1eaba9a SG |
721 | No arg means to have no executable file and no symbols.", &cmdlist); |
722 | c->completer = filename_completer; | |
bd5635a1 | 723 | |
b1eaba9a | 724 | c = add_cmd ("exec-file", class_files, exec_file_command, |
bd5635a1 RP |
725 | "Use FILE as program for getting contents of pure memory.\n\ |
726 | If FILE cannot be found as specified, your execution directory path\n\ | |
727 | is searched for a command of that name.\n\ | |
b1eaba9a SG |
728 | No arg means have no executable file.", &cmdlist); |
729 | c->completer = filename_completer; | |
bd5635a1 | 730 | |
f2fc6e7a JK |
731 | add_com ("section", class_files, set_section_command, |
732 | "Change the base address of section SECTION of the exec file to ADDR.\n\ | |
733 | This can be used if the exec file does not contain section addresses,\n\ | |
734 | (such as in the a.out format), or when the addresses specified in the\n\ | |
735 | file itself are wrong. Each section must be changed separately. The\n\ | |
736 | ``info files'' command lists all the sections and their addresses."); | |
737 | ||
bdbd5f50 JG |
738 | add_show_from_set |
739 | (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files, | |
740 | "Set writing into executable and core files.", | |
741 | &setlist), | |
742 | &showlist); | |
743 | ||
bd5635a1 RP |
744 | add_target (&exec_ops); |
745 | } |