3

请帮助我使用 arm926ejs cpu 的 gnu 汇编程序。

我尝试构建一个简单的程序(test.S):

.global _start 
_start:
    mov r0, #2
    bx lr

并成功构建它:

arm-none-linux-gnueabi-as -mthumb -o test.o test.S
arm-none-linux-gnueabi-ld -o test test.o

但是当我在 arm 目标 linux 环境中运行程序时,我得到一个错误:

./test 
Segmentation fault

我究竟做错了什么?_start 函数可以是拇指函数吗?或者它总是手臂功能?

4

3 回答 3

2

你的问题是你以

bx lr

并且您希望 Linux 在那之后接管。那条确切的线一定是Segmentation fault.

您可以尝试创建一个最小的可执行文件,然后尝试将其一分为二以查看内容并了解预期可执行文件的行为方式。

请参阅下面的工作示例:

.global _start
.thumb_func
_start:
    mov r0, #42
    mov r7, #1
    svc #0

编译

arm-linux-gnueabihf-as start.s -o start.o && arm-linux-gnueabihf-ld start.o -o start_test

转储看看胆量

$ arm-linux-gnueabihf-readelf -a -W start_test

现在你应该注意到奇数地址_start

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x8055
  Start of program headers:          52 (bytes into file)
  Start of section headers:          160 (bytes into file)
  Flags:                             0x5000000, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         1
  Size of section headers:           40 (bytes)
  Number of section headers:         6
  Section header string table index: 3

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00008054 000054 000006 00  AX  0   0  4
  [ 2] .ARM.attributes   ARM_ATTRIBUTES  00000000 00005a 000014 00      0   0  1
  [ 3] .shstrtab         STRTAB          00000000 00006e 000031 00      0   0  1
  [ 4] .symtab           SYMTAB          00000000 000190 0000e0 10      5   6  4
  [ 5] .strtab           STRTAB          00000000 000270 000058 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

There are no section groups in this file.

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x00008000 0x00008000 0x0005a 0x0005a R E 0x8000

 Section to Segment mapping:
  Segment Sections...
   00     .text 

There is no dynamic section in this file.

There are no relocations in this file.

There are no unwind sections in this file.

Symbol table '.symtab' contains 14 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00008054     0 SECTION LOCAL  DEFAULT    1 
     2: 00000000     0 SECTION LOCAL  DEFAULT    2 
     3: 00000000     0 FILE    LOCAL  DEFAULT  ABS start.o
     4: 00008054     0 NOTYPE  LOCAL  DEFAULT    1 $t
     5: 00000000     0 FILE    LOCAL  DEFAULT  ABS 
     6: 0001005a     0 NOTYPE  GLOBAL DEFAULT    1 _bss_end__
     7: 0001005a     0 NOTYPE  GLOBAL DEFAULT    1 __bss_start__
     8: 0001005a     0 NOTYPE  GLOBAL DEFAULT    1 __bss_end__
     9: 00008055     0 FUNC    GLOBAL DEFAULT    1 _start
    10: 0001005a     0 NOTYPE  GLOBAL DEFAULT    1 __bss_start
    11: 0001005c     0 NOTYPE  GLOBAL DEFAULT    1 __end__
    12: 0001005a     0 NOTYPE  GLOBAL DEFAULT    1 _edata
    13: 0001005c     0 NOTYPE  GLOBAL DEFAULT    1 _end

No version information found in this file.
Attribute Section: aeabi
File Attributes
  Tag_CPU_arch: v4T
  Tag_THUMB_ISA_use: Thumb-1
于 2013-12-04T11:43:04.747 回答
2

_start 可以是拇指函数(在 Linux 用户程序中)吗?

是的,它可以。这些步骤并不像您想象的那么简单。

.code 16按照其他人的描述使用。另请查看ARM Script 谓词;我的回答显示了如何检测拇指二进制文件。入口符号必须具有传统_start+1值,否则 Linux 将决定以_startARM模式调用您。

您的代码也试图模仿,

 int main(void) { return 2; }

_start符号不得这样做(根据auselen)。要在ARM_start模式下执行操作,您需要,main()

 #include <linux/unistd.h>
 static inline void exit(int status)
 {
         asm volatile ("mov      r0, %0\n\t"
                 "mov    r7, %1\n\t"
                 "swi    #7\n\t"
                 : : "r" (status),
                   "Ir" (__NR_exit)
                 : "r0", "r7");
 }
 /* Wrapper for main return code. */
 void __attribute__ ((unused)) estart (int argc, char*argv[])
 {
     int rval = main(argc,argv);
     exit(rval);
 }

 /* Setup arguments for estart [like main()]. */
 void __attribute__ ((naked)) _start (void)
 {
     asm(" sub     lr, lr, lr\n"   /* Clear the link register. */
         " ldr     r0, [sp]\n"     /* Get argc... */
         " add     r1, sp, #4\n"   /* ... and argv ... */
         " b       estart\n"       /* Let's go! */
         );
 }

最好清除lr以便堆栈跟踪将终止。如果你愿意,你可以避免argcargv处理。start展示了如何使用它。这estart只是将main()返回代码转换为exit()调用的包装器。

您需要将上述汇编程序转换为Thumb等效项。我建议使用gcc inline assembler。如果内联可以工作,则可以转换为纯汇编源代码。但是,在“C”源代码中执行此操作可能更实用,除非您尝试制作一个非常小的可执行文件。

有用的gcc争论是,

 -nostartfiles -static -nostdlib -isystem <path to linux user headers>

添加-mthumb,您应该有一个用于任何一种模式的线束。

于 2013-12-04T15:55:40.243 回答
0

这里回答。

谢谢大家。

http://stuff.mit.edu/afs/sipb/project/egcs/src/egcs/gcc/config/arm/README-interworking

  • BX如果在 ARM 模式下进行调用,则通过函数指针进行的调用应使用该指令:

        .code 32
        mov lr, pc
        bx  rX
    

    但是,此代码序列在 Thumb 模式下不起作用,因为 mov 指令不会设置lr寄存器的底部位。_call_via_rX相反,应该使用函数的分支和链接:

        .code 16
        bl  _call_via_rX
    

    whererX替换为包含函数地址的寄存器的名称。

于 2013-12-04T12:20:58.423 回答