289
320
objfile(library[i].file, library[i].pkg);
323
if(linkmode == LinkAuto) {
324
if(iscgo && externalobj)
325
linkmode = LinkExternal;
327
linkmode = LinkInternal;
330
if(linkmode == LinkInternal) {
331
// Drop all the cgo_import_static declarations.
332
// Turns out we won't be needing them.
333
for(s = allsym; s != S; s = s->allsym)
334
if(s->type == SHOSTOBJ) {
335
// If a symbol was marked both
336
// cgo_import_static and cgo_import_dynamic,
337
// then we want to make it cgo_import_dynamic
339
if(s->extname != nil && s->dynimplib != nil && s->cgoexport == 0) {
340
s->type = SDYNIMPORT;
346
// Now that we know the link mode, trim the dynexp list.
347
x = CgoExportDynamic;
348
if(linkmode == LinkExternal)
351
for(i=0; i<ndynexp; i++)
352
if(dynexp[i]->cgoexport & x)
353
dynexp[w++] = dynexp[i];
356
// In internal link mode, read the host object files.
357
if(linkmode == LinkInternal)
292
362
// We've loaded all the code now.
293
363
// If there are no dynamic libraries needed, gcc disables dynamic linking.
294
364
// Because of this, glibc's dynamic ELF loader occasionally (like in version 2.13)
431
ldobj(Biobuf *f, char *pkg, int64 len, char *pn, int whence)
501
dowrite(int fd, char *p, int n)
509
diag("write error: %r");
517
typedef struct Hostobj Hostobj;
521
void (*ld)(Biobuf*, char*, int64, char*);
533
// These packages can use internal linking mode.
534
// Others trigger external mode.
535
const char *internalpkg[] = {
544
ldhostobj(void (*ld)(Biobuf*, char*, int64, char*), Biobuf *f, char *pkg, int64 len, char *pn, char *file)
550
for(i=0; i<nelem(internalpkg); i++) {
551
if(strcmp(pkg, internalpkg[i]) == 0) {
560
if(nhostobj >= mhostobj) {
565
hostobj = erealloc(hostobj, mhostobj*sizeof hostobj[0]);
567
h = &hostobj[nhostobj++];
569
h->pkg = estrdup(pkg);
571
h->file = estrdup(file);
583
for(i=0; i<nhostobj; i++) {
585
f = Bopen(h->file, OREAD);
588
diag("cannot reopen %s: %r", h->pn);
592
h->ld(f, h->pkg, h->len, h->pn);
599
char* mktempdir(void);
600
void removeall(char*);
613
if(linkmode != LinkExternal)
616
// create temporary directory and arrange cleanup
618
tmpdir = mktempdir();
622
// change our output to temporary object file
624
p = smprint("%s/go.o", tmpdir);
625
cout = create(p, 1, 0775);
627
diag("cannot create %s: %r", p);
637
int c, i, w, n, argc, len;
640
static char buf[64<<10];
642
if(linkmode != LinkExternal || nerrors > 0)
653
p = strchr(p + 1, ' ');
656
argv = malloc((10+nhostobj+nldflag+c)*sizeof argv[0]);
660
argv[argc++] = extld;
663
argv[argc++] = "-m32";
666
argv[argc++] = "-m64";
670
argv[argc++] = "-gdwarf-2";
671
if(HEADTYPE == Hdarwin)
672
argv[argc++] = "-Wl,-no_pie,-pagezero_size,4000000";
674
argv[argc++] = outfile;
677
argv[argc++] = smprint("-Wl,-rpath,%s", rpath);
679
// Force global symbols to be exported for dlopen, etc.
681
argv[argc++] = "-rdynamic";
683
// already wrote main object file
684
// copy host objects to temporary directory
685
for(i=0; i<nhostobj; i++) {
687
f = Bopen(h->file, OREAD);
690
diag("cannot reopen %s: %r", h->pn);
694
p = smprint("%s/%06d.o", tmpdir, i);
696
w = create(p, 1, 0775);
699
diag("cannot create %s: %r", p);
703
while(len > 0 && (n = Bread(f, buf, sizeof buf)) > 0){
711
diag("cannot write %s: %r", p);
717
argv[argc++] = smprint("%s/go.o", tmpdir);
718
for(i=0; i<nldflag; i++)
719
argv[argc++] = ldflag[i];
728
p = strchr(p + 1, ' ');
735
Bprint(&bso, "host link:");
736
for(i=0; i<argc; i++)
737
Bprint(&bso, " %q", argv[i]);
742
if(runcmd(argv) < 0) {
744
diag("%s: running %s failed: %r", argv0, argv[0]);
750
ldobj(Biobuf *f, char *pkg, int64 len, char *pn, char *file, int whence)
434
753
int n, c1, c2, c3, c4;
452
771
magic = c1<<24 | c2<<16 | c3<<8 | c4;
453
772
if(magic == 0x7f454c46) { // \x7F E L F
454
ldelf(f, pkg, len, pn);
773
ldhostobj(ldelf, f, pkg, len, pn, file);
458
776
if((magic&~1) == 0xfeedface || (magic&~0x01000000) == 0xcefaedfe) {
459
ldmacho(f, pkg, len, pn);
777
ldhostobj(ldmacho, f, pkg, len, pn, file);
463
780
if(c1 == 0x4c && c2 == 0x01 || c1 == 0x64 && c2 == 0x86) {
464
ldpe(f, pkg, len, pn);
781
ldhostobj(ldpe, f, pkg, len, pn, file);
868
newsym(char *symb, int v)
873
l = strlen(symb) + 1;
876
Bprint(&bso, "newsym %s\n", symb);
881
s->name = mal(l + 1);
882
memmove(s->name, symb, l);
552
898
_lookup(char *symb, int v, int creat)
560
906
for(p=symb; c = *p; p++)
563
908
// not if(h < 0) h = ~h, because gcc 4.3 -O2 miscompiles it.
566
911
for(s = hash[h]; s != S; s = s->hash)
567
if(memcmp(s->name, symb, l) == 0)
912
if(strcmp(s->name, symb) == 0)
574
Bprint(&bso, "lookup %s\n", symb);
579
s->name = mal(l + 1);
580
memmove(s->name, symb, l);
918
s->extname = s->name;
582
919
s->hash = hash[h];
1472
1810
cwrite(void *buf, int n)
1475
if(write(cout, buf, n) != n) {
1476
diag("write error: %r");
1815
dowrite(cout, buf, n);
1822
fprint(2, "usage: %cl [options] main.%c\n", thechar, thechar);
1828
setheadtype(char *s)
1830
HEADTYPE = headtype(s);
1836
debug['I'] = 1; // denote cmdline interpreter override
1843
print("%cl version %s\n", thechar, getgoversion());
1848
genasmsym(void (*put)(Sym*, char*, int, vlong, vlong, int, Sym*))
1854
// These symbols won't show up in the first loop below because we
1855
// skip STEXT symbols. Normal STEXT symbols are emitted by walking textp.
1856
s = lookup("text", 0);
1857
if(s->type == STEXT)
1858
put(s, s->name, 'T', s->value, s->size, s->version, 0);
1859
s = lookup("etext", 0);
1860
if(s->type == STEXT)
1861
put(s, s->name, 'T', s->value, s->size, s->version, 0);
1863
for(s=allsym; s!=S; s=s->allsym) {
1864
if(s->hide || (s->name[0] == '.' && s->version == 0 && strcmp(s->name, ".rathole") != 0))
1866
switch(s->type&SMASK) {
1881
put(s, s->name, 'D', symaddr(s), s->size, s->version, s->gotype);
1889
diag("%s should not be bss (size=%d type=%d special=%d)", s->name, (int)s->np, s->type, s->special);
1890
put(s, s->name, 'B', symaddr(s), s->size, s->version, s->gotype);
1894
put(nil, s->name, 'f', s->value, 0, s->version, 0);
1899
for(s = textp; s != nil; s = s->next) {
1903
/* filenames first */
1904
for(a=s->autom; a; a=a->link)
1905
if(a->type == D_FILE)
1906
put(nil, a->asym->name, 'z', a->aoffset, 0, 0, 0);
1908
if(a->type == D_FILE1)
1909
put(nil, a->asym->name, 'Z', a->aoffset, 0, 0, 0);
1911
put(s, s->name, 'T', s->value, s->size, s->version, s->gotype);
1913
/* frame, locals, args, auto and param after */
1914
put(nil, ".frame", 'm', (uint32)s->text->to.offset+PtrSize, 0, 0, 0);
1915
put(nil, ".locals", 'm', s->locals, 0, 0, 0);
1916
if(s->text->textflag & NOSPLIT)
1917
put(nil, ".args", 'm', ArgsSizeUnknown, 0, 0, 0);
1919
put(nil, ".args", 'm', s->args, 0, 0, 0);
1921
for(a=s->autom; a; a=a->link) {
1922
// Emit a or p according to actual offset, even if label is wrong.
1923
// This avoids negative offsets, which cannot be encoded.
1924
if(a->type != D_AUTO && a->type != D_PARAM)
1927
// compute offset relative to FP
1928
if(a->type == D_PARAM)
1931
off = a->aoffset - PtrSize;
1935
put(nil, a->asym->name, 'p', off, 0, 0, a->gotype);
1940
if(off <= -PtrSize) {
1941
put(nil, a->asym->name, 'a', -(off+PtrSize), 0, 0, a->gotype);
1945
// Otherwise, off is addressing the saved program counter.
1946
// Something underhanded is going on. Say nothing.
1949
if(debug['v'] || debug['n'])
1950
Bprint(&bso, "symsize = %ud\n", symsize);
1960
diag("out of memory");
1967
erealloc(void *p, long n)
1972
diag("out of memory");