Kernel compiling and the PIC mode error

Kernel compiling and the PIC mode error

If you see this when compiling an Ubuntu or other kernel (my case was a Yocto kernel on an Ubuntu 17.04 distro)

...
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CC scripts/mod/empty.o
/usr/src/linux-4.4/scripts/mod/empty.c:1:0: error: code model kernel does not support PIC mode
/* empty file to figure out endianness / word size */

 

Then the issue is with your gcc installation. In gcc 6+ versions, PIE (position independent executables) is enabled by default. So in order to compile you need to disable it. Even gcc 5 has the issue. This is a known bug for gcc. Bug Link.

So far there is no official patch from gcc side, so the workaround is to patch the Makefile of kernel source.

If you are familiar with patching the source file use the codes from this link to create the patch file then try to compile.Patch File

Here’s the patch to add to your kernel Makefile to disable PIE compiling.

diff –git a/Makefile b/Makefile
index 5c18baa..e342473 100644
— a/Makefile
+++ b/Makefile
@@ -612,6 +612,12 @@ endif # $(dot-config)
# Defaults to vmlinux, but the arch makefile usually adds further targets
all: vmlinux

+# force no-pie for distro compilers that enable pie by default
+KBUILD_CFLAGS += $(call cc-option, -fno-pie)
+KBUILD_CFLAGS += $(call cc-option, -no-pie)
+KBUILD_AFLAGS += $(call cc-option, -fno-pie)
+KBUILD_CPPFLAGS += $(call cc-option, -fno-pie)

# The arch Makefile can set ARCH_{CPP,A,C}FLAGS to override the default
# values of the respective KBUILD_* variables
ARCH_CPPFLAGS :=

 

Leave a Reply

Your email address will not be published. Required fields are marked *