[klibc] ppc64: Add ppc64le support

Add PowerPC 64bit little endian support.

Signed-off-by: Anton Blanchard <anton@samba.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
diff --git a/Makefile b/Makefile
index a7da622..dc10fc5 100644
--- a/Makefile
+++ b/Makefile
@@ -30,8 +30,11 @@
 
 NOSTDINC_FLAGS := -nostdlib -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 
-ARCH	          := $(shell uname -m | sed -e s/i.86/i386/ -e s/parisc64/parisc/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ \
-			-e s/aarch64.*/arm64/ -e s/sh.*/sh/)
+ARCH	          := $(shell uname -m | sed -e s/i.86/i386/ \
+			-e s/parisc64/parisc/ -e s/sun4u/sparc64/ \
+			-e s/arm.*/arm/ -e s/sa110/arm/ \
+			-e s/aarch64.*/arm64/ -e s/sh.*/sh/ \
+			-e s/ppc64le/ppc64/)
 export KLIBCARCH  ?= $(ARCH)
 export KLIBCARCHDIR := $(shell echo $(KLIBCARCH) | sed -e s/s390x/s390/)
 
diff --git a/usr/klibc/arch/ppc64/MCONFIG b/usr/klibc/arch/ppc64/MCONFIG
index cb66614..0294c44 100644
--- a/usr/klibc/arch/ppc64/MCONFIG
+++ b/usr/klibc/arch/ppc64/MCONFIG
@@ -7,10 +7,10 @@
 # accordingly.
 #
 
-KLIBCARCHREQFLAGS = -m64 -mcall-aixdesc
+KLIBCARCHREQFLAGS = -m64
+KLIBCARCHREQFLAGS += $(call cc-option, -mcall-aixdesc, )
 KLIBCOPTFLAGS     += -Os
 KLIBCBITSIZE      = 64
-KLIBCLDFLAGS      = -m elf64ppc
 
 # Extra linkflags when building the shared version of the library
 # This address needs to be reachable using normal inter-module
diff --git a/usr/klibc/arch/ppc64/crt0.S b/usr/klibc/arch/ppc64/crt0.S
index c976d5c..ed14534 100644
--- a/usr/klibc/arch/ppc64/crt0.S
+++ b/usr/klibc/arch/ppc64/crt0.S
@@ -9,25 +9,35 @@
 # }
 #
 
-	.section ".toc","aw"
-.LC0:	.tc	environ[TC],environ
-
 	.text
-	.align 4
-
+	.balign 4
+	.globl	_start
+#if _CALL_ELF == 2
+	.type _start,@function
+_start:
+#else
 	.section ".opd","aw"
+	.balign 8
 _start:
 	.quad	._start, .TOC.@tocbase, 0
 	.previous
-	.size	_start, 24
 	.type	._start,@function
-	.globl	_start
-	.globl	._start
 ._start:
-	stdu    %r1,-32(%r1)
-	addi    %r3,%r1,32
+#endif
+
+#if _CALL_ELF == 2
+0:	addis	2,12,.TOC.-0b@ha
+	addi	2,2,.TOC.-0b@l
+#endif
+
+	stdu	%r1,-32(%r1)
+	addi	%r3,%r1,32
 	li	%r4,0		/* fini (unused) */
-	b 	.__libc_init
+	b	__libc_init
 	nop
 
-	.size ._start,.-._start
+#if _CALL_ELF == 2
+	.size _start,.-_start
+#else
+	.size _start,.-._start
+#endif
diff --git a/usr/klibc/arch/ppc64/setjmp.S b/usr/klibc/arch/ppc64/setjmp.S
index 30db419..ecf9717 100644
--- a/usr/klibc/arch/ppc64/setjmp.S
+++ b/usr/klibc/arch/ppc64/setjmp.S
@@ -5,17 +5,21 @@
 #
 
 	.text
-	.align 4
-
-	.section ".opd","aw"
-setjmp:
-	.quad	.setjmp,.TOC.@tocbase,0
-	.previous
-	.size	setjmp,24
-	.type	.setjmp,@function
+	.balign 4
 	.globl	setjmp
+#if _CALL_ELF == 2
+	.type setjmp,@function
+setjmp:
+#else
+	.section ".opd","aw"
+	.balign 8
+setjmp:
+	.quad	.setjmp, .TOC.@tocbase, 0
+	.previous
+	.type	.setjmp,@function
 	.globl	.setjmp
 .setjmp:
+#endif
 	mflr	%r11			/* save return address */
 	mfcr	%r12			/* save condition register */
 	std	%r2,0(%r3)		/* save TOC pointer (not needed) */
@@ -43,17 +47,28 @@
 	std	%r31,8(%r3)
 	li	%r3,0			/* indicate success */
 	blr				/* return */
+#if _CALL_ELF == 2
+	.size setjmp,.-setjmp
+#else
+	.size setjmp,.-.setjmp
+#endif
 
-	.size .setjmp,.-.setjmp
-	.section ".opd","aw"
-longjmp:
-	.quad	.longjmp,.TOC.@tocbase,0
-	.previous
-	.size	longjmp,24
-	.type	.longjmp,@function
+	.text
+	.balign 4
 	.globl	longjmp
+#if _CALL_ELF == 2
+	.type longjmp,@function
+longjmp:
+#else
+	.section ".opd","aw"
+	.balign 8
+longjmp:
+	.quad	.longjmp, .TOC.@tocbase, 0
+	.previous
+	.type	.longjmp,@function
 	.globl	.longjmp
 .longjmp:
+#endif
 	ld	%r2,0(%r3)		/* restore TOC pointer (not needed) */
 	ldu	%r1,8(%r3)		/* restore stack */
 	ldu	%r11,8(%r3)
@@ -81,5 +96,8 @@
 	mtcr	%r12			/* restore CR */
 	mr	%r3,%r4			/* get return value */
 	blr				/* return */
-
-	.size .longjmp,.-.longjmp
+#if _CALL_ELF == 2
+	.size longjmp,.-longjmp
+#else
+	.size longjmp,.-.longjmp
+#endif
diff --git a/usr/klibc/arch/ppc64/sysstub.ph b/usr/klibc/arch/ppc64/sysstub.ph
index 9ee9370..b3f6e38 100644
--- a/usr/klibc/arch/ppc64/sysstub.ph
+++ b/usr/klibc/arch/ppc64/sysstub.ph
@@ -9,22 +9,35 @@
     my($outputdir, $fname, $type, $sname, $stype, @args) = @_;
 
     open(OUT, '>', "${outputdir}/${fname}.S");
-    print OUT "#include <asm/unistd.h>\n";
-    print OUT "\n";
-    print OUT "\t.globl ${fname}\n";
-    print OUT "\t.section \".opd\",\"aw\"\n";
-    print OUT "\t.align 3\n";
-    print OUT "${fname}:\n";
-    print OUT "\t.quad .${fname},.TOC.\@tocbase,0\n";
-    print OUT "\t.text\n";
-    print OUT "\t.type .${fname},\@function\n";
-    print OUT "\t.globl .${fname}\n";
-    print OUT ".${fname}:\n";
-    print OUT "\tli 0,__NR_${sname}\n";
-    print OUT "\tsc\n";
-    print OUT "\tbnslr\n";
-    print OUT "\tb .__syscall_error\n";
-    print OUT "\t.size .${fname},.-.${fname}\n";
+    print OUT <<EOF;
+#include <asm/unistd.h>
+
+	.text
+	.balign 4
+	.globl	${fname}
+#if _CALL_ELF == 2
+	.type ${fname},\@function
+${fname}:
+#else
+	.section ".opd","aw"
+	.balign 8
+${fname}:
+	.quad	.${fname}, .TOC.\@tocbase, 0
+	.previous
+	.type	.${fname},\@function
+	.globl	.${fname}
+.${fname}:
+#endif
+	li	0, __NR_${sname}
+	sc
+	bnslr
+	b	__syscall_error
+#if _CALL_ELF == 2
+	.size ${fname},.-${fname}
+#else
+	.size ${fname},.-.${fname}
+#endif
+EOF
     close(OUT);
 }