mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 12:57:41 +00:00
build: move link flags to build.rs
Binary-specific link flags (-nostartfiles, -static, section stripping) now use cargo:rustc-link-arg-bin so they don't break proc-macro or build-script linking. ld-wrapper only strips sections from static binaries.
This commit is contained in:
parent
c426e88d99
commit
1539533c54
3 changed files with 29 additions and 23 deletions
|
|
@ -3,29 +3,14 @@
|
||||||
# it as a post-link step.
|
# it as a post-link step.
|
||||||
# See:
|
# See:
|
||||||
# <https://github.com/rui314/mold?tab=readme-ov-file#how-to-use>
|
# <https://github.com/rui314/mold?tab=readme-ov-file#how-to-use>
|
||||||
|
#
|
||||||
|
# Binary-specific link flags live in microfetch/build.rs via cargo:rustc-link-arg-bin
|
||||||
|
# so they only affect the final binary and don't break proc-macro or build-script linking.
|
||||||
[target.'cfg(target_os = "linux")']
|
[target.'cfg(target_os = "linux")']
|
||||||
linker = "scripts/ld-wrapper"
|
linker = "scripts/ld-wrapper"
|
||||||
rustflags = [
|
rustflags = [
|
||||||
# No C runtime, we provide _start ourselves
|
|
||||||
"-C",
|
|
||||||
"link-arg=-nostartfiles",
|
|
||||||
# Fully static, no dynamic linker, no .interp/.dynsym/.dynamic overhead
|
|
||||||
"-C",
|
|
||||||
"link-arg=-static",
|
|
||||||
# Static PIE is incompatible with -static :(
|
|
||||||
"-C",
|
|
||||||
"relocation-model=static",
|
|
||||||
# Suppress .eh_frame emission from our own codegen (does not cover compiler_builtins;
|
# Suppress .eh_frame emission from our own codegen (does not cover compiler_builtins;
|
||||||
# those remnants are removed by the linker wrapper via objcopy post-link)
|
# those remnants are removed by the linker wrapper via objcopy post-link)
|
||||||
"-C",
|
"-C",
|
||||||
"force-unwind-tables=no",
|
"force-unwind-tables=no",
|
||||||
# Linker flags
|
|
||||||
"-C",
|
|
||||||
"link-arg=-Wl,--gc-sections", # remove unreferenced input sections
|
|
||||||
"-C",
|
|
||||||
"link-arg=-Wl,--strip-all", # strip all symbol table entries
|
|
||||||
"-C",
|
|
||||||
"link-arg=-Wl,--build-id=none", # omit the .note.gnu.build-id section
|
|
||||||
"-C",
|
|
||||||
"link-arg=-Wl,-z,norelro", # disable RELRO (removes relro_padding)
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
17
microfetch/build.rs
Normal file
17
microfetch/build.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
fn main() {
|
||||||
|
// These flags only apply to the microfetch binary, not to proc-macro crates
|
||||||
|
// or other host-compiled artifacts.
|
||||||
|
|
||||||
|
// No C runtime, we provide _start ourselves
|
||||||
|
println!("cargo:rustc-link-arg-bin=microfetch=-nostartfiles");
|
||||||
|
// Fully static, no dynamic linker, no .interp/.dynsym/.dynamic overhead
|
||||||
|
println!("cargo:rustc-link-arg-bin=microfetch=-static");
|
||||||
|
// Remove unreferenced input sections
|
||||||
|
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--gc-sections");
|
||||||
|
// Strip all symbol table entries
|
||||||
|
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--strip-all");
|
||||||
|
// Omit the .note.gnu.build-id section
|
||||||
|
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,--build-id=none");
|
||||||
|
// Disable RELRO (removes relro_padding)
|
||||||
|
println!("cargo:rustc-link-arg-bin=microfetch=-Wl,-z,norelro");
|
||||||
|
}
|
||||||
|
|
@ -2,20 +2,23 @@
|
||||||
# Invoke mold, then strip junk sections from the output binary with objcopy.
|
# Invoke mold, then strip junk sections from the output binary with objcopy.
|
||||||
# This (more or less) removes sections that mold cannot discard itself, suck as:
|
# This (more or less) removes sections that mold cannot discard itself, suck as:
|
||||||
# - .eh_frame / .eh_frame_hdr - unwind tables from compiler_builtins
|
# - .eh_frame / .eh_frame_hdr - unwind tables from compiler_builtins
|
||||||
# - dynstr - mold emits this, even for fully static binaries
|
# - .dynstr - mold emits this, even for fully static binaries
|
||||||
# - .comment - compiler version string
|
# - .comment - compiler version string
|
||||||
#
|
#
|
||||||
# We forward everything to mold via -fuse-ld, then post-process the output in place.
|
# We forward everything to mold via -fuse-ld, then post-process the output in place.
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
# Locate the output file
|
# Locate the output file and detect static linking
|
||||||
|
IS_STATIC=0
|
||||||
OUTPUT=""
|
OUTPUT=""
|
||||||
prev=""
|
prev=""
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
|
case "$arg" in
|
||||||
|
-static) IS_STATIC=1 ;;
|
||||||
|
esac
|
||||||
if [ "$prev" = "-o" ]; then
|
if [ "$prev" = "-o" ]; then
|
||||||
OUTPUT="$arg"
|
OUTPUT="$arg"
|
||||||
break
|
|
||||||
fi
|
fi
|
||||||
prev="$arg"
|
prev="$arg"
|
||||||
done
|
done
|
||||||
|
|
@ -23,8 +26,9 @@ done
|
||||||
# Invoke mold via the cc driver, forward all original arguments
|
# Invoke mold via the cc driver, forward all original arguments
|
||||||
cc -fuse-ld=mold "$@"
|
cc -fuse-ld=mold "$@"
|
||||||
|
|
||||||
# Remove sections that mold cannot discard
|
# Only strip sections from fully static binaries.
|
||||||
if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then
|
# Dynamic executables (i.e. build scripts, proc-macros) need .dynstr at runtime.
|
||||||
|
if [ "$IS_STATIC" = 1 ] && [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then
|
||||||
objcopy \
|
objcopy \
|
||||||
--remove-section=.eh_frame \
|
--remove-section=.eh_frame \
|
||||||
--remove-section=.eh_frame_hdr \
|
--remove-section=.eh_frame_hdr \
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue