5

How should I produce raw binary file from two object (.o) files?

I want the plain binary format produced by nasm -f bin when compiling a .asm file, but for .o files.

By a plain binary, I mean a file which contains only the instructions, not some extra information, as many executable files contain a lot of extra helpful information.

See http://www.nasm.us/doc/nasmdoc7.html for information on that.

PS: I want to make a "plain binary" to start in QEMU.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
  • are you describing the linking process or something else? – fstab Aug 22 '14 at 08:03
  • 1
    All files are raw binary files. What do you actually want to do with the .o? – Tom Tanner Aug 22 '14 at 08:05
  • What I want is that I have two object files one from gcc and one from nasm, I can link them to produce a executable but how to produce a raw binary ? – Pratik Singhal Aug 22 '14 at 08:06
  • By raw binary I mean a plain binary without the details which are included in the executable files. – Pratik Singhal Aug 22 '14 at 08:06
  • 3
    (I wonder who is upvoting this) "a plain binary without the details which are included in the executable files" still doesn't make sense. – wRAR Aug 22 '14 at 08:10
  • @wRAR Check this :- http://stackoverflow.com/questions/1936844/what-is-the-difference-between-plain-binary-format-bin-and-windows-executable – Pratik Singhal Aug 22 '14 at 08:12
  • @ps06756 and that question has answers that say mostly "there is no such thing". – wRAR Aug 22 '14 at 08:13
  • I want the plain binary format produced by `nasm -f bin` when compiling a .asm file. – Pratik Singhal Aug 22 '14 at 08:14
  • `I want the plain binary format produced by nasm -f bin when compiling a .asm file` Include that in your question – 0xF1 Aug 22 '14 at 08:43
  • And please explain why `nasm -f bin` is not [the correct procedure](http://www.nasm.us/doc/nasmdoc7.html). – Jongware Aug 22 '14 at 08:49
  • @Jongware `nasm -f bin` produces plain binary from `.asm` source file, I want to produce the same thing from two `.o` object files. – Pratik Singhal Aug 22 '14 at 09:55
  • It is a matter of invoking and scripting correctly the GNU `ld` binutils linker. BTW, plain binary executables cannot exist: you need at the very least a convention about the starting address and state (e.g. initial register contents). So please **edit your question** to explain why do you ask, and how would the produced "plain binary" be run (and be started) – Basile Starynkevitch Aug 22 '14 at 10:28
  • 2
    I guess he wants a machine readable code, to run bare metal (without operating system) – Vitor Aug 22 '14 at 13:25
  • 1
    @Vitor That's what I want! – Pratik Singhal Aug 22 '14 at 13:30

2 Answers2

5

This brings back memories. I'm sure there is a better way to do this with linker scripts, but this is how I did it when I was young and stupid:

# compile some files
gcc -c -nostdlib -nostartfiles -nodefaultlibs -fno-builtin kernel.c -o kernel.o
gcc -c -nostdlib -nostartfiles -nodefaultlibs -fno-builtin io.c -o io.o

# link files and place code at known address so we can jump there from asm
ld -Ttext 0x100000 kernel.o io.o -o kernel.out

# get a flat binary
objcopy -S -O binary kernel.out kernel.bin

The file kernel.c started with

__asm__("call _kmain");
__asm__("ret");

void kmain(void) { ... }

The fun part is writing the loader in assembler.

Christoph
  • 164,997
  • 36
  • 182
  • 240
1

ld --oformat binary is a more direct option:

ld --oformat binary -o main.img -Ttext 0x7C00 main.o

The downside of this method is that I don't think it is possible to reuse the symbols to debug, as we'd want something like:

qemu-system-i386 -hda main.img -S -s &
gdb main.elf -ex 'target remote localhost:1234'

So in that case you should stick to objcopy. See also: https://stackoverflow.com/a/32960272/895245

Also make sure that you use your own clean linker script: https://stackoverflow.com/a/32594933/895245

Repository with working examples for some common cases:

Similar question: How to generate plain binaries like nasm -f bin with the GNU GAS assembler?

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985