From fa2b6122135ad18640e2e9b47450f50fa873c54a Mon Sep 17 00:00:00 2001
From: Oneirical <manchot@videotron.ca>
Date: Thu, 6 Jun 2024 11:55:24 -0400
Subject: [PATCH 1/4] Rewrite link-args-order to rmake

---
 src/doc/book                                  |  2 +-
 src/tools/cargo                               |  2 +-
 src/tools/run-make-support/src/rustc.rs       | 24 ++++++++++++
 .../tidy/src/allowed_run_make_makefiles.txt   |  1 -
 tests/run-make/link-args-order/Makefile       | 10 -----
 tests/run-make/link-args-order/rmake.rs       | 39 +++++++++++++++++++
 6 files changed, 65 insertions(+), 13 deletions(-)
 delete mode 100644 tests/run-make/link-args-order/Makefile
 create mode 100644 tests/run-make/link-args-order/rmake.rs

diff --git a/src/doc/book b/src/doc/book
index 45c1a6d69edfd..5228bfac8267a 160000
--- a/src/doc/book
+++ b/src/doc/book
@@ -1 +1 @@
-Subproject commit 45c1a6d69edfd1fc91fb7504cb73958dbd09441e
+Subproject commit 5228bfac8267ad24659a81b92ec5417976b5edbc
diff --git a/src/tools/cargo b/src/tools/cargo
index a1f47ec3f7cd0..4dcbca118ab7f 160000
--- a/src/tools/cargo
+++ b/src/tools/cargo
@@ -1 +1 @@
-Subproject commit a1f47ec3f7cd076986f1bfcd7061f2e8cb1a726e
+Subproject commit 4dcbca118ab7f9ffac4728004c983754bc6a04ff
diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs
index 331e1a5ab8b32..7e5a637e20e15 100644
--- a/src/tools/run-make-support/src/rustc.rs
+++ b/src/tools/run-make-support/src/rustc.rs
@@ -230,6 +230,24 @@ impl Rustc {
         self
     }
 
+    /// Add multiple extra arguments to the linker invocation, via `-Clink-args`.
+    pub fn link_args(&mut self, link_args: &str) -> &mut Self {
+        self.cmd.arg(format!("-Clink-args={link_args}"));
+        self
+    }
+
+    /// Add an extra argument to prepend the linker invocation, via `-Zpre-link-arg`.
+    pub fn pre_link_arg(&mut self, link_arg: &str) -> &mut Self {
+        self.cmd.arg(format!("-Zpre-link-arg={link_arg}"));
+        self
+    }
+
+    /// Add multiple extra arguments to the linker invocation, via `-Zpre-link-args`.
+    pub fn pre_link_args(&mut self, link_args: &str) -> &mut Self {
+        self.cmd.arg(format!("-Zpre-link-args={link_args}"));
+        self
+    }
+
     /// Specify a stdin input
     pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
         self.cmd.set_stdin(input.as_ref().to_vec().into_boxed_slice());
@@ -248,4 +266,10 @@ impl Rustc {
         self.cmd.arg(format!("-Clinker={linker}"));
         self
     }
+
+    /// Specify the linker flavor
+    pub fn linker_flavor(&mut self, linker_flavor: &str) -> &mut Self {
+        self.cmd.arg(format!("-Clinker-flavor={linker_flavor}"));
+        self
+    }
 }
diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index 1596257747fa9..feb31e25e5f85 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -100,7 +100,6 @@ run-make/libtest-json/Makefile
 run-make/libtest-junit/Makefile
 run-make/libtest-padding/Makefile
 run-make/libtest-thread-limit/Makefile
-run-make/link-args-order/Makefile
 run-make/link-cfg/Makefile
 run-make/link-framework/Makefile
 run-make/link-path-order/Makefile
diff --git a/tests/run-make/link-args-order/Makefile b/tests/run-make/link-args-order/Makefile
deleted file mode 100644
index c562cc1b396fa..0000000000000
--- a/tests/run-make/link-args-order/Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
-# ignore-msvc
-
-include ../tools.mk
-
-RUSTC_FLAGS = -C linker-flavor=ld -C link-arg=a -C link-args="b c" -C link-args="d e" -C link-arg=f
-RUSTC_FLAGS_PRE = -C linker-flavor=ld -Z pre-link-arg=a -Z pre-link-args="b c" -Z pre-link-args="d e" -Z pre-link-arg=f
-
-all:
-	$(RUSTC) $(RUSTC_FLAGS) empty.rs 2>&1 | $(CGREP) '"a" "b" "c" "d" "e" "f"'
-	$(RUSTC) $(RUSTC_FLAGS_PRE) empty.rs 2>&1 | $(CGREP) '"a" "b" "c" "d" "e" "f"'
diff --git a/tests/run-make/link-args-order/rmake.rs b/tests/run-make/link-args-order/rmake.rs
new file mode 100644
index 0000000000000..cd921a031292b
--- /dev/null
+++ b/tests/run-make/link-args-order/rmake.rs
@@ -0,0 +1,39 @@
+// Passing linker arguments to the compiler used to be lost or reordered in a messy way
+// as they were passed further to the linker. This was fixed in #70665, and this test
+// checks that linker arguments remain intact and in the order they were originally passed in.
+// See https://github.com/rust-lang/rust/pull/70665
+
+use run_make_support::rustc;
+
+fn main() {
+    assert!(
+        String::from_utf8(
+            rustc()
+                .input("empty.rs")
+                .linker_flavor("ld")
+                .link_arg("a")
+                .link_args("\"b c\"")
+                .link_args("\"d e\"")
+                .link_arg("f")
+                .run_fail()
+                .stderr
+        )
+        .unwrap()
+        .contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"")
+    );
+    assert!(
+        String::from_utf8(
+            rustc()
+                .input("empty.rs")
+                .linker_flavor("ld")
+                .pre_link_arg("a")
+                .pre_link_args("\"b c\"")
+                .pre_link_args("\"d e\"")
+                .pre_link_arg("f")
+                .run_fail()
+                .stderr
+        )
+        .unwrap()
+        .contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"")
+    );
+}

From 594135ea370085bfd80ce56f7f08b146cb97f3ca Mon Sep 17 00:00:00 2001
From: Oneirical <manchot@videotron.ca>
Date: Thu, 6 Jun 2024 14:51:55 -0400
Subject: [PATCH 2/4] Rewrite ls-metadata to rmake

---
 .../tidy/src/allowed_run_make_makefiles.txt     |  1 -
 tests/run-make/ls-metadata/Makefile             |  8 --------
 tests/run-make/ls-metadata/rmake.rs             | 17 +++++++++++++++++
 3 files changed, 17 insertions(+), 9 deletions(-)
 delete mode 100644 tests/run-make/ls-metadata/Makefile
 create mode 100644 tests/run-make/ls-metadata/rmake.rs

diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index feb31e25e5f85..57bb326ac954c 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -108,7 +108,6 @@ run-make/llvm-ident/Makefile
 run-make/long-linker-command-lines-cmd-exe/Makefile
 run-make/long-linker-command-lines/Makefile
 run-make/longjmp-across-rust/Makefile
-run-make/ls-metadata/Makefile
 run-make/lto-dylib-dep/Makefile
 run-make/lto-empty/Makefile
 run-make/lto-linkage-used-attr/Makefile
diff --git a/tests/run-make/ls-metadata/Makefile b/tests/run-make/ls-metadata/Makefile
deleted file mode 100644
index f03569baef7f2..0000000000000
--- a/tests/run-make/ls-metadata/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-# ignore-cross-compile
-include ../tools.mk
-
-all:
-	$(RUSTC) foo.rs
-	$(RUSTC) -Z ls=root $(TMPDIR)/foo
-	touch $(TMPDIR)/bar
-	$(RUSTC) -Z ls=root $(TMPDIR)/bar
diff --git a/tests/run-make/ls-metadata/rmake.rs b/tests/run-make/ls-metadata/rmake.rs
new file mode 100644
index 0000000000000..61dcd3774a193
--- /dev/null
+++ b/tests/run-make/ls-metadata/rmake.rs
@@ -0,0 +1,17 @@
+// Passing invalid files to -Z ls (which lists the symbols
+// defined by a library crate) used to cause a segmentation fault.
+// As this was fixed in #11262, this test checks that no segfault
+// occurs when passing the invalid file `bar` to -Z ls.
+// See https://github.com/rust-lang/rust/issues/11259
+
+//@ ignore-cross-compile
+
+use run_make_support::fs_wrapper;
+use run_make_support::{rmake_out_path, rustc};
+
+fn main() {
+    rustc().input("foo.rs");
+    rustc().arg("-Zls=root").input(rmake_out_path("foo"));
+    fs_wrapper::create_file(rmake_out_path("bar"));
+    rustc().arg("-Zls=root").input(rmake_out_path("bar"));
+}

From 03a4259c8b925330d5425e61e7553351c21e55be Mon Sep 17 00:00:00 2001
From: Oneirical <manchot@videotron.ca>
Date: Thu, 6 Jun 2024 15:20:42 -0400
Subject: [PATCH 3/4] Rewrite lto-readonly-lib to rmake

---
 .../tidy/src/allowed_run_make_makefiles.txt   |  1 -
 tests/run-make/link-args-order/rmake.rs       | 48 +++++++------------
 tests/run-make/ls-metadata/rmake.rs           | 10 ++--
 tests/run-make/lto-readonly-lib/Makefile      | 13 -----
 tests/run-make/lto-readonly-lib/rmake.rs      | 25 ++++++++++
 5 files changed, 48 insertions(+), 49 deletions(-)
 delete mode 100644 tests/run-make/lto-readonly-lib/Makefile
 create mode 100644 tests/run-make/lto-readonly-lib/rmake.rs

diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index 57bb326ac954c..0fdc6de0b644e 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -112,7 +112,6 @@ run-make/lto-dylib-dep/Makefile
 run-make/lto-empty/Makefile
 run-make/lto-linkage-used-attr/Makefile
 run-make/lto-no-link-whole-rlib/Makefile
-run-make/lto-readonly-lib/Makefile
 run-make/lto-smoke-c/Makefile
 run-make/macos-deployment-target/Makefile
 run-make/macos-fat-archive/Makefile
diff --git a/tests/run-make/link-args-order/rmake.rs b/tests/run-make/link-args-order/rmake.rs
index cd921a031292b..4530251c7a585 100644
--- a/tests/run-make/link-args-order/rmake.rs
+++ b/tests/run-make/link-args-order/rmake.rs
@@ -6,34 +6,22 @@
 use run_make_support::rustc;
 
 fn main() {
-    assert!(
-        String::from_utf8(
-            rustc()
-                .input("empty.rs")
-                .linker_flavor("ld")
-                .link_arg("a")
-                .link_args("\"b c\"")
-                .link_args("\"d e\"")
-                .link_arg("f")
-                .run_fail()
-                .stderr
-        )
-        .unwrap()
-        .contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"")
-    );
-    assert!(
-        String::from_utf8(
-            rustc()
-                .input("empty.rs")
-                .linker_flavor("ld")
-                .pre_link_arg("a")
-                .pre_link_args("\"b c\"")
-                .pre_link_args("\"d e\"")
-                .pre_link_arg("f")
-                .run_fail()
-                .stderr
-        )
-        .unwrap()
-        .contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"")
-    );
+    rustc()
+        .input("empty.rs")
+        .linker_flavor("ld")
+        .link_arg("a")
+        .link_args("\"b c\"")
+        .link_args("\"d e\"")
+        .link_arg("f")
+        .run_fail()
+        .assert_stderr_contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"");
+    rustc()
+        .input("empty.rs")
+        .linker_flavor("ld")
+        .pre_link_arg("a")
+        .pre_link_args("\"b c\"")
+        .pre_link_args("\"d e\"")
+        .pre_link_arg("f")
+        .run_fail()
+        .assert_stderr_contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"");
 }
diff --git a/tests/run-make/ls-metadata/rmake.rs b/tests/run-make/ls-metadata/rmake.rs
index 61dcd3774a193..0e60f2c46787a 100644
--- a/tests/run-make/ls-metadata/rmake.rs
+++ b/tests/run-make/ls-metadata/rmake.rs
@@ -7,11 +7,11 @@
 //@ ignore-cross-compile
 
 use run_make_support::fs_wrapper;
-use run_make_support::{rmake_out_path, rustc};
+use run_make_support::rustc;
 
 fn main() {
-    rustc().input("foo.rs");
-    rustc().arg("-Zls=root").input(rmake_out_path("foo"));
-    fs_wrapper::create_file(rmake_out_path("bar"));
-    rustc().arg("-Zls=root").input(rmake_out_path("bar"));
+    rustc().input("foo.rs").run();
+    rustc().arg("-Zls=root").input("foo").run();
+    fs_wrapper::create_file("bar");
+    rustc().arg("-Zls=root").input("bar").run();
 }
diff --git a/tests/run-make/lto-readonly-lib/Makefile b/tests/run-make/lto-readonly-lib/Makefile
deleted file mode 100644
index 11d944e3e3d4b..0000000000000
--- a/tests/run-make/lto-readonly-lib/Makefile
+++ /dev/null
@@ -1,13 +0,0 @@
-# ignore-cross-compile
-include ../tools.mk
-
-all:
-	$(RUSTC) lib.rs
-
-	# the compiler needs to copy and modify the rlib file when performing
-	# LTO, so we should ensure that it can cope with the original rlib
-	# being read-only.
-	chmod 444 $(TMPDIR)/*.rlib
-
-	$(RUSTC) main.rs -C lto
-	$(call RUN,main)
diff --git a/tests/run-make/lto-readonly-lib/rmake.rs b/tests/run-make/lto-readonly-lib/rmake.rs
new file mode 100644
index 0000000000000..bf1dafee697dc
--- /dev/null
+++ b/tests/run-make/lto-readonly-lib/rmake.rs
@@ -0,0 +1,25 @@
+// When the compiler is performing link time optimization, it will
+// need to copy the original rlib file, set the copy's permissions to read/write,
+// and modify that copy - even if the original
+// file is read-only. This test creates a read-only rlib, and checks that
+// compilation with LTO succeeds.
+// See https://github.com/rust-lang/rust/pull/17619
+
+//@ ignore-cross-compile
+
+use run_make_support::fs_wrapper;
+use run_make_support::{cwd, run, rustc};
+
+fn main() {
+    rustc().input("lib.rs").run();
+    let entries = fs_wrapper::read_dir(cwd());
+    for entry in entries {
+        if entry.path().extension().and_then(|s| s.to_str()) == Some("rlib") {
+            let mut perms = fs_wrapper::metadata(entry.path()).permissions();
+            perms.set_readonly(true);
+            fs_wrapper::set_permissions(entry.path(), perms);
+        }
+    }
+    rustc().input("main.rs").arg("-Clto").run();
+    run("main");
+}

From b3c51323b507192521cfa264a6ab129fcdb85b34 Mon Sep 17 00:00:00 2001
From: Oneirical <manchot@videotron.ca>
Date: Sun, 9 Jun 2024 16:43:24 -0400
Subject: [PATCH 4/4] make assert_stderr_contains print its contents on panic

---
 src/doc/book                             |  2 +-
 src/tools/cargo                          |  2 +-
 src/tools/run-make-support/src/rustc.rs  | 12 ------------
 tests/run-make/link-args-order/rmake.rs  | 19 +++++++++++--------
 tests/run-make/lto-readonly-lib/rmake.rs | 16 +++++-----------
 5 files changed, 18 insertions(+), 33 deletions(-)

diff --git a/src/doc/book b/src/doc/book
index 5228bfac8267a..45c1a6d69edfd 160000
--- a/src/doc/book
+++ b/src/doc/book
@@ -1 +1 @@
-Subproject commit 5228bfac8267ad24659a81b92ec5417976b5edbc
+Subproject commit 45c1a6d69edfd1fc91fb7504cb73958dbd09441e
diff --git a/src/tools/cargo b/src/tools/cargo
index 4dcbca118ab7f..a1f47ec3f7cd0 160000
--- a/src/tools/cargo
+++ b/src/tools/cargo
@@ -1 +1 @@
-Subproject commit 4dcbca118ab7f9ffac4728004c983754bc6a04ff
+Subproject commit a1f47ec3f7cd076986f1bfcd7061f2e8cb1a726e
diff --git a/src/tools/run-make-support/src/rustc.rs b/src/tools/run-make-support/src/rustc.rs
index 7e5a637e20e15..289d847a5728c 100644
--- a/src/tools/run-make-support/src/rustc.rs
+++ b/src/tools/run-make-support/src/rustc.rs
@@ -236,18 +236,6 @@ impl Rustc {
         self
     }
 
-    /// Add an extra argument to prepend the linker invocation, via `-Zpre-link-arg`.
-    pub fn pre_link_arg(&mut self, link_arg: &str) -> &mut Self {
-        self.cmd.arg(format!("-Zpre-link-arg={link_arg}"));
-        self
-    }
-
-    /// Add multiple extra arguments to the linker invocation, via `-Zpre-link-args`.
-    pub fn pre_link_args(&mut self, link_args: &str) -> &mut Self {
-        self.cmd.arg(format!("-Zpre-link-args={link_args}"));
-        self
-    }
-
     /// Specify a stdin input
     pub fn stdin<I: AsRef<[u8]>>(&mut self, input: I) -> &mut Self {
         self.cmd.set_stdin(input.as_ref().to_vec().into_boxed_slice());
diff --git a/tests/run-make/link-args-order/rmake.rs b/tests/run-make/link-args-order/rmake.rs
index 4530251c7a585..d238ad23f27c7 100644
--- a/tests/run-make/link-args-order/rmake.rs
+++ b/tests/run-make/link-args-order/rmake.rs
@@ -3,6 +3,9 @@
 // checks that linker arguments remain intact and in the order they were originally passed in.
 // See https://github.com/rust-lang/rust/pull/70665
 
+//@ ignore-msvc
+// Reason: the ld linker does not exist on Windows.
+
 use run_make_support::rustc;
 
 fn main() {
@@ -10,18 +13,18 @@ fn main() {
         .input("empty.rs")
         .linker_flavor("ld")
         .link_arg("a")
-        .link_args("\"b c\"")
-        .link_args("\"d e\"")
+        .link_args("b c")
+        .link_args("d e")
         .link_arg("f")
         .run_fail()
-        .assert_stderr_contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"");
+        .assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#);
     rustc()
         .input("empty.rs")
         .linker_flavor("ld")
-        .pre_link_arg("a")
-        .pre_link_args("\"b c\"")
-        .pre_link_args("\"d e\"")
-        .pre_link_arg("f")
+        .arg("-Zpre-link-arg=a")
+        .arg("-Zpre-link-args=b c")
+        .arg("-Zpre-link-args=d e")
+        .arg("-Zpre-link-arg=f")
         .run_fail()
-        .assert_stderr_contains("\"a\" \"b\" \"c\" \"d\" \"e\" \"f\"");
+        .assert_stderr_contains(r#""a" "b" "c" "d" "e" "f""#);
 }
diff --git a/tests/run-make/lto-readonly-lib/rmake.rs b/tests/run-make/lto-readonly-lib/rmake.rs
index bf1dafee697dc..9eb135addd9ec 100644
--- a/tests/run-make/lto-readonly-lib/rmake.rs
+++ b/tests/run-make/lto-readonly-lib/rmake.rs
@@ -8,18 +8,12 @@
 //@ ignore-cross-compile
 
 use run_make_support::fs_wrapper;
-use run_make_support::{cwd, run, rustc};
+use run_make_support::{run, rust_lib_name, rustc, test_while_readonly};
 
 fn main() {
     rustc().input("lib.rs").run();
-    let entries = fs_wrapper::read_dir(cwd());
-    for entry in entries {
-        if entry.path().extension().and_then(|s| s.to_str()) == Some("rlib") {
-            let mut perms = fs_wrapper::metadata(entry.path()).permissions();
-            perms.set_readonly(true);
-            fs_wrapper::set_permissions(entry.path(), perms);
-        }
-    }
-    rustc().input("main.rs").arg("-Clto").run();
-    run("main");
+    test_while_readonly(rust_lib_name("lib"), || {
+        rustc().input("main.rs").arg("-Clto").run();
+        run("main");
+    });
 }