What is the difference between executable file in elf format and relocatable file in elf format?
A relocatable file holds code and data suitable for linking with other object files to create an executable or a shared object file. An executable file holds a program suitable for execution; the file specifies how exec (BA_OS) creates a program's process image.
A relocatable object file holds sections containing code and data. This file is suitable to be linked with other relocatable object files to create dynamic executable files, shared object files, or another relocatable object. A dynamic executable file holds a program that is ready to execute.
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.
In computing, the Executable and Linkable Format (ELF, formerly named Extensible Linking Format), is a common standard file format for executable files, object code, shared libraries, and core dumps.
As it can be seen in the image below, relocatable ELF goes as the input to the linker, whereas the executable ELF is the product of the linker.
as you know every compiled executable file is a binary file with address relative and absolute, so relocatable format is a format in which function and other symbols still have there names definition in other word functions and variables are not bound to any specific address. Instead, the addresses are still symbols
look :
unix > gcc -c main.c
unix > readelf --symbols main.o
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 FILE LOCAL DEFAULT ABS main.c
2: 00000000 0 OBJECT GLOBAL DEFAULT 3 buf
3: 00000000 0 OBJECT GLOBAL DEFAULT 1 main
unix > gcc main.c -o main
unix > readelf --symbols main
Num: Value Size Type Bind Vis Ndx Name
53: 08048460 2 FUNC GLOBAL DEFAULT 13 __libc_csu_fini
54: 08048462 0 FUNC GLOBAL HIDDEN 13 __i686.get_pc_thunk.bx
55: 0804a018 4 OBJECT GLOBAL DEFAULT 13 bufp0
do you see what i'm talking about
most of the time we use them as static library
look to the example here :
#ifndef MATH_H
#define MATH_H
int add(int a, int b);
#endif
/* math_test.c */
#include <stdio.h>
#include "math.h"
int main(void)
{
int result = add(1, 2);
printf("result: %d\n", result);
return 0;
}
try to compile it
unix > gcc math_test.c -o math_test
/tmp/cclRibQq.o: In function `main':
math_test.c:(.text+0x19): undefined reference to `add'
collect2: ld returned 1 exit status
that due to function add
has no body in math_test.c
so we can do the flowing:
int add(int a, int b)
{
return a + b;
}
then compile it as a relocatable using gcc
unix > gcc -c math.c # Create relocatable obj file (math.o)
unix > readelf -h math.o | grep Type # Read math.o with readelf
Type: REL (Relocatable file) # and verify its type
then you can link it with linker like this :
unix > gcc math_test.c math.o -o math_test
unix > ./math_test
result: 3
a great article about the difference between executable file in elf format and relocatable file in elf format you can find here
ELF executable, as we can understand from its name, is a file that can be executed. This file can be generated from C code for example.
The process of relocation is fixing the addresses of labels and symbols which were created in the code. For example, if you write a program in assembly language and you look at the listing file of your source code, you'll find some places where [00000000] is written instead of a label mentioned at this line. This zeroes mean that he linker uses relocation in order to fix the address the its future value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With