gccelfobjcopy

binary file generated by gcc objcopy has a size much larger than sum of 'text' and 'data' in gcc size output


I use objcopy -S -g -O binary test.elf test.bin to get a .bin file from .elf.

$ ll test.bin 
-rwxr-xr-x 1 hongzhuwang md_psw 1472320 Oct 28 19:07 test.bin
$ size test.elf 
   text    data     bss     dec     hex filename
 173062    6328 6605424 6784814  67872e test.elf

The size of test.bin is apparently larger than the sum of text and data in test.elf. Why is there such a huge difference? Which sections will objcopy copy from .elf into .bin?


Solution

  • I found some NOBITSsections between PROGBITS sections in the linker script. The binary file will occupy a continuous address space, so the NOBITS sections' space are filled by zero by objcopy. I think that's the root cause for the difference.