Commit 916f896a authored by Russ Cox's avatar Russ Cox

ld: various bug fixes

Fixes #937.
Fixes #938.
Fixes #939.
Fixes #940.

R=r
CC=golang-dev
https://golang.org/cl/1886043
parent 8629e5a6
...@@ -51,28 +51,6 @@ char* paramspace = "FP"; ...@@ -51,28 +51,6 @@ char* paramspace = "FP";
* options used: 189BLQSWabcjlnpsvz * options used: 189BLQSWabcjlnpsvz
*/ */
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 void
usage(void) usage(void)
{ {
...@@ -400,10 +378,21 @@ main(int argc, char *argv[]) ...@@ -400,10 +378,21 @@ main(int argc, char *argv[])
errorexit(); errorexit();
} }
Sym*
zsym(char *pn, Biobuf *f, Sym *h[])
{
int o;
o = Bgetc(f);
if(o < 0 || o >= NSYM || h[o] == nil)
mangle(pn);
return h[o];
}
void void
zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[]) zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[])
{ {
int o, t; int t;
int32 l; int32 l;
Sym *s; Sym *s;
Auto *u; Auto *u;
...@@ -424,14 +413,8 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[]) ...@@ -424,14 +413,8 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[])
} }
} }
a->sym = S; a->sym = S;
if(t & T_SYM) { if(t & T_SYM)
o = Bgetc(f); a->sym = zsym(pn, f, h);
if(o < 0 || o >= NSYM || h[o] == nil) {
fprint(2, "%s: mangled input file\n", pn);
errorexit();
}
a->sym = h[o];
}
a->type = D_NONE; a->type = D_NONE;
if(t & T_FCONST) { if(t & T_FCONST) {
a->ieee.l = Bget4(f); a->ieee.l = Bget4(f);
...@@ -446,10 +429,15 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[]) ...@@ -446,10 +429,15 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[])
a->type = Bgetc(f); a->type = Bgetc(f);
adrgotype = S; adrgotype = S;
if(t & T_GOTYPE) if(t & T_GOTYPE)
adrgotype = h[Bgetc(f)]; adrgotype = zsym(pn, f, h);
s = a->sym; s = a->sym;
if(s == S) if(s == S) {
switch(a->type) {
case D_SIZE:
mangle(pn);
}
return; return;
}
t = a->type; t = a->type;
if(t != D_AUTO && t != D_PARAM) { if(t != D_AUTO && t != D_PARAM) {
...@@ -563,10 +551,8 @@ loop: ...@@ -563,10 +551,8 @@ loop:
if(debug['W']) if(debug['W'])
print(" ANAME %s\n", s->name); print(" ANAME %s\n", s->name);
if(o < 0 || o >= nelem(h)) { if(o < 0 || o >= nelem(h))
fprint(2, "%s: mangled input file\n", pn); mangle(pn);
errorexit();
}
h[o] = s; h[o] = s;
if((v == D_EXTERN || v == D_STATIC) && s->type == 0) if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
s->type = SXREF; s->type = SXREF;
......
...@@ -323,7 +323,7 @@ loop: ...@@ -323,7 +323,7 @@ loop:
if(a != ACALL) { if(a != ACALL) {
q = brchain(p->link); q = brchain(p->link);
if(q != P && q->mark) if(q != P && q->mark)
if(a != ALOOP) { if(a != ALOOP && a != ATEXT) {
p->as = relinv(a); p->as = relinv(a);
p->link = p->pcond; p->link = p->pcond;
p->pcond = q; p->pcond = q;
...@@ -376,6 +376,7 @@ relinv(int a) ...@@ -376,6 +376,7 @@ relinv(int a)
case AJOC: return AJOS; case AJOC: return AJOS;
} }
diag("unknown relation: %s in %s", anames[a], TNAME); diag("unknown relation: %s in %s", anames[a], TNAME);
errorexit();
return a; return a;
} }
...@@ -574,7 +575,7 @@ dostkoff(void) ...@@ -574,7 +575,7 @@ dostkoff(void)
for(i=0; i<nelem(morename); i++) { for(i=0; i<nelem(morename); i++) {
if(pmorestack[i] == P) if(pmorestack[i] == P)
diag("morestack trampoline not defined"); diag("morestack trampoline not defined - %s", morename[i]);
} }
curframe = 0; curframe = 0;
...@@ -1036,6 +1037,8 @@ newtext(Prog *p, Sym *s) ...@@ -1036,6 +1037,8 @@ newtext(Prog *p, Sym *s)
p->as = ATEXT; p->as = ATEXT;
p->from.sym = s; p->from.sym = s;
} }
if(p->from.sym == S)
abort();
s->type = STEXT; s->type = STEXT;
s->text = p; s->text = p;
s->value = pc; s->value = pc;
......
...@@ -56,28 +56,6 @@ char *thestring = "386"; ...@@ -56,28 +56,6 @@ char *thestring = "386";
* -H9 -Tx -Rx is FreeBSD ELF32 * -H9 -Tx -Rx is FreeBSD ELF32
*/ */
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 void
usage(void) usage(void)
{ {
...@@ -439,6 +417,17 @@ main(int argc, char *argv[]) ...@@ -439,6 +417,17 @@ main(int argc, char *argv[])
errorexit(); errorexit();
} }
Sym*
zsym(char *pn, Biobuf *f, Sym *h[])
{
int o;
o = Bgetc(f);
if(o < 0 || o >= NSYM || h[o] == nil)
mangle(pn);
return h[o];
}
void void
zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[]) zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[])
{ {
...@@ -464,14 +453,8 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[]) ...@@ -464,14 +453,8 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[])
a->type = D_CONST2; a->type = D_CONST2;
} }
a->sym = S; a->sym = S;
if(t & T_SYM) { if(t & T_SYM)
o = Bgetc(f); a->sym = zsym(pn, f, h);
if(o < 0 || o >= NSYM || h[o] == nil) {
fprint(2, "%s: mangled input file\n", pn);
errorexit();
}
a->sym = h[o];
}
if(t & T_FCONST) { if(t & T_FCONST) {
a->ieee.l = Bget4(f); a->ieee.l = Bget4(f);
a->ieee.h = Bget4(f); a->ieee.h = Bget4(f);
...@@ -485,7 +468,7 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[]) ...@@ -485,7 +468,7 @@ zaddr(char *pn, Biobuf *f, Adr *a, Sym *h[])
a->type = Bgetc(f); a->type = Bgetc(f);
adrgotype = S; adrgotype = S;
if(t & T_GOTYPE) if(t & T_GOTYPE)
adrgotype = h[Bgetc(f)]; adrgotype = zsym(pn, f, h);
t = a->type; t = a->type;
if(t == D_INDIR+D_GS) if(t == D_INDIR+D_GS)
...@@ -605,10 +588,8 @@ loop: ...@@ -605,10 +588,8 @@ loop:
if(debug['W']) if(debug['W'])
print(" ANAME %s\n", s->name); print(" ANAME %s\n", s->name);
if(o < 0 || o >= nelem(h)) { if(o < 0 || o >= nelem(h))
fprint(2, "%s: mangled input file\n", pn); mangle(pn);
errorexit();
}
h[o] = s; h[o] = s;
if((v == D_EXTERN || v == D_STATIC) && s->type == 0) if((v == D_EXTERN || v == D_STATIC) && s->type == 0)
s->type = SXREF; s->type = SXREF;
......
...@@ -294,16 +294,18 @@ loop: ...@@ -294,16 +294,18 @@ loop:
if(a != ACALL) { if(a != ACALL) {
q = brchain(p->link); q = brchain(p->link);
if(q != P && q->mark) if(q != P && q->mark)
if(a != ALOOP) { if(a != ALOOP && a != ATEXT) {
p->as = relinv(a); p->as = relinv(a);
p->link = p->pcond; p->link = p->pcond;
p->pcond = q; p->pcond = q;
} }
xfol(p->link); xfol(p->link);
q = brchain(p->pcond); if(a != ATEXT) {
if(q->mark) { q = brchain(p->pcond);
p->pcond = q; if(q->mark) {
return; p->pcond = q;
return;
}
} }
p = q; p = q;
goto loop; goto loop;
......
...@@ -901,3 +901,9 @@ iconv(Fmt *fp) ...@@ -901,3 +901,9 @@ iconv(Fmt *fp)
return 0; return 0;
} }
void
mangle(char *file)
{
fprint(2, "%s; mangled input file\n", file);
errorexit();
}
...@@ -83,6 +83,7 @@ void readundefs(char *f, int t); ...@@ -83,6 +83,7 @@ void readundefs(char *f, int t);
int32 Bget4(Biobuf *f); int32 Bget4(Biobuf *f);
void loadlib(void); void loadlib(void);
void errorexit(void); void errorexit(void);
void mangle(char*);
void objfile(char *file, char *pkg); void objfile(char *file, char *pkg);
void libinit(void); void libinit(void);
void Lflag(char *arg); void Lflag(char *arg);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment