1 /*-
2 * Copyright 1996-1998 John D. Polstra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD$
26 */
27
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/exec.h>
32 #include <sys/imgact.h>
33 #include <sys/malloc.h>
34 #include <sys/proc.h>
35 #include <sys/namei.h>
36 #include <sys/fcntl.h>
37 #include <sys/sysent.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/syscall.h>
40 #include <sys/signalvar.h>
41 #include <sys/vnode.h>
42 #include <sys/linker.h>
43
44 #include <vm/vm.h>
45 #include <vm/vm_param.h>
46
47 #include <machine/cpu.h>
48 #include <machine/elf.h>
49 #include <machine/md_var.h>
50
51 struct sysentvec elf32_freebsd_sysvec = {
52 SYS_MAXSYSCALL,
53 sysent,
54 0,
55 0,
56 NULL,
57 0,
58 NULL,
59 NULL,
60 __elfN(freebsd_fixup),
61 sendsig,
62 sigcode,
63 &szsigcode,
64 NULL,
65 "FreeBSD ELF32",
66 __elfN(coredump),
67 NULL,
68 MINSIGSTKSZ,
69 PAGE_SIZE,
70 VM_MIN_ADDRESS,
71 VM_MAXUSER_ADDRESS,
72 USRSTACK,
73 PS_STRINGS,
74 VM_PROT_ALL,
75 exec_copyout_strings,
76 exec_setregs,
77 NULL
78 };
79
80 static Elf32_Brandinfo freebsd_brand_info = {
81 ELFOSABI_FREEBSD,
82 EM_PPC,
83 "FreeBSD",
84 NULL,
85 "/libexec/ld-elf.so.1",
86 &elf32_freebsd_sysvec,
87 NULL,
88 };
89
90 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
91 (sysinit_cfunc_t) elf32_insert_brand_entry,
92 &freebsd_brand_info);
93
94 static Elf32_Brandinfo freebsd_brand_oinfo = {
95 ELFOSABI_FREEBSD,
96 EM_PPC,
97 "FreeBSD",
98 NULL,
99 "/usr/libexec/ld-elf.so.1",
100 &elf32_freebsd_sysvec,
101 NULL,
102 };
103
104 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
105 (sysinit_cfunc_t) elf32_insert_brand_entry,
106 &freebsd_brand_oinfo);
107
108
109 void
110 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
111 size_t *off __unused)
112 {
113 }
114
115
116 /* Process one elf relocation with addend. */
117 static int
118 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
119 int type, int local, elf_lookup_fn lookup)
120 {
121 Elf_Addr *where;
122 Elf_Half *hwhere;
123 Elf_Addr addr;
124 Elf_Addr addend;
125 Elf_Word rtype, symidx;
126 const Elf_Rela *rela;
127
128 switch (type) {
129 case ELF_RELOC_REL:
130 panic("PPC only supports RELA relocations");
131 break;
132 case ELF_RELOC_RELA:
133 rela = (const Elf_Rela *)data;
134 where = (Elf_Addr *) (relocbase + rela->r_offset);
135 hwhere = (Elf_Half *) (relocbase + rela->r_offset);
136 addend = rela->r_addend;
137 rtype = ELF_R_TYPE(rela->r_info);
138 symidx = ELF_R_SYM(rela->r_info);
139 break;
140 default:
141 panic("elf_reloc: unknown relocation mode %d\n", type);
142 }
143
144 switch (rtype) {
145
146 case R_PPC_NONE:
147 break;
148
149 case R_PPC_ADDR32: /* word32 S + A */
150 addr = lookup(lf, symidx, 1);
151 if (addr == 0)
152 return -1;
153 addr += addend;
154 *where = addr;
155 break;
156
157 case R_PPC_ADDR16_LO: /* #lo(S) */
158 addr = lookup(lf, symidx, 1);
159 if (addr == 0)
160 return -1;
161 /*
162 * addend values are sometimes relative to sections
163 * (i.e. .rodata) in rela, where in reality they
164 * are relative to relocbase. Detect this condition.
165 */
166 if (addr > relocbase && addr <= (relocbase + addend))
167 addr = relocbase + addend;
168 else
169 addr += addend;
170 *hwhere = addr & 0xffff;
171 break;
172
173 case R_PPC_ADDR16_HA: /* #ha(S) */
174 addr = lookup(lf, symidx, 1);
175 if (addr == 0)
176 return -1;
177 /*
178 * addend values are sometimes relative to sections
179 * (i.e. .rodata) in rela, where in reality they
180 * are relative to relocbase. Detect this condition.
181 */
182 if (addr > relocbase && addr <= (relocbase + addend))
183 addr = relocbase + addend;
184 else
185 addr += addend;
186 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
187 & 0xffff;
188 break;
189
190 case R_PPC_RELATIVE: /* word32 B + A */
191 *where = relocbase + addend;
192 break;
193
194 default:
195 printf("kldload: unexpected relocation type %d\n",
196 (int) rtype);
197 return -1;
198 }
199 return(0);
200 }
201
202 int
203 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
204 elf_lookup_fn lookup)
205 {
206
207 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
208 }
209
210 int
211 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
212 int type, elf_lookup_fn lookup)
213 {
214
215 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
216 }
217
218 int
219 elf_cpu_load_file(linker_file_t lf)
220 {
221 /* Only sync the cache for non-kernel modules */
222 if (lf->id != 1)
223 __syncicache(lf->address, lf->size);
224 return (0);
225 }
226
227 int
228 elf_cpu_unload_file(linker_file_t lf __unused)
229 {
230
231 return (0);
232 }
Cache object: 1a652e4dff70c9bf0898af74af15fc23
|