How to set RPATH and RUNPATH with GCC/LD?

The project GNU Binutils (containing GNU linker (ld)) is not at the origin of this change of behaviour, but Debian (2016)1, and Gentoo (2013!)2.

According to gentoo commit from Mike Frysinger on Jan 2013 :

"The "new" dtags options have been around for 14+ years now, so for Linux and GNU targets, enable them by default."

This change are not well received 3, 4, 5, as RUNPATH and RPATH have "undocumented behaviour difference"... Surprisingly this changes is now applied on Debian stable.

The problem is that using RUNPATH, leads to unpredictable problems... But mainly just works . From wikipedia :

The ld dynamic linker does not search DT_RUNPATH locations for transitive dependencies, unlike DT_RPATH.


Is there a way to force the old behavior?

Yes. You can use this option -Wl,--disable-new-dtags to tell the new linker to use the old behavior, i.e. RPATH.

Is it possible to tell the old version of linker to produce the new output (i.e. RUNPATH instead of RPATH)?

Yes. Use -Wl,--enable-new-dtags to tell the old linker to use the new behavior, i.e. RUNPATH.

I verified the executable with readelf and these two options seem to control what will be written in the ELF Dynamic section. I think the problem was caused by a change in the defaults for the new version, although, interestingly, the manual page for ld would suggest that it should still be the same:

--enable-new-dtags
--disable-new-dtags
This linker can create the new dynamic tags in ELF. But the older ELF systems may not understand them. If you specify --enable-new-dtags, the new dynamic tags will be created as needed and older dynamic tags will be omitted. If you specify --disable-new-dtags, no new dynamic tags will be created. By default, the new dynamic tags are not created. Note that those options are only available for ELF systems.