Skip to content

Commit cc2fe00

Browse files
authored
Unrolled build for #155517
Rollup merge of #155517 - madsmtm:test-macho-link-section-attrs, r=bjorn3 Add a test for Mach-O `#[link_section]` API inherited from LLVM The format of the `#[link_section]` attribute is under-documented, but on Mach-O, I think it's roughly the following BNF: ``` LinkSection -> Segment `,` Section (`,` (SectionType (`,` (SectionAttributes)?)?)?)? Segment -> <0 to 16 bytes> Section -> <0 to 16 bytes> SectionType -> `regular` | `zerofill` | `cstring_literals` | `4byte_literals` | `8byte_literals` | `literal_pointers` | `non_lazy_symbol_pointers` | `lazy_symbol_pointers` | `symbol_stubs` | `mod_init_funcs` | `mod_term_funcs` | `coalesced` | `interposing` | `16byte_literals` | `thread_local_regular` | `thread_local_zerofill` | `thread_local_variables` | `thread_local_variable_pointers` | `thread_local_init_function_pointers` SectionAttributes -> SectionAttribute (`+` SectionAttribute)* SectionAttribute -> `pure_instructions` | `no_toc` | `strip_static_syms` | `no_dead_strip` | `live_support`, `self_modifying_code` | `debug` ``` This PR adds a small test for a little part of this. Once #154429 is resolved, this should make it possible to test rust-lang/rustc_codegen_cranelift#1648 end-to-end. r? bjorn3
2 parents 91367b0 + 3695fef commit cc2fe00

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[unsafe(no_mangle)]
2+
#[unsafe(link_section = "__TEXT,custom_code,regular,pure_instructions")]
3+
static CODE: [u8; 10] = *b"0123456789";
4+
5+
#[unsafe(no_mangle)]
6+
#[unsafe(link_section = "__DATA,all_attributes,regular,pure_instructions\
7+
+no_toc+strip_static_syms+no_dead_strip+live_support\
8+
+self_modifying_code+debug")]
9+
static ALL_THE_ATTRIBUTES: u32 = 42;
10+
11+
#[unsafe(no_mangle)]
12+
#[unsafe(link_section = "__DATA,__mod_init_func,mod_init_funcs")]
13+
static CONSTRUCTOR: extern "C" fn() = constructor;
14+
extern "C" fn constructor() {}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Test that various Mach-O `#[link_section]` values are parsed and passed on correctly by codegen
2+
//! backends.
3+
//@ only-apple
4+
use run_make_support::{llvm_objdump, rustc};
5+
6+
fn main() {
7+
rustc().input("foo.rs").crate_type("lib").arg("--emit=obj").run();
8+
9+
let stdout =
10+
llvm_objdump().arg("--macho").arg("--private-headers").input("foo.o").run().stdout_utf8();
11+
12+
let expected = [
13+
("__TEXT", "custom_code", "S_REGULAR", "PURE_INSTRUCTIONS"),
14+
("__DATA", "__mod_init_func", "S_MOD_INIT_FUNC_POINTERS", "(none)"),
15+
(
16+
"__DATA",
17+
"all_attributes",
18+
"S_REGULAR",
19+
"PURE_INSTRUCTIONS NO_TOC STRIP_STATIC_SYMS \
20+
NO_DEAD_STRIP LIVE_SUPPORT SELF_MODIFYING_CODE DEBUG",
21+
),
22+
];
23+
24+
for (segment, section, section_type, section_attributes) in expected {
25+
let mut found = false;
26+
// Skip header.
27+
for section_info in stdout.split("Section").skip(1) {
28+
if section_info.contains(&format!("segname {segment}"))
29+
&& section_info.contains(&format!("sectname {section}"))
30+
{
31+
assert!(
32+
section_info.contains(&format!("type {section_type}")),
33+
"should have type {section_type:?}"
34+
);
35+
assert!(
36+
section_info.contains(&format!("attributes {section_attributes}\n")),
37+
"should have attributes {section_attributes:?}"
38+
);
39+
found = true;
40+
}
41+
}
42+
43+
if !found {
44+
panic!("could not find section {section} in binary");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)