0
|
1 /*
|
|
2
|
|
3 Package: dyncall
|
|
4 Library: dynload
|
|
5 File: dynload/dynload_syms_pe.c
|
281
|
6 Description:
|
0
|
7 License:
|
|
8
|
281
|
9 Copyright (c) 2007-2018 Daniel Adler <dadler@uni-goettingen.de>,
|
0
|
10 Tassilo Philipp <tphilipp@potion-studios.com>
|
|
11 Olivier Chafik <olivier.chafik@gmail.com>
|
|
12
|
|
13 Permission to use, copy, modify, and distribute this software for any
|
|
14 purpose with or without fee is hereby granted, provided that the above
|
|
15 copyright notice and this permission notice appear in all copies.
|
|
16
|
|
17 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
18 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
19 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
20 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
21 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
22 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
23 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
24
|
|
25 */
|
|
26
|
|
27
|
|
28
|
|
29 #include "dynload.h"
|
|
30 #include "dynload_alloc.h"
|
|
31
|
|
32 #include <windows.h>
|
|
33
|
|
34 struct DLSyms_
|
|
35 {
|
|
36 DLLib* pLib;
|
|
37 const char* pBase;
|
|
38 const DWORD* pNames;
|
|
39 const DWORD* pFuncs;
|
|
40 const unsigned short* pOrds;
|
|
41 size_t count;
|
|
42 };
|
|
43
|
|
44
|
|
45 DLSyms* dlSymsInit(const char* libPath)
|
|
46 {
|
230
|
47 DLLib* pLib;
|
|
48 DLSyms* pSyms;
|
|
49 IMAGE_DOS_HEADER* pDOSHeader;
|
|
50 IMAGE_NT_HEADERS* pNTHeader;
|
|
51 IMAGE_DATA_DIRECTORY* pExportsDataDir;
|
|
52 IMAGE_EXPORT_DIRECTORY* pExports;
|
|
53 const char* base;
|
0
|
54
|
230
|
55 pLib = dlLoadLibrary(libPath);
|
|
56 if(!pLib)
|
|
57 return NULL;
|
|
58
|
|
59 base = (const char*)pLib;
|
|
60 pDOSHeader = (IMAGE_DOS_HEADER*)base;
|
|
61 pNTHeader = (IMAGE_NT_HEADERS*)(base + pDOSHeader->e_lfanew);
|
|
62 pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
|
|
63 pExports = (IMAGE_EXPORT_DIRECTORY*)(base + pExportsDataDir->VirtualAddress);
|
|
64
|
|
65 pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms));
|
0
|
66 pSyms->pBase = base;
|
|
67 pSyms->pNames = (DWORD*)(base + pExports->AddressOfNames);
|
|
68 pSyms->pFuncs = (DWORD*)(base + pExports->AddressOfFunctions);
|
|
69 pSyms->pOrds = (unsigned short*)(base + pExports->AddressOfNameOrdinals);
|
|
70 pSyms->count = (size_t)pExports->NumberOfNames;
|
|
71 pSyms->pLib = pLib;
|
|
72
|
|
73 return pSyms;
|
|
74 }
|
|
75
|
|
76
|
|
77 void dlSymsCleanup(DLSyms* pSyms)
|
|
78 {
|
|
79 if(pSyms) {
|
|
80 dlFreeLibrary(pSyms->pLib);
|
|
81 dlFreeMem(pSyms);
|
|
82 }
|
|
83 }
|
|
84
|
|
85
|
|
86 int dlSymsCount(DLSyms* pSyms)
|
|
87 {
|
|
88 return (int)pSyms->count;
|
|
89 }
|
|
90
|
|
91
|
|
92 const char* dlSymsName(DLSyms* pSyms, int index)
|
|
93 {
|
230
|
94 return pSyms->pBase + pSyms->pNames[index];
|
0
|
95 }
|
|
96
|
|
97
|
230
|
98 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value)
|
0
|
99 {
|
|
100 int i, c=dlSymsCount(pSyms);
|
|
101 for(i=0; i<c; ++i)
|
|
102 {
|
230
|
103 if((void*)(pSyms->pBase + pSyms->pFuncs[pSyms->pOrds[i]]) == value)
|
0
|
104 return dlSymsName(pSyms, i);
|
|
105 }
|
|
106
|
|
107 /* Not found. */
|
|
108 return NULL;
|
|
109 }
|
230
|
110
|