Skip to content

Bump object to 0.19.0 #1767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ wasmtime-wast = { path = "crates/wast", version = "0.18.0" }
wasmtime-wasi = { path = "crates/wasi", version = "0.18.0" }
wasi-common = { path = "crates/wasi-common", version = "0.18.0" }
structopt = { version = "0.3.5", features = ["color", "suggestions"] }
object = { version = "0.18", default-features = false, features = ["write"] }
object = { version = "0.19", default-features = false, features = ["write"] }
anyhow = "1.0.19"
target-lexicon = { version = "0.10.0", default-features = false }
pretty_env_logger = "0.4.0"
Expand Down
3 changes: 2 additions & 1 deletion cranelift/object/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ edition = "2018"
[dependencies]
cranelift-module = { path = "../module", version = "0.65.0" }
cranelift-codegen = { path = "../codegen", version = "0.65.0", default-features = false, features = ["std"] }
object = { version = "0.18", default-features = false, features = ["write"] }
object = { version = "0.19", default-features = false, features = ["write"] }
target-lexicon = "0.10"
anyhow = "1.0"

[badges]
maintenance = { status = "experimental" }
63 changes: 50 additions & 13 deletions cranelift/object/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//! Defines `ObjectBackend`.

use anyhow::anyhow;
use cranelift_codegen::binemit::{
Addend, CodeOffset, NullStackmapSink, Reloc, RelocSink, TrapSink,
};
use cranelift_codegen::entity::SecondaryMap;
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::{self, binemit, ir};
use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleNamespace,
ModuleResult,
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleError,
ModuleNamespace, ModuleResult,
};
use object::write::{
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
Expand All @@ -18,11 +19,14 @@ use object::{
};
use std::collections::HashMap;
use std::mem;
use target_lexicon::{BinaryFormat, PointerWidth};
use target_lexicon::PointerWidth;

/// A builder for `ObjectBackend`.
pub struct ObjectBuilder {
isa: Box<dyn TargetIsa>,
binary_format: object::BinaryFormat,
architecture: object::Architecture,
endian: object::Endianness,
name: Vec<u8>,
libcall_names: Box<dyn Fn(ir::LibCall) -> String>,
function_alignment: u64,
Expand All @@ -40,13 +44,47 @@ impl ObjectBuilder {
isa: Box<dyn TargetIsa>,
name: V,
libcall_names: Box<dyn Fn(ir::LibCall) -> String>,
) -> Self {
Self {
) -> ModuleResult<Self> {
let binary_format = match isa.triple().binary_format {
target_lexicon::BinaryFormat::Elf => object::BinaryFormat::Elf,
target_lexicon::BinaryFormat::Coff => object::BinaryFormat::Coff,
target_lexicon::BinaryFormat::Macho => object::BinaryFormat::MachO,
target_lexicon::BinaryFormat::Wasm => {
return Err(ModuleError::Backend(anyhow!(
"binary format wasm is unsupported",
)))
}
target_lexicon::BinaryFormat::Unknown => {
return Err(ModuleError::Backend(anyhow!("binary format is unknown")))
}
};
let architecture = match isa.triple().architecture {
target_lexicon::Architecture::I386
| target_lexicon::Architecture::I586
| target_lexicon::Architecture::I686 => object::Architecture::I386,
target_lexicon::Architecture::X86_64 => object::Architecture::X86_64,
target_lexicon::Architecture::Arm(_) => object::Architecture::Arm,
target_lexicon::Architecture::Aarch64(_) => object::Architecture::Aarch64,
architecture => {
return Err(ModuleError::Backend(anyhow!(
"target architecture {:?} is unsupported",
architecture,
)))
}
};
let endian = match isa.triple().endianness().unwrap() {
target_lexicon::Endianness::Little => object::Endianness::Little,
target_lexicon::Endianness::Big => object::Endianness::Big,
};
Ok(Self {
isa,
binary_format,
architecture,
endian,
name: name.into(),
libcall_names,
function_alignment: 1,
}
})
}

/// Set the alignment used for functions.
Expand Down Expand Up @@ -85,8 +123,7 @@ impl Backend for ObjectBackend {

/// Create a new `ObjectBackend` using the given Cranelift target.
fn new(builder: ObjectBuilder) -> Self {
let triple = builder.isa.triple();
let mut object = Object::new(triple.binary_format, triple.architecture);
let mut object = Object::new(builder.binary_format, builder.architecture, builder.endian);
object.add_file_symbol(builder.name);
Self {
isa: builder.isa,
Expand Down Expand Up @@ -383,7 +420,7 @@ impl Backend for ObjectBackend {
}

// Indicate that this object has a non-executable stack.
if self.object.format() == BinaryFormat::Elf {
if self.object.format() == object::BinaryFormat::Elf {
self.object.add_section(
vec![],
".note.GNU-stack".as_bytes().to_vec(),
Expand Down Expand Up @@ -509,12 +546,12 @@ struct RelocRecord {
}

struct ObjectRelocSink {
format: BinaryFormat,
format: object::BinaryFormat,
relocs: Vec<RelocRecord>,
}

impl ObjectRelocSink {
fn new(format: BinaryFormat) -> Self {
fn new(format: object::BinaryFormat) -> Self {
Self {
format,
relocs: vec![],
Expand Down Expand Up @@ -552,7 +589,7 @@ impl RelocSink for ObjectRelocSink {
Reloc::ElfX86_64TlsGd => {
assert_eq!(
self.format,
BinaryFormat::Elf,
object::BinaryFormat::Elf,
"ElfX86_64TlsGd is not supported for this file format"
);
(
Expand All @@ -564,7 +601,7 @@ impl RelocSink for ObjectRelocSink {
Reloc::MachOX86_64Tlv => {
assert_eq!(
self.format,
BinaryFormat::Macho,
object::BinaryFormat::MachO,
"MachOX86_64Tlv is not supported for this file format"
);
addend += 4; // X86_64_RELOC_TLV has an implicit addend of -4
Expand Down
2 changes: 1 addition & 1 deletion crates/debug/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ edition = "2018"
[dependencies]
gimli = "0.21.0"
wasmparser = "0.57.0"
object = { version = "0.18", default-features = false, features = ["write"] }
object = { version = "0.19", default-features = false, features = ["write"] }
wasmtime-environ = { path = "../environ", version = "0.18.0" }
target-lexicon = { version = "0.10.0", default-features = false }
anyhow = "1.0"
Expand Down
16 changes: 12 additions & 4 deletions crates/debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#![allow(clippy::cast_ptr_alignment)]

use anyhow::Error;
use anyhow::{bail, Error};
use more_asserts::assert_gt;
use object::write::{Object, Relocation, StandardSegment};
use object::{RelocationEncoding, RelocationKind, SectionKind};
use object::{
Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationKind, SectionKind,
};
use std::collections::HashMap;
use target_lexicon::BinaryFormat;
use wasmtime_environ::isa::TargetIsa;

pub use crate::read_debuginfo::{read_debuginfo, DebugInfoData, WasmFileInfo};
Expand Down Expand Up @@ -91,7 +92,14 @@ pub fn write_debugsections_image(
code_region: (*const u8, usize),
funcs: &[*const u8],
) -> Result<Vec<u8>, Error> {
let mut obj = Object::new(BinaryFormat::Elf, isa.triple().architecture);
if isa.triple().architecture != target_lexicon::Architecture::X86_64 {
bail!(
"Unsupported architecture for DWARF image: {}",
isa.triple().architecture
);
}

let mut obj = Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);

assert!(!code_region.0.is_null() && code_region.1 > 0);
assert_gt!(funcs.len(), 0);
Expand Down
2 changes: 1 addition & 1 deletion crates/obj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"
[dependencies]
anyhow = "1.0"
wasmtime-environ = { path = "../environ", version = "0.18.0" }
object = { version = "0.18", default-features = false, features = ["write"] }
object = { version = "0.19", default-features = false, features = ["write"] }
more-asserts = "0.2.1"

[badges]
Expand Down
2 changes: 1 addition & 1 deletion crates/profiling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ wasmtime-runtime = { path = "../runtime", version = "0.18.0" }
ittapi-rs = { version = "0.1.5", optional = true }

[dependencies.object]
version = "0.18.0"
version = "0.19.0"
optional = true
default-features = false
features = ['read_core', 'elf', 'std']
Expand Down
39 changes: 38 additions & 1 deletion src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ use wasmtime_environ::{
use wasmtime_jit::native;
use wasmtime_obj::emit_module;

fn to_obj_format(
triple: &Triple,
) -> Result<(
object::BinaryFormat,
object::Architecture,
object::Endianness,
)> {
let binary_format = match triple.binary_format {
target_lexicon::BinaryFormat::Elf => object::BinaryFormat::Elf,
target_lexicon::BinaryFormat::Coff => object::BinaryFormat::Coff,
target_lexicon::BinaryFormat::Macho => object::BinaryFormat::MachO,
target_lexicon::BinaryFormat::Wasm => {
bail!("binary format wasm is unsupported");
}
target_lexicon::BinaryFormat::Unknown => {
bail!("binary format is unknown");
}
};
let architecture = match triple.architecture {
target_lexicon::Architecture::I386
| target_lexicon::Architecture::I586
| target_lexicon::Architecture::I686 => object::Architecture::I386,
target_lexicon::Architecture::X86_64 => object::Architecture::X86_64,
target_lexicon::Architecture::Arm(_) => object::Architecture::Arm,
target_lexicon::Architecture::Aarch64(_) => object::Architecture::Aarch64,
architecture => {
bail!("target architecture {:?} is unsupported", architecture,);
}
};
let endian = match triple.endianness().unwrap() {
target_lexicon::Endianness::Little => object::Endianness::Little,
target_lexicon::Endianness::Big => object::Endianness::Big,
};
Ok((binary_format, architecture, endian))
}

/// Creates object file from binary wasm data.
pub fn compile_to_obj(
wasm: &[u8],
Expand Down Expand Up @@ -50,7 +86,8 @@ pub fn compile_to_obj(

let isa = isa_builder.finish(settings::Flags::new(flag_builder));

let mut obj = Object::new(isa.triple().binary_format, isa.triple().architecture);
let (obj_format, obj_arch, obj_endian) = to_obj_format(isa.triple())?;
let mut obj = Object::new(obj_format, obj_arch, obj_endian);

// TODO: Expose the tunables as command-line flags.
let mut tunables = Tunables::default();
Expand Down