Search code examples
cgccoperating-systemldosdev

Compiling and linking C code without any additional data in output file


I am asking for help by professionals because of lack of my knowledge in using GCC and ld.I'm writing OS for educational purposes, and i have a problem with compiling and linking C code. To be honest, the is no any problem, but I'm confused by the unncessary data in output files generated by the GCC and LD like

GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 symtab..strtab..shstrtab..text..eh_frame..data..comment
.ELF..|

and etc. I really need to know which parameters use both with gcc and ld to reduce this unuseful (for my OS) data

Parameters I used before: -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -fstrength-reduce -finline-functions I also use linker script to organise segments.

I tried objcopy to reduce such blocks as .comment and .note from output, for me it was the best solution

objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin

Solution

  • Problem can be solved with the linker script. Using /DISCARD/ block. Ld manual says that this block excludes 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 to make output a plane binary file.

    OUTPUT_FORMAT("binary")
    

    Therefore, I do not need to use objcopy anymore.