<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/124091>124091</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Linker OVERLAY feature not working properly / Loading dependent on virtual not physical address
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
luposmi
</td>
</tr>
</table>
<pre>
# My issue
Before I explain the issue, I should start with what the problem is I try to solve:
I am working at a research institute where we are currently working on a processor where only one "management core" has access to the main memory and every other core, the processing cores, only have access to their cache "scratchpad". The loading and unloading of scratchpads and their dependencies are done at compile time and realized by the management core using a DMA at runtime.
For this, I need basically the following:
1. Have the .text section at virtual address 0x0, physical address 0x0
2. Have each other text section (e.g., *vector_program.o(.text)) at virtual address 0x0 but physical address 0x10000000 (stacking those text sections there). Those two regions are mapped to the names rom and ram.
The part with the dependencies resolution is all done at comp time before the linker script is called into action. The only thing required is that the other text sections are present in ram and that a symbol exist for their start and end. When they are needed to be copied into the scratchpad of that specific core is also managed previously.
# How it's in GCC
Disclaimer: I did this testing for x64. However, the stuff in LLVM was for riscv32.
Also, those examples do not all include the label generation, as they are trivial and not the problem.
## Option 1
For the requirements above, I have written a very basic linker script for GCC which looked like this:
```ls
.text0 0x0: AT 0x0 { *core1.o(.text)}
.text1 0x0: AT 0x10000000 { *core2.o(.text)}
```
Which worked already completely fine without any errors and did the right thing.
## Option 2
```ls
OVERLAY 0x0 : AT (0x10000000)
{
.text0{ *core1.o(.text) }
.text1{ *core2.o(.text) }
}
```
This does not completely do the right thing, as one text section needs to reside in rom, while all others in ram. However, this also works (you can just add another text section above at 0x0 no problems and then do all other sections in an OVERLAY)
# What I tried in LLVM
compile command:
```bash
clang-19 -target riscv32 -march=rv32imv -mabi=ilp32 -static -mcmodel=medany -fuse-ld=ld -Wno-unused-command-line-argument -g3 -fvisibility=hidden -nostdlib -nostartfiles -Wall -T lld_link.ld ./build/*.o -o out.elf```
```
## OVERLAY
### Variant one
```ls
OVERLAY 0x0 : AT (0x00200000)
{
.text0 { *vector_program.o(.text) }
.text1 {
*(.vectors)
*management_core.o(.text) }
}
```
Does not work, compiler output:
ld.lld: error: no memory region specified for section '.text0'
ld.lld: error: no memory region specified for section '.text1'
clang-19: error: ld.lld command failed with exit code 1 (use -v to see invocation)
error: Recipe `link` failed on line 14 with exit code 1
### Variant two
if we give it a region:
```ls
OVERLAY 0x0 : AT (0x00200000)
{
.text0 { *vector_program.o(.text) }
.text1 {
*(.vectors)
*management_core.o(.text) }
} > rom
.rodata : {
. = ALIGN(4);
*(.rodata);
} > ram
```
For any reason errors in the next line
ld.lld: error: lld_link.ld:70: : expected, but got .rodata
.rodata : {
^
## Option 3
```ls
.text0 0x0 : AT (0x0) { *management_core.o(.text)} > rom
.text1 0x0 (OVERLAY) : AT (0x10000000){ *vector_program.o(.text) } >ram
```
actually compiles, but does not add the required load commands:
ld.lld: warning: ignoring memory region assignment for non-allocatable section '.text_vector'
IDK if this behavior is wanted. But what I can definitely say is that it is very allocatable, as only the virtual address overlaps, not the physical. If i add OVERLAY to both, the other section is also not loaded.
## RAW
If we just use option 1 of gcc, aka
```ls
.text0 0x0 : AT (0x0) { *management_core.o(.text)} > rom
.text1 0x0 : AT (0x10000000){ *vector_program.o(.text) } >ram
```
we just get:
ld.lld: error: section .text_management virtual address range overlaps with .text_vector
.text_management range is [0x0, 0x13EF]
.text_vector range is [0x0, 0x3CF]
# Workarounds
There are a couple of workarounds. However, they are all anything from annoying to very annoying.
1. Setting every Symbol which gets an Offset through the virtual address manually
2. Editing the resulting elf file per hand to add the required load commands.
PS:
@MaskRay I have seen you work on this issue in the past and mentioning you would like to see reasons for this, so here is one.
Also, the Overlay option 3 has already an open bug report #41351, but nothing moved in the last six years.
We need to use LLVM as the earlier stages of the project require LLVM specific tools. Compiling with LLVM and then linking with GCC has shown to, unfortunately, throw random errors.
</pre>
<img width="1" height="1" alt=""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