Comments (8)
In the meantime, I wrote a workaround script in Bash that pretty much parses all the arguments passed to it, and then at some point passes them to a clang (passing -fuse-lld to use mold for most objects, and lld for LLVM IR objects in an intermediary pass).
#!/bin/bash
# Parse arguments and list input files
# - Primary linker invoker: default to /usr/bin/clang++
# (Requires -fuse-ld support)
: "${LD:=/usr/bin/clang++}"
# - Linker for IR bitcode: default to /usr/bin/clang++
# (Requires -fuse-ld support)
: "${IR_LD:=/usr/bin/clang++}"
# - lld path
: "${LLD:=/usr/bin/ld.lld}"
# - mold path
: "${MOLD:=/usr/bin/mold}"
# - Where to cache: defaults to /tmp/irld, set empty to not do.
: "${IRLD_CACHE_DIR:=/tmp/irld}"
inputs=()
output=""
options=()
function anymsg {
color="$1"
type="$2"
msg="$3"
printf "\e[1;%sm[%s] %s ::\e[0;%sm %s\e[m\n" "$color" "$(realpath "$0")" "$type" "$color" "$msg" >/dev/tty
}
function errormsg {
anymsg "31" "error" "$1"
}
function statusmsg {
anymsg "34" "info" "$1"
}
function err_exit {
errormsg "$1"
exit "$2"
}
function assert_arg {
if [[ -z "$1" ]]; then
errormsg "Option '$2' is incomplete; value expected before end of arguments!"
exit 1
fi
}
while [[ -n "$1" ]]; do
case "$1" in
-o*)
if [[ "$1" == "-o" ]]; then
assert_arg "$2" "$1"
shift
output="$1"
shift
else
output="${1:#-o}"
shift
fi ;;
-*)
options+=("$1")
shift ;;
"")
shift ;;
*)
inputs+=("$1")
shift ;;
esac
done
# Fetch and whitelist libraries among inputs
libraries=()
objects=()
for inp in "${inputs[@]}"; do
case "$inp" in
*.a|*.lib|*.so)
libraries+=("$inp") ;;
*)
objects+=("$inp") ;;
esac
done
# 'Transform' objects that are LLVM IR bitcode
transformed=()
regular=()
function irld_cache {
INPUT="$1"
OUTPUT="$1.lib"
# cache related parameters
KEY="$( ( echo "${options[*]};${libraries[*]}::"; cat "$INPUT" ) | shasum -a256 | od -An -tx1 | tr -d ' ' | tr -d '\n' )"
KEYDIR="$IRLD_CACHE_DIR/${KEY:0:2}"
KEYFN="$KEYDIR/${KEY:2}"
if [[ -n "$IRLD_CACHE_DIR" ]]; then
# ensure dir exists
mkdir -p "$IRLD_CACHE_DIR"
# check if record exists
if [[ -f "$KEYFN" ]]; then
statusmsg "Found precached existing ephemeral intermediary static library for LLVM IR: '$INPUT' ... reusing (at '$KEYFN')"
cp "$KEYFN" "$OUTPUT"
# done!
echo "$OUTPUT"
return 0
fi
fi
# record does not exist, or no caching is in place.
statusmsg "Building ephemeral intermediary static library with lld from LLVM IR: '$INPUT'"
"$IR_LD" "${options[@]}" "${libraries[@]}" "$INPUT" -o"$OUTPUT" -fuse-ld="$LLD" || {
errormsg "Could not build ephemeral intermediary static library from LLVM IR bitcode object '$INPUT'; error $?"
exit 2
}
# building succeeded
if [[ -n "$IRLD_CACHE_DIR" ]]; then
# we have already ensured the dir's existence, DRY.
# make the record
mkdir -p "$KEYDIR"
cp "$OUTPUT" "$KEYFN"
fi
echo "$OUTPUT"
return 0
}
for obj in "${objects[@]}"; do
case "$(file "$obj")" in
*LLVM*IR*|*LLVM*bitcode*)
transformed+=("$(irld_cache "$obj")") || exit 2
;;
*)
regular+=("$obj")
;;
esac
done
# Call the linker, finally!
statusmsg "Invoking primary linker like:"
statusmsg "$ $LD ${options[*]} ${libraries[*]} ${regular[*]} ${transformed[*]} -fuse-ld="$MOLD" -o "$output""
"$LD" "${options[@]}" "${libraries[@]}" "${regular[@]}" "${transformed[@]}" -fuse-ld="$MOLD" -o "$output" || {
errormsg "Primary linker returned error code $?"
exit 2
}
# Remove 'temporary' .lib transformations. (Remember the caching!)
for tra in "${transformed[@]}"; do
rm "$tra"
done
exit 0
It's pretty hacky and I don't know if it even works all that well. I think I'll pass on using it myself, for now. :P
from mold.
Recent versions of gcc and clang come with shared object files that can be used as linker plug-ins. Linkers are supposed to dlopen()
given shared object file and make some function calls to convert compiler's intermediate code into a compiled form. mold should be able to use the plug-in shared object files. I want to try that at some point in future.
For now, I thought that one can use mold only for the debug build and use lld for the release build. Did you always enable LTO?
from mold.
Hmmm... Those are interesting ideas and I back them fully. :)
And, apologies for my ignorance, but where does the LTO come in again? I'm slightly lost here. Are you referring to me trying to build LLVM or something?
from mold.
LTO is short for Link-Time Optimization. If you pass -flto
to clang, it emits not a natively-compiled ELF file but an LLVM bitcode. A linker that supports LLVM's LTO can take not only the native ELF file but also LLVM bitcode. If given, a linker passes bitcode files to the LLVM backend to turn it into native code.
So, if you have a LLVM bitcode file as an input to the linker, you are doing LTO. Can you search for -flto
in your project?
from mold.
Ah, I see why you asked about LTO now. :)
Yes, I was using link-time optimization in my attempt to build LLVM. Although I had unset it, as it was consuming an uneconomical amount of memory. Odd that there are still LTO-related object files laying around.. But thanks for the clarification nonetheless!
So what you mean, is that mold is not LTO-ready yet?
from mold.
mold is not LTO-ready yet. It's a nice-to-have feature but not high priority.
from mold.
Yeah, I think that's the general sentiment ^^
from mold.
I'll manage the progress of LTO support at #181.
from mold.
Related Issues (20)
- do applications with LLVM's polly flag and thilto, and linked with mold still segfault? HOT 14
- Fails to link on alpha: relocation truncated to fit: GPREL16 HOT 4
- mold wrapper issue when building in an armv7l container on a arm64/armv7l host using mold 2.4.0 HOT 5
- `--trace-symbol` flag affects `.symver` HOT 2
- Issues with `.symver` and symbol interpositioning HOT 6
- Is macos supported HOT 3
- Help with --section-order and putting sections at different addresses HOT 11
- Segfault linking llvm-sys HOT 18
- -Bsymbolic-non-weak-functions not a valid flag in mold HOT 1
- Mold does not support `-b binary`
- Test #324: x86_64-version failed with latest mold-git HOT 5
- Unknown command line option HOT 1
- mold refused to link GP-setup code for RISC-V HOT 1
- Adding illumos support HOT 2
- Cross compiling using arm-linux-gnueabihf toolchain does not work with LTO on macOS HOT 1
- LDPT_REGISTER_CLAIM_FILE_HOOK_V2 missing from LTO support HOT 2
- Update the performance comparison in the README, and include the GNU ld (BFD) linker HOT 4
- MOLD removes GCC's offload symbols when linking HOT 5
- Mold installation on RHEL8? HOT 2
- Rust on ppc64le: unknown relocation R_PPC64_DTPREL16_LO_DS HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mold.