readelf: Error: Not an ELF file - it has the wrong magic bytes at the start

Eric picture Eric · Jan 3, 2014 · Viewed 24k times · Source

I build a program and it works well(I mean that I can run this program). But when I use "readelf" to check whether there is debug information,errors come:

readelf: Error: Not an ELF file - it has the wrong magic bytes at the start    
readelf: Error: test/select: Failed to read file header

My linux distribution is Ubuntu-12. Somebody can help me?

Answer

paxdiablo picture paxdiablo · Jan 3, 2014

It may not actually be an ELF executable file. There are plenty of things that will run that are not ELF files (such as shell scripts, Perl files, Python py source and pyc compiled files). There are even things that will "run" without having an individual identifiable file at all (aliases or functions in your shell, bash built-ins and such).

I would first execute:

file /path/to/your/file

to see what sort of file it actually is, such as with:

pax> file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
         dynamically linked (uses shared libs), for GNU/Linux 2.6.26,
         BuildID[sha1]=0xd3280633faaabf56a14a26693d2f810a32222e51,
         stripped

Only if it's recognised as an ELF file should you try to treat it as such.

pax> readelf -h /bin/ls
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x804c1b4
  Start of program headers:          52 (bytes into file)
  Start of section headers:          111580 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         9
  Size of section headers:           40 (bytes)
  Number of section headers:         28
  Section header string table index: 27

For what it's worth, I have a backup script which executes just fine but which would fail your readelf assumption:

pax> file backup1.sh
backup1.sh: Bourne-Again shell script, ASCII text executable

pax> readelf -h backup1.sh 
readelf: Error: Unable to read in 0x253a bytes of section headers
readelf: Error: Not an ELF file - it has the wrong magic bytes at the start

As to what you do when you find out it isn't ELF format, that depends on what you're trying to ascertain, something you haven't actually specified. If all you want to do is run readelf on it, that won't work unless it's an ELF format file.

If you want a particular piece of information about the executable file, you need to tell us both:

  • what type it is (from file for example); and
  • the information you want.