文章目录

  • 1. 编译内核
    • 1.1. 修改gcc优化等级
    • 1.2. 防止`modpost: Section mismatches detected.`错误
    • 1.3. 根据需要编译内核
    • 1.4. 修改子目录Makefile
    • 1.5. 重新执行编译
  • 2. 参考资料

1. 编译内核

进入kernel 源码目录

1.1. 修改gcc优化等级

diff --git a/Makefile b/Makefile
index d4d36c619..1047c83c6 100644
--- a/Makefile
+++ b/Makefile
@@ -701,11 +701,11 @@ KBUILD_CFLAGS     += $(call cc-disable-warning, format-overflow)KBUILD_CFLAGS  += $(call cc-disable-warning, address-of-packed-member)ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE
-KBUILD_CFLAGS += -O2
+KBUILD_CFLAGS += -O0else ifdef CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE_O3
-KBUILD_CFLAGS += -O3
+KBUILD_CFLAGS += -O0else ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
-KBUILD_CFLAGS += -Os
+KBUILD_CFLAGS += -O0endififdef CONFIG_CC_DISABLE_WARN_MAYBE_UNINITIALIZED

1.2. 防止modpost: Section mismatches detected.错误

如果此时直接执行make进行编译,会出现modpost: Section mismatches detected.错误。

解决方法是修改scripts/mod/modpost.c

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index d2a30a7b3..58e2237c2 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -2670,7 +2670,7 @@ int main(int argc, char **argv)if (dump_write)write_dump(dump_write);
-       if (sec_mismatch_count && sec_mismatch_fatal)
+       if (0 && sec_mismatch_count && sec_mismatch_fatal)fatal("modpost: Section mismatches detected.\n""Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");for (n = 0; n < SYMBOL_HASH_SIZE; n++) {

1.3. 根据需要编译内核

make tinyconfig ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- 2>&1 | tee -a build.log
make vmlinux -j4 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- 2>&1 | tee -a build_error.log

此时编译失败,原因是部分函数未定义,具体原因可以参考宋宝华: 关于Linux编译优化几个必须掌握的姿势

  MODINFO modules.builtin.modinfoLD      vmlinux
mm/page-writeback.o: In function `page_index':
page-writeback.c:(.text+0x21a4): undefined reference to `__page_file_index'
page-writeback.c:(.text+0x21a4): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__page_file_index'
mm/truncate.o: In function `truncate_exceptional_pvec_entries':
truncate.c:(.text+0x199c): undefined reference to `dax_delete_mapping_entry'
truncate.c:(.text+0x199c): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `dax_delete_mapping_entry'
......
mm/rmap.o: In function `linear_page_index':
rmap.c:(.text+0x3110): undefined reference to `linear_hugepage_index'
Makefile:1077: recipe for target 'vmlinux' failed
make: *** [vmlinux] Error 1

1.4. 修改子目录Makefile

修改方法就是单独为提示存在未定义符号的文件修改gcc优化等级。方法是在Makefile中添加CFLAGS_file.o += -O

拷贝如下内容新建脚本enbale_O0.sh,使用enabel_O0.sh和上述编译失败的build_error.log文件,执行./enable_O0.sh build_error.log

#!/bin/bash# make clean -j16
# make all -j4 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- 2>&1 |tee -a build_error.logMAKE_LOG=""
BUILD_PATH="$(pwd -P)"if [ $# -ge 1 ]; thenMAKE_LOG=$1
elseecho "Usage: $(basename $0) build_error.log [build_path]"exit -1
fiif [ $# -ge 2 ]; thenif [ -d $2 ]; thenBUILD_PATH=$2BUILD_PATH=${BUILD_PATH%*/}elseecho "$2 No such directory"exit -1fi
fiif [ ! -f ${MAKE_LOG} ]; thenecho "${MAKE_LOG}: No such file"exit -1
fiadd_cflag()
{make_file=$1obj_file=$2if [ -e ${make_file} ]; thenline_no=$(grep -snv '^#' ${make_file}  \| cut -d : -f 1 | head -1)str_cflags="CFLAGS_${obj_file}"if [[ "" == "$(grep ${str_cflags}.*O ${make_file})" ]]; thencmd="${line_no}i\\${str_cflags} += -O"sed -i "${cmd}" ${make_file}echo "${make_file}: add ${str_cflags}"fifi
}vmlinux_add_cflag()
{log_file=$1filelist=$(grep -shw -B1 'undefined reference' ${log_file}      \| grep 'In function' | cut -d : -f 1 | sort -u)for f in ${filelist}dopath=$(dirname ${f})obj_file=$(basename ${f})make_file=${path}/Makefileadd_cflag ${make_file} ${obj_file}done
}process_one()
{# ERROR: "alloc_test_extent_buffer" [fs/btrfs/btrfs.ko] undefined!str=$1symbol=$(echo ${str} | awk '{print $2}')symbol=${symbol:1:0-1}      # 删除 ""ko=$(echo ${str} | awk '{print $3}')ko=${ko:1:0-1}             # 删除 []path=$(dirname ${ko})filelist=$(grep -rl ${symbol} ${BUILD_PATH}/${path}/*.o)echo $filelistmake_file=${path}/Makefilefor f in ${filelist}dof=${f#${BUILD_PATH}/}     # 从左边删除 "${BUILD_PATH}/"obj=$(basename ${f})add_cflag ${make_file} ${obj}done
}module_add_cflag()
{log_file=$1IFS=$'\n'       # 按行遍历str_list=$(grep -sh "^ERROR:.*undefined!" ${log_file} | sort -u)for str in ${str_list}doprocess_one ${str}done
}vmlinux_add_cflag ${MAKE_LOG}
module_add_cflag ${MAKE_LOG}

执行结束后,子目录Makefile改动如下:

diff --git a/mm/Makefile b/mm/Makefile
index d99684669..52a1962ea 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -2,6 +2,14 @@## Makefile for the linux memory manager.#
+CFLAGS_truncate.o += -O
+CFLAGS_rmap.o += -O
+CFLAGS_page-writeback.o += -O
+CFLAGS_mremap.o += -O
+CFLAGS_mprotect.o += -O
+CFLAGS_mincore.o += -O
+CFLAGS_memory.o += -O
+CFLAGS_gup.o += -OKASAN_SANITIZE_slab_common.o := nKASAN_SANITIZE_slab.o := n

1.5. 重新执行编译

make clean
make vmlinux -j4 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- 2>&1 | tee -a build_ok.log

此时可正常编译通过。

  MODINFO modules.builtin.modinfoLD      vmlinuxSORTEX  vmlinuxSYSMAP  System.map

2. 参考资料

  1. 宋宝华: 关于Linux编译优化几个必须掌握的姿势
  2. runninglinuxkernel_4

使用-O0编译Linux内核相关推荐

  1. linux编译.o文件,使用-O0编译Linux内核

    文章目录 1. 编译内核 1.1. 修改gcc优化等级 1.2. 防止`modpost: Section mismatches detected.`错误 1.3. 根据需要编译内核 1.4. 修改子目 ...

  2. Linux内核开发_1_编译LInux内核

    目录 1. 准备工作 1.1 学习环境 1.2 下载Linux内核源码 1.3 解压Linux内核 1.4 目录结构介绍 2. Linux内核配置 2.1 配置选项 1. make config 2. ...

  3. 安装debian总结以及编译linux内核

    1. 安装debian 使用unetbootin(http://unetbootin.sourceforge.net/)来创建启动盘,并且下载debian的基本包. 将磁盘进行压缩操作,并且保留出一个 ...

  4. 如何解决编译linux内核(解决声卡问题),遭遇fatal error: linux/limits.h: 没有那个文件或目录

    如何解决编译linux内核(解决声卡问题),遭遇fatal error: linux/limits.h: 没有那个文件或目录 参考文章: (1)如何解决编译linux内核(解决声卡问题),遭遇fata ...

  5. 【Linux 内核】编译 Linux 内核 ⑥ ( 安装 OpenSSL | 安装其它依赖库 | 内核编译完成 )

    文章目录 一.安装 OpenSSL 二.安装其它依赖库 三.Linux 内核编译完成 一.安装 OpenSSL 参考 [错误记录]编译 Linux 内核报错 ( fatal error: openss ...

  6. 【错误记录】编译 Linux 内核报错 ( fatal error: openssl/opensslv.h: No such file or directory )

    文章目录 一.报错信息 二.解决方案 一.报错信息 编译 Linux 内核 , 执行 sudo make 命令 , 开始正式编译 Linux 内核 , 报如下错误 : root@ubuntu:~/ke ...

  7. 【Linux 内核】编译 Linux 内核 ⑤ ( 查看 .config 编译配置文件 | 正式编译内核 )

    文章目录 一.查看 .config 编译配置文件 二.正式编译内核 一.查看 .config 编译配置文件 在上一篇博客 [Linux 内核]编译 Linux 内核 ④ ( 打开 Linux 内核编译 ...

  8. 【错误记录】编译 Linux 内核报错 ( /bin/sh: 1: bison: not found )

    文章目录 一.报错信息 二.解决方案 一.报错信息 编译 Linux 内核 , 执行 make menuconfig 配置菜单命令 , 报如下错误 : root@ubuntu:~/kernel/lin ...

  9. 【错误记录】编译 Linux 内核报错 ( /bin/sh: 1: flex: not found )

    文章目录 一.报错信息 二.解决方案 一.报错信息 编译 Linux 内核 , 执行 make menuconfig 配置菜单命令 , 报如下错误 : root@ubuntu:~/kernel/lin ...

最新文章

  1. python3 opencv_Python3 OpenCV3 图像处理基础
  2. BCZM : 1.13
  3. python 使用小知识总结(持续更新ing)
  4. 【CodeForces - 731C】Socks(并查集,思维)
  5. C++学习之路 | PTA乙级—— 1078 字符串压缩与解压 (20 分)(精简)
  6. 冲刺!11.14-11.15
  7. 通过JAVA的反射调用类中的公有私有方法
  8. 【安装包】Dev-cpp
  9. linux的源码安装步骤(以安装nginx为例)
  10. 服务器ghost备份后无法进入系统还原,ghost恢复后,系统不能启动的问题
  11. 双光耦开关电源电路图_简单的开关电源电路图大全(六款简单的开关电源电路设计原理图详解)...
  12. imx6,imx7和am335环境变量配置文件
  13. 合并两张图片php,php多张图片合并方法分享
  14. Visual Studio Code 配置C/C++编译环境流程及问题解决(Win10环境)
  15. MVC模式初体验 properties解析工具 与 Dao层的结合 —————— 开开开山怪
  16. 讯飞AIUI智能机器人1
  17. 苹果公司的“多样化”定义:包括加拿大人
  18. docker ps出错
  19. TraceMe.exe注册码破解及注册机编写
  20. python自动化办公读后感_《Python编程快速上手——让繁琐的工作自动化》读书笔记3...

热门文章

  1. 零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧
  2. React之Refs的使用
  3. 模仿英雄选择界面 setvisible的作用不仅仅是JFrame调用
  4. sql coalesce
  5. 随机数:Python3.7的random模块详解
  6. 你有一份超详细的深度学习装机指南等待认领!(下篇)
  7. 局域网 - 打印机共享与连接方法
  8. 【OpenGL学习】绘制三角形
  9. 【推荐系统】短视频推荐系统概述
  10. mxnet训练arcface加速实验