You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试x86-64汇编程序时GDB提示“无法识别文件格式”问题求助

Fixing "not in executable format" Error When Debugging x86-64 Assembly with GDB on Ubuntu

Got it, let's break down why you're hitting this error and fix it right away. The core issue is simple: you're trying to debug your plain-text assembly source file instead of the actual executable binary generated by your linker!

Here's what's going wrong in your current workflow:

  • When you run nasm -felf64 hello_world, it creates an object file hello_world.o (this is not an executable yet)
  • When you run ld hello_world.o without specifying an output name, ld defaults to spitting out an executable called a.out—not hello_world
  • So when you run gdb hello_world, you're pointing GDB at your original assembly source (which is why file hello_world says it's ASCII text)

Step-by-Step Fix

  1. Recompile and link with explicit output filenames
    Use the -o flag to tell both tools exactly what to name their outputs—this eliminates confusion between source, object, and executable files:

    # Assemble the source into an object file
    nasm -felf64 hello_world -o hello_world.o
    # Link the object file into a named executable
    ld hello_world.o -o hello_world
    

    Pro tip: If your assembly file uses a .asm suffix (like hello_world.asm), include it in the nasm command to make your intent even clearer.

  2. Verify the executable format
    Run file hello_world again—you should see output like this, confirming it's a valid 64-bit ELF executable:

    ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

  3. Launch GDB on the correct executable
    Now you can debug the actual binary without format errors:

    gdb ./hello_world
    

Quick Tip to Avoid This Later

Always double-check which file you're targeting with GDB. Run ls -l in your directory—you'll spot the difference instantly:

  • The source file will be small (just a few lines of text)
  • The object file (hello_world.o) will be slightly larger
  • The executable (hello_world) will be bigger and have executable permissions (marked with an x in the permissions column)

内容的提问来源于stack exchange,提问作者Sharon1111

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:20:52