Jok*_*777 0 linux x86 assembly nasm system-calls
我知道这int 0x80会在 linux 中造成中断。但是,我不明白这段代码是如何工作的。它会返回一些东西吗?
代表什么$ - msg?
global _start
section .data
msg db "Hello, world!", 0x0a
len equ $ - msg
section .text
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, len
int 0x80 ;What is this?
mov eax, 1
mov ebx, 0
int 0x80 ;and what is this?
Run Code Online (Sandbox Code Playgroud)
$ 在 NASM 中究竟是如何工作的?解释了如何$ - msg让 NASM 为您计算字符串长度作为汇编时间常数,而不是对其进行硬编码。
我最初为SO Docs编写了其余部分(主题 ID:1164,示例 ID:19078),重写了@runner 评论较少的基本示例。 这看起来是一个更好的地方,而不是作为我对另一个问题的回答的一部分,我之前在 SO docs 实验结束后移动了它。
系统调用是通过将参数放入寄存器,然后运行int 0x80(32 位模式)或syscall(64 位模式)来完成的。 什么是在i386 UNIX和Linux系统调用和x86-64的调用约定和权威指南到Linux系统调用。
可以将其int 0x80视为跨越用户/内核权限边界“调用”内核的一种方式。 内核根据int 0x80执行时寄存器中的值执行操作,然后最终返回。返回值在 EAX 中。
当执行到达内核的入口点时,它会查看 EAX 并根据 EAX 中的调用号分派到正确的系统调用。来自其他寄存器的值作为函数参数传递给该系统调用的内核处理程序。(例如 eax=4 /int 0x80将使内核调用其sys_write内核函数,实现 POSIXwrite系统调用。)
另请参阅如果在 64 位代码中使用 32 位 int 0x80 Linux ABI,会发生什么?- 该答案包括查看内核入口点中由int 0x80. (也适用于 32 位用户空间,而不仅仅是不应该使用的 64 位int 0x80)。
如果您还不了解底层 Unix 系统编程,您可能只想在 asm 中编写接受 args 并返回值(或通过指针 arg 更新数组)并从 C 或 C++ 程序调用它们的函数。然后,您只需担心学习如何处理寄存器和内存,而无需学习 POSIX 系统调用 API 和使用它的 ABI。这也使得将您的代码与 C 实现的编译器输出进行比较变得非常容易。编译器通常在编写高效代码方面做得很好,但很少是完美的。
libc 为系统调用提供包装函数,因此编译器生成的代码不会call write直接调用它int 0x80(或者如果您关心性能,sysenter)。(在X86-64码,使用syscall用于64位ABI)。另见syscalls(2)。
系统调用记录在第 2 部分手册页中,例如write(2). 有关 libc 包装函数和底层 Linux 系统调用之间的差异,请参阅 NOTES 部分。请注意,sys_exitis的包装器_exit(2),而不是exit(3)首先刷新 stdio 缓冲区和其他清理的ISO C 函数。还有一个结束所有线程的exit_group系统调用。 实际上使用它,因为在单线程进程中没有缺点。exit(3)
此代码进行 2 个系统调用:
我对它进行了大量评论(以至于它开始在没有颜色语法突出显示的情况下掩盖实际代码)。这是试图向初学者指出一些事情,而不是您应该如何正常评论您的代码。
section .text ; Executable code goes in the .text section
global _start ; The linker looks for this symbol to set the process entry point, so execution start here
;;;a name followed by a colon defines a symbol. The global _start directive modifies it so it's a global symbol, not just one that we can CALL or JMP to from inside the asm.
;;; note that _start isn't really a "function". You can't return from it, and the kernel passes argc, argv, and env differently than main() would expect.
_start:
;;; write(1, msg, len);
; Start by moving the arguments into registers, where the kernel will look for them
mov edx,len ; 3rd arg goes in edx: buffer length
mov ecx,msg ; 2nd arg goes in ecx: pointer to the buffer
;Set output to stdout (goes to your terminal, or wherever you redirect or pipe)
mov ebx,1 ; 1st arg goes in ebx: Unix file descriptor. 1 = stdout, which is normally connected to the terminal.
mov eax,4 ; system call number (from SYS_write / __NR_write from unistd_32.h).
int 0x80 ; generate an interrupt, activating the kernel's system-call handling code. 64-bit code uses a different instruction, different registers, and different call numbers.
;; eax = return value, all other registers unchanged.
;;;Second, exit the process. There's nothing to return to, so we can't use a ret instruction (like we could if this was main() or any function with a caller)
;;; If we don't exit, execution continues into whatever bytes are next in the memory page,
;;; typically leading to a segmentation fault because the padding 00 00 decodes to add [eax],al.
;;; _exit(0);
xor ebx,ebx ; first arg = exit status = 0. (will be truncated to 8 bits). Zeroing registers is a special case on x86, and mov ebx,0 would be less efficient.
;; leaving out the zeroing of ebx would mean we exit(1), i.e. with an error status, since ebx still holds 1 from earlier.
mov eax,1 ; put __NR_exit into eax
int 0x80 ;Execute the Linux function
section .rodata ; Section for read-only constants
;; msg is a label, and in this context doesn't need to be msg:. It could be on a separate line.
;; db = Data Bytes: assemble some literal bytes into the output file.
msg db 'Hello, world!',0xa ; ASCII string constant plus a newline (0x10)
;; No terminating zero byte is needed, because we're using write(), which takes a buffer + length instead of an implicit-length string.
;; To make this a C string that we could pass to puts or strlen, we'd need a terminating 0 byte. (e.g. "...", 0x10, 0)
len equ $ - msg ; Define an assemble-time constant (not stored by itself in the output file, but will appear as an immediate operand in insns that use it)
; Calculate len = string length. subtract the address of the start
; of the string from the current position ($)
;; equivalently, we could have put a str_end: label after the string and done len equ str_end - str
Run Code Online (Sandbox Code Playgroud)
请注意,我们不会将字符串长度存储在数据存储器中的任何地方。它是一个汇编时间常数,因此将其作为立即操作数比将其作为负载更有效。我们也可以用三条push imm32指令将字符串数据压入堆栈,但是代码量过大并不是一件好事。
在 Linux 上,您可以将此文件另存为Hello.asm并使用以下命令从中构建 32 位可执行文件:
nasm -felf32 Hello.asm # assemble as 32-bit code. Add -Worphan-labels -g -Fdwarf for debug symbols and warnings
gcc -static -nostdlib -m32 Hello.o -o Hello # link without CRT startup code or libc, making a static binary
Run Code Online (Sandbox Code Playgroud)
有关将程序集构建为 32 位或 64 位静态或动态链接的 Linux 可执行文件的更多详细信息,请参阅此答案,了解 NASM/YASM 语法或带有 GNUas指令的GNU AT&T 语法。(关键点:-m32在 64 位主机上构建 32 位代码时确保使用或等效,否则您将在运行时遇到令人困惑的问题。)
您可以跟踪它的执行strace以查看它所做的系统调用:
$ strace ./Hello
execve("./Hello", ["./Hello"], [/* 72 vars */]) = 0
[ Process PID=4019 runs in 32 bit mode. ]
write(1, "Hello, world!\n", 14Hello, world!
) = 14
_exit(0) = ?
+++ exited with 0 +++
Run Code Online (Sandbox Code Playgroud)
将此与动态链接进程的跟踪(如 gcc 从 hello.c 或 running 生成strace /bin/ls)进行比较,以了解动态链接和 C 库启动的幕后发生了多少事情。
stderr 上的跟踪和 stdout 上的常规输出都将到达此处的终端,因此它们会干扰write系统调用的线路。如果您愿意,可以重定向或跟踪到文件。请注意这如何让我们轻松查看系统调用返回值,而无需添加代码来打印它们,实际上比使用常规调试器(如 gdb)单步执行并查看它更容易eax。有关gdb asm 提示,请参阅x86 标记 wiki的底部。(标签 wiki 的其余部分充满了指向优质资源的链接。)
这个程序的 x86-64 版本非常相似,将相同的 args 传递给相同的系统调用,只是在不同的寄存器中,syscall而不是int 0x80. 见底部会发生什么事,如果你在64位代码中使用32位int 0x80的Linux的ABI?有关编写字符串并以 64 位代码退出的工作示例。
相关:关于为 Linux 创建真正小巧的 ELF 可执行文件的旋风教程。您可以运行的最小二进制文件,它只进行 exit() 系统调用。这是关于最小化二进制大小,而不是源大小,甚至只是实际运行的指令数。
| 归档时间: |
|
| 查看次数: |
1179 次 |
| 最近记录: |