OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 2:59 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: DLL issue
PostPosted: Fri May 29, 2020 10:53 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Hello,
I am making a PE DLL loader. I managed to successfully create a GetProcAddress function, but am having issues reading the import table. Here is the code:
Code:
PE_VA moduleBase = LoadDll(moduleAddr, i);
           PE_VA kernelBase = peHeader->optHeader.ImageBase;
           PIMAGE_IMPORT_DESCRIPTOR importDir = (PIMAGE_IMPORT_DESCRIPTOR)
                (peHeader->optHeader.DataDirectory[1].VirtualAddress + kernelBase);
            PIMAGE_THUNK_DATA thunk = (PIMAGE_THUNK_DATA)(importDir->OriginalFirstThunk + kernelBase);
            PIMAGE_IMPORT_BY_NAME import = (PIMAGE_IMPORT_BY_NAME)(thunk->u1.AddressOfData + kernelBase);
            PSTR name = (PSTR)(import->Name);
            DWORD proc = GetProcAddress(moduleBase, name);

Thank you for your help.

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: DLL issue
PostPosted: Fri May 29, 2020 11:51 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 402
nexos wrote:
Hello,
I am making a PE DLL loader. I managed to successfully create a GetProcAddress function, but am having issues reading the import table. Here is the code:
Code:
PE_VA moduleBase = LoadDll(moduleAddr, i);
           PE_VA kernelBase = peHeader->optHeader.ImageBase;
           PIMAGE_IMPORT_DESCRIPTOR importDir = (PIMAGE_IMPORT_DESCRIPTOR)
                (peHeader->optHeader.DataDirectory[1].VirtualAddress + kernelBase);
            PIMAGE_THUNK_DATA thunk = (PIMAGE_THUNK_DATA)(importDir->OriginalFirstThunk + kernelBase);
            PIMAGE_IMPORT_BY_NAME import = (PIMAGE_IMPORT_BY_NAME)(thunk->u1.AddressOfData + kernelBase);
            PSTR name = (PSTR)(import->Name);
            DWORD proc = GetProcAddress(moduleBase, name);

Thank you for your help.


Have you loaded the DLL at the virtual address given by peHeader->optHeader.ImageBase? As I understand it, PE executables are linked as though they're expected to be mapped at a specific memory location, and if not loaded there, they need to be relocated. Looking at your code, you're assuming moduleBase==peHeader->optHeader.ImageBase. Is that true?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 188 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group