This source file includes following definitions.
- yy_isalpha
- headtype
- headstr
- linknew
- linknewsym
- _lookup
- linklookup
- linkrlookup
- linksymfmt
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <link.h>
static int
yy_isalpha(int c)
{
return c >= 0 && c <= 0xFF && isalpha(c);
}
static struct {
char *name;
int val;
} headers[] = {
"darwin", Hdarwin,
"dragonfly", Hdragonfly,
"elf", Helf,
"freebsd", Hfreebsd,
"linux", Hlinux,
"nacl", Hnacl,
"netbsd", Hnetbsd,
"openbsd", Hopenbsd,
"plan9", Hplan9,
"solaris", Hsolaris,
"windows", Hwindows,
"windowsgui", Hwindows,
0, 0
};
int
headtype(char *name)
{
int i;
for(i=0; headers[i].name; i++)
if(strcmp(name, headers[i].name) == 0)
return headers[i].val;
return -1;
}
char*
headstr(int v)
{
static char buf[20];
int i;
for(i=0; headers[i].name; i++)
if(v == headers[i].val)
return headers[i].name;
snprint(buf, sizeof buf, "%d", v);
return buf;
}
Link*
linknew(LinkArch *arch)
{
Link *ctxt;
char *p;
char buf[1024];
nuxiinit();
ctxt = emallocz(sizeof *ctxt);
ctxt->arch = arch;
ctxt->version = HistVersion;
ctxt->goroot = getgoroot();
ctxt->goroot_final = getenv("GOROOT_FINAL");
if(ctxt->goroot_final != nil && ctxt->goroot_final[0] == '\0')
ctxt->goroot_final = nil;
p = getgoarch();
if(strcmp(p, arch->name) != 0)
sysfatal("invalid goarch %s (want %s)", p, arch->name);
if(getwd(buf, sizeof buf) == 0)
strcpy(buf, "/???");
if(yy_isalpha(buf[0]) && buf[1] == ':') {
ctxt->windows = 1;
for(p=buf; *p; p++)
if(*p == '\\')
*p = '/';
}
ctxt->pathname = strdup(buf);
ctxt->headtype = headtype(getgoos());
if(ctxt->headtype < 0)
sysfatal("unknown goos %s", getgoos());
switch(ctxt->headtype) {
default:
sysfatal("unknown thread-local storage offset for %s", headstr(ctxt->headtype));
case Hplan9:
ctxt->tlsoffset = -2*ctxt->arch->ptrsize;
break;
case Hwindows:
break;
case Hlinux:
case Hfreebsd:
case Hnetbsd:
case Hopenbsd:
case Hdragonfly:
case Hsolaris:
ctxt->tlsoffset = -2*ctxt->arch->ptrsize;
break;
case Hnacl:
switch(ctxt->arch->thechar) {
default:
sysfatal("unknown thread-local storage offset for nacl/%s", ctxt->arch->name);
case '6':
ctxt->tlsoffset = 0;
break;
case '8':
ctxt->tlsoffset = -8;
break;
}
break;
case Hdarwin:
switch(ctxt->arch->thechar) {
default:
sysfatal("unknown thread-local storage offset for darwin/%s", ctxt->arch->name);
case '6':
ctxt->tlsoffset = 0x8a0;
break;
case '8':
ctxt->tlsoffset = 0x468;
break;
}
break;
}
if(ctxt->arch->thechar == '5') {
p = getgoarm();
if(p != nil)
ctxt->goarm = atoi(p);
else
ctxt->goarm = 6;
}
return ctxt;
}
LSym*
linknewsym(Link *ctxt, char *symb, int v)
{
LSym *s;
int l;
l = strlen(symb) + 1;
s = malloc(sizeof(*s));
memset(s, 0, sizeof(*s));
s->dynid = -1;
s->plt = -1;
s->got = -1;
s->name = malloc(l + 1);
memmove(s->name, symb, l);
s->name[l] = '\0';
s->type = 0;
s->version = v;
s->value = 0;
s->sig = 0;
s->size = 0;
ctxt->nsymbol++;
s->allsym = ctxt->allsym;
ctxt->allsym = s;
return s;
}
static LSym*
_lookup(Link *ctxt, char *symb, int v, int creat)
{
LSym *s;
char *p;
uint32 h;
int c;
h = v;
for(p=symb; c = *p; p++)
h = h+h+h + c;
h &= 0xffffff;
h %= LINKHASH;
for(s = ctxt->hash[h]; s != nil; s = s->hash)
if(s->version == v && strcmp(s->name, symb) == 0)
return s;
if(!creat)
return nil;
s = linknewsym(ctxt, symb, v);
s->extname = s->name;
s->hash = ctxt->hash[h];
ctxt->hash[h] = s;
return s;
}
LSym*
linklookup(Link *ctxt, char *name, int v)
{
return _lookup(ctxt, name, v, 1);
}
LSym*
linkrlookup(Link *ctxt, char *name, int v)
{
return _lookup(ctxt, name, v, 0);
}
int
linksymfmt(Fmt *f)
{
LSym *s;
s = va_arg(f->args, LSym*);
if(s == nil)
return fmtstrcpy(f, "<nil>");
return fmtstrcpy(f, s->name);
}