Function sections

From eLinux.org
Jump to: navigation, search

Introduction

"Function sections" is a technique for reducing the size of the kernel image. It does this by placing each function into its own linker section, which then allows the linker to do better dead code removal.

Denys reported that usage of this technique got him about a 10% reduction in kernel size.

Theory of operation

Denys Vlasenko wrote:

-ffunction-sections instructs gcc to place each function
(including static ones) in its own section named .text.function_name
instead of placing all functions in one big .text section.
At link time, ld normally coalesces all such sections into one
output section .text again. It is achieved by having *(.text.*) spec
along with *(.text) spec in built-in linker scripts.
However, if ld is invoked with the option --gc-sections, it tracks references, starting
from the entry point and marks all input sections which are reachable
from there. Then it discards all input sections which are not marked.
This doesn't buy much if you have one big .text section per .o module,
because even one referenced function will pull in entire section.
However, if you use -ffunction-sections to split .text into per-function
sections it makes --gc-sections much more useful.
-fdata-sections is analogous: it places each global or static variable
into .data.variable_name, .rodata.variable_name or .bss.variable_name.

Status

Denys submitted patches in July 2008 to make the kernel compilable using "gcc -ffunction-sections -fdata-sections". See http://lkml.org/lkml/2008/7/1/499