Mercurial > pub > dyncall > dyncall
comparison dynload/dynload_syms_pe.c @ 398:a29af998e457
- dynload w/ PE files:
* some better header check
* check if export table exists (thanks Ashok!)
* make some dynsyms function behaviour consistent with ELF and mach-o (thanks Ashok!)
author | Tassilo Philipp |
---|---|
date | Tue, 06 Apr 2021 21:23:39 +0200 |
parents | f5577f6bf97a |
children | 2d9f1cb06352 |
comparison
equal
deleted
inserted
replaced
397:77ec5123e7ab | 398:a29af998e457 |
---|---|
57 return NULL; | 57 return NULL; |
58 | 58 |
59 base = (const char*)pLib; | 59 base = (const char*)pLib; |
60 pDOSHeader = (IMAGE_DOS_HEADER*)base; | 60 pDOSHeader = (IMAGE_DOS_HEADER*)base; |
61 pNTHeader = (IMAGE_NT_HEADERS*)(base + pDOSHeader->e_lfanew); | 61 pNTHeader = (IMAGE_NT_HEADERS*)(base + pDOSHeader->e_lfanew); |
62 | |
63 /* optional header present and big enough? this header should exist as it's only optional for object files */ | |
64 if(pNTHeader->FileHeader.SizeOfOptionalHeader < (&pNTHeader->OptionalHeader.DataDirectory - &pNTHeader->OptionalHeader)) | |
65 return NULL; | |
66 | |
67 /* export table available? */ | |
68 if(pNTHeader->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXPORT) | |
69 return NULL; | |
70 | |
62 pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | 71 pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; |
72 if(!pExportsDataDir->VirtualAddress) | |
73 return NULL; | |
74 | |
63 pExports = (IMAGE_EXPORT_DIRECTORY*)(base + pExportsDataDir->VirtualAddress); | 75 pExports = (IMAGE_EXPORT_DIRECTORY*)(base + pExportsDataDir->VirtualAddress); |
64 | 76 |
65 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | 77 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); |
66 pSyms->pBase = base; | 78 pSyms->pBase = base; |
67 pSyms->pNames = (DWORD*)(base + pExports->AddressOfNames); | 79 pSyms->pNames = (DWORD*)(base + pExports->AddressOfNames); |
83 } | 95 } |
84 | 96 |
85 | 97 |
86 int dlSymsCount(DLSyms* pSyms) | 98 int dlSymsCount(DLSyms* pSyms) |
87 { | 99 { |
88 return (int)pSyms->count; | 100 return pSyms ? (int)pSyms->count : 0; |
89 } | 101 } |
90 | 102 |
91 | 103 |
92 const char* dlSymsName(DLSyms* pSyms, int index) | 104 const char* dlSymsName(DLSyms* pSyms, int index) |
93 { | 105 { |
106 if(!pSyms || index < 0 || index >= pSyms->count) | |
107 return NULL; | |
94 return pSyms->pBase + pSyms->pNames[index]; | 108 return pSyms->pBase + pSyms->pNames[index]; |
95 } | 109 } |
96 | 110 |
97 | 111 |
98 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) | 112 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) |