与NASM尴尬的数据部分行为 [英] Awkward data section behavior with NASM

查看:147
本文介绍了与NASM尴尬的数据部分行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写汇编一些基本的程序,其中我只是做一些函数调用。我在OS X中,使用Mach-O的64位格式。然而,似乎是在我的数据部分的悟性上的瑕疵。据我所知,数据节的目的是用于初始化数据,和我已经决定要初始化的内存两大块,如下图所示:

I am writing some basic programs in assembly, in which I simply make some function calls. I am on OS X, using the Mach-O 64 bit format. However, there seems to be a flaw in my understanding of the data section. I understand that the data section is intended to be used for initialized data, and I have decided to initialize two blocks of memory, as shown below:

default rel

global _main
extern _puts

section .data
    first db "Message A", 0 ; null terminator
    second db "Message B", 0

section .text

_main:
    push rbp ; alignment
    mov rbp, rsp
    sub rsp, 0x10
    lea rdi, [second]
    call _puts
    lea rdi, [first]
    call _puts
    add rsp, 0x10
    pop rbp
    ret

(我链接到的libc中,很明显)

(I am linking against libc, obviously)

我的理解是,堆栈必须根据系统V ABI规范进行的16个字节。此外,RDI应该包含的第一个参数的函数调用。

My understanding is that the stack must be 16 byte aligned according to the System V ABI specification. Additionally, rdi should contain the first argument to the function call.

以上code将打印消息B,但失败后直接打印信息。这是因为如果消息被覆盖的地方。

The above code will print "Message B", but fail to print "Message A" directly after. It's as if "Message A" was overwritten somewhere.

什么是也很有趣的是,当我在的.text 部分输入两个字符串时,code按预期工作。由于的.text 是只读的,我几乎可以肯定,消息正在初始化后的某个覆盖。无论我在。数据部分多少字符串输入,我永远只能打印最后一个,其他的人只是覆盖。为什么最后的字符串初始化未覆盖的唯一一个?

What is also very interesting is that when I enter the two strings in the .text section, the code works as intended. Since .text is read-only, I am almost certain that the "Message A" is being overwritten sometime after initialization. No matter how many strings I enter in the .data section, I can only ever print the last one, and the other ones are simply overwritten. Why is the last string initialized the only one that is not overwritten?

推荐答案

在这一刻,似乎没有成为太大的工作,各地。这是在08年11月2日的错误,我想有些人已经报告了06年11月2日的问题一样,所以要05年2月11日可能是你最好的选择,或等待09年2月11日。

At this moment, there doesn't seem to be much of a work around. This is a bug in 2.11.08, and I think some people have reported problems with 2.11.06 as well, so going to 2.11.05 is probably your best bet, or waiting for 2.11.09.

这篇关于与NASM尴尬的数据部分行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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