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