Languages/C/Sections

(Difference between revisions)
Jump to: navigation, search
(Page creation)
 
(Program sections, or what are .text, .data, .bss?)
Line 7: Line 7:
 
Every section corresponds to a particular use, for instance storing variables or executable code.
 
Every section corresponds to a particular use, for instance storing variables or executable code.
  
This document is mainly focused on the [http://en.wikipedia.org/wiki/ELF_file_format ELF (Executable and Linkable Format)] file format used
+
This document is mainly focused on the [http://en.wikipedia.org/wiki/ELF_file_format ELF (Executable and Linkable Format)]
by Linux and the <code>gcc<code> toolchain, but most of this document is valid for most executable file formats.
+
file format used by Linux and the <code>gcc</code> toolchain, but most of this document is valid for most executable file formats.
 +
 
 +
Here are some reason why there are separate sections:
 +
* Code and data can be on separate buses ([http://en.wikipedia.org/wiki/Harvard_architecture Harvard architecture]).
 +
* Code could be read-only and your system can have read-only memory (flash on microcontroller).
 +
* Code can be shared between 2 processes but data can't.
 +
 
 +
Sections can also have permissions like read, write, execute.
  
 
== Universal sections ==
 
== Universal sections ==
Line 15: Line 22:
 
| '''Section''' || '''Description'''
 
| '''Section''' || '''Description'''
 
|-
 
|-
| .text || Executable (machine) code
+
| <code>.text</code> || Executable (machine) code
 
|-  
 
|-  
| .data || Initialized data
+
| <code>.data</code> || Initialized data
 
|-
 
|-
| .bss || Data initialized to zero
+
| <code>.bss</code> || Data initialized to zero
 
|-
 
|-
 +
| <code>.rodata</code> || Read-only
 +
|-
 
|}
 
|}

Revision as of 11:31, 20 November 2013

Contents

Program sections, or what are .text, .data, .bss?

When a program is compiled, the compiler splits the resulting code into different sections (sometimes called segments). Every section corresponds to a particular use, for instance storing variables or executable code.

This document is mainly focused on the ELF (Executable and Linkable Format) file format used by Linux and the gcc toolchain, but most of this document is valid for most executable file formats.

Here are some reason why there are separate sections:

  • Code and data can be on separate buses (Harvard architecture).
  • Code could be read-only and your system can have read-only memory (flash on microcontroller).
  • Code can be shared between 2 processes but data can't.

Sections can also have permissions like read, write, execute.

Universal sections

Section Description
.text Executable (machine) code
.data Initialized data
.bss Data initialized to zero
.rodata Read-only
Personal tools
Namespaces
Variants
Actions
Navigation
Browse
Toolbox