xref: /openbsd-src/sys/arch/amd64/conf/ld.script (revision 4c5e57a2bb21e6f8963cca482c2e6a369211a6f7)
1*4c5e57a2Sguenther/*	$OpenBSD: ld.script,v 1.19 2022/11/07 01:41:57 guenther Exp $	*/
20c88df2fSderaadt
30c88df2fSderaadt/*
40c88df2fSderaadt * Copyright (c) 2009 Tobias Weingartner <weingart@tepid.org>
50c88df2fSderaadt *
60c88df2fSderaadt * Permission to use, copy, modify, and distribute this software for any
70c88df2fSderaadt * purpose with or without fee is hereby granted, provided that the above
80c88df2fSderaadt * copyright notice and this permission notice appear in all copies.
90c88df2fSderaadt *
100c88df2fSderaadt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
110c88df2fSderaadt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
120c88df2fSderaadt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
130c88df2fSderaadt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
140c88df2fSderaadt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
150c88df2fSderaadt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
160c88df2fSderaadt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
170c88df2fSderaadt */
180c88df2fSderaadt
190c88df2fSderaadtOUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64")
200c88df2fSderaadtOUTPUT_ARCH(i386:x86-64)
210c88df2fSderaadt
220c88df2fSderaadt/* Define how we want out ELF binary to look like. */
230c88df2fSderaadtPHDRS
240c88df2fSderaadt{
250c88df2fSderaadt	text PT_LOAD FILEHDR PHDRS;
269718ab18Sguenther	rodata PT_LOAD FLAGS (4);
270c88df2fSderaadt	data PT_LOAD;
280c88df2fSderaadt	bss PT_LOAD;
29176f97ecSderaadt	openbsd_randomize PT_OPENBSD_RANDOMIZE;
300c88df2fSderaadt}
310c88df2fSderaadt
320c88df2fSderaadt/*
330c88df2fSderaadt * If we want the text/rodata/data sections aligned on 2M boundaries,
340c88df2fSderaadt * we could use the following instead.  Note, file size would increase
350c88df2fSderaadt * due to necessary padding.
360c88df2fSderaadt *
370c88df2fSderaadt *__ALIGN_SIZE = 0x200000;
380c88df2fSderaadt */
390c88df2fSderaadt__ALIGN_SIZE = 0x1000;
4035511468Sderaadt__kernel_base = 0xffffffff80000000;
4135511468Sderaadt__kernel_virt_base = __kernel_base + 0x1000000;
4235511468Sderaadt__kernel_phys_base = 0x1000000;
4329fcb0c6Sguenther__kernel_virt_to_phys = __kernel_phys_base - __kernel_virt_base;
440c88df2fSderaadt
4535511468SderaadtENTRY(start)
460c88df2fSderaadtSECTIONS
470c88df2fSderaadt{
483a759fb4Smortimer	.text ALIGN(__kernel_virt_base + SIZEOF_HEADERS, __ALIGN_SIZE) :
493a759fb4Smortimer	      AT (ALIGN(__kernel_phys_base + SIZEOF_HEADERS, __ALIGN_SIZE))
500c88df2fSderaadt	{
5135511468Sderaadt		start = .;
52527d7fdfSderaadt		locore0.o(.text)
530c88df2fSderaadt		*(.text .text.*)
541fc8fad1Sguenther		. = ALIGN(__ALIGN_SIZE);
551fc8fad1Sguenther		__text_page_start = ABSOLUTE(.);
561fc8fad1Sguenther		*(.ktext.page)
571fc8fad1Sguenther		__text_page_end = ABSOLUTE(.);
58b20d6be6Sderaadt	} :text =0xcccccccc
5935511468Sderaadt
60b767b017Sguenther	. = ALIGN(__ALIGN_SIZE);
6129fcb0c6Sguenther	__kernel_kutext_phys = . + __kernel_virt_to_phys;
62b767b017Sguenther	.kutext : AT (__kernel_kutext_phys)
63b767b017Sguenther	{
64b767b017Sguenther		__kutext_start = ABSOLUTE(.);
65b767b017Sguenther		*(.kutext)
66b767b017Sguenther		__kutext_end = ABSOLUTE(.);
67b767b017Sguenther	} :text =0xcccccccc
68b767b017Sguenther
695f6ecb19Ssf	. = ALIGN(__ALIGN_SIZE);
705f6ecb19Ssf	__kernel_cptext_phys = . + __kernel_virt_to_phys;
715f6ecb19Ssf	.cptext : AT (__kernel_cptext_phys)
725f6ecb19Ssf	{
735f6ecb19Ssf		__cptext_start = ABSOLUTE(.);
745f6ecb19Ssf		*(.cptext)
755f6ecb19Ssf		__cptext_end = ABSOLUTE(.);
765f6ecb19Ssf	} :text =0xcccccccc
775f6ecb19Ssf
780c88df2fSderaadt	PROVIDE (etext = .);
790c88df2fSderaadt	_etext = .;
800c88df2fSderaadt
810c88df2fSderaadt	/* Move rodata to the next page, so we can nuke X and W bit on them */
820c88df2fSderaadt	. = ALIGN(__ALIGN_SIZE);
8329fcb0c6Sguenther	__kernel_rodata_phys = . + __kernel_virt_to_phys;
8435511468Sderaadt	.rodata : AT (__kernel_rodata_phys)
850c88df2fSderaadt	{
860c88df2fSderaadt		__rodata_start = ABSOLUTE(.);
870c88df2fSderaadt		*(.rodata .rodata.*)
88*4c5e57a2Sguenther		. = ALIGN(8);
89*4c5e57a2Sguenther		__nofault_start = ABSOLUTE(.);
90*4c5e57a2Sguenther		*(.nofault.*) *(.nofault)
91*4c5e57a2Sguenther		__nofault_end = ABSOLUTE(.);
92d817e038Skettenis		*(.codepatch)
93d817e038Skettenis		*(.codepatchend)
94b20d6be6Sderaadt	} :rodata =0xcccccccc
9535511468Sderaadt
961fc8fad1Sguenther	. = ALIGN(__ALIGN_SIZE);
971fc8fad1Sguenther	__kernel_kutext_page_phys = . + __kernel_virt_to_phys;
981fc8fad1Sguenther	.kutext.page : AT (__kernel_kutext_page_phys)
991fc8fad1Sguenther	{
1001fc8fad1Sguenther		*(SORT_BY_ALIGNMENT(.kutext.page))
1011fc8fad1Sguenther	} :rodata =0xcccccccc
1021fc8fad1Sguenther
103176f97ecSderaadt	. = ALIGN(0x1000);
10429fcb0c6Sguenther	__kernel_randomdata_phys = . + __kernel_virt_to_phys;
105176f97ecSderaadt	.openbsd.randomdata : AT (__kernel_randomdata_phys)
106176f97ecSderaadt	{
107c2bc03caSguenther		__retguard_start = ABSOLUTE(.);
108f1bfbdc7Sguenther		*(.openbsd.randomdata.retguard .openbsd.randomdata.retguard.*)
109f1bfbdc7Sguenther		. = ALIGN(__ALIGN_SIZE);
110c2bc03caSguenther		__retguard_end = ABSOLUTE(.);
111c2bc03caSguenther		*(.openbsd.randomdata .openbsd.randomdata.*)
112b20d6be6Sderaadt	} :rodata :openbsd_randomize =0xcccccccc
113176f97ecSderaadt	. = ALIGN(0x1000);
1140c88df2fSderaadt	PROVIDE (erodata = .);
1150c88df2fSderaadt	_erodata = .;
1160c88df2fSderaadt	/* Move data to the next page, so we can add W bit on them */
1170c88df2fSderaadt	. = ALIGN(__ALIGN_SIZE);
11829fcb0c6Sguenther	__kernel_data_phys = . + __kernel_virt_to_phys;
11935511468Sderaadt	.data : AT (__kernel_data_phys)
1200c88df2fSderaadt	{
1210c88df2fSderaadt		__data_start = ABSOLUTE(.);
1220c88df2fSderaadt		*(.data .data.*)
123b20d6be6Sderaadt	} :data =0xcccccccc
124b767b017Sguenther
125b767b017Sguenther	. = ALIGN(__ALIGN_SIZE);
12629fcb0c6Sguenther	__kernel_kudata_phys = . + __kernel_virt_to_phys;
127b767b017Sguenther	.kudata : AT (__kernel_kudata_phys)
128b767b017Sguenther	{
129b767b017Sguenther		__kudata_start = ABSOLUTE(.);
130b767b017Sguenther		*(.kudata)
131b767b017Sguenther		__kudata_end = ABSOLUTE(.);
132b767b017Sguenther	} :data =0xcccccccc
133b767b017Sguenther
134b767b017Sguenther	. = ALIGN(0x1000);
1350c88df2fSderaadt	PROVIDE (edata = .);
1360c88df2fSderaadt	_edata = .;
1370c88df2fSderaadt
1380c88df2fSderaadt	/* BSS starts right after padded data */
13929fcb0c6Sguenther	__kernel_bss_phys = . + __kernel_virt_to_phys;
14035511468Sderaadt	.bss : AT (__kernel_bss_phys)
1410c88df2fSderaadt	{
1420c88df2fSderaadt		*(.bss .bss.*)
1430c88df2fSderaadt		*(COMMON)
144c7636a68Smlarkin		/* Align after .bss to ensure correct alignment even if the
145c7636a68Smlarkin		 * .bss section disappears because there are no input sections.
1460c88df2fSderaadt		 */
147c7636a68Smlarkin		. = ALIGN(0x1000);
1480c88df2fSderaadt	} :bss
149c7636a68Smlarkin	__kernel_bss_end = .;
150c7636a68Smlarkin	. = ALIGN(0x200000);
1510c88df2fSderaadt	_end = .;
1520c88df2fSderaadt	PROVIDE (end = .);
1534cffaa08Skettenis	__kernel_phys_end = . + __kernel_virt_to_phys;
1540c88df2fSderaadt
1550c88df2fSderaadt	/* XXX - hack alert, since we are not C++, nuke these */
1560c88df2fSderaadt	/DISCARD/ :
1570c88df2fSderaadt	{
1580c88df2fSderaadt		*(.note.GNU-stack)
1590c88df2fSderaadt		*(.eh_frame)
1600c88df2fSderaadt	}
1610c88df2fSderaadt}
162