NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: Dibya on September 10, 2017, 06:21:38 AM

Title: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 10, 2017, 06:21:38 AM
Hi guys and gals ,
How to declare dll import and export under NASM while using -fwin32 as -fobj is outdated .
I need some help . My code is perfect but i need some help regarding dll import and export .
FOr example i want to import RtlInterlockedCompareExchange64 from ntdll.dll and export MYFUNCTION1 in my own dll.
I am new here hope i will get some help .
Thanks in Advance .
As a linker I want to use either link.exe or Golink
I have used PE macro but compiled binaries are not proper enough for serious purpose ...
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: dreamCoder on September 11, 2017, 06:00:07 PM
I don't know what and how Rtl.... works but here's something that features both things. This will translate to a .DLL file and "myfunction1" is visible.

Code: [Select]
;nasm -f win32 prog.asm
;golink /dll prog.obj ntdll.dll
;(ignore the warning)

global myfunction1
export myfunction1

extern RtlInterlockedCompareExchange64

section .text

myfunction1:
        push    eax  ;just guessing the arguments.
        push    ebx
        push    ecx
        call    RtlInterlockedCompareExchange64
        ret
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 12, 2017, 03:43:10 PM
Thanks My friend You became my life savvier.
 If i have to include Kernel32 and shell32 then what should i do
golink /dll prog.obj ntdll.dll,shell32.dll,kernel32.dll is it alright to do so ?
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: dreamCoder on September 13, 2017, 05:06:18 AM
yes, but no commas.
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 13, 2017, 02:54:52 PM
Thanks a lot
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 16, 2017, 02:11:49 PM
  fdivp st qword (1),st
  push    dword [esi+34h]
  push    dword [eax+18h]
nasm is not accepting this opcodes what should i do?
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Frank Kotler on September 16, 2017, 06:00:56 PM
Use valid syntax?
Code: [Select]
fdivp st1, st0
Is that what you want? The rest of it looks okay to me...

Best,
Frank


Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 17, 2017, 05:27:17 AM
Thanks You are awesome
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 17, 2017, 02:08:55 PM
I am getting Some weird result with GoLink Please help
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: dreamCoder on September 17, 2017, 02:41:39 PM
try adding "/mix" at the end of the GoLink script.
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 18, 2017, 07:26:56 AM
Same issue please help . OBJ is ready but i am unable to get dll. :'(

which version of nasm do you use ?
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: dreamCoder on September 18, 2017, 07:37:27 AM
@Dibya

I can't offer u much help because you're not showing any code. I am working in 'guessing mode' only. If you don't want to share the entire code, just show / test the minimum layout of your code where import / export is involved. Start small and proceed slowly to reduce mistakes.
 
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Frank Kotler on September 18, 2017, 08:48:18 AM
At the risk of stating the obvious, "myfunction" is lowercase in the code shown above and uppercase in the error message... Sorry, but I'd feel like a fool if I didn't mention it...

Best,
Frank

Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 18, 2017, 03:57:16 PM
Nothing is Com pilling even simple one like this
EXPORT MYFUNCTION
GLOBAL MYFUNCTION
MYFUNCTION:
 xor eax,eax
 retn

Even i have tried code from other people who able to link properly
. Nasm Compiles with out issue but Golink fails
Even your code does not compile .
I am doing nothing just learning how to compile in NASM properly . I have been moving from MASM which became obsolete as it is not updated long .
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 18, 2017, 04:03:24 PM
Sorry Guys Its utter foolishness of mine
i have not mentioned section
section .text

; Any way is it possible to specify dll for import in ex tern . lets take if i wish to specify to another program.dll
Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: dreamCoder on September 18, 2017, 09:02:34 PM
@Dibya
Since you're not showing me your code, there's not much I can do. But I wrote a simple but classic example of creating a DLL library and linking to multiple libraries as an example and reference. You can use it to slowly extend the imports and externs one at a time to add more library as a practice and see how it goes. All of these are using GoLink as the main linker.

The library (prog.dll):
Code: [Select]
;----------------------
;Creating a DLL library with 2 services
;1. to print a null-terminated string
;2. To exit the code gracefully
;-----------------------
;nasm -f win32 prog.asm
;golink /dll prog.obj kernel32.dll msvcrt.dll
;-----------------------
global MyExit
export MyExit
global MyPrint
export MyPrint

extern _ExitProcess@4  ;from kernel32
extern printf          ;from msvcrt (C)

section .text

;To print an ASCIIZ string
;Push the address of the string
MyPrint:
        push    ebp
        mov     ebp,esp

        mov     esi,[ebp+8]
        push    esi
        call    printf
        add     esp,4   ;CDECL clean up

        mov     esp,ebp
        pop     ebp
        ret     4       ;stdcall clean up

;Exit the code
;No arguments
MyExit:
        push    0
        call    _ExitProcess@4
        ret

Now an example on how to access the library (prog.dll) AND another library from "user32.dll" just to make it more interesting;

Code: [Select]
;--------------------------------
;To access services provided by prog.dll (the lib)
;nasm -f win32 test.asm
;golink /console test.obj prog.dll user32.dll
;--------------------------------
global Start

extern MyExit           ;import from prog.dll
extern MyPrint          ;import from prog.dll
extern _MessageBoxA@16  ;import from user32.dll

section .data
hello db 'Hello World',0ah,0
msg db 'Hello',0

section .text
Start:
        push    hello
        call    MyPrint

        push    0
        push    msg
        push    msg
        push    0
        call    _MessageBoxA@16

        call    MyExit

Take it slowly and don't miss out any details.

Enjoy.

Title: Re: DLL IMPORT AND EXPORT In NASM Under -Fwin32
Post by: Dibya on September 19, 2017, 04:30:19 AM
Thanks Dreamcoder
Now i got it .
Now i can work on developing a visual studio like IDE for nasm ..
It will also help on my other projects
Thanks a lot
Edit ::
I have tried to use the dll in a c program which just call the dll .
When i used a c dll with xor eax,eax ret 4 (if error success message hello will be displayed ) my program says hello but when i use dll compiled with nasm i get badimage error and program does not work .
Edit:: I got it . I removed /mix and everything is fine .
Going some off topic
if a nasm IDE is required which features will be essential suggest me some thing i am going to work from tomorrow