如何强制 GCC 以线性方式转换 volatile 内联汇编语句?
我知道使内联汇编语句既 volatile 又依赖于内存将阻止 GCC 对它们重新排序。
但是,没有说GCC是否会将这些汇编语句线性地放置在输出文件中?
假设我有以下内联汇编语句(伪代码):
[...]
volate&mem_dependent_inline_asm_statement_1
volate&mem_dependent_inline_asm_statement_2
[...]
volate&mem_dependent_inline_asm_statement_n
[...]
然后我保证 GCC 将保留他们的顺序。但我如何确定 GCC 不会输出:
[...]
jmp label_1
label_2:
[...]
asm_statement_n
[...]
label_1:
asm_statement_1
asm_statement_2
[...]
jmp label_2
我知道我的示例有点晦涩,但我在运行时的防篡改应用取决于将内联汇编语句 block 转换为相应的汇编语句 block ,并保留顺序。
换句话说,我想要这样的输出:
[...]
asm_statement_1
asm_statement_2
[...]
asm_statement_n
[...]
有什么想法吗?
最佳答案
不幸的是,这是不可能的。
来自gcc docs :
Do not expect a sequence of asm statements to remain perfectly consecutive after compilation, even when you are using the volatile qualifier. If certain instructions need to remain consecutive in the output, put them in a single multi-instruction asm statement.
关于c++ - 如何强制 GCC 以线性方式转换 volatile 内联汇编语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44089504/