Skip to content

Commit 3695fef

Browse files
committed
Add a test for Mach-O link_section API inherited from LLVM
1 parent 0204aca commit 3695fef

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)