[PATCH 2/4] Darwin : Handle rpaths given on the command line.

Iain Sandoe iains.gcc@gmail.com
Wed Nov 17 21:03:17 GMT 2021


We want to produce a situation where a default rpath can be added
to each executable (or dylib), but that can be overridden by any
specific rpath provided by the user.

gcc/ChangeLog:

	* config.gcc: Include rpath.opt
	* config/darwin-driver.c (darwin_driver_init): Detect cases
	where the user has added rpaths via a -Wl or -Xlinker command
	and suppress default rpaths in that case.
	* config/darwin.h (DRIVER_SELF_SPECS): Handle -rpath.
	(DARWIN_RPATH_SPEC): New.
	* config/darwin.opt: Add nodefaultrpath option.
---
 gcc/config/darwin-driver.c | 18 ++++++++++++++++++
 gcc/config/darwin.h        | 11 ++++++++++-
 gcc/config/darwin.opt      |  4 ++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/gcc/config/darwin-driver.c b/gcc/config/darwin-driver.c
index 4f0c6bad61f..ccc288f20ce 100644
--- a/gcc/config/darwin-driver.c
+++ b/gcc/config/darwin-driver.c
@@ -281,6 +281,7 @@ darwin_driver_init (unsigned int *decoded_options_count,
   const char *vers_string = NULL;
   bool seen_version_min = false;
   bool seen_sysroot_p = false;
+  bool seen_rpath_p = false;
 
   for (i = 1; i < *decoded_options_count; i++)
     {
@@ -349,6 +350,13 @@ darwin_driver_init (unsigned int *decoded_options_count,
 	  seen_sysroot_p = true;
 	  break;
 
+	case OPT_Xlinker:
+	case OPT_Wl_:
+	  gcc_checking_assert ((*decoded_options)[i].arg);
+	  if (strncmp ((*decoded_options)[i].arg, "-rpath", 6) == 0)
+	    seen_rpath_p = true;
+	  break;
+
 	default:
 	  break;
 	}
@@ -474,4 +482,14 @@ darwin_driver_init (unsigned int *decoded_options_count,
 			  &(*decoded_options)[*decoded_options_count - 1]);
         }
     }
+
+  if (seen_rpath_p)
+    {
+      ++*decoded_options_count;
+      *decoded_options = XRESIZEVEC (struct cl_decoded_option,
+				     *decoded_options,
+				     *decoded_options_count);
+      generate_option (OPT_nodefaultrpath, NULL, 1, CL_DRIVER,
+		       &(*decoded_options)[*decoded_options_count - 1]);
+    }
 }
diff --git a/gcc/config/darwin.h b/gcc/config/darwin.h
index 7ed01efa694..4423933890b 100644
--- a/gcc/config/darwin.h
+++ b/gcc/config/darwin.h
@@ -384,6 +384,7 @@ extern GTY(()) int darwin_ms_struct;
     DARWIN_NOPIE_SPEC \
     DARWIN_RDYNAMIC \
     DARWIN_NOCOMPACT_UNWIND \
+    "%{!r:%{!nostdlib:%{!rpath:%{!nodefaultrpath:%(darwin_rpaths)}}}} " \
     "}}}}}}} %<pie %<no-pie %<rdynamic %<X %<rpath "
 
 /* Spec that controls whether the debug linker is run automatically for
@@ -516,7 +517,8 @@ extern GTY(()) int darwin_ms_struct;
   { "darwin_crt2", DARWIN_CRT2_SPEC },					\
   { "darwin_crt3", DARWIN_CRT3_SPEC },					\
   { "darwin_dylib1", DARWIN_DYLIB1_SPEC },				\
-  { "darwin_bundle1", DARWIN_BUNDLE1_SPEC },
+  { "darwin_bundle1", DARWIN_BUNDLE1_SPEC },				\
+  { "darwin_rpaths", DARWIN_RPATH_SPEC },
 
 #define DARWIN_CRT1_SPEC						\
   "%:version-compare(!> 10.5 mmacosx-version-min= -lcrt1.o)		\
@@ -542,6 +544,13 @@ extern GTY(()) int darwin_ms_struct;
 "%{!static:%:version-compare(< 10.6 mmacosx-version-min= -lbundle1.o)	\
 	   %{fgnu-tm: -lcrttms.o}}"
 
+/* A default rpath, that picks up dependent libraries installed in the same 
+   director as one being loaded.  */
+#define DARWIN_RPATH_SPEC \
+  "%:version-compare(>= 10.5 mmacosx-version-min= -rpath) \
+   %:version-compare(>= 10.5 mmacosx-version-min= @loader_path) \
+   %P "
+
 #ifdef HAVE_AS_MMACOSX_VERSION_MIN_OPTION
 /* Emit macosx version (but only major).  */
 #define ASM_MMACOSX_VERSION_MIN_SPEC \
diff --git a/gcc/config/darwin.opt b/gcc/config/darwin.opt
index d1d1f816912..021d67b17c7 100644
--- a/gcc/config/darwin.opt
+++ b/gcc/config/darwin.opt
@@ -233,6 +233,10 @@ no_dead_strip_inits_and_terms
 Driver RejectNegative
 (Obsolete) Current linkers never dead-strip these items, so the option is not needed.
 
+nodefaultrpath
+Driver RejectNegative
+Do not add a default rpath to executables, modules or dynamic libraries.
+
 nofixprebinding
 Driver RejectNegative
 (Obsolete after 10.3.9) Set MH_NOPREFIXBINDING, in an executable.
-- 
2.24.3 (Apple Git-128)



More information about the Gcc-patches mailing list