Jia*_* Xu 3 c assembly inline-assembly noreturn
我写了下面的代码来调用syscall exit而不用glibc链接:
// a.c
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
_Noreturn void _start()
{
register int syscall_num asm ("rax") = __NR_exit;
register int exit_code asm ("rdi") = 0;
// The actual syscall to exit
asm volatile ("syscall"
: /* no output Operands */
: "r" (syscall_num), "r" (exit_code));
}
Run Code Online (Sandbox Code Playgroud)
的Makefile:
.PHONY: clean
a.out: a.o
$(CC) -nostartfiles -nostdlib -Wl,--strip-all a.o
a.o: a.c
$(CC) -Oz -c a.c
clean:
rm a.o a.out
Run Code Online (Sandbox Code Playgroud)
我用make它CC=clang-7并且它工作得很好,除了当我检查生成的组件时objdump -d a.out:
a.out: file format elf64-x86-64
Disassembly of section .text:
0000000000201000 <.text>:
201000: 6a 3c pushq $0x3c
201002: 58 pop %rax
201003: 31 ff xor %edi,%edi
201005: 0f 05 syscall
201007: c3 retq
Run Code Online (Sandbox Code Playgroud)
有一个无用的retq跟随syscall.我想知道,有没有办法删除它而不需要在汇编中编写整个函数?
| 归档时间: |
|
| 查看次数: |
115 次 |
| 最近记录: |