github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/cmd/ld/lib.c (about)

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
     3  // http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
     4  //
     5  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     6  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     7  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     8  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     9  //	Portions Copyright © 2004,2006 Bruce Ellis
    10  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    11  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    12  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    13  //
    14  // Permission is hereby granted, free of charge, to any person obtaining a copy
    15  // of this software and associated documentation files (the "Software"), to deal
    16  // in the Software without restriction, including without limitation the rights
    17  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    18  // copies of the Software, and to permit persons to whom the Software is
    19  // furnished to do so, subject to the following conditions:
    20  //
    21  // The above copyright notice and this permission notice shall be included in
    22  // all copies or substantial portions of the Software.
    23  //
    24  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    25  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    26  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    27  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    28  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    29  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    30  // THE SOFTWARE.
    31  
    32  #include	"l.h"
    33  #include	"lib.h"
    34  #include	"../ld/elf.h"
    35  #include	"../ld/dwarf.h"
    36  #include	"../../runtime/stack.h"
    37  #include	"../../runtime/funcdata.h"
    38  
    39  #include	<ar.h>
    40  #if !(defined(_WIN32) || defined(PLAN9))
    41  #include	<sys/stat.h>
    42  #endif
    43  
    44  enum
    45  {
    46  	// Whether to assume that the external linker is "gold"
    47  	// (http://sourceware.org/ml/binutils/2008-03/msg00162.html).
    48  	AssumeGoldLinker = 0,
    49  };
    50  
    51  int iconv(Fmt*);
    52  
    53  char	symname[]	= SYMDEF;
    54  char	pkgname[]	= "__.PKGDEF";
    55  static int	cout = -1;
    56  
    57  extern int	version;
    58  
    59  // Set if we see an object compiled by the host compiler that is not
    60  // from a package that is known to support internal linking mode.
    61  static int	externalobj = 0;
    62  
    63  static	void	hostlinksetup(void);
    64  
    65  char*	goroot;
    66  char*	goarch;
    67  char*	goos;
    68  char*	theline;
    69  
    70  void
    71  Lflag(char *arg)
    72  {
    73  	char **p;
    74  
    75  	if(ctxt->nlibdir >= ctxt->maxlibdir) {
    76  		if (ctxt->maxlibdir == 0)
    77  			ctxt->maxlibdir = 8;
    78  		else
    79  			ctxt->maxlibdir *= 2;
    80  		p = erealloc(ctxt->libdir, ctxt->maxlibdir * sizeof(*p));
    81  		ctxt->libdir = p;
    82  	}
    83  	ctxt->libdir[ctxt->nlibdir++] = arg;
    84  }
    85  
    86  /*
    87   * Unix doesn't like it when we write to a running (or, sometimes,
    88   * recently run) binary, so remove the output file before writing it.
    89   * On Windows 7, remove() can force a subsequent create() to fail.
    90   * S_ISREG() does not exist on Plan 9.
    91   */
    92  static void
    93  mayberemoveoutfile(void) 
    94  {
    95  #if !(defined(_WIN32) || defined(PLAN9))
    96  	struct stat st;
    97  	if(lstat(outfile, &st) == 0 && !S_ISREG(st.st_mode))
    98  		return;
    99  #endif
   100  	remove(outfile);
   101  }
   102  
   103  void
   104  libinit(void)
   105  {
   106  	char *suffix, *suffixsep;
   107  
   108  	funcalign = FuncAlign;
   109  	fmtinstall('i', iconv);
   110  	fmtinstall('Y', Yconv);
   111  	fmtinstall('Z', Zconv);
   112  	mywhatsys();	// get goroot, goarch, goos
   113  
   114  	// add goroot to the end of the libdir list.
   115  	suffix = "";
   116  	suffixsep = "";
   117  	if(flag_installsuffix != nil) {
   118  		suffixsep = "_";
   119  		suffix = flag_installsuffix;
   120  	} else if(flag_race) {
   121  		suffixsep = "_";
   122  		suffix = "race";
   123  	}
   124  	Lflag(smprint("%s/pkg/%s_%s%s%s", goroot, goos, goarch, suffixsep, suffix));
   125  
   126  	mayberemoveoutfile();
   127  	cout = create(outfile, 1, 0775);
   128  	if(cout < 0) {
   129  		diag("cannot create %s: %r", outfile);
   130  		errorexit();
   131  	}
   132  
   133  	if(INITENTRY == nil) {
   134  		INITENTRY = mal(strlen(goarch)+strlen(goos)+20);
   135  		if(!flag_shared) {
   136  			sprint(INITENTRY, "_rt0_%s_%s", goarch, goos);
   137  		} else {
   138  			sprint(INITENTRY, "_rt0_%s_%s_lib", goarch, goos);
   139  		}
   140  	}
   141  	linklookup(ctxt, INITENTRY, 0)->type = SXREF;
   142  }
   143  
   144  void
   145  errorexit(void)
   146  {
   147  	if(cout >= 0) {
   148  		// For rmtemp run at atexit time on Windows.
   149  		close(cout);
   150  	}
   151  	if(nerrors) {
   152  		if(cout >= 0)
   153  			mayberemoveoutfile();
   154  		exits("error");
   155  	}
   156  	exits(0);
   157  }
   158  
   159  void
   160  loadinternal(char *name)
   161  {
   162  	char pname[1024];
   163  	int i, found;
   164  
   165  	found = 0;
   166  	for(i=0; i<ctxt->nlibdir; i++) {
   167  		snprint(pname, sizeof pname, "%s/%s.a", ctxt->libdir[i], name);
   168  		if(debug['v'])
   169  			Bprint(&bso, "searching for %s.a in %s\n", name, pname);
   170  		if(access(pname, AEXIST) >= 0) {
   171  			addlibpath(ctxt, "internal", "internal", pname, name);
   172  			found = 1;
   173  			break;
   174  		}
   175  	}
   176  	if(!found)
   177  		Bprint(&bso, "warning: unable to find %s.a\n", name);
   178  }
   179  
   180  void
   181  loadlib(void)
   182  {
   183  	int i, w, x;
   184  	LSym *s, *tlsg;
   185  	char* cgostrsym;
   186  
   187  	if(flag_shared) {
   188  		s = linklookup(ctxt, "runtime.islibrary", 0);
   189  		s->dupok = 1;
   190  		adduint8(ctxt, s, 1);
   191  	}
   192  
   193  	loadinternal("runtime");
   194  	if(thechar == '5')
   195  		loadinternal("math");
   196  	if(flag_race)
   197  		loadinternal("runtime/race");
   198  
   199  	for(i=0; i<ctxt->libraryp; i++) {
   200  		if(debug['v'] > 1)
   201  			Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), ctxt->library[i].file, ctxt->library[i].objref);
   202  		iscgo |= strcmp(ctxt->library[i].pkg, "runtime/cgo") == 0;
   203  		objfile(ctxt->library[i].file, ctxt->library[i].pkg);
   204  	}
   205  
   206  	if(linkmode == LinkAuto) {
   207  		if(iscgo && externalobj)
   208  			linkmode = LinkExternal;
   209  		else
   210  			linkmode = LinkInternal;
   211  
   212  		// Force external linking for android.
   213  		if(strcmp(goos, "android") == 0)
   214  			linkmode = LinkExternal;
   215  	}
   216  
   217  	if(linkmode == LinkExternal && !iscgo) {
   218  		// This indicates a user requested -linkmode=external.
   219  		// The startup code uses an import of runtime/cgo to decide
   220  		// whether to initialize the TLS.  So give it one.  This could
   221  		// be handled differently but it's an unusual case.
   222  		loadinternal("runtime/cgo");
   223  		if(i < ctxt->libraryp)
   224  			objfile(ctxt->library[i].file, ctxt->library[i].pkg);
   225  
   226  		// Pretend that we really imported the package.
   227  		s = linklookup(ctxt, "go.importpath.runtime/cgo.", 0);
   228  		s->type = SDATA;
   229  		s->dupok = 1;
   230  		s->reachable = 1;
   231  
   232  		// Provided by the code that imports the package.
   233  		// Since we are simulating the import, we have to provide this string.
   234  		cgostrsym = "go.string.\"runtime/cgo\"";
   235  		if(linkrlookup(ctxt, cgostrsym, 0) == nil) {
   236  			s = linklookup(ctxt, cgostrsym, 0);
   237  			s->type = SRODATA;
   238  			s->reachable = 1;
   239  			addstrdata(cgostrsym, "runtime/cgo");
   240  		}
   241  	}
   242  
   243  	if(linkmode == LinkInternal) {
   244  		// Drop all the cgo_import_static declarations.
   245  		// Turns out we won't be needing them.
   246  		for(s = ctxt->allsym; s != S; s = s->allsym)
   247  			if(s->type == SHOSTOBJ) {
   248  				// If a symbol was marked both
   249  				// cgo_import_static and cgo_import_dynamic,
   250  				// then we want to make it cgo_import_dynamic
   251  				// now.
   252  				if(s->extname != nil && s->dynimplib != nil && s->cgoexport == 0) {
   253  					s->type = SDYNIMPORT;
   254  				} else
   255  					s->type = 0;
   256  			}
   257  	}
   258  	
   259  	tlsg = linklookup(ctxt, "runtime.tlsg", 0);
   260  	tlsg->type = STLSBSS;
   261  	tlsg->size = PtrSize;
   262  	tlsg->hide = 1;
   263  	tlsg->reachable = 1;
   264  	ctxt->tlsg = tlsg;
   265  
   266  	// Now that we know the link mode, trim the dynexp list.
   267  	x = CgoExportDynamic;
   268  	if(linkmode == LinkExternal)
   269  		x = CgoExportStatic;
   270  	w = 0;
   271  	for(i=0; i<ndynexp; i++)
   272  		if(dynexp[i]->cgoexport & x)
   273  			dynexp[w++] = dynexp[i];
   274  	ndynexp = w;
   275  	
   276  	// In internal link mode, read the host object files.
   277  	if(linkmode == LinkInternal)
   278  		hostobjs();
   279  	else
   280  		hostlinksetup();
   281  
   282  	// We've loaded all the code now.
   283  	// If there are no dynamic libraries needed, gcc disables dynamic linking.
   284  	// Because of this, glibc's dynamic ELF loader occasionally (like in version 2.13)
   285  	// assumes that a dynamic binary always refers to at least one dynamic library.
   286  	// Rather than be a source of test cases for glibc, disable dynamic linking
   287  	// the same way that gcc would.
   288  	//
   289  	// Exception: on OS X, programs such as Shark only work with dynamic
   290  	// binaries, so leave it enabled on OS X (Mach-O) binaries.
   291  	// Also leave it enabled on Solaris which doesn't support
   292  	// statically linked binaries.
   293  	if(!flag_shared && !havedynamic && HEADTYPE != Hdarwin && HEADTYPE != Hsolaris)
   294  		debug['d'] = 1;
   295  	
   296  	importcycles();
   297  }
   298  
   299  /*
   300   * look for the next file in an archive.
   301   * adapted from libmach.
   302   */
   303  static vlong
   304  nextar(Biobuf *bp, vlong off, struct ar_hdr *a)
   305  {
   306  	int r;
   307  	int32 arsize;
   308  	char *buf;
   309  
   310  	if (off&01)
   311  		off++;
   312  	Bseek(bp, off, 0);
   313  	buf = Brdline(bp, '\n');
   314  	r = Blinelen(bp);
   315  	if(buf == nil) {
   316  		if(r == 0)
   317  			return 0;
   318  		return -1;
   319  	}
   320  	if(r != SAR_HDR)
   321  		return -1;
   322  	memmove(a, buf, SAR_HDR);
   323  	if(strncmp(a->fmag, ARFMAG, sizeof a->fmag))
   324  		return -1;
   325  	arsize = strtol(a->size, 0, 0);
   326  	if (arsize&1)
   327  		arsize++;
   328  	return arsize + r;
   329  }
   330  
   331  void
   332  objfile(char *file, char *pkg)
   333  {
   334  	vlong off, l;
   335  	Biobuf *f;
   336  	char magbuf[SARMAG];
   337  	char pname[150];
   338  	struct ar_hdr arhdr;
   339  
   340  	pkg = smprint("%i", pkg);
   341  
   342  	if(debug['v'] > 1)
   343  		Bprint(&bso, "%5.2f ldobj: %s (%s)\n", cputime(), file, pkg);
   344  	Bflush(&bso);
   345  	f = Bopen(file, 0);
   346  	if(f == nil) {
   347  		diag("cannot open file: %s", file);
   348  		errorexit();
   349  	}
   350  	l = Bread(f, magbuf, SARMAG);
   351  	if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){
   352  		/* load it as a regular file */
   353  		l = Bseek(f, 0L, 2);
   354  		Bseek(f, 0L, 0);
   355  		ldobj(f, pkg, l, file, file, FileObj);
   356  		Bterm(f);
   357  		free(pkg);
   358  		return;
   359  	}
   360  	
   361  	/* skip over optional __.GOSYMDEF and process __.PKGDEF */
   362  	off = Boffset(f);
   363  	l = nextar(f, off, &arhdr);
   364  	if(l <= 0) {
   365  		diag("%s: short read on archive file symbol header", file);
   366  		goto out;
   367  	}
   368  	if(strncmp(arhdr.name, symname, strlen(symname)) == 0) {
   369  		off += l;
   370  		l = nextar(f, off, &arhdr);
   371  		if(l <= 0) {
   372  			diag("%s: short read on archive file symbol header", file);
   373  			goto out;
   374  		}
   375  	}
   376  
   377  	if(strncmp(arhdr.name, pkgname, strlen(pkgname))) {
   378  		diag("%s: cannot find package header", file);
   379  		goto out;
   380  	}
   381  	off += l;
   382  
   383  	if(debug['u'])
   384  		ldpkg(f, pkg, atolwhex(arhdr.size), file, Pkgdef);
   385  
   386  	/*
   387  	 * load all the object files from the archive now.
   388  	 * this gives us sequential file access and keeps us
   389  	 * from needing to come back later to pick up more
   390  	 * objects.  it breaks the usual C archive model, but
   391  	 * this is Go, not C.  the common case in Go is that
   392  	 * we need to load all the objects, and then we throw away
   393  	 * the individual symbols that are unused.
   394  	 *
   395  	 * loading every object will also make it possible to
   396  	 * load foreign objects not referenced by __.GOSYMDEF.
   397  	 */
   398  	for(;;) {
   399  		l = nextar(f, off, &arhdr);
   400  		if(l == 0)
   401  			break;
   402  		if(l < 0) {
   403  			diag("%s: malformed archive", file);
   404  			goto out;
   405  		}
   406  		off += l;
   407  
   408  		l = SARNAME;
   409  		while(l > 0 && arhdr.name[l-1] == ' ')
   410  			l--;
   411  		snprint(pname, sizeof pname, "%s(%.*s)", file, utfnlen(arhdr.name, l), arhdr.name);
   412  		l = atolwhex(arhdr.size);
   413  		ldobj(f, pkg, l, pname, file, ArchiveObj);
   414  	}
   415  
   416  out:
   417  	Bterm(f);
   418  	free(pkg);
   419  }
   420  
   421  static void
   422  dowrite(int fd, char *p, int n)
   423  {
   424  	int m;
   425  	
   426  	while(n > 0) {
   427  		m = write(fd, p, n);
   428  		if(m <= 0) {
   429  			ctxt->cursym = S;
   430  			diag("write error: %r");
   431  			errorexit();
   432  		}
   433  		n -= m;
   434  		p += m;
   435  	}
   436  }
   437  
   438  typedef struct Hostobj Hostobj;
   439  
   440  struct Hostobj
   441  {
   442  	void (*ld)(Biobuf*, char*, int64, char*);
   443  	char *pkg;
   444  	char *pn;
   445  	char *file;
   446  	int64 off;
   447  	int64 len;
   448  };
   449  
   450  Hostobj *hostobj;
   451  int nhostobj;
   452  int mhostobj;
   453  
   454  // These packages can use internal linking mode.
   455  // Others trigger external mode.
   456  const char *internalpkg[] = {
   457  	"crypto/x509",
   458  	"net",
   459  	"os/user",
   460  	"runtime/cgo",
   461  	"runtime/race"
   462  };
   463  
   464  void
   465  ldhostobj(void (*ld)(Biobuf*, char*, int64, char*), Biobuf *f, char *pkg, int64 len, char *pn, char *file)
   466  {
   467  	int i, isinternal;
   468  	Hostobj *h;
   469  
   470  	isinternal = 0;
   471  	for(i=0; i<nelem(internalpkg); i++) {
   472  		if(strcmp(pkg, internalpkg[i]) == 0) {
   473  			isinternal = 1;
   474  			break;
   475  		}
   476  	}
   477  
   478  	// DragonFly declares errno with __thread, which results in a symbol
   479  	// type of R_386_TLS_GD or R_X86_64_TLSGD. The Go linker does not
   480  	// currently know how to handle TLS relocations, hence we have to
   481  	// force external linking for any libraries that link in code that
   482  	// uses errno. This can be removed if the Go linker ever supports
   483  	// these relocation types.
   484  	if(HEADTYPE == Hdragonfly)
   485  	if(strcmp(pkg, "net") == 0 || strcmp(pkg, "os/user") == 0)
   486  		isinternal = 0;
   487  
   488  	if(!isinternal)
   489  		externalobj = 1;
   490  
   491  	if(nhostobj >= mhostobj) {
   492  		if(mhostobj == 0)
   493  			mhostobj = 16;
   494  		else
   495  			mhostobj *= 2;
   496  		hostobj = erealloc(hostobj, mhostobj*sizeof hostobj[0]);
   497  	}
   498  	h = &hostobj[nhostobj++];
   499  	h->ld = ld;
   500  	h->pkg = estrdup(pkg);
   501  	h->pn = estrdup(pn);
   502  	h->file = estrdup(file);
   503  	h->off = Boffset(f);
   504  	h->len = len;
   505  }
   506  
   507  void
   508  hostobjs(void)
   509  {
   510  	int i;
   511  	Biobuf *f;
   512  	Hostobj *h;
   513  	
   514  	for(i=0; i<nhostobj; i++) {
   515  		h = &hostobj[i];
   516  		f = Bopen(h->file, OREAD);
   517  		if(f == nil) {
   518  			ctxt->cursym = S;
   519  			diag("cannot reopen %s: %r", h->pn);
   520  			errorexit();
   521  		}
   522  		Bseek(f, h->off, 0);
   523  		h->ld(f, h->pkg, h->len, h->pn);
   524  		Bterm(f);
   525  	}
   526  }
   527  
   528  // provided by lib9
   529  int runcmd(char**);
   530  char* mktempdir(void);
   531  void removeall(char*);
   532  
   533  static void
   534  rmtemp(void)
   535  {
   536  	removeall(tmpdir);
   537  }
   538  
   539  static void
   540  hostlinksetup(void)
   541  {
   542  	char *p;
   543  
   544  	if(linkmode != LinkExternal)
   545  		return;
   546  
   547  	// create temporary directory and arrange cleanup
   548  	if(tmpdir == nil) {
   549  		tmpdir = mktempdir();
   550  		atexit(rmtemp);
   551  	}
   552  
   553  	// change our output to temporary object file
   554  	close(cout);
   555  	p = smprint("%s/go.o", tmpdir);
   556  	cout = create(p, 1, 0775);
   557  	if(cout < 0) {
   558  		diag("cannot create %s: %r", p);
   559  		errorexit();
   560  	}
   561  	free(p);
   562  }
   563  
   564  void
   565  hostlink(void)
   566  {
   567  	char *p, **argv;
   568  	int c, i, w, n, argc, len;
   569  	Hostobj *h;
   570  	Biobuf *f;
   571  	static char buf[64<<10];
   572  
   573  	if(linkmode != LinkExternal || nerrors > 0)
   574  		return;
   575  
   576  	c = 0;
   577  	p = extldflags;
   578  	while(p != nil) {
   579  		while(*p == ' ')
   580  			p++;
   581  		if(*p == '\0')
   582  			break;
   583  		c++;
   584  		p = strchr(p + 1, ' ');
   585  	}
   586  
   587  	argv = malloc((14+nhostobj+nldflag+c)*sizeof argv[0]);
   588  	argc = 0;
   589  	if(extld == nil)
   590  		extld = "gcc";
   591  	argv[argc++] = extld;
   592  	switch(thechar){
   593  	case '8':
   594  		argv[argc++] = "-m32";
   595  		break;
   596  	case '6':
   597  	case '9':
   598  		argv[argc++] = "-m64";
   599  		break;
   600  	case '5':
   601  		argv[argc++] = "-marm";
   602  		break;
   603  	}
   604  	if(!debug['s'] && !debug_s) {
   605  		argv[argc++] = "-gdwarf-2"; 
   606  	} else {
   607  		argv[argc++] = "-s";
   608  	}
   609  	if(HEADTYPE == Hdarwin)
   610  		argv[argc++] = "-Wl,-no_pie,-pagezero_size,4000000";
   611  	if(HEADTYPE == Hopenbsd)
   612  		argv[argc++] = "-Wl,-nopie";
   613  	
   614  	if(iself && AssumeGoldLinker)
   615  		argv[argc++] = "-Wl,--rosegment";
   616  
   617  	if(flag_shared) {
   618  		argv[argc++] = "-Wl,-Bsymbolic";
   619  		argv[argc++] = "-shared";
   620  	}
   621  	argv[argc++] = "-o";
   622  	argv[argc++] = outfile;
   623  	
   624  	if(rpath)
   625  		argv[argc++] = smprint("-Wl,-rpath,%s", rpath);
   626  
   627  	// Force global symbols to be exported for dlopen, etc.
   628  	if(iself)
   629  		argv[argc++] = "-rdynamic";
   630  
   631  	if(strstr(argv[0], "clang") != nil)
   632  		argv[argc++] = "-Qunused-arguments";
   633  
   634  	// already wrote main object file
   635  	// copy host objects to temporary directory
   636  	for(i=0; i<nhostobj; i++) {
   637  		h = &hostobj[i];
   638  		f = Bopen(h->file, OREAD);
   639  		if(f == nil) {
   640  			ctxt->cursym = S;
   641  			diag("cannot reopen %s: %r", h->pn);
   642  			errorexit();
   643  		}
   644  		Bseek(f, h->off, 0);
   645  		p = smprint("%s/%06d.o", tmpdir, i);
   646  		argv[argc++] = p;
   647  		w = create(p, 1, 0775);
   648  		if(w < 0) {
   649  			ctxt->cursym = S;
   650  			diag("cannot create %s: %r", p);
   651  			errorexit();
   652  		}
   653  		len = h->len;
   654  		while(len > 0 && (n = Bread(f, buf, sizeof buf)) > 0){
   655  			if(n > len)
   656  				n = len;
   657  			dowrite(w, buf, n);
   658  			len -= n;
   659  		}
   660  		if(close(w) < 0) {
   661  			ctxt->cursym = S;
   662  			diag("cannot write %s: %r", p);
   663  			errorexit();
   664  		}
   665  		Bterm(f);
   666  	}
   667  	
   668  	argv[argc++] = smprint("%s/go.o", tmpdir);
   669  	for(i=0; i<nldflag; i++)
   670  		argv[argc++] = ldflag[i];
   671  
   672  	p = extldflags;
   673  	while(p != nil) {
   674  		while(*p == ' ')
   675  			*p++ = '\0';
   676  		if(*p == '\0')
   677  			break;
   678  		argv[argc++] = p;
   679  
   680  		// clang, unlike GCC, passes -rdynamic to the linker
   681  		// even when linking with -static, causing a linker
   682  		// error when using GNU ld.  So take out -rdynamic if
   683  		// we added it.  We do it in this order, rather than
   684  		// only adding -rdynamic later, so that -extldflags
   685  		// can override -rdynamic without using -static.
   686  		if(iself && strncmp(p, "-static", 7) == 0 && (p[7]==' ' || p[7]=='\0')) {
   687  			for(i=0; i<argc; i++) {
   688  				if(strcmp(argv[i], "-rdynamic") == 0)
   689  					argv[i] = "-static";
   690  			}
   691  		}
   692  
   693  		p = strchr(p + 1, ' ');
   694  	}
   695  
   696  	argv[argc] = nil;
   697  
   698  	quotefmtinstall();
   699  	if(debug['v']) {
   700  		Bprint(&bso, "host link:");
   701  		for(i=0; i<argc; i++)
   702  			Bprint(&bso, " %q", argv[i]);
   703  		Bprint(&bso, "\n");
   704  		Bflush(&bso);
   705  	}
   706  
   707  	if(runcmd(argv) < 0) {
   708  		ctxt->cursym = S;
   709  		diag("%s: running %s failed: %r", argv0, argv[0]);
   710  		errorexit();
   711  	}
   712  }
   713  
   714  void
   715  ldobj(Biobuf *f, char *pkg, int64 len, char *pn, char *file, int whence)
   716  {
   717  	char *line;
   718  	int n, c1, c2, c3, c4;
   719  	uint32 magic;
   720  	vlong import0, import1, eof;
   721  	char *t;
   722  
   723  	eof = Boffset(f) + len;
   724  
   725  	pn = estrdup(pn);
   726  
   727  	c1 = BGETC(f);
   728  	c2 = BGETC(f);
   729  	c3 = BGETC(f);
   730  	c4 = BGETC(f);
   731  	Bungetc(f);
   732  	Bungetc(f);
   733  	Bungetc(f);
   734  	Bungetc(f);
   735  
   736  	magic = c1<<24 | c2<<16 | c3<<8 | c4;
   737  	if(magic == 0x7f454c46) {	// \x7F E L F
   738  		ldhostobj(ldelf, f, pkg, len, pn, file);
   739  		return;
   740  	}
   741  	if((magic&~1) == 0xfeedface || (magic&~0x01000000) == 0xcefaedfe) {
   742  		ldhostobj(ldmacho, f, pkg, len, pn, file);
   743  		return;
   744  	}
   745  	if(c1 == 0x4c && c2 == 0x01 || c1 == 0x64 && c2 == 0x86) {
   746  		ldhostobj(ldpe, f, pkg, len, pn, file);
   747  		return;
   748  	}
   749  
   750  	/* check the header */
   751  	line = Brdline(f, '\n');
   752  	if(line == nil) {
   753  		if(Blinelen(f) > 0) {
   754  			diag("%s: not an object file", pn);
   755  			return;
   756  		}
   757  		goto eof;
   758  	}
   759  	n = Blinelen(f) - 1;
   760  	line[n] = '\0';
   761  	if(strncmp(line, "go object ", 10) != 0) {
   762  		if(strlen(pn) > 3 && strcmp(pn+strlen(pn)-3, ".go") == 0) {
   763  			print("%cl: input %s is not .%c file (use %cg to compile .go files)\n", thechar, pn, thechar, thechar);
   764  			errorexit();
   765  		}
   766  		if(strcmp(line, thestring) == 0) {
   767  			// old header format: just $GOOS
   768  			diag("%s: stale object file", pn);
   769  			return;
   770  		}
   771  		diag("%s: not an object file", pn);
   772  		free(pn);
   773  		return;
   774  	}
   775  	
   776  	// First, check that the basic goos, goarch, and version match.
   777  	t = smprint("%s %s %s ", goos, getgoarch(), getgoversion());
   778  	line[n] = ' ';
   779  	if(strncmp(line+10, t, strlen(t)) != 0 && !debug['f']) {
   780  		line[n] = '\0';
   781  		diag("%s: object is [%s] expected [%s]", pn, line+10, t);
   782  		free(t);
   783  		free(pn);
   784  		return;
   785  	}
   786  	
   787  	// Second, check that longer lines match each other exactly,
   788  	// so that the Go compiler and write additional information
   789  	// that must be the same from run to run.
   790  	line[n] = '\0';
   791  	if(n-10 > strlen(t)) {
   792  		if(theline == nil)
   793  			theline = estrdup(line+10);
   794  		else if(strcmp(theline, line+10) != 0) {
   795  			line[n] = '\0';
   796  			diag("%s: object is [%s] expected [%s]", pn, line+10, theline);
   797  			free(t);
   798  			free(pn);
   799  			return;
   800  		}
   801  	}
   802  	free(t);
   803  	line[n] = '\n';
   804  
   805  	/* skip over exports and other info -- ends with \n!\n */
   806  	import0 = Boffset(f);
   807  	c1 = '\n';	// the last line ended in \n
   808  	c2 = BGETC(f);
   809  	c3 = BGETC(f);
   810  	while(c1 != '\n' || c2 != '!' || c3 != '\n') {
   811  		c1 = c2;
   812  		c2 = c3;
   813  		c3 = BGETC(f);
   814  		if(c3 == Beof)
   815  			goto eof;
   816  	}
   817  	import1 = Boffset(f);
   818  
   819  	Bseek(f, import0, 0);
   820  	ldpkg(f, pkg, import1 - import0 - 2, pn, whence);	// -2 for !\n
   821  	Bseek(f, import1, 0);
   822  
   823  	ldobjfile(ctxt, f, pkg, eof - Boffset(f), pn);
   824  	free(pn);
   825  	return;
   826  
   827  eof:
   828  	diag("truncated object file: %s", pn);
   829  	free(pn);
   830  }
   831  
   832  void
   833  zerosig(char *sp)
   834  {
   835  	LSym *s;
   836  
   837  	s = linklookup(ctxt, sp, 0);
   838  	s->sig = 0;
   839  }
   840  
   841  void
   842  mywhatsys(void)
   843  {
   844  	goroot = getgoroot();
   845  	goos = getgoos();
   846  	goarch = getgoarch();
   847  
   848  	if(strncmp(goarch, thestring, strlen(thestring)) != 0)
   849  		sysfatal("cannot use %cc with GOARCH=%s", thechar, goarch);
   850  }
   851  
   852  int
   853  pathchar(void)
   854  {
   855  	return '/';
   856  }
   857  
   858  static	uchar*	hunk;
   859  static	uint32	nhunk;
   860  #define	NHUNK	(10UL<<20)
   861  
   862  void*
   863  mal(uint32 n)
   864  {
   865  	void *v;
   866  
   867  	n = (n+7)&~7;
   868  	if(n > NHUNK) {
   869  		v = malloc(n);
   870  		if(v == nil) {
   871  			diag("out of memory");
   872  			errorexit();
   873  		}
   874  		memset(v, 0, n);
   875  		return v;
   876  	}
   877  	if(n > nhunk) {
   878  		hunk = malloc(NHUNK);
   879  		if(hunk == nil) {
   880  			diag("out of memory");
   881  			errorexit();
   882  		}
   883  		nhunk = NHUNK;
   884  	}
   885  
   886  	v = hunk;
   887  	nhunk -= n;
   888  	hunk += n;
   889  
   890  	memset(v, 0, n);
   891  	return v;
   892  }
   893  
   894  void
   895  unmal(void *v, uint32 n)
   896  {
   897  	n = (n+7)&~7;
   898  	if(hunk - n == v) {
   899  		hunk -= n;
   900  		nhunk += n;
   901  	}
   902  }
   903  
   904  // Copied from ../gc/subr.c:/^pathtoprefix; must stay in sync.
   905  /*
   906   * Convert raw string to the prefix that will be used in the symbol table.
   907   * Invalid bytes turn into %xx.	 Right now the only bytes that need
   908   * escaping are %, ., and ", but we escape all control characters too.
   909   *
   910   * If you edit this, edit ../gc/subr.c:/^pathtoprefix too.
   911   * If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too.
   912   */
   913  static char*
   914  pathtoprefix(char *s)
   915  {
   916  	static char hex[] = "0123456789abcdef";
   917  	char *p, *r, *w, *l;
   918  	int n;
   919  
   920  	// find first character past the last slash, if any.
   921  	l = s;
   922  	for(r=s; *r; r++)
   923  		if(*r == '/')
   924  			l = r+1;
   925  
   926  	// check for chars that need escaping
   927  	n = 0;
   928  	for(r=s; *r; r++)
   929  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f)
   930  			n++;
   931  
   932  	// quick exit
   933  	if(n == 0)
   934  		return s;
   935  
   936  	// escape
   937  	p = mal((r-s)+1+2*n);
   938  	for(r=s, w=p; *r; r++) {
   939  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) {
   940  			*w++ = '%';
   941  			*w++ = hex[(*r>>4)&0xF];
   942  			*w++ = hex[*r&0xF];
   943  		} else
   944  			*w++ = *r;
   945  	}
   946  	*w = '\0';
   947  	return p;
   948  }
   949  
   950  int
   951  iconv(Fmt *fp)
   952  {
   953  	char *p;
   954  
   955  	p = va_arg(fp->args, char*);
   956  	if(p == nil) {
   957  		fmtstrcpy(fp, "<nil>");
   958  		return 0;
   959  	}
   960  	p = pathtoprefix(p);
   961  	fmtstrcpy(fp, p);
   962  	return 0;
   963  }
   964  
   965  Section*
   966  addsection(Segment *seg, char *name, int rwx)
   967  {
   968  	Section **l;
   969  	Section *sect;
   970  	
   971  	for(l=&seg->sect; *l; l=&(*l)->next)
   972  		;
   973  	sect = mal(sizeof *sect);
   974  	sect->rwx = rwx;
   975  	sect->name = name;
   976  	sect->seg = seg;
   977  	sect->align = PtrSize; // everything is at least pointer-aligned
   978  	*l = sect;
   979  	return sect;
   980  }
   981  
   982  uint16
   983  le16(uchar *b)
   984  {
   985  	return b[0] | b[1]<<8;
   986  }
   987  
   988  uint32
   989  le32(uchar *b)
   990  {
   991  	return b[0] | b[1]<<8 | b[2]<<16 | (uint32)b[3]<<24;
   992  }
   993  
   994  uint64
   995  le64(uchar *b)
   996  {
   997  	return le32(b) | (uint64)le32(b+4)<<32;
   998  }
   999  
  1000  uint16
  1001  be16(uchar *b)
  1002  {
  1003  	return b[0]<<8 | b[1];
  1004  }
  1005  
  1006  uint32
  1007  be32(uchar *b)
  1008  {
  1009  	return (uint32)b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
  1010  }
  1011  
  1012  uint64
  1013  be64(uchar *b)
  1014  {
  1015  	return (uvlong)be32(b)<<32 | be32(b+4);
  1016  }
  1017  
  1018  Endian be = { be16, be32, be64 };
  1019  Endian le = { le16, le32, le64 };
  1020  
  1021  typedef struct Chain Chain;
  1022  struct Chain
  1023  {
  1024  	LSym *sym;
  1025  	Chain *up;
  1026  	int limit;  // limit on entry to sym
  1027  };
  1028  
  1029  static int stkcheck(Chain*, int);
  1030  static void stkprint(Chain*, int);
  1031  static void stkbroke(Chain*, int);
  1032  static LSym *morestack;
  1033  static LSym *newstack;
  1034  
  1035  enum
  1036  {
  1037  	HasLinkRegister = (thechar == '5' || thechar == '9'),
  1038  };
  1039  
  1040  // TODO: Record enough information in new object files to
  1041  // allow stack checks here.
  1042  
  1043  static int
  1044  callsize(void)
  1045  {
  1046  	if(HasLinkRegister)
  1047  		return 0;
  1048  	return RegSize;
  1049  }
  1050  
  1051  void
  1052  dostkcheck(void)
  1053  {
  1054  	Chain ch;
  1055  	LSym *s;
  1056  
  1057  	morestack = linklookup(ctxt, "runtime.morestack", 0);
  1058  	newstack = linklookup(ctxt, "runtime.newstack", 0);
  1059  
  1060  	// Every splitting function ensures that there are at least StackLimit
  1061  	// bytes available below SP when the splitting prologue finishes.
  1062  	// If the splitting function calls F, then F begins execution with
  1063  	// at least StackLimit - callsize() bytes available.
  1064  	// Check that every function behaves correctly with this amount
  1065  	// of stack, following direct calls in order to piece together chains
  1066  	// of non-splitting functions.
  1067  	ch.up = nil;
  1068  	ch.limit = StackLimit - callsize();
  1069  
  1070  	// Check every function, but do the nosplit functions in a first pass,
  1071  	// to make the printed failure chains as short as possible.
  1072  	for(s = ctxt->textp; s != nil; s = s->next) {
  1073  		// runtime.racesymbolizethunk is called from gcc-compiled C
  1074  		// code running on the operating system thread stack.
  1075  		// It uses more than the usual amount of stack but that's okay.
  1076  		if(strcmp(s->name, "runtime.racesymbolizethunk") == 0)
  1077  			continue;
  1078  
  1079  		if(s->nosplit) {
  1080  			ctxt->cursym = s;
  1081  			ch.sym = s;
  1082  			stkcheck(&ch, 0);
  1083  		}
  1084  	}
  1085  	for(s = ctxt->textp; s != nil; s = s->next) {
  1086  		if(!s->nosplit) {
  1087  			ctxt->cursym = s;
  1088  			ch.sym = s;
  1089  			stkcheck(&ch, 0);
  1090  		}
  1091  	}
  1092  }
  1093  
  1094  static int
  1095  stkcheck(Chain *up, int depth)
  1096  {
  1097  	Chain ch, ch1;
  1098  	LSym *s;
  1099  	int limit;
  1100  	Reloc *r, *endr;
  1101  	Pciter pcsp;
  1102  	
  1103  	limit = up->limit;
  1104  	s = up->sym;
  1105  	
  1106  	// Don't duplicate work: only need to consider each
  1107  	// function at top of safe zone once.
  1108  	if(limit == StackLimit-callsize()) {
  1109  		if(s->stkcheck)
  1110  			return 0;
  1111  		s->stkcheck = 1;
  1112  	}
  1113  	
  1114  	if(depth > 100) {
  1115  		diag("nosplit stack check too deep");
  1116  		stkbroke(up, 0);
  1117  		return -1;
  1118  	}
  1119  
  1120  	if(s->external || s->pcln == nil) {
  1121  		// external function.
  1122  		// should never be called directly.
  1123  		// only diagnose the direct caller.
  1124  		if(depth == 1 && s->type != SXREF)
  1125  			diag("call to external function %s", s->name);
  1126  		return -1;
  1127  	}
  1128  
  1129  	if(limit < 0) {
  1130  		stkbroke(up, limit);
  1131  		return -1;
  1132  	}
  1133  
  1134  	// morestack looks like it calls functions,
  1135  	// but it switches the stack pointer first.
  1136  	if(s == morestack)
  1137  		return 0;
  1138  
  1139  	ch.up = up;
  1140  	
  1141  	// Walk through sp adjustments in function, consuming relocs.
  1142  	r = s->r;
  1143  	endr = r + s->nr;
  1144  	for(pciterinit(ctxt, &pcsp, &s->pcln->pcsp); !pcsp.done; pciternext(&pcsp)) {
  1145  		// pcsp.value is in effect for [pcsp.pc, pcsp.nextpc).
  1146  
  1147  		// Check stack size in effect for this span.
  1148  		if(limit - pcsp.value < 0) {
  1149  			stkbroke(up, limit - pcsp.value);
  1150  			return -1;
  1151  		}
  1152  
  1153  		// Process calls in this span.
  1154  		for(; r < endr && r->off < pcsp.nextpc; r++) {
  1155  			switch(r->type) {
  1156  			case R_CALL:
  1157  			case R_CALLARM:
  1158  			case R_CALLPOWER:
  1159  				// Direct call.
  1160  				ch.limit = limit - pcsp.value - callsize();
  1161  				ch.sym = r->sym;
  1162  				if(stkcheck(&ch, depth+1) < 0)
  1163  					return -1;
  1164  
  1165  				// If this is a call to morestack, we've just raised our limit back
  1166  				// to StackLimit beyond the frame size.
  1167  				if(strncmp(r->sym->name, "runtime.morestack", 17) == 0) {
  1168  					limit = StackLimit + s->locals;
  1169  					if(HasLinkRegister)
  1170  						limit += RegSize;
  1171  				}
  1172  				break;
  1173  
  1174  			case R_CALLIND:
  1175  				// Indirect call.  Assume it is a call to a splitting function,
  1176  				// so we have to make sure it can call morestack.
  1177  				// Arrange the data structures to report both calls, so that
  1178  				// if there is an error, stkprint shows all the steps involved.
  1179  				ch.limit = limit - pcsp.value - callsize();
  1180  				ch.sym = nil;
  1181  				ch1.limit = ch.limit - callsize(); // for morestack in called prologue
  1182  				ch1.up = &ch;
  1183  				ch1.sym = morestack;
  1184  				if(stkcheck(&ch1, depth+2) < 0)
  1185  					return -1;
  1186  				break;
  1187  			}
  1188  		}
  1189  	}
  1190  		
  1191  	return 0;
  1192  }
  1193  
  1194  static void
  1195  stkbroke(Chain *ch, int limit)
  1196  {
  1197  	diag("nosplit stack overflow");
  1198  	stkprint(ch, limit);
  1199  }
  1200  
  1201  static void
  1202  stkprint(Chain *ch, int limit)
  1203  {
  1204  	char *name;
  1205  
  1206  	if(ch->sym)
  1207  		name = ch->sym->name;
  1208  	else
  1209  		name = "function pointer";
  1210  
  1211  	if(ch->up == nil) {
  1212  		// top of chain.  ch->sym != nil.
  1213  		if(ch->sym->nosplit)
  1214  			print("\t%d\tassumed on entry to %s\n", ch->limit, name);
  1215  		else
  1216  			print("\t%d\tguaranteed after split check in %s\n", ch->limit, name);
  1217  	} else {
  1218  		stkprint(ch->up, ch->limit + (!HasLinkRegister)*RegSize);
  1219  		if(!HasLinkRegister)
  1220  			print("\t%d\ton entry to %s\n", ch->limit, name);
  1221  	}
  1222  	if(ch->limit != limit)
  1223  		print("\t%d\tafter %s uses %d\n", limit, name, ch->limit - limit);
  1224  }
  1225  
  1226  int
  1227  Yconv(Fmt *fp)
  1228  {
  1229  	LSym *s;
  1230  	Fmt fmt;
  1231  	int i;
  1232  	char *str;
  1233  
  1234  	s = va_arg(fp->args, LSym*);
  1235  	if (s == S) {
  1236  		fmtprint(fp, "<nil>");
  1237  	} else {
  1238  		fmtstrinit(&fmt);
  1239  		fmtprint(&fmt, "%s @0x%08llx [%lld]", s->name, (vlong)s->value, (vlong)s->size);
  1240  		for (i = 0; i < s->size; i++) {
  1241  			if (!(i%8)) fmtprint(&fmt,  "\n\t0x%04x ", i);
  1242  			fmtprint(&fmt, "%02x ", s->p[i]);
  1243  		}
  1244  		fmtprint(&fmt, "\n");
  1245  		for (i = 0; i < s->nr; i++) {
  1246  			fmtprint(&fmt, "\t0x%04x[%x] %d %s[%llx]\n",
  1247  			      s->r[i].off,
  1248  			      s->r[i].siz,
  1249  			      s->r[i].type,
  1250  			      s->r[i].sym->name,
  1251  			      (vlong)s->r[i].add);
  1252  		}
  1253  		str = fmtstrflush(&fmt);
  1254  		fmtstrcpy(fp, str);
  1255  		free(str);
  1256  	}
  1257  
  1258  	return 0;
  1259  }
  1260  
  1261  vlong coutpos;
  1262  
  1263  void
  1264  cflush(void)
  1265  {
  1266  	int n;
  1267  
  1268  	if(cbpmax < cbp)
  1269  		cbpmax = cbp;
  1270  	n = cbpmax - buf.cbuf;
  1271  	dowrite(cout, buf.cbuf, n);
  1272  	coutpos += n;
  1273  	cbp = buf.cbuf;
  1274  	cbc = sizeof(buf.cbuf);
  1275  	cbpmax = cbp;
  1276  }
  1277  
  1278  vlong
  1279  cpos(void)
  1280  {
  1281  	return coutpos + cbp - buf.cbuf;
  1282  }
  1283  
  1284  void
  1285  cseek(vlong p)
  1286  {
  1287  	vlong start;
  1288  	int delta;
  1289  
  1290  	if(cbpmax < cbp)
  1291  		cbpmax = cbp;
  1292  	start = coutpos;
  1293  	if(start <= p && p <= start+(cbpmax - buf.cbuf)) {
  1294  //print("cseek %lld in [%lld,%lld] (%lld)\n", p, start, start+sizeof(buf.cbuf), cpos());
  1295  		delta = p - (start + cbp - buf.cbuf);
  1296  		cbp += delta;
  1297  		cbc -= delta;
  1298  //print("now at %lld\n", cpos());
  1299  		return;
  1300  	}
  1301  
  1302  	cflush();
  1303  	seek(cout, p, 0);
  1304  	coutpos = p;
  1305  }
  1306  
  1307  void
  1308  cwrite(void *buf, int n)
  1309  {
  1310  	cflush();
  1311  	if(n <= 0)
  1312  		return;
  1313  	dowrite(cout, buf, n);
  1314  	coutpos += n;
  1315  }
  1316  
  1317  void
  1318  usage(void)
  1319  {
  1320  	fprint(2, "usage: %cl [options] main.%c\n", thechar, thechar);
  1321  	flagprint(2);
  1322  	exits("usage");
  1323  }
  1324  
  1325  void
  1326  setheadtype(char *s)
  1327  {
  1328  	int h;
  1329  	
  1330  	h = headtype(s);
  1331  	if(h < 0) {
  1332  		fprint(2, "unknown header type -H %s\n", s);
  1333  		errorexit();
  1334  	}
  1335  	headstring = s;
  1336  	HEADTYPE = headtype(s);
  1337  }
  1338  
  1339  void
  1340  setinterp(char *s)
  1341  {
  1342  	debug['I'] = 1; // denote cmdline interpreter override
  1343  	interpreter = s;
  1344  }
  1345  
  1346  void
  1347  doversion(void)
  1348  {
  1349  	print("%cl version %s\n", thechar, getgoversion());
  1350  	errorexit();
  1351  }
  1352  
  1353  void
  1354  genasmsym(void (*put)(LSym*, char*, int, vlong, vlong, int, LSym*))
  1355  {
  1356  	Auto *a;
  1357  	LSym *s;
  1358  	int32 off;
  1359  
  1360  	// These symbols won't show up in the first loop below because we
  1361  	// skip STEXT symbols. Normal STEXT symbols are emitted by walking textp.
  1362  	s = linklookup(ctxt, "runtime.text", 0);
  1363  	if(s->type == STEXT)
  1364  		put(s, s->name, 'T', s->value, s->size, s->version, 0);
  1365  	s = linklookup(ctxt, "runtime.etext", 0);
  1366  	if(s->type == STEXT)
  1367  		put(s, s->name, 'T', s->value, s->size, s->version, 0);
  1368  
  1369  	for(s=ctxt->allsym; s!=S; s=s->allsym) {
  1370  		if(s->hide || (s->name[0] == '.' && s->version == 0 && strcmp(s->name, ".rathole") != 0))
  1371  			continue;
  1372  		switch(s->type&SMASK) {
  1373  		case SCONST:
  1374  		case SRODATA:
  1375  		case SSYMTAB:
  1376  		case SPCLNTAB:
  1377  		case SDATA:
  1378  		case SNOPTRDATA:
  1379  		case SELFROSECT:
  1380  		case SMACHOGOT:
  1381  		case STYPE:
  1382  		case SSTRING:
  1383  		case SGOSTRING:
  1384  		case SWINDOWS:
  1385  			if(!s->reachable)
  1386  				continue;
  1387  			put(s, s->name, 'D', symaddr(s), s->size, s->version, s->gotype);
  1388  			continue;
  1389  
  1390  		case SBSS:
  1391  		case SNOPTRBSS:
  1392  			if(!s->reachable)
  1393  				continue;
  1394  			if(s->np > 0)
  1395  				diag("%s should not be bss (size=%d type=%d special=%d)", s->name, (int)s->np, s->type, s->special);
  1396  			put(s, s->name, 'B', symaddr(s), s->size, s->version, s->gotype);
  1397  			continue;
  1398  
  1399  		case SFILE:
  1400  			put(nil, s->name, 'f', s->value, 0, s->version, 0);
  1401  			continue;
  1402  		}
  1403  	}
  1404  
  1405  	for(s = ctxt->textp; s != nil; s = s->next) {
  1406  		put(s, s->name, 'T', s->value, s->size, s->version, s->gotype);
  1407  
  1408  		// NOTE(ality): acid can't produce a stack trace without .frame symbols
  1409  		put(nil, ".frame", 'm', s->locals+PtrSize, 0, 0, 0);
  1410  
  1411  		for(a=s->autom; a; a=a->link) {
  1412  			// Emit a or p according to actual offset, even if label is wrong.
  1413  			// This avoids negative offsets, which cannot be encoded.
  1414  			if(a->type != A_AUTO && a->type != A_PARAM)
  1415  				continue;
  1416  			
  1417  			// compute offset relative to FP
  1418  			if(a->type == A_PARAM)
  1419  				off = a->aoffset;
  1420  			else
  1421  				off = a->aoffset - PtrSize;
  1422  			
  1423  			// FP
  1424  			if(off >= 0) {
  1425  				put(nil, a->asym->name, 'p', off, 0, 0, a->gotype);
  1426  				continue;
  1427  			}
  1428  			
  1429  			// SP
  1430  			if(off <= -PtrSize) {
  1431  				put(nil, a->asym->name, 'a', -(off+PtrSize), 0, 0, a->gotype);
  1432  				continue;
  1433  			}
  1434  			
  1435  			// Otherwise, off is addressing the saved program counter.
  1436  			// Something underhanded is going on. Say nothing.
  1437  		}
  1438  	}
  1439  	if(debug['v'] || debug['n'])
  1440  		Bprint(&bso, "%5.2f symsize = %ud\n", cputime(), symsize);
  1441  	Bflush(&bso);
  1442  }
  1443  
  1444  vlong
  1445  symaddr(LSym *s)
  1446  {
  1447  	if(!s->reachable)
  1448  		diag("unreachable symbol in symaddr - %s", s->name);
  1449  	return s->value;
  1450  }
  1451  
  1452  void
  1453  xdefine(char *p, int t, vlong v)
  1454  {
  1455  	LSym *s;
  1456  
  1457  	s = linklookup(ctxt, p, 0);
  1458  	s->type = t;
  1459  	s->value = v;
  1460  	s->reachable = 1;
  1461  	s->special = 1;
  1462  }
  1463  
  1464  vlong
  1465  datoff(vlong addr)
  1466  {
  1467  	if(addr >= segdata.vaddr)
  1468  		return addr - segdata.vaddr + segdata.fileoff;
  1469  	if(addr >= segtext.vaddr)
  1470  		return addr - segtext.vaddr + segtext.fileoff;
  1471  	diag("datoff %#llx", addr);
  1472  	return 0;
  1473  }
  1474  
  1475  vlong
  1476  entryvalue(void)
  1477  {
  1478  	char *a;
  1479  	LSym *s;
  1480  
  1481  	a = INITENTRY;
  1482  	if(*a >= '0' && *a <= '9')
  1483  		return atolwhex(a);
  1484  	s = linklookup(ctxt, a, 0);
  1485  	if(s->type == 0)
  1486  		return INITTEXT;
  1487  	if(s->type != STEXT)
  1488  		diag("entry not text: %s", s->name);
  1489  	return s->value;
  1490  }
  1491  
  1492  static void
  1493  undefsym(LSym *s)
  1494  {
  1495  	int i;
  1496  	Reloc *r;
  1497  
  1498  	ctxt->cursym = s;
  1499  	for(i=0; i<s->nr; i++) {
  1500  		r = &s->r[i];
  1501  		if(r->sym == nil) // happens for some external ARM relocs
  1502  			continue;
  1503  		if(r->sym->type == Sxxx || r->sym->type == SXREF)
  1504  			diag("undefined: %s", r->sym->name);
  1505  		if(!r->sym->reachable)
  1506  			diag("use of unreachable symbol: %s", r->sym->name);
  1507  	}
  1508  }
  1509  
  1510  void
  1511  undef(void)
  1512  {
  1513  	LSym *s;
  1514  	
  1515  	for(s = ctxt->textp; s != nil; s = s->next)
  1516  		undefsym(s);
  1517  	for(s = datap; s != nil; s = s->next)
  1518  		undefsym(s);
  1519  	if(nerrors > 0)
  1520  		errorexit();
  1521  }
  1522  
  1523  void
  1524  callgraph(void)
  1525  {
  1526  	LSym *s;
  1527  	Reloc *r;
  1528  	int i;
  1529  
  1530  	if(!debug['c'])
  1531  		return;
  1532  
  1533  	for(s = ctxt->textp; s != nil; s = s->next) {
  1534  		for(i=0; i<s->nr; i++) {
  1535  			r = &s->r[i];
  1536  			if(r->sym == nil)
  1537  				continue;
  1538  			if((r->type == R_CALL || r->type == R_CALLARM || r->type == R_CALLPOWER) && r->sym->type == STEXT)
  1539  				Bprint(&bso, "%s calls %s\n", s->name, r->sym->name);
  1540  		}
  1541  	}
  1542  }
  1543  
  1544  void
  1545  diag(char *fmt, ...)
  1546  {
  1547  	char buf[1024], *tn, *sep;
  1548  	va_list arg;
  1549  
  1550  	tn = "";
  1551  	sep = "";
  1552  	if(ctxt->cursym != S) {
  1553  		tn = ctxt->cursym->name;
  1554  		sep = ": ";
  1555  	}
  1556  	va_start(arg, fmt);
  1557  	vseprint(buf, buf+sizeof(buf), fmt, arg);
  1558  	va_end(arg);
  1559  	print("%s%s%s\n", tn, sep, buf);
  1560  
  1561  	nerrors++;
  1562  	if(nerrors > 20) {
  1563  		print("too many errors\n");
  1564  		errorexit();
  1565  	}
  1566  }
  1567  
  1568  void
  1569  checkgo(void)
  1570  {
  1571  	LSym *s;
  1572  	Reloc *r;
  1573  	int i;
  1574  	int changed;
  1575  	
  1576  	if(!debug['C'])
  1577  		return;
  1578  	
  1579  	// TODO(rsc,khr): Eventually we want to get to no Go-called C functions at all,
  1580  	// which would simplify this logic quite a bit.
  1581  
  1582  	// Mark every Go-called C function with cfunc=2, recursively.
  1583  	do {
  1584  		changed = 0;
  1585  		for(s = ctxt->textp; s != nil; s = s->next) {
  1586  			if(s->cfunc == 0 || (s->cfunc == 2 && s->nosplit)) {
  1587  				for(i=0; i<s->nr; i++) {
  1588  					r = &s->r[i];
  1589  					if(r->sym == nil)
  1590  						continue;
  1591  					if((r->type == R_CALL || r->type == R_CALLARM) && r->sym->type == STEXT) {
  1592  						if(r->sym->cfunc == 1) {
  1593  							changed = 1;
  1594  							r->sym->cfunc = 2;
  1595  						}
  1596  					}
  1597  				}
  1598  			}
  1599  		}
  1600  	}while(changed);
  1601  
  1602  	// Complain about Go-called C functions that can split the stack
  1603  	// (that can be preempted for garbage collection or trigger a stack copy).
  1604  	for(s = ctxt->textp; s != nil; s = s->next) {
  1605  		if(s->cfunc == 0 || (s->cfunc == 2 && s->nosplit)) {
  1606  			for(i=0; i<s->nr; i++) {
  1607  				r = &s->r[i];
  1608  				if(r->sym == nil)
  1609  					continue;
  1610  				if((r->type == R_CALL || r->type == R_CALLARM) && r->sym->type == STEXT) {
  1611  					if(s->cfunc == 0 && r->sym->cfunc == 2 && !r->sym->nosplit)
  1612  						print("Go %s calls C %s\n", s->name, r->sym->name);
  1613  					else if(s->cfunc == 2 && s->nosplit && !r->sym->nosplit)
  1614  						print("Go calls C %s calls %s\n", s->name, r->sym->name);
  1615  				}
  1616  			}
  1617  		}
  1618  	}
  1619  }