📄 dlmain.c
字号:
RETAILMSG(TONE_DLL, ("__dlmap_module: 44\n"));
/* read program loadable segment */
if (read_file (hFile, (char*)(lp->p_vaddr+module->l_addr),
lp->p_filesz, lp->p_offset) != lp->p_filesz)
goto cleanup;
}
if (lp->p_memsz > lp->p_filesz)
{/* must set zero, some time this is the bss section */
memset ((void*)(module->l_addr + lp->p_vaddr + lp->p_filesz),
0, lp->p_memsz-lp->p_filesz);
}
if ((module->l_phdr == 0)&&(lp->p_offset <= ehdr->e_phoff)
&&((lp->p_filesz + lp->p_offset)
>= (ehdr->e_phoff + ehdr->e_phnum*sizeof(Elf32_Phdr))))
{
/* Found the program header in this segment. */
module->l_phdr = (Elf32_Phdr *)(ehdr->e_phoff + module->l_addr);
RETAILMSG(TONE_DLL, ("found phdr at: %x!\n", module->l_phdr));
}
VirtualProtect ((LPVOID)(lp->p_vaddr+module->l_addr), lp->p_memsz, nprotect, &oprotect);
break;
}
}
}
if ((module->l_phdr == NULL)||((Elf32_Addr)module->l_phdr < (module->l_addr + addr_start)))
{
ASSERT(0);
/* the PT_PHDR segment is not load, load it (just reuse) */
module->l_phdr = phdr;
module->l_phdr_allocated = 1;
RETAILMSG(TONE_DLL, ("__dlmap_module: >22\n"));
return module;
}
RETAILMSG(TONE_DLL, ("__dlmap_module: >11\n"));
if(!bRomModule)
__dlfree (phdr);
return module;
cleanup: /* goto cleanup must be used belowed the three resource be alloc */
VirtualFree ((LPVOID)module->l_map_start, size, MEM_DECOMMIT);
__dlfree (module);
if(!bRomModule)
__dlfree (phdr);
return NULL;
}
/* map a binary file to memory */
/*static struct __dlmodule * __dlmap_rom_module (HANDLE hFile, ElfW(Ehdr) *ehdr, ELF_INFO *pElfinfo)
{
struct __dlmodule *module;
Elf32_Phdr *phdr = NULL;
unsigned int size;
int inload = 0;
Elf32_Phdr *lp = NULL;
unsigned int i;
Elf32_Addr addr_start, addr_end = 0, addr_segstart;
size = ehdr->e_phentsize * ehdr->e_phnum;
//phdr = (Elf32_Phdr *)__dlmalloc (size);
phdr = (Elf32_Phdr *)(pElfinfo->ulFileBase + pElfinfo->ulPhdroff);
if (phdr == NULL)
return NULL;
RETAILMSG(TONE_DLL, ("phdr: %x\n", phdr));
for (lp = phdr, i = 0; i < ehdr->e_phnum; i ++, lp ++)
{
if(lp->p_vaddr == 0) //if the p_vaddr is 0, i think that is invalid phdr record...
continue;
switch (lp->p_type)
{
case PT_LOAD:
RETAILMSG(TONE_DLL, ("lp->p_vaddr: %x\n", lp->p_vaddr));
RETAILMSG(TONE_DLL, ("lp->p_offset: %x\n", lp->p_offset));
RETAILMSG(TONE_DLL, ("lp->p_align: %x\n", lp->p_align));
RETAILMSG(TONE_DLL, ("lp->p_memsz: %x\n", lp->p_memsz));
if (((lp->p_vaddr - lp->p_offset)&(lp->p_align - 1)) != 0)
{
RETAILMSG(TONE_DLL, ("loadable segment is not align!\n"));
__dlfree (phdr); // not align /
return NULL;
}
//addr_segstart = ALIGN (lp->p_vaddr, lp->p_align);
addr_segstart = lp->p_vaddr;
addr_end = addr_end > (addr_segstart + lp->p_memsz) ? addr_end : (addr_segstart + lp->p_memsz);
RETAILMSG(TONE_DLL, ("addr_segstart: %x, addr_end: %x\n", addr_segstart, addr_end));
if (inload == 0)
{ //* this is the first loadable phdr /
addr_start = addr_segstart;
//* addr_fstart = lp->p_offset; /
inload = 1;
}
break;
}
}
//* alloc module info memory /
module = (struct __dlmodule *) __dlmalloc (sizeof (struct __dlmodule));
if (module == NULL)
{
RETAILMSG(TONE_DLL, ("alloc memory for module info failure !\n"));
__dlfree (phdr);
return NULL;
}
memset (module, 0, sizeof(struct __dlmodule));
module->l_phnum = ehdr->e_phnum;
#ifdef WIN32_TEST
//* on win32 platform, virtual meory must align 64k size,
//this just for test on win32 platform /
addr_start = ALIGN(addr_start,ALIGN_SIZE);
#endif //* WIN32_TEST /
size = addr_end - addr_start;
RETAILMSG(TONE_DLL, ("size: %x\n", size));
if (ehdr->e_type == ET_EXEC)
{ //* execute program must load at fix address /
// module->l_map_start = (ElfW(Addr))VirtualAlloc ((LPVOID)addr_start, size,
// MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
module->l_map_start = (ElfW(Addr))VirtualAlloc ((LPVOID)addr_start, size,
MEM_RESERVE, PAGE_NOCACHE|PAGE_EXECUTE_READWRITE);
}
else
{
// module->l_map_start = (ElfW(Addr))VirtualAlloc (NULL, size,
// MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
module->l_map_start = (ElfW(Addr))VirtualAlloc (NULL, size,
MEM_RESERVE, PAGE_NOCACHE|PAGE_EXECUTE_READWRITE);
}
RETAILMSG(TONE_DLL, ("module->l_map_start: %x\n", module->l_map_start));
if ((module->l_map_start == 0)
||((module->l_map_start != addr_start)&&(ehdr->e_type == ET_EXEC)))
{
RETAILMSG(TONE_DLL, ("virtual alloc failure !\n"));
__dlfree (module);
__dlfree (phdr);
return NULL;
}
module->l_map_end = module->l_map_start + size;
module->l_addr = module->l_map_start - addr_start; //* in execute program this must be zero /
module->l_entry = ehdr->e_entry + module->l_addr;
RETAILMSG(TONE_DLL, ("module->l_map_end: %x\n", module->l_map_end));
RETAILMSG(TONE_DLL, ("module->l_addr: %x\n", module->l_addr));
RETAILMSG(TONE_DLL, ("module->l_entry: %x\n", module->l_entry));
for (lp = phdr, i = 0; i < ehdr->e_phnum; i ++, lp ++)
{
if(lp->p_vaddr == 0) //if the p_vaddr is 0, i think that is invalid phdr record...
continue;
switch (lp->p_type)
{
case PT_PHDR:
module->l_phdr = (ElfW(Phdr) *)(lp->p_vaddr + module->l_addr);
break;
case PT_DYNAMIC:
module->l_ld = (ElfW(Dyn) *)(lp->p_vaddr + module->l_addr);
module->l_ldnum = lp->p_memsz / sizeof (ElfW(Dyn));
break;
case PT_LOAD:
//* read program loadable segment /
// if (read_file (hFile, (char*)(lp->p_vaddr+module->l_addr),
// lp->p_filesz, lp->p_offset) != lp->p_filesz)
// goto cleanup;
{
unsigned int nprotect, oprotect;
if (lp->p_flags & PF_R)
nprotect = PAGE_READONLY;
if (lp->p_flags & PF_W)
nprotect = PAGE_READWRITE; //PAGE_WRITECOPY
if (lp->p_flags & PF_X)
nprotect = PAGE_EXECUTE_READWRITE; //PAGE_EXECUTE_WRITECOPY
nprotect |= PAGE_NOCACHE ;
RETAILMSG(TONE_DLL, ("call virtualcopy\n"));
if(!VirtualCopy((LPVOID)module->l_map_start, (LPVOID)(pElfinfo->ulFileBase), lp->p_memsz, nprotect)){
RETAILMSG(TONE_DLL, ("virtualcopy fail@\n"));
goto cleanup;
}
}
if (lp->p_memsz > lp->p_filesz)
{//* must set zero, some time this is the bss section /
memset ((void*)(module->l_addr + lp->p_vaddr + lp->p_filesz),
0, lp->p_memsz-lp->p_filesz);
}
if ((module->l_phdr == 0)&&(lp->p_offset <= ehdr->e_phoff)
&&((lp->p_filesz + lp->p_offset)
>= (ehdr->e_phoff + ehdr->e_phnum*sizeof(Elf32_Phdr))))
{
//* Found the program header in this segment. /
//module->l_phdr = (Elf32_Phdr *)(ehdr->e_phoff + module->l_addr);
module->l_phdr = (Elf32_Phdr *)(ehdr->e_phoff + module->l_addr + addr_start);
RETAILMSG(TONE_DLL, ("found phdr at: %x!\n", module->l_phdr));
}
{//* set the segment protect /
unsigned int nprotect, oprotect;
if (lp->p_flags & PF_R)
nprotect = PAGE_READONLY;
if (lp->p_flags & PF_W)
nprotect = PAGE_READWRITE; //PAGE_WRITECOPY
if (lp->p_flags & PF_X)
nprotect = PAGE_EXECUTE_READWRITE; //PAGE_EXECUTE_WRITECOPY
// VirtualProtect ((LPVOID)(lp->p_vaddr+module->l_addr), lp->p_memsz, nprotect, &oprotect);
}
break;
}
}
RETAILMSG(TONE_DLL, ("call virtualcopy ok!\n"));
if ((module->l_phdr == NULL)||((Elf32_Addr)module->l_phdr < (module->l_addr + addr_start)))
{
//* the PT_PHDR segment is not load, load it (just reuse) /
RETAILMSG(TONE_DLL, ("--PT_PHDR is not load !\n"));
//if run to here , it have error! because the PT_PHDR must be part of PT_LOAD section.
ASSERT(0);
module->l_phdr = phdr;
module->l_phdr_allocated = 1;
return module;
}
//__dlfree (phdr);
return module;
cleanup: //* goto cleanup must be used belowed the three resource be alloc /
VirtualFree ((LPVOID)module->l_map_start, size, MEM_DECOMMIT);
__dlfree (module);
//__dlfree (phdr);
return NULL;
}*/
// 2003-06-27 -add by ln
LPVOID WINAPI KL_OpenDllModule( LPCTSTR lpcszName, UINT uiMode, LPVOID lpvCaller )
{
return NULL;
}
/* // 2003-06-27 -del by ln
struct __dlmodule* KL_OpenDllModule (const char * name, unsigned int mode,
struct __dlmodule *caller)
{
return NULL;
}
*/
// 2003-06-27 -add by ln
int WINAPI KL_OpenDllDependence( LPVOID lpvModule, UINT uiMode )
{
return 0;
}
/* // 2003-06-27 -del by ln
int KL_OpenDllDependence (struct __dlmodule* module, unsigned int mode)
{
return 0;
}
*/
/* open a module by the name */
static struct __dlmodule* __dlopen_module (const char * name, unsigned int mode,
struct __dlmodule *caller,PMODULE_HANDLE lpHandle)
{
struct __dlmodule *module;
struct __dlmodule *dl;
char *file;
char *find;
HANDLE hFile;
Elf32_Ehdr ehdr;
unsigned int read;
DWORD dwFileAttr;
//EXE_ROM *pExeInfo;
UINT uFileType;
DWORD dwReturn;
ELF_INFO elfinfo;
//unsigned long romfileBase;
//unsigned long romfileType;
//unsigned long romfileEntry;
//unsigned long romphnum = 0;
//RETAILMSG(TONE_TEST, ("__dlopen_module: 11\n"));
//puthex_virtual(0xffff0001);
__dlassert (name != NULL);
RETAILMSG(TONE_DLL, ("--open module : %s \n", name));
if (! (mode & RTLD_LOAD))
return NULL; /* some stupid user */
file = __dlfind_module (name, caller);
if (file == NULL){
RETAILMSG(TONE_ERROR, ("No find module : %s \n", file));
return NULL; /* can't find the file */
}
RETAILMSG(TONE_DLL, ("--find module : %s \n", file));
RETAILMSG(TONE_DLL, ("--find module : %x, %x \n", __dl_loaded,__dl_nloaded));
//add code to here for check if the module has loaded before...
//....
//....
if (__dl_loaded != NULL) /* search loaded module first */
{
find = strrchr (file, '\\');
find = find == NULL ? file : find + 1;
dl = __dl_loaded;
while (dl != NULL)// && dl->l_next != NULL)
{
RETAILMSG(TONE_DLL, ("--open module: -->2001\n"));
if (dl->l_name != NULL) /* the execute name maybe null, take care it */
{
RETAILMSG(TONE_DLL, ("--open module: %s:%s\n", dl->l_name, find ));
if (strcmp (find, dl->l_name) == 0){
RETAILMSG(TONE_DLL, ("--open module: the module be loaded before...\n"));
return dl; /* find the module loaded, return it */
}
}
dl = dl->l_next;
RETAILMSG(TONE_DLL, ("--open module: %x\n", dl ));
}
}
RETAILMSG(TONE_DLL, ("--open module -->111\n"));
//hFile = CreateFile (file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL,
hFile = CreateFile (file, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
RETAILMSG(TONE_ERROR, ("open the module file error !\n"));
return NULL;
}
dwFileAttr = GetFileAttributes(file);
if (dwFileAttr == (DWORD)-1)
return 0;
RETAILMSG(TONE_DLL, ("-- file attr:%x !\n", dwFileAttr));
/* if(dwFileAttr & FILE_ATTR_ROMMODULE)
{
uFileType = ROM_MODULE;
}else if(dwFileAttr & FILE_ATTR_ROMFILE)
{
uFileType = ROM_FILE;
}else{
uFileType = RAM_FILE;
}
*/
if(dwFileAttr & FILE_ATTRIBUTE_ROMMODULE )
{
uFileType = XIP_MODULE;
}else{
uFileType = NON_XIP_MODULE;
}
//load rom file...
if(uFileType == XIP_MODULE){
RETAILMSG(TONE_DLL, ("deviceiocontrol: %x!\r\n", &elfinfo));
if(!DeviceIoControl(hFile, IOCTL_ROM_GET_ELF_INFO, NULL, 0, &elfinfo, 0, &dwReturn, NULL))
return 0;
RETAILMSG(TONE_DLL, ("deviceiocontrol ok!\r\n"));
//romfileBase = elfinfo.ulFileBase;
//romfileType = elfinfo.ulFileType;
//romfileEntry = elfinfo.ulEntry;
//romphnum = elfinfo.ulPhdrnum;
//phdr = (Elf32_Phdr *)(elfinfo.ulPhdroff + romfileBase);
memcpy((void*)&ehdr, (void*)elfinfo.ulFileBase, sizeof(Elf32_Ehdr));
RETAILMSG(TONE_DLL, ("base:%x, type:%x, entry:%x, phnum:%x, phdr:%x!\r\n",
elfinfo.ulFileBase, elfinfo.ulFileType, elfinfo.ulEntry,elfinfo.ulPhdrnum, elfinfo.ulPhdroff));
if (__dlcheck_elf (&ehdr) == 0)
{
RETAILMSG(TONE_ERROR, ("check elf hdr failure !\n"));
CloseHandle (hFile);
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -