Simple program compilation#
make hello
./hello4 compilation phases#
gcc -E -o hello.i hello.c # Stop after pre-processinggcc -S -o hello.s hello.i # Stop after compilergcc -c hello.s # Stop after assemblergcc -o hello hello.o # Stop after linkerTypical buildsystem behavior#
#gcc -c main.c -o main.o
#gcc -c hello.c -o hello.o
#gcc -o hello main.o hello.o
make -B helloInspect file types#
file hello.c
file hello.o
file helloELF Object Files#
readelf --file-header hello.oreadelf --sections hello.oreadelf --symbols hello.oreadelf -x .data hello.oreadelf -x .rodata hello.o
readelf -p .rodata hello.oreadelf --symbols hello.oobjdump --disassemble=f_defined hello.oreadelf --relocs hello.oELF Executables#
readelf -h helloreadelf --sections helloreadelf --symbols helloobjdump --disassemble=f_defined hello
objdump --disassemble=f_static hello
objdump --disassemble=f_external helloDynamic symbol resolution#
objdump --disassemble=main helloNote the call to printf@plt. Target address is in .plt section.
objdump -dj .plt hello.plt contains short linker-generated trampoline to the value in .got.plt section:
readelf -x .got.plt helloreadelf --relocs helloLoader view#
readelf --segments hello./hello blockcat /proc/$(pidof hello)/maps | grep helloreadelf --segments hello | grep LOAD -A1Dynamic loader operation#
readelf -p .interp helloreadelf --dynamic helloLD_SHOW_AUXV=1 ./hello blockcat /proc/$(pidof hello)/maps | grep ld-linuxLD_DEBUG=all ./helloDynamic library development#
#gcc -Wall -Wextra -c hello.c -o hello.o
#gcc -Wall -Wextra -c main_shared.c -o main_shared.o
#gcc shared_utils.c -o libshared_utils.so -shared -fPIC
#gcc hello.o main_shared.o -o hello_shared -L. -lshared_utils
make -B hello_sharedfile libshared_utils.soreadelf -h libshared_utils.soreadelf --dynamic hello_sharedreadelf --relocs hello_sharedLD_LIBRARY_PATH=. ./hello_shared blockcat /proc/$(pidof hello_shared)/maps | grep libshared_utils.soLD_DEBUG=all LD_LIBRARY_PATH=. ./hello_shared blockStatically linked executable#
#gcc -Wall -Wextra -c hello.c -o hello.o
#gcc -Wall -Wextra -c main.c -o main.o
#gcc -static hello.o main.o -o hello_static
make hello_statichello_staticfile hello_staticreadelf -h hello_staticobjdump --disassemble=f_defined hello_static