https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118389
Bug ID: 118389
Summary: Compiler Bugs (Emit Incorrect Intel Binaries)
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: witbring at gmail dot com
Target Milestone: ---
I am reporting a GCC compiler bug discovered during research.
The test was conducted using the latest version (GCC 14.2)
This issue occurs when using the -masm=intel flag.
1. Summary
- Issue 1: The bus occurs when a variable name matches some directive name, the
compiled code accesses an incorrect memory address.
- Issue 2: The bug occurs when a function name matches register name, the
compiled code converts the call instruction into an indirect call.
2. Example Code
Below is an example that reproduces the issues.
In the code, the variable far is assigned a value 4, and the function rax is
called.
```
#include <stdio.h>
void foo() { printf("hello world\n"); }
void rax() __attribute__((alias("foo")));
int far;
int main()
{
far = 4;
rax();
return 0;
}
```
3. Compilation Command
```
gcc -masm=intel example.c
```
4. Compiled Binary Output
- Issue 1
At address 0x1168, the mov instruction accesses the memory address 0x11078.
However, the actual address of the variable far is 0x4014.
Based on our findings, there are 12 directive-like names that cause similar
issues.
We also reported relevant issue to binutils Bugzilla.
For further details, refer to issue#32531
(https://sourceware.org/bugzilla/show_bug.cgi?id=32531)
- Issue 2
At address 0x1177, the call instruction has been converted into an indirect
call.
```
0000000000001149 <foo>:
...
0000000000001160 <main>:
1160: f3 0f 1e fa endbr64
1164: 55 push rbp
1165: 48 89 e5 mov rbp,rsp
1168: c7 05 06 ff 00 00 04 mov DWORD PTR [rip+0xff06],0x4
# 11078 <_end+0xd060>
116f: 00 00 00
1172: b8 00 00 00 00 mov eax,0x0
1177: ff d0 call rax
1179: b8 00 00 00 00 mov eax,0x0
117e: 5d pop rbp
117f: c3 ret
```
5. Reproduction
You can reproduce the result through Godbolt Compiler Explorer:
https://godbolt.org/z/oacsadc17