Commit d94bf762 authored by Russ Cox's avatar Russ Cox

ld: weak symbols

A reference to the address of weak.foo resolves at link time
to the address of the symbol foo if foo would end up in the
binary anyway, or to zero if foo would not be in the binary.

For example:

        int xxx = 1;
        int yyy = 2;
        int weak·xxx;
        int weak·yyy;

        void main·main(void) {
                runtime·printf("%p %p %p\n", &xxx, &weak·xxx, &weak·yyy);
        }

prints the same non-nil address twice, then 0 (because yyy is not
referenced so it was dropped from the binary).

This will be used by the reflection tables.

R=iant
CC=golang-dev
https://golang.org/cl/4223044
parent 4185a9e2
...@@ -280,6 +280,7 @@ main(int argc, char *argv[]) ...@@ -280,6 +280,7 @@ main(int argc, char *argv[])
symtab(); symtab();
dodata(); dodata();
address(); address();
doweak();
reloc(); reloc();
asmb(); asmb();
undef(); undef();
......
...@@ -35,18 +35,6 @@ ...@@ -35,18 +35,6 @@
static void xfol(Prog*, Prog**); static void xfol(Prog*, Prog**);
void
undef(void)
{
int i;
Sym *s;
for(i=0; i<NHASH; i++)
for(s = hash[i]; s != S; s = s->hash)
if(s->type == SXREF)
diag("%s: not defined", s->name);
}
Prog* Prog*
brchain(Prog *p) brchain(Prog *p)
{ {
......
...@@ -267,6 +267,7 @@ main(int argc, char *argv[]) ...@@ -267,6 +267,7 @@ main(int argc, char *argv[])
symtab(); symtab();
dodata(); dodata();
address(); address();
doweak();
reloc(); reloc();
asmb(); asmb();
undef(); undef();
......
...@@ -718,15 +718,3 @@ atolwhex(char *s) ...@@ -718,15 +718,3 @@ atolwhex(char *s)
n = -n; n = -n;
return n; return n;
} }
void
undef(void)
{
int i;
Sym *s;
for(i=0; i<NHASH; i++)
for(s = hash[i]; s != S; s = s->hash)
if(s->type == SXREF)
diag("%s: not defined", s->name);
}
...@@ -316,6 +316,7 @@ main(int argc, char *argv[]) ...@@ -316,6 +316,7 @@ main(int argc, char *argv[])
symtab(); symtab();
dodata(); dodata();
address(); address();
doweak();
reloc(); reloc();
asmb(); asmb();
undef(); undef();
......
...@@ -666,15 +666,3 @@ atolwhex(char *s) ...@@ -666,15 +666,3 @@ atolwhex(char *s)
n = -n; n = -n;
return n; return n;
} }
void
undef(void)
{
int i;
Sym *s;
for(i=0; i<NHASH; i++)
for(s = hash[i]; s != S; s = s->hash)
if(s->type == SXREF)
diag("%s(%d): not defined", s->name, s->version);
}
...@@ -550,6 +550,8 @@ mark(Sym *s) ...@@ -550,6 +550,8 @@ mark(Sym *s)
if(s == S || s->reachable) if(s == S || s->reachable)
return; return;
if(strncmp(s->name, "weak.", 5) == 0)
return;
s->reachable = 1; s->reachable = 1;
if(s->text) if(s->text)
marktext(s); marktext(s);
...@@ -654,6 +656,35 @@ deadcode(void) ...@@ -654,6 +656,35 @@ deadcode(void)
textp = nil; textp = nil;
else else
last->next = nil; last->next = nil;
for(i=0; i<NHASH; i++)
for(s = hash[i]; s != S; s = s->hash)
if(strncmp(s->name, "weak.", 5) == 0) {
s->special = 1; // do not lay out in data segment
s->reachable = 1;
}
}
void
doweak(void)
{
int i;
Sym *s, *t;
// resolve weak references only if
// target symbol will be in binary anyway.
for(i=0; i<NHASH; i++)
for(s = hash[i]; s != S; s = s->hash) {
if(strncmp(s->name, "weak.", 5) == 0) {
t = lookup(s->name+5, s->version);
if(t->type != 0 && t->reachable) {
s->value = t->value;
s->type = t->type;
} else
s->value = 0;
continue;
}
}
} }
void void
......
...@@ -1279,3 +1279,15 @@ headtype(char *name) ...@@ -1279,3 +1279,15 @@ headtype(char *name)
errorexit(); errorexit();
return -1; // not reached return -1; // not reached
} }
void
undef(void)
{
int i;
Sym *s;
for(i=0; i<NHASH; i++)
for(s = hash[i]; s != S; s = s->hash)
if(s->type == SXREF)
diag("%s(%d): not defined", s->name, s->version);
}
...@@ -167,6 +167,8 @@ int archreloc(Reloc*, Sym*, vlong*); ...@@ -167,6 +167,8 @@ int archreloc(Reloc*, Sym*, vlong*);
void adddynsym(Sym*); void adddynsym(Sym*);
void addexport(void); void addexport(void);
void dostkcheck(void); void dostkcheck(void);
void undef(void);
void doweak(void);
int pathchar(void); int pathchar(void);
void* mal(uint32); void* mal(uint32);
......
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