内核不支持PIC模式编译?

Joh*_*ong 15 compiling kernel gcc

我一直在尝试在 Ubuntu 16.10 上编译最新的 Linux v4.8.9 内核,在我设置默认 .config、使用 menuconfig 修改它并自行运行 make 后,此错误不断弹出。解压缩文件后,我还运行了 make mrproper。这是我运行 make 后的输出:

scripts/kconfig/conf  --silentoldconfig Kconfig
  SYSTBL  arch/x86/entry/syscalls/../../include/generated/asm/syscalls_32.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/asm/unistd_32_ia32.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/asm/unistd_64_x32.h
  SYSTBL  arch/x86/entry/syscalls/../../include/generated/asm/syscalls_64.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_32.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_64.h
  SYSHDR  arch/x86/entry/syscalls/../../include/generated/uapi/asm/unistd_x32.h
  HOSTCC  arch/x86/tools/relocs_32.o
  HOSTCC  arch/x86/tools/relocs_64.o
  HOSTCC  arch/x86/tools/relocs_common.o
  HOSTLD  arch/x86/tools/relocs
  CHK     include/config/kernel.release
  UPD     include/config/kernel.release
  WRAP    arch/x86/include/generated/asm/clkdev.h
  WRAP    arch/x86/include/generated/asm/cputime.h
  WRAP    arch/x86/include/generated/asm/dma-contiguous.h
  WRAP    arch/x86/include/generated/asm/early_ioremap.h
  WRAP    arch/x86/include/generated/asm/mcs_spinlock.h
  WRAP    arch/x86/include/generated/asm/mm-arch-hooks.h
  CHK     include/generated/uapi/linux/version.h
  UPD     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  UPD     include/generated/utsrelease.h
  CC      kernel/bounds.s
kernel/bounds.c:1:0: error: code model kernel does not support PIC mode
 /*

Kbuild:45: recipe for target 'kernel/bounds.s' failed
make[1]: *** [kernel/bounds.s] Error 1
Makefile:1015: recipe for target 'prepare0' failed
make: *** [prepare0] Error 2
Run Code Online (Sandbox Code Playgroud)

我还尝试在 make 命令之后使用 -no-pie 选项进行编译,但它会呈现一组新错误,不断说“缺少目标”。我有 gcc 版本 6.2.0。我还安装了 gcc-5,因为我一开始认为这可能是因为 gcc 太新了,但 gcc-5 中仍然存在同样的问题。邮件列表或其他论坛中的某个人提到通过直接从 git 克隆解决了问题,但这对我的场景也没有帮助。

我很好奇是否还有其他人遇到过这个问题,如果有,可能的解决方法是什么?

注意:如果这可能是问题的根源,我正在 Mac 上的 Virtualbox 中运行 Ubuntu。

Joy*_*Joy 14

问题在于您的 gcc 安装,在 gcc 6+ 版本中,默认情况下启用 PIE(位置无关可执行文件)。所以为了编译你需要禁用它。即使是 gcc 5 也有问题。这是 gcc 的一个已知错误。错误链接

目前gcc方面还没有官方补丁,所以解决方法是打补丁内核源的Makefile。

如果您熟悉修补源文件,请使用此链接中的代码创建修补文件,然后尝试编译。补丁文件

如果您在安装补丁时遇到困难,请告诉我。


Nat*_*tim 11

打开 Makefile,查找 CFLAGS_EXTRA 并添加以下选项 -fno-pie

我有这条线:

EXTRA_CFLAGS += $(CFLAGS_EXTRA)
Run Code Online (Sandbox Code Playgroud)

我将其更改为:

EXTRA_CFLAGS += $(CFLAGS_EXTRA) -fno-pie
Run Code Online (Sandbox Code Playgroud)

对于构建内核 4,上面的标志是:KBUILD_CFLAGS.

它再次开始编译。