首页 > 解决方案 > gdb 错误-“不是可执行格式:文件格式无法识别”

问题描述

在尝试调试(在编译和链接之后)名为 hello_world 的程序集 86-64x 程序时,我收到一个 gdb 错误“不是可执行格式:文件格式无法识别”。

ubuntu@ubuntu:~$ gdb hello_world
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
"/home/ubuntu/hello_world": not in executable format: File format not recognized

我使用 Ubuntu 64x 操作系统和 gdb 8.1.0 64x。

我一直在寻找其他答案,但不明白该怎么做,或者解决方案是针对 mac OS 的。

跑步时

`ubuntu@ubuntu:~$ file hello_world

我有

hello_world: ASCII text

看了这个答案后,我明白 gdb 不知道如何处理这个文件,但我不知道如何更改文件的格式。

我的 hello_world 程序:

global _start

section .text

 _start:
  mov rax,1
  mov rdi,1
  mov rsi,message
  mov rdx,13

  syscall

  mov rax,60
  xor rdi,rdi

  syscall

  section .data
  message: db "Hello, World",10

我已使用以下命令进行编译和链接:

   ubuntu@ubuntu:~$ nasm -felf64 hello_world
   ubuntu@ubuntu:~$ ld hello_world.o

标签: debuggingassemblygdbx86-64

解决方案


   ubuntu@ubuntu:~$ nasm -felf64 hello_world
   ubuntu@ubuntu:~$ ld hello_world.o

hello_world 你的源文件;这就是你运行 NASM 的原因。通常你会命名一个 NASM 源文件hello_world.asm,比如 C hello_world.c

的默认输出文件lda.out,因此您的命令创建了一个名为a.out. 如果要创建一个名为 的可执行文件hello_world,则需要使用
ld -o hello_world hello_world.o.

(这将覆盖您的源,除非您首先将其重命名为.asm. 这就是约定在源文件上使用扩展名的原因。)


ls -lcrt您可以通过运行按 inode 更改时间对目录列表进行排序来获得提示。你会a.out在底部看到 after hello_world.o,它会提醒你ld创建了那个而不是hello_world


推荐阅读