linkme-impl-0.3.36/.cargo_vcs_info.json0000644000000001421046102023000133620ustar { "git": { "sha1": "349b4182ec00deb1e5d57f4b992e4d09e92a0d6a" }, "path_in_vcs": "impl" }linkme-impl-0.3.36/Cargo.lock0000644000000021361046102023000113420ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "linkme-impl" version = "0.3.36" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "proc-macro2" version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" dependencies = [ "proc-macro2", ] [[package]] name = "syn" version = "2.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" linkme-impl-0.3.36/Cargo.toml0000644000000030001046102023000113540ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.71" name = "linkme-impl" version = "0.3.36" authors = ["David Tolnay "] build = "build.rs" autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Implementation detail of the linkme crate" documentation = "https://docs.rs/linkme" readme = false license = "MIT OR Apache-2.0" repository = "https://github.com/dtolnay/linkme" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = [ "--generate-link-to-definition", "--generate-macro-expansion", "--extern-html-root-url=core=https://doc.rust-lang.org", "--extern-html-root-url=alloc=https://doc.rust-lang.org", "--extern-html-root-url=std=https://doc.rust-lang.org", "--extern-html-root-url=proc_macro=https://doc.rust-lang.org", ] [features] used_linker = [] [lib] name = "linkme_impl" path = "src/lib.rs" proc-macro = true [dependencies.proc-macro2] version = "1.0.74" [dependencies.quote] version = "1.0.35" [dependencies.syn] version = "2.0.46" linkme-impl-0.3.36/Cargo.toml.orig000064400000000000000000000015161046102023000150250ustar 00000000000000[package] name = "linkme-impl" version = "0.3.36" authors = ["David Tolnay "] description = "Implementation detail of the linkme crate" documentation = "https://docs.rs/linkme" edition = "2021" license = "MIT OR Apache-2.0" repository = "https://github.com/dtolnay/linkme" rust-version = "1.71" [lib] proc-macro = true [features] used_linker = [] [dependencies] proc-macro2 = "1.0.74" quote = "1.0.35" syn = "2.0.46" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = [ "--generate-link-to-definition", "--generate-macro-expansion", "--extern-html-root-url=core=https://doc.rust-lang.org", "--extern-html-root-url=alloc=https://doc.rust-lang.org", "--extern-html-root-url=std=https://doc.rust-lang.org", "--extern-html-root-url=proc_macro=https://doc.rust-lang.org", ] linkme-impl-0.3.36/LICENSE-APACHE000064400000000000000000000227731046102023000140720ustar 00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS linkme-impl-0.3.36/LICENSE-MIT000064400000000000000000000017771046102023000136030ustar 00000000000000Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. linkme-impl-0.3.36/build.rs000064400000000000000000000021241046102023000135770ustar 00000000000000use std::env; use std::process::Command; use std::str; fn main() { println!("cargo:rerun-if-changed=build.rs"); let Some(rustc) = rustc_minor_version() else { return; }; if rustc >= 80 { println!("cargo:rustc-check-cfg=cfg(exhaustive)"); println!("cargo:rustc-check-cfg=cfg(no_unsafe_attributes)"); println!("cargo:rustc-check-cfg=cfg(no_unsafe_extern_blocks)"); } if rustc < 82 { // https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html#unsafe-attributes println!("cargo:rustc-cfg=no_unsafe_attributes"); // https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html#safe-items-with-unsafe-extern println!("cargo:rustc-cfg=no_unsafe_extern_blocks"); } } fn rustc_minor_version() -> Option { let rustc = env::var_os("RUSTC").unwrap(); let output = Command::new(rustc).arg("--version").output().ok()?; let version = str::from_utf8(&output.stdout).ok()?; let mut pieces = version.split('.'); if pieces.next() != Some("rustc 1") { return None; } pieces.next()?.parse().ok() } linkme-impl-0.3.36/src/args.rs000064400000000000000000000013041046102023000142220ustar 00000000000000use syn::parse::{Error, Parse, ParseStream, Result}; use syn::{LitInt, Path, Token}; pub enum Args { None, Path(Path), PathPos(Path, usize), } impl Parse for Args { fn parse(input: ParseStream) -> Result { if input.is_empty() { return Ok(Args::None); } let path: Path = input.parse()?; if input.is_empty() { return Ok(Args::Path(path)); } input.parse::()?; let lit: LitInt = input.parse()?; let pos: usize = lit.base10_parse()?; if pos > 9999 { return Err(Error::new(lit.span(), "maximum 9999 is supported")); } Ok(Args::PathPos(path, pos)) } } linkme-impl-0.3.36/src/attr.rs000064400000000000000000000022261046102023000142440ustar 00000000000000use syn::parse::{Error, Result}; use syn::{parse_quote, Attribute, Path}; // #[linkme(crate = path::to::linkme)] pub(crate) fn linkme_path(attrs: &mut Vec) -> Result { let mut linkme_path = None; let mut errors: Option = None; attrs.retain(|attr| { if !attr.path().is_ident("linkme") { return true; } if let Err(err) = attr.parse_nested_meta(|meta| { if meta.path.is_ident("crate") { if linkme_path.is_some() { return Err(meta.error("duplicate linkme crate attribute")); } let path = meta.value()?.call(Path::parse_mod_style)?; linkme_path = Some(path); Ok(()) } else { Err(meta.error("unsupported linkme attribute")) } }) { match &mut errors { None => errors = Some(err), Some(errors) => errors.combine(err), } } false }); match errors { None => Ok(linkme_path.unwrap_or_else(|| parse_quote!(::linkme))), Some(errors) => Err(errors), } } linkme-impl-0.3.36/src/declaration.rs000064400000000000000000000342071046102023000155630ustar 00000000000000use crate::{attr, linker, private, ty}; use proc_macro2::{Span, TokenStream}; use quote::quote; use syn::parse::{Parse, ParseStream, Result}; use syn::{bracketed, Attribute, Error, Ident, Token, Type, Visibility}; struct Declaration { attrs: Vec, vis: Visibility, ident: Ident, ty: Type, } impl Parse for Declaration { fn parse(input: ParseStream) -> Result { let attrs = input.call(Attribute::parse_outer)?; let vis: Visibility = input.parse()?; input.parse::()?; let mut_token: Option = input.parse()?; if let Some(mut_token) = mut_token { return Err(Error::new_spanned( mut_token, "static mut is not supported by distributed_slice", )); } let ident: Ident = input.parse()?; input.parse::()?; let ty: Type = input.parse()?; let eq_token: Option = input.parse()?; if eq_token.is_some() { let content; bracketed!(content in input); content.parse::()?; } input.parse::()?; Ok(Declaration { attrs, vis, ident, ty, }) } } pub fn expand(input: TokenStream) -> TokenStream { let msg = "distributed_slice is not implemented for this platform"; let error = Error::new_spanned(&input, msg); let unsupported_platform = error.to_compile_error(); let decl: Declaration = match syn::parse2(input) { Ok(decl) => decl, Err(err) => return err.to_compile_error(), }; let mut attrs = decl.attrs; let vis = decl.vis; let ident = decl.ident; let mut ty = decl.ty; let name = ident.to_string(); let linkme_path = match attr::linkme_path(&mut attrs) { Ok(path) => path, Err(err) => return err.to_compile_error(), }; ty::populate_static_lifetimes(&mut ty); let used = if cfg!(feature = "used_linker") { quote!(#[used(linker)]) } else { quote!(#[used]) }; let linux_section = linker::linux::section(&ident); let linux_section_start = linker::linux::section_start(&ident); let linux_section_stop = linker::linux::section_stop(&ident); let linux_dupcheck = linux_section.replacen("linkme", "linkm2", 1); let linux_dupcheck_start = linux_section_start.replacen("linkme", "linkm2", 1); let linux_dupcheck_stop = linux_section_stop.replacen("linkme", "linkm2", 1); let macho_section = linker::macho::section(&ident); let macho_section_start = linker::macho::section_start(&ident); let macho_section_stop = linker::macho::section_stop(&ident); let macho_dupcheck = macho_section.replacen("linkme", "linkm2", 1); let macho_dupcheck_start = macho_section_start.replacen("linkme", "linkm2", 1); let macho_dupcheck_stop = macho_section_stop.replacen("linkme", "linkm2", 1); let windows_section = linker::windows::section(&ident); let windows_section_start = linker::windows::section_start(&ident); let windows_section_stop = linker::windows::section_stop(&ident); let windows_dupcheck = windows_section.replacen("linkme", "linkm2", 1); let windows_dupcheck_start = windows_section_start.replacen("linkme", "linkm2", 1); let windows_dupcheck_stop = windows_section_stop.replacen("linkme", "linkm2", 1); let illumos_section = linker::illumos::section(&ident); let illumos_section_start = linker::illumos::section_start(&ident); let illumos_section_stop = linker::illumos::section_stop(&ident); let illumos_dupcheck = illumos_section.replacen("linkme", "linkm2", 1); let illumos_dupcheck_start = illumos_section_start.replacen("linkme", "linkm2", 1); let illumos_dupcheck_stop = illumos_section_stop.replacen("linkme", "linkm2", 1); let bsd_section = linker::bsd::section(&ident); let bsd_section_start = linker::bsd::section_start(&ident); let bsd_section_stop = linker::bsd::section_stop(&ident); let bsd_dupcheck = bsd_section.replacen("linkme", "linkm2", 1); let bsd_dupcheck_start = bsd_section_start.replacen("linkme", "linkm2", 1); let bsd_dupcheck_stop = bsd_section_stop.replacen("linkme", "linkm2", 1); let call_site = Span::call_site(); let link_section_macro_str = format!("_linkme_macro_{}", ident); let link_section_macro = Ident::new(&link_section_macro_str, call_site); let unsafe_extern = if cfg!(no_unsafe_extern_blocks) { None } else { Some(Token![unsafe](call_site)) }; let (unsafe_attr, link_section_attr) = if cfg!(no_unsafe_attributes) { // #[cfg_attr(all(), link_section = ...)] ( Ident::new("cfg_attr", call_site), quote!(all(), link_section), ) } else { // #[unsafe(link_section = ...)] (Ident::new("unsafe", call_site), quote!(link_section)) }; quote! { #(#attrs)* #vis static #ident: #linkme_path::DistributedSlice<#ty> = { #[cfg(any( target_os = "none", target_os = "linux", target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "android", target_os = "fuchsia", target_os = "illumos", target_os = "freebsd", target_os = "openbsd", target_os = "psp", ))] #unsafe_extern extern "Rust" { #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), link_name = #linux_section_start)] #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), link_name = #macho_section_start)] #[cfg_attr(target_os = "illumos", link_name = #illumos_section_start)] #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), link_name = #bsd_section_start)] static LINKME_START: [<#ty as #linkme_path::#private::Slice>::Element; 0]; #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), link_name = #linux_section_stop)] #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), link_name = #macho_section_stop)] #[cfg_attr(target_os = "illumos", link_name = #illumos_section_stop)] #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), link_name = #bsd_section_stop)] static LINKME_STOP: [<#ty as #linkme_path::#private::Slice>::Element; 0]; #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), link_name = #linux_dupcheck_start)] #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), link_name = #macho_dupcheck_start)] #[cfg_attr(target_os = "illumos", link_name = #illumos_dupcheck_start)] #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), link_name = #bsd_dupcheck_start)] static DUPCHECK_START: (); #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), link_name = #linux_dupcheck_stop)] #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), link_name = #macho_dupcheck_stop)] #[cfg_attr(target_os = "illumos", link_name = #illumos_dupcheck_stop)] #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), link_name = #bsd_dupcheck_stop)] static DUPCHECK_STOP: (); } // On Windows/UEFI, use non-ZST boundary elements because some // codegen backends emit non-zero bytes for ZST statics in custom // PE/COFF sections, which corrupts section boundary and dupcheck // arithmetic. #[cfg(any(target_os = "uefi", target_os = "windows"))] #[#unsafe_attr(#link_section_attr = #windows_section_start)] static LINKME_START: #linkme_path::#private::BoundaryElement<#ty> = #linkme_path::#private::BoundaryElement::<#ty>::uninit(); #[cfg(any(target_os = "uefi", target_os = "windows"))] #[#unsafe_attr(#link_section_attr = #windows_section_stop)] static LINKME_STOP: #linkme_path::#private::BoundaryElement<#ty> = #linkme_path::#private::BoundaryElement::<#ty>::uninit(); #[cfg(any(target_os = "uefi", target_os = "windows"))] #[#unsafe_attr(#link_section_attr = #windows_dupcheck_start)] static DUPCHECK_START: #linkme_path::#private::isize = 0; #[cfg(any(target_os = "uefi", target_os = "windows"))] #[#unsafe_attr(#link_section_attr = #windows_dupcheck_stop)] static DUPCHECK_STOP: #linkme_path::#private::isize = 0; #used #[cfg(any( target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "illumos", target_os = "freebsd", target_os = "openbsd", target_os = "psp", ))] #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), #unsafe_attr(#link_section_attr = #linux_section))] #[cfg_attr(target_os = "illumos", #unsafe_attr(#link_section_attr = #illumos_section))] #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), #unsafe_attr(#link_section_attr = #bsd_section))] static mut LINKME_PLEASE: [<#ty as #linkme_path::#private::Slice>::Element; 0] = []; #used #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), #unsafe_attr(#link_section_attr = #linux_dupcheck))] #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), #unsafe_attr(#link_section_attr = #macho_dupcheck))] #[cfg_attr(any(target_os = "uefi", target_os = "windows"), #unsafe_attr(#link_section_attr = #windows_dupcheck))] #[cfg_attr(target_os = "illumos", #unsafe_attr(#link_section_attr = #illumos_dupcheck))] #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), #unsafe_attr(#link_section_attr = #bsd_dupcheck))] static DUPCHECK: #linkme_path::#private::isize = 1; #[cfg(not(any( target_os = "none", target_os = "linux", target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "windows", target_os = "uefi", target_os = "android", target_os = "fuchsia", target_os = "illumos", target_os = "freebsd", target_os = "openbsd", target_os = "psp", )))] #unsupported_platform unsafe { #linkme_path::DistributedSlice::private_new( #name, #linkme_path::#private::ptr::addr_of!(LINKME_START) .cast::<<#ty as #linkme_path::#private::Slice>::Element>(), #linkme_path::#private::ptr::addr_of!(LINKME_STOP) .cast::<<#ty as #linkme_path::#private::Slice>::Element>(), #linkme_path::#private::ptr::addr_of!(DUPCHECK_START) .cast::<#linkme_path::#private::isize>(), #linkme_path::#private::ptr::addr_of!(DUPCHECK_STOP) .cast::<#linkme_path::#private::isize>(), ) } }; #[doc(hidden)] #[macro_export] macro_rules! #link_section_macro { ( #![linkme_macro = $macro:path] #![linkme_sort_key = $key:tt] $item:item ) => { $macro ! { #![linkme_linux_section = concat!(#linux_section, $key)] #![linkme_macho_section = concat!(#macho_section, $key)] #![linkme_windows_section = concat!(#windows_section, $key)] #![linkme_illumos_section = concat!(#illumos_section, $key)] #![linkme_bsd_section = concat!(#bsd_section, $key)] $item } }; ( #![linkme_macro = $macro:path] $item:item ) => { $macro ! { #![linkme_linux_section = #linux_section] #![linkme_macho_section = #macho_section] #![linkme_windows_section = #windows_section] #![linkme_illumos_section = #illumos_section] #![linkme_bsd_section = #bsd_section] $item } }; ( #![linkme_linux_section = $linux_section:expr] #![linkme_macho_section = $macho_section:expr] #![linkme_windows_section = $windows_section:expr] #![linkme_illumos_section = $illumos_section:expr] #![linkme_bsd_section = $bsd_section:expr] $item:item ) => { #used #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), #unsafe_attr(#link_section_attr = $linux_section))] #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), #unsafe_attr(#link_section_attr = $macho_section))] #[cfg_attr(any(target_os = "uefi", target_os = "windows"), #unsafe_attr(#link_section_attr = $windows_section))] #[cfg_attr(target_os = "illumos", #unsafe_attr(#link_section_attr = $illumos_section))] #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), #unsafe_attr(#link_section_attr = $bsd_section))] $item }; } #[doc(hidden)] #vis use #link_section_macro as #ident; } } linkme-impl-0.3.36/src/element.rs000064400000000000000000000206211046102023000147220ustar 00000000000000use crate::{attr, private, ty}; use proc_macro2::{Span, TokenStream, TokenTree}; use quote::{format_ident, quote, quote_spanned}; use syn::parse::{Error, Parse, ParseStream, Result}; use syn::punctuated::Punctuated; use syn::{ braced, parenthesized, parse_quote, Abi, Attribute, BareFnArg, BoundLifetimes, GenericParam, Generics, Ident, Path, ReturnType, Token, Type, TypeBareFn, Visibility, WhereClause, }; pub struct Element { attrs: Vec, vis: Visibility, ident: Ident, ty: Type, expr: TokenStream, orig_item: Option, start_span: Span, end_span: Span, } impl Parse for Element { fn parse(input: ParseStream) -> Result { let attrs = input.call(Attribute::parse_outer)?; let item = input.cursor(); let vis: Visibility = input.parse()?; let static_token: Option = input.parse()?; if static_token.is_some() { let mut_token: Option = input.parse()?; if let Some(mut_token) = mut_token { return Err(Error::new_spanned( mut_token, "static mut is not supported by distributed_slice", )); } let ident: Ident = input.parse()?; input.parse::()?; let start_span = input.span(); let ty: Type = input.parse()?; let end_span = quote!(#ty).into_iter().last().unwrap().span(); input.parse::()?; let mut expr_semi = Vec::from_iter(input.parse::()?); if let Some(tail) = expr_semi.pop() { syn::parse2::(TokenStream::from(tail))?; } let expr = TokenStream::from_iter(expr_semi); Ok(Element { attrs, vis, ident, ty, expr, orig_item: None, start_span, end_span, }) } else { let constness: Option = input.parse()?; let asyncness: Option = input.parse()?; let unsafety: Option = input.parse()?; let abi: Option = input.parse()?; let fn_token: Token![fn] = input.parse().map_err(|_| { Error::new_spanned( item.token_stream(), "distributed element must be either static or function item", ) })?; let ident: Ident = input.parse()?; let generics: Generics = input.parse()?; let content; let paren_token = parenthesized!(content in input); let mut inputs = Punctuated::new(); while !content.is_empty() { content.parse::>()?; let ident = if let Some(wild) = content.parse::>()? { Ident::from(wild) } else { content.parse()? }; let colon_token: Token![:] = content.parse()?; let ty: Type = content.parse()?; inputs.push_value(BareFnArg { attrs: Vec::new(), name: Some((ident, colon_token)), ty, }); if !content.is_empty() { let comma: Token![,] = content.parse()?; inputs.push_punct(comma); } } let output: ReturnType = input.parse()?; let where_clause: Option = input.parse()?; let content; braced!(content in input); content.parse::()?; if let Some(constness) = constness { return Err(Error::new_spanned( constness, "const fn distributed slice element is not supported", )); } if let Some(asyncness) = asyncness { return Err(Error::new_spanned( asyncness, "async fn distributed slice element is not supported", )); } let lifetimes = if generics.params.is_empty() { None } else { let mut bound = BoundLifetimes { for_token: Token![for](generics.lt_token.unwrap().span), lt_token: generics.lt_token.unwrap(), lifetimes: Punctuated::new(), gt_token: generics.gt_token.unwrap(), }; for param in generics.params.into_pairs() { let (param, punct) = param.into_tuple(); if let GenericParam::Lifetime(_) = param { bound.lifetimes.push_value(param); if let Some(punct) = punct { bound.lifetimes.push_punct(punct); } } else { return Err(Error::new_spanned( param, "cannot have generic parameters on distributed slice element", )); } } Some(bound) }; if let Some(where_clause) = where_clause { return Err(Error::new_spanned( where_clause, "where-clause is not allowed on distributed slice elements", )); } let start_span = item.span(); let end_span = quote!(#output) .into_iter() .last() .as_ref() .map_or(paren_token.span.close(), TokenTree::span); let mut original_attrs = attrs; let linkme_path = attr::linkme_path(&mut original_attrs)?; let attrs = vec![ parse_quote! { #[allow(non_upper_case_globals)] }, parse_quote! { #[linkme(crate = #linkme_path)] }, ]; let vis = Visibility::Inherited; let expr = parse_quote!(#ident); let ty = Type::BareFn(TypeBareFn { lifetimes, unsafety, abi, fn_token, paren_token, inputs, variadic: None, output, }); let ident = format_ident!("_LINKME_ELEMENT_{}", ident); let item = item.token_stream(); let orig_item = Some(quote!( #(#original_attrs)* #item )); Ok(Element { attrs, vis, ident, ty, expr, orig_item, start_span, end_span, }) } } } pub fn expand(path: Path, pos: impl Into>, input: Element) -> TokenStream { let pos = pos.into(); do_expand(path, pos, input) } fn do_expand(path: Path, pos: Option, input: Element) -> TokenStream { let mut attrs = input.attrs; let vis = input.vis; let ident = input.ident; let mut ty = input.ty; let expr = input.expr; let orig_item = input.orig_item; ty::populate_static_lifetimes(&mut ty); let linkme_path = match attr::linkme_path(&mut attrs) { Ok(path) => path, Err(err) => return err.to_compile_error(), }; let sort_key = pos.into_iter().map(|pos| format!("{:04}", pos)); let factory = quote_spanned!(input.start_span=> __new); let get = quote_spanned!(input.end_span=> #factory()); quote! { #path ! { #![linkme_macro = #path] #( #![linkme_sort_key = #sort_key] )* #(#attrs)* #vis static #ident : #ty = { #[allow(clippy::no_effect_underscore_binding)] unsafe fn __typecheck(_: #linkme_path::#private::Void) { #[allow(clippy::ref_option_ref)] let #factory = || -> fn() -> &'static #ty { || &#ident }; unsafe { #linkme_path::DistributedSlice::private_typecheck(#path, #get); } } #expr }; } #orig_item } } linkme-impl-0.3.36/src/hash.rs000064400000000000000000000026021046102023000142130ustar 00000000000000use std::collections::hash_map; use std::fmt::{self, Display, Write}; use std::hash::{Hash, Hasher}; use syn::Ident; // 8-character symbol hash consisting of a-zA-Z0-9. We use 8 character because // Mach-O section specifiers are restricted to at most 16 characters (see // https://github.com/dtolnay/linkme/issues/35) and we leave room for a // linkme-specific prefix. pub(crate) struct Symbol(u64); pub(crate) fn hash(ident: &Ident) -> Symbol { let mut hasher = hash_map::DefaultHasher::new(); ident.hash(&mut hasher); Symbol(hasher.finish()) } impl Display for Symbol { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { // log(62^8)/log(2) is 47.6 so we have enough bits in the 64-bit // standard library hash to produce a good distribution over 8 digits // from a 62-character alphabet. let mut remainder = self.0; for _ in 0..8 { let digit = (remainder % 62) as u8; remainder /= 62; formatter.write_char(match digit { 0..=25 => b'a' + digit, 26..=51 => b'A' + digit - 26, 52..=61 => b'0' + digit - 52, _ => unreachable!(), } as char)?; } Ok(()) } } #[test] fn test_hash() { let ident = Ident::new("EXAMPLE", proc_macro2::Span::call_site()); assert_eq!(hash(&ident).to_string(), "0GPSzIoo"); } linkme-impl-0.3.36/src/lib.rs000064400000000000000000000024471046102023000140450ustar 00000000000000#![cfg_attr(all(test, exhaustive), feature(non_exhaustive_omitted_patterns_lint))] #![allow( clippy::cast_possible_truncation, // https://github.com/rust-lang/rust-clippy/issues/7486 clippy::needless_pass_by_value, clippy::too_many_lines, clippy::uninlined_format_args, )] mod args; mod attr; mod declaration; mod element; mod hash; mod linker; mod ty; use crate::args::Args; use crate::hash::hash; use proc_macro::TokenStream; use proc_macro2::{Ident, Span}; use quote::{ToTokens, TokenStreamExt as _}; use syn::parse_macro_input; #[proc_macro_attribute] pub fn distributed_slice(args: TokenStream, input: TokenStream) -> TokenStream { let args = parse_macro_input!(args as Args); let expanded = match args { Args::None => declaration::expand(parse_macro_input!(input)), Args::Path(path) => element::expand(path, None, parse_macro_input!(input)), Args::PathPos(path, pos) => element::expand(path, pos, parse_macro_input!(input)), }; TokenStream::from(expanded) } #[allow(non_camel_case_types)] struct private; impl ToTokens for private { fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { tokens.append(Ident::new( concat!("__private", env!("CARGO_PKG_VERSION_PATCH")), Span::call_site(), )); } } linkme-impl-0.3.36/src/linker.rs000064400000000000000000000034161046102023000145600ustar 00000000000000pub mod linux { use syn::Ident; pub fn section(ident: &Ident) -> String { format!("linkme_{}", ident) } pub fn section_start(ident: &Ident) -> String { format!("__start_linkme_{}", ident) } pub fn section_stop(ident: &Ident) -> String { format!("__stop_linkme_{}", ident) } } pub mod bsd { use syn::Ident; pub fn section(ident: &Ident) -> String { format!("linkme_{}", ident) } pub fn section_start(ident: &Ident) -> String { format!("__start_linkme_{}", ident) } pub fn section_stop(ident: &Ident) -> String { format!("__stop_linkme_{}", ident) } } pub mod macho { use syn::Ident; pub fn section(ident: &Ident) -> String { format!( "__DATA,__linkme{},regular,no_dead_strip", crate::hash(ident), ) } pub fn section_start(ident: &Ident) -> String { format!("\x01section$start$__DATA$__linkme{}", crate::hash(ident)) } pub fn section_stop(ident: &Ident) -> String { format!("\x01section$end$__DATA$__linkme{}", crate::hash(ident)) } } pub mod windows { use syn::Ident; pub fn section(ident: &Ident) -> String { format!(".linkme_{}$b", ident) } pub fn section_start(ident: &Ident) -> String { format!(".linkme_{}$a", ident) } pub fn section_stop(ident: &Ident) -> String { format!(".linkme_{}$c", ident) } } pub mod illumos { use syn::Ident; pub fn section(ident: &Ident) -> String { format!("set_linkme_{}", ident) } pub fn section_start(ident: &Ident) -> String { format!("__start_set_linkme_{}", ident) } pub fn section_stop(ident: &Ident) -> String { format!("__stop_set_linkme_{}", ident) } } linkme-impl-0.3.36/src/ty.rs000064400000000000000000000032061046102023000137250ustar 00000000000000use syn::{GenericArgument, Lifetime, PathArguments, Type}; pub(crate) fn populate_static_lifetimes(ty: &mut Type) { match ty { #![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))] Type::Array(ty) => populate_static_lifetimes(&mut ty.elem), Type::Group(ty) => populate_static_lifetimes(&mut ty.elem), Type::Paren(ty) => populate_static_lifetimes(&mut ty.elem), Type::Path(ty) => { if let Some(qself) = &mut ty.qself { populate_static_lifetimes(&mut qself.ty); } for segment in &mut ty.path.segments { if let PathArguments::AngleBracketed(segment) = &mut segment.arguments { for arg in &mut segment.args { if let GenericArgument::Type(arg) = arg { populate_static_lifetimes(arg); } } } } } Type::Ptr(ty) => populate_static_lifetimes(&mut ty.elem), Type::Reference(ty) => { if ty.lifetime.is_none() { ty.lifetime = Some(Lifetime::new("'static", ty.and_token.span)); } populate_static_lifetimes(&mut ty.elem); } Type::Slice(ty) => populate_static_lifetimes(&mut ty.elem), Type::Tuple(ty) => ty.elems.iter_mut().for_each(populate_static_lifetimes), Type::ImplTrait(_) | Type::Infer(_) | Type::Macro(_) | Type::Never(_) | Type::TraitObject(_) | Type::BareFn(_) | Type::Verbatim(_) => {} _ => unimplemented!("unknown Type"), } }