Skip to content

Commit 1135071

Browse files
zte-majianglaboger
authored andcommitted
cmd/link, runtime: add initial cgo support for ppc64
We should be able to build docker after this get applied. Updates #13192 Change-Id: I5378d3518fac52d6bd4c97828884c1b382b7ace5 GitHub-Last-Rev: 210b7bc GitHub-Pull-Request: #28546 Reviewed-on: https://go-review.googlesource.com/c/146898 Reviewed-by: Jiang Ma <[email protected]> Reviewed-by: Clément Chigot <[email protected]> Reviewed-by: Lynn Boger <[email protected]> Run-TryBot: Tobias Klauser <[email protected]> Run-TryBot: Lynn Boger <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 2cc6d62 commit 1135071

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

src/cmd/link/internal/ppc64/asm.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,13 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
374374
}
375375

376376
func elfreloc1(ctxt *ld.Link, r *sym.Reloc, sectoff int64) bool {
377+
// Beware that bit0~bit15 start from the third byte of a instruction in Big-Endian machines.
378+
if r.Type == objabi.R_ADDR || r.Type == objabi.R_POWER_TLS || r.Type == objabi.R_CALLPOWER {
379+
} else {
380+
if ctxt.Arch.ByteOrder == binary.BigEndian {
381+
sectoff += 2
382+
}
383+
}
377384
ctxt.Out.Write64(uint64(sectoff))
378385

379386
elfsym := r.Xsym.ElfsymForReloc()

src/cmd/link/internal/ppc64/obj.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ func archinit(ctxt *ld.Link) {
9393
}
9494

9595
case objabi.Hlinux: /* ppc64 elf */
96-
if ctxt.Arch == sys.ArchPPC64 {
97-
*ld.FlagD = true // TODO(austin): ELF ABI v1 not supported yet
98-
}
9996
ld.Elfinit(ctxt)
10097
ld.HEADR = ld.ELFRESERVE
10198
if *ld.FlagTextAddr == -1 {

src/runtime/asm_ppc64x.s

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ TEXT runtime·rt0_go(SB),NOSPLIT,$0
3636
MOVD _cgo_init(SB), R12
3737
CMP R0, R12
3838
BEQ nocgo
39+
#ifdef GOARCH_ppc64
40+
// ppc64 use elf ABI v1. we must get the real entry address from
41+
// first slot of the function descriptor before call.
42+
MOVD 8(R12), R2
43+
MOVD (R12), R12
44+
#endif
3945
MOVD R12, CTR // r12 = "global function entry point"
4046
MOVD R13, R5 // arg 2: TLS base pointer
4147
MOVD $setg_gcc<>(SB), R4 // arg 1: setg
@@ -597,6 +603,16 @@ g0:
597603
#endif
598604
// This is a "global call", so put the global entry point in r12
599605
MOVD R3, R12
606+
607+
#ifdef GOARCH_ppc64
608+
// ppc64 use elf ABI v1. we must get the real entry address from
609+
// first slot of the function descriptor before call.
610+
#ifndef GOOS_aix
611+
// aix just passes the function pointer for the moment, see golang.org/cl/146898 for details.
612+
MOVD 8(R12), R2
613+
MOVD (R12), R12
614+
#endif
615+
#endif
600616
MOVD R12, CTR
601617
MOVD R4, R3 // arg in r3
602618
BL (CTR)
@@ -754,9 +770,20 @@ TEXT runtime·setg(SB), NOSPLIT, $0-8
754770
BL runtime·save_g(SB)
755771
RET
756772

773+
#ifdef GOARCH_ppc64
774+
TEXT setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
775+
DWORD $_setg_gcc<>(SB)
776+
DWORD $0
777+
DWORD $0
778+
#endif
779+
757780
// void setg_gcc(G*); set g in C TLS.
758781
// Must obey the gcc calling convention.
782+
#ifdef GOARCH_ppc64le
759783
TEXT setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
784+
#else
785+
TEXT _setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
786+
#endif
760787
// The standard prologue clobbers R31, which is callee-save in
761788
// the C ABI, so we have to use $-8-0 and save LR ourselves.
762789
MOVD LR, R4

src/runtime/rt0_linux_ppc64.s

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@ TEXT _rt0_ppc64_linux(SB),NOSPLIT,$0
66
DWORD $0
77
DWORD $0
88

9+
TEXT main(SB),NOSPLIT,$0
10+
DWORD $_main<>(SB)
11+
DWORD $0
12+
DWORD $0
13+
914
TEXT _main<>(SB),NOSPLIT,$-8
1015
// In a statically linked binary, the stack contains argc,
1116
// argv as argc string pointers followed by a NULL, envv as a
1217
// sequence of string pointers followed by a NULL, and auxv.
1318
// There is no TLS base pointer.
1419
//
1520
// TODO(austin): Support ABI v1 dynamic linking entry point
16-
MOVD 0(R1), R3 // argc
17-
ADD $8, R1, R4 // argv
18-
BR main(SB)
19-
20-
TEXT main(SB),NOSPLIT,$-8
2121
MOVD $runtime·rt0_go(SB), R12
2222
MOVD R12, CTR
23+
MOVBZ runtime·iscgo(SB), R5
24+
CMP R5, $0
25+
BEQ nocgo
26+
BR (CTR)
27+
nocgo:
28+
MOVD 0(R1), R3 // argc
29+
ADD $8, R1, R4 // argv
2330
BR (CTR)

src/runtime/sys_linux_ppc64x.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ TEXT runtime·cgoSigtramp(SB),NOSPLIT|NOFRAME,$0
414414
DWORD $0
415415
DWORD $0
416416
TEXT runtime·_cgoSigtramp(SB),NOSPLIT,$0
417-
JMP runtime·sigtramp(SB)
417+
JMP runtime·_sigtramp(SB)
418418
#endif
419419

420420
TEXT runtime·sigprofNonGoWrapper<>(SB),NOSPLIT,$0

0 commit comments

Comments
 (0)