Difference between revisions of "Function sections"

From eLinux.org
Jump to: navigation, search
(Create page describing function sections feature)
 
(Add category)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
 
"Function sections" is a technique for reducing the size of the kernel image.
 
"Function sections" is a technique for reducing the size of the kernel image.
It does this by placing each function into it's own linker section, which then
+
It does this by placing each function into its own linker section, which then
 
allows the linker to do better dead code removal.
 
allows the linker to do better dead code removal.
  
Line 10: Line 10:
 
Denys Vlasenko wrote:
 
Denys Vlasenko wrote:
  
-ffunction-sections instructs gcc to place each function
+
-ffunction-sections instructs gcc to place each function
(including static ones) in it's own section named .text.function_name
+
(including static ones) in its own section named .text.function_name
instead of placing all functions in one big .text section.
+
instead of placing all functions in one big .text section.
  
At link time, ld normally coalesces all such sections into one
+
At link time, ld normally coalesces all such sections into one
output section .text again. It is achieved by having *(.text.*) spec
+
output section .text again. It is achieved by having *(.text.*) spec
along with *(.text) spec in built-in linker scripts.
+
along with *(.text) spec in built-in linker scripts.
  
However, if ld is invoked with the option --gc-sections, it tracks references, starting
+
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 the entry point and marks all input sections which are reachable
from there. Then it discards all input sections which are not marked.
+
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,
+
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.
+
because even one referenced function will pull in entire section.
However, if you use -ffunction-sections to split .text into per-function
+
However, if you use -ffunction-sections to split .text into per-function
sections it makes --gc-sections much more useful.
+
sections it makes --gc-sections much more useful.
  
-fdata-sections is analogous: it places each global or static variable
+
-fdata-sections is analogous: it places each global or static variable
into .data.variable_name, .rodata.variable_name or .bss.variable_name.
+
into .data.variable_name, .rodata.variable_name or .bss.variable_name.
  
 
== Status ==
 
== Status ==
 
Denys submitted patches in July 2008 to make the kernel compilable using
 
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
 
"gcc -ffunction-sections -fdata-sections".  See http://lkml.org/lkml/2008/7/1/499
 +
 +
[[Category:Tips and Tricks]]
 +
[[Category:Kernel]]

Latest revision as of 10:33, 28 October 2011

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