Linux ARM 上程序寄存器和堆栈的初始状态 [英] Initial state of program registers and stack on Linux ARM

查看:26
本文介绍了Linux ARM 上程序寄存器和堆栈的初始状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Linux 上使用 ARM 程序集作为学习练习.我正在使用裸"程序集,即没有 libcrt 或 libgcc.任何人都可以向我指出有关堆栈指针和其他寄存器在调用第一条指令之前在程序开始时处于什么状态的信息吗?很明显pc/r15指向_start,其余的似乎都初始化为0,有两个例外;sp/r13 指向一个远离我程序的地址,r1 指向一个稍高的地址.

I'm currently playing with ARM assembly on Linux as a learning exercise. I'm using 'bare' assembly, i.e. no libcrt or libgcc. Can anybody point me to information about what state the stack-pointer and other registers will at the start of the program before the first instruction is called? Obviously pc/r15 points at _start, and the rest appear to be initialised to 0, with two exceptions; sp/r13 points to an address far outside my program, and r1 points to a slightly higher address.

对于一些可靠的问题:

  • r1 中的值是多少?
  • sp 中的值是否是内核分配的合法堆栈?
  • 如果不是,分配堆栈的首选方法是什么;使用 brk 还是分配静态 .bss 部分?

任何指针将不胜感激.

推荐答案

以下是我使用编译器启动 Linux/ARM 程序的方法:

Here's what I use to get a Linux/ARM program started with my compiler:

/** The initial entry point.
 */
asm(
"       .text\n"
"       .globl  _start\n"
"       .align  2\n"
"_start:\n"
"       sub     lr, lr, lr\n"           // Clear the link register.
"       ldr     r0, [sp]\n"             // Get argc...
"       add     r1, sp, #4\n"           // ... and argv ...
"       add     r2, r1, r0, LSL #2\n"   // ... and compute environ.
"       bl      _estart\n"              // Let's go!
"       b       .\n"                    // Never gets here.
"       .size   _start, .-_start\n"
);

如您所见,我只是从 [sp] 的堆栈中获取了 argc、argv 和环境内容.

As you can see, I just get the argc, argv, and environ stuff from the stack at [sp].

稍微澄清一下:堆栈指针指向进程内存中的有效区域.r0、r1、r2 和 r3 是被调用函数的前三个参数.我分别用argc、argv和environ填充它们.

A little clarification: The stack pointer points to a valid area in the process' memory. r0, r1, r2, and r3 are the first three parameters to the function being called. I populate them with argc, argv, and environ, respectively.

这篇关于Linux ARM 上程序寄存器和堆栈的初始状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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