Skip to content

Commit e4c3925

Browse files
committed
cmd/link: use libmsvcrt.a during internal link
When using recent versions of gcc with cgo, internal link fails with c:\>go test debug/pe --- FAIL: TestInternalLinkerDWARF (0.94s)     file_test.go:394: building test executable for linktype 2 failed: exit status 2 # command-line-arguments         runtime/cgo(.text): relocation target __acrt_iob_func not defined for ABI0 (but is defined for ABI0)         runtime/cgo(.text): relocation target __acrt_iob_func not defined for ABI0 (but is defined for ABI0)         runtime/cgo(.text): relocation target __acrt_iob_func not defined for ABI0 (but is defined for ABI0) FAIL FAIL    debug/pe        4.572s FAIL It appears that __acrt_iob_func is defined in libmsvcrt.a. And this change adds libmsvcrt.a to the list of libraries always used byi internal linker. libmsvcrt.a also implements __imp___acrt_iob_func. So this change also prevents rewriting __imp___acrt_iob_func name into __acrt_iob_func, otherwise we end up with duplicate __acrt_iob_func symbol error. Fixes #23649 Change-Id: Ie9864cd17e907501e9a8a3672bbc33e02ca20e5c Reviewed-on: https://go-review.googlesource.com/c/go/+/197977 Run-TryBot: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 39cfb76 commit e4c3925

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/cmd/link/internal/ld/lib.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ func (ctxt *Link) loadlib() {
582582
if p := ctxt.findLibPath("libmingw32.a"); p != "none" {
583583
hostArchive(ctxt, p)
584584
}
585+
// Link libmsvcrt.a to resolve '__acrt_iob_func' symbol
586+
// (see https://golang.org/issue/23649 for details).
587+
if p := ctxt.findLibPath("libmsvcrt.a"); p != "none" {
588+
hostArchive(ctxt, p)
589+
}
585590
// TODO: maybe do something similar to peimporteddlls to collect all lib names
586591
// and try link them all to final exe just like libmingwex.a and libmingw32.a:
587592
/*

src/cmd/link/internal/loadpe/ldpe.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,26 @@ func readpesym(arch *sys.Arch, syms *sym.Symbols, f *pe.File, pesym *pe.COFFSymb
445445
name = sectsyms[f.Sections[pesym.SectionNumber-1]].Name
446446
} else {
447447
name = symname
448-
name = strings.TrimPrefix(name, "__imp_") // __imp_Name => Name
449-
if arch.Family == sys.I386 && name[0] == '_' {
450-
name = name[1:] // _Name => Name
448+
switch arch.Family {
449+
case sys.AMD64:
450+
if name == "__imp___acrt_iob_func" {
451+
// Do not rename __imp___acrt_iob_func into __acrt_iob_func,
452+
// becasue __imp___acrt_iob_func symbol is real
453+
// (see commit b295099 from git://git.code.sf.net/p/mingw-w64/mingw-w64 for detials).
454+
} else {
455+
name = strings.TrimPrefix(name, "__imp_") // __imp_Name => Name
456+
}
457+
case sys.I386:
458+
if name == "__imp____acrt_iob_func" {
459+
// Do not rename __imp____acrt_iob_func into ___acrt_iob_func,
460+
// becasue __imp____acrt_iob_func symbol is real
461+
// (see commit b295099 from git://git.code.sf.net/p/mingw-w64/mingw-w64 for detials).
462+
} else {
463+
name = strings.TrimPrefix(name, "__imp_") // __imp_Name => Name
464+
}
465+
if name[0] == '_' {
466+
name = name[1:] // _Name => Name
467+
}
451468
}
452469
}
453470

0 commit comments

Comments
 (0)