From 14e6f3f5625fb719cb337b21722c88cbbc85f884 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Wed, 13 Dec 2023 13:28:00 +0800 Subject: [PATCH 1/3] Fix XCOFF metadata --- .../rustc_codegen_ssa/src/back/metadata.rs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index bc0e3a8280636..d855042898477 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -158,12 +158,13 @@ pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a file.symbols().find(|sym| sym.name() == Ok(AIX_METADATA_SYMBOL_NAME)) { let offset = metadata_symbol.address() as usize; - if offset < 8 { + // The offset specifies the location of rustc metadata in the .info section of XCOFF. + // Each string stored in .info section of XCOFF is preceded by a 4-byte lenght field. + if offset < 4 { return Err(format!("Invalid metadata symbol offset: {offset}")); } - // The offset specifies the location of rustc metadata in the comment section. - // The metadata is preceded by a 8-byte length field. - let len = u64::from_le_bytes(info_data[(offset - 8)..offset].try_into().unwrap()) as usize; + // XCOFF format uses big-endian byte order. + let len = u32::from_be_bytes(info_data[(offset - 4)..offset].try_into().unwrap()) as usize; if offset + len > (info_data.len() as usize) { return Err(format!( "Metadata at offset {offset} with size {len} is beyond .info section" @@ -478,9 +479,9 @@ pub fn create_wrapper_file( file.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text); file.section_mut(section).flags = SectionFlags::Xcoff { s_flags: xcoff::STYP_INFO as u32 }; - - let len = data.len() as u64; - let offset = file.append_section_data(section, &len.to_le_bytes(), 1); + // Encode string stored in .info section of XCOFF. + let len = data.len() as u32; + let offset = file.append_section_data(section, &len.to_be_bytes(), 1); // Add a symbol referring to the data in .info section. file.add_symbol(Symbol { name: AIX_METADATA_SYMBOL_NAME.into(), @@ -599,12 +600,12 @@ pub fn create_compressed_metadata_file_for_xcoff( section: SymbolSection::Section(data_section), flags: SymbolFlags::None, }); - let len = data.len() as u64; - let offset = file.append_section_data(section, &len.to_le_bytes(), 1); + let len = data.len() as u32; + let offset = file.append_section_data(section, &len.to_be_bytes(), 1); // Add a symbol referring to the rustc metadata. file.add_symbol(Symbol { name: AIX_METADATA_SYMBOL_NAME.into(), - value: offset + 8, // The metadata is preceded by a 8-byte length field. + value: offset + 4, // The metadata is preceded by a 4-byte length field. size: 0, kind: SymbolKind::Unknown, scope: SymbolScope::Dynamic, From ce9a02eaac80ad5f4843dd93228e72f04ec1cb4c Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Thu, 14 Dec 2023 10:31:07 +0800 Subject: [PATCH 2/3] Address comment --- compiler/rustc_codegen_ssa/src/back/metadata.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index d855042898477..06ef0be461559 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -159,7 +159,7 @@ pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a { let offset = metadata_symbol.address() as usize; // The offset specifies the location of rustc metadata in the .info section of XCOFF. - // Each string stored in .info section of XCOFF is preceded by a 4-byte lenght field. + // Each string stored in .info section of XCOFF is preceded by a 4-byte length field. if offset < 4 { return Err(format!("Invalid metadata symbol offset: {offset}")); } @@ -480,7 +480,10 @@ pub fn create_wrapper_file( file.section_mut(section).flags = SectionFlags::Xcoff { s_flags: xcoff::STYP_INFO as u32 }; // Encode string stored in .info section of XCOFF. - let len = data.len() as u32; + // FIXME: The length of data here is not guaranteed to fit in a u32. + // We may have to split the data into multiple pieces in order to + // store in .info section. + let len: u32 = data.len().try_into().unwrap(); let offset = file.append_section_data(section, &len.to_be_bytes(), 1); // Add a symbol referring to the data in .info section. file.add_symbol(Symbol { From a8e1da317104cf33b9485be29c10da57c648b873 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Mon, 18 Dec 2023 09:41:36 +0800 Subject: [PATCH 3/3] Address comment --- 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 06ef0be461559..b683e1b45a8d1 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -603,7 +603,7 @@ pub fn create_compressed_metadata_file_for_xcoff( section: SymbolSection::Section(data_section), flags: SymbolFlags::None, }); - let len = data.len() as u32; + let len: u32 = data.len().try_into().unwrap(); let offset = file.append_section_data(section, &len.to_be_bytes(), 1); // Add a symbol referring to the rustc metadata. file.add_symbol(Symbol {