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