<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/123402>123402</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
assembler .space gives "error: invalid number of bytes" in LLVM 19 but works in 18
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kernigh
</td>
</tr>
</table>
<pre>
I expect LLVM to assemble this x86-64 test case. It fails in today's git f719771,
```
$ clang --target=amd64-openbsd -c exam.s
exam.s:38:9: error: invalid number of bytes
.space (p_nop - p_1st) - (. - q_1st), 0xcc
^
```
It also fails with the OpenBSD package of llvm-19.1.7p0, but succeeds with the OpenBSD package of llvm-18.1.8p3.
```s
.section .p
p_1st:
0: pause
lfence
jmp 0b
.section .q
q_1st:
addl 11,%eax
addl 22,%eax
q_cli:
cli
0: pause
lfence
jmp 0b
.section .p
.space (q_cli - q_1st) - (. - p_1st), 0xcc
cli
.section .q
q_sti:
sti
.section .p
.space (q_sti - q_1st) - (. - p_1st), 0xcc
sti
addl 33,%eax
addl 44,%eax
p_nop:
nop
.section .q
0: pause
lfence
jmp 0b
.space (p_nop - p_1st) - (. - q_1st), 0xcc
nop
```
This test case has 2 sections (.p and .q). Each .space should evaluate to `.space 7, 0xcc`. (Then cli in .p and cli in .q will have the same offset, and same for sti and nop.) LLVM 18 emits the correct .o file, for which `llvm-objdump -d -j.p -j.q exam.o` shows 3 groups of 7 bytes of 0xcc = int3.
I need a `jmp 0b` to reproduce the error in LLVM 19 or git. If I change the 1st `jmp 0b` (on line 5) to `incl %eax` (both instructions are 2 bytes), then the error goes away, and each .space inserts the correct 7 bytes.
The wrong "error: invalid number of bytes" comes from a check `if (Size < 0)` in llvm/lib/MC/MCAssembler.cpp. I modified LLVM 19 to print the Size in each check. For this test case, I got
- exam.s:19 if 7 < 0
- exam.s:38 if -15 < 0, error
- exam.s:27 if 7 < 0
- exam.s:19 if 7 < 0
- exam.s:27 if 7 < 0
- exam.s:38 if 7 < 0
It visited the check `if (Size < 0)` twice for each .space line. The 1st visit to line 38 got the wrong size -15 and gave the error, though the 2nd visit got the correct size 7. The 1st visits with line 38 before 27 are in the wrong order; you can't calculate the size at line 38 unless you know the size at line 27. After I changed the 1st `jmp 0b` to `incl %eax`, it visited 27 before 38.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVl1vozwW_jXOzVGQMSTARS4y7Vsp0vtqL2a0tyNjTsAtsaltmnZ__eoYSNJOdz60ESLGH4-f53zZ0nvdGsQd23xhm_uVHENn3e4JndFtt6pt87Y7AL4OqAL8_fe__4FgQXqPp7pHCJ328Fpu19scAvoASnpM4BDgKHXvQRsItpFvTBQeWh3gWKRVUaRM3DG-p2fL54fvmchB9dK0sF4H6VoMLLuXp2abr-2ApvYNrBXgqzwlHgCA8f30wbJ9VrJsX7FsD-icddTQ5kX2ugEznmp0YI9QvwX0jO9h_iV-kAqBiXL4buwAaxi-pz4wUcGaehNYw_Pcw8Qd8FelbpYvP7b566MSvj8EkL23sx3OOnQQOoR_DWi-fL2HQaon2SKx6vuX0zqtkjQpBk7b1GMAPyqF2PzOyjJJk3LIkg8G9fG7SjyqoK2BZGB8P-nLaCKPf9UgR4_T1P6IRs3tx9PAeMXrGfQW55nx_fMVh_FKNk3PeJVGt4oNytd3_UK8798_f1e9vqym9v9BZ1g6yJeMV0yUEf_Gd1dvDj9688Lgc50-XJlS-zcJ-PAnBBbgxWJZ9rkl8_y2PwbthRy1PxfxB6Z9p-KPkuLK4H0efKMKcSkN0EkPAmZ6PuINIE1DTEWVwF9SdUte-s6OfQP4IvtRBqTCw7Z8Hiwue295QjjfOjRAftfkk4i5fD3DWfc9dPIFYyp5eaL8OXoMhEJTY9fROiDHUYexQ0KaY8lLS8CTDj6uVtY5KoaJhaPukRBo4bnTqiOCMStt_diMpwHWDawfk4Fez1PpsmzLSdrZQwats-PgKZeLqTpRk1QBy-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