1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666 |
- #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)
- * -H3 -T4128 -R4096 is plan9 32-bit format
- * -H5 -T0x80110000 -R4096 is ELF32
- *
- * options used: 189BLQSWabcjlnpsvz
- */
- void
- usage(void)
- {
- diag("usage: 6l [-options] objects");
- 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;
- Binit(&bso, 1, OWRITE);
- cout = -1;
- listinit();
- memset(debug, 0, sizeof(debug));
- nerrors = 0;
- outfile = "6.out";
- Lflag(".");
- HEADTYPE = -1;
- INITTEXT = -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':
- Lflag(EARGF(usage()));
- break;
- case 'T':
- a = ARGF();
- if(a)
- INITTEXT = 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;
- if(HEADTYPE == -1) {
- if(debug['B'])
- HEADTYPE = 2;
- if(debug['9'])
- HEADTYPE = 2;
- }
- Lflag(smprint("%s/amd64/lib", (a = getenv("NXM")) == 0 ? "" : a));
- 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 3: /* plan 9 */
- HEADR = 32L;
- if(INITTEXT == -1)
- INITTEXT = 4096+HEADR;
- if(INITDAT == -1)
- INITDAT = 0;
- if(INITRND == -1)
- INITRND = 4096;
- break;
- case 5: /* elf32 executable */
- HEADR = rnd(52L+3*32L, 16);
- if(INITTEXT == -1)
- INITTEXT = 0xf0110000L;
- if(INITDAT == -1)
- INITDAT = 0;
- if(INITRND == -1)
- INITRND = 4096;
- break;
- }
- 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", 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
- Lflag(char *arg)
- {
- char **p;
- if(nlibdir >= maxlibdir) {
- if (maxlibdir == 0)
- maxlibdir = 8;
- else
- maxlibdir *= 2;
- p = realloc(libdir, maxlibdir * sizeof(*p));
- if (p == nil) {
- print("too many -L's: %d\n", nlibdir);
- usage();
- }
- libdir = p;
- }
- libdir[nlibdir++] = strdup(arg);
- }
- 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;
- int i;
- Sym *s;
- char magbuf[SARMAG];
- char libfile[100];
- char name[100], pname[150];
- struct ar_hdr arhdr;
- char *e, *start, *stop;
- if(file[0] == '-' && file[1] == 'l') {
- sprint(libfile, "lib");
- strcat(libfile, file+2);
- strcat(libfile, ".a");
- // if(debug['9'])
- // sprint(name, "/%s/lib/lib", thestring);
- // else
- // sprint(name, "/usr/%clib/lib", thechar);
- // strcat(name, file+2);
- // strcat(name, ".a");
- // file = name;
- for (i = 0; i < nlibdir; i++) {
- sprint(name, "%s/", libdir[i]);
- strcat(name, libfile);
- if (!access(name, AREAD)) {
- print("found at %s\n", name);
- file = name;
- break;
- }
- }
- }
- if(debug['v'])
- Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file);
- Bflush(&bso);
- f = open(file, 0);
- if(f < 0) {
- diag("cannot open file: %s", 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;
- }
- l = read(f, &arhdr, SAR_HDR);
- if(l != SAR_HDR) {
- diag("%s: short read on archive file symbol header", file);
- goto out;
- }
- if(strncmp(arhdr.name, symname, strlen(symname))) {
- diag("%s: first entry not symbol header", file);
- goto out;
- }
- esym = SARMAG + SAR_HDR + atolwhex(arhdr.size);
- off = SARMAG + SAR_HDR;
- /*
- * just bang the whole symbol file into memory
- */
- seek(f, off, 0);
- cnt = esym - off;
- start = malloc(cnt + 10);
- cnt = read(f, start, cnt);
- if(cnt <= 0){
- close(f);
- return;
- }
- stop = &start[cnt];
- memset(stop, 0, 10);
- work = 1;
- while(work) {
- if(debug['v'])
- Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file);
- Bflush(&bso);
- work = 0;
- for(e = start; e < stop; e = strchr(e+5, 0) + 1) {
- s = lookup(e+5, 0);
- if(s->type != SXREF)
- continue;
- sprint(pname, "%s(%s)", file, s->name);
- if(debug['v'])
- Bprint(&bso, "%5.2f library: %s\n", cputime(), pname);
- Bflush(&bso);
- l = e[1] & 0xff;
- l |= (e[2] & 0xff) << 8;
- l |= (e[3] & 0xff) << 16;
- l |= (e[4] & 0xff) << 24;
- seek(f, l, 0);
- l = read(f, &arhdr, SAR_HDR);
- if(l != SAR_HDR)
- goto bad;
- if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag)))
- goto bad;
- l = atolwhex(arhdr.size);
- ldobj(f, l, pname);
- if(s->type == SXREF) {
- diag("%s: failed to load: %s", file, s->name);
- errorexit();
- }
- work = 1;
- xrefresolv = 1;
- }
- }
- return;
- bad:
- diag("%s: bad or out of date archive", file);
- out:
- close(f);
- }
- int
- zaddr(uchar *p, Adr *a, Sym *h[])
- {
- int c, t, i;
- int32 l;
- Sym *s;
- Auto *u;
- t = p[0];
- c = 1;
- if(t & T_INDEX) {
- a->index = p[c];
- a->scale = p[c+1];
- c += 2;
- } else {
- a->index = D_NONE;
- a->scale = 0;
- }
- a->offset = 0;
- if(t & T_OFFSET) {
- /*
- * Hack until Charles fixes the compiler.
- a->offset = (long)(p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24));
- */
- l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
- a->offset = l;
- c += 4;
- if(t & T_64) {
- l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
- a->offset = ((vlong)l<<32) | (a->offset & 0xFFFFFFFFUL);
- c += 4;
- }
- }
- a->sym = S;
- if(t & T_SYM) {
- a->sym = h[p[c]];
- c++;
- }
- a->type = D_NONE;
- if(t & T_FCONST) {
- a->ieee.l = p[c] | (p[c+1]<<8) | (p[c+2]<<16) | (p[c+3]<<24);
- a->ieee.h = p[c+4] | (p[c+5]<<8) | (p[c+6]<<16) | (p[c+7]<<24);
- c += 8;
- a->type = D_FCONST;
- } else
- if(t & T_SCONST) {
- for(i=0; i<NSNAME; i++)
- a->scon[i] = p[c+i];
- c += NSNAME;
- a->type = D_SCONST;
- }
- if(t & T_TYPE) {
- a->type = p[c];
- c++;
- }
- s = a->sym;
- if(s == S)
- return c;
- t = a->type;
- if(t != D_AUTO && t != D_PARAM)
- return c;
- l = a->offset;
- for(u=curauto; u; u=u->link) {
- if(u->asym == s)
- if(u->type == t) {
- if(u->aoffset > l)
- u->aoffset = l;
- return c;
- }
- }
- while(nhunk < sizeof(Auto))
- gethunk();
- u = (Auto*)hunk;
- nhunk -= sizeof(Auto);
- hunk += sizeof(Auto);
- u->link = curauto;
- curauto = u;
- u->asym = s;
- u->aoffset = l;
- u->type = t;
- return c;
- }
- void
- addlib(char *obj)
- {
- char name[1024], pname[1024], comp[256], *p;
- int i;
- int search = 0;
- if(histfrogp <= 0)
- return;
- if(histfrog[0]->name[1] == '/') {
- sprint(name, "");
- i = 1;
- } else
- if(histfrog[0]->name[1] == '.') {
- sprint(name, ".");
- i = 0;
- } else {
- sprint(name, "");
- search = 1;
- i = 0;
- // if(debug['9'])
- // sprint(name, "/%s/lib", thestring);
- // else
- // sprint(name, "/usr/%clib", thechar);
- // i = 0;
- }
- for(; i<histfrogp; i++) {
- snprint(comp, sizeof comp, "%s", histfrog[i]->name+1);
- for(;;) {
- p = strstr(comp, "$O");
- if(p == 0)
- break;
- memmove(p+1, p+2, strlen(p+2)+1);
- p[0] = thechar;
- }
- for(;;) {
- p = strstr(comp, "$M");
- if(p == 0)
- break;
- if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) {
- diag("library component too long");
- return;
- }
- memmove(p+strlen(thestring), p+2, strlen(p+2)+1);
- memmove(p, thestring, strlen(thestring));
- }
- if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) {
- diag("library component too long");
- return;
- }
- if(i > 0 || !search)
- strcat(name, "/");
- strcat(name, comp);
- }
- cleanname(name);
- if (search) {
- // try dot, then -L "libdir", then $NXM/amd64/lib
- for (i = 0; i < nlibdir; i++) {
- snprint(pname, sizeof pname, "%s/%s", libdir[i], name);
- if(debug['v'])
- Bprint(&bso, "searching for %s at %s\n", name, pname);
- if (access(pname, AEXIST) >= 0)
- break;
- }
- } else {
- strcpy(pname, name);
- }
- for(i=0; i<libraryp; i++)
- if(strcmp(pname, library[i]) == 0)
- return;
- if(libraryp == nelem(library)){
- diag("too many autolibs; skipping %s", pname);
- return;
- }
- p = malloc(strlen(pname) + 1);
- strcpy(p, pname);
- library[libraryp] = p;
- p = malloc(strlen(obj) + 1);
- strcpy(p, obj);
- libraryobj[libraryp] = p;
- libraryp++;
- }
- void
- addhist(long line, int type)
- {
- Auto *u;
- Sym *s;
- int i, j, k;
- u = malloc(sizeof(Auto));
- s = malloc(sizeof(Sym));
- s->name = malloc(2*(histfrogp+1) + 1);
- u->asym = s;
- u->type = type;
- u->aoffset = line;
- u->link = curhist;
- curhist = u;
- j = 1;
- for(i=0; i<histfrogp; i++) {
- k = histfrog[i]->value;
- s->name[j+0] = k>>8;
- s->name[j+1] = k;
- j += 2;
- }
- }
- void
- histtoauto(void)
- {
- Auto *l;
- while(l = curhist) {
- curhist = l->link;
- l->link = curauto;
- curauto = l;
- }
- }
- void
- collapsefrog(Sym *s)
- {
- int i;
- /*
- * bad encoding of path components only allows
- * MAXHIST components. if there is an overflow,
- * first try to collapse xxx/..
- */
- for(i=1; i<histfrogp; i++)
- if(strcmp(histfrog[i]->name+1, "..") == 0) {
- memmove(histfrog+i-1, histfrog+i+1,
- (histfrogp-i-1)*sizeof(histfrog[0]));
- histfrogp--;
- goto out;
- }
- /*
- * next try to collapse .
- */
- for(i=0; i<histfrogp; i++)
- if(strcmp(histfrog[i]->name+1, ".") == 0) {
- memmove(histfrog+i, histfrog+i+1,
- (histfrogp-i-1)*sizeof(histfrog[0]));
- goto out;
- }
- /*
- * last chance, just truncate from front
- */
- memmove(histfrog+0, histfrog+1,
- (histfrogp-1)*sizeof(histfrog[0]));
- out:
- histfrog[histfrogp-1] = s;
- }
- void
- nopout(Prog *p)
- {
- p->as = ANOP;
- p->from.type = D_NONE;
- p->to.type = D_NONE;
- }
- uchar*
- readsome(int f, uchar *buf, uchar *good, uchar *stop, int max)
- {
- int n;
- n = stop - good;
- memmove(buf, good, stop - good);
- stop = buf + n;
- n = MAXIO - n;
- if(n > max)
- n = max;
- n = read(f, stop, n);
- if(n <= 0)
- return 0;
- return stop + n;
- }
- void
- ldobj(int f, long c, char *pn)
- {
- vlong ipc;
- Prog *p, *t;
- uchar *bloc, *bsize, *stop;
- int v, o, r, skip, mode;
- Sym *h[NSYM], *s, *di;
- ulong sig;
- static int files = 0;
- static char **filen;
- char **nfilen;
- if((files&511) == 0){
- nfilen = malloc((files+512)*sizeof(char*));
- memmove(nfilen, filen, files*sizeof(char*));
- free(filen);
- filen = nfilen;
- }
- filen[files++] = strdup(pn);
- bsize = buf.xbuf;
- bloc = buf.xbuf;
- di = S;
- newloop:
- memset(h, 0, sizeof(h));
- version++;
- histfrogp = 0;
- ipc = pc;
- skip = 0;
- mode = 64;
- loop:
- if(c <= 0)
- goto eof;
- r = bsize - bloc;
- if(r < 100 && r < c) { /* enough for largest prog */
- bsize = readsome(f, buf.xbuf, bloc, bsize, c);
- if(bsize == 0)
- goto eof;
- bloc = buf.xbuf;
- goto loop;
- }
- o = bloc[0] | (bloc[1] << 8);
- if(o <= AXXX || o >= ALAST) {
- if(o < 0)
- goto eof;
- diag("%s: opcode out of range %d", pn, o);
- print(" probably not a .6 file\n");
- errorexit();
- }
- if(o == ANAME || o == ASIGNAME) {
- sig = 0;
- if(o == ASIGNAME) {
- sig = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24);
- bloc += 4;
- c -= 4;
- }
- stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
- if(stop == 0){
- bsize = readsome(f, buf.xbuf, bloc, bsize, c);
- if(bsize == 0)
- goto eof;
- bloc = buf.xbuf;
- stop = memchr(&bloc[4], 0, bsize-&bloc[4]);
- if(stop == 0){
- fprint(2, "%s: name too long\n", pn);
- errorexit();
- }
- }
- v = bloc[2]; /* type */
- o = bloc[3]; /* sym */
- bloc += 4;
- c -= 4;
- r = 0;
- if(v == D_STATIC)
- r = version;
- s = lookup((char*)bloc, r);
- c -= &stop[1] - bloc;
- bloc = stop + 1;
- if(debug['S'] && r == 0)
- sig = 1729;
- if(sig != 0){
- if(s->sig != 0 && s->sig != sig)
- diag("incompatible type signatures %lux(%s) and %lux(%s) for %s", s->sig, filen[s->file], sig, pn, s->name);
- s->sig = sig;
- s->file = files-1;
- }
- if(debug['W'])
- print(" ANAME %s\n", s->name);
- h[o] = s;
- if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
- s->type = SXREF;
- if(v == D_FILE) {
- if(s->type != SFILE) {
- histgen++;
- s->type = SFILE;
- s->value = histgen;
- }
- if(histfrogp < MAXHIST) {
- histfrog[histfrogp] = s;
- histfrogp++;
- } else
- collapsefrog(s);
- }
- goto loop;
- }
- while(nhunk < sizeof(Prog))
- gethunk();
- p = (Prog*)hunk;
- nhunk -= sizeof(Prog);
- hunk += sizeof(Prog);
- p->as = o;
- p->line = bloc[2] | (bloc[3] << 8) | (bloc[4] << 16) | (bloc[5] << 24);
- p->back = 2;
- p->mode = mode;
- r = zaddr(bloc+6, &p->from, h) + 6;
- r += zaddr(bloc+r, &p->to, h);
- bloc += r;
- c -= r;
- if(debug['W'])
- print("%P\n", p);
- switch(p->as) {
- case AHISTORY:
- if(p->to.offset == -1) {
- addlib(pn);
- histfrogp = 0;
- goto loop;
- }
- addhist(p->line, D_FILE); /* 'z' */
- if(p->to.offset)
- addhist(p->to.offset, D_FILE1); /* 'Z' */
- histfrogp = 0;
- goto loop;
- case AEND:
- histtoauto();
- if(curtext != P)
- curtext->to.autom = curauto;
- curauto = 0;
- curtext = P;
- if(c)
- goto newloop;
- return;
- case AGLOBL:
- s = p->from.sym;
- if(s->type == 0 || s->type == SXREF) {
- s->type = SBSS;
- s->value = 0;
- }
- if(s->type != SBSS) {
- diag("%s: redefinition: %s in %s",
- pn, s->name, TNAME);
- s->type = SBSS;
- s->value = 0;
- }
- if(p->to.offset > s->value)
- s->value = p->to.offset;
- goto loop;
- case ADYNT:
- if(p->to.sym == S) {
- diag("DYNT without a sym\n%P", p);
- break;
- }
- di = p->to.sym;
- p->from.scale = 4;
- if(di->type == SXREF) {
- if(debug['z'])
- Bprint(&bso, "%P set to %d\n", p, dtype);
- di->type = SCONST;
- di->value = dtype;
- dtype += 4;
- }
- if(p->from.sym == S)
- break;
- p->from.offset = di->value;
- p->from.sym->type = SDATA;
- if(curtext == P) {
- diag("DYNT not in text: %P", p);
- break;
- }
- p->to.sym = curtext->from.sym;
- p->to.type = D_ADDR;
- p->to.index = D_EXTERN;
- goto data;
- case AINIT:
- if(p->from.sym == S) {
- diag("INIT without a sym\n%P", p);
- break;
- }
- if(di == S) {
- diag("INIT without previous DYNT\n%P", p);
- break;
- }
- p->from.offset = di->value;
- p->from.sym->type = SDATA;
- goto data;
- case ADATA:
- data:
- if(edatap == P)
- datap = p;
- else
- edatap->link = p;
- edatap = p;
- p->link = P;
- goto loop;
- case AGOK:
- diag("%s: GOK opcode in %s", pn, TNAME);
- pc++;
- goto loop;
- case ATEXT:
- if(curtext != P) {
- histtoauto();
- curtext->to.autom = curauto;
- curauto = 0;
- }
- skip = 0;
- curtext = p;
- s = p->from.sym;
- if(s == S) {
- diag("%s: no TEXT symbol: %P", pn, p);
- errorexit();
- }
- if(s->type != 0 && s->type != SXREF) {
- if(p->from.scale & DUPOK) {
- skip = 1;
- goto casdef;
- }
- diag("%s: redefinition: %s\n%P", pn, s->name, p);
- }
- s->type = STEXT;
- s->value = pc;
- lastp->link = p;
- lastp = p;
- p->pc = pc;
- pc++;
- if(textp == P) {
- textp = p;
- etextp = p;
- goto loop;
- }
- etextp->pcond = p;
- etextp = p;
- goto loop;
- case AMODE:
- if(p->from.type == D_CONST || p->from.type == D_INDIR+D_NONE){
- switch((int)p->from.offset){
- case 16: case 32: case 64:
- mode = p->from.offset;
- break;
- }
- }
- goto loop;
- case AFMOVF:
- case AFADDF:
- case AFSUBF:
- case AFSUBRF:
- case AFMULF:
- case AFDIVF:
- case AFDIVRF:
- case AFCOMF:
- case AFCOMFP:
- case AMOVSS:
- case AADDSS:
- case ASUBSS:
- case AMULSS:
- case ADIVSS:
- case ACOMISS:
- case AUCOMISS:
- if(skip)
- goto casdef;
- if(p->from.type == D_FCONST) {
- /* size sb 9 max */
- sprint(literal, "$%lux", ieeedtof(&p->from.ieee));
- s = lookup(literal, 0);
- if(s->type == 0) {
- s->type = SBSS;
- s->value = 4;
- t = prg();
- t->as = ADATA;
- t->line = p->line;
- t->from.type = D_EXTERN;
- t->from.sym = s;
- t->from.scale = 4;
- t->to = p->from;
- if(edatap == P)
- datap = t;
- else
- edatap->link = t;
- edatap = t;
- t->link = P;
- }
- p->from.type = D_EXTERN;
- p->from.sym = s;
- p->from.offset = 0;
- }
- goto casdef;
- case AFMOVD:
- case AFADDD:
- case AFSUBD:
- case AFSUBRD:
- case AFMULD:
- case AFDIVD:
- case AFDIVRD:
- case AFCOMD:
- case AFCOMDP:
- case AMOVSD:
- case AADDSD:
- case ASUBSD:
- case AMULSD:
- case ADIVSD:
- case ACOMISD:
- case AUCOMISD:
- if(skip)
- goto casdef;
- if(p->from.type == D_FCONST) {
- /* size sb 18 max */
- sprint(literal, "$%lux.%lux",
- p->from.ieee.l, p->from.ieee.h);
- s = lookup(literal, 0);
- if(s->type == 0) {
- s->type = SBSS;
- s->value = 8;
- t = prg();
- t->as = ADATA;
- t->line = p->line;
- t->from.type = D_EXTERN;
- t->from.sym = s;
- t->from.scale = 8;
- t->to = p->from;
- if(edatap == P)
- datap = t;
- else
- edatap->link = t;
- edatap = t;
- t->link = P;
- }
- p->from.type = D_EXTERN;
- p->from.sym = s;
- p->from.offset = 0;
- }
- goto casdef;
- casdef:
- default:
- if(skip)
- nopout(p);
- if(p->to.type == D_BRANCH)
- p->to.offset += ipc;
- lastp->link = p;
- lastp = p;
- p->pc = pc;
- pc++;
- goto loop;
- }
- goto loop;
- eof:
- diag("truncated object file: %s", pn);
- }
- Sym*
- lookup(char *symb, int v)
- {
- Sym *s;
- char *p;
- long h;
- int l, c;
- h = v;
- for(p=symb; c = *p; p++)
- h = h+h+h + c;
- l = (p - symb) + 1;
- // not if(h < 0) h = ~h, because gcc 4.3 -O2 miscompiles it.
- h &= 0xffffff;
- h %= NHASH;
- for(s = hash[h]; s != S; s = s->link)
- if(s->version == v)
- if(memcmp(s->name, symb, l) == 0)
- return s;
- while(nhunk < sizeof(Sym))
- gethunk();
- s = (Sym*)hunk;
- nhunk -= sizeof(Sym);
- hunk += sizeof(Sym);
- s->name = malloc(l + 1);
- memmove(s->name, symb, l);
- s->link = hash[h];
- s->type = 0;
- s->version = v;
- s->value = 0;
- s->sig = 0;
- hash[h] = s;
- nsymbol++;
- return s;
- }
- Prog*
- prg(void)
- {
- Prog *p;
- while(nhunk < sizeof(Prog))
- gethunk();
- p = (Prog*)hunk;
- nhunk -= sizeof(Prog);
- hunk += sizeof(Prog);
- *p = zprg;
- return p;
- }
- Prog*
- copyp(Prog *q)
- {
- Prog *p;
- p = prg();
- *p = *q;
- return p;
- }
- Prog*
- appendp(Prog *q)
- {
- Prog *p;
- p = prg();
- p->link = q->link;
- q->link = p;
- p->line = q->line;
- p->mode = q->mode;
- return p;
- }
- void
- gethunk(void)
- {
- char *h;
- long nh;
- nh = NHUNK;
- if(thunk >= 5L*NHUNK) {
- nh = 5L*NHUNK;
- if(thunk >= 25L*NHUNK)
- nh = 25L*NHUNK;
- }
- h = malloc(nh); //mysbrk(nh);
- if(h == NULL) {
- diag("out of memory");
- errorexit();
- }
- hunk = h;
- nhunk = nh;
- thunk += nh;
- }
- void
- doprof1(void)
- {
- Sym *s;
- long n;
- Prog *p, *q;
- if(debug['v'])
- Bprint(&bso, "%5.2f profile 1\n", cputime());
- Bflush(&bso);
- s = lookup("__mcount", 0);
- n = 1;
- for(p = firstp->link; p != P; p = p->link) {
- if(p->as == ATEXT) {
- q = prg();
- q->line = p->line;
- q->link = datap;
- datap = q;
- q->as = ADATA;
- q->from.type = D_EXTERN;
- q->from.offset = n*4;
- q->from.sym = s;
- q->from.scale = 4;
- q->to = p->from;
- q->to.type = D_CONST;
- q = prg();
- q->line = p->line;
- q->pc = p->pc;
- q->link = p->link;
- p->link = q;
- p = q;
- p->as = AADDL;
- p->from.type = D_CONST;
- p->from.offset = 1;
- p->to.type = D_EXTERN;
- p->to.sym = s;
- p->to.offset = n*4 + 4;
- n += 2;
- continue;
- }
- }
- q = prg();
- q->line = 0;
- q->link = datap;
- datap = q;
- q->as = ADATA;
- q->from.type = D_EXTERN;
- q->from.sym = s;
- q->from.scale = 4;
- q->to.type = D_CONST;
- q->to.offset = n;
- s->type = SBSS;
- s->value = n*4;
- }
- void
- doprof2(void)
- {
- Sym *s2, *s4;
- Prog *p, *q, *q2, *ps2, *ps4;
- if(debug['v'])
- Bprint(&bso, "%5.2f profile 2\n", cputime());
- Bflush(&bso);
- if(debug['e']){
- s2 = lookup("_tracein", 0);
- s4 = lookup("_traceout", 0);
- }else{
- s2 = lookup("_profin", 0);
- s4 = lookup("_profout", 0);
- }
- if(s2->type != STEXT || s4->type != STEXT) {
- if(debug['e'])
- diag("_tracein/_traceout not defined %d %d", s2->type, s4->type);
- else
- diag("_profin/_profout not defined");
- return;
- }
- ps2 = P;
- ps4 = P;
- for(p = firstp; p != P; p = p->link) {
- if(p->as == ATEXT) {
- if(p->from.sym == s2) {
- p->from.scale = 1;
- ps2 = p;
- }
- if(p->from.sym == s4) {
- p->from.scale = 1;
- ps4 = p;
- }
- }
- }
- for(p = firstp; p != P; p = p->link) {
- if(p->as == ATEXT) {
- curtext = p;
- if(p->from.scale & NOPROF) { /* dont profile */
- for(;;) {
- q = p->link;
- if(q == P)
- break;
- if(q->as == ATEXT)
- break;
- p = q;
- }
- continue;
- }
- /*
- * JMPL profin
- */
- q = prg();
- q->line = p->line;
- q->pc = p->pc;
- q->link = p->link;
- if(debug['e']){ /* embedded tracing */
- q2 = prg();
- p->link = q2;
- q2->link = q;
- q2->line = p->line;
- q2->pc = p->pc;
- q2->as = AJMP;
- q2->to.type = D_BRANCH;
- q2->to.sym = p->to.sym;
- q2->pcond = q->link;
- }else
- p->link = q;
- p = q;
- p->as = ACALL;
- p->to.type = D_BRANCH;
- p->pcond = ps2;
- p->to.sym = s2;
- continue;
- }
- if(p->as == ARET) {
- /*
- * RET (default)
- */
- if(debug['e']){ /* embedded tracing */
- q = prg();
- q->line = p->line;
- q->pc = p->pc;
- q->link = p->link;
- p->link = q;
- p = q;
- }
- /*
- * RET
- */
- q = prg();
- q->as = ARET;
- q->from = p->from;
- q->to = p->to;
- q->link = p->link;
- p->link = q;
- /*
- * JAL profout
- */
- p->as = ACALL;
- p->from = zprg.from;
- p->to = zprg.to;
- p->to.type = D_BRANCH;
- p->pcond = ps4;
- p->to.sym = s4;
- p = q;
- continue;
- }
- }
- }
- void
- nuxiinit(void)
- {
- int i, c;
- for(i=0; i<4; i++) {
- c = find1(0x04030201L, i+1);
- if(i < 2)
- inuxi2[i] = c;
- if(i < 1)
- inuxi1[i] = c;
- inuxi4[i] = c;
- inuxi8[i] = c;
- inuxi8[i+4] = c+4;
- fnuxi4[i] = c;
- fnuxi8[i] = c;
- fnuxi8[i+4] = c+4;
- }
- if(debug['v']) {
- Bprint(&bso, "inuxi = ");
- for(i=0; i<1; i++)
- Bprint(&bso, "%d", inuxi1[i]);
- Bprint(&bso, " ");
- for(i=0; i<2; i++)
- Bprint(&bso, "%d", inuxi2[i]);
- Bprint(&bso, " ");
- for(i=0; i<4; i++)
- Bprint(&bso, "%d", inuxi4[i]);
- Bprint(&bso, " ");
- for(i=0; i<8; i++)
- Bprint(&bso, "%d", inuxi8[i]);
- Bprint(&bso, "\nfnuxi = ");
- for(i=0; i<4; i++)
- Bprint(&bso, "%d", fnuxi4[i]);
- Bprint(&bso, " ");
- for(i=0; i<8; i++)
- Bprint(&bso, "%d", fnuxi8[i]);
- Bprint(&bso, "\n");
- }
- Bflush(&bso);
- }
- int
- find1(long l, int c)
- {
- char *p;
- int i;
- p = (char*)&l;
- for(i=0; i<4; i++)
- if(*p++ == c)
- return i;
- return 0;
- }
- int
- find2(long l, int c)
- {
- short *p;
- int i;
- p = (short*)&l;
- for(i=0; i<4; i+=2) {
- if(((*p >> 8) & 0xff) == c)
- return i;
- if((*p++ & 0xff) == c)
- return i+1;
- }
- return 0;
- }
- long
- ieeedtof(Ieee *e)
- {
- int exp;
- long v;
- if(e->h == 0)
- return 0;
- exp = (e->h>>20) & ((1L<<11)-1L);
- exp -= (1L<<10) - 2L;
- v = (e->h & 0xfffffL) << 3;
- v |= (e->l >> 29) & 0x7L;
- if((e->l >> 28) & 1) {
- v++;
- if(v & 0x800000L) {
- v = (v & 0x7fffffL) >> 1;
- exp++;
- }
- }
- if(exp <= -126 || exp >= 130)
- diag("double fp to single fp overflow");
- v |= ((exp + 126) & 0xffL) << 23;
- v |= e->h & 0x80000000L;
- return v;
- }
- double
- ieeedtod(Ieee *ieeep)
- {
- Ieee e;
- double fr;
- int exp;
- if(ieeep->h & (1L<<31)) {
- e.h = ieeep->h & ~(1L<<31);
- e.l = ieeep->l;
- return -ieeedtod(&e);
- }
- if(ieeep->l == 0 && ieeep->h == 0)
- return 0;
- fr = ieeep->l & ((1L<<16)-1L);
- fr /= 1L<<16;
- fr += (ieeep->l>>16) & ((1L<<16)-1L);
- fr /= 1L<<16;
- fr += (ieeep->h & (1L<<20)-1L) | (1L<<20);
- fr /= 1L<<21;
- exp = (ieeep->h>>20) & ((1L<<11)-1L);
- exp -= (1L<<10) - 2L;
- return ldexp(fr, exp);
- }
- void
- undefsym(Sym *s)
- {
- int n;
- n = imports;
- if(s->value != 0)
- diag("value != 0 on SXREF");
- if(n >= 1<<Rindex)
- diag("import index %d out of range", n);
- s->value = n<<Roffset;
- s->type = SUNDEF;
- imports++;
- }
- void
- zerosig(char *sp)
- {
- Sym *s;
- s = lookup(sp, 0);
- s->sig = 0;
- }
- void
- readundefs(char *f, int t)
- {
- int i, n;
- Sym *s;
- Biobuf *b;
- char *l, buf[256], *fields[64];
- if(f == nil)
- return;
- b = Bopen(f, OREAD);
- if(b == nil){
- diag("could not open %s: %r", f);
- errorexit();
- }
- while((l = Brdline(b, '\n')) != nil){
- n = Blinelen(b);
- if(n >= sizeof(buf)){
- diag("%s: line too long", f);
- errorexit();
- }
- memmove(buf, l, n);
- buf[n-1] = '\0';
- n = getfields(buf, fields, nelem(fields), 1, " \t\r\n");
- if(n == nelem(fields)){
- diag("%s: bad format", f);
- errorexit();
- }
- for(i = 0; i < n; i++){
- s = lookup(fields[i], 0);
- s->type = SXREF;
- s->subtype = t;
- if(t == SIMPORT)
- nimports++;
- else
- nexports++;
- }
- }
- Bterm(b);
- }
|