我有一小段代码,其中有一些内联程序集可以在 O0 中正确打印 argv[0],但不会在 O2 中打印任何内容(使用 Clang 时。另一方面,GCC 打印存储在 envp[0] 中的字符串打印 argv[0] 时)。这个问题也仅限于 argv(其他两个函数参数可以在启用或不启用优化的情况下按预期使用)。我用 GCC 和 Clang 对此进行了测试,两个编译器都有这个问题。
这是代码:
void exit(unsigned long long status) {
asm volatile("movq $60, %%rax;" //system call 60 is exit
"movq %0, %%rdi;" //return code 0
"syscall"
: //no outputs
:"r"(status)
:"rax", "rdi");
}
int open(const char *pathname, unsigned long long flags) {
asm volatile("movq $2, %%rax;" //system call 2 is open
"movq %0, %%rdi;"
"movq %1, %%rsi;"
"syscall"
: //no outputs
:"r"(pathname), "r"(flags)
:"rax", "rdi", …Run Code Online (Sandbox Code Playgroud)