Skip to content

Commit 253436c

Browse files
committed
Better parsing of #[section_name] on Mach-O
This is required by the objc2 crate Fixes #1504
1 parent f35bd40 commit 253436c

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/constant.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,43 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
383383

384384
if let Some(section_name) = section_name {
385385
let (segment_name, section_name) = if tcx.sess.target.is_like_osx {
386-
let section_name = section_name.as_str();
387-
if let Some(names) = section_name.split_once(',') {
388-
names
389-
} else {
386+
// See https://github.com/llvm/llvm-project/blob/main/llvm/lib/MC/MCSectionMachO.cpp
387+
let mut parts = section_name.as_str().split(',');
388+
let Some(segment_name) = parts.next() else {
390389
tcx.dcx().fatal(format!(
391390
"#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma",
392391
section_name
393392
));
393+
};
394+
let Some(section_name) = parts.next() else {
395+
tcx.dcx().fatal(format!(
396+
"#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma",
397+
section_name
398+
));
399+
};
400+
if section_name.len() > 16 {
401+
tcx.dcx().fatal(format!(
402+
"#[link_section = \"{}\"] is not valid for macos target: section name bigger than 16 bytes",
403+
section_name
404+
));
405+
}
406+
let section_type = parts.next().unwrap_or("regular");
407+
if section_type != "regular" && section_type != "cstring_literals" {
408+
tcx.dcx().fatal(format!(
409+
"#[link_section = \"{}\"] is not supported: unsupported section type {}",
410+
section_name, section_type,
411+
));
412+
}
413+
let _attrs = parts.next();
414+
if parts.next().is_some() {
415+
tcx.dcx().fatal(format!(
416+
"#[link_section = \"{}\"] is not valid for macos target: too many components",
417+
section_name
418+
));
394419
}
420+
// FIXME(bytecodealliance/wasmtime#8901) set S_CSTRING_LITERALS section type when
421+
// cstring_literals is specified
422+
(segment_name, section_name)
395423
} else {
396424
("", section_name.as_str())
397425
};

0 commit comments

Comments
 (0)