@@ -26,6 +26,82 @@ 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+ /// ```no_run
55+ /// use object::read::macho::{MachHeader, MachOFile};
56+ /// use object::{File, Object, ObjectSection};
57+ ///
58+ /// fn read(file: MachOFile<'_, impl MachHeader>) {
59+ /// for section in file.sections() {
60+ /// dbg!(section.name().unwrap(), section.data().unwrap(), section.align());
61+ /// for reloc in section.relocations() {
62+ /// dbg!(reloc);
63+ /// }
64+ /// }
65+ /// }
66+ ///
67+ /// fn main() {
68+ /// match File::parse(&*std::fs::read("foo.o").unwrap()).unwrap() {
69+ /// File::MachO32(file) => read(file),
70+ /// File::MachO64(file) => read(file),
71+ /// _ => unimplemented!(),
72+ /// }
73+ /// }
74+ /// ```
75+ pub ( super ) fn add_data_and_relocation (
76+ file : & mut object:: write:: Object < ' _ > ,
77+ section : object:: write:: SectionId ,
78+ symbol : object:: write:: SymbolId ,
79+ target : & Target ,
80+ ) -> object:: write:: Result < ( ) > {
81+ let ( data, align, addend, r_type) : ( & [ u8 ] , _ , _ , _ ) = match & * target. arch {
82+ "arm" => ( & [ 0xff , 0xf7 , 0xfe , 0xbf ] , 2 , 0 , object:: macho:: ARM_THUMB_RELOC_BR22 ) ,
83+ "aarch64" => ( & [ 0 , 0 , 0 , 0x14 ] , 4 , 0 , object:: macho:: ARM64_RELOC_BRANCH26 ) ,
84+ "x86_64" => (
85+ & [ 0x55 , 0x48 , 0x89 , 0xe5 , 0x5d , 0xe9 , 0 , 0 , 0 , 0 ] ,
86+ 16 ,
87+ -4 ,
88+ object:: macho:: X86_64_RELOC_BRANCH ,
89+ ) ,
90+ "x86" => ( & [ 0x55 , 0x89 , 0xe5 , 0x5d , 0xe9 , 0xf7 , 0xff , 0xff , 0xff ] , 16 , -4 , 0 ) ,
91+ arch => unimplemented ! ( "unsupported Apple architecture {arch:?}" ) ,
92+ } ;
93+
94+ let offset = file. section_mut ( section) . append_data ( data, align) ;
95+ file. add_relocation ( section, object:: write:: Relocation {
96+ offset,
97+ addend,
98+ symbol,
99+ flags : object:: write:: RelocationFlags :: MachO { r_type, r_pcrel : true , r_length : 2 } ,
100+ } ) ?;
101+
102+ Ok ( ( ) )
103+ }
104+
29105/// Deployment target or SDK version.
30106///
31107/// The size of the numbers in here are limited by Mach-O's `LC_BUILD_VERSION`.
0 commit comments