如何告诉编译器在_Noreturn函数中内联asm后不生成"retq"?

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)

我用makeCC=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.我想知道,有没有办法删除它而不需要在汇编中编写整个函数?

prl*_*prl 5

在不返回的系统调用之后添加:

    __builtin_unreachable();
Run Code Online (Sandbox Code Playgroud)