-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinker-script.ld
84 lines (73 loc) · 2.54 KB
/
linker-script.ld
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
LINKER RELAXATION, GLOBAL POINTER (gp) and SMALL SECTIONS (.sbss, .sdata, .srodata)
Linker relaxation is an optimization that permit to reduce the code size.
The x3 (gp) register can be used to store a fixed address that can be used to
access variables with relative addressing.
Accessing a generic symbol normally requires 2 instructions: one instructions to load
the upper part of the symbol's address in a register and a relative load or store.
Accessing a variable with relative addressing using gp base register requires
1 instruction only.
Symbols that can be addressed using gp must be in +-2K range near gp.
gp must be early initialized in the startup file and must not be changed during
program execution.
__global_pointer$ is usually placed 2K after the start of a small section so all
the offsets can be used to access symbols.
Note that relaxation must be disabled while setting gp, otherwise "la gp, __global_pointer$"
is relaxed by the linker in "mv gp, gp". See (.option norelax).
Linker relaxation is enabled by default (-mrelax is the default option).
You can disable linker relaxation in gcc using the -mno-relax option.
Small sections (.sbss .sdata .srodata) contains variables that have a size less
than o equal to 8 bytes.
When a variable size is <= 8 bytes the compiler, which from C code generates
assembly code, insted of putting the variable in .bss, .data or .rodata, puts
the variable in .sbss, .sdata, .srodata.
In this way __global_pointer$ can be placed near one of those sections to
optimise the access to the variables that the section contains.
The maximum size of variables that are placed in small sections can be changed
with -msmall-data-limit=n option.
Small sections can be disabled with -msmall-data-limit=0 option.
*/
MEMORY
{
flash : ORIGIN = 0x00000000, LENGTH = 256
ram : ORIGIN = 0x10000000, LENGTH = 256
}
stack_size = 100;
SECTIONS
{
.mytext :
{
*(.startup)
*(.text)
*(.srodata)
*(.rodata)
. = ALIGN(4);
_srcStartData = .;
}> flash
.mydata :
{
. = ALIGN(4);
_dstStartData = .;
/*
you can define "__global_pointer$" symbol to enable linker relaxation
*/
__global_pointer$ = (. + 2K - 8);
*(.sdata)
*(.data)
. = ALIGN(4);
_dstEnddata = .;
}> ram AT> flash
.mybss :
{
. = ALIGN(4);
_startBss = .;
*(.bss)
*(COMMON)
. = ALIGN(4);
_endBss = .;
_estack = .;
. = . + stack_size;
. = ALIGN(4);
_sstack = .;
}> ram
}