From f83b7bd44c3c82cb71237124705d8687e658da4f Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Wed, 10 Dec 2025 23:26:46 -0800 Subject: [PATCH] [MachO] Fix handling of relocations for self-bound data symbols Fixes https://github.com/Vector35/binaryninja-api/issues/7781. --- view/macho/machoview.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/view/macho/machoview.cpp b/view/macho/machoview.cpp index 1c8d85120c..3c55896318 100644 --- a/view/macho/machoview.cpp +++ b/view/macho/machoview.cpp @@ -2127,14 +2127,26 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_ switch (ordinal) { case BindSpecialDylibSelf: - if (auto symbol = GetSymbolByRawName(name, GetInternalNameSpace()); symbol) + { + // When multiple symbols are defined with the same name, which can happen for a symbol is both in the + // symbol table and self-bound, `GetSymbolByRawName` prefers the symbol with the lowest type value. + // Since `ImportAddressSymbol` is a lower value than `DataSymbol`, using `GetSymbolByRawName` would + // return the symbol representing the import we're binding to rather than the actual symbol definition. + auto symbols = GetSymbolsByRawName(name, GetInternalNameSpace()); + auto it = std::ranges::find_if(symbols, [](const Ref& sym) { + return sym->GetType() != ImportAddressSymbol; + }); + + if (it != symbols.end()) { + auto symbol = *it; DefineRelocation(m_arch, relocation, symbol, relocation.address); if (objcProcessor) objcProcessor->AddRelocatedPointer(relocation.address, symbol->GetAddress()); handled = true; } break; + } case BindSpecialDylibMainExecutable: case BindSpecialDylibFlatLookup: