@@ -158,12 +158,13 @@ pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a
158158 file. symbols ( ) . find ( |sym| sym. name ( ) == Ok ( AIX_METADATA_SYMBOL_NAME ) )
159159 {
160160 let offset = metadata_symbol. address ( ) as usize ;
161- if offset < 8 {
161+ // The offset specifies the location of rustc metadata in the .info section of XCOFF.
162+ // Each string stored in .info section of XCOFF is preceded by a 4-byte length field.
163+ if offset < 4 {
162164 return Err ( format ! ( "Invalid metadata symbol offset: {offset}" ) ) ;
163165 }
164- // The offset specifies the location of rustc metadata in the comment section.
165- // The metadata is preceded by a 8-byte length field.
166- let len = u64:: from_le_bytes ( info_data[ ( offset - 8 ) ..offset] . try_into ( ) . unwrap ( ) ) as usize ;
166+ // XCOFF format uses big-endian byte order.
167+ let len = u32:: from_be_bytes ( info_data[ ( offset - 4 ) ..offset] . try_into ( ) . unwrap ( ) ) as usize ;
167168 if offset + len > ( info_data. len ( ) as usize ) {
168169 return Err ( format ! (
169170 "Metadata at offset {offset} with size {len} is beyond .info section"
@@ -478,9 +479,12 @@ pub fn create_wrapper_file(
478479 file. add_section ( Vec :: new ( ) , b".text" . to_vec ( ) , SectionKind :: Text ) ;
479480 file. section_mut ( section) . flags =
480481 SectionFlags :: Xcoff { s_flags : xcoff:: STYP_INFO as u32 } ;
481-
482- let len = data. len ( ) as u64 ;
483- let offset = file. append_section_data ( section, & len. to_le_bytes ( ) , 1 ) ;
482+ // Encode string stored in .info section of XCOFF.
483+ // FIXME: The length of data here is not guaranteed to fit in a u32.
484+ // We may have to split the data into multiple pieces in order to
485+ // store in .info section.
486+ let len: u32 = data. len ( ) . try_into ( ) . unwrap ( ) ;
487+ let offset = file. append_section_data ( section, & len. to_be_bytes ( ) , 1 ) ;
484488 // Add a symbol referring to the data in .info section.
485489 file. add_symbol ( Symbol {
486490 name : AIX_METADATA_SYMBOL_NAME . into ( ) ,
@@ -599,12 +603,12 @@ pub fn create_compressed_metadata_file_for_xcoff(
599603 section : SymbolSection :: Section ( data_section) ,
600604 flags : SymbolFlags :: None ,
601605 } ) ;
602- let len = data. len ( ) as u64 ;
603- let offset = file. append_section_data ( section, & len. to_le_bytes ( ) , 1 ) ;
606+ let len: u32 = data. len ( ) . try_into ( ) . unwrap ( ) ;
607+ let offset = file. append_section_data ( section, & len. to_be_bytes ( ) , 1 ) ;
604608 // Add a symbol referring to the rustc metadata.
605609 file. add_symbol ( Symbol {
606610 name : AIX_METADATA_SYMBOL_NAME . into ( ) ,
607- value : offset + 8 , // The metadata is preceded by a 8 -byte length field.
611+ value : offset + 4 , // The metadata is preceded by a 4 -byte length field.
608612 size : 0 ,
609613 kind : SymbolKind :: Unknown ,
610614 scope : SymbolScope :: Dynamic ,
0 commit comments