Skip to content

Commit 01699d3

Browse files
committed
fix clippy lints
1 parent 09ea500 commit 01699d3

File tree

6 files changed

+85
-72
lines changed

6 files changed

+85
-72
lines changed

src/arch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ cfg_match! {
141141
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = Some(entry_trampoline);
142142

143143
#[naked]
144-
unsafe extern "C" fn entry_trampoline() -> ! {
144+
unsafe extern "C" fn entry_trampoline() -> ! { unsafe {
145145
core::arch::naked_asm!(
146146
"lea r10, [rip + {context}]",
147147
"mov r11, [r10 + {size} * 1]", // .env_entry
@@ -154,7 +154,7 @@ cfg_match! {
154154
context = sym TRAMPOLINE_CONTEXT,
155155
size = const core::mem::size_of::<*const u8>(),
156156
)
157-
}
157+
}}
158158
}
159159
target_arch = "aarch64" => {
160160
pub const ENTRY_TRAMPOLINE: Option<unsafe extern "C" fn() -> !> = Some(entry_trampoline);

src/args.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,18 @@ impl CStrExt for CStr {
8484

8585
impl Args {
8686
pub unsafe fn new(argc: usize, argv: *const *const u8, envp: *const *const u8) -> Self {
87-
let (envc, auxv) = count_env(envp);
88-
89-
Self {
90-
env_iterated: false,
91-
argc,
92-
argv,
93-
envp,
94-
envc,
95-
auxv: AuxVec::from_raw(auxv),
96-
extra_env: None,
87+
unsafe {
88+
let (envc, auxv) = count_env(envp);
89+
90+
Self {
91+
env_iterated: false,
92+
argc,
93+
argv,
94+
envp,
95+
envc,
96+
auxv: AuxVec::from_raw(auxv),
97+
extra_env: None,
98+
}
9799
}
98100
}
99101

@@ -390,11 +392,13 @@ impl<'a> StackShifter<'a> {
390392
}
391393

392394
unsafe fn count_env(envp: *const *const u8) -> (usize, *const usize) {
393-
let mut envc = 0;
394-
let mut cur = envp;
395-
while !(*cur).is_null() {
396-
cur = cur.add(1);
397-
envc += 1;
395+
unsafe {
396+
let mut envc = 0;
397+
let mut cur = envp;
398+
while !(*cur).is_null() {
399+
cur = cur.add(1);
400+
envc += 1;
401+
}
402+
(envc, cur.add(1) as *const usize)
398403
}
399-
(envc, cur.add(1) as *const usize)
400404
}

src/elf.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! ELF wrangling.
22

3-
use core::ffi::{c_void, CStr};
3+
use core::ffi::{CStr, c_void};
44
use core::fmt;
55
use core::mem;
66
use core::ptr;
77

88
pub use crate::arch::elf_types;
99
use crate::arch::elf_types::{
10-
header::{Header, ET_DYN},
11-
program_header::{ProgramHeader, PF_R, PF_W, PF_X, PT_LOAD},
10+
header::{ET_DYN, Header},
11+
program_header::{PF_R, PF_W, PF_X, PT_LOAD, ProgramHeader},
1212
};
13-
use crate::arch::{elf_jmp, EM_SELF};
13+
use crate::arch::{EM_SELF, elf_jmp};
1414
#[rustfmt::skip]
1515
use crate::sys::{
1616
self, errno, Error as IoError, File, Read,
@@ -302,7 +302,9 @@ impl ElfMapping {
302302

303303
/// Jumps to the entry point with a stack.
304304
pub unsafe fn jump_with_sp(self, sp: *const c_void) -> ! {
305-
elf_jmp!(sp, self.entry_point);
305+
unsafe {
306+
elf_jmp!(sp, self.entry_point);
307+
}
306308
}
307309
}
308310

src/fixup.rs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use core::slice;
99
use crate::arch::R_RELATIVE;
1010
use crate::auxv::AuxVec;
1111
use crate::elf::{
12-
elf_types,
12+
ProgramHeaders, elf_types,
1313
elf_types::dynamic::{DT_NULL, DT_REL, DT_RELA, DT_RELAENT, DT_RELASZ, DT_RELENT, DT_RELSZ},
1414
elf_types::header::Header,
1515
elf_types::program_header::PT_DYNAMIC,
16-
ProgramHeaders,
1716
};
1817
use crate::support::explode;
1918

@@ -119,54 +118,61 @@ impl Dynamic {
119118
}
120119

121120
pub unsafe fn fixup_relocs(envp: *const *const u8) {
122-
// Reference: <https://gist.github.com/Amanieu/588e3f9d330019c5d39f3ce60e8e0aae>
123-
let auxv = find_auxv(envp);
124-
let auxv = AuxVec::from_raw(auxv);
125-
126-
let at_base = auxv.at_base.as_ref().map_or_else(ptr::null, |v| v.value());
127-
let (load_offset, phs) = if at_base.is_null() {
128-
// We were executed directly
129-
if let (Some(phdr), Some(phent), Some(phnum)) =
130-
(&auxv.at_phdr, &auxv.at_phent, &auxv.at_phnum)
131-
{
132-
if phdr.value().is_null() {
133-
explode("AT_PHDR is null");
121+
unsafe {
122+
// Reference: <https://gist.github.com/Amanieu/588e3f9d330019c5d39f3ce60e8e0aae>
123+
let auxv = find_auxv(envp);
124+
let auxv = AuxVec::from_raw(auxv);
125+
126+
let at_base = auxv.at_base.as_ref().map_or_else(ptr::null, |v| v.value());
127+
let (load_offset, phs) = if at_base.is_null() {
128+
// We were executed directly
129+
if let (Some(phdr), Some(phent), Some(phnum)) =
130+
(&auxv.at_phdr, &auxv.at_phent, &auxv.at_phnum)
131+
{
132+
if phdr.value().is_null() {
133+
explode("AT_PHDR is null");
134+
}
135+
let phs = ProgramHeaders::from_raw(phdr.value(), phent.value(), phnum.value());
136+
let load_offset = phdr.value() as usize - mem::size_of::<Header>();
137+
(load_offset, phs)
138+
} else {
139+
explode("AT_PHDR, AT_PHENT, AT_PHNUM must exist");
134140
}
135-
let phs = ProgramHeaders::from_raw(phdr.value(), phent.value(), phnum.value());
136-
let load_offset = phdr.value() as usize - mem::size_of::<Header>();
137-
(load_offset, phs)
138141
} else {
139-
explode("AT_PHDR, AT_PHENT, AT_PHNUM must exist");
140-
}
141-
} else {
142-
// We are the loader
143-
let ehdr: *const Header = at_base.cast();
144-
let header: &Header = unsafe { &*ehdr };
142+
// We are the loader
143+
let ehdr: *const Header = at_base.cast();
144+
let header: &Header = &*ehdr;
145145

146-
if &header.e_ident[..4] != b"\x7fELF".as_slice() {
147-
explode("We are not an ELF ourselves");
148-
}
146+
if &header.e_ident[..4] != b"\x7fELF".as_slice() {
147+
explode("We are not an ELF ourselves");
148+
}
149149

150-
let phdr = unsafe { ehdr.add(1).cast() };
150+
let phdr = ehdr.add(1).cast();
151151

152-
let phs =
153-
ProgramHeaders::from_raw(phdr, header.e_phentsize as usize, header.e_phnum as usize);
154-
(at_base as usize, phs)
155-
};
152+
let phs = ProgramHeaders::from_raw(
153+
phdr,
154+
header.e_phentsize as usize,
155+
header.e_phnum as usize,
156+
);
157+
(at_base as usize, phs)
158+
};
156159

157-
let dynamic = if let Some(dynamic) = Dynamic::from_program_headers(&phs, load_offset) {
158-
dynamic
159-
} else {
160-
explode("No dynamic section in own executable");
161-
};
160+
let dynamic = if let Some(dynamic) = Dynamic::from_program_headers(&phs, load_offset) {
161+
dynamic
162+
} else {
163+
explode("No dynamic section in own executable");
164+
};
162165

163-
dynamic.fixup();
166+
dynamic.fixup();
167+
}
164168
}
165169

166170
unsafe fn find_auxv(envp: *const *const u8) -> *const usize {
167-
let mut cur = envp;
168-
while !(*cur).is_null() {
169-
cur = cur.add(1);
171+
unsafe {
172+
let mut cur = envp;
173+
while !(*cur).is_null() {
174+
cur = cur.add(1);
175+
}
176+
cur.add(1) as *const usize
170177
}
171-
cur.add(1) as *const usize
172178
}

src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod fixup;
1313
mod support;
1414
mod sys;
1515

16-
use core::ffi::{c_void, CStr};
16+
use core::ffi::{CStr, c_void};
1717
use core::mem::MaybeUninit;
1818
use core::ptr;
1919

@@ -49,11 +49,13 @@ struct Context {
4949

5050
#[unsafe(no_mangle)]
5151
unsafe extern "C" fn main(argc: usize, argv: *const *const u8, envp: *const *const u8) -> ! {
52-
fixup::fixup_relocs(envp);
52+
unsafe {
53+
fixup::fixup_relocs(envp);
5354

54-
ARGS.write(Args::new(argc, argv, envp));
55-
let stack = STACK.assume_init_mut().bottom();
56-
arch::main_relocate_stack!(stack, real_main);
55+
ARGS.write(Args::new(argc, argv, envp));
56+
let stack = STACK.assume_init_mut().bottom();
57+
arch::main_relocate_stack!(stack, real_main);
58+
}
5759
}
5860

5961
#[unsafe(no_mangle)]

src/sys.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! This dependency may be reduced further. For memory operations,
1010
//! compiler-builtins provides faster implementations.
1111

12-
use core::ffi::{c_char, c_int, c_void, CStr};
12+
use core::ffi::{CStr, c_char, c_int, c_void};
1313
use core::fmt;
1414
use core::ptr;
1515
use core::slice;
@@ -182,5 +182,4 @@ pub fn new_slice_leak(size: usize) -> Option<&'static mut [u8]> {
182182
#[cfg(not(test))]
183183
#[lang = "eh_personality"]
184184
#[unsafe(no_mangle)]
185-
pub extern "C" fn rust_eh_personality() {
186-
}
185+
pub extern "C" fn rust_eh_personality() {}

0 commit comments

Comments
 (0)