A c-startup code of msp430 and a linker script to link it.

It is very interesting to trace the actual path of a program… but its very difficult to understand it. Now MSP430 being a less complex system which has less number of instructions to handle the task becomes much more easier….. As i have mentioned earlier, there are mainly four phases for the compilation of a C-program. Of this we can now look into the 4th phase i.e. LINKING phase in detail.
While writing a C-program its a basic rule to write a main() in it as its said that the execution begins at the main(). But how the control comes to main()?? For this we need to know from where the execution starts. Before making an attempt to write my own start up code, it was essential for me to study a C-start up code and its control transfer. For this msp430 was chosen being a simple controller.

The above show a simple program in which an initialised and an uninitialized global variables are declared. Let us take the objdump: msp430-objdump -D ./a.out | less



Its clear that different sections are present. Usually we go directly into the main function. Let us check out what happens before it??
The CPU starts program execution at the word address stored in the reset vector, 0xFFFEh. Look into the vector section and the address stored in the address location 0xFFFE. The address stored is f800 (the processor is liitle endian).
If the first part of the program is verified its clear that 0xf800 has .text section.
_init_stack : which initialize the stack pointer to 0x280.

This is at 0xf800.

_low_level_init : Here the WDT(Watch Dog Timer) is initialized and holded.

This is at 0xf804.

_do_copy_data : Here the value of the initialized global variable is copied to the RAM from 0x200(_data_start Section).

This is at 0xf80a

_do_clear_bss : Here the uninitialized global variables are initialized to zero and stored in the RAM. This is stored in the _bss_start Section.

Its after this step that the control gets transferred to main in

So a start up code is nothing but a C-program which gats executed before main. So we can write a minmal Start up function from this and keep its address in the reset vector for execution. In msp430-gcc tool chain we can find the actual start up functions in the form crt430x—-.o (msp430-gcc-4.4.3/msp430/lib/)

Linker scripts are text files. 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. when the command map430-gcc -mmcu=ms430x2013 filename is given, the linker script corresponding to the msp430x2013 is taken from the library.(msp430-gcc-4.4.3/msp430/lib/ldscripts/) A linker script mainly has 2 parts, a Memory{} and a Section{}.

The linkers default configuration permits allocation of all available memory. The MEMORY command describes the location and size of blocks of memory in the target that may be used by linker. The linker script may contain at most 1 memory command. THe syntax is :

MEMORY

{

name [( attr )] : ORIGIN = origin , LENGTH = len

}

The attr string is optional. The attr string must consist of the following characters and more:

+ R
Read-only section
+W
Read/write section
+X
Executable section

The SECTIONS command tells the linker how to map input sections into output sections, and how to place the output sections in memory. The format of the SECTIONS command is:

SECTIONS

{

sections – command

sections – command

}

This two forms the basic of a linker script.

Leave a comment