@@ -2085,8 +2085,19 @@ fn add_linked_symbol_object(
20852085 file. set_mangling ( object:: write:: Mangling :: None ) ;
20862086 }
20872087
2088+ // ld64 requires a relocation to load undefined symbols, see below.
2089+ let ld64_section_helper = if file. format ( ) == object:: BinaryFormat :: MachO {
2090+ Some ( file. add_section (
2091+ file. segment_name ( object:: write:: StandardSegment :: Text ) . to_vec ( ) ,
2092+ "__text" . into ( ) ,
2093+ object:: SectionKind :: Text ,
2094+ ) )
2095+ } else {
2096+ None
2097+ } ;
2098+
20882099 for ( sym, kind) in symbols. iter ( ) {
2089- file. add_symbol ( object:: write:: Symbol {
2100+ let symbol = file. add_symbol ( object:: write:: Symbol {
20902101 name : sym. clone ( ) . into ( ) ,
20912102 value : 0 ,
20922103 size : 0 ,
@@ -2100,6 +2111,23 @@ fn add_linked_symbol_object(
21002111 section : object:: write:: SymbolSection :: Undefined ,
21012112 flags : object:: SymbolFlags :: None ,
21022113 } ) ;
2114+
2115+ // TODO: Explain.
2116+ if let Some ( section) = ld64_section_helper {
2117+ // TODO: Use architecture-specific data.
2118+ let offset = file. section_mut ( section) . append_data ( & [ 0 , 0 , 0 , 20 ] , 4 ) ;
2119+ file. add_relocation ( section, object:: write:: Relocation {
2120+ offset,
2121+ addend : 0 ,
2122+ symbol,
2123+ flags : object:: write:: RelocationFlags :: MachO {
2124+ r_type : object:: macho:: ARM64_RELOC_BRANCH26 ,
2125+ r_pcrel : true ,
2126+ r_length : 2 ,
2127+ } ,
2128+ } )
2129+ . expect ( "failed adding relocation" ) ;
2130+ }
21032131 }
21042132
21052133 let path = tmpdir. join ( "symbols.o" ) ;
0 commit comments