在mac上的程序集和linux上的程序集有什么区别? [英] What is the difference between assembly on mac and assembly on linux?
问题描述
我一直试图熟悉mac上的程序集,并且据我所知,文档非常稀少,大多数关于该主题的书籍都针对windows或linux。我认为我可以很容易地从linux转换到mac,但是这(linux)
.filesimple.c
.text
.globl简单
.type简单,@function
简单:
pushl%ebp
movl%esp,%ebp
movl 8(%ebp),%edx
movl 12(%ebp),%eax
addl(%edx),%eax
movl%eax,(%edx)
popl%ebp
ret
.size simple,.- simple
.identGCC:(Ubuntu 4.3.2-1ubuntu11)4.3.2
.section .note。 GNU-stack,,@ progbits
看起来与此不同(mac)
.section __TEXT,__ text,regular,pure_instructions
.globl _simple
.align 4,0x90
_simple :## @simple
.cfi_startproc
## BB#0:
pushq%rbp
Ltmp2:
.cfi_def_cfa_offset 16
Ltmp3:
.cfi_offset%rbp,-16
movq%rsp,%rbp
Ltmp4 :
.cfi_def_cfa_register%rbp
addl(%rdi),%esi
movl%esi,(%rdi)
movl%esi,%eax
popq%rbp
ret
.cfi_endproc
$ b .b .subsections_via_symbols
正常(缺少一个更好的单词)指令和寄存器(如 pushq%rbp
)不担心我。但是在机器指令中间敲击的奇怪的如 .cfi_startproc
和 Ltmp2:
没有任何意义。
我不知道要去哪里找出这些是什么以及它们的含义。我正要把我的头发拉出来,因为我一直在努力为几个月的初学者找到一个好的资源。有什么建议?
首先,您将32位x86汇编与64位x86-64进行比较。 OS X Mach-O ABI支持32位IA32,我怀疑你需要 x86-64 SysV ABI 。 (谢天谢地, x86-64.org 网站似乎再次出现)。 Mach-O x86-64 模型基本上这是ELF / SysV ABI的一个变体,所以对于用户空间代码,即使使用不同的汇编器,差异也相对较小。
$ b $ <
ELF x64-64汇编器:
.text
.p2align 4
.globl my_function
.type my_function,@ function
$ b $ my_function:
...
.L__some_address:
.size my_function,[.- my_function]
Mach-O x86-64汇编器:
.text
.p2align 4
.globl _my_function
_my_function:
...
L__some_address:
,汇编器之间的主要区别在于:对于标签,Mach-O函数名称的前导下划线 .L
vs L
(目的地)。具有OS X的汇编程序理解'.p2align'指令。 .align 4,0x90
本质上是做同样的事情。
并非编译器生成的代码中的所有指令都是必需的为汇编程序生成有效的目标代码。它们 需要生成堆栈帧(调试)和异常处理数据。有关更多信息,请参阅链接。
I've been trying to get familiar with assembly on mac, and from what I can tell, the documentation is really sparse, and most books on the subject are for windows or linux. I thought I would be able to translate from linux to mac pretty easily, however this (linux)
.file "simple.c"
.text
.globl simple
.type simple, @function
simple:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx
movl 12(%ebp), %eax
addl (%edx), %eax
movl %eax, (%edx)
popl %ebp
ret
.size simple, .-simple
.ident "GCC: (Ubuntu 4.3.2-1ubuntu11) 4.3.2"
.section .note.GNU-stack,"",@progbits
seems pretty different from this (mac)
.section __TEXT,__text,regular,pure_instructions
.globl _simple
.align 4, 0x90
_simple: ## @simple
.cfi_startproc
## BB#0:
pushq %rbp
Ltmp2:
.cfi_def_cfa_offset 16
Ltmp3:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp4:
.cfi_def_cfa_register %rbp
addl (%rdi), %esi
movl %esi, (%rdi)
movl %esi, %eax
popq %rbp
ret
.cfi_endproc
.subsections_via_symbols
The "normal" (for lack of a better word) instructions and registers such as pushq %rbp
don't worry me. But the "weird" ones like .cfi_startproc
and Ltmp2:
which are smack dab in the middle of the machine instructions don't make any sense.
I have no idea where to go to find out what these are and what they mean. I'm about to pull my hair out as I've been trying to find a good resource for beginners for months. Any suggestions?
To begin with, you're comparing 32-bit x86 assembly with 64-bit x86-64. While the OS X Mach-O ABI supports 32-bit IA32, I suspect you want the x86-64 SysV ABI. (Thankfully, the x86-64.org site seems to be up again). The Mach-O x86-64 model is essentially a variant of the ELF / SysV ABI, so the differences are relatively minor for user-space code, even with different assemblers.
The .cfi
directives are DWARF debugging directives that you don't strictly need for assembly - they are used for call frame information, etc. Here are some minimal examples:
ELF x64-64 assembler:
.text
.p2align 4
.globl my_function
.type my_function,@function
my_function:
...
.L__some_address:
.size my_function,[.-my_function]
Mach-O x86-64 assembler:
.text
.p2align 4
.globl _my_function
_my_function:
...
L__some_address:
Short of writing an asm tutorial, the main differences between the assemblers are: leading underscores for Mach-O functions names, .L
vs L
for labels (destinations). The assembler with OS X understands the '.p2align' directive. .align 4, 0x90
essentially does the same thing.
Not all the directives in compiler-generated code are essential for the assembler to generate valid object code. They are required to generate stack frame (debugging) and exception handling data. Refer to the links for more information.
这篇关于在mac上的程序集和linux上的程序集有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!