Skip to content

Commit

Permalink
WIP Added default flags to fix alignment.
Browse files Browse the repository at this point in the history
There's an issue with compiling C(++) code on 64-bit CPUs running 32-bit
OSes, which have 32-bit-aligned allocators. The resulting binary will
use aligned accesses in release mode.

See romanz/electrs#226 for an example of such
issue.

This change adds default flags setting alignment to whatever the pointer
alignment is. I realize the limitations of this change such as:

* Works in Cargo build scripts only (the major use case for `cc` crate)
* Is unnecessary if allocator actually supports 64 bit alingment
* Is incorrect if allocator supports smaller alignment than what's
  pointer width.

However, resolving this issue requires either matching on all possible
triples, which I don't have the time to implement or somehow fetching
the configuration for given triple, which I don't know how to do. This
is also an experiment to see if it even helps at all.
  • Loading branch information
Kixunil committed Apr 8, 2020
1 parent a20cdff commit f6e079e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,24 @@ impl Build {
}
}

// This won't work outside of build scripts now, but should be helpful already
if let Some(target_ptr_width) = std::env::var("CARGO_CFG_TARGET_POINTER_WIDTH").map_err(|err| if let std::env::VarError::NotUnicode(_) = err { panic!("Invalid encoding of env var CARGO_CFG_TARGET_POINTER_WIDTH") }).ok() {
match cmd.family {
ToolFamily::Clang => {
cmd.args.push(format!("-fnew-alignment={}", target_ptr_width).into());
cmd.args.push(format!("-fmax-type-align={}", target_ptr_width).into());
},
ToolFamily::Gnu => {
cmd.args.push(format!("-mstack-align={}", target_ptr_width).into());
cmd.args.push(format!("-mdata-align={}", target_ptr_width).into());
cmd.args.push(format!("-mconst-align={}", target_ptr_width).into());
},
ToolFamily::Msvc { .. } => {
// I don't know what needs to be set up here.
},
}
}

if target.contains("-ios") {
// FIXME: potential bug. iOS is always compiled with Clang, but Gcc compiler may be
// detected instead.
Expand Down

0 comments on commit f6e079e

Please sign in to comment.