version 1.1.1.1, 1999/12/03 07:39:12 |
version 1.2, 2000/01/19 05:05:04 |
|
|
/* $OpenXM: OpenXM/src/asir99/parse/load.c,v 1.1.1.1 1999/11/10 08:12:34 noro Exp $ */ |
/* $OpenXM: OpenXM_contrib2/asir2000/parse/load.c,v 1.1.1.1 1999/12/03 07:39:12 noro Exp $ */ |
#include "ca.h" |
#include "ca.h" |
#include "parse.h" |
#include "parse.h" |
#if defined(THINK_C) |
#if defined(THINK_C) |
|
|
|
|
static NODE objfile = 0; |
static NODE objfile = 0; |
|
|
#if defined(apollo) || defined(VISUAL) || defined(_PA_RISC1_1) || defined(__alpha) || defined(linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(SYSV) || defined(__SVR4) |
|
|
|
int loadfile(s) |
int loadfile(s) |
char *s; |
char *s; |
{ |
{ |
|
|
} |
} |
|
|
int loadfiles(node) NODE node; { return 0; } |
int loadfiles(node) NODE node; { return 0; } |
|
|
#else |
|
#if defined(THINK_C) |
|
|
|
int loadfile(s) |
|
char *s; |
|
{ |
|
FILE *in; |
|
|
|
if ( in = fopen(s,"r") ) { |
|
loadasirfile(s); fclose(in); |
|
return 1; |
|
} else |
|
return 0; |
|
} |
|
|
|
int loadfiles(node) NODE node; { return 0; } |
|
|
|
int finder_loadfile() { |
|
Point p; |
|
SFTypeList t; |
|
SFReply r; |
|
int ret; |
|
|
|
p.h = p.v = 50; t[0] = 'TEXT'; |
|
SFGetFile(p,"\p",0,1,t,0,&r); |
|
if ( r.good ) { |
|
setDir(r.vRefNum); |
|
ret = loadfile(PtoCstr(r.fName)); |
|
resetDir(); |
|
return ret; |
|
} else |
|
return 0; |
|
} |
|
|
|
#else |
|
#if defined(NeXT) |
|
#include <rld.h> |
|
#include <ldsyms.h> |
|
|
|
void loadmachofile(char **); |
|
void unloadmachofile(void); |
|
|
|
int loadfile(s) |
|
char *s; |
|
{ |
|
FILE *in; |
|
unsigned magic; |
|
char **oname; |
|
|
|
if ( in = fopen(s,"r") ) { |
|
fread(&magic,sizeof(unsigned),1,in); fclose(in); |
|
if (magic == MH_MAGIC) { |
|
oname = (char **)ALLOCA(BUFSIZ); |
|
oname[0] = s; oname[1] = "/usr/lib/libm.a",oname[2] = 0; |
|
loadmachofile(oname); |
|
} else |
|
loadasirfile(s); |
|
return 1; |
|
} else |
|
return 0; |
|
} |
|
|
|
int loadfiles(node) |
|
NODE node; |
|
{ |
|
int i; |
|
NODE n; |
|
char *name; |
|
char **oname; |
|
|
|
for ( i = 0, n = node; n; i++, n = NEXT(n) ); |
|
if ( OID(BDY(n)) != O_STR ) |
|
return 0; |
|
oname = (char **)ALLOCA(i+1); |
|
for ( i = 0, n = node; n; i++, n = NEXT(n) ) { |
|
searchasirpath(BDY((STRING)BDY(node)),&name); |
|
if ( !name ) |
|
return 0; |
|
else |
|
oname[i] = name; |
|
} |
|
oname[i] = 0; |
|
loadmachofile(oname); |
|
return 1; |
|
} |
|
|
|
static NXStream *nxs = 0; |
|
|
|
void loadmachofile(oname) |
|
char **oname; |
|
{ |
|
struct mach_header *h; |
|
unsigned int data_start,data_end; |
|
struct section *tsec,*dsec,*bsec,*csec; |
|
struct section *getsectbynamefromheader(); |
|
|
|
if ( !nxs ) |
|
nxs = NXOpenFile(fileno(stderr),NX_WRITEONLY); |
|
if ( rld_load(nxs,&h,oname,"/tmp/afo") ) { |
|
tsec = getsectbynamefromheader(h,"__TEXT","__text"); |
|
dsec = getsectbynamefromheader(h,"__DATA","__data"); |
|
bsec = getsectbynamefromheader(h,"__DATA","__bss"); |
|
csec = getsectbynamefromheader(h,"__DATA","__common"); |
|
data_start = dsec->addr; data_end = dsec->addr+dsec->size; |
|
if ( bsec->size ) { |
|
data_start = MIN(data_start,bsec->addr); |
|
data_end = MAX(data_end,(bsec->addr+bsec->size)); |
|
} |
|
if ( csec->size ) { |
|
data_start = MIN(data_start,csec->addr); |
|
data_end = MAX(data_end,(csec->addr+csec->size)); |
|
} |
|
#if 0 |
|
if ( data_start != data_end ) |
|
add_data(data_start,data_end); |
|
#endif |
|
(*(int(*)())(tsec->addr))(); |
|
} else if ( nxs ) |
|
NXFlush(nxs); |
|
} |
|
|
|
void unloadmachofile() |
|
{ |
|
if ( !rld_unload(nxs) && nxs ) |
|
NXFlush(nxs); |
|
} |
|
|
|
#else |
|
#ifdef mips |
|
#include <sys/exec.h> |
|
#else |
|
#include <a.out.h> |
|
#endif |
|
|
|
void loadaoutfile(/* char ** */); |
|
|
|
int loadfiles(node) NODE node; { return 0; } |
|
|
|
int loadfile(s) |
|
char *s; |
|
{ |
|
FILE *in; |
|
struct exec h; |
|
char **oname; |
|
|
|
if ( in = fopen(s,"r") ) { |
|
fread(&h,sizeof(h),1,in); fclose(in); |
|
#if defined(__NetBSD__) |
|
if ((N_GETMAGIC(h) == OMAGIC) || (N_GETMAGIC(h) == ZMAGIC)) { |
|
#else |
|
if ((h.a_magic == OMAGIC) || (h.a_magic == ZMAGIC)) { |
|
#endif |
|
oname = (char **)ALLOCA(BUFSIZ); |
|
oname[0] = s; oname[1] = 0; |
|
loadaoutfile(oname); |
|
} else |
|
loadasirfile(s); |
|
return 1; |
|
} else |
|
return 0; |
|
} |
|
|
|
#if 0 |
|
void loadaoutfile(name) |
|
char **name; |
|
{ |
|
#include <sys/wait.h> |
|
FILE *in; |
|
char *tmpf,*buf; |
|
char **n; |
|
NODE tn; |
|
struct exec h; |
|
char com[BUFSIZ]; |
|
char *ldargv[256]; |
|
char nbuf[BUFSIZ]; |
|
int i,w; |
|
union wait status; |
|
static char ldcom1[] = "ld -d -N -x -A %s -T %x "; |
|
static char ldcom2[] = " -lm -lc -o "; |
|
extern char asirname[]; |
|
|
|
tmpf = tempnam(0,"asir"); |
|
ldargv[0] = "ld"; ldargv[1] = "-d"; ldargv[2] = "-N"; |
|
ldargv[3] = "-x"; ldargv[4] = "-A"; ldargv[5] = asirname; |
|
ldargv[6] = "-T"; ldargv[7] = "0"; |
|
for ( i = 8, n = name; *n; n++, i++ ) |
|
ldargv[i] = *n; |
|
ldargv[i++] = "-lm"; ldargv[i++] = "-lc"; ldargv[i++] = "-o"; |
|
ldargv[i++] = tmpf; ldargv[i] = 0; |
|
if ( !vfork() ) { |
|
if ( execv("/bin/ld",ldargv) < 0 ) |
|
_exit(0); |
|
} else |
|
wait(&status); |
|
if ( status.w_status ) { |
|
error("loadaoutfile : faild"); |
|
} |
|
in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in); |
|
fclose(in); unlink(tmpf); |
|
buf = (char *)CALLOC(h.a_text+h.a_data+h.a_bss,1); |
|
sprintf(nbuf,"%x",buf); ldargv[7] = nbuf; |
|
if ( !vfork() ) { |
|
if ( execv("/bin/ld",ldargv) < 0 ); |
|
_exit(0); |
|
} else |
|
wait(&status); |
|
in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in); |
|
#ifdef mips |
|
fseek(in,N_TXTOFF(h.ex_f,h.ex_o),0); |
|
#endif |
|
fread(buf,h.a_text+h.a_data,1,in); |
|
MKNODE(tn,buf,objfile); objfile = tn; |
|
(*(int(*)())buf)(); |
|
fclose(in); unlink(tmpf); |
|
} |
|
#endif |
|
|
|
#if 1 |
|
void loadaoutfile(name) |
|
char **name; |
|
{ |
|
FILE *in; |
|
char *tmpf,*buf,*p; |
|
char **n; |
|
NODE tn; |
|
struct exec h; |
|
char com[BUFSIZ]; |
|
int status,i,len; |
|
#ifdef mips |
|
static char ldcom1[] = "ld -G 0 -d -N -x -A %s -T %x "; |
|
static char ldcom2[] = " -lm_G0 -lc -o "; |
|
#else |
|
static char ldcom1[] = "ld -d -N -x -A %s -T %x "; |
|
static char ldcom2[] = " -lm -lc -o "; |
|
#endif |
|
extern char asirname[]; |
|
|
|
tmpf = tempnam(0,"asir"); |
|
sprintf(com,ldcom1,asirname,0); |
|
for ( n = name; *n; n++ ) |
|
strcat(com,*n); |
|
strcat(com,ldcom2); strcat(com,tmpf); |
|
status = system(com); |
|
if ( status ) { |
|
fprintf(stderr,"system() : status = %d\n",status); |
|
error("loadaoutfile : could not exec system()"); |
|
} |
|
in = fopen(tmpf,"r"); |
|
fread(&h,sizeof(h),1,in); |
|
fclose(in); unlink(tmpf); |
|
buf = (char *)CALLOC(h.a_text+h.a_data+h.a_bss,1); |
|
sprintf(com,ldcom1,asirname,buf); |
|
for ( n = name; *n; n++ ) |
|
strcat(com,*n); |
|
strcat(com,ldcom2); strcat(com,tmpf); |
|
system(com); |
|
in = fopen(tmpf,"r"); fread(&h,sizeof(h),1,in); |
|
#ifdef mips |
|
fseek(in,N_TXTOFF(h.ex_f,h.ex_o),0); |
|
#endif |
|
len = h.a_text+h.a_data; fread(buf,1,len,in); |
|
/* to avoid a bug on RISC-NEWS */ |
|
fprintf(stderr,"text=%d data=%d bss=%d\n",h.a_text,h.a_data,h.a_bss); |
|
MKNODE(tn,buf,objfile); objfile = tn; |
|
/* fprintf(stderr,"calling reg_sysf()...\n"); */ |
|
(*(int(*)())buf)(); |
|
fclose(in); unlink(tmpf); |
|
} |
|
#endif |
|
#endif |
|
#endif |
|
#endif |
|
|
|
static unsigned char encrypt_tab[128][2] = { |
static unsigned char encrypt_tab[128][2] = { |
{137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231}, |
{137,40},{1,194},{133,79},{48,20},{254,76},{98,17},{110,233},{19,231}, |