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

1

10

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?

Knickknack answered 3/1, 2014 at 7:48 Comment(1)
The file you're giving readelf is not an ELF file - I think that's pretty obvious. Without any more information that's all the help you can get. Perhaps show your readelf command line?Geraud
M
13

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.
Megaron answered 3/1, 2014 at 7:51 Comment(3)
you are just explaining how to check why it's failing. Great to know but how can you solve the problem with this information?Murdocca
@Magpie, you run file whateverYourFileIs to see what kind of file it is, as per my answer. I guarantee it's not an ELF executable (otherwise readelf would work). What you do after that depends on why you're trying to run readelf on it (ie, what information you're actually after), which the OP hasn't specified. I thought that would be self-evident but maybe I'm mistaken, so I'll clarify.Megaron
Ok thanks, but I can't tell you what @Knickknack is having trouble with and I take your point. It's not been made clear so more info is needed that is true.Murdocca

© 2022 - 2024 — McMap. All rights reserved.