<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/65159>65159</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
in-file position computation goes wrong w.r.t computeFileOffset and end up with huge value .
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
UmeshKalappa0
</td>
</tr>
</table>
<pre>
Hi ,
the testcase has the issue https://godbolt.org/z/j5baEPWfc like
```
- $ cat test.ld
- ENTRY(_start)
- SECTIONS
- {
- .text (VMA_START) : AT(VMA_START + LMA_START)
- {
- *(.text)
- }
- . += LMA_START;
- _TEXTCORE_LOAD_ADDR = .;
- .text.cpu :
- AT(_TEXTCORE_LOAD_ADDR)
- {
- *(.text_cpu)
- }
- . = . -LMA_START;
- .sbss :
- {
- *(.sbss)
- }
- PROVIDE (wrs_kernel_bss_start = ADDR(.sbss)) ;
- }
```
```
$llvm-mc -filetype=obj -triple=x86_64 test.s -o test.o
$ld.lld -m elf_x86_64 --no-relax -X -N --eh-frame-hdr -e _start --defsym ENTRY_SYMBOL=_start --defsym VMA_START=0xffffffff90008000 --defsym LMA_START=0x0 --defsym LMA_START=0x70000000 -Ttext 00008000 -defsym DATA_ALIGN=1 test.o -T ./test.ld
**ld.lld: error: output file too large: 18446744071830504616 bytes**
section sizes:
.text 13
.text.cpu 0
.sbss 0
.data 4
.comment 19
.symtab 192
.shstrtab 64
.strtab 77
```
and we investigate the issue and found that the below code at
```
ELF/Writer.cpp
@@ -2519,8 +2519,10 @@ static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
// If two sections share the same PT_LOAD the file offset is calculated
// using this formula: Off2 = Off1 + (VA2 - VA1).
OutputSection *first = os->ptLoad->firstSec;
return first->offset + os->addr - first->addr;
and
>>this formula: Off2 = Off1 + (VA2 - VA1). can end up with huge value like stated in the above error .
```
So we recommend the changes like
```
diff --git a/ELF/Writer.cpp b/ELF/Writer.cpp
index 6fbee17..d76184d 100644
--- a/ELF/Writer.cpp
+++ b/ELF/Writer.cpp
@@ -2519,8 +2519,10 @@ static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
// If two sections share the same PT_LOAD the file offset is calculated
// using this formula: Off2 = Off1 + (VA2 - VA1).
- OutputSection *first = os->ptLoad->firstSec;
- return first->offset + os->addr - first->addr;
+ OutputSection *first = os->ptLoad->firstSec;
+ if (os->addr > first->addr)
+ return first->offset + os->addr - first->addr;
+ return off;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV0tv474R_zT0ZUCBomTJOvigxHYbNP94kbj7OBmUNLK4K4uCSMXJfvqClPyIm6QotscahsPHzG9mOPMbMkJruWsQ52R6Q6aLiehNpbr5P_eoq3-IWrStYJNMFa_zv0sg_JawBWHp8GsqBIPa5EIjVEKDXZBa9wiVMa0mQUr4ivDVThWZqo2nuh3hq9-Er35OM7H88q3MoZa_EEbUiI3fCyMUCA8hF8aZ8uoCjuvLh83jD8JnW21EZwhPjhtPy9vN3frh6QQQ3xyHAJ7BFwOEz77-lW6fNunjhvAESJBCurlcBcJv4P5C5gzxFhCA8JTwmQO-lltcGLaIJFhcgAYnmO1m-X1zu35cbu_X6WKbLhaPYGW9C5nRdy9ve-vvcdm5_Y7-G1c-9nibt_2V6FunrRdA33MawNOZ1pfOfGTJyn1s5cvj-uvdYmmzcuj09hd2DdbbTOshtc6HIaQzkkvazTnFi8uiua6kqykP6_p5T_c50FLWaF5bJMFCZT8BqOlkW9vpyyzaRuFQdRqoGkbqDFF4dV0A3QPW5XaUprRRtMNavAD9DvQBKMWKlp3YI62Kzh0JRRjjorTAUr_uh1LePv3462Z9T4LF9fa5VIMFeynHT8IYmzHGznL3b-Q-3IjZ8LG-bBwd2AlqVFikm3Sb3t_97YEEC38MHegGPMJXIxHfHLhNczocieUSdp3q7ED1pu0N2GMGoxTUotuh3fBnYRjFYchifxawKQsjP4Ls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