3

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
1

2 Answers 2

4

Split you compilation and linking stages. Use the "-s" option for GCC to reduce .o files. (the link in pmg's comment is of particular interest, though not related to linker scripts)

The linker's script for a simple kernel is described here (tutorial for building the kernel using LD).

Use the

ld -T <yourscript> <objectfilelist> -o kernel.bin

command to get the desired binary.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.