Issue
I have to compile a simple binary for a very old Linux system. I have no gcc/build libs available on the target machine but I do have access to the machine.
I am having trouble compiling the code on my machine and having it execute on target machine.
I am copying libc.so to my local machine and trying to compile and link the program so that it will execute on the target machine.
I have copied the libc from the target machine to mine and tried compiling it with my target executable.
this has gotten closes to a successful execution:
gcc -nostdlib ./libc-[version].so myFile.c -emain -o outfile.out
upon execution a very simple PoC test program runs, and then seg faults upon exit. the actual program simply seg faults.
It seems I have somewhat of a lack of understanding of linking. Any help?
Solution
It seems I have somewhat of a lack of understanding of linking.
You do. A "normal" user-level program doesn't start executing at main
, it starts at _start
.
The _start
symbol typically comes from crt0.o
file (part of libc), and knows how to "interface" between the way the kernel supplies arguments, and the way main
expects to find them. It also initializes various data that must be initialized before main
runs (e.g. stdio
streams).
What you want to do then is:
- Find out the actual link command that
gcc main.o
performs. You can do so by adding the-v
flag. - Replicate such command, providing
crt0.o
and other input files, appropriate for your version of target libc.
It might be easier to spin up a VM with the OS matching your target (and with old tools that target it), and build your program inside of that VM.
Otherwise you'll likely have to set up a full cross-compiler environment (which includes libc and all other libraries you need). This is not a trivial proposition, and is certainly not accomplished by copying libc.so
from the target machine. But it is well-documented and is certainly doable (with some skill).
Answered By - Employed Russian