0

I want to add a custom label in my compiled C code which I will be using later. For example, the C code might look like:

#include <stdio.h>                                                                                                                                             
#include <stdbool.h>

int main()
{
        volatile bool x = false;
        volatile int y = 0;
        asm(".MyLabel");
        if(x)
        {
                y = 1;
        }   
        return 0;
}

I want to add a label before the if(x) statement. As shown above, I tried to do this by inserting a label via inline assembly. I am compile my code using:

$ gcc -O3 -g -march=native -pedantic

However, when I look at the assembly generated using objdump, I get:

Disassembly of section .text:

0000000000001020 <main>:
    1020:   c6 44 24 fb 00          movb   $0x0,-0x5(%rsp)
    1025:   31 c0                   xor    %eax,%eax
    1027:   c7 44 24 fc 00 00 00    movl   $0x0,-0x4(%rsp)
    102e:   00 
    102f:   c3                      ret

0000000000001030 <_start>:
    1030:   f3 0f 1e fa             endbr64
    1034:   31 ed                   xor    %ebp,%ebp
    1036:   49 89 d1                mov    %rdx,%r9
    1039:   5e                      pop    %rsi
    103a:   48 89 e2                mov    %rsp,%rdx
    103d:   48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    1041:   50                      push   %rax
    1042:   54                      push   %rsp
    ...

As you can see, in the main section of the assembly, there is no label called MyLabel. How can I force gcc to insert the label before the if condition?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Setu
  • 180
  • 8
  • That's not a label, there's no `:` following it. With optimization disabled, `gcc foo.c` gives an error from GAS when I try compiling for x86-64 GNU/Linux: ```Error: unknown pseudo-op: `.mylabel'``` (because it starts with a `.` and doesn't end with a `:`, it's assumed to be a GAS directive.) – Peter Cordes Feb 08 '23 at 08:28
  • 1
    Near duplicate: [How to tell gcc to keep my unused labels?](https://stackoverflow.com/q/70086938) or [Define a unique and global assembly label/symbol inside C functions](https://stackoverflow.com/q/52730007) – Peter Cordes Feb 08 '23 at 08:31
  • 2
    What is supposed to happen with your label if the function in question is inlined? You can only have one of every label. – fuz Feb 08 '23 at 08:41

0 Answers0