CompCert icon indicating copy to clipboard operation
CompCert copied to clipboard

feature request: asm block outside of a function

Open didickman opened this issue 11 years ago • 3 comments

gcc allows basic asm blocks outside of any function:

$ cat foo.c
asm("cli");
int main() { return 1; }
$ gcc foo.c

compcert, doesn't seem to support this:

$ ccomp -finline-asm foo.c
foo.c:1: syntax error.
Fatal error; compilation aborted.
1 error detected.

didickman avatar Dec 28 '14 07:12 didickman

In CompCert, "asm" is a statement, like "return" or "if", so naturally it can only occur within a function body. This is consistent with ISO C99 J.5.10 ("The most common implementation is a statement of the form..."). I don't understand the purpose of a "top-level" asm statement like in the foo.c example. GCC just dumps a "cli" instruction outside of any function, where it will never be executed...

xavierleroy avatar Dec 29 '14 12:12 xavierleroy

You're right, my example was not very good. Here's a better one:

#include <stdio.h>

__asm__( \
        ".section .gnu.warning." __STRING(gets)  \
        " ; .ascii \"" "gets() is unsafe." "\" ; .text");

int main(int argc, char **argv)
{
   char str[50];
   gets(str);
}

The above adds a warning anytime gets(3) is seen:

$ gcc foo.c
/tmp/ccVEAzyx.o: In function `main':
foo.c:(.text+0x26): warning: gets() is unsafe.

It can also be done outside of the compiler if needed:

$ ccomp -S foo.c
$ cat custom.s foo.s > foo.new.s
$ ccomp foo.new.s
foo.new.o(.text+0x14): In function `main':
: warning: gets() is unsafe

didickman avatar Dec 30 '14 01:12 didickman

Perhaps we should close this one? It’s been a few years and as I point out it can be done outside of compcert? Leave the final decision to you all.

didickman avatar Oct 01 '21 17:10 didickman