Can _start be the thumb function?

后端 未结 3 736
礼貌的吻别
礼貌的吻别 2020-11-30 15:47

Help me please with gnu assembler for arm926ejs cpu.

I try to build a simple program(test.S):

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


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 15:59

    Your problem is you end with

    bx lr
    

    and you expect Linux to take over after that. That exact line must be the cause of Segmentation fault.

    You can try to create a minimal executable then try to bisect it to see the guts and understand how an executable is expected to behave.

    See below for a working example:

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

    compile with

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

    and dump to see the guts

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

    Now you should notice the odd address of _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
    

提交回复
热议问题