Skip to content

Commit 9a43a4d

Browse files
committed
[MachO] Fix handling of relocations for self-bound data symbols
Fixes #7781.
1 parent baa7f50 commit 9a43a4d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

view/macho/machoview.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,14 +2127,26 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_
21272127
switch (ordinal)
21282128
{
21292129
case BindSpecialDylibSelf:
2130-
if (auto symbol = GetSymbolByRawName(name, GetInternalNameSpace()); symbol)
2130+
{
2131+
// When multiple symbols are defined with the same name, which can happen for a symbol is both in the
2132+
// symbol table and self-bound, `GetSymbolByRawName` prefers the symbol with the lowest type value.
2133+
// Since `ImportAddressSymbol` is a lower value than `DataSymbol`, using `GetSymbolByRawName` would
2134+
// return the symbol representing the import we're binding to rather than the actual symbol definition.
2135+
auto symbols = GetSymbolsByRawName(name, GetInternalNameSpace());
2136+
auto it = std::ranges::find_if(symbols, [](const Ref<Symbol>& sym) {
2137+
return sym->GetType() != ImportAddressSymbol;
2138+
});
2139+
2140+
if (it != symbols.end())
21312141
{
2142+
auto symbol = *it;
21322143
DefineRelocation(m_arch, relocation, symbol, relocation.address);
21332144
if (objcProcessor)
21342145
objcProcessor->AddRelocatedPointer(relocation.address, symbol->GetAddress());
21352146
handled = true;
21362147
}
21372148
break;
2149+
}
21382150

21392151
case BindSpecialDylibMainExecutable:
21402152
case BindSpecialDylibFlatLookup:

0 commit comments

Comments
 (0)