@@ -2093,10 +2093,74 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_
2093
2093
if (objcProcessor)
2094
2094
objcProcessor->AddRelocatedPointer (relocationLocation, slidTarget);
2095
2095
}
2096
- for (auto & [relocation, name] : header.externalRelocations )
2096
+ for (auto & [relocation, name, ordinal ] : header.bindingRelocations )
2097
2097
{
2098
- if (auto symbol = GetSymbolByRawName (name, GetExternalNameSpace ()); symbol)
2099
- DefineRelocation (m_arch, relocation, symbol, relocation.address );
2098
+ bool handled = false ;
2099
+
2100
+ switch (ordinal)
2101
+ {
2102
+ case BindSpecialDylibSelf:
2103
+ if (auto symbol = GetSymbolByRawName (name, GetInternalNameSpace ()); symbol)
2104
+ {
2105
+ DefineRelocation (m_arch, relocation, symbol, relocation.address );
2106
+ if (objcProcessor)
2107
+ objcProcessor->AddRelocatedPointer (relocation.address , symbol->GetAddress ());
2108
+ handled = true ;
2109
+ }
2110
+ break ;
2111
+
2112
+ case BindSpecialDylibMainExecutable:
2113
+ case BindSpecialDylibFlatLookup:
2114
+ case BindSpecialDylibWeakLookup:
2115
+ // In cases where we are the primary executable, flat lookup should find us first,
2116
+ // it seems like our best course of action is to try and find internally first on
2117
+ // executables, and externally on libraries.
2118
+ if (header.ident .filetype == MH_EXECUTE)
2119
+ {
2120
+ if (auto symbol = GetSymbolByRawName (name, GetInternalNameSpace ()); symbol)
2121
+ {
2122
+ DefineRelocation (m_arch, relocation, symbol, relocation.address );
2123
+ if (objcProcessor)
2124
+ objcProcessor->AddRelocatedPointer (relocation.address , symbol->GetAddress ());
2125
+ handled = true ;
2126
+ }
2127
+ else if (auto symbol = GetSymbolByRawName (name, GetExternalNameSpace ()); symbol)
2128
+ {
2129
+ DefineRelocation (m_arch, relocation, symbol, relocation.address );
2130
+ handled = true ;
2131
+ }
2132
+ }
2133
+ else
2134
+ {
2135
+ if (auto symbol = GetSymbolByRawName (name, GetExternalNameSpace ()); symbol)
2136
+ {
2137
+ DefineRelocation (m_arch, relocation, symbol, relocation.address );
2138
+ handled = true ;
2139
+ }
2140
+ else if (auto symbol = GetSymbolByRawName (name, GetInternalNameSpace ()); symbol)
2141
+ {
2142
+ DefineRelocation (m_arch, relocation, symbol, relocation.address );
2143
+ if (objcProcessor)
2144
+ objcProcessor->AddRelocatedPointer (relocation.address , symbol->GetAddress ());
2145
+ handled = true ;
2146
+ }
2147
+ }
2148
+ break ;
2149
+
2150
+ default :
2151
+ if (ordinal > 0 )
2152
+ {
2153
+ if (auto symbol = GetSymbolByRawName (name, GetExternalNameSpace ()); symbol)
2154
+ {
2155
+ DefineRelocation (m_arch, relocation, symbol, relocation.address );
2156
+ handled = true ;
2157
+ }
2158
+ }
2159
+ break ;
2160
+ }
2161
+
2162
+ if (!handled)
2163
+ m_logger->LogError (" Failed to find external symbol '%s', couldn't bind symbol at 0x%llx" , name.c_str (), relocation.address );
2100
2164
}
2101
2165
2102
2166
auto relocationHandler = m_arch->GetRelocationHandler (" Mach-O" );
@@ -2848,7 +2912,7 @@ void MachoView::ParseDynamicTable(BinaryReader& reader, MachOHeader& header, BNS
2848
2912
BNRelocationInfo externReloc;
2849
2913
2850
2914
BNSymbolType symtype = incomingType;
2851
- // uint64_t ordinal = 0;
2915
+ uint64_t ordinal = 0 ;
2852
2916
// int64_t addend = 0;
2853
2917
uint64_t segmentIndex = 0 ;
2854
2918
uint64_t address = 0 ;
@@ -2866,7 +2930,7 @@ void MachoView::ParseDynamicTable(BinaryReader& reader, MachOHeader& header, BNS
2866
2930
switch (opcode)
2867
2931
{
2868
2932
case BindOpcodeDone:
2869
- // ordinal = 0;
2933
+ ordinal = 0 ;
2870
2934
// addend = 0;
2871
2935
segmentIndex = 0 ;
2872
2936
address = 0 ;
@@ -2876,9 +2940,9 @@ void MachoView::ParseDynamicTable(BinaryReader& reader, MachOHeader& header, BNS
2876
2940
type = 0 ;
2877
2941
symtype = incomingType;
2878
2942
break ;
2879
- case BindOpcodeSetDylibOrdinalImmediate: /* ordinal = imm; */ break ;
2880
- case BindOpcodeSetDylibOrdinalULEB: /* ordinal = */ readLEB128 (table, tableSize, i); break ;
2881
- case BindOpcodeSetDylibSpecialImmediate: /* ordinal = -imm; */ break ;
2943
+ case BindOpcodeSetDylibOrdinalImmediate: ordinal = imm;break ;
2944
+ case BindOpcodeSetDylibOrdinalULEB: ordinal = readLEB128 (table, tableSize, i); break ;
2945
+ case BindOpcodeSetDylibSpecialImmediate: ordinal = -imm; break ;
2882
2946
case BindOpcodeSetSymbolTrailingFlagsImmediate:
2883
2947
/* flags = imm; */
2884
2948
name = (char *)&table[i];
@@ -2911,8 +2975,7 @@ void MachoView::ParseDynamicTable(BinaryReader& reader, MachOHeader& header, BNS
2911
2975
externReloc.size = m_addressSize;
2912
2976
externReloc.pcRelative = false ;
2913
2977
externReloc.external = true ;
2914
- header.externalRelocations .emplace_back (externReloc, string (name));
2915
-
2978
+ header.bindingRelocations .emplace_back (externReloc, string (name), ordinal);
2916
2979
address += m_addressSize;
2917
2980
break ;
2918
2981
case BindOpcodeDoBindAddAddressULEB:
@@ -2925,7 +2988,7 @@ void MachoView::ParseDynamicTable(BinaryReader& reader, MachOHeader& header, BNS
2925
2988
externReloc.size = m_addressSize;
2926
2989
externReloc.pcRelative = false ;
2927
2990
externReloc.external = true ;
2928
- header.externalRelocations .emplace_back (externReloc, string (name));
2991
+ header.bindingRelocations .emplace_back (externReloc, string (name), ordinal );
2929
2992
2930
2993
address += m_addressSize;
2931
2994
address += readLEB128 (table, tableSize, i);
@@ -2940,7 +3003,7 @@ void MachoView::ParseDynamicTable(BinaryReader& reader, MachOHeader& header, BNS
2940
3003
externReloc.size = m_addressSize;
2941
3004
externReloc.pcRelative = false ;
2942
3005
externReloc.external = true ;
2943
- header.externalRelocations .emplace_back (externReloc, string (name));
3006
+ header.bindingRelocations .emplace_back (externReloc, string (name), ordinal );
2944
3007
address += m_addressSize;
2945
3008
address += (imm * m_addressSize);
2946
3009
break ;
@@ -2959,7 +3022,7 @@ void MachoView::ParseDynamicTable(BinaryReader& reader, MachOHeader& header, BNS
2959
3022
externReloc.size = m_addressSize;
2960
3023
externReloc.pcRelative = false ;
2961
3024
externReloc.external = true ;
2962
- header.externalRelocations .emplace_back (externReloc, string (name));
3025
+ header.bindingRelocations .emplace_back (externReloc, string (name), ordinal );
2963
3026
2964
3027
address += skip + m_addressSize;
2965
3028
}
@@ -3288,7 +3351,7 @@ void MachoView::ParseChainedFixups(
3288
3351
{
3289
3352
uint32_t importEntry = parentReader.Read32 ();
3290
3353
dyld_chained_import import = *(reinterpret_cast <dyld_chained_import*>(&importEntry));
3291
- processChainedImport (import .lib_ordinal , 0 , import .name_offset , import .weak_import , parentReader);
3354
+ processChainedImport (static_cast < int8_t >( import .lib_ordinal ) , 0 , import .name_offset , import .weak_import , parentReader);
3292
3355
}
3293
3356
break ;
3294
3357
}
@@ -3298,7 +3361,7 @@ void MachoView::ParseChainedFixups(
3298
3361
{
3299
3362
dyld_chained_import_addend import ;
3300
3363
parentReader.Read (&import , sizeof (import ));
3301
- processChainedImport (import .lib_ordinal , import .addend , import .name_offset , import .weak_import , parentReader);
3364
+ processChainedImport (static_cast < int8_t >( import .lib_ordinal ) , import .addend , import .name_offset , import .weak_import , parentReader);
3302
3365
}
3303
3366
break ;
3304
3367
}
@@ -3308,7 +3371,7 @@ void MachoView::ParseChainedFixups(
3308
3371
{
3309
3372
dyld_chained_import_addend64 import ;
3310
3373
parentReader.Read (&import , sizeof (import ));
3311
- processChainedImport (import .lib_ordinal , import .addend , import .name_offset , import .weak_import , parentReader);
3374
+ processChainedImport (static_cast < int16_t >( import .lib_ordinal ) , import .addend , import .name_offset , import .weak_import , parentReader);
3312
3375
}
3313
3376
break ;
3314
3377
}
@@ -3521,7 +3584,7 @@ void MachoView::ParseChainedFixups(
3521
3584
chainEntryAddress += (nextEntryStrideCount * strideSize);
3522
3585
if (chainEntryAddress > pageAddress + starts.page_size )
3523
3586
{
3524
- m_logger->LogDebug (" Chained Fixups: Pointer at %llx left page" ,
3587
+ m_logger->LogError (" Chained Fixups: Pointer at %llx left page" ,
3525
3588
GetStart () + ((chainEntryAddress - (nextEntryStrideCount * strideSize))) - m_universalImageOffset);
3526
3589
fixupsDone = true ;
3527
3590
}
@@ -3546,9 +3609,8 @@ void MachoView::ParseChainedFixups(
3546
3609
externReloc.address = targetAddress;
3547
3610
externReloc.size = m_addressSize;
3548
3611
externReloc.pcRelative = false ;
3549
- externReloc.external = true ;
3550
3612
externReloc.addend = entry.addend ;
3551
- header.externalRelocations .emplace_back (externReloc, entry.name );
3613
+ header.bindingRelocations .emplace_back (externReloc, entry.name , entry. lib_ordinal );
3552
3614
}
3553
3615
else
3554
3616
{
0 commit comments