@@ -882,6 +882,33 @@ def _will_emit_object_file(emit):
882882def _remove_codegen_units (flag ):
883883 return None if flag .startswith ("-Ccodegen-units" ) else flag
884884
885+ def _should_add_oso_prefix (feature_configuration , ld_is_direct_driver , toolchain ):
886+ """Whether to add -oso_prefix to strip absolute paths from N_OSO entries.
887+
888+ On macOS, ld64 embeds absolute paths in N_OSO stab entries which breaks
889+ build reproducibility. The -oso_prefix flag strips a prefix from these
890+ entries. This function gates the flag on linker support.
891+
892+ Args:
893+ feature_configuration (FeatureConfiguration): Feature configuration to be queried.
894+ ld_is_direct_driver (bool): Whether the linker is invoked directly (e.g. rust-lld).
895+ toolchain (rust_toolchain): The current Rust toolchain.
896+
897+ Returns:
898+ bool: True if -oso_prefix should be added.
899+ """
900+ if not toolchain .target_os .startswith (("mac" , "darwin" , "ios" )):
901+ return False
902+
903+ if not ld_is_direct_driver :
904+ return feature_configuration and cc_common .is_enabled (
905+ feature_configuration = feature_configuration ,
906+ feature_name = "oso_prefix_is_pwd" ,
907+ )
908+
909+ # lld-macho has supported -oso_prefix since LLVM 14 (2021).
910+ return True
911+
885912def construct_arguments (
886913 * ,
887914 ctx ,
@@ -938,7 +965,9 @@ def construct_arguments(
938965 include_link_flags (bool, optional): Whether to include flags like `-l` that instruct the linker to search for a library.
939966 stamp (bool, optional): Whether or not workspace status stamping is enabled. For more details see
940967 https://docs.bazel.build/versions/main/user-manual.html#flag--stamp
941- remap_path_prefix (str, optional): A value used to remap `${pwd}` to. If set to None, no prefix will be set.
968+ remap_path_prefix (str, optional): A value used to remap `${pwd}`, `${exec_root}`, and `${output_base}` to.
969+ If set to None, no remapping will be applied. On macOS, also adds `-oso_prefix` to strip absolute paths
970+ from N_OSO linker entries.
942971 use_json_output (bool): Have rustc emit json and process_wrapper parse json messages to output rendered output.
943972 build_metadata (bool): Generate CLI arguments for building *only* .rmeta files. This requires use_json_output.
944973 force_depend_on_objects (bool): Force using `.rlib` object files instead of metadata (`.rmeta`) files even if they are available.
@@ -988,6 +1017,8 @@ def construct_arguments(
9881017 # Since we cannot get the `exec_root` from starlark, we cheat a little and
9891018 # use `${pwd}` which resolves the `exec_root` at action execution time.
9901019 process_wrapper_flags .add ("--subst" , "pwd=${pwd}" )
1020+ process_wrapper_flags .add ("--subst" , "exec_root=${exec_root}" )
1021+ process_wrapper_flags .add ("--subst" , "output_base=${output_base}" )
9911022
9921023 # If stamping is enabled, enable the functionality in the process wrapper
9931024 if stamp :
@@ -1075,6 +1106,8 @@ def construct_arguments(
10751106 # For determinism to help with build distribution and such
10761107 if remap_path_prefix != None :
10771108 rustc_flags .add ("--remap-path-prefix=${{pwd}}={}" .format (remap_path_prefix ))
1109+ rustc_flags .add ("--remap-path-prefix=${{exec_root}}={}" .format (remap_path_prefix ))
1110+ rustc_flags .add ("--remap-path-prefix=${{output_base}}={}" .format (remap_path_prefix ))
10781111
10791112 emit_without_paths = []
10801113 for kind in emit :
@@ -1139,6 +1172,17 @@ def construct_arguments(
11391172 # Additional context: https://github.com/rust-lang/rust/pull/36574
11401173 rustc_flags .add_all (link_args , format_each = "--codegen=link-arg=%s" )
11411174
1175+ if remap_path_prefix != None and _should_add_oso_prefix (
1176+ feature_configuration ,
1177+ ld_is_direct_driver ,
1178+ toolchain ,
1179+ ):
1180+ if ld_is_direct_driver :
1181+ rustc_flags .add ("--codegen=link-arg=-oso_prefix" )
1182+ rustc_flags .add ("${output_base}/" , format = "--codegen=link-arg=%s" )
1183+ else :
1184+ rustc_flags .add ("--codegen=link-arg=-Wl,-oso_prefix,${output_base}/" )
1185+
11421186 _add_native_link_flags (
11431187 rustc_flags ,
11441188 dep_info ,
0 commit comments