summaryrefslogtreecommitdiff
path: root/view/sharedcache/core/MachO.h
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-03-10 11:05:40 -0400
committerMason Reed <mason@vector35.com>2025-04-02 05:36:54 -0400
commit25cc02431b61097b2adfc2fbc493b648b0300c3b (patch)
treea79d9c4f4f67234d3bf9bda413e8608f479a4cc8 /view/sharedcache/core/MachO.h
parentfa85bf28502286c4821427c5d0ed91a7ed46f8f6 (diff)
[SharedCache] Refactor Shared Cache
In absence of a better name, this commit refactors the shared cache code.
Diffstat (limited to 'view/sharedcache/core/MachO.h')
-rw-r--r--view/sharedcache/core/MachO.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/view/sharedcache/core/MachO.h b/view/sharedcache/core/MachO.h
new file mode 100644
index 00000000..37b82915
--- /dev/null
+++ b/view/sharedcache/core/MachO.h
@@ -0,0 +1,76 @@
+#pragma once
+
+#include "VirtualMemory.h"
+
+// TODO: Including this adds a bunch of binary ninja specific stuff :ugh:
+#include "view/macho/machoview.h"
+
+struct CacheSymbol;
+
+struct SharedCacheMachOHeader
+{
+ uint64_t textBase = 0;
+ uint64_t loadCommandOffset = 0;
+ BinaryNinja::mach_header_64 ident;
+ // NOTE: This should never be empty.
+ std::string identifierPrefix;
+ std::string installName;
+
+ std::vector<std::pair<uint64_t, bool>> entryPoints;
+ std::vector<uint64_t> m_entryPoints; // list of entrypoints
+
+ BinaryNinja::symtab_command symtab;
+ BinaryNinja::dysymtab_command dysymtab;
+ BinaryNinja::dyld_info_command dyldInfo;
+ BinaryNinja::routines_command_64 routines64;
+ BinaryNinja::function_starts_command functionStarts;
+ std::vector<BinaryNinja::section_64> moduleInitSections;
+ BinaryNinja::linkedit_data_command exportTrie;
+ BinaryNinja::linkedit_data_command chainedFixups {};
+
+ uint64_t relocationBase;
+ // Section and program headers, internally use 64-bit form as it is a superset of 32-bit
+ std::vector<BinaryNinja::segment_command_64> segments; // only three types of sections __TEXT, __DATA, __IMPORT
+ BinaryNinja::segment_command_64 linkeditSegment;
+ std::vector<BinaryNinja::section_64> sections;
+ std::vector<std::string> sectionNames;
+
+ std::vector<BinaryNinja::section_64> symbolStubSections;
+ std::vector<BinaryNinja::section_64> symbolPointerSections;
+
+ std::vector<std::string> dylibs;
+
+ BinaryNinja::build_version_command buildVersion;
+ std::vector<BinaryNinja::build_tool_version> buildToolVersions;
+
+ std::string exportTriePath;
+
+ bool linkeditPresent = false;
+ bool dysymPresent = false;
+ bool dyldInfoPresent = false;
+ bool exportTriePresent = false;
+ bool chainedFixupsPresent = false;
+ bool routinesPresent = false;
+ bool functionStartsPresent = false;
+ bool relocatable = false;
+
+ // The base address of the link edit file.
+ // Use this if you want to read offsets relative to the file containing the link edit segment.
+ uint64_t GetLinkEditFileBase() const { return linkeditSegment.vmaddr - linkeditSegment.fileoff; };
+
+ static std::optional<SharedCacheMachOHeader> ParseHeaderForAddress(
+ std::shared_ptr<VirtualMemory> vm, uint64_t address, const std::string& imagePath);
+
+ // TODO: Replace view with address size?
+ std::vector<CacheSymbol> ReadSymbolTable(BinaryNinja::BinaryView& view, VirtualMemory& vm) const;
+
+ std::optional<CacheSymbol> AddExportTerminalSymbol(
+ const std::string& symbolName, const uint8_t* current, const uint8_t* end) const;
+
+ bool ProcessLinkEditTrie(std::vector<CacheSymbol>& symbols, const std::string& currentText, const uint8_t* begin,
+ const uint8_t* current, const uint8_t* end) const;
+
+ std::vector<CacheSymbol> ReadExportSymbolTrie(VirtualMemory& vm) const;
+
+ std::vector<uint64_t> ReadFunctionTable(VirtualMemory& vm) const;
+};