Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc: How to avoid "used but never defined" warning for function defined in assembly

For Reasons, I'm trying to use top-level assembly in GCC to define some static functions. However, since GCC doesn't "see" the body of those functions, it warns me that they are "used but never defined. A simple source code example could look like this:

/* I'm going to patch the jump offset manually. */
asm(".pushsection .slen,\"awx\",@progbits;"
    ".type test1, @function;"
    "test1: jmp 0;"
    ".popsection;");
/* Give GCC a C prototype: */
static void test(void);

int main(int argc, char **argv)
{
    /* ... */
    test();
    /* ... */
}

And then,

$ gcc -c -o test.o test.c
test.c:74:13: warning: ‘test’ used but never defined [enabled by default]
 static void test(void);
             ^

How to avoid this?

like image 480
Dolda2000 Avatar asked Dec 25 '22 15:12

Dolda2000


2 Answers

gcc is being smart here because you've marked the function as static, meaning that it's expected be defined in this translation unit.

The first thing I'd do is get rid of the static specifier. This will permit (but not require) you to define it in a different translation unit so gcc will not be able to complain at the point of compilation.

That may introduce other problems, we'll have to see.

like image 122
paxdiablo Avatar answered Apr 28 '23 18:04

paxdiablo


You could use the symbol renaming pragma

 asm(".pushsection .slen,\"awx\",@progbits;"
      ".type test1_in_asm, @function;"
      "test1_in_asm: jmp 0;"
      ".popsection;");
 static void test(void);
 #pragma redefine_extname test test1_in_asm

or (using the same asm chunk above) the asm labels:

 static void test(void) asm("test1_in_asm");

or perhaps the diagnostic pragmas to selectively avoid the warning

like image 34
Basile Starynkevitch Avatar answered Apr 28 '23 19:04

Basile Starynkevitch