Mercurial > pub > dyncall > dyncall
comparison dynload/dynload_syms_pe.c @ 0:3e629dc19168
initial from svn dyncall-1745
author | Daniel Adler |
---|---|
date | Thu, 19 Mar 2015 22:24:28 +0100 |
parents | |
children | 6c7591cef6a8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3e629dc19168 |
---|---|
1 /* | |
2 | |
3 Package: dyncall | |
4 Library: dynload | |
5 File: dynload/dynload_syms_pe.c | |
6 Description: | |
7 License: | |
8 | |
9 Copyright (c) 2007-2015 Daniel Adler <dadler@uni-goettingen.de>, | |
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 DLLib_ | |
35 { | |
36 IMAGE_DOS_HEADER dos_header; | |
37 }; | |
38 | |
39 | |
40 struct DLSyms_ | |
41 { | |
42 DLLib* pLib; | |
43 const char* pBase; | |
44 const DWORD* pNames; | |
45 const DWORD* pFuncs; | |
46 const unsigned short* pOrds; | |
47 size_t count; | |
48 }; | |
49 | |
50 | |
51 DLSyms* dlSymsInit(const char* libPath) | |
52 { | |
53 DLLib* pLib = dlLoadLibrary(libPath); | |
54 DLSyms* pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); | |
55 const char* base = (const char*) pLib; | |
56 IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*) base; | |
57 IMAGE_NT_HEADERS* pNTHeader = (IMAGE_NT_HEADERS*) ( base + pDOSHeader->e_lfanew ); | |
58 IMAGE_DATA_DIRECTORY* pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | |
59 IMAGE_EXPORT_DIRECTORY* pExports = (IMAGE_EXPORT_DIRECTORY*) (base + pExportsDataDir->VirtualAddress); | |
60 | |
61 pSyms->pBase = base; | |
62 pSyms->pNames = (DWORD*)(base + pExports->AddressOfNames); | |
63 pSyms->pFuncs = (DWORD*)(base + pExports->AddressOfFunctions); | |
64 pSyms->pOrds = (unsigned short*)(base + pExports->AddressOfNameOrdinals); | |
65 pSyms->count = (size_t)pExports->NumberOfNames; | |
66 pSyms->pLib = pLib; | |
67 | |
68 return pSyms; | |
69 } | |
70 | |
71 | |
72 void dlSymsCleanup(DLSyms* pSyms) | |
73 { | |
74 if(pSyms) { | |
75 dlFreeLibrary(pSyms->pLib); | |
76 dlFreeMem(pSyms); | |
77 } | |
78 } | |
79 | |
80 | |
81 int dlSymsCount(DLSyms* pSyms) | |
82 { | |
83 return (int)pSyms->count; | |
84 } | |
85 | |
86 | |
87 const char* dlSymsName(DLSyms* pSyms, int index) | |
88 { | |
89 return (const char*)((const char*)pSyms->pBase + pSyms->pNames[index]); | |
90 } | |
91 | |
92 | |
93 void* dlSymsValue(DLSyms* pSyms, int index) | |
94 { | |
95 return (void*)(pSyms->pBase + pSyms->pFuncs[pSyms->pOrds[index]]); | |
96 } | |
97 | |
98 | |
99 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value) | |
100 { | |
101 int i, c=dlSymsCount(pSyms); | |
102 for(i=0; i<c; ++i) | |
103 { | |
104 if(dlSymsValue(pSyms, i) == value) | |
105 return dlSymsName(pSyms, i); | |
106 } | |
107 | |
108 /* Not found. */ | |
109 return NULL; | |
110 } |