I'm practicing operating system development and am having trouble getting gcc and ld to produce pure binary without any metadata or section labels. For example:

int function(){ return 100;}

would compile only this code and would not add code or metadata. So when I disassemble the code there is no extra code produced by the compiler and linker.

I am following the instructions out of this book, I'm supposed to compile and link the C code using the following instructions:

$ gcc -ffreestanding -c test.c -o test.o
$ ld -o test.bin -Ttext 0x0 --oformat binary test.o

This is supposed to produce the following disassembled code:

push ebp
mov ebp, esp
mov eax, 0xbaba
pop ebp

However these instructions instead produced this code and a LOT of other random code, probably just the disassembler interpreting meta data as binary code.

  • 2
    For starters, static linking and stripping and no glibc or anything that calls system calls. I'll wait for a proper answer myself.
    – orion
    Mar 11 '14 at 20:25
  • What are you trying to build? Some code you got, or are you trying to cook up your own? Have you looked how programs that run standalone, like memtest pulls it off?
    – vonbrand
    Mar 11 '14 at 20:30
  • Show the instructions, what you get and what you expect. Otherwise it is hard to diagnose your problem.
    – vonbrand
    Mar 12 '14 at 1:25
  • @vonbrand I added my expectations to the above, but I did not add my results because it is quite long, basically the first part of the output is the code I expected, however after that the rest of the code is random add and pop instructions.
    – raystubbs
    Mar 14 '14 at 22:01
  • If it generates that, you should be OK (the ret at the end goes back; whatever comes later won't be exeecuted).
    – vonbrand
    Mar 15 '14 at 0:11

This is what I found:

If you use the -fno-builtin option then gcc will not use the built-in C libraries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.