Skip to content

Commit 52a81ef

Browse files
committed
wip
1 parent 7f42cbd commit 52a81ef

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

view/sharedcache/core/MachO.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ std::optional<SharedCacheMachOHeader> SharedCacheMachOHeader::ParseHeaderForAddr
434434
char sectionName[17];
435435
memcpy(sectionName, section.sectname, sizeof(section.sectname));
436436
sectionName[16] = 0;
437-
if (header.identifierPrefix.empty())
438-
header.sectionNames.emplace_back(sectionName);
439-
else
440-
header.sectionNames.push_back(header.identifierPrefix + "::" + sectionName);
437+
header.sectionNames.push_back(header.identifierPrefix + "::" + sectionName);
441438
}
442439
}
443440
catch (ReadException&)
@@ -565,30 +562,40 @@ std::optional<CacheSymbol> SharedCacheMachOHeader::AddExportTerminalSymbol(const
565562
if (symbolName.empty() || symbolAddress == 0)
566563
return std::nullopt;
567564

568-
CacheSymbol exportSym;
569-
exportSym.name = symbolName;
570-
exportSym.type = DataSymbol;
571-
exportSym.address = symbolAddress;
572-
573-
uint32_t sectionFlags = 0;
574-
for (const auto& section : sections)
575-
{
576-
if (section.addr < symbolAddress && section.addr + section.size > symbolAddress)
577-
{
578-
// Take the flags from the first containing section.
579-
sectionFlags = section.flags;
580-
break;
581-
}
582-
}
583-
584-
// TODO: Is this enough to determine a function symbol?
585-
// TODO: Might be the cause of https://github.com/Vector35/binaryninja-api/issues/6526
586-
// Check the sections flags to see if we actually have a function symbol instead.
587-
if ((sectionFlags & S_ATTR_PURE_INSTRUCTIONS) == S_ATTR_PURE_INSTRUCTIONS
588-
|| (sectionFlags & S_ATTR_SOME_INSTRUCTIONS) == S_ATTR_SOME_INSTRUCTIONS)
589-
exportSym.type = FunctionSymbol;
590-
591-
return exportSym;
565+
// Tries to get the symbol type based off the section containing it.
566+
auto sectionSymbolType = [&]() -> BNSymbolType {
567+
uint32_t sectionFlags = 0;
568+
for (const auto& section : sections)
569+
{
570+
if (symbolAddress >= section.addr && symbolAddress < section.addr + section.size)
571+
{
572+
// Take the flags from the first containing section.
573+
sectionFlags = section.flags;
574+
break;
575+
}
576+
}
577+
578+
// TODO: Is this enough to determine a function symbol?
579+
// TODO: Might be the cause of https://github.com/Vector35/binaryninja-api/issues/6526
580+
// Check the sections flags to see if we actually have a function symbol instead.
581+
if (sectionFlags & S_ATTR_PURE_INSTRUCTIONS || sectionFlags & S_ATTR_SOME_INSTRUCTIONS)
582+
return FunctionSymbol;
583+
584+
// By default, just return data symbol.
585+
return DataSymbol;
586+
};
587+
588+
switch (symbolFlags & EXPORT_SYMBOL_FLAGS_KIND_MASK)
589+
{
590+
case EXPORT_SYMBOL_FLAGS_KIND_REGULAR:
591+
case EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL:
592+
return CacheSymbol(sectionSymbolType(), symbolAddress, symbolName);
593+
case EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE:
594+
return CacheSymbol(DataSymbol, symbolAddress, symbolName);
595+
default:
596+
LogWarn("Unhandled export symbol kind: %x", symbolFlags & EXPORT_SYMBOL_FLAGS_KIND_MASK);
597+
return std::nullopt;
598+
}
592599
}
593600

594601
// TODO: This is like 90% of the runtime.

view/sharedcache/core/MachO.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct SharedCacheMachOHeader
1212
uint64_t textBase = 0;
1313
uint64_t loadCommandOffset = 0;
1414
BinaryNinja::mach_header_64 ident;
15+
// NOTE: This should never be empty.
1516
std::string identifierPrefix;
1617
std::string installName;
1718

view/sharedcache/core/SharedCache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ enum class CacheRegionType
3838
NonImage,
3939
};
4040

41-
// TODO: Make an ImageCacheRegion that holds the image start?
42-
// TODO: Holding the image start here is annoying AF
4341
struct CacheRegion
4442
{
4543
CacheRegionType type;

view/sharedcache/core/SharedCacheController.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,12 @@ bool SharedCacheController::ApplyRegion(BinaryView& view, const CacheRegion& reg
126126
return false;
127127
}
128128

129+
// Unique memory region name so that we don't cause collisions.
130+
const auto memoryRegionName = fmt::format("{}_0x{:x}", region.name, region.start);
131+
129132
// NOTE: Adding a data memory region will store the entire contents of the region in the BNDB.
130133
// TODO: We can use the AddRemoteMemoryRegion if we want to reload on view init.
131-
view.GetMemoryMap()->AddDataMemoryRegion(region.name, region.start, buffer, region.flags);
134+
view.GetMemoryMap()->AddDataMemoryRegion(memoryRegionName, region.start, buffer, region.flags);
132135
// TODO: We might want to make this auto if we decide to "reload" all loaded region in view init.
133136
// view.AddUserSection(region.name, region.start, region.size, region.SectionSemanticsForRegion());
134137

0 commit comments

Comments
 (0)