1
0
mirror of https://github.com/golang/go synced 2024-10-05 10:31:22 -06:00
go/src/liblink/sym.c

253 lines
5.5 KiB
C
Raw Normal View History

liblink: create new library based on linker code There is an enormous amount of code moving around in this CL, but the code is the same, and it is invoked in the same ways. This CL is preparation for the new linker structure, not the new structure itself. The new library's definition is in include/link.h. The main change is the use of a Link structure to hold all the linker-relevant state, replacing the smattering of global variables. The Link structure should both make it clearer which state must be carried around and make it possible to parallelize more easily later. The main body of the linker has moved into the architecture-independent cmd/ld directory. That includes the list of known header types, so the distinction between Hplan9x32 and Hplan9x64 is removed (no other header type distinguished 32- and 64-bit formats), and code for unused formats such as ipaq kernels has been deleted. The code being deleted from 5l, 6l, and 8l reappears in liblink or in ld. Because multiple files are being merged in the liblink directory, it is not possible to show the diffs nicely in hg. The Prog and Addr structures have been unified into an architecture-independent form and moved to link.h, where they will be shared by all tools: the assemblers, the compilers, and the linkers. The unification makes it possible to write architecture-independent traversal of Prog lists, among other benefits. The Sym structures cannot be unified: they are too fundamentally different between the linker and the compilers. Instead, liblink defines an LSym - a linker Sym - to be used in the Prog and Addr structures, and the linker now refers exclusively to LSyms. The compilers will keep using their own syms but will fill out the corresponding LSyms in the Prog and Addr structures. Although code from 5l, 6l, and 8l is now in a single library, the code has been arranged so that only one architecture needs to be linked into a particular program: 5l will not contain the code needed for x86 instruction layout, for example. The object file writing code in liblink/obj.c is from cmd/gc/obj.c. Preparation for golang.org/s/go13linker work. This CL does not build by itself. It depends on 35740044 and will be submitted at the same time. R=iant CC=golang-dev https://golang.org/cl/35790044
2013-12-08 20:49:37 -07:00
// Derived from Inferno utils/6l/obj.c and utils/6l/span.c
// http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
// http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
//
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
// Portions Copyright © 1997-1999 Vita Nuova Limited
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
// Portions Copyright © 2004,2006 Bruce Ellis
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
// Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#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,
"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;
}
liblink: create new library based on linker code There is an enormous amount of code moving around in this CL, but the code is the same, and it is invoked in the same ways. This CL is preparation for the new linker structure, not the new structure itself. The new library's definition is in include/link.h. The main change is the use of a Link structure to hold all the linker-relevant state, replacing the smattering of global variables. The Link structure should both make it clearer which state must be carried around and make it possible to parallelize more easily later. The main body of the linker has moved into the architecture-independent cmd/ld directory. That includes the list of known header types, so the distinction between Hplan9x32 and Hplan9x64 is removed (no other header type distinguished 32- and 64-bit formats), and code for unused formats such as ipaq kernels has been deleted. The code being deleted from 5l, 6l, and 8l reappears in liblink or in ld. Because multiple files are being merged in the liblink directory, it is not possible to show the diffs nicely in hg. The Prog and Addr structures have been unified into an architecture-independent form and moved to link.h, where they will be shared by all tools: the assemblers, the compilers, and the linkers. The unification makes it possible to write architecture-independent traversal of Prog lists, among other benefits. The Sym structures cannot be unified: they are too fundamentally different between the linker and the compilers. Instead, liblink defines an LSym - a linker Sym - to be used in the Prog and Addr structures, and the linker now refers exclusively to LSyms. The compilers will keep using their own syms but will fill out the corresponding LSyms in the Prog and Addr structures. Although code from 5l, 6l, and 8l is now in a single library, the code has been arranged so that only one architecture needs to be linked into a particular program: 5l will not contain the code needed for x86 instruction layout, for example. The object file writing code in liblink/obj.c is from cmd/gc/obj.c. Preparation for golang.org/s/go13linker work. This CL does not build by itself. It depends on 35740044 and will be submitted at the same time. R=iant CC=golang-dev https://golang.org/cl/35790044
2013-12-08 20:49:37 -07:00
Link*
linknew(LinkArch *arch)
{
Link *ctxt;
char *p;
char buf[1024];
nuxiinit();
ctxt = emallocz(sizeof *ctxt);
ctxt->arch = arch;
ctxt->version = HistVersion;
p = getgoarch();
if(strncmp(p, arch->name, strlen(arch->name)) != 0)
sysfatal("invalid goarch %s (want %s or derivative)", p, arch->name);
if(getwd(buf, sizeof buf) == 0)
strcpy(buf, "/???");
if(yy_isalpha(buf[0]) && buf[1] == ':') {
// On Windows.
ctxt->windows = 1;
// Canonicalize path by converting \ to / (Windows accepts both).
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());
// Record thread-local storage offset.
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:
/*
* ELF uses TLS offset negative from FS.
* Translate 0(FS) and 8(FS) into -16(FS) and -8(FS).
* Known to low-level assembly in package runtime and runtime/cgo.
*/
ctxt->tlsoffset = -2*ctxt->arch->ptrsize;
break;
case Hdarwin:
/*
* OS X system constants - offset from 0(GS) to our TLS.
* Explained in ../../pkg/runtime/cgo/gcc_darwin_*.c.
*/
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;
}
// On arm, record goarm.
if(ctxt->arch->thechar == '5') {
p = getgoarm();
if(p != nil)
ctxt->goarm = atoi(p);
else
ctxt->goarm = 6;
}
liblink: create new library based on linker code There is an enormous amount of code moving around in this CL, but the code is the same, and it is invoked in the same ways. This CL is preparation for the new linker structure, not the new structure itself. The new library's definition is in include/link.h. The main change is the use of a Link structure to hold all the linker-relevant state, replacing the smattering of global variables. The Link structure should both make it clearer which state must be carried around and make it possible to parallelize more easily later. The main body of the linker has moved into the architecture-independent cmd/ld directory. That includes the list of known header types, so the distinction between Hplan9x32 and Hplan9x64 is removed (no other header type distinguished 32- and 64-bit formats), and code for unused formats such as ipaq kernels has been deleted. The code being deleted from 5l, 6l, and 8l reappears in liblink or in ld. Because multiple files are being merged in the liblink directory, it is not possible to show the diffs nicely in hg. The Prog and Addr structures have been unified into an architecture-independent form and moved to link.h, where they will be shared by all tools: the assemblers, the compilers, and the linkers. The unification makes it possible to write architecture-independent traversal of Prog lists, among other benefits. The Sym structures cannot be unified: they are too fundamentally different between the linker and the compilers. Instead, liblink defines an LSym - a linker Sym - to be used in the Prog and Addr structures, and the linker now refers exclusively to LSyms. The compilers will keep using their own syms but will fill out the corresponding LSyms in the Prog and Addr structures. Although code from 5l, 6l, and 8l is now in a single library, the code has been arranged so that only one architecture needs to be linked into a particular program: 5l will not contain the code needed for x86 instruction layout, for example. The object file writing code in liblink/obj.c is from cmd/gc/obj.c. Preparation for golang.org/s/go13linker work. This CL does not build by itself. It depends on 35740044 and will be submitted at the same time. R=iant CC=golang-dev https://golang.org/cl/35790044
2013-12-08 20:49:37 -07:00
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(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);
}
// read-only lookup
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);
}