flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Using libc in assembly

Author
Thread Post new topic Reply to topic
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 15 Dec 2006, 16:02
This example demonstrates how to write portable 32bit app in assembly, by using libc (standard C library).


Description:
Download
Filename: flibc.zip
Filesize: 34.15 KB
Downloaded: 10411 Time(s)

Post 15 Dec 2006, 16:02
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
ChaperonNoir



Joined: 22 Feb 2007
Posts: 1
ChaperonNoir 03 Jan 2008, 03:33
Learned quite a few things here about FASM, thank you.
Nice example code...
Post 03 Jan 2008, 03:33
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jun 2012, 05:47
wanna ask,
usually we include do import like this
Code:
section '.idata' import data readable writeable
library kernel32,       'kernel32.dll',\
         user32, 'user32.dll',\
           msvcrt, 'msvcrt.dll'

          include 'API\KERNEL32.INC'
               include 'API\USER32.INC'
         include 'API\MSVCRT.INC'
    


i see you do import in flibcw.asm like this,
Code:
; import functions from libc
extrn '__errno'  as _errno
extrn '_printf' as printf
extrn '_puts'        as puts
extrn '_scanf'     as scanf
extrn '_exit'     as exit
    


does it means, we need to convert every those functions inside KERNEL.INC file to

extrn 'ExitProcess' as ExitProcess

then use stdcall instead of invoke?

maybe an example of combination libc with win32 api?

thank you vid
Post 11 Jun 2012, 05:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Jun 2012, 08:23
sleepsleep wrote:
does it means, we need to convert every those functions inside KERNEL.INC file to

extrn 'ExitProcess' as ExitProcess

then use stdcall instead of invoke?
No. 'extrn' is for the linker. If you start using the Windows API then the code is not portable anymore and you might as well just use the normal PE file imports.
Post 11 Jun 2012, 08:23
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jun 2012, 09:35
i was thinking about assemble asm file to obj and do static link with windows lib or mingw .a file.

so, how to use invoke windowsapi inside the MS COFF file?
Post 11 Jun 2012, 09:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Jun 2012, 09:46
Did you check the "examples" folder?
Code:
; example of making Win32 COFF object file

format MS COFF

extrn '__imp__MessageBoxA@16' as MessageBox:dword

section '.text' code readable executable

 public _demo

 _demo:
  push    0
   push    _caption
    push    _message
    push    0
   call    [MessageBox]
        ret

section '.data' data readable writeable

 _caption db 'Win32 assembly',0
 _message db 'Coffee time!',0    
Post 11 Jun 2012, 09:46
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jun 2012, 10:04
hi, revolution,

but where could i get the
Code:
extrn '__imp__MessageBoxA@16' as MessageBox:dword 
    

? (does it means i need to convert everything to extrn with param count?

and how could i use include 'win32ax.inc' ?

please forgive me for my confusion.
Post 11 Jun 2012, 10:04
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Jun 2012, 10:14
sleepsleep wrote:
but where could i get the
Code:
extrn '__imp__MessageBoxA@16' as MessageBox:dword 
    

? (does it means i need to convert everything to extrn with param count?
Yes and no. You will have to match the external label name to the library you link to. Find you how your library names things.

Note that each library is different. Some will have different names. You can't simply assume that all the libraries out there for use with HLL's are the same.


Last edited by revolution on 11 Jun 2012, 10:40; edited 1 time in total
Post 11 Jun 2012, 10:14
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jun 2012, 10:32
so, what tool could help me to extract the external labels name from .lib file and .a file?
Post 11 Jun 2012, 10:32
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jun 2012, 11:05
ok, i got some clue after using google,
i could use nm.exe (mingw32) tools to extract the .a information

but the output need more process.

nm -g libkernel32.a
Code:
dchdt.o:
00000000 I __libkernel32_a_iname

dchdh.o:
00000000 I __head_libkernel32_a
         U __libkernel32_a_iname

dchds01308.o:
         U __head_libkernel32_a
00000000 I __imp__lstrlenW@4
00000000 T _lstrlenW@4

dchds01307.o:
         U __head_libkernel32_a
00000000 I __imp__lstrlenA@4
00000000 T _lstrlenA@4

dchds01306.o:
         U __head_libkernel32_a
00000000 I __imp__lstrlen@4
00000000 T _lstrlen@4

dchds01305.o:
         U __head_libkernel32_a
00000000 I __imp__lstrcpynW@12
00000000 T _lstrcpynW@12
    
Post 11 Jun 2012, 11:05
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jun 2012, 12:21
idk where the faults.

i tried to link the example file but to no avail.
Code:
D:\Fasm\EXAMPLES\MSCOFF>ld -v -b pe-i386 -e "_demo" -L D:/MinGW/lib -lcrtdll -lm
svcrt -lkernel32 -luser32 MSCOFF.OBJ -o MSCOFF.EXE
GNU ld (GNU Binutils) 2.22
MSCOFF.OBJSad.text+0x10): undefined reference to `_imp__MessageBoxA@16'
    

notice the shorten one underscore `_imp__MessageBoxA@16'
compare the one we import
extrn '__imp__MessageBoxA@16' as MessageBox:dword
Post 11 Jun 2012, 12:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Jun 2012, 12:33
Try the shorter version: _MessageBoxA@16
Post 11 Jun 2012, 12:33
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 11 Jun 2012, 12:41
no luck still
Code:
D:\Fasm\EXAMPLES\MSCOFF>fasm MSCOFF.ASM
flat assembler  version 1.70.01  (1572863 kilobytes memory)
3 passes, 271 bytes.

D:\Fasm\EXAMPLES\MSCOFF>ld -v -b pe-i386 -e "_demo" -L D:/MinGW/lib -lkernel32 -luser32 MSCOFF.OBJ -o MSCOFF.EXE
GNU ld (GNU Binutils) 2.22
MSCOFF.OBJSad.text+0x10): undefined reference to `MessageBoxA@16'
    


i tried the following, maybe the way how to use ld.exe is the culprit, but afaik, i am using it correctly.

Code:
#include <windows.h> 

int WINAPI 
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nShowCmd) 
{ 
  MessageBox (NULL, "Hello World!", "hello", MB_OK | MB_ICONINFORMATION); 
  return 0; 
}
    

gcc a.c
resulted functional windows program

but if i gcc -c a.c
then i do
D:\Fasm\EXAMPLES\MSCOFF>ld -v -b pe-i386 -L D:/MinGW/lib -lkernel32 -luser32 A.O -o A.EXE
GNU ld (GNU Binutils) 2.22
A.O:a.cSad.text+0x26): undefined reference to `MessageBoxA@16'

it still results same error,
anyone know how to use this ld.exe ?
Post 11 Jun 2012, 12:41
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 12736
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 0010456
sleepsleep 12 Jun 2012, 16:36
i figure out what the problem.

it just kinda stupid, damn it, i wanna blame the ld.exe but i guess whatever.

Code:
D:\Fasm\EXAMPLES\MSCOFF>ld --help
Usage: ld [options] file...
Options:
    


the usage shows, ld [options] then only file... but it doesn't seem so.

Code:
ld -v --subsystem windows -L D:/MinGW/lib -luser32 a.o

will result
a.o:a.cSad.text+0x26): undefined reference to `MessageBoxA@16'
    


but

Code:
ld -v --subsystem windows a.o -L D:/MinGW/lib -luser32
will works
    

the --subsystem windows just to prevent the console pop up.

i find it weird, but who am i to say anything.

btw, the correct import is

Code:
extrn '__imp__MessageBoxA@16' as MessageBox:dword
    
Post 12 Jun 2012, 16:36
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.