From 10b781c4b4d9a55d1fdf734b9fb9bd962190520c Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 14 Mar 2023 04:32:11 +0000 Subject: [PATCH 01/22] Sketch out adding a new section --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 4ab56699922f5..f91bcc12e6eb8 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -201,6 +201,23 @@ pub(crate) fn create_object_file(sess: &Session) -> Option elf::ELFOSABI_NONE, }; let abi_version = 0; + if binary_format == BinaryFormat::Elf{ + if architecture == Architecture::I386 || architecture == Architecture::Arm{ + if let Some(bp) = sess.opts.unstable_opts.branch_protection { + if bp.bti{ + //&sess.target.options.features + // Information it should include Owner, Data size,Description + let name:Vec = b"hello".to_vec();//.new.gnu.property" + let segment:Vec = b"world".to_vec(); + let kind: SectionKind = SectionKind::Note; + let section = file.add_section(segment, name, kind); + let data: &[u8] = b"howdy"; + file.append_section_data(section, data, 1); + () + } + } + } + } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; Some(file) } From a1ad5692bbd10829da7d029b42a1945d43b1a271 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 14 Mar 2023 04:32:11 +0000 Subject: [PATCH 02/22] Sketch out adding a new section --- .../rustc_codegen_ssa/src/back/metadata.rs | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index f91bcc12e6eb8..02855bb025be9 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -100,7 +100,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Endianness::Big, }; let architecture = match &sess.target.arch[..] { - "arm" => Architecture::Arm, + "arm" => { Architecture::Arm}, "aarch64" => { if sess.target.pointer_width == 32 { Architecture::Aarch64_Ilp32 @@ -112,7 +112,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Architecture::S390x, "mips" => Architecture::Mips, "mips64" => Architecture::Mips64, - "x86_64" => { + "x86_64" => { if sess.target.pointer_width == 32 { Architecture::X86_64_X32 } else { @@ -201,21 +201,23 @@ pub(crate) fn create_object_file(sess: &Session) -> Option elf::ELFOSABI_NONE, }; let abi_version = 0; + if binary_format == BinaryFormat::Elf{ - if architecture == Architecture::I386 || architecture == Architecture::Arm{ - if let Some(bp) = sess.opts.unstable_opts.branch_protection { - if bp.bti{ - //&sess.target.options.features - // Information it should include Owner, Data size,Description - let name:Vec = b"hello".to_vec();//.new.gnu.property" - let segment:Vec = b"world".to_vec(); - let kind: SectionKind = SectionKind::Note; - let section = file.add_section(segment, name, kind); - let data: &[u8] = b"howdy"; - file.append_section_data(section, data, 1); - () - } - } + if architecture == Architecture::X86_64{ + // check bti protection not working + //if let Some(bp) = sess.opts.unstable_opts.branch_protection { if bp.bti{name = b"bti true".to_vec();}} + + let name:Vec = b".new.gnu.property".to_vec(); + let segment:Vec = file.segment_name(StandardSegment::Data).to_vec(); + let kind = SectionKind::Note; + let section = file.add_section(segment, name, kind); + + // b"GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring) Build ID: 1.."; + let size: u8 = 14; + let id:u8 = 1; + let data: &[u8] = &[b'G', b'N', b'U', size, b'N', b'T', b'_',b'G', b'N', b'U',b'_',b'B', b'U', b'I',b'L', b'D', b'_',b'I',b'D', b' ', b'(', b'u', b'n',b'i', b'q', b'u',b'e',b' ', b'b', b'u',b'i', b'l', b'd', b' ', b'I',b'D', b' ', b'b',b'i',b't', b's', b't',b'r', b'i',b'n',b'g',b')', b' ', b'B', b'u', b'i',b'l', b'd', b' ',b'I',b'D',b':', id]; + file.append_section_data(section, data, 1); + } } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; From ce84427eac4d09c8f1ba58ce423557205e0b68e3 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 14 Mar 2023 04:32:11 +0000 Subject: [PATCH 03/22] Sketch out adding a new section --- .../rustc_codegen_ssa/src/back/metadata.rs | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 02855bb025be9..3c82b8732ae36 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -201,24 +201,20 @@ pub(crate) fn create_object_file(sess: &Session) -> Option elf::ELFOSABI_NONE, }; let abi_version = 0; - - if binary_format == BinaryFormat::Elf{ - if architecture == Architecture::X86_64{ - // check bti protection not working - //if let Some(bp) = sess.opts.unstable_opts.branch_protection { if bp.bti{name = b"bti true".to_vec();}} - - let name:Vec = b".new.gnu.property".to_vec(); - let segment:Vec = file.segment_name(StandardSegment::Data).to_vec(); - let kind = SectionKind::Note; - let section = file.add_section(segment, name, kind); - - // b"GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring) Build ID: 1.."; - let size: u8 = 14; - let id:u8 = 1; - let data: &[u8] = &[b'G', b'N', b'U', size, b'N', b'T', b'_',b'G', b'N', b'U',b'_',b'B', b'U', b'I',b'L', b'D', b'_',b'I',b'D', b' ', b'(', b'u', b'n',b'i', b'q', b'u',b'e',b' ', b'b', b'u',b'i', b'l', b'd', b' ', b'I',b'D', b' ', b'b',b'i',b't', b's', b't',b'r', b'i',b'n',b'g',b')', b' ', b'B', b'u', b'i',b'l', b'd', b' ',b'I',b'D',b':', id]; - file.append_section_data(section, data, 1); - - } + + // check bti protection + let mut check_cfprotection = false; + 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;} + else if sess.opts.unstable_opts.branch_protection.is_some() && sess.target.arch == "aarch64" + {check_cfprotection =true;} + if check_cfprotection && binary_format == BinaryFormat::Elf{ + let name:Vec = b".new.gnu.property".to_vec(); + let segment:Vec = file.segment_name(StandardSegment::Data).to_vec(); + let kind = SectionKind::Note; + let section = file.add_section(segment, name, kind); + let data: &[u8] = &[b'G', b'N', b'U']; + let _ = file.append_section_data(section, data, 1); } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; Some(file) From 64483e377f8d2feaa31f6f76dc54eceda287b6ab Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 14 Mar 2023 04:32:11 +0000 Subject: [PATCH 04/22] Sketch out adding a new section --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 3c82b8732ae36..0d48ec7c88f52 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -100,7 +100,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Endianness::Big, }; let architecture = match &sess.target.arch[..] { - "arm" => { Architecture::Arm}, + "arm" => Architecture::Arm, "aarch64" => { if sess.target.pointer_width == 32 { Architecture::Aarch64_Ilp32 @@ -112,7 +112,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Architecture::S390x, "mips" => Architecture::Mips, "mips64" => Architecture::Mips64, - "x86_64" => { + "x86_64" => { if sess.target.pointer_width == 32 { Architecture::X86_64_X32 } else { From f7f61a77eb9067e4323afc4e4e87a786beffe797 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 14 Mar 2023 04:32:11 +0000 Subject: [PATCH 05/22] Sketch out adding a new section --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 0d48ec7c88f52..ebc42bae8e6f4 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -209,11 +209,13 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = b".new.gnu.property".to_vec(); + let name:Vec = b".note.gnu.property".to_vec(); let segment:Vec = file.segment_name(StandardSegment::Data).to_vec(); let kind = SectionKind::Note; let section = file.add_section(segment, name, kind); let data: &[u8] = &[b'G', b'N', b'U']; + //sess.opts.unstable_opts.branch_protection = Some(rustc_session::config::BranchProtection{bti:true, pac_ret:None}); + let _ = file.append_section_data(section, data, 1); } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; From 25367a59e548af865cb22f7d1388d0e884fc4688 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 28 Mar 2023 17:59:36 +0000 Subject: [PATCH 06/22] Develop property section --- .../rustc_codegen_ssa/src/back/metadata.rs | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index ebc42bae8e6f4..52c16aab37c3f 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -22,6 +22,7 @@ use rustc_session::cstore::MetadataLoader; use rustc_session::Session; use rustc_target::abi::Endian; use rustc_target::spec::{RelocModel, Target}; +use object::elf::NT_GNU_PROPERTY_TYPE_0; /// The default metadata loader. This is used by cg_llvm and cg_clif. /// @@ -213,9 +214,46 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = file.segment_name(StandardSegment::Data).to_vec(); let kind = SectionKind::Note; let section = file.add_section(segment, name, kind); - let data: &[u8] = &[b'G', b'N', b'U']; - //sess.opts.unstable_opts.branch_protection = Some(rustc_session::config::BranchProtection{bti:true, pac_ret:None}); - + + //Emit the note header. + // OutStreamer.emitValueToAlignment(Align(8).value()); + + // Size of the n_name field + // OutStreamer.emitIntValue(4, 4); // data size for "GNU\0" + let n_namsz = 4; + + //Size of the n_desc field + //OutStreamer.emitIntValue(4 * 4, 4); // Elf_Prop size + let n_descz = 16; + + // Type of note descriptor + //OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4); + let n_type = NT_GNU_PROPERTY_TYPE_0.try_into().unwrap(); + + // Owner of the program property note + //OutStreamer.emitBytes(StringRef("GNU", 4)); // note name + let _n_name: &[u8] = &[b'G', b'N', b'U']; + + // The type of program property + //OutStreamer.emitIntValue(ELF::GNU_PROPERTY_AARCH64_FEATURE_1_AND, 4); + // kernel_loader::elf::GNU_PROPERTY_AARCH64_FEATURE_1_AND <- out of scope + //pub const GNU_PROPERTY_AARCH64_FEATURE_1_AND: u32 = 3221225472; + // GNU_PROPERTY_AARCH64_FEATURE_1_AND.try_into().unwrap(); <-- too bug + let pr_type = 5;// TODO: unknown contents; + + // The size of the pr_data field + //OutStreamer.emitIntValue(4, 4); // data size + let pr_datasz = 4; + + // The program property descriptor + //OutStreamer.emitIntValue(Flags, 4); // data + let pr_data = 0;// TODO: unknown contents; + + // The padding if necessary + //OutStreamer.emitIntValue(0, 4); // pad + let pr_padding = 0; + + let data: &[u8] = &[n_namsz, n_descz, n_type, b'G', b'N', b'U', pr_type, pr_datasz, pr_data, pr_padding]; let _ = file.append_section_data(section, data, 1); } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; From fdee5f5f63b3a298754f48e1f27d67e2b54006b6 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 28 Mar 2023 20:03:39 +0000 Subject: [PATCH 07/22] Try using u32 : --- .../rustc_codegen_ssa/src/back/metadata.rs | 67 ++++++++++++------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 52c16aab37c3f..d210bbb365ea6 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -24,6 +24,7 @@ use rustc_target::abi::Endian; use rustc_target::spec::{RelocModel, Target}; use object::elf::NT_GNU_PROPERTY_TYPE_0; + /// The default metadata loader. This is used by cg_llvm and cg_clif. /// /// # Metadata location @@ -216,44 +217,58 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Date: Wed, 29 Mar 2023 18:53:35 +0000 Subject: [PATCH 08/22] conversion from u32 to u8 --- .../rustc_codegen_ssa/src/back/metadata.rs | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index d210bbb365ea6..32c59922a2093 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -216,60 +216,74 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = Vec::new(); //Emit the note header. //.balign 8; // OutStreamer.emitValueToAlignment(Align(8).value()); + let _align = 8; /* Size of the n_name field .word 4; OutStreamer.emitIntValue(4, 4) */ - let n_namsz = 4; - + //let n_namsz:u32 = 4; + let n_namsz = (4 as u32).to_ne_bytes(); + data.extend_from_slice(&n_namsz); + /*Size of the n_desc field .word 16; /* n_descsz */ OutStreamer.emitIntValue(4 * 4, 4); */ - let n_descz = 16; + let n_descz = (16 as u32).to_ne_bytes(); + data.extend_from_slice(&n_descz); /*Type of note descriptor .word 5; /* n_type = NT_GNU_PROPERTY_TYPE_0 */ OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4); */ - let n_type = NT_GNU_PROPERTY_TYPE_0.try_into().unwrap(); - + let n_type = (NT_GNU_PROPERTY_TYPE_0 as u32).to_ne_bytes(); + data.extend_from_slice(&n_type); + /* Owner of the program property note .asciz "GNU"; /* n_name */ OutStreamer.emitBytes(StringRef("GNU", 4)); // note name */ - let _n_name = &[b'G', b'N', b'U']; + data.push(b'G'); + data.push(b'N'); + data.push(b'U'); + data.push(0); /* The type of program property .word 0xc0000000; /* pr_type = GNU_PROPERTY_AARCH64_FEATURE_1_AND */ OutStreamer.emitIntValue(ELF::GNU_PROPERTY_AARCH64_FEATURE_1_AND, 4); pub const GNU_PROPERTY_AARCH64_FEATURE_1_AND: u32 = 3221225472; */ - let pr_type = 0xc0000000 as u32; + let pr_type = (3221225472 as u32).to_ne_bytes(); + data.extend_from_slice(&pr_type); /* The size of the pr_data field .word 4; /* pr_datasz */ OutStreamer.emitIntValue(4, 4); // data size */ - let pr_datasz = 4; + let pr_datasz = (4 as u32).to_ne_bytes(); + data.extend_from_slice(&pr_datasz); /* The program property descriptor .word features; /* pr_data = features */ OutStreamer.emitIntValue(Flags, 4); // data */ - let pr_data = e_flags.try_into().unwrap(); + let pr_data = (e_flags as u32).to_ne_bytes(); + data.extend_from_slice(&pr_data); /*The padding if necessary .word 0; /* pr_padding */ OutStreamer.emitIntValue(0, 4); // pad */ - let pr_padding = 0; - let data: &[u32] = &[n_namsz, n_descz, n_type, 'G' as u32, 'N' as u32, 'U' as u32, pr_type, pr_datasz, pr_data, pr_padding]; - let _ = file.append_section_data(section, data, 1); + let pr_padding = (0 as u32).to_ne_bytes(); + data.extend_from_slice(&pr_padding); + + + let _ = file.append_section_data(section, &data, 8); } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; Some(file) From 7b9cf2a78a818128c3fa0f8169f17bc8dcc090ae Mon Sep 17 00:00:00 2001 From: Charisee Date: Thu, 30 Mar 2023 01:54:48 +0000 Subject: [PATCH 09/22] unknown property type but printing --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 32c59922a2093..9ba3c5eae5189 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -217,11 +217,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = Vec::new(); - //Emit the note header. - //.balign 8; - // OutStreamer.emitValueToAlignment(Align(8).value()); - let _align = 8; - + //Emit the note header /* Size of the n_name field .word 4; OutStreamer.emitIntValue(4, 4) @@ -258,7 +254,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Option Date: Fri, 31 Mar 2023 18:53:16 +0000 Subject: [PATCH 10/22] Use different endianness --- .../rustc_codegen_ssa/src/back/metadata.rs | 68 ++++++------------- 1 file changed, 21 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 9ba3c5eae5189..0415388c6b292 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -215,69 +215,43 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = file.segment_name(StandardSegment::Data).to_vec(); let kind = SectionKind::Note; let section = file.add_section(segment, name, kind); - let mut data:Vec = Vec::new(); - //Emit the note header - /* Size of the n_name field - .word 4; - OutStreamer.emitIntValue(4, 4) - */ - //let n_namsz:u32 = 4; + // Size of the n_name field let n_namsz = (4 as u32).to_ne_bytes(); data.extend_from_slice(&n_namsz); - - /*Size of the n_desc field - .word 16; /* n_descsz */ - OutStreamer.emitIntValue(4 * 4, 4); - */ + // Size of the n_desc field let n_descz = (16 as u32).to_ne_bytes(); - data.extend_from_slice(&n_descz); - - /*Type of note descriptor - .word 5; /* n_type = NT_GNU_PROPERTY_TYPE_0 */ - OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4); - */ + data.extend_from_slice(&n_descz); + // Type of note descriptor let n_type = (NT_GNU_PROPERTY_TYPE_0 as u32).to_ne_bytes(); data.extend_from_slice(&n_type); - - /* Owner of the program property note - .asciz "GNU"; /* n_name */ - OutStreamer.emitBytes(StringRef("GNU", 4)); // note name - */ + // Owner of the program property note + /* + data.push(0); + data.push(b'U'); + data.push(b'N'); + data.push(b'G'); + */ data.push(b'G'); data.push(b'N'); data.push(b'U'); - data.push(0); - + data.push(0); /* The type of program property .word 0xc0000000; /* pr_type = GNU_PROPERTY_AARCH64_FEATURE_1_AND */ OutStreamer.emitIntValue(ELF::GNU_PROPERTY_AARCH64_FEATURE_1_AND, 4); pub const GNU_PROPERTY_AARCH64_FEATURE_1_AND: u32 = 3221225472; - */ - let pr_type = (0xc0000000 as u32).to_be_bytes(); - data.extend_from_slice(&pr_type); - - /* The size of the pr_data field - .word 4; /* pr_datasz */ - OutStreamer.emitIntValue(4, 4); // data size - */ + */ + let pr_type = (0xc0000000 as u32).to_ne_bytes(); + data.extend_from_slice(&pr_type); + // The size of the pr_data field let pr_datasz = (4 as u32).to_ne_bytes(); - data.extend_from_slice(&pr_datasz); - - /* The program property descriptor - .word features; /* pr_data = features */ - OutStreamer.emitIntValue(Flags, 4); // data - */ + data.extend_from_slice(&pr_datasz); + // The program property descriptor let pr_data = (e_flags as u32).to_ne_bytes(); - data.extend_from_slice(&pr_data); - - /*The padding if necessary - .word 0; /* pr_padding */ - OutStreamer.emitIntValue(0, 4); // pad - */ + data.extend_from_slice(&pr_data); + // The padding if necessary let pr_padding = (0 as u32).to_ne_bytes(); - data.extend_from_slice(&pr_padding); - + data.extend_from_slice(&pr_padding); let x = data.len(); assert_eq!(x, 32); let _ = file.append_section_data(section, &data, 8); From 933ac92f73ef1758959e7ffe855981f16f05a8af Mon Sep 17 00:00:00 2001 From: Charisee Date: Mon, 3 Apr 2023 21:44:23 +0000 Subject: [PATCH 11/22] use parameters that work for x86 --- .../rustc_codegen_ssa/src/back/metadata.rs | 51 ++++++------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 0415388c6b292..c8191bc0535fd 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -22,7 +22,7 @@ use rustc_session::cstore::MetadataLoader; use rustc_session::Session; use rustc_target::abi::Endian; use rustc_target::spec::{RelocModel, Target}; -use object::elf::NT_GNU_PROPERTY_TYPE_0; +//use object::elf::NT_GNU_PROPERTY_TYPE_0; /// The default metadata loader. This is used by cg_llvm and cg_clif. @@ -216,45 +216,24 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = Vec::new(); - // Size of the n_name field - let n_namsz = (4 as u32).to_ne_bytes(); - data.extend_from_slice(&n_namsz); - // Size of the n_desc field - let n_descz = (16 as u32).to_ne_bytes(); - data.extend_from_slice(&n_descz); - // Type of note descriptor - let n_type = (NT_GNU_PROPERTY_TYPE_0 as u32).to_ne_bytes(); - data.extend_from_slice(&n_type); - // Owner of the program property note - /* - data.push(0); - data.push(b'U'); - data.push(b'N'); - data.push(b'G'); - */ - data.push(b'G'); + let n_namsz:u32 = 4; // Size of the n_name field + let n_descz:u32 = 16; // Size of the n_desc field + let n_type:u32 = 5; // Type of note descriptor + let values = [n_namsz, n_descz, 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); - /* The type of program property - .word 0xc0000000; /* pr_type = GNU_PROPERTY_AARCH64_FEATURE_1_AND */ - OutStreamer.emitIntValue(ELF::GNU_PROPERTY_AARCH64_FEATURE_1_AND, 4); - pub const GNU_PROPERTY_AARCH64_FEATURE_1_AND: u32 = 3221225472; - */ - let pr_type = (0xc0000000 as u32).to_ne_bytes(); - data.extend_from_slice(&pr_type); - // The size of the pr_data field - let pr_datasz = (4 as u32).to_ne_bytes(); - data.extend_from_slice(&pr_datasz); - // The program property descriptor - let pr_data = (e_flags as u32).to_ne_bytes(); - data.extend_from_slice(&pr_data); - // The padding if necessary - let pr_padding = (0 as u32).to_ne_bytes(); - data.extend_from_slice(&pr_padding); + data.push(0); + let pr_type:u32 = 0xc0000002; + let pr_datasz:u32 = 4 ;//size of the pr_data field + let pr_data:u32 = 3; //program property descriptor + let pr_padding:u32 = 3;//padding + 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 _ = file.append_section_data(section, &data, 8); + let _ = file.append_section_data(section, &data, 3); } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; Some(file) From 5e9fa09739c25cc351843b586dd24c59d8cdcebd Mon Sep 17 00:00:00 2001 From: Charisee Date: Mon, 3 Apr 2023 21:44:23 +0000 Subject: [PATCH 12/22] use parameters that work for x86 --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index c8191bc0535fd..4036289d20d0f 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -218,7 +218,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = Vec::new(); let n_namsz:u32 = 4; // Size of the n_name field let n_descz:u32 = 16; // Size of the n_desc field - let n_type:u32 = 5; // Type of note descriptor + let n_type:u32 = NT_GNU_PROPERTY_TYPE_0;// Type of note descriptor let values = [n_namsz, n_descz, n_type]; values.map(|v| data.extend_from_slice(&(v.to_ne_bytes()))); data.push(b'G'); // Owner of the program property note @@ -226,9 +226,9 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Date: Mon, 3 Apr 2023 21:44:23 +0000 Subject: [PATCH 13/22] use parameters that work for x86 --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 4036289d20d0f..f6f89c3724271 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -22,7 +22,7 @@ use rustc_session::cstore::MetadataLoader; use rustc_session::Session; use rustc_target::abi::Endian; use rustc_target::spec::{RelocModel, Target}; -//use object::elf::NT_GNU_PROPERTY_TYPE_0; +use object::elf::NT_GNU_PROPERTY_TYPE_0; /// The default metadata loader. This is used by cg_llvm and cg_clif. From 1f0e5f08100bc6f89802db70fad6f6426c113e3d Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 4 Apr 2023 00:27:33 +0000 Subject: [PATCH 14/22] Different parameters based on arch --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index f6f89c3724271..5b5f7c7b71fda 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -225,15 +225,16 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Date: Tue, 4 Apr 2023 18:19:12 +0000 Subject: [PATCH 15/22] working version of x86 --- .../rustc_codegen_ssa/src/back/metadata.rs | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 5b5f7c7b71fda..c7dee89946cb8 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -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; @@ -22,8 +23,6 @@ use rustc_session::cstore::MetadataLoader; use rustc_session::Session; use rustc_target::abi::Endian; use rustc_target::spec::{RelocModel, Target}; -use object::elf::NT_GNU_PROPERTY_TYPE_0; - /// The default metadata loader. This is used by cg_llvm and cg_clif. /// @@ -203,39 +202,40 @@ pub(crate) fn create_object_file(sess: &Session) -> Option elf::ELFOSABI_NONE, }; let abi_version = 0; - + // check bti protection let mut check_cfprotection = false; - if architecture == Architecture::X86_64 && + 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;} else if sess.opts.unstable_opts.branch_protection.is_some() && sess.target.arch == "aarch64" {check_cfprotection =true;} - if check_cfprotection && binary_format == BinaryFormat::Elf{ - let name:Vec = b".note.gnu.property".to_vec(); - let segment:Vec = file.segment_name(StandardSegment::Data).to_vec(); - let kind = SectionKind::Note; - let section = file.add_section(segment, name, kind); - let mut data:Vec = Vec::new(); - let n_namsz:u32 = 4; // Size of the n_name field - let n_descz: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_descz, 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_type:u32 = if architecture == Architecture::X86_64 {0xc0000002} else {0xc0000000}; - let pr_datasz:u32 = 4 ; //size of the pr_data field - let pr_data:u32 = 3; //program property descriptor - let pr_padding:u32 = if architecture == Architecture::X86_64 {3} else {0}; - 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 = if architecture == Architecture::X86_64 {3} else {8}; - let _ = file.append_section_data(section, &data, align); - } + if check_cfprotection && binary_format == BinaryFormat::Elf { + let name: Vec = b".note.gnu.property".to_vec(); + let segment: Vec = file.segment_name(StandardSegment::Data).to_vec(); + let kind = SectionKind::Note; + let section = file.add_section(segment, name, kind); + let mut data: Vec = Vec::new(); + let n_namsz: u32 = 4; // Size of the n_name field + let n_descz: 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_descz, 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_type: u32 = + if architecture == Architecture::X86_64 { 0xc0000002 } else { 0xc0000000 }; + let pr_datasz: u32 = 4; //size of the pr_data field + let pr_data: u32 = 3; //program property descriptor + let pr_padding: u32 = if architecture == Architecture::X86_64 { 3 } else { 0 }; + 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 = if architecture == Architecture::X86_64 { 4 } else { 8 }; + let _ = file.append_section_data(section, &data, align); + } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; Some(file) } From e773636b04aaeb3f9e3755989634ef1569e4d3e6 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 4 Apr 2023 18:19:12 +0000 Subject: [PATCH 16/22] working version of x86 --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index c7dee89946cb8..b422f465cef0d 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -228,12 +228,20 @@ pub(crate) fn create_object_file(sess: &Session) -> Option>>>>>> 53bf0484362 (working version of x86) 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); +<<<<<<< HEAD let align = if architecture == Architecture::X86_64 { 4 } else { 8 }; +======= + let align = 4; +>>>>>>> 53bf0484362 (working version of x86) let _ = file.append_section_data(section, &data, align); } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; From 6bff1624e25560ff84a7625a7541895222dce521 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 4 Apr 2023 18:19:12 +0000 Subject: [PATCH 17/22] working version of x86 --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index b422f465cef0d..d51cda5408725 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -228,20 +228,12 @@ pub(crate) fn create_object_file(sess: &Session) -> Option>>>>>> 53bf0484362 (working version of x86) 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); -<<<<<<< HEAD - let align = if architecture == Architecture::X86_64 { 4 } else { 8 }; -======= let align = 4; ->>>>>>> 53bf0484362 (working version of x86) let _ = file.append_section_data(section, &data, align); } file.flags = FileFlags::Elf { os_abi, abi_version, e_flags }; From d05ead175ea239d53dbdbaec62ee89d16b6e8a0f Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 4 Apr 2023 23:16:51 +0000 Subject: [PATCH 18/22] clean up --- .../rustc_codegen_ssa/src/back/metadata.rs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index d51cda5408725..8c3cc738db770 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -205,31 +205,35 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = b".note.gnu.property".to_vec(); let segment: Vec = file.segment_name(StandardSegment::Data).to_vec(); + let name: Vec = b".note.gnu.property".to_vec(); let kind = SectionKind::Note; let section = file.add_section(segment, name, kind); let mut data: Vec = Vec::new(); - let n_namsz: u32 = 4; // Size of the n_name field - let n_descz: u32 = 16; // Size of the n_desc field + let n_name_sz: u32 = 4; // Size of the n_name field + let n_desc_sz: 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_descz, n_type]; + let values = [n_name_sz, n_desc_sz, 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_type: u32 = - if architecture == Architecture::X86_64 { 0xc0000002 } else { 0xc0000000 }; - let pr_datasz: u32 = 4; //size of the pr_data field + let pr_data_sz: 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]; + let values = [pr_type, pr_data_sz, pr_data, pr_padding]; values.map(|v| data.extend_from_slice(&(v.to_ne_bytes()))); let x = data.len(); assert_eq!(x, 32); From b13372fe64f3bfaee0caf9ab949423204e05cfaf Mon Sep 17 00:00:00 2001 From: Charisee Date: Wed, 5 Apr 2023 15:38:35 +0000 Subject: [PATCH 19/22] change names to be consistent with documentation --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 8c3cc738db770..4f7016432f089 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -205,7 +205,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option Option = Vec::new(); - let n_name_sz: u32 = 4; // Size of the n_name field - let n_desc_sz: u32 = 16; // Size of the n_desc field + 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_name_sz, n_desc_sz, n_type]; + 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_data_sz: u32 = 4; //size of the pr_data field + 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_data_sz, pr_data, pr_padding]; + 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); From c556c037df278a6f597b262b6877ad3befe7c9ed Mon Sep 17 00:00:00 2001 From: Charisee Date: Wed, 5 Apr 2023 15:38:35 +0000 Subject: [PATCH 20/22] change names to be consistent with documentation --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 4f7016432f089..8c0412f30021e 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -222,15 +222,15 @@ pub(crate) fn create_object_file(sess: &Session) -> Option = 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 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_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]; From 64c8090043f568d78edc8d093cc3bc6614f9b6cf Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 28 Mar 2023 17:59:36 +0000 Subject: [PATCH 21/22] Generate GNU property note --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 8c0412f30021e..1d6eb4d834401 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -23,6 +23,8 @@ use rustc_session::cstore::MetadataLoader; use rustc_session::Session; use rustc_target::abi::Endian; use rustc_target::spec::{RelocModel, Target}; +//use object::elf::NT_GNU_PROPERTY_TYPE_0; + /// The default metadata loader. This is used by cg_llvm and cg_clif. /// From b95737da55d49b01aa14d423c18cc003b0d51c85 Mon Sep 17 00:00:00 2001 From: Charisee Date: Tue, 14 Mar 2023 04:32:11 +0000 Subject: [PATCH 22/22] Generate GNU property note --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 1d6eb4d834401..1052a1115e77b 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -23,7 +23,7 @@ use rustc_session::cstore::MetadataLoader; use rustc_session::Session; use rustc_target::abi::Endian; use rustc_target::spec::{RelocModel, Target}; -//use object::elf::NT_GNU_PROPERTY_TYPE_0; + /// The default metadata loader. This is used by cg_llvm and cg_clif.