Skip to contentSkip to conversation

Move position-independant code flags into the target specific Makefiles

PATCH In review Thomas Huth 2 participants 2 messages Nov 13 – Nov 18

Subsystems (6)
KVM Kbuild RISC-V s390 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Move position-independant code flags into the target specific Makefiles

The settings for position-independant code are currently quite a mess:
It depends on the target whether we need the flags or rather should even disable PIC / PIE, but currently the settings are spread between the main Makefile (-fno-pic is the default, unless CONFIG_EFI has been set) and the target specific Makefiles.
This leads to some weird situations where we have -fno-pic in the CFLAGS, but link with -pie in the LDFLAGS since we still want to get a relocation table, but then again need to add a "-z notext" to the linker flags to avoid warnings about relocation entries in the text segment. Let's face it: Always adding -fno-pic / -no-pie in the main Makefile is a bad idea. Move the settings to the target specific Makefiles instead where we know what we really want, and then we can finally drop the "-z notext" settings, too.

---
 Makefile            | 6 +-----
 arm/Makefile.arm    | 2 ++
 arm/Makefile.arm64  | 3 +--
 powerpc/Makefile    | 5 ++++-
 riscv/Makefile      | 1 +
 s390x/Makefile      | 3 +++
 x86/Makefile.common | 5 +++++
 7 files changed, 17 insertions(+), 8 deletions(-)
Makefile +1 -5
index 8e002043..b0a0b89f 100644
@@ -70,8 +70,6 @@ fomit_frame_pointer := $(call cc-option, $(frame-pointer-flag), "")
 fno_stack_protector := $(call cc-option, -fno-stack-protector, "")
 fno_stack_protector_all := $(call cc-option, -fno-stack-protector-all, "")
 wno_frame_address := $(call cc-option, -Wno-frame-address, "")
-fno_pic := $(call cc-option, -fno-pic, "")
-no_pie := $(call cc-option, -no-pie, "")
 wclobbered := $(call cc-option, -Wclobbered, "")
 wunused_but_set_parameter := $(call cc-option, -Wunused-but-set-parameter, "")
 wmissing_parameter_type := $(call cc-option, -Wmissing-parameter-type, "")
@@ -85,8 +83,6 @@ COMMON_CFLAGS += $(wno_frame_address)
 COMMON_CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
 ifeq ($(CONFIG_EFI),y)
 COMMON_CFLAGS += $(EFI_CFLAGS)
-else
-COMMON_CFLAGS += $(fno_pic)
 endif
 COMMON_CFLAGS += $(wclobbered)
 COMMON_CFLAGS += $(wunused_but_set_parameter)
@@ -103,7 +99,7 @@ CFLAGS += $(LATE_CFLAGS)
 
 autodepend-flags = -MMD -MP -MF $(dir $*).$(notdir $*).d
 
-LDFLAGS += -nostdlib $(no_pie) -z noexecstack
+LDFLAGS += -nostdlib -z noexecstack
 
 $(libcflat): $(cflatobjs)
 	$(AR) rcs $@ $^
arm/Makefile.arm +2 -0
index 7734e17f..7313c843 100644
@@ -13,6 +13,8 @@ endif
 
 CFLAGS += $(machine)
 CFLAGS += -mno-unaligned-access
+CFLAGS += -fno-pic
+LDFLAGS += -no-pie
 
 ifeq ($(TARGET),qemu)
 arch_LDFLAGS = -Ttext=40010000
arm/Makefile.arm64 +1 -2
index a40c830d..ea0202eb 100644
@@ -7,8 +7,7 @@ bits = 64
 ldarch = elf64-littleaarch64
 
 arch_LDFLAGS = -pie -n
-arch_LDFLAGS += -z notext
-CFLAGS += -mstrict-align
+CFLAGS += -fpie -mstrict-align
 
 sve_flag := $(call cc-option, -march=armv8.5-a+sve, "")
 ifneq ($(strip $(sve_flag)),)
powerpc/Makefile +4 -1
index 41e752b0..eff129b0 100644
@@ -66,6 +66,9 @@ CFLAGS += -ffreestanding
 CFLAGS += -O2 -msoft-float -mno-altivec $(mabi_no_altivec)
 CFLAGS += -I $(SRCDIR)/lib -I $(SRCDIR)/lib/libfdt -I lib
 CFLAGS += -Wa,-mregnames
+CFLAGS += -fpie
+
+LDFLAGS += -pie
 
 # We want to keep intermediate files
 .PRECIOUS: %.o
@@ -78,7 +81,7 @@ include $(SRCDIR)/scripts/asm-offsets.mak
 
 FLATLIBS = $(libcflat) $(LIBFDT_archive)
 %.elf: CFLAGS += $(arch_CFLAGS)
-%.elf: LDFLAGS += $(arch_LDFLAGS) -pie -n
+%.elf: LDFLAGS += $(arch_LDFLAGS) -n
 %.elf: %.o $(FLATLIBS) $(SRCDIR)/powerpc/flat.lds $(cstart.o) $(reloc.o) %.aux.o
 	$(LD) $(LDFLAGS) -o $@ \
 		-T $(SRCDIR)/powerpc/flat.lds --build-id=none \
riscv/Makefile +1 -0
index 348a30a7..79a9c1ad 100644
@@ -93,6 +93,7 @@ CFLAGS += -std=gnu99
 CFLAGS += -ffreestanding
 CFLAGS += -O2
 CFLAGS += -I $(SRCDIR)/lib -I $(SRCDIR)/lib/libfdt -I lib -I $(SRCDIR)/riscv
+CFLAGS += -fno-pic
 
 asm-offsets = lib/riscv/asm-offsets.h riscv/sbi-asm-offsets.h
 include $(SRCDIR)/scripts/asm-offsets.mak
s390x/Makefile +3 -0
index 97ed0b47..29bea5cc 100644
@@ -88,7 +88,10 @@ CFLAGS += -O2
 CFLAGS += -march=zEC12
 CFLAGS += -mbackchain
 CFLAGS += -fno-delete-null-pointer-checks
+CFLAGS += -fno-pic
+
 LDFLAGS += -Wl,--build-id=none
+LDFLAGS += -no-pie
 
 # We want to keep intermediate files
 .PRECIOUS: %.o %.lds
x86/Makefile.common +5 -0
index 054e2391..a0201fcd 100644
@@ -40,6 +40,11 @@ COMMON_CFLAGS += -Wa,--divide
 endif
 COMMON_CFLAGS += -O1
 
+ifneq ($(CONFIG_EFI),y)
+CFLAGS += -fno-pic
+LDFLAGS += -no-pie
+endif
+
 # stack.o relies on frame pointers.
 KEEP_FRAME_POINTER := y
 
Signature · 1 line
2.56.0
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Thomas Huth

The settings for position-independant code are currently quite a mess:
It depends on the target whether we need the flags or rather should even disable PIC / PIE, but currently the settings are spread between the main Makefile (-fno-pic is the default, unless CONFIG_EFI has been set) and the target specific Makefiles.
This leads to some weird situations where we have -fno-pic in the CFLAGS, but link with -pie in the LDFLAGS since we still want to get a relocation table, but then again need to add a "-z notext" to the linker flags to avoid warnings about relocation entries in the text segment. Let's face it: Always adding -fno-pic / -no-pie in the main Makefile is a bad idea. Move the settings to the target specific Makefiles instead where we know what we really want, and then we can finally drop the "-z notext" settings, too.

Quoted 10 lines
---
 Makefile            | 6 +-----
 arm/Makefile.arm    | 2 ++
 arm/Makefile.arm64  | 3 +--
 powerpc/Makefile    | 5 ++++-
 riscv/Makefile      | 1 +
 s390x/Makefile      | 3 +++
 x86/Makefile.common | 5 +++++
 7 files changed, 17 insertions(+), 8 deletions(-)