|
|
Power 6/32 Unix version 1.2b
/*
* adb - routines to read a.out+core at startup
*/
#include "defs.h"
#include <sys/stat.h>
off_t datbas; /* offset of the base of the data segment */
off_t stksiz; /* stack size in the core image */
INT sigcode; /* belongs in head.h */
char *symfil = "a.out";
char *corfil = "core";
setsym()
{
off_t loc;
struct exec hdr;
register struct nlist *sp;
int ssiz;
register char *strtab;
fsym = getfile(symfil, 1);
txtmap.ufd = fsym;
if (fsym < 0)
return;
ssiz = read(fsym, (char *)&hdr, sizeof hdr);
#ifdef vax
swap4((long *)&hdr, 8);
#endif
if (ssiz != sizeof hdr || N_BADMAG(hdr)) {
txtmap.e1 = MAXFILE;
printf("%s not executable", symfil);
return;
}
filhdr = hdr;
loc = filhdr.a_text+filhdr.a_data;
txtmap.f1 = txtmap.f2 = N_TXTOFF(filhdr);
txtmap.b1 = 0;
switch (filhdr.a_magic) {
case OMAGIC:
txtmap.b1 = txtmap.e1 = 0;
txtmap.b2 = datbas = 0;
txtmap.e2 = loc;
break;
case ZMAGIC:
case NMAGIC:
txtmap.e1 = filhdr.a_text;
txtmap.b2 = datbas = round(filhdr.a_text, PAGSIZ);
txtmap.e2 = datbas + filhdr.a_data;
txtmap.f2 += txtmap.e1;
}
loc = N_SYMOFF(filhdr);
symtab = (struct nlist *) malloc(filhdr.a_syms);
esymtab = &symtab[filhdr.a_syms / sizeof (struct nlist)];
if (symtab == NULL)
goto nospac;
lseek(fsym, loc, 0);
if (filhdr.a_syms == 0)
goto nosymt;
/* SHOULD SQUISH OUT STABS HERE!!! */
if (read(fsym, symtab, filhdr.a_syms) != filhdr.a_syms)
goto readerr;
if (read(fsym, &ssiz, sizeof (ssiz)) != sizeof (ssiz))
goto oldfmt;
#ifdef vax
for (sp = symtab; sp < esymtab; sp++) {
/* should also swap n_desc, but it isn't used */
swap4((long *)&sp->n_un, 1);
swap4((long *)&sp->n_value, 1);
}
swap4((long *)&ssiz, 1);
#endif
strtab = (char *) malloc(ssiz);
if (strtab == 0)
goto nospac;
*(int *)strtab = ssiz;
ssiz -= sizeof (ssiz);
if (read(fsym, strtab + sizeof (ssiz), ssiz) != ssiz)
goto readerr;
for (sp = symtab; sp < esymtab; sp++)
if (sp->n_un.n_strx)
/* SHOULD PERFORM RANGE CHECK HERE */
sp->n_un.n_name = strtab + sp->n_un.n_strx;
nosymt:
if (INKERNEL(filhdr.a_entry)) {
txtmap.b1 += KERNOFF;
txtmap.e1 += KERNOFF+KERNSTART;
txtmap.f1 -= KERNSTART;
txtmap.b2 += KERNOFF+KERNSTART;
txtmap.e2 += KERNOFF+KERNSTART;
}
return;
readerr:
printf("Error reading symbol|string table\n");
exit(1);
nospac:
printf("Not enough space for symbol|string table\n");
exit(1);
oldfmt:
printf("Old format a.out - no string table\n");
exit(1);
}
#ifdef vax
swap4(adr, n)
register long *adr;
register n;
{
union lb {
long l;
char b[4];
} lb;
register i;
do {
for(i=4; --i>=0; )
lb.b[i] = ((union lb *)adr)->b[3-i];
*adr++ = lb.l;
} while(--n > 0);
}
#endif
setcor()
{
struct nlist *symp;
fcor = datmap.ufd = getfile(corfil,2);
if (fcor != -1 && INKERNEL(filhdr.a_entry)) {
struct stat stb;
fstat(fcor, &stb);
datmap.b1 = 0;
datmap.e1 = MAXFILE;
datmap.e2 = MAXFILE;
if (kernel == 0 && (stb.st_mode&S_IFMT) == S_IFREG)
datmap.b1 = KERNOFF;
symp = lookup("_Sysmap");
sbr = (struct pte *)symp->n_value;
symp = lookup("_Syssize");
slr = symp->n_value;
return;
}
if (read(fcor, (char *)&u, ctob(UPAGES))!=ctob(UPAGES) ||
!INUDOT(u.u_pcb.pcb_ksp) || !INSTACK(u.u_pcb.pcb_usp)) {
datmap.e1 = MAXFILE;
return;
}
signo = u.u_arg[0];
sigcode = u.u_code;
filhdr.a_text = ctob(u.u_tsize);
filhdr.a_data = ctob(u.u_dsize);
stksiz = ctob(u.u_ssize);
switch (filhdr.a_magic) {
case OMAGIC:
datmap.b1 = 0;
datmap.e1 = filhdr.a_text+filhdr.a_data;
datmap.f2 = ctob(UPAGES) + datmap.e1;
break;
case NMAGIC:
case ZMAGIC:
datmap.b1 = round(filhdr.a_text, PAGSIZ);
datmap.e1 = datmap.b1 + filhdr.a_data;
datmap.f2 = ctob(UPAGES) + filhdr.a_data;
break;
}
datbas = datmap.b1;
datmap.f1 = ctob(UPAGES);
datmap.b2 = MAXSTOR - stksiz;
datmap.e2 = MAXSTOR;
if (filhdr.a_magic && u.u_exdata.ux_mag &&
filhdr.a_magic != u.u_exdata.ux_mag)
printf("corefile not from this program");
}
create(f)
char *f;
{
register int fd;
fd = creat(f, 0644);
if (fd < 0)
return (-1);
close(fd);
return (open(f, wtflag));
}
getfile(filnam, cnt)
char *filnam;
{
register int fsym;
if (!strcmp(filnam, "-"))
return (-1);
fsym = open(filnam, wtflag);
if (fsym < 0 && xargc > cnt) {
if (wtflag)
fsym = create(filnam);
if (fsym < 0)
printf("cannot open `%s'\n", filnam);
}
return (fsym);
}
setvar()
{
var[varchk('b')] = datbas;
var[varchk('d')] = filhdr.a_data;
var[varchk('e')] = filhdr.a_entry;
var[varchk('m')] = filhdr.a_magic;
var[varchk('s')] = stksiz;
var[varchk('t')] = filhdr.a_text;
}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.