Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

elf

readelf

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?

like image 314
Eric Avatar asked Jan 03 '14 07:01

Eric


People also ask

What is the Readelf command in Linux?

readelf displays information about one or more ELF format object files. The options control what particular information to display. elffile... are the object files to be examined. 32-bit and 64-bit ELF files are supported, as are archives containing ELF files.

How ELF file is executed?

An ELF file consists of zero or more segments, and describe how to create a process/memory image for runtime execution. When the kernel sees these segments, it uses them to map them into virtual address space, using the mmap(2) system call. In other words, it converts predefined instructions into a memory image.

What is ELF in assembly language?

Relocatable ELF files produced by the assembler consist of: An ELF header. A section header table.

How an ELF file is loaded in memory?

ELF files are used by two tools: the linker and the loader. A linker combines multiple ELF files into an executable or a library and a loader loads the executable ELF file in the memory of the process.


1 Answers

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.
like image 171
paxdiablo Avatar answered Sep 27 '22 22:09

paxdiablo