1 /* Execute AIXcoff files, for GDB.
2 Copyright 1988, 1989, 1991, 1992 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. */
26 #include <sys/types.h>
27 #include <sys/param.h>
41 #include "libbfd.h" /* BFD internals (sigh!) FIXME */
43 /* Prototypes for local functions */
46 file_command PARAMS ((char *, int));
49 exec_close PARAMS ((int));
51 struct section_table *exec_sections, *exec_sections_end;
53 #define eq(s0, s1) !strcmp(s0, s1)
55 /* Whether to open exec and core files read-only or read-write. */
59 extern int info_verbose;
61 bfd *exec_bfd; /* needed by core.c */
63 extern char *getenv();
64 extern void child_create_inferior (), child_attach ();
65 extern void add_syms_addr_command ();
66 extern void symbol_file_command ();
67 static void exec_files_info();
68 extern struct objfile *lookup_objfile_bfd ();
71 * the vmap struct is used to describe the virtual address space of
72 * the target we are manipulating. The first entry is always the "exec"
73 * file. Subsequent entries correspond to other objects that are
74 * mapped into the address space of a process created from the "exec" file.
75 * These are either in response to exec()ing the file, in which case all
76 * shared libraries are loaded, or a "load" system call, followed by the
77 * user's issuance of a "load" command.
80 struct vmap *nxt; /* ^ to next in chain */
81 bfd *bfd; /* BFD for mappable object library */
82 char *name; /* ^ to object file name */
83 char *member; /* ^ to member name */
84 CORE_ADDR tstart; /* virtual addr where member is mapped */
85 CORE_ADDR tend; /* virtual upper bound of member */
86 CORE_ADDR tadj; /* heuristically derived adjustment */
87 CORE_ADDR dstart; /* virtual address of data start */
88 CORE_ADDR dend; /* vitrual address of data end */
97 static struct vmap *vmap; /* current vmap */
99 extern struct target_ops exec_ops;
102 /* exec_close - done with exec file, clean up all resources. */
107 register struct vmap *vp, *nxt;
110 for (nxt = vmap; vp = nxt; )
114 /* if there is an objfile associated with this bfd,
115 free_objfile() will do proper cleanup of objfile *and* bfd. */
117 if (obj = lookup_objfile_bfd (vp->bfd))
122 free_named_symtabs(vp->name);
131 * exec_file_command - handle the "exec" command, &c.
134 exec_file_command(filename, from_tty)
139 target_preopen(from_tty);
140 unpush_target(&exec_ops);
142 /* Now open and digest the file the user requested, if any. */
145 char *scratch_pathname;
148 filename = tilde_expand(filename);
149 make_cleanup (free, filename);
151 scratch_chan = openp(getenv("PATH"), 1, filename, O_RDONLY, 0
152 , &scratch_pathname);
153 if (scratch_chan < 0)
154 perror_with_name(filename);
156 bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan);
158 error("Could not open `%s' as an executable file: %s"
159 , scratch_pathname, bfd_errmsg(bfd_error));
161 /* make sure we have an object file */
163 if (!bfd_check_format(bfd, bfd_object))
164 error("\"%s\": not in executable format: %s."
165 , scratch_pathname, bfd_errmsg(bfd_error));
168 /* setup initial vmap */
172 error("Can't find the file sections in `%s': %s"
173 , bfd->filename, bfd_errmsg(bfd_error));
177 if (build_section_table (exec_bfd, &exec_sections, &exec_sections_end))
178 error ("Can't find the file sections in `%s': %s",
179 exec_bfd->filename, bfd_errmsg (bfd_error));
181 /* make sure core, if present, matches */
184 push_target(&exec_ops);
186 /* Tell display code(if any) about the changed file name. */
188 if (exec_file_display_hook)
189 (*exec_file_display_hook)(filename);
192 exec_close(0); /* just in case */
194 printf("No exec file now.\n");
198 /* Set both the exec file and the symbol file, in one command. What a
199 * novelty. Why did GDB go through four major releases before this
203 file_command(arg, from_tty)
206 exec_file_command(arg, from_tty);
207 symbol_file_command(arg, from_tty);
210 /* Locate all mappable sections of a BFD file.
211 table_pp_char is a char * to get it through bfd_map_over_sections;
212 we cast it back to its proper type. */
215 add_to_section_table (abfd, asect, table_pp_char)
220 struct section_table **table_pp = (struct section_table **)table_pp_char;
223 aflag = bfd_get_section_flags (abfd, asect);
224 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
225 if (!(aflag & SEC_LOAD))
227 (*table_pp)->sec_ptr = asect;
228 (*table_pp)->addr = bfd_section_vma (abfd, asect);
229 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
234 build_section_table (some_bfd, start, end)
236 struct section_table **start, **end;
240 count = bfd_count_sections (some_bfd);
242 abort(); /* return 1? */
245 *start = (struct section_table *) xmalloc (count * sizeof (**start));
247 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
248 if (*end > *start + count)
250 /* We could realloc the table, but it probably loses for most files. */
255 * lookup_symtab_bfd - find if we currently have any symbol tables from bfd
258 lookup_objfile_bfd(bfd *bfd) {
259 register struct objfile *s;
261 for (s = object_files; s; s = s->next)
269 sex_to_vmap(bfd *bf, sec_ptr sex, struct vmap_and_bfd *vmap_bfd)
271 register struct vmap *vp, **vpp;
272 register struct symtab *syms;
273 bfd *arch = vmap_bfd->pbfd;
274 vp = vmap_bfd->pvmap;
276 if ((bfd_get_section_flags(bf, sex) & SEC_LOAD) == 0)
279 if (!strcmp(bfd_section_name(bf, sex), ".text")) {
281 vp->tend = vp->tstart + bfd_section_size(bf, sex);
283 /* This is quite a tacky way to recognize the `exec' load segment (rather
284 than shared libraries. You should use `arch' instead. FIXMEmgo */
286 vp->tadj = sex->filepos - bfd_section_vma(bf, sex);
291 else if (!strcmp(bfd_section_name(bf, sex), ".data")) {
293 vp->dend = vp->dstart + bfd_section_size(bf, sex);
296 else if (!strcmp(bfd_section_name(bf, sex), ".bss")) /* FIXMEmgo */
297 printf ("bss section in exec! Don't know what the heck to do!\n");
300 /* Make a vmap for the BFD "bf", which might be a member of the archive
301 BFD "arch". If we have not yet read in symbols for this file, do so. */
303 map_vmap (bfd *bf, bfd *arch)
305 struct vmap_and_bfd vmap_bfd;
306 struct vmap *vp, **vpp;
309 vp = (void*) xmalloc (sizeof (*vp));
312 vp->name = bfd_get_filename(arch ? arch : bf);
313 vp->member = arch ? bfd_get_filename(bf) : "";
315 vmap_bfd.pbfd = arch;
317 bfd_map_over_sections (bf, sex_to_vmap, &vmap_bfd);
319 obj = lookup_objfile_bfd (bf);
320 if (exec_bfd && !obj) {
321 obj = allocate_objfile (bf, bfd_get_filename (bf), 0);
322 syms_from_objfile (obj, 0, 0, 0);
325 /* find the end of the list, and append. */
326 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
331 /* Called via iterate_over_msymbols to relocate minimal symbols */
334 relocate_minimal_symbol (objfile, msymbol, arg1, arg2, arg3)
335 struct objfile *objfile;
336 struct minimal_symbol *msymbol;
341 if (msymbol->address < TEXT_SEGMENT_BASE)
342 msymbol -> address += (int) arg1;
346 /* true, if symbol table and minimal symbol table are relocated. */
348 int symtab_relocated = 0;
351 /* vmap_symtab - handle symbol translation on vmapping */
353 vmap_symtab(vp, old_start, vip)
354 register struct vmap *vp;
358 register struct symtab *s;
359 register struct objfile *objfile;
362 * for each symbol table generated from the vp->bfd
364 for (objfile = object_files; objfile != NULL; objfile = objfile -> next)
366 for (s = objfile -> symtabs; s != NULL; s = s -> next) {
368 /* skip over if this is not relocatable and doesn't have a line table */
369 if (s->nonreloc && !LINETABLE (s))
372 /* matching the symbol table's BFD and the *vp's BFD is hairy.
373 exec_file creates a seperate BFD for possibly the
374 same file as symbol_file.FIXME ALL THIS MUST BE RECTIFIED. */
376 if (objfile->obfd == vp->bfd) {
377 /* if they match, we luck out. */
379 } else if (vp->member[0]) {
380 /* no match, and member present, not this one. */
387 * no match, and no member. need to be sure.
389 io = bfd_cache_lookup(objfile->obfd);
391 fatal("cannot find BFD's iostream for sym");
393 * see if we are referring to the same file
395 if (fstat(fileno(io), &si) < 0)
396 fatal("cannot fstat BFD for sym");
398 if (si.st_dev != vip->st_dev
399 || si.st_ino != vip->st_ino)
403 if (vp->tstart != old_start)
404 vmap_symtab_1(s, vp, old_start);
407 if (vp->tstart != old_start) {
408 (void) iterate_over_msymbols (relocate_minimal_symbol,
409 (PTR) (vp->tstart - old_start),
410 (PTR) NULL, (PTR) NULL);
412 /* breakpoints need to be relocated as well. */
413 fixup_breakpoints (0, TEXT_SEGMENT_BASE, vp->tstart - old_start);
416 symtab_relocated = 1;
419 vmap_symtab_1(s, vp, old_start)
420 register struct symtab *s;
421 register struct vmap *vp;
426 register struct linetable *l;
427 struct blockvector *bv;
428 register struct block *b;
430 register ulong reloc, dreloc;
432 if ((reloc = vp->tstart - old_start) == 0)
435 dreloc = vp->dstart; /* data relocation */
438 * The line table must be relocated. This is only present for
439 * .text sections, so only vp->text type maps need be considered.
444 for (i = 0; i < len; i++)
445 l->item[i].pc += reloc;
448 /* if this symbol table is not relocatable, only line table should
449 be relocated and the rest ignored. */
454 len = BLOCKVECTOR_NBLOCKS(bv);
456 for (i = 0; i < len; i++) {
457 b = BLOCKVECTOR_BLOCK(bv, i);
459 BLOCK_START(b) += reloc;
460 BLOCK_END(b) += reloc;
462 blen = BLOCK_NSYMS(b);
463 for (j = 0; j < blen; j++) {
464 register struct symbol *sym;
466 sym = BLOCK_SYM(b, j);
467 switch (SYMBOL_NAMESPACE(sym)) {
468 case STRUCT_NAMESPACE:
469 case UNDEF_NAMESPACE:
472 case LABEL_NAMESPACE:
477 switch (SYMBOL_CLASS(sym)) {
479 case LOC_CONST_BYTES:
493 SYMBOL_VALUE_ADDRESS(sym) += reloc;
497 SYMBOL_VALUE_ADDRESS(sym) += dreloc;
504 fatal("botched symbol class %x"
505 , SYMBOL_CLASS(sym));
513 * add_vmap - add a new vmap entry based on ldinfo() information
516 register struct ld_info *ldi; {
520 mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1;
521 bfd = bfd_fdopenr(ldi->ldinfo_filename, NULL, ldi->ldinfo_fd);
523 error("Could not open `%s' as an executable file: %s"
524 , ldi->ldinfo_filename, bfd_errmsg(bfd_error));
527 /* make sure we have an object file */
529 if (bfd_check_format(bfd, bfd_object))
532 else if (bfd_check_format(bfd, bfd_archive)) {
535 * FIXME??? am I tossing BFDs? bfd?
537 while (last = bfd_openr_next_archived_file(bfd, last))
538 if (eq(mem, last->filename))
543 /* FIXME -- should be error */
544 warning("\"%s\": member \"%s\" missing.",
549 if (!bfd_check_format(last, bfd_object)) {
550 bfd_close(last); /* XXX??? */
554 map_vmap (last, bfd);
559 /* FIXME -- should be error */
560 warning("\"%s\": not in executable format: %s."
561 , ldi->ldinfo_filename, bfd_errmsg(bfd_error));
567 /* As well as symbol tables, exec_sections need relocation. Otherwise after
568 the inferior process terminates, symbol table is relocated but there is
569 no inferior process. Thus, we have to use `exec' bfd, rather than the inferior
570 process's memory space, when lookipng at symbols.
571 `exec_sections' need to be relocated only once though, as long as the exec
572 file was not changed.
577 if (execbfd == exec_bfd)
582 if (!vmap || !exec_sections) {
583 printf ("WARNING: vmap not found in vmap_exec()!\n");
586 /* First exec section is `.text', second is `.data'. If this is changed,
587 then this routine will choke. Better you should check section names,
589 exec_sections [0].addr += vmap->tstart;
590 exec_sections [0].endaddr += vmap->tstart;
591 exec_sections [1].addr += vmap->dstart;
592 exec_sections [1].endaddr += vmap->dstart;
597 text_adjustment (abfd)
601 static int adjustment;
604 if (exec_bfd == execbfd)
607 sect = bfd_get_section_by_name (abfd, ".text");
609 adjustment = sect->filepos - sect->vma;
611 adjustment = 0x200; /* just a wild assumption */
618 * vmap_ldinfo - update VMAP info with ldinfo() information
621 * ldi - ^ to ldinfo() results.
624 register struct ld_info *ldi;
627 register struct vmap *vp;
628 register got_one, retried;
632 * for each *ldi, see if we have a corresponding *vp
633 * if so, update the mapping, and symbol table.
634 * if not, add an entry and symbol table.
637 char *name = ldi->ldinfo_filename;
638 char *memb = name + strlen(name) + 1;
642 if (fstat(ldi->ldinfo_fd, &ii) < 0)
643 fatal("cannot fstat(%d) on %s"
647 for (got_one = 0, vp = vmap; vp; vp = vp->nxt) {
650 /* The filenames are not always sufficient to match on. */
652 && !eq(name, vp->name))
653 || (memb[0] && !eq(memb, vp->member)))
656 /* totally opaque! */
657 io = bfd_cache_lookup(vp->bfd);
659 fatal("cannot find BFD's iostream for %s"
662 /* see if we are referring to the same file */
663 if (fstat(fileno(io), &vi) < 0)
664 fatal("cannot fstat BFD for %s", vp->name);
666 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
670 close(ldi->ldinfo_fd);
674 /* found a corresponding VMAP. remap! */
677 vp->tstart = ldi->ldinfo_textorg;
678 vp->tend = vp->tstart + ldi->ldinfo_textsize;
679 vp->dstart = ldi->ldinfo_dataorg;
680 vp->dend = vp->dstart + ldi->ldinfo_datasize;
683 vp->tstart += vp->tadj;
684 vp->tend += vp->tadj;
687 /* relocate symbol table(s). */
688 vmap_symtab(vp, ostart, &vi);
690 /* there may be more, so we don't break out of the loop. */
694 * if there was no matching *vp, we must perforce create
697 if (!got_one && !retried) {
702 } while (ldi->ldinfo_next
703 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
708 * vmap_inferior - print VMAP info for inferior
712 if (inferior_pid == 0)
713 return 0; /* normal processing */
720 /* Read or write the exec file.
722 Args are address within exec file, address within gdb address-space,
723 length, and a flag indicating whether to read or write.
727 0: We cannot handle this address and length.
728 > 0: We have handled N bytes starting at this address.
729 (If N == length, we did it all.) We might be able
730 to handle more bytes beyond this length, but no
732 < 0: We cannot handle this address, but if somebody
733 else handles (-N) bytes, we can start from there.
735 The same routine is used to handle both core and exec files;
736 we just tail-call it with more arguments to select between them. */
739 xfer_memory (memaddr, myaddr, len, write, target)
744 struct target_ops *target;
747 struct section_table *p;
748 CORE_ADDR nextsectaddr, memend;
749 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
754 memend = memaddr + len;
755 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
756 nextsectaddr = memend;
758 for (p = target->to_sections; p < target->to_sections_end; p++)
760 if (p->addr <= memaddr)
761 if (p->endaddr >= memend)
763 /* Entire transfer is within this section. */
764 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
765 return (res != false)? len: 0;
767 else if (p->endaddr <= memaddr)
769 /* This section ends before the transfer starts. */
774 /* This section overlaps the transfer. Just do half. */
775 len = p->endaddr - memaddr;
776 res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
777 return (res != false)? len: 0;
779 else if (p->addr < nextsectaddr)
780 nextsectaddr = p->addr;
783 if (nextsectaddr >= memend)
784 return 0; /* We can't help */
786 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
790 print_section_info (t, abfd)
791 struct target_ops *t;
795 struct section_table *p;
797 printf_filtered ("\t`%s', ", bfd_get_filename(abfd));
799 printf_filtered ("file type %s.\n", bfd_get_target(abfd));
801 for (p = t->to_sections; p < t->to_sections_end; p++) {
802 printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08"));
803 printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08"));
805 printf_filtered (" @ %s",
806 local_hex_string_custom (p->sec_ptr->filepos, "08"));
807 printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr));
808 if (p->bfd != abfd) {
809 printf_filtered (" in %s", bfd_get_filename (p->bfd));
811 printf_filtered ("\n");
814 register struct vmap *vp = vmap;
819 printf("\tMapping info for file `%s'.\n", vp->name);
820 printf("\t %8.8s %8.8s %8.8s %s\n"
821 , "start", "end", "section", "file(member)");
823 for (; vp; vp = vp->nxt)
824 printf("\t0x%8.8x 0x%8.8x %s%s%s%s\n"
828 , *vp->member ? "(" : ""
830 , *vp->member ? ")" : "");
836 struct target_ops *t;
838 print_section_info (t, exec_bfd);
842 /* Damon's implementation of set_section_command! It is based on the sex member
843 (which is a section pointer from vmap) of vmap.
844 We will not have multiple vmap entries (one for each section), rather transmit
845 text and data base offsets and fix them at the same time. Elimination of sex
846 entry in vmap make this function obsolute, use the one from exec.c.
847 Need further testing!! FIXMEmgo. */
850 set_section_command(args, from_tty)
853 register struct vmap *vp = vmap;
856 unsigned long secaddr;
861 error("Must specify section name and its virtual address");
863 /* Parse out section name */
864 for (secname = args; !isspace(*args); args++)
866 seclen = args - secname;
868 /* Parse out new virtual address */
869 secaddr = parse_and_eval_address(args);
871 for (vp = vmap; vp; vp = vp->nxt) {
873 , bfd_section_name(vp->bfd, vp->sex), seclen)
874 && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
875 offset = secaddr - vp->tstart;
876 vp->tstart += offset;
883 if (seclen >= sizeof(secprint))
884 seclen = sizeof(secprint) - 1;
885 strncpy(secprint, secname, seclen);
886 secprint[seclen] = '\0';
887 error("Section %s not found", secprint);
891 set_section_command (args, from_tty)
895 struct section_table *p;
898 unsigned long secaddr;
903 error ("Must specify section name and its virtual address");
905 /* Parse out section name */
906 for (secname = args; !isspace(*args); args++) ;
907 seclen = args - secname;
909 /* Parse out new virtual address */
910 secaddr = parse_and_eval_address (args);
912 for (p = exec_sections; p < exec_sections_end; p++) {
913 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
914 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
915 offset = secaddr - p->addr;
917 p->endaddr += offset;
922 if (seclen >= sizeof (secprint))
923 seclen = sizeof (secprint) - 1;
924 strncpy (secprint, secname, seclen);
925 secprint[seclen] = '\0';
926 error ("Section %s not found", secprint);
931 struct target_ops exec_ops = {
932 "exec", "Local exec file",
933 "Use an executable file as a target.\n\
934 Specify the filename of the executable file.",
935 exec_file_command, exec_close, /* open, close */
936 child_attach, 0, 0, 0, /* attach, detach, resume, wait, */
937 0, 0, /* fetch_registers, store_registers, */
938 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */
939 xfer_memory, exec_files_info,
940 0, 0, /* insert_breakpoint, remove_breakpoint, */
941 0, 0, 0, 0, 0, /* terminal stuff */
942 0, 0, /* kill, load */
944 child_create_inferior,
945 0, /* mourn_inferior */
946 file_stratum, 0, /* next */
947 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
948 0, 0, /* section pointers */
949 OPS_MAGIC, /* Always the last thing */
957 add_com("file", class_files, file_command,
958 "Use FILE as program to be debugged.\n\
959 It is read for its symbols, for getting the contents of pure memory,\n\
960 and it is the program executed when you use the `run' command.\n\
961 If FILE cannot be found as specified, your execution directory path\n\
962 ($PATH) is searched for a command of that name.\n\
963 No arg means to have no executable file and no symbols.");
965 add_com("exec-file", class_files, exec_file_command,
966 "Use FILE as program for getting contents of pure memory.\n\
967 If FILE cannot be found as specified, your execution directory path\n\
968 is searched for a command of that name.\n\
969 No arg means have no executable file.");
971 add_com("section", class_files, set_section_command,
972 "Change the base address of section SECTION of the exec file to ADDR.\n\
973 This can be used if the exec file does not contain section addresses,\n\
974 (such as in the a.out format), or when the addresses specified in the\n\
975 file itself are wrong. Each section must be changed separately. The\n\
976 ``info files'' command lists all the sections and their addresses.");
978 add_target(&exec_ops);