LLD does not report forward reference errors in linker script
Unlike GNU LD, LLD does not report forward reference error. This can result in unexpected output image layout.
Reproducible example:
#!/usr/bin/env bash
cat >1.c <<\EOF
int foo() { return 1; }
int bar() { return 3; }
EOF
cat >script.t <<\EOF
SECTIONS {
FOO ALIGN(ADDR(BAR), 0x1000) : {
*(*foo*)
}
BAR : {
*(*bar*)
}
}
EOF
clang-15 -o 1.o 1.c -c -ffunction-sections
ld.bfd -o 1.bfd.elf 1.o -T script.t # reports the error, "ld.bfd:script.t:2: non constant or forward reference address expression for section FOO"
ld.lld -o 1.lld.elf 1.o -T script.t # Links fine; no error or warning reported related to forward reference
If the decision to not report forward references in linker script diagnostics is intentional, then can anyone please tell why is it so when it can lead to unexpected final layout?
The ALIGN syntax is described by https://sourceware.org/binutils/docs/ld/Output-Section-Description.html
lld uses an fixed point iteration method and ensures that the value ALIGN(ADDR(BAR), 0x1000) converges (or report an error if the maximum of passes exceeds), so there should not be unexpected final layout. I think it is still wise to avoid alignment expressions.
Hi @MaskRay,
Thank you for your response.
I believe the issue is in the evaluation of ADDR(BAR) instead of ALIGN. The evaluation of ADDR(BAR) results in forward reference error in GNU, whereas in LLD ADDR(BAR) evaluates to 0, thus the overall expression becomes ALIGN(0, 0x1000).
so there should not be unexpected final layout.
Please allow me to illustrate why I believe this can lead to unexpected final layout.
In the example provided in the issue description, the VMA of FOO is set to ALIGN(ADDR(BAR), 0X1000). This gives the impression that FOO VMA will be aligned 0x1000 upwards of the BAR VMA. However, this is not the case. This is because when ALIGN(ADDR(BAR), 0x1000) is being evaluated at that time no address has yet been assigned to BAR output section. LLD in this case evaluates ADDR(BAR) to 0. This leads to a difference in the expected and the actual value of FOO address. GNU LD reports forward reference error to avoid discrepancy.
The fixed point iteration method lld uses will evaluate ALIGN(ADDR(BAR), 0x1000) including the subexpression ADDR(BAR) multiple times. It's true that the first evaluation of ADDR(BAR) is zero, but the subsequent evaluations use the latest address of BAR. lld stops when the addresses converge.
I think it would be feasible to implement GNU ld errors, but it's extra work and can easily impose restrictions more than necessary.
@MaskRay, ALIGN was not necessary in this example and is only a distraction. Suppose, the linker script is rewritten as follows:
SECTIONS {
FOO ADDR(BAR)+0x1000 : { *(*foo*) }
BAR : { *(*bar*) }
}
GNU ld reports an error in this case, which can be arguably too strict if the author's intent was to place FOO at 0x1000 and BAR at 0x0. lld places section FOO at 0x200b and section BAR at 0x2016 (x86). There are two separate problems with this:
- the solution that lld converges at is incorrect, the address of
BARis not 0x1000 higher than ofFOO; - if it did converge, the solution would depend on unspecified boundary conditions, which might deserve a warning to the user. Thanks!
My comment above assumed that BAR can be placed anywhere. If there is a requirement that the following sections without an explicit address must be placed immediately after the previous section, there would be no solution at all. Why does lld converge in this case?
Customers with complex linker scripts on target such as RISC-V may get weird behaviors if we don't issue diagnostics of when to error out or either warn.
We have seen few issues when customers build images using GNU linker and use modified Zephyr linker scripts.
Some of the are hit with issues such as "backwards movement detected with dot expressions" and forward referencing errors. One such use case is the above linker script snippet from customer use-cases.
When they try out LLD they are confused why LLD does not produce a similar error.
Also It might be useful to come up with a guide when user linker scripts produce warn or error. It would be useful for customers to understand and fix the linker scripts accordingly.
We might want to annotate the map file with some extra information so its easy for customers to know what is causing the error.
Let me know what you think ?
GNU ld reports non constant or forward reference address expression for section for an output section address but doesn't when ADDR is inside the description. x1.lds below demonstrates a case that using a symbol as the output section address can circumvent the error.
There is an argument that can be made to implement an error when output section addresses don't converge (FOO ADDR(BAR)+0x1000). With that, we probably do not need ``non constant or forward reference address expression for section`
set -e
cat > a.s <<e
.text; .space 4
.data; .byte 0
e
cat > x.lds <<e
SECTIONS {
.text ADDR(.data)+0x1000 : { *(.text) }
.data : { *(.data) }
}
e
cat > x1.lds <<e
SECTIONS {
.text foo : { *(.text) }
.data : { *(.data) }
foo = ADDR(.data)+0x1000;
}
e
cat > y.lds <<e
SECTIONS {
.text : { foo = ADDR(.data); *(.text) }
.data : { *(.data) }
}
e
gcc -c a.s
not ld.bfd -T x.lds a.o # non constant or forward reference address expression for section .text
ld.bfd -T x1.lds a.o # essentially identical to x.lds, but accepted
ld.bfd -T y.lds a.o # accepted
#93888 added an error. See also https://discourse.llvm.org/t/lld-and-layout-convergence/79232/8?u=maskray