Skip to content

Commit a512c6c

Browse files
committed
Auto merge of #101550 - CraftSpider:link-dead-windows, r=wesleywiser
Make compressed rmeta contain compressed data length after header Fixes #90056, which is caused by link.exe introducing padding to the `.rustc` section, since it assumes this will have no effect besides allowing it to possibly use the extra space in future links.
2 parents bb18388 + 79f0021 commit a512c6c

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

compiler/rustc_codegen_ssa/src/back/metadata.rs

+6
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,13 @@ pub fn create_compressed_metadata_file(
306306
symbol_name: &str,
307307
) -> Vec<u8> {
308308
let mut compressed = rustc_metadata::METADATA_HEADER.to_vec();
309+
// Our length will be backfilled once we're done writing
310+
compressed.write_all(&[0; 4]).unwrap();
309311
FrameEncoder::new(&mut compressed).write_all(metadata.raw_data()).unwrap();
312+
let meta_len = rustc_metadata::METADATA_HEADER.len();
313+
let data_len = (compressed.len() - meta_len - 4) as u32;
314+
compressed[meta_len..meta_len + 4].copy_from_slice(&data_len.to_be_bytes());
315+
310316
let Some(mut file) = create_object_file(sess) else {
311317
return compressed.to_vec();
312318
};

compiler/rustc_metadata/src/locator.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,9 @@ fn get_metadata_section<'p>(
789789
loader.get_dylib_metadata(target, filename).map_err(MetadataError::LoadFailure)?;
790790
// The header is uncompressed
791791
let header_len = METADATA_HEADER.len();
792+
// header + u32 length of data
793+
let data_start = header_len + 4;
794+
792795
debug!("checking {} bytes of metadata-version stamp", header_len);
793796
let header = &buf[..cmp::min(header_len, buf.len())];
794797
if header != METADATA_HEADER {
@@ -798,8 +801,14 @@ fn get_metadata_section<'p>(
798801
)));
799802
}
800803

804+
// Length of the compressed stream - this allows linkers to pad the section if they want
805+
let Ok(len_bytes) = <[u8; 4]>::try_from(&buf[header_len..cmp::min(data_start, buf.len())]) else {
806+
return Err(MetadataError::LoadFailure("invalid metadata length found".to_string()));
807+
};
808+
let compressed_len = u32::from_be_bytes(len_bytes) as usize;
809+
801810
// Header is okay -> inflate the actual metadata
802-
let compressed_bytes = &buf[header_len..];
811+
let compressed_bytes = &buf[data_start..(data_start + compressed_len)];
803812
debug!("inflating {} bytes of compressed metadata", compressed_bytes.len());
804813
// Assume the decompressed data will be at least the size of the compressed data, so we
805814
// don't have to grow the buffer as much.

compiler/rustc_metadata/src/rmeta/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ pub(crate) fn rustc_version() -> String {
5555
/// Metadata encoding version.
5656
/// N.B., increment this if you change the format of metadata such that
5757
/// the rustc version can't be found to compare with `rustc_version()`.
58-
const METADATA_VERSION: u8 = 6;
58+
const METADATA_VERSION: u8 = 7;
5959

6060
/// Metadata header which includes `METADATA_VERSION`.
6161
///
62-
/// This header is followed by the position of the `CrateRoot`,
63-
/// which is encoded as a 32-bit big-endian unsigned integer,
64-
/// and further followed by the rustc version string.
62+
/// This header is followed by the length of the compressed data, then
63+
/// the position of the `CrateRoot`, which is encoded as a 32-bit big-endian
64+
/// unsigned integer, and further followed by the rustc version string.
6565
pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
6666

6767
/// A value of type T referred to by its absolute position

0 commit comments

Comments
 (0)