简体   繁体   中英

Calling C function in library from assembly code

I need need to call some functions from different C libraries in my 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM