1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// NOTE TO SELF: These macros are horrible copy/pasta. Rewrite.

//! Send arguments to the linker from within `main.rs` or `lib.rs`.
//! Currently only supports Windows MSVC.
//!
//! # Usage
//!
//! Add this to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! link_args = "0.5"
//! ```
//!
//! # Examples
//! 
//! ## `msvc` macros
//! 
//! ```rust
//! // Reserve 8 MiB for the stack.
//! link_args::msvc::stack_size!(0x800000);
//! 
//! // Only set these in release mode.
//! #[cfg(not(debug_assertions))]
//! link_args::msvc! {
//!     // Link the ucrt dynamically and vcruntime statically.
//!     default_lib("ucrt", "libvcruntime", "libcmt");
//!     // Disable the other C runtime libraries.
//!     no_default_lib(
//!         "libvcruntimed.lib", "vcruntime.lib", "vcruntimed.lib",
//!         "libcmtd.lib", "msvcrt.lib", "msvcrtd.lib",
//!         "libucrt.lib", "libucrtd.lib", "ucrtd.lib",
//!     );
//! }
//! ```
//! 
//! ## Raw arguments
//! 
//! ```rust
//! link_args::msvc::raw!(unsafe "/STACK:0x800000 /ENTRY:mainCRTStartup");
//! ```
//! 
//! This reserves 8 MiB (8,388,608 bytes) for the stack and sets
//! the application's entry point to `mainCRTStartup`.
//!
//! <style>#macros + table > tbody > tr:not(:first-child) { display: none !important; }</style>
//!

#![no_std]

/// Embeds raw linker arguments for msvc targets.
///
/// Many arguments that work on the command line will not work here.
///
/// # Example
///
/// ```rust
/// link_args::msvc::raw!(unsafe "/STACK:0x800000 /ENTRY:mainCRTStartup");
/// ```
///
/// # Possible arguments
///
/// * [/DEFAULTLIB](https://docs.microsoft.com/en-us/cpp/build/reference/defaultlib-specify-default-library?view=msvc-160)
/// * [/NODEFAULTLIB](https://docs.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries?view=msvc-160)
/// * [/STACK](https://docs.microsoft.com/en-us/cpp/build/reference/stack-stack-allocations?view=msvc-160)
/// * [/HEAP](https://docs.microsoft.com/en-us/cpp/build/reference/heap-set-heap-size?view=msvc-160)
/// * [/SUBSYSTEM](https://docs.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=msvc-160)
/// * [/EXPORT](https://docs.microsoft.com/en-us/cpp/build/reference/export-exports-a-function?view=msvc-160)
/// * [/INCLUDE](https://docs.microsoft.com/en-us/cpp/build/reference/include-force-symbol-references?view=msvc-160)
/// * [/MANIFESTDEPENDENCY](https://docs.microsoft.com/en-us/cpp/build/reference/manifestdependency-specify-manifest-dependencies?view=msvc-160)
/// * [/MERGE](https://docs.microsoft.com/en-us/cpp/build/reference/merge-combine-sections?view=msvc-160)
/// * [/SECTION](https://docs.microsoft.com/en-us/cpp/build/reference/section-specify-section-attributes?view=msvc-160)
/// * [/ENTRY](https://docs.microsoft.com/en-us/cpp/build/reference/entry-entry-point-symbol?view=msvc-160)
///
/// # Limitations
///
/// Different versions of the MSVC linker may support (or not support) different
/// embeded arguments. Unsupported arguments or values may be silently ignored
/// by the linker.
#[macro_export]
macro_rules! msvc_raw {
    (unsafe { $args:expr }) => {
        $crate::msvc::raw!(unsafe $args);
    };
    (unsafe $args:expr) => {
        $crate::msvc::raw!(>>>INTERNAL<<<, $args.as_bytes());
    };
    (>>>INTERNAL<<<, $___:expr) => {
        const _: () = {
            const ARGS: &[u8] = $___;
            #[cfg(all(windows, target_env = "msvc"))]
            #[link_section = ".drectve"]
            #[used]
            static DIRECTIVE: [u8; ARGS.len() + 1] = {
                let mut array = [0; ARGS.len() + 1];
                let mut index = 0;
                while index < ARGS.len() {
                    array[index] = ARGS[index];
                    index += 1;
                }
                array[index] = b' ';
                array
            };
        };
    };
}

#[doc(hidden)]
pub const fn has_quote(s: &[u8]) -> bool {
    let mut index = 0;
    while index < s.len() {
        if s[index] == b'"' { return true; }
        index += 1;
    }
    false
}

/// Add one or more default libraries.
#[macro_export]
macro_rules! msvc_defaultlib {
    ($library:expr) => {
        const _: () = {
            const _Str_: &str = $library;
            if _Str_.len() > 0 && !$crate::has_quote(_Str_.as_bytes()) {
                const S: &str = concat!("/DEFAULTLIB:\"", $library, "\"");
                $crate::msvc::raw!(unsafe S);
            }
        };
    };
    ($($library:expr),+$(,)?) => {
        $(
            $crate::msvc::default_lib!($library);
        )+
    };
}

/// Prevent one or more libraries being loaded unless explicitly added on the
/// command line.
///
/// This will override any library that is also specified in [msvc::default_lib].
///
/// # Examples
///
/// Prevent kernel32 from being linked.
///
/// ```rust
/// # #[cfg(not(debug_assertions))]
/// link_args::msvc::no_default_lib!("kernel32");
/// ```
///
/// Prevent any library being linked except through the command line:
///
/// ```rust
/// # #[cfg(not(debug_assertions))]
/// link_args::msvc::no_default_lib!();
/// ```
#[macro_export]
macro_rules! msvc_nodefaultlib {
    () => {
        $crate::msvc::raw!(unsafe "/NODEFAULTLIB");
    };
    ($library:expr) => {
        const _: () = {
            const _Str_: &str = $library;
            if _Str_.len() > 0 && !$crate::has_quote(_Str_.as_bytes()) {
                const S: &str = concat!("/NODEFAULTLIB:\"", $library, "\"");
                $crate::msvc::raw!(unsafe S);
            }
        };
    };
    ($($library:expr),+$(,)?) => {
        $(
            $crate::msvc::no_default_lib!($library);
        )+
    };
}

/// Set how much virtual memory is avaliable for the stack.
///
/// You can also optionally allocate physical memory upfront. Be aware that
/// Rust's `std::thread` can and will override these settings for all but the
/// main thread.
///
/// # Examples
///
/// Reserve 8 MiB of virtual memory for the stack.
///
/// ```rust
/// link_args::msvc::stack_size!(0x800000);
/// ```
///
/// Reserve 8 MiB for the stack and allocate 4 MiB as soon as the program starts.
///
/// ```rust
/// link_args::msvc::stack_size!(0x800000, 0x400000);
/// ```
#[macro_export]
macro_rules! msvc_stack_size {
    ($reserve:expr, $commit:expr) => {
        const _: () = {
            #[cfg(all(windows, target_env = "msvc"))]
            #[link_section = ".drectve"]
            #[used]
            static DIRECTIVE: [u8; 29] = {
                let lookup = [
                    b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8',
                    b'9', b'a', b'b', b'c', b'd', b'e', b'f',
                ];
                let mut reserve: u32 = $reserve;
                let mut commit: u32 = $commit;
                let mut array = *b"/STACK:0x00000000,0x00000000 ";
                let mut index = 16;
                while index > 16-8 {
                    array[index] = lookup[(reserve & 0xf) as usize];
                    reserve >>= 4;
                    index -= 1;
                }
                let mut index = 27;
                while index > 27-8 {
                    array[index] = lookup[(commit & 0xf) as usize];
                    commit >>= 4;
                    index -= 1;
                }
                array
            };
        };
    };
    ($reserve:expr) => {
        const _: () = {
            #[cfg(all(windows, target_env = "msvc"))]
            #[link_section = ".drectve"]
            #[used]
            static DIRECTIVE: [u8; 18] = {
                let lookup = [
                    b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8',
                    b'9', b'a', b'b', b'c', b'd', b'e', b'f',
                ];
                let mut reserve: u32 = $reserve;
                let mut array = *b"/STACK:0x00000000 ";
                let mut index = 16;
                while index > 16-8 {
                    array[index] = lookup[(reserve & 0xf) as usize];
                    reserve >>= 4;
                    index -= 1;
                }
                array
            };
        };
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! msvc_internal {
    (>>>INTERNAL<<<, stack_size($reserve:expr$(, $commit:expr)?)) => {
        $crate::msvc::stack_size!($reserve$(, $commit)?);
    };
    (>>>INTERNAL<<<, default_lib($($args:expr),+)) => {
        $crate::msvc::default_lib!($($args),+);
    };
    (>>>INTERNAL<<<, no_default_lib($($args:expr),+)) => {
        $crate::msvc::no_default_lib!($($args),+);
    };
    (>>>INTERNAL<<<, no_default_lib) => {
        $crate::msvc::no_default_lib!();
    };
    (>>>INTERNAL<<<, raw($tt:tt)) => {
        $crate::msvc::raw!();
    };
}

/// Set a group of arguments for the msvc linker.
///
/// The macro can have one or more lines in the form `argument(value1, value1);`.
///
/// # Example
///
/// ```rust
/// link_args::msvc! {
///     // Link the ucrt dynamically and vcruntime statically.
///     default_lib("ucrt", "libvcruntime", "libcmt");
///     // Disable the other C runtime libraries.
///     no_default_lib(
///         "libvcruntimed.lib", "vcruntime.lib", "vcruntimed.lib",
///         "libcmtd.lib", "msvcrt.lib", "msvcrtd.lib",
///         "libucrt.lib", "libucrtd.lib", "ucrtd.lib",
///     );
/// }
/// ```
#[macro_export]
macro_rules! msvc {
    ($($tt:tt($($($args:expr),+$(,)?)?));+$(;)?) => {
        $(
            $crate::msvc_internal!(>>>INTERNAL<<<, $tt$(($($args),+))?);
        )+
    };
}

pub mod msvc {
    #[doc(inline)]
    pub use crate::msvc_stack_size as stack_size;

    #[doc(inline)]
    pub use crate::msvc_defaultlib as default_lib;

    #[doc(inline)]
    pub use crate::msvc_nodefaultlib as no_default_lib;

    #[doc(inline)]
    pub use crate::msvc_raw as raw;
}