llvm / llvm/llvm-project

[Clang][ICE][x86_64] Inline assembly symbols

Open
#160,983 0 comments 0 reactions 0 assignees View on GitHub
inline-asm
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Code:

```c
#include
#include

unsigned long SYMBOL = 0x000000000000002A;

__attribute__((noinline, used))
unsigned long read_symbol(void) {
unsigned long result = 0;
__asm__ __volatile__ (
"movq %[symbol]@GOTPCREL(%%rip), %%rax\n\t"
"movq (%%rax), %[result]"
: [result] "=r" (result)
: [symbol] "m" (SYMBOL)
: "rax"
);
return result;
}

int main(void) {
unsigned long result = read_symbol();
printf("Value of symbol: 0x%08" PRIx64 "\n", result);
return 0;
}
```

Compiler automatically adds (%rip), error:

```log
:10:9: error: unexpected token in argument list
10 | "movq %[symbol]@GOTPCREL(%%rip), %%rax\n\t"
| ^
:1:19: note: instantiated into assembly here
1 | movq SYMBOL(%rip)@GOTPCREL(%rip), %rax
| ^
1 error generated.
```

It is also not entirely clear how to specify the symbol; in some cases, "m" is sufficient, while in other cases, ":" is [documented](https://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#index-_003a-in-constraint), but it does not work. This is a temporary solution:

```c
__attribute__((noinline, used))
unsigned long read_symbol(void) {
unsigned long result = 0;
__asm__ __volatile__ (
"movq SYMBOL@GOTPCREL(%%rip), %%rax\n\t"
"movq (%%rax), %[result]"
: [result] "=r" (result)
:
: "rax"
);
return result;
}
```

But this version is difficult to manage; when you change the slot name, you also need to change ASM. This problem exists only for x86 (64) for i686, i386 - everything is fine! The compiler automatically expands `%[symbol]` to `%[symbol](%rip)` on the x86 (64) platform, which is true for all cases:

```asm
"movq %[symbol](%%rip), %[result]\n\t"
"leaq %[symbol](%%rip), %%rax\n\t"
"movq %[symbol]@GOTPCREL(%%rip), %%rax\n\t"
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.