static Optab* opindex[ALAST+1];
static vlong vaddr(Link*, Addr*, Reloc*);
+// isextern reports whether s describes an external symbol that must avoid pc-relative addressing.
+// This happens on systems like Solaris that call .so functions instead of system calls.
+// It does not seem to be necessary for any other systems. This is probably working
+// around a Solaris-specific bug that should be fixed differently, but we don't know
+// what that bug is. And this does fix it.
+static int
+isextern(LSym *s)
+{
+ // All the Solaris dynamic imports from libc.so begin with "libc·", which
+ // the compiler rewrites to "libc." by the time liblink gets it.
+ return strncmp(s->name, "libc.", 5) == 0;
+}
+
// single-instruction no-ops of various lengths.
// constructed by hand and disassembled with gdb to verify.
// see http://www.agner.org/optimize/optimizing_assembly.pdf for discussion.
switch(a->index) {
case D_EXTERN:
case D_STATIC:
+ if(a->sym != nil && isextern(a->sym))
+ return Yi32;
return Yiauto; // use pc-relative addressing
case D_AUTO:
case D_PARAM:
return Yi32; /* unsigned */
return Yi64;
}
- return Yi32; /* TO DO: D_ADDR as Yi64 */
+ return Yi32;
case D_BRANCH:
return Ybr;
ctxt->diag("need reloc for %D", a);
sysfatal("reloc");
}
- r->siz = 4; // TODO: 8 for external symbols
+ if(isextern(s)) {
+ r->siz = 4;
+ r->type = R_ADDR;
+ } else {
+ r->siz = 4;
+ r->type = R_PCREL;
+ }
r->off = -1; // caller must fill in
r->sym = s;
r->add = v;
v = 0;
- r->type = R_PCREL;
if(s->type == STLSBSS) {
r->xadd = r->add - r->siz;
r->type = R_TLS;
switch(t) {
default:
goto bad;
+ case D_EXTERN:
+ case D_STATIC:
+ if(!isextern(a->sym))
+ goto bad;
+ t = D_NONE;
+ v = vaddr(ctxt, a, &rel);
+ break;
case D_AUTO:
case D_PARAM:
t = D_SP;
ctxt->rexflag |= (regrex[t] & Rxb) | rex;
if(t == D_NONE || (D_CS <= t && t <= D_GS) || t == D_TLS) {
- if(t == D_NONE && (a->type == D_STATIC || a->type == D_EXTERN) || ctxt->asmode != 64) {
+ if((a->sym == nil || !isextern(a->sym)) && t == D_NONE && (a->type == D_STATIC || a->type == D_EXTERN) || ctxt->asmode != 64) {
*ctxt->andptr++ = (0 << 6) | (5 << 0) | (r << 3);
goto putrelv;
}