【问题标题】:gdb error- "not in executable format: file format not recognized"gdb 错误-“不是可执行格式:文件格式无法识别”
【发布时间】:2019-03-19 15:04:51
【问题描述】:

在尝试调试(在编译和链接之后)名为 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

看了answer之后,我明白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

【问题讨论】:

  • 你使用什么命令来构建这个文件,来自什么来源?前 10 行是什么样的? head hello_world。如果它是真的 ASCII 文本,请将其发布在您的问题中,以便我们告诉您它实际上是什么类型的文件。实际的 ELF 可执行文件永远不会被误认为是 ASCII 文本——它们以二进制的非 ASCII“幻数”开头。
  • 正如彼得问的,你是如何组装和链接的?您是否加载了正确的文件(链接器的输出)?
  • 所以,hello_world 是您的来源。您真的应该按照惯例将其命名为 hello_world.asmld 默认生成名为 a.out 的输出,因此将其加载到 gdb 中。您可以使用-o 选项指定其他名称。确保不要覆盖您的输入:)

标签: debugging assembly gdb x86-64


【解决方案1】:
   ubuntu@ubuntu:~$ nasm -felf64 hello_world
   ubuntu@ubuntu:~$ ld hello_world.o

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

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

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


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

【讨论】:

  • @fuz: GNU binutils ld 在输入文件名之前或之后接受 -o output。手册页仅记录在输入之前放置选项,但实际上它可以以任何方式工作。我会保留您的编辑,因为它确实与文档相符。
  • 并非所有工具链都以这种方式工作,因此传播这种坏习惯并不是一个好主意。 POSIX 标准的Utility Syntax Guidelines 指定选项位于操作数之前; GNU 项目是不遵守这一约定的主要参与者。
猜你喜欢
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
相关资源
最近更新 更多