From 296d6871a8283cf6dcbaa916526da4608a37397b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 3 May 2011 01:56:23 -0400 Subject: [PATCH] ld: make ELF binaries with no shared library dependencies static binaries $ file $GOROOT/bin/{godoc,goyacc} /home/rsc/g/go/bin/godoc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not strpped /home/rsc/g/go/bin/goyacc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped $ Fixes #1758. R=iant CC=golang-dev https://golang.org/cl/4428079 --- src/cmd/ld/go.c | 2 ++ src/cmd/ld/lib.c | 12 ++++++++++++ src/cmd/ld/lib.h | 1 + 3 files changed, 15 insertions(+) diff --git a/src/cmd/ld/go.c b/src/cmd/ld/go.c index e52c5cb34d3..a19fe460db6 100644 --- a/src/cmd/ld/go.c +++ b/src/cmd/ld/go.c @@ -454,6 +454,7 @@ loaddynimport(char *file, char *pkg, char *p, int n) if(strcmp(name, "_") == 0 && strcmp(def, "_") == 0) { // allow #pragma dynimport _ _ "foo.so" // to force a link of foo.so. + havedynamic = 1; adddynlib(lib); continue; } @@ -468,6 +469,7 @@ loaddynimport(char *file, char *pkg, char *p, int n) s->dynimpname = def; s->dynimpvers = q; s->type = SDYNIMPORT; + havedynamic = 1; } } return; diff --git a/src/cmd/ld/lib.c b/src/cmd/ld/lib.c index 15219ba1165..105d982e4b1 100644 --- a/src/cmd/ld/lib.c +++ b/src/cmd/ld/lib.c @@ -259,6 +259,18 @@ loadlib(void) Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i].file, library[i].objref); objfile(library[i].file, library[i].pkg); } + + // We've loaded all the code now. + // If there are no dynamic libraries needed, gcc disables dynamic linking. + // Because of this, glibc's dynamic ELF loader occasionally (like in version 2.13) + // assumes that a dynamic binary always refers to at least one dynamic library. + // Rather than be a source of test cases for glibc, disable dynamic linking + // the same way that gcc would. + // + // Exception: on OS X, programs such as Shark only work with dynamic + // binaries, so leave it enabled on OS X (Mach-O) binaries. + if(!havedynamic && HEADTYPE != Hdarwin) + debug['d'] = 1; } /* diff --git a/src/cmd/ld/lib.h b/src/cmd/ld/lib.h index 8b603a04a68..447045f01b8 100644 --- a/src/cmd/ld/lib.h +++ b/src/cmd/ld/lib.h @@ -122,6 +122,7 @@ EXTERN char* outfile; EXTERN int32 nsymbol; EXTERN char* thestring; EXTERN int ndynexp; +EXTERN int havedynamic; EXTERN Segment segtext; EXTERN Segment segdata;