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