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/i386/i386/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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright 1996-1998 John D. Polstra.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD$");
   30 
   31 #include "opt_cpu.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/kernel.h>
   35 #include <sys/systm.h>
   36 #include <sys/exec.h>
   37 #include <sys/imgact.h>
   38 #include <sys/linker.h>
   39 #include <sys/reg.h>
   40 #include <sys/proc.h>
   41 #include <sys/sysent.h>
   42 #include <sys/imgact_elf.h>
   43 #include <sys/syscall.h>
   44 #include <sys/signalvar.h>
   45 #include <sys/vnode.h>
   46 
   47 #include <vm/vm.h>
   48 #include <vm/pmap.h>
   49 #include <vm/vm_param.h>
   50 
   51 #include <machine/elf.h>
   52 #include <machine/md_var.h>
   53 #include <machine/npx.h>
   54 
   55 struct sysentvec elf32_freebsd_sysvec = {
   56         .sv_size        = SYS_MAXSYSCALL,
   57         .sv_table       = sysent,
   58         .sv_fixup       = __elfN(freebsd_fixup),
   59         .sv_sendsig     = sendsig,
   60         .sv_sigcode     = sigcode,
   61         .sv_szsigcode   = &szsigcode,
   62         .sv_name        = "FreeBSD ELF32",
   63         .sv_coredump    = __elfN(coredump),
   64         .sv_elf_core_osabi = ELFOSABI_FREEBSD,
   65         .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
   66         .sv_elf_core_prepare_notes = __elfN(prepare_notes),
   67         .sv_imgact_try  = NULL,
   68         .sv_minsigstksz = MINSIGSTKSZ,
   69         .sv_minuser     = VM_MIN_ADDRESS,
   70         .sv_maxuser     = VM_MAXUSER_ADDRESS,
   71         .sv_usrstack    = USRSTACK,
   72         .sv_psstrings   = PS_STRINGS,
   73         .sv_psstringssz = sizeof(struct ps_strings),
   74         .sv_stackprot   = VM_PROT_ALL,
   75         .sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
   76         .sv_copyout_strings     = exec_copyout_strings,
   77         .sv_setregs     = exec_setregs,
   78         .sv_fixlimit    = NULL,
   79         .sv_maxssiz     = NULL,
   80         .sv_flags       = SV_ABI_FREEBSD | SV_ASLR | SV_IA32 | SV_ILP32 |
   81                             SV_SHP | SV_TIMEKEEP | SV_RNG_SEED_VER,
   82         .sv_set_syscall_retval = cpu_set_syscall_retval,
   83         .sv_fetch_syscall_args = cpu_fetch_syscall_args,
   84         .sv_syscallnames = syscallnames,
   85         .sv_shared_page_base = SHAREDPAGE,
   86         .sv_shared_page_len = PAGE_SIZE,
   87         .sv_schedtail   = NULL,
   88         .sv_thread_detach = NULL,
   89         .sv_trap        = NULL,
   90         .sv_onexec_old  = exec_onexec_old,
   91         .sv_onexit      = exit_onexit,
   92         .sv_set_fork_retval = x86_set_fork_retval,
   93         .sv_regset_begin = SET_BEGIN(__elfN(regset)),
   94         .sv_regset_end  = SET_LIMIT(__elfN(regset)),
   95 };
   96 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
   97 
   98 static Elf32_Brandinfo freebsd_brand_info = {
   99         .brand          = ELFOSABI_FREEBSD,
  100         .machine        = EM_386,
  101         .compat_3_brand = "FreeBSD",
  102         .emul_path      = NULL,
  103         .interp_path    = "/libexec/ld-elf.so.1",
  104         .sysvec         = &elf32_freebsd_sysvec,
  105         .interp_newpath = NULL,
  106         .brand_note     = &elf32_freebsd_brandnote,
  107         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
  108 };
  109 
  110 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
  111         (sysinit_cfunc_t) elf32_insert_brand_entry,
  112         &freebsd_brand_info);
  113 
  114 static Elf32_Brandinfo freebsd_brand_oinfo = {
  115         .brand          = ELFOSABI_FREEBSD,
  116         .machine        = EM_386,
  117         .compat_3_brand = "FreeBSD",
  118         .emul_path      = NULL,
  119         .interp_path    = "/usr/libexec/ld-elf.so.1",
  120         .sysvec         = &elf32_freebsd_sysvec,
  121         .interp_newpath = NULL,
  122         .brand_note     = &elf32_freebsd_brandnote,
  123         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
  124 };
  125 
  126 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
  127         (sysinit_cfunc_t) elf32_insert_brand_entry,
  128         &freebsd_brand_oinfo);
  129 
  130 static Elf32_Brandinfo kfreebsd_brand_info = {
  131         .brand          = ELFOSABI_FREEBSD,
  132         .machine        = EM_386,
  133         .compat_3_brand = "FreeBSD",
  134         .emul_path      = NULL,
  135         .interp_path    = "/lib/ld.so.1",
  136         .sysvec         = &elf32_freebsd_sysvec,
  137         .interp_newpath = NULL,
  138         .brand_note     = &elf32_kfreebsd_brandnote,
  139         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
  140 };
  141 
  142 SYSINIT(kelf32, SI_SUB_EXEC, SI_ORDER_ANY,
  143         (sysinit_cfunc_t) elf32_insert_brand_entry,
  144         &kfreebsd_brand_info);
  145 
  146 void
  147 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
  148 {
  149         void *buf;
  150         size_t len;
  151 
  152         len = 0;
  153         if (use_xsave) {
  154                 if (dst != NULL) {
  155                         npxgetregs(td);
  156                         len += elf32_populate_note(NT_X86_XSTATE,
  157                             get_pcb_user_save_td(td), dst,
  158                             cpu_max_ext_state_size, &buf);
  159                         *(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
  160                             xsave_mask;
  161                 } else
  162                         len += elf32_populate_note(NT_X86_XSTATE, NULL, NULL,
  163                             cpu_max_ext_state_size, NULL);
  164         }
  165         *off = len;
  166 }
  167 
  168 bool
  169 elf_is_ifunc_reloc(Elf_Size r_info)
  170 {
  171 
  172         return (ELF_R_TYPE(r_info) == R_386_IRELATIVE);
  173 }
  174 
  175 #define ERI_LOCAL       0x0001
  176 
  177 /* Process one elf relocation with addend. */
  178 static int
  179 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
  180     int type, elf_lookup_fn lookup, int flags)
  181 {
  182         Elf_Addr *where;
  183         Elf_Addr addr;
  184         Elf_Addr addend;
  185         Elf_Word rtype, symidx;
  186         const Elf_Rel *rel;
  187         const Elf_Rela *rela;
  188         int error;
  189 
  190         switch (type) {
  191         case ELF_RELOC_REL:
  192                 rel = (const Elf_Rel *)data;
  193                 where = (Elf_Addr *) (relocbase + rel->r_offset);
  194                 addend = *where;
  195                 rtype = ELF_R_TYPE(rel->r_info);
  196                 symidx = ELF_R_SYM(rel->r_info);
  197                 break;
  198         case ELF_RELOC_RELA:
  199                 rela = (const Elf_Rela *)data;
  200                 where = (Elf_Addr *) (relocbase + rela->r_offset);
  201                 addend = rela->r_addend;
  202                 rtype = ELF_R_TYPE(rela->r_info);
  203                 symidx = ELF_R_SYM(rela->r_info);
  204                 break;
  205         default:
  206                 panic("unknown reloc type %d\n", type);
  207         }
  208 
  209         if ((flags & ERI_LOCAL) != 0) {
  210                 if (rtype == R_386_RELATIVE) {  /* A + B */
  211                         addr = elf_relocaddr(lf, relocbase + addend);
  212                         if (*where != addr)
  213                                 *where = addr;
  214                 }
  215                 return (0);
  216         }
  217 
  218         switch (rtype) {
  219                 case R_386_NONE:        /* none */
  220                         break;
  221 
  222                 case R_386_32:          /* S + A */
  223                         error = lookup(lf, symidx, 1, &addr);
  224                         if (error != 0)
  225                                 return (-1);
  226                         addr += addend;
  227                         if (*where != addr)
  228                                 *where = addr;
  229                         break;
  230 
  231                 case R_386_PC32:        /* S + A - P */
  232                         error = lookup(lf, symidx, 1, &addr);
  233                         if (error != 0)
  234                                 return (-1);
  235                         addr += addend - (Elf_Addr)where;
  236                         if (*where != addr)
  237                                 *where = addr;
  238                         break;
  239 
  240                 case R_386_COPY:        /* none */
  241                         /*
  242                          * There shouldn't be copy relocations in kernel
  243                          * objects.
  244                          */
  245                         printf("kldload: unexpected R_COPY relocation, "
  246                             "symbol index %d\n", symidx);
  247                         return (-1);
  248                         break;
  249 
  250                 case R_386_GLOB_DAT:    /* S */
  251                         error = lookup(lf, symidx, 1, &addr);
  252                         if (error != 0)
  253                                 return (-1);
  254                         if (*where != addr)
  255                                 *where = addr;
  256                         break;
  257 
  258                 case R_386_RELATIVE:
  259                         break;
  260 
  261                 case R_386_IRELATIVE:
  262                         addr = relocbase + addend;
  263                         addr = ((Elf_Addr (*)(void))addr)();
  264                         if (*where != addr)
  265                                 *where = addr;
  266                         break;
  267 
  268                 default:
  269                         printf("kldload: unexpected relocation type %d, "
  270                             "symbol index %d\n", rtype, symidx);
  271                         return (-1);
  272         }
  273         return(0);
  274 }
  275 
  276 int
  277 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
  278     elf_lookup_fn lookup)
  279 {
  280 
  281         return (elf_reloc_internal(lf, relocbase, data, type, lookup, 0));
  282 }
  283 
  284 int
  285 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
  286     int type, elf_lookup_fn lookup)
  287 {
  288 
  289         return (elf_reloc_internal(lf, relocbase, data, type, lookup,
  290             ERI_LOCAL));
  291 }
  292 
  293 int
  294 elf_cpu_load_file(linker_file_t lf __unused)
  295 {
  296 
  297         return (0);
  298 }
  299 
  300 int
  301 elf_cpu_unload_file(linker_file_t lf __unused)
  302 {
  303 
  304         return (0);
  305 }
  306 
  307 int
  308 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)
  309 {
  310 
  311         return (0);
  312 }

Cache object: 72cced58d71ed2a96dc927c6513c75b9


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