Skip to content

Commit 716ebce

Browse files
authored
Remove Function argument from define_function_bytes (#10346)
* Remove Function argument from define_function_bytes define_function_bytes is supposed to be used when the code was not compiled by Cranelift and thus wouldn't have any associated Function. * Avoid collecting the relocations into a Vec twice
1 parent 9ed0b18 commit 716ebce

File tree

3 files changed

+69
-67
lines changed

3 files changed

+69
-67
lines changed

cranelift/jit/src/backend.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{compiled_blob::CompiledBlob, memory::BranchProtection, memory::Memor
44
use cranelift_codegen::binemit::Reloc;
55
use cranelift_codegen::isa::{OwnedTargetIsa, TargetIsa};
66
use cranelift_codegen::settings::Configurable;
7-
use cranelift_codegen::{ir, settings, FinalizedMachReloc};
7+
use cranelift_codegen::{ir, settings};
88
use cranelift_control::ControlPlane;
99
use cranelift_entity::SecondaryMap;
1010
use cranelift_module::{
@@ -772,10 +772,9 @@ impl Module for JITModule {
772772
fn define_function_bytes(
773773
&mut self,
774774
id: FuncId,
775-
func: &ir::Function,
776775
alignment: u64,
777776
bytes: &[u8],
778-
relocs: &[FinalizedMachReloc],
777+
relocs: &[ModuleReloc],
779778
) -> ModuleResult<()> {
780779
info!("defining function {} with bytes", id);
781780
let decl = self.declarations.get_function_decl(id);
@@ -812,10 +811,7 @@ impl Module for JITModule {
812811
self.compiled_functions[id] = Some(CompiledBlob {
813812
ptr,
814813
size,
815-
relocs: relocs
816-
.iter()
817-
.map(|reloc| ModuleReloc::from_mach_reloc(reloc, func, id))
818-
.collect(),
814+
relocs: relocs.to_owned(),
819815
});
820816

821817
if self.isa.flags().is_pic() {

cranelift/module/src/module.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -974,10 +974,9 @@ pub trait Module {
974974
fn define_function_bytes(
975975
&mut self,
976976
func_id: FuncId,
977-
func: &ir::Function,
978977
alignment: u64,
979978
bytes: &[u8],
980-
relocs: &[FinalizedMachReloc],
979+
relocs: &[ModuleReloc],
981980
) -> ModuleResult<()>;
982981

983982
/// Define a data object, producing the data contents from the given `DataContext`.
@@ -1076,12 +1075,11 @@ impl<M: Module + ?Sized> Module for &mut M {
10761075
fn define_function_bytes(
10771076
&mut self,
10781077
func_id: FuncId,
1079-
func: &ir::Function,
10801078
alignment: u64,
10811079
bytes: &[u8],
1082-
relocs: &[FinalizedMachReloc],
1080+
relocs: &[ModuleReloc],
10831081
) -> ModuleResult<()> {
1084-
(**self).define_function_bytes(func_id, func, alignment, bytes, relocs)
1082+
(**self).define_function_bytes(func_id, alignment, bytes, relocs)
10851083
}
10861084

10871085
fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()> {
@@ -1181,12 +1179,11 @@ impl<M: Module + ?Sized> Module for Box<M> {
11811179
fn define_function_bytes(
11821180
&mut self,
11831181
func_id: FuncId,
1184-
func: &ir::Function,
11851182
alignment: u64,
11861183
bytes: &[u8],
1187-
relocs: &[FinalizedMachReloc],
1184+
relocs: &[ModuleReloc],
11881185
) -> ModuleResult<()> {
1189-
(**self).define_function_bytes(func_id, func, alignment, bytes, relocs)
1186+
(**self).define_function_bytes(func_id, alignment, bytes, relocs)
11901187
}
11911188

11921189
fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()> {

cranelift/object/src/backend.rs

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use anyhow::anyhow;
44
use cranelift_codegen::binemit::{Addend, CodeOffset, Reloc};
55
use cranelift_codegen::entity::SecondaryMap;
6+
use cranelift_codegen::ir;
67
use cranelift_codegen::isa::{OwnedTargetIsa, TargetIsa};
7-
use cranelift_codegen::{ir, FinalizedMachReloc};
88
use cranelift_control::ControlPlane;
99
use cranelift_module::{
1010
DataDescription, DataId, FuncId, Init, Linkage, Module, ModuleDeclarations, ModuleError,
@@ -345,65 +345,29 @@ impl Module for ObjectModule {
345345
let res = ctx.compile(self.isa(), ctrl_plane)?;
346346
let alignment = res.buffer.alignment as u64;
347347

348-
self.define_function_bytes(
349-
func_id,
350-
&ctx.func,
351-
alignment,
352-
ctx.compiled_code().unwrap().code_buffer(),
353-
ctx.compiled_code().unwrap().buffer.relocs(),
354-
)
348+
let buffer = &ctx.compiled_code().unwrap().buffer;
349+
let relocs = buffer
350+
.relocs()
351+
.iter()
352+
.map(|reloc| {
353+
self.process_reloc(&ModuleReloc::from_mach_reloc(&reloc, &ctx.func, func_id))
354+
})
355+
.collect::<Vec<_>>();
356+
self.define_function_inner(func_id, alignment, buffer.data(), relocs)
355357
}
356358

357359
fn define_function_bytes(
358360
&mut self,
359361
func_id: FuncId,
360-
func: &ir::Function,
361362
alignment: u64,
362363
bytes: &[u8],
363-
relocs: &[FinalizedMachReloc],
364+
relocs: &[ModuleReloc],
364365
) -> ModuleResult<()> {
365-
info!("defining function {} with bytes", func_id);
366-
let decl = self.declarations.get_function_decl(func_id);
367-
let decl_name = decl.linkage_name(func_id);
368-
if !decl.linkage.is_definable() {
369-
return Err(ModuleError::InvalidImportDefinition(decl_name.into_owned()));
370-
}
371-
372-
let &mut (symbol, ref mut defined) = self.functions[func_id].as_mut().unwrap();
373-
if *defined {
374-
return Err(ModuleError::DuplicateDefinition(decl_name.into_owned()));
375-
}
376-
*defined = true;
377-
378-
let align = alignment
379-
.max(self.isa.function_alignment().minimum.into())
380-
.max(self.isa.symbol_alignment());
381-
let section = if self.per_function_section {
382-
// FIXME pass empty symbol name once add_subsection produces `.text` as section name
383-
// instead of `.text.` when passed an empty symbol name. (object#748) Until then pass
384-
// `subsection` to produce `.text.subsection` as section name to reduce confusion.
385-
self.object
386-
.add_subsection(StandardSection::Text, b"subsection")
387-
} else {
388-
self.object.section_id(StandardSection::Text)
389-
};
390-
let offset = self.object.add_symbol_data(symbol, section, bytes, align);
391-
392-
if !relocs.is_empty() {
393-
let relocs = relocs
394-
.iter()
395-
.map(|record| {
396-
self.process_reloc(&ModuleReloc::from_mach_reloc(&record, func, func_id))
397-
})
398-
.collect();
399-
self.relocs.push(SymbolRelocs {
400-
section,
401-
offset,
402-
relocs,
403-
});
404-
}
405-
406-
Ok(())
366+
let relocs = relocs
367+
.iter()
368+
.map(|reloc| self.process_reloc(reloc))
369+
.collect();
370+
self.define_function_inner(func_id, alignment, bytes, relocs)
407371
}
408372

409373
fn define_data(&mut self, data_id: DataId, data: &DataDescription) -> ModuleResult<()> {
@@ -511,6 +475,51 @@ impl Module for ObjectModule {
511475
}
512476

513477
impl ObjectModule {
478+
fn define_function_inner(
479+
&mut self,
480+
func_id: FuncId,
481+
alignment: u64,
482+
bytes: &[u8],
483+
relocs: Vec<ObjectRelocRecord>,
484+
) -> Result<(), ModuleError> {
485+
info!("defining function {} with bytes", func_id);
486+
let decl = self.declarations.get_function_decl(func_id);
487+
let decl_name = decl.linkage_name(func_id);
488+
if !decl.linkage.is_definable() {
489+
return Err(ModuleError::InvalidImportDefinition(decl_name.into_owned()));
490+
}
491+
492+
let &mut (symbol, ref mut defined) = self.functions[func_id].as_mut().unwrap();
493+
if *defined {
494+
return Err(ModuleError::DuplicateDefinition(decl_name.into_owned()));
495+
}
496+
*defined = true;
497+
498+
let align = alignment
499+
.max(self.isa.function_alignment().minimum.into())
500+
.max(self.isa.symbol_alignment());
501+
let section = if self.per_function_section {
502+
// FIXME pass empty symbol name once add_subsection produces `.text` as section name
503+
// instead of `.text.` when passed an empty symbol name. (object#748) Until then pass
504+
// `subsection` to produce `.text.subsection` as section name to reduce confusion.
505+
self.object
506+
.add_subsection(StandardSection::Text, b"subsection")
507+
} else {
508+
self.object.section_id(StandardSection::Text)
509+
};
510+
let offset = self.object.add_symbol_data(symbol, section, bytes, align);
511+
512+
if !relocs.is_empty() {
513+
self.relocs.push(SymbolRelocs {
514+
section,
515+
offset,
516+
relocs,
517+
});
518+
}
519+
520+
Ok(())
521+
}
522+
514523
/// Finalize all relocations and output an object.
515524
pub fn finish(mut self) -> ObjectProduct {
516525
let symbol_relocs = mem::take(&mut self.relocs);

0 commit comments

Comments
 (0)