Specifications

A Minimal PowerPCª Boot Sequence for
13
Executing Compiled C Programs
Sample Boot Sequence
4.4 Code Relocation
The code relocation depends on variables that are allocated in the Þle ld.script. The text, data, and bss
sections of the program may be relocated from ROM to RAM using these variables.
The Þrst relocation that takes place is the text relocation. The relocation code looks at the ld.script variables
_img_text_start and _Þnal_text_start to determine if the text must be relocated. If the two variables are
equal, then no text relocation occurs. This typically speeds up execution in a simulated environment, and
when the user program to be run is fairly simple. If the user program is large or performs large numbers of
iterations, execution may be speeded by moving the text from ROM to RAM if ROM accesses are slow.
The start address of the section to be copied is stored in the symbol _img_text_start deÞned in ld.script. The
length of the copy is determined using the symbol _img_text_end also deÞned in ld.script. The program
starts copying at _img_text_start and copies data to _Þnal_text_start until it reaches the address
_img_text_end.
Next, the data and bss sections may be relocated. For standard systems where the boot program exists in a
read-only ROM, these sections must be moved so they can be modiÞed by the user program. If the code is
not initially located in a ROM, or if the ROM is writeable, then these sections do not need to be relocated.
The ROM image location of the data section is stored in the symbol _img_data_start, deÞned in ld.script. It
will be relocated to the address deÞned in _Þnal_data_start. If _img_data_start and _Þnal_data_start are not
equal, the relocation program starts copying from _img_data_start to _Þnal_data_start. When the copy-to
address is equal to _Þnal_data_end, deÞned in ld.script, the copy is complete. If _img_data_start and
_Þnal_data_start are equal, the program skips the data copy.
The bss section is not actually copied since it only holds uninitialized data. Instead, the region starting at
_bss_start and ending at _bss_end, both deÞned in ld.script, is initialized to all zeroes. This code may be
commented out for programs which do not depend on zero-Þlled bss.
The user may control the ROM image and relocation addresses of the different sections by modifying the
Þle ld.script, as speciÞed in Section 4.5, ÒGCC Compilation and Linking.Ó
4.5 GCC Compilation and Linking
The compilation and linking procedure for a standalone bootable program is fairly complex. The compiled
program should not include standard libraries or startup code, and needs to be in a format that can be copied
into a simulated or real ROM device or memory component. Most importantly, the code needs to be located
at a speciÞc absolute start point so that it begins execution on system reset. In addition, the executable needs
to be built so that references to symbols and variables refer to the location of variables after the relocation
to RAM (if any) has occurred. Most of this work is accomplished through the use of a linker script.
Note that this document refers to the target of the build as a ÒROM image.Ó Whether this image is actually
loaded into a ROM component or some other simulated or real memory device is implementation
dependent.
The compilation procedure described in this application note uses the GNU cross-compiler version 2.8.1 (ld
version 2.9.1) which is free and publicly available from many different sources on the internet. The GNU
make utility and the GNU assembler and linker are also used. Other versions of the GNU tools may work
as well.