⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 executable.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
		fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);		break;	case FARM:		fp->txtaddr = mach->kbase+0x8010;		fp->name = "ARM plan 9 boot image";		fp->dataddr = fp->txtaddr+fp->txtsz;		return;	case FALPHA:		fp->type = FALPHAB;		fp->txtaddr = (u32int)fp->entry;		fp->name = "alpha plan 9 boot image";		fp->dataddr = fp->txtaddr+fp->txtsz;		break;	case FPOWER:		fp->type = FPOWERB;		fp->txtaddr = (u32int)fp->entry;		fp->name = "power plan 9 boot image";		fp->dataddr = fp->txtaddr+fp->txtsz;		break;	case FAMD64:		fp->type = FAMD64B;		fp->txtaddr = fp->entry;		fp->name = "amd64 plan 9 boot image";		fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);		break;	default:		return;	}	fp->hdrsz = 0;			/* header stripped */}/* *	_MAGIC() style headers and *	alpha plan9-style bootable images for axp "headerless" boot * */static intcommon(int fd, Fhdr *fp, ExecHdr *hp){	adotout(fd, fp, hp);	if(hp->e.magic & DYN_MAGIC) {		fp->txtaddr = 0;		fp->dataddr = fp->txtsz;		return 1;	}	commonboot(fp);	return 1;}static intcommonllp64(int, Fhdr *fp, ExecHdr *hp){	long pgsize;	uvlong entry;	hswal(&hp->e, sizeof(Exec)/sizeof(long), beswal);	if(!(hp->e.magic & HDR_MAGIC))		return 0;	/*	 * There can be more magic here if the	 * header ever needs more expansion.	 * For now just catch use of any of the	 * unused bits.	 */	if((hp->e.magic & ~DYN_MAGIC)>>16)		return 0;	entry = beswav(hp->e.hdr[0]);	pgsize = mach->pgsize;	settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);	setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),		hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);	setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);	if(hp->e.magic & DYN_MAGIC) {		fp->txtaddr = 0;		fp->dataddr = fp->txtsz;		return 1;	}	commonboot(fp);	return 1;}/* *	mips bootable image. */static intmipsboot(int fd, Fhdr *fp, ExecHdr *hp){	USED(fd);	fp->type = FMIPSB;	switch(hp->e.amagic) {	default:	case 0407:	/* some kind of mips */		settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,			hp->e.tsize, sizeof(struct mipsexec)+4);		setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,			fp->txtoff+hp->e.tsize, hp->e.bsize);		break;	case 0413:	/* some kind of mips */		settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,			hp->e.tsize, 0);		setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,			hp->e.tsize, hp->e.bsize);		break;	}	setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);	fp->hdrsz = 0;			/* header stripped */	return 1;}/* *	mips4k bootable image. */static intmips4kboot(int fd, Fhdr *fp, ExecHdr *hp){	USED(fd);	fp->type = FMIPSB;	switch(hp->e.h.amagic) {	default:	case 0407:	/* some kind of mips */		settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,			hp->e.h.tsize, sizeof(struct mips4kexec));		setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,			fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);		break;	case 0413:	/* some kind of mips */		settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,			hp->e.h.tsize, 0);		setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,			hp->e.h.tsize, hp->e.h.bsize);		break;	}	setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);	fp->hdrsz = 0;			/* header stripped */	return 1;}/* *	sparc bootable image */static intsparcboot(int fd, Fhdr *fp, ExecHdr *hp){	USED(fd);	fp->type = FSPARCB;	settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,		sizeof(struct sparcexec));	setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,		fp->txtoff+hp->e.stext, hp->e.sbss);	setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);	fp->hdrsz = 0;			/* header stripped */	return 1;}/* *	next bootable image */static intnextboot(int fd, Fhdr *fp, ExecHdr *hp){	USED(fd);	fp->type = FNEXTB;	settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,		hp->e.texts.size, hp->e.texts.offset);	setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,		hp->e.datas.offset, hp->e.bsss.size);	setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,		hp->e.symc.symoff);	fp->hdrsz = 0;			/* header stripped */	return 1;}/* * Elf32 binaries. */static intelfdotout(int fd, Fhdr *fp, ExecHdr *hp){	ulong (*swal)(ulong);	ushort (*swab)(ushort);	Ehdr *ep;	Phdr *ph;	int i, it, id, is, phsz;	/* bitswap the header according to the DATA format */	ep = &hp->e;	if(ep->ident[CLASS] != ELFCLASS32) {		werrstr("bad ELF class - not 32 bit");		return 0;	}	if(ep->ident[DATA] == ELFDATA2LSB) {		swab = leswab;		swal = leswal;	} else if(ep->ident[DATA] == ELFDATA2MSB) {		swab = beswab;		swal = beswal;	} else {		werrstr("bad ELF encoding - not big or little endian");		return 0;	}	ep->type = swab(ep->type);	ep->machine = swab(ep->machine);	ep->version = swal(ep->version);	ep->elfentry = swal(ep->elfentry);	ep->phoff = swal(ep->phoff);	ep->shoff = swal(ep->shoff);	ep->flags = swal(ep->flags);	ep->ehsize = swab(ep->ehsize);	ep->phentsize = swab(ep->phentsize);	ep->phnum = swab(ep->phnum);	ep->shentsize = swab(ep->shentsize);	ep->shnum = swab(ep->shnum);	ep->shstrndx = swab(ep->shstrndx);	if(ep->type != EXEC || ep->version != CURRENT)		return 0;	/* we could definitely support a lot more machines here */	fp->magic = ELF_MAG;	fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;	switch(ep->machine) {	case I386:		mach = &mi386;		fp->type = FI386;		break;	case MIPS:		mach = &mmips;		fp->type = FMIPS;		break;	case SPARC64:		mach = &msparc64;		fp->type = FSPARC64;		break;	case POWER:		mach = &mpower;		fp->type = FPOWER;		break;	case AMD64:		mach = &mamd64;		fp->type = FAMD64;		break;	default:		return 0;	}	if(ep->phentsize != sizeof(Phdr)) {		werrstr("bad ELF header size");		return 0;	}	phsz = sizeof(Phdr)*ep->phnum;	ph = malloc(phsz);	if(!ph)		return 0;	seek(fd, ep->phoff, 0);	if(read(fd, ph, phsz) < 0) {		free(ph);		return 0;	}	hswal(ph, phsz/sizeof(ulong), swal);	/* find text, data and symbols and install them */	it = id = is = -1;	for(i = 0; i < ep->phnum; i++) {		if(ph[i].type == LOAD		&& (ph[i].flags & (R|X)) == (R|X) && it == -1)			it = i;		else if(ph[i].type == LOAD		&& (ph[i].flags & (R|W)) == (R|W) && id == -1)			id = i;		else if(ph[i].type == NOPTYPE && is == -1)			is = i;	}	if(it == -1 || id == -1) {		/*		 * The SPARC64 boot image is something of an ELF hack.		 * Text+Data+BSS are represented by ph[0].  Symbols		 * are represented by ph[1]:		 *		 *		filesz, memsz, vaddr, paddr, off		 * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff		 * ph[1] : symsz, lcsz, 0, 0, symoff		 */		if(ep->machine == SPARC64 && ep->phnum == 2) {			ulong txtaddr, txtsz, dataddr, bsssz;			txtaddr = ph[0].vaddr | 0x80000000;			txtsz = ph[0].filesz - ph[0].paddr;			dataddr = txtaddr + txtsz;			bsssz = ph[0].memsz - ph[0].filesz;			settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);			setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);			setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);			free(ph);			return 1;		}		werrstr("No TEXT or DATA sections");		free(ph);		return 0;	}	settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);	setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);	if(is != -1)		setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);	free(ph);	return 1;}/* * (Free|Net)BSD ARM header. */static intarmdotout(int fd, Fhdr *fp, ExecHdr *hp){	uvlong kbase;	USED(fd);	settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));	setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);	setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);	kbase = 0xF0000000;	if ((fp->entry & kbase) == kbase) {		/* Boot image */		fp->txtaddr = kbase+sizeof(Exec);		fp->name = "ARM *BSD boot image";		fp->hdrsz = 0;		/* header stripped */		fp->dataddr = kbase+fp->txtsz;	}	return 1;}static voidsettext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off){	fp->txtaddr = a;	fp->entry = e;	fp->txtsz = s;	fp->txtoff = off;}static voidsetdata(Fhdr *fp, uvlong a, long s, vlong off, long bss){	fp->dataddr = a;	fp->datsz = s;	fp->datoff = off;	fp->bsssz = bss;}static voidsetsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff){	fp->symsz = symsz;	fp->symoff = symoff;	fp->sppcsz = sppcsz;	fp->sppcoff = fp->symoff+fp->symsz;	fp->lnpcsz = lnpcsz;	fp->lnpcoff = fp->sppcoff+fp->sppcsz;}static uvlong_round(uvlong a, ulong b){	uvlong w;	w = (a/b)*b;	if (a!=w)		w += b;	return(w);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -