Force default rpath in LLVM-compiled binaries

I’m trying to build LLVM which sets a particular RPATH in all binaries it compiles (not the LLVM binaries themselves). I realize this is an unusual choice, which I’ll explain below.

Building GCC, I can do what I need using by giving a spec to the configure script:

–with-specs=‘%{!static:%x{-rpath=/opt/lib} %x{-enable-new-dtags}}’

After searching around repeatedly, I’m having trouble finding a method to build LLVM to similarly force a particular RPATH in its compiler output.

I realize this is a bad idea for a LLVM intended to produce binaries that can be distributed. The system I’m working with is built on hardened BSD VMs where /usr/local and other typical paths are off-limits as part of the base image subject to replacement. All our add-on tools are built in /opt which is preserved across base system upgrades. The compiler will be used to produce binaries intended to run only on the local system, and I want to avoid asking each developer using the VMs to set LD_LIBRARY_PATH.

Any hints on where I should be looking to modify the default linker flags of the produced compiler would be great.

Dave Hart

Have you considered -Wl,--rpath? From lld --help:
--rpath=<value> Add a DT_RUNPATH to the output
gold and bfd has this option, too.

That would do the trick if included on every invocation of the compiler/linker. I’m trying to do the same without requiring the user to do anything special – I want to build a clang that acts as if that option were given by default.

That’s a buildsystem feature. For instance, CMake has a notion of toolchain files exactly for the purpose of setting up the toolchain and environment, compiler options included.

If you want this to be done at compiler level, I guess you’ll have to patch the compiler driver (e.g. clang++) a bit.

I’m not sure how this works with gcc, is this really in the compiler output? That is ELF object can carry this information? I would think this is a linker flag, and so something you’d like hardcoded in lld, or more likely in the clang driver (if you’re using the clang driver for linking).

Yes, the compiler driver is adding the -Wl,-rpath,/opt/lib automatically to all binaries linked via the compiler driver, without requiring any flags to the compiler or linker.

I recognize this is an unusual request. I’m trying to provide a development environment that acts as if it were a normal system despite being built on a hardened VM image that prohibits modification to the typical directories, which are subject to replacement as the VM image is updated to a newer version. ldconfig could also be used in more typical situations, but again that’s part of the base system and I’m trying to build an overlay which has additional tools installed in /opt which can be used as if they were installed in the base system. Some of those additional tools have shared libraries installed in /opt/lib which I want to be found by default by binaries compiled with the clang in /opt. If I’m being unclear about why this is useful to me, please ask questions.

Maybe you can use a default config file for your usecase: Clang Compiler User’s Manual — Clang 18.0.0git documentation

Thanks for the pointer, configuration files should work well for this situation.