Cranelift: Module data apis should allow specifying the object file section #1640

This commit is contained in:
Carlo Kok
2020-06-06 16:08:21 +02:00
parent 4bb58940c7
commit 6b47079884
2 changed files with 32 additions and 15 deletions

View File

@@ -46,6 +46,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 section: Option<(std::string::String, std::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 +65,7 @@ impl DataContext {
data_decls: PrimaryMap::new(), data_decls: PrimaryMap::new(),
function_relocs: vec![], function_relocs: vec![],
data_relocs: vec![], data_relocs: vec![],
section: None
}, },
} }
} }
@@ -90,6 +93,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_section(&mut self, seg: &str, sec: &str) {
self.description.section = Some((std::string::String::from(seg), std::string::String::from(sec)))
}
/// 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

@@ -234,6 +234,7 @@ impl Backend for ObjectBackend {
ref data_decls, ref data_decls,
ref function_relocs, ref function_relocs,
ref data_relocs, ref data_relocs,
section: ref datasection
} = 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() {
@@ -264,22 +265,30 @@ impl Backend for ObjectBackend {
} }
let symbol = self.data_objects[data_id].unwrap(); let symbol = self.data_objects[data_id].unwrap();
let section_kind = if let Init::Zeros { .. } = *init { let section =
if tls { if datasection.is_none() {
StandardSection::UninitializedTls let section_kind = if let Init::Zeros { .. } = *init {
if tls {
StandardSection::UninitializedTls
} else {
StandardSection::UninitializedData
}
} else if tls {
StandardSection::Tls
} else if writable {
StandardSection::Data
} else if relocs.is_empty() {
StandardSection::ReadOnlyData
} else {
StandardSection::ReadOnlyDataWithRel
};
self.object.section_id(section_kind)
} else { } else {
StandardSection::UninitializedData let (seg, sec) = &datasection.as_ref().unwrap();
} self.object.add_section(seg.clone().into_bytes(), sec.clone().into_bytes(),
} else if tls { if writable { SectionKind::Data } else { SectionKind::ReadOnlyData }
StandardSection::Tls )
} else if writable { };
StandardSection::Data
} else if relocs.is_empty() {
StandardSection::ReadOnlyData
} else {
StandardSection::ReadOnlyDataWithRel
};
let section = self.object.section_id(section_kind);
let align = u64::from(align.unwrap_or(1)); let align = u64::from(align.unwrap_or(1));
let offset = match *init { let offset = match *init {