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/powerpc/powerpc/elf64_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  * $FreeBSD$
   26  */
   27 
   28 #include <sys/param.h>
   29 #include <sys/kernel.h>
   30 #include <sys/systm.h>
   31 #include <sys/exec.h>
   32 #include <sys/imgact.h>
   33 #include <sys/malloc.h>
   34 #include <sys/proc.h>
   35 #include <sys/namei.h>
   36 #include <sys/fcntl.h>
   37 #include <sys/sysent.h>
   38 #include <sys/imgact_elf.h>
   39 #include <sys/syscall.h>
   40 #include <sys/signalvar.h>
   41 #include <sys/vnode.h>
   42 #include <sys/linker.h>
   43 
   44 #include <vm/vm.h>
   45 #include <vm/vm_param.h>
   46 
   47 #include <machine/altivec.h>
   48 #include <machine/cpu.h>
   49 #include <machine/elf.h>
   50 #include <machine/md_var.h>
   51 
   52 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
   53     u_long stack);
   54 
   55 struct sysentvec elf64_freebsd_sysvec_v1 = {
   56         .sv_size        = SYS_MAXSYSCALL,
   57         .sv_table       = sysent,
   58         .sv_mask        = 0,
   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     = sigcode64,
   65         .sv_szsigcode   = &szsigcode64,
   66         .sv_name        = "FreeBSD ELF64",
   67         .sv_coredump    = __elfN(coredump),
   68         .sv_imgact_try  = NULL,
   69         .sv_minsigstksz = MINSIGSTKSZ,
   70         .sv_pagesize    = PAGE_SIZE,
   71         .sv_minuser     = VM_MIN_ADDRESS,
   72         .sv_maxuser     = VM_MAXUSER_ADDRESS,
   73         .sv_usrstack    = USRSTACK,
   74         .sv_psstrings   = PS_STRINGS,
   75         .sv_stackprot   = VM_PROT_ALL,
   76         .sv_copyout_strings = exec_copyout_strings,
   77         .sv_setregs     = exec_setregs_funcdesc,
   78         .sv_fixlimit    = NULL,
   79         .sv_maxssiz     = NULL,
   80         .sv_flags       = SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
   81         .sv_set_syscall_retval = cpu_set_syscall_retval,
   82         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
   83         .sv_syscallnames = syscallnames,
   84         .sv_shared_page_base = SHAREDPAGE,
   85         .sv_shared_page_len = PAGE_SIZE,
   86         .sv_schedtail   = NULL,
   87         .sv_thread_detach = NULL,
   88         .sv_trap        = NULL,
   89 };
   90 INIT_SYSENTVEC(elf64_sysvec_v1, &elf64_freebsd_sysvec_v1);
   91 
   92 struct sysentvec elf64_freebsd_sysvec_v2 = {
   93         .sv_size        = SYS_MAXSYSCALL,
   94         .sv_table       = sysent,
   95         .sv_mask        = 0,
   96         .sv_errsize     = 0,
   97         .sv_errtbl      = NULL,
   98         .sv_transtrap   = NULL,
   99         .sv_fixup       = __elfN(freebsd_fixup),
  100         .sv_sendsig     = sendsig,
  101         .sv_sigcode     = sigcode64_elfv2,
  102         .sv_szsigcode   = &szsigcode64_elfv2,
  103         .sv_name        = "FreeBSD ELF64 V2",
  104         .sv_coredump    = __elfN(coredump),
  105         .sv_imgact_try  = NULL,
  106         .sv_minsigstksz = MINSIGSTKSZ,
  107         .sv_pagesize    = PAGE_SIZE,
  108         .sv_minuser     = VM_MIN_ADDRESS,
  109         .sv_maxuser     = VM_MAXUSER_ADDRESS,
  110         .sv_usrstack    = USRSTACK,
  111         .sv_psstrings   = PS_STRINGS,
  112         .sv_stackprot   = VM_PROT_ALL,
  113         .sv_copyout_strings = exec_copyout_strings,
  114         .sv_setregs     = exec_setregs,
  115         .sv_fixlimit    = NULL,
  116         .sv_maxssiz     = NULL,
  117         .sv_flags       = SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
  118         .sv_set_syscall_retval = cpu_set_syscall_retval,
  119         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
  120         .sv_syscallnames = syscallnames,
  121         .sv_shared_page_base = SHAREDPAGE,
  122         .sv_shared_page_len = PAGE_SIZE,
  123         .sv_schedtail   = NULL,
  124         .sv_thread_detach = NULL,
  125 };
  126 INIT_SYSENTVEC(elf64_sysvec_v2, &elf64_freebsd_sysvec_v2);
  127 
  128 static boolean_t ppc64_elfv1_header_match(struct image_params *params);
  129 static boolean_t ppc64_elfv2_header_match(struct image_params *params);
  130 
  131 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
  132         .brand          = ELFOSABI_FREEBSD,
  133         .machine        = EM_PPC64,
  134         .compat_3_brand = "FreeBSD",
  135         .emul_path      = NULL,
  136         .interp_path    = "/libexec/ld-elf.so.1",
  137         .sysvec         = &elf64_freebsd_sysvec_v1,
  138         .interp_newpath = NULL,
  139         .brand_note     = &elf64_freebsd_brandnote,
  140         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
  141         .header_supported = &ppc64_elfv1_header_match
  142 };
  143 
  144 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
  145     (sysinit_cfunc_t) elf64_insert_brand_entry,
  146     &freebsd_brand_info_elfv1);
  147 
  148 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
  149         .brand          = ELFOSABI_FREEBSD,
  150         .machine        = EM_PPC64,
  151         .compat_3_brand = "FreeBSD",
  152         .emul_path      = NULL,
  153         .interp_path    = "/libexec/ld-elf.so.1",
  154         .sysvec         = &elf64_freebsd_sysvec_v2,
  155         .interp_newpath = NULL,
  156         .brand_note     = &elf64_freebsd_brandnote,
  157         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
  158         .header_supported = &ppc64_elfv2_header_match
  159 };
  160 
  161 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
  162     (sysinit_cfunc_t) elf64_insert_brand_entry,
  163     &freebsd_brand_info_elfv2);
  164 
  165 static Elf64_Brandinfo freebsd_brand_oinfo = {
  166         .brand          = ELFOSABI_FREEBSD,
  167         .machine        = EM_PPC64,
  168         .compat_3_brand = "FreeBSD",
  169         .emul_path      = NULL,
  170         .interp_path    = "/usr/libexec/ld-elf.so.1",
  171         .sysvec         = &elf64_freebsd_sysvec_v1,
  172         .interp_newpath = NULL,
  173         .brand_note     = &elf64_freebsd_brandnote,
  174         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
  175         .header_supported = &ppc64_elfv1_header_match
  176 };
  177 
  178 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
  179         (sysinit_cfunc_t) elf64_insert_brand_entry,
  180         &freebsd_brand_oinfo);
  181 
  182 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
  183 
  184 static boolean_t
  185 ppc64_elfv1_header_match(struct image_params *params)
  186 {
  187         const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
  188         int abi = (hdr->e_flags & 3);
  189 
  190         return (abi == 0 || abi == 1);
  191 }
  192 
  193 static boolean_t
  194 ppc64_elfv2_header_match(struct image_params *params)
  195 {
  196         const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
  197         int abi = (hdr->e_flags & 3);
  198 
  199         return (abi == 2);
  200 }
  201 
  202 static void  
  203 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
  204     u_long stack)
  205 {
  206         struct trapframe *tf;
  207         register_t entry_desc[3];
  208 
  209         tf = trapframe(td);
  210         exec_setregs(td, imgp, stack);
  211 
  212         /*
  213          * For 64-bit ELFv1, we need to disentangle the function
  214          * descriptor
  215          *
  216          * 0. entry point
  217          * 1. TOC value (r2)
  218          * 2. Environment pointer (r11)
  219          */
  220 
  221         (void)copyin((void *)imgp->entry_addr, entry_desc,
  222             sizeof(entry_desc));
  223         tf->srr0 = entry_desc[0] + imgp->reloc_base;
  224         tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
  225         tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
  226 }
  227 
  228 void
  229 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
  230 {
  231         size_t len;
  232         struct pcb *pcb;
  233 
  234         len = 0;
  235         pcb = td->td_pcb;
  236         if (pcb->pcb_flags & PCB_VEC) {
  237                 save_vec_nodrop(td);
  238                 if (dst != NULL) {
  239                         len += elf64_populate_note(NT_PPC_VMX,
  240                             &pcb->pcb_vec, dst,
  241                             sizeof(pcb->pcb_vec), NULL);
  242                 } else
  243                         len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
  244                             sizeof(pcb->pcb_vec), NULL);
  245         }
  246         *off = len;
  247 }
  248 
  249 bool
  250 elf_is_ifunc_reloc(Elf_Size r_info __unused)
  251 {
  252 
  253         return (false);
  254 }
  255 
  256 /* Process one elf relocation with addend. */
  257 static int
  258 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
  259     int type, int local, elf_lookup_fn lookup)
  260 {
  261         Elf_Addr *where;
  262         Elf_Addr addr;
  263         Elf_Addr addend;
  264         Elf_Word rtype, symidx;
  265         const Elf_Rela *rela;
  266         int error;
  267 
  268         switch (type) {
  269         case ELF_RELOC_REL:
  270                 panic("PPC only supports RELA relocations");
  271                 break;
  272         case ELF_RELOC_RELA:
  273                 rela = (const Elf_Rela *)data;
  274                 where = (Elf_Addr *) (relocbase + rela->r_offset);
  275                 addend = rela->r_addend;
  276                 rtype = ELF_R_TYPE(rela->r_info);
  277                 symidx = ELF_R_SYM(rela->r_info);
  278                 break;
  279         default:
  280                 panic("elf_reloc: unknown relocation mode %d\n", type);
  281         }
  282 
  283         switch (rtype) {
  284 
  285         case R_PPC_NONE:
  286                 break;
  287 
  288         case R_PPC64_ADDR64:    /* doubleword64 S + A */
  289                 error = lookup(lf, symidx, 1, &addr);
  290                 if (error != 0)
  291                         return -1;
  292                 addr += addend;
  293                 *where = addr;
  294                 break;
  295 
  296         case R_PPC_RELATIVE:    /* doubleword64 B + A */
  297                 *where = elf_relocaddr(lf, relocbase + addend);
  298                 break;
  299 
  300         case R_PPC_JMP_SLOT:    /* function descriptor copy */
  301                 lookup(lf, symidx, 1, &addr);
  302 #if !defined(_CALL_ELF) || _CALL_ELF == 1
  303                 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
  304 #else
  305                 memcpy(where, (Elf_Addr *)addr, sizeof(Elf_Addr));
  306 #endif
  307                 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
  308                 break;
  309 
  310         default:
  311                 printf("kldload: unexpected relocation type %d\n",
  312                     (int) rtype);
  313                 return -1;
  314         }
  315         return(0);
  316 }
  317 
  318 void
  319 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
  320 {
  321         Elf_Rela *rela = NULL, *relalim;
  322         Elf_Addr relasz = 0;
  323         Elf_Addr *where;
  324 
  325         /*
  326          * Extract the rela/relasz values from the dynamic section
  327          */
  328         for (; dynp->d_tag != DT_NULL; dynp++) {
  329                 switch (dynp->d_tag) {
  330                 case DT_RELA:
  331                         rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
  332                         break;
  333                 case DT_RELASZ:
  334                         relasz = dynp->d_un.d_val;
  335                         break;
  336                 }
  337         }
  338 
  339         /*
  340          * Relocate these values
  341          */
  342         relalim = (Elf_Rela *)((caddr_t)rela + relasz);
  343         for (; rela < relalim; rela++) {
  344                 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
  345                         continue;
  346                 where = (Elf_Addr *)(relocbase + rela->r_offset);
  347                 *where = (Elf_Addr)(relocbase + rela->r_addend);
  348         }
  349 }
  350 
  351 int
  352 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
  353     elf_lookup_fn lookup)
  354 {
  355 
  356         return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
  357 }
  358 
  359 int
  360 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
  361     int type, elf_lookup_fn lookup)
  362 {
  363 
  364         return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
  365 }
  366 
  367 int
  368 elf_cpu_load_file(linker_file_t lf)
  369 {
  370         /* Only sync the cache for non-kernel modules */
  371         if (lf->id != 1)
  372                 __syncicache(lf->address, lf->size);
  373         return (0);
  374 }
  375 
  376 int
  377 elf_cpu_unload_file(linker_file_t lf __unused)
  378 {
  379 
  380         return (0);
  381 }

Cache object: 1c7bcad953a31753d3a151cc580b6916


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