@@ -26,6 +26,87 @@ pub(super) fn macho_platform(target: &Target) -> u32 {
2626 }
2727}
2828
29+ /// Add relocation and section data needed for a symbol to be considered
30+ /// undefined by ld64.
31+ ///
32+ /// Inherently very architecture-specific (unfortunately).
33+ ///
34+ /// # New architectures
35+ ///
36+ /// The values here can be found by compiling the following program:
37+ ///
38+ /// ```c
39+ /// void foo(void);
40+ ///
41+ /// void use_foo() {
42+ /// foo();
43+ /// }
44+ /// ```
45+ ///
46+ /// With:
47+ ///
48+ /// ```console
49+ /// $ clang -c foo.c -O2 -g0 -fno-exceptions -target $CLANG_TARGET
50+ /// ```
51+ ///
52+ /// And then inspecting with `objdump -d foo.o` and/or:
53+ ///
54+ /// ```rust,ignore
55+ /// //! ```cargo
56+ /// //! [dependencies]
57+ /// //! object = "0.36"
58+ /// //! ```
59+ ///
60+ /// use object::read::macho::{MachHeader, MachOFile};
61+ /// use object::{File, Object, ObjectSection};
62+ ///
63+ /// fn read(file: MachOFile<'_, impl MachHeader>) {
64+ /// for section in file.sections() {
65+ /// dbg!(section.name().unwrap(), section.data().unwrap(), section.align());
66+ /// for reloc in section.relocations() {
67+ /// dbg!(reloc);
68+ /// }
69+ /// }
70+ /// }
71+ ///
72+ /// fn main() {
73+ /// match File::parse(&*std::fs::read("foo.o").unwrap()).unwrap() {
74+ /// File::MachO32(file) => read(file),
75+ /// File::MachO64(file) => read(file),
76+ /// _ => unimplemented!(),
77+ /// }
78+ /// }
79+ /// ```
80+ pub ( super ) fn add_data_and_relocation (
81+ file : & mut object:: write:: Object < ' _ > ,
82+ section : object:: write:: SectionId ,
83+ symbol : object:: write:: SymbolId ,
84+ target : & Target ,
85+ ) -> object:: write:: Result < ( ) > {
86+ let ( data, align, addend, r_type) : ( & [ u8 ] , _ , _ , _ ) = match & * target. arch {
87+ "arm" => ( & [ 0xff , 0xf7 , 0xfe , 0xbf ] , 2 , 0 , object:: macho:: ARM_THUMB_RELOC_BR22 ) ,
88+ "aarch64" => ( & [ 0 , 0 , 0 , 0x14 ] , 4 , 0 , object:: macho:: ARM64_RELOC_BRANCH26 ) ,
89+ "x86_64" => (
90+ & [ 0x55 , 0x48 , 0x89 , 0xe5 , 0x5d , 0xe9 , 0 , 0 , 0 , 0 ] ,
91+ 16 ,
92+ -4 ,
93+ object:: macho:: X86_64_RELOC_BRANCH ,
94+ ) ,
95+ "x86" => ( & [ 0x55 , 0x89 , 0xe5 , 0x5d , 0xe9 , 0xf7 , 0xff , 0xff , 0xff ] , 16 , -4 , 0 ) ,
96+ arch => unimplemented ! ( "unsupported Apple architecture {arch:?}" ) ,
97+ } ;
98+
99+ let offset = file. section_mut ( section) . append_data ( data, align) ;
100+ file. add_relocation ( section, object:: write:: Relocation {
101+ offset,
102+ addend,
103+ symbol,
104+ flags : object:: write:: RelocationFlags :: MachO { r_type, r_pcrel : true , r_length : 2 } ,
105+ } ) ?;
106+
107+ Ok ( ( ) )
108+ }
109+
29110/// Deployment target or SDK version.
30111///
31112/// The size of the numbers in here are limited by Mach-O's `LC_BUILD_VERSION`.
0 commit comments