Skip to content

Commit

Permalink
codegen: fix i686-unknown-linux-gnu
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongkeunahn committed Dec 14, 2024
1 parent f2557bb commit 14cb9e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
35 changes: 13 additions & 22 deletions basm-std/src/platform/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,14 @@ pub unsafe extern "win64" fn _basm_start() -> ! {
}

#[cfg(target_arch = "x86")]
#[unsafe(no_mangle)]
#[naked]
#[unsafe(link_section = ".data")]
unsafe extern "cdecl" fn _get_start_offset() -> ! {
unsafe {
naked_asm!("lea eax, [_basm_start]", "ret");
}
extern "cdecl" fn _get_start_offset() -> u32 {
_basm_start as u32
}

#[cfg(target_arch = "x86")]
#[unsafe(no_mangle)]
#[naked]
#[unsafe(link_section = ".data")]
unsafe extern "cdecl" fn _get_dynamic_section_offset() -> ! {
unsafe {
naked_asm!("lea eax, [_DYNAMIC]", "ret");
}
extern "cdecl" fn _get_dynamic_section_offset() -> u32 {
unsafe extern "C" { fn _DYNAMIC(); }
_DYNAMIC as u32
}

#[cfg(target_arch = "x86")]
Expand Down Expand Up @@ -227,17 +218,17 @@ pub unsafe extern "cdecl" fn _basm_start() -> ! {
"3:",
"call 4f",
"4:",
"pop ecx", // ecx = _basm_start + 36 (obtained by counting the opcode size in bytes)
"push edx", // [esp + 0] = PLATFORM_DATA table
"pop ebx", // ebx = _basm_start + 36 (obtained by counting the opcode size in bytes)
"push edx", // [esp + 0] = PLATFORM_DATA table, stack is aligned
"call {2}", // eax = offset of _basm_start from the image base
"sub ecx, eax",
"sub ecx, 36", // ecx = the in-memory image base (i.e., __ehdr_start)
"sub ebx, eax",
"sub ebx, 36", // ebx = the in-memory image base (i.e., __ehdr_start)
"call {3}", // eax = offset of _DYNAMIC table from the image base
"add eax, ecx", // eax = _DYNAMIC table
"lea esi, [ebx + eax]", // esi = _DYNAMIC table
"sub esp, 8", // For stack alignment
"push eax",
"push ecx",
"call {0}",
"push esi",
"push ebx",
"call {0}", // call loader::i686_elf::relocate
"add esp, 16",
"call {1}",
"mov esp, ebp",
Expand Down
8 changes: 1 addition & 7 deletions basm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ fn main() {
link_args_basm.push("-fno-unwind-tables");
link_args_basm.push("-fno-stack-protector");
link_args_basm.push("-fno-plt");
if target == "i686-unknown-linux-gnu" {
// Prevent linker from putting data into text, which is non-writable and hence not relocatable.
// This prevents the hack for getting the _DYNAMIC symbol in the entrypoint.
link_args_basm.push("-Wl,--entry=_basm_start,--build-id=none,--gc-sections,--export-dynamic,--no-eh-frame-hdr,-z,norelro,-z,notext");
} else {
link_args_basm.push("-Wl,--entry=_basm_start,--build-id=none,--gc-sections,--export-dynamic,--no-eh-frame-hdr,-z,norelro");
}
link_args_basm.push("-Wl,--entry=_basm_start,--build-id=none,--gc-sections,--export-dynamic,--no-eh-frame-hdr,-z,norelro");
link_args_basm_submit.push("-Wl,-z,max-page-size=128");
}
"aarch64-apple-darwin" => {
Expand Down

0 comments on commit 14cb9e3

Please sign in to comment.