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

Cache object: 222a0828a98eab532501ddc20729ad74


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