Skip to content

Commit ce7068b

Browse files
zte-majiangmajiang31312
authored andcommitted
cmd/link, runtime: add initial cgo support for ppc64
We should be able to build docker after this get applied. Updates #13192
1 parent 8ccafb1 commit ce7068b

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-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: 24 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
@@ -564,6 +570,13 @@ g0:
564570
MOVD R0, 0(R1) // clear back chain pointer (TODO can we give it real back trace information?)
565571
// This is a "global call", so put the global entry point in r12
566572
MOVD R3, R12
573+
574+
#ifdef GOARCH_ppc64
575+
// ppc64 use elf ABI v1. we must get the real entry address from
576+
// first slot of the function descriptor before call.
577+
MOVD 8(R12), R2
578+
MOVD (R12), R12
579+
#endif
567580
MOVD R12, CTR
568581
MOVD R4, R3 // arg in r3
569582
BL (CTR)
@@ -722,9 +735,20 @@ TEXT runtime·setg(SB), NOSPLIT, $0-8
722735
BL runtime·save_g(SB)
723736
RET
724737

738+
#ifdef GOARCH_ppc64
739+
TEXT setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
740+
DWORD $_setg_gcc<>(SB)
741+
DWORD $0
742+
DWORD $0
743+
#endif
744+
725745
// void setg_gcc(G*); set g in C TLS.
726746
// Must obey the gcc calling convention.
747+
#ifdef GOARCH_ppc64le
727748
TEXT setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
749+
#else
750+
TEXT _setg_gcc<>(SB),NOSPLIT|NOFRAME,$0-0
751+
#endif
728752
// The standard prologue clobbers R31, which is callee-save in
729753
// the C ABI, so we have to use $-8-0 and save LR ourselves.
730754
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)