数据字符串位于这个反汇编中的哪里?

huangapple go评论51阅读模式
英文:

Where is the data string located in this disassembly?

问题

I am going through the OS-dev book by Nick Blundell and I am disassembling the code in section 5.1.4.

Specifically, I have this c code stored in string.c:

void my_function()
{
    char* str = "Hello";
}

I compile it in 32-bit mode (since I am on 64-bit Linux) this way:

gcc -ffreestanding -fno-pie -m32 -c string.c -o string.o

Linking step:

ld -m elf_i386 -o string.bin -Ttext 0x0 --oformat binary string.o

And disassembly:

ndisasm -b 32 string.bin > string.dis

And here is my result:

00000000  55                push ebp
00000001  89E5              mov ebp,esp
00000003  83EC10            sub esp,byte +0x10
00000006  C745FC00100000    mov dword [ebp-0x4],0x1000
0000000D  90                nop
0000000E  C9                leave
0000000F  C3                ret

I see that the compiler is allocating 16 bytes on the third line but I don't see where the "Hello" string is located. I especially don't understand what happens at the 4th line.

Any help would be appreciated.

英文:

I am going through the OS-dev book by Nick Blundell and I am disassembling the code in section 5.1.4.

Specifically, I have this c code stored in string.c:

void my_function()
{
    char* str = "Hello";
}

I compile it in 32-bit mode (since I am on 64-bit linux) this way:

gcc -ffreestanding -fno-pie -m32 -c string.c -o string.o

Linking step:

ld -m elf_i386 -o string.bin -Ttext 0x0 --oformat binary string.o

And disassembly:

ndisasm -b 32 string.bin > string.dis

And here is my result:

00000000  55                push ebp
00000001  89E5              mov ebp,esp
00000003  83EC10            sub esp,byte +0x10
00000006  C745FC00100000    mov dword [ebp-0x4],0x1000
0000000D  90                nop
0000000E  C9                leave
0000000F  C3                ret

I see that the compiler is allocating 16-bytes on the third line but I don't see where the "hello" string is located. I especially don't understand what happens at the 4th line.

Any help would be appreciated.

答案1

得分: 1

0x1000

不要在代码段中查找字符串。它们不在那里。唯一不属于序言或尾声的指令是 mov dword [ebp-0x4],0x1000,它明显将局部变量 str 设置为 0x1000。我们知道 str 指向该字符串。

字符串不在文本段中。它们位于rodata段中。你在链接时是否省略了rodata段?

(你以 -O0 编译。因此,未使用的代码变量分配未被删除。)

英文:

0x1000

Don't look for strings in the code segment. That's not where they are. The only instruction that is not part of the prologue or epilogue is mov dword [ebp-0x4],0x1000; which is clearly setting the local variable str to 0x1000. And we know that str points to the string.

Strings aren't in the text segment. They're in the rodata segment. Did you omit the rodata segment on linking?

(You compiled at -O0. Thus the dead code variable assignment was not removed.)

huangapple
  • 本文由 发表于 2023年7月13日 09:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定