rust/compiler
Jonathan Brouwer a1d588b35e
Rollup merge of #150992 - cezarbbb:cstyle-export-rules2, r=bjorn3,petrochenkov
link modifier `export-symbols`: export all global symbols from selected uptream c static libraries

In order to be able to export symbols from a specified upstream C static library, I redesigned a solution that, compared to a previous PR rust-lang/rust#150335 I submitted, will not have any extra symbols leaking out.

The following points should be noted:
 - This attribute will select and import the `Global` symbols of the first matching library it finds.
 - Developers should ensure that there are no libraries with the same name.
 - This modifier is only compatible with `static` linking kind
 - By default, upstream C static libraries will not export their `Global` symbols regardless of whether `LTO` optimization is enabled. However, after enabling this attribute, if the upstream C static library has `LTO` optimization enabled, the compiler will issue an error to inform the developer that the linked C library is invalid.

The test code is the same as the PR rust-lang/rust#150335.
Here are the results:
1. `cargo +include-libs rustc --release -- -L. -lstatic:+export-symbols=c_add`
(or you can use `#[link(name = "c_add", kind= "static", modifier = "+export-symbols")]` in the file)
```bash
                 U abort@GLIBC_2.2.5
                 U bcmp@GLIBC_2.2.5
0000000000014f60 T c_add
                 U calloc@GLIBC_2.2.5
                 U close@GLIBC_2.2.5
0000000000014f70 T c_sub
                 w __cxa_finalize@GLIBC_2.2.5
                 w __cxa_thread_atexit_impl@GLIBC_2.18
                 U dl_iterate_phdr@GLIBC_2.2.5
0000000000014ee0 T downstream_add
                 U __errno_location@GLIBC_2.2.5
                 U free@GLIBC_2.2.5
                 U fstat64@GLIBC_2.33
                 U getcwd@GLIBC_2.2.5
                 U getenv@GLIBC_2.2.5
                 w __gmon_start__
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U lseek64@GLIBC_2.2.5
                 U malloc@GLIBC_2.2.5
                 U memcpy@GLIBC_2.14
                 U memmove@GLIBC_2.2.5
                 U memset@GLIBC_2.2.5
                 U mmap64@GLIBC_2.2.5
                 U munmap@GLIBC_2.2.5
                 U open64@GLIBC_2.2.5
                 U posix_memalign@GLIBC_2.2.5
                 U pthread_key_create@GLIBC_2.34
                 U pthread_key_delete@GLIBC_2.34
                 U pthread_setspecific@GLIBC_2.34
                 U read@GLIBC_2.2.5
                 U readlink@GLIBC_2.2.5
                 U realloc@GLIBC_2.2.5
                 U realpath@GLIBC_2.3
                 U stat64@GLIBC_2.33
                 w statx@GLIBC_2.28
                 U strlen@GLIBC_2.2.5
                 U syscall@GLIBC_2.2.5
                 U __tls_get_addr@GLIBC_2.3
                 U _Unwind_Backtrace@GCC_3.3
                 U _Unwind_DeleteException@GCC_3.0
                 U _Unwind_GetDataRelBase@GCC_3.0
                 U _Unwind_GetIP@GCC_3.0
                 U _Unwind_GetIPInfo@GCC_4.2.0
                 U _Unwind_GetLanguageSpecificData@GCC_3.0
                 U _Unwind_GetRegionStart@GCC_3.0
                 U _Unwind_GetTextRelBase@GCC_3.0
                 U _Unwind_RaiseException@GCC_3.0
                 U _Unwind_Resume@GCC_3.0
                 U _Unwind_SetGR@GCC_3.0
                 U _Unwind_SetIP@GCC_3.0
                 U write@GLIBC_2.2.5
                 U writev@GLIBC_2.2.5
```
3. `cargo +nightly rustc --release -- -L ./`
```bash
                 U abort@GLIBC_2.2.5
                 U bcmp@GLIBC_2.2.5
                 U calloc@GLIBC_2.2.5
                 U close@GLIBC_2.2.5
                 w __cxa_finalize@GLIBC_2.2.5
                 w __cxa_thread_atexit_impl@GLIBC_2.18
                 U dl_iterate_phdr@GLIBC_2.2.5
0000000000011e10 T downstream_add
                 U __errno_location@GLIBC_2.2.5
                 U free@GLIBC_2.2.5
                 U fstat64@GLIBC_2.33
                 U getcwd@GLIBC_2.2.5
                 U getenv@GLIBC_2.2.5
                 w gettid@GLIBC_2.30
                 w __gmon_start__
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U lseek64@GLIBC_2.2.5
                 U malloc@GLIBC_2.2.5
                 U memcpy@GLIBC_2.14
                 U memmove@GLIBC_2.2.5
                 U memset@GLIBC_2.2.5
                 U mmap64@GLIBC_2.2.5
                 U munmap@GLIBC_2.2.5
                 U open64@GLIBC_2.2.5
                 U posix_memalign@GLIBC_2.2.5
                 U pthread_key_create@GLIBC_2.34
                 U pthread_key_delete@GLIBC_2.34
                 U pthread_setspecific@GLIBC_2.34
                 U read@GLIBC_2.2.5
                 U readlink@GLIBC_2.2.5
                 U realloc@GLIBC_2.2.5
                 U realpath@GLIBC_2.3
                 U stat64@GLIBC_2.33
                 w statx@GLIBC_2.28
                 U strlen@GLIBC_2.2.5
                 U syscall@GLIBC_2.2.5
                 U __tls_get_addr@GLIBC_2.3
                 U _Unwind_Backtrace@GCC_3.3
                 U _Unwind_GetDataRelBase@GCC_3.0
                 U _Unwind_GetIP@GCC_3.0
                 U _Unwind_GetIPInfo@GCC_4.2.0
                 U _Unwind_GetLanguageSpecificData@GCC_3.0
                 U _Unwind_GetRegionStart@GCC_3.0
                 U _Unwind_GetTextRelBase@GCC_3.0
                 U _Unwind_RaiseException@GCC_3.0
                 U _Unwind_Resume@GCC_3.0
                 U _Unwind_SetGR@GCC_3.0
                 U _Unwind_SetIP@GCC_3.0
                 U write@GLIBC_2.2.5
                 U writev@GLIBC_2.2.5
```

r? @bjorn3
2026-02-04 14:39:17 +01:00
..
rustc Auto merge of #148925 - madsmtm:jemalloc-perf, r=Kobzol 2025-11-23 20:34:07 +00:00
rustc_abi Auto merge of #151065 - nagisa:add-preserve-none-abi, r=petrochenkov 2026-01-25 02:49:32 +00:00
rustc_arena Remove unused method DroplessArena::contains_slice 2026-01-31 22:56:49 +11:00
rustc_ast Handle unbalanced delimiters gracefully in make_attr_token_stream 2026-02-01 13:16:32 +00:00
rustc_ast_ir Implement &pin patterns and ref pin bindings 2025-11-10 09:57:08 +08:00
rustc_ast_lowering Rollup merge of #147996 - pmur:murp/stabilize-ppc-inlineasm, r=Amanieu 2026-01-27 12:50:49 +11:00
rustc_ast_passes Auto merge of #151065 - nagisa:add-preserve-none-abi, r=petrochenkov 2026-01-25 02:49:32 +00:00
rustc_ast_pretty Fix missing syntax context in lifetime hygiene debug output 2026-01-29 12:07:01 +00:00
rustc_attr_parsing Rollup merge of #150992 - cezarbbb:cstyle-export-rules2, r=bjorn3,petrochenkov 2026-02-04 14:39:17 +01:00
rustc_baked_icu_data Unify the configuration of the compiler docs 2025-11-05 11:25:27 +00:00
rustc_borrowck Rollup merge of #151172 - estebank:default-field-values, r=dianne 2026-02-02 18:52:13 +01:00
rustc_builtin_macros Use rustc_parse's Recovery instead of a boolean 2026-02-02 15:01:32 +01:00
rustc_codegen_cranelift Auto merge of #151065 - nagisa:add-preserve-none-abi, r=petrochenkov 2026-01-25 02:49:32 +00:00
rustc_codegen_gcc Omit standard copyright notice 2026-01-26 17:31:34 +00:00
rustc_codegen_llvm Rollup merge of #150893 - ZuseZ4:move-un-register-lib, r=oli-obk 2026-01-28 19:03:51 +11:00
rustc_codegen_ssa link modifier export-symbols: export all global symbols from selected uptream c static libraries 2026-02-04 09:26:21 +08:00
rustc_const_eval Rollup merge of #148967 - RalfJung:const-eval-preserve-src-padding, r=JonathanBrouwer,traviscross 2026-02-03 23:29:55 +01:00
rustc_data_structures Auto merge of #151929 - camsteffen:lengg, r=BoxyUwU 2026-02-03 22:20:57 +00:00
rustc_driver Unify the configuration of the compiler docs 2025-11-05 11:25:27 +00:00
rustc_driver_impl Convert to inline diagnostics in rustc_interface 2026-02-04 08:10:10 +01:00
rustc_error_codes Fix tidy formatting manually for E0423.md 2026-01-31 10:46:58 +05:30
rustc_error_messages Add inline syntax for diagnostic messages 2026-02-01 11:11:59 +01:00
rustc_errors Introduce inline_fluent macro 2026-02-03 10:19:57 +01:00
rustc_expand Auto merge of #151929 - camsteffen:lengg, r=BoxyUwU 2026-02-03 22:20:57 +00:00
rustc_feature fix issues and ui tests, address reviews 2026-01-30 15:59:34 +00:00
rustc_fluent_macro Bump stage0 2026-01-21 20:03:56 -05:00
rustc_fs_util Avoid using env::temp when linking a binary 2025-12-24 06:41:42 +00:00
rustc_graphviz change non-canonical clone impl to {*self}, fix some doc comments 2025-12-20 13:46:22 +00:00
rustc_hashes some cleanups in compiler 2025-10-12 08:08:30 +00:00
rustc_hir link modifier export-symbols: export all global symbols from selected uptream c static libraries 2026-02-04 09:26:21 +08:00
rustc_hir_analysis Rollup merge of #149263 - frank-king:feature/unpin-check, r=BoxyUwU 2026-02-03 18:54:47 +01:00
rustc_hir_id
rustc_hir_pretty Introduce hir::ConstArgKind::Array 2026-01-10 12:41:50 +09:00
rustc_hir_typeck Rollup merge of #151874 - khyperia:error-unsized-anonconst, r=BoxyUwU 2026-02-03 18:54:49 +01:00
rustc_incremental Convert to inline diagnostics in rustc_incremental 2026-02-03 23:22:48 +01:00
rustc_index simplify words initialization using Rc::new_zeroed 2026-01-16 04:25:12 +05:30
rustc_index_macros
rustc_infer Convert to inline diagnostics in rustc_infer 2026-02-03 15:14:49 +01:00
rustc_interface Rollup merge of #150992 - cezarbbb:cstyle-export-rules2, r=bjorn3,petrochenkov 2026-02-04 14:39:17 +01:00
rustc_lexer lexer/parser: ensure deps use the same unicode version 2025-12-27 11:20:42 +01:00
rustc_lint Rollup merge of #151886 - chenyukang:yukang-fix-151846-unused-allocation-box-ref, r=petrochenkov 2026-01-31 21:42:45 +01:00
rustc_lint_defs resolve: Report more early resolution ambiguities for imports 2026-02-02 14:44:13 +03:00
rustc_llvm Rollup merge of #150780 - fzakaria:fzakaria/section-threshold, r=jackh726 2026-01-23 11:07:55 +01:00
rustc_log Restrict sysroot crate imports to those defined in this repo. 2025-10-15 13:17:25 +01:00
rustc_macros Auto merge of #152089 - JonathanBrouwer:rollup-h74gaTC, r=JonathanBrouwer 2026-02-04 08:37:48 +00:00
rustc_metadata Rollup merge of #150992 - cezarbbb:cstyle-export-rules2, r=bjorn3,petrochenkov 2026-02-04 14:39:17 +01:00
rustc_middle Remove imports in queries only used by define_callbacks! 2026-02-03 21:33:16 +01:00
rustc_mir_build refactor: add an enum DerefAdjustKind in favor of Option<OverloadedDeref> 2026-01-31 16:25:32 +08:00
rustc_mir_dataflow Convert to inline diagnostics in rustc_mir_dataflow 2026-02-03 23:25:59 +01:00
rustc_mir_transform coverage: Add a test case for a previously-unknown span mismatch 2026-02-03 22:53:38 +11:00
rustc_monomorphize Rollup merge of #151441 - Keith-Cancel:mgca3, r=BoxyUwU 2026-01-22 13:35:42 +01:00
rustc_next_trait_solver Part 2 refactoring of moving placeholder types to rustc_type_ir 2026-01-29 11:11:40 +00:00
rustc_parse Rollup merge of #151102 - zachs18:mut-ref-in-struct-pattern-shorthand-gate, r=JonathanBrouwer 2026-01-31 21:42:46 +01:00
rustc_parse_format Rollup merge of #151013 - ehuss:fmt-clarification, r=joboet 2026-01-28 19:03:51 +11:00
rustc_passes Rollup merge of #152054 - lapla-cogito:diag_const_trait_impl, r=estebank 2026-02-03 23:29:57 +01:00
rustc_pattern_analysis refactor: remove Ty::pinned_ref in favor of Ty::maybe_pinned_ref 2026-01-30 14:22:41 +08:00
rustc_privacy Auto merge of #151168 - petrochenkov:rprivtit, r=eholk 2026-01-30 01:45:52 +00:00
rustc_proc_macro Update literal-escaper version to 0.0.7 2026-01-08 14:10:33 +01:00
rustc_public Rollup merge of #150271 - Jamesbarford:chore/refactor-struct-placeholder-pt2, r=lcnr 2026-01-29 17:47:29 +01:00
rustc_public_bridge Overhaul filename handling for cross-compiler consistency 2025-12-12 07:33:09 +01:00
rustc_query_impl Rollup merge of #151893 - Zoxc:query-mod-move, r=nnethercote 2026-02-04 08:12:40 +01:00
rustc_query_system Auto merge of #151929 - camsteffen:lengg, r=BoxyUwU 2026-02-03 22:20:57 +00:00
rustc_resolve Auto merge of #152025 - jhpratt:rollup-Kxb6k3Y, r=jhpratt 2026-02-03 04:44:02 +00:00
rustc_sanitizers Include assoc const projections in CFI trait object 2026-02-01 22:45:43 +08:00
rustc_serialize Allow internal_features lint in doc tests 2025-11-05 11:25:29 +00:00
rustc_session link modifier export-symbols: export all global symbols from selected uptream c static libraries 2026-02-04 09:26:21 +08:00
rustc_span link modifier export-symbols: export all global symbols from selected uptream c static libraries 2026-02-04 09:26:21 +08:00
rustc_symbol_mangling Fix v0 symbol mangling for assoc const bindings 2026-01-21 12:53:45 +01:00
rustc_target Rollup merge of #151568 - androm3da:bcain/hexagon_linker_cfg, r=Noratrieb 2026-02-02 10:28:32 +11:00
rustc_thread_pool change non-canonical clone impl to {*self}, fix some doc comments 2025-12-20 13:46:22 +00:00
rustc_trait_selection Rollup merge of #152053 - TaKO8Ki:err-tail-semicolon-suggest, r=nnethercote 2026-02-04 08:12:42 +01:00
rustc_traits implied bounds comments 2026-01-19 16:08:54 +00:00
rustc_transmute Clean up src/dst transmute mess. 2026-01-12 09:22:58 +11:00
rustc_ty_utils Part 2 refactoring of moving placeholder types to rustc_type_ir 2026-01-29 11:11:40 +00:00
rustc_type_ir Part 2 refactoring of moving placeholder types to rustc_type_ir 2026-01-29 11:11:40 +00:00
rustc_type_ir_macros Provide an extended framework for type visit, for use in rust-analyzer 2025-12-16 01:47:28 +02:00
rustc_windows_rc