Skip to content

Commit 04642e9

Browse files
committed
cmd/internal/ld, cmd/8l: external linking for windows/386
Update #4069: this CL fixes the issue on windows/386. Signed-off-by: Shenghou Ma <[email protected]> Change-Id: I2d2ea233f976aab3f356f9b508cdd246d5013e2e Reviewed-on: https://go-review.googlesource.com/7283 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent b6ed943 commit 04642e9

File tree

6 files changed

+309
-41
lines changed

6 files changed

+309
-41
lines changed

src/cmd/8l/asm.go

+35
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ func adddynrel(s *ld.LSym, r *ld.Reloc) {
235235
r.Type = 256 // ignore during relocsym
236236
return
237237
}
238+
239+
if ld.HEADTYPE == ld.Hwindows && s.Size == PtrSize {
240+
// nothing to do, the relocation will be laid out in pereloc1
241+
return
242+
}
238243
}
239244

240245
ld.Ctxt.Cursym = s
@@ -332,6 +337,36 @@ func machoreloc1(r *ld.Reloc, sectoff int64) int {
332337
return 0
333338
}
334339

340+
func pereloc1(r *ld.Reloc, sectoff int64) bool {
341+
var v uint32
342+
343+
rs := r.Xsym
344+
345+
if rs.Dynid < 0 {
346+
ld.Diag("reloc %d to non-coff symbol %s type=%d", r.Type, rs.Name, rs.Type)
347+
return false
348+
}
349+
350+
ld.Thearch.Lput(uint32(sectoff))
351+
ld.Thearch.Lput(uint32(rs.Dynid))
352+
353+
switch r.Type {
354+
default:
355+
return false
356+
357+
case ld.R_ADDR:
358+
v = ld.IMAGE_REL_I386_DIR32
359+
360+
case ld.R_CALL,
361+
ld.R_PCREL:
362+
v = ld.IMAGE_REL_I386_REL32
363+
}
364+
365+
ld.Thearch.Wput(uint16(v))
366+
367+
return true
368+
}
369+
335370
func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
336371
if ld.Linkmode == ld.LinkExternal {
337372
return -1

src/cmd/8l/obj.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func linkarchinit() {
6868
ld.Thearch.Elfsetupplt = elfsetupplt
6969
ld.Thearch.Gentext = gentext
7070
ld.Thearch.Machoreloc1 = machoreloc1
71+
ld.Thearch.PEreloc1 = pereloc1
7172
ld.Thearch.Lput = ld.Lputl
7273
ld.Thearch.Wput = ld.Wputl
7374
ld.Thearch.Vput = ld.Vputl
@@ -99,7 +100,8 @@ func archinit() {
99100
ld.Hfreebsd,
100101
ld.Hlinux,
101102
ld.Hnetbsd,
102-
ld.Hopenbsd:
103+
ld.Hopenbsd,
104+
ld.Hwindows:
103105
break
104106
}
105107

src/cmd/internal/ld/data.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ func relocsym(s *LSym) {
443443
if rs.Type != SHOSTOBJ {
444444
o += Symaddr(rs)
445445
}
446+
} else if HEADTYPE == Hwindows {
447+
// nothing to do
446448
} else {
447449
Diag("unhandled pcrel relocation for %s", headstring)
448450
}
@@ -497,6 +499,8 @@ func relocsym(s *LSym) {
497499
} else {
498500
o += int64(r.Siz)
499501
}
502+
} else if HEADTYPE == Hwindows {
503+
// nothing to do
500504
} else {
501505
Diag("unhandled pcrel relocation for %s", headstring)
502506
}
@@ -584,7 +588,7 @@ func reloc() {
584588
}
585589

586590
func dynrelocsym(s *LSym) {
587-
if HEADTYPE == Hwindows {
591+
if HEADTYPE == Hwindows && Linkmode != LinkExternal {
588592
rel := Linklookup(Ctxt, ".rel", 0)
589593
if s == rel {
590594
return

src/cmd/internal/ld/go.go

-5
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,6 @@ func loadcgo(file string, pkg string, p string) {
462462
}
463463

464464
if f[0] == "cgo_export_static" || f[0] == "cgo_export_dynamic" {
465-
// TODO: Remove once we know Windows is okay.
466-
if f[0] == "cgo_export_static" && HEADTYPE == Hwindows {
467-
continue
468-
}
469-
470465
if len(f) < 2 || len(f) > 3 {
471466
goto err
472467
}

src/cmd/internal/ld/lib.go

+18
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type Arch struct {
9999
Elfsetupplt func()
100100
Gentext func()
101101
Machoreloc1 func(*Reloc, int64) int
102+
PEreloc1 func(*Reloc, int64) bool
102103
Lput func(uint32)
103104
Wput func(uint16)
104105
Vput func(uint64)
@@ -744,6 +745,13 @@ func hostlink() {
744745
if HEADTYPE == Hopenbsd {
745746
argv = append(argv, "-Wl,-nopie")
746747
}
748+
if HEADTYPE == Hwindows {
749+
if headstring == "windowsgui" {
750+
argv = append(argv, "-mwindows")
751+
} else {
752+
argv = append(argv, "-mconsole")
753+
}
754+
}
747755

748756
if Iself && AssumeGoldLinker != 0 /*TypeKind(100016)*/ {
749757
argv = append(argv, "-Wl,--rosegment")
@@ -844,6 +852,9 @@ func hostlink() {
844852
}
845853
}
846854
}
855+
if HEADTYPE == Hwindows {
856+
argv = append(argv, peimporteddlls()...)
857+
}
847858

848859
if Debug['v'] != 0 {
849860
fmt.Fprintf(&Bso, "host link:")
@@ -1379,6 +1390,13 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) {
13791390
case SFILE:
13801391
put(nil, s.Name, 'f', s.Value, 0, int(s.Version), nil)
13811392
continue
1393+
1394+
case SHOSTOBJ:
1395+
if HEADTYPE == Hwindows {
1396+
put(s, s.Name, 'U', s.Value, 0, int(s.Version), nil)
1397+
}
1398+
continue
1399+
13821400
}
13831401
}
13841402

0 commit comments

Comments
 (0)