-
Notifications
You must be signed in to change notification settings - Fork 140
Parse and set Mach-O section type and attributes #1648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
madsmtm
wants to merge
1
commit into
main
Choose a base branch
from
madsmtm/mach-o-section-flags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−29
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -414,48 +414,60 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant | |
| data.set_align(alloc.align.bytes()); | ||
|
|
||
| if let Some(section_name) = section_name { | ||
| let (segment_name, section_name) = if tcx.sess.target.is_like_darwin { | ||
| let (segment_name, section_name, macho_flags) = if tcx.sess.target.is_like_darwin { | ||
| // 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 { | ||
|
|
||
| let section_err = |msg| -> ! { | ||
| tcx.dcx().fatal(format!( | ||
| "#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma", | ||
| section_name | ||
| )); | ||
| "#[link_section = {section_name:?}] is not valid for Mach-O target: {msg}", | ||
| )) | ||
| }; | ||
|
|
||
| let Some(segment_name) = parts.next() else { | ||
| section_err("must be segment and section separated by comma"); | ||
| }; | ||
|
|
||
| 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 | ||
| )); | ||
| section_err("must be segment and section separated by comma"); | ||
| }; | ||
| 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 | ||
| )); | ||
| section_err("section name bigger than 16 bytes"); | ||
| } | ||
|
|
||
| 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, | ||
| )); | ||
|
|
||
| // The custom Mach-O section flags. | ||
| // The flags are the section type packed together with the attributes. | ||
| let mut macho_flags = if let Some((_, val)) = | ||
| MACHO_SECTION_TYPES.iter().find(|(name, _)| *name == section_type) | ||
| { | ||
| *val | ||
| } else { | ||
| section_err(&format!("unsupported section type {section_type:?}")); | ||
| }; | ||
|
|
||
| if let Some(section_attributes) = parts.next() { | ||
| for attr in section_attributes.split('+') { | ||
| macho_flags |= if let Some((_, val)) = | ||
| MACHO_SECTION_ATTRIBUTES.iter().find(|(name, _)| *name == attr) | ||
| { | ||
| *val | ||
| } else { | ||
| section_err(&format!("unsupported section attribute {attr:?}")); | ||
| }; | ||
| } | ||
| } | ||
| 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 | ||
| )); | ||
| section_err("too many components"); | ||
| } | ||
| // FIXME(bytecodealliance/wasmtime#8901) set S_CSTRING_LITERALS section type when | ||
| // cstring_literals is specified | ||
| (segment_name, section_name) | ||
|
|
||
| (segment_name, section_name, macho_flags) | ||
| } else { | ||
| ("", section_name.as_str()) | ||
| ("", section_name.as_str(), 0) | ||
| }; | ||
| data.set_segment_section(segment_name, section_name); | ||
| data.set_segment_section(segment_name, section_name, macho_flags); | ||
| } | ||
|
|
||
| let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len()).to_vec(); | ||
|
|
@@ -649,3 +661,45 @@ pub(crate) fn mir_operand_get_const_val<'tcx>( | |
| } | ||
| } | ||
| } | ||
|
|
||
| // We support the same custom section type / attrs naming as LLVM: | ||
| // <https://github.com/llvm/llvm-project/blob/llvmorg-22.1.3/llvm/lib/MC/MCSectionMachO.cpp#L23-L91> | ||
| // <https://github.com/llvm/llvm-project/blob/llvmorg-22.1.3/llvm/include/llvm/BinaryFormat/MachO.h#L120-L223> | ||
| const MACHO_SECTION_TYPES: &[(&str, u32)] = &[ | ||
| ("regular", object::macho::S_REGULAR), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
| ("zerofill", object::macho::S_ZEROFILL), | ||
| ("cstring_literals", object::macho::S_CSTRING_LITERALS), | ||
| ("4byte_literals", object::macho::S_4BYTE_LITERALS), | ||
| ("8byte_literals", object::macho::S_8BYTE_LITERALS), | ||
| ("literal_pointers", object::macho::S_LITERAL_POINTERS), | ||
| ("non_lazy_symbol_pointers", object::macho::S_NON_LAZY_SYMBOL_POINTERS), | ||
| ("lazy_symbol_pointers", object::macho::S_LAZY_SYMBOL_POINTERS), | ||
| ("symbol_stubs", object::macho::S_SYMBOL_STUBS), | ||
| ("mod_init_funcs", object::macho::S_MOD_INIT_FUNC_POINTERS), | ||
| ("mod_term_funcs", object::macho::S_MOD_TERM_FUNC_POINTERS), | ||
| ("coalesced", object::macho::S_COALESCED), | ||
| // S_GB_ZEROFILL (not supported by LLVM) | ||
| ("interposing", object::macho::S_INTERPOSING), | ||
| ("16byte_literals", object::macho::S_16BYTE_LITERALS), | ||
| // S_DTRACE_DOF (not supported by LLVM) | ||
| // S_LAZY_DYLIB_SYMBOL_POINTERS (not supported by LLVM) | ||
| ("thread_local_regular", object::macho::S_THREAD_LOCAL_REGULAR), | ||
| ("thread_local_zerofill", object::macho::S_THREAD_LOCAL_ZEROFILL), | ||
| ("thread_local_variables", object::macho::S_THREAD_LOCAL_VARIABLES), | ||
| ("thread_local_variable_pointers", object::macho::S_THREAD_LOCAL_VARIABLE_POINTERS), | ||
| ("thread_local_init_function_pointers", object::macho::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS), | ||
| // S_INIT_FUNC_OFFSETS (not supported by LLVM) | ||
| ]; | ||
| const MACHO_SECTION_ATTRIBUTES: &[(&str, u32)] = &[ | ||
| ("pure_instructions", object::macho::S_ATTR_PURE_INSTRUCTIONS), | ||
| ("no_toc", object::macho::S_ATTR_NO_TOC), | ||
| ("strip_static_syms", object::macho::S_ATTR_STRIP_STATIC_SYMS), | ||
| ("no_dead_strip", object::macho::S_ATTR_NO_DEAD_STRIP), | ||
| ("live_support", object::macho::S_ATTR_LIVE_SUPPORT), | ||
| ("self_modifying_code", object::macho::S_ATTR_SELF_MODIFYING_CODE), | ||
| ("debug", object::macho::S_ATTR_DEBUG), | ||
| // System settable attributes are not supported by LLVM: | ||
| // S_ATTR_SOME_INSTRUCTIONS | ||
| // S_ATTR_EXT_RELOC | ||
| // S_ATTR_LOC_RELOC | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this as a table originally because I kinda wanted to print the available options / have suggestions for mistyped section types, but I ended up not implementing that, because I didn't really know if there were a good way to do so with
rustc's diagnostic APIs?I can do a
matchinstead if you prefer?View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using tables here is fine by me.