I have a C source file which contains only one function which does nothing but returns a value to a function, I have generated an object file from it by using gcc -ffreestanding options to output an object file without standard library and entry point but when I try to use the gnu linker to output a binary file it adds extra assembly instruction to it the below is the source file
int test_function() {
return 0x4141;
}
I used this gcc command to generate an object file gcc -ffreestanding -c source.c -o source.o
I used this command for ld linker ld -o source.bin -Ttext 0x0 --oformat binary source.o
the result I got after using the linker is
dumped using ndisasm -b 32 source.bin
00000000 F30F1EFA rep hint_nop55 edx
00000004 55 push ebp
00000005 48 dec eax
00000006 89E5 mov ebp,esp
00000008 B8BABA0000 mov eax,0xbaba
0000000D 5D pop ebp
0000000E C3 ret
0000000F 0000 add [eax],al
00000011 0000 add [eax],al
00000013 0000 add [eax],al
00000015 0000 add [eax],al
00000017 0000 add [eax],al
00000019 0000 add [eax],al
0000001B 0000 add [eax],al
0000001D 0000 add [eax],al
0000001F 0000 add [eax],al
00000021 0000 add [eax],al
00000023 0000 add [eax],al
00000025 0000 add [eax],al
00000027 0000 add [eax],al
00000029 0000 add [eax],al
0000002B 0000 add [eax],al
0000002D 0000 add [eax],al
0000002F 0000 add [eax],al
00000031 0000 add [eax],al
00000033 0000 add [eax],al
00000035 0000 add [eax],al
00000037 0000 add [eax],al
00000039 0000 add [eax],al
0000003B 0000 add [eax],al
0000003D 0000 add [eax],al
0000003F 0000 add [eax],al
00000041 0000 add [eax],al
00000043 0000 add [eax],al
00000045 0000 add [eax],al
00000047 0000 add [eax],al
00000049 0000 add [eax],al
the add [eax], al asm instruction is added by the linker and I just don't want that, I only want the .text segment (THE FIRST 7 INSTRUCTIONS) are the C program assembly equivalent instrution the rest I don't know where it came from.
Any help?
Thanks.
CodePudding user response:
Those instructions are required for alignment. Notice that the byte values are all 0.