What does "#APP" in the assembly file generated by compiler mean?
Asked Answered
A

3

5

I wrote the following code:

// 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));
}

And then compiled it using clang-7 -Oz -pipe -flto -c a.c and used llc-7 -filetype=asm a.o to turn it into an human-readable assembly file a.o.s:

    .text
    .file   "a.c"
    .globl  _start                  # -- Begin function _start
    .type   _start,@function
_start:                                 # @_start
    .cfi_startproc
# %bb.0:
    pushq   $60
    .cfi_adjust_cfa_offset 8
    popq    %rax
    .cfi_adjust_cfa_offset -8
    xorl    %edi, %edi
    #APP
    syscall
    #NO_APP
    retq
.Lfunc_end0:
    .size   _start, .Lfunc_end0-_start
    .cfi_endproc
                                        # -- End function

    .ident  "clang version 7.0.1-svn348686-1~exp1~20181211133235.57 (branches/release_70)"
    .section    ".note.GNU-stack","",@progbits

In the assembly above, the directive #APP appears before syscall, which is the assembly I wrote and the directive #NO_APP appears right after it.

I know it must have something to do with the use of asm, like to prevent it from being optimized out, but I can't find any documentation of it after googling.

Thanks advanced.

Antibaryon answered 28/12, 2018 at 13:47 Comment(0)
S
7

Historically at least, the compiler told as it could use a fast-parsing mode for parsing pure compiler output, switching back for full parsing for blocks from inline-asm. There should be a #APP somewhere early in the file that enters this mode. The #NO_APP switches out of this mode.

I'm not sure as does anything with that hint anymore, so it may not even look for #NO_APP at all, and just treat it like any other comment.

In practice, it delimits the block of text that came from the inline asm template, vs. compiler generated asm. It doesn't do anything beyond that, and if you're hand-editing the asm for some reason, it's totally find to remove it.

Slavophile answered 28/12, 2018 at 14:4 Comment(2)
Are you sure the fast parser is gone? I believe it still exists, but I'm not sure.Rifle
@fuz: I'm not sure, but I think I read that a while ago. I just tried assembling a hand-written file that started with #APP, and comments and a .equ definition worked fine inside it. I didn't try a .macro.Slavophile
Z
2

Those macros disable (#NO_APP) or enable (#APP) the preprocessor, allowing for faster parsing by disabling the preprocessor when it is not needed.

From the gas manual,

If the first line of an input file is '#NO_APP' or if you use the '-f' option, whitespace and comments are not removed from the input file. Within an input file, you can ask for whitespace and comment removal in specific portions of the by putting a line that says '#APP' before the text that may contain whitespace or comments, and putting a line that says '#NO_APP' after this text. This feature is mainly intend to support 'asm' statements in compilers whose output is otherwise free of comments and whitespace.

You can find this warning in the section about the -f option:

Warning: if you use '-f' when the files actually need to be preprocessed (if they contain comments, for example), 'as' does not work correctly.

Zymotic answered 11/8, 2022 at 8:48 Comment(0)
F
0

It removes the whitespace and the comments between it and #NO_APP.

In this case it was placed here just in case as you have the comment in your asm()

Forwardlooking answered 28/12, 2018 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.