<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/88516>88516</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[x86] Assembler syntax check bugs
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
witbring
</td>
</tr>
</table>
<pre>
Clang assembler does not thoroughly check the syntax of the assembly code.
It proceeds with assembling even if there are syntax errors present, which should ideally prompt warning messages or errors to help programmers identify mistakes in their code.
# Confusing memory operand as label.
First, we observed that Clang (v16.0.0) often misinterprets a memory operand as a label.
For instance, when you write and assemble code like this:
```
$ cat buggy1.s
.intel_syntax noprefix
ja BYTE PTR [1]
```
Clang (x86/x64) generates the following binary file.
```
$ bin/clang -c buggy1.s -o buggy1.o
$ objdump -d -M intel buggy1.o
0000000000000000 <.text>:
0: 0f 87 00 00 00 00 ja 0x6
$ readelf -r buggy1.o
Relocation section '.rela.text' at offset 0xc8 contains 21 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000002 000000000002 R_X86_64_PC32 -3
```
Clang translates memory operands into labels (or absolute addresses).
We observed such cases when Clang (x86/x64) assembles 'ja', 'jae', 'jb', 'jbe', 'je', 'jecxz', 'jge', 'jl', 'jle', 'jne', 'jno', 'jnp', 'jns', 'jo', 'jp', ' jrcxz', 'js', 'jmp', 'loop', 'loope', 'loopne' instructions.
# Ignore pointer directives.
Second, we often observed that Clang ignore pointer directives
For instance, when you write and assemble code like this:
```
.intel_syntax noprefix
call BYTE PTR [RAX]
call WORD PTR [RAX]
call DWORD PTR [RAX]
call QWORD PTR [RAX]
call XMMWORD PTR [RAX]
```
Clang ignores the pointer directives and emits the following binary code.
```
$ bin/clang -c buggy2.s -o buggy2.o
$ objdump -d -M intel buggy2.o
0000000000000000 <.text>:
0: ff 18 call FWORD PTR [rax]
2: ff 18 call FWORD PTR [rax]
4: ff 18 call FWORD PTR [rax]
6: ff 10 call QWORD PTR [rax]
8: ff 18 call FWORD PTR [rax]
```
We observed such cases from
- Clang(x86): 'aesdecwide128kl', 'aesdecwide256kl', 'aesencwide128kl', 'aesencwide256kl', 'call', 'clrssbsy', 'fldenv', 'fnsave', 'frstor', 'fxsave', 'fnstenv', 'fxrstor', 'rstorssp', 'xrstor', 'xrstors', 'xsave', 'xsavec', 'xsaveopt', 'xsaves'
- Clang(x64): 'aesencwide128kl', 'aesencwide256kl', 'aesdecwide128kl', 'aesdecwide256kl', 'call', 'fldenv', 'fnsave', 'frstor', 'fxsave', 'fnstenv', 'fxrstor', 'fxsave64', 'fxrstor64', 'ldtilecfg', 'lgdt', 'lidt', 'sgdt', 'sttilecfg', 'xbegin'
In my opinion, when assembling the example code, Clang should output warning or error messages.
Thanks.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0V0tv27wS_TXyZmBDpvzSwos8aiCL3rZpcNu7CihxJDGhSIFDxfb99R9E-UErSdvgQwXD1uE8ODwkD01OJEuNuI7m19H8dsRbVxm73kqXWanLUWbEfn2juC6BE2GdKbQgDBJo46DzNW1ZqT3kFebP4CoE2mvHd2AKjw5Re8iNwAlE8W0UX905aKzJEQXBVrrq6CV1CfiCGqQPtgjcnhKitcYSNBYJtYvYDWwrmVdAlWmVACmQK7XvEteNgy23uktXIxEvkcDYYwZnoELVdJ6l5XWNlrpo7WSxh1qS489IIHVXgrR94X3dh2-WwI3RRUt9B7WxezANWq4FcALFM1QXERtpqa8YwWSE9gUFuIo76KmN2OplupjEkzhiKZjCoe4KkdqhbSw6Av5GP_ytnowFqclxnWNPEWrYmxa2VjqEPrKfRz8wUPIZwVWSouQ4vEV8-BxGO4OcO8jastxPJ9S3Trra1ONharRpLBZy19sAAJ44XP_v4RN8fbiHaH49jea3b2Y_LIgTDbvVImKb3WLWEVGiRssdkl9JhVHKbDvKM6m53UMh1WliXtecSR2xTe4Tj_NT-TA2x3dzdjbZk2jrBsYCxp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