I'm currently working on an implementation of MD5 just because I'm curious and want to get a better understanding of how hashing works. I have 1 file with all the code in it, called easy_md5.c
and I'm compiling it with the following Makefile:
easy_md5: easy_md5.c
gcc -c easy_md5.c -o easy_md5
clean:
-rm -f easy_md5.o
-rm -f easy_md5
I ran chmod x easy_md5
on the executable to give it permission, and yet I'm getting the following error when I try to run it:
[gabe@void easy_md5]$ ./easy_md5 hello
bash: ./easy_md5: cannot execute binary file: Exec format error
Running file easy_md5
shows the following:
easy_md5: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
and running arch
confirms my machine has x86-64 architecture. Since its a 64-bit executable, there's no reason it should throw a format error when I try to run it on my 64-bit machine. What is going wrong?
CodePudding user response:
You didn't create an executable at all. The -c
option tells gcc to compile only, and output a relocatable object file. You'd normally name that file easy_md5.o
and use it when linking, but the -o
option forced it to be named easy_md5
which looks like an executable name even though it isn't.
To compile and link in one step, just drop the -c
: gcc easy_md5.c -o easy_md5
.
There were a couple other hints:
I ran
chmod x easy_md5
on the executable
When you actually create an executable, this isn't necessary. gcc sets execute permissions on it for you.
Running file easy_md5 shows the following:
easy_md5: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
This indicates an object file. An actual executable is reported as something like
a.out: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=7108d6b0e7767b20e8db974561938d3bfcbbe12a, for GNU/Linux 3.2.0, not stripped