| 1 | <h2>Full example</h2> |
| 2 | <pre><code>/* Source: https://github.com/stivale/stivale2-barebones/blob/master/kernel/linker.ld */ |
| 3 | |
| 4 | /* We want the symbol _start to be our entry point */ |
| 5 | ENTRY(_start) |
| 6 | |
| 7 | /* Define the program headers we want so the bootloader gives us the right */ |
| 8 | /* MMU permissions */ |
| 9 | PHDRS |
| 10 | { |
| 11 | null PT_NULL FLAGS(0) ; /* Null segment */ |
| 12 | text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */ |
| 13 | rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */ |
| 14 | data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */ |
| 15 | } |
| 16 | |
| 17 | SECTIONS |
| 18 | { |
| 19 | /* We wanna be placed in the topmost 2GiB of the address space, for optimisations */ |
| 20 | /* and because that is what the stivale2 spec mandates. */ |
| 21 | /* Any address in this region will do, but often 0xffffffff80000000 is chosen as */ |
| 22 | /* that is the beginning of the region. */ |
| 23 | . = 0xffffffff80000000; |
| 24 | |
| 25 | .text : { |
| 26 | *(.text .text.*) |
| 27 | } :text |
| 28 | |
| 29 | /* Move to the next memory page for .rodata */ |
| 30 | . += CONSTANT(MAXPAGESIZE); |
| 31 | |
| 32 | /* We place the .stivale2hdr section containing the header in its own section, */ |
| 33 | /* and we use the KEEP directive on it to make sure it doesn't get discarded. */ |
| 34 | .stivale2hdr : { |
| 35 | KEEP(*(.stivale2hdr)) |
| 36 | } :rodata |
| 37 | |
| 38 | .rodata : { |
| 39 | *(.rodata .rodata.*) |
| 40 | } :rodata |
| 41 | |
| 42 | /* Move to the next memory page for .data */ |
| 43 | . += CONSTANT(MAXPAGESIZE); |
| 44 | |
| 45 | .data : { |
| 46 | *(.data .data.*) |
| 47 | } :data |
| 48 | |
| 49 | .bss : { |
| 50 | *(COMMON) |
| 51 | *(.bss .bss.*) |
| 52 | } :data |
| 53 | }</code></pre> |