xref: /haiku/src/system/kernel/arch/sparc/arch_elf.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2019, 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 <arch/elf.h>
15 
16 
17 //#define TRACE_ARCH_ELF
18 #ifdef TRACE_ARCH_ELF
19 #	define TRACE(x) dprintf x
20 #	define CHATTY 1
21 #else
22 #	define TRACE(x) ;
23 #	define CHATTY 0
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(struct preloaded_elf64_image *image, Elf64_Rel *rel,
42 	int rel_len)
43 #else
44 int
45 arch_elf_relocate_rel(struct elf_image_info *image,
46 	struct elf_image_info *resolve_image, Elf64_Rel *rel, int rel_len)
47 #endif
48 {
49 	// there are no rel entries in M68K elf
50 	return B_NO_ERROR;
51 }
52 
53 
54 static inline void
55 write_32(addr_t P, Elf32_Word value)
56 {
57 	*(Elf32_Word*)P = value;
58 }
59 
60 
61 static inline void
62 write_16(addr_t P, Elf32_Word value)
63 {
64 	// bits 16:29
65 	*(Elf32_Half*)P = (Elf32_Half)value;
66 }
67 
68 
69 static inline bool
70 write_16_check(addr_t P, Elf32_Word value)
71 {
72 	// bits 15:0
73 	if ((value & 0xffff0000) && (~value & 0xffff8000))
74 		return false;
75 	*(Elf32_Half*)P = (Elf32_Half)value;
76 	return true;
77 }
78 
79 
80 static inline bool
81 write_8(addr_t P, Elf32_Word value)
82 {
83 	// bits 7:0
84 	*(uint8 *)P = (uint8)value;
85 	return true;
86 }
87 
88 
89 static inline bool
90 write_8_check(addr_t P, Elf32_Word value)
91 {
92 	// bits 7:0
93 	if ((value & 0xffffff00) && (~value & 0xffffff80))
94 		return false;
95 	*(uint8 *)P = (uint8)value;
96 	return true;
97 }
98 
99 
100 #ifdef _BOOT_MODE
101 status_t
102 boot_arch_elf_relocate_rela(struct preloaded_elf64_image *image,
103 	Elf64_Rela *rel, int rel_len)
104 #else
105 int
106 arch_elf_relocate_rela(struct elf_image_info *image,
107 	struct elf_image_info *resolve_image, Elf64_Rela *rel, int rel_len)
108 #endif
109 {
110 	return B_OK;
111 }
112