gcc -O2和没有使用-O2的区别导致了错误。

6

编译包含open("FILENAME", O_RDONLY);的文件时,如果没有使用-O2标志,一切都很好。但是当打开了-O2后,我遇到了以下问题:

/usr/include/x86_64-linux-gnu/bits/fcntl2.h: In function ‘open’:
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:44:7: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:45:26: error: call to ‘__open_too_many_args’ declared with attribute error: open can be called either with 2 or 3 arguments, not more
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:42:1: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:60:3: error: invalid use of ‘__builtin_va_arg_pack ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h: In function ‘open64’:
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:76:7: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:77:28: error: call to ‘__open64_too_many_args’ declared with attribute error: open64 can be called either with 2 or 3 arguments, not more
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:74:1: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:92:3: error: invalid use of ‘__builtin_va_arg_pack ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h: In function ‘openat’:
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:120:7: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:121:28: error: call to ‘__openat_too_many_args’ declared with attribute error: openat can be called either with 3 or 4 arguments, not more
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:118:1: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:136:3: error: invalid use of ‘__builtin_va_arg_pack ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h: In function ‘openat64’:
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:154:7: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:155:30: error: call to ‘__openat64_too_many_args’ declared with attribute error: openat64 can be called either with 3 or 4 arguments, not more
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:152:1: error: invalid use of ‘__builtin_va_arg_pack_len ()’
/usr/include/x86_64-linux-gnu/bits/fcntl2.h:170:3: error: invalid use of ‘__builtin_va_arg_pack ()’

问题出在哪里?这是一个混合的C/C++项目,但这个问题出现在C部分。gcc 4.6.1,内核3.0.0。

编辑:事实证明,注释掉那些行会导致另一种类型的错误,例如:

/usr/include/x86_64-linux-gnu/bits/stdio2.h: In function ‘sprintf’:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:34:3: error: invalid use of ‘__builtin_va_arg_pack ()’

6
您能否提供一个最小但完整的代码示例,以触发此错误? - luiscubal
奇怪,你确定它来自这行吗?你包含了所有需要的东西吗?(用于open和O_RDONLY) - BenjaminB
你是调用 g++ 还是 gcc - 0xC0000022L
4个回答

8

3
尝试使用-fno-builtins进行编译。如果问题得到解决,那么显然你肯定遇到了某种问题,但它可能不在你的源代码中。

2

我会简单地下载一个不同(稍旧?)的内核构建版本:

这里是一个漏洞报告,无论价值如何:

https://bugs.archlinux.org/task/27100

而且,我完全不知道为什么“-O2”会与此特定错误有关……

补充说明: 这个链接可能会给你更多关于错误信息本身的解释。但是再次强调-我建议您将尝试一个不同的内核构建作为您的第一步:

http://gcc.gnu.org/ml/gcc-patches/2007-09/msg00675.html


O2 会导致错误,因为编译器在优化时使用了内置函数。而在非优化时,编译器则使用库函数。 - Zan Lynx
通常,一些检测错误的代码属于各种优化过程。因此,只有在启用这些过程时,编译器才能检测到这些错误。 - janneb

1

如果您想忽略此错误,请考虑删除标志-Wp,-D_FORTIFY_SOURCE=2。 例如,如果您使用rpmbuild,则此标志由RPM_OPT_FLAGS引入。

%build
export CFLAGS="$RPM_OPT_FLAGS"
export CXXFLAGS="$RPM_OPT_FLAGS"
./configure …

这里有一个简单的方法,可以保留除了提到的标志以外的所有内容。

OPT_FLAGS=`echo $RPM_OPT_FLAGS | sed 's/-Wp,-D_FORTIFY_SOURCE=2 //'`
export CFLAGS="$OPT_FLAGS"
export CXXFLAGS="$OPT_FLAGS"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接