Parse fewer names in linking (#3226)

We don't need an auxiliary map to tell us function addresses, we can
query the symbol instead.
This commit is contained in:
Alex Crichton
2021-08-23 14:35:48 -05:00
committed by GitHub
parent 925b771d2d
commit 22ab535ad9
2 changed files with 14 additions and 37 deletions

View File

@@ -426,12 +426,7 @@ fn build_code_memory(
trampolines.push((i, fnptr)); trampolines.push((i, fnptr));
} }
link_module( link_module(&allocation.obj, allocation.code_range);
&allocation.obj,
&module,
allocation.code_range,
&finished_functions,
);
let code_range = (allocation.code_range.as_ptr(), allocation.code_range.len()); let code_range = (allocation.code_range.as_ptr(), allocation.code_range.len());

View File

@@ -3,8 +3,6 @@
use object::read::{Object, ObjectSection, Relocation, RelocationTarget}; use object::read::{Object, ObjectSection, Relocation, RelocationTarget};
use object::{elf, File, ObjectSymbol, RelocationEncoding, RelocationKind}; use object::{elf, File, ObjectSymbol, RelocationEncoding, RelocationKind};
use std::ptr::{read_unaligned, write_unaligned}; use std::ptr::{read_unaligned, write_unaligned};
use wasmtime_environ::obj::try_parse_func_name;
use wasmtime_environ::{DefinedFuncIndex, Module, PrimaryMap};
use wasmtime_runtime::libcalls; use wasmtime_runtime::libcalls;
use wasmtime_runtime::VMFunctionBody; use wasmtime_runtime::VMFunctionBody;
@@ -16,45 +14,28 @@ use wasmtime_runtime::VMFunctionBody;
/// Currently, the produced ELF image can be trusted. /// Currently, the produced ELF image can be trusted.
/// TODO refactor logic to remove panics and add defensive code the image data /// TODO refactor logic to remove panics and add defensive code the image data
/// becomes untrusted. /// becomes untrusted.
pub fn link_module( pub fn link_module(obj: &File, code_range: &mut [u8]) {
obj: &File,
module: &Module,
code_range: &mut [u8],
finished_functions: &PrimaryMap<DefinedFuncIndex, *mut [VMFunctionBody]>,
) {
// Read the ".text" section and process its relocations. // Read the ".text" section and process its relocations.
let text_section = obj.section_by_name(".text").unwrap(); let text_section = obj.section_by_name(".text").unwrap();
let body = code_range.as_ptr() as *const VMFunctionBody; let body = code_range.as_ptr() as *const VMFunctionBody;
for (offset, r) in text_section.relocations() { for (offset, r) in text_section.relocations() {
apply_reloc(module, obj, finished_functions, body, offset, r); apply_reloc(obj, body, offset, r);
} }
} }
fn apply_reloc( fn apply_reloc(obj: &File, body: *const VMFunctionBody, offset: u64, r: Relocation) {
module: &Module,
obj: &File,
finished_functions: &PrimaryMap<DefinedFuncIndex, *mut [VMFunctionBody]>,
body: *const VMFunctionBody,
offset: u64,
r: Relocation,
) {
let target_func_address: usize = match r.target() { let target_func_address: usize = match r.target() {
RelocationTarget::Symbol(i) => { RelocationTarget::Symbol(i) => {
// Processing relocation target is a named symbols that is compiled // Processing relocation target is a named symbols that is compiled
// wasm function or runtime libcall. // wasm function or runtime libcall.
let sym = obj.symbol_by_index(i).unwrap(); let sym = obj.symbol_by_index(i).unwrap();
if sym.is_local() {
unsafe { body.add(sym.address() as usize) as usize }
} else {
match sym.name() { match sym.name() {
Ok(name) => { Ok(name) => {
if let Some(index) = try_parse_func_name(name) { if let Some(addr) = to_libcall_address(name) {
match module.defined_func_index(index) {
Some(f) => {
let fatptr: *const [VMFunctionBody] = finished_functions[f];
fatptr as *const VMFunctionBody as usize
}
None => panic!("direct call to import"),
}
} else if let Some(addr) = to_libcall_address(name) {
addr addr
} else { } else {
panic!("unknown function to link: {}", name); panic!("unknown function to link: {}", name);
@@ -63,6 +44,7 @@ fn apply_reloc(
Err(_) => panic!("unexpected relocation target: not a symbol"), Err(_) => panic!("unexpected relocation target: not a symbol"),
} }
} }
}
_ => panic!("unexpected relocation target"), _ => panic!("unexpected relocation target"),
}; };