Input Section Wildcards - Untitled
Description: Untitled

Next: Input Section Common, Previous: Input Section Basics, Up: Input Section

##### 3.6.4.2 Input Section Wildcard Patterns

In an input section description, either the file name or the section name or both may be wildcard patterns.

The file name of ‘`*`’ seen in many examples is a simple wildcard pattern for the file name.

The wildcard patterns are like those used by the Unix shell.

‘`*`’
  matches any number of characters
‘`?`’
  matches any single character
‘`[chars]`’
  matches a single instance of any of the chars; the ‘`-`’ character may be used to specify a range of characters, as in ‘`[a-z]`’ to match any lower case letter
‘`\`’
  quotes the following character

When a file name is matched with a wildcard, the wildcard characters will not match a ‘`/`’ character (used to separate directory names on Unix). A pattern consisting of a single ‘`*`’ character is an exception; it will always match any file name, whether it contains a ‘`/`’ or not. In a section name, the wildcard characters will match a ‘`/`’ character.

File name wildcard patterns only match files which are explicitly specified on the command line or in an `INPUT` command. The linker does not search directories to expand wildcards.

If a file name matches more than one wildcard pattern, or if a file name appears explicitly and is also matched by a wildcard pattern, the linker will use the first match in the linker script. For example, this sequence of input section descriptions is probably in error, because the `data.o` rule will not be used:

         .data : { *(.data) }
         .data1 : { data.o(.data) }

Normally, the linker will place files and sections matched by wildcards in the order in which they are seen during the link. You can change this by using the `SORT_BY_NAME` keyword, which appears before a wildcard pattern in parentheses (e.g., `SORT_BY_NAME(.text*)`). When the `SORT_BY_NAME` keyword is used, the linker will sort the files or sections into ascending order by name before placing them in the output file.

`SORT_BY_ALIGNMENT` is very similar to `SORT_BY_NAME`. The difference is `SORT_BY_ALIGNMENT` will sort sections into ascending order by alignment before placing them in the output file.

`SORT_BY_INIT_PRIORITY` is very similar to `SORT_BY_NAME`. The difference is `SORT_BY_INIT_PRIORITY` will sort sections into ascending order by numerical value of the GCC init_priority attribute encoded in the section name before placing them in the output file.

`SORT` is an alias for `SORT_BY_NAME`.

When there are nested section sorting commands in linker script, there can be at most 1 level of nesting for section sorting commands.

1. `SORT_BY_NAME` (`SORT_BY_ALIGNMENT` (wildcard section pattern)). It will sort the input sections by name first, then by alignment if 2 sections have the same name.
2. `SORT_BY_ALIGNMENT` (`SORT_BY_NAME` (wildcard section pattern)). It will sort the input sections by alignment first, then by name if 2 sections have the same alignment.
3. `SORT_BY_NAME` (`SORT_BY_NAME` (wildcard section pattern)) is treated the same as `SORT_BY_NAME` (wildcard section pattern).
4. `SORT_BY_ALIGNMENT` (`SORT_BY_ALIGNMENT` (wildcard section pattern)) is treated the same as `SORT_BY_ALIGNMENT` (wildcard section pattern).
5. All other nested section sorting commands are invalid.

When both command line section sorting option and linker script section sorting command are used, section sorting command always takes precedence over the command line option.

If the section sorting command in linker script isn't nested, the command line option will make the section sorting command to be treated as nested sorting command.

1. `SORT_BY_NAME` (wildcard section pattern ) with `--sort-sections alignment` is equivalent to `SORT_BY_NAME` (`SORT_BY_ALIGNMENT` (wildcard section pattern)).
2. `SORT_BY_ALIGNMENT` (wildcard section pattern) with `--sort-section name` is equivalent to `SORT_BY_ALIGNMENT` (`SORT_BY_NAME` (wildcard section pattern)).

If the section sorting command in linker script is nested, the command line option will be ignored.

`SORT_NONE` disables section sorting by ignoring the command line section sorting option.

If you ever get confused about where input sections are going, use the ‘`-M`’ linker option to generate a map file. The map file shows precisely how input sections are mapped to output sections.

This example shows how wildcard patterns might be used to partition files. This linker script directs the linker to place all ‘`.text`’ sections in ‘`.text`’ and all ‘`.bss`’ sections in ‘`.bss`’. The linker will place the ‘`.data`’ section from all files beginning with an upper case character in ‘`.DATA`’; for all other files, the linker will place the ‘`.data`’ section in ‘`.data`’.

         SECTIONS {
           .text : { *(.text) }
           .DATA : { [A-Z]*(.data) }
           .data : { *(.data) }
           .bss : { *(.bss) }
         }