Objcopy, how does he make binary output?

Since I am new to binutils, gcc ant others, I have some general questions that I have not found in the manuals.

I use C and assembly (nasm syntax) and I need the source binaries in the output. First of all, I compile my code into an objec file with parameters:

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

Then I link all the files with a simple script that only puts the segments in the right order.

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

And to get the raw binary I use objcopy

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

In general, I only need a binary file 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 this? (it doesn't matter if it's easier or harder)


Thank you for your help.


I have no problems compiling ASM code, almost all problems with C code.

+4
source share
2 answers

As soon as I came across the ld manual page and the /DISCARD/ block, as was said, excluded everything listed in it from the final output.

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

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

Like this line at the very beginning of my linker script.

 OUTPUT_FORMAT("binary") 

Therefore, I no longer need to use objcopy .

0
source

You need to compile the source files with this command

 nasm -o bin <SOURCE FILES> 

This will produce pure binary output.

-1
source

All Articles