-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#[link_section] is only usable from the root crate #67209
Comments
Can you try using |
Also, could you check the .rlib file generated for the dependency crate? Make sure it has a non-empty |
Sorry, I had both information pieces already available, but forget to write them down...
The visibility of the static (or any parent module) doesn't change the behavior.
I had a look at the file and it contains the section with its contents:
As you can see, the section is present and has the size expected (4 bytes of the So the linker should have all required information about the section and its data. |
I did some more investigation: when using the system linker
Reproduction: mkdir mwe-asm
cd mwe-asm/
cat <<EOF > b.s
.section .mysection
.align 4
.global X
X:
.word 42
EOF
cat <<EOF > a.s
.comm X, 4, 4
.global main
main:
ret
EOF
as -o a.o a.s
as -o b.o b.s
cat <<EOF > link.x
ENTRY(main)
SECTIONS {
.mysection : ALIGN(8) {
_START_ = .;
KEEP(*(.mysection))
_END_ = .;
}
}
ASSERT(_END_ != _START_, "Section empty");
EOF
# link directly
ld -o archive-no.elf a.o b.o -Tlink.x --gc-sections
# create archive and link
ar rcs libb.a b.o
ld -o archive-yes.elf a.o b.o -Tlink.x -lb -L. --gc-sections Both Has anybody an idea how to proceed? |
A bit outdated, but I found that updating my toolchain seems to have fixed a similar issue I have (works on 2022-05-01, but not 2022-01-01 or earlier). |
Confirmed fixed with the toolchain 1.62.1! When redoing the steps in the issue description, the current toolchain 1.62.1 produce the desired result: ~/mwe/a$ cargo build --target x86_64-unknown-linux-musl
~/mwe/a$ readelf -S target/x86_64-unknown-linux-musl/debug/a | grep -A1 .mysection
[703] .mysection PROGBITS 00000000000478d0 000488d0
0000000000000004 0000000000000000 A 0 0 8 As you can see in the last output line, the section is now non-empty and has a size of 4 (a single Seems like this bug was fixed by accident 😉 @Amanieu should we add a test to the Rust repo, so that this doesn't break accidentally? Or should this bug simply be closed as fixed? |
Possibly related to #95604? |
Consider the following MNWE (minimal non-working example 😄):
The setup contains two crates: the binary
a
and the dependent crateb
. A custom linker script is used in order to put the contents of.mysection
in a section with the same name. The assert statement makes the error visible: the static variableX
should be moved into that section, making the section 4 bytes in size, but the section is empty.If the
static X
is defined in cratea
, everything works fine:So the behavior is different when the
#[used] #[link_section] static
is placed in a dependency or in the currently build crate.Am I doing something wrong (in which case it should be documented more clearly) or is there really a bug in the linker?
The text was updated successfully, but these errors were encountered: