ld: Simple Example

1 
1 3.3 Simple Linker Script Example
1 ================================
1 
1 Many linker scripts are fairly simple.
1 
1    The simplest possible linker script has just one command: 'SECTIONS'.
1 You use the 'SECTIONS' command to describe the memory layout of the
1 output file.
1 
1    The 'SECTIONS' command is a powerful command.  Here we will describe
1 a simple use of it.  Let's assume your program consists only of code,
1 initialized data, and uninitialized data.  These will be in the '.text',
1 '.data', and '.bss' sections, respectively.  Let's assume further that
1 these are the only sections which appear in your input files.
1 
1    For this example, let's say that the code should be loaded at address
1 0x10000, and that the data should start at address 0x8000000.  Here is a
1 linker script which will do that:
1      SECTIONS
1      {
1        . = 0x10000;
1        .text : { *(.text) }
1        . = 0x8000000;
1        .data : { *(.data) }
1        .bss : { *(.bss) }
1      }
1 
1    You write the 'SECTIONS' command as the keyword 'SECTIONS', followed
1 by a series of symbol assignments and output section descriptions
1 enclosed in curly braces.
1 
1    The first line inside the 'SECTIONS' command of the above example
1 sets the value of the special symbol '.', which is the location counter.
1 If you do not specify the address of an output section in some other way
1 (other ways are described later), the address is set from the current
1 value of the location counter.  The location counter is then incremented
1 by the size of the output section.  At the start of the 'SECTIONS'
1 command, the location counter has the value '0'.
1 
1    The second line defines an output section, '.text'.  The colon is
1 required syntax which may be ignored for now.  Within the curly braces
1 after the output section name, you list the names of the input sections
1 which should be placed into this output section.  The '*' is a wildcard
1 which matches any file name.  The expression '*(.text)' means all
1 '.text' input sections in all input files.
1 
1    Since the location counter is '0x10000' when the output section
1 '.text' is defined, the linker will set the address of the '.text'
1 section in the output file to be '0x10000'.
1 
1    The remaining lines define the '.data' and '.bss' sections in the
1 output file.  The linker will place the '.data' output section at
1 address '0x8000000'.  After the linker places the '.data' output
1 section, the value of the location counter will be '0x8000000' plus the
1 size of the '.data' output section.  The effect is that the linker will
1 place the '.bss' output section immediately after the '.data' output
1 section in memory.
1 
1    The linker will ensure that each output section has the required
1 alignment, by increasing the location counter if necessary.  In this
1 example, the specified addresses for the '.text' and '.data' sections
1 will probably satisfy any alignment constraints, but the linker may have
1 to create a small gap between the '.data' and '.bss' sections.
1 
1    That's it!  That's a simple and complete linker script.
1