source: rtems/doc/bsp_howto/linkcmds.t @ 183369d

4.104.114.84.95
Last change on this file since 183369d was 183369d, checked in by Joel Sherrill <joel.sherrill@…>, on 12/17/98 at 18:15:39

Incorporated Jeff's suggestions.

  • Property mode set to 100644
File size: 11.7 KB
Line 
1@c
2@c  COPYRIGHT (c) 1988-1998.
3@c  On-Line Applications Research Corporation (OAR).
4@c  All rights reserved.
5@c
6@c  $Id$
7@c
8
9@chapter Linker Script
10
11@section What is a "linkcmds" file?
12
13The @code{linkcmds} file is a script which is passed to the linker at linking
14time.  This file describes the memory configuration of the board as needed
15to link the program.  Specifically it specifies where the code and data
16for the application will reside in memory.
17
18@section Program Sections
19
20An embedded systems programmer must be much more aware of the
21placement of their executable image in memory than the average
22applications programmer.  A program destined to be embedded as well
23as the target system have some specific properties that must be
24taken into account. Embedded machines often mean average performances
25and small memory usage.  It is the memory usage that concerns us
26when examining the linker command file.
27
28Two types of memories have to be distinguished:
29
30@itemize @bullet
31@item RAM - volatile offering read and write access
32@item ROM - non-volatile but read only
33@end itemize
34
35Even though RAM and ROM can be found in every personal computer,
36one generally doesn't care about them.  In a personal computer,
37a program is nearly always stored on disk and executed in RAM.  Things
38are a bit different for embedded targets: the target will execute the
39program each time it is rebooted or switched on.   The application
40program is stored in non-volatile memory such as ROM, PROM, EEPROM,
41or Flash. On the other hand, data processing occurs in RAM.
42
43This leads us to the structure of an embedded program.  In rough terms,
44an embedded program is made of sections.  It is the responsibility of
45the application programmer to place these sections in the appropriate
46place in target memory.  To make this clearer, if using the COFF
47object file format on the Motorola m68k family of microprocessors,
48the following sections will be present:
49
50@itemize @bullet
51
52@item @b{code (@code{.text}) section}:
53is the program's code and it should not be modified.
54This section may be placed in ROM.
55
56@item @b{non-initialized data (@code{.bss}) section}:
57holds uninitialized variables of the program. It can stay in RAM.
58
59@item @b{initialized data (@code{.data}) section}:
60holds the initialized program data which may be modified during the
61program's life.  This means they have to be in RAM.
62On the other hand, these variables must be set to predefined values, and
63those predefined values have to be stored in ROM.
64
65@end itemize
66
67@b{NOTE:} Many programs and support libraries unknowingly assume that the
68@code{.bss} section and, possibly, the application heap are initialized
69to zero at program start.  This is not required by the ISO/ANSI C Standard
70but is such a common requirement that most BSPs do this.
71
72That brings us up to the notion of the image of an executable: it consists
73of the set of the sections that together constitute the application.
74
75@section Image of an Executable
76
77As a program executable has many sections (note that the user can define
78their own, and that compilers define theirs without any notice), one has to
79specify the placement of each section as well as the type of memory
80(RAM or ROM) the sections will be placed into.
81For instance, a program compiled for a Personal Computer will see all the
82sections to go to RAM, while a program destined to be embedded will see
83some of his sections going into the ROM.
84
85The connection between a section and where that section is loaded into
86memory is made at link time.  One has to let the linker know where
87the different sections are to be placed once they are in memory.
88
89The following example shows a simple layout of program sections.  With
90some object formats, there are many more sections but the basic
91layout is conceptually similar.
92
93@example
94@group
95          +-----------------+
96          |     .text       |  RAM or ROM
97          +-----------------+
98          |     .data       |  RAM
99          +-----------------+
100          |     .bss        |  RAM
101          +-----------------+
102@end group
103@end example
104
105@section Example Linker Command Script
106
107The GNU linker has a command language to specify the image format.  This
108command language can be quite complicated but most of what is required
109can be learned by careful examination of a well-documented example.
110The following is a heavily commented version of the linker script
111used with the the @code{gen68340} BSP  This file can be found at
112$BSP340_ROOT/startup/linkcmds.
113
114@example
115/*
116 *  Specify that the output is to be coff-m68k regardless of what the
117 *  native object format is.
118 */
119
120OUTPUT_FORMAT(coff-m68k)
121
122/*
123 *  Set the amount of RAM on the target board.
124 *
125 *  NOTE: The default may be overridden by passing an argument to ld.
126 */
127
128RamSize = DEFINED(RamSize) ? RamSize : 4M;
129
130/*
131 *  Set the amount of RAM to be used for the application heap.  Objects
132 *  allocated using malloc() come from this area.  Having a tight heap size
133 *  is somewhat difficult and multiple attempts to squeeze it may be needed
134 *  reducing memory usage is important.  If all objects are allocated from
135 *  the heap at system initialization time, this eases the sizing of the
136 *  application heap. 
137 *
138 *  NOTE 1: The default may be overridden by passing an argument to ld.
139 *
140 *  NOTE 2: The TCP/IP stack requires additional memory in the Heap.
141 * 
142 *  NOTE 3: The GNAT/RTEMS run-time requires additional memory in the Heap.
143 */
144
145HeapSize = DEFINED(HeapSize) ? HeapSize : 0x10000;
146
147/*
148 *  Set the size of the starting stack used during BSP initialization
149 *  until first task switch.  After that point, task stacks allocated
150 *  by RTEMS are used.
151 *
152 *  NOTE: The default may be overridden by passing an argument to ld.
153 */
154
155StackSize = DEFINED(StackSize) ? StackSize : 0x1000;
156
157/*
158 *  Starting addresses and length of RAM and ROM.
159 *
160 *  The addresses must be valid addresses on the board.  The Chip Selects
161 *  should be initialized such that the code addresses are valid.
162 */
163
164MEMORY @{
165    ram : ORIGIN = 0x10000000, LENGTH = 4M
166    rom : ORIGIN = 0x01000000, LENGTH = 4M
167@}
168
169/*
170 *  This is for the network driver.  See the Networking documentation
171 *  for more details.
172 */
173
174ETHERNET_ADDRESS = DEFINED(ETHERNET_ADDRESS) ? ETHERNET_ADDRESS : 0xDEAD12;
175
176/*
177 *  The following defines the order in which the sections should go.
178 *  It also defines a number of variables which can be used by the
179 *  application program.
180 *
181 *  NOTE: Each variable appears with 1 or 2 leading underscores to insure
182 *        that the variable is accessible from C code with a single
183 *        underscore.  Some object formats automatically add a leading
184 *        underscore to all C global symbols.
185 */
186
187SECTIONS @{
188
189  /*
190   *  Make the RomBase variable available to the application.
191   */
192
193  _RamSize = RamSize;
194  __RamSize = RamSize;
195
196  /*
197   *  Boot PROM  - Set the RomBase variable to the start of the ROM.
198   */
199
200  rom : @{
201    _RomBase = .;
202    __RomBase = .;
203  @} >rom
204
205  /*
206   * Dynamic RAM - set the RamBase variable to the start of the RAM.
207   */
208
209  ram : @{
210    _RamBase = .;
211    __RamBase = .;
212  @} >ram
213
214  /*
215   *  Text (code) goes into ROM
216   */
217
218  .text : @{
219    /*
220     *  Create a symbol for each object (.o).
221     */
222
223    CREATE_OBJECT_SYMBOLS
224
225    /*
226     *  Put all the object files code sections here.
227     */
228
229    *(.text)
230
231    . = ALIGN (16);      /*  go to a 16-byte boundary */
232
233    /*
234     *  C++ constructors and destructors
235     *
236     *  NOTE:  See the CROSSGCC mailing-list FAQ for
237     *         more details about the "[......]".
238     */
239
240    __CTOR_LIST__ = .;
241   [......]
242    __DTOR_END__ = .;
243
244    /*
245     *  Declares where the .text section ends.
246     */
247
248    etext = .;
249    _etext = .;
250  @} >rom
251
252  /*
253   *  Exception Handler Frame section
254   */
255
256  .eh_fram : @{
257    . = ALIGN (16);
258    *(.eh_fram)
259  @} >ram
260
261  /*
262   *  GCC Exception section
263   */
264
265  .gcc_exc : @{
266    . = ALIGN (16);
267    *(.gcc_exc)
268  @} >ram
269
270  /*
271   *  Special variable to let application get to the dual-ported
272   *  memory.
273   */
274
275  dpram : @{
276    m340 = .;
277    _m340 = .;
278    . += (8 * 1024);
279  @} >ram
280
281  /*
282   *  Initialized Data section goes in RAM
283   */
284
285  .data : @{
286    copy_start = .;
287    *(.data)
288
289    . = ALIGN (16);
290    _edata = .;
291    copy_end = .;
292  @} >ram
293
294  /*
295   *  Uninitialized Data section goes in ROM
296   */
297
298  .bss : @{
299    /*
300     *  M68K specific: Reserve some room for the Vector Table
301     *  (256 vectors of 4 bytes).
302     */
303
304    M68Kvec = .;
305    _M68Kvec = .;
306    . += (256 * 4);
307
308    /*
309     *  Start of memory to zero out at initialization time.
310     */
311
312    clear_start = .;
313
314    /*
315     *  Put all the object files uninitialized data sections
316     *  here.
317     */
318
319    *(.bss)
320
321    *(COMMON)
322
323    . = ALIGN (16);
324    _end = .;
325
326    /*
327     *  Start of the Application Heap
328     */
329
330    _HeapStart = .;
331    __HeapStart = .;
332    . += HeapSize; 
333
334    /*
335     *  The Starting Stack goes after the Application Heap.
336     *  M68K stack grows down so start at high address.
337     */
338
339    . += StackSize;
340    . = ALIGN (16);
341    stack_init = .;
342
343    clear_end = .;
344
345    /*
346     *  The RTEMS Executive Workspace goes here.  RTEMS
347     *  allocates tasks, stacks, semaphores, etc. from this
348     *  memory.
349     */
350
351    _WorkspaceBase = .;
352    __WorkspaceBase = .;
353  @} >ram
354@}
355@end example
356
357@section Initialized Data
358
359Now there's a problem with the initialized data: the @code{.data} section
360has to be in RAM as this data may be modified during the program execution.
361But how will the values be initialized at boot time?
362
363One approach is to place the entire program image in RAM and reload
364the image in its entirety each time the program is run.  This is fine
365for use in a debug environment where a high-speed connection is available
366between the development host computer and the target.  But even in this
367environment, it is cumbersome.
368
369The solution is to place a copy of the initialized data in a separate
370area of memory and copy it into the proper location each time the
371program is started.  It is common practice to place a copy of the initialized
372@code{.data} section at the end of the code (@code{.text}) section
373in ROM when building a PROM image. The GNU tool @code{objcopy}
374can be used for this purpose.
375
376The following figure illustrates the steps a linked program goes through
377to become a downloadable image.
378
379@example
380@group
381+--------------+     +--------------------+
382| .data    RAM |     | .data          RAM |
383+--------------+     +--------------------+
384| .bss     RAM |     | .bss           RAM |
385+--------------+     +--------------------+         +----------------+
386| .text    ROM |     | .text          ROM |         |     .text      |
387+--------------+     +--------------------+         +----------------+
388                     | copy of .data  ROM |         | copy of .data  |
389                     +--------------------+         +----------------+
390
391      Step 1                Step 2                       Step 3
392@end group
393@end example
394
395In Step 1, the program is linked together using the BSP linker script.
396
397In Step 2, a copy is made of the @code{.data} section and placed
398after the @code{.text} section so it can be placed in PROM.  This step
399is done after the linking time.  There is an example
400of doing this in the file $RTEMS_ROOT/make/custom/gen68340.cfg:
401
402@example
403# make a PROM image using objcopy
404m68k-rtems-objcopy \
405--adjust-section-vma .data= \
406
407`m68k-rtems-objdump --section-headers \
408$(basename $@@).exe \
409| awk '[...]` \
410$(basename $@@).exe
411@end example
412
413NOTE: The address of the "copy of @code{.data} section" is
414created by extracting the last address in the @code{.text}
415section with an @code{awk} script.  The details of how
416this is done are not relevant.
417
418Step 3 shows the final executable image as it logically appears in
419the target's non-volatile program memory.  The board initialization
420code will copy the ""copy of @code{.data} section" (which are stored in
421ROM) to their reserved location in RAM.
422
Note: See TracBrowser for help on using the repository browser.