[klibc] Re: got pointer wrong in shared klibc binary

Alan Modra amodra at bigpond.net.au
Wed Sep 15 13:45:49 PDT 2004


On Wed, Sep 15, 2004 at 10:48:02AM +0930, Alan Modra wrote:
> After using SHAREDFLAGS = -Ttext 0x0f000200 on ppc64 (which I see you
> suggested in another email), I see the problem you're talking about.
> 
> klibc is fundamentally broken on ppc64.  You just can't call into
> another chunk of code (klibc.so in this case) which has a separate toc
> and expect things to work.
> 
> You *might* get something working by copying the klibc.so .toc section
> into each app linking to klibc.so.

Well, the Horrible Hack does seem to work for me.  Attached is a diff,
including a fix for setjmp.S too.  stmw/lmw only save the low 32 bits
so can't really be used, and stmd/lmd aren't available.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre
-------------- next part --------------
diff -urpN klibc-0.173/MCONFIG klibc/MCONFIG
--- klibc-0.173/MCONFIG	2004-07-02 09:09:19.000000000 +0930
+++ klibc/MCONFIG	2004-09-15 11:54:05.258092171 +0930
@@ -19,6 +22,7 @@ RANLIB  = $(CROSS)ranlib
 NM	= $(CROSS)nm
 PERL    = perl
 STRIP   = $(CROSS)strip --strip-all -R .comment -R .note
+OBJCOPY = $(CROSS)objcopy
 
 HOST_CC      = gcc
 HOST_CFLAGS  = -g -O
diff -urpN klibc-0.173/klibc/Makefile klibc/klibc/Makefile
--- klibc-0.173/klibc/Makefile	2004-08-26 15:33:41.000000000 +0930
+++ klibc/klibc/Makefile	2004-09-15 11:54:06.301925693 +0930
@@ -57,6 +57,8 @@ SOHASH  = klibc.so
 CRT0    = crt0.o
 LIB     = libc.a
 
+INTERP_O = interp.o
+
 all: tests $(CRT0) $(LIB) $(SOLIB) klibc.so
 
 # Add any architecture-specific rules
@@ -109,7 +111,7 @@ $(SOHASH): $(SOLIB) $(SOLIB).hash
 	rm -f klibc-??????????????????????.so
 	ln -f $@ klibc-`cat $(SOLIB).hash`.so
 
-interp.o: interp.S $(SOLIB).hash
+$(INTERP_O): interp.S $(SOLIB).hash
 	$(CC) $(CFLAGS) -D__ASSEMBLY__ -DLIBDIR=\"$(SHLIBDIR)\" \
 		-DSOHASH=\"`cat $(SOLIB).hash`\" \
 		-c -o $@ $<
diff -urpN klibc-0.173/klibc/arch/ppc64/MCONFIG klibc/klibc/arch/ppc64/MCONFIG
--- klibc-0.173/klibc/arch/ppc64/MCONFIG	2002-08-13 13:34:47.000000000 +0930
+++ klibc/klibc/arch/ppc64/MCONFIG	2004-09-15 09:22:01.452221449 +0930
@@ -9,3 +9,10 @@
 
 OPTFLAGS = -Os -fomit-frame-pointer
 BITSIZE  = 64
+
+# Extra linkflags when building the shared version of the library
+# This address needs to be reachable using normal inter-module
+# calls, and work on the memory models for this architecture
+# 256-16 MB - normal binaries start at 256 MB, and jumps are limited
+# to +/- 16 MB
+SHAREDFLAGS     = -Ttext 0x0f000200
diff -urpN klibc-0.173/klibc/arch/ppc64/Makefile.inc klibc/klibc/arch/ppc64/Makefile.inc
--- klibc-0.173/klibc/arch/ppc64/Makefile.inc	2004-06-07 16:25:06.000000000 +0930
+++ klibc/klibc/arch/ppc64/Makefile.inc	2004-09-15 12:10:35.090216287 +0930
@@ -13,4 +13,13 @@ ARCHOBJS = \
 
 ARCHSOOBJS = $(patsubst %.o,%.lo,$(ARCHOBJS))
 
+INTERP_O = interp1.o
+
+interp.o: interp1.o klibc.got
+	$(LD) $(LDFLAGS) -r -o $@ interp1.o klibc.got
+
+klibc.got: $(SOHASH)
+	$(OBJCOPY) -j.got $< $@
+
 archclean:
+	rm -f klibc.got
diff -urpN klibc-0.173/klibc/arch/ppc64/setjmp.S klibc/klibc/arch/ppc64/setjmp.S
--- klibc-0.173/klibc/arch/ppc64/setjmp.S	2003-12-13 14:09:13.000000000 +1030
+++ klibc/klibc/arch/ppc64/setjmp.S	2004-09-15 12:28:42.160328339 +0930
@@ -17,13 +16,33 @@ setjmp:
 	.globl	setjmp
 	.globl	.setjmp
 .setjmp:
-        mflr    %r11                    /* save return address */
-        mfcr    %r12                    /* save condition register */
-        mr      %r10,%r1                /* save stack pointer */
-        mr      %r9,%r2                 /* save GPR2 (not needed) */
-        stmw    %r9,0(%r3)              /* save r9..r31 */
-        li      %r3,0                   /* indicate success */
-        blr                             /* return */
+	mflr	%r11			/* save return address */
+	mfcr	%r12			/* save condition register */
+	std	%r2,0(%r3)		/* save TOC pointer (not needed) */
+	stdu	%r1,8(%r3)		/* save stack pointer */
+	stdu	%r11,8(%r3)
+	stdu	%r12,8(%r3)
+	stdu	%r13,8(%r3)		/* save caller saved regs */
+	stdu	%r14,8(%r3)
+	stdu	%r15,8(%r3)
+	stdu	%r16,8(%r3)
+	stdu	%r17,8(%r3)
+	stdu	%r18,8(%r3)
+	stdu	%r19,8(%r3)
+	stdu	%r20,8(%r3)
+	stdu	%r21,8(%r3)
+	stdu	%r22,8(%r3)
+	stdu	%r23,8(%r3)
+	stdu	%r24,8(%r3)
+	stdu	%r25,8(%r3)
+	stdu	%r26,8(%r3)
+	stdu	%r27,8(%r3)
+	stdu	%r28,8(%r3)
+	stdu	%r29,8(%r3)
+	stdu	%r30,8(%r3)
+	std	%r31,8(%r3)
+	li	%r3,0			/* indicate success */
+	blr				/* return */
 
 	.size .setjmp,.-.setjmp
 	.section ".opd","aw"
@@ -35,12 +54,32 @@ longjmp:
 	.globl	longjmp
 	.globl	.longjmp
 .longjmp:
-        lmw     %r9,0(%r3)              /* save r9..r31 */
-        mtlr    %r11                    /* restore LR */
-        mtcr    %r12                    /* restore CR */
-        mr      %r2,%r9                 /* restore GPR2 (not needed) */
-        mr      %r1,%r10                /* restore stack */
-        mr      %r3,%r4                 /* get return value */
-        blr                             /* return */
+	ld	%r2,0(%r3)		/* restore TOC pointer (not needed) */
+	ldu	%r1,8(%r3)		/* restore stack */
+	ldu	%r11,8(%r3)
+	ldu	%r12,8(%r3)
+	ldu	%r13,8(%r3)		/* restore caller saved regs */
+	ldu	%r14,8(%r3)
+	ldu	%r15,8(%r3)
+	ldu	%r16,8(%r3)
+	ldu	%r17,8(%r3)
+	ldu	%r18,8(%r3)
+	ldu	%r19,8(%r3)
+	ldu	%r20,8(%r3)
+	ldu	%r21,8(%r3)
+	ldu	%r22,8(%r3)
+	ldu	%r23,8(%r3)
+	ldu	%r24,8(%r3)
+	ldu	%r25,8(%r3)
+	ldu	%r26,8(%r3)
+	ldu	%r27,8(%r3)
+	ldu	%r28,8(%r3)
+	ldu	%r29,8(%r3)
+	ldu	%r30,8(%r3)
+	ld	%r31,8(%r3)
+	mtlr	%r11			/* restore LR */
+	mtcr	%r12			/* restore CR */
+	mr	%r3,%r4			/* get return value */
+	blr				/* return */
 
 	.size .longjmp,.-.longjmp


More information about the klibc mailing list