/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #define EXTERN#include "l.h"#include <ar.h> #ifndef DEFAULT#define DEFAULT '9'#endif char *noname = "<none>";char symname[] = SYMDEF;char thechar = '6';char *thestring = "amd64";char *paramspace = "FP"; char** libdir;int nlibdir = 0;static int maxlibdir = 0; /* * -H2 -T0x200028 -R0x200000 is plan9 format (was -T4136 -R4096) * -H5 -T0x80110000 -R4096 is ELF32 * -H6 -T0x2000e8 -R0x200000 is ELF64 * * options used: 189BLPQSVWabcjlnpsvz */ voidusage(void){ diag("usage: %s [-options] objects", argv0); errorexit();} static intisobjfile(char *f){ int n, v; Biobuf *b; char buf1[5], buf2[SARMAG]; b = Bopen(f, OREAD); if(b == nil) return 0; n = Bread(b, buf1, 5); if(n == 5 && (buf1[2] == 1 && buf1[3] == '<' || buf1[3] == 1 && buf1[4] == '<')) v = 1; /* good enough for our purposes */ else{ Bseek(b, 0, 0); n = Bread(b, buf2, SARMAG); v = n == SARMAG && strncmp(buf2, ARMAG, SARMAG) == 0; } Bterm(b); return v;} voidmain(int argc, char *argv[]){ int i, c; char *a; char name[LIBNAMELEN]; Binit(&bso, 1, OWRITE); cout = -1; listinit(); memset(debug, 0, sizeof(debug)); nerrors = 0; outfile = "6.out"; HEADTYPE = -1; INITTEXT = -1; INITTEXTP = -1; INITDAT = -1; INITRND = -1; INITENTRY = 0; ARGBEGIN { default: c = ARGC(); if(c >= 0 && c < sizeof(debug)) debug[c]++; break; case 'o': /* output to (next arg) */ outfile = ARGF(); break; case 'E': a = ARGF(); if(a) INITENTRY = a; break; case 'H': a = ARGF(); if(a) HEADTYPE = atolwhex(a); break; case 'L': addlibpath(EARGF(usage())); break; case 'T': a = ARGF(); if(a) INITTEXT = atolwhex(a); break; case 'P': a = ARGF(); if(a) INITTEXTP = atolwhex(a); break; case 'D': a = ARGF(); if(a) INITDAT = atolwhex(a); break; case 'R': a = ARGF(); if(a) INITRND = atolwhex(a); break; case 'x': /* produce export table */ doexp = 1; if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1])) readundefs(ARGF(), SEXPORT); break; case 'u': /* produce dynamically loadable module */ dlm = 1; debug['l']++; if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1])) readundefs(ARGF(), SIMPORT); break; } ARGEND USED(argc); if(*argv == 0) usage(); if(!debug['9'] && !debug['U'] && !debug['B']) debug[DEFAULT] = 1; a = getenv("ccroot"); if(a != nil && *a != '\0') { if(!fileexists(a)) { diag("nonexistent $ccroot: %s", a); errorexit(); } }else a = ""; snprint(name, sizeof(name), "%s/%s/lib", a, thestring); addlibpath(name); if(HEADTYPE == -1) { if(debug['B']) HEADTYPE = 2; if(debug['9']) HEADTYPE = 2; } switch(HEADTYPE) { default: diag("unknown -H option"); errorexit(); case 2: /* plan 9 */ HEADR = 32L+8L; if(INITTEXT == -1) INITTEXT = 0x200000+HEADR; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 0x200000; break; case 5: /* elf32 executable */ HEADR = rnd(Ehdr32sz+3*Phdr32sz, 16); if(INITTEXT == -1) INITTEXT = 0xf0110000L; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4096; break; case 6: /* ELF64 executable */ HEADR = rnd(Ehdr64sz+3*Phdr64sz, 16); if(INITTEXT == -1) INITTEXT = 0x200000+HEADR; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 0x200000; break; } if (INITTEXTP == -1) INITTEXTP = INITTEXT; if(INITDAT != 0 && INITRND != 0) print("warning: -D0x%llux is ignored because of -R0x%lux\n", INITDAT, INITRND); if(debug['v']) Bprint(&bso, "HEADER = -H%ld -T0x%llux -D0x%llux -R0x%lux\n", HEADTYPE, INITTEXT, INITDAT, INITRND); Bflush(&bso); for(i=1; optab[i].as; i++) { c = optab[i].as; if(opindex[c] != nil) { diag("phase error in optab: %d (%A)", i, c); errorexit(); } opindex[c] = &optab[i]; } for(i=0; i<Ymax; i++) ycover[i*Ymax + i] = 1; ycover[Yi0*Ymax + Yi8] = 1; ycover[Yi1*Ymax + Yi8] = 1; ycover[Yi0*Ymax + Ys32] = 1; ycover[Yi1*Ymax + Ys32] = 1; ycover[Yi8*Ymax + Ys32] = 1; ycover[Yi0*Ymax + Yi32] = 1; ycover[Yi1*Ymax + Yi32] = 1; ycover[Yi8*Ymax + Yi32] = 1; ycover[Ys32*Ymax + Yi32] = 1; ycover[Yi0*Ymax + Yi64] = 1; ycover[Yi1*Ymax + Yi64] = 1; ycover[Yi8*Ymax + Yi64] = 1; ycover[Ys32*Ymax + Yi64] = 1; ycover[Yi32*Ymax + Yi64] = 1; ycover[Yal*Ymax + Yrb] = 1; ycover[Ycl*Ymax + Yrb] = 1; ycover[Yax*Ymax + Yrb] = 1; ycover[Ycx*Ymax + Yrb] = 1; ycover[Yrx*Ymax + Yrb] = 1; ycover[Yrl*Ymax + Yrb] = 1; ycover[Ycl*Ymax + Ycx] = 1; ycover[Yax*Ymax + Yrx] = 1; ycover[Ycx*Ymax + Yrx] = 1; ycover[Yax*Ymax + Yrl] = 1; ycover[Ycx*Ymax + Yrl] = 1; ycover[Yrx*Ymax + Yrl] = 1; ycover[Yf0*Ymax + Yrf] = 1; ycover[Yal*Ymax + Ymb] = 1; ycover[Ycl*Ymax + Ymb] = 1; ycover[Yax*Ymax + Ymb] = 1; ycover[Ycx*Ymax + Ymb] = 1; ycover[Yrx*Ymax + Ymb] = 1; ycover[Yrb*Ymax + Ymb] = 1; ycover[Yrl*Ymax + Ymb] = 1; ycover[Ym*Ymax + Ymb] = 1; ycover[Yax*Ymax + Yml] = 1; ycover[Ycx*Ymax + Yml] = 1; ycover[Yrx*Ymax + Yml] = 1; ycover[Yrl*Ymax + Yml] = 1; ycover[Ym*Ymax + Yml] = 1; ycover[Yax*Ymax + Ymm] = 1; ycover[Ycx*Ymax + Ymm] = 1; ycover[Yrx*Ymax + Ymm] = 1; ycover[Yrl*Ymax + Ymm] = 1; ycover[Ym*Ymax + Ymm] = 1; ycover[Ymr*Ymax + Ymm] = 1; ycover[Yax*Ymax + Yxm] = 1; ycover[Ycx*Ymax + Yxm] = 1; ycover[Yrx*Ymax + Yxm] = 1; ycover[Yrl*Ymax + Yxm] = 1; ycover[Ym*Ymax + Yxm] = 1; ycover[Yxr*Ymax + Yxm] = 1; for(i=0; i<D_NONE; i++) { reg[i] = -1; if(i >= D_AL && i <= D_R15B) { reg[i] = (i-D_AL) & 7; if(i >= D_SPB && i <= D_DIB) regrex[i] = 0x40; if(i >= D_R8B && i <= D_R15B) regrex[i] = Rxr | Rxx | Rxb; } if(i >= D_AH && i<= D_BH) reg[i] = 4 + ((i-D_AH) & 7); if(i >= D_AX && i <= D_R15) { reg[i] = (i-D_AX) & 7; if(i >= D_R8) regrex[i] = Rxr | Rxx | Rxb; } if(i >= D_F0 && i <= D_F0+7) reg[i] = (i-D_F0) & 7; if(i >= D_M0 && i <= D_M0+7) reg[i] = (i-D_M0) & 7; if(i >= D_X0 && i <= D_X0+15) { reg[i] = (i-D_X0) & 7; if(i >= D_X0+8) regrex[i] = Rxr | Rxx | Rxb; } if(i >= D_CR+8 && i <= D_CR+15) regrex[i] = Rxr; } zprg.link = P; zprg.pcond = P; zprg.back = 2; zprg.as = AGOK; zprg.from.type = D_NONE; zprg.from.index = D_NONE; zprg.from.scale = 1; zprg.to = zprg.from; zprg.mode = 64; pcstr = "%.6llux "; nuxiinit(); histgen = 0; textp = P; datap = P; edatap = P; pc = 0; dtype = 4; cout = create(outfile, 1, 0775); if(cout < 0) { diag("cannot create %s: %r", outfile); errorexit(); } version = 0; cbp = buf.cbuf; cbc = sizeof(buf.cbuf); firstp = prg(); lastp = firstp; if(INITENTRY == 0) { INITENTRY = "_main"; if(debug['p']) INITENTRY = "_mainp"; if(!debug['l']) lookup(INITENTRY, 0)->type = SXREF; } else if(!(*INITENTRY >= '0' && *INITENTRY <= '9')) lookup(INITENTRY, 0)->type = SXREF; while(*argv) objfile(*argv++); if(!debug['l']) loadlib(); firstp = firstp->link; if(firstp == P) errorexit(); if(doexp || dlm){ EXPTAB = "_exporttab"; zerosig(EXPTAB); zerosig("etext"); zerosig("edata"); zerosig("end"); if(dlm){ import(); HEADTYPE = 2; INITTEXT = 0; INITDAT = 0; INITRND = 8; INITENTRY = EXPTAB; } export(); } patch(); follow(); dodata(); dostkoff(); paramspace = "SP"; /* (FP) now (SP) on output */ if(debug['p']) if(debug['1']) doprof1(); else doprof2(); span(); doinit(); asmb(); undef(); if(debug['v']) { Bprint(&bso, "%5.2f cpu time\n", cputime()); Bprint(&bso, "%ld symbols\n", nsymbol); Bprint(&bso, "%ld memory used\n", thunk); Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); } Bflush(&bso); errorexit();} voidaddlibpath(char *arg){ char **p; if(nlibdir >= maxlibdir) { if(maxlibdir == 0) maxlibdir = 8; else maxlibdir *= 2; p = malloc(maxlibdir*sizeof(*p)); if(p == nil) { diag("out of memory"); errorexit(); } memmove(p, libdir, nlibdir*sizeof(*p)); free(libdir); libdir = p; } libdir[nlibdir++] = strdup(arg);} char*findlib(char *file){ int i; char name[LIBNAMELEN]; for(i = 0; i < nlibdir; i++) { snprint(name, sizeof(name), "%s/%s", libdir[i], file); if(fileexists(name)) return libdir[i]; } return nil;} voidloadlib(void){ int i; long h; Sym *s; loop: xrefresolv = 0; for(i=0; i<libraryp; i++) { if(debug['v']) Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]); objfile(library[i]); } if(xrefresolv) for(h=0; h<nelem(hash); h++) for(s = hash[h]; s != S; s = s->link) if(s->type == SXREF) goto loop;} voiderrorexit(void){ if(nerrors) { if(cout >= 0) remove(outfile); exits("error"); } exits(0);} voidobjfile(char *file){ long off, esym, cnt, l; int f, work; Sym *s; char magbuf[SARMAG]; char name[LIBNAMELEN], pname[LIBNAMELEN]; struct ar_hdr arhdr; char *e, *start, *stop; if(debug['v']) Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file); Bflush(&bso); if(file[0] == '-' && file[1] == 'l') { snprint(pname, sizeof(pname), "lib%s.a", file+2); e = findlib(pname); if(e == nil) { diag("cannot find library: %s", file); errorexit(); } snprint(name, sizeof(name), "%s/%s", e, pname); file = name; } f = open(file, 0); if(f < 0) { diag("cannot open %s: %r", file); errorexit(); } l = read(f, magbuf, SARMAG); if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ /* load it as a regular file */ l = seek(f, 0L, 2); seek(f, 0L, 0); ldobj(f, l, file); close(f); return; } Lines 1–500 of 1683. Load more
/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #define EXTERN #include "l.h" #include <ar.h> #ifndef DEFAULT #define DEFAULT '9' #endif char *noname = "<none>"; char symname[] = SYMDEF; char thechar = '6'; char *thestring = "amd64"; char *paramspace = "FP"; char** libdir; int nlibdir = 0; static int maxlibdir = 0; /* * -H2 -T0x200028 -R0x200000 is plan9 format (was -T4136 -R4096) * -H5 -T0x80110000 -R4096 is ELF32 * -H6 -T0x2000e8 -R0x200000 is ELF64 * * options used: 189BLPQSVWabcjlnpsvz */ void usage(void) { diag("usage: %s [-options] objects", argv0); errorexit(); } static int isobjfile(char *f) { int n, v; Biobuf *b; char buf1[5], buf2[SARMAG]; b = Bopen(f, OREAD); if(b == nil) return 0; n = Bread(b, buf1, 5); if(n == 5 && (buf1[2] == 1 && buf1[3] == '<' || buf1[3] == 1 && buf1[4] == '<')) v = 1; /* good enough for our purposes */ else{ Bseek(b, 0, 0); n = Bread(b, buf2, SARMAG); v = n == SARMAG && strncmp(buf2, ARMAG, SARMAG) == 0; } Bterm(b); return v; } void main(int argc, char *argv[]) { int i, c; char *a; char name[LIBNAMELEN]; Binit(&bso, 1, OWRITE); cout = -1; listinit(); memset(debug, 0, sizeof(debug)); nerrors = 0; outfile = "6.out"; HEADTYPE = -1; INITTEXT = -1; INITTEXTP = -1; INITDAT = -1; INITRND = -1; INITENTRY = 0; ARGBEGIN { default: c = ARGC(); if(c >= 0 && c < sizeof(debug)) debug[c]++; break; case 'o': /* output to (next arg) */ outfile = ARGF(); break; case 'E': a = ARGF(); if(a) INITENTRY = a; break; case 'H': a = ARGF(); if(a) HEADTYPE = atolwhex(a); break; case 'L': addlibpath(EARGF(usage())); break; case 'T': a = ARGF(); if(a) INITTEXT = atolwhex(a); break; case 'P': a = ARGF(); if(a) INITTEXTP = atolwhex(a); break; case 'D': a = ARGF(); if(a) INITDAT = atolwhex(a); break; case 'R': a = ARGF(); if(a) INITRND = atolwhex(a); break; case 'x': /* produce export table */ doexp = 1; if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1])) readundefs(ARGF(), SEXPORT); break; case 'u': /* produce dynamically loadable module */ dlm = 1; debug['l']++; if(argv[1] != nil && argv[1][0] != '-' && !isobjfile(argv[1])) readundefs(ARGF(), SIMPORT); break; } ARGEND USED(argc); if(*argv == 0) usage(); if(!debug['9'] && !debug['U'] && !debug['B']) debug[DEFAULT] = 1; a = getenv("ccroot"); if(a != nil && *a != '\0') { if(!fileexists(a)) { diag("nonexistent $ccroot: %s", a); errorexit(); } }else a = ""; snprint(name, sizeof(name), "%s/%s/lib", a, thestring); addlibpath(name); if(HEADTYPE == -1) { if(debug['B']) HEADTYPE = 2; if(debug['9']) HEADTYPE = 2; } switch(HEADTYPE) { default: diag("unknown -H option"); errorexit(); case 2: /* plan 9 */ HEADR = 32L+8L; if(INITTEXT == -1) INITTEXT = 0x200000+HEADR; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 0x200000; break; case 5: /* elf32 executable */ HEADR = rnd(Ehdr32sz+3*Phdr32sz, 16); if(INITTEXT == -1) INITTEXT = 0xf0110000L; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4096; break; case 6: /* ELF64 executable */ HEADR = rnd(Ehdr64sz+3*Phdr64sz, 16); if(INITTEXT == -1) INITTEXT = 0x200000+HEADR; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 0x200000; break; } if (INITTEXTP == -1) INITTEXTP = INITTEXT; if(INITDAT != 0 && INITRND != 0) print("warning: -D0x%llux is ignored because of -R0x%lux\n", INITDAT, INITRND); if(debug['v']) Bprint(&bso, "HEADER = -H%ld -T0x%llux -D0x%llux -R0x%lux\n", HEADTYPE, INITTEXT, INITDAT, INITRND); Bflush(&bso); for(i=1; optab[i].as; i++) { c = optab[i].as; if(opindex[c] != nil) { diag("phase error in optab: %d (%A)", i, c); errorexit(); } opindex[c] = &optab[i]; } for(i=0; i<Ymax; i++) ycover[i*Ymax + i] = 1; ycover[Yi0*Ymax + Yi8] = 1; ycover[Yi1*Ymax + Yi8] = 1; ycover[Yi0*Ymax + Ys32] = 1; ycover[Yi1*Ymax + Ys32] = 1; ycover[Yi8*Ymax + Ys32] = 1; ycover[Yi0*Ymax + Yi32] = 1; ycover[Yi1*Ymax + Yi32] = 1; ycover[Yi8*Ymax + Yi32] = 1; ycover[Ys32*Ymax + Yi32] = 1; ycover[Yi0*Ymax + Yi64] = 1; ycover[Yi1*Ymax + Yi64] = 1; ycover[Yi8*Ymax + Yi64] = 1; ycover[Ys32*Ymax + Yi64] = 1; ycover[Yi32*Ymax + Yi64] = 1; ycover[Yal*Ymax + Yrb] = 1; ycover[Ycl*Ymax + Yrb] = 1; ycover[Yax*Ymax + Yrb] = 1; ycover[Ycx*Ymax + Yrb] = 1; ycover[Yrx*Ymax + Yrb] = 1; ycover[Yrl*Ymax + Yrb] = 1; ycover[Ycl*Ymax + Ycx] = 1; ycover[Yax*Ymax + Yrx] = 1; ycover[Ycx*Ymax + Yrx] = 1; ycover[Yax*Ymax + Yrl] = 1; ycover[Ycx*Ymax + Yrl] = 1; ycover[Yrx*Ymax + Yrl] = 1; ycover[Yf0*Ymax + Yrf] = 1; ycover[Yal*Ymax + Ymb] = 1; ycover[Ycl*Ymax + Ymb] = 1; ycover[Yax*Ymax + Ymb] = 1; ycover[Ycx*Ymax + Ymb] = 1; ycover[Yrx*Ymax + Ymb] = 1; ycover[Yrb*Ymax + Ymb] = 1; ycover[Yrl*Ymax + Ymb] = 1; ycover[Ym*Ymax + Ymb] = 1; ycover[Yax*Ymax + Yml] = 1; ycover[Ycx*Ymax + Yml] = 1; ycover[Yrx*Ymax + Yml] = 1; ycover[Yrl*Ymax + Yml] = 1; ycover[Ym*Ymax + Yml] = 1; ycover[Yax*Ymax + Ymm] = 1; ycover[Ycx*Ymax + Ymm] = 1; ycover[Yrx*Ymax + Ymm] = 1; ycover[Yrl*Ymax + Ymm] = 1; ycover[Ym*Ymax + Ymm] = 1; ycover[Ymr*Ymax + Ymm] = 1; ycover[Yax*Ymax + Yxm] = 1; ycover[Ycx*Ymax + Yxm] = 1; ycover[Yrx*Ymax + Yxm] = 1; ycover[Yrl*Ymax + Yxm] = 1; ycover[Ym*Ymax + Yxm] = 1; ycover[Yxr*Ymax + Yxm] = 1; for(i=0; i<D_NONE; i++) { reg[i] = -1; if(i >= D_AL && i <= D_R15B) { reg[i] = (i-D_AL) & 7; if(i >= D_SPB && i <= D_DIB) regrex[i] = 0x40; if(i >= D_R8B && i <= D_R15B) regrex[i] = Rxr | Rxx | Rxb; } if(i >= D_AH && i<= D_BH) reg[i] = 4 + ((i-D_AH) & 7); if(i >= D_AX && i <= D_R15) { reg[i] = (i-D_AX) & 7; if(i >= D_R8) regrex[i] = Rxr | Rxx | Rxb; } if(i >= D_F0 && i <= D_F0+7) reg[i] = (i-D_F0) & 7; if(i >= D_M0 && i <= D_M0+7) reg[i] = (i-D_M0) & 7; if(i >= D_X0 && i <= D_X0+15) { reg[i] = (i-D_X0) & 7; if(i >= D_X0+8) regrex[i] = Rxr | Rxx | Rxb; } if(i >= D_CR+8 && i <= D_CR+15) regrex[i] = Rxr; } zprg.link = P; zprg.pcond = P; zprg.back = 2; zprg.as = AGOK; zprg.from.type = D_NONE; zprg.from.index = D_NONE; zprg.from.scale = 1; zprg.to = zprg.from; zprg.mode = 64; pcstr = "%.6llux "; nuxiinit(); histgen = 0; textp = P; datap = P; edatap = P; pc = 0; dtype = 4; cout = create(outfile, 1, 0775); if(cout < 0) { diag("cannot create %s: %r", outfile); errorexit(); } version = 0; cbp = buf.cbuf; cbc = sizeof(buf.cbuf); firstp = prg(); lastp = firstp; if(INITENTRY == 0) { INITENTRY = "_main"; if(debug['p']) INITENTRY = "_mainp"; if(!debug['l']) lookup(INITENTRY, 0)->type = SXREF; } else if(!(*INITENTRY >= '0' && *INITENTRY <= '9')) lookup(INITENTRY, 0)->type = SXREF; while(*argv) objfile(*argv++); if(!debug['l']) loadlib(); firstp = firstp->link; if(firstp == P) errorexit(); if(doexp || dlm){ EXPTAB = "_exporttab"; zerosig(EXPTAB); zerosig("etext"); zerosig("edata"); zerosig("end"); if(dlm){ import(); HEADTYPE = 2; INITTEXT = 0; INITDAT = 0; INITRND = 8; INITENTRY = EXPTAB; } export(); } patch(); follow(); dodata(); dostkoff(); paramspace = "SP"; /* (FP) now (SP) on output */ if(debug['p']) if(debug['1']) doprof1(); else doprof2(); span(); doinit(); asmb(); undef(); if(debug['v']) { Bprint(&bso, "%5.2f cpu time\n", cputime()); Bprint(&bso, "%ld symbols\n", nsymbol); Bprint(&bso, "%ld memory used\n", thunk); Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); } Bflush(&bso); errorexit(); } void addlibpath(char *arg) { char **p; if(nlibdir >= maxlibdir) { if(maxlibdir == 0) maxlibdir = 8; else maxlibdir *= 2; p = malloc(maxlibdir*sizeof(*p)); if(p == nil) { diag("out of memory"); errorexit(); } memmove(p, libdir, nlibdir*sizeof(*p)); free(libdir); libdir = p; } libdir[nlibdir++] = strdup(arg); } char* findlib(char *file) { int i; char name[LIBNAMELEN]; for(i = 0; i < nlibdir; i++) { snprint(name, sizeof(name), "%s/%s", libdir[i], file); if(fileexists(name)) return libdir[i]; } return nil; } void loadlib(void) { int i; long h; Sym *s; loop: xrefresolv = 0; for(i=0; i<libraryp; i++) { if(debug['v']) Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]); objfile(library[i]); } if(xrefresolv) for(h=0; h<nelem(hash); h++) for(s = hash[h]; s != S; s = s->link) if(s->type == SXREF) goto loop; } void errorexit(void) { if(nerrors) { if(cout >= 0) remove(outfile); exits("error"); } exits(0); } void objfile(char *file) { long off, esym, cnt, l; int f, work; Sym *s; char magbuf[SARMAG]; char name[LIBNAMELEN], pname[LIBNAMELEN]; struct ar_hdr arhdr; char *e, *start, *stop; if(debug['v']) Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file); Bflush(&bso); if(file[0] == '-' && file[1] == 'l') { snprint(pname, sizeof(pname), "lib%s.a", file+2); e = findlib(pname); if(e == nil) { diag("cannot find library: %s", file); errorexit(); } snprint(name, sizeof(name), "%s/%s", e, pname); file = name; } f = open(file, 0); if(f < 0) { diag("cannot open %s: %r", file); errorexit(); } l = read(f, magbuf, SARMAG); if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ /* load it as a regular file */ l = seek(f, 0L, 2); seek(f, 0L, 0); ldobj(f, l, file); close(f); return; }