rust/tests/ui/attributes
bors c51b9b6d52 Auto merge of #133832 - madsmtm:apple-symbols.o, r=DianQK
Make `#[used]` work when linking with `ld64`

To make `#[used]` work in static libraries, we use the `symbols.o` trick introduced in https://github.com/rust-lang/rust/pull/95604.

However, the linker shipped with Xcode, ld64, works a bit differently from other linkers; in particular, [it completely ignores undefined symbols by themselves](https://github.com/apple-oss-distributions/ld64/blob/ld64-954.16/src/ld/parsers/macho_relocatable_file.cpp#L2455-L2468), and only consider them if they have relocations (something something atoms something fixups, I don't know the details).

So to make the `symbols.o` file work on ld64, we need to actually insert a relocation. That's kinda cumbersome to do though, since the relocation must be valid, and hence must point to a valid piece of machine code, and is hence very architecture-specific.

Fixes https://github.com/rust-lang/rust/issues/133491, see that for investigation.

---

Another option would be to pass `-u _foo` to the final linker invocation. This has the problem that `-u` causes the linker to not be able to dead-strip the symbol, which is undesirable. (If we did this, we would possibly also want to do it by putting the arguments in a file by itself, and passing that file via ``@`,` e.g. ``@undefined_symbols.txt`,` similar to https://github.com/rust-lang/rust/issues/52699, though that [is only supported since Xcode 12](https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes#Linking), and I'm not sure we wanna bump that).

Various other options that are probably all undesirable as they affect link time performance:
- Pass `-all_load` to the linker.
- Pass `-ObjC` to the linker (the Objective-C support in the linker has different code paths that load more of the binary), and instrument the binaries that contain `#[used]` symbols.
- Pass `-force_load` to libraries that contain `#[used]` symbols.

Failed attempt: Embed `-u _foo` in the object file with `LC_LINKER_OPTION`, akin to https://github.com/rust-lang/rust/issues/121293. Doesn't work, both because `ld64` doesn't read that from archive members unless it already has a reason to load the member (which is what this PR is trying to make it do), and because `ld64` only support the `-l`, `-needed-l`, `-framework` and `-needed_framework` flags in there.

---

TODO:
- [x] Support all Apple architectures.
- [x] Ensure that this works regardless of the actual type of the symbol.
- [x] Write up more docs.
- [x] Wire up a few proper tests.

`@rustbot` label O-apple
2025-02-25 11:59:11 +00:00
..
auxiliary Make #[used] work when linking with ld64 2025-02-24 04:04:59 +01:00
inline Adjust tests/ui/attr-usage-inline.rs 2024-12-17 19:57:42 +08:00
main-removed-2 Update tests to use new proc-macro header 2024-11-27 07:18:25 -08:00
unsafe Stabilize the 2024 edition 2024-11-22 11:12:15 -08:00
arg-error-issue-121425.rs fix spans of arguments in diagnostic 2024-03-03 10:48:40 +08:00
arg-error-issue-121425.stderr Fix test output expectations 2025-02-24 14:31:19 +01:00
assoc-expr.rs Overhaul token collection. 2024-08-16 09:07:55 +10:00
attr-bad-crate-attr.rs Adjust tests/ui/attr-bad-crate-attr.rs 2024-12-17 19:18:30 +08:00
attr-bad-crate-attr.stderr Adjust tests/ui/attr-bad-crate-attr.rs 2024-12-17 19:18:30 +08:00
attr-before-view-item.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
attr-before-view-item2.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
attr-eq-token-tree.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
attr-eq-token-tree.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
attr-mix-new.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
attr-shebang.rs Adjust tests/ui/attr-shebang.rs 2024-12-17 19:22:21 +08:00
attrs-on-params.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
attrs-on-params.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
attrs-with-no-formal-in-generics-1.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
attrs-with-no-formal-in-generics-1.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
attrs-with-no-formal-in-generics-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
attrs-with-no-formal-in-generics-2.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
attrs-with-no-formal-in-generics-3.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
attrs-with-no-formal-in-generics-3.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
check-builtin-attr-ice.rs tests: add regression test for incorrect "builtin attribute is checked" assertion ICE 2024-08-04 01:50:55 +00:00
check-builtin-attr-ice.stderr Reword "crate not found" resolve message 2025-01-24 01:19:50 +00:00
check-cfg_attr-ice.rs tests: add regression test to make sure cfg_attr isn't considered unhandled 2024-08-06 06:12:10 +00:00
check-cfg_attr-ice.stderr Reword "crate not found" resolve message 2025-01-24 01:19:50 +00:00
class-attributes-1.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
class-attributes-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
collapse-debuginfo-invalid.rs debuginfo: Stabilize -Z debug-macros, -Z collapse-macro-debuginfo and #[collapse_debuginfo] 2024-04-25 22:14:47 +03:00
collapse-debuginfo-invalid.stderr Filter empty lines, comments and delimiters from previous to last multiline span rendering 2024-12-12 23:36:27 +00:00
const-stability-on-macro.rs Update since stability attributes in tests 2023-10-23 13:04:47 -07:00
const-stability-on-macro.stderr Update since stability attributes in tests 2023-10-23 13:04:47 -07:00
crate-name-macro-call.rs Reject macro calls inside of #![crate_name] 2025-02-15 16:47:30 +01:00
crate-name-macro-call.stderr Reject macro calls inside of #![crate_name] 2025-02-15 16:47:30 +01:00
crate-type-delimited.rs Move #![crate_type] UI tests into attributes directory 2025-02-15 03:41:07 +01:00
crate-type-delimited.stderr Move #![crate_type] UI tests into attributes directory 2025-02-15 03:41:07 +01:00
crate-type-empty.rs Move #![crate_type] UI tests into attributes directory 2025-02-15 03:41:07 +01:00
crate-type-empty.stderr Move #![crate_type] UI tests into attributes directory 2025-02-15 03:41:07 +01:00
crate-type-macro-call.rs Move #![crate_type] UI tests into attributes directory 2025-02-15 03:41:07 +01:00
crate-type-macro-call.stderr Move #![crate_type] UI tests into attributes directory 2025-02-15 03:41:07 +01:00
doc-attr.rs Update ui tests 2024-02-29 14:43:43 +01:00
doc-attr.stderr Update ui tests 2024-02-29 14:43:43 +01:00
doc-test-literal.rs Update ui tests 2024-02-29 14:43:43 +01:00
doc-test-literal.stderr Update ui tests 2024-02-29 14:43:43 +01:00
dump-preds.rs Remove the -test suffix from normalize directives 2024-12-27 19:58:16 +11:00
dump-preds.stderr rustc_type_ir: derivative -> derive-where 2024-07-12 21:52:04 +03:00
dump_def_parents.rs Remove the -test suffix from normalize directives 2024-12-27 19:58:16 +11:00
dump_def_parents.stderr Filter empty lines, comments and delimiters from previous to last multiline span rendering 2024-12-12 23:36:27 +00:00
duplicated-attributes.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
duplicated-attributes.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
extented-attribute-macro-error.rs Remove the -test suffix from normalize directives 2024-12-27 19:58:16 +11:00
extented-attribute-macro-error.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
field-attributes-vis-unresolved.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
field-attributes-vis-unresolved.stderr Reword "crate not found" resolve message 2025-01-24 01:19:50 +00:00
invalid-repr.rs add test for invalid places of repr align 2023-04-17 12:42:02 +00:00
invalid-repr.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
invalid_macro_export_argument.deny.stderr Add invalid_macro_export_arguments to built-in macro list 2023-04-27 18:33:39 +00:00
invalid_macro_export_argument.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-40962.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-90873.rs Improve an error involving attribute values. 2023-12-12 15:54:25 +11:00
issue-90873.stderr Make parse error suggestions verbose and fix spans 2024-07-12 03:02:57 +00:00
issue-100631.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-100631.stderr Fix test output expectations 2025-02-24 14:31:19 +01:00
issue-105594-invalid-attr-validation.rs remove #[cmse_nonsecure_entry] 2024-09-21 13:05:21 +02:00
issue-105594-invalid-attr-validation.stderr remove #[cmse_nonsecure_entry] 2024-09-21 13:05:21 +02:00
issue-115264-expr-field.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
issue-115264-pat-field.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
item-attributes.rs Remove libc from rust_get_test_int uses 2024-04-17 09:18:14 -04:00
key-value-expansion-on-mac.rs Improve an error involving attribute values. 2023-12-12 15:54:25 +11:00
key-value-expansion-on-mac.stderr Improve an error involving attribute values. 2023-12-12 15:54:25 +11:00
key-value-expansion-scope-pass.rs resolve: Implement a lint for out-of-scope use of macro_rules 2024-06-24 17:12:08 +03:00
key-value-expansion-scope.rs Specify scope in out_of_scope_macro_calls lint 2025-02-19 18:29:00 +00:00
key-value-expansion-scope.stderr Reword message 2025-02-20 17:55:31 +00:00
key-value-expansion.rs Update tests. 2025-01-07 16:04:14 +01:00
key-value-expansion.stderr Improve an error involving attribute values. 2023-12-12 15:54:25 +11:00
key-value-non-ascii.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
key-value-non-ascii.stderr Show diff suggestion format on verbose replacement 2025-02-10 20:21:39 +00:00
linkage.rs Emit an error for invalid use of the linkage attribute 2024-08-17 15:03:20 +09:00
linkage.stderr Emit an error for invalid use of the linkage attribute 2024-08-17 15:03:20 +09:00
log-backtrace.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
macro_export_on_decl_macro.rs check_attrs: Warn when #[macro_export] is used on macros 2.0 2023-08-07 21:14:28 +02:00
macro_export_on_decl_macro.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
main-removed-1.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
main-removed-1.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
malformed-fn-align.rs add test to verify that #132391 can be closed 2025-02-24 14:31:19 +01:00
malformed-fn-align.stderr add test to verify that #132391 can be closed 2025-02-24 14:31:19 +01:00
may_dangle.rs Check that #[may_dangle] is properly applied 2024-08-18 20:32:00 +03:00
may_dangle.stderr Check that #[may_dangle] is properly applied 2024-08-18 20:32:00 +03:00
method-attributes.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
mixed_export_name_and_no_mangle.fixed Fix test output expectations 2025-02-24 14:31:19 +01:00
mixed_export_name_and_no_mangle.rs Fix test output expectations 2025-02-24 14:31:19 +01:00
mixed_export_name_and_no_mangle.stderr Fix test output expectations 2025-02-24 14:31:19 +01:00
multiple-invalid.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
multiple-invalid.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
no-mangle-closure.rs Skip no_mangle if the item has no name. 2023-03-28 18:05:12 +00:00
no-mangle-closure.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
no-sanitize.rs Allow disabling ASan instrumentation for globals 2024-11-02 22:35:34 +01:00
no-sanitize.stderr Allow disabling ASan instrumentation for globals 2024-11-02 22:35:34 +01:00
nonterminal-expansion.rs Fix test output expectations 2025-02-24 14:31:19 +01:00
nonterminal-expansion.stderr Fix test output expectations 2025-02-24 14:31:19 +01:00
obsolete-attr.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
obsolete-attr.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
optimize.rs misapplied optimize attribute throws a compilation error (#128488) 2024-10-20 08:34:15 -06:00
optimize.stderr misapplied optimize attribute throws a compilation error (#128488) 2024-10-20 08:34:15 -06:00
repr-align-in-trait-issue-132391.rs Fix test output expectations 2025-02-24 14:31:19 +01:00
repr-align-in-trait-issue-132391.stderr Fix test output expectations 2025-02-24 14:31:19 +01:00
rustc_confusables.rs Deduplicate some logic and reword output 2024-02-22 18:05:28 +00:00
rustc_confusables.stderr Fix test output expectations 2025-02-24 14:31:19 +01:00
rustc_confusables_std_cases.rs Add get_line confusable to Stdin::read_line() 2024-10-02 23:19:26 -05:00
rustc_confusables_std_cases.stderr Trim suggestion parts to the subset that is purely additive 2025-02-14 00:44:10 -08:00
rustc_pub_transparent.rs New #[rustc_pub_transparent] attribute 2024-08-24 23:05:37 +03:00
rustc_pub_transparent.stderr New #[rustc_pub_transparent] attribute 2024-08-24 23:05:37 +03:00
statement-attribute-validation.rs Add regression test for #117058 2023-10-24 08:21:52 +00:00
statement-attribute-validation.stderr Add regression test for #117058 2023-10-24 08:21:52 +00:00
suffixed-literal-meta.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
suffixed-literal-meta.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
tool_attributes.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unknown-attr.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unknown-attr.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unnamed-field-attributes-dup.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unnamed-field-attributes-vis.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unnamed-field-attributes.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unrestricted-attribute-tokens.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
unused-item-in-attr.rs Improve an error involving attribute values. 2023-12-12 15:54:25 +11:00
unused-item-in-attr.stderr Improve an error involving attribute values. 2023-12-12 15:54:25 +11:00
used-issue-126789.rs Show used attribute's kind for user when find it isn't applied to a static variable. 2024-06-29 19:39:09 +08:00
used-issue-126789.stderr Show used attribute's kind for user when find it isn't applied to a static variable. 2024-06-29 19:39:09 +08:00
used_with_archive.rs Make #[used] work when linking with ld64 2025-02-24 04:04:59 +01:00
used_with_archive.run.stdout Make #[used] work when linking with ld64 2025-02-24 04:04:59 +01:00
used_with_arg.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
used_with_arg.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
used_with_arg_no_mangle.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
used_with_multi_args.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
used_with_multi_args.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
validation-on-associated-items-issue-121537.rs fix attribute validation on associated items in traits 2024-02-24 08:14:38 -05:00
validation-on-associated-items-issue-121537.stderr fix attribute validation on associated items in traits 2024-02-24 08:14:38 -05:00
variant-attributes.rs tests: remove //@ pretty-expanded usages 2024-11-26 02:50:48 +08:00
z-crate-attr.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00