Skip to content

Commit 68a5bb4

Browse files
author
Charisee
committed
Add GNU Property Note
1 parent cb4f815 commit 68a5bb4

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fn add_gnu_property_note(
9898
file: &mut write::Object<'static>,
9999
architecture: Architecture,
100100
binary_format: BinaryFormat,
101+
endianness: Endianness,
101102
) {
102103
// check bti protection
103104
if binary_format != BinaryFormat::Elf
@@ -115,23 +116,26 @@ fn add_gnu_property_note(
115116
let n_namsz: u32 = 4; // Size of the n_name field
116117
let n_descsz: u32 = 16; // Size of the n_desc field
117118
let n_type: u32 = NT_GNU_PROPERTY_TYPE_0; // Type of note descriptor
118-
let values = [n_namsz, n_descsz, n_type];
119-
values.map(|v| data.extend_from_slice(&(v.to_le_bytes())));
120-
data.push(b'G'); // Owner of the program property note
121-
data.push(b'N');
122-
data.push(b'U');
123-
data.push(0);
119+
let header_values = [n_namsz, n_descsz, n_type];
120+
match endianness {
121+
Endianness::Little => header_values.map(|v| data.extend_from_slice(&(v.to_le_bytes()))),
122+
Endianness::Big => header_values.map(|v| data.extend_from_slice(&(v.to_be_bytes()))),
123+
};
124+
data.extend_from_slice(b"GNU\0"); // Owner of the program property note
124125
let pr_type: u32 = match architecture {
125126
Architecture::X86_64 => 0xc0000002,
126127
Architecture::Aarch64 => 0xc0000000,
127128
_ => unreachable!(),
128129
};
129130
let pr_datasz: u32 = 4; //size of the pr_data field
130131
let pr_data: u32 = 3; //program property descriptor
131-
let pr_padding: u32 = 3;
132-
let values = [pr_type, pr_datasz, pr_data, pr_padding];
133-
values.map(|v| data.extend_from_slice(&(v.to_le_bytes())));
134-
file.append_section_data(section, &data, 4);
132+
let pr_padding: u32 = 0;
133+
let property_values = [pr_type, pr_datasz, pr_data, pr_padding];
134+
match endianness {
135+
Endianness::Little => property_values.map(|v| data.extend_from_slice(&(v.to_le_bytes()))),
136+
Endianness::Big => property_values.map(|v| data.extend_from_slice(&(v.to_be_bytes()))),
137+
};
138+
file.append_section_data(section, &data, 8);
135139
}
136140

137141
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
@@ -246,7 +250,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
246250
_ => elf::ELFOSABI_NONE,
247251
};
248252
let abi_version = 0;
249-
add_gnu_property_note(&mut file, architecture, binary_format);
253+
add_gnu_property_note(&mut file, architecture, binary_format, endianness);
250254
file.flags = FileFlags::Elf { os_abi, abi_version, e_flags };
251255
Some(file)
252256
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Check for GNU Property Note
2+
3+
include ../tools.mk
4+
5+
# How to run this
6+
# python3 x.py test --target x86_64-unknown-linux-gnu tests/run-make/branch-protection-check-IBT/
7+
8+
# only-x86_64
9+
10+
all:
11+
ifeq ($(filter x86,$(LLVM_COMPONENTS)),x86_64)
12+
$(RUSTC) --target x86_64-unknown-linux-gnu -Z cf-protection=branch -L$(TMPDIR) -C link-args='-nostartfiles' -C save-temps ./main.rs -o $(TMPDIR)/rsmain
13+
readelf -nW $(TMPDIR)/rsmain | $(CGREP) -e ".note.gnu.property"
14+
endif
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("hello world");
3+
}

0 commit comments

Comments
 (0)