diff options
| author | forsyth <forsyth@lavoro.terzarima.net> | 2013-06-03 21:01:14 +0000 |
|---|---|---|
| committer | forsyth <forsyth@lavoro.terzarima.net> | 2013-06-03 21:01:14 +0000 |
| commit | 45a20ab721a513710138340faff3d59a31c3e01e (patch) | |
| tree | eea29d2684c51cc73725b8992a2125bede48e118 /utils/6l/obj.c | |
| parent | cd8e99851af33e52bcdf8faf34f9d4e62fa0cbaf (diff) | |
sync compilers with Plan 9
remove 1[acl] 2[acl]
Diffstat (limited to 'utils/6l/obj.c')
| -rw-r--r-- | utils/6l/obj.c | 207 |
1 files changed, 159 insertions, 48 deletions
diff --git a/utils/6l/obj.c b/utils/6l/obj.c index 9f57d612..ed52ec2b 100644 --- a/utils/6l/obj.c +++ b/utils/6l/obj.c @@ -12,14 +12,25 @@ char thechar = '6'; char *thestring = "amd64"; char *paramspace = "FP"; +char** libdir; +int nlibdir = 0; +static int maxlibdir = 0; + /* - * -H2 -T4136 -R4096 is plan9 64-bit format - * -H3 -T4128 -R4096 is plan9 32-bit format + * -H2 -T0x200028 -R0x200000 is plan9 format (was -T4136 -R4096) * -H5 -T0x80110000 -R4096 is ELF32 + * -H6 -T0x2000e8 -R0x200000 is ELF64 * - * options used: 189BLQSWabcjlnpsvz + * options used: 189BLPQSVWabcjlnpsvz */ +void +usage(void) +{ + diag("usage: %s [-options] objects", argv0); + errorexit(); +} + static int isobjfile(char *f) { @@ -47,6 +58,7 @@ main(int argc, char *argv[]) { int i, c; char *a; + char name[LIBNAMELEN]; Binit(&bso, 1, OWRITE); cout = -1; @@ -56,6 +68,7 @@ main(int argc, char *argv[]) outfile = "6.out"; HEADTYPE = -1; INITTEXT = -1; + INITTEXTP = -1; INITDAT = -1; INITRND = -1; INITENTRY = 0; @@ -78,11 +91,19 @@ main(int argc, char *argv[]) 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) @@ -106,12 +127,20 @@ main(int argc, char *argv[]) break; } ARGEND USED(argc); - if(*argv == 0) { - diag("usage: 6l [-options] objects"); - errorexit(); - } + 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; @@ -125,31 +154,33 @@ main(int argc, char *argv[]) case 2: /* plan 9 */ HEADR = 32L+8L; if(INITTEXT == -1) - INITTEXT = 4096+HEADR; + INITTEXT = 0x200000+HEADR; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) - INITRND = 4096; + INITRND = 0x200000; break; - case 3: /* plan 9 */ - HEADR = 32L; + case 5: /* elf32 executable */ + HEADR = rnd(Ehdr32sz+3*Phdr32sz, 16); if(INITTEXT == -1) - INITTEXT = 4096+32; + INITTEXT = 0xf0110000L; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) INITRND = 4096; break; - case 5: /* elf32 executable */ - HEADR = rnd(52L+3*32L, 16); + case 6: /* ELF64 executable */ + HEADR = rnd(Ehdr64sz+3*Phdr64sz, 16); if(INITTEXT == -1) - INITTEXT = 0x80110000L; + INITTEXT = 0x200000+HEADR; if(INITDAT == -1) INITDAT = 0; if(INITRND == -1) - INITRND = 4096; + 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); @@ -283,7 +314,7 @@ main(int argc, char *argv[]) dtype = 4; cout = create(outfile, 1, 0775); if(cout < 0) { - diag("cannot create %s", outfile); + diag("cannot create %s: %r", outfile); errorexit(); } version = 0; @@ -351,6 +382,42 @@ main(int argc, char *argv[]) } 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; @@ -390,25 +457,26 @@ objfile(char *file) int f, work; Sym *s; char magbuf[SARMAG]; - char name[100], pname[150]; + char name[LIBNAMELEN], pname[LIBNAMELEN]; struct ar_hdr arhdr; char *e, *start, *stop; - if(file[0] == '-' && file[1] == 'l') { - 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; - } 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 file: %s", file); + diag("cannot open %s: %r", file); errorexit(); } l = read(f, magbuf, SARMAG); @@ -467,7 +535,8 @@ objfile(char *file) l |= (e[3] & 0xff) << 16; l |= (e[4] & 0xff) << 24; seek(f, l, 0); - l = read(f, &arhdr, SAR_HDR); + /* need readn to read the dumps (at least) */ + l = readn(f, &arhdr, SAR_HDR); if(l != SAR_HDR) goto bad; if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag))) @@ -494,7 +563,7 @@ int zaddr(uchar *p, Adr *a, Sym *h[]) { int c, t, i; - long l; + int l; Sym *s; Auto *u; @@ -579,25 +648,24 @@ zaddr(uchar *p, Adr *a, Sym *h[]) void addlib(char *obj) { - char name[1024], comp[256], *p; - int i; + char fn1[LIBNAMELEN], fn2[LIBNAMELEN], comp[LIBNAMELEN], *p, *name; + int i, search; if(histfrogp <= 0) return; + name = fn1; + search = 0; if(histfrog[0]->name[1] == '/') { sprint(name, ""); i = 1; - } else - if(histfrog[0]->name[1] == '.') { + } else if(histfrog[0]->name[1] == '.') { sprint(name, "."); i = 0; } else { - if(debug['9']) - sprint(name, "/%s/lib", thestring); - else - sprint(name, "/usr/%clib", thechar); + sprint(name, ""); i = 0; + search = 1; } for(; i<histfrogp; i++) { @@ -620,13 +688,25 @@ addlib(char *obj) memmove(p+strlen(thestring), p+2, strlen(p+2)+1); memmove(p, thestring, strlen(thestring)); } - if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) { + if(strlen(fn1) + strlen(comp) + 3 >= sizeof(fn1)) { diag("library component too long"); return; } - strcat(name, "/"); - strcat(name, comp); + if(i > 0 || !search) + strcat(fn1, "/"); + strcat(fn1, comp); + } + + cleanname(name); + + if(search){ + p = findlib(name); + if(p != nil){ + snprint(fn2, sizeof(fn2), "%s/%s", p, name); + name = fn2; + } } + for(i=0; i<libraryp; i++) if(strcmp(name, library[i]) == 0) return; @@ -1138,8 +1218,7 @@ lookup(char *symb, int v) for(p=symb; c = *p; p++) h = h+h+h + c; l = (p - symb) + 1; - if(h < 0) - h = ~h; + h &= 0xffffff; h %= NHASH; for(s = hash[h]; s != S; s = s->link) if(s->version == v) @@ -1288,16 +1367,24 @@ void doprof2(void) { Sym *s2, *s4; - Prog *p, *q, *ps2, *ps4; + Prog *p, *q, *q2, *ps2, *ps4; if(debug['v']) Bprint(&bso, "%5.2f profile 2\n", cputime()); Bflush(&bso); - s2 = lookup("_profin", 0); - s4 = lookup("_profout", 0); + 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) { - diag("_profin/_profout not defined"); + if(debug['e']) + diag("_tracein/_traceout not defined %d %d", s2->type, s4->type); + else + diag("_profin/_profout not defined"); return; } @@ -1338,7 +1425,20 @@ doprof2(void) q->line = p->line; q->pc = p->pc; q->link = p->link; - p->link = q; + 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; @@ -1349,6 +1449,17 @@ doprof2(void) } 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(); |
