Skip to content

Statics aligned to greater than a page cause linker error #70144

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
retep998 opened this issue Mar 19, 2020 · 7 comments
Open

Statics aligned to greater than a page cause linker error #70144

retep998 opened this issue Mar 19, 2020 · 7 comments
Labels
A-align Area: alignment control (`repr(align(N))` and so on) A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@retep998
Copy link
Member

retep998 commented Mar 19, 2020

On Windows link.exe requires that the alignment of any section is less than or equal to the /ALIGN value. Statics with an alignment of 8192 or greater cause the section to have an alignment of 8192 (but not greater, see #70022), but link.exe has a default /ALIGN of 4096 causing fatal error LNK1164: section 0x6 alignment (8192) greater than /ALIGN value. Specifying -Clink-arg="/ALIGN:8192" causes the error to go away.

Minimal example

#[repr(align(0x100000))]
struct Aligned(u8);

static X: Aligned = Aligned(0);

fn main() {
    println!("{:#x}", &X as *const _ as usize);
}
@retep998 retep998 added O-windows-msvc Toolchain: MSVC, Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 19, 2020
@jonas-schievink jonas-schievink added A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. labels Mar 19, 2020
ijackson added a commit to ijackson/rust that referenced this issue Jul 21, 2021
Alignments > 4k are not supported,
  rust-lang#70022
  rust-lang#70144

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
@ChrisDenton
Copy link
Member

I no longer get a linker error. Instead rustc outright crashes with STATUS_ILLEGAL_INSTRUCTION. I don't think this is an improvement.

@jieyouxu

This comment has been minimized.

@jieyouxu jieyouxu added O-windows Operating system: Windows A-align Area: alignment control (`repr(align(N))` and so on) and removed O-windows-msvc Toolchain: MSVC, Operating system: Windows labels Jan 20, 2025
@purplesyringa

This comment has been minimized.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. labels Jan 20, 2025
@retep998
Copy link
Member Author

retep998 commented Jan 21, 2025

There is an issue here specific to Windows MSVC, that Rust is creating sections with a higher requirement, but not informing the linker of that via /ALIGN, thus resulting in a LNK1164 linker error.

It's just that now Rust no longer reaches that linker error due to another related bug, causing a crash in rustc itself...

@jieyouxu

This comment has been minimized.

@jieyouxu jieyouxu removed the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jan 21, 2025
@purplesyringa

This comment has been minimized.

@rustbot rustbot removed the I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. label Jan 21, 2025
@beetrees
Copy link
Contributor

beetrees commented Apr 23, 2025

This error can still be reached by using an alignment of 8192:

#[repr(align(8192))]
struct Aligned(u8);

static X: Aligned = Aligned(0);

fn main() {
    println!("{:#x}", &X as *const _ as usize);
}

Note that MinGW and LLVM lld-link instead silently miscompile this example by completely ignoring the section alignment from the object file and use the value passed to /ALIGN (4096 by default) as the section alignment in the EXE/DLL. I've reported this to LLVM in llvm/llvm-project#137043.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-align Area: alignment control (`repr(align(N))` and so on) A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-windows Operating system: Windows T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants