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 */
21ENTRY(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
30SECTIONS
31{
32	.dtb (NOLOAD) : {
33		dtb_begin = .;
34		. += LENGTH(dtb_region);
35		dtb_end = .;
36	} >dtb_region
37
38	/*
39	 * Collect together the code. This is page aligned so it can be mapped
40	 * as executable-only.
41	 */
42	.text : ALIGN(4096) {
43		text_begin = .;
44		*(.init.entry)
45		*(.init.*)
46		*(.text.*)
47	} >image
48	text_end = .;
49
50	/*
51	 * Collect together read-only data. This is page aligned so it can be
52	 * mapped as read-only and non-executable.
53	 */
54	.rodata : ALIGN(4096) {
55		rodata_begin = .;
56		*(.rodata.*)
57	} >image
58	.got : {
59		*(.got)
60	} >image
61	rodata_end = .;
62
63	.eh_stack (NOLOAD) : ALIGN(4096) {
64		/*
65		 * Get stack overflow guard from the previous page being from
66		 * .rodata and mapped read-only or left unmapped.
67		 */
68		eh_stack_limit = .;
69		. += 4096;
70		. = ALIGN(4096);
71		init_eh_stack_pointer = .;
72	} >writable_data
73
74	/*
75	 * Collect together the read-write data including .bss at the end which
76	 * will be zero'd by the entry code. This is page aligned so it can be
77	 * mapped as non-executable.
78	 */
79	.data : ALIGN(4096) {
80		data_begin = .;
81		*(.data.*)
82		/*
83		 * The entry point code assumes that .data is a multiple of 32
84		 * bytes long.
85		 */
86		. = ALIGN(32);
87		data_end = .;
88	} >writable_data AT>image
89	data_lma = LOADADDR(.data);
90
91	/* Everything beyond this point will not be included in the binary. */
92	bin_end = data_lma + SIZEOF(.data);
93
94	/* The entry point code assumes that .bss is 16-byte aligned. */
95	.bss : ALIGN(16)  {
96		bss_begin = .;
97		*(.bss.*)
98		*(COMMON)
99		. = ALIGN(16);
100		bss_end = .;
101	} >writable_data
102
103	init_stack_pointer = ORIGIN(writable_data) + LENGTH(writable_data);
104	.stack (NOLOAD) : ALIGN(4096) {
105		. += 4096; /* Ensure we have one guard page for overflow. */
106		stack_limit = .;
107		. = init_stack_pointer;
108	} >writable_data
109
110	/* Make our Bionic stack protector compatible with mainline LLVM */
111	__stack_chk_guard = __bionic_tls + 40;
112
113	/*
114	 * Remove unused sections from the image.
115	 */
116	/DISCARD/ : {
117		/* The image loads itself so doesn't need these sections. */
118		*(.gnu.hash)
119		*(.hash)
120		*(.interp)
121		*(.eh_frame_hdr)
122		*(.eh_frame)
123		*(.note.gnu.build-id)
124	}
125}
126