1 /* Execute AIXcoff files, for GDB.
2 Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
3 Derived from exec.c. Modified by IBM Corporation.
4 Donated by IBM Corporation and Cygnus Support.
6 This file is part of GDB.
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.
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.
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. */
22 /* xcoff-exec - deal with executing XCOFF files. */
25 #include <sys/types.h>
26 #include <sys/param.h>
42 #include "libbfd.h" /* BFD internals (sigh!) FIXME */
44 struct section_table
*exec_sections
, *exec_sections_end
;
46 #define eq(s0, s1) !strcmp(s0, s1)
48 /* Whether to open exec and core files read-only or read-write. */
52 bfd
*exec_bfd
; /* needed by core.c */
54 extern char *getenv();
55 extern void child_create_inferior (), child_attach ();
56 extern void add_syms_addr_command ();
57 extern void symbol_file_command ();
58 static void exec_files_info();
61 * the vmap struct is used to describe the virtual address space of
62 * the target we are manipulating. The first entry is always the "exec"
63 * file. Subsequent entries correspond to other objects that are
64 * mapped into the address space of a process created from the "exec" file.
65 * These are either in response to exec()ing the file, in which case all
66 * shared libraries are loaded, or a "load" system call, followed by the
67 * user's issuance of a "load" command.
70 struct vmap
*nxt
; /* ^ to next in chain */
71 bfd
*bfd
; /* BFD for mappable object library */
72 char *name
; /* ^ to object file name */
73 char *member
; /* ^ to member name */
74 CORE_ADDR tstart
; /* virtual addr where member is mapped */
75 CORE_ADDR tend
; /* virtual upper bound of member */
76 CORE_ADDR tadj
; /* heuristically derived adjustment */
77 CORE_ADDR dstart
; /* virtual address of data start */
78 CORE_ADDR dend
; /* vitrual address of data end */
87 static struct vmap
*vmap
; /* current vmap */
89 extern struct target_ops exec_ops
;
92 /* exec_close - done with exec file, clean up all resources. */
95 exec_close(quitting
) {
96 register struct vmap
*vp
, *nxt
;
98 for (nxt
= vmap
; vp
= nxt
; ) {
101 free_named_symtabs(vp
->name
, vp
->member
); /* XXX */
110 * exec_file_command - handle the "exec" command, &c.
113 exec_file_command(filename
, from_tty
)
118 target_preopen(from_tty
);
119 unpush_target(&exec_ops
);
121 /* Now open and digest the file the user requested, if any. */
124 char *scratch_pathname
;
127 filename
= tilde_expand(filename
);
128 make_cleanup(free
, filename
);
130 scratch_chan
= openp(getenv("PATH"), 1, filename
, O_RDONLY
, 0
131 , &scratch_pathname
);
132 if (scratch_chan
< 0)
133 perror_with_name(filename
);
135 bfd
= bfd_fdopenr(scratch_pathname
, NULL
, scratch_chan
);
137 error("Could not open `%s' as an executable file: %s"
138 , scratch_pathname
, bfd_errmsg(bfd_error
));
140 /* make sure we have an object file */
142 if (!bfd_check_format(bfd
, bfd_object
))
143 error("\"%s\": not in executable format: %s."
144 , scratch_pathname
, bfd_errmsg(bfd_error
));
147 /* setup initial vmap */
151 error("Can't find the file sections in `%s': %s"
152 , bfd
->filename
, bfd_errmsg(bfd_error
));
156 if (build_section_table (exec_bfd
, &exec_sections
, &exec_sections_end
))
157 error ("Can't find the file sections in `%s': %s",
158 exec_bfd
->filename
, bfd_errmsg (bfd_error
));
160 /* make sure core, if present, matches */
163 push_target(&exec_ops
);
165 /* Tell display code(if any) about the changed file name. */
167 if (exec_file_display_hook
)
168 (*exec_file_display_hook
)(filename
);
171 exec_close(0); /* just in case */
173 printf("No exec file now.\n");
177 /* Set both the exec file and the symbol file, in one command. What a
178 * novelty. Why did GDB go through four major releases before this
182 file_command(arg
, from_tty
)
185 exec_file_command(arg
, from_tty
);
186 symbol_file_command(arg
, from_tty
);
189 /* Locate all mappable sections of a BFD file.
190 table_pp_char is a char * to get it through bfd_map_over_sections;
191 we cast it back to its proper type. */
194 add_to_section_table (abfd
, asect
, table_pp_char
)
199 struct section_table
**table_pp
= (struct section_table
**)table_pp_char
;
202 aflag
= bfd_get_section_flags (abfd
, asect
);
203 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
204 if (!(aflag
& SEC_LOAD
))
206 (*table_pp
)->sec_ptr
= asect
;
207 (*table_pp
)->addr
= bfd_section_vma (abfd
, asect
);
208 (*table_pp
)->endaddr
= (*table_pp
)->addr
+ bfd_section_size (abfd
, asect
);
213 build_section_table (some_bfd
, start
, end
)
215 struct section_table
**start
, **end
;
219 count
= bfd_count_sections (some_bfd
);
221 abort(); /* return 1? */
224 *start
= (struct section_table
*) xmalloc (count
* sizeof (**start
));
226 bfd_map_over_sections (some_bfd
, add_to_section_table
, (char *)end
);
227 if (*end
> *start
+ count
)
229 /* We could realloc the table, but it probably loses for most files. */
234 * lookup_symtab_bfd - find if we currently have any symbol tables from bfd
237 lookup_objfile_bfd(bfd
*bfd
) {
238 register struct objfile
*s
;
240 for (s
= object_files
; s
; s
= s
->next
)
248 sex_to_vmap(bfd
*bf
, sec_ptr sex
, struct vmap_and_bfd
*vmap_bfd
)
250 register struct vmap
*vp
, **vpp
;
251 register struct symtab
*syms
;
252 bfd
*arch
= vmap_bfd
->pbfd
;
253 vp
= vmap_bfd
->pvmap
;
255 if ((bfd_get_section_flags(bf
, sex
) & SEC_LOAD
) == 0)
258 if (!strcmp(bfd_section_name(bf
, sex
), ".text")) {
260 vp
->tend
= vp
->tstart
+ bfd_section_size(bf
, sex
);
262 /* This is quite a tacky way to recognize the `exec' load segment (rather
263 than shared libraries. You should use `arch' instead. FIXMEmgo */
265 vp
->tadj
= sex
->filepos
- bfd_section_vma(bf
, sex
);
270 else if (!strcmp(bfd_section_name(bf
, sex
), ".data")) {
272 vp
->dend
= vp
->dstart
+ bfd_section_size(bf
, sex
);
275 else if (!strcmp(bfd_section_name(bf
, sex
), ".bss")) /* FIXMEmgo */
276 printf ("bss section in exec! Don't know what the heck to do!\n");
279 /* Make a vmap for the BFD "bf", which might be a member of the archive
280 BFD "arch". If we have not yet read in symbols for this file, do so. */
282 map_vmap (bfd
*bf
, bfd
*arch
)
284 struct vmap_and_bfd vmap_bfd
;
285 struct vmap
*vp
, **vpp
;
289 vp
= (void*) xmalloc (sizeof (*vp
));
292 vp
->name
= bfd_get_filename(arch
? arch
: bf
);
293 vp
->member
= arch
? bfd_get_filename(bf
) : "";
295 vmap_bfd
.pbfd
= arch
;
297 bfd_map_over_sections (bf
, sex_to_vmap
, &vmap_bfd
);
299 obj
= lookup_objfile_bfd (bf
);
300 if (exec_bfd
&& !obj
) {
301 name
= savestring (bfd_get_filename (bf
), strlen (bfd_get_filename (bf
)));
302 obj
= allocate_objfile (bf
, name
);
303 syms_from_objfile (obj
, 0, 0);
306 /* find the end of the list, and append. */
307 for (vpp
= &vmap
; *vpp
; vpp
= &(*vpp
)->nxt
)
313 /* true, if symbol table and misc_function_vector is relocated. */
315 int symtab_relocated
= 0;
318 /* vmap_symtab - handle symbol translation on vmapping */
320 vmap_symtab(vp
, old_start
, vip
)
321 register struct vmap
*vp
;
325 register struct symtab
*s
;
328 * for each symbol table generated from the vp->bfd
330 for (s
= symtab_list
; s
; s
= s
->next
) {
332 /* skip over if this is not relocatable and doesn't have a line table */
333 if (s
->nonreloc
&& !LINETABLE (s
))
336 /* matching the symbol table's BFD and the *vp's BFD is hairy.
337 exec_file creates a seperate BFD for possibly the
338 same file as symbol_file.FIXME ALL THIS MUST BE RECTIFIED. */
340 if (s
->objfile
->obfd
== vp
->bfd
) {
341 /* if they match, we luck out. */
343 } else if (vp
->member
[0]) {
344 /* no match, and member present, not this one. */
351 * no match, and no member. need to be sure.
353 io
= bfd_cache_lookup(s
->objfile
->obfd
);
355 fatal("cannot find BFD's iostream for sym");
357 * see if we are referring to the same file
359 if (fstat(fileno(io
), &si
) < 0)
360 fatal("cannot fstat BFD for sym");
362 if (si
.st_dev
!= vip
->st_dev
363 || si
.st_ino
!= vip
->st_ino
)
367 if (vp
->tstart
!= old_start
)
368 vmap_symtab_1(s
, vp
, old_start
);
371 if (vp
->tstart
!= old_start
)
372 fixup_misc_vector (vp
->tstart
- old_start
);
374 symtab_relocated
= 1;
378 fixup_misc_vector (int disp
)
381 for (ii
=0; ii
< misc_function_count
; ++ii
)
382 if (misc_function_vector
[ii
].address
< 0x10000000)
383 misc_function_vector
[ii
].address
+= disp
;
387 vmap_symtab_1(s
, vp
, old_start
)
388 register struct symtab
*s
;
389 register struct vmap
*vp
;
394 register struct linetable
*l
;
395 struct blockvector
*bv
;
396 register struct block
*b
;
398 register ulong reloc
, dreloc
;
400 if ((reloc
= vp
->tstart
- old_start
) == 0)
403 dreloc
= vp
->dstart
; /* data relocation */
406 * The line table must be relocated. This is only present for
407 * b.text sections, so only vp->text type maps need be considered.
411 for (i
= 0; i
< len
; i
++)
412 l
->item
[i
].pc
+= reloc
;
414 /* if this symbol table is not relocatable, only line table should
415 be relocated and the rest ignored. */
420 len
= BLOCKVECTOR_NBLOCKS(bv
);
422 for (i
= 0; i
< len
; i
++) {
423 b
= BLOCKVECTOR_BLOCK(bv
, i
);
425 BLOCK_START(b
) += reloc
;
426 BLOCK_END(b
) += reloc
;
428 blen
= BLOCK_NSYMS(b
);
429 for (j
= 0; j
< blen
; j
++) {
430 register struct symbol
*sym
;
432 sym
= BLOCK_SYM(b
, j
);
433 switch (SYMBOL_NAMESPACE(sym
)) {
434 case STRUCT_NAMESPACE
:
435 case UNDEF_NAMESPACE
:
438 case LABEL_NAMESPACE
:
443 switch (SYMBOL_CLASS(sym
)) {
445 case LOC_CONST_BYTES
:
459 SYMBOL_VALUE_ADDRESS(sym
) += reloc
;
463 SYMBOL_VALUE_ADDRESS(sym
) += dreloc
;
470 fatal("botched symbol class %x"
471 , SYMBOL_CLASS(sym
));
479 * add_vmap - add a new vmap entry based on ldinfo() information
482 register struct ld_info
*ldi
; {
486 mem
= ldi
->ldinfo_filename
+ strlen(ldi
->ldinfo_filename
) + 1;
487 bfd
= bfd_fdopenr(ldi
->ldinfo_filename
, NULL
, ldi
->ldinfo_fd
);
489 error("Could not open `%s' as an executable file: %s"
490 , ldi
->ldinfo_filename
, bfd_errmsg(bfd_error
));
493 /* make sure we have an object file */
495 if (bfd_check_format(bfd
, bfd_object
))
498 else if (bfd_check_format(bfd
, bfd_archive
)) {
501 * FIXME??? am I tossing BFDs? bfd?
503 while (last
= bfd_openr_next_archived_file(bfd
, last
))
504 if (eq(mem
, last
->filename
))
509 /* FIXME -- should be error */
510 warning("\"%s\": member \"%s\" missing.",
515 if (!bfd_check_format(last
, bfd_object
)) {
516 bfd_close(last
); /* XXX??? */
520 map_vmap (last
, bfd
);
525 /* FIXME -- should be error */
526 warning("\"%s\": not in executable format: %s."
527 , ldi
->ldinfo_filename
, bfd_errmsg(bfd_error
));
533 /* As well as symbol tables, exec_sections need relocation. Otherwise after
534 the inferior process terminates, symbol table is relocated but there is
535 no inferior process. Thus, we have to use `exec' bfd, rather than the inferior
536 process's memory space, when lookipng at symbols.
537 `exec_sections' need to be relocated only once though, as long as the exec
538 file was not changed.
543 if (execbfd
== exec_bfd
)
548 if (!vmap
|| !exec_sections
) {
549 printf ("WARNING: vmap not found in vmap_exec()!\n");
552 /* First exec section is `.text', second is `.data'. If this is changed,
553 then this routine will choke. Better you should check section names,
555 exec_sections
[0].addr
+= vmap
->tstart
;
556 exec_sections
[0].endaddr
+= vmap
->tstart
;
557 exec_sections
[1].addr
+= vmap
->dstart
;
558 exec_sections
[1].endaddr
+= vmap
->dstart
;
563 text_adjustment (abfd
)
567 static int adjustment
;
570 if (exec_bfd
== execbfd
)
573 sect
= bfd_get_section_by_name (abfd
, ".text");
575 adjustment
= sect
->filepos
- sect
->vma
;
577 adjustment
= 0x200; /* just a wild assumption */
584 * vmap_ldinfo - update VMAP info with ldinfo() information
587 * ldi - ^ to ldinfo() results.
590 register struct ld_info
*ldi
;
593 register struct vmap
*vp
;
594 register got_one
, retried
;
598 * for each *ldi, see if we have a corresponding *vp
599 * if so, update the mapping, and symbol table.
600 * if not, add an entry and symbol table.
603 char *name
= ldi
->ldinfo_filename
;
604 char *memb
= name
+ strlen(name
) + 1;
608 if (fstat(ldi
->ldinfo_fd
, &ii
) < 0)
609 fatal("cannot fstat(%d) on %s"
613 for (got_one
= 0, vp
= vmap
; vp
; vp
= vp
->nxt
) {
616 /* The filenames are not always sufficient to match on. */
618 && !eq(name
, vp
->name
))
619 || (memb
[0] && !eq(memb
, vp
->member
)))
622 /* totally opaque! */
623 io
= bfd_cache_lookup(vp
->bfd
);
625 fatal("cannot find BFD's iostream for %s"
628 /* see if we are referring to the same file */
629 if (fstat(fileno(io
), &vi
) < 0)
630 fatal("cannot fstat BFD for %s", vp
->name
);
632 if (ii
.st_dev
!= vi
.st_dev
|| ii
.st_ino
!= vi
.st_ino
)
636 close(ldi
->ldinfo_fd
);
640 /* found a corresponding VMAP. remap! */
643 vp
->tstart
= ldi
->ldinfo_textorg
;
644 vp
->tend
= vp
->tstart
+ ldi
->ldinfo_textsize
;
645 vp
->dstart
= ldi
->ldinfo_dataorg
;
646 vp
->dend
= vp
->dstart
+ ldi
->ldinfo_datasize
;
649 vp
->tstart
+= vp
->tadj
;
650 vp
->tend
+= vp
->tadj
;
653 /* relocate symbol table(s). */
654 vmap_symtab(vp
, ostart
, &vi
);
656 /* there may be more, so we don't break out of the loop. */
660 * if there was no matching *vp, we must perforce create
663 if (!got_one
&& !retried
) {
668 } while (ldi
->ldinfo_next
669 && (ldi
= (void *) (ldi
->ldinfo_next
+ (char *) ldi
)));
675 * vmap_inferior - print VMAP info for inferior
679 if (inferior_pid
== 0)
680 return 0; /* normal processing */
687 /* Read or write the exec file.
689 Args are address within exec file, address within gdb address-space,
690 length, and a flag indicating whether to read or write.
694 0: We cannot handle this address and length.
695 > 0: We have handled N bytes starting at this address.
696 (If N == length, we did it all.) We might be able
697 to handle more bytes beyond this length, but no
699 < 0: We cannot handle this address, but if somebody
700 else handles (-N) bytes, we can start from there.
702 The same routine is used to handle both core and exec files;
703 we just tail-call it with more arguments to select between them. */
706 xfer_memory (memaddr
, myaddr
, len
, write
, abfd
, sections
, sections_end
)
712 struct section_table
*sections
, *sections_end
;
715 struct section_table
*p
;
716 CORE_ADDR nextsectaddr
, memend
;
717 boolean (*xfer_fn
) ();
722 memend
= memaddr
+ len
;
723 xfer_fn
= write
? bfd_set_section_contents
: bfd_get_section_contents
;
724 nextsectaddr
= memend
;
726 for (p
= sections
; p
< sections_end
; p
++)
728 if (p
->addr
<= memaddr
)
729 if (p
->endaddr
>= memend
)
731 /* Entire transfer is within this section. */
732 res
= xfer_fn (abfd
, p
->sec_ptr
, myaddr
, memaddr
- p
->addr
, len
);
733 return (res
!= false)? len
: 0;
735 else if (p
->endaddr
<= memaddr
)
737 /* This section ends before the transfer starts. */
742 /* This section overlaps the transfer. Just do half. */
743 len
= p
->endaddr
- memaddr
;
744 res
= xfer_fn (abfd
, p
->sec_ptr
, myaddr
, memaddr
- p
->addr
, len
);
745 return (res
!= false)? len
: 0;
747 else if (p
->addr
< nextsectaddr
)
748 nextsectaddr
= p
->addr
;
751 if (nextsectaddr
>= memend
)
752 return 0; /* We can't help */
754 return - (nextsectaddr
- memaddr
); /* Next boundary where we can help */
757 /* The function called by target_xfer_memory via our target_ops */
760 exec_xfer_memory (memaddr
, myaddr
, len
, write
)
766 return xfer_memory (memaddr
, myaddr
, len
, write
,
767 exec_bfd
, exec_sections
, exec_sections_end
);
771 * exec_files_info - "info files" command processor
775 register struct vmap
*vp
= vmap
;
780 printf("\tMapping info for file `%s'.\n", vp
->name
);
781 printf("\t %8.8s %8.8s %8.8s %s\n"
782 , "start", "end", "section", "file(member)");
784 for (; vp
; vp
= vp
->nxt
)
785 printf("\t0x%8.8x 0x%8.8x %s%s%s%s\n"
789 , *vp
->member
? "(" : ""
791 , *vp
->member
? ")" : "");
795 Damon
's implementation of set_section_command! It is based on the sex member
796 (which is a section pointer from vmap) of vmap.
797 We will not have multiple vmap entries (one for each section), rather transmit
798 text and data base offsets and fix them at the same time. Elimination of sex
799 entry in vmap make this function obsolute, use the one from exec.c.
800 Need further testing!! FIXMEmgo.
803 set_section_command(args, from_tty)
806 register struct vmap *vp = vmap;
809 unsigned long secaddr;
814 error("Must specify section name and its virtual address");
816 /* Parse out section name */
817 for (secname = args; !isspace(*args); args++)
819 seclen = args - secname;
821 /* Parse out new virtual address */
822 secaddr = parse_and_eval_address(args);
824 for (vp = vmap; vp; vp = vp->nxt) {
826 , bfd_section_name(vp->bfd, vp->sex), seclen)
827 && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\
0') {
828 offset
= secaddr
- vp
->tstart
;
829 vp
->tstart
+= offset
;
836 if (seclen
>= sizeof(secprint
))
837 seclen
= sizeof(secprint
) - 1;
838 strncpy(secprint
, secname
, seclen
);
839 secprint
[seclen
] = '\0';
840 error("Section %s not found", secprint
);
844 set_section_command (args
, from_tty
)
848 struct section_table
*p
;
851 unsigned long secaddr
;
856 error ("Must specify section name and its virtual address");
858 /* Parse out section name */
859 for (secname
= args
; !isspace(*args
); args
++) ;
860 seclen
= args
- secname
;
862 /* Parse out new virtual address */
863 secaddr
= parse_and_eval_address (args
);
865 for (p
= exec_sections
; p
< exec_sections_end
; p
++) {
866 if (!strncmp (secname
, bfd_section_name (exec_bfd
, p
->sec_ptr
), seclen
)
867 && bfd_section_name (exec_bfd
, p
->sec_ptr
)[seclen
] == '\0') {
868 offset
= secaddr
- p
->addr
;
870 p
->endaddr
+= offset
;
875 if (seclen
>= sizeof (secprint
))
876 seclen
= sizeof (secprint
) - 1;
877 strncpy (secprint
, secname
, seclen
);
878 secprint
[seclen
] = '\0';
879 error ("Section %s not found", secprint
);
884 struct target_ops exec_ops
= {
885 "exec", "Local exec file",
886 "Use an executable file as a target.\n\
887 Specify the filename of the executable file.",
888 exec_file_command
, exec_close
, /* open, close */
889 child_attach
, 0, 0, 0, /* attach, detach, resume, wait, */
890 0, 0, /* fetch_registers, store_registers, */
891 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */
892 exec_xfer_memory
, exec_files_info
,
893 0, 0, /* insert_breakpoint, remove_breakpoint, */
894 0, 0, 0, 0, 0, /* terminal stuff */
895 0, 0, /* kill, load */
896 0, 0, /* call fn, lookup sym */
897 child_create_inferior
,
898 0, /* mourn_inferior */
899 file_stratum
, 0, /* next */
900 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
901 0, 0, /* section pointers */
902 OPS_MAGIC
, /* Always the last thing */
910 add_com("file", class_files
, file_command
,
911 "Use FILE as program to be debugged.\n\
912 It is read for its symbols, for getting the contents of pure memory,\n\
913 and it is the program executed when you use the `run' command.\n\
914 If FILE cannot be found as specified, your execution directory path\n\
915 ($PATH) is searched for a command of that name.\n\
916 No arg means to have no executable file and no symbols.");
918 add_com("exec-file", class_files
, exec_file_command
,
919 "Use FILE as program for getting contents of pure memory.\n\
920 If FILE cannot be found as specified, your execution directory path\n\
921 is searched for a command of that name.\n\
922 No arg means have no executable file.");
924 add_com("section", class_files
, set_section_command
,
925 "Change the base address of section SECTION of the exec file to ADDR.\n\
926 This can be used if the exec file does not contain section addresses,\n\
927 (such as in the a.out format), or when the addresses specified in the\n\
928 file itself are wrong. Each section must be changed separately. The\n\
929 ``info files'' command lists all the sections and their addresses.");
931 add_target(&exec_ops
);
This page took 0.048169 seconds and 4 git commands to generate.