[uClibc] PATCH: pie usage generalisation

Peter S. Mazinger ps.m at gmx.net
Wed Oct 13 20:41:17 UTC 2004


Hello!

The attached patch generalizes the use of PIE (all archs are brought in 
sync that use/mention it: x86/ppc/frv) and makes use of it building the 
target utils.

Tested on x86, ppc should be tested, frv uses -fPIE at one location, but 
at another place -fpie, I don't know which is correct (could be both) and 
misses the target addition in Config.in.

The test for ppc (requires the earlier sent crt-correction patch to work 
correctly):
enable UCLIBC_PIE_SUPPORT, build uClibc and utils, check:
file ./utils/ldd, it should show shared object (instead of executable)

Peter

-- 
Peter S. Mazinger <ps dot m at gmx dot net>           ID: 0xA5F059F2
Key fingerprint = 92A4 31E1 56BC 3D5A 2D08  BB6E C389 975E A5F0 59F2
-------------- next part --------------
--- uClibc-0.9.26/libc/sysdeps/linux/i386/Makefile.pie~	2004-10-10 20:07:44.000000000 +0200
+++ uClibc-0.9.26/libc/sysdeps/linux/i386/Makefile	2004-10-10 20:07:44.000000000 +0200
@@ -23,7 +23,7 @@
 CRT0_SRC = crt0.S
 CRT0_OBJ = crt0.o crt1.o
 ifeq ($(strip $(UCLIBC_PIE_SUPPORT)),y)
-CRT0_OBJ += Scrt0.o Scrt1.o
+SCRT0_OBJ = $(patsubst %,S%, $(CRT0_OBJ))
 endif
 CRT0_DEPS=gmon-start.S
 CTOR_TARGETS=$(TOPDIR)lib/crti.o $(TOPDIR)lib/crtn.o
@@ -41,9 +41,9 @@
 
 $(LIBC): ar-target 
 
-ar-target: $(OBJS) $(CRT0_OBJ) $(CTOR_TARGETS)
+ar-target: $(OBJS) $(CRT0_OBJ) $(SCRT0_OBJ) $(CTOR_TARGETS)
 	$(AR) $(ARFLAGS) $(LIBC) $(OBJS)
-	cp $(CRT0_OBJ) $(TOPDIR)lib/
+	cp $(CRT0_OBJ) $(SCRT0_OBJ) $(TOPDIR)lib/
 ifeq ($(strip $(UCLIBC_PIE_SUPPORT)),y)
 ifeq ($(strip $(UCLIBC_CTOR_DTOR)),y)
 	$(RM) $(TOPDIR)lib/Scrt0.o
@@ -56,6 +56,10 @@
 	$(CC) $(CFLAGS) -DL_$* $< -c -o $*.o
 	$(STRIPTOOL) -x -R .note -R .comment $*.o
 
+$(SCRT0_OBJ): $(CRT0_SRC)
+	$(CC) $(CFLAGS) $(PIEFLAG) -DL_$* $< -c -o $*.o
+	$(STRIPTOOL) -x -R .note -R .comment $*.o
+
 $(SOBJS): %.o : %.S
 	$(CC) $(CFLAGS) -c $< -o $@
 	$(STRIPTOOL) -x -R .note -R .comment $*.o
--- uClibc-0.9.26/libc/sysdeps/linux/powerpc/Makefile.pie~	2004-10-10 20:07:44.000000000 +0200
+++ uClibc-0.9.26/libc/sysdeps/linux/powerpc/Makefile	2004-10-10 20:07:44.000000000 +0200
@@ -23,7 +23,7 @@
 CRT0_SRC = crt0.S
 CRT0_OBJ = crt0.o crt1.o
 ifeq ($(strip $(UCLIBC_PIE_SUPPORT)),y)
-CRT0_OBJ += Scrt0.o Scrt1.o
+SCRT0_OBJ = $(patsubst %,S%, $(CRT0_OBJ))
 endif
 CRT0_DEPS=gmon-start.S
 CTOR_TARGETS=$(TOPDIR)lib/crti.o $(TOPDIR)lib/crtn.o
@@ -41,9 +41,9 @@
 
 $(LIBC): ar-target 
 
-ar-target: $(OBJS) $(CRT0_OBJ) $(CTOR_TARGETS)
+ar-target: $(OBJS) $(CRT0_OBJ) $(SCRT0_OBJ) $(CTOR_TARGETS)
 	$(AR) $(ARFLAGS) $(LIBC) $(OBJS)
-	cp $(CRT0_OBJ) $(TOPDIR)lib/
+	cp $(CRT0_OBJ) $(SCRT0_OBJ) $(TOPDIR)lib/
 ifeq ($(strip $(UCLIBC_PIE_SUPPORT)),y)
 ifeq ($(strip $(UCLIBC_CTOR_DTOR)),y)
 	$(RM) $(TOPDIR)lib/Scrt0.o
@@ -56,6 +56,10 @@
 	$(CC) $(CFLAGS) -DL_$* $< -c -o $*.o
 	$(STRIPTOOL) -x -R .note -R .comment $*.o
 
+$(SCRT0_OBJ): $(CRT0_SRC)
+	$(CC) $(CFLAGS) $(PIEFLAG) -DL_$* $< -c -o $*.o
+	$(STRIPTOOL) -x -R .note -R .comment $*.o
+
 $(SOBJS): %.o : %.S
 	$(CC) $(CFLAGS) -c $< -o $@
 	$(STRIPTOOL) -x -R .note -R .comment $*.o
--- uClibc-0.9.26/libc/sysdeps/linux/frv/Makefile.pie~	2004-10-10 20:07:44.000000000 +0200
+++ uClibc-0.9.26/libc/sysdeps/linux/frv/Makefile	2004-10-10 20:07:44.000000000 +0200
@@ -23,7 +23,9 @@
 
 CRT0_SRC = crt0.S
 CRT0_OBJ = crt0.o crt1.o
+ifeq $(strip $(UCLIBC_PIE_SUPPORT)),y)
 SCRT0_OBJ = $(patsubst %,S%, $(CRT0_OBJ))
+endif
 CRT0_DEPS=gmon-start.S
 
 CTOR_TARGETS = crti.o crtn.o
@@ -54,11 +56,11 @@
 	$(CC) $(CFLAGS) -c $< -o $@
 
 $(SCRT0_OBJ): $(CRT0_SRC) Scrtreloc.o
-	$(CC) $(CFLAGS) -fPIE -DL_$* -r -nostdlib $< Scrtreloc.o -o $*.o
+	$(CC) $(CFLAGS) $(PIEFLAG) -DL_$* -r -nostdlib $< Scrtreloc.o -o $*.o
 	$(STRIPTOOL) -x -R .note -R .comment $*.o
 
 Scrtreloc.o: crtreloc.c
-	$(CC) $(CFLAGS) -fPIE -c $< -o $@
+	$(CC) $(CFLAGS) $(PIEFLAG) -c $< -o $@
 
 $(CTOR_TARGETS): %.o : %.S
 	$(CC) $(CFLAGS) -c $< -o $@
--- uClibc-0.9.26/utils/Makefile.pie~	2004-10-10 20:07:44.000000000 +0200
+++ uClibc-0.9.26/utils/Makefile	2004-10-10 20:08:57.000000000 +0200
@@ -54,7 +54,7 @@
 	$(STRIPTOOL) -x -R .note -R .comment $@
 
 ldd:	ldd.c
-	$(CC) $(CFLAGS) $(XXFLAGS) -Wl,-s \
+	$(CC) $(CFLAGS) $(XXFLAGS) $(PIEFLAG) $(LDPIEFLAG) -Wl,-s \
 		-DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \
 		-DUCLIBC_LDSO=$(UCLIBC_LDSO) -I. -I../ldso/include \
 		-nostdlib -o $@ $(BIN_START_FILES) $^ $(LDADD_LIBFLOAT) \
@@ -62,7 +62,7 @@
 	$(STRIPTOOL) -x -R .note -R .comment $@
 
 iconv: ../libc/misc/wchar/wchar.c
-	$(CC) $(CFLAGS) $(XXFLAGS) -Wl,-s -DL_iconv_main \
+	$(CC) $(CFLAGS) $(XXFLAGS) $(PIEFLAG) $(LDPIEFLAG) -Wl,-s -DL_iconv_main \
 		-nostdlib -o $@ $(BIN_START_FILES) $^ $(LDADD_LIBFLOAT) \
 		-L../lib -lc $(LIBGCC) $(BIN_END_FILES)
 	$(STRIPTOOL) -x -R .note -R .comment $@
--- uClibc-0.9.26/Rules.mak.pie~	2004-10-10 20:07:44.000000000 +0200
+++ uClibc-0.9.26/Rules.mak	2004-10-10 20:07:44.000000000 +0200
@@ -89,6 +89,10 @@
 
 OPTIMIZATION:=
 PICFLAG:=-fPIC
+PIEFLAG:=$(call check_gcc,-fPIE,)
+ifeq ($(strip $(PIEFLAG)),-fPIE)
+LDPIEFLAG:=-Wl,-pie
+endif
 
 # Some nice CPU specific optimizations
 ifeq ($(strip $(TARGET_ARCH)),i386)
@@ -172,6 +176,7 @@
 	CPU_LDFLAGS-$(CONFIG_CRIS)+=-mcrislinux
 	CPU_CFLAGS-$(CONFIG_CRIS)+=-mlinux
 	PICFLAG:=-fpic
+	PIEFLAG:=$(call check_gcc,-fpie,)
 endif
 
 ifeq ($(strip $(TARGET_ARCH)),powerpc)
@@ -179,13 +184,14 @@
 # enough. Therefore use -fpic which will reduce code size and generates
 # faster code.
 	PICFLAG:=-fpic
+	PIEFLAG=$(call check_gcc,-fpie,)
 endif
 
 ifeq ($(strip $(TARGET_ARCH)),frv)
 	CPU_LDFLAGS-$(CONFIG_FRV)+=-melf32frvfd
 	CPU_CFLAGS-$(CONFIG_FRV)+=-mfdpic
 	PICFLAG=-fPIC -DPIC
-	PIEFLAG=-fpie
+	PIEFLAG=$(call check_gcc,-fpie,)
 	# Using -pie causes the program to have an interpreter, which is
 	# forbidden, so we must make do with -shared.  Unfortunately,
 	# -shared by itself would get us global function descriptors
@@ -220,6 +226,11 @@
 endif
 endif
 
+ifneq ($(strip $(UCLIBC_PIE_SUPPORT)),y)
+PIEFLAG=
+LDPIEFLAG=
+endif
+
 ifeq ($(SSP_CFLAGS),)
 SSP_CFLAGS=$(call check_gcc,-fno-stack-protector-all,)
 SSP_CFLAGS+=$(call check_gcc,-fstack-protector,)


More information about the uClibc mailing list