* xcoffexec.c (exec_close): If quitting, don't call clear_symtab_users.
[deliverable/binutils-gdb.git] / gdb / xcoffexec.c
1 /* Execute AIXcoff files, for GDB.
2 Copyright 1988, 1989, 1991, 1992, 1994 Free Software Foundation, Inc.
3 Derived from exec.c. Modified by IBM Corporation.
4 Donated by IBM Corporation and Cygnus Support.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 /* xcoff-exec - deal with executing XCOFF files. */
23
24 #include "defs.h"
25
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <sys/stat.h>
32
33 #include "frame.h"
34 #include "inferior.h"
35 #include "target.h"
36 #include "gdbcmd.h"
37 #include "gdbcore.h"
38 #include "language.h"
39 #include "symfile.h"
40 #include "objfiles.h"
41
42 #include "bfd.h"
43 #include "xcoffsolib.h"
44
45 /* Prototypes for local functions */
46
47 static void
48 file_command PARAMS ((char *, int));
49
50 static void
51 exec_close PARAMS ((int));
52
53 struct vmap *
54 map_vmap PARAMS ((bfd *, bfd *));
55
56 struct section_table *exec_sections, *exec_sections_end;
57
58 /* Whether to open exec and core files read-only or read-write. */
59
60 int write_files = 0;
61
62 extern int info_verbose;
63
64 bfd *exec_bfd; /* needed by core.c */
65
66 extern char *getenv();
67 extern void add_syms_addr_command ();
68 extern void symbol_file_command ();
69 static void exec_files_info();
70
71 struct vmap *vmap; /* current vmap */
72
73 extern struct target_ops exec_ops;
74
75 /* exec_close - done with exec file, clean up all resources. */
76
77 static void
78 exec_close (quitting)
79 int quitting;
80 {
81 register struct vmap *vp, *nxt;
82 int need_symtab_cleanup = 0;
83
84 for (nxt = vmap; nxt; )
85 {
86 vp = nxt;
87 nxt = vp->nxt;
88
89 /* if there is an objfile associated with this bfd,
90 free_objfile() will do proper cleanup of objfile *and* bfd. */
91
92 if (vp->objfile)
93 {
94 free_objfile (vp->objfile);
95 need_symtab_cleanup = 1;
96 }
97 else
98 bfd_close (vp->bfd);
99
100 /* FIXME: This routine is #if 0'd in symfile.c. What should we
101 be doing here? Should we just free everything in
102 vp->objfile->symtabs? Should free_objfile do that? */
103 free_named_symtabs (vp->name);
104 free (vp);
105 }
106
107 vmap = 0;
108
109 /* exec_bfd was already closed (the exec file has a vmap entry). */
110 exec_bfd = NULL;
111
112 if (exec_ops.to_sections)
113 {
114 free (exec_ops.to_sections);
115 exec_ops.to_sections = NULL;
116 exec_ops.to_sections_end = NULL;
117 }
118
119 /* If we are quitting, we don't want to call breakpoint_re_set which may
120 output messages which would just be confusing in this context. */
121 if (!quitting && need_symtab_cleanup)
122 clear_symtab_users ();
123 }
124
125 /* Process the first arg in ARGS as the new exec file.
126
127 Note that we have to explicitly ignore additional args, since we can
128 be called from file_command(), which also calls symbol_file_command()
129 which can take multiple args. */
130
131 void
132 exec_file_command (filename, from_tty)
133 char *filename;
134 int from_tty;
135 {
136 target_preopen (from_tty);
137
138 /* Remove any previous exec file. */
139 unpush_target (&exec_ops);
140
141 /* Now open and digest the file the user requested, if any. */
142
143 if (filename)
144 {
145 char *scratch_pathname;
146 int scratch_chan;
147
148 filename = tilde_expand (filename);
149 make_cleanup (free, filename);
150
151 scratch_chan = openp (getenv ("PATH"), 1, filename,
152 write_files? O_RDWR: O_RDONLY, 0,
153 &scratch_pathname);
154 if (scratch_chan < 0)
155 perror_with_name (filename);
156
157 exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
158 if (!exec_bfd)
159 error ("Could not open `%s' as an executable file: %s",
160 scratch_pathname, bfd_errmsg(bfd_get_error ()));
161
162 /* make sure we have an object file */
163
164 if (!bfd_check_format (exec_bfd, bfd_object))
165 error ("\"%s\": not in executable format: %s.", scratch_pathname,
166 bfd_errmsg (bfd_get_error ()));
167
168 /* setup initial vmap */
169
170 map_vmap (exec_bfd, 0);
171 if (!vmap)
172 error ("Can't find the file sections in `%s': %s", exec_bfd->filename,
173 bfd_errmsg(bfd_get_error ()));
174
175 if (build_section_table (exec_bfd, &exec_ops.to_sections,
176 &exec_ops.to_sections_end))
177 error ("Can't find the file sections in `%s': %s", exec_bfd->filename,
178 bfd_errmsg (bfd_get_error ()));
179
180 /* make sure core, if present, matches */
181 validate_files ();
182
183 push_target(&exec_ops);
184
185 /* Tell display code (if any) about the changed file name. */
186 if (exec_file_display_hook)
187 (*exec_file_display_hook) (filename);
188 }
189 else
190 {
191 exec_close (0); /* just in case */
192 if (from_tty)
193 printf_unfiltered ("No exec file now.\n");
194 }
195 }
196
197 /* Set both the exec file and the symbol file, in one command. What a
198 novelty. Why did GDB go through four major releases before this
199 command was added? */
200
201 static void
202 file_command (arg, from_tty)
203 char *arg;
204 int from_tty;
205 {
206 /* FIXME, if we lose on reading the symbol file, we should revert
207 the exec file, but that's rough. */
208 exec_file_command (arg, from_tty);
209 symbol_file_command (arg, from_tty);
210 }
211
212 /* Locate all mappable sections of a BFD file.
213 table_pp_char is a char * to get it through bfd_map_over_sections;
214 we cast it back to its proper type. */
215
216 static void
217 add_to_section_table (abfd, asect, table_pp_char)
218 bfd *abfd;
219 sec_ptr asect;
220 char *table_pp_char;
221 {
222 struct section_table **table_pp = (struct section_table **)table_pp_char;
223 flagword aflag;
224
225 aflag = bfd_get_section_flags (abfd, asect);
226 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
227 if (!(aflag & SEC_LOAD))
228 return;
229 if (0 == bfd_section_size (abfd, asect))
230 return;
231 (*table_pp)->bfd = abfd;
232 (*table_pp)->the_bfd_section = asect;
233 (*table_pp)->addr = bfd_section_vma (abfd, asect);
234 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
235 (*table_pp)++;
236 }
237
238 int
239 build_section_table (some_bfd, start, end)
240 bfd *some_bfd;
241 struct section_table **start, **end;
242 {
243 unsigned count;
244
245 count = bfd_count_sections (some_bfd);
246 if (count == 0)
247 fatal ("aborting"); /* return 1? */
248 if (*start)
249 free (*start);
250 *start = (struct section_table *) xmalloc (count * sizeof (**start));
251 *end = *start;
252 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
253 if (*end > *start + count)
254 fatal ("aborting");
255 /* We could realloc the table, but it probably loses for most files. */
256 return 0;
257 }
258 \f
259 static void
260 bfdsec_to_vmap(bf, sect, arg3)
261 bfd *bf;
262 sec_ptr sect;
263 PTR arg3;
264 {
265 struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
266 register struct vmap *vp;
267 vp = vmap_bfd->pvmap;
268
269 if ((bfd_get_section_flags (bf, sect) & SEC_LOAD) == 0)
270 return;
271
272 if (STREQ(bfd_section_name (bf, sect), ".text"))
273 {
274 vp->tstart = 0;
275 vp->tend = vp->tstart + bfd_section_size (bf, sect);
276
277 /* When it comes to this adjustment value, in contrast to our previous
278 belief shared objects should behave the same as the main load segment.
279 This is the offset from the beginning of text section to the first
280 real instruction. */
281
282 vp->tadj = sect->filepos - bfd_section_vma (bf, sect);
283 }
284 else if (STREQ(bfd_section_name (bf, sect), ".data"))
285 {
286 vp->dstart = 0;
287 vp->dend = vp->dstart + bfd_section_size (bf, sect);
288 }
289 else if (STREQ(bfd_section_name(bf, sect), ".bss")) /* FIXMEmgo */
290 printf_unfiltered ("bss section in exec! Don't know what the heck to do!\n");
291 }
292
293 /* Make a vmap for the BFD "bf", which might be a member of the archive
294 BFD "arch". Return the new vmap. */
295
296 struct vmap *
297 map_vmap (bf, arch)
298 bfd *bf;
299 bfd *arch;
300 {
301 struct vmap_and_bfd vmap_bfd;
302 struct vmap *vp, **vpp;
303
304 vp = (PTR) xmalloc (sizeof (*vp));
305 memset (vp, '\0', sizeof (*vp));
306 vp->nxt = 0;
307 vp->bfd = bf;
308 vp->name = bfd_get_filename (arch ? arch : bf);
309 vp->member = arch ? bfd_get_filename (bf) : "";
310
311 vmap_bfd.pbfd = arch;
312 vmap_bfd.pvmap = vp;
313 bfd_map_over_sections (bf, bfdsec_to_vmap, &vmap_bfd);
314
315 /* find the end of the list, and append. */
316 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
317 ;
318 *vpp = vp;
319
320 return vp;
321 }
322
323 /* Read or write the exec file.
324
325 Args are address within exec file, address within gdb address-space,
326 length, and a flag indicating whether to read or write.
327
328 Result is a length:
329
330 0: We cannot handle this address and length.
331 > 0: We have handled N bytes starting at this address.
332 (If N == length, we did it all.) We might be able
333 to handle more bytes beyond this length, but no
334 promises.
335 < 0: We cannot handle this address, but if somebody
336 else handles (-N) bytes, we can start from there.
337
338 The same routine is used to handle both core and exec files;
339 we just tail-call it with more arguments to select between them. */
340
341 int
342 xfer_memory (memaddr, myaddr, len, write, target)
343 CORE_ADDR memaddr;
344 char *myaddr;
345 int len;
346 int write;
347 struct target_ops *target;
348 {
349 boolean res;
350 struct section_table *p;
351 CORE_ADDR nextsectaddr, memend;
352 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
353
354 if (len <= 0)
355 fatal ("aborting");
356
357 memend = memaddr + len;
358 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
359 nextsectaddr = memend;
360
361 for (p = target->to_sections; p < target->to_sections_end; p++)
362 {
363 if (p->addr <= memaddr)
364 if (p->endaddr >= memend)
365 {
366 /* Entire transfer is within this section. */
367 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
368 return (res != 0) ? len : 0;
369 }
370 else if (p->endaddr <= memaddr)
371 {
372 /* This section ends before the transfer starts. */
373 continue;
374 }
375 else
376 {
377 /* This section overlaps the transfer. Just do half. */
378 len = p->endaddr - memaddr;
379 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, memaddr - p->addr, len);
380 return (res != 0) ? len : 0;
381 }
382 else if (p->addr < nextsectaddr)
383 nextsectaddr = p->addr;
384 }
385
386 if (nextsectaddr >= memend)
387 return 0; /* We can't help */
388 else
389 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
390 }
391
392 void
393 print_section_info (t, abfd)
394 struct target_ops *t;
395 bfd *abfd;
396 {
397 struct section_table *p;
398
399 /* FIXME-32x64: Need a version of print_address_numeric with field width. */
400 printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
401 wrap_here (" ");
402 printf_filtered ("file type %s.\n", bfd_get_target(abfd));
403
404 for (p = t->to_sections; p < t->to_sections_end; p++) {
405 printf_filtered ("\t%s",
406 local_hex_string_custom ((unsigned long) p->addr, "08l"));
407 printf_filtered (" - %s",
408 local_hex_string_custom ((unsigned long) p->endaddr, "08l"));
409 if (info_verbose)
410 printf_filtered (" @ %s",
411 local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l"));
412 printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
413 if (p->bfd != abfd) {
414 printf_filtered (" in %s", bfd_get_filename (p->bfd));
415 }
416 printf_filtered ("\n");
417 }
418 }
419
420
421 static void
422 exec_files_info (t)
423 struct target_ops *t;
424 {
425 register struct vmap *vp = vmap;
426
427 print_section_info (t, exec_bfd);
428
429 if (!vp)
430 return;
431
432 printf_unfiltered ("\tMapping info for file `%s'.\n", vp->name);
433
434 printf_unfiltered ("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n",
435 "tstart", "tend", "dstart", "dend", "section", "file(member)");
436
437 for (; vp; vp = vp->nxt)
438 printf_unfiltered ("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n",
439 vp->tstart,
440 vp->tend,
441 vp->dstart,
442 vp->dend,
443 vp->name,
444 *vp->member ? "(" : "",
445 vp->member,
446 *vp->member ? ")" : "");
447 }
448
449 #ifdef DAMON
450 /* Damon's implementation of set_section_command! It is based on the sex member
451 (which is a section pointer from vmap) of vmap.
452 We will not have multiple vmap entries (one for each section), rather transmit
453 text and data base offsets and fix them at the same time. Elimination of sex
454 entry in vmap make this function obsolute, use the one from exec.c.
455 Need further testing!! FIXMEmgo. */
456
457 static void
458 set_section_command(args, from_tty)
459 char *args;
460 {
461 register struct vmap *vp = vmap;
462 char *secname;
463 unsigned seclen;
464 unsigned long secaddr;
465 char secprint[100];
466 long offset;
467
468 if (args == 0)
469 error("Must specify section name and its virtual address");
470
471 /* Parse out section name */
472 for (secname = args; !isspace(*args); args++)
473 ;
474 seclen = args - secname;
475
476 /* Parse out new virtual address */
477 secaddr = parse_and_eval_address(args);
478
479 for (vp = vmap; vp; vp = vp->nxt) {
480 if (!strncmp(secname
481 , bfd_section_name(vp->bfd, vp->sex), seclen)
482 && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
483 offset = secaddr - vp->tstart;
484 vp->tstart += offset;
485 vp->tend += offset;
486 exec_files_info();
487 return;
488 }
489 }
490
491 if (seclen >= sizeof(secprint))
492 seclen = sizeof(secprint) - 1;
493 strncpy(secprint, secname, seclen);
494 secprint[seclen] = '\0';
495 error("Section %s not found", secprint);
496 }
497 #else
498 static void
499 set_section_command (args, from_tty)
500 char *args;
501 int from_tty;
502 {
503 struct section_table *p;
504 char *secname;
505 unsigned seclen;
506 unsigned long secaddr;
507 char secprint[100];
508 long offset;
509
510 if (args == 0)
511 error ("Must specify section name and its virtual address");
512
513 /* Parse out section name */
514 for (secname = args; !isspace(*args); args++) ;
515 seclen = args - secname;
516
517 /* Parse out new virtual address */
518 secaddr = parse_and_eval_address (args);
519
520 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++)
521 if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
522 && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0')
523 {
524 offset = secaddr - p->addr;
525 p->addr += offset;
526 p->endaddr += offset;
527 if (from_tty)
528 exec_files_info (&exec_ops);
529 return;
530 }
531
532 if (seclen >= sizeof (secprint))
533 seclen = sizeof (secprint) - 1;
534 strncpy (secprint, secname, seclen);
535 secprint[seclen] = '\0';
536 error ("Section %s not found", secprint);
537 }
538
539 #endif /* !DAMON */
540
541 struct target_ops exec_ops = {
542 "exec", "Local exec file",
543 "Use an executable file as a target.\n\
544 Specify the filename of the executable file.",
545 exec_file_command, exec_close, /* open, close */
546 find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
547 0, 0, /* fetch_registers, store_registers, */
548 0, /* prepare_to_store */
549 xfer_memory, exec_files_info,
550 0, 0, /* insert_breakpoint, remove_breakpoint, */
551 0, 0, 0, 0, 0, /* terminal stuff */
552 0, 0, /* kill, load */
553 0, /* lookup sym */
554 find_default_create_inferior,
555 0, /* mourn_inferior */
556 0, /* can_run */
557 0, /* notice_signals */
558 file_stratum, 0, /* next */
559 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
560 0, 0, /* section pointers */
561 OPS_MAGIC, /* Always the last thing */
562 };
563
564 void
565 _initialize_exec()
566 {
567
568 add_com("file", class_files, file_command,
569 "Use FILE as program to be debugged.\n\
570 It is read for its symbols, for getting the contents of pure memory,\n\
571 and it is the program executed when you use the `run' command.\n\
572 If FILE cannot be found as specified, your execution directory path\n\
573 ($PATH) is searched for a command of that name.\n\
574 No arg means to have no executable file and no symbols.");
575
576 add_com("exec-file", class_files, exec_file_command,
577 "Use FILE as program for getting contents of pure memory.\n\
578 If FILE cannot be found as specified, your execution directory path\n\
579 is searched for a command of that name.\n\
580 No arg means have no executable file.");
581
582 add_com("section", class_files, set_section_command,
583 "Change the base address of section SECTION of the exec file to ADDR.\n\
584 This can be used if the exec file does not contain section addresses,\n\
585 (such as in the a.out format), or when the addresses specified in the\n\
586 file itself are wrong. Each section must be changed separately. The\n\
587 ``info files'' command lists all the sections and their addresses.");
588
589 add_target(&exec_ops);
590 }
This page took 0.041332 seconds and 4 git commands to generate.