Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Code will start running at this symbol which is placed at the start of the |
| 19 | * image. |
| 20 | */ |
| 21 | ENTRY(entry) |
| 22 | |
| 23 | /* |
| 24 | * The following would be useful to check that .init code is not called back |
| 25 | * into once it has completed but it isn't supported by ld.lld. |
| 26 | * |
| 27 | * NOCROSSREFS_TO(.init .text) |
| 28 | */ |
| 29 | |
| 30 | SECTIONS |
| 31 | { |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 32 | /* |
| 33 | * Collect together the code. This is page aligned so it can be mapped |
| 34 | * as executable-only. |
| 35 | */ |
Pierre-Clément Tosi | cbbb849 | 2023-04-21 15:19:32 +0100 | [diff] [blame] | 36 | .text : ALIGN(4096) { |
Pierre-Clément Tosi | 1f7c523 | 2024-08-13 19:51:25 +0100 | [diff] [blame] | 37 | KEEP(*(.init.head)); |
| 38 | *(.init.head) |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 39 | text_begin = .; |
| 40 | *(.init.entry) |
| 41 | *(.init.*) |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 42 | *(.text.*) |
| 43 | } >image |
| 44 | text_end = .; |
| 45 | |
| 46 | /* |
| 47 | * Collect together read-only data. This is page aligned so it can be |
| 48 | * mapped as read-only and non-executable. |
| 49 | */ |
| 50 | .rodata : ALIGN(4096) { |
| 51 | rodata_begin = .; |
| 52 | *(.rodata.*) |
| 53 | } >image |
| 54 | .got : { |
| 55 | *(.got) |
| 56 | } >image |
| 57 | rodata_end = .; |
| 58 | |
| 59 | /* |
| 60 | * Collect together the read-write data including .bss at the end which |
| 61 | * will be zero'd by the entry code. This is page aligned so it can be |
| 62 | * mapped as non-executable. |
| 63 | */ |
| 64 | .data : ALIGN(4096) { |
| 65 | data_begin = .; |
| 66 | *(.data.*) |
| 67 | /* |
| 68 | * The entry point code assumes that .data is a multiple of 32 |
| 69 | * bytes long. |
| 70 | */ |
| 71 | . = ALIGN(32); |
| 72 | data_end = .; |
| 73 | } >writable_data AT>image |
| 74 | data_lma = LOADADDR(.data); |
| 75 | |
| 76 | /* Everything beyond this point will not be included in the binary. */ |
Pierre-Clément Tosi | 3c6d4d4 | 2022-10-14 17:22:47 +0100 | [diff] [blame] | 77 | bin_end = data_lma + SIZEOF(.data); |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 78 | |
Pierre-Clément Tosi | b996399 | 2024-11-28 22:53:02 +0000 | [diff] [blame] | 79 | /* Data may be appended at load time to our binary. */ |
| 80 | .image_footer (NOLOAD) : ALIGN(4096) { |
| 81 | image_footer_begin = .; |
| 82 | . = ALIGN(LENGTH(image)); |
| 83 | image_footer_end = .; |
| 84 | } >image |
| 85 | |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 86 | /* The entry point code assumes that .bss is 16-byte aligned. */ |
| 87 | .bss : ALIGN(16) { |
| 88 | bss_begin = .; |
| 89 | *(.bss.*) |
| 90 | *(COMMON) |
| 91 | . = ALIGN(16); |
| 92 | bss_end = .; |
| 93 | } >writable_data |
| 94 | |
Pierre-Clément Tosi | bb65701 | 2024-11-28 22:06:32 +0000 | [diff] [blame] | 95 | /* Left unmapped, to catch overflows of the exception handler stack. */ |
| 96 | .eh_stack_guard_page (NOLOAD) : ALIGN(4096) { |
| 97 | . += 4096; |
| 98 | } >writable_data |
| 99 | |
| 100 | /* Exception handler stack, mapped read-write. */ |
| 101 | .eh_stack (NOLOAD) : ALIGN(4096) { |
| 102 | eh_stack_limit = .; |
| 103 | . += 4096; |
| 104 | init_eh_stack_pointer = .; |
| 105 | } >writable_data |
| 106 | |
Pierre-Clément Tosi | 0d5434d | 2024-11-28 21:50:56 +0000 | [diff] [blame] | 107 | /* Left unmapped, to catch overflows of the stack. */ |
| 108 | .stack_guard_page (NOLOAD) : ALIGN(4096) { |
| 109 | . += 4096; |
| 110 | } >writable_data |
| 111 | |
| 112 | /* Stack, mapped read-write (possibly partially). */ |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 113 | .stack (NOLOAD) : ALIGN(4096) { |
Pierre-Clément Tosi | 23aba52 | 2023-04-21 17:03:50 +0100 | [diff] [blame] | 114 | stack_limit = .; |
Pierre-Clément Tosi | 0d5434d | 2024-11-28 21:50:56 +0000 | [diff] [blame] | 115 | . = ALIGN(LENGTH(writable_data)); |
| 116 | init_stack_pointer = .; |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 117 | } >writable_data |
| 118 | |
Pierre-Clément Tosi | 67108c3 | 2023-06-30 11:04:02 +0000 | [diff] [blame] | 119 | /* Make our Bionic stack protector compatible with mainline LLVM */ |
| 120 | __stack_chk_guard = __bionic_tls + 40; |
| 121 | |
Andrew Walbran | a5b7af5 | 2022-07-06 15:06:20 +0000 | [diff] [blame] | 122 | /* |
| 123 | * Remove unused sections from the image. |
| 124 | */ |
| 125 | /DISCARD/ : { |
| 126 | /* The image loads itself so doesn't need these sections. */ |
| 127 | *(.gnu.hash) |
| 128 | *(.hash) |
| 129 | *(.interp) |
| 130 | *(.eh_frame_hdr) |
| 131 | *(.eh_frame) |
| 132 | *(.note.gnu.build-id) |
| 133 | } |
| 134 | } |