Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C function in library from assembly code

Tags:

c

assembly

nasm

I need to call some functions from different C libraries in a nasm program.

Libraries:

 <sys/ptrace.h>
 <sys/wait.h>

... and functions like ptrace, execl, wait, etc.

How to use c library function fgets in assembly language?

like image 658
Vahid Avatar asked Feb 04 '26 09:02

Vahid


1 Answers

There are generally two ways to use C/or any other HLL function from assembly program:

  1. Static linking - if you are using linker, you can link your program together with the needed HLL generated .obj or .lib file and

  2. Dynamic linking - your program links to the needed functions during the loading, not the compilation. There are two possible implementations:

    2.1. Manually load dynamic libraries and get the addresses of the needed functions. You have to use OS provided services for this. For example in Linux this is sys_uselib (pretty obsolete) or to load the library yourself and parse the ELF file for the function addresses;

    2.2. Build import table containing list of libraries and functions you want to use. Then the OS loader will automatically provide addresses of the functions in a placeholder variables from where you can call them indirectly.

All these methods are highly OS and assembler dependent, so I can provide example only for the assembler I use:

Import macros for FreshLib that build import tables for Linux.

The same for Windows

Example of use for the library "libc.so" in Linux

Example of use for the library "user32.dll" in Windows

like image 124
johnfound Avatar answered Feb 06 '26 21:02

johnfound