如何强制GCC以线性方式转换易失的内联汇编语句? [英] How to enforce GCC to translate volatile inline assembly statements in a linear fashion?

查看:69
本文介绍了如何强制GCC以线性方式转换易失的内联汇编语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制GCC以线性方式转换易失的内联汇编语句?

How to enforce GCC to translate volatile inline assembly statements in a linear fashion?

我知道使内联汇编语句既易失又依赖于内存将阻止GCC重新排序它们.

I know that making inline assembly statements both volatile and memory dependent will prevent GCC from reordering them.

但是,没有什么说GCC是否将这些汇编语句线性地放置在输出文件中?

However, nothing is said whether GCC will place these assembly statements linearly in the output file?

假设我有以下内联汇编语句(以伪代码):

Suppose I have the following inline assembly statements (in pseudo code):

[...]
volate&mem_dependent_inline_asm_statement_1
volate&mem_dependent_inline_asm_statement_2
[...]
volate&mem_dependent_inline_asm_statement_n
[...]

然后,我保证GCC会保留其顺序.但是我如何确定GCC不会输出:

Then I'm guaranteed that GCC will preserve their ordering. But how can I be certain that GCC will not output:

[...]
jmp label_1
label_2:
[...]
asm_statement_n
[...]
label_1:
asm_statement_1
asm_statement_2
[...]
jmp label_2

我知道我的示例有点晦涩难懂,但是我在运行时进行防篡改的应用取决于将内联汇编语句块转换为保留顺序的相应汇编语句块.

I know my example is kinda obscure, but my application of tamper-proofing at runtime depends on a block of inline assembly statements being translated to a corresponding block of assembly statements with ordering preserved.

换句话说,我想要这样的输出:

In other words, I want output like this:

[...]
asm_statement_1
asm_statement_2
[...]
asm_statement_n
[...]

有什么想法吗?

推荐答案

不幸的是,这不可能.

gcc文档:

不要期望一系列的asm语句保持完美 编译后连续,即使您使用的是volatile 限定词.如果某些指令需要在指令中保持连续 输出,然后将它们放在单个多指令asm语句中.

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.

这篇关于如何强制GCC以线性方式转换易失的内联汇编语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆