|
| 1 | +use proc_macro::TokenStream; |
| 2 | +use proc_macro2::{Literal, Span}; |
| 3 | +use quote::quote; |
| 4 | +use syn::{ |
| 5 | + parse::{Parse, ParseStream}, |
| 6 | + parse_macro_input, Error, Ident, ItemFn, LitStr, Result, Token, |
| 7 | +}; |
| 8 | + |
| 9 | +struct AttributeArgs { |
| 10 | + descriptor_link_section: Option<String>, |
| 11 | +} |
| 12 | + |
| 13 | +impl Parse for AttributeArgs { |
| 14 | + fn parse(input: ParseStream) -> Result<Self> { |
| 15 | + if input.is_empty() { |
| 16 | + return Ok(Self { |
| 17 | + descriptor_link_section: None, |
| 18 | + }); |
| 19 | + } |
| 20 | + |
| 21 | + let arg_name: Ident = input.parse()?; |
| 22 | + if arg_name != "descriptor_link_section" { |
| 23 | + return Err(Error::new( |
| 24 | + arg_name.span(), |
| 25 | + "expected `descriptor_link_section`", |
| 26 | + )); |
| 27 | + } |
| 28 | + input.parse::<Token![=]>()?; |
| 29 | + let descriptor_link_section: LitStr = input.parse()?; |
| 30 | + |
| 31 | + Ok(Self { |
| 32 | + descriptor_link_section: Some(descriptor_link_section.value()), |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#[proc_macro_attribute] |
| 38 | +pub fn checkct(args: TokenStream, input: TokenStream) -> TokenStream { |
| 39 | + let args = parse_macro_input!(args as AttributeArgs); |
| 40 | + // By default the descriptor will be stored in the .note.checkct section |
| 41 | + let descriptor_link_section = Literal::string( |
| 42 | + args.descriptor_link_section |
| 43 | + .as_deref() |
| 44 | + .unwrap_or(".note.checkct"), |
| 45 | + ); |
| 46 | + |
| 47 | + let item_fn = parse_macro_input!(input as ItemFn); |
| 48 | + let name = &item_fn.sig.ident; |
| 49 | + |
| 50 | + let entrypoint_descriptor_name = Ident::new( |
| 51 | + &format!("__checkct_entrypoint_descriptor__{name}"), |
| 52 | + Span::call_site(), |
| 53 | + ); |
| 54 | + let entrypoint_descriptor_type = Ident::new( |
| 55 | + &format!("__CheckctEntrypointDescriptor__{name}"), |
| 56 | + Span::call_site(), |
| 57 | + ); |
| 58 | + |
| 59 | + quote! { |
| 60 | + // The descriptor is laid out as an ELF note (which binsec parses), since it is stored |
| 61 | + // in a note section by default. It ends with the address of the entrypoint. |
| 62 | + #[doc(hidden)] |
| 63 | + #[allow(non_camel_case_types, dead_code)] |
| 64 | + #[repr(C, packed(4))] |
| 65 | + pub struct #entrypoint_descriptor_type { |
| 66 | + namesz: u32, |
| 67 | + descsz: u32, |
| 68 | + kind: u32, |
| 69 | + name: [u8; 8], |
| 70 | + entrypoint: fn(), |
| 71 | + } |
| 72 | + |
| 73 | + // This variable is used to prevent the compiler from removing the code of the entrypoint |
| 74 | + // function if it is unused. Moreover this variable name has a special format which allows |
| 75 | + // cargo-checkct to find it. |
| 76 | + // Make sure that the descriptor link section is kept by the linker. |
| 77 | + #[unsafe(link_section = #descriptor_link_section)] |
| 78 | + #[used] |
| 79 | + pub static #entrypoint_descriptor_name: #entrypoint_descriptor_type = |
| 80 | + #entrypoint_descriptor_type { |
| 81 | + namesz: 8, |
| 82 | + descsz: ::core::mem::size_of::<fn()>() as u32, |
| 83 | + kind: 1, |
| 84 | + name: *b"checkct\0", |
| 85 | + entrypoint: #name, |
| 86 | + }; |
| 87 | + |
| 88 | + #item_fn |
| 89 | + } |
| 90 | + .into() |
| 91 | +} |
0 commit comments