📄 xcoffexec.c
字号:
case LOC_REF_ARG: case LOC_REGPARM: case LOC_TYPEDEF: continue; #ifdef FIXME case LOC_EXTERNAL:#endif case LOC_LABEL: SYMBOL_VALUE_ADDRESS(sym) += reloc; break; case LOC_STATIC: SYMBOL_VALUE_ADDRESS(sym) += dreloc; break; case LOC_BLOCK: break; default: fatal("botched symbol class %x" , SYMBOL_CLASS(sym)); break; } } }}/* * add_vmap - add a new vmap entry based on ldinfo() information */add_vmap(ldi)register struct ld_info *ldi; { bfd *bfd, *last; register char *mem, *objname; /* This ldi structure was allocated using alloca() in xcoff_relocate_symtab(). Now we need to have persistent object and member names, so we should save them. */ mem = ldi->ldinfo_filename + strlen(ldi->ldinfo_filename) + 1; mem = savestring (mem, strlen (mem)); objname = savestring (ldi->ldinfo_filename, strlen (ldi->ldinfo_filename)); bfd = bfd_fdopenr(objname, NULL, ldi->ldinfo_fd); if (!bfd) error("Could not open `%s' as an executable file: %s", objname, bfd_errmsg(bfd_error)); /* make sure we have an object file */ if (bfd_check_format(bfd, bfd_object)) map_vmap (bfd, 0); else if (bfd_check_format(bfd, bfd_archive)) { last = 0; /* * FIXME??? am I tossing BFDs? bfd? */ while (last = bfd_openr_next_archived_file(bfd, last)) if (eq(mem, last->filename)) break; if (!last) { bfd_close(bfd); /* FIXME -- should be error */ warning("\"%s\": member \"%s\" missing.", bfd->filename, mem); return; } if (!bfd_check_format(last, bfd_object)) { bfd_close(last); /* XXX??? */ goto obj_err; } map_vmap (last, bfd); } else { obj_err: bfd_close(bfd);/* FIXME -- should be error */ warning("\"%s\": not in executable format: %s." , objname, bfd_errmsg(bfd_error)); return; }}/* As well as symbol tables, exec_sections need relocation. After the inferior process' termination, there will be a relocated symbol table exist with no corresponding inferior process. At that time, we need to use `exec' bfd, rather than the inferior process's memory space to look up symbols. `exec_sections' need to be relocated only once, as long as the exec file remains unchanged.*/vmap_exec (){ static bfd *execbfd; int i; if (execbfd == exec_bfd) return; execbfd = exec_bfd; if (!vmap || !exec_ops.to_sections) error ("vmap_exec: vmap or exec_ops.to_sections == 0\n"); for (i=0; &exec_ops.to_sections[i] < exec_ops.to_sections_end; i++) { if (strcmp(".text", exec_ops.to_sections[i].sec_ptr->name) == 0) { exec_ops.to_sections[i].addr += vmap->tstart; exec_ops.to_sections[i].endaddr += vmap->tstart; } else if (strcmp(".data", exec_ops.to_sections[i].sec_ptr->name) == 0) { exec_ops.to_sections[i].addr += vmap->dstart; exec_ops.to_sections[i].endaddr += vmap->dstart; } }}inttext_adjustment (abfd)bfd *abfd;{ static bfd *execbfd; static int adjustment; sec_ptr sect; if (exec_bfd == execbfd) return adjustment; sect = bfd_get_section_by_name (abfd, ".text"); if (sect) adjustment = sect->filepos - sect->vma; else adjustment = 0x200; /* just a wild assumption */ return adjustment;}/* * vmap_ldinfo - update VMAP info with ldinfo() information * * Input: * ldi - ^ to ldinfo() results. */vmap_ldinfo(ldi)register struct ld_info *ldi;{ struct stat ii, vi; register struct vmap *vp; register got_one, retried; CORE_ADDR ostart; /* * for each *ldi, see if we have a corresponding *vp * if so, update the mapping, and symbol table. * if not, add an entry and symbol table. */ do { char *name = ldi->ldinfo_filename; char *memb = name + strlen(name) + 1; retried = 0; if (fstat(ldi->ldinfo_fd, &ii) < 0) fatal("cannot fstat(%d) on %s" , ldi->ldinfo_fd , name);retry: for (got_one = 0, vp = vmap; vp; vp = vp->nxt) { FILE *io; /* First try to find a `vp', which is the same as in ldinfo. If not the same, just continue and grep the next `vp'. If same, relocate its tstart, tend, dstart, dend values. If no such `vp' found, get out of this for loop, add this ldi entry as a new vmap (add_vmap) and come back, fins its `vp' and so on... */ /* The filenames are not always sufficient to match on. */ if ((name[0] == "/" && !eq(name, vp->name)) || (memb[0] && !eq(memb, vp->member))) continue; io = bfd_cache_lookup(vp->bfd); /* totally opaque! */ if (!io) fatal("cannot find BFD's iostream for %s", vp->name); /* see if we are referring to the same file */ if (fstat(fileno(io), &vi) < 0) fatal("cannot fstat BFD for %s", vp->name); if (ii.st_dev != vi.st_dev || ii.st_ino != vi.st_ino) continue; if (!retried) close(ldi->ldinfo_fd); ++got_one; /* found a corresponding VMAP. remap! */ ostart = vp->tstart; vp->tstart = ldi->ldinfo_textorg; vp->tend = vp->tstart + ldi->ldinfo_textsize; vp->dstart = ldi->ldinfo_dataorg; vp->dend = vp->dstart + ldi->ldinfo_datasize; if (vp->tadj) { vp->tstart += vp->tadj; vp->tend += vp->tadj; } /* relocate symbol table(s). */ vmap_symtab(vp, ostart, &vi); /* there may be more, so we don't break out of the loop. */ } /* if there was no matching *vp, we must perforce create the sucker(s) */ if (!got_one && !retried) { add_vmap(ldi); ++retried; goto retry; } } while (ldi->ldinfo_next && (ldi = (void *) (ldi->ldinfo_next + (char *) ldi)));}/* * vmap_inferior - print VMAP info for inferior */vmap_inferior() { if (inferior_pid == 0) return 0; /* normal processing */ exec_files_info(); return 1;}/* Read or write the exec file. Args are address within exec file, address within gdb address-space, length, and a flag indicating whether to read or write. Result is a length: 0: We cannot handle this address and length. > 0: We have handled N bytes starting at this address. (If N == length, we did it all.) We might be able to handle more bytes beyond this length, but no promises. < 0: We cannot handle this address, but if somebody else handles (-N) bytes, we can start from there. The same routine is used to handle both core and exec files; we just tail-call it with more arguments to select between them. */intxfer_memory (memaddr, myaddr, len, write, target) CORE_ADDR memaddr; char *myaddr; int len; int write; struct target_ops *target;{ boolean res; struct section_table *p; CORE_ADDR nextsectaddr, memend; boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); if (len <= 0) abort(); memend = memaddr + len; xfer_fn = write? bfd_set_section_contents: bfd_get_section_contents; nextsectaddr = memend; for (p = target->to_sections; p < target->to_sections_end; p++) { if (p->addr <= memaddr) if (p->endaddr >= memend) { /* Entire transfer is within this section. */ res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); return (res != false)? len: 0; } else if (p->endaddr <= memaddr) { /* This section ends before the transfer starts. */ continue; } else { /* This section overlaps the transfer. Just do half. */ len = p->endaddr - memaddr; res = xfer_fn (p->bfd, p->sec_ptr, myaddr, memaddr - p->addr, len); return (res != false)? len: 0; } else if (p->addr < nextsectaddr) nextsectaddr = p->addr; } if (nextsectaddr >= memend) return 0; /* We can't help */ else return - (nextsectaddr - memaddr); /* Next boundary where we can help */}voidprint_section_info (t, abfd) struct target_ops *t; bfd *abfd;{ struct section_table *p; printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); wrap_here (" "); printf_filtered ("file type %s.\n", bfd_get_target(abfd)); for (p = t->to_sections; p < t->to_sections_end; p++) { printf_filtered ("\t%s", local_hex_string_custom (p->addr, "08")); printf_filtered (" - %s", local_hex_string_custom (p->endaddr, "08")); if (info_verbose) printf_filtered (" @ %s", local_hex_string_custom (p->sec_ptr->filepos, "08")); printf_filtered (" is %s", bfd_section_name (p->bfd, p->sec_ptr)); if (p->bfd != abfd) { printf_filtered (" in %s", bfd_get_filename (p->bfd)); } printf_filtered ("\n"); }}static voidexec_files_info (t) struct target_ops *t;{ register struct vmap *vp = vmap; print_section_info (t, exec_bfd); if (!vp) return; printf("\tMapping info for file `%s'.\n", vp->name); printf("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n", "tstart", "tend", "dstart", "dend", "section", "file(member)"); for (; vp; vp = vp->nxt) printf("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n", vp->tstart, vp->tend, vp->dstart, vp->dend, vp->name, *vp->member ? "(" : "", vp->member, *vp->member ? ")" : "");}#ifdef DAMON/* Damon's implementation of set_section_command! It is based on the sex member (which is a section pointer from vmap) of vmap. We will not have multiple vmap entries (one for each section), rather transmit text and data base offsets and fix them at the same time. Elimination of sex entry in vmap make this function obsolute, use the one from exec.c. Need further testing!! FIXMEmgo. */static voidset_section_command(args, from_tty)char *args; { register struct vmap *vp = vmap; char *secname; unsigned seclen; unsigned long secaddr; char secprint[100]; long offset; if (args == 0) error("Must specify section name and its virtual address"); /* Parse out section name */ for (secname = args; !isspace(*args); args++) ; seclen = args - secname; /* Parse out new virtual address */ secaddr = parse_and_eval_address(args); for (vp = vmap; vp; vp = vp->nxt) { if (!strncmp(secname , bfd_section_name(vp->bfd, vp->sex), seclen) && bfd_section_name(vp->bfd, vp->sex)[seclen] == '\0') { offset = secaddr - vp->tstart; vp->tstart += offset; vp->tend += offset; exec_files_info(); return; } } if (seclen >= sizeof(secprint)) seclen = sizeof(secprint) - 1; strncpy(secprint, secname, seclen); secprint[seclen] = '\0'; error("Section %s not found", secprint);}#elsestatic voidset_section_command (args, from_tty) char *args; int from_tty;{ struct section_table *p; char *secname; unsigned seclen; unsigned long secaddr; char secprint[100]; long offset; if (args == 0) error ("Must specify section name and its virtual address"); /* Parse out section name */ for (secname = args; !isspace(*args); args++) ; seclen = args - secname; /* Parse out new virtual address */ secaddr = parse_and_eval_address (args); for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { if (!strncmp (secname, bfd_section_name (exec_bfd, p->sec_ptr), seclen) && bfd_section_name (exec_bfd, p->sec_ptr)[seclen] == '\0') { offset = secaddr - p->addr; p->addr += offset; p->endaddr += offset; if (from_tty) exec_files_info(&exec_ops); return; } } if (seclen >= sizeof (secprint)) seclen = sizeof (secprint) - 1; strncpy (secprint, secname, seclen); secprint[seclen] = '\0'; error ("Section %s not found", secprint);}#endif /* !DAMON */struct target_ops exec_ops = { "exec", "Local exec file", "Use an executable file as a target.\n\Specify the filename of the executable file.", exec_file_command, exec_close, /* open, close */ find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */ 0, 0, /* fetch_registers, store_registers, */ 0, /* prepare_to_store */ xfer_memory, exec_files_info, 0, 0, /* insert_breakpoint, remove_breakpoint, */ 0, 0, 0, 0, 0, /* terminal stuff */ 0, 0, /* kill, load */ 0, /* lookup sym */ find_default_create_inferior, 0, /* mourn_inferior */ 0, /* can_run */ 0, /* notice_signals */ file_stratum, 0, /* next */ 0, 1, 0, 0, 0, /* all mem, mem, stack, regs, exec */ 0, 0, /* section pointers */ OPS_MAGIC, /* Always the last thing */};void_initialize_exec(){ add_com("file", class_files, file_command, "Use FILE as program to be debugged.\n\It is read for its symbols, for getting the contents of pure memory,\n\and it is the program executed when you use the `run' command.\n\If FILE cannot be found as specified, your execution directory path\n\($PATH) is searched for a command of that name.\n\No arg means to have no executable file and no symbols."); add_com("exec-file", class_files, exec_file_command, "Use FILE as program for getting contents of pure memory.\n\If FILE cannot be found as specified, your execution directory path\n\is searched for a command of that name.\n\No arg means have no executable file."); add_com("section", class_files, set_section_command, "Change the base address of section SECTION of the exec file to ADDR.\n\This can be used if the exec file does not contain section addresses,\n\(such as in the a.out format), or when the addresses specified in the\n\file itself are wrong. Each section must be changed separately. The\n\``info files'' command lists all the sections and their addresses."); add_target(&exec_ops);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -