繁体   English   中英

mmap2函数在asm中写入,在c中调用

[英]mmap2 function write in asm, call in c

我在ASM AT&T中编写MMAP2并在C中调用时遇到了问题。我编写了此文件,但不知道它应该如何工作。 我知道代码不好,但是我非常需要帮助。
你能告诉我它看起来如何吗?
感谢帮助!

.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。 当直接从程序集使用syscall时, 错误通常以错误的否定形式返回 ,例如-EINVAL或-22。

现在,关于为什么会出错,这是mmap2手册页的摘录:

   EINVAL (Various platforms where the page size is not 4096 bytes.)
          offset * 4096 is not a multiple of the system page size.

查看您的代码,您将传递-1作为offset参数,但这是有效的,所以这不是问题。

问题很可能与您的flags参数有关:

   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.

如此处所述,您必须在flags参数中包括MAP_SHARED或MAP_PRIVATE。 添加它,您的程序应该可以运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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