about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>2024-07-04 21:52:25 +0200
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>2024-07-04 21:52:25 +0200
commit253436c04c87b7d8dfed2fb14e42a67427196bc1 (patch)
tree579f7dac934f44971643ee854fab7fff558b73e3
parentf35bd40d18c39c58fbc9985d42ea601886a6bc63 (diff)
downloadrust-253436c04c87b7d8dfed2fb14e42a67427196bc1.tar.gz
rust-253436c04c87b7d8dfed2fb14e42a67427196bc1.zip
Better parsing of #[section_name] on Mach-O
This is required by the objc2 crate

Fixes rust-lang/rustc_codegen_cranelift#1504
-rw-r--r--src/constant.rs36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/constant.rs b/src/constant.rs
index 87c5da3b7c3..77203ece312 100644
--- a/src/constant.rs
+++ b/src/constant.rs
@@ -383,15 +383,43 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
 
         if let Some(section_name) = section_name {
             let (segment_name, section_name) = if tcx.sess.target.is_like_osx {
-                let section_name = section_name.as_str();
-                if let Some(names) = section_name.split_once(',') {
-                    names
-                } else {
+                // See https://github.com/llvm/llvm-project/blob/main/llvm/lib/MC/MCSectionMachO.cpp
+                let mut parts = section_name.as_str().split(',');
+                let Some(segment_name) = parts.next() else {
                     tcx.dcx().fatal(format!(
                         "#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma",
                         section_name
                     ));
+                };
+                let Some(section_name) = parts.next() else {
+                    tcx.dcx().fatal(format!(
+                        "#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma",
+                        section_name
+                    ));
+                };
+                if section_name.len() > 16 {
+                    tcx.dcx().fatal(format!(
+                        "#[link_section = \"{}\"] is not valid for macos target: section name bigger than 16 bytes",
+                        section_name
+                    ));
+                }
+                let section_type = parts.next().unwrap_or("regular");
+                if section_type != "regular" && section_type != "cstring_literals" {
+                    tcx.dcx().fatal(format!(
+                        "#[link_section = \"{}\"] is not supported: unsupported section type {}",
+                        section_name, section_type,
+                    ));
+                }
+                let _attrs = parts.next();
+                if parts.next().is_some() {
+                    tcx.dcx().fatal(format!(
+                        "#[link_section = \"{}\"] is not valid for macos target: too many components",
+                        section_name
+                    ));
                 }
+                // FIXME(bytecodealliance/wasmtime#8901) set S_CSTRING_LITERALS section type when
+                // cstring_literals is specified
+                (segment_name, section_name)
             } else {
                 ("", section_name.as_str())
             };