【问题标题】:Failed to compile or link yasm program that call c function [duplicate]无法编译或链接调用c函数的yasm程序[重复]
【发布时间】:2016-06-21 06:48:33
【问题描述】:

在 linux (x86-64) 上尝试从汇编代码 (yasm) 调用 c 函数时,执行失败。


代码

function_call_c.asm:

; yasm assembly program, instruction - call c function
; compile: yasm -f elf64 function_call_c.asm -g dwarf2 && ld function_call_c.o -lc
; execute: ./a.out

section .data
msg_format db "hello"

section .text
extern printf
global _start

_start:
    lea rdi, [msg_format]
    call printf


_exit:
    ; exit
    mov eax,1
    mov ebx,5
    int 0x80

编译:

yasm -f elf64 function_call_c.asm -g dwarf2 && ld function_call_c.o -lc

执行时:

提示:

bash: ./a.out: 没有这样的文件或目录

但是a.out确实存在,并且有执行权限。

使用objdump,获取可执行文件的信息:

a.out:     file format elf64-x86-64


Disassembly of section .interp:

0000000000400158 <.interp>:
  400158:   2f                      (bad)  
  400159:   6c                      insb   (%dx),%es:(%rdi)
  40015a:   69 62 2f 6c 64 36 34    imul   $0x3436646c,0x2f(%rdx),%esp
  400161:   2e 73 6f                jae,pn 4001d3 <printf@plt-0x4d>
  400164:   2e 31 00                xor    %eax,%cs:(%rax)

Disassembly of section .hash:

0000000000400168 <.hash>:
  400168:   01 00                   add    %eax,(%rax)
  40016a:   00 00                   add    %al,(%rax)
  40016c:   02 00                   add    (%rax),%al
  40016e:   00 00                   add    %al,(%rax)
  400170:   01 00                   add    %eax,(%rax)
    ...

Disassembly of section .dynsym:

0000000000400180 <.dynsym>:
    ...
  400198:   0b 00                   or     (%rax),%eax
  40019a:   00 00                   add    %al,(%rax)
  40019c:   12 00                   adc    (%rax),%al
    ...

Disassembly of section .dynstr:

00000000004001b0 <.dynstr>:
  4001b0:   00 6c 69 62             add    %ch,0x62(%rcx,%rbp,2)
  4001b4:   63 2e                   movslq (%rsi),%ebp
  4001b6:   73 6f                   jae    400227 <printf@plt+0x7>
  4001b8:   2e 36 00 70 72          cs add %dh,%cs:%ss:0x72(%rax)
  4001bd:   69 6e 74 66 00 47 4c    imul   $0x4c470066,0x74(%rsi),%ebp
  4001c4:   49                      rex.WB
  4001c5:   42                      rex.X
  4001c6:   43 5f                   rex.XB pop %r15
  4001c8:   32 2e                   xor    (%rsi),%ch
  4001ca:   32 2e                   xor    (%rsi),%ch
  4001cc:   35                      .byte 0x35
    ...

Disassembly of section .gnu.version:

00000000004001ce <.gnu.version>:
  4001ce:   00 00                   add    %al,(%rax)
  4001d0:   02 00                   add    (%rax),%al

Disassembly of section .gnu.version_r:

00000000004001d8 <.gnu.version_r>:
  4001d8:   01 00                   add    %eax,(%rax)
  4001da:   01 00                   add    %eax,(%rax)
  4001dc:   01 00                   add    %eax,(%rax)
  4001de:   00 00                   add    %al,(%rax)
  4001e0:   10 00                   adc    %al,(%rax)
  4001e2:   00 00                   add    %al,(%rax)
  4001e4:   00 00                   add    %al,(%rax)
  4001e6:   00 00                   add    %al,(%rax)
  4001e8:   75 1a                   jne    400204 <printf@plt-0x1c>
  4001ea:   69 09 00 00 02 00       imul   $0x20000,(%rcx),%ecx
  4001f0:   12 00                   adc    (%rax),%al
  4001f2:   00 00                   add    %al,(%rax)
  4001f4:   00 00                   add    %al,(%rax)
    ...

Disassembly of section .rela.plt:

00000000004001f8 <.rela.plt>:
  4001f8:   a8 03                   test   $0x3,%al
  4001fa:   60                      (bad)  
  4001fb:   00 00                   add    %al,(%rax)
  4001fd:   00 00                   add    %al,(%rax)
  4001ff:   00 07                   add    %al,(%rdi)
  400201:   00 00                   add    %al,(%rax)
  400203:   00 01                   add    %al,(%rcx)
    ...

Disassembly of section .plt:

0000000000400210 <printf@plt-0x10>:
  400210:   ff 35 82 01 20 00       pushq  0x200182(%rip)        # 600398 <_GLOBAL_OFFSET_TABLE_+0x8>
  400216:   ff 25 84 01 20 00       jmpq   *0x200184(%rip)        # 6003a0 <_GLOBAL_OFFSET_TABLE_+0x10>
  40021c:   0f 1f 40 00             nopl   0x0(%rax)

0000000000400220 <printf@plt>:
  400220:   ff 25 82 01 20 00       jmpq   *0x200182(%rip)        # 6003a8 <_GLOBAL_OFFSET_TABLE_+0x18>
  400226:   68 00 00 00 00          pushq  $0x0
  40022b:   e9 e0 ff ff ff          jmpq   400210 <printf@plt-0x10>

Disassembly of section .text:

0000000000400230 <_start>:
  400230:   48 8d 3c 25 b0 03 60    lea    0x6003b0,%rdi
  400237:   00 
  400238:   e8 e3 ff ff ff          callq  400220 <printf@plt>

000000000040023d <_exit>:
  40023d:   b8 01 00 00 00          mov    $0x1,%eax
  400242:   bb 05 00 00 00          mov    $0x5,%ebx
  400247:   cd 80                   int    $0x80

Disassembly of section .dynamic:

0000000000600250 <_DYNAMIC>:
  600250:   01 00                   add    %eax,(%rax)
  600252:   00 00                   add    %al,(%rax)
  600254:   00 00                   add    %al,(%rax)
  600256:   00 00                   add    %al,(%rax)
  600258:   01 00                   add    %eax,(%rax)
  60025a:   00 00                   add    %al,(%rax)
  60025c:   00 00                   add    %al,(%rax)
  60025e:   00 00                   add    %al,(%rax)
  600260:   04 00                   add    $0x0,%al
  600262:   00 00                   add    %al,(%rax)
  600264:   00 00                   add    %al,(%rax)
  600266:   00 00                   add    %al,(%rax)
  600268:   68 01 40 00 00          pushq  $0x4001
  60026d:   00 00                   add    %al,(%rax)
  60026f:   00 05 00 00 00 00       add    %al,0x0(%rip)        # 600275 <_DYNAMIC+0x25>
  600275:   00 00                   add    %al,(%rax)
  600277:   00 b0 01 40 00 00       add    %dh,0x4001(%rax)
  60027d:   00 00                   add    %al,(%rax)
  60027f:   00 06                   add    %al,(%rsi)
  600281:   00 00                   add    %al,(%rax)
  600283:   00 00                   add    %al,(%rax)
  600285:   00 00                   add    %al,(%rax)
  600287:   00 80 01 40 00 00       add    %al,0x4001(%rax)
  60028d:   00 00                   add    %al,(%rax)
  60028f:   00 0a                   add    %cl,(%rdx)
  600291:   00 00                   add    %al,(%rax)
  600293:   00 00                   add    %al,(%rax)
  600295:   00 00                   add    %al,(%rax)
  600297:   00 1e                   add    %bl,(%rsi)
  600299:   00 00                   add    %al,(%rax)
  60029b:   00 00                   add    %al,(%rax)
  60029d:   00 00                   add    %al,(%rax)
  60029f:   00 0b                   add    %cl,(%rbx)
  6002a1:   00 00                   add    %al,(%rax)
  6002a3:   00 00                   add    %al,(%rax)
  6002a5:   00 00                   add    %al,(%rax)
  6002a7:   00 18                   add    %bl,(%rax)
  6002a9:   00 00                   add    %al,(%rax)
  6002ab:   00 00                   add    %al,(%rax)
  6002ad:   00 00                   add    %al,(%rax)
  6002af:   00 15 00 00 00 00       add    %dl,0x0(%rip)        # 6002b5 <_DYNAMIC+0x65>
    ...
  6002bd:   00 00                   add    %al,(%rax)
  6002bf:   00 03                   add    %al,(%rbx)
  6002c1:   00 00                   add    %al,(%rax)
  6002c3:   00 00                   add    %al,(%rax)
  6002c5:   00 00                   add    %al,(%rax)
  6002c7:   00 90 03 60 00 00       add    %dl,0x6003(%rax)
  6002cd:   00 00                   add    %al,(%rax)
  6002cf:   00 02                   add    %al,(%rdx)
  6002d1:   00 00                   add    %al,(%rax)
  6002d3:   00 00                   add    %al,(%rax)
  6002d5:   00 00                   add    %al,(%rax)
  6002d7:   00 18                   add    %bl,(%rax)
  6002d9:   00 00                   add    %al,(%rax)
  6002db:   00 00                   add    %al,(%rax)
  6002dd:   00 00                   add    %al,(%rax)
  6002df:   00 14 00                add    %dl,(%rax,%rax,1)
  6002e2:   00 00                   add    %al,(%rax)
  6002e4:   00 00                   add    %al,(%rax)
  6002e6:   00 00                   add    %al,(%rax)
  6002e8:   07                      (bad)  
  6002e9:   00 00                   add    %al,(%rax)
  6002eb:   00 00                   add    %al,(%rax)
  6002ed:   00 00                   add    %al,(%rax)
  6002ef:   00 17                   add    %dl,(%rdi)
  6002f1:   00 00                   add    %al,(%rax)
  6002f3:   00 00                   add    %al,(%rax)
  6002f5:   00 00                   add    %al,(%rax)
  6002f7:   00 f8                   add    %bh,%al
  6002f9:   01 40 00                add    %eax,0x0(%rax)
  6002fc:   00 00                   add    %al,(%rax)
  6002fe:   00 00                   add    %al,(%rax)
  600300:   fe                      (bad)  
  600301:   ff                      (bad)  
  600302:   ff 6f 00                ljmpq  *0x0(%rdi)
  600305:   00 00                   add    %al,(%rax)
  600307:   00 d8                   add    %bl,%al
  600309:   01 40 00                add    %eax,0x0(%rax)
  60030c:   00 00                   add    %al,(%rax)
  60030e:   00 00                   add    %al,(%rax)
  600310:   ff                      (bad)  
  600311:   ff                      (bad)  
  600312:   ff 6f 00                ljmpq  *0x0(%rdi)
  600315:   00 00                   add    %al,(%rax)
  600317:   00 01                   add    %al,(%rcx)
  600319:   00 00                   add    %al,(%rax)
  60031b:   00 00                   add    %al,(%rax)
  60031d:   00 00                   add    %al,(%rax)
  60031f:   00 f0                   add    %dh,%al
  600321:   ff                      (bad)  
  600322:   ff 6f 00                ljmpq  *0x0(%rdi)
  600325:   00 00                   add    %al,(%rax)
  600327:   00 ce                   add    %cl,%dh
  600329:   01 40 00                add    %eax,0x0(%rax)
    ...

Disassembly of section .got.plt:

0000000000600390 <_GLOBAL_OFFSET_TABLE_>:
  600390:   50                      push   %rax
  600391:   02 60 00                add    0x0(%rax),%ah
    ...
  6003a8:   26 02 40 00             add    %es:0x0(%rax),%al
  6003ac:   00 00                   add    %al,(%rax)
    ...

Disassembly of section .data:

00000000006003b0 <msg_format>:
  6003b0:   68 65 6c 6c 6f          pushq  $0x6f6c6c65

Disassembly of section .debug_aranges:

0000000000000000 <.debug_aranges>:
   0:   2c 00                   sub    $0x0,%al
   2:   00 00                   add    %al,(%rax)
   4:   02 00                   add    (%rax),%al
   6:   00 00                   add    %al,(%rax)
   8:   00 00                   add    %al,(%rax)
   a:   08 00                   or     %al,(%rax)
   c:   00 00                   add    %al,(%rax)
   e:   00 00                   add    %al,(%rax)
  10:   30 02                   xor    %al,(%rdx)
  12:   40 00 00                add    %al,(%rax)
  15:   00 00                   add    %al,(%rax)
  17:   00 19                   add    %bl,(%rcx)
    ...

Disassembly of section .debug_info:

0000000000000000 <.debug_info>:
   0:   85 00                   test   %eax,(%rax)
   2:   00 00                   add    %al,(%rax)
   4:   02 00                   add    (%rax),%al
   6:   00 00                   add    %al,(%rax)
   8:   00 00                   add    %al,(%rax)
   a:   08 01                   or     %al,(%rcx)
   c:   00 00                   add    %al,(%rax)
   e:   00 00                   add    %al,(%rax)
  10:   30 02                   xor    %al,(%rdx)
  12:   40 00 00                add    %al,(%rax)
  15:   00 00                   add    %al,(%rax)
  17:   00 49 02                add    %cl,0x2(%rcx)
  1a:   40 00 00                add    %al,(%rax)
  1d:   00 00                   add    %al,(%rax)
  1f:   00 66 75                add    %ah,0x75(%rsi)
  22:   6e                      outsb  %ds:(%rsi),(%dx)
  23:   63 74 69 6f             movslq 0x6f(%rcx,%rbp,2),%esi
  27:   6e                      outsb  %ds:(%rsi),(%dx)
  28:   5f                      pop    %rdi
  29:   63 61 6c                movslq 0x6c(%rcx),%esp
  2c:   6c                      insb   (%dx),%es:(%rdi)
  2d:   5f                      pop    %rdi
  2e:   63 2e                   movslq (%rsi),%ebp
  30:   61                      (bad)  
  31:   73 6d                   jae    a0 <printf@plt-0x400180>
  33:   00 2f                   add    %ch,(%rdi)
  35:   6d                      insl   (%dx),%es:(%rdi)
  36:   6e                      outsb  %ds:(%rsi),(%dx)
  37:   74 2f                   je     68 <printf@plt-0x4001b8>
  39:   73 74                   jae    af <printf@plt-0x400171>
  3b:   61                      (bad)  
  3c:   72 2f                   jb     6d <printf@plt-0x4001b3>
  3e:   67 69 74 5f 72 65 70    imul   $0x736f7065,0x72(%edi,%ebx,2),%esi
  45:   6f 73 
  47:   69 74 6f 72 79 2f 77    imul   $0x6f772f79,0x72(%rdi,%rbp,2),%esi
  4e:   6f 
  4f:   72 6b                   jb     bc <printf@plt-0x400164>
  51:   73 70                   jae    c3 <printf@plt-0x40015d>
  53:   61                      (bad)  
  54:   63 65 2f                movslq 0x2f(%rbp),%esp
  57:   61                      (bad)  
  58:   73 73                   jae    cd <printf@plt-0x400153>
  5a:   65                      gs
  5b:   6d                      insl   (%dx),%es:(%rdi)
  5c:   62                      (bad)  
  5d:   6c                      insb   (%dx),%es:(%rdi)
  5e:   79 5f                   jns    bf <printf@plt-0x400161>
  60:   77 6f                   ja     d1 <printf@plt-0x40014f>
  62:   72 6b                   jb     cf <printf@plt-0x400151>
  64:   70 6c                   jo     d2 <printf@plt-0x40014e>
  66:   61                      (bad)  
  67:   63 65 2f                movslq 0x2f(%rbp),%esp
  6a:   79 61                   jns    cd <printf@plt-0x400153>
  6c:   73 6d                   jae    db <printf@plt-0x400145>
  6e:   2f                      (bad)  
  6f:   69 6e 73 74 72 75 63    imul   $0x63757274,0x73(%rsi),%ebp
  76:   74 69                   je     e1 <printf@plt-0x40013f>
  78:   6f                      outsl  %ds:(%rsi),(%dx)
  79:   6e                      outsb  %ds:(%rsi),(%dx)
  7a:   2f                      (bad)  
  7b:   00 79 61                add    %bh,0x61(%rcx)
  7e:   73 6d                   jae    ed <printf@plt-0x400133>
  80:   20 31                   and    %dh,(%rcx)
  82:   2e 32 2e                xor    %cs:(%rsi),%ch
  85:   30 00                   xor    %al,(%rax)
  87:   01                      .byte 0x1
  88:   80                      .byte 0x80

Disassembly of section .debug_abbrev:

0000000000000000 <.debug_abbrev>:
   0:   01 11                   add    %edx,(%rcx)
   2:   00 10                   add    %dl,(%rax)
   4:   06                      (bad)  
   5:   11 01                   adc    %eax,(%rcx)
   7:   12 01                   adc    (%rcx),%al
   9:   03 08                   add    (%rax),%ecx
   b:   1b 08                   sbb    (%rax),%ecx
   d:   25 08 13 05 00          and    $0x51308,%eax
    ...

Disassembly of section .debug_line:

0000000000000000 <.debug_line>:
   0:   47 00 00                rex.RXB add %r8b,(%r8)
   3:   00 02                   add    %al,(%rdx)
   5:   00 2a                   add    %ch,(%rdx)
   7:   00 00                   add    %al,(%rax)
   9:   00 01                   add    %al,(%rcx)
   b:   01 fb                   add    %edi,%ebx
   d:   0e                      (bad)  
   e:   0d 00 01 01 01          or     $0x1010100,%eax
  13:   01 00                   add    %eax,(%rax)
  15:   00 00                   add    %al,(%rax)
  17:   01 00                   add    %eax,(%rax)
  19:   00 01                   add    %al,(%rcx)
  1b:   00 66 75                add    %ah,0x75(%rsi)
  1e:   6e                      outsb  %ds:(%rsi),(%dx)
  1f:   63 74 69 6f             movslq 0x6f(%rcx,%rbp,2),%esi
  23:   6e                      outsb  %ds:(%rsi),(%dx)
  24:   5f                      pop    %rdi
  25:   63 61 6c                movslq 0x6c(%rcx),%esp
  28:   6c                      insb   (%dx),%es:(%rdi)
  29:   5f                      pop    %rdi
  2a:   63 2e                   movslq (%rsi),%ebp
  2c:   61                      (bad)  
  2d:   73 6d                   jae    9c <printf@plt-0x400184>
  2f:   00 00                   add    %al,(%rax)
  31:   00 00                   add    %al,(%rax)
  33:   00 00                   add    %al,(%rax)
  35:   09 02                   or     %eax,(%rdx)
  37:   30 02                   xor    %al,(%rdx)
  39:   40 00 00                add    %al,(%rax)
  3c:   00 00                   add    %al,(%rax)
  3e:   00 03                   add    %al,(%rbx)
  40:   0c 01                   or     $0x1,%al
  42:   83 5d 59 59             sbbl   $0x59,0x59(%rbp)
  46:   02 02                   add    (%rdx),%al
  48:   00 01                   add    %al,(%rcx)
  4a:   01                      .byte 0x1

使用readelf,获取可执行文件的信息:

ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x400230
  Start of program headers:          64 (bytes into file)
  Start of section headers:          1432 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         5
  Size of section headers:           64 (bytes)
  Number of section headers:         21
  Section header string table index: 18

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000400158  00000158
       000000000000000f  0000000000000000   A       0     0     1
  [ 2] .hash             HASH             0000000000400168  00000168
       0000000000000014  0000000000000004   A       3     0     8
  [ 3] .dynsym           DYNSYM           0000000000400180  00000180
       0000000000000030  0000000000000018   A       4     1     8
  [ 4] .dynstr           STRTAB           00000000004001b0  000001b0
       000000000000001e  0000000000000000   A       0     0     1
  [ 5] .gnu.version      VERSYM           00000000004001ce  000001ce
       0000000000000004  0000000000000002   A       3     0     2
  [ 6] .gnu.version_r    VERNEED          00000000004001d8  000001d8
       0000000000000020  0000000000000000   A       4     1     8
  [ 7] .rela.plt         RELA             00000000004001f8  000001f8
       0000000000000018  0000000000000018   A       3     8     8
  [ 8] .plt              PROGBITS         0000000000400210  00000210
       0000000000000020  0000000000000010  AX       0     0     16
  [ 9] .text             PROGBITS         0000000000400230  00000230
       0000000000000019  0000000000000000  AX       0     0     16
  [10] .eh_frame         PROGBITS         0000000000400250  00000250
       0000000000000000  0000000000000000   A       0     0     8
  [11] .dynamic          DYNAMIC          0000000000600250  00000250
       0000000000000140  0000000000000010  WA       4     0     8
  [12] .got.plt          PROGBITS         0000000000600390  00000390
       0000000000000020  0000000000000008  WA       0     0     8
  [13] .data             PROGBITS         00000000006003b0  000003b0
       0000000000000005  0000000000000000  WA       0     0     4
  [14] .debug_aranges    PROGBITS         0000000000000000  000003c0
       0000000000000030  0000000000000000           0     0     16
  [15] .debug_info       PROGBITS         0000000000000000  000003f0
       0000000000000089  0000000000000000           0     0     1
  [16] .debug_abbrev     PROGBITS         0000000000000000  00000479
       0000000000000014  0000000000000000           0     0     1
  [17] .debug_line       PROGBITS         0000000000000000  0000048d
       000000000000004b  0000000000000000           0     0     1
  [18] .shstrtab         STRTAB           0000000000000000  000004d8
       00000000000000bc  0000000000000000           0     0     1
  [19] .symtab           SYMTAB           0000000000000000  00000ad8
       00000000000002b8  0000000000000018          20    24     8
  [20] .strtab           STRTAB           0000000000000000  00000d90
       0000000000000078  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
  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              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
         0x0000000000000118 0x0000000000000118  R E    8
  INTERP         0x0000000000000158 0x0000000000400158 0x0000000000400158
         0x000000000000000f 0x000000000000000f  R      1
      [Requesting program interpreter: /lib/ld64.so.1]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
         0x0000000000000250 0x0000000000000250  R E    200000
  LOAD           0x0000000000000250 0x0000000000600250 0x0000000000600250
         0x0000000000000165 0x0000000000000165  RW     200000
  DYNAMIC        0x0000000000000250 0x0000000000600250 0x0000000000600250
         0x0000000000000140 0x0000000000000140  RW     8

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .interp .hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.plt .plt .text 
   03     .dynamic .got.plt .data 
   04     .dynamic 

Dynamic section at offset 0x250 contains 15 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000004 (HASH)               0x400168
 0x0000000000000005 (STRTAB)             0x4001b0
 0x0000000000000006 (SYMTAB)             0x400180
 0x000000000000000a (STRSZ)              30 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x600390
 0x0000000000000002 (PLTRELSZ)           24 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x4001f8
 0x000000006ffffffe (VERNEED)            0x4001d8
 0x000000006fffffff (VERNEEDNUM)         1
 0x000000006ffffff0 (VERSYM)             0x4001ce
 0x0000000000000000 (NULL)               0x0

Relocation section '.rela.plt' at offset 0x1f8 contains 1 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
0000006003a8  000100000007 R_X86_64_JUMP_SLO 0000000000000000 printf + 0

The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.

Symbol table '.dynsym' contains 2 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)

Symbol table '.symtab' contains 29 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000400158     0 SECTION LOCAL  DEFAULT    1 
     2: 0000000000400168     0 SECTION LOCAL  DEFAULT    2 
     3: 0000000000400180     0 SECTION LOCAL  DEFAULT    3 
     4: 00000000004001b0     0 SECTION LOCAL  DEFAULT    4 
     5: 00000000004001ce     0 SECTION LOCAL  DEFAULT    5 
     6: 00000000004001d8     0 SECTION LOCAL  DEFAULT    6 
     7: 00000000004001f8     0 SECTION LOCAL  DEFAULT    7 
     8: 0000000000400210     0 SECTION LOCAL  DEFAULT    8 
     9: 0000000000400230     0 SECTION LOCAL  DEFAULT    9 
    10: 0000000000400250     0 SECTION LOCAL  DEFAULT   10 
    11: 0000000000600250     0 SECTION LOCAL  DEFAULT   11 
    12: 0000000000600390     0 SECTION LOCAL  DEFAULT   12 
    13: 00000000006003b0     0 SECTION LOCAL  DEFAULT   13 
    14: 0000000000000000     0 SECTION LOCAL  DEFAULT   14 
    15: 0000000000000000     0 SECTION LOCAL  DEFAULT   15 
    16: 0000000000000000     0 SECTION LOCAL  DEFAULT   16 
    17: 0000000000000000     0 SECTION LOCAL  DEFAULT   17 
    18: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS function_call_c.asm
    19: 000000000040023d     0 NOTYPE  LOCAL  DEFAULT    9 _exit
    20: 00000000006003b0     0 NOTYPE  LOCAL  DEFAULT   13 msg_format
    21: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
    22: 0000000000600250     0 OBJECT  LOCAL  DEFAULT   11 _DYNAMIC
    23: 0000000000600390     0 OBJECT  LOCAL  DEFAULT   12 _GLOBAL_OFFSET_TABLE_
    24: 00000000006003b5     0 NOTYPE  GLOBAL DEFAULT   13 _edata
    25: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@@GLIBC_2.2.5
    26: 00000000006003b8     0 NOTYPE  GLOBAL DEFAULT   13 _end
    27: 0000000000400230     0 NOTYPE  GLOBAL DEFAULT    9 _start
    28: 00000000006003b5     0 NOTYPE  GLOBAL DEFAULT   13 __bss_start

Histogram for bucket list length (total of 1 buckets):
 Length  Number     % of total  Coverage
      0  0          (  0.0%)
      1  1          (100.0%)    100.0%

Version symbols section '.gnu.version' contains 2 entries:
 Addr: 00000000004001ce  Offset: 0x0001ce  Link: 3 (.dynsym)
  000:   0 (*local*)       2 (GLIBC_2.2.5)

Version needs section '.gnu.version_r' contains 1 entries:
 Addr: 0x00000000004001d8  Offset: 0x0001d8  Link: 4 (.dynstr)
  000000: Version: 1  File: libc.so.6  Cnt: 1
  0x0010:   Name: GLIBC_2.2.5  Flags: none  Version: 2

问题是:

  • 为什么执行失败,是代码错误吗?还是我编译的方式不对?

【问题讨论】:

  • 尝试将动态链接器(64 位)添加到您的 LD 命令中。类似ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 function_call_c.o -lc
  • @MichaelPetch 这确实解决了执行a.out 时的问题,现在它可以执行了。但它没有将 msg 打印到控制台,我会检查更多信息,谢谢。
  • 关于您的打印问题,很可能是因为您使用int 0x80 退出而不是C 库。 int 0x80 sys_exit 调用不会刷新 C 库输出缓冲区。在字符串的末尾添加一个 Linux 行尾字符,它应该打印出来,即:msg_format db "hello",0x0a。与 64 位代码一样,您应该考虑避免使用 int 0x80 以支持 syscall(参见此 tutorial
  • 您可以调用 Cexit 函数,而不是使用 sys_exit 系统调用。您不必在字符串上添加额外的行尾字符,因为exit 应该在终止程序之前刷新输出缓冲区。
  • @MichaelPetch 我将字符串行更改为msg_format db 'hello world',0x0A,0,并通过call exit 退出,现在代码完美运行。干杯,您的建议正是代码所需要的!

标签: c linux assembly yasm


【解决方案1】:

刚刚运行了您的示例并遇到了同样的问题。
原因是我的情况是不正确的动态链接器(ELF 解释器)。
要验证这是问题所在,请输入 file ./a.out 您应该会得到与以下输出类似的内容:
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld64.so.1, not stripped

ld 的默认选择动态链接器 (/lib/ld64.so.1) 在大多数 Linux 系统上不存在,因此存在问题。您可以使用 patchelf (apt-get install patchelf) 和以下命令来纠正此问题:

patchelf --set-interpreter [path_to_interpreter] ./a.out

您可以通过键入file /bin/ls 获得应用作 ELF 解释器的正确路径 - 将打印标准动态链接器路径。 (另外,readelf 可以打印这些内容等等)。


最好的方法首先是使用ld-dynamic-linker标志来正确设置elf header中的INTERP部分。

另请参阅this answer,了解有关将 asm 构建/链接到静态或动态二进制文件(无论是否使用 libc)的更多详细信息。

【讨论】:

  • 在我的机器上,file a.out 会打印出a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped。而且,file /usr/bin/ld 将打印 /usr/bin/ld.bfdld 程序适用于不调用 c 函数的其他汇编代码,所以我猜 ld 程序本身存在并且可以工作。
  • 此文件:/lib/ld64.so.1 是否存在于您的计算机上?
  • @EricWang :这是因为当您使用 GCC 时,默认包含 C 运行时。将您的 _start 标签更改为 mainC 运行时包含一个 _start 标签,该标签执行初始化并调用您提供的标签 main。如果您希望绕过 C 运行时启动,您必须告诉 GCC 不包括它们。试试gcc function_call_c.o -nostartfiles
  • @EricWang 将_start 更改为main 并使用GCC 链接到C 运行时通常是首选机制。这会正确设置 C 运行时环境(某些 C 库需要这样才能正常工作)。您也可以使用retmain 返回以退出您的程序。如果您确实将 _start 更改为 main 并使用 GCC 您可以将其与 GCC 链接,如下所示:gcc function_call_c.o
  • @EricWang:如果你真的阅读了我包含的链接,你会看到关于使用 gcc(和 nasm 或 yasm)链接 32 位或 64 位 asm 代码的各种方法的完整说明.用yasm组装,用gcc链接。这个答案甚至有我用来组装和链接静态二进制文件的脚本,来自定义_start的asm源。
【解决方案2】:

正如@Peter Cordes 所建议的那样,我想给出一个总结问题和其他答案下的 cmets 的答案,以便将来的搜索者更清楚。

原代码&编译/链接策略的问题:

  • 最好使用main 而不是_start。原因:因为(正如@Michael Petch 的评论中最初提到的),C 运行时包含_start 标签,当使用gcc 链接时,这是冲突的。尽管可以通过在链接时向 gcc 添加-nostartfiles 选项来解决此问题,但简单的gcc xxx.o 更简单。所以,只需使用main 而不是_start

  • 通过ret 而不是int 0x80 退出主程序。原因:因为(最初在 @Michael Petch 的评论中提到),ret 将有助于刷新标准输出,而 int 0x80 不会,所以使用 int 0x80 将看不到 @ 987654334@ 在控制台中。调用C函数exit是另一种解决方案,但它是对c的额外函数调用,所以使用ret效率更高,它会从c运行时返回_start

  • 通过gcc 而不是ld 更好地链接。原因:因为(正如@Peter Cordes 和@Michael Petch 的 cmets 中提到的),C 运行时默认包含在 gcc 中,所以在使用 gcc 时不需要添加像 -lc 这样的选项.但是ld 不包括 C 运行时。所以,再一次,使用gcc 会更简单。

更多详情,请参考问题下的cmets和其他答案。


(所以,按照上面的解释。)

解决问题的步骤

  • 在汇编源代码中将_start 更改为main
  • 通过ret而不是int 0x80退出程序。
  • 通过yasm编译,例如yasm -f elf64 function_call_c.asm -g dwarf2
  • 通过gcc链接,例如gcc function_call_c.o

(这是新版本的代码。)

function_call_c.asm:

; yasm assembly program, instruction - call c function
; compile: yasm -f elf64 function_call_c.asm -g dwarf2 && gcc function_call_c.o
; execute: ./a.out
; check printed char count (immediately after execution): echo $?

section .data
msg_format db 'hello world',0x0A,0

section .text
extern printf
extern exit
global main

main:
    mov rax, 0
    lea rdi, [msg_format]
    call printf

_exit:
    ; eax now contains count of chars printed, due to previous 'printf' call,
    ret; return from main, this will flush the output,

【讨论】:

  • 我在我的一个 cmets 中简要提到了它。一旦你开始使用 C 运行时并且它的_start 标签是入口点,它最终会在初始化之后调用main。因为mainCALLed,所以你可以像对待任何其他函数一样对待main,并执行ret 来返回而不是call exit。返回值放在ret 之前的EAX 中。您不能从_start 执行ret,因为没有C 运行时(堆栈上没有返回地址)可以返回。使用ret 也会在终止前刷新缓冲区。
  • @MichaelPetch 答案已更新,现在我更好地理解了_startmain 之间的关系,很棒的提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 2011-01-29
  • 2019-01-25
  • 2020-08-01
  • 2021-11-29
相关资源
最近更新 更多