MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / InitializeSegmentsForHeader

Method InitializeSegmentsForHeader

view/kernelcache/core/KernelCache.cpp:1044–1392  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1042}
1043
1044bool KernelCache::InitializeSegmentsForHeader(Ref<BinaryView> view, const KernelCacheMachOHeader& header, const KernelCacheImage& targetImage)
1045{
1046 // FIXME this uselessly loads chained fixups if an image is already loaded.
1047 auto logger = LogRegistry::GetLogger("KernelCache", view->GetFile()->GetSessionId());
1048
1049 bool hasRegionsToLoad = false;
1050
1051 auto reader = BinaryReader(view->GetParentView());
1052
1053 reader.Seek(0x10);
1054 uint32_t ncmds = reader.Read32();
1055 uint64_t off = 0x20; // FIXME 32 bit
1056
1057 uint64_t kernelBaseAddress = 0;
1058
1059 uint32_t chainedFixupDataOff = 0;
1060 uint32_t chainedFixupDataSize = 0;
1061
1062 while (ncmds--)
1063 {
1064 reader.Seek(off);
1065 uint32_t cmd = reader.Read32();
1066 uint32_t cmdsize = reader.Read32();
1067 if (cmd == LC_SEGMENT_64)
1068 {
1069 segment_command_64 segment {};
1070 reader.Read(&segment.segname, 16);
1071 if (strncmp(segment.segname, "__TEXT\0", 7) == 0)
1072 {
1073 kernelBaseAddress = reader.Read64();
1074 }
1075 }
1076 if (cmd == LC_DYLD_CHAINED_FIXUPS)
1077 {
1078 chainedFixupDataOff = reader.Read32();
1079 chainedFixupDataSize = reader.Read32();
1080 }
1081 off += cmdsize;
1082 }
1083
1084 BNRelocationInfo reloc;
1085 memset(&reloc, 0, sizeof(BNRelocationInfo));
1086 reloc.type = StandardRelocationType;
1087 reloc.size = 8;
1088 reloc.nativeType = BINARYNINJA_MANUAL_RELOCATION;
1089 logger->LogDebug("Processing Chained Fixups");
1090
1091 std::vector<std::pair<uint64_t, uint64_t>> relocations;
1092 if (chainedFixupDataOff && chainedFixupDataSize)
1093 {
1094 BinaryReader parentReader(view->GetParentView());
1095
1096 try {
1097 dyld_chained_fixups_header fixupsHeader {};
1098 uint64_t fixupHeaderAddress = chainedFixupDataOff;
1099 parentReader.Seek(fixupHeaderAddress);
1100 fixupsHeader.fixups_version = parentReader.Read32();
1101 fixupsHeader.starts_offset = parentReader.Read32();

Callers

nothing calls this directly

Calls 15

ReadExceptionClass · 0.85
GetParentViewMethod · 0.80
LogDebugMethod · 0.80
LogErrorMethod · 0.80
push_backMethod · 0.80
LogTraceMethod · 0.80
IsValidOffsetMethod · 0.80
c_strMethod · 0.80
AddAutoSegmentMethod · 0.80
DefineRelocationMethod · 0.80
LogWarnMethod · 0.80

Tested by

no test coverage detected