Skip to content
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

rust-analyzer fails to expand proc-macro when mold linker is used #80

Closed
pacak opened this issue Jul 6, 2021 · 7 comments
Closed

rust-analyzer fails to expand proc-macro when mold linker is used #80

pacak opened this issue Jul 6, 2021 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@pacak
Copy link

pacak commented Jul 6, 2021

I'm using stable rustc, most recent rust-analyzer and static build of mold as of a week ago or so.

config.toml:

[build]
rustflags = "-C target-feature=+fma,+avx2"

[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=/path/to/mold"]

When deriving via proc macro is used (here serde, but any other proc macro fails

use serde::Serialize;

#[derive(Debug, Serialize)]
pub enum Distance {
    Long,
    Short,
}

rust analyzer fails to expand the macro with an error like this:
(this error is most likely about some different proc macro)

proc-macro /path/target/debug/deps/libtracing_attributes-7c1265016b85e087.so failed to find the given version. Reason: Invalid ELF section header offset/size/alignment                                                       
Failed to find proc macros. Error: ExpansionError(                                                                                                                                                                                            
    "Cannot create expander for /path/target/debug/deps/libtracing_attributes-7c1265016b85e087.so: Custom { kind: InvalidData, error: Error(\"Invalid ELF section header offset/size/alignment\") }",
)

You can invoke rust analyzer like this

rust-analyzer analysis-stats . 

The same code works as expected when compiled in a usual way

@rui314 rui314 self-assigned this Jul 6, 2021
@rui314 rui314 added the bug Something isn't working label Jul 7, 2021
@rui314
Copy link
Owner

rui314 commented Jul 7, 2021

I created a new Rust project on my machine, copy-n-paste your code and build it with mold. I also built rust-analyzer with mold. But I couldn't reproduce the issue -- rust-analyzer analysis-stats . works with and without mold. Is there more hidden conditions to reproduce the issue? Did you observe the same issue when building programs that are publicly available? (If so, I can try that too)

@pacak
Copy link
Author

pacak commented Jul 7, 2021 via email

@rui314
Copy link
Owner

rui314 commented Jul 7, 2021

Yeah, it's great if you can reproduce the issue in a Docker.

@nehaljwani
Copy link
Contributor

nehaljwani commented Jul 7, 2021

Steps to reproduce the problem:

# cat Cargo.toml 
[package]
name = "dummy"
version = "0.0.0"
edition = "2018"

[dependencies]
object = { version = "0.25.3", default-features = false, features = ["read_core", "elf"] }
memmap2 = "0.3.0"

# cat src/main.rs 
use std::env;

use std::fs::File;
use memmap2::MmapOptions;
use object::read::{File as BinaryFile};

fn main() {
    let args: Vec<String> = env::args().collect();
    let file = File::open(&args[1]).expect(":'(\t");
    let mmap = unsafe { MmapOptions::new().map(&file).expect(":'(\t") };
    BinaryFile::parse(&*mmap).expect(":(\t");
}

# ~/.cargo/bin/cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s

# echo 'void foo() {};' | clang -xc - -fuse-ld=/root/mold/mold -shared -o libfoo.so

# ./target/debug/dummy /lib/x86_64-linux-gnu/libc.so.6
# echo $?
0

# ./target/debug/dummy $(readlink -f libfoo.so)
thread 'main' panicked at ':(	: Error("Invalid ELF section header offset/size/alignment")', src/main.rs:11:31
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

# echo $?
101

@nehaljwani
Copy link
Contributor

nehaljwani commented Jul 7, 2021

Update: If I add the "unaligned" feature to the object dependency, then it works. This is not present at https://github.com/rust-analyzer/rust-analyzer/search?l=TOML&q=object . I don't know why it works though.

@pacak
Copy link
Author

pacak commented Jul 7, 2021

Let me know if an example above is working for you. If not - I'll try to make something in docker.

@rui314 rui314 closed this as completed in 50b7497 Jul 8, 2021
@rui314
Copy link
Owner

rui314 commented Jul 8, 2021

@nehaljwani Thank you very much for the detailed instructions on how to reproduce the issue! With that, I could easily reproduce it and found the cause of the issue. The bug was a little bit surprising -- we haven't aligned the section header to a 4 or 8 byte boundaries until now. x86 isn't sensitive to data alignments, so it happened to work. The problem has been fixed.

rui314 added a commit that referenced this issue Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants