bool NativePluginModel::IsPlugin2(const void* Module) { try { auto pDOSHeader = reinterpret_cast<const IMAGE_DOS_HEADER*>(Module); if (pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE) return false; auto pPEHeader = reinterpret_cast<const IMAGE_NT_HEADERS*>(reinterpret_cast<const char*>(Module) + pDOSHeader->e_lfanew); if (pPEHeader->Signature != IMAGE_NT_SIGNATURE) return false; if (!(pPEHeader->FileHeader.Characteristics & IMAGE_FILE_DLL)) return false; static WORD FarMachineType = 0; if (!FarMachineType) { HMODULE FarModule = GetModuleHandle(nullptr); FarMachineType = reinterpret_cast<const IMAGE_NT_HEADERS*>(reinterpret_cast<const char*>(FarModule) + reinterpret_cast<const IMAGE_DOS_HEADER*>(FarModule)->e_lfanew)->FileHeader.Machine; } if (pPEHeader->FileHeader.Machine != FarMachineType) return false; DWORD dwExportAddr = pPEHeader->OptionalHeader.DataDirectory[0].VirtualAddress; if (!dwExportAddr) return false; auto pSection = IMAGE_FIRST_SECTION(pPEHeader); for (int i = 0; i < pPEHeader->FileHeader.NumberOfSections; i++) { if ((pSection[i].VirtualAddress == dwExportAddr) || ((pSection[i].VirtualAddress <= dwExportAddr) && ((pSection[i].Misc.VirtualSize+pSection[i].VirtualAddress) > dwExportAddr))) { int nDiff = pSection[i].VirtualAddress-pSection[i].PointerToRawData; auto pExportDir = reinterpret_cast<const IMAGE_EXPORT_DIRECTORY*>(reinterpret_cast<const char*>(Module) + dwExportAddr - nDiff); auto pNames = reinterpret_cast<const DWORD*>(reinterpret_cast<const char*>(Module) + pExportDir->AddressOfNames-nDiff); for (DWORD n = 0; n < pExportDir->NumberOfNames; n++) { if (FindExport(reinterpret_cast<const char *>(Module) + pNames[n]-nDiff)) { return true; } } } } } catch (SException&) { } return false; }
int IsUnresolved (unsigned Name) /* Check if this symbol is an unresolved export */ { /* Find the export */ return IsUnresolvedExport (FindExport (Name)); }
static void ParseSymbols (void) /* Parse a symbols section */ { static const IdentTok Attributes[] = { { "VALUE", CFGTOK_VALUE }, { "WEAK", CFGTOK_WEAK }, }; while (CfgTok == CFGTOK_IDENT) { long Val = 0L; int Weak = 0; Export* E; /* Remember the name */ unsigned Name = GetStringId (CfgSVal); CfgNextTok (); /* Support both, old and new syntax here. New syntax is a colon * followed by an attribute list, old syntax is an optional equal * sign plus a value. */ if (CfgTok != CFGTOK_COLON) { /* Old syntax */ /* Allow an optional assignment */ CfgOptionalAssign (); /* Make sure the next token is an integer expression, read and * skip it. */ Val = CfgIntExpr (); } else { /* Bitmask to remember the attributes we got already */ enum { atNone = 0x0000, atValue = 0x0001, atWeak = 0x0002 }; unsigned AttrFlags = atNone; /* New syntax - skip the colon */ CfgNextTok (); /* Parse the attributes */ while (1) { /* Map the identifier to a token */ cfgtok_t AttrTok; CfgSpecialToken (Attributes, ENTRY_COUNT (Attributes), "Attribute"); AttrTok = CfgTok; /* Skip the attribute name */ CfgNextTok (); /* An optional assignment follows */ CfgOptionalAssign (); /* Check which attribute was given */ switch (AttrTok) { case CFGTOK_VALUE: /* Don't allow this twice */ FlagAttr (&AttrFlags, atValue, "VALUE"); /* We expect a numeric expression */ Val = CfgIntExpr (); break; case CFGTOK_WEAK: /* Don't allow this twice */ FlagAttr (&AttrFlags, atWeak, "WEAK"); CfgBoolToken (); Weak = (CfgTok == CFGTOK_TRUE); CfgNextTok (); break; default: FAIL ("Unexpected attribute token"); } /* Semicolon ends the decl, otherwise accept an optional comma */ if (CfgTok == CFGTOK_SEMI) { break; } else if (CfgTok == CFGTOK_COMMA) { CfgNextTok (); } } /* Check if we have all mandatory attributes */ AttrCheck (AttrFlags, atValue, "VALUE"); /* Weak is optional, the default are non weak symbols */ if ((AttrFlags & atWeak) == 0) { Weak = 0; } } /* Check if the symbol is already defined */ if ((E = FindExport (Name)) != 0 && !IsUnresolvedExport (E)) { /* If the symbol is not marked as weak, this is an error. * Otherwise ignore the symbol from the config. */ if (!Weak) { CfgError ("Symbol `%s' is already defined", GetString (Name)); } } else { /* The symbol is undefined, generate an export */ CreateConstExport (Name, Val); } /* Skip the semicolon */ CfgConsumeSemi (); } /* Remember we had this section */ SectionsEncountered |= SE_SYMBOLS; }