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>
41 #include "libbfd.h" /* BFD internals (sigh!) FIXME */
43 struct section_table *exec_sections, *exec_sections_end;
45 #define eq(s0, s1) !strcmp(s0, s1)
47 /* Whether to open exec and core files read-only or read-write. */
51 bfd *exec_bfd; /* needed by core.c */
53 extern char *getenv();
54 extern void child_create_inferior (), child_attach ();
55 extern void add_syms_addr_command ();
56 extern void symbol_file_command ();
57 static void exec_files_info();
60 * the vmap struct is used to describe the virtual address space of
61 * the target we are manipulating. The first entry is always the "exec"
62 * file. Subsequent entries correspond to other objects that are
63 * mapped into the address space of a process created from the "exec" file.
64 * These are either in response to exec()ing the file, in which case all
65 * shared libraries are loaded, or a "load" system call, followed by the
66 * user's issuance of a "load" command.
69 struct vmap *nxt; /* ^ to next in chain */
70 bfd *bfd; /* BFD for mappable object library */
71 char *name; /* ^ to object file name */
72 char *member; /* ^ to member name */
73 CORE_ADDR tstart; /* virtual addr where member is mapped */
74 CORE_ADDR tend; /* virtual upper bound of member */
75 CORE_ADDR tadj; /* heuristically derived adjustment */
76 CORE_ADDR dstart; /* virtual address of data start */
77 CORE_ADDR dend; /* vitrual address of data end */
86 static struct vmap *vmap; /* current vmap */
88 extern struct target_ops exec_ops;
91 /* exec_close - done with exec file, clean up all resources. */
94 exec_close(quitting) {
95 register struct vmap *vp, *nxt;
97 for (nxt = vmap; vp = nxt; ) {
100 free_named_symtabs(vp->name, vp->member); /* XXX */
109 * exec_file_command - handle the "exec" command, &c.
112 exec_file_command(filename, from_tty)
117 target_preopen(from_tty);
118 unpush_target(&exec_ops);
120 /* Now open and digest the file the user requested, if any. */
123 char *scratch_pathname;
126 filename = tilde_expand(filename);
127 make_cleanup(free, filename);
129 scratch_chan = openp(getenv("PATH"), 1, filename, O_RDONLY, 0
130 , &scratch_pathname);
131 if (scratch_chan < 0)
132 perror_with_name(filename);
134 bfd = bfd_fdopenr(scratch_pathname, NULL, scratch_chan);
136 error("Could not open `%s' as an executable file: %s"
137 , scratch_pathname, bfd_errmsg(bfd_error));
139 /* make sure we have an object file */
141 if (!bfd_check_format(bfd, bfd_object))
142 error("\"%s\": not in executable format: %s."
143 , scratch_pathname, bfd_errmsg(bfd_error));
146 /* setup initial vmap */
150 error("Can't find the file sections in `%s': %s"
151 , bfd->filename, bfd_errmsg(bfd_error));
155 if (build_section_table (exec_bfd, &exec_sections, &exec_sections_end))
156 error ("Can't find the file sections in `%s': %s",
157 exec_bfd->filename, bfd_errmsg (bfd_error));
159 /* make sure core, if present, matches */
162 push_target(&exec_ops);
164 /* Tell display code(if any) about the changed file name. */
166 if (exec_file_display_hook)
167 (*exec_file_display_hook)(filename);
170 exec_close(0); /* just in case */
172 printf("No exec file now.\n");
176 /* Set both the exec file and the symbol file, in one command. What a
177 * novelty. Why did GDB go through four major releases before this
181 file_command(arg, from_tty)
184 exec_file_command(arg, from_tty);
185 symbol_file_command(arg, from_tty);
188 /* Locate all mappable sections of a BFD file.
189 table_pp_char is a char * to get it through bfd_map_over_sections;
190 we cast it back to its proper type. */
193 add_to_section_table (abfd, asect, table_pp_char)
198 struct section_table **table_pp = (struct section_table **)table_pp_char;
201 aflag = bfd_get_section_flags (abfd, asect);
202 /* FIXME, we need to handle BSS segment here...it alloc's but doesn't load */
203 if (!(aflag & SEC_LOAD))
205 (*table_pp)->sec_ptr = asect;
206 (*table_pp)->addr = bfd_section_vma (abfd, asect);
207 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
212 build_section_table (some_bfd, start, end)
214 struct section_table **start, **end;
218 count = bfd_count_sections (some_bfd);
220 abort(); /* return 1? */
223 *start = (struct section_table *) xmalloc (count * sizeof (**start));
225 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end);
226 if (*end > *start + count)
228 /* We could realloc the table, but it probably loses for most files. */
233 * lookup_symtab_bfd - find if we currently have any symbol tables from bfd
236 lookup_objfile_bfd(bfd *bfd) {
237 register struct objfile *s;
239 for (s = object_files; s; s = s->next)
247 sex_to_vmap(bfd *bf, sec_ptr sex, struct vmap_and_bfd *vmap_bfd)
249 register struct vmap *vp, **vpp;
250 register struct symtab *syms;
251 bfd *arch = vmap_bfd->pbfd;
252 vp = vmap_bfd->pvmap;
254 if ((bfd_get_section_flags(bf, sex) & SEC_LOAD) == 0)
257 if (!strcmp(bfd_section_name(bf, sex), ".text")) {
259 vp->tend = vp->tstart + bfd_section_size(bf, sex);
261 /* This is quite a tacky way to recognize the `exec' load segment (rather
262 than shared libraries. You should use `arch' instead. FIXMEmgo */
264 vp->tadj = sex->filepos - bfd_section_vma(bf, sex);
269 else if (!strcmp(bfd_section_name(bf, sex), ".data")) {
271 vp->dend = vp->dstart + bfd_section_size(bf, sex);
274 else if (!strcmp(bfd_section_name(bf, sex), ".bss")) /* FIXMEmgo */
275 printf ("bss section in exec! Don't know what the heck to do!\n");
278 /* Make a vmap for the BFD "bf", which might be a member of the archive
279 BFD "arch". If we have not yet read in symbols for this file, do so. */
281 map_vmap (bfd *bf, bfd *arch)
283 struct vmap_and_bfd vmap_bfd;
284 struct vmap *vp, **vpp;
288 vp = (void*) xmalloc (sizeof (*vp));
291 vp->name = bfd_get_filename(arch ? arch : bf);
292 vp->member = arch ? bfd_get_filename(bf) : "";
294 vmap_bfd.pbfd = arch;
296 bfd_map_over_sections (bf, sex_to_vmap, &vmap_bfd);
298 obj = lookup_objfile_bfd (bf);
299 if (exec_bfd && !obj) {
300 name = savestring (bfd_get_filename (bf), strlen (bfd_get_filename (bf)));
301 obj = allocate_objfile (bf, name);
302 syms_from_objfile (obj, 0, 0, 0);
305 /* find the end of the list, and append. */
306 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
312 /* true, if symbol table and misc_function_vector is relocated. */
314 int symtab_relocated = 0;
317 /* vmap_symtab - handle symbol translation on vmapping */
319 vmap_symtab(vp, old_start, vip)
320 register struct vmap *vp;
324 register struct symtab *s;
327 * for each symbol table generated from the vp->bfd
329 for (s = symtab_list; s; s = s->next) {
331 /* skip over if this is not relocatable and doesn't have a line table */
332 if (s->nonreloc && !LINETABLE (s))
335 /* matching the symbol table's BFD and the *vp's BFD is hairy.
336 exec_file creates a seperate BFD for possibly the
337 same file as symbol_file.FIXME ALL THIS MUST BE RECTIFIED. */
339 if (s->objfile->obfd == vp->bfd) {
340 /* if they match, we luck out. */
342 } else if (vp->member[0]) {
343 /* no match, and member present, not this one. */
350 * no match, and no member. need to be sure.
352 io = bfd_cache_lookup(s->objfile->obfd);
354 fatal("cannot find BFD's iostream for sym");
356 * see if we are referring to the same file
358 if (fstat(fileno(io), &si) < 0)
359 fatal("cannot fstat BFD for sym");
361 if (si.st_dev != vip->st_dev
362 || si.st_ino != vip->st_ino)
366 if (vp->tstart != old_start)
367 vmap_symtab_1(s, vp, old_start);
370 if (vp->tstart != old_start)
371 fixup_misc_vector (vp->tstart - old_start);
373 symtab_relocated = 1;
377 fixup_misc_vector (int disp)
380 for (ii=0; ii < misc_function_count; ++ii)
381 if (misc_function_vector[ii].address < 0x10000000)
382 misc_function_vector[ii].address += disp;
386 vmap_symtab_1(s, vp, old_start)
387 register struct symtab *s;
388 register struct vmap *vp;
393 register struct linetable *l;
394 struct blockvector *bv;
395 register struct block *b;
397 register ulong reloc, dreloc;
399 if ((reloc = vp->tstart - old_start) == 0)
402 dreloc = vp->dstart; /* data relocation */
405 * The line table must be relocated. This is only present for
406 * b.text sections, so only vp->text type maps need be considered.
410 for (i = 0; i < len; i++)
411 l->item[i].pc += reloc;
413 /* if this symbol table is not relocatable, only line table should
414 be relocated and the rest ignored. */
419 len = BLOCKVECTOR_NBLOCKS(bv);
421 for (i = 0; i < len; i++) {
422 b = BLOCKVECTOR_BLOCK(bv, i);
424 BLOCK_START(b) += reloc;
425 BLOCK_END(b) += reloc;
427 blen = BLOCK_NSYMS(b);
428 for (j = 0; j < blen; j++) {
429 register struct symbol *sym;
431 sym = BLOCK_SYM(b, j);
432 switch (SYMBOL_NAMESPACE(sym)) {
433 case STRUCT_NAMESPACE:
434 case UNDEF_NAMESPACE:
437 case LABEL_NAMESPACE:
442 switch (SYMBOL_CLASS(sym)) {
444 case LOC_CONST_BYTES:
458 SYMBOL_VALUE_ADDRESS(sym) += reloc;
462 SYMBOL_VALUE_ADDRESS(sym) += dreloc;
469 fatal("botched symbol class %x"
470 , SYMBOL_CLASS(sym));
478 * add_vmap - add a new vmap entry based on ldinfo() information
481 register struct ld_info *ldi; {
485 mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1;
486 bfd = bfd_fdopenr(ldi->ldinfo_filename, NULL, ldi->ldinfo_fd);
488 error("Could not open `%s' as an executable file: %s"
489 , ldi->ldinfo_filename, bfd_errmsg(bfd_error));
492 /* make sure we have an object file */
494 if (bfd_check_format(bfd, bfd_object))
497 else if (bfd_check_format(bfd, bfd_archive)) {
500 * FIXME??? am I tossing BFDs? bfd?
502 while (last = bfd_openr_next_archived_file(bfd, last))
503 if (eq(mem, last->filename))
508 /* FIXME -- should be error */
509 warning("\"%s\": member \"%s\" missing.",
514 if (!bfd_check_format(last, bfd_object)) {
515 bfd_close(last); /* XXX??? */
519 map_vmap (last, bfd);
524 /* FIXME -- should be error */
525 warning("\"%s\": not in executable format: %s."
526 , ldi->ldinfo_filename, bfd_errmsg(bfd_error));
532 /* As well as symbol tables, exec_sections need relocation. Otherwise after
533 the inferior process terminates, symbol table is relocated but there is
534 no inferior process. Thus, we have to use `exec' bfd, rather than the inferior
535 process's memory space, when lookipng at symbols.
536 `exec_sections' need to be relocated only once though, as long as the exec
537 file was not changed.
542 if (execbfd == exec_bfd)
547 if (!vmap || !exec_sections) {
548 printf ("WARNING: vmap not found in vmap_exec()!\n");
551 /* First exec section is `.text', second is `.data'. If this is changed,
552 then this routine will choke. Better you should check section names,
554 exec_sections [0].addr += vmap->tstart;
555 exec_sections [0].endaddr += vmap->tstart;
556 exec_sections [1].addr += vmap->dstart;
557 exec_sections [1].endaddr += vmap->dstart;
562 text_adjustment (abfd)
566 static int adjustment;
569 if (exec_bfd == execbfd)
572 sect = bfd_get_section_by_name (abfd, ".text");
574 adjustment = sect->filepos - sect->vma;
576 adjustment = 0x200; /* just a wild assumption */
583 * vmap_ldinfo - update VMAP info with ldinfo() information
586 * ldi - ^ to ldinfo() results.
589 register struct ld_info *ldi;
592 register struct vmap *vp;
593 register got_one, retried;
597 * for each *ldi, see if we have a corresponding *vp
598 * if so, update the mapping, and symbol table.
599 * if not, add an entry and symbol table.
602 char *name = ldi->ldinfo_filename;
603 char *memb = name + strlen(name) + 1;
607 if (fstat(ldi->ldinfo_fd, &ii) < 0)
608 fatal("cannot fstat(%d) on %s"
612 for (got_one = 0, vp = vmap; vp; vp = vp->nxt) {
615 /* The filenames are not always sufficient to match on. */
617 && !eq(name, vp->name))
618 || (memb[0] && !eq(memb, vp->member)))
621 /* totally opaque! */
622 io = bfd_cache_lookup(vp->bfd);
624 fatal("cannot find BFD's iostream for %s"
627 /* see if we are referring to the same file */
628 if (fstat(fileno(io), &vi) < 0)
629 fatal("cannot fstat BFD for %s", vp->name);
631 if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino)
635 close(ldi->ldinfo_fd);
639 /* found a corresponding VMAP. remap! */
642 vp->tstart = ldi->ldinfo_textorg;
643 vp->tend = vp->tstart + ldi->ldinfo_textsize;
644 vp->dstart = ldi->ldinfo_dataorg;
645 vp->dend = vp->dstart + ldi->ldinfo_datasize;
648 vp->tstart += vp->tadj;
649 vp->tend += vp->tadj;
652 /* relocate symbol table(s). */
653 vmap_symtab(vp, ostart, &vi);
655 /* there may be more, so we don't break out of the loop. */
659 * if there was no matching *vp, we must perforce create
662 if (!got_one && !retried) {
667 } while (ldi->ldinfo_next
668 && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));
674 * vmap_inferior - print VMAP info for inferior
678 if (inferior_pid == 0)
679 return 0; /* normal processing */
686 /* Read or write the exec file.
688 Args are address within exec file, address within gdb address-space,
689 length, and a flag indicating whether to read or write.
693 0: We cannot handle this address and length.
694 > 0: We have handled N bytes starting at this address.
695 (If N == length, we did it all.) We might be able
696 to handle more bytes beyond this length, but no
698 < 0: We cannot handle this address, but if somebody
699 else handles (-N) bytes, we can start from there.
701 The same routine is used to handle both core and exec files;
702 we just tail-call it with more arguments to select between them. */
705 xfer_memory (memaddr, myaddr, len, write, abfd, sections, sections_end)
711 struct section_table *sections, *sections_end;
714 struct section_table *p;
715 CORE_ADDR nextsectaddr, memend;
716 boolean (*xfer_fn) ();
721 memend = memaddr + len;
722 xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents;
723 nextsectaddr = memend;
725 for (p = sections; p < sections_end; p++)
727 if (p->addr <= memaddr)
728 if (p->endaddr >= memend)
730 /* Entire transfer is within this section. */
731 res = xfer_fn (abfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
732 return (res != false)? len: 0;
734 else if (p->endaddr <= memaddr)
736 /* This section ends before the transfer starts. */
741 /* This section overlaps the transfer. Just do half. */
742 len = p->endaddr - memaddr;
743 res = xfer_fn (abfd, p->sec_ptr, myaddr, memaddr - p->addr, len);
744 return (res != false)? len: 0;
746 else if (p->addr < nextsectaddr)
747 nextsectaddr = p->addr;
750 if (nextsectaddr >= memend)
751 return 0; /* We can't help */
753 return - (nextsectaddr - memaddr); /* Next boundary where we can help */
756 /* The function called by target_xfer_memory via our target_ops */
759 exec_xfer_memory (memaddr, myaddr, len, write)
765 return xfer_memory (memaddr, myaddr, len, write,
766 exec_bfd, exec_sections, exec_sections_end);
770 * exec_files_info - "info files" command processor
774 register struct vmap *vp = vmap;
779 printf("\tMapping info for file `%s'.\n", vp->name);
780 printf("\t %8.8s %8.8s %8.8s %s\n"
781 , "start", "end", "section", "file(member)");
783 for (; vp; vp = vp->nxt)
784 printf("\t0x%8.8x 0x%8.8x %s%s%s%s\n"
788 , *vp->member ? "(" : ""
790 , *vp->member ? ")" : "");
794 /* Damon's implementation of set_section_command! It is based on the sex member
795 (which is a section pointer from vmap) of vmap.
796 We will not have multiple vmap entries (one for each section), rather transmit
797 text and data base offsets and fix them at the same time. Elimination of sex
798 entry in vmap make this function obsolute, use the one from exec.c.
799 Need further testing!! FIXMEmgo. */
802 set_section_command(args, from_tty)
805 register struct vmap *vp = vmap;
808 unsigned long secaddr;
813 error("Must specify section name and its virtual address");
815 /* Parse out section name */
816 for (secname = args; !isspace(*args); args++)
818 seclen = args - secname;
820 /* Parse out new virtual address */
821 secaddr = parse_and_eval_address(args);
823 for (vp = vmap; vp; vp = vp->nxt) {
825 , bfd_section_name(vp->bfd, vp->sex), seclen)
826 && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') {
827 offset = secaddr - vp->tstart;
828 vp->tstart += offset;
835 if (seclen >= sizeof(secprint))
836 seclen = sizeof(secprint) - 1;
837 strncpy(secprint, secname, seclen);
838 secprint[seclen] = '\0';
839 error("Section %s not found", secprint);
843 set_section_command (args, from_tty)
847 struct section_table *p;
850 unsigned long secaddr;
855 error ("Must specify section name and its virtual address");
857 /* Parse out section name */
858 for (secname = args; !isspace(*args); args++) ;
859 seclen = args - secname;
861 /* Parse out new virtual address */
862 secaddr = parse_and_eval_address (args);
864 for (p = exec_sections; p < exec_sections_end; p++) {
865 if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen)
866 && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') {
867 offset = secaddr - p->addr;
869 p->endaddr += offset;
874 if (seclen >= sizeof (secprint))
875 seclen = sizeof (secprint) - 1;
876 strncpy (secprint, secname, seclen);
877 secprint[seclen] = '\0';
878 error ("Section %s not found", secprint);
883 struct target_ops exec_ops = {
884 "exec", "Local exec file",
885 "Use an executable file as a target.\n\
886 Specify the filename of the executable file.",
887 exec_file_command, exec_close, /* open, close */
888 child_attach, 0, 0, 0, /* attach, detach, resume, wait, */
889 0, 0, /* fetch_registers, store_registers, */
890 0, 0, 0, /* prepare_to_store, conv_to, conv_from, */
891 exec_xfer_memory, exec_files_info,
892 0, 0, /* insert_breakpoint, remove_breakpoint, */
893 0, 0, 0, 0, 0, /* terminal stuff */
894 0, 0, /* kill, load */
895 0, 0, /* call fn, lookup sym */
896 child_create_inferior,
897 0, /* mourn_inferior */
898 file_stratum, 0, /* next */
899 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */
900 0, 0, /* section pointers */
901 OPS_MAGIC, /* Always the last thing */
909 add_com("file", class_files, file_command,
910 "Use FILE as program to be debugged.\n\
911 It is read for its symbols, for getting the contents of pure memory,\n\
912 and it is the program executed when you use the `run' command.\n\
913 If FILE cannot be found as specified, your execution directory path\n\
914 ($PATH) is searched for a command of that name.\n\
915 No arg means to have no executable file and no symbols.");
917 add_com("exec-file", class_files, exec_file_command,
918 "Use FILE as program for getting contents of pure memory.\n\
919 If FILE cannot be found as specified, your execution directory path\n\
920 is searched for a command of that name.\n\
921 No arg means have no executable file.");
923 add_com("section", class_files, set_section_command,
924 "Change the base address of section SECTION of the exec file to ADDR.\n\
925 This can be used if the exec file does not contain section addresses,\n\
926 (such as in the a.out format), or when the addresses specified in the\n\
927 file itself are wrong. Each section must be changed separately. The\n\
928 ``info files'' command lists all the sections and their addresses.");
930 add_target(&exec_ops);