<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62423>62423</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
lld incorrectly emits region overflow errors when linker relaxation would prevent an overflow
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
korran
</td>
</tr>
</table>
<pre>
I've written an LLD test-case that fails at top-of-tree:
```
# REQUIRES: riscv
# RUN: rm -rf %t && split-file %s %t && cd %t
## Check that relaxation prevents region overflow
# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+c,+relax a.s -o a.32c.o
# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+c,+relax b.s -o b.32c.o
# RUN: ld.lld -T lds a.32c.o b.32c.o -o 32c
# RUN: llvm-objdump --section-headers 32c | FileCheck %s --check-prefixes=RELAX_SECTIONS
# RELAX_SECTIONS: 1 .text 0000000a 00000000 TEXT
## Check that we still overflow with relaxation disabled
# RUN: not ld.lld -T lds a.32c.o b.32c.o --no-relax -o /dev/null 2>&1 | FileCheck --check-prefix=ERR0 %s
# ERR0: ld.lld: error: section '.text' will not fit in region 'ROM'
#--- a.s
.global _start
_start:
# These calls can be relaxed to be much smaller, enough to fit within the
# tiny ROM region
call bar
call bar
call bar
call bar
#--- b.s
.global bar
bar:
ret
#--- lds
MEMORY {
ROM (rx) : ORIGIN = 0, LENGTH = 12
}
SECTIONS {
.text 0x00000 : { *(.text) } > ROM
}
```
Because of this bug, the call to ld.ldd on line 9 will fail with this error:
```
ld.lld: error: section '.text' will not fit in region 'ROM': overflowed by 22 bytes
```
If I increase the length of the ROM region, it succeeds, **and the .text section in the output file is only 10 bytes**; less than the original region size.
```
$ tail lds
MEMORY {
ROM (rx) : ORIGIN = 0, LENGTH = 40
}
$ llvm-objdump --section-headers 32c
Sections:
Idx Name Size VMA Type
0 00000000 00000000
1 .text 0000000a 00000000 TEXT
<snip>
```
I believe the first call to LinkerScript::assignAddresses() is the one that triggers the error, and it occurs before any relaxation passes have been done. Later passes that happen after relaxation don't trigger any errors, but it's too late.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vl1zIisT_jV40zXWyPh54YXxY1-r8lGvcU_tudpimNZhgzAFjMb99acAR6N7kuzFnqmUoWlonu5-moZZK7YKcUx6d6Q3a7HaldqMX7QxTLVyXRzHS0IHe4SDEc6hAqbg_n4GDq1LOLMIrmQONkxIC8yB01WiN4kziCSbkHRG0ua3n57-okgzWM3__3W5mj-TbAJGWL6HqIM3S74-Bu0OErMBQnsOCO0T2gdbSeGSjZDop-2Vjhcn8cactzgtkb9E1AYle2VOaAWVwT0qZ8Hg1st6j2Yj9eFdRFLud8mOQ0DgjhWSbKbzH5A4IyrppeBRRiHZMecMyWaE3nFCp4TehYOBtS0kGlg7o7yt_5Mj8nhEfjrik-9XBEVbygKSNcjCnoE21hINGeXv4Nb5j6LeVZAkFrkPcVIiK9DYuGcwhYWQGJMR8pck3AtJZXAjXtGSbLaa30--fX-eT9fLp8fnTxBfrfUwADrQdvjqzqvT-LFmkMJ6_m39GUsOCNYJKS-cOAhXviVPISzLJRbvkkVp91ksE6WTmLNEA6GLAveELlQtJVCSzQntd26idh0wks3mq1UaY3kVGT99SaYfoTHa-MEpNUDoIESK0AEcvKse8EY4EKopCEIHq6cHv-B3v9tgJEkSKB8n2lupcybhu3XMNJUaBY_stBm8A-sSLQJnUlrgTEGOMfhYgNNe2tW8BLtjUqIhdAqodL0tvdL74LMlFLgSr6w6oY6weno4O3jS-XMgZ6aZuJU_n7jyOP_F48vKnJm3vhr89cbyJjxf4sTD_OFp9TeQwd15k_eA0KF5JXQE3trTavll-Qgkm0Hqg3E_f_yy_l-QO7QxPJidRufiemszVk36GmskQPRaOiF0eOLJCIKNbB4B3Fi9vevD7x1yVlsEvQFXCgt5vfX4fF5CBJ0OFC0K0AqkUAijSEbfXGLN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