flat assembler
Message board for the users of flat assembler.

Index > Windows > Cant link to .dll generated with flat assembler (vs2010)

Author
Thread Post new topic Reply to topic
fredlllll



Joined: 17 Apr 2013
Posts: 56
fredlllll 17 Apr 2013, 23:39
Hi there

Im trying to dynamically link to a .dll made with flat assembler (because neither c or c++ give me the freedom to do these bad things, like pushing something on the stack but not popping it again) and im using visualstudio 2010 for that.

so i dont find a way to import the dll into my code in visual studio.
i tried it with this here:
Code:
#define DllImport   __declspec( dllimport )
#pragma comment(lib, "test.dll")
extern "C"{
        DllImport void Start(void);
}    

but that doesnt work. visual studio tells me "LNK4003: invalid library format, ignoring library" and therefore i cant find Start();
i saw samples using a .lib instead of .dll, but i dont know how to get a lib out of flat assembler

here is my assembler code for testing:
Code:
format PE GUI 4.0 DLL
include 'win32a.inc'

section '.text' code readable executable
start:
mov eax,2

section '.edata' export data readable
export 'test.DLL',start,'Start'
    

i can see the exports with pe-explorer(something like dependency walker)

i hope someone can help me with this.
additional info:
even though im trying this on windows at the moment, i will(want to) use similar methods on linux later (using gcc/g++) so if you know how it works on linux that would be great. also, if this works with mingw, tell me. i also have this, but didnt have time yet to check it out.
Post 17 Apr 2013, 23:39
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20530
Location: In your JS exploiting you and your system
revolution 17 Apr 2013, 23:57
What is the naming convention for "C" DLLs. Do they use a leading underscore and/or a trailing parameter size?

Something like: _Start@0

BTW: fasm does not generate any of the proprietary lib formats.
Post 17 Apr 2013, 23:57
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 18 Apr 2013, 00:00
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:02; edited 1 time in total
Post 18 Apr 2013, 00:00
View user's profile Send private message Reply with quote
fredlllll



Joined: 17 Apr 2013
Posts: 56
fredlllll 18 Apr 2013, 00:00
a C export just gives me the name of the function. no param list or size.
but this doesnt matter now, because visual studio just ignores my .dll so this is the problem now
Post 18 Apr 2013, 00:00
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 18 Apr 2013, 00:04
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:02; edited 2 times in total
Post 18 Apr 2013, 00:04
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20530
Location: In your JS exploiting you and your system
revolution 18 Apr 2013, 00:06
You will need to import a DLL, not a lib file. Perhaps the VS documentation will give you the answer?
Post 18 Apr 2013, 00:06
View user's profile Send private message Visit poster's website Reply with quote
fredlllll



Joined: 17 Apr 2013
Posts: 56
fredlllll 18 Apr 2013, 00:24
@HaHaAnonymous: yeah i think that vs is the problem, but i want to know why??
and yes you are right... added a "retn" =)

@revolution
VS documentation doesnt give me much =/
it seems that i just have to recreate the dll in vs, generate an import lib file and use this file (yeah that should work, but i wanted to avoid that way)
Post 18 Apr 2013, 00:24
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 18 Apr 2013, 01:03
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:01; edited 1 time in total
Post 18 Apr 2013, 01:03
View user's profile Send private message Reply with quote
Walter



Joined: 26 Jan 2013
Posts: 155
Walter 18 Apr 2013, 05:07
Something that I tried. Maybe works here.

Create your dll with fasm.
Code:
format PE GUI 4.0 DLL
entry DllMain

include 'win32a.inc'

section '.code' code readable executable

proc DllMain, hinstDll, fdwReason, lpvReserved
    mov     eax, TRUE
    ret
endp
        
proc Ret42
    mov     eax, 42
    ret
endp

section '.edata' export data readable
  export  'test.dll',\
           Ret42, 'Ret42'

section '.reloc' data readable discardable
  data fixups
  end data
  dd ?
    

Note that this has an entry point and relocs.

I think the dll example that comes with fasm is incomplete
since the bug for relocs was fixed. Maybe someone that
is smart around here can confirm this.

Create a def file.

test.def

Code:
LIBRARY test
EXPORTS 
  "_Ret42@0"
    

Get def2lib tool.


Run:

def2lib test.def

Write a C source with project that uses this new lib.
Code:
#include <stdio.h>

int __declspec(dllimport) __stdcall Ret42(void); 

int main(int argc, char *argv[])
{
    printf("Answer: %i.\n", Ret42());
    return 0;
}
    

Works for me. Maybe for you?
Post 18 Apr 2013, 05:07
View user's profile Send private message Reply with quote
fredlllll



Joined: 17 Apr 2013
Posts: 56
fredlllll 18 Apr 2013, 08:37
okay i did following:
make a new project containing and exporting the same functions as the asm exports.
then building this, which generates a test.lib. then link to this using #pragma comment(lib,"test.lib") and later replacing that mockup dll with the real dll from flatassembler.

i had to take that reloc part into my code. didnt run without it. but it doesnt need an entry point to work.

asm:
Code:
format PE GUI 4.0 DLL
include 'win32a.inc'

section '.text' code readable executable
start:
mov eax,2
retn

section '.edata' export data readable
export 'test.DLL',\
         start,'Start'

section '.reloc' data readable discardable
  data fixups 
  end data 
  dd ?    

c-header:
Code:
#define DllImport   __declspec( dllimport )
//test.lib = lib generated with the mockup project
#pragma comment(lib, "test.lib")
extern "C"{ //extern C so we dont have to mangle the name in flat assembler.
        DllImport int Start();
}    


and now replace the mockup dll with the assembler one and yay it works

thanks for your help
Post 18 Apr 2013, 08:37
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20530
Location: In your JS exploiting you and your system
revolution 18 Apr 2013, 08:46
fredlllll wrote:
... but it doesnt need an entry point to work.
I think you will find that you code is not properly "working". Your entry point is set by fasm to be the first instruction at start. This gets executed when the dll is loaded and your function returns 2 and is interpreted as TRUE, but the problem is your retn does not restore the stack properly. Sometimes these stack problems are "fixed" by the OS when the caller function itself returns but this is not guaranteed and should not be relied upon. Ideally you should have a separate initialisation function that correctly uses retn 12 to keep everything nice and proper.
Post 18 Apr 2013, 08:46
View user's profile Send private message Visit poster's website Reply with quote
fredlllll



Joined: 17 Apr 2013
Posts: 56
fredlllll 18 Apr 2013, 09:22
why doesnt it restore the stack properly??
Post 18 Apr 2013, 09:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20530
Location: In your JS exploiting you and your system
revolution 18 Apr 2013, 09:28
Windows calls the entry point ("start" in your case) with three parameters pushed into the stack. Since Windows uses sdtcall convention it is the called function that is supposed to pop those parameters off the stack during return. Hence the requirement for retn 12.

Also note that declaring your functions as "C" in VS will mean that your must write those functions to use the ccall convention. So your entry point function, called by Windows, must be stdcall compliant. And the exported functions, called by the VS code, must be ccall compliant. In the code above your "start" function gets called both by Windows and by the VS code and each caller expects a different calling convention.
Post 18 Apr 2013, 09:28
View user's profile Send private message Visit poster's website Reply with quote
fredlllll



Joined: 17 Apr 2013
Posts: 56
fredlllll 18 Apr 2013, 09:32
ohh okay.
popping 3 elements from stack, got it
and retn increments the stack pointer by 12?
and what are the 3 things pushed to the stack by windows (im new to x86 asm)
Post 18 Apr 2013, 09:32
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2909
Location: 0x77760000
typedef 18 Apr 2013, 10:41
fredlllll wrote:
ohh okay.
popping 3 elements from stack, got it
and retn increments the stack pointer by 12?
and what are the 3 things pushed to the stack by windows (im new to x86 asm)


Hello. Welcome, read this and learn some more !!

http://stackoverflow.com/questions/3699283/what-is-stack-frame-in-assembly

http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames
Post 18 Apr 2013, 10:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20530
Location: In your JS exploiting you and your system
revolution 18 Apr 2013, 10:57
fredlllll wrote:
and what are the 3 things pushed to the stack by windows (im new to x86 asm)
Walter wrote:
Code:
proc DllMain, hinstDll, fdwReason, lpvReserved    
Post 18 Apr 2013, 10:57
View user's profile Send private message Visit poster's website Reply with quote
fredlllll



Joined: 17 Apr 2013
Posts: 56
fredlllll 18 Apr 2013, 11:32
okay thx. now i know how the calling works
revolution wrote:
Walter wrote:
Code:
proc DllMain, hinstDll, fdwReason, lpvReserved    

but this doesnt tell me anything Shocked

/edit: oops
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
that explains that
thx
Post 18 Apr 2013, 11:32
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.