Charles.Forsyth20060303644188019 years agoHistory
1403 lines · 30.0 KB · C
RawDownload
#include	"l.h" long	OFFSET;/*long	BADOFFSET	=	-1; 		if(OFFSET <= BADOFFSET && OFFSET+4 > BADOFFSET)\			abort();\		OFFSET += 4;\ 		if(OFFSET == BADOFFSET)\			abort();\		OFFSET++;\*/ #define	LPUT(c)\	{\		cbp[0] = (c)>>24;\		cbp[1] = (c)>>16;\		cbp[2] = (c)>>8;\		cbp[3] = (c);\		cbp += 4;\		cbc -= 4;\		if(cbc <= 0)\			cflush();\	} #define	CPUT(c)\	{\		cbp[0] = (c);\		cbp++;\		cbc--;\		if(cbc <= 0)\			cflush();\	} longentryvalue(void){	char *a;	Sym *s; 	a = INITENTRY;	if(*a >= '0' && *a <= '9')		return atolwhex(a);	s = lookup(a, 0);	if(s->type == 0)		return INITTEXT;	if(s->type != STEXT && s->type != SLEAF)		diag("entry not text: %s", s->name);	return s->value;} voidasmb(void){	Prog *p;	long t, etext;	Optab *o; 	if(debug['v'])		Bprint(&bso, "%5.2f asm\n", cputime());	Bflush(&bso);	OFFSET = HEADR;	seek(cout, OFFSET, 0);	pc = INITTEXT;	for(p = firstp; p != P; p = p->link) {		if(p->as == ATEXT) {			curtext = p;			autosize = p->to.offset + 4;		}		if(p->pc != pc) {			diag("phase error %lux sb %lux",				p->pc, pc);			if(!debug['a'])				prasm(curp);			pc = p->pc;		}		curp = p;		o = oplook(p);	/* could probably avoid this call */		if(asmout(p, o, 0)) {			p = p->link;			pc += 4;		}		pc += o->size;	}	if(debug['a'])		Bprint(&bso, "\n");	Bflush(&bso);	cflush(); 	etext = INITTEXT + textsize;	for(t = pc; t < etext; t += sizeof(buf)-100) {		if(etext-t > sizeof(buf)-100)			datblk(t, sizeof(buf)-100, 1);		else			datblk(t, etext-t, 1);	} 	Bflush(&bso);	cflush(); 	curtext = P;	switch(HEADTYPE) {	case 0:	case 4:		OFFSET = rnd(HEADR+textsize, 4096);		seek(cout, OFFSET, 0);		break;	case 1:	case 2:	case 3:	case 5:		OFFSET = HEADR+textsize;		seek(cout, OFFSET, 0);		break;	}	for(t = 0; t < datsize; t += sizeof(buf)-100) {		if(datsize-t > sizeof(buf)-100)			datblk(t, sizeof(buf)-100, 0);		else			datblk(t, datsize-t, 0);	} 	symsize = 0;	lcsize = 0;	if(!debug['s']) {		if(debug['v'])			Bprint(&bso, "%5.2f sym\n", cputime());		Bflush(&bso);		switch(HEADTYPE) {		case 0:		case 4:			OFFSET = rnd(HEADR+textsize, 4096)+datsize;			seek(cout, OFFSET, 0);			break;		case 3:		case 2:		case 1:		case 5:			OFFSET = HEADR+textsize+datsize;			seek(cout, OFFSET, 0);			break;		}		if(!debug['s'])			asmsym();		if(debug['v'])			Bprint(&bso, "%5.2f pc\n", cputime());		Bflush(&bso);		if(!debug['s'])			asmlc();		cflush();	} 	if(debug['v'])		Bprint(&bso, "%5.2f header\n", cputime());	Bflush(&bso);	OFFSET = 0;	seek(cout, OFFSET, 0);	switch(HEADTYPE) {	case 0:		lput(0x160L<<16);		/* magic and sections */		lput(0L);			/* time and date */		lput(rnd(HEADR+textsize, 4096)+datsize);		lput(symsize);			/* nsyms */		lput((0x38L<<16)|7L);		/* size of optional hdr and flags */		lput((0413<<16)|0437L);		/* magic and version */		lput(rnd(HEADR+textsize, 4096));	/* sizes */		lput(datsize);		lput(bsssize);		lput(entryvalue());		/* va of entry */		lput(INITTEXT-HEADR);		/* va of base of text */		lput(INITDAT);			/* va of base of data */		lput(INITDAT+datsize);		/* va of base of bss */		lput(~0L);			/* gp reg mask */		lput(0L);		lput(0L);		lput(0L);		lput(0L);		lput(~0L);			/* gp value ?? */		break;	case 1:		lput(0x160L<<16);		/* magic and sections */		lput(0L);			/* time and date */		lput(HEADR+textsize+datsize);		lput(symsize);			/* nsyms */		lput((0x38L<<16)|7L);		/* size of optional hdr and flags */ 		lput((0407<<16)|0437L);		/* magic and version */		lput(textsize);			/* sizes */		lput(datsize);		lput(bsssize);		lput(entryvalue());		/* va of entry */		lput(INITTEXT);			/* va of base of text */		lput(INITDAT);			/* va of base of data */		lput(INITDAT+datsize);		/* va of base of bss */		lput(~0L);			/* gp reg mask */		lput(lcsize);		lput(0L);		lput(0L);		lput(0L);		lput(~0L);			/* gp value ?? */		lput(0L);			/* complete mystery */		break;	case 2:		lput(0x407);			/* magic */		lput(textsize);			/* sizes */		lput(datsize);		lput(bsssize);		lput(symsize);			/* nsyms */		lput(entryvalue());		/* va of entry */		lput(0L);		lput(lcsize);		break;	case 3:		lput((0x160L<<16)|3L);		/* magic and sections */		lput(time(0));			/* time and date */		lput(HEADR+textsize+datsize);		lput(symsize);			/* nsyms */		lput((0x38L<<16)|7L);		/* size of optional hdr and flags */ 		lput((0407<<16)|0437L);		/* magic and version */		lput(textsize);			/* sizes */		lput(datsize);		lput(bsssize);		lput(entryvalue());		/* va of entry */		lput(INITTEXT);			/* va of base of text */		lput(INITDAT);			/* va of base of data */		lput(INITDAT+datsize);		/* va of base of bss */		lput(~0L);			/* gp reg mask */		lput(lcsize);		lput(0L);		lput(0L);		lput(0L);		lput(~0L);			/* gp value ?? */ 		strnput(".text", 8);		/* text segment */		lput(INITTEXT);			/* address */		lput(INITTEXT);		lput(textsize);		lput(HEADR);		lput(0L);		lput(HEADR+textsize+datsize+symsize);		lput(lcsize);			/* line number size */		lput(0x20L);			/* flags */ 		strnput(".data", 8);		/* data segment */		lput(INITDAT);			/* address */		lput(INITDAT);		lput(datsize);		lput(HEADR+textsize);		lput(0L);		lput(0L);		lput(0L);		lput(0x40L);			/* flags */ 		strnput(".bss", 8);		/* bss segment */		lput(INITDAT+datsize);		/* address */		lput(INITDAT+datsize);		lput(bsssize);		lput(0L);		lput(0L);		lput(0L);		lput(0L);		lput(0x80L);			/* flags */		break;	case 4: 		lput((0x160L<<16)|3L);		/* magic and sections */		lput(time(0));			/* time and date */		lput(rnd(HEADR+textsize, 4096)+datsize);		lput(symsize);			/* nsyms */		lput((0x38L<<16)|7L);		/* size of optional hdr and flags */ 		lput((0413<<16)|01012L);	/* magic and version */		lput(textsize);			/* sizes */		lput(datsize);		lput(bsssize);		lput(entryvalue());		/* va of entry */		lput(INITTEXT);			/* va of base of text */		lput(INITDAT);			/* va of base of data */		lput(INITDAT+datsize);		/* va of base of bss */		lput(~0L);			/* gp reg mask */		lput(lcsize);		lput(0L);		lput(0L);		lput(0L);		lput(~0L);			/* gp value ?? */ 		strnput(".text", 8);		/* text segment */		lput(INITTEXT);			/* address */		lput(INITTEXT);		lput(textsize);		lput(HEADR);		lput(0L);		lput(HEADR+textsize+datsize+symsize);		lput(lcsize);			/* line number size */		lput(0x20L);			/* flags */ 		strnput(".data", 8);		/* data segment */		lput(INITDAT);			/* address */		lput(INITDAT);		lput(datsize);		lput(rnd(HEADR+textsize, 4096));	/* sizes */		lput(0L);		lput(0L);		lput(0L);		lput(0x40L);			/* flags */ 		strnput(".bss", 8);		/* bss segment */		lput(INITDAT+datsize);		/* address */		lput(INITDAT+datsize);		lput(bsssize);		lput(0L);		lput(0L);		lput(0L);		lput(0L);		lput(0x80L);			/* flags */		break;	case 5:		strnput("\177ELF", 4);		/* e_ident */		CPUT(1);			/* class = 32 bit */		CPUT(2);			/* data = MSB */		CPUT(1);			/* version = CURRENT */		strnput("", 9);		lput((2L<<16)|8L);		/* type = EXEC; machine = MIPS */		lput(1L);			/* version = CURRENT */		lput(entryvalue());		/* entry vaddr */		lput(52L);			/* offset to first phdr */		lput(0L);			/* offset to first shdr */		lput(0L);			/* flags = MIPS */		lput((52L<<16)|32L);		/* Ehdr & Phdr sizes*/		lput((3L<<16)|0L);		/* # Phdrs & Shdr size */		lput((0L<<16)|0L);		/* # Shdrs & shdr string size */ 		lput(1L);			/* text - type = PT_LOAD */		lput(0L);			/* file offset */		lput(INITTEXT-HEADR);		/* vaddr */		lput(INITTEXT-HEADR);		/* paddr */		lput(HEADR+textsize);		/* file size */		lput(HEADR+textsize);		/* memory size */		lput(0x05L);			/* protections = RX */		lput(0x10000L);			/* alignment code?? */ 		lput(1L);			/* data - type = PT_LOAD */		lput(HEADR+textsize);		/* file offset */		lput(INITDAT);			/* vaddr */		lput(INITDAT);			/* paddr */		lput(datsize);			/* file size */		lput(datsize+bsssize);		/* memory size */		lput(0x06L);			/* protections = RW */		lput(0x10000L);			/* alignment code?? */ 		lput(0L);			/* data - type = PT_NULL */		lput(HEADR+textsize+datsize);	/* file offset */		lput(0L);		lput(0L);		lput(symsize);			/* symbol table size */		lput(lcsize);			/* line number size */		lput(0x04L);			/* protections = R */		lput(0x04L);			/* alignment code?? */	}	cflush();} voidstrnput(char *s, int n){	for(; *s; s++){		CPUT(*s);		n--;	}	for(; n > 0; n--)		CPUT(0);} voidlput(long l){ 	LPUT(l);} voidcflush(void){	int n; 	n = sizeof(buf.cbuf) - cbc;	if(n)		write(cout, buf.cbuf, n);	cbp = buf.cbuf;	cbc = sizeof(buf.cbuf);} voidnopstat(char *f, Count *c){	if(c->outof)	Bprint(&bso, "%s delay %ld/%ld (%.2f)\n", f,		c->outof - c->count, c->outof,		(double)(c->outof - c->count)/c->outof);} voidasmsym(void){	Prog *p;	Auto *a;	Sym *s;	int h; 	s = lookup("etext", 0);	if(s->type == STEXT)		putsymb(s->name, 'T', s->value, s->version); 	for(h=0; h<NHASH; h++)		for(s=hash[h]; s!=S; s=s->link)			switch(s->type) {			case SCONST:				putsymb(s->name, 'D', s->value, s->version);				continue; 			case SSTRING:				putsymb(s->name, 'T', s->value, s->version);				continue; 			case SDATA:				putsymb(s->name, 'D', s->value+INITDAT, s->version);				continue; 			case SBSS:				putsymb(s->name, 'B', s->value+INITDAT, s->version);				continue; 			case SFILE:				putsymb(s->name, 'f', s->value, s->version);				continue;			} 	for(p=textp; p!=P; p=p->cond) {		s = p->from.sym;		if(s->type != STEXT && s->type != SLEAF)			continue; 		/* filenames first */		for(a=p->to.autom; a; a=a->link)			if(a->type == D_FILE)				putsymb(a->asym->name, 'z', a->aoffset, 0);			else			if(a->type == D_FILE1)				putsymb(a->asym->name, 'Z', a->aoffset, 0); 		if(s->type == STEXT)			putsymb(s->name, 'T', s->value, s->version);		else			putsymb(s->name, 'L', s->value, s->version); 		/* frame, auto and param after */		putsymb(".frame", 'm', p->to.offset+4, 0);		for(a=p->to.autom; a; a=a->link)			if(a->type == D_AUTO)				putsymb(a->asym->name, 'a', -a->aoffset, 0);			else			if(a->type == D_PARAM)				putsymb(a->asym->name, 'p', a->aoffset, 0);	}	if(debug['v'] || debug['n'])		Bprint(&bso, "symsize = %lud\n", symsize);	Bflush(&bso);} voidputsymb(char *s, int t, long v, int ver){	int i, f; 	if(t == 'f')		s++;	LPUT(v);	if(ver)		t += 'a' - 'A';	CPUT(t+0x80);			/* 0x80 is variable length */ 	if(t == 'Z' || t == 'z') {		CPUT(s[0]);		for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) {			CPUT(s[i]);			CPUT(s[i+1]);		}		CPUT(0);		CPUT(0);		i++;	}	else {		for(i=0; s[i]; i++)			CPUT(s[i]);		CPUT(0);	}	symsize += 4 + 1 + i + 1;

Lines 1–500 of 1403.