peloader.c
来自「适合KS8695X」· C语言 代码 · 共 587 行 · 第 1/2 页
C
587 行
pageOffset = baseReloc->PageRVA - hMod->textBase;
numFixups = (baseReloc->BlockSize - sizeof(BASE_RELOCATION)) / sizeof(ushort);
fixup = (ushort*)(baseReloc + 1);
for (i = 0; i < numFixups; i++) {
relocType = *fixup >> 12;
if (relocType) {
offset = pageOffset + (*fixup & 0x0FFF);
*(ulong*)(hMod->text + offset) += delta;
}
fixup++;
}
/* Move to next relocation block */
baseReloc = (BASE_RELOCATION*)((ulong)baseReloc + baseReloc->BlockSize);
}
/* Initialise the C runtime library for the loaded DLL */
result = PE_unableToInitLibC;
if ((InitLibC = (InitLibC_t)PE_getProcAddress(hMod,"_InitLibC")) == NULL)
goto Error;
if (!InitLibC(&___imports,PM_getOSType()))
goto Error;
/* Clean up, close the file and return the loaded module handle */
PM_free(reloc);
result = PE_ok;
return hMod;
Error:
if (shared)
PM_freeShared(hMod);
else
PM_free(hMod);
PM_free(reloc);
return NULL;
}
/****************************************************************************
DESCRIPTION:
Loads a Portable Binary DLL into memory
HEADER:
peloader.h
PARAMETERS:
szDLLName - Name of the PE DLL library to load
shared - True to load module into shared memory
RETURNS:
Handle to loaded PE DLL, or NULL on failure.
REMARKS:
This function loads a Portable Binary DLL library from disk, relocates
the code and returns a handle to the loaded library. This function
will only work on DLL's that do not have any imports, since we don't
resolve import dependencies in this function.
SEE ALSO:
PE_getProcAddress, PE_freeLibrary
****************************************************************************/
PE_MODULE * PEAPI PE_loadLibrary(
const char *szDLLName,
ibool shared)
{
PE_MODULE *hMod;
#if (defined(__WINDOWS32__) || defined(__DRIVER__)) && defined(CHECKED)
if (!shared) {
PM_MODULE hInst;
InitLibC_t InitLibC;
/* For Win32 if are building checked libraries for debugging, we use
* the real Win32 DLL functions so that we can debug the resulting DLL
* files with the Win32 debuggers. Note that we can't do this if
* we need to load the files into a shared memory context.
*/
if ((hInst = PM_loadLibrary(szDLLName)) == NULL) {
result = PE_fileNotFound;
return NULL;
}
/* Initialise the C runtime library for the loaded DLL */
result = PE_unableToInitLibC;
if ((InitLibC = (void*)PM_getProcAddress(hInst,"_InitLibC")) == NULL)
return NULL;
if (!InitLibC(&___imports,PM_getOSType()))
return NULL;
/* Allocate the PE_MODULE structure */
if ((hMod = PM_malloc(sizeof(*hMod))) == NULL)
return NULL;
hMod->text = (void*)hInst;
hMod->shared = -1;
/* DLL loaded successfully so return module handle */
result = PE_ok;
return hMod;
}
else
#endif
{
FILE *f;
ulong size;
/* Attempt to open the file on disk */
if (shared < 0)
shared = 0;
if ((f = fopen(szDLLName,"rb")) == NULL) {
result = PE_fileNotFound;
return NULL;
}
hMod = PE_loadLibraryExt(f,0,&size,shared);
fclose(f);
return hMod;
}
}
/****************************************************************************
DESCRIPTION:
Loads a Portable Binary DLL into memory
HEADER:
peloader.h
PARAMETERS:
szDLLName - Name of the PE DLL library to load
shared - True to load module into shared memory
RETURNS:
Handle to loaded PE DLL, or NULL on failure.
REMARKS:
This function is the same as the regular PE_loadLibrary function, except
that it looks for the drivers in the MGL_ROOT/drivers directory or a
/drivers directory relative to the current directory.
SEE ALSO:
PE_loadLibraryMGL, PE_getProcAddress, PE_freeLibrary
****************************************************************************/
PE_MODULE * PEAPI PE_loadLibraryMGL(
const char *szDLLName,
ibool shared)
{
#if !defined(__WIN32_VXD__) && !defined(__NT_DRIVER__)
PE_MODULE *hMod;
#endif
char path[256] = "";
/* We look in the 'drivers' directory, optionally under the MGL_ROOT
* environment variable directory.
*/
#if !defined(__WIN32_VXD__) && !defined(__NT_DRIVER__)
if (getenv("MGL_ROOT")) {
strcpy(path,getenv("MGL_ROOT"));
PM_backslash(path);
}
strcat(path,"drivers");
PM_backslash(path);
strcat(path,szDLLName);
if ((hMod = PE_loadLibrary(path,shared)) != NULL)
return hMod;
#endif
strcpy(path,"drivers");
PM_backslash(path);
strcat(path,szDLLName);
return PE_loadLibrary(path,shared);
}
/****************************************************************************
DESCRIPTION:
Gets a function address from a Portable Binary DLL
HEADER:
peloader.h
PARAMETERS:
hModule - Handle to a loaded PE DLL library
szProcName - Name of the function to get the address of
RETURNS:
Pointer to the function, or NULL on failure.
REMARKS:
This function searches for the named, exported function in a loaded PE
DLL library, and returns the address of the function. If the function is
not found in the library, this function return NULL.
SEE ALSO:
PE_loadLibrary, PE_freeLibrary
****************************************************************************/
void * PEAPI PE_getProcAddress(
PE_MODULE *hModule,
const char *szProcName)
{
#if (defined(__WINDOWS32__) || defined(__DRIVER__)) && defined(CHECKED)
if (hModule->shared == -1)
return (void*)PM_getProcAddress(hModule->text,szProcName);
else
#endif
{
uint i;
EXPORT_DIRECTORY *exports;
ulong funcOffset;
ulong *AddressTable;
ulong *NameTable;
ushort *OrdinalTable;
char *name;
/* Find the address of the export tables from the export section */
if (!hModule)
return NULL;
exports = (EXPORT_DIRECTORY*)(hModule->export + hModule->exportDir);
AddressTable = (ulong*)(hModule->export + exports->AddressTableRVA - hModule->exportBase);
NameTable = (ulong*)(hModule->export + exports->NameTableRVA - hModule->exportBase);
OrdinalTable = (ushort*)(hModule->export + exports->OrdinalTableRVA - hModule->exportBase);
/* Search the export name table to find the function name */
for (i = 0; i < exports->NumberOfNamePointers; i++) {
name = (char*)(hModule->export + NameTable[i] - hModule->exportBase);
if (strcmp(name,szProcName) == 0)
break;
}
if (i == exports->NumberOfNamePointers)
return NULL;
funcOffset = AddressTable[OrdinalTable[i]];
if (!funcOffset)
return NULL;
return (void*)(hModule->text + funcOffset - hModule->textBase);
}
}
/****************************************************************************
DESCRIPTION:
Frees a loaded Portable Binary DLL
HEADER:
peloader.h
PARAMETERS:
hModule - Handle to a loaded PE DLL library to free
REMARKS:
This function frees a loaded PE DLL library from memory.
SEE ALSO:
PE_getProcAddress, PE_loadLibrary
****************************************************************************/
void PEAPI PE_freeLibrary(
PE_MODULE *hModule)
{
TerminateLibC_t TerminateLibC;
#if (defined(__WINDOWS32__) || defined(__DRIVER__)) && defined(CHECKED)
if (hModule->shared == -1) {
/* Run the C runtime library exit code on module unload */
if ((TerminateLibC = (TerminateLibC_t)PM_getProcAddress(hModule->text,"_TerminateLibC")) != NULL)
TerminateLibC();
PM_freeLibrary(hModule->text);
PM_free(hModule);
}
else
#endif
{
if (hModule) {
/* Run the C runtime library exit code on module unload */
if ((TerminateLibC = (TerminateLibC_t)PE_getProcAddress(hModule,"_TerminateLibC")) != NULL)
TerminateLibC();
if (hModule->shared)
PM_freeShared(hModule);
else
PM_free(hModule);
}
}
}
/****************************************************************************
DESCRIPTION:
Returns the error code for the last operation
HEADER:
peloader.h
RETURNS:
Error code for the last operation.
SEE ALSO:
PE_getProcAddress, PE_loadLibrary
****************************************************************************/
int PEAPI PE_getError(void)
{
return result;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?