2021-03-29 11:39:13 +02:00
|
|
|
|
//! Reading of the rustc metadata for rlibs and dylibs
|
|
|
|
|
|
2024-02-22 08:12:09 -08:00
|
|
|
|
use std::borrow::Cow;
|
2021-03-29 11:39:13 +02:00
|
|
|
|
use std::fs::File;
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
use std::io::Write;
|
2021-03-29 11:39:13 +02:00
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
use object::write::{self, StandardSegment, Symbol, SymbolSection};
|
|
|
|
|
use object::{
|
2023-05-23 16:23:59 +08:00
|
|
|
|
elf, pe, xcoff, Architecture, BinaryFormat, Endianness, FileFlags, Object, ObjectSection,
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
|
ObjectSymbol, SectionFlags, SectionKind, SubArchitecture, SymbolFlags, SymbolKind, SymbolScope,
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
};
|
2021-03-29 11:39:13 +02:00
|
|
|
|
use rustc_data_structures::memmap::Mmap;
|
2023-04-10 15:30:11 +00:00
|
|
|
|
use rustc_data_structures::owned_slice::{try_slice_owned, OwnedSlice};
|
2023-11-28 15:48:31 +11:00
|
|
|
|
use rustc_metadata::creader::MetadataLoader;
|
2022-04-24 19:34:35 +09:00
|
|
|
|
use rustc_metadata::fs::METADATA_FILENAME;
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
use rustc_metadata::EncodedMetadata;
|
2024-04-29 13:56:41 +10:00
|
|
|
|
use rustc_middle::bug;
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
use rustc_session::Session;
|
2023-08-01 16:30:02 +01:00
|
|
|
|
use rustc_span::sym;
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
use rustc_target::abi::Endian;
|
2023-01-08 22:59:41 -06:00
|
|
|
|
use rustc_target::spec::{ef_avr_arch, RelocModel, Target};
|
2021-03-29 11:39:13 +02:00
|
|
|
|
|
|
|
|
|
/// The default metadata loader. This is used by cg_llvm and cg_clif.
|
|
|
|
|
///
|
|
|
|
|
/// # Metadata location
|
|
|
|
|
///
|
|
|
|
|
/// <dl>
|
|
|
|
|
/// <dt>rlib</dt>
|
|
|
|
|
/// <dd>The metadata can be found in the `lib.rmeta` file inside of the ar archive.</dd>
|
|
|
|
|
/// <dt>dylib</dt>
|
|
|
|
|
/// <dd>The metadata can be found in the `.rustc` section of the shared library.</dd>
|
|
|
|
|
/// </dl>
|
2023-02-16 14:07:42 +00:00
|
|
|
|
#[derive(Debug)]
|
2024-09-05 15:16:55 +10:00
|
|
|
|
pub(crate) struct DefaultMetadataLoader;
|
2021-03-29 11:39:13 +02:00
|
|
|
|
|
2023-05-23 16:23:59 +08:00
|
|
|
|
static AIX_METADATA_SYMBOL_NAME: &'static str = "__aix_rust_metadata";
|
|
|
|
|
|
2021-03-29 11:39:13 +02:00
|
|
|
|
fn load_metadata_with(
|
|
|
|
|
path: &Path,
|
2023-04-06 17:03:42 +00:00
|
|
|
|
f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
|
2023-04-10 15:30:11 +00:00
|
|
|
|
) -> Result<OwnedSlice, String> {
|
2021-05-10 09:49:42 +02:00
|
|
|
|
let file =
|
|
|
|
|
File::open(path).map_err(|e| format!("failed to open file '{}': {}", path.display(), e))?;
|
2023-04-05 13:26:26 +00:00
|
|
|
|
|
|
|
|
|
unsafe { Mmap::map(file) }
|
|
|
|
|
.map_err(|e| format!("failed to mmap file '{}': {}", path.display(), e))
|
|
|
|
|
.and_then(|mmap| try_slice_owned(mmap, |mmap| f(mmap)))
|
2021-03-29 11:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MetadataLoader for DefaultMetadataLoader {
|
2023-05-23 16:23:59 +08:00
|
|
|
|
fn get_rlib_metadata(&self, target: &Target, path: &Path) -> Result<OwnedSlice, String> {
|
2021-03-29 11:39:13 +02:00
|
|
|
|
load_metadata_with(path, |data| {
|
|
|
|
|
let archive = object::read::archive::ArchiveFile::parse(&*data)
|
2021-05-10 09:49:42 +02:00
|
|
|
|
.map_err(|e| format!("failed to parse rlib '{}': {}", path.display(), e))?;
|
2021-03-29 11:39:13 +02:00
|
|
|
|
|
|
|
|
|
for entry_result in archive.members() {
|
2021-05-10 09:49:42 +02:00
|
|
|
|
let entry = entry_result
|
|
|
|
|
.map_err(|e| format!("failed to parse rlib '{}': {}", path.display(), e))?;
|
2021-03-29 11:39:13 +02:00
|
|
|
|
if entry.name() == METADATA_FILENAME.as_bytes() {
|
2021-04-22 11:53:33 -07:00
|
|
|
|
let data = entry
|
|
|
|
|
.data(data)
|
|
|
|
|
.map_err(|e| format!("failed to parse rlib '{}': {}", path.display(), e))?;
|
2023-05-23 16:23:59 +08:00
|
|
|
|
if target.is_like_aix {
|
|
|
|
|
return get_metadata_xcoff(path, data);
|
|
|
|
|
} else {
|
|
|
|
|
return search_for_section(path, data, ".rmeta");
|
|
|
|
|
}
|
2021-03-29 11:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 09:49:42 +02:00
|
|
|
|
Err(format!("metadata not found in rlib '{}'", path.display()))
|
2021-03-29 11:39:13 +02:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:23:59 +08:00
|
|
|
|
fn get_dylib_metadata(&self, target: &Target, path: &Path) -> Result<OwnedSlice, String> {
|
|
|
|
|
if target.is_like_aix {
|
|
|
|
|
load_metadata_with(path, |data| get_metadata_xcoff(path, data))
|
|
|
|
|
} else {
|
|
|
|
|
load_metadata_with(path, |data| search_for_section(path, data, ".rustc"))
|
|
|
|
|
}
|
2021-03-29 11:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-22 11:53:33 -07:00
|
|
|
|
|
2022-10-16 17:05:53 +03:00
|
|
|
|
pub(super) fn search_for_section<'a>(
|
2021-04-22 11:53:33 -07:00
|
|
|
|
path: &Path,
|
|
|
|
|
bytes: &'a [u8],
|
|
|
|
|
section: &str,
|
|
|
|
|
) -> Result<&'a [u8], String> {
|
2022-02-19 00:48:49 +01:00
|
|
|
|
let Ok(file) = object::File::parse(bytes) else {
|
2021-04-22 11:53:33 -07:00
|
|
|
|
// The parse above could fail for odd reasons like corruption, but for
|
|
|
|
|
// now we just interpret it as this target doesn't support metadata
|
|
|
|
|
// emission in object files so the entire byte slice itself is probably
|
|
|
|
|
// a metadata file. Ideally though if necessary we could at least check
|
|
|
|
|
// the prefix of bytes to see if it's an actual metadata object and if
|
|
|
|
|
// not forward the error along here.
|
2022-02-19 00:48:49 +01:00
|
|
|
|
return Ok(bytes);
|
2021-04-22 11:53:33 -07:00
|
|
|
|
};
|
|
|
|
|
file.section_by_name(section)
|
|
|
|
|
.ok_or_else(|| format!("no `{}` section in '{}'", section, path.display()))?
|
|
|
|
|
.data()
|
|
|
|
|
.map_err(|e| format!("failed to read {} section in '{}': {}", section, path.display(), e))
|
|
|
|
|
}
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
|
2023-04-22 01:00:36 +00:00
|
|
|
|
fn add_gnu_property_note(
|
|
|
|
|
file: &mut write::Object<'static>,
|
|
|
|
|
architecture: Architecture,
|
|
|
|
|
binary_format: BinaryFormat,
|
2023-05-05 18:32:20 +00:00
|
|
|
|
endianness: Endianness,
|
2023-04-22 01:00:36 +00:00
|
|
|
|
) {
|
|
|
|
|
// check bti protection
|
|
|
|
|
if binary_format != BinaryFormat::Elf
|
|
|
|
|
|| !matches!(architecture, Architecture::X86_64 | Architecture::Aarch64)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let section = file.add_section(
|
|
|
|
|
file.segment_name(StandardSegment::Data).to_vec(),
|
|
|
|
|
b".note.gnu.property".to_vec(),
|
|
|
|
|
SectionKind::Note,
|
|
|
|
|
);
|
|
|
|
|
let mut data: Vec<u8> = Vec::new();
|
|
|
|
|
let n_namsz: u32 = 4; // Size of the n_name field
|
|
|
|
|
let n_descsz: u32 = 16; // Size of the n_desc field
|
2023-07-11 23:48:18 +00:00
|
|
|
|
let n_type: u32 = object::elf::NT_GNU_PROPERTY_TYPE_0; // Type of note descriptor
|
2023-05-05 18:32:20 +00:00
|
|
|
|
let header_values = [n_namsz, n_descsz, n_type];
|
2023-05-05 19:06:14 +00:00
|
|
|
|
header_values.iter().for_each(|v| {
|
|
|
|
|
data.extend_from_slice(&match endianness {
|
|
|
|
|
Endianness::Little => v.to_le_bytes(),
|
|
|
|
|
Endianness::Big => v.to_be_bytes(),
|
|
|
|
|
})
|
|
|
|
|
});
|
2023-05-05 18:32:20 +00:00
|
|
|
|
data.extend_from_slice(b"GNU\0"); // Owner of the program property note
|
2023-04-22 01:00:36 +00:00
|
|
|
|
let pr_type: u32 = match architecture {
|
2023-07-11 23:48:18 +00:00
|
|
|
|
Architecture::X86_64 => object::elf::GNU_PROPERTY_X86_FEATURE_1_AND,
|
|
|
|
|
Architecture::Aarch64 => object::elf::GNU_PROPERTY_AARCH64_FEATURE_1_AND,
|
2023-04-22 01:00:36 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
let pr_datasz: u32 = 4; //size of the pr_data field
|
|
|
|
|
let pr_data: u32 = 3; //program property descriptor
|
2023-05-05 18:32:20 +00:00
|
|
|
|
let pr_padding: u32 = 0;
|
|
|
|
|
let property_values = [pr_type, pr_datasz, pr_data, pr_padding];
|
2023-05-05 19:47:00 +00:00
|
|
|
|
property_values.iter().for_each(|v| {
|
|
|
|
|
data.extend_from_slice(&match endianness {
|
|
|
|
|
Endianness::Little => v.to_le_bytes(),
|
|
|
|
|
Endianness::Big => v.to_be_bytes(),
|
|
|
|
|
})
|
|
|
|
|
});
|
2023-05-05 18:32:20 +00:00
|
|
|
|
file.append_section_data(section, &data, 8);
|
2023-04-22 01:00:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:23:59 +08:00
|
|
|
|
pub(super) fn get_metadata_xcoff<'a>(path: &Path, data: &'a [u8]) -> Result<&'a [u8], String> {
|
|
|
|
|
let Ok(file) = object::File::parse(data) else {
|
|
|
|
|
return Ok(data);
|
|
|
|
|
};
|
|
|
|
|
let info_data = search_for_section(path, data, ".info")?;
|
|
|
|
|
if let Some(metadata_symbol) =
|
|
|
|
|
file.symbols().find(|sym| sym.name() == Ok(AIX_METADATA_SYMBOL_NAME))
|
|
|
|
|
{
|
|
|
|
|
let offset = metadata_symbol.address() as usize;
|
2023-12-13 13:28:00 +08:00
|
|
|
|
// The offset specifies the location of rustc metadata in the .info section of XCOFF.
|
2023-12-14 10:31:07 +08:00
|
|
|
|
// Each string stored in .info section of XCOFF is preceded by a 4-byte length field.
|
2023-12-13 13:28:00 +08:00
|
|
|
|
if offset < 4 {
|
2023-07-25 23:04:01 +02:00
|
|
|
|
return Err(format!("Invalid metadata symbol offset: {offset}"));
|
2023-05-23 16:23:59 +08:00
|
|
|
|
}
|
2023-12-13 13:28:00 +08:00
|
|
|
|
// XCOFF format uses big-endian byte order.
|
|
|
|
|
let len = u32::from_be_bytes(info_data[(offset - 4)..offset].try_into().unwrap()) as usize;
|
2023-05-23 16:23:59 +08:00
|
|
|
|
if offset + len > (info_data.len() as usize) {
|
|
|
|
|
return Err(format!(
|
2023-07-25 23:04:01 +02:00
|
|
|
|
"Metadata at offset {offset} with size {len} is beyond .info section"
|
2023-05-23 16:23:59 +08:00
|
|
|
|
));
|
|
|
|
|
}
|
2024-09-09 12:22:00 +02:00
|
|
|
|
Ok(&info_data[offset..(offset + len)])
|
2023-05-23 16:23:59 +08:00
|
|
|
|
} else {
|
2024-09-09 12:22:00 +02:00
|
|
|
|
Err(format!("Unable to find symbol {AIX_METADATA_SYMBOL_NAME}"))
|
|
|
|
|
}
|
2023-05-23 16:23:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-02 22:54:51 +01:00
|
|
|
|
pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static>> {
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
let endianness = match sess.target.options.endian {
|
|
|
|
|
Endian::Little => Endianness::Little,
|
|
|
|
|
Endian::Big => Endianness::Big,
|
|
|
|
|
};
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
|
let (architecture, sub_architecture) = match &sess.target.arch[..] {
|
|
|
|
|
"arm" => (Architecture::Arm, None),
|
|
|
|
|
"aarch64" => (
|
2023-01-09 17:49:24 +00:00
|
|
|
|
if sess.target.pointer_width == 32 {
|
|
|
|
|
Architecture::Aarch64_Ilp32
|
|
|
|
|
} else {
|
|
|
|
|
Architecture::Aarch64
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
|
},
|
|
|
|
|
None,
|
|
|
|
|
),
|
|
|
|
|
"x86" => (Architecture::I386, None),
|
|
|
|
|
"s390x" => (Architecture::S390x, None),
|
|
|
|
|
"mips" | "mips32r6" => (Architecture::Mips, None),
|
|
|
|
|
"mips64" | "mips64r6" => (Architecture::Mips64, None),
|
|
|
|
|
"x86_64" => (
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
if sess.target.pointer_width == 32 {
|
|
|
|
|
Architecture::X86_64_X32
|
|
|
|
|
} else {
|
|
|
|
|
Architecture::X86_64
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
|
},
|
|
|
|
|
None,
|
|
|
|
|
),
|
|
|
|
|
"powerpc" => (Architecture::PowerPc, None),
|
|
|
|
|
"powerpc64" => (Architecture::PowerPc64, None),
|
|
|
|
|
"riscv32" => (Architecture::Riscv32, None),
|
|
|
|
|
"riscv64" => (Architecture::Riscv64, None),
|
2024-08-07 09:56:28 +02:00
|
|
|
|
"sparc" => (Architecture::Sparc32Plus, None),
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
|
"sparc64" => (Architecture::Sparc64, None),
|
|
|
|
|
"avr" => (Architecture::Avr, None),
|
|
|
|
|
"msp430" => (Architecture::Msp430, None),
|
|
|
|
|
"hexagon" => (Architecture::Hexagon, None),
|
|
|
|
|
"bpf" => (Architecture::Bpf, None),
|
|
|
|
|
"loongarch64" => (Architecture::LoongArch64, None),
|
|
|
|
|
"csky" => (Architecture::Csky, None),
|
|
|
|
|
"arm64ec" => (Architecture::Aarch64, Some(SubArchitecture::Arm64EC)),
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
// Unsupported architecture.
|
|
|
|
|
_ => return None,
|
|
|
|
|
};
|
|
|
|
|
let binary_format = if sess.target.is_like_osx {
|
|
|
|
|
BinaryFormat::MachO
|
|
|
|
|
} else if sess.target.is_like_windows {
|
|
|
|
|
BinaryFormat::Coff
|
2023-05-23 16:23:59 +08:00
|
|
|
|
} else if sess.target.is_like_aix {
|
|
|
|
|
BinaryFormat::Xcoff
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
} else {
|
|
|
|
|
BinaryFormat::Elf
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut file = write::Object::new(binary_format, architecture, endianness);
|
Add arm64ec-pc-windows-msvc target
Introduces the `arm64ec-pc-windows-msvc` target for building Arm64EC ("Emulation Compatible") binaries for Windows.
For more information about Arm64EC see <https://learn.microsoft.com/en-us/windows/arm/arm64ec>.
Tier 3 policy:
> A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.)
I will be the maintainer for this target.
> Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target.
Target uses the `arm64ec` architecture to match LLVM and MSVC, and the `-pc-windows-msvc` suffix to indicate that it targets Windows via the MSVC environment.
> Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it.
Target name exactly specifies the type of code that will be produced.
> If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo.
Done.
> Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users.
> The target must not introduce license incompatibilities.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0).
Understood.
> The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements.
> Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3.
> "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users.
Uses the same dependencies, requirements and licensing as the other `*-pc-windows-msvc` targets.
> Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions.
> This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements.
Understood, I am not a member of the Rust team.
> Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions.
Both `core` and `alloc` are supported.
Support for `std` dependends on making changes to the standard library, `stdarch` and `backtrace` which cannot be done yet as the bootstrapping compiler raises a warning ("unexpected `cfg` condition value") for `target_arch = "arm64ec"`.
> The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary.
Documentation is provided in src/doc/rustc/src/platform-support/arm64ec-pc-windows-msvc.md
> Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via @) to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages.
> Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications.
> Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target.
> In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target.
Understood.
2023-12-15 16:46:34 -08:00
|
|
|
|
file.set_sub_architecture(sub_architecture);
|
2023-05-09 00:14:14 -07:00
|
|
|
|
if sess.target.is_like_osx {
|
2023-11-15 14:55:18 +04:00
|
|
|
|
if macho_is_arm64e(&sess.target) {
|
|
|
|
|
file.set_macho_cpu_subtype(object::macho::CPU_SUBTYPE_ARM64E);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 20:18:03 -07:00
|
|
|
|
file.set_macho_build_version(macho_object_build_version_for_target(&sess.target))
|
2023-05-09 00:14:14 -07:00
|
|
|
|
}
|
2023-10-23 19:59:36 -07:00
|
|
|
|
if binary_format == BinaryFormat::Coff {
|
|
|
|
|
// Disable the default mangler to avoid mangling the special "@feat.00" symbol name.
|
|
|
|
|
let original_mangling = file.mangling();
|
|
|
|
|
file.set_mangling(object::write::Mangling::None);
|
|
|
|
|
|
|
|
|
|
let mut feature = 0;
|
|
|
|
|
|
|
|
|
|
if file.architecture() == object::Architecture::I386 {
|
|
|
|
|
// When linking with /SAFESEH on x86, lld requires that all linker inputs be marked as
|
|
|
|
|
// safe exception handling compatible. Metadata files masquerade as regular COFF
|
|
|
|
|
// objects and are treated as linker inputs, despite containing no actual code. Thus,
|
|
|
|
|
// they still need to be marked as safe exception handling compatible. See #96498.
|
|
|
|
|
// Reference: https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
|
|
|
|
|
feature |= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.add_symbol(object::write::Symbol {
|
|
|
|
|
name: "@feat.00".into(),
|
|
|
|
|
value: feature,
|
|
|
|
|
size: 0,
|
|
|
|
|
kind: object::SymbolKind::Data,
|
|
|
|
|
scope: object::SymbolScope::Compilation,
|
|
|
|
|
weak: false,
|
|
|
|
|
section: object::write::SymbolSection::Absolute,
|
|
|
|
|
flags: object::SymbolFlags::None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
file.set_mangling(original_mangling);
|
|
|
|
|
}
|
2022-06-01 23:13:46 +02:00
|
|
|
|
let e_flags = match architecture {
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
Architecture::Mips => {
|
2022-05-01 21:45:50 -04:00
|
|
|
|
let arch = match sess.target.options.cpu.as_ref() {
|
|
|
|
|
"mips1" => elf::EF_MIPS_ARCH_1,
|
|
|
|
|
"mips2" => elf::EF_MIPS_ARCH_2,
|
|
|
|
|
"mips3" => elf::EF_MIPS_ARCH_3,
|
|
|
|
|
"mips4" => elf::EF_MIPS_ARCH_4,
|
|
|
|
|
"mips5" => elf::EF_MIPS_ARCH_5,
|
|
|
|
|
s if s.contains("r6") => elf::EF_MIPS_ARCH_32R6,
|
|
|
|
|
_ => elf::EF_MIPS_ARCH_32R2,
|
|
|
|
|
};
|
2023-07-08 18:44:50 -07:00
|
|
|
|
|
|
|
|
|
let mut e_flags = elf::EF_MIPS_CPIC | arch;
|
|
|
|
|
|
|
|
|
|
// If the ABI is explicitly given, use it or default to O32.
|
|
|
|
|
match sess.target.options.llvm_abiname.to_lowercase().as_str() {
|
|
|
|
|
"n32" => e_flags |= elf::EF_MIPS_ABI2,
|
|
|
|
|
"o32" => e_flags |= elf::EF_MIPS_ABI_O32,
|
|
|
|
|
_ => e_flags |= elf::EF_MIPS_ABI_O32,
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-01 21:45:50 -04:00
|
|
|
|
if sess.target.options.relocation_model != RelocModel::Static {
|
|
|
|
|
e_flags |= elf::EF_MIPS_PIC;
|
|
|
|
|
}
|
|
|
|
|
if sess.target.options.cpu.contains("r6") {
|
|
|
|
|
e_flags |= elf::EF_MIPS_NAN2008;
|
|
|
|
|
}
|
2022-06-01 23:13:46 +02:00
|
|
|
|
e_flags
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
}
|
|
|
|
|
Architecture::Mips64 => {
|
|
|
|
|
// copied from `mips64el-linux-gnuabi64-gcc foo.c -c`
|
2022-01-07 10:47:27 +08:00
|
|
|
|
let e_flags = elf::EF_MIPS_CPIC
|
|
|
|
|
| elf::EF_MIPS_PIC
|
|
|
|
|
| if sess.target.options.cpu.contains("r6") {
|
|
|
|
|
elf::EF_MIPS_ARCH_64R6 | elf::EF_MIPS_NAN2008
|
|
|
|
|
} else {
|
|
|
|
|
elf::EF_MIPS_ARCH_64R2
|
|
|
|
|
};
|
2022-06-01 23:13:46 +02:00
|
|
|
|
e_flags
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
}
|
2023-01-09 00:17:58 -08:00
|
|
|
|
Architecture::Riscv32 | Architecture::Riscv64 => {
|
|
|
|
|
// Source: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/079772828bd10933d34121117a222b4cc0ee2200/riscv-elf.adoc
|
|
|
|
|
let mut e_flags: u32 = 0x0;
|
2023-08-01 16:30:02 +01:00
|
|
|
|
|
2023-01-09 00:17:58 -08:00
|
|
|
|
// Check if compressed is enabled
|
2023-08-02 19:21:19 +01:00
|
|
|
|
// `unstable_target_features` is used here because "c" is gated behind riscv_target_feature.
|
2023-08-01 16:30:02 +01:00
|
|
|
|
if sess.unstable_target_features.contains(&sym::c) {
|
2023-01-09 00:17:58 -08:00
|
|
|
|
e_flags |= elf::EF_RISCV_RVC;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 16:30:02 +01:00
|
|
|
|
// Set the appropriate flag based on ABI
|
|
|
|
|
// This needs to match LLVM `RISCVELFStreamer.cpp`
|
|
|
|
|
match &*sess.target.llvm_abiname {
|
2023-08-04 15:56:01 +01:00
|
|
|
|
"" | "ilp32" | "lp64" => (),
|
2023-08-01 16:30:02 +01:00
|
|
|
|
"ilp32f" | "lp64f" => e_flags |= elf::EF_RISCV_FLOAT_ABI_SINGLE,
|
|
|
|
|
"ilp32d" | "lp64d" => e_flags |= elf::EF_RISCV_FLOAT_ABI_DOUBLE,
|
|
|
|
|
"ilp32e" => e_flags |= elf::EF_RISCV_RVE,
|
|
|
|
|
_ => bug!("unknown RISC-V ABI name"),
|
2023-01-09 00:17:58 -08:00
|
|
|
|
}
|
2023-08-01 16:30:02 +01:00
|
|
|
|
|
2022-06-01 23:13:46 +02:00
|
|
|
|
e_flags
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
}
|
2023-02-03 11:47:27 +08:00
|
|
|
|
Architecture::LoongArch64 => {
|
2023-06-12 19:38:56 +08:00
|
|
|
|
// Source: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc#e_flags-identifies-abi-type-and-version
|
|
|
|
|
let mut e_flags: u32 = elf::EF_LARCH_OBJABI_V1;
|
|
|
|
|
|
2023-08-01 16:30:02 +01:00
|
|
|
|
// Set the appropriate flag based on ABI
|
|
|
|
|
// This needs to match LLVM `LoongArchELFStreamer.cpp`
|
|
|
|
|
match &*sess.target.llvm_abiname {
|
|
|
|
|
"ilp32s" | "lp64s" => e_flags |= elf::EF_LARCH_ABI_SOFT_FLOAT,
|
|
|
|
|
"ilp32f" | "lp64f" => e_flags |= elf::EF_LARCH_ABI_SINGLE_FLOAT,
|
|
|
|
|
"ilp32d" | "lp64d" => e_flags |= elf::EF_LARCH_ABI_DOUBLE_FLOAT,
|
2024-03-08 14:25:11 +08:00
|
|
|
|
_ => bug!("unknown LoongArch ABI name"),
|
2023-06-12 19:38:56 +08:00
|
|
|
|
}
|
2023-08-01 16:30:02 +01:00
|
|
|
|
|
2023-06-12 19:38:56 +08:00
|
|
|
|
e_flags
|
2023-02-03 11:47:27 +08:00
|
|
|
|
}
|
2023-01-08 22:59:41 -06:00
|
|
|
|
Architecture::Avr => {
|
|
|
|
|
// Resolve the ISA revision and set
|
|
|
|
|
// the appropriate EF_AVR_ARCH flag.
|
|
|
|
|
ef_avr_arch(&sess.target.options.cpu)
|
|
|
|
|
}
|
2023-07-14 19:16:38 +08:00
|
|
|
|
Architecture::Csky => {
|
|
|
|
|
let e_flags = match sess.target.options.abi.as_ref() {
|
|
|
|
|
"abiv2" => elf::EF_CSKY_ABIV2,
|
|
|
|
|
_ => elf::EF_CSKY_ABIV1,
|
|
|
|
|
};
|
|
|
|
|
e_flags
|
|
|
|
|
}
|
2022-06-01 23:13:46 +02:00
|
|
|
|
_ => 0,
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
};
|
2022-06-02 00:16:00 +02:00
|
|
|
|
// adapted from LLVM's `MCELFObjectTargetWriter::getOSABI`
|
|
|
|
|
let os_abi = match sess.target.options.os.as_ref() {
|
|
|
|
|
"hermit" => elf::ELFOSABI_STANDALONE,
|
|
|
|
|
"freebsd" => elf::ELFOSABI_FREEBSD,
|
|
|
|
|
"solaris" => elf::ELFOSABI_SOLARIS,
|
|
|
|
|
_ => elf::ELFOSABI_NONE,
|
|
|
|
|
};
|
|
|
|
|
let abi_version = 0;
|
2023-05-05 18:32:20 +00:00
|
|
|
|
add_gnu_property_note(&mut file, architecture, binary_format, endianness);
|
2022-06-02 00:16:00 +02:00
|
|
|
|
file.flags = FileFlags::Elf { os_abi, abi_version, e_flags };
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
Some(file)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 06:45:02 +02:00
|
|
|
|
/// Mach-O files contain information about:
|
|
|
|
|
/// - The platform/OS they were built for (macOS/watchOS/Mac Catalyst/iOS simulator etc).
|
|
|
|
|
/// - The minimum OS version / deployment target.
|
|
|
|
|
/// - The version of the SDK they were targetting.
|
|
|
|
|
///
|
|
|
|
|
/// In the past, this was accomplished using the LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
|
|
|
|
|
/// LC_VERSION_MIN_TVOS or LC_VERSION_MIN_WATCHOS load commands, which each contain information
|
|
|
|
|
/// about the deployment target and SDK version, and implicitly, by their presence, which OS they
|
|
|
|
|
/// target. Simulator targets were determined if the architecture was x86_64, but there was e.g. a
|
|
|
|
|
/// LC_VERSION_MIN_IPHONEOS present.
|
|
|
|
|
///
|
|
|
|
|
/// This is of course brittle and limited, so modern tooling emit the LC_BUILD_VERSION load
|
|
|
|
|
/// command (which contains all three pieces of information in one) when the deployment target is
|
|
|
|
|
/// high enough, or the target is something that wouldn't be encodable with the old load commands
|
|
|
|
|
/// (such as Mac Catalyst, or Aarch64 iOS simulator).
|
|
|
|
|
///
|
|
|
|
|
/// Since Xcode 15, Apple's LD apparently requires object files to use this load command, so this
|
|
|
|
|
/// returns the `MachOBuildVersion` for the target to do so.
|
2023-07-26 20:18:03 -07:00
|
|
|
|
fn macho_object_build_version_for_target(target: &Target) -> object::write::MachOBuildVersion {
|
2023-05-09 00:14:14 -07:00
|
|
|
|
/// The `object` crate demands "X.Y.Z encoded in nibbles as xxxx.yy.zz"
|
|
|
|
|
/// e.g. minOS 14.0 = 0x000E0000, or SDK 16.2 = 0x00100200
|
2024-09-05 06:45:02 +02:00
|
|
|
|
fn pack_version((major, minor, patch): (u16, u8, u8)) -> u32 {
|
|
|
|
|
let (major, minor, patch) = (major as u32, minor as u32, patch as u32);
|
|
|
|
|
(major << 16) | (minor << 8) | patch
|
2023-05-09 00:14:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 20:18:03 -07:00
|
|
|
|
let platform =
|
|
|
|
|
rustc_target::spec::current_apple_platform(target).expect("unknown Apple target OS");
|
2024-09-05 06:45:02 +02:00
|
|
|
|
let min_os = rustc_target::spec::current_apple_deployment_target(target);
|
|
|
|
|
let (sdk_major, sdk_minor) =
|
2023-07-26 20:18:03 -07:00
|
|
|
|
rustc_target::spec::current_apple_sdk_version(platform).expect("unknown Apple target OS");
|
2023-05-09 00:14:14 -07:00
|
|
|
|
|
|
|
|
|
let mut build_version = object::write::MachOBuildVersion::default();
|
|
|
|
|
build_version.platform = platform;
|
|
|
|
|
build_version.minos = pack_version(min_os);
|
2024-09-05 06:45:02 +02:00
|
|
|
|
build_version.sdk = pack_version((sdk_major, sdk_minor, 0));
|
2023-07-26 20:18:03 -07:00
|
|
|
|
build_version
|
2023-05-09 00:14:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 14:55:18 +04:00
|
|
|
|
/// Is Apple's CPU subtype `arm64e`s
|
|
|
|
|
fn macho_is_arm64e(target: &Target) -> bool {
|
2024-09-09 12:22:00 +02:00
|
|
|
|
target.llvm_target.starts_with("arm64e")
|
2023-11-15 14:55:18 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-05 15:16:55 +10:00
|
|
|
|
pub(crate) enum MetadataPosition {
|
2022-02-10 18:43:51 +01:00
|
|
|
|
First,
|
|
|
|
|
Last,
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
|
/// For rlibs we "pack" rustc metadata into a dummy object file.
|
|
|
|
|
///
|
|
|
|
|
/// Historically it was needed because rustc linked rlibs as whole-archive in some cases.
|
|
|
|
|
/// In that case linkers try to include all files located in an archive, so if metadata is stored
|
|
|
|
|
/// in an archive then it needs to be of a form that the linker is able to process.
|
|
|
|
|
/// Now it's not clear whether metadata still needs to be wrapped into an object file or not.
|
|
|
|
|
///
|
|
|
|
|
/// Note, though, that we don't actually want this metadata to show up in any
|
|
|
|
|
/// final output of the compiler. Instead this is purely for rustc's own
|
|
|
|
|
/// metadata tracking purposes.
|
|
|
|
|
///
|
|
|
|
|
/// With the above in mind, each "flavor" of object format gets special
|
|
|
|
|
/// handling here depending on the target:
|
|
|
|
|
///
|
|
|
|
|
/// * MachO - macos-like targets will insert the metadata into a section that
|
|
|
|
|
/// is sort of fake dwarf debug info. Inspecting the source of the macos
|
|
|
|
|
/// linker this causes these sections to be skipped automatically because
|
|
|
|
|
/// it's not in an allowlist of otherwise well known dwarf section names to
|
|
|
|
|
/// go into the final artifact.
|
|
|
|
|
///
|
2024-02-02 13:26:18 -08:00
|
|
|
|
/// * WebAssembly - this uses wasm files themselves as the object file format
|
|
|
|
|
/// so an empty file with no linking metadata but a single custom section is
|
|
|
|
|
/// created holding our metadata.
|
2022-11-27 11:15:06 +00:00
|
|
|
|
///
|
|
|
|
|
/// * COFF - Windows-like targets create an object with a section that has
|
|
|
|
|
/// the `IMAGE_SCN_LNK_REMOVE` flag set which ensures that if the linker
|
|
|
|
|
/// ever sees the section it doesn't process it and it's removed.
|
|
|
|
|
///
|
|
|
|
|
/// * ELF - All other targets are similar to Windows in that there's a
|
|
|
|
|
/// `SHF_EXCLUDE` flag we can set on sections in an object file to get
|
|
|
|
|
/// automatically removed from the final output.
|
2024-09-05 15:16:55 +10:00
|
|
|
|
pub(crate) fn create_wrapper_file(
|
2022-10-16 17:05:53 +03:00
|
|
|
|
sess: &Session,
|
2024-02-22 08:12:09 -08:00
|
|
|
|
section_name: String,
|
2022-10-16 17:05:53 +03:00
|
|
|
|
data: &[u8],
|
|
|
|
|
) -> (Vec<u8>, MetadataPosition) {
|
2022-02-15 05:58:25 +01:00
|
|
|
|
let Some(mut file) = create_object_file(sess) else {
|
2024-02-02 13:26:18 -08:00
|
|
|
|
if sess.target.is_like_wasm {
|
2024-02-22 08:12:09 -08:00
|
|
|
|
return (
|
|
|
|
|
create_metadata_file_for_wasm(sess, data, §ion_name),
|
|
|
|
|
MetadataPosition::First,
|
|
|
|
|
);
|
2024-02-02 13:26:18 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Targets using this branch don't have support implemented here yet or
|
|
|
|
|
// they're not yet implemented in the `object` crate and will likely
|
|
|
|
|
// fill out this module over time.
|
2022-10-16 17:05:53 +03:00
|
|
|
|
return (data.to_vec(), MetadataPosition::Last);
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
};
|
2023-05-23 16:23:59 +08:00
|
|
|
|
let section = if file.format() == BinaryFormat::Xcoff {
|
|
|
|
|
file.add_section(Vec::new(), b".info".to_vec(), SectionKind::Debug)
|
|
|
|
|
} else {
|
|
|
|
|
file.add_section(
|
|
|
|
|
file.segment_name(StandardSegment::Debug).to_vec(),
|
2024-02-22 08:12:09 -08:00
|
|
|
|
section_name.into_bytes(),
|
2023-05-23 16:23:59 +08:00
|
|
|
|
SectionKind::Debug,
|
|
|
|
|
)
|
|
|
|
|
};
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
match file.format() {
|
|
|
|
|
BinaryFormat::Coff => {
|
|
|
|
|
file.section_mut(section).flags =
|
2021-12-19 17:36:48 +01:00
|
|
|
|
SectionFlags::Coff { characteristics: pe::IMAGE_SCN_LNK_REMOVE };
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
}
|
|
|
|
|
BinaryFormat::Elf => {
|
2021-12-19 17:36:48 +01:00
|
|
|
|
file.section_mut(section).flags =
|
|
|
|
|
SectionFlags::Elf { sh_flags: elf::SHF_EXCLUDE as u64 };
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
}
|
2023-05-23 16:23:59 +08:00
|
|
|
|
BinaryFormat::Xcoff => {
|
2023-06-05 15:15:09 +08:00
|
|
|
|
// AIX system linker may aborts if it meets a valid XCOFF file in archive with no .text, no .data and no .bss.
|
2023-05-23 16:23:59 +08:00
|
|
|
|
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 };
|
2023-12-13 13:28:00 +08:00
|
|
|
|
// Encode string stored in .info section of XCOFF.
|
2023-12-14 10:31:07 +08:00
|
|
|
|
// 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();
|
2023-12-13 13:28:00 +08:00
|
|
|
|
let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
|
2023-05-23 16:23:59 +08:00
|
|
|
|
// Add a symbol referring to the data in .info section.
|
|
|
|
|
file.add_symbol(Symbol {
|
|
|
|
|
name: AIX_METADATA_SYMBOL_NAME.into(),
|
|
|
|
|
value: offset + 4,
|
|
|
|
|
size: 0,
|
|
|
|
|
kind: SymbolKind::Unknown,
|
2023-06-05 15:15:09 +08:00
|
|
|
|
scope: SymbolScope::Compilation,
|
2023-05-23 16:23:59 +08:00
|
|
|
|
weak: false,
|
|
|
|
|
section: SymbolSection::Section(section),
|
|
|
|
|
flags: SymbolFlags::Xcoff {
|
|
|
|
|
n_sclass: xcoff::C_INFO,
|
|
|
|
|
x_smtyp: xcoff::C_HIDEXT,
|
|
|
|
|
x_smclas: xcoff::C_HIDEXT,
|
|
|
|
|
containing_csect: None,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
_ => {}
|
|
|
|
|
};
|
2022-10-16 17:05:53 +03:00
|
|
|
|
file.append_section_data(section, data, 1);
|
2022-02-10 18:43:51 +01:00
|
|
|
|
(file.write().unwrap(), MetadataPosition::First)
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Historical note:
|
|
|
|
|
//
|
|
|
|
|
// When using link.exe it was seen that the section name `.note.rustc`
|
|
|
|
|
// was getting shortened to `.note.ru`, and according to the PE and COFF
|
|
|
|
|
// specification:
|
|
|
|
|
//
|
|
|
|
|
// > Executable images do not use a string table and do not support
|
|
|
|
|
// > section names longer than 8 characters
|
|
|
|
|
//
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
|
|
|
|
|
//
|
|
|
|
|
// As a result, we choose a slightly shorter name! As to why
|
|
|
|
|
// `.note.rustc` works on MinGW, see
|
|
|
|
|
// https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/lld/COFF/Writer.cpp#L1190-L1197
|
|
|
|
|
pub fn create_compressed_metadata_file(
|
|
|
|
|
sess: &Session,
|
|
|
|
|
metadata: &EncodedMetadata,
|
|
|
|
|
symbol_name: &str,
|
|
|
|
|
) -> Vec<u8> {
|
2023-07-19 14:53:26 +00:00
|
|
|
|
let mut packed_metadata = rustc_metadata::METADATA_HEADER.to_vec();
|
2023-11-26 20:45:56 -05:00
|
|
|
|
packed_metadata.write_all(&(metadata.raw_data().len() as u64).to_le_bytes()).unwrap();
|
2023-07-19 14:53:26 +00:00
|
|
|
|
packed_metadata.extend(metadata.raw_data());
|
2023-02-13 13:39:28 -05:00
|
|
|
|
|
2022-02-15 05:58:25 +01:00
|
|
|
|
let Some(mut file) = create_object_file(sess) else {
|
2024-02-02 13:26:18 -08:00
|
|
|
|
if sess.target.is_like_wasm {
|
2024-02-22 08:12:09 -08:00
|
|
|
|
return create_metadata_file_for_wasm(sess, &packed_metadata, ".rustc");
|
2024-02-02 13:26:18 -08:00
|
|
|
|
}
|
2023-07-19 14:53:26 +00:00
|
|
|
|
return packed_metadata.to_vec();
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
};
|
2023-05-23 16:23:59 +08:00
|
|
|
|
if file.format() == BinaryFormat::Xcoff {
|
2023-07-19 14:53:26 +00:00
|
|
|
|
return create_compressed_metadata_file_for_xcoff(file, &packed_metadata, symbol_name);
|
2023-05-23 16:23:59 +08:00
|
|
|
|
}
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
let section = file.add_section(
|
|
|
|
|
file.segment_name(StandardSegment::Data).to_vec(),
|
|
|
|
|
b".rustc".to_vec(),
|
2021-12-17 11:26:39 +01:00
|
|
|
|
SectionKind::ReadOnlyData,
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
);
|
2021-12-17 11:26:39 +01:00
|
|
|
|
match file.format() {
|
|
|
|
|
BinaryFormat::Elf => {
|
|
|
|
|
// Explicitly set no flags to avoid SHF_ALLOC default for data section.
|
|
|
|
|
file.section_mut(section).flags = SectionFlags::Elf { sh_flags: 0 };
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
};
|
2023-07-19 14:53:26 +00:00
|
|
|
|
let offset = file.append_section_data(section, &packed_metadata, 1);
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
|
|
|
|
|
// For MachO and probably PE this is necessary to prevent the linker from throwing away the
|
|
|
|
|
// .rustc section. For ELF this isn't necessary, but it also doesn't harm.
|
|
|
|
|
file.add_symbol(Symbol {
|
|
|
|
|
name: symbol_name.as_bytes().to_vec(),
|
|
|
|
|
value: offset,
|
2023-07-19 14:53:26 +00:00
|
|
|
|
size: packed_metadata.len() as u64,
|
Use object crate for .rustc metadata generation
We already use the object crate for generating uncompressed .rmeta
metadata object files. This switches the generation of compressed
.rustc object files to use the object crate as well. These have
slightly different requirements in that .rmeta should be completely
excluded from any final compilation artifacts, while .rustc should
be part of shared objects, but not loaded into memory.
The primary motivation for this change is #90326: In LLVM 14, the
current way of setting section flags (and in particular, preventing
the setting of SHF_ALLOC) will no longer work. There are other ways
we could work around this, but switching to the object crate seems
like the most elegant, as we already use it for .rmeta, and as it
makes this independent of the codegen backend. In particular, we
don't need separate handling in codegen_llvm and codegen_gcc.
codegen_cranelift should be able to reuse the implementation as
well, though I have omitted that here, as it is not based on
codegen_ssa.
This change mostly extracts the existing code for .rmeta handling
to allow using it for .rustc as well, and adjust the codegen
infrastructure to handle the metadata object file separately: We
no longer create a backend-specific module for it, and directly
produce the compiled module instead.
This does not fix #90326 by itself yet, as .llvmbc will need to be
handled separately.
2021-12-02 12:24:25 +01:00
|
|
|
|
kind: SymbolKind::Data,
|
|
|
|
|
scope: SymbolScope::Dynamic,
|
|
|
|
|
weak: false,
|
|
|
|
|
section: SymbolSection::Section(section),
|
|
|
|
|
flags: SymbolFlags::None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
file.write().unwrap()
|
|
|
|
|
}
|
2023-05-23 16:23:59 +08:00
|
|
|
|
|
|
|
|
|
/// * Xcoff - On AIX, custom sections are merged into predefined sections,
|
|
|
|
|
/// so custom .rustc section is not preserved during linking.
|
|
|
|
|
/// For this reason, we store metadata in predefined .info section, and
|
|
|
|
|
/// define a symbol to reference the metadata. To preserve metadata during
|
|
|
|
|
/// linking on AIX, we have to
|
|
|
|
|
/// 1. Create an empty .text section, a empty .data section.
|
|
|
|
|
/// 2. Define an empty symbol named `symbol_name` inside .data section.
|
|
|
|
|
/// 3. Define an symbol named `AIX_METADATA_SYMBOL_NAME` referencing
|
|
|
|
|
/// data inside .info section.
|
|
|
|
|
/// From XCOFF's view, (2) creates a csect entry in the symbol table, the
|
|
|
|
|
/// symbol created by (3) is a info symbol for the preceding csect. Thus
|
|
|
|
|
/// two symbols are preserved during linking and we can use the second symbol
|
|
|
|
|
/// to reference the metadata.
|
|
|
|
|
pub fn create_compressed_metadata_file_for_xcoff(
|
|
|
|
|
mut file: write::Object<'_>,
|
|
|
|
|
data: &[u8],
|
|
|
|
|
symbol_name: &str,
|
|
|
|
|
) -> Vec<u8> {
|
|
|
|
|
assert!(file.format() == BinaryFormat::Xcoff);
|
2023-06-05 15:15:09 +08:00
|
|
|
|
// AIX system linker may aborts if it meets a valid XCOFF file in archive with no .text, no .data and no .bss.
|
2023-05-23 16:23:59 +08:00
|
|
|
|
file.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text);
|
|
|
|
|
let data_section = file.add_section(Vec::new(), b".data".to_vec(), SectionKind::Data);
|
|
|
|
|
let section = file.add_section(Vec::new(), b".info".to_vec(), SectionKind::Debug);
|
|
|
|
|
file.add_file_symbol("lib.rmeta".into());
|
|
|
|
|
file.section_mut(section).flags = SectionFlags::Xcoff { s_flags: xcoff::STYP_INFO as u32 };
|
|
|
|
|
// Add a global symbol to data_section.
|
|
|
|
|
file.add_symbol(Symbol {
|
|
|
|
|
name: symbol_name.as_bytes().into(),
|
|
|
|
|
value: 0,
|
|
|
|
|
size: 0,
|
|
|
|
|
kind: SymbolKind::Data,
|
|
|
|
|
scope: SymbolScope::Dynamic,
|
|
|
|
|
weak: true,
|
|
|
|
|
section: SymbolSection::Section(data_section),
|
|
|
|
|
flags: SymbolFlags::None,
|
|
|
|
|
});
|
2023-12-18 09:41:36 +08:00
|
|
|
|
let len: u32 = data.len().try_into().unwrap();
|
2023-12-13 13:28:00 +08:00
|
|
|
|
let offset = file.append_section_data(section, &len.to_be_bytes(), 1);
|
2023-05-23 16:23:59 +08:00
|
|
|
|
// Add a symbol referring to the rustc metadata.
|
|
|
|
|
file.add_symbol(Symbol {
|
|
|
|
|
name: AIX_METADATA_SYMBOL_NAME.into(),
|
2023-12-13 13:28:00 +08:00
|
|
|
|
value: offset + 4, // The metadata is preceded by a 4-byte length field.
|
2023-05-23 16:23:59 +08:00
|
|
|
|
size: 0,
|
|
|
|
|
kind: SymbolKind::Unknown,
|
|
|
|
|
scope: SymbolScope::Dynamic,
|
|
|
|
|
weak: false,
|
|
|
|
|
section: SymbolSection::Section(section),
|
|
|
|
|
flags: SymbolFlags::Xcoff {
|
|
|
|
|
n_sclass: xcoff::C_INFO,
|
|
|
|
|
x_smtyp: xcoff::C_HIDEXT,
|
|
|
|
|
x_smclas: xcoff::C_HIDEXT,
|
|
|
|
|
containing_csect: None,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
file.append_section_data(section, data, 1);
|
|
|
|
|
file.write().unwrap()
|
|
|
|
|
}
|
2024-02-02 13:26:18 -08:00
|
|
|
|
|
|
|
|
|
/// Creates a simple WebAssembly object file, which is itself a wasm module,
|
|
|
|
|
/// that contains a custom section of the name `section_name` with contents
|
|
|
|
|
/// `data`.
|
|
|
|
|
///
|
2024-02-23 18:26:01 +08:00
|
|
|
|
/// NB: the `object` crate does not yet have support for writing the wasm
|
2024-02-22 08:12:09 -08:00
|
|
|
|
/// object file format. In lieu of that the `wasm-encoder` crate is used to
|
|
|
|
|
/// build a wasm file by hand.
|
2024-02-02 13:26:18 -08:00
|
|
|
|
///
|
2024-02-22 08:12:09 -08:00
|
|
|
|
/// The wasm object file format is defined at
|
2024-02-02 13:26:18 -08:00
|
|
|
|
/// <https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md>
|
2024-02-22 08:12:09 -08:00
|
|
|
|
/// and mainly consists of a `linking` custom section. In this case the custom
|
|
|
|
|
/// section there is empty except for a version marker indicating what format
|
|
|
|
|
/// it's in.
|
2024-02-02 13:26:18 -08:00
|
|
|
|
///
|
2024-02-22 08:12:09 -08:00
|
|
|
|
/// The main purpose of this is to contain a custom section with `section_name`,
|
|
|
|
|
/// which is then appended after `linking`.
|
|
|
|
|
///
|
|
|
|
|
/// As a further detail the object needs to have a 64-bit memory if `wasm64` is
|
|
|
|
|
/// the target or otherwise it's interpreted as a 32-bit object which is
|
|
|
|
|
/// incompatible with 64-bit ones.
|
|
|
|
|
pub fn create_metadata_file_for_wasm(sess: &Session, data: &[u8], section_name: &str) -> Vec<u8> {
|
|
|
|
|
assert!(sess.target.is_like_wasm);
|
|
|
|
|
let mut module = wasm_encoder::Module::new();
|
|
|
|
|
let mut imports = wasm_encoder::ImportSection::new();
|
|
|
|
|
|
|
|
|
|
if sess.target.pointer_width == 64 {
|
|
|
|
|
imports.import(
|
|
|
|
|
"env",
|
|
|
|
|
"__linear_memory",
|
2024-07-21 13:56:16 +03:00
|
|
|
|
wasm_encoder::MemoryType {
|
|
|
|
|
minimum: 0,
|
|
|
|
|
maximum: None,
|
|
|
|
|
memory64: true,
|
|
|
|
|
shared: false,
|
|
|
|
|
page_size_log2: None,
|
|
|
|
|
},
|
2024-02-02 13:26:18 -08:00
|
|
|
|
);
|
2024-02-22 08:12:09 -08:00
|
|
|
|
}
|
2024-02-02 13:26:18 -08:00
|
|
|
|
|
2024-02-22 08:12:09 -08:00
|
|
|
|
if imports.len() > 0 {
|
|
|
|
|
module.section(&imports);
|
|
|
|
|
}
|
|
|
|
|
module.section(&wasm_encoder::CustomSection {
|
|
|
|
|
name: "linking".into(),
|
|
|
|
|
data: Cow::Borrowed(&[2]),
|
|
|
|
|
});
|
|
|
|
|
module.section(&wasm_encoder::CustomSection { name: section_name.into(), data: data.into() });
|
|
|
|
|
module.finish()
|
2024-02-02 13:26:18 -08:00
|
|
|
|
}
|