aboutsummaryrefslogtreecommitdiff
path: root/src/liblink
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2014-04-15 20:46:46 -0400
committerRuss Cox <rsc@golang.org>2014-04-15 20:46:46 -0400
commite97b3ab1f942a1bb2812e6b5c890c052903fa0b0 (patch)
tree5cffe3d3307b1b7c67c1e42f079b7c26b5313001 /src/liblink
parentfdade68379abdd9706881f4273e5f8cd9c0eb518 (diff)
downloadgo-e97b3ab1f942a1bb2812e6b5c890c052903fa0b0.tar.xz
build: remove tmp dir names from objects, support GOROOT_FINAL again
If we compile a generated file stored in a temporary directory - let's say /tmp/12345/work/x.c - then by default 6c stores the full path and then the pcln table in the final binary includes the full path. This makes repeated builds (using different temporary directories) produce different binaries, even if the inputs are the same. In the old 'go tool pack', the P flag specified a prefix to remove from all stored paths (if present), and cmd/go invoked 'go tool pack grcP $WORK' to remove references to the temporary work directory. We've changed the build to avoid pack as much as possible, under the theory that instead of making pack convert from .6 to .a, the tools should just write the .a directly and save a round of I/O. Instead of going back to invoking pack always, define a common flag -trimpath in the assemblers, C compilers, and Go compilers, implemented in liblink, and arrange for cmd/go to use the flag. Then the object files being written out have the shortened paths from the start. While we are here, reimplement pcln support for GOROOT_FINAL. A build in /tmp/go uses GOROOT=/tmp/go, but if GOROOT_FINAL=/usr/local/go is set, then a source file named /tmp/go/x.go is recorded instead as /usr/local/go/x.go. We use this so that we can prepare distributions to be installed in /usr/local/go without actually working in that directory. The conversion to liblink deleted all the old file name handling code, including the GOROOT_FINAL translation. Bring the GOROOT_FINAL translation back. Before this CL, using GOROOT_FINAL=/goroot make.bash: g% strings $(which go) | grep -c $TMPDIR 6 g% strings $(which go) | grep -c $GOROOT 793 g% After this CL: g% strings $(which go) | grep -c $TMPDIR 0 g% strings $(which go) | grep -c $GOROOT 0 g% (The references to $TMPDIR tend to be cgo-generated source files.) Adding the -trimpath flag to the assemblers required converting them to the new Go-semantics flag parser. The text in go1.3.html is copied and adjusted from go1.1.html, which is when we applied that conversion to the compilers and linkers. Fixes #6989. LGTM=iant R=r, iant CC=golang-codereviews https://golang.org/cl/88300045
Diffstat (limited to 'src/liblink')
-rw-r--r--src/liblink/obj.c46
-rw-r--r--src/liblink/sym.c4
2 files changed, 49 insertions, 1 deletions
diff --git a/src/liblink/obj.c b/src/liblink/obj.c
index 856227fe83..53ae470354 100644
--- a/src/liblink/obj.c
+++ b/src/liblink/obj.c
@@ -87,6 +87,34 @@ linklinefmt(Link *ctxt, Fmt *fp)
return 0;
}
+// Does s have t as a path prefix?
+// That is, does s == t or does s begin with t followed by a slash?
+// For portability, we allow ASCII case folding, so that haspathprefix("a/b/c", "A/B") is true.
+// Similarly, we allow slash folding, so that haspathprefix("a/b/c", "a\\b") is true.
+static int
+haspathprefix(char *s, char *t)
+{
+ int i, cs, ct;
+
+ if(t == nil)
+ return 0;
+ for(i=0; t[i]; i++) {
+ cs = s[i];
+ ct = t[i];
+ if('A' <= cs && cs <= 'Z')
+ cs += 'a' - 'A';
+ if('A' <= ct && ct <= 'Z')
+ ct += 'a' - 'A';
+ if(cs == '\\')
+ cs = '/';
+ if(ct == '\\')
+ ct = '/';
+ if(cs != ct)
+ return 0;
+ }
+ return s[i] == '\0' || s[i] == '/' || s[i] == '\\';
+}
+
// This is a simplified copy of linklinefmt above.
// It doesn't allow printing the full stack, and it returns the file name and line number separately.
// TODO: Unify with linklinefmt somehow.
@@ -103,7 +131,7 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l)
int32 lno, d, dlno;
int n;
Hist *h;
- char buf[1024], *file;
+ char buf[1024], buf1[1024], *file;
lno = line;
n = 0;
@@ -159,6 +187,22 @@ linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l)
snprint(buf, sizeof buf, "%s", file);
else
snprint(buf, sizeof buf, "%s/%s", ctxt->pathname, file);
+
+ // Remove leading ctxt->trimpath, or else rewrite $GOROOT to $GOROOT_FINAL.
+ if(haspathprefix(buf, ctxt->trimpath)) {
+ if(strlen(buf) == strlen(ctxt->trimpath))
+ strcpy(buf, "??");
+ else {
+ snprint(buf1, sizeof buf1, "%s", buf+strlen(ctxt->trimpath)+1);
+ if(buf1[0] == '\0')
+ strcpy(buf1, "??");
+ strcpy(buf, buf1);
+ }
+ } else if(ctxt->goroot_final != nil && haspathprefix(buf, ctxt->goroot)) {
+ snprint(buf1, sizeof buf1, "%s%s", ctxt->goroot_final, buf+strlen(ctxt->goroot));
+ strcpy(buf, buf1);
+ }
+
lno -= dlno;
*f = linklookup(ctxt, buf, HistVersion);
*l = lno;
diff --git a/src/liblink/sym.c b/src/liblink/sym.c
index 29fc036bcb..ff51b3df89 100644
--- a/src/liblink/sym.c
+++ b/src/liblink/sym.c
@@ -95,6 +95,10 @@ linknew(LinkArch *arch)
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)