10.5. Linker Command Files

Linker command files allow you to put linker options and directives in a file; this is useful when you invoke the linker often with the same options and directives. Linker command files are also useful because they allow you to use the MEMORY and SECTIONS directives to customize your application. You must use these directives in a command file; you cannot use them on the command line.

Linker command files are ASCII files that contain one or more of the following:

  • Input filenames, which specify object files, archive libraries, or other command files. (If a command file calls another command file as input, this statement must be the last statement in the calling command file. The linker does not return from called command files.)

  • Linker options, which can be used in the command file in the same manner that they are used on the command line.

  • The MEMORY and SECTIONS linker directives. The MEMORY directive defines the target memory configuration (see The MEMORY Directive). The SECTIONS directive controls how sections are built and allocated (see The SECTIONS Directive).

  • Assignment statements, which define and assign values to global symbols.

To invoke the linker with a command file, enter the tiarmclang command and follow it with the name of the command file:

tiarmclang command_filename

The linker processes input files in the order that it encounters them. If the linker recognizes a file as an object file, it links the file. Otherwise, it assumes that a file is a command file and begins reading and processing commands from it. Command filenames are case sensitive, regardless of the system used.

The following sample linker command file, link.cmd, specifies two object files to link and two command line options to use:

a.c.o                    /*  First input filename          */
b.c.o                    /*  Second input filename         */
--output_file=prog.out   /*  Option to specify output file */
--map_file=prog.map      /*  Option to specify map file    */

This sample linker command file contains only filenames and options. (You can place comments in a command file by delimiting them with /* and */.) To invoke the linker with this command file, enter:

tiarmclang link.cmd

You can place other parameters on the command line when you use a command file:

tiarmclang -Wl,--relocatable link.cmd x.c.o y.c.o

The linker processes the command file as soon as it encounters the filename, so a.c.o and b.c.o are linked into the output module before x.c.o and y.c.o.

You can specify multiple command files. If, for example, you have a file called names.lst that contains filenames and another file called dir.cmd that contains linker directives, you could enter:

tiarmclang names.lst dir.cmd

One command file can call another command file; this type of nesting is limited to 16 levels. If a command file calls another command file as input, this statement must be the last statement in the calling command file.

Blanks and blank lines are insignificant in a command file except as delimiters. This also applies to the format of linker directives in a command file. The following example linker command file that contains linker directives.

a.o b.o c.o                 /* Input filenames       */
--output_file=prog.out      /* Options               */
--map_file=prog.map

MEMORY                      /* MEMORY directive      */
{
    FAST_MEM:  origin = 0x0100    length = 0x0100
    SLOW_MEM:  origin = 0x7000    length = 0x1000
}

SECTIONS                    /* SECTIONS directive    */
{
    .text:  > SLOW_MEM
    .data:  > SLOW_MEM
    .bss:   > FAST_MEM
}

For more information, see The MEMORY Directive for the MEMORY directive, and The SECTIONS Directive for the SECTIONS directive.

Contents: