让 GCC 编译而不插入对 memcpy 的调用
我目前正在使用 GCC 4.5.3,为 PowerPC 440 编译,并且正在编译一些不需要 libc 的代码。我没有对 memcpy() 进行任何直接调用,但编译器似乎在构建过程中插入了一个。
有诸如 -nostdlib、-nostartfiles、-nodefaultlibs 之类的链接器选项,但我无法使用它们,因为我没有进行链接阶段。我只是编译。有了这样的内容:
$ powerpc-440-eabi-gcc -O2 -g -c -o output.o input.c
如果我用 nm 检查 output.o,我会看到对 memcpy 的引用:
$ powerpc-440-eabi-nm output.o | grep memcpy
U memcpy
$
GCC 手册页清楚地说明了如何使用链接器删除对 memcpy 的调用和其他 libc 调用,但我不希望编译器首先将它们插入,因为我使用的是完全不同的链接器(不是 GNU 的 ld,而且它不知道 libc)。
感谢您提供的任何帮助。
I'm currently using GCC 4.5.3, compiled for PowerPC 440, and am compiling some code that doesn't require libc. I don't have any direct calls to memcpy(), but the compiler seems to be inserting one during the build.
There are linker options like -nostdlib, -nostartfiles, -nodefaultlibs but I'm unable to use them as I'm not doing the linking phase. I'm only compiling. With something like this:
$ powerpc-440-eabi-gcc -O2 -g -c -o output.o input.c
If I check the output.o with nm, I see a reference to memcpy:
$ powerpc-440-eabi-nm output.o | grep memcpy
U memcpy
$
The GCC man page makes it clear how to remove calls to memcpy and other libc calls with the linker, but I don't want the compiler to insert them in the first place, as I'm using a completely different linker (not GNU's ld, and it doesn't know about libc).
Thanks for any help you can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不需要
-fno-builtins
或-ffreestand
因为它们会不必要地禁用许多重要的优化这实际上是由 gcc 的 tree-loop-distribute-patterns“优化”的,因此,要在保留有用的内置功能的同时禁用不需要的行为,您只需使用:
-fno-tree-loop-distribute-patterns
Musl-libc使用此标志进行构建,并在其配置脚本中包含以下注释(我查看了源代码,没有找到任何宏,所以这应该就足够了)
您还可以使用其优化属性将其作为属性添加到 gcc 中的各个函数中,以便其他函数可以从调用
mem*()
中受益或者,(至少现在)您可以在循环中添加一个令人困惑的 null asm 语句来阻止模式识别。
There is no need to
-fno-builtins
or-ffreestanding
as they will unnecessarily disable many important optimizationsThis is actually "optimized" by gcc's tree-loop-distribute-patterns, so to disable the unwanted behavior while keeping the useful builtin capabilities, you can just use:
-fno-tree-loop-distribute-patterns
Musl-libc uses this flag for its build and has the following note in their configure script (I looked through the source and didn't find any macros, so this should be enough)
You can also add this as an attribute to individual functions in gcc using its optimize attribute, so that other functions can benefit from calling
mem*()
Alternatively, (at least for now) you may add a confounding null asm statement into your loop to thwart the pattern recognition.
在某些情况下,Gcc 会发出对 memcpy 的调用,例如,如果您正在复制结构。
无法更改 GCC 行为,但您可以尝试通过修改代码来避免这种复制。最好的办法是查看程序集,找出 gcc 发出 memcpy 的原因并尝试解决它。但这会很烦人,因为您基本上需要了解 gcc 的工作原理。
摘自http://gcc.gnu.org/onlinedocs/gcc/Standards.html:
GCC 使用的大多数编译器支持例程都存在于 libgcc 中,但也有一些例外。 GCC 要求独立环境提供 memcpy、memmove、memset 和 memcmp。最后,如果使用 __builtin_trap,并且目标未实现陷阱模式,则 GCC 将发出中止调用。
Gcc emits call to memcpy in some circumstance, for example if you are copying a structure.
There is no way to change GCC behaviour but you can try to avoid this by modifying your code to avoid such copy. Best bet is to look at the assembly to figure out why gcc emitted the memcpy and try to work around it. This is going to be annoying though, since you basically need to understand how gcc works.
Extract from http://gcc.gnu.org/onlinedocs/gcc/Standards.html:
Most of the compiler support routines used by GCC are present in libgcc, but there are a few exceptions. GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp. Finally, if __builtin_trap is used, and the target does not implement the trap pattern, then GCC will emit a call to abort.
您需要使用
-fno-builtin
禁用该优化。我在尝试为 C 库编译 memcpy 时遇到过这个问题。它自称。哎呀!You need to disable a that optimization with
-fno-builtin
. I had this problem once when trying to compilememcpy
for a C library. It called itself. Oops!您还可以使您的二进制文件成为“独立”二进制文件:
(我添加的段落)
更多信息此处。相应的 gcc 选项(关键字
-ffreestand
或-fno-builtin
)可以在 此处。You can also make your binary a "freestanding" one:
(paragraph added by me)
More here. And the corresponding gcc option/s (keywords
-ffreestanding
or-fno-builtin
) can be found here.这是一个相当老的问题,但我遇到了同样的问题,并且这里的解决方案都不起作用。
所以我定义了这个函数:
然后用它代替memcpy。这已经永久解决了我的内联问题。如果您正在编译某种库,则不是很有用。
This is quite an old question, but I've hit the same issue, and none of the solutions here worked.
So I defined this function:
And then used it instead of memcpy. This has solved the inlining issue for me permanently. Not very useful if you are compiling some sort of library though.