1//! Reading of the rustc metadata for rlibs and dylibs
23use std::borrow::Cow;
4use std::fs::File;
5use std::io::Write;
6use std::path::Path;
78use itertools::Itertools;
9use object::write::{self, StandardSegment, Symbol, SymbolSection};
10use object::{
11Architecture, BinaryFormat, Endianness, FileFlags, Object, ObjectSection, ObjectSymbol,
12SectionFlags, SectionKind, SymbolFlags, SymbolKind, SymbolScope, elf, pe, xcoff,
13};
14use rustc_abi::Endian;
15use rustc_data_structures::memmap::Mmap;
16use rustc_data_structures::owned_slice::{OwnedSlice, try_slice_owned};
17use rustc_metadata::EncodedMetadata;
18use rustc_metadata::creader::MetadataLoader;
19use rustc_metadata::fs::METADATA_FILENAME;
20use rustc_middle::bug;
21use rustc_session::Session;
22use rustc_span::sym;
23use rustc_target::spec::{Abi, Os, RelocModel, Target, ef_avr_arch};
24use tracing::debug;
2526use super::apple;
2728/// The default metadata loader. This is used by cg_llvm and cg_clif.
29///
30/// # Metadata location
31///
32/// <dl>
33/// <dt>rlib</dt>
34/// <dd>The metadata can be found in the `lib.rmeta` file inside of the ar archive.</dd>
35/// <dt>dylib</dt>
36/// <dd>The metadata can be found in the `.rustc` section of the shared library.</dd>
37/// </dl>
38#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DefaultMetadataLoader {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f, "DefaultMetadataLoader")
}
}Debug)]
39pub(crate) struct DefaultMetadataLoader;
4041static AIX_METADATA_SYMBOL_NAME: &'static str = "__aix_rust_metadata";
4243fn load_metadata_with(
44 path: &Path,
45 f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
46) -> Result<OwnedSlice, String> {
47let file =
48File::open(path).map_err(|e| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to open file \'{0}\': {1}",
path.display(), e))
})format!("failed to open file '{}': {}", path.display(), e))?;
4950unsafe { Mmap::map(file) }
51 .map_err(|e| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to mmap file \'{0}\': {1}",
path.display(), e))
})format!("failed to mmap file '{}': {}", path.display(), e))
52 .and_then(|mmap| try_slice_owned(mmap, |mmap| f(mmap)))
53}
5455impl MetadataLoader for DefaultMetadataLoader {
56fn get_rlib_metadata(&self, target: &Target, path: &Path) -> Result<OwnedSlice, String> {
57{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/metadata.rs:57",
"rustc_codegen_ssa::back::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/metadata.rs"),
::tracing_core::__macro_support::Option::Some(57u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::metadata"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("getting rlib metadata for {0}",
path.display()) as &dyn Value))])
});
} else { ; }
};debug!("getting rlib metadata for {}", path.display());
58load_metadata_with(path, |data| {
59let archive = object::read::archive::ArchiveFile::parse(&*data)
60 .map_err(|e| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to parse rlib \'{0}\': {1}",
path.display(), e))
})format!("failed to parse rlib '{}': {}", path.display(), e))?;
6162for entry_result in archive.members() {
63let entry = entry_result
64 .map_err(|e| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to parse rlib \'{0}\': {1}",
path.display(), e))
})format!("failed to parse rlib '{}': {}", path.display(), e))?;
65if entry.name() == METADATA_FILENAME.as_bytes() {
66let data = entry
67 .data(data)
68 .map_err(|e| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to parse rlib \'{0}\': {1}",
path.display(), e))
})format!("failed to parse rlib '{}': {}", path.display(), e))?;
69if target.is_like_aix {
70return get_metadata_xcoff(path, data);
71 } else {
72return search_for_section(path, data, ".rmeta");
73 }
74 }
75 }
7677Err(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("metadata not found in rlib \'{0}\'",
path.display()))
})format!("metadata not found in rlib '{}'", path.display()))
78 })
79 }
8081fn get_dylib_metadata(&self, target: &Target, path: &Path) -> Result<OwnedSlice, String> {
82{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/back/metadata.rs:82",
"rustc_codegen_ssa::back::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/back/metadata.rs"),
::tracing_core::__macro_support::Option::Some(82u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::back::metadata"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("getting dylib metadata for {0}",
path.display()) as &dyn Value))])
});
} else { ; }
};debug!("getting dylib metadata for {}", path.display());
83if target.is_like_aix {
84load_metadata_with(path, |data| {
85let archive = object::read::archive::ArchiveFile::parse(&*data).map_err(|e| {
86::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to parse aix dylib \'{0}\': {1}",
path.display(), e))
})format!("failed to parse aix dylib '{}': {}", path.display(), e)87 })?;
8889match archive.members().exactly_one() {
90Ok(lib) => {
91let lib = lib.map_err(|e| {
92::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to parse aix dylib \'{0}\': {1}",
path.display(), e))
})format!("failed to parse aix dylib '{}': {}", path.display(), e)93 })?;
94let data = lib.data(data).map_err(|e| {
95::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to parse aix dylib \'{0}\': {1}",
path.display(), e))
})format!("failed to parse aix dylib '{}': {}", path.display(), e)96 })?;
97get_metadata_xcoff(path, data)
98 }
99Err(e) => Err(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to parse aix dylib \'{0}\': {1}",
path.display(), e))
})format!("failed to parse aix dylib '{}': {}", path.display(), e)),
100 }
101 })
102 } else {
103load_metadata_with(path, |data| search_for_section(path, data, ".rustc"))
104 }
105 }
106}
107108pub(super) fn search_for_section<'a>(
109 path: &Path,
110 bytes: &'a [u8],
111 section: &str,
112) -> Result<&'a [u8], String> {
113let Ok(file) = object::File::parse(bytes) else {
114// The parse above could fail for odd reasons like corruption, but for
115 // now we just interpret it as this target doesn't support metadata
116 // emission in object files so the entire byte slice itself is probably
117 // a metadata file. Ideally though if necessary we could at least check
118 // the prefix of bytes to see if it's an actual metadata object and if
119 // not forward the error along here.
120return Ok(bytes);
121 };
122file.section_by_name(section)
123 .ok_or_else(|| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("no `{0}` section in \'{1}\'",
section, path.display()))
})format!("no `{}` section in '{}'", section, path.display()))?
124.data()
125 .map_err(|e| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("failed to read {0} section in \'{1}\': {2}",
section, path.display(), e))
})format!("failed to read {} section in '{}': {}", section, path.display(), e))
126}
127128fn add_gnu_property_note(
129 file: &mut write::Object<'static>,
130 architecture: Architecture,
131 binary_format: BinaryFormat,
132 endianness: Endianness,
133) {
134// check bti protection
135if binary_format != BinaryFormat::Elf136 || !#[allow(non_exhaustive_omitted_patterns)] match architecture {
Architecture::X86_64 | Architecture::Aarch64 => true,
_ => false,
}matches!(architecture, Architecture::X86_64 | Architecture::Aarch64)137 {
138return;
139 }
140141let section = file.add_section(
142file.segment_name(StandardSegment::Data).to_vec(),
143b".note.gnu.property".to_vec(),
144 SectionKind::Note,
145 );
146let mut data: Vec<u8> = Vec::new();
147let n_namsz: u32 = 4; // Size of the n_name field
148let n_descsz: u32 = 16; // Size of the n_desc field
149let n_type: u32 = object::elf::NT_GNU_PROPERTY_TYPE_0; // Type of note descriptor
150let header_values = [n_namsz, n_descsz, n_type];
151header_values.iter().for_each(|v| {
152data.extend_from_slice(&match endianness {
153 Endianness::Little => v.to_le_bytes(),
154 Endianness::Big => v.to_be_bytes(),
155 })
156 });
157data.extend_from_slice(b"GNU\0"); // Owner of the program property note
158let pr_type: u32 = match architecture {
159 Architecture::X86_64 => object::elf::GNU_PROPERTY_X86_FEATURE_1_AND,
160 Architecture::Aarch64 => object::elf::GNU_PROPERTY_AARCH64_FEATURE_1_AND,
161_ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
162 };
163let pr_datasz: u32 = 4; //size of the pr_data field
164let pr_data: u32 = 3; //program property descriptor
165let pr_padding: u32 = 0;
166let property_values = [pr_type, pr_datasz, pr_data, pr_padding];
167property_values.iter().for_each(|v| {
168data.extend_from_slice(&match endianness {
169 Endianness::Little => v.to_le_bytes(),
170 Endianness::Big => v.to_be_bytes(),
171 })
172 });
173file.append_section_data(section, &data, 8);
174}
175176pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a [u8], String> {
177let Ok(file) = object::File::parse(data) else {
178return Ok(data);
179 };
180let info_data = search_for_section(path, data, ".info")?;
181if let Some(metadata_symbol) =
182file.symbols().find(|sym| sym.name() == Ok(AIX_METADATA_SYMBOL_NAME))
183 {
184let offset = metadata_symbol.address() as usize;
185// The offset specifies the location of rustc metadata in the .info section of XCOFF.
186 // Each string stored in .info section of XCOFF is preceded by a 4-byte length field.
187if offset < 4 {
188return Err(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Invalid metadata symbol offset: {0}",
offset))
})format!("Invalid metadata symbol offset: {offset}"));
189 }
190// XCOFF format uses big-endian byte order.
191let len = u32::from_be_bytes(info_data[(offset - 4)..offset].try_into().unwrap()) as usize;
192if offset + len > (info_data.len() as usize) {
193return Err(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Metadata at offset {0} with size {1} is beyond .info section",
offset, len))
})format!(
194"Metadata at offset {offset} with size {len} is beyond .info section"
195));
196 }
197Ok(&info_data[offset..(offset + len)])
198 } else {
199Err(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("Unable to find symbol {0}",
AIX_METADATA_SYMBOL_NAME))
})format!("Unable to find symbol {AIX_METADATA_SYMBOL_NAME}"))
200 }
201}
202203pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
204let endianness = match sess.target.options.endian {
205 Endian::Little => Endianness::Little,
206 Endian::Big => Endianness::Big,
207 };
208let Some((architecture, sub_architecture)) =
209sess.target.object_architecture(&sess.unstable_target_features)
210else {
211return None;
212 };
213let binary_format = sess.target.binary_format.to_object();
214215let mut file = write::Object::new(binary_format, architecture, endianness);
216file.set_sub_architecture(sub_architecture);
217if sess.target.is_like_darwin {
218if macho_is_arm64e(&sess.target) {
219file.set_macho_cpu_subtype(object::macho::CPU_SUBTYPE_ARM64E);
220 }
221222file.set_macho_build_version(macho_object_build_version_for_target(sess))
223 }
224if binary_format == BinaryFormat::Coff {
225// Disable the default mangler to avoid mangling the special "@feat.00" symbol name.
226let original_mangling = file.mangling();
227file.set_mangling(object::write::Mangling::None);
228229let mut feature = 0;
230231if file.architecture() == object::Architecture::I386 {
232// When linking with /SAFESEH on x86, lld requires that all linker inputs be marked as
233 // safe exception handling compatible. Metadata files masquerade as regular COFF
234 // objects and are treated as linker inputs, despite containing no actual code. Thus,
235 // they still need to be marked as safe exception handling compatible. See #96498.
236 // Reference: https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
237feature |= 1;
238 }
239240file.add_symbol(object::write::Symbol {
241 name: "@feat.00".into(),
242 value: feature,
243 size: 0,
244 kind: object::SymbolKind::Data,
245 scope: object::SymbolScope::Compilation,
246 weak: false,
247 section: object::write::SymbolSection::Absolute,
248 flags: object::SymbolFlags::None,
249 });
250251file.set_mangling(original_mangling);
252 }
253let e_flags = elf_e_flags(architecture, sess);
254// adapted from LLVM's `MCELFObjectTargetWriter::getOSABI`
255let os_abi = elf_os_abi(sess);
256let abi_version = 0;
257add_gnu_property_note(&mut file, architecture, binary_format, endianness);
258file.flags = FileFlags::Elf { os_abi, abi_version, e_flags };
259Some(file)
260}
261262pub(super) fn elf_os_abi(sess: &Session) -> u8 {
263match sess.target.options.os {
264 Os::Hermit => elf::ELFOSABI_STANDALONE,
265 Os::FreeBsd => elf::ELFOSABI_FREEBSD,
266 Os::Solaris => elf::ELFOSABI_SOLARIS,
267_ => elf::ELFOSABI_NONE,
268 }
269}
270271pub(super) fn elf_e_flags(architecture: Architecture, sess: &Session) -> u32 {
272match architecture {
273 Architecture::Mips | Architecture::Mips64 | Architecture::Mips64_N32 => {
274// "N32" indicates an "ILP32" data model on a 64-bit MIPS CPU
275 // like SPARC's "v8+", x86_64's "x32", or the watchOS "arm64_32".
276let is_32bit = architecture == Architecture::Mips;
277let mut e_flags = match sess.target.options.cpu.as_ref() {
278"mips1" if is_32bit => elf::EF_MIPS_ARCH_1,
279"mips2" if is_32bit => elf::EF_MIPS_ARCH_2,
280"mips3" => elf::EF_MIPS_ARCH_3,
281"mips4" => elf::EF_MIPS_ARCH_4,
282"mips5" => elf::EF_MIPS_ARCH_5,
283"mips32r2" if is_32bit => elf::EF_MIPS_ARCH_32R2,
284"mips32r6" if is_32bit => elf::EF_MIPS_ARCH_32R6,
285"mips64r2" if !is_32bit => elf::EF_MIPS_ARCH_64R2,
286"mips64r6" if !is_32bit => elf::EF_MIPS_ARCH_64R6,
287 s if s.starts_with("mips32") && !is_32bit => {
288sess.dcx().fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid CPU `{0}` for 64-bit MIPS target",
s))
})format!("invalid CPU `{}` for 64-bit MIPS target", s))
289 }
290 s if s.starts_with("mips64") && is_32bit => {
291sess.dcx().fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid CPU `{0}` for 32-bit MIPS target",
s))
})format!("invalid CPU `{}` for 32-bit MIPS target", s))
292 }
293_ if is_32bit => elf::EF_MIPS_ARCH_32R2,
294_ => elf::EF_MIPS_ARCH_64R2,
295 };
296297// If the ABI is explicitly given, use it, or default to O32 on 32-bit MIPS,
298 // which is the only "true" 32-bit option that LLVM supports.
299match sess.target.options.llvm_abiname.as_ref() {
300"o32" if is_32bit => e_flags |= elf::EF_MIPS_ABI_O32,
301"n32" if !is_32bit => e_flags |= elf::EF_MIPS_ABI2,
302"n64" if !is_32bit => {}
303"" if is_32bit => e_flags |= elf::EF_MIPS_ABI_O32,
304"" => sess.dcx().fatal("LLVM ABI must be specified for 64-bit MIPS targets"),
305 s if is_32bit => {
306sess.dcx().fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid LLVM ABI `{0}` for 32-bit MIPS target",
s))
})format!("invalid LLVM ABI `{}` for 32-bit MIPS target", s))
307 }
308 s => sess.dcx().fatal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("invalid LLVM ABI `{0}` for 64-bit MIPS target",
s))
})format!("invalid LLVM ABI `{}` for 64-bit MIPS target", s)),
309 };
310311if sess.target.options.relocation_model != RelocModel::Static {
312// PIC means position-independent code. CPIC means "calls PIC".
313 // CPIC was mutually exclusive with PIC according to
314 // the SVR4 MIPS ABI https://refspecs.linuxfoundation.org/elf/mipsabi.pdf
315 // and should have only appeared on static objects with dynamically calls.
316 // At some point someone (GCC?) decided to set CPIC even for PIC.
317 // Nowadays various things expect both set on the same object file
318 // and may even error if you mix CPIC and non-CPIC object files,
319 // despite that being the entire point of the CPIC ABI extension!
320 // As we are in Rome, we do as the Romans do.
321e_flags |= elf::EF_MIPS_PIC | elf::EF_MIPS_CPIC;
322 }
323if sess.target.options.cpu.contains("r6") {
324e_flags |= elf::EF_MIPS_NAN2008;
325 }
326e_flags327 }
328 Architecture::Riscv32 | Architecture::Riscv64 => {
329// Source: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/079772828bd10933d34121117a222b4cc0ee2200/riscv-elf.adoc
330let mut e_flags: u32 = 0x0;
331332// Check if compression is enabled
333if sess.target_features.contains(&sym::zca) {
334e_flags |= elf::EF_RISCV_RVC;
335 }
336337// Check if RVTSO is enabled
338if sess.target_features.contains(&sym::ztso) {
339e_flags |= elf::EF_RISCV_TSO;
340 }
341342// Set the appropriate flag based on ABI
343 // This needs to match LLVM `RISCVELFStreamer.cpp`
344match &*sess.target.llvm_abiname {
345"ilp32" | "lp64" => (),
346"ilp32f" | "lp64f" => e_flags |= elf::EF_RISCV_FLOAT_ABI_SINGLE,
347"ilp32d" | "lp64d" => e_flags |= elf::EF_RISCV_FLOAT_ABI_DOUBLE,
348// Note that the `lp64e` is still unstable as it's not (yet) part of the ELF psABI.
349"ilp32e" | "lp64e" => e_flags |= elf::EF_RISCV_RVE,
350_ => ::rustc_middle::util::bug::bug_fmt(format_args!("unknown RISC-V ABI name"))bug!("unknown RISC-V ABI name"),
351 }
352353e_flags354 }
355 Architecture::LoongArch32 | Architecture::LoongArch64 => {
356// Source: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#e_flags-identifies-abi-type-and-version
357let mut e_flags: u32 = elf::EF_LARCH_OBJABI_V1;
358359// Set the appropriate flag based on ABI
360 // This needs to match LLVM `LoongArchELFStreamer.cpp`
361match &*sess.target.llvm_abiname {
362"ilp32s" | "lp64s" => e_flags |= elf::EF_LARCH_ABI_SOFT_FLOAT,
363"ilp32f" | "lp64f" => e_flags |= elf::EF_LARCH_ABI_SINGLE_FLOAT,
364"ilp32d" | "lp64d" => e_flags |= elf::EF_LARCH_ABI_DOUBLE_FLOAT,
365_ => ::rustc_middle::util::bug::bug_fmt(format_args!("unknown LoongArch ABI name"))bug!("unknown LoongArch ABI name"),
366 }
367368e_flags369 }
370 Architecture::Avr => {
371// Resolve the ISA revision and set
372 // the appropriate EF_AVR_ARCH flag.
373if let Some(ref cpu) = sess.opts.cg.target_cpu {
374ef_avr_arch(cpu)
375 } else {
376::rustc_middle::util::bug::bug_fmt(format_args!("AVR CPU not explicitly specified"))bug!("AVR CPU not explicitly specified")377 }
378 }
379 Architecture::Csky => {
380if #[allow(non_exhaustive_omitted_patterns)] match sess.target.options.abi {
Abi::AbiV2 => true,
_ => false,
}matches!(sess.target.options.abi, Abi::AbiV2) {
381 elf::EF_CSKY_ABIV2382 } else {
383 elf::EF_CSKY_ABIV1384 }
385 }
386 Architecture::PowerPc64 => {
387const EF_PPC64_ABI_UNKNOWN: u32 = 0;
388const EF_PPC64_ABI_ELF_V1: u32 = 1;
389const EF_PPC64_ABI_ELF_V2: u32 = 2;
390391match sess.target.options.llvm_abiname.as_ref() {
392// If the flags do not correctly indicate the ABI,
393 // linkers such as ld.lld assume that the ppc64 object files are always ELFv2
394 // which leads to broken binaries if ELFv1 is used for the object files.
395"elfv1" => EF_PPC64_ABI_ELF_V1,
396"elfv2" => EF_PPC64_ABI_ELF_V2,
397"" if sess.target.options.binary_format.to_object() == BinaryFormat::Elf => {
398::rustc_middle::util::bug::bug_fmt(format_args!("No ABI specified for this PPC64 ELF target"));bug!("No ABI specified for this PPC64 ELF target");
399 }
400// Fall back
401_ => EF_PPC64_ABI_UNKNOWN,
402 }
403 }
404_ => 0,
405 }
406}
407408/// Mach-O files contain information about:
409/// - The platform/OS they were built for (macOS/watchOS/Mac Catalyst/iOS simulator etc).
410/// - The minimum OS version / deployment target.
411/// - The version of the SDK they were targetting.
412///
413/// In the past, this was accomplished using the LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
414/// LC_VERSION_MIN_TVOS or LC_VERSION_MIN_WATCHOS load commands, which each contain information
415/// about the deployment target and SDK version, and implicitly, by their presence, which OS they
416/// target. Simulator targets were determined if the architecture was x86_64, but there was e.g. a
417/// LC_VERSION_MIN_IPHONEOS present.
418///
419/// This is of course brittle and limited, so modern tooling emit the LC_BUILD_VERSION load
420/// command (which contains all three pieces of information in one) when the deployment target is
421/// high enough, or the target is something that wouldn't be encodable with the old load commands
422/// (such as Mac Catalyst, or Aarch64 iOS simulator).
423///
424/// Since Xcode 15, Apple's LD apparently requires object files to use this load command, so this
425/// returns the `MachOBuildVersion` for the target to do so.
426fn macho_object_build_version_for_target(sess: &Session) -> object::write::MachOBuildVersion {
427/// The `object` crate demands "X.Y.Z encoded in nibbles as xxxx.yy.zz"
428 /// e.g. minOS 14.0 = 0x000E0000, or SDK 16.2 = 0x00100200
429fn pack_version(apple::OSVersion { major, minor, patch }: apple::OSVersion) -> u32 {
430let (major, minor, patch) = (majoras u32, minoras u32, patchas u32);
431 (major << 16) | (minor << 8) | patch432 }
433434let platform = apple::macho_platform(&sess.target);
435let min_os = sess.apple_deployment_target();
436437let mut build_version = object::write::MachOBuildVersion::default();
438build_version.platform = platform;
439build_version.minos = pack_version(min_os);
440// The version here does not _really_ matter, since it is only used at runtime, and we specify
441 // it when linking the final binary, so we will omit the version. This is also what LLVM does,
442 // and the tooling also allows this (and shows the SDK version as `n/a`). Finally, it is the
443 // semantically correct choice, as the SDK has not influenced the binary generated by rustc at
444 // this point in time.
445build_version.sdk = 0;
446447build_version448}
449450/// Is Apple's CPU subtype `arm64e`s
451fn macho_is_arm64e(target: &Target) -> bool {
452target.llvm_target.starts_with("arm64e")
453}
454455pub(crate) enum MetadataPosition {
456 First,
457 Last,
458}
459460/// For rlibs we "pack" rustc metadata into a dummy object file.
461///
462/// Historically it was needed because rustc linked rlibs as whole-archive in some cases.
463/// In that case linkers try to include all files located in an archive, so if metadata is stored
464/// in an archive then it needs to be of a form that the linker is able to process.
465/// Now it's not clear whether metadata still needs to be wrapped into an object file or not.
466///
467/// Note, though, that we don't actually want this metadata to show up in any
468/// final output of the compiler. Instead this is purely for rustc's own
469/// metadata tracking purposes.
470///
471/// With the above in mind, each "flavor" of object format gets special
472/// handling here depending on the target:
473///
474/// * MachO - macos-like targets will insert the metadata into a section that
475/// is sort of fake dwarf debug info. Inspecting the source of the macos
476/// linker this causes these sections to be skipped automatically because
477/// it's not in an allowlist of otherwise well known dwarf section names to
478/// go into the final artifact.
479///
480/// * WebAssembly - this uses wasm files themselves as the object file format
481/// so an empty file with no linking metadata but a single custom section is
482/// created holding our metadata.
483///
484/// * COFF - Windows-like targets create an object with a section that has
485/// the `IMAGE_SCN_LNK_REMOVE` flag set which ensures that if the linker
486/// ever sees the section it doesn't process it and it's removed.
487///
488/// * ELF - All other targets are similar to Windows in that there's a
489/// `SHF_EXCLUDE` flag we can set on sections in an object file to get
490/// automatically removed from the final output.
491pub(crate) fn create_wrapper_file(
492 sess: &Session,
493 section_name: String,
494 data: &[u8],
495) -> (Vec<u8>, MetadataPosition) {
496let Some(mut file) = create_object_file(sess) else {
497if sess.target.is_like_wasm {
498return (
499create_metadata_file_for_wasm(sess, data, §ion_name),
500 MetadataPosition::First,
501 );
502 }
503504// Targets using this branch don't have support implemented here yet or
505 // they're not yet implemented in the `object` crate and will likely
506 // fill out this module over time.
507return (data.to_vec(), MetadataPosition::Last);
508 };
509let section = if file.format() == BinaryFormat::Xcoff {
510file.add_section(Vec::new(), b".info".to_vec(), SectionKind::Debug)
511 } else {
512file.add_section(
513file.segment_name(StandardSegment::Debug).to_vec(),
514section_name.into_bytes(),
515 SectionKind::Debug,
516 )
517 };
518match file.format() {
519 BinaryFormat::Coff => {
520file.section_mut(section).flags =
521 SectionFlags::Coff { characteristics: pe::IMAGE_SCN_LNK_REMOVE };
522 }
523 BinaryFormat::Elf => {
524file.section_mut(section).flags =
525 SectionFlags::Elf { sh_flags: elf::SHF_EXCLUDEas u64 };
526 }
527 BinaryFormat::Xcoff => {
528// AIX system linker may aborts if it meets a valid XCOFF file in archive with no .text, no .data and no .bss.
529file.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text);
530file.section_mut(section).flags =
531 SectionFlags::Xcoff { s_flags: xcoff::STYP_INFOas u32 };
532// Encode string stored in .info section of XCOFF.
533 // FIXME: The length of data here is not guaranteed to fit in a u32.
534 // We may have to split the data into multiple pieces in order to
535 // store in .info section.
536let len: u32 = data.len().try_into().unwrap();
537let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
538// Add a symbol referring to the data in .info section.
539file.add_symbol(Symbol {
540 name: AIX_METADATA_SYMBOL_NAME.into(),
541 value: offset + 4,
542 size: 0,
543 kind: SymbolKind::Unknown,
544 scope: SymbolScope::Compilation,
545 weak: false,
546 section: SymbolSection::Section(section),
547 flags: SymbolFlags::Xcoff {
548 n_sclass: xcoff::C_INFO,
549 x_smtyp: xcoff::C_HIDEXT,
550 x_smclas: xcoff::C_HIDEXT,
551 containing_csect: None,
552 },
553 });
554 }
555_ => {}
556 };
557file.append_section_data(section, data, 1);
558 (file.write().unwrap(), MetadataPosition::First)
559}
560561// Historical note:
562//
563// When using link.exe it was seen that the section name `.note.rustc`
564// was getting shortened to `.note.ru`, and according to the PE and COFF
565// specification:
566//
567// > Executable images do not use a string table and do not support
568// > section names longer than 8 characters
569//
570// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
571//
572// As a result, we choose a slightly shorter name! As to why
573// `.note.rustc` works on MinGW, see
574// https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/lld/COFF/Writer.cpp#L1190-L1197
575pub fn create_compressed_metadata_file(
576 sess: &Session,
577 metadata: &EncodedMetadata,
578 symbol_name: &str,
579) -> Vec<u8> {
580let mut packed_metadata = rustc_metadata::METADATA_HEADER.to_vec();
581packed_metadata.write_all(&(metadata.stub_or_full().len() as u64).to_le_bytes()).unwrap();
582packed_metadata.extend(metadata.stub_or_full());
583584let Some(mut file) = create_object_file(sess) else {
585if sess.target.is_like_wasm {
586return create_metadata_file_for_wasm(sess, &packed_metadata, ".rustc");
587 }
588return packed_metadata.to_vec();
589 };
590if file.format() == BinaryFormat::Xcoff {
591return create_compressed_metadata_file_for_xcoff(file, &packed_metadata, symbol_name);
592 }
593let section = file.add_section(
594file.segment_name(StandardSegment::Data).to_vec(),
595b".rustc".to_vec(),
596 SectionKind::ReadOnlyData,
597 );
598match file.format() {
599 BinaryFormat::Elf => {
600// Explicitly set no flags to avoid SHF_ALLOC default for data section.
601file.section_mut(section).flags = SectionFlags::Elf { sh_flags: 0 };
602 }
603_ => {}
604 };
605let offset = file.append_section_data(section, &packed_metadata, 1);
606607// For MachO and probably PE this is necessary to prevent the linker from throwing away the
608 // .rustc section. For ELF this isn't necessary, but it also doesn't harm.
609file.add_symbol(Symbol {
610 name: symbol_name.as_bytes().to_vec(),
611 value: offset,
612 size: packed_metadata.len() as u64,
613 kind: SymbolKind::Data,
614 scope: SymbolScope::Dynamic,
615 weak: false,
616 section: SymbolSection::Section(section),
617 flags: SymbolFlags::None,
618 });
619620file.write().unwrap()
621}
622623/// * Xcoff - On AIX, custom sections are merged into predefined sections,
624/// so custom .rustc section is not preserved during linking.
625/// For this reason, we store metadata in predefined .info section, and
626/// define a symbol to reference the metadata. To preserve metadata during
627/// linking on AIX, we have to
628/// 1. Create an empty .text section, a empty .data section.
629/// 2. Define an empty symbol named `symbol_name` inside .data section.
630/// 3. Define an symbol named `AIX_METADATA_SYMBOL_NAME` referencing
631/// data inside .info section.
632/// From XCOFF's view, (2) creates a csect entry in the symbol table, the
633/// symbol created by (3) is a info symbol for the preceding csect. Thus
634/// two symbols are preserved during linking and we can use the second symbol
635/// to reference the metadata.
636pub fn create_compressed_metadata_file_for_xcoff(
637mut file: write::Object<'_>,
638 data: &[u8],
639 symbol_name: &str,
640) -> Vec<u8> {
641if !(file.format() == BinaryFormat::Xcoff) {
::core::panicking::panic("assertion failed: file.format() == BinaryFormat::Xcoff")
};assert!(file.format() == BinaryFormat::Xcoff);
642// AIX system linker may aborts if it meets a valid XCOFF file in archive with no .text, no .data and no .bss.
643file.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text);
644let data_section = file.add_section(Vec::new(), b".data".to_vec(), SectionKind::Data);
645let section = file.add_section(Vec::new(), b".info".to_vec(), SectionKind::Debug);
646file.add_file_symbol("lib.rmeta".into());
647file.section_mut(section).flags = SectionFlags::Xcoff { s_flags: xcoff::STYP_INFOas u32 };
648// Add a global symbol to data_section.
649file.add_symbol(Symbol {
650 name: symbol_name.as_bytes().into(),
651 value: 0,
652 size: 0,
653 kind: SymbolKind::Data,
654 scope: SymbolScope::Dynamic,
655 weak: true,
656 section: SymbolSection::Section(data_section),
657 flags: SymbolFlags::None,
658 });
659let len: u32 = data.len().try_into().unwrap();
660let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
661// Add a symbol referring to the rustc metadata.
662file.add_symbol(Symbol {
663 name: AIX_METADATA_SYMBOL_NAME.into(),
664 value: offset + 4, // The metadata is preceded by a 4-byte length field.
665size: 0,
666 kind: SymbolKind::Unknown,
667 scope: SymbolScope::Dynamic,
668 weak: false,
669 section: SymbolSection::Section(section),
670 flags: SymbolFlags::Xcoff {
671 n_sclass: xcoff::C_INFO,
672 x_smtyp: xcoff::C_HIDEXT,
673 x_smclas: xcoff::C_HIDEXT,
674 containing_csect: None,
675 },
676 });
677file.append_section_data(section, data, 1);
678file.write().unwrap()
679}
680681/// Creates a simple WebAssembly object file, which is itself a wasm module,
682/// that contains a custom section of the name `section_name` with contents
683/// `data`.
684///
685/// NB: the `object` crate does not yet have support for writing the wasm
686/// object file format. In lieu of that the `wasm-encoder` crate is used to
687/// build a wasm file by hand.
688///
689/// The wasm object file format is defined at
690/// <https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md>
691/// and mainly consists of a `linking` custom section. In this case the custom
692/// section there is empty except for a version marker indicating what format
693/// it's in.
694///
695/// The main purpose of this is to contain a custom section with `section_name`,
696/// which is then appended after `linking`.
697///
698/// As a further detail the object needs to have a 64-bit memory if `wasm64` is
699/// the target or otherwise it's interpreted as a 32-bit object which is
700/// incompatible with 64-bit ones.
701pub fn create_metadata_file_for_wasm(sess: &Session, data: &[u8], section_name: &str) -> Vec<u8> {
702if !sess.target.is_like_wasm {
::core::panicking::panic("assertion failed: sess.target.is_like_wasm")
};assert!(sess.target.is_like_wasm);
703let mut module = wasm_encoder::Module::new();
704let mut imports = wasm_encoder::ImportSection::new();
705706if sess.target.pointer_width == 64 {
707imports.import(
708"env",
709"__linear_memory",
710 wasm_encoder::MemoryType {
711 minimum: 0,
712 maximum: None,
713 memory64: true,
714 shared: false,
715 page_size_log2: None,
716 },
717 );
718 }
719720if imports.len() > 0 {
721module.section(&imports);
722 }
723module.section(&wasm_encoder::CustomSection {
724 name: "linking".into(),
725 data: Cow::Borrowed(&[2]),
726 });
727module.section(&wasm_encoder::CustomSection { name: section_name.into(), data: data.into() });
728module.finish()
729}