3 ms·
Manually with: gcc -c -O0 -no-pie main.c Probably also be worth throwing in -nostdlib while learning to keep it bare-bones, otherwise will need to link th
by sparkie 2y ago
Manually with:
gcc -c -O0 -no-pie main.c
Probably also be worth throwing in -nostdlib while learning to keep it bare-bones, otherwise will need to link the c runtime.
Assemble the emitted .S file:
as -o main.o main.S
Link with c runtime
ld -o main -lc main.o crt1.o crti.o crtn.o --entry main
If using -nostdlib, add your own start.S with a simple entry which calls main with no command line arguments.
BITS 64
%define SYS_exit 60
global _start
_start:
xor edi, edi
xor esi, esi
call main
xor eax, eax
mov al, SYS_exit
xor edi, edi
syscall
Assemble with:
nasm -felf64 -o start.o start.S
And link both:
ld -o main main.o start.o --strip-debug
Check the result:
objdump -x -S main
To keep even more minimal, add your own link.ld file instead of using the default:
OUTPUT_FORMAT("elf64-x86-64")
OUTPUT(main)
ENTRY(_start)
INPUT(main.o)
INPUT(start.o)
SECTIONS
{
PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000));
. = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;
.text : { *(.text) }
.eh_frame : { *(.eh_frame) ; }
.data : { *(.data) ; }
.bss : { *(.bss) ; }
}
Link with:
ld -T link.ld
- rramadass 2y agoNice! PSA: Read Hongjiu Lu's classic paper ELF: From The Programmer's Perspective as a preliminary to get an idea of how the above steps fit together.