c++ - 如何强制 GCC 以线性方式转换 volatile 内联汇编语句?

标签 c++ gcc assembly binary x86

如何强制 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/

相关文章:

c++ - 为什么这个编译器错误? - 没有匹配函数调用 'std::basic_ofstream<char>::open(std::string&)'

assembly - 将 .data 部分中定义的整数移至寄存器

c - 是否将指针转换到 intptr_t,对其进行算术运算,然后转换回已定义的行为?

assembly - X86 切换到 32 位保护模式

c - 从汇编代码设计 C 函数

c++ - 力扣 : TwoSum Solution

c++ - 前向声明,不完整类型

c++ - Allegro C++ 如何刷新删除行?

c++ - 如何归还或护理项目 list 基于几个类别

python - 如何使用gcc在windows for linux上进行跨平台编译?