Skip to content

Generate Property Note #110300

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

Closed
wants to merge 23 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use object::{

use snap::write::FrameEncoder;

use object::elf::NT_GNU_PROPERTY_TYPE_0;
use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::rustc_erase_owner;
Expand All @@ -23,6 +24,8 @@ use rustc_session::Session;
use rustc_target::abi::Endian;
use rustc_target::spec::{RelocModel, Target};



/// The default metadata loader. This is used by cg_llvm and cg_clif.
///
/// # Metadata location
Expand Down Expand Up @@ -201,6 +204,44 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
_ => elf::ELFOSABI_NONE,
};
let abi_version = 0;

// check bti protection
let mut check_cfprotection = false;
let mut pr_type: u32 = 0;
if architecture == Architecture::X86_64 && let rustc_session::config::CFProtection::Branch | rustc_session::config::CFProtection::Full = sess.opts.unstable_opts.cf_protection {
check_cfprotection =true;
pr_type = 0xc0000002;
}
if architecture == Architecture::Aarch64 && sess.opts.unstable_opts.branch_protection.is_some()
{
check_cfprotection = true;
pr_type = 0xc0000000;
}
if check_cfprotection && binary_format == BinaryFormat::Elf {
let segment: Vec<u8> = file.segment_name(StandardSegment::Data).to_vec();
let name: Vec<u8> = b".note.gnu.property".to_vec();
let kind = SectionKind::Note;
let section = file.add_section(segment, name, kind);
let mut data: Vec<u8> = Vec::new();
let n_namsz: u32 = 4; // Size of the n_name field
let n_descsz: u32 = 16; // Size of the n_desc field
let n_type: u32 = NT_GNU_PROPERTY_TYPE_0; // Type of note descriptor
let values = [n_namsz, n_descsz, n_type];
values.map(|v| data.extend_from_slice(&(v.to_ne_bytes())));
data.push(b'G'); // Owner of the program property note
data.push(b'N');
data.push(b'U');
data.push(0);
let pr_datasz: u32 = 4; //size of the pr_data field
let pr_data: u32 = 3; //program property descriptor
let pr_padding: u32 = 3;
let values = [pr_type, pr_datasz, pr_data, pr_padding];
values.map(|v| data.extend_from_slice(&(v.to_ne_bytes())));
let x = data.len();
assert_eq!(x, 32);
let align = 4;
let _ = file.append_section_data(section, &data, align);
}
file.flags = FileFlags::Elf { os_abi, abi_version, e_flags };
Some(file)
}
Expand Down