Support writing riscv64 object files (#4995)

This commit is contained in:
bjorn3
2022-10-03 19:27:07 +02:00
committed by GitHub
parent 7bab5c1b28
commit f1fce6c60d

View File

@@ -29,6 +29,7 @@ pub struct ObjectBuilder {
isa: Box<dyn TargetIsa>, isa: Box<dyn TargetIsa>,
binary_format: object::BinaryFormat, binary_format: object::BinaryFormat,
architecture: object::Architecture, architecture: object::Architecture,
flags: object::FileFlags,
endian: object::Endianness, endian: object::Endianness,
name: Vec<u8>, name: Vec<u8>,
libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>, libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>,
@@ -48,6 +49,7 @@ impl ObjectBuilder {
name: V, name: V,
libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>, libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>,
) -> ModuleResult<Self> { ) -> ModuleResult<Self> {
let mut file_flags = object::FileFlags::None;
let binary_format = match isa.triple().binary_format { let binary_format = match isa.triple().binary_format {
target_lexicon::BinaryFormat::Elf => object::BinaryFormat::Elf, target_lexicon::BinaryFormat::Elf => object::BinaryFormat::Elf,
target_lexicon::BinaryFormat::Coff => object::BinaryFormat::Coff, target_lexicon::BinaryFormat::Coff => object::BinaryFormat::Coff,
@@ -72,7 +74,21 @@ impl ObjectBuilder {
target_lexicon::Architecture::X86_64 => object::Architecture::X86_64, target_lexicon::Architecture::X86_64 => object::Architecture::X86_64,
target_lexicon::Architecture::Arm(_) => object::Architecture::Arm, target_lexicon::Architecture::Arm(_) => object::Architecture::Arm,
target_lexicon::Architecture::Aarch64(_) => object::Architecture::Aarch64, target_lexicon::Architecture::Aarch64(_) => object::Architecture::Aarch64,
target_lexicon::Architecture::Riscv64(_) => object::Architecture::Riscv64, target_lexicon::Architecture::Riscv64(_) => {
if binary_format != object::BinaryFormat::Elf {
return Err(ModuleError::Backend(anyhow!(
"binary format {:?} is not supported for riscv64",
binary_format,
)));
}
// FIXME(#4994) get the right variant from the TargetIsa
file_flags = object::FileFlags::Elf {
os_abi: object::elf::ELFOSABI_NONE,
abi_version: 0,
e_flags: object::elf::EF_RISCV_RVC | object::elf::EF_RISCV_FLOAT_ABI_DOUBLE,
};
object::Architecture::Riscv64
}
target_lexicon::Architecture::S390x => object::Architecture::S390x, target_lexicon::Architecture::S390x => object::Architecture::S390x,
architecture => { architecture => {
return Err(ModuleError::Backend(anyhow!( return Err(ModuleError::Backend(anyhow!(
@@ -89,6 +105,7 @@ impl ObjectBuilder {
isa, isa,
binary_format, binary_format,
architecture, architecture,
flags: file_flags,
endian, endian,
name: name.into(), name: name.into(),
libcall_names, libcall_names,
@@ -125,6 +142,7 @@ impl ObjectModule {
/// Create a new `ObjectModule` using the given Cranelift target. /// Create a new `ObjectModule` using the given Cranelift target.
pub fn new(builder: ObjectBuilder) -> Self { pub fn new(builder: ObjectBuilder) -> Self {
let mut object = Object::new(builder.binary_format, builder.architecture, builder.endian); let mut object = Object::new(builder.binary_format, builder.architecture, builder.endian);
object.flags = builder.flags;
object.add_file_symbol(builder.name); object.add_file_symbol(builder.name);
Self { Self {
isa: builder.isa, isa: builder.isa,
@@ -690,6 +708,18 @@ impl ObjectModule {
0, 0,
) )
} }
Reloc::RiscvCall => {
assert_eq!(
self.object.format(),
object::BinaryFormat::Elf,
"RiscvCall is not supported for this file format"
);
(
RelocationKind::Elf(object::elf::R_RISCV_CALL),
RelocationEncoding::Generic,
0,
)
}
// FIXME // FIXME
reloc => unimplemented!("{:?}", reloc), reloc => unimplemented!("{:?}", reloc),
}; };