gpt4 book ai didi

linux - 如何告诉编译器在每个 C 函数上填充特定数量的字节?

转载 作者:太空狗 更新时间:2023-10-29 11:42:08 25 4
gpt4 key购买 nike

我正在尝试练习一些实时检测,我看到有一个链接器选项 -call-nop=prefix-nop,但它有一些限制,因为它只适用于 GOT 函数(我不知道如何强制编译器生成 GOT 函数,也不确定出于性能原因这是否是个好主意。)此外,-call-nop=* 不能填充超过 1 个字节。

理想情况下,我希望看到一个编译器选项来填充任何特定数量的字节,并且编译器仍将执行所有正常的函数对齐。

一旦我有了这个填充区域,我就可以在运行时重用这些填充区域来存储一些值或重定向控制流。

附言我相信 Linux 内核使用类似的技巧来动态启用某些软件跟踪点。

最佳答案

-pg 适用于 profile-guided optimization .正确的选项是 -fpatchable-function-entry

  • -fpatchable-function-entry=N[,M]

    Generate N NOPs right at the beginning of each function, with the function entry point before the Mth NOP. If M is omitted, it defaults to 0 so the function entry points to the address just at the first NOP. The NOP instructions reserve extra space which can be used to patch in any desired instrumentation at run time, provided that the code segment is writable. The amount of space is controllable indirectly via the number of NOPs; the NOP instruction used corresponds to the instruction emitted by the internal GCC back-end interface gen_nop. This behavior is target-specific and may also depend on the architecture variant and/or other compilation options.

它将插入 N 个单字节 0x90 NOP 和 doesn't make use of multi-byte NOPs因此性能不如应有的好,但在这种情况下您可能不关心这一点,因此该选项应该可以正常工作

关于linux - 如何告诉编译器在每个 C 函数上填充特定数量的字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40189754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com