The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/mips/mips/elf_machdep.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/11.2/sys/mips/mips/elf_machdep.c 331722 2018-03-29 02:50:57Z eadler $");
   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         .flags          = 0
   96 };
   97 
   98 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY,
   99     (sysinit_cfunc_t) elf64_insert_brand_entry,
  100     &freebsd_brand_info);
  101 
  102 void
  103 elf64_dump_thread(struct thread *td __unused, void *dst __unused,
  104     size_t *off __unused)
  105 {
  106 }
  107 #else
  108 struct sysentvec elf32_freebsd_sysvec = {
  109         .sv_size        = SYS_MAXSYSCALL,
  110         .sv_table       = sysent,
  111         .sv_mask        = 0,
  112         .sv_errsize     = 0,
  113         .sv_errtbl      = NULL,
  114         .sv_transtrap   = NULL,
  115         .sv_fixup       = __elfN(freebsd_fixup),
  116         .sv_sendsig     = sendsig,
  117         .sv_sigcode     = sigcode,
  118         .sv_szsigcode   = &szsigcode,
  119         .sv_name        = "FreeBSD ELF32",
  120         .sv_coredump    = __elfN(coredump),
  121         .sv_imgact_try  = NULL,
  122         .sv_minsigstksz = MINSIGSTKSZ,
  123         .sv_pagesize    = PAGE_SIZE,
  124         .sv_minuser     = VM_MIN_ADDRESS,
  125         .sv_maxuser     = VM_MAXUSER_ADDRESS,
  126         .sv_usrstack    = USRSTACK,
  127         .sv_psstrings   = PS_STRINGS,
  128         .sv_stackprot   = VM_PROT_ALL,
  129         .sv_copyout_strings = exec_copyout_strings,
  130         .sv_setregs     = exec_setregs,
  131         .sv_fixlimit    = NULL,
  132         .sv_maxssiz     = NULL,
  133         .sv_flags       = SV_ABI_FREEBSD | SV_ILP32,
  134         .sv_set_syscall_retval = cpu_set_syscall_retval,
  135         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
  136         .sv_syscallnames = syscallnames,
  137         .sv_schedtail   = NULL,
  138         .sv_thread_detach = NULL,
  139         .sv_trap        = NULL,
  140 };
  141 
  142 static Elf32_Brandinfo freebsd_brand_info = {
  143         .brand          = ELFOSABI_FREEBSD,
  144         .machine        = EM_MIPS,
  145         .compat_3_brand = "FreeBSD",
  146         .emul_path      = NULL,
  147         .interp_path    = "/libexec/ld-elf.so.1",
  148         .sysvec         = &elf32_freebsd_sysvec,
  149         .interp_newpath = NULL,
  150         .flags          = 0
  151 };
  152 
  153 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
  154     (sysinit_cfunc_t) elf32_insert_brand_entry,
  155     &freebsd_brand_info);
  156 
  157 void
  158 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
  159     size_t *off __unused)
  160 {
  161 }
  162 #endif
  163 
  164 /* Process one elf relocation with addend. */
  165 static int
  166 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
  167     int type, int local, elf_lookup_fn lookup)
  168 {
  169         Elf32_Addr *where = (Elf32_Addr *)NULL;
  170         Elf_Addr addr;
  171         Elf_Addr addend = (Elf_Addr)0;
  172         Elf_Word rtype = (Elf_Word)0, symidx;
  173         const Elf_Rel *rel = NULL;
  174         const Elf_Rela *rela = NULL;
  175         int error;
  176 
  177         /*
  178          * Stash R_MIPS_HI16 info so we can use it when processing R_MIPS_LO16
  179          */
  180         static Elf_Addr ahl;
  181         static Elf32_Addr *where_hi16;
  182 
  183         switch (type) {
  184         case ELF_RELOC_REL:
  185                 rel = (const Elf_Rel *)data;
  186                 where = (Elf32_Addr *) (relocbase + rel->r_offset);
  187                 rtype = ELF_R_TYPE(rel->r_info);
  188                 symidx = ELF_R_SYM(rel->r_info);
  189                 switch (rtype) {
  190                 case R_MIPS_64:
  191                         addend = *(Elf64_Addr *)where;
  192                         break;
  193                 default:
  194                         addend = *where;
  195                         break;
  196                 }
  197 
  198                 break;
  199         case ELF_RELOC_RELA:
  200                 rela = (const Elf_Rela *)data;
  201                 where = (Elf32_Addr *) (relocbase + rela->r_offset);
  202                 addend = rela->r_addend;
  203                 rtype = ELF_R_TYPE(rela->r_info);
  204                 symidx = ELF_R_SYM(rela->r_info);
  205                 break;
  206         default:
  207                 panic("unknown reloc type %d\n", type);
  208         }
  209 
  210         switch (rtype) {
  211         case R_MIPS_NONE:       /* none */
  212                 break;
  213 
  214         case R_MIPS_32:         /* S + A */
  215                 error = lookup(lf, symidx, 1, &addr);
  216                 if (error != 0)
  217                         return (-1);
  218                 addr += addend;
  219                 if (*where != addr)
  220                         *where = (Elf32_Addr)addr;
  221                 break;
  222 
  223         case R_MIPS_26:         /* ((A << 2) | (P & 0xf0000000) + S) >> 2 */
  224                 error = lookup(lf, symidx, 1, &addr);
  225                 if (error != 0)
  226                         return (-1);
  227 
  228                 addend &= 0x03ffffff;
  229                 /*
  230                  * Addendum for .rela R_MIPS_26 is not shifted right
  231                  */
  232                 if (rela == NULL)
  233                         addend <<= 2;
  234 
  235                 addr += ((Elf_Addr)where & 0xf0000000) | addend;
  236                 addr >>= 2;
  237 
  238                 *where &= ~0x03ffffff;
  239                 *where |= addr & 0x03ffffff;
  240                 break;
  241 
  242         case R_MIPS_64:         /* S + A */
  243                 error = lookup(lf, symidx, 1, &addr);
  244                 if (error != 0)
  245                         return (-1);
  246                 addr += addend;
  247                 if (*(Elf64_Addr*)where != addr)
  248                         *(Elf64_Addr*)where = addr;
  249                 break;
  250 
  251         case R_MIPS_HI16:       /* ((AHL + S) - ((short)(AHL + S)) >> 16 */
  252                 if (rela != NULL) {
  253                         error = lookup(lf, symidx, 1, &addr);
  254                         if (error != 0)
  255                                 return (-1);
  256                         addr += addend;
  257                         *where &= 0xffff0000;
  258                         *where |= ((((long long) addr + 0x8000LL) >> 16) & 0xffff);
  259                 }
  260                 else {
  261                         ahl = addend << 16;
  262                         where_hi16 = where;
  263                 }
  264                 break;
  265 
  266         case R_MIPS_LO16:       /* AHL + S */
  267                 if (rela != NULL) {
  268                         error = lookup(lf, symidx, 1, &addr);
  269                         if (error != 0)
  270                                 return (-1);
  271                         addr += addend;
  272                         *where &= 0xffff0000;
  273                         *where |= addr & 0xffff;
  274                 }
  275                 else {
  276                         ahl += (int16_t)addend;
  277                         error = lookup(lf, symidx, 1, &addr);
  278                         if (error != 0)
  279                                 return (-1);
  280 
  281                         addend &= 0xffff0000;
  282                         addend |= (uint16_t)(ahl + addr);
  283                         *where = addend;
  284 
  285                         addend = *where_hi16;
  286                         addend &= 0xffff0000;
  287                         addend |= ((ahl + addr) - (int16_t)(ahl + addr)) >> 16;
  288                         *where_hi16 = addend;
  289                 }
  290 
  291                 break;
  292 
  293         case R_MIPS_HIGHER:     /* %higher(A+S) */
  294                 error = lookup(lf, symidx, 1, &addr);
  295                 if (error != 0)
  296                         return (-1);
  297                 addr += addend;
  298                 *where &= 0xffff0000;
  299                 *where |= (((long long)addr + 0x80008000LL) >> 32) & 0xffff;
  300                 break;
  301 
  302         case R_MIPS_HIGHEST:    /* %highest(A+S) */
  303                 error = lookup(lf, symidx, 1, &addr);
  304                 if (error != 0)
  305                         return (-1);
  306                 addr += addend;
  307                 *where &= 0xffff0000;
  308                 *where |= (((long long)addr + 0x800080008000LL) >> 48) & 0xffff;
  309                 break;
  310 
  311         default:
  312                 printf("kldload: unexpected relocation type %d\n",
  313                         rtype);
  314                 return (-1);
  315         }
  316 
  317         return(0);
  318 }
  319 
  320 int
  321 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
  322     elf_lookup_fn lookup)
  323 {
  324 
  325         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
  326 }
  327 
  328 int
  329 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
  330     int type, elf_lookup_fn lookup)
  331 {
  332 
  333         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
  334 }
  335 
  336 int
  337 elf_cpu_load_file(linker_file_t lf __unused)
  338 {
  339 
  340         /*
  341          * Sync the I and D caches to make sure our relocations are visible.
  342          */
  343         mips_icache_sync_all();
  344 
  345         return (0);
  346 }
  347 
  348 int
  349 elf_cpu_unload_file(linker_file_t lf __unused)
  350 {
  351 
  352         return (0);
  353 }

Cache object: 54225394d3e6a058da9eb290a80ea3ae


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.