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