mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 12:57:41 +00:00
build: be more aggressive with linker optimizations; wrap mold
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I0e3132ab1499684eda715c3cee9b27a16a6a6964
This commit is contained in:
parent
5ce0f3b1e8
commit
781de52ca6
4 changed files with 69 additions and 9 deletions
|
|
@ -1,5 +1,31 @@
|
|||
# Link with Mold, and without libc! We use nostartfiles to avoid the C runtime
|
||||
# Use a linker wrapper that invokes mold then strips junk sections with objcopy.
|
||||
# mold cannot discard .eh_frame/.dynstr/.comment via linker scripts, so we do
|
||||
# it as a post-link step.
|
||||
# See:
|
||||
# <https://github.com/rui314/mold?tab=readme-ov-file#how-to-use>
|
||||
[target.'cfg(target_os = "linux")']
|
||||
rustflags = [ "-C", "link-arg=-fuse-ld=mold", "-C", "link-arg=-nostartfiles" ]
|
||||
linker = "scripts/ld-wrapper"
|
||||
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;
|
||||
# those remnants are removed by the linker wrapper via objcopy post-link)
|
||||
"-C",
|
||||
"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)
|
||||
]
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ publish = false
|
|||
[dependencies]
|
||||
hotpath = { optional = true, version = "0.14.0" }
|
||||
microfetch-alloc.workspace = true
|
||||
microfetch-lib.workspace = true
|
||||
microfetch-asm.workspace = true
|
||||
microfetch-lib.workspace = true
|
||||
|
||||
[features]
|
||||
hotpath = [ "dep:hotpath" ]
|
||||
|
|
|
|||
34
scripts/ld-wrapper
Executable file
34
scripts/ld-wrapper
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/env sh
|
||||
# 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:
|
||||
# - .eh_frame / .eh_frame_hdr - unwind tables from compiler_builtins
|
||||
# - dynstr - mold emits this, even for fully static binaries
|
||||
# - .comment - compiler version string
|
||||
#
|
||||
# We forward everything to mold via -fuse-ld, then post-process the output in place.
|
||||
|
||||
set -eu
|
||||
|
||||
# Locate the output file
|
||||
OUTPUT=""
|
||||
prev=""
|
||||
for arg in "$@"; do
|
||||
if [ "$prev" = "-o" ]; then
|
||||
OUTPUT="$arg"
|
||||
break
|
||||
fi
|
||||
prev="$arg"
|
||||
done
|
||||
|
||||
# Invoke mold via the cc driver, forward all original arguments
|
||||
cc -fuse-ld=mold "$@"
|
||||
|
||||
# Remove sections that mold cannot discard
|
||||
if [ -n "$OUTPUT" ] && [ -f "$OUTPUT" ]; then
|
||||
objcopy \
|
||||
--remove-section=.eh_frame \
|
||||
--remove-section=.eh_frame_hdr \
|
||||
--remove-section=.dynstr \
|
||||
--remove-section=.comment \
|
||||
"$OUTPUT"
|
||||
fi
|
||||
Loading…
Add table
Add a link
Reference in a new issue