mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
redirect libc functions to their roc equivalents
This commit is contained in:
parent
52404a4085
commit
2ad7dc0ca0
4 changed files with 23 additions and 20 deletions
|
@ -20,6 +20,7 @@ use crate::metadata::{self, Metadata, VirtualOffset};
|
||||||
use crate::{
|
use crate::{
|
||||||
align_by_constraint, align_to_offset_by_constraint, load_struct_inplace,
|
align_by_constraint, align_to_offset_by_constraint, load_struct_inplace,
|
||||||
load_struct_inplace_mut, load_structs_inplace_mut, open_mmap, open_mmap_mut,
|
load_struct_inplace_mut, load_structs_inplace_mut, open_mmap, open_mmap_mut,
|
||||||
|
redirect_libc_functions,
|
||||||
};
|
};
|
||||||
|
|
||||||
const MIN_SECTION_ALIGNMENT: usize = 0x40;
|
const MIN_SECTION_ALIGNMENT: usize = 0x40;
|
||||||
|
@ -77,12 +78,8 @@ fn collect_roc_definitions<'a>(object: &object::File<'a, &'a [u8]>) -> MutMap<St
|
||||||
let address = sym.address() as u64;
|
let address = sym.address() as u64;
|
||||||
|
|
||||||
// special exceptions for memcpy and memset.
|
// special exceptions for memcpy and memset.
|
||||||
if name == "roc_memcpy" {
|
if let Some(name) = redirect_libc_functions(name) {
|
||||||
vaddresses.insert("memcpy".to_string(), address);
|
vaddresses.insert(name.to_string(), address);
|
||||||
} else if name == "roc_memset" {
|
|
||||||
vaddresses.insert("memset".to_string(), address);
|
|
||||||
} else if name == "roc_memmove" {
|
|
||||||
vaddresses.insert("memmove".to_string(), address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vaddresses.insert(name.to_string(), address);
|
vaddresses.insert(name.to_string(), address);
|
||||||
|
|
|
@ -377,6 +377,16 @@ fn surgery(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Redirect `memcpy` and similar libc functions to their roc equivalents
|
||||||
|
pub(crate) fn redirect_libc_functions(name: &str) -> Option<&str> {
|
||||||
|
match name {
|
||||||
|
"memcpy" => Some("roc_memcpy"),
|
||||||
|
"memset" => Some("roc_memset"),
|
||||||
|
"memmove" => Some("roc_memmove"),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn align_by_constraint(offset: usize, constraint: usize) -> usize {
|
pub(crate) fn align_by_constraint(offset: usize, constraint: usize) -> usize {
|
||||||
if offset % constraint == 0 {
|
if offset % constraint == 0 {
|
||||||
offset
|
offset
|
||||||
|
|
|
@ -18,7 +18,7 @@ use target_lexicon::Triple;
|
||||||
use crate::{
|
use crate::{
|
||||||
align_by_constraint, align_to_offset_by_constraint, load_struct_inplace,
|
align_by_constraint, align_to_offset_by_constraint, load_struct_inplace,
|
||||||
load_struct_inplace_mut, load_structs_inplace, load_structs_inplace_mut, open_mmap,
|
load_struct_inplace_mut, load_structs_inplace, load_structs_inplace_mut, open_mmap,
|
||||||
open_mmap_mut,
|
open_mmap_mut, redirect_libc_functions,
|
||||||
};
|
};
|
||||||
|
|
||||||
const MIN_SECTION_ALIGNMENT: usize = 0x40;
|
const MIN_SECTION_ALIGNMENT: usize = 0x40;
|
||||||
|
@ -70,10 +70,8 @@ fn collect_roc_definitions<'a>(object: &object::File<'a, &'a [u8]>) -> MutMap<St
|
||||||
let address = sym.address() as u64;
|
let address = sym.address() as u64;
|
||||||
|
|
||||||
// special exceptions for memcpy and memset.
|
// special exceptions for memcpy and memset.
|
||||||
if name == "roc_memcpy" {
|
if let Some(name) = redirect_libc_functions(name) {
|
||||||
vaddresses.insert("memcpy".to_string(), address);
|
vaddresses.insert(name.to_string(), address);
|
||||||
} else if name == "roc_memset" {
|
|
||||||
vaddresses.insert("memset".to_string(), address);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vaddresses.insert(name.to_string(), address);
|
vaddresses.insert(name.to_string(), address);
|
||||||
|
|
|
@ -21,7 +21,7 @@ use roc_error_macros::internal_error;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
generate_dylib::APP_DLL, load_struct_inplace, load_struct_inplace_mut,
|
generate_dylib::APP_DLL, load_struct_inplace, load_struct_inplace_mut,
|
||||||
load_structs_inplace_mut, open_mmap, open_mmap_mut,
|
load_structs_inplace_mut, open_mmap, open_mmap_mut, redirect_libc_functions,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The metadata stores information about/from the host .exe because
|
/// The metadata stores information about/from the host .exe because
|
||||||
|
@ -1062,15 +1062,13 @@ impl AppSections {
|
||||||
|
|
||||||
let address = symbol.as_ref().map(|s| s.address()).unwrap_or_default();
|
let address = symbol.as_ref().map(|s| s.address()).unwrap_or_default();
|
||||||
let name = symbol.and_then(|s| s.name()).unwrap_or_default();
|
let name = symbol.and_then(|s| s.name()).unwrap_or_default();
|
||||||
|
let name = redirect_libc_functions(name).unwrap_or(name).to_string();
|
||||||
|
|
||||||
relocations
|
relocations.entry(name).or_default().push(AppRelocation {
|
||||||
.entry(name.to_string())
|
offset_in_section,
|
||||||
.or_default()
|
address,
|
||||||
.push(AppRelocation {
|
relocation,
|
||||||
offset_in_section,
|
});
|
||||||
address,
|
|
||||||
relocation,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue