From bdb62855f9b6cb85fcf64dba0a067d9056f09af5 Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Fri, 12 Sep 2025 13:41:50 -0700 Subject: [MachO] Populate external links with library imports for files in projects A mapping from imported symbol names to libraries is added to metadata. This is used to populate external links for files within projects, following the lead of `PEView`. --- view/macho/machoview.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/view/macho/machoview.cpp b/view/macho/machoview.cpp index 1c65629a..52315485 100644 --- a/view/macho/machoview.cpp +++ b/view/macho/machoview.cpp @@ -790,6 +790,9 @@ MachOHeader MachoView::HeaderForAddress(BinaryView* data, uint64_t address, bool } break; case LC_LOAD_DYLIB: + case LC_LOAD_WEAK_DYLIB: + case LC_REEXPORT_DYLIB: + case LC_LOAD_UPWARD_DYLIB: { uint32_t offset = reader.Read32(); reader.Read32(); // timestamp @@ -2087,6 +2090,8 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_ if (objcProcessor) objcProcessor->AddRelocatedPointer(relocationLocation, slidTarget); } + + Ref symbolToLibraryMapping = new Metadata(KeyValueDataType); for (auto& [relocation, name, ordinal] : header.bindingRelocations) { bool handled = false; @@ -2149,6 +2154,8 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_ DefineRelocation(m_arch, relocation, symbol, relocation.address); handled = true; } + if (ordinal - 1 < header.dylibs.size()) + symbolToLibraryMapping->SetValueForKey(name, new Metadata(header.dylibs[ordinal - 1].first)); } break; } @@ -2157,6 +2164,8 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_ m_logger->LogErrorF("Failed to find external symbol {:?}, couldn't bind symbol at {:#x}", name, relocation.address); } + StoreMetadata("SymbolExternalLibraryMapping", std::move(symbolToLibraryMapping), true); + auto relocationHandler = m_arch->GetRelocationHandler("Mach-O"); if (relocationHandler) { -- cgit v1.3.1