Creating ELF instead of a.out - c

I need to generate a simple "Hello World" ELF32 executable using gcc.
I don't seem to have the gcc-elf command though.
Is it possible to create ELF binaries instead of a.out without building gcc again?
(I'm assuming it should be possible with some options, but am unsure how to proceed)

Check the file a.out
$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
I believe the default name is retained as a.out but the format is ELF.

a.out is very old, we're talking kernel version 1.2 of linux. Assuming you are operating on any remotely recent linux platform, you are generating elf executables by default. Use the file command on the output executable to verify. E.g.:
$ file server
server: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

Related

32 bit linux executable failing with Exec format error. Binary file not executable

I built a 32 bit binary on linux 64 bit machine and trying to run.
It fails with error Exec format error. Binary file not executable.
./test
./test: Exec format error. Binary file not executable.
file test
test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=b8710a266a084a5d6e8d63a585cc6824e382a5e6, not stripped
Machine details
uname -a
Linux den03cif 4.1.12-124.52.5.el7uek.x86_64 #2 SMP Wed Jul 21 14:58:50 PDT 2021 x86_64 x86_64 x86_64 GNU/Linux
I'm pretty sure its because your machine architecture is x86_64 but you are trying to run something that was meant for i386 architecture

run 32bit elf on aarch64

I have installed Debian on qemu 64-bit ARM (followed this tutorial)
uname -a
Linux test 4.9.0-7-arm64 #1 SMP Debian 4.9.110-1 (2018-07-05) aarch64 GNU/Linux
and I am trying to run 32 bit elf files on it, but some work some don't:
bash: ./file_2: cannot execute binary file: Exec format error
running file command on the file that runs, I get:
file_1: ELF 32-bit LSB executable, ARM, EABI4 version 1 (SYSV), statically linked, not stripped
and the one that does not run:
file_2: ELF 32-bit LSB executable, ARM, version 1 (ARM), statically
linked, stripped
and both work on 32bit system(armv7l)
the only difference is that SYSV/ARM version.
is there any work around without recompiling the kernel?(read this post)
As the linked post suggests, this requires CONFIG_COMPAT to be enabled in the kernel. On the other hand I would be surprised if your kernel didn't have it -- the Debian 4.9.0-4 kernel I have from doing that tutorial does set CONFIG_COMPAT. You can check whether your kernel has it enabled by looking at the config file for it which will be in /boot/ in the guest. If it's missing then you need a new kernel, and nothing else will help.
However in your case you do have CONFIG_COMPAT, and some executables work. The difference between the ones that work and the ones that don't is that the working ones are EABI, and the non-working ones are OABI. OABI is an old and obsolete ABI for 32-bit Arm Linux binaries (the "O" stands for "old", and it's been a bad choice for a decade or so...) and it is not supported by the 64-bit kernel's CONFIG_COMPAT. You'll need to rebuild those binaries from source as EABI binaries if you want to run them under a 64-bit kernel.

ltrace exit with "is not an ELF file" for an executable that file finds "ELF 32-bit LSB shared object" on armv7

I am working on a armv7-a and try to use ltrace installed via buildroot.
# ltrace /usr/bin/ssh-keygen -t ecdsa -b 256
"/usr/bin/ssh-keygen" is not an ELF file
# file /usr/bin/ssh-keygen
/usr/bin/ssh-keygen: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, stripped
Upstream supports armv7, but says:
The following targets are currently (at least somewhat) supported.
Some of them may be more or less broken in reality
Has one made it work please?

Cross Compiled Binary Does Not Exist?

I cross compiled a program on Ubuntu 12.04 running on x86 using gcc-arm-linux-gnueabi and binutils-arm-linux-gnueabi and compiling with arm-linux-gnueabi-gcc instead of gcc with my target architecture being ARM. It compiles fine with no errors or warnings.
When I try to run it on the ARM machine (Pandaboard - also running Ubuntu 12.04) I get:
bash: ./sttyl: No such file or directory
I know the file is there and it has the proper permissions:
-rwxrwxr-x 1 haziz haziz 8.5K Feb 10 10:34 sttyl
The output of file sttyl is
sttyl: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31,BuildID[sha1]=0x6d504f7d84b93603122223a89e2b5960c840309f, not stripped
When I compile it natively on the Pandaboard it compiles and runs fine. This is the output of file sttyl on the natively compiled copy:
sttyl: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31,BuildID[sha1]=0x9897c785266c5b7cdf580e08091aba239db84ecf, not stripped
What am I doing wrong? Moreover if I had made a mistake in the cross compilation I would have expected the shell/kernel to tell me effectively that the executable is for the wrong architecture not that it does not exist!
It isn't telling you that ./sttyl doesn't exist.
It's telling you that it spawned a new process, which exited with error No such file or directory. No information is provided about which file or directory was missing.
For example, a shell script starting with #!/bin/specialsh could generate that error if the interpreter /bin/specialsh was missing, even though the script existed and was executable.
Try using strace to find out what call (and path) caused the error.

Are my Go tools built for the wrong architecture?

I'm a big fan of Go language, and now I am trying to explore other architectures than x86/amd64. I build Go for ARM, and it seems to have good support for that target. All libraries build successfully, and tests fail (because it's trying to run ARM test binaries on my amd64 system).
After all, I have 5g/5l in my $GOBIN folder and they produce valid and working ARM binaries.
But what's happening to other Go tools:
5a: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
5c: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
5g: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
5l: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
6cov: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
6nm: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
6prof: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
cgo: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
ebnflint: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
godefs: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
godoc: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
gofix: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
gofmt: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
goinstall: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
gomake: POSIX shell script, ASCII text executable
gopack: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
gopprof: a perl script, ASCII text executable
gotest: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
gotry: a bash script, ASCII text executable
gotype: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
govet: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
goyacc: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
hgpatch: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
See? cgo is an ARM binary, gofix and gofmt are ARM binaries as well. I thought it's expected that if I provide $GOHOSTOS/$GOHOSTARCH variables I will cross-compile my apps? How to get cgo work for ARM target?
The Go linux/arm port is incomplete. For example, the cgo runtime for linux/arm is unimplemented. See $GOROOT/src/pkg/runtime/cgo/gcc_arm.S and $GOROOT/src/pkg/runtime/cgo/gcc_linux_arm.c.
[cgo on arm.] It is not planned for Go 1.

Resources