Skip to content

Commit

Permalink
cmd/internal/ld, cmd/8l: external linking for windows/386
Browse files Browse the repository at this point in the history
Update #4069: this CL fixes the issue on windows/386.

Signed-off-by: Shenghou Ma <minux@golang.org>
Change-Id: I2d2ea233f976aab3f356f9b508cdd246d5013e2e
Reviewed-on: https://go-review.googlesource.com/7283
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
minux committed Mar 24, 2015
1 parent b6ed943 commit 04642e9
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 41 deletions.
35 changes: 35 additions & 0 deletions src/cmd/8l/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ func adddynrel(s *ld.LSym, r *ld.Reloc) {
r.Type = 256 // ignore during relocsym
return
}

if ld.HEADTYPE == ld.Hwindows && s.Size == PtrSize {
// nothing to do, the relocation will be laid out in pereloc1
return
}
}

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

func pereloc1(r *ld.Reloc, sectoff int64) bool {
var v uint32

rs := r.Xsym

if rs.Dynid < 0 {
ld.Diag("reloc %d to non-coff symbol %s type=%d", r.Type, rs.Name, rs.Type)
return false
}

ld.Thearch.Lput(uint32(sectoff))
ld.Thearch.Lput(uint32(rs.Dynid))

switch r.Type {
default:
return false

case ld.R_ADDR:
v = ld.IMAGE_REL_I386_DIR32

case ld.R_CALL,
ld.R_PCREL:
v = ld.IMAGE_REL_I386_REL32
}

ld.Thearch.Wput(uint16(v))

return true
}

func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
if ld.Linkmode == ld.LinkExternal {
return -1
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/8l/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func linkarchinit() {
ld.Thearch.Elfsetupplt = elfsetupplt
ld.Thearch.Gentext = gentext
ld.Thearch.Machoreloc1 = machoreloc1
ld.Thearch.PEreloc1 = pereloc1
ld.Thearch.Lput = ld.Lputl
ld.Thearch.Wput = ld.Wputl
ld.Thearch.Vput = ld.Vputl
Expand Down Expand Up @@ -99,7 +100,8 @@ func archinit() {
ld.Hfreebsd,
ld.Hlinux,
ld.Hnetbsd,
ld.Hopenbsd:
ld.Hopenbsd,
ld.Hwindows:
break
}

Expand Down
6 changes: 5 additions & 1 deletion src/cmd/internal/ld/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ func relocsym(s *LSym) {
if rs.Type != SHOSTOBJ {
o += Symaddr(rs)
}
} else if HEADTYPE == Hwindows {
// nothing to do
} else {
Diag("unhandled pcrel relocation for %s", headstring)
}
Expand Down Expand Up @@ -497,6 +499,8 @@ func relocsym(s *LSym) {
} else {
o += int64(r.Siz)
}
} else if HEADTYPE == Hwindows {
// nothing to do
} else {
Diag("unhandled pcrel relocation for %s", headstring)
}
Expand Down Expand Up @@ -584,7 +588,7 @@ func reloc() {
}

func dynrelocsym(s *LSym) {
if HEADTYPE == Hwindows {
if HEADTYPE == Hwindows && Linkmode != LinkExternal {
rel := Linklookup(Ctxt, ".rel", 0)
if s == rel {
return
Expand Down
5 changes: 0 additions & 5 deletions src/cmd/internal/ld/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,6 @@ func loadcgo(file string, pkg string, p string) {
}

if f[0] == "cgo_export_static" || f[0] == "cgo_export_dynamic" {
// TODO: Remove once we know Windows is okay.
if f[0] == "cgo_export_static" && HEADTYPE == Hwindows {
continue
}

if len(f) < 2 || len(f) > 3 {
goto err
}
Expand Down
18 changes: 18 additions & 0 deletions src/cmd/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type Arch struct {
Elfsetupplt func()
Gentext func()
Machoreloc1 func(*Reloc, int64) int
PEreloc1 func(*Reloc, int64) bool
Lput func(uint32)
Wput func(uint16)
Vput func(uint64)
Expand Down Expand Up @@ -744,6 +745,13 @@ func hostlink() {
if HEADTYPE == Hopenbsd {
argv = append(argv, "-Wl,-nopie")
}
if HEADTYPE == Hwindows {
if headstring == "windowsgui" {
argv = append(argv, "-mwindows")
} else {
argv = append(argv, "-mconsole")
}
}

if Iself && AssumeGoldLinker != 0 /*TypeKind(100016)*/ {
argv = append(argv, "-Wl,--rosegment")
Expand Down Expand Up @@ -844,6 +852,9 @@ func hostlink() {
}
}
}
if HEADTYPE == Hwindows {
argv = append(argv, peimporteddlls()...)
}

if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "host link:")
Expand Down Expand Up @@ -1379,6 +1390,13 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) {
case SFILE:
put(nil, s.Name, 'f', s.Value, 0, int(s.Version), nil)
continue

case SHOSTOBJ:
if HEADTYPE == Hwindows {
put(s, s.Name, 'U', s.Value, 0, int(s.Version), nil)
}
continue

}
}

Expand Down
Loading

0 comments on commit 04642e9

Please sign in to comment.