diff --git a/Cargo.lock b/Cargo.lock index 4093f046f66..2c41ab7aea6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,11 +8,11 @@ checksum = "955f37ac58af2416bac687c8ab66a4ccba282229bd7422a28d2281a5e66a6116" [[package]] name = "addr2line" -version = "0.14.1" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7" +checksum = "03345e98af8f3d786b6d9f656ccfa6ac316d954e92bc4841f0bba20789d5fb5a" dependencies = [ - "gimli", + "gimli 0.24.0", ] [[package]] @@ -65,9 +65,9 @@ checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" [[package]] name = "assert_cmd" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2475b58cd94eb4f70159f4fd8844ba3b807532fe3131b3373fae060bbe30396" +checksum = "8f57fec1ac7e4de72dcc69811795f1a7172ed06012f80a5d1ee651b62484f588" dependencies = [ "bstr", "doc-comment", @@ -96,9 +96,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "backtrace" -version = "0.3.58" +version = "0.3.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88fb5a785d6b44fd9d6700935608639af1b8356de1e55d5f7c2740f4faa15d82" +checksum = "4717cfcbfaa661a0fd48f8453951837ae7e8f81e481fbb136e3202d72805a744" dependencies = [ "addr2line", "cc", @@ -393,7 +393,7 @@ dependencies = [ "cranelift-codegen-meta", "cranelift-codegen-shared", "cranelift-entity", - "gimli", + "gimli 0.23.0", "hashbrown", "log", "regalloc", @@ -666,9 +666,9 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" [[package]] name = "dynasm" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d7d1242462849390bb2ad38aeed769499f1afc7383affa2ab0c1baa894c0200" +checksum = "cdc2d9a5e44da60059bd38db2d05cbb478619541b8c79890547861ec1e3194f0" dependencies = [ "bitflags", "byteorder", @@ -681,9 +681,9 @@ dependencies = [ [[package]] name = "dynasmrt" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1dd4d1d5ca12258cef339a57a7643e8b233a42dea9bb849630ddd9dd7726aa9" +checksum = "42276e3f205fe63887cca255aa9a65a63fb72764c30b9a6252a7c7e46994f689" dependencies = [ "byteorder", "dynasm", @@ -766,9 +766,9 @@ dependencies = [ [[package]] name = "field-offset" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf539fba70056b50f40a22e0da30639518a12ee18c35807858a63b158cb6dde7" +checksum = "1e1c54951450cbd39f3dbcf1005ac413b49487dabf18a720ad2383eccfeffb92" dependencies = [ "memoffset", "rustc_version 0.3.3", @@ -875,6 +875,12 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "gimli" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" + [[package]] name = "glob" version = "0.3.0" @@ -1054,9 +1060,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" [[package]] name = "js-sys" -version = "0.3.50" +version = "0.3.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d99f9e3e84b8f67f846ef5b4cbbc3b1c29f6c759fcbce6f01aa0e73d932a24c" +checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" dependencies = [ "wasm-bindgen", ] @@ -1087,9 +1093,9 @@ checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" [[package]] name = "libffi" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bafef83ee22d51c27348aaf6b2da007a32b9f5004809d09271432e5ea2a795dd" +checksum = "f4fcb8a029c57c99c3465a2ae1a41b9cbbf7cdb7b52e482e1e3c7301686372ae" dependencies = [ "abort_on_panic", "libc", @@ -1098,9 +1104,9 @@ dependencies = [ [[package]] name = "libffi-sys" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b6d65142f1c3b06ca3f4216da4d32b3124d14d932cef8dfd8792037acd2160b" +checksum = "7e91e835dcfc5532b8b680fc903d6a3ae1db5dc6a40175d75e25c19c2353994d" dependencies = [ "cc", "make-cmd", @@ -1316,13 +1322,12 @@ dependencies = [ [[package]] name = "object" -version = "0.23.0" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" +checksum = "1a5b3dd1c072ee7963717671d1ca129f1048fda25edea6b752bfc71ac8854170" dependencies = [ "crc32fast", "indexmap", - "wasmparser 0.57.0", ] [[package]] @@ -1617,9 +1622,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85dd92e586f7355c633911e11f77f3d12f04b1b1bd76a198bd34ae3af8341ef2" +checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" dependencies = [ "bitflags", ] @@ -1653,9 +1658,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.3" +version = "1.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce5f1ceb7f74abbce32601642fcf8e8508a8a8991e0621c7d750295b9095702b" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" dependencies = [ "aho-corasick", "memchr", @@ -1700,9 +1705,9 @@ dependencies = [ [[package]] name = "rkyv" -version = "0.6.2" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20ddfdfd8384df988eca0ae1763d42eb48c9ad027ec7c7145296aafffa864842" +checksum = "59c6286f153ea0ca2abf231251fc6758cd5aef042d9e189b3a182abf2399c207" dependencies = [ "memoffset", "ptr_meta", @@ -1712,9 +1717,9 @@ dependencies = [ [[package]] name = "rkyv_derive" -version = "0.6.0" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c848da2aad0c8395f7986a717b8f28a8fc05dd94463d949673803f4955d9f73a" +checksum = "9e3fccbf52ee0b76a29417794226e225798dd6bd32e40debd9e284cecc20dd76" dependencies = [ "proc-macro2", "quote", @@ -1765,9 +1770,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb5d2a036dc6d2d8fd16fde3498b04306e29bd193bf306a57427019b823d5acd" +checksum = "61b3909d758bb75c79f23d4736fac9433868679d3ad2ea7a61e3c25cfda9a088" [[package]] name = "ryu" @@ -1859,9 +1864,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.125" +version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" +checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" dependencies = [ "serde_derive", ] @@ -1887,9 +1892,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.125" +version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" +checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" dependencies = [ "proc-macro2", "quote", @@ -1945,9 +1950,9 @@ checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" [[package]] name = "syn" -version = "1.0.71" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad184cc9470f9117b2ac6817bfe297307418819ba40552f9b3846f05c33d5373" +checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" dependencies = [ "proc-macro2", "quote", @@ -2249,9 +2254,9 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "wasm-bindgen" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83240549659d187488f91f33c0f8547cbfef0b2088bc470c116d1d260ef623d9" +checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" dependencies = [ "cfg-if 1.0.0", "wasm-bindgen-macro", @@ -2259,9 +2264,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae70622411ca953215ca6d06d3ebeb1e915f0f6613e3b495122878d7ebec7dae" +checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" dependencies = [ "bumpalo", "lazy_static", @@ -2274,9 +2279,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e734d91443f177bfdb41969de821e15c516931c3c3db3d318fa1b68975d0f6f" +checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2284,9 +2289,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53739ff08c8a68b0fdbcd54c372b8ab800b1449ab3c9d706503bc7dd1621b2c" +checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" dependencies = [ "proc-macro2", "quote", @@ -2297,9 +2302,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.73" +version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9a543ae66aa233d14bb765ed9af4a33e81b8b58d1584cf1b47ff8cd0b9e4489" +checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" [[package]] name = "wasmer" @@ -2421,7 +2426,7 @@ dependencies = [ "thiserror", "wasmer-types", "wasmer-vm", - "wasmparser 0.78.0", + "wasmparser", ] [[package]] @@ -2431,7 +2436,7 @@ dependencies = [ "cranelift-codegen", "cranelift-entity", "cranelift-frontend", - "gimli", + "gimli 0.23.0", "hashbrown", "lazy_static", "loupe", @@ -2738,15 +2743,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.57.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6" - -[[package]] -name = "wasmparser" -version = "0.78.0" +version = "0.78.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c7a8b20306d43c09c2c34e0ef68bf2959a11b01a5cae35e4c5dc1e7145547b6" +checksum = "2f9255225d0124a59dd44def5cf98b62d70a27aed921f2c51e28035f08a73b0c" [[package]] name = "wast" @@ -2838,9 +2837,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.50" +version = "0.3.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a905d57e488fec8861446d3393670fb50d27a262344013181c2cdf9fff5481be" +checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Makefile b/Makefile index 4dff184da34..4875fa77f2c 100644 --- a/Makefile +++ b/Makefile @@ -203,6 +203,11 @@ ifeq ($(ENABLE_CRANELIFT), 1) ifneq ($(LIBC), musl) compilers_engines += cranelift-native endif + else ifeq ($(IS_AARCH64), 1) + # The object crate doesn't support yet Darwin + Aarch64 relocations + ifneq ($(IS_DARWIN), 1) + compilers_engines += cranelift-native + endif endif endif endif diff --git a/deny.toml b/deny.toml index c5c5a462219..ad7ad2c82e1 100644 --- a/deny.toml +++ b/deny.toml @@ -174,9 +174,8 @@ skip = [ { name = "semver", version = "=0.9.0" }, { name = "semver-parser", version = "=0.7.0" }, { name = "rustc_version", version = "=0.2.3" }, + { name = "gimli", version = "=0.24.0" }, { name = "itertools", version = "0.9.0" }, - { name = "wasmparser", version = "0.57.0" }, - { name = "wast", version = "=24.0.0" }, ] # Similarly to `skip` allows you to skip certain crates during duplicate # detection. Unlike skip, it also includes the entire tree of transitive diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index fcb94952ae4..d89c87b935e 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -58,7 +58,7 @@ dependencies = [ "cfg-if 1.0.0", "libc", "miniz_oxide", - "object", + "object 0.23.0", "rustc-demangle", ] @@ -571,10 +571,15 @@ name = "object" version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" + +[[package]] +name = "object" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a5b3dd1c072ee7963717671d1ca129f1048fda25edea6b752bfc71ac8854170" dependencies = [ "crc32fast", "indexmap", - "wasmparser 0.57.0", ] [[package]] @@ -1137,7 +1142,7 @@ dependencies = [ "thiserror", "wasmer-types", "wasmer-vm", - "wasmparser 0.78.0", + "wasmparser", ] [[package]] @@ -1169,7 +1174,7 @@ dependencies = [ "lazy_static", "libc", "loupe", - "object", + "object 0.24.0", "rayon", "regex", "rustc_version", @@ -1277,7 +1282,7 @@ dependencies = [ name = "wasmer-object" version = "1.0.2" dependencies = [ - "object", + "object 0.24.0", "thiserror", "wasmer-compiler", "wasmer-types", @@ -1314,12 +1319,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "wasmparser" -version = "0.57.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6" - [[package]] name = "wasmparser" version = "0.78.0" @@ -1333,7 +1332,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ccec894c70710c2e4669320a532cb2b9cfb97adb0429745642f8ce76916ed85" dependencies = [ "anyhow", - "wasmparser 0.78.0", + "wasmparser", ] [[package]] diff --git a/lib/compiler-llvm/Cargo.toml b/lib/compiler-llvm/Cargo.toml index e343a7522a7..320c73daeb7 100644 --- a/lib/compiler-llvm/Cargo.toml +++ b/lib/compiler-llvm/Cargo.toml @@ -17,7 +17,7 @@ wasmer-vm = { path = "../vm", version = "1.0.2" } wasmer-types = { path = "../types", version = "1.0.2" } target-lexicon = { version = "0.12", default-features = false } smallvec = "1.6" -object = { version = "0.23", default-features = false, features = ["read"] } +object = { version = "0.24", default-features = false, features = ["read"] } libc = { version = "^0.2", default-features = false } byteorder = "1" itertools = "0.10" diff --git a/lib/compiler/src/relocation.rs b/lib/compiler/src/relocation.rs index ab71705104d..c591809df3b 100644 --- a/lib/compiler/src/relocation.rs +++ b/lib/compiler/src/relocation.rs @@ -46,18 +46,14 @@ pub enum RelocationKind { X86CallPLTRel4, /// x86 GOT PC-relative 4-byte X86GOTPCRel4, - // /// Arm32 call target - // Arm32Call, - - // /// Arm64 call target - // Arm64Call, - + /// Arm32 call target + Arm32Call, + /// Arm64 call target + Arm64Call, // /// RISC-V call target // RiscvCall, - - // /// Elf x86_64 32 bit signed PC relative offset to two GOT entries for GD symbol. - // ElfX86_64TlsGd, - + /// Elf x86_64 32 bit signed PC relative offset to two GOT entries for GD symbol. + ElfX86_64TlsGd, // /// Mach-O x86_64 32 bit signed PC relative offset to a `__thread_vars` entry. // MachOX86_64Tlv, } @@ -75,9 +71,8 @@ impl fmt::Display for RelocationKind { Self::X86CallPCRel4 => write!(f, "CallPCRel4"), Self::X86CallPLTRel4 => write!(f, "CallPLTRel4"), Self::X86GOTPCRel4 => write!(f, "GOTPCRel4"), - // Self::Arm32Call | Self::Arm64Call | Self::RiscvCall => write!(f, "Call"), - - // Self::ElfX86_64TlsGd => write!(f, "ElfX86_64TlsGd"), + Self::Arm32Call | Self::Arm64Call => write!(f, "Call"), + Self::ElfX86_64TlsGd => write!(f, "ElfX86_64TlsGd"), // Self::MachOX86_64Tlv => write!(f, "MachOX86_64Tlv"), } } diff --git a/lib/object/Cargo.toml b/lib/object/Cargo.toml index bc7839ee4be..b90d8194417 100644 --- a/lib/object/Cargo.toml +++ b/lib/object/Cargo.toml @@ -16,5 +16,5 @@ wasmer-compiler = { path = "../compiler", version = "1.0.2", default-features = "std", "translator" ] } -object = { version = "0.23", default-features = false, features = ["write"] } +object = { version = "0.24", default-features = false, features = ["write"] } thiserror = "1.0" diff --git a/lib/object/src/module.rs b/lib/object/src/module.rs index 8aba8b72b21..8fef1c00b66 100644 --- a/lib/object/src/module.rs +++ b/lib/object/src/module.rs @@ -1,10 +1,18 @@ use crate::error::ObjectError; -use object::write::{Object, Relocation, StandardSection, Symbol as ObjSymbol, SymbolSection}; -use object::{RelocationEncoding, RelocationKind, SymbolFlags, SymbolKind, SymbolScope}; +use object::write::{ + Object, Relocation, StandardSection, StandardSegment, Symbol as ObjSymbol, SymbolSection, +}; +use object::{ + elf, RelocationEncoding, RelocationKind, SectionKind, SymbolFlags, SymbolKind, SymbolScope, +}; use wasmer_compiler::{ - Architecture, BinaryFormat, Compilation, CustomSectionProtection, Endianness, RelocationTarget, - Symbol, SymbolRegistry, Triple, + Architecture, BinaryFormat, Compilation, CustomSectionProtection, Endianness, + RelocationKind as Reloc, RelocationTarget, SectionIndex, Symbol, SymbolRegistry, Triple, }; +use wasmer_types::entity::PrimaryMap; +use wasmer_types::LocalFunctionIndex; + +const DWARF_SECTION_NAME: &[u8] = b".eh_frame"; /// Create an object for a given target `Triple`. /// @@ -128,126 +136,179 @@ pub fn emit_compilation( let function_call_trampolines = compilation.get_function_call_trampolines(); let dynamic_function_trampolines = compilation.get_dynamic_function_trampolines(); + let debug_index = compilation.get_debug().map(|d| d.eh_frame); + + let align = match triple.architecture { + Architecture::X86_64 => 1, + // In Arm64 is recommended a 4-byte alignment + Architecture::Aarch64(_) => 4, + _ => 1, + }; + // Add sections - for (section_index, custom_section) in custom_sections.iter() { - // TODO: We need to rename the sections corresponding to the DWARF information - // to the proper names (like `.eh_frame`) - let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index)); - let (section_kind, standard_section) = match custom_section.protection { - CustomSectionProtection::ReadExecute => (SymbolKind::Text, StandardSection::Text), - // TODO: Fix this to be StandardSection::Data - CustomSectionProtection::Read => (SymbolKind::Data, StandardSection::Text), - }; - let symbol_id = obj.add_symbol(ObjSymbol { - name: section_name.into_bytes(), - value: 0, - size: 0, - kind: section_kind, - scope: SymbolScope::Dynamic, - weak: false, - section: SymbolSection::Undefined, - flags: SymbolFlags::None, - }); - let section_id = obj.section_id(standard_section); - obj.add_symbol_data(symbol_id, section_id, custom_section.bytes.as_slice(), 1); - } + let custom_section_ids = custom_sections + .into_iter() + .map(|(section_index, custom_section)| { + if debug_index.map(|d| d == section_index).unwrap_or(false) { + // If this is the debug section + let segment = obj.segment_name(StandardSegment::Debug).to_vec(); + let section_id = + obj.add_section(segment, DWARF_SECTION_NAME.to_vec(), SectionKind::Debug); + obj.append_section_data(section_id, custom_section.bytes.as_slice(), align); + let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index)); + let symbol_id = obj.add_symbol(ObjSymbol { + name: section_name.into_bytes(), + value: 0, + size: custom_section.bytes.len() as _, + kind: SymbolKind::Data, + scope: SymbolScope::Compilation, + weak: false, + section: SymbolSection::Section(section_id), + flags: SymbolFlags::None, + }); + (section_id, symbol_id) + } else { + let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index)); + let (section_kind, standard_section) = match custom_section.protection { + CustomSectionProtection::ReadExecute => { + (SymbolKind::Text, StandardSection::Text) + } + CustomSectionProtection::Read => (SymbolKind::Data, StandardSection::Data), + }; + let section_id = obj.section_id(standard_section); + let symbol_id = obj.add_symbol(ObjSymbol { + name: section_name.into_bytes(), + value: 0, + size: custom_section.bytes.len() as _, + kind: section_kind, + scope: SymbolScope::Dynamic, + weak: false, + section: SymbolSection::Section(section_id), + flags: SymbolFlags::None, + }); + obj.add_symbol_data(symbol_id, section_id, custom_section.bytes.as_slice(), align); + (section_id, symbol_id) + } + }) + .collect::>(); // Add functions - for (function_local_index, function) in function_bodies.into_iter() { - let function_name = - symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index)); - let symbol_id = obj.add_symbol(ObjSymbol { - name: function_name.into_bytes(), - value: 0, - size: 0, - kind: SymbolKind::Text, - scope: SymbolScope::Dynamic, - weak: false, - section: SymbolSection::Undefined, - flags: SymbolFlags::None, - }); - - let section_id = obj.section_id(StandardSection::Text); - obj.add_symbol_data(symbol_id, section_id, &function.body, 1); - } + let function_symbol_ids = function_bodies + .into_iter() + .map(|(function_local_index, function)| { + let function_name = + symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index)); + let section_id = obj.section_id(StandardSection::Text); + let symbol_id = obj.add_symbol(ObjSymbol { + name: function_name.into_bytes(), + value: 0, + size: function.body.len() as _, + kind: SymbolKind::Text, + scope: SymbolScope::Dynamic, + weak: false, + section: SymbolSection::Section(section_id), + flags: SymbolFlags::None, + }); + obj.add_symbol_data(symbol_id, section_id, &function.body, align); + (section_id, symbol_id) + }) + .collect::>(); // Add function call trampolines for (signature_index, function) in function_call_trampolines.into_iter() { let function_name = symbol_registry.symbol_to_name(Symbol::FunctionCallTrampoline(signature_index)); + let section_id = obj.section_id(StandardSection::Text); let symbol_id = obj.add_symbol(ObjSymbol { name: function_name.into_bytes(), value: 0, - size: 0, + size: function.body.len() as _, kind: SymbolKind::Text, scope: SymbolScope::Dynamic, weak: false, - section: SymbolSection::Undefined, + section: SymbolSection::Section(section_id), flags: SymbolFlags::None, }); - let section_id = obj.section_id(StandardSection::Text); - obj.add_symbol_data(symbol_id, section_id, &function.body, 1); + obj.add_symbol_data(symbol_id, section_id, &function.body, align); } // Add dynamic function trampolines for (func_index, function) in dynamic_function_trampolines.into_iter() { let function_name = symbol_registry.symbol_to_name(Symbol::DynamicFunctionTrampoline(func_index)); + let section_id = obj.section_id(StandardSection::Text); let symbol_id = obj.add_symbol(ObjSymbol { name: function_name.into_bytes(), value: 0, - size: 0, + size: function.body.len() as _, kind: SymbolKind::Text, scope: SymbolScope::Dynamic, weak: false, - section: SymbolSection::Undefined, + section: SymbolSection::Section(section_id), flags: SymbolFlags::None, }); - let section_id = obj.section_id(StandardSection::Text); - obj.add_symbol_data(symbol_id, section_id, &function.body, 1); + obj.add_symbol_data(symbol_id, section_id, &function.body, align); } - // Add relocations (function and sections) - let (relocation_size, relocation_kind, relocation_encoding) = match triple.architecture { - Architecture::X86_64 => (32, RelocationKind::GotRelative, RelocationEncoding::Generic), - // Object doesn't fully support it yet - // Architecture::Aarch64(_) => ( - // 32, - // RelocationKind::PltRelative, - // RelocationEncoding::Generic, - // ), - architecture => { - return Err(ObjectError::UnsupportedArchitecture( - architecture.to_string(), - )) - } - }; let mut all_relocations = Vec::new(); for (function_local_index, relocations) in function_relocations.into_iter() { - let function_name = - symbol_registry.symbol_to_name(Symbol::LocalFunction(function_local_index)); - let symbol_id = obj.symbol_id(function_name.as_bytes()).unwrap(); - all_relocations.push((symbol_id, relocations)) + let (section_id, symbol_id) = function_symbol_ids.get(function_local_index).unwrap(); + all_relocations.push((*section_id, *symbol_id, relocations)) } for (section_index, relocations) in custom_section_relocations.into_iter() { - let section_name = symbol_registry.symbol_to_name(Symbol::Section(section_index)); - let symbol_id = obj.symbol_id(section_name.as_bytes()).unwrap(); - all_relocations.push((symbol_id, relocations)) + if !debug_index.map(|d| d == section_index).unwrap_or(false) { + // Skip DWARF relocations just yet + let (section_id, symbol_id) = custom_section_ids.get(section_index).unwrap(); + all_relocations.push((*section_id, *symbol_id, relocations)); + } } - for (symbol_id, relocations) in all_relocations.into_iter() { + for (section_id, symbol_id, relocations) in all_relocations.into_iter() { let (_symbol_id, section_offset) = obj.symbol_section_and_offset(symbol_id).unwrap(); - let section_id = obj.section_id(StandardSection::Text); for r in relocations { + let (relocation_kind, relocation_encoding, relocation_size) = match r.kind { + Reloc::Abs4 => (RelocationKind::Absolute, RelocationEncoding::Generic, 32), + Reloc::Abs8 => (RelocationKind::Absolute, RelocationEncoding::Generic, 64), + Reloc::X86PCRel4 => (RelocationKind::Relative, RelocationEncoding::Generic, 32), + Reloc::X86CallPCRel4 => { + (RelocationKind::Relative, RelocationEncoding::X86Branch, 32) + } + Reloc::X86CallPLTRel4 => ( + RelocationKind::PltRelative, + RelocationEncoding::X86Branch, + 32, + ), + Reloc::X86GOTPCRel4 => { + (RelocationKind::GotRelative, RelocationEncoding::Generic, 32) + } + // Reloc::X86PCRelRodata4 => { + // } + Reloc::Arm64Call => ( + RelocationKind::Elf(elf::R_AARCH64_CALL26), + RelocationEncoding::Generic, + 32, + ), + Reloc::ElfX86_64TlsGd => ( + RelocationKind::Elf(elf::R_X86_64_TLSGD), + RelocationEncoding::Generic, + 32, + ), + other => { + return Err(ObjectError::UnsupportedArchitecture(format!( + "{} (relocation: {}", + triple.architecture, other + ))) + } + }; + let relocation_address = section_offset + r.offset as u64; match r.reloc_target { RelocationTarget::LocalFunc(index) => { - let target_name = symbol_registry.symbol_to_name(Symbol::LocalFunction(index)); - let target_symbol = obj.symbol_id(target_name.as_bytes()).unwrap(); + let (_, target_symbol) = function_symbol_ids.get(index).unwrap(); obj.add_relocation( section_id, Relocation { @@ -255,7 +316,7 @@ pub fn emit_compilation( size: relocation_size, kind: relocation_kind, encoding: relocation_encoding, - symbol: target_symbol, + symbol: *target_symbol, addend: r.addend, }, ) @@ -290,9 +351,7 @@ pub fn emit_compilation( .map_err(ObjectError::Write)?; } RelocationTarget::CustomSection(section_index) => { - let target_name = - symbol_registry.symbol_to_name(Symbol::Section(section_index)); - let target_symbol = obj.symbol_id(target_name.as_bytes()).unwrap(); + let (_, target_symbol) = custom_section_ids.get(section_index).unwrap(); obj.add_relocation( section_id, Relocation { @@ -300,7 +359,7 @@ pub fn emit_compilation( size: relocation_size, kind: relocation_kind, encoding: relocation_encoding, - symbol: target_symbol, + symbol: *target_symbol, addend: r.addend, }, )