]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/elf_machdep.c
This commit was generated by cvs2svn to compensate for changes in r179191,
[FreeBSD/FreeBSD.git] / sys / mips / mips / elf_machdep.c
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  *      from: src/sys/i386/i386/elf_machdep.c,v 1.20 2004/08/11 02:35:05 marcel
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/imgact.h>
36 #include <sys/linker.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
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 #include <vm/vm_param.h>
46
47 #include <machine/elf.h>
48 #include <machine/md_var.h>
49
50 struct sysentvec elf32_freebsd_sysvec = {
51         SYS_MAXSYSCALL,
52         sysent,
53         0,
54         0,
55         NULL,
56         0,
57         NULL,
58         NULL,
59         __elfN(freebsd_fixup),
60         sendsig,
61         sigcode,
62         &szsigcode,
63         NULL,
64         "FreeBSD ELF32",
65         __elfN(coredump),
66         NULL,
67         MINSIGSTKSZ,
68         PAGE_SIZE,
69         VM_MIN_ADDRESS,
70         VM_MAXUSER_ADDRESS,
71         USRSTACK,
72         PS_STRINGS,
73         VM_PROT_ALL,
74         exec_copyout_strings,
75         exec_setregs,
76         NULL
77 };
78
79 static Elf32_Brandinfo freebsd_brand_info = {
80                                                 ELFOSABI_FREEBSD,
81                                                 EM_MIPS,
82                                                 "FreeBSD",
83                                                 NULL,
84                                                 "/libexec/ld-elf.so.1",
85                                                 &elf32_freebsd_sysvec,
86                                                 NULL,
87                                           };
88
89 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY,
90         (sysinit_cfunc_t) elf32_insert_brand_entry,
91         &freebsd_brand_info);
92
93 static Elf32_Brandinfo freebsd_brand_oinfo = {
94                                                 ELFOSABI_FREEBSD,
95                                                 EM_MIPS,
96                                                 "FreeBSD",
97                                                 NULL,
98                                                 "/usr/libexec/ld-elf.so.1",
99                                                 &elf32_freebsd_sysvec,
100                                                 NULL,
101                                           };
102
103 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
104         (sysinit_cfunc_t) elf32_insert_brand_entry,
105         &freebsd_brand_oinfo);
106
107
108 void
109 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
110     size_t *off __unused)
111 {
112 }
113
114 /* Process one elf relocation with addend. */
115 static int
116 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
117     int type, int local, elf_lookup_fn lookup)
118 {
119         Elf_Addr *where = (Elf_Addr *)NULL;;
120         Elf_Addr addr;
121         Elf_Addr addend = (Elf_Addr)0;
122         Elf_Word rtype = (Elf_Word)0, symidx;
123         const Elf_Rel *rel;
124         const Elf_Rela *rela;
125
126         switch (type) {
127         case ELF_RELOC_REL:
128                 rel = (const Elf_Rel *)data;
129                 where = (Elf_Addr *) (relocbase + rel->r_offset);
130                 addend = *where;
131                 rtype = ELF_R_TYPE(rel->r_info);
132                 symidx = ELF_R_SYM(rel->r_info);
133                 break;
134         case ELF_RELOC_RELA:
135                 rela = (const Elf_Rela *)data;
136                 where = (Elf_Addr *) (relocbase + rela->r_offset);
137                 addend = rela->r_addend;
138                 rtype = ELF_R_TYPE(rela->r_info);
139                 symidx = ELF_R_SYM(rela->r_info);
140                 break;
141         default:
142                 panic("unknown reloc type %d\n", type);
143         }
144
145         if (local) {
146 #if 0 /* TBD  */
147                 if (rtype == R_386_RELATIVE) {  /* A + B */
148                         addr = relocbase + addend;
149                         if (*where != addr)
150                                 *where = addr;
151                 }
152                 return (0);
153 #endif
154         }
155
156         switch (rtype) {
157
158                 case R_MIPS_NONE:       /* none */
159                         break;
160
161                 case R_MIPS_16:     /* S + sign-extend(A) */
162                         /*
163                          * There shouldn't be R_MIPS_16 relocs in kernel objects.
164                          */
165                         printf("kldload: unexpected R_MIPS_16 relocation\n");
166                         return -1;
167                         break;
168
169                 case R_MIPS_32: /* S + A - P */
170                         addr = lookup(lf, symidx, 1);
171                         if (addr == 0)
172                                 return -1;
173                         addr += addend;
174                         if (*where != addr)
175                                 *where = addr;
176                         break;
177
178                 case R_MIPS_REL32:              /* A - EA + S */
179                         /*
180                          * There shouldn't be R_MIPS_REL32 relocs in kernel objects?
181                          */
182                         printf("kldload: unexpected R_MIPS_REL32 relocation\n");
183                         return -1;
184                         break;
185
186                 case R_MIPS_26:      /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
187                         break;
188
189                 case R_MIPS_HI16:
190                         /* extern/local: ((AHL + S) - ((short)(AHL + S)) >> 16 */
191                         /* _gp_disp: ((AHL + GP - P) - (short)(AHL + GP - P)) >> 16 */
192                         break;
193
194                 case R_MIPS_LO16:
195                         /* extern/local: AHL + S */
196                         /* _gp_disp: AHL + GP - P + 4 */
197                         break;
198
199                 case R_MIPS_GPREL16:
200                         /* extern/local: ((AHL + S) - ((short)(AHL + S)) >> 16 */
201                         /* _gp_disp: ((AHL + GP - P) - (short)(AHL + GP - P)) >> 16 */
202                         break;
203
204                 case R_MIPS_LITERAL: /* sign-extend(A) + L */
205                         break;
206
207                 case R_MIPS_GOT16: /* external: G */
208                         /* local: tbd */
209                         break;
210
211                 case R_MIPS_PC16: /* sign-extend(A) + S - P */
212                         break;
213
214                 case R_MIPS_CALL16: /* G */
215                         break;
216
217                 case R_MIPS_GPREL32: /* A + S + GP0 - GP */
218                         break;
219
220                 case R_MIPS_GOTHI16: /* (G - (short)G) >> 16 + A */
221                         break;
222
223                 case R_MIPS_GOTLO16: /* G & 0xffff */
224                         break;
225
226                 case R_MIPS_CALLHI16: /* (G - (short)G) >> 16 + A */
227                         break;
228
229                 case R_MIPS_CALLLO16: /* G & 0xffff */
230                         break;
231
232                 default:
233                         printf("kldload: unexpected relocation type %d\n",
234                             rtype);
235                         return (-1);
236         }
237         return(0);
238 }
239
240 int
241 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
242     elf_lookup_fn lookup)
243 {
244
245         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
246 }
247
248 int
249 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
250     int type, elf_lookup_fn lookup)
251 {
252
253         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
254 }
255
256 int
257 elf_cpu_load_file(linker_file_t lf __unused)
258 {
259
260         return (0);
261 }
262
263 int
264 elf_cpu_unload_file(linker_file_t lf __unused)
265 {
266
267         return (0);
268 }