comparison dynload/dynload_syms_pe.c @ 230:6c7591cef6a8

- dynload_syms_pe.c cleanup
author Tassilo Philipp
date Sun, 16 Apr 2017 16:17:47 +0200
parents 3e629dc19168
children f5577f6bf97a
comparison
equal deleted inserted replaced
229:0ce6beba55df 230:6c7591cef6a8
1 /* 1 /*
2 2
3 Package: dyncall 3 Package: dyncall
4 Library: dynload 4 Library: dynload
5 File: dynload/dynload_syms_pe.c 5 File: dynload/dynload_syms_pe.c
6 Description: 6 Description:
7 License: 7 License:
8 8
9 Copyright (c) 2007-2015 Daniel Adler <dadler@uni-goettingen.de>, 9 Copyright (c) 2007-2017 Daniel Adler <dadler@uni-goettingen.de>,
10 Tassilo Philipp <tphilipp@potion-studios.com> 10 Tassilo Philipp <tphilipp@potion-studios.com>
11 Olivier Chafik <olivier.chafik@gmail.com> 11 Olivier Chafik <olivier.chafik@gmail.com>
12 12
13 Permission to use, copy, modify, and distribute this software for any 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 14 purpose with or without fee is hereby granted, provided that the above
29 #include "dynload.h" 29 #include "dynload.h"
30 #include "dynload_alloc.h" 30 #include "dynload_alloc.h"
31 31
32 #include <windows.h> 32 #include <windows.h>
33 33
34 struct DLLib_
35 {
36 IMAGE_DOS_HEADER dos_header;
37 };
38
39
40 struct DLSyms_ 34 struct DLSyms_
41 { 35 {
42 DLLib* pLib; 36 DLLib* pLib;
43 const char* pBase; 37 const char* pBase;
44 const DWORD* pNames; 38 const DWORD* pNames;
48 }; 42 };
49 43
50 44
51 DLSyms* dlSymsInit(const char* libPath) 45 DLSyms* dlSymsInit(const char* libPath)
52 { 46 {
53 DLLib* pLib = dlLoadLibrary(libPath); 47 DLLib* pLib;
54 DLSyms* pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); 48 DLSyms* pSyms;
55 const char* base = (const char*) pLib; 49 IMAGE_DOS_HEADER* pDOSHeader;
56 IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*) base; 50 IMAGE_NT_HEADERS* pNTHeader;
57 IMAGE_NT_HEADERS* pNTHeader = (IMAGE_NT_HEADERS*) ( base + pDOSHeader->e_lfanew ); 51 IMAGE_DATA_DIRECTORY* pExportsDataDir;
58 IMAGE_DATA_DIRECTORY* pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; 52 IMAGE_EXPORT_DIRECTORY* pExports;
59 IMAGE_EXPORT_DIRECTORY* pExports = (IMAGE_EXPORT_DIRECTORY*) (base + pExportsDataDir->VirtualAddress); 53 const char* base;
60 54
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));
61 pSyms->pBase = base; 66 pSyms->pBase = base;
62 pSyms->pNames = (DWORD*)(base + pExports->AddressOfNames); 67 pSyms->pNames = (DWORD*)(base + pExports->AddressOfNames);
63 pSyms->pFuncs = (DWORD*)(base + pExports->AddressOfFunctions); 68 pSyms->pFuncs = (DWORD*)(base + pExports->AddressOfFunctions);
64 pSyms->pOrds = (unsigned short*)(base + pExports->AddressOfNameOrdinals); 69 pSyms->pOrds = (unsigned short*)(base + pExports->AddressOfNameOrdinals);
65 pSyms->count = (size_t)pExports->NumberOfNames; 70 pSyms->count = (size_t)pExports->NumberOfNames;
84 } 89 }
85 90
86 91
87 const char* dlSymsName(DLSyms* pSyms, int index) 92 const char* dlSymsName(DLSyms* pSyms, int index)
88 { 93 {
89 return (const char*)((const char*)pSyms->pBase + pSyms->pNames[index]); 94 return pSyms->pBase + pSyms->pNames[index];
90 } 95 }
91 96
92 97
93 void* dlSymsValue(DLSyms* pSyms, int index) 98 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value)
94 {
95 return (void*)(pSyms->pBase + pSyms->pFuncs[pSyms->pOrds[index]]);
96 }
97
98
99 const char* dlSymsNameFromValue(DLSyms* pSyms, void* value)
100 { 99 {
101 int i, c=dlSymsCount(pSyms); 100 int i, c=dlSymsCount(pSyms);
102 for(i=0; i<c; ++i) 101 for(i=0; i<c; ++i)
103 { 102 {
104 if(dlSymsValue(pSyms, i) == value) 103 if((void*)(pSyms->pBase + pSyms->pFuncs[pSyms->pOrds[i]]) == value)
105 return dlSymsName(pSyms, i); 104 return dlSymsName(pSyms, i);
106 } 105 }
107 106
108 /* Not found. */ 107 /* Not found. */
109 return NULL; 108 return NULL;
110 } 109 }
110