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

Cache object: 675273995f2f5e963517e36571027252


[ 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.