github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/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 if(strcmp(goos, "akaros") == 0) 199 loadinternal("runtime/parlib"); 200 201 for(i=0; i<ctxt->libraryp; i++) { 202 if(debug['v'] > 1) 203 Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), ctxt->library[i].file, ctxt->library[i].objref); 204 iscgo |= strcmp(ctxt->library[i].pkg, "runtime/cgo") == 0; 205 objfile(ctxt->library[i].file, ctxt->library[i].pkg); 206 } 207 208 if(linkmode == LinkExternal && !iscgo) { 209 // This indicates a user requested -linkmode=external. 210 // The startup code uses an import of runtime/cgo to decide 211 // whether to initialize the TLS. So give it one. This could 212 // be handled differently but it's an unusual case. 213 loadinternal("runtime/cgo"); 214 if(i < ctxt->libraryp) 215 objfile(ctxt->library[i].file, ctxt->library[i].pkg); 216 217 // Pretend that we really imported the package. 218 s = linklookup(ctxt, "go.importpath.runtime/cgo.", 0); 219 s->type = SDATA; 220 s->dupok = 1; 221 s->reachable = 1; 222 223 // Provided by the code that imports the package. 224 // Since we are simulating the import, we have to provide this string. 225 cgostrsym = "go.string.\"runtime/cgo\""; 226 if(linkrlookup(ctxt, cgostrsym, 0) == nil) { 227 s = linklookup(ctxt, cgostrsym, 0); 228 s->type = SRODATA; 229 s->reachable = 1; 230 addstrdata(cgostrsym, "runtime/cgo"); 231 } 232 } 233 234 if(linkmode == LinkAuto) { 235 if(iscgo && externalobj) 236 linkmode = LinkExternal; 237 else 238 linkmode = LinkInternal; 239 240 // Force external linking for android. 241 if(strcmp(goos, "android") == 0) 242 linkmode = LinkExternal; 243 } 244 245 if(linkmode == LinkInternal) { 246 // Drop all the cgo_import_static declarations. 247 // Turns out we won't be needing them. 248 for(s = ctxt->allsym; s != S; s = s->allsym) 249 if(s->type == SHOSTOBJ) { 250 // If a symbol was marked both 251 // cgo_import_static and cgo_import_dynamic, 252 // then we want to make it cgo_import_dynamic 253 // now. 254 if(s->extname != nil && s->dynimplib != nil && s->cgoexport == 0) { 255 s->type = SDYNIMPORT; 256 } else 257 s->type = 0; 258 } 259 } 260 261 tlsg = linklookup(ctxt, "runtime.tlsg", 0); 262 tlsg->type = STLSBSS; 263 tlsg->size = PtrSize; 264 tlsg->hide = 1; 265 tlsg->reachable = 1; 266 ctxt->tlsg = tlsg; 267 268 // Now that we know the link mode, trim the dynexp list. 269 x = CgoExportDynamic; 270 if(linkmode == LinkExternal) 271 x = CgoExportStatic; 272 w = 0; 273 for(i=0; i<ndynexp; i++) 274 if(dynexp[i]->cgoexport & x) 275 dynexp[w++] = dynexp[i]; 276 ndynexp = w; 277 278 // In internal link mode, read the host object files. 279 if(linkmode == LinkInternal) 280 hostobjs(); 281 else 282 hostlinksetup(); 283 284 // We've loaded all the code now. 285 // If there are no dynamic libraries needed, gcc disables dynamic linking. 286 // Because of this, glibc's dynamic ELF loader occasionally (like in version 2.13) 287 // assumes that a dynamic binary always refers to at least one dynamic library. 288 // Rather than be a source of test cases for glibc, disable dynamic linking 289 // the same way that gcc would. 290 // 291 // Exception: on OS X, programs such as Shark only work with dynamic 292 // binaries, so leave it enabled on OS X (Mach-O) binaries. 293 // Also leave it enabled on Solaris which doesn't support 294 // statically linked binaries. 295 if(!flag_shared && !havedynamic && HEADTYPE != Hdarwin && HEADTYPE != Hsolaris) 296 debug['d'] = 1; 297 298 importcycles(); 299 } 300 301 /* 302 * look for the next file in an archive. 303 * adapted from libmach. 304 */ 305 static vlong 306 nextar(Biobuf *bp, vlong off, struct ar_hdr *a) 307 { 308 int r; 309 int32 arsize; 310 char *buf; 311 312 if (off&01) 313 off++; 314 Bseek(bp, off, 0); 315 buf = Brdline(bp, '\n'); 316 r = Blinelen(bp); 317 if(buf == nil) { 318 if(r == 0) 319 return 0; 320 return -1; 321 } 322 if(r != SAR_HDR) 323 return -1; 324 memmove(a, buf, SAR_HDR); 325 if(strncmp(a->fmag, ARFMAG, sizeof a->fmag)) 326 return -1; 327 arsize = strtol(a->size, 0, 0); 328 if (arsize&1) 329 arsize++; 330 return arsize + r; 331 } 332 333 void 334 objfile(char *file, char *pkg) 335 { 336 vlong off, l; 337 Biobuf *f; 338 char magbuf[SARMAG]; 339 char pname[150]; 340 struct ar_hdr arhdr; 341 342 pkg = smprint("%i", pkg); 343 344 if(debug['v'] > 1) 345 Bprint(&bso, "%5.2f ldobj: %s (%s)\n", cputime(), file, pkg); 346 Bflush(&bso); 347 f = Bopen(file, 0); 348 if(f == nil) { 349 diag("cannot open file: %s", file); 350 errorexit(); 351 } 352 l = Bread(f, magbuf, SARMAG); 353 if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ 354 /* load it as a regular file */ 355 l = Bseek(f, 0L, 2); 356 Bseek(f, 0L, 0); 357 ldobj(f, pkg, l, file, file, FileObj); 358 Bterm(f); 359 free(pkg); 360 return; 361 } 362 363 /* skip over optional __.GOSYMDEF and process __.PKGDEF */ 364 off = Boffset(f); 365 l = nextar(f, off, &arhdr); 366 if(l <= 0) { 367 diag("%s: short read on archive file symbol header", file); 368 goto out; 369 } 370 if(strncmp(arhdr.name, symname, strlen(symname)) == 0) { 371 off += l; 372 l = nextar(f, off, &arhdr); 373 if(l <= 0) { 374 diag("%s: short read on archive file symbol header", file); 375 goto out; 376 } 377 } 378 379 if(strncmp(arhdr.name, pkgname, strlen(pkgname))) { 380 diag("%s: cannot find package header", file); 381 goto out; 382 } 383 off += l; 384 385 if(debug['u']) 386 ldpkg(f, pkg, atolwhex(arhdr.size), file, Pkgdef); 387 388 /* 389 * load all the object files from the archive now. 390 * this gives us sequential file access and keeps us 391 * from needing to come back later to pick up more 392 * objects. it breaks the usual C archive model, but 393 * this is Go, not C. the common case in Go is that 394 * we need to load all the objects, and then we throw away 395 * the individual symbols that are unused. 396 * 397 * loading every object will also make it possible to 398 * load foreign objects not referenced by __.GOSYMDEF. 399 */ 400 for(;;) { 401 l = nextar(f, off, &arhdr); 402 if(l == 0) 403 break; 404 if(l < 0) { 405 diag("%s: malformed archive", file); 406 goto out; 407 } 408 off += l; 409 410 l = SARNAME; 411 while(l > 0 && arhdr.name[l-1] == ' ') 412 l--; 413 snprint(pname, sizeof pname, "%s(%.*s)", file, utfnlen(arhdr.name, l), arhdr.name); 414 l = atolwhex(arhdr.size); 415 ldobj(f, pkg, l, pname, file, ArchiveObj); 416 } 417 418 out: 419 Bterm(f); 420 free(pkg); 421 } 422 423 static void 424 dowrite(int fd, char *p, int n) 425 { 426 int m; 427 428 while(n > 0) { 429 m = write(fd, p, n); 430 if(m <= 0) { 431 ctxt->cursym = S; 432 diag("write error: %r"); 433 errorexit(); 434 } 435 n -= m; 436 p += m; 437 } 438 } 439 440 typedef struct Hostobj Hostobj; 441 442 struct Hostobj 443 { 444 void (*ld)(Biobuf*, char*, int64, char*); 445 char *pkg; 446 char *pn; 447 char *file; 448 int64 off; 449 int64 len; 450 }; 451 452 Hostobj *hostobj; 453 int nhostobj; 454 int mhostobj; 455 456 // These packages can use internal linking mode. 457 // Others trigger external mode. 458 const char *internalpkg[] = { 459 "crypto/x509", 460 "net", 461 "os/user", 462 "runtime/cgo", 463 "runtime/race" 464 }; 465 466 void 467 ldhostobj(void (*ld)(Biobuf*, char*, int64, char*), Biobuf *f, char *pkg, int64 len, char *pn, char *file) 468 { 469 int i, isinternal; 470 Hostobj *h; 471 472 isinternal = 0; 473 for(i=0; i<nelem(internalpkg); i++) { 474 if(strcmp(pkg, internalpkg[i]) == 0) { 475 isinternal = 1; 476 break; 477 } 478 } 479 480 // DragonFly declares errno with __thread, which results in a symbol 481 // type of R_386_TLS_GD or R_X86_64_TLSGD. The Go linker does not 482 // currently know how to handle TLS relocations, hence we have to 483 // force external linking for any libraries that link in code that 484 // uses errno. This can be removed if the Go linker ever supports 485 // these relocation types. 486 if(HEADTYPE == Hdragonfly) 487 if(strcmp(pkg, "net") == 0 || strcmp(pkg, "os/user") == 0) 488 isinternal = 0; 489 490 if(!isinternal) 491 externalobj = 1; 492 493 if(nhostobj >= mhostobj) { 494 if(mhostobj == 0) 495 mhostobj = 16; 496 else 497 mhostobj *= 2; 498 hostobj = erealloc(hostobj, mhostobj*sizeof hostobj[0]); 499 } 500 h = &hostobj[nhostobj++]; 501 h->ld = ld; 502 h->pkg = estrdup(pkg); 503 h->pn = estrdup(pn); 504 h->file = estrdup(file); 505 h->off = Boffset(f); 506 h->len = len; 507 } 508 509 void 510 hostobjs(void) 511 { 512 int i; 513 Biobuf *f; 514 Hostobj *h; 515 516 for(i=0; i<nhostobj; i++) { 517 h = &hostobj[i]; 518 f = Bopen(h->file, OREAD); 519 if(f == nil) { 520 ctxt->cursym = S; 521 diag("cannot reopen %s: %r", h->pn); 522 errorexit(); 523 } 524 Bseek(f, h->off, 0); 525 h->ld(f, h->pkg, h->len, h->pn); 526 Bterm(f); 527 } 528 } 529 530 // provided by lib9 531 int runcmd(char**); 532 char* mktempdir(void); 533 void removeall(char*); 534 535 static void 536 rmtemp(void) 537 { 538 removeall(tmpdir); 539 } 540 541 static void 542 hostlinksetup(void) 543 { 544 char *p; 545 546 if(linkmode != LinkExternal) 547 return; 548 549 // create temporary directory and arrange cleanup 550 if(tmpdir == nil) { 551 tmpdir = mktempdir(); 552 atexit(rmtemp); 553 } 554 555 // change our output to temporary object file 556 close(cout); 557 p = smprint("%s/go.o", tmpdir); 558 cout = create(p, 1, 0775); 559 if(cout < 0) { 560 diag("cannot create %s: %r", p); 561 errorexit(); 562 } 563 free(p); 564 } 565 566 void 567 hostlink(void) 568 { 569 char *p, **argv; 570 int c, i, w, n, argc, len; 571 Hostobj *h; 572 Biobuf *f; 573 static char buf[64<<10]; 574 575 if(linkmode != LinkExternal || nerrors > 0) 576 return; 577 578 c = 0; 579 p = extldflags; 580 while(p != nil) { 581 while(*p == ' ') 582 p++; 583 if(*p == '\0') 584 break; 585 c++; 586 p = strchr(p + 1, ' '); 587 } 588 589 argv = malloc((14+nhostobj+nldflag+c)*sizeof argv[0]); 590 argc = 0; 591 if(extld == nil) 592 extld = "gcc"; 593 argv[argc++] = extld; 594 switch(thechar){ 595 case '8': 596 argv[argc++] = "-m32"; 597 break; 598 case '6': 599 argv[argc++] = "-m64"; 600 break; 601 case '5': 602 argv[argc++] = "-marm"; 603 break; 604 } 605 if(!debug['s'] && !debug_s) { 606 argv[argc++] = "-gdwarf-2"; 607 } else { 608 argv[argc++] = "-s"; 609 } 610 if(HEADTYPE == Hdarwin) 611 argv[argc++] = "-Wl,-no_pie,-pagezero_size,4000000"; 612 if(HEADTYPE == Hopenbsd) 613 argv[argc++] = "-Wl,-nopie"; 614 615 if(iself && AssumeGoldLinker) 616 argv[argc++] = "-Wl,--rosegment"; 617 618 if(flag_shared) { 619 argv[argc++] = "-Wl,-Bsymbolic"; 620 argv[argc++] = "-shared"; 621 } 622 argv[argc++] = "-o"; 623 argv[argc++] = outfile; 624 625 if(rpath) 626 argv[argc++] = smprint("-Wl,-rpath,%s", rpath); 627 628 // Force global symbols to be exported for dlopen, etc. 629 if(iself) 630 argv[argc++] = "-Wl,-export-dynamic"; 631 632 if(strstr(argv[0], "clang") != nil) 633 argv[argc++] = "-Qunused-arguments"; 634 635 // already wrote main object file 636 // copy host objects to temporary directory 637 for(i=0; i<nhostobj; i++) { 638 h = &hostobj[i]; 639 f = Bopen(h->file, OREAD); 640 if(f == nil) { 641 ctxt->cursym = S; 642 diag("cannot reopen %s: %r", h->pn); 643 errorexit(); 644 } 645 Bseek(f, h->off, 0); 646 p = smprint("%s/%06d.o", tmpdir, i); 647 argv[argc++] = p; 648 w = create(p, 1, 0775); 649 if(w < 0) { 650 ctxt->cursym = S; 651 diag("cannot create %s: %r", p); 652 errorexit(); 653 } 654 len = h->len; 655 while(len > 0 && (n = Bread(f, buf, sizeof buf)) > 0){ 656 if(n > len) 657 n = len; 658 dowrite(w, buf, n); 659 len -= n; 660 } 661 if(close(w) < 0) { 662 ctxt->cursym = S; 663 diag("cannot write %s: %r", p); 664 errorexit(); 665 } 666 Bterm(f); 667 } 668 669 argv[argc++] = smprint("%s/go.o", tmpdir); 670 for(i=0; i<nldflag; i++) 671 argv[argc++] = ldflag[i]; 672 673 p = extldflags; 674 while(p != nil) { 675 while(*p == ' ') 676 *p++ = '\0'; 677 if(*p == '\0') 678 break; 679 argv[argc++] = p; 680 681 // clang, unlike GCC, passes -rdynamic to the linker 682 // even when linking with -static, causing a linker 683 // error when using GNU ld. So take out -rdynamic if 684 // we added it. We do it in this order, rather than 685 // only adding -rdynamic later, so that -extldflags 686 // can override -rdynamic without using -static. 687 if(iself && strncmp(p, "-static", 7) == 0 && (p[7]==' ' || p[7]=='\0')) { 688 for(i=0; i<argc; i++) { 689 if(strcmp(argv[i], "-rdynamic") == 0) 690 argv[i] = "-static"; 691 } 692 } 693 694 p = strchr(p + 1, ' '); 695 } 696 697 argv[argc] = nil; 698 699 quotefmtinstall(); 700 if(debug['v']) { 701 Bprint(&bso, "host link:"); 702 for(i=0; i<argc; i++) 703 Bprint(&bso, " %q", argv[i]); 704 Bprint(&bso, "\n"); 705 Bflush(&bso); 706 } 707 708 if(runcmd(argv) < 0) { 709 ctxt->cursym = S; 710 diag("%s: running %s failed: %r", argv0, argv[0]); 711 errorexit(); 712 } 713 } 714 715 void 716 ldobj(Biobuf *f, char *pkg, int64 len, char *pn, char *file, int whence) 717 { 718 char *line; 719 int n, c1, c2, c3, c4; 720 uint32 magic; 721 vlong import0, import1, eof; 722 char *t; 723 724 eof = Boffset(f) + len; 725 726 pn = estrdup(pn); 727 728 c1 = BGETC(f); 729 c2 = BGETC(f); 730 c3 = BGETC(f); 731 c4 = BGETC(f); 732 Bungetc(f); 733 Bungetc(f); 734 Bungetc(f); 735 Bungetc(f); 736 737 magic = c1<<24 | c2<<16 | c3<<8 | c4; 738 if(magic == 0x7f454c46) { // \x7F E L F 739 ldhostobj(ldelf, f, pkg, len, pn, file); 740 return; 741 } 742 if((magic&~1) == 0xfeedface || (magic&~0x01000000) == 0xcefaedfe) { 743 ldhostobj(ldmacho, f, pkg, len, pn, file); 744 return; 745 } 746 if(c1 == 0x4c && c2 == 0x01 || c1 == 0x64 && c2 == 0x86) { 747 ldhostobj(ldpe, f, pkg, len, pn, file); 748 return; 749 } 750 751 /* check the header */ 752 line = Brdline(f, '\n'); 753 if(line == nil) { 754 if(Blinelen(f) > 0) { 755 diag("%s: not an object file", pn); 756 return; 757 } 758 goto eof; 759 } 760 n = Blinelen(f) - 1; 761 line[n] = '\0'; 762 if(strncmp(line, "go object ", 10) != 0) { 763 if(strlen(pn) > 3 && strcmp(pn+strlen(pn)-3, ".go") == 0) { 764 print("%cl: input %s is not .%c file (use %cg to compile .go files)\n", thechar, pn, thechar, thechar); 765 errorexit(); 766 } 767 if(strcmp(line, thestring) == 0) { 768 // old header format: just $GOOS 769 diag("%s: stale object file", pn); 770 return; 771 } 772 diag("%s: not an object file", pn); 773 free(pn); 774 return; 775 } 776 777 // First, check that the basic goos, goarch, and version match. 778 t = smprint("%s %s %s ", goos, getgoarch(), getgoversion()); 779 line[n] = ' '; 780 if(strncmp(line+10, t, strlen(t)) != 0 && !debug['f']) { 781 line[n] = '\0'; 782 diag("%s: object is [%s] expected [%s]", pn, line+10, t); 783 free(t); 784 free(pn); 785 return; 786 } 787 788 // Second, check that longer lines match each other exactly, 789 // so that the Go compiler and write additional information 790 // that must be the same from run to run. 791 line[n] = '\0'; 792 if(n-10 > strlen(t)) { 793 if(theline == nil) 794 theline = estrdup(line+10); 795 else if(strcmp(theline, line+10) != 0) { 796 line[n] = '\0'; 797 diag("%s: object is [%s] expected [%s]", pn, line+10, theline); 798 free(t); 799 free(pn); 800 return; 801 } 802 } 803 free(t); 804 line[n] = '\n'; 805 806 /* skip over exports and other info -- ends with \n!\n */ 807 import0 = Boffset(f); 808 c1 = '\n'; // the last line ended in \n 809 c2 = BGETC(f); 810 c3 = BGETC(f); 811 while(c1 != '\n' || c2 != '!' || c3 != '\n') { 812 c1 = c2; 813 c2 = c3; 814 c3 = BGETC(f); 815 if(c3 == Beof) 816 goto eof; 817 } 818 import1 = Boffset(f); 819 820 Bseek(f, import0, 0); 821 ldpkg(f, pkg, import1 - import0 - 2, pn, whence); // -2 for !\n 822 Bseek(f, import1, 0); 823 824 ldobjfile(ctxt, f, pkg, eof - Boffset(f), pn); 825 free(pn); 826 return; 827 828 eof: 829 diag("truncated object file: %s", pn); 830 free(pn); 831 } 832 833 void 834 zerosig(char *sp) 835 { 836 LSym *s; 837 838 s = linklookup(ctxt, sp, 0); 839 s->sig = 0; 840 } 841 842 void 843 mywhatsys(void) 844 { 845 goroot = getgoroot(); 846 goos = getgoos(); 847 goarch = getgoarch(); 848 849 if(strncmp(goarch, thestring, strlen(thestring)) != 0) 850 sysfatal("cannot use %cc with GOARCH=%s", thechar, goarch); 851 } 852 853 int 854 pathchar(void) 855 { 856 return '/'; 857 } 858 859 static uchar* hunk; 860 static uint32 nhunk; 861 #define NHUNK (10UL<<20) 862 863 void* 864 mal(uint32 n) 865 { 866 void *v; 867 868 n = (n+7)&~7; 869 if(n > NHUNK) { 870 v = malloc(n); 871 if(v == nil) { 872 diag("out of memory"); 873 errorexit(); 874 } 875 memset(v, 0, n); 876 return v; 877 } 878 if(n > nhunk) { 879 hunk = malloc(NHUNK); 880 if(hunk == nil) { 881 diag("out of memory"); 882 errorexit(); 883 } 884 nhunk = NHUNK; 885 } 886 887 v = hunk; 888 nhunk -= n; 889 hunk += n; 890 891 memset(v, 0, n); 892 return v; 893 } 894 895 void 896 unmal(void *v, uint32 n) 897 { 898 n = (n+7)&~7; 899 if(hunk - n == v) { 900 hunk -= n; 901 nhunk += n; 902 } 903 } 904 905 // Copied from ../gc/subr.c:/^pathtoprefix; must stay in sync. 906 /* 907 * Convert raw string to the prefix that will be used in the symbol table. 908 * Invalid bytes turn into %xx. Right now the only bytes that need 909 * escaping are %, ., and ", but we escape all control characters too. 910 * 911 * If you edit this, edit ../gc/subr.c:/^pathtoprefix too. 912 * If you edit this, edit ../../debug/goobj/read.go:/importPathToPrefix too. 913 */ 914 static char* 915 pathtoprefix(char *s) 916 { 917 static char hex[] = "0123456789abcdef"; 918 char *p, *r, *w, *l; 919 int n; 920 921 // find first character past the last slash, if any. 922 l = s; 923 for(r=s; *r; r++) 924 if(*r == '/') 925 l = r+1; 926 927 // check for chars that need escaping 928 n = 0; 929 for(r=s; *r; r++) 930 if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) 931 n++; 932 933 // quick exit 934 if(n == 0) 935 return s; 936 937 // escape 938 p = mal((r-s)+1+2*n); 939 for(r=s, w=p; *r; r++) { 940 if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) { 941 *w++ = '%'; 942 *w++ = hex[(*r>>4)&0xF]; 943 *w++ = hex[*r&0xF]; 944 } else 945 *w++ = *r; 946 } 947 *w = '\0'; 948 return p; 949 } 950 951 int 952 iconv(Fmt *fp) 953 { 954 char *p; 955 956 p = va_arg(fp->args, char*); 957 if(p == nil) { 958 fmtstrcpy(fp, "<nil>"); 959 return 0; 960 } 961 p = pathtoprefix(p); 962 fmtstrcpy(fp, p); 963 return 0; 964 } 965 966 Section* 967 addsection(Segment *seg, char *name, int rwx) 968 { 969 Section **l; 970 Section *sect; 971 972 for(l=&seg->sect; *l; l=&(*l)->next) 973 ; 974 sect = mal(sizeof *sect); 975 sect->rwx = rwx; 976 sect->name = name; 977 sect->seg = seg; 978 sect->align = PtrSize; // everything is at least pointer-aligned 979 *l = sect; 980 return sect; 981 } 982 983 uint16 984 le16(uchar *b) 985 { 986 return b[0] | b[1]<<8; 987 } 988 989 uint32 990 le32(uchar *b) 991 { 992 return b[0] | b[1]<<8 | b[2]<<16 | (uint32)b[3]<<24; 993 } 994 995 uint64 996 le64(uchar *b) 997 { 998 return le32(b) | (uint64)le32(b+4)<<32; 999 } 1000 1001 uint16 1002 be16(uchar *b) 1003 { 1004 return b[0]<<8 | b[1]; 1005 } 1006 1007 uint32 1008 be32(uchar *b) 1009 { 1010 return (uint32)b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3]; 1011 } 1012 1013 uint64 1014 be64(uchar *b) 1015 { 1016 return (uvlong)be32(b)<<32 | be32(b+4); 1017 } 1018 1019 Endian be = { be16, be32, be64 }; 1020 Endian le = { le16, le32, le64 }; 1021 1022 typedef struct Chain Chain; 1023 struct Chain 1024 { 1025 LSym *sym; 1026 Chain *up; 1027 int limit; // limit on entry to sym 1028 }; 1029 1030 static int stkcheck(Chain*, int); 1031 static void stkprint(Chain*, int); 1032 static void stkbroke(Chain*, int); 1033 static LSym *morestack; 1034 static LSym *newstack; 1035 1036 enum 1037 { 1038 HasLinkRegister = (thechar == '5'), 1039 }; 1040 1041 // TODO: Record enough information in new object files to 1042 // allow stack checks here. 1043 1044 static int 1045 callsize(void) 1046 { 1047 if(thechar == '5') 1048 return 0; 1049 return RegSize; 1050 } 1051 1052 void 1053 dostkcheck(void) 1054 { 1055 Chain ch; 1056 LSym *s; 1057 1058 morestack = linklookup(ctxt, "runtime.morestack", 0); 1059 newstack = linklookup(ctxt, "runtime.newstack", 0); 1060 1061 // Every splitting function ensures that there are at least StackLimit 1062 // bytes available below SP when the splitting prologue finishes. 1063 // If the splitting function calls F, then F begins execution with 1064 // at least StackLimit - callsize() bytes available. 1065 // Check that every function behaves correctly with this amount 1066 // of stack, following direct calls in order to piece together chains 1067 // of non-splitting functions. 1068 ch.up = nil; 1069 ch.limit = StackLimit - callsize(); 1070 1071 // Check every function, but do the nosplit functions in a first pass, 1072 // to make the printed failure chains as short as possible. 1073 for(s = ctxt->textp; s != nil; s = s->next) { 1074 // runtime.racesymbolizethunk is called from gcc-compiled C 1075 // code running on the operating system thread stack. 1076 // It uses more than the usual amount of stack but that's okay. 1077 if(strcmp(s->name, "runtime.racesymbolizethunk") == 0) 1078 continue; 1079 1080 if(s->nosplit) { 1081 ctxt->cursym = s; 1082 ch.sym = s; 1083 stkcheck(&ch, 0); 1084 } 1085 } 1086 for(s = ctxt->textp; s != nil; s = s->next) { 1087 if(!s->nosplit) { 1088 ctxt->cursym = s; 1089 ch.sym = s; 1090 stkcheck(&ch, 0); 1091 } 1092 } 1093 } 1094 1095 static int 1096 stkcheck(Chain *up, int depth) 1097 { 1098 Chain ch, ch1; 1099 LSym *s; 1100 int limit; 1101 Reloc *r, *endr; 1102 Pciter pcsp; 1103 1104 limit = up->limit; 1105 s = up->sym; 1106 1107 // Don't duplicate work: only need to consider each 1108 // function at top of safe zone once. 1109 if(limit == StackLimit-callsize()) { 1110 if(s->stkcheck) 1111 return 0; 1112 s->stkcheck = 1; 1113 } 1114 1115 if(depth > 100) { 1116 diag("nosplit stack check too deep"); 1117 stkbroke(up, 0); 1118 return -1; 1119 } 1120 1121 if(s->external || s->pcln == nil) { 1122 // external function. 1123 // should never be called directly. 1124 // only diagnose the direct caller. 1125 if(depth == 1 && s->type != SXREF) 1126 diag("call to external function %s", s->name); 1127 return -1; 1128 } 1129 1130 if(limit < 0) { 1131 stkbroke(up, limit); 1132 return -1; 1133 } 1134 1135 // morestack looks like it calls functions, 1136 // but it switches the stack pointer first. 1137 if(s == morestack) 1138 return 0; 1139 1140 ch.up = up; 1141 1142 // Walk through sp adjustments in function, consuming relocs. 1143 r = s->r; 1144 endr = r + s->nr; 1145 for(pciterinit(ctxt, &pcsp, &s->pcln->pcsp); !pcsp.done; pciternext(&pcsp)) { 1146 // pcsp.value is in effect for [pcsp.pc, pcsp.nextpc). 1147 1148 // Check stack size in effect for this span. 1149 if(limit - pcsp.value < 0) { 1150 stkbroke(up, limit - pcsp.value); 1151 return -1; 1152 } 1153 1154 // Process calls in this span. 1155 for(; r < endr && r->off < pcsp.nextpc; r++) { 1156 switch(r->type) { 1157 case R_CALL: 1158 case R_CALLARM: 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(thechar == '5') 1170 limit += 4; // saved LR 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)*PtrSize); 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->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 }