#ifndef IG_OBFUSCATOR_PASS_UTILS_H #define IG_OBFUSCATOR_PASS_UTILS_H #include "IGPolicyConfig.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Module.h" #include "llvm/Support/SHA256.h" #include "llvm/TargetParser/Triple.h" #include #include #include namespace igobf { inline llvm::StringRef stripObjCMarker(llvm::StringRef Name) { if (!Name.empty() && Name.front() == '\x01') return Name.drop_front(1); return Name; } inline bool isMachOAArch64NonArm64e(const llvm::Module &M) { if (M.getTargetTriple().empty()) return true; llvm::Triple TT(M.getTargetTriple()); return TT.isOSBinFormatMachO() && TT.isAArch64() && TT.getArchName() != "arm64e"; } inline std::string computeShortModuleTag(llvm::StringRef Seed, llvm::StringRef ModId, llvm::StringRef SourcePath) { llvm::SHA256 Hasher; Hasher.update(Seed); Hasher.update("|"); Hasher.update(ModId); Hasher.update("|"); Hasher.update(SourcePath); auto Digest = Hasher.final(); return llvm::toHex(llvm::ArrayRef(Digest.data(), 4), /*LowerCase=*/true); } inline bool isCompilerSynthesizedFunction(const llvm::Function &F, const IGPolicyConfig &Policy) { if (Policy.hasAnnotation(F, "ig_flatten")) return false; llvm::StringRef Name = stripObjCMarker(F.getName()); if (Name.starts_with("block_copy_helper") || Name.starts_with("block_destroy_helper") || Name.starts_with("__copy_helper_block_") || Name.starts_with("___copy_helper_block_") || Name.starts_with("__destroy_helper_block_") || Name.starts_with("___destroy_helper_block_") || Name.starts_with("objectdestroy") || Name.ends_with(" .cxx_destruct]") || Name.contains(".resume.")) { return true; } if (Name.starts_with("$s") || Name.starts_with("_$s")) { if (Name.ends_with("Tm") || Name.ends_with("Wl") || Name.ends_with("WL") || Name.ends_with("CMa") || Name.ends_with("VMa") || Name.ends_with("OMa") || Name.ends_with("WOh") || Name.ends_with("WOy") || Name.ends_with("WOe") || Name.ends_with("WOc") || Name.ends_with("WOb") || Name.ends_with("WOd") || Name.ends_with("TR") || Name.ends_with("TA")) { return true; } } if (const llvm::Module *M = F.getParent()) { if (M->getNamedMetadata("llvm.dbg.cu") != nullptr) { const llvm::DISubprogram *SP = F.getSubprogram(); if (!SP || SP->isArtificial() || SP->getFilename().contains("")) { return true; } } } return false; } inline bool matchesAnySubstring(const llvm::Function &F, const std::vector &Patterns) { llvm::StringRef CleanName = stripObjCMarker(F.getName()); for (const std::string &Pat : Patterns) { if (!Pat.empty() && (CleanName.contains(Pat) || F.getName().contains(Pat))) { return true; } } return false; } inline bool isFunctionSafeForTransform( const llvm::Function &F, const IGPolicyConfig &Policy, const std::vector *ExtraExemptSubstrings = nullptr) { if (F.isDeclaration() || Policy.isFunctionExempt(F)) return false; if (llvm::GlobalValue::isWeakForLinker(F.getLinkage()) || F.hasAvailableExternallyLinkage()) { return false; } if (F.isPresplitCoroutine() || F.hasFnAttribute(llvm::Attribute::Naked)) return false; if (F.getCallingConv() == llvm::CallingConv::SwiftTail) return false; if (F.hasSection()) { llvm::StringRef Sec = F.getSection(); if (Sec.contains("__mod_init_func") || Sec.contains("__objc_init_func") || Sec.contains("__mod_term_func")) { return false; } } if (isCompilerSynthesizedFunction(F, Policy)) return false; if (ExtraExemptSubstrings && matchesAnySubstring(F, *ExtraExemptSubstrings)) { return false; } return true; } inline llvm::DebugLoc resolveInstDebugLoc(const llvm::Instruction *I, const llvm::Function &F) { if (I && I->getDebugLoc()) return I->getDebugLoc(); if (llvm::DISubprogram *SP = F.getSubprogram()) { unsigned Line = SP->getScopeLine() ? SP->getScopeLine() : SP->getLine(); return llvm::DILocation::get(F.getContext(), Line, 0, SP); } return llvm::DebugLoc(); } } // namespace igobf #endif