From 7d507dc6e608e800bc26a5850259bbdb05abdf65 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sun, 8 Dec 2013 22:49:37 -0500 Subject: 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 --- src/liblink/obj.c | 403 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 403 insertions(+) create mode 100644 src/liblink/obj.c (limited to 'src/liblink/obj.c') diff --git a/src/liblink/obj.c b/src/liblink/obj.c new file mode 100644 index 0000000000..eacbc4011a --- /dev/null +++ b/src/liblink/obj.c @@ -0,0 +1,403 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +#include +#include +#include +#include + +enum +{ + HISTSZ = 10, + NSYM = 50, +}; + +int +linklinefmt(Link *ctxt, Fmt *fp) +{ + struct + { + Hist* incl; /* start of this include file */ + int32 idel; /* delta line number to apply to include */ + Hist* line; /* start of this #line directive */ + int32 ldel; /* delta line number to apply to #line */ + } a[HISTSZ]; + int32 lno, d; + int i, n; + Hist *h; + + lno = va_arg(fp->args, int32); + + n = 0; + for(h=ctxt->hist; h!=nil; h=h->link) { + if(h->offset < 0) + continue; + if(lno < h->line) + break; + if(h->name) { + if(h->offset > 0) { + // #line directive + if(n > 0 && n < HISTSZ) { + a[n-1].line = h; + a[n-1].ldel = h->line - h->offset + 1; + } + } else { + // beginning of file + if(n < HISTSZ) { + a[n].incl = h; + a[n].idel = h->line; + a[n].line = 0; + } + n++; + } + continue; + } + n--; + if(n > 0 && n < HISTSZ) { + d = h->line - a[n].incl->line; + a[n-1].ldel += d; + a[n-1].idel += d; + } + } + + if(n > HISTSZ) + n = HISTSZ; + + for(i=n-1; i>=0; i--) { + if(i != n-1) { + if(fp->flags & ~(FmtWidth|FmtPrec)) + break; + fmtprint(fp, " "); + } + if(ctxt->debugline || (fp->flags&FmtLong)) + fmtprint(fp, "%s/", ctxt->pathname); + if(a[i].line) + fmtprint(fp, "%s:%d[%s:%d]", + a[i].line->name, lno-a[i].ldel+1, + a[i].incl->name, lno-a[i].idel+1); + else + fmtprint(fp, "%s:%d", + a[i].incl->name, lno-a[i].idel+1); + lno = a[i].incl->line - 1; // now print out start of this file + } + if(n == 0) + fmtprint(fp, ""); + + return 0; +} + +static void +outzfile(Link *ctxt, Biobuf *b, char *p) +{ + char *q, *q2; + + while(p) { + q = utfrune(p, '/'); + if(ctxt->windows) { + q2 = utfrune(p, '\\'); + if(q2 && (!q || q2 < q)) + q = q2; + } + if(!q) { + ctxt->arch->zfile(b, p, strlen(p)); + return; + } + if(q > p) + ctxt->arch->zfile(b, p, q-p); + p = q + 1; + } +} + +#define isdelim(c) (c == '/' || c == '\\') + +static void +outwinname(Link *ctxt, Biobuf *b, Hist *h, char *ds, char *p) +{ + if(isdelim(p[0])) { + // full rooted name + ctxt->arch->zfile(b, ds, 3); // leading "c:/" + outzfile(ctxt, b, p+1); + } else { + // relative name + if(h->offset >= 0 && ctxt->pathname && ctxt->pathname[1] == ':') { + if(tolowerrune(ds[0]) == tolowerrune(ctxt->pathname[0])) { + // using current drive + ctxt->arch->zfile(b, ctxt->pathname, 3); // leading "c:/" + outzfile(ctxt, b, ctxt->pathname+3); + } else { + // using drive other then current, + // we don't have any simple way to + // determine current working directory + // there, therefore will output name as is + ctxt->arch->zfile(b, ds, 2); // leading "c:" + } + } + outzfile(ctxt, b, p); + } +} + +void +linkouthist(Link *ctxt, Biobuf *b) +{ + Hist *h; + char *p, ds[] = {'c', ':', '/', 0}; + char *tofree; + int n; + static int first = 1; + static char *goroot, *goroot_final; + + if(first) { + // Decide whether we need to rewrite paths from $GOROOT to $GOROOT_FINAL. + first = 0; + goroot = getenv("GOROOT"); + goroot_final = getenv("GOROOT_FINAL"); + if(goroot == nil) + goroot = ""; + if(goroot_final == nil) + goroot_final = goroot; + if(strcmp(goroot, goroot_final) == 0) { + goroot = nil; + goroot_final = nil; + } + } + + tofree = nil; + for(h = ctxt->hist; h != nil; h = h->link) { + p = h->name; + if(p) { + if(goroot != nil) { + n = strlen(goroot); + if(strncmp(p, goroot, strlen(goroot)) == 0 && p[n] == '/') { + tofree = smprint("%s%s", goroot_final, p+n); + p = tofree; + } + } + if(ctxt->windows) { + // if windows variable is set, then, we know already, + // pathname is started with windows drive specifier + // and all '\' were replaced with '/' (see lex.c) + if(isdelim(p[0]) && isdelim(p[1])) { + // file name has network name in it, + // like \\server\share\dir\file.go + ctxt->arch->zfile(b, "//", 2); // leading "//" + outzfile(ctxt, b, p+2); + } else if(p[1] == ':') { + // file name has drive letter in it + ds[0] = p[0]; + outwinname(ctxt, b, h, ds, p+2); + } else { + // no drive letter in file name + outwinname(ctxt, b, h, ctxt->pathname, p); + } + } else { + if(p[0] == '/') { + // full rooted name, like /home/rsc/dir/file.go + ctxt->arch->zfile(b, "/", 1); // leading "/" + outzfile(ctxt, b, p+1); + } else { + // relative name, like dir/file.go + if(h->offset >= 0 && ctxt->pathname && ctxt->pathname[0] == '/') { + ctxt->arch->zfile(b, "/", 1); // leading "/" + outzfile(ctxt, b, ctxt->pathname+1); + } + outzfile(ctxt, b, p); + } + } + } + ctxt->arch->zhist(b, h->line, h->offset); + if(tofree) { + free(tofree); + tofree = nil; + } + } +} + +void +linklinehist(Link *ctxt, int lineno, char *f, int offset) +{ + Hist *h; + + if(0) // debug['f'] + if(f) { + if(offset) + print("%4d: %s (#line %d)\n", lineno, f, offset); + else + print("%4d: %s\n", lineno, f); + } else + print("%4d: \n", lineno); + + h = malloc(sizeof(Hist)); + memset(h, 0, sizeof *h); + h->name = f; + h->line = lineno; + h->offset = offset; + h->link = nil; + if(ctxt->ehist == nil) { + ctxt->hist = h; + ctxt->ehist = h; + return; + } + ctxt->ehist->link = h; + ctxt->ehist = h; +} + +void +linkprfile(Link *ctxt, int32 l) +{ + int i, n; + Hist a[HISTSZ], *h; + int32 d; + + n = 0; + for(h = ctxt->hist; h != nil; h = h->link) { + if(l < h->line) + break; + if(h->name) { + if(h->offset == 0) { + if(n >= 0 && n < HISTSZ) + a[n] = *h; + n++; + continue; + } + if(n > 0 && n < HISTSZ) + if(a[n-1].offset == 0) { + a[n] = *h; + n++; + } else + a[n-1] = *h; + continue; + } + n--; + if(n >= 0 && n < HISTSZ) { + d = h->line - a[n].line; + for(i=0; i HISTSZ) + n = HISTSZ; + for(i=0; iplist == nil) + ctxt->plist = pl; + else + ctxt->plast->link = pl; + ctxt->plast = pl; + + return pl; +} + +static struct { + struct { LSym *sym; short type; } h[NSYM]; + int sym; +} z; + +static void +zsymreset(void) +{ + for(z.sym=0; z.symsymid; + if(i < 0 || i >= NSYM) + i = 0; + if(z.h[i].type == t && z.h[i].sym == s) + return i; + i = z.sym; + s->symid = i; + ctxt->arch->zname(b, s, t); + z.h[i].sym = s; + z.h[i].type = t; + if(++z.sym >= NSYM) + z.sym = 1; + *new = 1; + return i; +} + +static int +zsymaddr(Link *ctxt, Biobuf *b, Addr *a, int *new) +{ + return zsym(ctxt, b, a->sym, ctxt->arch->symtype(a), new); +} + +void +linkwritefuncs(Link *ctxt, Biobuf *b) +{ + int32 pcloc; + Plist *pl; + LSym *s; + Prog *p; + int sf, st, gf, gt, new; + + zsymreset(); + + // fix up pc + pcloc = 0; + for(pl=ctxt->plist; pl!=nil; pl=pl->link) { + if(pl->name != nil && strcmp(pl->name->name, "_") == 0) + continue; + for(p=pl->firstpc; p!=nil; p=p->link) { + p->loc = pcloc; + if(!ctxt->arch->isdata(p)) + pcloc++; + } + } + + // put out functions + for(pl=ctxt->plist; pl!=nil; pl=pl->link) { + if(pl->name != nil && strcmp(pl->name->name, "_") == 0) + continue; + + // -S prints code; -S -S prints code and data + if(ctxt->debugasm && (pl->name || ctxt->debugasm>1)) { + s = pl->name; + print("\n--- prog list \"%lS\" ---\n", s); + for(p=pl->firstpc; p!=nil; p=p->link) + print("%P\n", p); + } + + for(p=pl->firstpc; p!=nil; p=p->link) { + for(;;) { + sf = zsymaddr(ctxt, b, &p->from, &new); + gf = zsym(ctxt, b, p->from.gotype, ctxt->arch->D_EXTERN, &new); + if(new && sf == gf) + continue; + st = zsymaddr(ctxt, b, &p->to, &new); + if(new && (st == sf || st == gf)) + continue; + gt = zsym(ctxt, b, p->to.gotype, ctxt->arch->D_EXTERN, &new); + if(new && (gt == sf || gt == gf || gt == st)) + continue; + break; + } + ctxt->arch->zprog(ctxt, b, p, sf, gf, st, gt); + } + } +} -- cgit v1.3