在汇编Intel x64中,为什么要在stdin周围放置方括号 [英] In assembly Intel x64 why should place square brackets around stdin

查看:191
本文介绍了在汇编Intel x64中,为什么要在stdin周围放置方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天正在通过从程序集调用 fgets 进行练习。
将FILE *传递给文件流我写了 mov rdx,[stdin]



但为什么它应该放在方括号内?因为我不需要该指针的值只是指针本身。应该提到,lea rdx,[stdin]也不起作用。据我所知,它导致SIGSEGV被发送到程序。



据我所知, mov 操作数中的方括号,指令意味着与C中的解引用指针相同。另外我知道libc在运行时动态链接到我的可执行文件,并映射到程序的虚拟内存地址空间的栈和堆之间。



YASM,glibc,Linux x64。

解决方案

FILE * stdin; 是指针类型的全局变量。这意味着存储在静态位置的值就是你想要的指针。由于我们想要传递的是存储在标有 stdin 的位置的指针,我们需要从加载[ stdin] 而不是传递 stdin 符号的地址。



传递地址 stdin (带有 lea rdx,[rel stdin] )如果它被声明为 FILE stdin ,并在C中用作 fgets(buf,size,& stdin)






libc的定义 FILE * stdin; 等同于这个asm。

  section .data 
stdin:dq stdin_FILE_struct;这是一个存储在内存中的指针

section .bss
...
stdin_FILE_struct:;;完全制作完成,实际上并不像这个
resq 1;东西
resd 1; something_else
resb 4096;缓冲区

stdin 实际上可能在BSS ,它所指向的数据很可能是由glibc初始化函数动态分配的。

stdin 是一个不透明的指针。你不需要关心它指向什么,只是它保存了一个指向实际的 FILE 对象的指针,这就是你需要传递给fgets的东西。


Today was doing exercise with calling fgets from assembly. For passing FILE* to file stream I wrote mov rdx, [stdin].

But why it should be in square brackets? Because I do not need value of that pointer just the pointer itself. Should mention, that lea rdx, [stdin] also does not work. As I remember, it causes SIGSEGV to be sent to program.

As I understand square brackets in operand of mov instruction mean same as dereferencing pointer in C. Also I know that libc is dynamically linked to my executable at runtime and mapped somewhere between the stack and heap of program's virtual memory address space.

YASM, glibc, Linux x64.

解决方案

FILE *stdin; is a global variable of pointer type. That means the value stored at the static location is the pointer you want. Since the value we want to pass is a pointer stored at the location labelled with stdin, we need to load from [stdin] instead of passing the address of the stdin symbol.

Passing the address of stdin (with lea rdx, [rel stdin]) would work if it was declared as FILE stdin, and used in C as fgets(buf, size, &stdin).


libc's definition of FILE *stdin; is equivalent to this asm.

section .data
stdin: dq  stdin_FILE_struct           ; This is a pointer stored in memory

section .bss
...
stdin_FILE_struct:           ;; TOTALLY MADE UP, it's not actually like this
    resq 1                       ; something
    resd 1                       ; something_else
    resb 4096                    ; buffer

stdin might actually be in the BSS, and the data it points to may well be dynamically allocated by glibc init functions.

stdin is an opaque pointer. You don't need to care what it points to, just that it holds a pointer to the actual FILE object, and that's what you need to pass to fgets.

这篇关于在汇编Intel x64中,为什么要在stdin周围放置方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆