flat assembler
Message board for the users of flat assembler.

Index > Linux > mmap

Author
Thread Post new topic Reply to topic
moveax41h



Joined: 18 Feb 2018
Posts: 59
moveax41h
Are there any working examples of using mmap in fasm? I'd like to allocate some memory using mmap and perhaps write my own malloc. However, mmap's C code heavily relies on macros which are typically retrieved in a header file. I don't see any such macros in fasm and was wondering how I could go about "porting" this to fasm. What I mean is stuff like PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANONYMOUS, and so on.

_________________
-moveax41h
Post 04 Dec 2019, 05:15
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
If I can I avoid looking at C code to decipher things. Instead I go to the docs:

https://linux.die.net/man/2/mmap
Post 04 Dec 2019, 05:50
View user's profile Send private message Visit poster's website Reply with quote
redsock



Joined: 09 Oct 2009
Posts: 365
Location: Australia
redsock

_________________
2 Ton Digital - https://2ton.com.au/
Post 04 Dec 2019, 20:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17671
Location: In your JS exploiting you and your system
revolution
@moveax41h, note that when calling Linux system functions you place the arguments in registers and then use int 0x80.

There are also other ways to call the system with sysenter and syscall. The exact usage depends upon your CPU (AMD or Intel) and the Linux version (32 or 64 bits). But int 0x80 is the safest and works on all variants.

Some will say that using int 0x80 is not performant, and they might be correct. But unless you intend to be calling the system thousands of times per second it probably won't matter much.
Post 05 Dec 2019, 04:18
View user's profile Send private message Visit poster's website Reply with quote
st



Joined: 12 Jul 2019
Posts: 45
Location: Russia
st
fasmg itself is using mmap inside malloc since Linux kernel 5.2 changes https://github.com/tgrysztar/fasmg/blob/274b098b9d5af2058b29e70f218f9073ccf8d2c8/core/source/linux/x64/malloc.inc#L143

revolution wrote:

Some will say that using int 0x80 is not performant, and they might be correct. But unless you intend to be calling the system thousands of times per second it probably won't matter much.
Besides int 0x80 is 'a slow path' it is documented as 32-bit interface.
Code:
/*
 * 32-bit legacy system call entry.
 *
 * 32-bit x86 Linux system calls traditionally used the INT $0x80
 * instruction.  INT $0x80 lands here.
 *
 * This entry point can be used by 32-bit and 64-bit programs to perform
 * 32-bit system calls.  Instances of INT $0x80 can be found inline in
 * various programs and libraries.  It is also used by the vDSO's
 * __kernel_vsyscall fallback for hardware that doesn't support a faster
 * entry method.  Restarted 32-bit system calls also fall back to INT
 * $0x80 regardless of what instruction was originally used to do the
 * system call.
 *
 * This is considered a slow path.  It is not used by most libc
 * implementations on modern hardware except during process startup.
 *
 * Arguments:
 * eax  system call number
 * ebx  arg1
 * ecx  arg2
 * edx  arg3
 * esi  arg4
 * edi  arg5
 * ebp  arg6
 */
ENTRY(entry_INT80_compat)    

https://github.com/torvalds/linux/blob/v5.3/arch/x86/entry/entry_64_compat.S#L316-L342
Post 08 Dec 2019, 04:05
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.