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