mmap2 函数在 asm 中写入,在 c 中调用 [英] mmap2 function write in asm, call in c
问题描述
我在 ASM AT&T 中编写 MMAP2 并在 C 中调用它时遇到问题.我写了这个,但不知道它应该如何工作.我知道代码不好,但我非常需要帮助.
你能告诉我它应该是什么样子吗?
感谢帮助!
I have problem with writing MMAP2 in ASM AT&T and call it in C. I wrote this but didn't know how should it works. I am knowingly that code is not good but I very need help.
Can you tell me how should it looks ?
Thanks for help!
.data
MMAP2 = 192
MUNMAP = 91
PROT_READ = 0x1
MAP_ANONYMOUS = 0x20
.bss
.text
.global moje_mmap
.type moje_map @function
moje_mmap:
push %ebp
mov %esp, %ebp
xor %ebx, %ebx
mov 8(%ebp), %ecx
mov $PROT_READ, %edx
mov $MAP_ANONYMOUS, %esi
mov $-1, %edi
mov $MMAP2, %eax
int $0x80
mov %ebp, %esp
pop %ebp
ret
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
void* moje_mmap(size_t dlugosc);
int main() {
moje_mmap(30);
return 0; }
推荐答案
您实际上从汇编函数中正确返回了值.-22 是来自 mmap2 的有效返回值,表示 EINVAL.当您直接从程序集中使用系统调用时,错误通常作为否定版本返回错误,例如 -EINVAL 或 -22.
You're actually returning the value correctly from your assembly function. -22 is a valid return value from mmap2, meaning EINVAL. When you use syscalls directly from assembly, errors are usually returned as the negative version of the error, for example -EINVAL or -22.
现在,至于为什么会出现错误,这里有一个摘录 来自 mmap2 手册页:
Now, as to why you're getting an error, here's an excerpt from the mmap2 man page:
EINVAL (Various platforms where the page size is not 4096 bytes.)
offset * 4096 is not a multiple of the system page size.
查看您的代码,您将 -1 作为偏移参数传递,但这是有效的,所以这不是问题.
Looking at your code, you're passing -1 as the offset parameter, but that's valid, so it's not the problem.
问题更有可能出在您的 flags
参数上:
The problem is more likely to be with your flags
parameter:
The flags argument determines whether updates to the mapping are
visible to other processes mapping the same region, and whether
updates are carried through to the underlying file. This behavior is
determined by including exactly one of the following values in flags:
MAP_SHARED Share this mapping. Updates to the mapping are visible to
other processes that map this file, and are carried
through to the underlying file. (To precisely control
when updates are carried through to the underlying file
requires the use of msync(2).)
MAP_PRIVATE
Create a private copy-on-write mapping. Updates to the
mapping are not visible to other processes mapping the
same file, and are not carried through to the underlying
file. It is unspecified whether changes made to the file
after the mmap() call are visible in the mapped region.
如此处所述,您必须在标志参数中包含 MAP_SHARED 或 MAP_PRIVATE.添加这个,你的程序应该可以工作.
As stated here, you must include either MAP_SHARED or MAP_PRIVATE in your flags parameter. Add this, and your program should work.
这篇关于mmap2 函数在 asm 中写入,在 c 中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!