Merge pull request #1836 from carlokok/feature/object_file_section

Cranelift: Module data apis should allow specifying the object file section
This commit is contained in:
Pat Hickey
2020-06-17 10:49:09 -07:00
committed by GitHub
4 changed files with 52 additions and 13 deletions

View File

@@ -211,8 +211,17 @@ impl Backend for FaerieBackend {
ref data_decls, ref data_decls,
ref function_relocs, ref function_relocs,
ref data_relocs, ref data_relocs,
ref custom_segment_section,
} = data_ctx.description(); } = data_ctx.description();
if let Some((segment, section)) = custom_segment_section {
return Err(cranelift_module::ModuleError::Backend(anyhow::anyhow!(
"Custom section not supported by cranelift-faerie: `{}:{}`",
segment,
section
)));
}
for &(offset, id) in function_relocs { for &(offset, id) in function_relocs {
let to = &namespace.get_function_decl(&function_decls[id]).name; let to = &namespace.get_function_decl(&function_decls[id]).name;
self.artifact self.artifact

View File

@@ -3,7 +3,9 @@
use cranelift_codegen::binemit::{Addend, CodeOffset}; use cranelift_codegen::binemit::{Addend, CodeOffset};
use cranelift_codegen::entity::PrimaryMap; use cranelift_codegen::entity::PrimaryMap;
use cranelift_codegen::ir; use cranelift_codegen::ir;
use std::borrow::ToOwned;
use std::boxed::Box; use std::boxed::Box;
use std::string::String;
use std::vec::Vec; use std::vec::Vec;
/// This specifies how data is to be initialized. /// This specifies how data is to be initialized.
@@ -46,6 +48,8 @@ pub struct DataDescription {
pub function_relocs: Vec<(CodeOffset, ir::FuncRef)>, pub function_relocs: Vec<(CodeOffset, ir::FuncRef)>,
/// Data addresses to write at specified offsets. /// Data addresses to write at specified offsets.
pub data_relocs: Vec<(CodeOffset, ir::GlobalValue, Addend)>, pub data_relocs: Vec<(CodeOffset, ir::GlobalValue, Addend)>,
/// Object file section
pub custom_segment_section: Option<(String, String)>,
} }
/// This is to data objects what cranelift_codegen::Context is to functions. /// This is to data objects what cranelift_codegen::Context is to functions.
@@ -63,6 +67,7 @@ impl DataContext {
data_decls: PrimaryMap::new(), data_decls: PrimaryMap::new(),
function_relocs: vec![], function_relocs: vec![],
data_relocs: vec![], data_relocs: vec![],
custom_segment_section: None,
}, },
} }
} }
@@ -90,6 +95,11 @@ impl DataContext {
self.description.init = Init::Bytes { contents }; self.description.init = Init::Bytes { contents };
} }
/// Override the segment/section for data, only supported on Object backend
pub fn set_segment_section(&mut self, seg: &str, sec: &str) {
self.description.custom_segment_section = Some((seg.to_owned(), sec.to_owned()))
}
/// Declare an external function import. /// Declare an external function import.
/// ///
/// Users of the `Module` API generally should call /// Users of the `Module` API generally should call

View File

@@ -271,6 +271,7 @@ impl Backend for ObjectBackend {
ref data_decls, ref data_decls,
ref function_relocs, ref function_relocs,
ref data_relocs, ref data_relocs,
ref custom_segment_section,
} = data_ctx.description(); } = data_ctx.description();
let reloc_size = match self.isa.triple().pointer_width().unwrap() { let reloc_size = match self.isa.triple().pointer_width().unwrap() {
@@ -301,6 +302,7 @@ impl Backend for ObjectBackend {
} }
let symbol = self.data_objects[data_id].unwrap(); let symbol = self.data_objects[data_id].unwrap();
let section = if custom_segment_section.is_none() {
let section_kind = if let Init::Zeros { .. } = *init { let section_kind = if let Init::Zeros { .. } = *init {
if tls { if tls {
StandardSection::UninitializedTls StandardSection::UninitializedTls
@@ -316,7 +318,24 @@ impl Backend for ObjectBackend {
} else { } else {
StandardSection::ReadOnlyDataWithRel StandardSection::ReadOnlyDataWithRel
}; };
let section = self.object.section_id(section_kind); self.object.section_id(section_kind)
} else {
if tls {
return Err(cranelift_module::ModuleError::Backend(anyhow::anyhow!(
"Custom section not supported for TLS"
)));
}
let (seg, sec) = &custom_segment_section.as_ref().unwrap();
self.object.add_section(
seg.clone().into_bytes(),
sec.clone().into_bytes(),
if writable {
SectionKind::Data
} else {
SectionKind::ReadOnlyData
},
)
};
let align = u64::from(align.unwrap_or(1)); let align = u64::from(align.unwrap_or(1));
let offset = match *init { let offset = match *init {

View File

@@ -361,6 +361,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
ref data_decls, ref data_decls,
ref function_relocs, ref function_relocs,
ref data_relocs, ref data_relocs,
custom_segment_section: _,
} = data.description(); } = data.description();
let size = init.size(); let size = init.size();