gcc7 编译 linux内核提示 undefined reference to `____ilog2_NaN'

1. 问题

 gcc7.3 编译linux内核linux-4.10.4时,出现如下错误:

 

  GEN     .version
  CHK     include/generated/compile.h
  UPD     include/generated/compile.h
  CC      init/version.o
  LD      init/built-in.o
  LD      vmlinux.o
  MODPOST vmlinux.o
kernel/built-in.o: In function `update_wall_time':
/home/ubuntu/linux-4.10.4/kernel/time/timekeeping.c:2088: undefined reference to `____ilog2_NaN'
Makefile:969: recipe for target 'vmlinux' failed
make: *** [vmlinux] Error 1

2. 网上搜了下, 需要打patch

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/diff/?id=474c90156c8dcc2fa815e6716cc9394d7930cb9c

diff --git a/include/linux/log2.h b/include/linux/log2.h
index ef3d4f67118c..c373295f359f 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -16,12 +16,6 @@
 #include <linux/bitops.h>
 
 /*
- * deal with unrepresentable constant logarithms
- */
-extern __attribute__((const, noreturn))
-int ____ilog2_NaN(void);
-
-/*
  * non-constant log of base 2 calculators
  * - the arch may override these in asm/bitops.h if they can be implemented
  *   more efficiently than using fls() and fls64()
@@ -85,7 +79,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 #define ilog2(n)				\
 (						\
 	__builtin_constant_p(n) ? (		\
-		(n) < 1 ? ____ilog2_NaN() :	\
+		(n) < 2 ? 0 :			\
 		(n) & (1ULL << 63) ? 63 :	\
 		(n) & (1ULL << 62) ? 62 :	\
 		(n) & (1ULL << 61) ? 61 :	\
@@ -148,10 +142,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 		(n) & (1ULL <<  4) ?  4 :	\
 		(n) & (1ULL <<  3) ?  3 :	\
 		(n) & (1ULL <<  2) ?  2 :	\
-		(n) & (1ULL <<  1) ?  1 :	\
-		(n) & (1ULL <<  0) ?  0 :	\
-		____ilog2_NaN()			\
-				   ) :		\
+		1 ) :				\
 	(sizeof(n) <= 4) ?			\
 	__ilog2_u32(n) :			\
 	__ilog2_u64(n)				\
diff --git a/tools/include/linux/log2.h b/tools/include/linux/log2.h
index 41446668ccce..d5677d39c1e4 100644
--- a/tools/include/linux/log2.h
+++ b/tools/include/linux/log2.h
@@ -13,12 +13,6 @@
 #define _TOOLS_LINUX_LOG2_H
 
 /*
- * deal with unrepresentable constant logarithms
- */
-extern __attribute__((const, noreturn))
-int ____ilog2_NaN(void);
-
-/*
  * non-constant log of base 2 calculators
  * - the arch may override these in asm/bitops.h if they can be implemented
  *   more efficiently than using fls() and fls64()
@@ -78,7 +72,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 #define ilog2(n)				\
 (						\
 	__builtin_constant_p(n) ? (		\
-		(n) < 1 ? ____ilog2_NaN() :	\
+		(n) < 2 ? 0 :			\
 		(n) & (1ULL << 63) ? 63 :	\
 		(n) & (1ULL << 62) ? 62 :	\
 		(n) & (1ULL << 61) ? 61 :	\
@@ -141,10 +135,7 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
 		(n) & (1ULL <<  4) ?  4 :	\
 		(n) & (1ULL <<  3) ?  3 :	\
 		(n) & (1ULL <<  2) ?  2 :	\
-		(n) & (1ULL <<  1) ?  1 :	\
-		(n) & (1ULL <<  0) ?  0 :	\
-		____ilog2_NaN()			\
-				   ) :		\
+		1 ) :				\
 	(sizeof(n) <= 4) ?			\
 	__ilog2_u32(n) :			\
 	__ilog2_u64(n)				\

复制patch内容 保存成 patch.diff文件, 拷贝文件到linux内核路径下,

运行命令: patch -i patch.diff, 提示输入操作文件时, 先后输入 include/linux/log2.h, tools/include/linux/log2.h即可。

 

3. 后记

看patch的意思,就是不用'____ilog2_NaN'函数。

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
根据提供的引用内容,"undefined reference to __ashldi3"错误通常是由于缺少对应的库文件或链接错误导致的。这个错误通常与编译器和链接器有关。 解决这个问题的方法有以下几种: 1. 确保链接器能够找到正确的库文件。你可以尝试在编译命令中添加对应的库文件路径和库文件名。例如,如果你使用的是gcc编译器,可以使用"-L"选项指定库文件路径,使用"-l"选项指定库文件名。例如: ```shell gcc -o output_file source_file.c -L/path/to/library -lmylibrary ``` 2. 检查库文件是否正确安装。如果你使用的是第三方库,确保你已经正确安装了该库,并且库文件的路径正确。 3. 检查编译选项是否正确。有时候,编译选项可能会导致链接错误。你可以尝试修改编译选项,例如添加或删除一些选项,以解决链接错误。 4. 检查代码中是否存在语法错误或其他错误。有时候,链接错误可能是由于代码中的错误导致的。确保你的代码没有语法错误,并且所有的函数和变量都正确定义和引用。 5. 如果以上方法都无效,你可以尝试重新编译和链接整个项目。有时候,重新编译和链接可以解决一些链接错误。 请注意,以上方法只是一些常见的解决链接错误的方法,具体的解决方法可能因情况而异。如果你能提供更多的上下文信息,例如你使用的编译器和链接器版本,以及你的代码和编译命令,我可以给出更具体的建议。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值