Skip to content

Commit a2b6b08

Browse files
committed
get rid of cfg-match
1 parent 7fc3147 commit a2b6b08

File tree

3 files changed

+93
-105
lines changed

3 files changed

+93
-105
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ test = false
1111
name = "tests"
1212

1313
[dependencies]
14-
cfg-match = "0.2.1"
15-
1614
embedded-io = "0.6.1"
1715
goblin = { version = "0.9.3", default-features = false, features = [
1816
"elf32",

src/arch.rs

Lines changed: 93 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,53 @@ use core::ffi::c_void;
44
use core::mem;
55
use core::ptr;
66

7-
use cfg_match::cfg_match;
8-
97
use crate::args::EnvEdit;
108
use crate::const_concat::concat;
119

1210
#[cfg(not(target_os = "linux"))]
1311
compiler_error!("Only Linux is supported");
1412

15-
cfg_match! {
16-
target_pointer_width = "64" => {
17-
pub use goblin::elf64 as elf_types;
18-
}
19-
target_pointer_width = "32" => {
20-
pub use goblin::elf32 as elf_types;
21-
}
22-
}
13+
#[cfg(target_pointer_width = "32")]
14+
pub use goblin::elf32 as elf_types;
15+
#[cfg(target_pointer_width = "64")]
16+
pub use goblin::elf64 as elf_types;
2317

2418
// Typically 16 is required
2519
pub const STACK_ALIGNMENT: usize = 32;
2620

2721
pub const EM_SELF: u16 = {
2822
use elf_types::header::*;
29-
cfg_match! {
30-
target_arch = "x86_64" => EM_X86_64,
31-
target_arch = "x86" => EM_386,
32-
target_arch = "aarch64" => EM_AARCH64,
33-
}
23+
#[cfg(target_arch = "x86_64")]
24+
const VALUE: u16 = EM_X86_64;
25+
#[cfg(target_arch = "x86")]
26+
const VALUE: u16 = EM_386;
27+
#[cfg(target_arch = "aarch64")]
28+
const VALUE: u16 = EM_AARCH64;
29+
VALUE
3430
};
3531

3632
pub const R_RELATIVE: u32 = {
3733
use elf_types::reloc::*;
38-
cfg_match! {
39-
target_arch = "x86_64" => R_X86_64_RELATIVE,
40-
target_arch = "x86" => R_386_RELATIVE,
41-
target_arch = "aarch64" => R_AARCH64_RELATIVE,
42-
}
34+
#[cfg(target_arch = "x86_64")]
35+
const VALUE: u32 = R_X86_64_RELATIVE;
36+
#[cfg(target_arch = "x86")]
37+
const VALUE: u32 = R_386_RELATIVE;
38+
#[cfg(target_arch = "aarch64")]
39+
const VALUE: u32 = R_AARCH64_RELATIVE;
40+
VALUE
4341
};
4442

4543
pub const NIX_SYSTEM: &str = match option_env!("NIX_SYSTEM") {
4644
Some(system) => system,
47-
None => cfg_match! {
48-
target_arch = "x86_64" => "x86_64_linux",
49-
target_arch = "x86" => "i686_linux",
50-
target_arch = "aarch64" => "aarch64_linux",
51-
},
45+
None => {
46+
#[cfg(target_arch = "x86_64")]
47+
const VALUE: &str = "x86_64_linux";
48+
#[cfg(target_arch = "x86")]
49+
const VALUE: &str = "i686_linux";
50+
#[cfg(target_arch = "aarch64")]
51+
const VALUE: &str = "aarch64_linux";
52+
VALUE
53+
}
5254
};
5355

5456
pub const NIX_LD_SYSTEM_ENV: &str = concat!("NIX_LD_", NIX_SYSTEM);
@@ -62,34 +64,24 @@ pub const NIX_LD_LIBRARY_PATH_SYSTEM_ENV_BYTES: &[u8] = NIX_LD_LIBRARY_PATH_SYST
6264

6365
macro_rules! main_relocate_stack {
6466
($sp:ident, $func:ident) => {
65-
cfg_match::cfg_match! {
66-
target_arch = "x86_64" => {
67-
core::arch::asm!("mov rsp, {}; call {}", in(reg) $sp, sym $func, options(noreturn));
68-
}
69-
target_arch = "x86" => {
70-
core::arch::asm!("mov esp, {}; call {}", in(reg) $sp, sym $func, options(noreturn));
71-
}
72-
target_arch = "aarch64" => {
73-
core::arch::asm!("mov sp, {}; bl {}", in(reg) $sp, sym $func, options(noreturn));
74-
}
75-
}
67+
#[cfg(target_arch = "x86_64")]
68+
core::arch::asm!("mov rsp, {}; call {}", in(reg) $sp, sym $func, options(noreturn));
69+
#[cfg(target_arch = "x86")]
70+
core::arch::asm!("mov esp, {}; call {}", in(reg) $sp, sym $func, options(noreturn));
71+
#[cfg(target_arch = "aarch64")]
72+
core::arch::asm!("mov sp, {}; bl {}", in(reg) $sp, sym $func, options(noreturn));
7673
};
7774
}
7875
pub(crate) use main_relocate_stack;
7976

8077
macro_rules! elf_jmp {
8178
($sp:ident, $target:expr) => {
82-
cfg_match::cfg_match! {
83-
target_arch = "x86_64" => {
84-
core::arch::asm!("mov rsp, {}; jmp {}", in(reg) $sp, in(reg) $target, options(noreturn));
85-
}
86-
target_arch = "x86" => {
87-
core::arch::asm!("mov esp, {}; jmp {}", in(reg) $sp, in(reg) $target, options(noreturn));
88-
}
89-
target_arch = "aarch64" => {
90-
core::arch::asm!("mov sp, {}; br {}", in(reg) $sp, in(reg) $target, options(noreturn));
91-
}
92-
}
79+
#[cfg(target_arch = "x86_64")]
80+
core::arch::asm!("mov rsp, {}; jmp {}", in(reg) $sp, in(reg) $target, options(noreturn));
81+
#[cfg(target_arch = "x86")]
82+
core::arch::asm!("mov esp, {}; jmp {}", in(reg) $sp, in(reg) $target, options(noreturn));
83+
#[cfg(target_arch = "aarch64")]
84+
core::arch::asm!("mov sp, {}; br {}", in(reg) $sp, in(reg) $target, options(noreturn));
9385
};
9486
}
9587
pub(crate) use elf_jmp;
@@ -133,54 +125,59 @@ pub static mut TRAMPOLINE_CONTEXT: TrampolineContext = TrampolineContext {
133125
env_string: ptr::null(),
134126
};
135127

136-
cfg_match! {
137-
not(feature = "entry_trampoline") => {
138-
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = None;
139-
}
140-
target_arch = "x86_64" => {
141-
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = Some(entry_trampoline);
142-
143-
#[naked]
144-
unsafe extern "C" fn entry_trampoline() -> ! { unsafe {
145-
core::arch::naked_asm!(
146-
"lea r10, [rip + {context}]",
147-
"mov r11, [r10 + {size} * 1]", // .env_entry
148-
"test r11, r11",
149-
"jz 2f",
150-
"mov r10, [r10 + {size} * 2]", // .env_string
151-
"mov [r11], r10",
152-
"2:",
153-
"jmp [rip + {context}]",
154-
context = sym TRAMPOLINE_CONTEXT,
155-
size = const core::mem::size_of::<*const u8>(),
156-
)
157-
}}
158-
}
159-
target_arch = "aarch64" => {
160-
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = Some(entry_trampoline);
161-
162-
#[naked]
163-
unsafe extern "C" fn entry_trampoline() -> ! {
164-
core::arch::naked_asm!(
165-
"adrp x8, {context}",
166-
"ldr x9, [x8, {env_entry_off}]", // .env_entry
167-
"cbz x9, 2f",
168-
"ldr x10, [x8, {env_string_off}]", // .env_string
169-
"str x10, [x9]",
170-
"2:",
171-
"ldr x8, [x8]",
172-
"br x8",
173-
context = sym TRAMPOLINE_CONTEXT,
174-
env_entry_off = const TrampolineContext::ENV_ENTRY_OFFSET,
175-
env_string_off = const TrampolineContext::ENV_STRING_OFFSET,
176-
)
177-
}
178-
}
179-
// !!!!
180-
// After adding a trampoline, remember to enable test_ld_path_restore for
181-
// the target_arch in tests/tests.rs as well
182-
// !!!!
183-
_ => {
184-
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = None;
128+
#[cfg(not(feature = "entry_trampoline"))]
129+
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = None;
130+
131+
#[cfg(target_arch = "x86_64")]
132+
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = Some(entry_trampoline);
133+
134+
#[cfg(target_arch = "x86_64")]
135+
#[naked]
136+
unsafe extern "C" fn entry_trampoline() -> ! {
137+
unsafe {
138+
core::arch::naked_asm!(
139+
"lea r10, [rip + {context}]",
140+
"mov r11, [r10 + {size} * 1]", // .env_entry
141+
"test r11, r11",
142+
"jz 2f",
143+
"mov r10, [r10 + {size} * 2]", // .env_string
144+
"mov [r11], r10",
145+
"2:",
146+
"jmp [rip + {context}]",
147+
context = sym TRAMPOLINE_CONTEXT,
148+
size = const core::mem::size_of::<*const u8>(),
149+
)
185150
}
186151
}
152+
153+
#[cfg(target_arch = "aarch64")]
154+
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = Some(entry_trampoline);
155+
156+
#[cfg(target_arch = "aarch64")]
157+
#[naked]
158+
unsafe extern "C" fn entry_trampoline() -> ! {
159+
core::arch::naked_asm!(
160+
"adrp x8, {context}",
161+
"ldr x9, [x8, {env_entry_off}]", // .env_entry
162+
"cbz x9, 2f",
163+
"ldr x10, [x8, {env_string_off}]", // .env_string
164+
"str x10, [x9]",
165+
"2:",
166+
"ldr x8, [x8]",
167+
"br x8",
168+
context = sym TRAMPOLINE_CONTEXT,
169+
env_entry_off = const TrampolineContext::ENV_ENTRY_OFFSET,
170+
env_string_off = const TrampolineContext::ENV_STRING_OFFSET,
171+
)
172+
}
173+
174+
// !!!!
175+
// After adding a trampoline, remember to enable test_ld_path_restore for
176+
// the target_arch in tests/tests.rs as well
177+
// !!!!
178+
#[cfg(all(
179+
feature = "entry_trampoline",
180+
not(target_arch = "x86_64"),
181+
not(target_arch = "aarch64")
182+
))]
183+
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = None;

0 commit comments

Comments
 (0)