xref: /haiku/src/system/kernel/arch/sparc/arch_elf.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
1 /*
2  * Copyright 2019-2020, Adrien Destugues <pulkomandy@pulkomandy.tk>
3  * Copyright 2010, Ithamar R. Adema <ithamar.adema@team-embedded.nl>
4  * Copyright 2009, Johannes Wischert, johanneswi@gmail.com.
5  * Copyright 2005, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
6  * Copyright 2002, Travis Geiselbrecht. All rights reserved.
7  * Distributed under the terms of the MIT License.
8  */
9 
10 
11 #include <KernelExport.h>
12 
13 #include <elf_priv.h>
14 #include <boot/elf.h>
15 #include <arch/elf.h>
16 
17 
18 //#define TRACE_ARCH_ELF
19 #ifdef TRACE_ARCH_ELF
20 #	define TRACE(x) dprintf x
21 #	define CHATTY 1
22 #else
23 #	define TRACE(x) ;
24 #	define CHATTY 0
25 #endif
26 
27 
28 #ifndef _BOOT_MODE
29 static bool
30 is_in_image(struct elf_image_info *image, addr_t address)
31 {
32 	return (address >= image->text_region.start
33 			&& address < image->text_region.start + image->text_region.size)
34 		|| (address >= image->data_region.start
35 			&& address < image->data_region.start + image->data_region.size);
36 }
37 #endif	// !_BOOT_MODE
38 
39 
40 #ifdef _BOOT_MODE
41 status_t
42 boot_arch_elf_relocate_rel(struct preloaded_elf64_image *image, Elf64_Rel *rel,
43 	int rel_len)
44 #else
45 int
46 arch_elf_relocate_rel(struct elf_image_info *image,
47 	struct elf_image_info *resolve_image, Elf64_Rel *rel, int rel_len)
48 #endif
49 {
50 	// there are no rel entries in M68K elf
51 	return B_NO_ERROR;
52 }
53 
54 
55 static inline void
56 write_word32(addr_t P, Elf64_Word value)
57 {
58 	*(Elf64_Word*)P = value;
59 }
60 
61 
62 static inline void
63 write_word64(addr_t P, Elf64_Xword value)
64 {
65 	*(Elf64_Xword*)P = value;
66 }
67 
68 
69 static inline void
70 write_hi30(addr_t P, Elf64_Word value)
71 {
72 	*(Elf64_Word*)P |= value >> 2;
73 }
74 
75 
76 static inline void
77 write_hi22(addr_t P, Elf64_Word value)
78 {
79 	*(Elf64_Word*)P |= value >> 10;
80 }
81 
82 
83 static inline void
84 write_lo10(addr_t P, Elf64_Word value)
85 {
86 	*(Elf64_Word*)P |= value & 0x3ff;
87 }
88 
89 
90 #ifdef _BOOT_MODE
91 status_t
92 boot_arch_elf_relocate_rela(struct preloaded_elf64_image *image,
93 	Elf64_Rela *rel, int rel_len)
94 #else
95 int
96 arch_elf_relocate_rela(struct elf_image_info *image,
97 	struct elf_image_info *resolve_image, Elf64_Rela *rel, int rel_len)
98 #endif
99 {
100 	int i;
101 	Elf64_Sym *sym;
102 	int vlErr;
103 
104 	Elf64_Addr S = 0;	// symbol address
105 	//addr_t R = 0;		// section relative symbol address
106 
107 	//addr_t G = 0;		// GOT address
108 	//addr_t L = 0;		// PLT address
109 
110 	#define P	((addr_t)(image->text_region.delta + rel[i].r_offset))
111 	#define A	((addr_t)rel[i].r_addend)
112 	#define B	(image->text_region.delta)
113 
114 	// TODO: Get the GOT address!
115 	#define REQUIRE_GOT	\
116 		if (G == 0) {	\
117 			dprintf("arch_elf_relocate_rela(): Failed to get GOT address!\n"); \
118 			return B_ERROR;	\
119 		}
120 
121 	// TODO: Get the PLT address!
122 	#define REQUIRE_PLT	\
123 		if (L == 0) {	\
124 			dprintf("arch_elf_relocate_rela(): Failed to get PLT address!\n"); \
125 			return B_ERROR;	\
126 		}
127 
128 	for (i = 0; i * (int)sizeof(Elf64_Rela) < rel_len; i++) {
129 #if CHATTY
130 		dprintf("looking at rel type %" PRIu64 ", offset 0x%lx, sym 0x%lx, "
131 			"addend 0x%lx\n", ELF64_R_TYPE(rel[i].r_info), rel[i].r_offset,
132 			ELF64_R_SYM(rel[i].r_info), rel[i].r_addend);
133 #endif
134 		// Relocation types and what to do with them are defined in Oracle docs
135 		// Documentation Home > Linker and Libraries Guide
136 		// 	> Chapter 7 Object File Format > File Format > Relocation Sections
137 		// 	> Relocation Types (Processor-Specific) > SPARC: Relocation Types
138 		// https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter6-24/index.html
139 		// https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter6-24-1/index.html
140 		switch (ELF64_R_TYPE(rel[i].r_info)) {
141 			case R_SPARC_WDISP30:
142 			case R_SPARC_HI22:
143 			case R_SPARC_LO10:
144 			case R_SPARC_GLOB_DAT:
145 			case R_SPARC_JMP_SLOT:
146 			case R_SPARC_64:
147 				sym = SYMBOL(image, ELF64_R_SYM(rel[i].r_info));
148 #ifdef _BOOT_MODE
149 				vlErr = boot_elf_resolve_symbol(image, sym, &S);
150 #else
151 				vlErr = elf_resolve_symbol(image, sym, resolve_image, &S);
152 #endif
153 				if (vlErr < 0) {
154 					dprintf("%s(): Failed to relocate "
155 						"entry index %d, rel type %" PRIu64 ", offset 0x%lx, "
156 						"sym 0x%lx, addend 0x%lx\n", __FUNCTION__, i,
157 						ELF64_R_TYPE(rel[i].r_info),
158 						rel[i].r_offset, ELF64_R_SYM(rel[i].r_info),
159 						rel[i].r_addend);
160 					return vlErr;
161 				}
162 				break;
163 		}
164 
165 		switch (ELF64_R_TYPE(rel[i].r_info)) {
166 			case R_SPARC_WDISP30:
167 			{
168 				write_hi30(P, S + A - P);
169 			}
170 			case R_SPARC_HI22:
171 			{
172 				write_hi22(P, S + A);
173 				break;
174 			}
175 			case R_SPARC_LO10:
176 			{
177 				write_lo10(P, S + A);
178 				break;
179 			}
180 			case R_SPARC_GLOB_DAT:
181 			{
182 				write_word64(P, S + A);
183 				break;
184 			}
185 			case R_SPARC_JMP_SLOT:
186 			{
187 				// Created by the link-editor for dynamic objects to provide
188 				// lazy binding. The relocation offset member gives the
189 				// location of a procedure linkage table entry. The runtime
190 				// linker modifies the procedure linkage table entry to
191 				// transfer control to the designated symbol address.
192 				addr_t jumpOffset = S - (P + 8);
193 				if ((jumpOffset & 0xc0000000) != 0
194 					&& (~jumpOffset & 0xc0000000) != 0) {
195 					// Offset > 30 bit.
196 					// TODO: Implement!
197 					// See https://docs.oracle.com/cd/E26502_01/html/E26507/chapter6-1235.html
198 					// examples .PLT102 and .PLT103
199 					dprintf("arch_elf_relocate_rela(): R_SPARC_JMP_SLOT: "
200 						"Offsets > 30 bit currently not supported!\n");
201 					dprintf("jumpOffset: %p\n", (void*)jumpOffset);
202 					return B_ERROR;
203 				} else {
204 					uint32* instructions = (uint32*)P;
205 					// We need to use a call instruction because it has a lot
206 					// of space for the destination (30 bits). However, it
207 					// erases o7, which we don't want.
208 					// We could avoid this with a JMPL if the displacement was
209 					// small enough, but it probably isn't.
210 					// So, we store o7 in g1 before the call, and restore it
211 					// in the branch delay slot. Crazy, but it works!
212 					instructions[0] = 0x01000000; // NOP to preserve the alignment?
213 					instructions[1] = 0x8210000f; // MOV %o7, %g1
214 					instructions[2] = 0x40000000 | ((jumpOffset >> 2) & 0x3fffffff);
215 					instructions[3] = 0x9e100001; // MOV %g1, %o7
216 				}
217 				break;
218 			}
219 			case R_SPARC_RELATIVE:
220 			{
221 				write_word32(P, B + A);
222 				break;
223 			}
224 			case R_SPARC_64:
225 			{
226 				write_word64(P, S + A);
227 				break;
228 			}
229 			default:
230 				dprintf("arch_elf_relocate_rela: unhandled relocation type %"
231 					PRIu64 "\n", ELF64_R_TYPE(rel[i].r_info));
232 				return B_ERROR;
233 		}
234 	}
235 	return B_OK;
236 }
237