Question

As I'm new to binutils, gcc ant others, I have some general questions, anwsers on which I havn't found in manuals.

I'm using C and assembly(nasm syntax) and I need raw binary files on output. First of all, I compile my code to objec file with parameters:

cc -nostartfiles -nostdlib -c -ffreestanding <input file(s)> ;cc or gcc no matter

Then I link all the files using simple script which only puts segments in needed order.

ld -T <script> -o <o.file> <in.file(s)>  ;nothing special here

And to get raw binary I use objcopy

objcopy -O binary <o.file> <in.file> ;can't be simplier

All in all, I need binary file only with .text and .data segments in it and 32-bit code.

1.Can i get this way what I want?

2.Are there other ways to do that? (no matter easier or more complicated)


Thank you for help.


I haven't problems compiling Asm code, almost all problems with C code.

Était-ce utile?

La solution

Once I came across a ld manual page and /DISCARD/ block was said to exclude everything listed in it from final output.

So I've inserted this block after the .text, .data and .bss blocks

/DISCARD/ : 
{
    *(.comment)
    *(.eh_frame)
    *(.note.GNU-stack)
}

As well as this line in the very beginning of my linker script.

OUTPUT_FORMAT("binary")

Therefore, I do not need to use objcopy anymore.

Autres conseils

You need to compile the source files using this command

nasm -o bin <SOURCE FILES>

This will produce pure binary output.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top