From 4803ce2696950f8c27c83de4fe46ae196f35af16 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 6 Aug 2014 12:36:46 -0700 Subject: [PATCH] Fix uses of -fPIC and -fPIE. We've been using -fPIC and -fPIE together in the global cflags all this time. These options are incompatible. The only reason we haven't been hit by this before is because of the forced -Bsymbolic in GCC. To fix this, pass -fpic when compiling objects for shared libraries and -fpie when compiling objects for executables. For static libraries, also use -fpic. We have to do this because static libraries might be included in either a shared library or an executable. Code compiled with -fpie cannot be included in a shared library, but code compiled with -fpic may be included in an executable. We've also been using -fpic and -fPIC together. These are different options, and only the latter will take effect. http://stackoverflow.com/a/967010 The final thing this fixes is that we had -f(PIC|PIE) flags being passed to link commands. These are compile time flags, and don't do anything at link time. Bug: 16823325 Change-Id: Ic76f47e63dc2c81b7e1a8058bae1b3dc8565d606 --- core/binary.mk | 6 ++++++ core/combo/TARGET_linux-arm.mk | 4 ++-- core/combo/TARGET_linux-arm64.mk | 3 +-- core/combo/TARGET_linux-mips.mk | 3 +-- core/combo/TARGET_linux-mips64.mk | 3 +-- core/combo/TARGET_linux-x86.mk | 2 +- core/combo/TARGET_linux-x86_64.mk | 2 +- core/definitions.mk | 2 +- 8 files changed, 14 insertions(+), 11 deletions(-) diff --git a/core/binary.mk b/core/binary.mk index 3a53e48a6e..6e89ac0d3e 100644 --- a/core/binary.mk +++ b/core/binary.mk @@ -113,6 +113,12 @@ my_cxx := $(LOCAL_CXX) my_c_includes := $(LOCAL_C_INCLUDES) my_generated_sources := $(LOCAL_GENERATED_SOURCES) +ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES) +my_cflags += -fpie +else +my_cflags += -fPIC +endif + my_src_files += $(LOCAL_SRC_FILES_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_SRC_FILES_$(my_32_64_bit_suffix)) my_shared_libraries += $(LOCAL_SHARED_LIBRARIES_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_SHARED_LIBRARIES_$(my_32_64_bit_suffix)) my_cflags += $(LOCAL_CFLAGS_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_CFLAGS_$(my_32_64_bit_suffix)) diff --git a/core/combo/TARGET_linux-arm.mk b/core/combo/TARGET_linux-arm.mk index a06f2dddd4..51c1814175 100644 --- a/core/combo/TARGET_linux-arm.mk +++ b/core/combo/TARGET_linux-arm.mk @@ -95,7 +95,7 @@ endif android_config_h := $(call select-android-config-h,linux-arm) $(combo_2nd_arch_prefix)TARGET_GLOBAL_CFLAGS += \ - -msoft-float -fpic -fPIE \ + -msoft-float \ -ffunction-sections \ -fdata-sections \ -funwind-tables \ @@ -221,7 +221,7 @@ $(hide) $(PRIVATE_CXX) \ endef define $(combo_2nd_arch_prefix)transform-o-to-executable-inner -$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -fPIE -pie \ +$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -pie \ -Wl,-dynamic-linker,/system/bin/linker \ -Wl,--gc-sections \ -Wl,-z,nocopyreloc \ diff --git a/core/combo/TARGET_linux-arm64.mk b/core/combo/TARGET_linux-arm64.mk index f57f69576e..7a40b33f3f 100644 --- a/core/combo/TARGET_linux-arm64.mk +++ b/core/combo/TARGET_linux-arm64.mk @@ -73,7 +73,6 @@ TARGET_GLOBAL_CFLAGS += \ android_config_h := $(call select-android-config-h,linux-arm64) TARGET_GLOBAL_CFLAGS += \ - -fpic -fPIE \ -fstack-protector \ -ffunction-sections \ -fdata-sections \ @@ -186,7 +185,7 @@ $(hide) $(PRIVATE_CXX) \ endef define transform-o-to-executable-inner -$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -fPIE -pie \ +$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -pie \ -Wl,-dynamic-linker,/system/bin/linker64 \ -Wl,--gc-sections \ -Wl,-z,nocopyreloc \ diff --git a/core/combo/TARGET_linux-mips.mk b/core/combo/TARGET_linux-mips.mk index 0069bd0584..0e4cb9d5e7 100644 --- a/core/combo/TARGET_linux-mips.mk +++ b/core/combo/TARGET_linux-mips.mk @@ -84,7 +84,6 @@ android_config_h := $(call select-android-config-h,linux-mips) $(combo_2nd_arch_prefix)TARGET_GLOBAL_CFLAGS += \ $(TARGET_mips_CFLAGS) \ -U__unix -U__unix__ -Umips \ - -fpic -fPIE\ -ffunction-sections \ -fdata-sections \ -funwind-tables \ @@ -194,7 +193,7 @@ $(hide) $(PRIVATE_CXX) \ endef define $(combo_2nd_arch_prefix)transform-o-to-executable-inner -$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -fPIE -pie \ +$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -pie \ -Wl,-dynamic-linker,/system/bin/linker \ -Wl,--gc-sections \ -Wl,-z,nocopyreloc \ diff --git a/core/combo/TARGET_linux-mips64.mk b/core/combo/TARGET_linux-mips64.mk index 463c7a4bb7..d255e10d9e 100644 --- a/core/combo/TARGET_linux-mips64.mk +++ b/core/combo/TARGET_linux-mips64.mk @@ -84,7 +84,6 @@ android_config_h := $(call select-android-config-h,linux-mips64) TARGET_GLOBAL_CFLAGS += \ $(TARGET_mips_CFLAGS) \ -U__unix -U__unix__ -Umips \ - -fpic -fPIE\ -ffunction-sections \ -fdata-sections \ -funwind-tables \ @@ -198,7 +197,7 @@ $(hide) $(PRIVATE_CXX) \ endef define transform-o-to-executable-inner -$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -fPIE -pie \ +$(hide) $(PRIVATE_CXX) -nostdlib -Bdynamic -pie \ -Wl,-dynamic-linker,/system/bin/linker64 \ -Wl,--gc-sections \ -Wl,-z,nocopyreloc \ diff --git a/core/combo/TARGET_linux-x86.mk b/core/combo/TARGET_linux-x86.mk index 18331d52e1..b17efece24 100644 --- a/core/combo/TARGET_linux-x86.mk +++ b/core/combo/TARGET_linux-x86.mk @@ -180,7 +180,7 @@ $(hide) $(PRIVATE_CXX) \ $(PRIVATE_TARGET_GLOBAL_LDFLAGS) \ -nostdlib -Bdynamic \ -Wl,-z,nocopyreloc \ - -fPIE -pie \ + -pie \ $(PRIVATE_TARGET_GLOBAL_LD_DIRS) \ -Wl,-rpath-link=$(PRIVATE_TARGET_OUT_INTERMEDIATE_LIBRARIES) \ $(if $(filter true,$(PRIVATE_NO_CRT)),,$(PRIVATE_TARGET_CRTBEGIN_DYNAMIC_O)) \ diff --git a/core/combo/TARGET_linux-x86_64.mk b/core/combo/TARGET_linux-x86_64.mk index 4482a17a0a..4f4e7a2d74 100644 --- a/core/combo/TARGET_linux-x86_64.mk +++ b/core/combo/TARGET_linux-x86_64.mk @@ -183,7 +183,7 @@ $(hide) $(PRIVATE_CXX) \ $(PRIVATE_TARGET_GLOBAL_LDFLAGS) \ -nostdlib -Bdynamic \ -Wl,-z,nocopyreloc \ - -fPIE -pie \ + -pie \ $(PRIVATE_TARGET_GLOBAL_LD_DIRS) \ -Wl,-rpath-link=$(PRIVATE_TARGET_OUT_INTERMEDIATE_LIBRARIES) \ $(if $(filter true,$(PRIVATE_NO_CRT)),,$(PRIVATE_TARGET_CRTBEGIN_DYNAMIC_O)) \ diff --git a/core/definitions.mk b/core/definitions.mk index 177dbc771c..24c7a07df8 100644 --- a/core/definitions.mk +++ b/core/definitions.mk @@ -1413,7 +1413,7 @@ endef ifdef BUILD_HOST_static HOST_FPIE_FLAGS := else -HOST_FPIE_FLAGS := -fPIE -pie +HOST_FPIE_FLAGS := -pie endif ifneq ($(HOST_CUSTOM_LD_COMMAND),true)