Sharon1111
Sharon1111

Reputation: 11

gdb error- "not in executable format: file format not recognized"

When trying to debug (after compiling and linking) assembly 86-64x program called hello_world, I got a gdb error "not in executable format: file format not recognized".

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

I use Ubuntu 64x OS and gdb 8.1.0 64x.

I have looked for other answers, but couldn't understand what to do or the solution was for mac OS.

When running

`ubuntu@ubuntu:~$ file hello_world

I got

hello_world: ASCII text

After looking this answer I understood that gdb doesn't know what to do with this file, but I didn't figure out how to change the file's format.

My hello_world program:

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

I have compiled and linked using the next commands:

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

Upvotes: 1

Views: 5202

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 365576

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

hello_world is your source file; it's what you ran NASM on. Normally you'd name a NASM source file hello_world.asm, like a C hello_world.c.

The default output file for ld is a.out, so your command created an executable called a.out. If you want to create an executable called hello_world, you need to use
ld -o hello_world hello_world.o.

(Which would overwrite your source unless you first renamed it to .asm. This is why the convention is to use an extension on source files.)


You could have gotten a hint by running ls -lcrt to sort a directory listing by inode-change time. You'll see a.out at the bottom, after hello_world.o, which will remind you that ld created that instead of hello_world.

Upvotes: 7

Related Questions