Skip to content

Commit 76d8e60

Browse files
Vladimir Stefanovicbradfitz
Vladimir Stefanovic
authored andcommitted
cmd/link: add support for GOARCH=mips{,le}
Only internal linking without cgo is supported for now. Change-Id: I772d2ba496a613c78bee7e93f29e9538e6407bdc Reviewed-on: https://go-review.googlesource.com/31481 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent f4c9975 commit 76d8e60

File tree

4 files changed

+376
-1
lines changed

4 files changed

+376
-1
lines changed

Diff for: src/cmd/link/internal/ld/elf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2199,7 +2199,7 @@ func Asmbelf(ctxt *Link, symo int64) {
21992199
switch SysArch.Family {
22002200
default:
22012201
Exitf("unknown architecture in asmbelf: %v", SysArch.Family)
2202-
case sys.MIPS64:
2202+
case sys.MIPS, sys.MIPS64:
22032203
eh.machine = EM_MIPS
22042204
case sys.ARM:
22052205
eh.machine = EM_ARM

Diff for: src/cmd/link/internal/mips/asm.go

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Inferno utils/5l/asm.c
2+
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/5l/asm.c
3+
//
4+
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5+
// Portions Copyright © 1995-1997 C H Forsyth ([email protected])
6+
// Portions Copyright © 1997-1999 Vita Nuova Limited
7+
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8+
// Portions Copyright © 2004,2006 Bruce Ellis
9+
// Portions Copyright © 2005-2007 C H Forsyth ([email protected])
10+
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11+
// Portions Copyright © 2016 The Go Authors. All rights reserved.
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in
21+
// all copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29+
// THE SOFTWARE.
30+
31+
package mips
32+
33+
import (
34+
"cmd/internal/obj"
35+
"cmd/link/internal/ld"
36+
"fmt"
37+
"log"
38+
)
39+
40+
func gentext(ctxt *ld.Link) {
41+
return
42+
}
43+
44+
func adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) bool {
45+
log.Fatalf("adddynrel not implemented")
46+
return false
47+
}
48+
49+
func elfreloc1(ctxt *ld.Link, r *ld.Reloc, sectoff int64) int {
50+
return -1
51+
}
52+
53+
func elfsetupplt(ctxt *ld.Link) {
54+
return
55+
}
56+
57+
func machoreloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) int {
58+
return -1
59+
}
60+
61+
func archreloc(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, val *int64) int {
62+
if ld.Linkmode == ld.LinkExternal {
63+
return -1
64+
}
65+
66+
switch r.Type {
67+
case obj.R_CONST:
68+
*val = r.Add
69+
return 0
70+
71+
case obj.R_GOTOFF:
72+
*val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0))
73+
return 0
74+
75+
case obj.R_ADDRMIPS,
76+
obj.R_ADDRMIPSU:
77+
t := ld.Symaddr(r.Sym) + r.Add
78+
o1 := ld.SysArch.ByteOrder.Uint32(s.P[r.Off:])
79+
if r.Type == obj.R_ADDRMIPS {
80+
*val = int64(o1&0xffff0000 | uint32(t)&0xffff)
81+
} else {
82+
*val = int64(o1&0xffff0000 | uint32((t+1<<15)>>16)&0xffff)
83+
}
84+
return 0
85+
86+
case obj.R_CALLMIPS,
87+
obj.R_JMPMIPS:
88+
// Low 26 bits = (S + A) >> 2
89+
t := ld.Symaddr(r.Sym) + r.Add
90+
o1 := ld.SysArch.ByteOrder.Uint32(s.P[r.Off:])
91+
*val = int64(o1&0xfc000000 | uint32(t>>2)&^0xfc000000)
92+
return 0
93+
}
94+
95+
return -1
96+
}
97+
98+
func archrelocvariant(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, t int64) int64 {
99+
return -1
100+
}
101+
102+
func asmb(ctxt *ld.Link) {
103+
if ctxt.Debugvlog != 0 {
104+
ctxt.Logf("%5.2f asmb\n", obj.Cputime())
105+
}
106+
107+
if ld.Iself {
108+
ld.Asmbelfsetup()
109+
}
110+
111+
sect := ld.Segtext.Sect
112+
ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
113+
ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
114+
for sect = sect.Next; sect != nil; sect = sect.Next {
115+
ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
116+
ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
117+
}
118+
119+
if ld.Segrodata.Filelen > 0 {
120+
if ctxt.Debugvlog != 0 {
121+
ctxt.Logf("%5.2f rodatblk\n", obj.Cputime())
122+
}
123+
124+
ld.Cseek(int64(ld.Segrodata.Fileoff))
125+
ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
126+
}
127+
128+
if ctxt.Debugvlog != 0 {
129+
ctxt.Logf("%5.2f datblk\n", obj.Cputime())
130+
}
131+
132+
ld.Cseek(int64(ld.Segdata.Fileoff))
133+
ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
134+
135+
ld.Cseek(int64(ld.Segdwarf.Fileoff))
136+
ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
137+
138+
/* output symbol table */
139+
ld.Symsize = 0
140+
141+
ld.Lcsize = 0
142+
symo := uint32(0)
143+
if !*ld.FlagS {
144+
if !ld.Iself {
145+
ld.Errorf(nil, "unsupported executable format")
146+
}
147+
if ctxt.Debugvlog != 0 {
148+
ctxt.Logf("%5.2f sym\n", obj.Cputime())
149+
}
150+
symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
151+
symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound)))
152+
153+
ld.Cseek(int64(symo))
154+
if ctxt.Debugvlog != 0 {
155+
ctxt.Logf("%5.2f elfsym\n", obj.Cputime())
156+
}
157+
ld.Asmelfsym(ctxt)
158+
ld.Cflush()
159+
ld.Cwrite(ld.Elfstrdat)
160+
161+
if ctxt.Debugvlog != 0 {
162+
ctxt.Logf("%5.2f dwarf\n", obj.Cputime())
163+
}
164+
165+
if ld.Linkmode == ld.LinkExternal {
166+
ld.Elfemitreloc(ctxt)
167+
}
168+
}
169+
170+
if ctxt.Debugvlog != 0 {
171+
ctxt.Logf("%5.2f header\n", obj.Cputime())
172+
}
173+
174+
ld.Cseek(0)
175+
switch ld.Headtype {
176+
default:
177+
ld.Errorf(nil, "unsupported operating system")
178+
case obj.Hlinux:
179+
ld.Asmbelf(ctxt, int64(symo))
180+
}
181+
182+
ld.Cflush()
183+
if *ld.FlagC {
184+
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
185+
fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)
186+
fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen)
187+
fmt.Printf("symsize=%d\n", ld.Symsize)
188+
fmt.Printf("lcsize=%d\n", ld.Lcsize)
189+
fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize))
190+
}
191+
}

Diff for: src/cmd/link/internal/mips/l.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Inferno utils/5l/asm.c
2+
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/5l/asm.c
3+
//
4+
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5+
// Portions Copyright © 1995-1997 C H Forsyth ([email protected])
6+
// Portions Copyright © 1997-1999 Vita Nuova Limited
7+
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8+
// Portions Copyright © 2004,2006 Bruce Ellis
9+
// Portions Copyright © 2005-2007 C H Forsyth ([email protected])
10+
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11+
// Portions Copyright © 2016 The Go Authors. All rights reserved.
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in
21+
// all copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29+
// THE SOFTWARE.
30+
31+
package mips
32+
33+
// Writing object files.
34+
35+
// cmd/9l/l.h from Vita Nuova.
36+
//
37+
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
38+
// Portions Copyright © 1995-1997 C H Forsyth ([email protected])
39+
// Portions Copyright © 1997-1999 Vita Nuova Limited
40+
// Portions Copyright © 2000-2008 Vita Nuova Holdings Limited (www.vitanuova.com)
41+
// Portions Copyright © 2004,2006 Bruce Ellis
42+
// Portions Copyright © 2005-2007 C H Forsyth ([email protected])
43+
// Revisions Copyright © 2000-2008 Lucent Technologies Inc. and others
44+
// Portions Copyright © 2016 The Go Authors. All rights reserved.
45+
//
46+
// Permission is hereby granted, free of charge, to any person obtaining a copy
47+
// of this software and associated documentation files (the "Software"), to deal
48+
// in the Software without restriction, including without limitation the rights
49+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
50+
// copies of the Software, and to permit persons to whom the Software is
51+
// furnished to do so, subject to the following conditions:
52+
//
53+
// The above copyright notice and this permission notice shall be included in
54+
// all copies or substantial portions of the Software.
55+
//
56+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
62+
// THE SOFTWARE.
63+
64+
const (
65+
MaxAlign = 32 // max data alignment
66+
MinAlign = 1 // min data alignment
67+
FuncAlign = 4
68+
)
69+
70+
/* Used by ../internal/ld/dwarf.go */
71+
const (
72+
DWARFREGSP = 29
73+
DWARFREGLR = 31
74+
)

Diff for: src/cmd/link/internal/mips/obj.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Inferno utils/5l/obj.c
2+
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/5l/obj.c
3+
//
4+
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5+
// Portions Copyright © 1995-1997 C H Forsyth ([email protected])
6+
// Portions Copyright © 1997-1999 Vita Nuova Limited
7+
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8+
// Portions Copyright © 2004,2006 Bruce Ellis
9+
// Portions Copyright © 2005-2007 C H Forsyth ([email protected])
10+
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11+
// Portions Copyright © 2016 The Go Authors. All rights reserved.
12+
//
13+
// Permission is hereby granted, free of charge, to any person obtaining a copy
14+
// of this software and associated documentation files (the "Software"), to deal
15+
// in the Software without restriction, including without limitation the rights
16+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
// copies of the Software, and to permit persons to whom the Software is
18+
// furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in
21+
// all copies or substantial portions of the Software.
22+
//
23+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29+
// THE SOFTWARE.
30+
31+
package mips
32+
33+
import (
34+
"cmd/internal/obj"
35+
"cmd/internal/sys"
36+
"cmd/link/internal/ld"
37+
"fmt"
38+
)
39+
40+
// Reading object files.
41+
42+
func Init() {
43+
if obj.GOARCH == "mipsle" {
44+
ld.SysArch = sys.ArchMIPSLE
45+
} else {
46+
ld.SysArch = sys.ArchMIPS
47+
}
48+
49+
ld.Thearch.Funcalign = FuncAlign
50+
ld.Thearch.Maxalign = MaxAlign
51+
ld.Thearch.Minalign = MinAlign
52+
ld.Thearch.Dwarfregsp = DWARFREGSP
53+
ld.Thearch.Dwarfreglr = DWARFREGLR
54+
55+
ld.Thearch.Adddynrel = adddynrel
56+
ld.Thearch.Archinit = archinit
57+
ld.Thearch.Archreloc = archreloc
58+
ld.Thearch.Archrelocvariant = archrelocvariant
59+
ld.Thearch.Asmb = asmb
60+
ld.Thearch.Elfreloc1 = elfreloc1
61+
ld.Thearch.Elfsetupplt = elfsetupplt
62+
ld.Thearch.Gentext = gentext
63+
ld.Thearch.Machoreloc1 = machoreloc1
64+
if ld.SysArch == sys.ArchMIPSLE {
65+
ld.Thearch.Lput = ld.Lputl
66+
ld.Thearch.Wput = ld.Wputl
67+
ld.Thearch.Vput = ld.Vputl
68+
ld.Thearch.Append16 = ld.Append16l
69+
ld.Thearch.Append32 = ld.Append32l
70+
ld.Thearch.Append64 = ld.Append64l
71+
} else {
72+
ld.Thearch.Lput = ld.Lputb
73+
ld.Thearch.Wput = ld.Wputb
74+
ld.Thearch.Vput = ld.Vputb
75+
ld.Thearch.Append16 = ld.Append16b
76+
ld.Thearch.Append32 = ld.Append32b
77+
ld.Thearch.Append64 = ld.Append64b
78+
}
79+
80+
ld.Thearch.Linuxdynld = "/lib/ld.so.1"
81+
82+
ld.Thearch.Freebsddynld = "XXX"
83+
ld.Thearch.Openbsddynld = "XXX"
84+
ld.Thearch.Netbsddynld = "XXX"
85+
ld.Thearch.Dragonflydynld = "XXX"
86+
ld.Thearch.Solarisdynld = "XXX"
87+
}
88+
89+
func archinit(ctxt *ld.Link) {
90+
switch ld.Headtype {
91+
default:
92+
ld.Exitf("unknown -H option: %v", ld.Headtype)
93+
case obj.Hlinux: /* mips elf */
94+
ld.Elfinit(ctxt)
95+
ld.HEADR = ld.ELFRESERVE
96+
if *ld.FlagTextAddr == -1 {
97+
*ld.FlagTextAddr = 0x10000 + int64(ld.HEADR)
98+
}
99+
if *ld.FlagDataAddr == -1 {
100+
*ld.FlagDataAddr = 0
101+
}
102+
if *ld.FlagRound == -1 {
103+
*ld.FlagRound = 0x10000
104+
}
105+
}
106+
107+
if *ld.FlagDataAddr != 0 && *ld.FlagRound != 0 {
108+
fmt.Printf("warning: -D0x%x is ignored because of -R0x%x\n", uint64(*ld.FlagDataAddr), uint32(*ld.FlagRound))
109+
}
110+
}

0 commit comments

Comments
 (0)