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