GNU Linker Scripts
Linker scripts use the .ld
file extension.
Entry Point
The entry point of a program can be specified in a few different ways1:
- Using the
ENTRY
command. - The value to the
start
symbol. - The address of the first byte of the
.text
section. - The address
0
.
For example, if we were to use the ENTRY()
command:
Memory Layout
You can use the MEMORY
command to describe the size and location of memory “blocks” in the target system. Based on this information, the linker will place requested sections into the correct memory blocks and also raise errors if any block overflows.
A .ld
file can contain only one MEMORY
command. Inside the command, you can define as many blocks of memory as you want. The syntax is1:
name1
is the name of the memory block, used internally by the linker to refer to that region.attr
is a list of attributes for the memory block. The supported attributes are:r
for read-only.w
for read/write sections.x
for executable sections.a
for allocatable sections.i
for initialized sections.l
same asi
.
origin1
is the starting address of the memory block.length1
is the size (in bytes) of the memory block.
Sections
The following example creates an output file with three sections: .text
, .data
, and .bss
. It takes all input files and takes the corresponding sections from them (.text
goes to .text
) and places them in the output file:
Footnotes
-
GNU. Manuals > ld v2.9.1. > Command Language. Retrieved 2024-09-17, from https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_chapter/ld_3.html. ↩ ↩2