From 3a3556a74c46b9b0607c44c7dd338eee9626ab1c Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 31 Dec 2024 16:17:34 +0100 Subject: [PATCH 1/3] compiler: bump object to 0.36.7 Update all callsites using `write::Relocation` to the new API. --- Cargo.lock | 42 ++++-- Cargo.toml | 2 +- crates/compiler/gen_dev/src/object_builder.rs | 129 +++++++++--------- 3 files changed, 100 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d44a1b6355..b432181ebda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -216,7 +216,7 @@ dependencies = [ "cfg-if", "libc", "miniz_oxide", - "object", + "object 0.32.2", "rustc-demangle", ] @@ -724,7 +724,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ "cfg-if", - "hashbrown", + "hashbrown 0.14.3", "lock_api", "once_cell", "parking_lot_core", @@ -932,6 +932,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" + [[package]] name = "form_urlencoded" version = "1.2.0" @@ -1117,6 +1123,15 @@ dependencies = [ "allocator-api2", ] +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +dependencies = [ + "foldhash", +] + [[package]] name = "heck" version = "0.4.1" @@ -1302,7 +1317,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.14.3", ] [[package]] @@ -1719,9 +1734,18 @@ name = "object" version = "0.32.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6a622008b6e321afc04970976f62ee297fdbaa6f95318ca343e3eebb9648441" +dependencies = [ + "memchr", +] + +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" dependencies = [ "crc32fast", - "hashbrown", + "hashbrown 0.15.2", "indexmap", "memchr", ] @@ -2466,7 +2490,7 @@ dependencies = [ "bitvec", "bumpalo", "fnv", - "hashbrown", + "hashbrown 0.14.3", "im", "im-rc", "smallvec", @@ -2596,7 +2620,7 @@ version = "0.0.1" dependencies = [ "bumpalo", "capstone", - "object", + "object 0.36.7", "packed_struct", "roc_builtins", "roc_can", @@ -2756,7 +2780,7 @@ dependencies = [ "libc", "mach_object", "memmap2 0.5.10", - "object", + "object 0.36.7", "roc_collections", "roc_error_macros", "roc_load", @@ -2874,7 +2898,7 @@ dependencies = [ "arrayvec 0.7.4", "bitvec", "bumpalo", - "hashbrown", + "hashbrown 0.14.3", "indoc", "parking_lot", "roc_builtins", @@ -3176,7 +3200,7 @@ dependencies = [ "arrayvec 0.7.4", "bitvec", "bumpalo", - "hashbrown", + "hashbrown 0.14.3", "indoc", "parking_lot", "pretty_assertions", diff --git a/Cargo.toml b/Cargo.toml index 1fea315c526..103d0f744c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ repository = "https://github.com/roc-lang/roc" version = "0.0.1" [workspace.dependencies] -object = { version = "0.32.2", default-features = false, features = [ +object = { version = "0.36.7", default-features = false, features = [ "read", "write", ] } diff --git a/crates/compiler/gen_dev/src/object_builder.rs b/crates/compiler/gen_dev/src/object_builder.rs index 54ca0b2924e..9ce6b3ff70a 100644 --- a/crates/compiler/gen_dev/src/object_builder.rs +++ b/crates/compiler/gen_dev/src/object_builder.rs @@ -4,8 +4,8 @@ use bumpalo::collections::Vec; use object::write::{self, SectionId, SymbolId}; use object::write::{Object, StandardSection, StandardSegment, Symbol, SymbolSection}; use object::{ - Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationKind, SectionKind, - SymbolFlags, SymbolKind, SymbolScope, + Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationFlags, RelocationKind, + SectionKind, SymbolFlags, SymbolKind, SymbolScope, }; use roc_collections::all::MutMap; use roc_error_macros::internal_error; @@ -245,11 +245,11 @@ fn generate_roc_panic<'a, B: Backend<'a>>(backend: &mut B, output: &mut Object) // 0000000000000700: R_AARCH64_ADR_PREL_PG_HI21 .rodata+0x650 let relocation = write::Relocation { offset: offset + proc_offset, - size: 21, - kind: RelocationKind::Elf(object::elf::R_AARCH64_ADR_PREL_PG_HI21), - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: 0, + flags: write::RelocationFlags::Elf { + r_type: object::elf::R_AARCH64_ADR_PREL_PG_HI21, + }, }; output.add_relocation(text_section, relocation).unwrap(); @@ -258,25 +258,24 @@ fn generate_roc_panic<'a, B: Backend<'a>>(backend: &mut B, output: &mut Object) // 0000000000000704: R_AARCH64_ADD_ABS_LO12_NC .rodata+0x650 write::Relocation { offset: offset + proc_offset + 4, - size: 12, - kind: RelocationKind::Elf(object::elf::R_AARCH64_ADD_ABS_LO12_NC), - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: 0, + flags: write::RelocationFlags::Elf { + r_type: object::elf::R_AARCH64_ADD_ABS_LO12_NC, + }, } } else if cfg!(all(target_arch = "aarch64", target_os = "macos")) { // 4dc: 90000001 adrp x1, 0x0 // 00000000000004dc: ARM64_RELOC_PAGE21 ___unnamed_6 let relocation = write::Relocation { offset: offset + proc_offset, - size: 32, - kind: RelocationKind::MachO { - value: object::macho::ARM64_RELOC_PAGE21, - relative: true, - }, - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: 0, + flags: write::RelocationFlags::MachO { + r_type: object::macho::ARM64_RELOC_PAGE21, + r_pcrel: true, + r_length: 2, + }, }; output.add_relocation(text_section, relocation).unwrap(); @@ -285,23 +284,24 @@ fn generate_roc_panic<'a, B: Backend<'a>>(backend: &mut B, output: &mut Object) // 00000000000004e0: ARM64_RELOC_PAGEOFF12 ___unnamed_6 write::Relocation { offset: offset + proc_offset + 4, - size: 32, - kind: RelocationKind::MachO { - value: object::macho::ARM64_RELOC_PAGEOFF12, - relative: false, - }, - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: 0, + flags: write::RelocationFlags::MachO { + r_type: object::macho::ARM64_RELOC_PAGEOFF12, + r_pcrel: false, + r_length: 2, + }, } } else { write::Relocation { offset: offset + proc_offset, - size: 32, - kind: RelocationKind::GotRelative, - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: -4, + flags: write::RelocationFlags::Generic { + kind: RelocationKind::GotRelative, + encoding: RelocationEncoding::Generic, + size: 32, + }, } } } else { @@ -383,45 +383,46 @@ fn generate_wrapper<'a, B: Backend<'a>>( } fn create_relocation(target: Target, symbol: SymbolId, offset: u64) -> write::Relocation { - let (encoding, size, addend, kind) = match target.architecture() { + let (flags, addend) = match target.architecture() { roc_target::Architecture::Aarch32 => todo!(), roc_target::Architecture::Aarch64 => { if cfg!(target_os = "macos") { ( - RelocationEncoding::Generic, - 26, - 0, - RelocationKind::MachO { - value: 2, - relative: true, + RelocationFlags::MachO { + r_type: object::macho::ARM64_RELOC_BRANCH26, + r_pcrel: true, + r_length: 2, }, + 0, ) } else { ( - RelocationEncoding::AArch64Call, - 26, + RelocationFlags::Generic { + kind: RelocationKind::PltRelative, + encoding: RelocationEncoding::AArch64Call, + size: 26, + }, 0, - RelocationKind::PltRelative, ) } } roc_target::Architecture::Wasm32 => todo!(), roc_target::Architecture::X86_32 => todo!(), roc_target::Architecture::X86_64 => ( - RelocationEncoding::X86Branch, - 32, + RelocationFlags::Generic { + kind: RelocationKind::PltRelative, + encoding: RelocationEncoding::X86Branch, + size: 32, + }, -4, - RelocationKind::PltRelative, ), }; write::Relocation { offset, - size, - kind, - encoding, symbol, addend, + flags, } } @@ -957,11 +958,13 @@ fn build_proc<'a, B: Backend<'a>>( output.add_symbol_data(data_id, data_section, data, 4); write::Relocation { offset: offset + proc_offset, - size: 32, - kind: RelocationKind::Relative, - encoding: RelocationEncoding::Generic, symbol: data_id, addend: -4, + flags: RelocationFlags::Generic { + kind: RelocationKind::Relative, + encoding: RelocationEncoding::Generic, + size: 32, + }, } } Relocation::LinkedData { offset, name } => { @@ -973,11 +976,11 @@ fn build_proc<'a, B: Backend<'a>>( // 0000000000000700: R_AARCH64_ADR_PREL_PG_HI21 .rodata+0x650 let r = write::Relocation { offset: proc_offset + offset, - size: 21, - kind: RelocationKind::Elf(object::elf::R_AARCH64_ADR_PREL_PG_HI21), - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: -4, + flags: RelocationFlags::Elf { + r_type: object::elf::R_AARCH64_ADR_PREL_PG_HI21, + }, }; relocations.push((section_id, r)); @@ -986,25 +989,24 @@ fn build_proc<'a, B: Backend<'a>>( // 0000000000000704: R_AARCH64_ADD_ABS_LO12_NC .rodata+0x650 write::Relocation { offset: proc_offset + offset + 4, - size: 12, - kind: RelocationKind::Elf(object::elf::R_AARCH64_ADD_ABS_LO12_NC), - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: 0, + flags: RelocationFlags::Elf { + r_type: object::elf::R_AARCH64_ADD_ABS_LO12_NC, + }, } } else if cfg!(all(target_arch = "aarch64", target_os = "macos")) { // 4ed0: 90000000 adrp x0, 0x4000 <_std.unicode.utf8Decode4+0x16c> // 0000000000004ed0: ARM64_RELOC_PAGE21 ___unnamed_11 let r = write::Relocation { offset: proc_offset + offset, - size: 21, - kind: RelocationKind::MachO { - value: object::macho::ARM64_RELOC_PAGE21, - relative: true, - }, - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: 0, + flags: RelocationFlags::MachO { + r_type: object::macho::ARM64_RELOC_PAGE21, + r_pcrel: true, + r_length: 2, + }, }; relocations.push((section_id, r)); @@ -1013,23 +1015,24 @@ fn build_proc<'a, B: Backend<'a>>( // 0000000000004ed4: ARM64_RELOC_PAGEOFF12 ___unnamed_11 write::Relocation { offset: proc_offset + offset + 4, - size: 12, - kind: RelocationKind::MachO { - value: object::macho::ARM64_RELOC_PAGEOFF12, - relative: false, - }, - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: 0, + flags: RelocationFlags::MachO { + r_type: object::macho::ARM64_RELOC_PAGEOFF12, + r_pcrel: false, + r_length: 2, + }, } } else { write::Relocation { offset: offset + proc_offset, - size: 32, - kind: RelocationKind::GotRelative, - encoding: RelocationEncoding::Generic, symbol: sym_id, addend: -4, + flags: RelocationFlags::Generic { + kind: RelocationKind::GotRelative, + encoding: RelocationEncoding::Generic, + size: 32, + }, } } } else { From e4fc3184a0acdbedec6341414f13124c740b8c84 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 31 Dec 2024 16:43:43 +0100 Subject: [PATCH 2/3] linker/macho: update relocation handling to new object API --- crates/linker/src/macho.rs | 87 +++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/crates/linker/src/macho.rs b/crates/linker/src/macho.rs index 7402156587a..0eb7f734c13 100644 --- a/crates/linker/src/macho.rs +++ b/crates/linker/src/macho.rs @@ -4,8 +4,8 @@ use memmap2::MmapMut; use object::macho; use object::{ CompressedFileRange, CompressionFormat, LittleEndian as LE, Object, ObjectSection, - ObjectSymbol, RelocationKind, RelocationTarget, Section, SectionIndex, SectionKind, Symbol, - SymbolIndex, SymbolSection, + ObjectSymbol, RelocationFlags, RelocationKind, RelocationTarget, Section, SectionIndex, + SectionKind, Symbol, SymbolIndex, SymbolSection, }; use roc_collections::all::MutMap; use roc_error_macros::internal_error; @@ -1363,23 +1363,36 @@ fn surgery_macho_help( println!("\t\tTODO synthesise __got entry for {name}") } } - RelocationKind::MachO { value, relative: _ } => match value { - macho::ARM64_RELOC_GOT_LOAD_PAGE21 - | macho::ARM64_RELOC_GOT_LOAD_PAGEOFF12 => { - if verbose { - println!("\t\tTODO synthesise __got entry for {name}") - } - } - macho::ARM64_RELOC_BRANCH26 => { - if verbose { - println!("\t\tTODO synthesise __stub entry for {name}") + RelocationKind::Unknown => { + if let RelocationFlags::MachO { r_type, .. } = rel.1.flags() { + match r_type { + macho::ARM64_RELOC_GOT_LOAD_PAGE21 + | macho::ARM64_RELOC_GOT_LOAD_PAGEOFF12 => { + if verbose { + println!( + "\t\tTODO synthesise __got entry for {name}" + ) + } + } + macho::ARM64_RELOC_BRANCH26 => { + if verbose { + println!( + "\t\tTODO synthesise __stub entry for {name}" + ) + } + } + _ => internal_error!( + "Invalid relocation for libc symbol, {:+x?}: {name}", + rel + ), } + } else { + internal_error!( + "Invalid relocation found for Mach-O: {:?}", + rel + ); } - _ => internal_error!( - "Invalid relocation for libc symbol, {:+x?}: {name}", - rel - ), - }, + } _ => internal_error!( "Invalid relocation for libc symbol, {:+x?}: {name}", rel @@ -1416,25 +1429,31 @@ fn surgery_macho_help( }) .unwrap() } - RelocationKind::MachO { value, relative: _ } => match value { - macho::ARM64_RELOC_SUBTRACTOR => { - if subtractor.is_some() { - internal_error!("Malformed object: SUBTRACTOR must not be followed by SUBTRACTOR"); - } else { - subtractor = Some(index); - } - continue; - } - _ => { - if verbose { - println!( - "\t\tHandle other MachO relocs: {}", - format_reloc_type(value) - ); + RelocationKind::Unknown => { + if let RelocationFlags::MachO { r_type, .. } = rel.1.flags() { + match r_type { + macho::ARM64_RELOC_SUBTRACTOR => { + if subtractor.is_some() { + internal_error!("Malformed object: SUBTRACTOR must not be followed by SUBTRACTOR"); + } else { + subtractor = Some(index); + } + continue; + } + _ => { + if verbose { + println!( + "\t\tHandle other MachO relocs: {}", + format_reloc_type(r_type) + ); + } + 0 + } } - 0 + } else { + internal_error!("Invalid relocation found for Mach-O: {:?}", rel); } - }, + } x => { internal_error!("Relocation Kind not yet support: {:?}", x); } From 19dc28aa346a6c85939e26b9e1c2a5c8fbfbaf74 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Tue, 31 Dec 2024 16:57:10 +0100 Subject: [PATCH 3/3] linker/elf: update relocation handling to new object API --- crates/linker/src/elf.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/crates/linker/src/elf.rs b/crates/linker/src/elf.rs index 0949768996a..35c9b964693 100644 --- a/crates/linker/src/elf.rs +++ b/crates/linker/src/elf.rs @@ -4,8 +4,8 @@ use memmap2::MmapMut; use object::{elf, endian}; use object::{ CompressedFileRange, CompressionFormat, LittleEndian as LE, Object, ObjectSection, - ObjectSymbol, RelocationKind, RelocationTarget, Section, SectionIndex, SectionKind, Symbol, - SymbolIndex, SymbolSection, + ObjectSymbol, RelocationFlags, RelocationKind, RelocationTarget, Section, SectionIndex, + SectionKind, Symbol, SymbolIndex, SymbolSection, }; use roc_collections::all::MutMap; use roc_error_macros::{internal_error, user_error}; @@ -407,8 +407,12 @@ pub(crate) fn preprocess_elf_le( } }) .filter_map(|(_, reloc)| { - if let RelocationKind::Elf(7) = reloc.kind() { - Some(reloc) + if let RelocationFlags::Elf { r_type} = reloc.flags() { + if r_type == elf::R_X86_64_JUMP_SLOT { + Some(reloc) + } else { + None + } } else { None } @@ -1069,10 +1073,12 @@ fn scan_elf_dynamic_deps( } }) .filter_map(|(_, reloc)| { - if let RelocationKind::Elf(elf::R_X86_64_GLOB_DAT) = reloc.kind() { - for symbol in app_syms.iter() { - if reloc.target() == RelocationTarget::Symbol(symbol.index()) { - return Some((symbol.name().unwrap().to_string(), symbol.index().0)); + if let RelocationFlags::Elf { r_type } = reloc.flags() { + if r_type == elf::R_X86_64_GLOB_DAT { + for symbol in app_syms.iter() { + if reloc.target() == RelocationTarget::Symbol(symbol.index()) { + return Some((symbol.name().unwrap().to_string(), symbol.index().0)); + } } } } @@ -1088,10 +1094,12 @@ fn scan_elf_dynamic_deps( } }) .filter_map(|(_, reloc)| { - if let RelocationKind::Elf(elf::R_X86_64_JUMP_SLOT) = reloc.kind() { - for symbol in app_syms.iter() { - if reloc.target() == RelocationTarget::Symbol(symbol.index()) { - return Some(symbol.index().0); + if let RelocationFlags::Elf { r_type } = reloc.flags() { + if r_type == elf::R_X86_64_JUMP_SLOT { + for symbol in app_syms.iter() { + if reloc.target() == RelocationTarget::Symbol(symbol.index()) { + return Some(symbol.index().0); + } } } }