Skip to content

Commit 2947208

Browse files
authored
Allow passing Mach-O flags to set_segment_section (#13137)
This enables `rustc_codegen_cranelift` to control these.
1 parent 5cf8974 commit 2947208

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

cranelift/module/src/data_context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub struct DataDescription {
6060
pub function_relocs: Vec<(CodeOffset, ir::FuncRef)>,
6161
/// Data addresses to write at specified offsets.
6262
pub data_relocs: Vec<(CodeOffset, ir::GlobalValue, Addend)>,
63-
/// Object file section
64-
pub custom_segment_section: Option<(String, String)>,
63+
/// Object file segment, section and Mach-O flags.
64+
pub custom_segment_section: Option<(String, String, u32)>,
6565
/// Alignment in bytes. `None` means that the default alignment of the
6666
/// respective module should be used.
6767
pub align: Option<u64>,
@@ -112,8 +112,8 @@ impl DataDescription {
112112
}
113113

114114
/// Override the segment/section for data, only supported on Object backend
115-
pub fn set_segment_section(&mut self, seg: &str, sec: &str) {
116-
self.custom_segment_section = Some((seg.to_owned(), sec.to_owned()))
115+
pub fn set_segment_section(&mut self, seg: &str, sec: &str, macho_flags: u32) {
116+
self.custom_segment_section = Some((seg.to_owned(), sec.to_owned(), macho_flags))
117117
}
118118

119119
/// Set the alignment for data. The alignment must be a power of two.

cranelift/object/src/backend.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ impl Module for ObjectModule {
503503
"Custom section not supported for TLS"
504504
)));
505505
}
506-
let (seg, sec) = &custom_segment_section.as_ref().unwrap();
507-
self.object.add_section(
506+
let (seg, sec, macho_flags) = &custom_segment_section.as_ref().unwrap();
507+
let section = self.object.add_section(
508508
seg.clone().into_bytes(),
509509
sec.clone().into_bytes(),
510510
if decl.writable {
@@ -514,7 +514,22 @@ impl Module for ObjectModule {
514514
} else {
515515
SectionKind::ReadOnlyDataWithRel
516516
},
517-
)
517+
);
518+
519+
match self.object.section_flags_mut(section) {
520+
SectionFlags::MachO { flags } => {
521+
*flags = *macho_flags;
522+
}
523+
_ => {
524+
if *macho_flags != 0 {
525+
return Err(cranelift_module::ModuleError::Backend(anyhow::anyhow!(
526+
"unsupported Mach-O flags for this platform: {macho_flags:?}"
527+
)));
528+
}
529+
}
530+
}
531+
532+
section
518533
};
519534

520535
if used {

0 commit comments

Comments
 (0)