1 /* $NetBSD: kobj_machdep.c,v 1.4 2023/04/28 07:33:55 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*-
30 * Copyright 1996-1998 John D. Polstra.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.4 2023/04/28 07:33:55 skrll Exp $");
56
57 #define ELFSIZE ARCH_ELFSIZE
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kobj.h>
62 #include <sys/exec.h>
63 #include <sys/exec_elf.h>
64
65 int
kobj_reloc(kobj_t ko,uintptr_t relocbase,const void * data,bool isrela,bool local)66 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
67 bool isrela, bool local)
68 {
69 Elf_Addr *where;
70 Elf_Addr addr;
71 Elf_Addr addend;
72 uintptr_t rtype, symidx;
73 const Elf_Rel *rel;
74 const Elf_Rela *rela;
75 int error;
76
77 if (isrela) {
78 rela = (const Elf_Rela *)data;
79 where = (Elf_Addr *) (relocbase + rela->r_offset);
80 addend = rela->r_addend;
81 rtype = ELF_R_TYPE(rela->r_info);
82 symidx = ELF_R_SYM(rela->r_info);
83 } else {
84 rel = (const Elf_Rel *)data;
85 where = (Elf_Addr *) (relocbase + rel->r_offset);
86 addend = *where;
87 rtype = ELF_R_TYPE(rel->r_info);
88 symidx = ELF_R_SYM(rel->r_info);
89 }
90
91 const Elf_Sym *sym = kobj_symbol(ko, symidx);
92
93 if (!local && ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
94 return 0;
95 }
96
97 switch (rtype) {
98 case R_ALPHA_NONE:
99 break;
100
101 case R_ALPHA_REFQUAD:
102 error = kobj_sym_lookup(ko, symidx, &addr);
103 if (error)
104 return -1;
105 addr += addend;
106 if (*where != addr)
107 *where = addr;
108 break;
109
110 case R_ALPHA_GLOB_DAT:
111 error = kobj_sym_lookup(ko, symidx, &addr);
112 if (error)
113 return -1;
114 addr += addend;
115 if (*where != addr)
116 *where = addr;
117 break;
118
119 case R_ALPHA_JMP_SLOT:
120 /* No point in lazy binding for kernel modules. */
121 error = kobj_sym_lookup(ko, symidx, &addr);
122 if (error)
123 return -1;
124 if (*where != addr)
125 *where = addr;
126 break;
127
128 case R_ALPHA_RELATIVE:
129 addr = relocbase + addend;
130 if (*where != addr)
131 *where = addr;
132 break;
133
134 case R_ALPHA_COPY:
135 /*
136 * There shouldn't be copy relocations in kernel
137 * objects.
138 */
139 printf("kobj_reloc: unexpected R_COPY relocation\n");
140 return -1;
141
142 default:
143 printf("kobj_reloc: unexpected relocation type %d\n",
144 (int)rtype);
145 return -1;
146 }
147
148 return 0;
149 }
150
151 int
kobj_machdep(kobj_t ko,void * base,size_t size,bool load)152 kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
153 {
154
155 return 0;
156 }
157