about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-03-04 15:39:44 +0000
committerbors <bors@rust-lang.org>2025-03-04 15:39:44 +0000
commitf9e0239a7bc813b4aceffc7f069f4797cde3175c (patch)
treef128676be91c2dd790e76ba5570d648917ee56d1 /tests
parentfd17deacce374a4185c882795be162e17b557050 (diff)
parenta954c512809a2d6f3e592c0287dbe41264b54808 (diff)
downloadrust-f9e0239a7bc813b4aceffc7f069f4797cde3175c.tar.gz
rust-f9e0239a7bc813b4aceffc7f069f4797cde3175c.zip
Auto merge of #135695 - Noratrieb:elf-raw-dylib, r=bjorn3
Support raw-dylib link kind on ELF

raw-dylib is a link kind that allows rustc to link against a library without having any library files present.
This currently only exists on Windows. rustc will take all the symbols from raw-dylib link blocks and put them in an import library, where they can then be resolved by the linker.

While import libraries don't exist on ELF, it would still be convenient to have this same functionality. Not having the libraries present at build-time can be convenient for several reasons, especially cross-compilation. With raw-dylib, code linking against a library can be cross-compiled without needing to have these libraries available on the build machine. If the libc crate makes use of this, it would allow cross-compilation without having any libc available on the build machine. This is not yet possible with this implementation, at least against libc's like glibc that use symbol versioning. The raw-dylib kind could be extended with support for symbol versioning in the future.

This implementation is very experimental and I have not tested it very well. I have tested it for a toy example and the lz4-sys crate, where it was able to successfully link a binary despite not having a corresponding library at build-time.

I was inspired by Björn's comments in https://internals.rust-lang.org/t/bundle-zig-cc-in-rustup-by-default/22096/27
Tracking issue: #135694

r? bjorn3

try-job: aarch64-apple
try-job: x86_64-msvc-1
try-job: x86_64-msvc-2
try-job: test-various
Diffstat (limited to 'tests')
-rw-r--r--tests/run-make/linker-warning/rmake.rs2
-rw-r--r--tests/run-make/linker-warning/short-error.txt2
-rw-r--r--tests/run-make/raw-dylib-elf-verbatim-absolute/main.rs11
-rw-r--r--tests/run-make/raw-dylib-elf-verbatim-absolute/output.txt1
-rw-r--r--tests/run-make/raw-dylib-elf-verbatim-absolute/rmake.rs20
-rw-r--r--tests/run-make/raw-dylib-elf-verbatim/library.c3
-rw-r--r--tests/run-make/raw-dylib-elf-verbatim/main.rs11
-rw-r--r--tests/run-make/raw-dylib-elf-verbatim/output.txt1
-rw-r--r--tests/run-make/raw-dylib-elf-verbatim/rmake.rs31
-rw-r--r--tests/run-make/raw-dylib-elf/library.c3
-rw-r--r--tests/run-make/raw-dylib-elf/main.rs11
-rw-r--r--tests/run-make/raw-dylib-elf/output.txt1
-rw-r--r--tests/run-make/raw-dylib-elf/rmake.rs29
-rw-r--r--tests/run-make/reproducible-build/linker.rs2
-rw-r--r--tests/ui/feature-gates/feature-gate-raw-dylib-elf.rs9
-rw-r--r--tests/ui/feature-gates/feature-gate-raw-dylib-elf.stderr13
-rw-r--r--tests/ui/linkage-attr/raw-dylib/elf/multiple-libraries.rs37
-rw-r--r--tests/ui/linkage-attr/raw-dylib/elf/single-symbol.rs28
-rw-r--r--tests/ui/linkage-attr/raw-dylib/elf/verbatim.rs29
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/dlltool-failed.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/dlltool-failed.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-invalid-format.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-invalid-format.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-invalid-format.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-invalid-format.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-multiple.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-multiple.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-multiple.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-multiple.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unknown-value.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unknown-value.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unknown-value.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unknown-value.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unsupported-link-kind.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unsupported-link-kind.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unsupported-link-kind.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unsupported-link-kind.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-x86-only.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-x86-only.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/import-name-type-x86-only.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-x86-only.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/invalid-dlltool.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/invalid-dlltool.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/invalid-dlltool.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/invalid-dlltool.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-and-name.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-and-name.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-and-name.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-and-name.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-invalid-format.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-invalid-format.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-missing-argument.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-missing-argument.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-missing-argument.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-missing-argument.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-multiple.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-multiple.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-multiple.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-multiple.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-not-foreign-fn.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-not-foreign-fn.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-large.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-large.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-large.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-large.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-many-arguments.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-many-arguments.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-unsupported-link-kind.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-unsupported-link-kind.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-unsupported-link-kind.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-unsupported-link-kind.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/multiple-declarations.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/multiple-declarations.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/multiple-declarations.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/multiple-declarations.stderr)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.elf.stderr13
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.notelf.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/raw-dylib-windows-only.stderr)2
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.rs9
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/unsupported-abi.rs (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/unsupported-abi.rs)0
-rw-r--r--tests/ui/linkage-attr/raw-dylib/windows/unsupported-abi.stderr (renamed from tests/ui/rfcs/rfc-2627-raw-dylib/unsupported-abi.stderr)0
-rw-r--r--tests/ui/rfcs/rfc-2627-raw-dylib/raw-dylib-windows-only.rs5
57 files changed, 266 insertions, 7 deletions
diff --git a/tests/run-make/linker-warning/rmake.rs b/tests/run-make/linker-warning/rmake.rs
index 30387af428c..73ad248b6f3 100644
--- a/tests/run-make/linker-warning/rmake.rs
+++ b/tests/run-make/linker-warning/rmake.rs
@@ -60,6 +60,8 @@ fn main() {
                 regex::escape(run_make_support::build_root().to_str().unwrap()),
                 "/build-root",
             )
+            .normalize(r#""[^"]*\/symbols.o""#, "\"/symbols.o\"")
+            .normalize(r#""[^"]*\/raw-dylibs""#, "\"/raw-dylibs\"")
             .run();
     }
 
diff --git a/tests/run-make/linker-warning/short-error.txt b/tests/run-make/linker-warning/short-error.txt
index dd3b742bbfd..a7f48af885a 100644
--- a/tests/run-make/linker-warning/short-error.txt
+++ b/tests/run-make/linker-warning/short-error.txt
@@ -1,6 +1,6 @@
 error: linking with `./fake-linker` failed: exit status: 1
   |
-  = note:  "./fake-linker" "-m64" "/tmp/rustc/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,liballoc-*,librustc_std_workspace_core-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/build-root/test/run-make/linker-warning/rmake_out" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "run_make_error"
+  = note:  "./fake-linker" "-m64" "/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,liballoc-*,librustc_std_workspace_core-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/raw-dylibs" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/build-root/test/run-make/linker-warning/rmake_out" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "run_make_error"
   = note: some arguments are omitted. use `--verbose` to show all linker arguments
   = note: error: baz
           
diff --git a/tests/run-make/raw-dylib-elf-verbatim-absolute/main.rs b/tests/run-make/raw-dylib-elf-verbatim-absolute/main.rs
new file mode 100644
index 00000000000..75bd7747f4f
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf-verbatim-absolute/main.rs
@@ -0,0 +1,11 @@
+#![feature(raw_dylib_elf)]
+#![allow(incomplete_features)]
+
+#[link(name = "/absolute-path/liblibrary.so.1", kind = "raw-dylib", modifiers = "+verbatim")]
+unsafe extern "C" {
+    safe fn this_is_a_library_function() -> core::ffi::c_int;
+}
+
+fn main() {
+    println!("{}", this_is_a_library_function())
+}
diff --git a/tests/run-make/raw-dylib-elf-verbatim-absolute/output.txt b/tests/run-make/raw-dylib-elf-verbatim-absolute/output.txt
new file mode 100644
index 00000000000..d81cc0710eb
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf-verbatim-absolute/output.txt
@@ -0,0 +1 @@
+42
diff --git a/tests/run-make/raw-dylib-elf-verbatim-absolute/rmake.rs b/tests/run-make/raw-dylib-elf-verbatim-absolute/rmake.rs
new file mode 100644
index 00000000000..d2a9b0477e5
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf-verbatim-absolute/rmake.rs
@@ -0,0 +1,20 @@
+//@ only-elf
+//@ ignore-cross-compile: Runs a binary.
+//@ needs-dynamic-linking
+// FIXME(raw_dylib_elf): Debug the failures on other targets.
+//@ only-gnu
+//@ only-x86_64
+
+//! Ensure ELF raw-dylib is able to link against a non-existent verbatim absolute path
+//! by embedding the absolute path in the DT_SONAME and passing a different path for
+//! the linker for the stub.
+
+use run_make_support::{build_native_dynamic_lib, cwd, diff, rfs, run, rustc};
+
+fn main() {
+    // We compile the binary without having the library present.
+    // The verbatim library name is an absolute path.
+    rustc().crate_type("bin").input("main.rs").run();
+
+    // FIXME(raw_dylib_elf): Read the NEEDED of the library to ensure it's the absolute path.
+}
diff --git a/tests/run-make/raw-dylib-elf-verbatim/library.c b/tests/run-make/raw-dylib-elf-verbatim/library.c
new file mode 100644
index 00000000000..2e3a95b7ede
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf-verbatim/library.c
@@ -0,0 +1,3 @@
+int this_is_a_library_function() {
+    return 42;
+}
diff --git a/tests/run-make/raw-dylib-elf-verbatim/main.rs b/tests/run-make/raw-dylib-elf-verbatim/main.rs
new file mode 100644
index 00000000000..044b7400a84
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf-verbatim/main.rs
@@ -0,0 +1,11 @@
+#![feature(raw_dylib_elf)]
+#![allow(incomplete_features)]
+
+#[link(name = "liblibrary.so.1", kind = "raw-dylib", modifiers = "+verbatim")]
+unsafe extern "C" {
+    safe fn this_is_a_library_function() -> core::ffi::c_int;
+}
+
+fn main() {
+    println!("{}", this_is_a_library_function())
+}
diff --git a/tests/run-make/raw-dylib-elf-verbatim/output.txt b/tests/run-make/raw-dylib-elf-verbatim/output.txt
new file mode 100644
index 00000000000..d81cc0710eb
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf-verbatim/output.txt
@@ -0,0 +1 @@
+42
diff --git a/tests/run-make/raw-dylib-elf-verbatim/rmake.rs b/tests/run-make/raw-dylib-elf-verbatim/rmake.rs
new file mode 100644
index 00000000000..319534b24c8
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf-verbatim/rmake.rs
@@ -0,0 +1,31 @@
+//@ only-elf
+//@ ignore-cross-compile: Runs a binary.
+//@ needs-dynamic-linking
+// FIXME(raw_dylib_elf): Debug the failures on other targets.
+//@ only-gnu
+//@ only-x86_64
+
+//! Ensure ELF raw-dylib is able to link against the verbatim versioned library
+//! without it being present, and then be executed against this library.
+
+use run_make_support::{build_native_dynamic_lib, cwd, diff, rfs, run, rustc};
+
+fn main() {
+    // We compile the binary without having the library present.
+    // We also set the rpath to the current directory so we can pick up the library at runtime.
+    rustc()
+        .crate_type("bin")
+        .input("main.rs")
+        .arg(&format!("-Wl,-rpath={}", cwd().display()))
+        .run();
+
+    // Now, *after* building the binary, we build the library...
+    build_native_dynamic_lib("library");
+    // ... rename it to have the versioned library name...
+    rfs::rename("liblibrary.so", "liblibrary.so.1");
+
+    // ... and run with this library, ensuring it was linked correctly at runtime.
+    let output = run("main").stdout_utf8();
+
+    diff().expected_file("output.txt").actual_text("actual", output).run();
+}
diff --git a/tests/run-make/raw-dylib-elf/library.c b/tests/run-make/raw-dylib-elf/library.c
new file mode 100644
index 00000000000..2e3a95b7ede
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf/library.c
@@ -0,0 +1,3 @@
+int this_is_a_library_function() {
+    return 42;
+}
diff --git a/tests/run-make/raw-dylib-elf/main.rs b/tests/run-make/raw-dylib-elf/main.rs
new file mode 100644
index 00000000000..3be944d2951
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf/main.rs
@@ -0,0 +1,11 @@
+#![feature(raw_dylib_elf)]
+#![allow(incomplete_features)]
+
+#[link(name = "library", kind = "raw-dylib")]
+unsafe extern "C" {
+    safe fn this_is_a_library_function() -> core::ffi::c_int;
+}
+
+fn main() {
+    println!("{}", this_is_a_library_function())
+}
diff --git a/tests/run-make/raw-dylib-elf/output.txt b/tests/run-make/raw-dylib-elf/output.txt
new file mode 100644
index 00000000000..d81cc0710eb
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf/output.txt
@@ -0,0 +1 @@
+42
diff --git a/tests/run-make/raw-dylib-elf/rmake.rs b/tests/run-make/raw-dylib-elf/rmake.rs
new file mode 100644
index 00000000000..59f901ac1ed
--- /dev/null
+++ b/tests/run-make/raw-dylib-elf/rmake.rs
@@ -0,0 +1,29 @@
+//@ only-elf
+//@ ignore-cross-compile: Runs a binary.
+//@ needs-dynamic-linking
+// FIXME(raw_dylib_elf): Debug the failures on other targets.
+//@ only-gnu
+//@ only-x86_64
+
+//! Ensure ELF raw-dylib is able to link the binary without having the library present,
+//! and then successfully run against the real library.
+
+use run_make_support::{build_native_dynamic_lib, cwd, diff, run, rustc};
+
+fn main() {
+    // We compile the binary without having the library present.
+    // We also set the rpath to the current directory so we can pick up the library at runtime.
+    rustc()
+        .crate_type("bin")
+        .input("main.rs")
+        .arg(&format!("-Wl,-rpath={}", cwd().display()))
+        .run();
+
+    // Now, *after* building the binary, we build the library...
+    build_native_dynamic_lib("library");
+
+    // ... and run with this library, ensuring it was linked correctly at runtime.
+    let output = run("main").stdout_utf8();
+
+    diff().expected_file("output.txt").actual_text("actual", output).run();
+}
diff --git a/tests/run-make/reproducible-build/linker.rs b/tests/run-make/reproducible-build/linker.rs
index ab3b4049cc3..2f57d9f9be9 100644
--- a/tests/run-make/reproducible-build/linker.rs
+++ b/tests/run-make/reproducible-build/linker.rs
@@ -17,6 +17,8 @@ fn main() {
     for arg in env::args().skip(1) {
         let path = Path::new(&arg);
         if !path.is_file() {
+            // This directory is produced during linking in a temporary directory (ELF only).
+            let arg = if arg.ends_with("/raw-dylibs") { "/raw-dylibs" } else { &*arg };
             out.push_str(&arg);
             out.push_str("\n");
             continue;
diff --git a/tests/ui/feature-gates/feature-gate-raw-dylib-elf.rs b/tests/ui/feature-gates/feature-gate-raw-dylib-elf.rs
new file mode 100644
index 00000000000..ec49e53d9b4
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-raw-dylib-elf.rs
@@ -0,0 +1,9 @@
+//@ only-elf
+//@ needs-dynamic-linking
+
+#[link(name = "meow", kind = "raw-dylib")] //~ ERROR: link kind `raw-dylib` is unstable on ELF platforms
+unsafe extern "C" {
+  safe fn meowmeow();
+}
+
+fn main() {}
diff --git a/tests/ui/feature-gates/feature-gate-raw-dylib-elf.stderr b/tests/ui/feature-gates/feature-gate-raw-dylib-elf.stderr
new file mode 100644
index 00000000000..dfbf39f5a18
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-raw-dylib-elf.stderr
@@ -0,0 +1,13 @@
+error[E0658]: link kind `raw-dylib` is unstable on ELF platforms
+  --> $DIR/feature-gate-raw-dylib-elf.rs:4:30
+   |
+LL | #[link(name = "meow", kind = "raw-dylib")]
+   |                              ^^^^^^^^^^^
+   |
+   = note: see issue #135694 <https://github.com/rust-lang/rust/issues/135694> for more information
+   = help: add `#![feature(raw_dylib_elf)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/linkage-attr/raw-dylib/elf/multiple-libraries.rs b/tests/ui/linkage-attr/raw-dylib/elf/multiple-libraries.rs
new file mode 100644
index 00000000000..f4715ff2d3d
--- /dev/null
+++ b/tests/ui/linkage-attr/raw-dylib/elf/multiple-libraries.rs
@@ -0,0 +1,37 @@
+//@ only-elf
+//@ needs-dynamic-linking
+// FIXME(raw_dylib_elf): Debug the failures on other targets.
+//@ only-gnu
+//@ only-x86_64
+
+//@ revisions: with without
+
+//@ [without] build-fail
+//@ [without] regex-error-pattern:error: linking with `.*` failed
+//@ [without] dont-check-compiler-stderr
+
+//@ [with] build-pass
+
+//! Ensures that linking fails when there's an undefined symbol,
+//! and that it does succeed with raw-dylib.
+
+#![feature(raw_dylib_elf)]
+#![allow(incomplete_features)]
+
+#[cfg_attr(with, link(name = "rawdylibbutforcats", kind = "raw-dylib"))]
+#[cfg_attr(without, link(name = "rawdylibbutforcats"))]
+unsafe extern "C" {
+  safe fn meooooooooooooooow();
+}
+
+
+#[cfg_attr(with, link(name = "rawdylibbutfordogs", kind = "raw-dylib"))]
+#[cfg_attr(without, link(name = "rawdylibbutfordogs"))]
+unsafe extern "C" {
+  safe fn woooooooooooooooooof();
+}
+
+fn main() {
+  meooooooooooooooow();
+  woooooooooooooooooof();
+}
diff --git a/tests/ui/linkage-attr/raw-dylib/elf/single-symbol.rs b/tests/ui/linkage-attr/raw-dylib/elf/single-symbol.rs
new file mode 100644
index 00000000000..fe9c7884e54
--- /dev/null
+++ b/tests/ui/linkage-attr/raw-dylib/elf/single-symbol.rs
@@ -0,0 +1,28 @@
+//@ only-elf
+//@ needs-dynamic-linking
+// FIXME(raw_dylib_elf): Debug the failures on other targets.
+//@ only-gnu
+//@ only-x86_64
+//@ revisions: with without
+
+//@ [without] build-fail
+//@ [without] regex-error-pattern:error: linking with `.*` failed
+//@ [without] dont-check-compiler-stderr
+
+//@ [with] build-pass
+
+//! Ensures that linking fails when there's an undefined symbol,
+//! and that it does succeed with raw-dylib.
+
+#![feature(raw_dylib_elf)]
+#![allow(incomplete_features)]
+
+#[cfg_attr(with, link(name = "rawdylibbutforcats", kind = "raw-dylib"))]
+#[cfg_attr(without, link(name = "rawdylibbutforcats"))]
+unsafe extern "C" {
+  safe fn meooooooooooooooow();
+}
+
+fn main() {
+  meooooooooooooooow();
+}
diff --git a/tests/ui/linkage-attr/raw-dylib/elf/verbatim.rs b/tests/ui/linkage-attr/raw-dylib/elf/verbatim.rs
new file mode 100644
index 00000000000..72cba18d841
--- /dev/null
+++ b/tests/ui/linkage-attr/raw-dylib/elf/verbatim.rs
@@ -0,0 +1,29 @@
+//@ only-elf
+//@ needs-dynamic-linking
+// FIXME(raw_dylib_elf): Debug the failures on other targets.
+//@ only-gnu
+//@ only-x86_64
+
+//@ revisions: with without
+
+//@ [without] build-fail
+//@ [without] regex-error-pattern:error: linking with `.*` failed
+//@ [without] dont-check-compiler-stderr
+
+//@ [with] build-pass
+
+//! Ensures that linking fails when there's an undefined symbol,
+//! and that it does succeed with raw-dylib, but with verbatim.
+
+#![feature(raw_dylib_elf)]
+#![allow(incomplete_features)]
+
+#[cfg_attr(with, link(name = "rawdylibbutforcats", kind = "raw-dylib", modifiers = "+verbatim"))]
+#[cfg_attr(without, link(name = "rawdylibbutforcats", modifiers = "+verbatim"))]
+unsafe extern "C" {
+  safe fn meooooooooooooooow();
+}
+
+fn main() {
+  meooooooooooooooow();
+}
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/dlltool-failed.rs b/tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.rs
index e69a4537935..e69a4537935 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/dlltool-failed.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/dlltool-failed.stderr b/tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.stderr
index 90cca83d1c1..90cca83d1c1 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/dlltool-failed.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/dlltool-failed.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-invalid-format.rs b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-invalid-format.rs
index 50ad8a173ad..50ad8a173ad 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-invalid-format.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-invalid-format.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-invalid-format.stderr b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-invalid-format.stderr
index d2cf7a0ba1f..d2cf7a0ba1f 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-invalid-format.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-invalid-format.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-multiple.rs b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-multiple.rs
index cf456b9b261..cf456b9b261 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-multiple.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-multiple.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-multiple.stderr b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-multiple.stderr
index 8e65baf65df..8e65baf65df 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-multiple.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-multiple.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unknown-value.rs b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unknown-value.rs
index b3859ba1ce6..b3859ba1ce6 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unknown-value.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unknown-value.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unknown-value.stderr b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unknown-value.stderr
index 4b8b90eb6e2..4b8b90eb6e2 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unknown-value.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unknown-value.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unsupported-link-kind.rs b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unsupported-link-kind.rs
index 3ead5cb1fd7..3ead5cb1fd7 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unsupported-link-kind.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unsupported-link-kind.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unsupported-link-kind.stderr b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unsupported-link-kind.stderr
index 75cadc471c4..75cadc471c4 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-unsupported-link-kind.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-unsupported-link-kind.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-x86-only.rs b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-x86-only.rs
index ab0dcda64e6..ab0dcda64e6 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-x86-only.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-x86-only.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-x86-only.stderr b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-x86-only.stderr
index 757f1f7994e..757f1f7994e 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/import-name-type-x86-only.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/import-name-type-x86-only.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/invalid-dlltool.rs b/tests/ui/linkage-attr/raw-dylib/windows/invalid-dlltool.rs
index 057242246f0..057242246f0 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/invalid-dlltool.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/invalid-dlltool.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/invalid-dlltool.stderr b/tests/ui/linkage-attr/raw-dylib/windows/invalid-dlltool.stderr
index 4bbad9b30a7..4bbad9b30a7 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/invalid-dlltool.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/invalid-dlltool.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-and-name.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-and-name.rs
index b04c2facbcd..b04c2facbcd 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-and-name.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-and-name.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-and-name.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-and-name.stderr
index f1e54d37827..f1e54d37827 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-and-name.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-and-name.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-invalid-format.rs
index 9b7e8d70743..9b7e8d70743 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-invalid-format.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-invalid-format.stderr
index 6341e57a0be..6341e57a0be 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-invalid-format.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-missing-argument.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-missing-argument.rs
index 6b8cd49566d..6b8cd49566d 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-missing-argument.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-missing-argument.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-missing-argument.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-missing-argument.stderr
index 1b04bb228e7..1b04bb228e7 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-missing-argument.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-missing-argument.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-multiple.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-multiple.rs
index f5fb1649cdc..f5fb1649cdc 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-multiple.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-multiple.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-multiple.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-multiple.stderr
index 2e6cf3761c2..2e6cf3761c2 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-multiple.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-multiple.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-not-foreign-fn.rs
index 5982c771033..5982c771033 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-not-foreign-fn.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-not-foreign-fn.stderr
index 8f279508720..8f279508720 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-not-foreign-fn.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-not-foreign-fn.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-large.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-large.rs
index 9d741630fc9..9d741630fc9 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-large.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-large.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-large.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-large.stderr
index 811145e77ee..811145e77ee 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-large.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-large.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-many-arguments.rs
index 9988115fd8b..9988115fd8b 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-many-arguments.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-many-arguments.stderr
index d5ce8aff34f..d5ce8aff34f 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-too-many-arguments.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-unsupported-link-kind.rs b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-unsupported-link-kind.rs
index 14e915d602a..14e915d602a 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-unsupported-link-kind.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-unsupported-link-kind.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-unsupported-link-kind.stderr b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-unsupported-link-kind.stderr
index 200b8f62874..200b8f62874 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/link-ordinal-unsupported-link-kind.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/link-ordinal-unsupported-link-kind.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/multiple-declarations.rs b/tests/ui/linkage-attr/raw-dylib/windows/multiple-declarations.rs
index bf3c5e4d435..bf3c5e4d435 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/multiple-declarations.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/multiple-declarations.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/multiple-declarations.stderr b/tests/ui/linkage-attr/raw-dylib/windows/multiple-declarations.stderr
index b766b5c8dd8..b766b5c8dd8 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/multiple-declarations.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/multiple-declarations.stderr
diff --git a/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.elf.stderr b/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.elf.stderr
new file mode 100644
index 00000000000..70945ed6fc0
--- /dev/null
+++ b/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.elf.stderr
@@ -0,0 +1,13 @@
+error[E0658]: link kind `raw-dylib` is unstable on ELF platforms
+  --> $DIR/raw-dylib-windows-only.rs:6:29
+   |
+LL | #[link(name = "foo", kind = "raw-dylib")]
+   |                             ^^^^^^^^^^^
+   |
+   = note: see issue #135694 <https://github.com/rust-lang/rust/issues/135694> for more information
+   = help: add `#![feature(raw_dylib_elf)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0658`.
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/raw-dylib-windows-only.stderr b/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.notelf.stderr
index ede20cb8c3f..daed15d784a 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/raw-dylib-windows-only.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.notelf.stderr
@@ -1,5 +1,5 @@
 error[E0455]: link kind `raw-dylib` is only supported on Windows targets
-  --> $DIR/raw-dylib-windows-only.rs:3:29
+  --> $DIR/raw-dylib-windows-only.rs:6:29
    |
 LL | #[link(name = "foo", kind = "raw-dylib")]
    |                             ^^^^^^^^^^^
diff --git a/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.rs b/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.rs
new file mode 100644
index 00000000000..935c59b5aaa
--- /dev/null
+++ b/tests/ui/linkage-attr/raw-dylib/windows/raw-dylib-windows-only.rs
@@ -0,0 +1,9 @@
+//@ revisions: elf notelf
+//@ [elf] only-elf
+//@ [notelf] ignore-windows
+//@ [notelf] ignore-elf
+//@ compile-flags: --crate-type lib
+#[link(name = "foo", kind = "raw-dylib")]
+//[notelf]~^ ERROR: link kind `raw-dylib` is only supported on Windows targets
+//[elf]~^^ ERROR: link kind `raw-dylib` is unstable on ELF platforms
+extern "C" {}
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/unsupported-abi.rs b/tests/ui/linkage-attr/raw-dylib/windows/unsupported-abi.rs
index 48af6b009d3..48af6b009d3 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/unsupported-abi.rs
+++ b/tests/ui/linkage-attr/raw-dylib/windows/unsupported-abi.rs
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/unsupported-abi.stderr b/tests/ui/linkage-attr/raw-dylib/windows/unsupported-abi.stderr
index ef022404e7f..ef022404e7f 100644
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/unsupported-abi.stderr
+++ b/tests/ui/linkage-attr/raw-dylib/windows/unsupported-abi.stderr
diff --git a/tests/ui/rfcs/rfc-2627-raw-dylib/raw-dylib-windows-only.rs b/tests/ui/rfcs/rfc-2627-raw-dylib/raw-dylib-windows-only.rs
deleted file mode 100644
index 3b982857db8..00000000000
--- a/tests/ui/rfcs/rfc-2627-raw-dylib/raw-dylib-windows-only.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-//@ ignore-windows
-//@ compile-flags: --crate-type lib
-#[link(name = "foo", kind = "raw-dylib")]
-//~^ ERROR: link kind `raw-dylib` is only supported on Windows targets
-extern "C" {}