Skip to content

Commit 14e6f3f

Browse files
author
Kai Luo
committed
Fix XCOFF metadata
1 parent 3340d49 commit 14e6f3f

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -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 lenght 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,9 @@ 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+
let len = data.len() as u32;
484+
let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
484485
// Add a symbol referring to the data in .info section.
485486
file.add_symbol(Symbol {
486487
name: AIX_METADATA_SYMBOL_NAME.into(),
@@ -599,12 +600,12 @@ pub fn create_compressed_metadata_file_for_xcoff(
599600
section: SymbolSection::Section(data_section),
600601
flags: SymbolFlags::None,
601602
});
602-
let len = data.len() as u64;
603-
let offset = file.append_section_data(section, &len.to_le_bytes(), 1);
603+
let len = data.len() as u32;
604+
let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
604605
// Add a symbol referring to the rustc metadata.
605606
file.add_symbol(Symbol {
606607
name: AIX_METADATA_SYMBOL_NAME.into(),
607-
value: offset + 8, // The metadata is preceded by a 8-byte length field.
608+
value: offset + 4, // The metadata is preceded by a 4-byte length field.
608609
size: 0,
609610
kind: SymbolKind::Unknown,
610611
scope: SymbolScope::Dynamic,

0 commit comments

Comments
 (0)