Home > Bachelor Thesis, Linux Kernel > Linker Scripts – Inner Concepts

Linker Scripts – Inner Concepts

Every link is controlled by a linker script. This script is written in the linker command language. The main purpose of the linker script is to describe how the sections in the input files should be mapped into the output file and to control the memory layout of the output file. Linker always uses linker script. If we do not specify then it takes in the default script that is compiled into the linker executable.

Basic Linker Script Concepts :

The linker combines input files into a single output file. Both output file and input file are object files. The output file is often executable. These object file contains different sections, which have its own name and size. A section may be marked as loadable, allocatable or none. The loadable section means that the contents should be loaded into the memory when the output file is run. The allocatable sections with no contents, means a certain area in memory should be kept aside, but nothing in particular should be loaded there A section which is neither loadable nor allocatable contains some sort of debugging information.

Every loadable or allocatable section will have two addresses:

1. VMA – Virtual Memory Address. This is the address the section will have when the output file is run.

2. LMA – load Memory Address. This is the address at which the section will be loaded.

In many cases the two address will be the same. An interesting example where these two address are different is when a data section is loaded into ROM and then later copied into RAM when the program starts. Here the ROM address would be LMA and RAM address would be VMA.

Every object file also has a list of symbols called symbol table. A symbol may be defined or undefined. Each symbol has a name and each defined symbol will have an address. After compiling a C or C++ file, we get a defined symbol for every defined function and global or static variable. Every undefined function or global variable which is referenced in the input file will become an undefined symbol.

Simple Linker Script Example :

The simplest possible linker script has just one command: ‘SECTIONS’. We use the ‘SECTIONS’ command to describe the memory layout of the output file.

Lets assume our program consists only of code, initialized data and uninitialized data. These will be in ‘.text’,’.data’ and ‘.bss’ sections respectively. Assume also that these are the only sections that appear in our input files.

Example : lets say that the code should be loaded at the address 0×10000 and that the data should start at address 0×8000000. The corresponding linker script for this would be

1. SECTIONS

2. {

3.     . = 0×10000;

4.     .text : { *(.text) }

5.     . = 0×8000000;

6.     .data : { *(.data) }

7.     .bss : { *(.bss) }

8. }

‘SECTIONS’ is a keyword followed by a series of symbol assignments and output section descriptions enclosed in curly braces.

Line 3 sets the value of the special symbol ‘.’ which is the location counter. If address is not specified then the address is set from the current value of the location counter. The location counter is then incremented by the size of the output section. At the start of the ‘SECTIONS’ command, the location counter has the value ‘0’.

Line 4 defines an output section ‘.text’. Within the curly braces , we list the names of the input sections which should be placed into this output section. The ‘*’ is a wildcard which matches any file name. The expression ‘*(.text)’ means all ‘.text’ input sections in all input files.

Since the locations counter is 0×10000, when the output section ‘.text’ is defined, the linker will set the address of the ‘.text’ section in the output file to be ‘0×10000’.

The remaining lines define the ‘.data’ and ‘.bss’ sections in the output file. The linker will place ‘.data’ output section at address 0×8000000. After that the location counter will be 0×8000000 plus the size of the ‘.data’ output section. The linker will then place the ‘.bss’ output section immediately after the ‘.data’ output section in memory.

The linker will ensure that each output section has the required alignment, by increasing the location counter if necessary

In the above example, the specified address for the ‘.text’ and ‘.data’ sections will probably satisfy any alignment constraints, but the linker may have to create a small gap between the ‘.data’ and ‘.bss’ sections.

Reference : The GNU Linker

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.