]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/elf_machdep.c
Get rid of sv_errtbl and SV_ABI_ERRNO().
[FreeBSD/FreeBSD.git] / sys / mips / mips / elf_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 1996-1998 John D. Polstra.
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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *      from: src/sys/i386/i386/elf_machdep.c,v 1.20 2004/08/11 02:35:05 marcel
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/exec.h>
37 #include <sys/imgact.h>
38 #include <sys/linker.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/proc.h>
42 #include <sys/syscall.h>
43 #include <sys/signalvar.h>
44 #include <sys/vnode.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_param.h>
49
50 #include <machine/elf.h>
51 #include <machine/md_var.h>
52 #include <machine/cache.h>
53
54 static struct sysentvec elf_freebsd_sysvec = {
55         .sv_size        = SYS_MAXSYSCALL,
56         .sv_table       = sysent,
57         .sv_transtrap   = NULL,
58         .sv_fixup       = __elfN(freebsd_fixup),
59         .sv_sendsig     = sendsig,
60         .sv_sigcode     = sigcode,
61         .sv_szsigcode   = &szsigcode,
62 #ifdef __mips_n64
63         .sv_name        = "FreeBSD ELF64",
64 #else
65         .sv_name        = "FreeBSD ELF32",
66 #endif
67         .sv_coredump    = __elfN(coredump),
68         .sv_imgact_try  = NULL,
69         .sv_minsigstksz = MINSIGSTKSZ,
70         .sv_minuser     = VM_MIN_ADDRESS,
71         .sv_maxuser     = VM_MAXUSER_ADDRESS,
72         .sv_usrstack    = USRSTACK,
73         .sv_psstrings   = PS_STRINGS,
74         .sv_stackprot   = VM_PROT_ALL,
75         .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
76         .sv_copyout_strings = exec_copyout_strings,
77         .sv_setregs     = exec_setregs,
78         .sv_fixlimit    = NULL,
79         .sv_maxssiz     = NULL,
80 #ifdef __mips_n64
81         .sv_flags       = SV_ABI_FREEBSD | SV_LP64 | SV_ASLR,
82 #else
83         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32 | SV_ASLR,
84 #endif
85         .sv_set_syscall_retval = cpu_set_syscall_retval,
86         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
87         .sv_syscallnames = syscallnames,
88         .sv_schedtail   = NULL,
89         .sv_thread_detach = NULL,
90         .sv_trap        = NULL,
91 };
92
93 static __ElfN(Brandinfo) freebsd_brand_info = {
94         .brand          = ELFOSABI_FREEBSD,
95         .machine        = EM_MIPS,
96         .compat_3_brand = "FreeBSD",
97         .emul_path      = NULL,
98         .interp_path    = "/libexec/ld-elf.so.1",
99         .sysvec         = &elf_freebsd_sysvec,
100         .interp_newpath = NULL,
101         .brand_note     = &__elfN(freebsd_brandnote),
102         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
103 };
104
105 SYSINIT(elf, SI_SUB_EXEC, SI_ORDER_ANY,
106     (sysinit_cfunc_t) __elfN(insert_brand_entry),
107     &freebsd_brand_info);
108
109 void
110 __elfN(dump_thread)(struct thread *td __unused, void *dst __unused,
111     size_t *off __unused)
112 {
113 }
114
115 /*
116  * The following MIPS relocation code for tracking multiple
117  * consecutive HI32/LO32 entries is because of the following:
118  *
119  * https://dmz-portal.mips.com/wiki/MIPS_relocation_types
120  *
121  * ===
122  *
123  * + R_MIPS_HI16
124  *
125  * An R_MIPS_HI16 must be followed eventually by an associated R_MIPS_LO16
126  * relocation record in the same SHT_REL section. The contents of the two
127  * fields to be relocated are combined to form a full 32-bit addend AHL.
128  * An R_MIPS_LO16 entry which does not immediately follow a R_MIPS_HI16 is
129  * combined with the most recent one encountered, i.e. multiple R_MIPS_LO16
130  * entries may be associated with a single R_MIPS_HI16. Use of these
131  * relocation types in a SHT_REL section is discouraged and may be
132  * forbidden to avoid this complication.
133  *
134  * A GNU extension allows multiple R_MIPS_HI16 records to share the same
135  * R_MIPS_LO16 relocation record(s). The association works like this within
136  * a single relocation section:
137  *
138  * + From the beginning of the section moving to the end of the section,
139  *   until R_MIPS_LO16 is not found each found R_MIPS_HI16 relocation will
140  *   be associated with the first R_MIPS_LO16.
141  *
142  * + Until another R_MIPS_HI16 record is found all found R_MIPS_LO16
143  *   relocations found are associated with the last R_MIPS_HI16.
144  *
145  * ===
146  *
147  * This is so gcc can do dead code detection/removal without having to
148  * generate HI/LO pairs even if one of them would be deleted.
149  *
150  * So, the summary is:
151  *
152  * + A HI16 entry must occur before any LO16 entries;
153  * + Multiple consecutive HI16 RELA entries need to be buffered until the
154  *   first LO16 RELA entry occurs - and then all HI16 RELA relocations use
155  *   the offset in the LOW16 RELA for calculating their offsets;
156  * + The last HI16 RELA entry before a LO16 RELA entry is used (the AHL)
157  *   for the first subsequent LO16 calculation;
158  * + If multiple consecutive LO16 RELA entries occur, only the first
159  *   LO16 RELA entry triggers an update of buffered HI16 RELA entries;
160  *   any subsequent LO16 RELA entry before another HI16 RELA entry will
161  *   not cause any further updates to the HI16 RELA entries.
162  *
163  * Additionally, flush out any outstanding HI16 entries that don't have
164  * a LO16 entry in case some garbage entries are left in the file.
165  */
166
167 struct mips_tmp_reloc;
168 struct mips_tmp_reloc {
169         struct mips_tmp_reloc *next;
170
171         Elf_Addr ahl;
172         Elf32_Addr *where_hi16;
173 };
174
175 static struct mips_tmp_reloc *ml = NULL;
176
177 /*
178  * Add a temporary relocation (ie, a HI16 reloc type.)
179  */
180 static int
181 mips_tmp_reloc_add(Elf_Addr ahl, Elf32_Addr *where_hi16)
182 {
183         struct mips_tmp_reloc *r;
184
185         r = malloc(sizeof(struct mips_tmp_reloc), M_TEMP, M_NOWAIT);
186         if (r == NULL) {
187                 printf("%s: failed to malloc\n", __func__);
188                 return (0);
189         }
190
191         r->ahl = ahl;
192         r->where_hi16 = where_hi16;
193         r->next = ml;
194         ml = r;
195
196         return (1);
197 }
198
199 /*
200  * Flush the temporary relocation list.
201  *
202  * This should be done after a file is completely loaded
203  * so no stale relocations exist to confuse the next
204  * load.
205  */
206 static void
207 mips_tmp_reloc_flush(void)
208 {
209         struct mips_tmp_reloc *r, *rn;
210
211         r = ml;
212         ml = NULL;
213         while (r != NULL) {
214                 rn = r->next;
215                 free(r, M_TEMP);
216                 r = rn;
217         }
218 }
219
220 /*
221  * Get an entry from the reloc list; or NULL if we've run out.
222  */
223 static struct mips_tmp_reloc *
224 mips_tmp_reloc_get(void)
225 {
226         struct mips_tmp_reloc *r;
227
228         r = ml;
229         if (r == NULL)
230                 return (NULL);
231         ml = ml->next;
232         return (r);
233 }
234
235 /*
236  * Free a relocation entry.
237  */
238 static void
239 mips_tmp_reloc_free(struct mips_tmp_reloc *r)
240 {
241
242         free(r, M_TEMP);
243 }
244
245 bool
246 elf_is_ifunc_reloc(Elf_Size r_info __unused)
247 {
248
249         return (false);
250 }
251
252 /* Process one elf relocation with addend. */
253 static int
254 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
255     int type, int local, elf_lookup_fn lookup)
256 {
257         Elf32_Addr *where = (Elf32_Addr *)NULL;
258         Elf_Addr addr;
259         Elf_Addr addend = (Elf_Addr)0;
260         Elf_Word rtype = (Elf_Word)0, symidx;
261         struct mips_tmp_reloc *r;
262         const Elf_Rel *rel = NULL;
263         const Elf_Rela *rela = NULL;
264         int error;
265
266         /* Store the last seen ahl from a HI16 for LO16 processing */
267         static Elf_Addr last_ahl;
268
269         switch (type) {
270         case ELF_RELOC_REL:
271                 rel = (const Elf_Rel *)data;
272                 where = (Elf32_Addr *) (relocbase + rel->r_offset);
273                 rtype = ELF_R_TYPE(rel->r_info);
274                 symidx = ELF_R_SYM(rel->r_info);
275                 switch (rtype) {
276                 case R_MIPS_64:
277                         addend = *(Elf64_Addr *)where;
278                         break;
279                 default:
280                         addend = *where;
281                         break;
282                 }
283
284                 break;
285         case ELF_RELOC_RELA:
286                 rela = (const Elf_Rela *)data;
287                 where = (Elf32_Addr *) (relocbase + rela->r_offset);
288                 addend = rela->r_addend;
289                 rtype = ELF_R_TYPE(rela->r_info);
290                 symidx = ELF_R_SYM(rela->r_info);
291                 break;
292         default:
293                 panic("unknown reloc type %d\n", type);
294         }
295
296         switch (rtype) {
297         case R_MIPS_NONE:       /* none */
298                 break;
299
300         case R_MIPS_32:         /* S + A */
301                 error = lookup(lf, symidx, 1, &addr);
302                 if (error != 0)
303                         return (-1);
304                 addr += addend;
305                 if (*where != addr)
306                         *where = (Elf32_Addr)addr;
307                 break;
308
309         case R_MIPS_26:         /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
310                 error = lookup(lf, symidx, 1, &addr);
311                 if (error != 0)
312                         return (-1);
313
314                 addend &= 0x03ffffff;
315                 /*
316                  * Addendum for .rela R_MIPS_26 is not shifted right
317                  */
318                 if (rela == NULL)
319                         addend <<= 2;
320
321                 addr += ((Elf_Addr)where & 0xf0000000) | addend;
322                 addr >>= 2;
323
324                 *where &= ~0x03ffffff;
325                 *where |= addr & 0x03ffffff;
326                 break;
327
328         case R_MIPS_64:         /* S + A */
329                 error = lookup(lf, symidx, 1, &addr);
330                 if (error != 0)
331                         return (-1);
332                 addr += addend;
333                 if (*(Elf64_Addr*)where != addr)
334                         *(Elf64_Addr*)where = addr;
335                 break;
336
337         /*
338          * Handle the two GNU extension cases:
339          *
340          * + Multiple HI16s followed by a LO16, and
341          * + A HI16 followed by multiple LO16s.
342          *
343          * The former is tricky - the HI16 relocations need
344          * to be buffered until a LO16 occurs, at which point
345          * each HI16 is replayed against the LO16 relocation entry
346          * (with the relevant overflow information.)
347          *
348          * The latter should be easy to handle - when the
349          * first LO16 is seen, write out and flush the
350          * HI16 buffer.  Any subsequent LO16 entries will
351          * find a blank relocation buffer.
352          *
353          */
354
355         case R_MIPS_HI16:       /* ((AHL + S) - ((short)(AHL + S)) >> 16 */
356                 if (rela != NULL) {
357                         error = lookup(lf, symidx, 1, &addr);
358                         if (error != 0)
359                                 return (-1);
360                         addr += addend;
361                         *where &= 0xffff0000;
362                         *where |= ((((long long) addr + 0x8000LL) >> 16) & 0xffff);
363                 } else {
364                         /*
365                          * Add a temporary relocation to the list;
366                          * will pop it off / free the list when
367                          * we've found a suitable HI16.
368                          */
369                         if (mips_tmp_reloc_add(addend << 16, where) == 0)
370                                 return (-1);
371                         /*
372                          * Track the last seen HI16 AHL for use by
373                          * the first LO16 AHL calculation.
374                          *
375                          * The assumption is any intermediary deleted
376                          * LO16's were optimised out, so the last
377                          * HI16 before the LO16 is the "true" relocation
378                          * entry to use for that LO16 write.
379                          */
380                         last_ahl = addend << 16;
381                 }
382                 break;
383
384         case R_MIPS_LO16:       /* AHL + S */
385                 if (rela != NULL) {
386                         error = lookup(lf, symidx, 1, &addr);
387                         if (error != 0)
388                                 return (-1);
389                         addr += addend;
390                         *where &= 0xffff0000;
391                         *where |= addr & 0xffff;
392                 } else {
393                         Elf_Addr tmp_ahl;
394                         Elf_Addr tmp_addend;
395
396                         tmp_ahl = last_ahl + (int16_t) addend;
397                         error = lookup(lf, symidx, 1, &addr);
398                         if (error != 0)
399                                 return (-1);
400
401                         tmp_addend = addend & 0xffff0000;
402
403                         /* Use the last seen ahl for calculating addend */
404                         tmp_addend |= (uint16_t)(tmp_ahl + addr);
405                         *where = tmp_addend;
406
407                         /*
408                          * This logic implements the "we saw multiple HI16
409                          * before a LO16" assignment /and/ "we saw multiple
410                          * LO16s".
411                          *
412                          * Multiple LO16s will be handled as a blank
413                          * relocation list.
414                          *
415                          * Multple HI16's are iterated over here.
416                          */
417                         while ((r = mips_tmp_reloc_get()) != NULL) {
418                                 Elf_Addr rahl;
419
420                                 /*
421                                  * We have the ahl from the HI16 entry, so
422                                  * offset it by the 16 bits of the low ahl.
423                                  */
424                                 rahl = r->ahl;
425                                 rahl += (int16_t) addend;
426
427                                 tmp_addend = *(r->where_hi16);
428                                 tmp_addend &= 0xffff0000;
429                                 tmp_addend |= ((rahl + addr) -
430                                     (int16_t)(rahl + addr)) >> 16;
431                                 *(r->where_hi16) = tmp_addend;
432                                 mips_tmp_reloc_free(r);
433                         }
434                 }
435
436                 break;
437
438         case R_MIPS_HIGHER:     /* %higher(A+S) */
439                 error = lookup(lf, symidx, 1, &addr);
440                 if (error != 0)
441                         return (-1);
442                 addr += addend;
443                 *where &= 0xffff0000;
444                 *where |= (((long long)addr + 0x80008000LL) >> 32) & 0xffff;
445                 break;
446
447         case R_MIPS_HIGHEST:    /* %highest(A+S) */
448                 error = lookup(lf, symidx, 1, &addr);
449                 if (error != 0)
450                         return (-1);
451                 addr += addend;
452                 *where &= 0xffff0000;
453                 *where |= (((long long)addr + 0x800080008000LL) >> 48) & 0xffff;
454                 break;
455
456         default:
457                 printf("kldload: unexpected relocation type %d\n",
458                         rtype);
459                 return (-1);
460         }
461
462         return(0);
463 }
464
465 int
466 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
467     elf_lookup_fn lookup)
468 {
469
470         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
471 }
472
473 int
474 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
475     int type, elf_lookup_fn lookup)
476 {
477
478         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
479 }
480
481 int
482 elf_cpu_load_file(linker_file_t lf __unused)
483 {
484
485         /*
486          * Sync the I and D caches to make sure our relocations are visible.
487          */
488         mips_icache_sync_all();
489
490         /* Flush outstanding relocations */
491         mips_tmp_reloc_flush();
492
493         return (0);
494 }
495
496 int
497 elf_cpu_unload_file(linker_file_t lf __unused)
498 {
499
500         return (0);
501 }
502
503 int
504 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
505 {
506
507         return (0);
508 }