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/kern/link_elf_obj.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 (c) 1998-2000 Doug Rabson
    3  * Copyright (c) 2004 Peter Wemm
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: releng/11.2/sys/kern/link_elf_obj.c 333720 2018-05-17 15:24:53Z kib $");
   30 
   31 #include "opt_ddb.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/kernel.h>
   36 #include <sys/lock.h>
   37 #include <sys/malloc.h>
   38 #include <sys/mutex.h>
   39 #include <sys/mount.h>
   40 #include <sys/proc.h>
   41 #include <sys/namei.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/vnode.h>
   44 #include <sys/linker.h>
   45 
   46 #include <machine/elf.h>
   47 
   48 #include <net/vnet.h>
   49 
   50 #include <security/mac/mac_framework.h>
   51 
   52 #include <vm/vm.h>
   53 #include <vm/vm_param.h>
   54 #include <vm/vm_object.h>
   55 #include <vm/vm_kern.h>
   56 #include <vm/vm_extern.h>
   57 #include <vm/pmap.h>
   58 #include <vm/vm_map.h>
   59 
   60 #include <sys/link_elf.h>
   61 
   62 #ifdef DDB_CTF
   63 #include <sys/zlib.h>
   64 #endif
   65 
   66 #include "linker_if.h"
   67 
   68 typedef struct {
   69         void            *addr;
   70         Elf_Off         size;
   71         int             flags;
   72         int             sec;    /* Original section */
   73         char            *name;
   74 } Elf_progent;
   75 
   76 typedef struct {
   77         Elf_Rel         *rel;
   78         int             nrel;
   79         int             sec;
   80 } Elf_relent;
   81 
   82 typedef struct {
   83         Elf_Rela        *rela;
   84         int             nrela;
   85         int             sec;
   86 } Elf_relaent;
   87 
   88 
   89 typedef struct elf_file {
   90         struct linker_file lf;          /* Common fields */
   91 
   92         int             preloaded;
   93         caddr_t         address;        /* Relocation address */
   94         vm_object_t     object;         /* VM object to hold file pages */
   95         Elf_Shdr        *e_shdr;
   96 
   97         Elf_progent     *progtab;
   98         int             nprogtab;
   99 
  100         Elf_relaent     *relatab;
  101         int             nrelatab;
  102 
  103         Elf_relent      *reltab;
  104         int             nreltab;
  105 
  106         Elf_Sym         *ddbsymtab;     /* The symbol table we are using */
  107         long            ddbsymcnt;      /* Number of symbols */
  108         caddr_t         ddbstrtab;      /* String table */
  109         long            ddbstrcnt;      /* number of bytes in string table */
  110 
  111         caddr_t         shstrtab;       /* Section name string table */
  112         long            shstrcnt;       /* number of bytes in string table */
  113 
  114         caddr_t         ctftab;         /* CTF table */
  115         long            ctfcnt;         /* number of bytes in CTF table */
  116         caddr_t         ctfoff;         /* CTF offset table */
  117         caddr_t         typoff;         /* Type offset table */
  118         long            typlen;         /* Number of type entries. */
  119 
  120 } *elf_file_t;
  121 
  122 #include <kern/kern_ctf.c>
  123 
  124 static int      link_elf_link_preload(linker_class_t cls,
  125                     const char *, linker_file_t *);
  126 static int      link_elf_link_preload_finish(linker_file_t);
  127 static int      link_elf_load_file(linker_class_t, const char *, linker_file_t *);
  128 static int      link_elf_lookup_symbol(linker_file_t, const char *,
  129                     c_linker_sym_t *);
  130 static int      link_elf_symbol_values(linker_file_t, c_linker_sym_t,
  131                     linker_symval_t *);
  132 static int      link_elf_search_symbol(linker_file_t, caddr_t value,
  133                     c_linker_sym_t *sym, long *diffp);
  134 
  135 static void     link_elf_unload_file(linker_file_t);
  136 static int      link_elf_lookup_set(linker_file_t, const char *,
  137                     void ***, void ***, int *);
  138 static int      link_elf_each_function_name(linker_file_t,
  139                     int (*)(const char *, void *), void *);
  140 static int      link_elf_each_function_nameval(linker_file_t,
  141                                 linker_function_nameval_callback_t,
  142                                 void *);
  143 static int      link_elf_reloc_local(linker_file_t);
  144 static long     link_elf_symtab_get(linker_file_t, const Elf_Sym **);
  145 static long     link_elf_strtab_get(linker_file_t, caddr_t *);
  146 
  147 static int      elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps,
  148                     Elf_Addr *);
  149 
  150 static kobj_method_t link_elf_methods[] = {
  151         KOBJMETHOD(linker_lookup_symbol,        link_elf_lookup_symbol),
  152         KOBJMETHOD(linker_symbol_values,        link_elf_symbol_values),
  153         KOBJMETHOD(linker_search_symbol,        link_elf_search_symbol),
  154         KOBJMETHOD(linker_unload,               link_elf_unload_file),
  155         KOBJMETHOD(linker_load_file,            link_elf_load_file),
  156         KOBJMETHOD(linker_link_preload,         link_elf_link_preload),
  157         KOBJMETHOD(linker_link_preload_finish,  link_elf_link_preload_finish),
  158         KOBJMETHOD(linker_lookup_set,           link_elf_lookup_set),
  159         KOBJMETHOD(linker_each_function_name,   link_elf_each_function_name),
  160         KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
  161         KOBJMETHOD(linker_ctf_get,              link_elf_ctf_get),
  162         KOBJMETHOD(linker_symtab_get,           link_elf_symtab_get),
  163         KOBJMETHOD(linker_strtab_get,           link_elf_strtab_get),
  164         { 0, 0 }
  165 };
  166 
  167 static struct linker_class link_elf_class = {
  168 #if ELF_TARG_CLASS == ELFCLASS32
  169         "elf32_obj",
  170 #else
  171         "elf64_obj",
  172 #endif
  173         link_elf_methods, sizeof(struct elf_file)
  174 };
  175 
  176 static int      relocate_file(elf_file_t ef);
  177 static void     elf_obj_cleanup_globals_cache(elf_file_t);
  178 
  179 static void
  180 link_elf_error(const char *filename, const char *s)
  181 {
  182         if (filename == NULL)
  183                 printf("kldload: %s\n", s);
  184         else
  185                 printf("kldload: %s: %s\n", filename, s);
  186 }
  187 
  188 static void
  189 link_elf_init(void *arg)
  190 {
  191 
  192         linker_add_class(&link_elf_class);
  193 }
  194 
  195 SYSINIT(link_elf_obj, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
  196 
  197 static int
  198 link_elf_link_preload(linker_class_t cls, const char *filename,
  199     linker_file_t *result)
  200 {
  201         Elf_Ehdr *hdr;
  202         Elf_Shdr *shdr;
  203         Elf_Sym *es;
  204         void *modptr, *baseptr, *sizeptr;
  205         char *type;
  206         elf_file_t ef;
  207         linker_file_t lf;
  208         Elf_Addr off;
  209         int error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
  210 
  211         /* Look to see if we have the file preloaded */
  212         modptr = preload_search_by_name(filename);
  213         if (modptr == NULL)
  214                 return ENOENT;
  215 
  216         type = (char *)preload_search_info(modptr, MODINFO_TYPE);
  217         baseptr = preload_search_info(modptr, MODINFO_ADDR);
  218         sizeptr = preload_search_info(modptr, MODINFO_SIZE);
  219         hdr = (Elf_Ehdr *)preload_search_info(modptr, MODINFO_METADATA |
  220             MODINFOMD_ELFHDR);
  221         shdr = (Elf_Shdr *)preload_search_info(modptr, MODINFO_METADATA |
  222             MODINFOMD_SHDR);
  223         if (type == NULL || (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE)
  224             " obj module") != 0 &&
  225             strcmp(type, "elf obj module") != 0)) {
  226                 return (EFTYPE);
  227         }
  228         if (baseptr == NULL || sizeptr == NULL || hdr == NULL ||
  229             shdr == NULL)
  230                 return (EINVAL);
  231 
  232         lf = linker_make_file(filename, &link_elf_class);
  233         if (lf == NULL)
  234                 return (ENOMEM);
  235 
  236         ef = (elf_file_t)lf;
  237         ef->preloaded = 1;
  238         ef->address = *(caddr_t *)baseptr;
  239         lf->address = *(caddr_t *)baseptr;
  240         lf->size = *(size_t *)sizeptr;
  241 
  242         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
  243             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
  244             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
  245             hdr->e_version != EV_CURRENT ||
  246             hdr->e_type != ET_REL ||
  247             hdr->e_machine != ELF_TARG_MACH) {
  248                 error = EFTYPE;
  249                 goto out;
  250         }
  251         ef->e_shdr = shdr;
  252 
  253         /* Scan the section header for information and table sizing. */
  254         symtabindex = -1;
  255         symstrindex = -1;
  256         for (i = 0; i < hdr->e_shnum; i++) {
  257                 switch (shdr[i].sh_type) {
  258                 case SHT_PROGBITS:
  259                 case SHT_NOBITS:
  260 #ifdef __amd64__
  261                 case SHT_X86_64_UNWIND:
  262 #endif
  263                         /* Ignore sections not loaded by the loader. */
  264                         if (shdr[i].sh_addr == 0)
  265                                 break;
  266                         ef->nprogtab++;
  267                         break;
  268                 case SHT_SYMTAB:
  269                         symtabindex = i;
  270                         symstrindex = shdr[i].sh_link;
  271                         break;
  272                 case SHT_REL:
  273                         /*
  274                          * Ignore relocation tables for sections not
  275                          * loaded by the loader.
  276                          */
  277                         if (shdr[shdr[i].sh_info].sh_addr == 0)
  278                                 break;
  279                         ef->nreltab++;
  280                         break;
  281                 case SHT_RELA:
  282                         if (shdr[shdr[i].sh_info].sh_addr == 0)
  283                                 break;
  284                         ef->nrelatab++;
  285                         break;
  286                 }
  287         }
  288 
  289         shstrindex = hdr->e_shstrndx;
  290         if (ef->nprogtab == 0 || symstrindex < 0 ||
  291             symstrindex >= hdr->e_shnum ||
  292             shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
  293             shstrindex >= hdr->e_shnum ||
  294             shdr[shstrindex].sh_type != SHT_STRTAB) {
  295                 printf("%s: bad/missing section headers\n", filename);
  296                 error = ENOEXEC;
  297                 goto out;
  298         }
  299 
  300         /* Allocate space for tracking the load chunks */
  301         if (ef->nprogtab != 0)
  302                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
  303                     M_LINKER, M_WAITOK | M_ZERO);
  304         if (ef->nreltab != 0)
  305                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
  306                     M_LINKER, M_WAITOK | M_ZERO);
  307         if (ef->nrelatab != 0)
  308                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
  309                     M_LINKER, M_WAITOK | M_ZERO);
  310         if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
  311             (ef->nreltab != 0 && ef->reltab == NULL) ||
  312             (ef->nrelatab != 0 && ef->relatab == NULL)) {
  313                 error = ENOMEM;
  314                 goto out;
  315         }
  316 
  317         /* XXX, relocate the sh_addr fields saved by the loader. */
  318         off = 0;
  319         for (i = 0; i < hdr->e_shnum; i++) {
  320                 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
  321                         off = shdr[i].sh_addr;
  322         }
  323         for (i = 0; i < hdr->e_shnum; i++) {
  324                 if (shdr[i].sh_addr != 0)
  325                         shdr[i].sh_addr = shdr[i].sh_addr - off +
  326                             (Elf_Addr)ef->address;
  327         }
  328 
  329         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
  330         ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
  331         ef->ddbstrcnt = shdr[symstrindex].sh_size;
  332         ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
  333         ef->shstrcnt = shdr[shstrindex].sh_size;
  334         ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
  335 
  336         /* Now fill out progtab and the relocation tables. */
  337         pb = 0;
  338         rl = 0;
  339         ra = 0;
  340         for (i = 0; i < hdr->e_shnum; i++) {
  341                 switch (shdr[i].sh_type) {
  342                 case SHT_PROGBITS:
  343                 case SHT_NOBITS:
  344 #ifdef __amd64__
  345                 case SHT_X86_64_UNWIND:
  346 #endif
  347                         if (shdr[i].sh_addr == 0)
  348                                 break;
  349                         ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
  350                         if (shdr[i].sh_type == SHT_PROGBITS)
  351                                 ef->progtab[pb].name = "<<PROGBITS>>";
  352 #ifdef __amd64__
  353                         else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
  354                                 ef->progtab[pb].name = "<<UNWIND>>";
  355 #endif
  356                         else
  357                                 ef->progtab[pb].name = "<<NOBITS>>";
  358                         ef->progtab[pb].size = shdr[i].sh_size;
  359                         ef->progtab[pb].sec = i;
  360                         if (ef->shstrtab && shdr[i].sh_name != 0)
  361                                 ef->progtab[pb].name =
  362                                     ef->shstrtab + shdr[i].sh_name;
  363                         if (ef->progtab[pb].name != NULL && 
  364                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
  365                                 void *dpcpu;
  366 
  367                                 dpcpu = dpcpu_alloc(shdr[i].sh_size);
  368                                 if (dpcpu == NULL) {
  369                                         error = ENOSPC;
  370                                         goto out;
  371                                 }
  372                                 memcpy(dpcpu, ef->progtab[pb].addr,
  373                                     ef->progtab[pb].size);
  374                                 dpcpu_copy(dpcpu, shdr[i].sh_size);
  375                                 ef->progtab[pb].addr = dpcpu;
  376 #ifdef VIMAGE
  377                         } else if (ef->progtab[pb].name != NULL &&
  378                             !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
  379                                 void *vnet_data;
  380 
  381                                 vnet_data = vnet_data_alloc(shdr[i].sh_size);
  382                                 if (vnet_data == NULL) {
  383                                         error = ENOSPC;
  384                                         goto out;
  385                                 }
  386                                 memcpy(vnet_data, ef->progtab[pb].addr,
  387                                     ef->progtab[pb].size);
  388                                 vnet_data_copy(vnet_data, shdr[i].sh_size);
  389                                 ef->progtab[pb].addr = vnet_data;
  390 #endif
  391                         } else if (ef->progtab[pb].name != NULL &&
  392                             !strcmp(ef->progtab[pb].name, ".ctors")) {
  393                                 lf->ctors_addr = ef->progtab[pb].addr;
  394                                 lf->ctors_size = shdr[i].sh_size;
  395                         }
  396 
  397                         /* Update all symbol values with the offset. */
  398                         for (j = 0; j < ef->ddbsymcnt; j++) {
  399                                 es = &ef->ddbsymtab[j];
  400                                 if (es->st_shndx != i)
  401                                         continue;
  402                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
  403                         }
  404                         pb++;
  405                         break;
  406                 case SHT_REL:
  407                         if (shdr[shdr[i].sh_info].sh_addr == 0)
  408                                 break;
  409                         ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
  410                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
  411                         ef->reltab[rl].sec = shdr[i].sh_info;
  412                         rl++;
  413                         break;
  414                 case SHT_RELA:
  415                         if (shdr[shdr[i].sh_info].sh_addr == 0)
  416                                 break;
  417                         ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
  418                         ef->relatab[ra].nrela =
  419                             shdr[i].sh_size / sizeof(Elf_Rela);
  420                         ef->relatab[ra].sec = shdr[i].sh_info;
  421                         ra++;
  422                         break;
  423                 }
  424         }
  425         if (pb != ef->nprogtab) {
  426                 printf("%s: lost progbits\n", filename);
  427                 error = ENOEXEC;
  428                 goto out;
  429         }
  430         if (rl != ef->nreltab) {
  431                 printf("%s: lost reltab\n", filename);
  432                 error = ENOEXEC;
  433                 goto out;
  434         }
  435         if (ra != ef->nrelatab) {
  436                 printf("%s: lost relatab\n", filename);
  437                 error = ENOEXEC;
  438                 goto out;
  439         }
  440 
  441         /* Local intra-module relocations */
  442         error = link_elf_reloc_local(lf);
  443         if (error != 0)
  444                 goto out;
  445 
  446         *result = lf;
  447         return (0);
  448 
  449 out:
  450         /* preload not done this way */
  451         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
  452         return (error);
  453 }
  454 
  455 static void
  456 link_elf_invoke_ctors(caddr_t addr, size_t size)
  457 {
  458         void (**ctor)(void);
  459         size_t i, cnt;
  460 
  461         if (addr == NULL || size == 0)
  462                 return;
  463         cnt = size / sizeof(*ctor);
  464         ctor = (void *)addr;
  465         for (i = 0; i < cnt; i++) {
  466                 if (ctor[i] != NULL)
  467                         (*ctor[i])();
  468         }
  469 }
  470 
  471 static int
  472 link_elf_link_preload_finish(linker_file_t lf)
  473 {
  474         elf_file_t ef;
  475         int error;
  476 
  477         ef = (elf_file_t)lf;
  478         error = relocate_file(ef);
  479         if (error)
  480                 return error;
  481 
  482         /* Notify MD code that a module is being loaded. */
  483         error = elf_cpu_load_file(lf);
  484         if (error)
  485                 return (error);
  486 
  487         /* Invoke .ctors */
  488         link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
  489         return (0);
  490 }
  491 
  492 static int
  493 link_elf_load_file(linker_class_t cls, const char *filename,
  494     linker_file_t *result)
  495 {
  496         struct nameidata nd;
  497         struct thread *td = curthread;  /* XXX */
  498         Elf_Ehdr *hdr;
  499         Elf_Shdr *shdr;
  500         Elf_Sym *es;
  501         int nbytes, i, j;
  502         vm_offset_t mapbase;
  503         size_t mapsize;
  504         int error = 0;
  505         ssize_t resid;
  506         int flags;
  507         elf_file_t ef;
  508         linker_file_t lf;
  509         int symtabindex;
  510         int symstrindex;
  511         int shstrindex;
  512         int nsym;
  513         int pb, rl, ra;
  514         int alignmask;
  515 
  516         shdr = NULL;
  517         lf = NULL;
  518         mapsize = 0;
  519         hdr = NULL;
  520 
  521         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
  522         flags = FREAD;
  523         error = vn_open(&nd, &flags, 0, NULL);
  524         if (error)
  525                 return error;
  526         NDFREE(&nd, NDF_ONLY_PNBUF);
  527         if (nd.ni_vp->v_type != VREG) {
  528                 error = ENOEXEC;
  529                 goto out;
  530         }
  531 #ifdef MAC
  532         error = mac_kld_check_load(td->td_ucred, nd.ni_vp);
  533         if (error) {
  534                 goto out;
  535         }
  536 #endif
  537 
  538         /* Read the elf header from the file. */
  539         hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
  540         error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
  541             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  542             &resid, td);
  543         if (error)
  544                 goto out;
  545         if (resid != 0){
  546                 error = ENOEXEC;
  547                 goto out;
  548         }
  549 
  550         if (!IS_ELF(*hdr)) {
  551                 error = ENOEXEC;
  552                 goto out;
  553         }
  554 
  555         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
  556             || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
  557                 link_elf_error(filename, "Unsupported file layout");
  558                 error = ENOEXEC;
  559                 goto out;
  560         }
  561         if (hdr->e_ident[EI_VERSION] != EV_CURRENT
  562             || hdr->e_version != EV_CURRENT) {
  563                 link_elf_error(filename, "Unsupported file version");
  564                 error = ENOEXEC;
  565                 goto out;
  566         }
  567         if (hdr->e_type != ET_REL) {
  568                 error = ENOSYS;
  569                 goto out;
  570         }
  571         if (hdr->e_machine != ELF_TARG_MACH) {
  572                 link_elf_error(filename, "Unsupported machine");
  573                 error = ENOEXEC;
  574                 goto out;
  575         }
  576 
  577         lf = linker_make_file(filename, &link_elf_class);
  578         if (!lf) {
  579                 error = ENOMEM;
  580                 goto out;
  581         }
  582         ef = (elf_file_t) lf;
  583         ef->nprogtab = 0;
  584         ef->e_shdr = 0;
  585         ef->nreltab = 0;
  586         ef->nrelatab = 0;
  587 
  588         /* Allocate and read in the section header */
  589         nbytes = hdr->e_shnum * hdr->e_shentsize;
  590         if (nbytes == 0 || hdr->e_shoff == 0 ||
  591             hdr->e_shentsize != sizeof(Elf_Shdr)) {
  592                 error = ENOEXEC;
  593                 goto out;
  594         }
  595         shdr = malloc(nbytes, M_LINKER, M_WAITOK);
  596         ef->e_shdr = shdr;
  597         error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
  598             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
  599         if (error)
  600                 goto out;
  601         if (resid) {
  602                 error = ENOEXEC;
  603                 goto out;
  604         }
  605 
  606         /* Scan the section header for information and table sizing. */
  607         nsym = 0;
  608         symtabindex = -1;
  609         symstrindex = -1;
  610         for (i = 0; i < hdr->e_shnum; i++) {
  611                 if (shdr[i].sh_size == 0)
  612                         continue;
  613                 switch (shdr[i].sh_type) {
  614                 case SHT_PROGBITS:
  615                 case SHT_NOBITS:
  616 #ifdef __amd64__
  617                 case SHT_X86_64_UNWIND:
  618 #endif
  619                         if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
  620                                 break;
  621                         ef->nprogtab++;
  622                         break;
  623                 case SHT_SYMTAB:
  624                         nsym++;
  625                         symtabindex = i;
  626                         symstrindex = shdr[i].sh_link;
  627                         break;
  628                 case SHT_REL:
  629                         /*
  630                          * Ignore relocation tables for unallocated
  631                          * sections.
  632                          */
  633                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
  634                                 break;
  635                         ef->nreltab++;
  636                         break;
  637                 case SHT_RELA:
  638                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
  639                                 break;
  640                         ef->nrelatab++;
  641                         break;
  642                 case SHT_STRTAB:
  643                         break;
  644                 }
  645         }
  646         if (ef->nprogtab == 0) {
  647                 link_elf_error(filename, "file has no contents");
  648                 error = ENOEXEC;
  649                 goto out;
  650         }
  651         if (nsym != 1) {
  652                 /* Only allow one symbol table for now */
  653                 link_elf_error(filename, "file has no valid symbol table");
  654                 error = ENOEXEC;
  655                 goto out;
  656         }
  657         if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
  658             shdr[symstrindex].sh_type != SHT_STRTAB) {
  659                 link_elf_error(filename, "file has invalid symbol strings");
  660                 error = ENOEXEC;
  661                 goto out;
  662         }
  663 
  664         /* Allocate space for tracking the load chunks */
  665         if (ef->nprogtab != 0)
  666                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
  667                     M_LINKER, M_WAITOK | M_ZERO);
  668         if (ef->nreltab != 0)
  669                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
  670                     M_LINKER, M_WAITOK | M_ZERO);
  671         if (ef->nrelatab != 0)
  672                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
  673                     M_LINKER, M_WAITOK | M_ZERO);
  674 
  675         if (symtabindex == -1) {
  676                 link_elf_error(filename, "lost symbol table index");
  677                 error = ENOEXEC;
  678                 goto out;
  679         }
  680         /* Allocate space for and load the symbol table */
  681         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
  682         ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
  683         error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
  684             shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
  685             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  686             &resid, td);
  687         if (error)
  688                 goto out;
  689         if (resid != 0){
  690                 error = EINVAL;
  691                 goto out;
  692         }
  693 
  694         if (symstrindex == -1) {
  695                 link_elf_error(filename, "lost symbol string index");
  696                 error = ENOEXEC;
  697                 goto out;
  698         }
  699         /* Allocate space for and load the symbol strings */
  700         ef->ddbstrcnt = shdr[symstrindex].sh_size;
  701         ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
  702         error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
  703             shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
  704             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  705             &resid, td);
  706         if (error)
  707                 goto out;
  708         if (resid != 0){
  709                 error = EINVAL;
  710                 goto out;
  711         }
  712 
  713         /* Do we have a string table for the section names?  */
  714         shstrindex = -1;
  715         if (hdr->e_shstrndx != 0 &&
  716             shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
  717                 shstrindex = hdr->e_shstrndx;
  718                 ef->shstrcnt = shdr[shstrindex].sh_size;
  719                 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
  720                     M_WAITOK);
  721                 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
  722                     shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
  723                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  724                     &resid, td);
  725                 if (error)
  726                         goto out;
  727                 if (resid != 0){
  728                         error = EINVAL;
  729                         goto out;
  730                 }
  731         }
  732 
  733         /* Size up code/data(progbits) and bss(nobits). */
  734         alignmask = 0;
  735         for (i = 0; i < hdr->e_shnum; i++) {
  736                 if (shdr[i].sh_size == 0)
  737                         continue;
  738                 switch (shdr[i].sh_type) {
  739                 case SHT_PROGBITS:
  740                 case SHT_NOBITS:
  741 #ifdef __amd64__
  742                 case SHT_X86_64_UNWIND:
  743 #endif
  744                         if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
  745                                 break;
  746                         alignmask = shdr[i].sh_addralign - 1;
  747                         mapsize += alignmask;
  748                         mapsize &= ~alignmask;
  749                         mapsize += shdr[i].sh_size;
  750                         break;
  751                 }
  752         }
  753 
  754         /*
  755          * We know how much space we need for the text/data/bss/etc.
  756          * This stuff needs to be in a single chunk so that profiling etc
  757          * can get the bounds and gdb can associate offsets with modules
  758          */
  759         ef->object = vm_object_allocate(OBJT_DEFAULT,
  760             round_page(mapsize) >> PAGE_SHIFT);
  761         if (ef->object == NULL) {
  762                 error = ENOMEM;
  763                 goto out;
  764         }
  765         ef->address = (caddr_t) vm_map_min(kernel_map);
  766 
  767         /*
  768          * In order to satisfy amd64's architectural requirements on the
  769          * location of code and data in the kernel's address space, request a
  770          * mapping that is above the kernel.  
  771          */
  772 #ifdef __amd64__
  773         mapbase = KERNBASE;
  774 #else
  775         mapbase = VM_MIN_KERNEL_ADDRESS;
  776 #endif
  777         error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
  778             round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
  779             VM_PROT_ALL, 0);
  780         if (error) {
  781                 vm_object_deallocate(ef->object);
  782                 ef->object = 0;
  783                 goto out;
  784         }
  785 
  786         /* Wire the pages */
  787         error = vm_map_wire(kernel_map, mapbase,
  788             mapbase + round_page(mapsize),
  789             VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
  790         if (error != KERN_SUCCESS) {
  791                 error = ENOMEM;
  792                 goto out;
  793         }
  794 
  795         /* Inform the kld system about the situation */
  796         lf->address = ef->address = (caddr_t)mapbase;
  797         lf->size = mapsize;
  798 
  799         /*
  800          * Now load code/data(progbits), zero bss(nobits), allocate space for
  801          * and load relocs
  802          */
  803         pb = 0;
  804         rl = 0;
  805         ra = 0;
  806         alignmask = 0;
  807         for (i = 0; i < hdr->e_shnum; i++) {
  808                 if (shdr[i].sh_size == 0)
  809                         continue;
  810                 switch (shdr[i].sh_type) {
  811                 case SHT_PROGBITS:
  812                 case SHT_NOBITS:
  813 #ifdef __amd64__
  814                 case SHT_X86_64_UNWIND:
  815 #endif
  816                         if ((shdr[i].sh_flags & SHF_ALLOC) == 0)
  817                                 break;
  818                         alignmask = shdr[i].sh_addralign - 1;
  819                         mapbase += alignmask;
  820                         mapbase &= ~alignmask;
  821                         if (ef->shstrtab != NULL && shdr[i].sh_name != 0) {
  822                                 ef->progtab[pb].name =
  823                                     ef->shstrtab + shdr[i].sh_name;
  824                                 if (!strcmp(ef->progtab[pb].name, ".ctors")) {
  825                                         lf->ctors_addr = (caddr_t)mapbase;
  826                                         lf->ctors_size = shdr[i].sh_size;
  827                                 }
  828                         } else if (shdr[i].sh_type == SHT_PROGBITS)
  829                                 ef->progtab[pb].name = "<<PROGBITS>>";
  830 #ifdef __amd64__
  831                         else if (shdr[i].sh_type == SHT_X86_64_UNWIND)
  832                                 ef->progtab[pb].name = "<<UNWIND>>";
  833 #endif
  834                         else
  835                                 ef->progtab[pb].name = "<<NOBITS>>";
  836                         if (ef->progtab[pb].name != NULL && 
  837                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
  838                                 ef->progtab[pb].addr =
  839                                     dpcpu_alloc(shdr[i].sh_size);
  840 #ifdef VIMAGE
  841                         else if (ef->progtab[pb].name != NULL &&
  842                             !strcmp(ef->progtab[pb].name, VNET_SETNAME))
  843                                 ef->progtab[pb].addr =
  844                                     vnet_data_alloc(shdr[i].sh_size);
  845 #endif
  846                         else
  847                                 ef->progtab[pb].addr =
  848                                     (void *)(uintptr_t)mapbase;
  849                         if (ef->progtab[pb].addr == NULL) {
  850                                 error = ENOSPC;
  851                                 goto out;
  852                         }
  853                         ef->progtab[pb].size = shdr[i].sh_size;
  854                         ef->progtab[pb].sec = i;
  855                         if (shdr[i].sh_type == SHT_PROGBITS
  856 #ifdef __amd64__
  857                             || shdr[i].sh_type == SHT_X86_64_UNWIND
  858 #endif
  859                             ) {
  860                                 error = vn_rdwr(UIO_READ, nd.ni_vp,
  861                                     ef->progtab[pb].addr,
  862                                     shdr[i].sh_size, shdr[i].sh_offset,
  863                                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
  864                                     NOCRED, &resid, td);
  865                                 if (error)
  866                                         goto out;
  867                                 if (resid != 0){
  868                                         error = EINVAL;
  869                                         goto out;
  870                                 }
  871                                 /* Initialize the per-cpu or vnet area. */
  872                                 if (ef->progtab[pb].addr != (void *)mapbase &&
  873                                     !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
  874                                         dpcpu_copy(ef->progtab[pb].addr,
  875                                             shdr[i].sh_size);
  876 #ifdef VIMAGE
  877                                 else if (ef->progtab[pb].addr !=
  878                                     (void *)mapbase &&
  879                                     !strcmp(ef->progtab[pb].name, VNET_SETNAME))
  880                                         vnet_data_copy(ef->progtab[pb].addr,
  881                                             shdr[i].sh_size);
  882 #endif
  883                         } else
  884                                 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
  885 
  886                         /* Update all symbol values with the offset. */
  887                         for (j = 0; j < ef->ddbsymcnt; j++) {
  888                                 es = &ef->ddbsymtab[j];
  889                                 if (es->st_shndx != i)
  890                                         continue;
  891                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
  892                         }
  893                         mapbase += shdr[i].sh_size;
  894                         pb++;
  895                         break;
  896                 case SHT_REL:
  897                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
  898                                 break;
  899                         ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
  900                             M_WAITOK);
  901                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
  902                         ef->reltab[rl].sec = shdr[i].sh_info;
  903                         error = vn_rdwr(UIO_READ, nd.ni_vp,
  904                             (void *)ef->reltab[rl].rel,
  905                             shdr[i].sh_size, shdr[i].sh_offset,
  906                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  907                             &resid, td);
  908                         if (error)
  909                                 goto out;
  910                         if (resid != 0){
  911                                 error = EINVAL;
  912                                 goto out;
  913                         }
  914                         rl++;
  915                         break;
  916                 case SHT_RELA:
  917                         if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0)
  918                                 break;
  919                         ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
  920                             M_WAITOK);
  921                         ef->relatab[ra].nrela =
  922                             shdr[i].sh_size / sizeof(Elf_Rela);
  923                         ef->relatab[ra].sec = shdr[i].sh_info;
  924                         error = vn_rdwr(UIO_READ, nd.ni_vp,
  925                             (void *)ef->relatab[ra].rela,
  926                             shdr[i].sh_size, shdr[i].sh_offset,
  927                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  928                             &resid, td);
  929                         if (error)
  930                                 goto out;
  931                         if (resid != 0){
  932                                 error = EINVAL;
  933                                 goto out;
  934                         }
  935                         ra++;
  936                         break;
  937                 }
  938         }
  939         if (pb != ef->nprogtab) {
  940                 link_elf_error(filename, "lost progbits");
  941                 error = ENOEXEC;
  942                 goto out;
  943         }
  944         if (rl != ef->nreltab) {
  945                 link_elf_error(filename, "lost reltab");
  946                 error = ENOEXEC;
  947                 goto out;
  948         }
  949         if (ra != ef->nrelatab) {
  950                 link_elf_error(filename, "lost relatab");
  951                 error = ENOEXEC;
  952                 goto out;
  953         }
  954         if (mapbase != (vm_offset_t)ef->address + mapsize) {
  955                 printf(
  956                     "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
  957                     filename != NULL ? filename : "<none>",
  958                     (u_long)mapbase, ef->address, (u_long)mapsize,
  959                     (u_long)(vm_offset_t)ef->address + mapsize);
  960                 error = ENOMEM;
  961                 goto out;
  962         }
  963 
  964         /* Local intra-module relocations */
  965         error = link_elf_reloc_local(lf);
  966         if (error != 0)
  967                 goto out;
  968 
  969         /* Pull in dependencies */
  970         VOP_UNLOCK(nd.ni_vp, 0);
  971         error = linker_load_dependencies(lf);
  972         vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
  973         if (error)
  974                 goto out;
  975 
  976         /* External relocations */
  977         error = relocate_file(ef);
  978         if (error)
  979                 goto out;
  980 
  981         /* Notify MD code that a module is being loaded. */
  982         error = elf_cpu_load_file(lf);
  983         if (error)
  984                 goto out;
  985 
  986         /* Invoke .ctors */
  987         link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
  988 
  989         *result = lf;
  990 
  991 out:
  992         VOP_UNLOCK(nd.ni_vp, 0);
  993         vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
  994         if (error && lf)
  995                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
  996         free(hdr, M_LINKER);
  997 
  998         return error;
  999 }
 1000 
 1001 static void
 1002 link_elf_unload_file(linker_file_t file)
 1003 {
 1004         elf_file_t ef = (elf_file_t) file;
 1005         int i;
 1006 
 1007         /* Notify MD code that a module is being unloaded. */
 1008         elf_cpu_unload_file(file);
 1009 
 1010         if (ef->progtab) {
 1011                 for (i = 0; i < ef->nprogtab; i++) {
 1012                         if (ef->progtab[i].size == 0)
 1013                                 continue;
 1014                         if (ef->progtab[i].name == NULL)
 1015                                 continue;
 1016                         if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
 1017                                 dpcpu_free(ef->progtab[i].addr,
 1018                                     ef->progtab[i].size);
 1019 #ifdef VIMAGE
 1020                         else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
 1021                                 vnet_data_free(ef->progtab[i].addr,
 1022                                     ef->progtab[i].size);
 1023 #endif
 1024                 }
 1025         }
 1026         if (ef->preloaded) {
 1027                 free(ef->reltab, M_LINKER);
 1028                 free(ef->relatab, M_LINKER);
 1029                 free(ef->progtab, M_LINKER);
 1030                 free(ef->ctftab, M_LINKER);
 1031                 free(ef->ctfoff, M_LINKER);
 1032                 free(ef->typoff, M_LINKER);
 1033                 if (file->filename != NULL)
 1034                         preload_delete_name(file->filename);
 1035                 /* XXX reclaim module memory? */
 1036                 return;
 1037         }
 1038 
 1039         for (i = 0; i < ef->nreltab; i++)
 1040                 free(ef->reltab[i].rel, M_LINKER);
 1041         for (i = 0; i < ef->nrelatab; i++)
 1042                 free(ef->relatab[i].rela, M_LINKER);
 1043         free(ef->reltab, M_LINKER);
 1044         free(ef->relatab, M_LINKER);
 1045         free(ef->progtab, M_LINKER);
 1046 
 1047         if (ef->object) {
 1048                 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
 1049                     (vm_offset_t) ef->address +
 1050                     (ef->object->size << PAGE_SHIFT));
 1051         }
 1052         free(ef->e_shdr, M_LINKER);
 1053         free(ef->ddbsymtab, M_LINKER);
 1054         free(ef->ddbstrtab, M_LINKER);
 1055         free(ef->shstrtab, M_LINKER);
 1056         free(ef->ctftab, M_LINKER);
 1057         free(ef->ctfoff, M_LINKER);
 1058         free(ef->typoff, M_LINKER);
 1059 }
 1060 
 1061 static const char *
 1062 symbol_name(elf_file_t ef, Elf_Size r_info)
 1063 {
 1064         const Elf_Sym *ref;
 1065 
 1066         if (ELF_R_SYM(r_info)) {
 1067                 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
 1068                 return ef->ddbstrtab + ref->st_name;
 1069         } else
 1070                 return NULL;
 1071 }
 1072 
 1073 static Elf_Addr
 1074 findbase(elf_file_t ef, int sec)
 1075 {
 1076         int i;
 1077         Elf_Addr base = 0;
 1078 
 1079         for (i = 0; i < ef->nprogtab; i++) {
 1080                 if (sec == ef->progtab[i].sec) {
 1081                         base = (Elf_Addr)ef->progtab[i].addr;
 1082                         break;
 1083                 }
 1084         }
 1085         return base;
 1086 }
 1087 
 1088 static int
 1089 relocate_file(elf_file_t ef)
 1090 {
 1091         const Elf_Rel *rellim;
 1092         const Elf_Rel *rel;
 1093         const Elf_Rela *relalim;
 1094         const Elf_Rela *rela;
 1095         const char *symname;
 1096         const Elf_Sym *sym;
 1097         int i;
 1098         Elf_Size symidx;
 1099         Elf_Addr base;
 1100 
 1101 
 1102         /* Perform relocations without addend if there are any: */
 1103         for (i = 0; i < ef->nreltab; i++) {
 1104                 rel = ef->reltab[i].rel;
 1105                 if (rel == NULL) {
 1106                         link_elf_error(ef->lf.filename, "lost a reltab!");
 1107                         return (ENOEXEC);
 1108                 }
 1109                 rellim = rel + ef->reltab[i].nrel;
 1110                 base = findbase(ef, ef->reltab[i].sec);
 1111                 if (base == 0) {
 1112                         link_elf_error(ef->lf.filename, "lost base for reltab");
 1113                         return (ENOEXEC);
 1114                 }
 1115                 for ( ; rel < rellim; rel++) {
 1116                         symidx = ELF_R_SYM(rel->r_info);
 1117                         if (symidx >= ef->ddbsymcnt)
 1118                                 continue;
 1119                         sym = ef->ddbsymtab + symidx;
 1120                         /* Local relocs are already done */
 1121                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
 1122                                 continue;
 1123                         if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
 1124                             elf_obj_lookup)) {
 1125                                 symname = symbol_name(ef, rel->r_info);
 1126                                 printf("link_elf_obj: symbol %s undefined\n",
 1127                                     symname);
 1128                                 return (ENOENT);
 1129                         }
 1130                 }
 1131         }
 1132 
 1133         /* Perform relocations with addend if there are any: */
 1134         for (i = 0; i < ef->nrelatab; i++) {
 1135                 rela = ef->relatab[i].rela;
 1136                 if (rela == NULL) {
 1137                         link_elf_error(ef->lf.filename, "lost a relatab!");
 1138                         return (ENOEXEC);
 1139                 }
 1140                 relalim = rela + ef->relatab[i].nrela;
 1141                 base = findbase(ef, ef->relatab[i].sec);
 1142                 if (base == 0) {
 1143                         link_elf_error(ef->lf.filename,
 1144                             "lost base for relatab");
 1145                         return (ENOEXEC);
 1146                 }
 1147                 for ( ; rela < relalim; rela++) {
 1148                         symidx = ELF_R_SYM(rela->r_info);
 1149                         if (symidx >= ef->ddbsymcnt)
 1150                                 continue;
 1151                         sym = ef->ddbsymtab + symidx;
 1152                         /* Local relocs are already done */
 1153                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
 1154                                 continue;
 1155                         if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
 1156                             elf_obj_lookup)) {
 1157                                 symname = symbol_name(ef, rela->r_info);
 1158                                 printf("link_elf_obj: symbol %s undefined\n",
 1159                                     symname);
 1160                                 return (ENOENT);
 1161                         }
 1162                 }
 1163         }
 1164 
 1165         /*
 1166          * Only clean SHN_FBSD_CACHED for successful return.  If we
 1167          * modified symbol table for the object but found an
 1168          * unresolved symbol, there is no reason to roll back.
 1169          */
 1170         elf_obj_cleanup_globals_cache(ef);
 1171 
 1172         return (0);
 1173 }
 1174 
 1175 static int
 1176 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
 1177 {
 1178         elf_file_t ef = (elf_file_t) lf;
 1179         const Elf_Sym *symp;
 1180         const char *strp;
 1181         int i;
 1182 
 1183         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1184                 strp = ef->ddbstrtab + symp->st_name;
 1185                 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
 1186                         *sym = (c_linker_sym_t) symp;
 1187                         return 0;
 1188                 }
 1189         }
 1190         return ENOENT;
 1191 }
 1192 
 1193 static int
 1194 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
 1195     linker_symval_t *symval)
 1196 {
 1197         elf_file_t ef;
 1198         const Elf_Sym *es;
 1199         caddr_t val;
 1200 
 1201         ef = (elf_file_t) lf;
 1202         es = (const Elf_Sym*) sym;
 1203         val = (caddr_t)es->st_value;
 1204         if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
 1205                 symval->name = ef->ddbstrtab + es->st_name;
 1206                 val = (caddr_t)es->st_value;
 1207                 if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
 1208                         val = ((caddr_t (*)(void))val)();
 1209                 symval->value = val;
 1210                 symval->size = es->st_size;
 1211                 return 0;
 1212         }
 1213         return ENOENT;
 1214 }
 1215 
 1216 static int
 1217 link_elf_search_symbol(linker_file_t lf, caddr_t value,
 1218     c_linker_sym_t *sym, long *diffp)
 1219 {
 1220         elf_file_t ef = (elf_file_t) lf;
 1221         u_long off = (uintptr_t) (void *) value;
 1222         u_long diff = off;
 1223         u_long st_value;
 1224         const Elf_Sym *es;
 1225         const Elf_Sym *best = NULL;
 1226         int i;
 1227 
 1228         for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
 1229                 if (es->st_name == 0)
 1230                         continue;
 1231                 st_value = es->st_value;
 1232                 if (off >= st_value) {
 1233                         if (off - st_value < diff) {
 1234                                 diff = off - st_value;
 1235                                 best = es;
 1236                                 if (diff == 0)
 1237                                         break;
 1238                         } else if (off - st_value == diff) {
 1239                                 best = es;
 1240                         }
 1241                 }
 1242         }
 1243         if (best == NULL)
 1244                 *diffp = off;
 1245         else
 1246                 *diffp = diff;
 1247         *sym = (c_linker_sym_t) best;
 1248 
 1249         return 0;
 1250 }
 1251 
 1252 /*
 1253  * Look up a linker set on an ELF system.
 1254  */
 1255 static int
 1256 link_elf_lookup_set(linker_file_t lf, const char *name,
 1257     void ***startp, void ***stopp, int *countp)
 1258 {
 1259         elf_file_t ef = (elf_file_t)lf;
 1260         void **start, **stop;
 1261         int i, count;
 1262 
 1263         /* Relative to section number */
 1264         for (i = 0; i < ef->nprogtab; i++) {
 1265                 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
 1266                     strcmp(ef->progtab[i].name + 4, name) == 0) {
 1267                         start  = (void **)ef->progtab[i].addr;
 1268                         stop = (void **)((char *)ef->progtab[i].addr +
 1269                             ef->progtab[i].size);
 1270                         count = stop - start;
 1271                         if (startp)
 1272                                 *startp = start;
 1273                         if (stopp)
 1274                                 *stopp = stop;
 1275                         if (countp)
 1276                                 *countp = count;
 1277                         return (0);
 1278                 }
 1279         }
 1280         return (ESRCH);
 1281 }
 1282 
 1283 static int
 1284 link_elf_each_function_name(linker_file_t file,
 1285     int (*callback)(const char *, void *), void *opaque)
 1286 {
 1287         elf_file_t ef = (elf_file_t)file;
 1288         const Elf_Sym *symp;
 1289         int i, error;
 1290         
 1291         /* Exhaustive search */
 1292         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1293                 if (symp->st_value != 0 &&
 1294                     (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
 1295                     ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
 1296                         error = callback(ef->ddbstrtab + symp->st_name, opaque);
 1297                         if (error)
 1298                                 return (error);
 1299                 }
 1300         }
 1301         return (0);
 1302 }
 1303 
 1304 static int
 1305 link_elf_each_function_nameval(linker_file_t file,
 1306     linker_function_nameval_callback_t callback, void *opaque)
 1307 {
 1308         linker_symval_t symval;
 1309         elf_file_t ef = (elf_file_t)file;
 1310         const Elf_Sym* symp;
 1311         int i, error;
 1312 
 1313         /* Exhaustive search */
 1314         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1315                 if (symp->st_value != 0 &&
 1316                     (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
 1317                     ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
 1318                         error = link_elf_symbol_values(file,
 1319                             (c_linker_sym_t)symp, &symval);
 1320                         if (error)
 1321                                 return (error);
 1322                         error = callback(file, i, &symval, opaque);
 1323                         if (error)
 1324                                 return (error);
 1325                 }
 1326         }
 1327         return (0);
 1328 }
 1329 
 1330 static void
 1331 elf_obj_cleanup_globals_cache(elf_file_t ef)
 1332 {
 1333         Elf_Sym *sym;
 1334         Elf_Size i;
 1335 
 1336         for (i = 0; i < ef->ddbsymcnt; i++) {
 1337                 sym = ef->ddbsymtab + i;
 1338                 if (sym->st_shndx == SHN_FBSD_CACHED) {
 1339                         sym->st_shndx = SHN_UNDEF;
 1340                         sym->st_value = 0;
 1341                 }
 1342         }
 1343 }
 1344 
 1345 /*
 1346  * Symbol lookup function that can be used when the symbol index is known (ie
 1347  * in relocations). It uses the symbol index instead of doing a fully fledged
 1348  * hash table based lookup when such is valid. For example for local symbols.
 1349  * This is not only more efficient, it's also more correct. It's not always
 1350  * the case that the symbol can be found through the hash table.
 1351  */
 1352 static int
 1353 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
 1354 {
 1355         elf_file_t ef = (elf_file_t)lf;
 1356         Elf_Sym *sym;
 1357         const char *symbol;
 1358         Elf_Addr res1;
 1359 
 1360         /* Don't even try to lookup the symbol if the index is bogus. */
 1361         if (symidx >= ef->ddbsymcnt) {
 1362                 *res = 0;
 1363                 return (EINVAL);
 1364         }
 1365 
 1366         sym = ef->ddbsymtab + symidx;
 1367 
 1368         /* Quick answer if there is a definition included. */
 1369         if (sym->st_shndx != SHN_UNDEF) {
 1370                 *res = sym->st_value;
 1371                 return (0);
 1372         }
 1373 
 1374         /* If we get here, then it is undefined and needs a lookup. */
 1375         switch (ELF_ST_BIND(sym->st_info)) {
 1376         case STB_LOCAL:
 1377                 /* Local, but undefined? huh? */
 1378                 *res = 0;
 1379                 return (EINVAL);
 1380 
 1381         case STB_GLOBAL:
 1382         case STB_WEAK:
 1383                 /* Relative to Data or Function name */
 1384                 symbol = ef->ddbstrtab + sym->st_name;
 1385 
 1386                 /* Force a lookup failure if the symbol name is bogus. */
 1387                 if (*symbol == 0) {
 1388                         *res = 0;
 1389                         return (EINVAL);
 1390                 }
 1391                 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
 1392 
 1393                 /*
 1394                  * Cache global lookups during module relocation. The failure
 1395                  * case is particularly expensive for callers, who must scan
 1396                  * through the entire globals table doing strcmp(). Cache to
 1397                  * avoid doing such work repeatedly.
 1398                  *
 1399                  * After relocation is complete, undefined globals will be
 1400                  * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
 1401                  * above.
 1402                  */
 1403                 if (res1 != 0) {
 1404                         sym->st_shndx = SHN_FBSD_CACHED;
 1405                         sym->st_value = res1;
 1406                         *res = res1;
 1407                         return (0);
 1408                 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
 1409                         sym->st_value = 0;
 1410                         *res = 0;
 1411                         return (0);
 1412                 }
 1413                 return (EINVAL);
 1414 
 1415         default:
 1416                 return (EINVAL);
 1417         }
 1418 }
 1419 
 1420 static void
 1421 link_elf_fix_link_set(elf_file_t ef)
 1422 {
 1423         static const char startn[] = "__start_";
 1424         static const char stopn[] = "__stop_";
 1425         Elf_Sym *sym;
 1426         const char *sym_name, *linkset_name;
 1427         Elf_Addr startp, stopp;
 1428         Elf_Size symidx;
 1429         int start, i;
 1430 
 1431         startp = stopp = 0;
 1432         for (symidx = 1 /* zero entry is special */;
 1433                 symidx < ef->ddbsymcnt; symidx++) {
 1434                 sym = ef->ddbsymtab + symidx;
 1435                 if (sym->st_shndx != SHN_UNDEF)
 1436                         continue;
 1437 
 1438                 sym_name = ef->ddbstrtab + sym->st_name;
 1439                 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
 1440                         start = 1;
 1441                         linkset_name = sym_name + sizeof(startn) - 1;
 1442                 }
 1443                 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
 1444                         start = 0;
 1445                         linkset_name = sym_name + sizeof(stopn) - 1;
 1446                 }
 1447                 else
 1448                         continue;
 1449 
 1450                 for (i = 0; i < ef->nprogtab; i++) {
 1451                         if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
 1452                                 startp = (Elf_Addr)ef->progtab[i].addr;
 1453                                 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
 1454                                 break;
 1455                         }
 1456                 }
 1457                 if (i == ef->nprogtab)
 1458                         continue;
 1459 
 1460                 sym->st_value = start ? startp : stopp;
 1461                 sym->st_shndx = i;
 1462         }
 1463 }
 1464 
 1465 static int
 1466 link_elf_reloc_local(linker_file_t lf)
 1467 {
 1468         elf_file_t ef = (elf_file_t)lf;
 1469         const Elf_Rel *rellim;
 1470         const Elf_Rel *rel;
 1471         const Elf_Rela *relalim;
 1472         const Elf_Rela *rela;
 1473         const Elf_Sym *sym;
 1474         Elf_Addr base;
 1475         int i;
 1476         Elf_Size symidx;
 1477 
 1478         link_elf_fix_link_set(ef);
 1479 
 1480         /* Perform relocations without addend if there are any: */
 1481         for (i = 0; i < ef->nreltab; i++) {
 1482                 rel = ef->reltab[i].rel;
 1483                 if (rel == NULL) {
 1484                         link_elf_error(ef->lf.filename, "lost a reltab");
 1485                         return (ENOEXEC);
 1486                 }
 1487                 rellim = rel + ef->reltab[i].nrel;
 1488                 base = findbase(ef, ef->reltab[i].sec);
 1489                 if (base == 0) {
 1490                         link_elf_error(ef->lf.filename, "lost base for reltab");
 1491                         return (ENOEXEC);
 1492                 }
 1493                 for ( ; rel < rellim; rel++) {
 1494                         symidx = ELF_R_SYM(rel->r_info);
 1495                         if (symidx >= ef->ddbsymcnt)
 1496                                 continue;
 1497                         sym = ef->ddbsymtab + symidx;
 1498                         /* Only do local relocs */
 1499                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
 1500                                 continue;
 1501                         elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
 1502                             elf_obj_lookup);
 1503                 }
 1504         }
 1505 
 1506         /* Perform relocations with addend if there are any: */
 1507         for (i = 0; i < ef->nrelatab; i++) {
 1508                 rela = ef->relatab[i].rela;
 1509                 if (rela == NULL) {
 1510                         link_elf_error(ef->lf.filename, "lost a relatab!");
 1511                         return (ENOEXEC);
 1512                 }
 1513                 relalim = rela + ef->relatab[i].nrela;
 1514                 base = findbase(ef, ef->relatab[i].sec);
 1515                 if (base == 0) {
 1516                         link_elf_error(ef->lf.filename, "lost base for reltab");
 1517                         return (ENOEXEC);
 1518                 }
 1519                 for ( ; rela < relalim; rela++) {
 1520                         symidx = ELF_R_SYM(rela->r_info);
 1521                         if (symidx >= ef->ddbsymcnt)
 1522                                 continue;
 1523                         sym = ef->ddbsymtab + symidx;
 1524                         /* Only do local relocs */
 1525                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
 1526                                 continue;
 1527                         elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
 1528                             elf_obj_lookup);
 1529                 }
 1530         }
 1531         return (0);
 1532 }
 1533 
 1534 static long
 1535 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
 1536 {
 1537     elf_file_t ef = (elf_file_t)lf;
 1538     
 1539     *symtab = ef->ddbsymtab;
 1540     
 1541     if (*symtab == NULL)
 1542         return (0);
 1543 
 1544     return (ef->ddbsymcnt);
 1545 }
 1546     
 1547 static long
 1548 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
 1549 {
 1550     elf_file_t ef = (elf_file_t)lf;
 1551 
 1552     *strtab = ef->ddbstrtab;
 1553 
 1554     if (*strtab == NULL)
 1555         return (0);
 1556 
 1557     return (ef->ddbstrcnt);
 1558 }

Cache object: fb971442b91579bf6b01580405ef9378


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