Skip to contentSkip to conversation

build: disable PIE during linking if necessary

PATCH In review Wei Liu 3 participants 5 messages Feb 1, 2017

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 build: disable PIE during linking if necessary

Starting from ee3e265688, $(CC) is used for linking. That means all default $(CC) flags coming from distro takes effect.

On Debian Stretch, gcc contains -pie by default, which makes the final object fail to link. We need to explicitly disable PIE when linking. Since not all versions of gcc support -no-pie, test its availability before adding.

Example error message:

/usr/bin/ld: /local/work/xtf.git/arch/x86/boot/head_pv64.o: relocation R_X86_64_32S against symbol `start_info' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /local/work/xtf.git/arch/x86/entry_64-pv64.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

---
 build/common.mk | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
build/common.mk +10 -1
index 7c4870e..8e08c7e 100644
@@ -50,6 +50,15 @@ obj-perarch :=
 obj-perenv  :=
 include $(ROOT)/build/files.mk
 
+
+cc-option = $(shell if [ -z "`echo 'int p=1;' | $(CC) $(1) -S -o /dev/null -x c - 2>&1`" ]; \
+			then echo y; else echo n; fi)
+
+# Disable PIE, but need to check if compiler supports it
+ifeq ($(call cc-option,-no-pie),y)
+DISABLE_PIE_LDFLAGS := -no-pie
+endif
+
 # Run once per environment to set up some common bits & pieces
 define PERENV_setup
 
@@ -61,7 +70,7 @@ CFLAGS_$(1) := $$(CFLAGS_$($(1)_arch)) $$(COMMON_CFLAGS-$(1)) -DCONFIG_ENV_$(1)
 
 link-$(1) := $(ROOT)/arch/x86/link-$(1).lds
 
-LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib
+LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib $(DISABLE_PIE_LDFLAGS)
 
 # Needs to pick up test-provided obj-perenv and obj-perarch
 DEPS-$(1) = $(head-$(1)) \
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 10 lines · Wei Liu

Starting from ee3e265688, $(CC) is used for linking. That means all default $(CC) flags coming from distro takes effect.

On Debian Stretch, gcc contains -pie by default, which makes the final object fail to link. We need to explicitly disable PIE when linking. Since not all versions of gcc support -no-pie, test its availability before adding.

Example error message:

Quoted 8 lines

/usr/bin/ld: /local/work/xtf.git/arch/x86/boot/head_pv64.o: relocation R_X86_64_32S against symbol `start_info' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /local/work/xtf.git/arch/x86/entry_64-pv64.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

LGTM. Any objection if I s/DISABLE_PIE_LDFLAGS/COMMON_LDFLAGS/ on commit? I don't think the name needs to be that specific.

~Andrew

Quoted 25 lines
---
 build/common.mk | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
build/common.mk +9 -0
index 7c4870e..8e08c7e 100644
@@ -50,6 +50,15 @@ obj-perarch :=
 obj-perenv  :=
 include $(ROOT)/build/files.mk
 
+
+cc-option = $(shell if [ -z "`echo 'int p=1;' | $(CC) $(1) -S -o /dev/null -x c - 2>&1`" ]; \
+			then echo y; else echo n; fi)
+
+# Disable PIE, but need to check if compiler supports it
+ifeq ($(call cc-option,-no-pie),y)
+DISABLE_PIE_LDFLAGS := -no-pie
+endif
+
 # Run once per environment to set up some common bits & pieces
 define PERENV_setup
 

build/common.mk CFLAGS_$(1) := $$(CFLAGS_$($(1)_arch)) $$(COMMON_CFLAGS-$(1)) -DCONFIG_ENV_$(1) · line 76

Quoted 8 lines
build/common.mk +1 -1
@@ -61,7 +70,7 @@ CFLAGS_$(1) := $$(CFLAGS_$($(1)_arch)) $$(COMMON_CFLAGS-$(1)) -DCONFIG_ENV_$(1)
 
 link-$(1) := $(ROOT)/arch/x86/link-$(1).lds
 
-LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib
+LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib $(DISABLE_PIE_LDFLAGS)
 
 # Needs to pick up test-provided obj-perenv and obj-perarch
 DEPS-$(1) = $(head-$(1)) \

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

author
#2 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Wei Liu

Starting from ee3e265688, $(CC) is used for linking. That means all default $(CC) flags coming from distro takes effect.

On Debian Stretch, gcc contains -pie by default, which makes the final object fail to link. We need to explicitly disable PIE when linking. Since not all versions of gcc support -no-pie, test its availability before adding.

Example error message:

/usr/bin/ld: /local/work/xtf.git/arch/x86/boot/head_pv64.o: relocation R_X86_64_32S against symbol `start_info' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /local/work/xtf.git/arch/x86/entry_64-pv64.o: relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

Quoted 4 lines
Quoted 1 line

LGTM. Any objection if I s/DISABLE_PIE_LDFLAGS/COMMON_LDFLAGS/ on commit? I don't think the name needs to be that specific.

NP. Do whatever you like.

I just want to get it fixed. ;-)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
In reply to Wei Liu
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 17:21, <[email protected]> wrote:
build/common.mk +0 -0
@@ -50,6 +50,15 @@ obj-perarch :=
 obj-perenv  :=

build/common.mk obj-perarch := · line 60

Quoted 10 lines
build/common.mk +8 -0
 include $(ROOT)/build/files.mk
 
+
+cc-option = $(shell if [ -z "`echo 'int p=1;' | $(CC) $(1) -S -o /dev/null -x c - 2>&1`" ]; \
+			then echo y; else echo n; fi)
+
+# Disable PIE, but need to check if compiler supports it
+ifeq ($(call cc-option,-no-pie),y)
+DISABLE_PIE_LDFLAGS := -no-pie
+endif

How about

LDFLAGS-$(call cc-option,-no-pie) += -no-pie

and ...

build/common.mk CFLAGS_$(1) := $$(CFLAGS_$($(1)_arch)) $$(COMMON_CFLAGS-$(1)) -DCONFIG_ENV_$(1) · line 73

Quoted 6 lines
build/common.mk +1 -1
@@ -61,7 +70,7 @@ CFLAGS_$(1) := $$(CFLAGS_$($(1)_arch)) $$(COMMON_CFLAGS-$(1)) -DCONFIG_ENV_$(1)
 
 link-$(1) := $(ROOT)/arch/x86/link-$(1).lds
 
-LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib
+LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib $(DISABLE_PIE_LDFLAGS)

LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib $(LDFLAGS-y)

(or something along those lines)?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
Quoted 23 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 01.02.17 at 17:21, <[email protected]> wrote:
build/common.mk +0 -0
@@ -50,6 +50,15 @@ obj-perarch :=
 obj-perenv  :=

build/common.mk obj-perarch := · line 60

Quoted 10 lines
build/common.mk +8 -0
 include $(ROOT)/build/files.mk
 
+
+cc-option = $(shell if [ -z "`echo 'int p=1;' | $(CC) $(1) -S -o /dev/null -x c - 2>&1`" ]; \
+			then echo y; else echo n; fi)
+
+# Disable PIE, but need to check if compiler supports it
+ifeq ($(call cc-option,-no-pie),y)
+DISABLE_PIE_LDFLAGS := -no-pie
+endif

How about

LDFLAGS-$(call cc-option,-no-pie) += -no-pie

and ...

Quoted 2 lines
build/common.mk +0 -0
@@ -61,7 +70,7 @@ CFLAGS_$(1) := $$(CFLAGS_$($(1)_arch)) $$(COMMON_CFLAGS-$(1)) -DCONFIG_ENV_$(1)
 
Quoted 7 lines

build/common.mk CFLAGS_$(1) := $$(CFLAGS_$($(1)_arch)) $$(COMMON_CFLAGS-$(1)) -DCONFIG_ENV_$(1) · line 73

Quoted 4 lines
build/common.mk +1 -1
 link-$(1) := $(ROOT)/arch/x86/link-$(1).lds
 
-LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib
+LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib $(DISABLE_PIE_LDFLAGS)

LDFLAGS_$(1) := -Wl,-T,$$(link-$(1)) -nostdlib $(LDFLAGS-y)

(or something along those lines)?

Ok - will fix up to this suggestion.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel