我跟踪了this页并编译了以下代码
; assembly program that calls a C function on 64-bit Linux
;
; int main(void) {
; printf(fmt,1,msg1);
; printf(fmt,2,msg2);
; return 0;
;
; Assemble in 64-bit: nasm -f elf64 -o hp64.o -l hp64.lst hello-printf-64.asm
;
; Link: ld hp64.o -o hp64 -lc --dynamic-linker /lib/ld-2.7.so
; or maybe ld hp64.o -o hp64 -lc --dynamic-linker /lib/ld-linux-x86-64.so.2
; (the "-lc" option is needed to resolve "printf")
;---------------------------------------
section .data
fmt db "%u %s",10,0
msg1 db "Hello",0
msg2 db "Goodbye",0
section .text
extern printf
global _start
_start:
mov edx,msg1
mov esi,1
mov edi,fmt
mov eax,0 ; no f.p. args
call printf
mov edx,msg2
mov esi,2
mov edi,0 ; no f.p. args
call printf
mov ebx,0 ; return value
mov eax,1
int 0x80
通过
nasm -f elf64 -o hp64.o -l hp64.lst hello-printf-64.asm
ld hp64.o -o hp64A -lc --dynamic-linker /lib/ld-2.7.so
ld hp64.o -o hp64B -lc --dynamic-linker /lib/ld-linux-x86-64.so.2
可执行文件hp64A
和hp64B
均无法运行。
$ ./hp64A
bash: ./hp64A: No such file or directory
$ ./hp64B
bash: ./hp64B: No such file or directory
两者都是可执行文件。
$ ll
total 30
drwxrwxrwx 1 ar2015 ar2015 4096 Nov 7 23:23 ./
drwxrwxrwx 1 ar2015 ar2015 4096 Nov 7 22:46 ../
-rwxrwxrwx 1 ar2015 ar2015 928 Nov 7 22:47 hello-printf-64.asm*
-rwxrwxrwx 1 ar2015 ar2015 2960 Nov 7 23:21 hp64A*
-rwxrwxrwx 1 ar2015 ar2015 2976 Nov 7 23:21 hp64B*
-rwxrwxrwx 1 ar2015 ar2015 2448 Nov 7 23:21 hp64.lst*
-rwxrwxrwx 1 ar2015 ar2015 1104 Nov 7 23:21 hp64.o*
我的机器是
$ uname -a
Linux ar2015co 4.15.0-66-generic #75~16.04.1-Ubuntu SMP Tue Oct 1 14:01:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
如何运行这些可执行文件?