xref: /haiku/src/system/kernel/arch/arm64/arch_elf.cpp (revision 8c78892580f132d10e624aef96f835df8d94bf19)
1 /*
2  * Copyright 2004-2018 Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  * Copyright 2002, Travis Geiselbrecht. All rights reserved.
5  * Distributed under the terms of the NewOS License.
6  */
7 
8 
9 #ifdef _BOOT_MODE
10 #	include <boot/arch.h>
11 #endif
12 
13 #include <KernelExport.h>
14 
15 #include <elf_priv.h>
16 #include <arch/elf.h>
17 
18 
19 //#define TRACE_ARCH_ELF
20 #ifdef TRACE_ARCH_ELF
21 #	define TRACE(x) dprintf x
22 #else
23 #	define TRACE(x) ;
24 #endif
25 
26 
27 #ifndef _BOOT_MODE
28 static bool
29 is_in_image(struct elf_image_info *image, addr_t address)
30 {
31 	return (address >= image->text_region.start
32 			&& address < image->text_region.start + image->text_region.size)
33 		|| (address >= image->data_region.start
34 			&& address < image->data_region.start + image->data_region.size);
35 }
36 #endif	// !_BOOT_MODE
37 
38 
39 #ifdef _BOOT_MODE
40 status_t
41 boot_arch_elf_relocate_rel(preloaded_elf64_image* image, Elf64_Rel* rel,
42 	int relLength)
43 #else
44 int
45 arch_elf_relocate_rel(struct elf_image_info *image,
46 	struct elf_image_info *resolveImage, Elf64_Rel *rel, int relLength)
47 #endif
48 {
49 	dprintf("arch_elf_relocate_rel: not supported on arm64\n");
50 	return B_ERROR;
51 }
52 
53 
54 #ifdef _BOOT_MODE
55 status_t
56 boot_arch_elf_relocate_rela(preloaded_elf64_image* image, Elf64_Rela* rel,
57 	int relLength)
58 #else
59 int
60 arch_elf_relocate_rela(struct elf_image_info *image,
61 	struct elf_image_info *resolveImage, Elf64_Rela *rel, int relLength)
62 #endif
63 {
64 	for (int i = 0; i < relLength / (int)sizeof(Elf64_Rela); i++) {
65 		int type = ELF64_R_TYPE(rel[i].r_info);
66 		int symIndex = ELF64_R_SYM(rel[i].r_info);
67 		Elf64_Addr symAddr = 0;
68 
69 		// Resolve the symbol, if any.
70 		if (symIndex != 0) {
71 			Elf64_Sym* symbol = SYMBOL(image, symIndex);
72 
73 			status_t status;
74 #ifdef _BOOT_MODE
75 			status = boot_elf_resolve_symbol(image, symbol, &symAddr);
76 #else
77 			status = elf_resolve_symbol(image, symbol, resolveImage, &symAddr);
78 #endif
79 			if (status < B_OK)
80 				return status;
81 		}
82 
83 		// Address of the relocation.
84 		Elf64_Addr relocAddr = image->text_region.delta + rel[i].r_offset;
85 
86 		// Calculate the relocation value.
87 		Elf64_Addr relocValue;
88 		switch (type) {
89 			default:
90 				dprintf("arch_elf_relocate_rela: unhandled relocation type %d\n",
91 					type);
92 				return B_BAD_DATA;
93 		}
94 #ifdef _BOOT_MODE
95 		boot_elf64_set_relocation(relocAddr, relocValue);
96 #else
97 		if (!is_in_image(image, relocAddr)) {
98 			dprintf("arch_elf_relocate_rela: invalid offset %#lx\n",
99 				rel[i].r_offset);
100 			return B_BAD_ADDRESS;
101 		}
102 #endif
103 	}
104 
105 	return B_OK;
106 }
107