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

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?

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.

Related

qemu valid dtb and unrecognized/unsupported machine ID

I am following this tutorial to run Raspbian on my Windows PC, but it does not work.
I renamed my system img and kernel to fit the command, but no success.
valid dtb and unrecognized/unsupported machine ID
r1=0x00000183, r2=0x00000100
r2[]=05 00 00 00 01 00 41 54 01 00 00 00 00 10 00 00
Available machine support:
ID (hex) NAME
ffffffff Generic DT based system
ffffffff ARM-Versatile (Device Tree Support)
The command I was executing:
qemu-system-arm -kernel kernel-qemu-4.4.34-jessie -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -drive "file=2017-03-02-raspbian-jessie.img,index=0,media=disk,format=raw"
You've cut off the first part of the error message, which is confusing. The full text is "Error: invalid dtb and unrecognized/unsupported machine ID".
What the kernel is trying to tell you here is that you must provide it with the correct device tree blob (dtb) file for the board you are running it on. Newer kernels can't boot without a dtb. You need to pass QEMU the correct file via its -dtb option, which in this case is "versatile-pb.dtb". That file should be built when you build the kernel from source, or if you got your kernel from somebody else they should also provide the dtb.

Linux Executable fails to run with error “/usr/bin/strace: exec: No such file or directory” on running strace

I need to analyse linux binary executable file using strace for capturing the system calls.
On running the command:
/usr/bin/strace ./005f32fffe1da3bc100e7dcd8b2f8f2c
I got this error:
execve("./005f32fffe1da3bc100e7dcd8b2f8f2c", ["./005f32fffe1da3bc100e7dcd8b2f8f"...], 0x7fffd9d0a120 /* 53 vars */) = -1 ENOENT (No such file or directory) fstat(2, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0 write(2, "/usr/bin/strace: exec: No such f"..., 49/usr/bin/strace: exec: No such file or directory ) = 49 getpid() = 3699 exit_group(1) = ? +++ exited with 1 +++
My file type is 32 bit ELF binary.
file 005f32fffe1da3bc100e7dcd8b2f8f2c
005f32fffe1da3bc100e7dcd8b2f8f2c: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-, stripped
My machine is 64 bit running Ubuntu and I have libc6-i386 lib32stdc++6 lib32gcc1 lib32ncurses5 lib32z1 installed.
This binary is strange: it is linked to use /lib/ld- as a dynamic loader.
The 32-bit i386 dynamic loader is usually called /lib/ld-linux.so.2. We can assume that your binary has been corrupted in some way.
In your other comment, you said that the binary will not run without strace (because /lib/ld- does not exist). Running the binary under strace can magically make the binary work.
You should be able to run it under strace by using explicit loader invocation:
strace -ff /lib/ld-linux.so.2 ./005f32fffe1da3bc100e7dcd8b2f8f2c
Update:
I am getting some system calls like this :
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
stat64("/lib/i686/sse2", 0xffe9b300) = -1 ENOENT (No such file or directory)
+++ exited with 127 +++
I am not sure what kind of system calls are these ?
This is the dynamic linker attempting to load your program, and using access and stat64 system calls.
how to run the following file with strace? How do know which library to use in strace command with -ff option based on file command output ?
file mosquitto_pub
mosquitto_pub: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=7ced951dc3a859a6829feb46fd5cf757a6073361, not stripped
This is a 64-bit binary with similarly corrupted /lib64/l dynamic loader.
The standard 64-bit loader for GLIBC on x86-64 is /lib64/ld-linux-x86-64.so.2, so the command you want is:
strace -ff /lib64/ld-linux-x86-64.so.2 ./mosquitto_pub

"IOError: Can't find a path to system files" for x86 full system on Mac

I'm trying to set up gem5 x86 full system on Mac OS Mojave (10.14)
First I did a git clone to get the gem5 sources, which are located at ~/gem5.
Then I ran scons build/x86/gem5.fast to build the whole thing. I had to change some of the -Werror flags to get it to compile, but it seems to work.
To test it, I ran build/x86/gem5.fast configs/example/se.py -c tests/test-progs/hello/bin/x86/linux/hello and got the following output:
gem5 Simulator System. http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.
gem5 compiled Jan 18 2020 15:28:50
gem5 started Jan 18 2020 17:48:35
gem5 executing on My-MacBook-Pro-208.local, pid 89984
command line: build/x86/gem5.fast configs/example/se.py -c tests/test-progs/hello/bin/x86/linux/hello
Global frequency set at 1000000000000 ticks per second
warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
0: system.remote_gdb: listening for remote gdb on port 7000
**** REAL SIMULATION ****
info: Entering event queue # 0. Starting simulation...
Hello world!
Exiting # tick 5941500 because exiting with last active thread context
I wanted to configure full system simulation, so I went to the "Full-System Stuff" section at http://gem5.org/Download and downloaded the Full System Files. I extracted the tar into ~/gem5/x86-system.
So now there's ~/gem5/x86-system/binaries which contains x86_64-vmlinux-2.6.22.9 and ~/gem5/x86-system/disks which contains linux-x86.img
In ~/.bash_profile I added export M5_PATH="/Users/me/gem5/x86-system".
However, when I run scons build/x86/tests/fast/quick, almost all of the tests fail. A lot of them have a failure like this:
...
File "/Users/me/gem5/configs/common/SysPaths.py", line 62, in __call__
raise IOError("Can't find a path to system files.")
IOError: Can't find a path to system files.
I also tried to run build/x86/gem5.fast configs/example/fs.py but I get the following error:
...
File "/Users/me/gem5/configs/common/SysPaths.py", line 71, in __call__
raise IOError("Can't find file '%s' on path." % filename)
IOError: Can't find file 'x86root.img' on path.
I'm not sure what part of configuration I'm missing. The docs and google searches aren't giving any working solutions...

How do I configure crosstools-ng sample for cross compiling toolchain for my router?

I want to cross compile shadowsocks for my router ( netcore q3, made in china) on CentOS 7. So I got crosstools-ng, I ssh into the router, and get following info (The router is on a Tomato Firmware 1.28.0000 MIPSR2-131 K26 Max by shibby), I used How to build a cross compiler for your Raspberry Pi as my reference
$less /proc/cpuinfo
system type : Broadcom BCM53572 chip rev 1 pkg 8
processor : 0
cpu model : MIPS 74K V4.9
BogoMIPS : 149.50
cpu MHz : 300
wait instruction : no
microsecond timers : yes
tlb_entries : 32
extra interrupt vector : no
hardware watchpoint : yes
ASEs implemented : mips16 dsp
shadow register sets : 1
VCED exceptions : not available
VCEI exceptions : not available
unaligned_instructions : 0
I also copied a .so file from /lib
$ scp root#router:/lib/libc.so.0 .
[oglop#localhost tmp]$ readelf -h libc.so.0
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: DYN (Shared object file)
Machine: MIPS R3000
Version: 0x1
Entry point address: 0x6d50
Start of program headers: 52 (bytes into file)
Start of section headers: 260672 (bytes into file)
Flags: 0x50001007, noreorder, pic, cpic, o32, mips32
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 5
Size of section headers: 40 (bytes)
Number of section headers: 18
Section header string table index: 17
files in /lib on the router
root#unknown:/tmp/home/root# ls /lib
ld-uClibc.so.0 libgcc_s.so.1 libutil.so.0
libc.so.0 libm.so.0 modules
libcrypt.so.0 libnsl.so.0 openvpn_plugin_auth_nvram.so
libdl.so.0 libpthread.so.0
Can I get all the information from these two? Which sample should I choose ?
So from the .so file :
endianness : little endian
bitness: ?
abi: ?
target os: linux or bare-metal?
c library: glibc, musl or uClibc ?
...
I guess I should starts with mips-unknown-linux-uclibc and change it to little endian ?
[oglop#localhost tmp]$ ct-ng list-samples | grep mips
[G.X] mips64el-n32-linux-uclibc
[G.X] mips64el-n64-linux-uclibc
[G..] mips-ar2315-linux-gnu
[G..] mipsel-sde-elf
[G..] mipsel-unknown-linux-gnu
[G..] mips-malta-linux-gnu
[G..] mips-unknown-elf
[G..] mips-unknown-linux-uclibc
root#unknown:/tmp/home/root# cat /proc/version
Linux version 2.6.22.19 (root#tomato) (gcc version 4.2.4) #16 Tue Aug 4 00:16:31 CEST 2015
I tried to figure out the libc version tomato uses, but
root#unknown:/tmp/home/root# /lib/libc.so.0
Segmentation fault
root#unknown:/tmp/home/root# ldd --version
-sh: --version: not found
Or Can I get the info I need from tomato source shibby branch on github?

How to build FLAT userspace binary for ucLinux on buildroot with arm2010q1 toolchain

I have a buildroot system for cortex-M3 without MMU based on ucLinux kernel 2.6.33. Support for FLAT, ZFLAT, SHARED_FLAT enabled in kernel.
Inside buildroot everything is set to M3 etc.
I have working system with working kernel and busybox (which is only ok binary in userspace). There are no uClibc on system or any other libs.
I'm trying to build simplest userspace app like this: main(){}
I tried with different flags. Latest combination was like this:
^[[7m>>> zpm_t 1.0 Building^[[27m
/usr/bin/make -j5 FLAGS="-c -mthumb -march=armv7 -mfix-cortex-m3-ldrd" EXTRA_LDFLAGS="-Wl,-elf2flt,--static" CC="/mnt/repos/repos/github/nocturn_buildroot/output/host/usr/bin/ccache /mnt/repos/repos/github/nocturn_buildroot/output/host/usr/bin/arm-uclinuxeabi-gcc" LD="/mnt/repos/repos/github/nocturn_buildroot/output/host/usr/bin/arm-uclinuxeabi-ld" HEADERS="-I/mnt/repos/repos/github/nocturn_buildroot/output/target/usr/include/SDL" LIBS="-L/mnt/repos/repos/github/nocturn_buildroot/../nocturn_tools/arm-2010q1/arm-uclinuxeabi/libc/usr/lib" -C /mnt/repos/repos/github/nocturn_buildroot/output/build/zpm_t-1.0 all
make[1]: Entering directory /mnt/repos/repos/github/nocturn_buildroot/output/build/zpm_t-1.0'
/mnt/repos/repos/github/nocturn_buildroot/output/host/usr/bin/ccache /mnt/repos/repos/github/nocturn_buildroot/output/host/usr/bin/arm-uclinuxeabi-gcc -c -mthumb -march=armv7 -mfix-cortex-m3-ldrd -I/mnt/repos/repos/github/nocturn_buildroot/output/target/usr/include/SDL t.c
/mnt/repos/repos/github/nocturn_buildroot/output/host/usr/bin/ccache /mnt/repos/repos/github/nocturn_buildroot/output/host/usr/bin/arm-uclinuxeabi-gcc -Wl,-elf2flt,--static t.o -L/mnt/repos/repos/github/nocturn_buildroot/../nocturn_tools/arm-2010q1/arm-uclinuxeabi/libc/usr/lib -o t
make[1]: Leaving directory/mnt/repos/repos/github/nocturn_buildroot/output/build/zpm_t-1.0'
flat file seems to be ok:
$ arm-uclinuxeabi-flthdr output/target/opt/t
output/target/opt/t
Magic: bFLT
Rev: 4
Build Date: Wed Sep 24 00:28:31 2014
Entry: 0x45
Data Start: 0x1f00
Data End: 0x1f70
BSS End: 0x1fa0
Stack Size: 0x1000
Reloc Start: 0x1f70
Reloc Count: 0x29
Flags: 0x1 ( Load-to-Ram )
But on system I always have a segfault which is not so informative:
/opt # ./t
[ 23.200000]
[ 23.200000]
[ 23.200000] t: fault at 0xa0501dd4 [pc=0xa0501dd4, sp=0xa0503f28]
[ 23.200000] Invalid ISA state
[ 23.200000]
SEGV
What did I miss? It should be something very simple. Also - what to do with uClibc? Should it be compiled into every binary executable or should I place it on the system somehow?
I have linked my app with wrong library. I had to take pre-built one from thumb2 folder inside toolchain (for M3):
LIBS="-L$(TOPDIR)/../tools/arm-2010q1/arm-uclinuxeabi/libc/thumb2/usr/lib"
Just fixing it helps. As I have libc.a and libm.a as static archives - just fixing LIBS is enough. And no flags like -lm needed. During buildroot compilation it takes needed part of static libs and creates a working FLAT binary.