xref: /linux/arch/arm64/kernel/image.h (revision 120dc60d)
1*caab277bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2a2c1d73bSMark Rutland /*
3a2c1d73bSMark Rutland  * Linker script macros to generate Image header fields.
4a2c1d73bSMark Rutland  *
5a2c1d73bSMark Rutland  * Copyright (C) 2014 ARM Ltd.
6a2c1d73bSMark Rutland  */
7f56063c5SAKASHI Takahiro #ifndef __ARM64_KERNEL_IMAGE_H
8f56063c5SAKASHI Takahiro #define __ARM64_KERNEL_IMAGE_H
9a2c1d73bSMark Rutland 
10a2c1d73bSMark Rutland #ifndef LINKER_SCRIPT
11a2c1d73bSMark Rutland #error This file should only be included in vmlinux.lds.S
12a2c1d73bSMark Rutland #endif
13a2c1d73bSMark Rutland 
14f56063c5SAKASHI Takahiro #include <asm/image.h>
15f56063c5SAKASHI Takahiro 
16a2c1d73bSMark Rutland /*
17a2c1d73bSMark Rutland  * There aren't any ELF relocations we can use to endian-swap values known only
18a2c1d73bSMark Rutland  * at link time (e.g. the subtraction of two symbol addresses), so we must get
19a2c1d73bSMark Rutland  * the linker to endian-swap certain values before emitting them.
206ad1fe5dSArd Biesheuvel  *
216ad1fe5dSArd Biesheuvel  * Note that, in order for this to work when building the ELF64 PIE executable
226ad1fe5dSArd Biesheuvel  * (for KASLR), these values should not be referenced via R_AARCH64_ABS64
236ad1fe5dSArd Biesheuvel  * relocations, since these are fixed up at runtime rather than at build time
246ad1fe5dSArd Biesheuvel  * when PIE is in effect. So we need to split them up in 32-bit high and low
256ad1fe5dSArd Biesheuvel  * words.
26a2c1d73bSMark Rutland  */
27a2c1d73bSMark Rutland #ifdef CONFIG_CPU_BIG_ENDIAN
286ad1fe5dSArd Biesheuvel #define DATA_LE32(data)				\
296ad1fe5dSArd Biesheuvel 	((((data) & 0x000000ff) << 24) |	\
306ad1fe5dSArd Biesheuvel 	 (((data) & 0x0000ff00) << 8)  |	\
316ad1fe5dSArd Biesheuvel 	 (((data) & 0x00ff0000) >> 8)  |	\
326ad1fe5dSArd Biesheuvel 	 (((data) & 0xff000000) >> 24))
33a2c1d73bSMark Rutland #else
346ad1fe5dSArd Biesheuvel #define DATA_LE32(data) ((data) & 0xffffffff)
35a2c1d73bSMark Rutland #endif
36a2c1d73bSMark Rutland 
376ad1fe5dSArd Biesheuvel #define DEFINE_IMAGE_LE64(sym, data)				\
386ad1fe5dSArd Biesheuvel 	sym##_lo32 = DATA_LE32((data) & 0xffffffff);		\
396ad1fe5dSArd Biesheuvel 	sym##_hi32 = DATA_LE32((data) >> 32)
406ad1fe5dSArd Biesheuvel 
41f56063c5SAKASHI Takahiro #define __HEAD_FLAG(field)	(__HEAD_FLAG_##field << \
42f56063c5SAKASHI Takahiro 					ARM64_IMAGE_FLAG_##field##_SHIFT)
43f56063c5SAKASHI Takahiro 
44a2c1d73bSMark Rutland #ifdef CONFIG_CPU_BIG_ENDIAN
45f56063c5SAKASHI Takahiro #define __HEAD_FLAG_BE		ARM64_IMAGE_FLAG_BE
46a2c1d73bSMark Rutland #else
47f56063c5SAKASHI Takahiro #define __HEAD_FLAG_BE		ARM64_IMAGE_FLAG_LE
48a2c1d73bSMark Rutland #endif
49a2c1d73bSMark Rutland 
509d372c9fSArd Biesheuvel #define __HEAD_FLAG_PAGE_SIZE	((PAGE_SHIFT - 10) / 2)
519d372c9fSArd Biesheuvel 
52a7f8de16SArd Biesheuvel #define __HEAD_FLAG_PHYS_BASE	1
53a7f8de16SArd Biesheuvel 
54f56063c5SAKASHI Takahiro #define __HEAD_FLAGS		(__HEAD_FLAG(BE)	| \
55f56063c5SAKASHI Takahiro 				 __HEAD_FLAG(PAGE_SIZE) | \
56f56063c5SAKASHI Takahiro 				 __HEAD_FLAG(PHYS_BASE))
57a2c1d73bSMark Rutland 
58a2c1d73bSMark Rutland /*
59a2c1d73bSMark Rutland  * These will output as part of the Image header, which should be little-endian
60a2c1d73bSMark Rutland  * regardless of the endianness of the kernel. While constant values could be
61a2c1d73bSMark Rutland  * endian swapped in head.S, all are done here for consistency.
62a2c1d73bSMark Rutland  */
63a2c1d73bSMark Rutland #define HEAD_SYMBOLS						\
646ad1fe5dSArd Biesheuvel 	DEFINE_IMAGE_LE64(_kernel_size_le, _end - _text);	\
656ad1fe5dSArd Biesheuvel 	DEFINE_IMAGE_LE64(_kernel_flags_le, __HEAD_FLAGS);
66a2c1d73bSMark Rutland 
67f56063c5SAKASHI Takahiro #endif /* __ARM64_KERNEL_IMAGE_H */
68