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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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: stable/10/sys/kern/link_elf_obj.c 302234 2016-06-27 21:50:30Z bdrewery $");
   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 <net/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_AMD64_UNWIND:
  262 #endif
  263                         ef->nprogtab++;
  264                         break;
  265                 case SHT_SYMTAB:
  266                         symtabindex = i;
  267                         symstrindex = shdr[i].sh_link;
  268                         break;
  269                 case SHT_REL:
  270                         ef->nreltab++;
  271                         break;
  272                 case SHT_RELA:
  273                         ef->nrelatab++;
  274                         break;
  275                 }
  276         }
  277 
  278         shstrindex = hdr->e_shstrndx;
  279         if (ef->nprogtab == 0 || symstrindex < 0 ||
  280             symstrindex >= hdr->e_shnum ||
  281             shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
  282             shstrindex >= hdr->e_shnum ||
  283             shdr[shstrindex].sh_type != SHT_STRTAB) {
  284                 printf("%s: bad/missing section headers\n", filename);
  285                 error = ENOEXEC;
  286                 goto out;
  287         }
  288 
  289         /* Allocate space for tracking the load chunks */
  290         if (ef->nprogtab != 0)
  291                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
  292                     M_LINKER, M_WAITOK | M_ZERO);
  293         if (ef->nreltab != 0)
  294                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
  295                     M_LINKER, M_WAITOK | M_ZERO);
  296         if (ef->nrelatab != 0)
  297                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
  298                     M_LINKER, M_WAITOK | M_ZERO);
  299         if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
  300             (ef->nreltab != 0 && ef->reltab == NULL) ||
  301             (ef->nrelatab != 0 && ef->relatab == NULL)) {
  302                 error = ENOMEM;
  303                 goto out;
  304         }
  305 
  306         /* XXX, relocate the sh_addr fields saved by the loader. */
  307         off = 0;
  308         for (i = 0; i < hdr->e_shnum; i++) {
  309                 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
  310                         off = shdr[i].sh_addr;
  311         }
  312         for (i = 0; i < hdr->e_shnum; i++) {
  313                 if (shdr[i].sh_addr != 0)
  314                         shdr[i].sh_addr = shdr[i].sh_addr - off +
  315                             (Elf_Addr)ef->address;
  316         }
  317 
  318         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
  319         ef->ddbsymtab = (Elf_Sym *)shdr[symtabindex].sh_addr;
  320         ef->ddbstrcnt = shdr[symstrindex].sh_size;
  321         ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
  322         ef->shstrcnt = shdr[shstrindex].sh_size;
  323         ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
  324 
  325         /* Now fill out progtab and the relocation tables. */
  326         pb = 0;
  327         rl = 0;
  328         ra = 0;
  329         for (i = 0; i < hdr->e_shnum; i++) {
  330                 switch (shdr[i].sh_type) {
  331                 case SHT_PROGBITS:
  332                 case SHT_NOBITS:
  333 #ifdef __amd64__
  334                 case SHT_AMD64_UNWIND:
  335 #endif
  336                         ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
  337                         if (shdr[i].sh_type == SHT_PROGBITS)
  338                                 ef->progtab[pb].name = "<<PROGBITS>>";
  339 #ifdef __amd64__
  340                         else if (shdr[i].sh_type == SHT_AMD64_UNWIND)
  341                                 ef->progtab[pb].name = "<<UNWIND>>";
  342 #endif
  343                         else
  344                                 ef->progtab[pb].name = "<<NOBITS>>";
  345                         ef->progtab[pb].size = shdr[i].sh_size;
  346                         ef->progtab[pb].sec = i;
  347                         if (ef->shstrtab && shdr[i].sh_name != 0)
  348                                 ef->progtab[pb].name =
  349                                     ef->shstrtab + shdr[i].sh_name;
  350                         if (ef->progtab[pb].name != NULL && 
  351                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME)) {
  352                                 void *dpcpu;
  353 
  354                                 dpcpu = dpcpu_alloc(shdr[i].sh_size);
  355                                 if (dpcpu == NULL) {
  356                                         error = ENOSPC;
  357                                         goto out;
  358                                 }
  359                                 memcpy(dpcpu, ef->progtab[pb].addr,
  360                                     ef->progtab[pb].size);
  361                                 dpcpu_copy(dpcpu, shdr[i].sh_size);
  362                                 ef->progtab[pb].addr = dpcpu;
  363 #ifdef VIMAGE
  364                         } else if (ef->progtab[pb].name != NULL &&
  365                             !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
  366                                 void *vnet_data;
  367 
  368                                 vnet_data = vnet_data_alloc(shdr[i].sh_size);
  369                                 if (vnet_data == NULL) {
  370                                         error = ENOSPC;
  371                                         goto out;
  372                                 }
  373                                 memcpy(vnet_data, ef->progtab[pb].addr,
  374                                     ef->progtab[pb].size);
  375                                 vnet_data_copy(vnet_data, shdr[i].sh_size);
  376                                 ef->progtab[pb].addr = vnet_data;
  377 #endif
  378                         }
  379 
  380                         /* Update all symbol values with the offset. */
  381                         for (j = 0; j < ef->ddbsymcnt; j++) {
  382                                 es = &ef->ddbsymtab[j];
  383                                 if (es->st_shndx != i)
  384                                         continue;
  385                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
  386                         }
  387                         pb++;
  388                         break;
  389                 case SHT_REL:
  390                         ef->reltab[rl].rel = (Elf_Rel *)shdr[i].sh_addr;
  391                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
  392                         ef->reltab[rl].sec = shdr[i].sh_info;
  393                         rl++;
  394                         break;
  395                 case SHT_RELA:
  396                         ef->relatab[ra].rela = (Elf_Rela *)shdr[i].sh_addr;
  397                         ef->relatab[ra].nrela =
  398                             shdr[i].sh_size / sizeof(Elf_Rela);
  399                         ef->relatab[ra].sec = shdr[i].sh_info;
  400                         ra++;
  401                         break;
  402                 }
  403         }
  404         if (pb != ef->nprogtab) {
  405                 printf("%s: lost progbits\n", filename);
  406                 error = ENOEXEC;
  407                 goto out;
  408         }
  409         if (rl != ef->nreltab) {
  410                 printf("%s: lost reltab\n", filename);
  411                 error = ENOEXEC;
  412                 goto out;
  413         }
  414         if (ra != ef->nrelatab) {
  415                 printf("%s: lost relatab\n", filename);
  416                 error = ENOEXEC;
  417                 goto out;
  418         }
  419 
  420         /* Local intra-module relocations */
  421         error = link_elf_reloc_local(lf);
  422         if (error != 0)
  423                 goto out;
  424 
  425         *result = lf;
  426         return (0);
  427 
  428 out:
  429         /* preload not done this way */
  430         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
  431         return (error);
  432 }
  433 
  434 static int
  435 link_elf_link_preload_finish(linker_file_t lf)
  436 {
  437         elf_file_t ef;
  438         int error;
  439 
  440         ef = (elf_file_t)lf;
  441         error = relocate_file(ef);
  442         if (error)
  443                 return error;
  444 
  445         /* Notify MD code that a module is being loaded. */
  446         error = elf_cpu_load_file(lf);
  447         if (error)
  448                 return (error);
  449 
  450         return (0);
  451 }
  452 
  453 static int
  454 link_elf_load_file(linker_class_t cls, const char *filename,
  455     linker_file_t *result)
  456 {
  457         struct nameidata nd;
  458         struct thread *td = curthread;  /* XXX */
  459         Elf_Ehdr *hdr;
  460         Elf_Shdr *shdr;
  461         Elf_Sym *es;
  462         int nbytes, i, j;
  463         vm_offset_t mapbase;
  464         size_t mapsize;
  465         int error = 0;
  466         ssize_t resid;
  467         int flags;
  468         elf_file_t ef;
  469         linker_file_t lf;
  470         int symtabindex;
  471         int symstrindex;
  472         int shstrindex;
  473         int nsym;
  474         int pb, rl, ra;
  475         int alignmask;
  476 
  477         shdr = NULL;
  478         lf = NULL;
  479         mapsize = 0;
  480         hdr = NULL;
  481 
  482         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
  483         flags = FREAD;
  484         error = vn_open(&nd, &flags, 0, NULL);
  485         if (error)
  486                 return error;
  487         NDFREE(&nd, NDF_ONLY_PNBUF);
  488         if (nd.ni_vp->v_type != VREG) {
  489                 error = ENOEXEC;
  490                 goto out;
  491         }
  492 #ifdef MAC
  493         error = mac_kld_check_load(td->td_ucred, nd.ni_vp);
  494         if (error) {
  495                 goto out;
  496         }
  497 #endif
  498 
  499         /* Read the elf header from the file. */
  500         hdr = malloc(sizeof(*hdr), M_LINKER, M_WAITOK);
  501         error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)hdr, sizeof(*hdr), 0,
  502             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  503             &resid, td);
  504         if (error)
  505                 goto out;
  506         if (resid != 0){
  507                 error = ENOEXEC;
  508                 goto out;
  509         }
  510 
  511         if (!IS_ELF(*hdr)) {
  512                 error = ENOEXEC;
  513                 goto out;
  514         }
  515 
  516         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
  517             || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
  518                 link_elf_error(filename, "Unsupported file layout");
  519                 error = ENOEXEC;
  520                 goto out;
  521         }
  522         if (hdr->e_ident[EI_VERSION] != EV_CURRENT
  523             || hdr->e_version != EV_CURRENT) {
  524                 link_elf_error(filename, "Unsupported file version");
  525                 error = ENOEXEC;
  526                 goto out;
  527         }
  528         if (hdr->e_type != ET_REL) {
  529                 error = ENOSYS;
  530                 goto out;
  531         }
  532         if (hdr->e_machine != ELF_TARG_MACH) {
  533                 link_elf_error(filename, "Unsupported machine");
  534                 error = ENOEXEC;
  535                 goto out;
  536         }
  537 
  538         lf = linker_make_file(filename, &link_elf_class);
  539         if (!lf) {
  540                 error = ENOMEM;
  541                 goto out;
  542         }
  543         ef = (elf_file_t) lf;
  544         ef->nprogtab = 0;
  545         ef->e_shdr = 0;
  546         ef->nreltab = 0;
  547         ef->nrelatab = 0;
  548 
  549         /* Allocate and read in the section header */
  550         nbytes = hdr->e_shnum * hdr->e_shentsize;
  551         if (nbytes == 0 || hdr->e_shoff == 0 ||
  552             hdr->e_shentsize != sizeof(Elf_Shdr)) {
  553                 error = ENOEXEC;
  554                 goto out;
  555         }
  556         shdr = malloc(nbytes, M_LINKER, M_WAITOK);
  557         ef->e_shdr = shdr;
  558         error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shdr, nbytes, hdr->e_shoff,
  559             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED, &resid, td);
  560         if (error)
  561                 goto out;
  562         if (resid) {
  563                 error = ENOEXEC;
  564                 goto out;
  565         }
  566 
  567         /* Scan the section header for information and table sizing. */
  568         nsym = 0;
  569         symtabindex = -1;
  570         symstrindex = -1;
  571         for (i = 0; i < hdr->e_shnum; i++) {
  572                 if (shdr[i].sh_size == 0)
  573                         continue;
  574                 switch (shdr[i].sh_type) {
  575                 case SHT_PROGBITS:
  576                 case SHT_NOBITS:
  577 #ifdef __amd64__
  578                 case SHT_AMD64_UNWIND:
  579 #endif
  580                         ef->nprogtab++;
  581                         break;
  582                 case SHT_SYMTAB:
  583                         nsym++;
  584                         symtabindex = i;
  585                         symstrindex = shdr[i].sh_link;
  586                         break;
  587                 case SHT_REL:
  588                         ef->nreltab++;
  589                         break;
  590                 case SHT_RELA:
  591                         ef->nrelatab++;
  592                         break;
  593                 case SHT_STRTAB:
  594                         break;
  595                 }
  596         }
  597         if (ef->nprogtab == 0) {
  598                 link_elf_error(filename, "file has no contents");
  599                 error = ENOEXEC;
  600                 goto out;
  601         }
  602         if (nsym != 1) {
  603                 /* Only allow one symbol table for now */
  604                 link_elf_error(filename, "file has no valid symbol table");
  605                 error = ENOEXEC;
  606                 goto out;
  607         }
  608         if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
  609             shdr[symstrindex].sh_type != SHT_STRTAB) {
  610                 link_elf_error(filename, "file has invalid symbol strings");
  611                 error = ENOEXEC;
  612                 goto out;
  613         }
  614 
  615         /* Allocate space for tracking the load chunks */
  616         if (ef->nprogtab != 0)
  617                 ef->progtab = malloc(ef->nprogtab * sizeof(*ef->progtab),
  618                     M_LINKER, M_WAITOK | M_ZERO);
  619         if (ef->nreltab != 0)
  620                 ef->reltab = malloc(ef->nreltab * sizeof(*ef->reltab),
  621                     M_LINKER, M_WAITOK | M_ZERO);
  622         if (ef->nrelatab != 0)
  623                 ef->relatab = malloc(ef->nrelatab * sizeof(*ef->relatab),
  624                     M_LINKER, M_WAITOK | M_ZERO);
  625 
  626         if (symtabindex == -1) {
  627                 link_elf_error(filename, "lost symbol table index");
  628                 error = ENOEXEC;
  629                 goto out;
  630         }
  631         /* Allocate space for and load the symbol table */
  632         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
  633         ef->ddbsymtab = malloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
  634         error = vn_rdwr(UIO_READ, nd.ni_vp, (void *)ef->ddbsymtab,
  635             shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
  636             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  637             &resid, td);
  638         if (error)
  639                 goto out;
  640         if (resid != 0){
  641                 error = EINVAL;
  642                 goto out;
  643         }
  644 
  645         if (symstrindex == -1) {
  646                 link_elf_error(filename, "lost symbol string index");
  647                 error = ENOEXEC;
  648                 goto out;
  649         }
  650         /* Allocate space for and load the symbol strings */
  651         ef->ddbstrcnt = shdr[symstrindex].sh_size;
  652         ef->ddbstrtab = malloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
  653         error = vn_rdwr(UIO_READ, nd.ni_vp, ef->ddbstrtab,
  654             shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
  655             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  656             &resid, td);
  657         if (error)
  658                 goto out;
  659         if (resid != 0){
  660                 error = EINVAL;
  661                 goto out;
  662         }
  663 
  664         /* Do we have a string table for the section names?  */
  665         shstrindex = -1;
  666         if (hdr->e_shstrndx != 0 &&
  667             shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
  668                 shstrindex = hdr->e_shstrndx;
  669                 ef->shstrcnt = shdr[shstrindex].sh_size;
  670                 ef->shstrtab = malloc(shdr[shstrindex].sh_size, M_LINKER,
  671                     M_WAITOK);
  672                 error = vn_rdwr(UIO_READ, nd.ni_vp, ef->shstrtab,
  673                     shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
  674                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  675                     &resid, td);
  676                 if (error)
  677                         goto out;
  678                 if (resid != 0){
  679                         error = EINVAL;
  680                         goto out;
  681                 }
  682         }
  683 
  684         /* Size up code/data(progbits) and bss(nobits). */
  685         alignmask = 0;
  686         for (i = 0; i < hdr->e_shnum; i++) {
  687                 if (shdr[i].sh_size == 0)
  688                         continue;
  689                 switch (shdr[i].sh_type) {
  690                 case SHT_PROGBITS:
  691                 case SHT_NOBITS:
  692 #ifdef __amd64__
  693                 case SHT_AMD64_UNWIND:
  694 #endif
  695                         alignmask = shdr[i].sh_addralign - 1;
  696                         mapsize += alignmask;
  697                         mapsize &= ~alignmask;
  698                         mapsize += shdr[i].sh_size;
  699                         break;
  700                 }
  701         }
  702 
  703         /*
  704          * We know how much space we need for the text/data/bss/etc.
  705          * This stuff needs to be in a single chunk so that profiling etc
  706          * can get the bounds and gdb can associate offsets with modules
  707          */
  708         ef->object = vm_object_allocate(OBJT_DEFAULT,
  709             round_page(mapsize) >> PAGE_SHIFT);
  710         if (ef->object == NULL) {
  711                 error = ENOMEM;
  712                 goto out;
  713         }
  714         ef->address = (caddr_t) vm_map_min(kernel_map);
  715 
  716         /*
  717          * In order to satisfy amd64's architectural requirements on the
  718          * location of code and data in the kernel's address space, request a
  719          * mapping that is above the kernel.  
  720          */
  721 #ifdef __amd64__
  722         mapbase = KERNBASE;
  723 #else
  724         mapbase = VM_MIN_KERNEL_ADDRESS;
  725 #endif
  726         error = vm_map_find(kernel_map, ef->object, 0, &mapbase,
  727             round_page(mapsize), 0, VMFS_OPTIMAL_SPACE, VM_PROT_ALL,
  728             VM_PROT_ALL, 0);
  729         if (error) {
  730                 vm_object_deallocate(ef->object);
  731                 ef->object = 0;
  732                 goto out;
  733         }
  734 
  735         /* Wire the pages */
  736         error = vm_map_wire(kernel_map, mapbase,
  737             mapbase + round_page(mapsize),
  738             VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
  739         if (error != KERN_SUCCESS) {
  740                 error = ENOMEM;
  741                 goto out;
  742         }
  743 
  744         /* Inform the kld system about the situation */
  745         lf->address = ef->address = (caddr_t)mapbase;
  746         lf->size = mapsize;
  747 
  748         /*
  749          * Now load code/data(progbits), zero bss(nobits), allocate space for
  750          * and load relocs
  751          */
  752         pb = 0;
  753         rl = 0;
  754         ra = 0;
  755         alignmask = 0;
  756         for (i = 0; i < hdr->e_shnum; i++) {
  757                 if (shdr[i].sh_size == 0)
  758                         continue;
  759                 switch (shdr[i].sh_type) {
  760                 case SHT_PROGBITS:
  761                 case SHT_NOBITS:
  762 #ifdef __amd64__
  763                 case SHT_AMD64_UNWIND:
  764 #endif
  765                         alignmask = shdr[i].sh_addralign - 1;
  766                         mapbase += alignmask;
  767                         mapbase &= ~alignmask;
  768                         if (ef->shstrtab && shdr[i].sh_name != 0)
  769                                 ef->progtab[pb].name =
  770                                     ef->shstrtab + shdr[i].sh_name;
  771                         else if (shdr[i].sh_type == SHT_PROGBITS)
  772                                 ef->progtab[pb].name = "<<PROGBITS>>";
  773 #ifdef __amd64__
  774                         else if (shdr[i].sh_type == SHT_AMD64_UNWIND)
  775                                 ef->progtab[pb].name = "<<UNWIND>>";
  776 #endif
  777                         else
  778                                 ef->progtab[pb].name = "<<NOBITS>>";
  779                         if (ef->progtab[pb].name != NULL && 
  780                             !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
  781                                 ef->progtab[pb].addr =
  782                                     dpcpu_alloc(shdr[i].sh_size);
  783 #ifdef VIMAGE
  784                         else if (ef->progtab[pb].name != NULL &&
  785                             !strcmp(ef->progtab[pb].name, VNET_SETNAME))
  786                                 ef->progtab[pb].addr =
  787                                     vnet_data_alloc(shdr[i].sh_size);
  788 #endif
  789                         else
  790                                 ef->progtab[pb].addr =
  791                                     (void *)(uintptr_t)mapbase;
  792                         if (ef->progtab[pb].addr == NULL) {
  793                                 error = ENOSPC;
  794                                 goto out;
  795                         }
  796                         ef->progtab[pb].size = shdr[i].sh_size;
  797                         ef->progtab[pb].sec = i;
  798                         if (shdr[i].sh_type == SHT_PROGBITS
  799 #ifdef __amd64__
  800                             || shdr[i].sh_type == SHT_AMD64_UNWIND
  801 #endif
  802                             ) {
  803                                 error = vn_rdwr(UIO_READ, nd.ni_vp,
  804                                     ef->progtab[pb].addr,
  805                                     shdr[i].sh_size, shdr[i].sh_offset,
  806                                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
  807                                     NOCRED, &resid, td);
  808                                 if (error)
  809                                         goto out;
  810                                 if (resid != 0){
  811                                         error = EINVAL;
  812                                         goto out;
  813                                 }
  814                                 /* Initialize the per-cpu or vnet area. */
  815                                 if (ef->progtab[pb].addr != (void *)mapbase &&
  816                                     !strcmp(ef->progtab[pb].name, DPCPU_SETNAME))
  817                                         dpcpu_copy(ef->progtab[pb].addr,
  818                                             shdr[i].sh_size);
  819 #ifdef VIMAGE
  820                                 else if (ef->progtab[pb].addr !=
  821                                     (void *)mapbase &&
  822                                     !strcmp(ef->progtab[pb].name, VNET_SETNAME))
  823                                         vnet_data_copy(ef->progtab[pb].addr,
  824                                             shdr[i].sh_size);
  825 #endif
  826                         } else
  827                                 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
  828 
  829                         /* Update all symbol values with the offset. */
  830                         for (j = 0; j < ef->ddbsymcnt; j++) {
  831                                 es = &ef->ddbsymtab[j];
  832                                 if (es->st_shndx != i)
  833                                         continue;
  834                                 es->st_value += (Elf_Addr)ef->progtab[pb].addr;
  835                         }
  836                         mapbase += shdr[i].sh_size;
  837                         pb++;
  838                         break;
  839                 case SHT_REL:
  840                         ef->reltab[rl].rel = malloc(shdr[i].sh_size, M_LINKER,
  841                             M_WAITOK);
  842                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
  843                         ef->reltab[rl].sec = shdr[i].sh_info;
  844                         error = vn_rdwr(UIO_READ, nd.ni_vp,
  845                             (void *)ef->reltab[rl].rel,
  846                             shdr[i].sh_size, shdr[i].sh_offset,
  847                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  848                             &resid, td);
  849                         if (error)
  850                                 goto out;
  851                         if (resid != 0){
  852                                 error = EINVAL;
  853                                 goto out;
  854                         }
  855                         rl++;
  856                         break;
  857                 case SHT_RELA:
  858                         ef->relatab[ra].rela = malloc(shdr[i].sh_size, M_LINKER,
  859                             M_WAITOK);
  860                         ef->relatab[ra].nrela =
  861                             shdr[i].sh_size / sizeof(Elf_Rela);
  862                         ef->relatab[ra].sec = shdr[i].sh_info;
  863                         error = vn_rdwr(UIO_READ, nd.ni_vp,
  864                             (void *)ef->relatab[ra].rela,
  865                             shdr[i].sh_size, shdr[i].sh_offset,
  866                             UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  867                             &resid, td);
  868                         if (error)
  869                                 goto out;
  870                         if (resid != 0){
  871                                 error = EINVAL;
  872                                 goto out;
  873                         }
  874                         ra++;
  875                         break;
  876                 }
  877         }
  878         if (pb != ef->nprogtab) {
  879                 link_elf_error(filename, "lost progbits");
  880                 error = ENOEXEC;
  881                 goto out;
  882         }
  883         if (rl != ef->nreltab) {
  884                 link_elf_error(filename, "lost reltab");
  885                 error = ENOEXEC;
  886                 goto out;
  887         }
  888         if (ra != ef->nrelatab) {
  889                 link_elf_error(filename, "lost relatab");
  890                 error = ENOEXEC;
  891                 goto out;
  892         }
  893         if (mapbase != (vm_offset_t)ef->address + mapsize) {
  894                 printf(
  895                     "%s: mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
  896                     filename != NULL ? filename : "<none>",
  897                     (u_long)mapbase, ef->address, (u_long)mapsize,
  898                     (u_long)(vm_offset_t)ef->address + mapsize);
  899                 error = ENOMEM;
  900                 goto out;
  901         }
  902 
  903         /* Local intra-module relocations */
  904         error = link_elf_reloc_local(lf);
  905         if (error != 0)
  906                 goto out;
  907 
  908         /* Pull in dependencies */
  909         VOP_UNLOCK(nd.ni_vp, 0);
  910         error = linker_load_dependencies(lf);
  911         vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
  912         if (error)
  913                 goto out;
  914 
  915         /* External relocations */
  916         error = relocate_file(ef);
  917         if (error)
  918                 goto out;
  919 
  920         /* Notify MD code that a module is being loaded. */
  921         error = elf_cpu_load_file(lf);
  922         if (error)
  923                 goto out;
  924 
  925         *result = lf;
  926 
  927 out:
  928         VOP_UNLOCK(nd.ni_vp, 0);
  929         vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
  930         if (error && lf)
  931                 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
  932         free(hdr, M_LINKER);
  933 
  934         return error;
  935 }
  936 
  937 static void
  938 link_elf_unload_file(linker_file_t file)
  939 {
  940         elf_file_t ef = (elf_file_t) file;
  941         int i;
  942 
  943         /* Notify MD code that a module is being unloaded. */
  944         elf_cpu_unload_file(file);
  945 
  946         if (ef->progtab) {
  947                 for (i = 0; i < ef->nprogtab; i++) {
  948                         if (ef->progtab[i].size == 0)
  949                                 continue;
  950                         if (ef->progtab[i].name == NULL)
  951                                 continue;
  952                         if (!strcmp(ef->progtab[i].name, DPCPU_SETNAME))
  953                                 dpcpu_free(ef->progtab[i].addr,
  954                                     ef->progtab[i].size);
  955 #ifdef VIMAGE
  956                         else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
  957                                 vnet_data_free(ef->progtab[i].addr,
  958                                     ef->progtab[i].size);
  959 #endif
  960                 }
  961         }
  962         if (ef->preloaded) {
  963                 free(ef->reltab, M_LINKER);
  964                 free(ef->relatab, M_LINKER);
  965                 free(ef->progtab, M_LINKER);
  966                 free(ef->ctftab, M_LINKER);
  967                 free(ef->ctfoff, M_LINKER);
  968                 free(ef->typoff, M_LINKER);
  969                 if (file->filename != NULL)
  970                         preload_delete_name(file->filename);
  971                 /* XXX reclaim module memory? */
  972                 return;
  973         }
  974 
  975         for (i = 0; i < ef->nreltab; i++)
  976                 free(ef->reltab[i].rel, M_LINKER);
  977         for (i = 0; i < ef->nrelatab; i++)
  978                 free(ef->relatab[i].rela, M_LINKER);
  979         free(ef->reltab, M_LINKER);
  980         free(ef->relatab, M_LINKER);
  981         free(ef->progtab, M_LINKER);
  982 
  983         if (ef->object) {
  984                 vm_map_remove(kernel_map, (vm_offset_t) ef->address,
  985                     (vm_offset_t) ef->address +
  986                     (ef->object->size << PAGE_SHIFT));
  987         }
  988         free(ef->e_shdr, M_LINKER);
  989         free(ef->ddbsymtab, M_LINKER);
  990         free(ef->ddbstrtab, M_LINKER);
  991         free(ef->shstrtab, M_LINKER);
  992         free(ef->ctftab, M_LINKER);
  993         free(ef->ctfoff, M_LINKER);
  994         free(ef->typoff, M_LINKER);
  995 }
  996 
  997 static const char *
  998 symbol_name(elf_file_t ef, Elf_Size r_info)
  999 {
 1000         const Elf_Sym *ref;
 1001 
 1002         if (ELF_R_SYM(r_info)) {
 1003                 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
 1004                 return ef->ddbstrtab + ref->st_name;
 1005         } else
 1006                 return NULL;
 1007 }
 1008 
 1009 static Elf_Addr
 1010 findbase(elf_file_t ef, int sec)
 1011 {
 1012         int i;
 1013         Elf_Addr base = 0;
 1014 
 1015         for (i = 0; i < ef->nprogtab; i++) {
 1016                 if (sec == ef->progtab[i].sec) {
 1017                         base = (Elf_Addr)ef->progtab[i].addr;
 1018                         break;
 1019                 }
 1020         }
 1021         return base;
 1022 }
 1023 
 1024 static int
 1025 relocate_file(elf_file_t ef)
 1026 {
 1027         const Elf_Rel *rellim;
 1028         const Elf_Rel *rel;
 1029         const Elf_Rela *relalim;
 1030         const Elf_Rela *rela;
 1031         const char *symname;
 1032         const Elf_Sym *sym;
 1033         int i;
 1034         Elf_Size symidx;
 1035         Elf_Addr base;
 1036 
 1037 
 1038         /* Perform relocations without addend if there are any: */
 1039         for (i = 0; i < ef->nreltab; i++) {
 1040                 rel = ef->reltab[i].rel;
 1041                 if (rel == NULL) {
 1042                         link_elf_error(ef->lf.filename, "lost a reltab!");
 1043                         return (ENOEXEC);
 1044                 }
 1045                 rellim = rel + ef->reltab[i].nrel;
 1046                 base = findbase(ef, ef->reltab[i].sec);
 1047                 if (base == 0) {
 1048                         link_elf_error(ef->lf.filename, "lost base for reltab");
 1049                         return (ENOEXEC);
 1050                 }
 1051                 for ( ; rel < rellim; rel++) {
 1052                         symidx = ELF_R_SYM(rel->r_info);
 1053                         if (symidx >= ef->ddbsymcnt)
 1054                                 continue;
 1055                         sym = ef->ddbsymtab + symidx;
 1056                         /* Local relocs are already done */
 1057                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
 1058                                 continue;
 1059                         if (elf_reloc(&ef->lf, base, rel, ELF_RELOC_REL,
 1060                             elf_obj_lookup)) {
 1061                                 symname = symbol_name(ef, rel->r_info);
 1062                                 printf("link_elf_obj: symbol %s undefined\n",
 1063                                     symname);
 1064                                 return (ENOENT);
 1065                         }
 1066                 }
 1067         }
 1068 
 1069         /* Perform relocations with addend if there are any: */
 1070         for (i = 0; i < ef->nrelatab; i++) {
 1071                 rela = ef->relatab[i].rela;
 1072                 if (rela == NULL) {
 1073                         link_elf_error(ef->lf.filename, "lost a relatab!");
 1074                         return (ENOEXEC);
 1075                 }
 1076                 relalim = rela + ef->relatab[i].nrela;
 1077                 base = findbase(ef, ef->relatab[i].sec);
 1078                 if (base == 0) {
 1079                         link_elf_error(ef->lf.filename,
 1080                             "lost base for relatab");
 1081                         return (ENOEXEC);
 1082                 }
 1083                 for ( ; rela < relalim; rela++) {
 1084                         symidx = ELF_R_SYM(rela->r_info);
 1085                         if (symidx >= ef->ddbsymcnt)
 1086                                 continue;
 1087                         sym = ef->ddbsymtab + symidx;
 1088                         /* Local relocs are already done */
 1089                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
 1090                                 continue;
 1091                         if (elf_reloc(&ef->lf, base, rela, ELF_RELOC_RELA,
 1092                             elf_obj_lookup)) {
 1093                                 symname = symbol_name(ef, rela->r_info);
 1094                                 printf("link_elf_obj: symbol %s undefined\n",
 1095                                     symname);
 1096                                 return (ENOENT);
 1097                         }
 1098                 }
 1099         }
 1100 
 1101         /*
 1102          * Only clean SHN_FBSD_CACHED for successful return.  If we
 1103          * modified symbol table for the object but found an
 1104          * unresolved symbol, there is no reason to roll back.
 1105          */
 1106         elf_obj_cleanup_globals_cache(ef);
 1107 
 1108         return (0);
 1109 }
 1110 
 1111 static int
 1112 link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
 1113 {
 1114         elf_file_t ef = (elf_file_t) lf;
 1115         const Elf_Sym *symp;
 1116         const char *strp;
 1117         int i;
 1118 
 1119         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1120                 strp = ef->ddbstrtab + symp->st_name;
 1121                 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
 1122                         *sym = (c_linker_sym_t) symp;
 1123                         return 0;
 1124                 }
 1125         }
 1126         return ENOENT;
 1127 }
 1128 
 1129 static int
 1130 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
 1131     linker_symval_t *symval)
 1132 {
 1133         elf_file_t ef = (elf_file_t) lf;
 1134         const Elf_Sym *es = (const Elf_Sym*) sym;
 1135 
 1136         if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
 1137                 symval->name = ef->ddbstrtab + es->st_name;
 1138                 symval->value = (caddr_t)es->st_value;
 1139                 symval->size = es->st_size;
 1140                 return 0;
 1141         }
 1142         return ENOENT;
 1143 }
 1144 
 1145 static int
 1146 link_elf_search_symbol(linker_file_t lf, caddr_t value,
 1147     c_linker_sym_t *sym, long *diffp)
 1148 {
 1149         elf_file_t ef = (elf_file_t) lf;
 1150         u_long off = (uintptr_t) (void *) value;
 1151         u_long diff = off;
 1152         u_long st_value;
 1153         const Elf_Sym *es;
 1154         const Elf_Sym *best = 0;
 1155         int i;
 1156 
 1157         for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
 1158                 if (es->st_name == 0)
 1159                         continue;
 1160                 st_value = es->st_value;
 1161                 if (off >= st_value) {
 1162                         if (off - st_value < diff) {
 1163                                 diff = off - st_value;
 1164                                 best = es;
 1165                                 if (diff == 0)
 1166                                         break;
 1167                         } else if (off - st_value == diff) {
 1168                                 best = es;
 1169                         }
 1170                 }
 1171         }
 1172         if (best == 0)
 1173                 *diffp = off;
 1174         else
 1175                 *diffp = diff;
 1176         *sym = (c_linker_sym_t) best;
 1177 
 1178         return 0;
 1179 }
 1180 
 1181 /*
 1182  * Look up a linker set on an ELF system.
 1183  */
 1184 static int
 1185 link_elf_lookup_set(linker_file_t lf, const char *name,
 1186     void ***startp, void ***stopp, int *countp)
 1187 {
 1188         elf_file_t ef = (elf_file_t)lf;
 1189         void **start, **stop;
 1190         int i, count;
 1191 
 1192         /* Relative to section number */
 1193         for (i = 0; i < ef->nprogtab; i++) {
 1194                 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
 1195                     strcmp(ef->progtab[i].name + 4, name) == 0) {
 1196                         start  = (void **)ef->progtab[i].addr;
 1197                         stop = (void **)((char *)ef->progtab[i].addr +
 1198                             ef->progtab[i].size);
 1199                         count = stop - start;
 1200                         if (startp)
 1201                                 *startp = start;
 1202                         if (stopp)
 1203                                 *stopp = stop;
 1204                         if (countp)
 1205                                 *countp = count;
 1206                         return (0);
 1207                 }
 1208         }
 1209         return (ESRCH);
 1210 }
 1211 
 1212 static int
 1213 link_elf_each_function_name(linker_file_t file,
 1214     int (*callback)(const char *, void *), void *opaque)
 1215 {
 1216         elf_file_t ef = (elf_file_t)file;
 1217         const Elf_Sym *symp;
 1218         int i, error;
 1219         
 1220         /* Exhaustive search */
 1221         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1222                 if (symp->st_value != 0 &&
 1223                     ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
 1224                         error = callback(ef->ddbstrtab + symp->st_name, opaque);
 1225                         if (error)
 1226                                 return (error);
 1227                 }
 1228         }
 1229         return (0);
 1230 }
 1231 
 1232 static int
 1233 link_elf_each_function_nameval(linker_file_t file,
 1234     linker_function_nameval_callback_t callback, void *opaque)
 1235 {
 1236         linker_symval_t symval;
 1237         elf_file_t ef = (elf_file_t)file;
 1238         const Elf_Sym* symp;
 1239         int i, error;
 1240 
 1241         /* Exhaustive search */
 1242         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1243                 if (symp->st_value != 0 &&
 1244                     ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
 1245                         error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
 1246                         if (error)
 1247                                 return (error);
 1248                         error = callback(file, i, &symval, opaque);
 1249                         if (error)
 1250                                 return (error);
 1251                 }
 1252         }
 1253         return (0);
 1254 }
 1255 
 1256 static void
 1257 elf_obj_cleanup_globals_cache(elf_file_t ef)
 1258 {
 1259         Elf_Sym *sym;
 1260         Elf_Size i;
 1261 
 1262         for (i = 0; i < ef->ddbsymcnt; i++) {
 1263                 sym = ef->ddbsymtab + i;
 1264                 if (sym->st_shndx == SHN_FBSD_CACHED) {
 1265                         sym->st_shndx = SHN_UNDEF;
 1266                         sym->st_value = 0;
 1267                 }
 1268         }
 1269 }
 1270 
 1271 /*
 1272  * Symbol lookup function that can be used when the symbol index is known (ie
 1273  * in relocations). It uses the symbol index instead of doing a fully fledged
 1274  * hash table based lookup when such is valid. For example for local symbols.
 1275  * This is not only more efficient, it's also more correct. It's not always
 1276  * the case that the symbol can be found through the hash table.
 1277  */
 1278 static int
 1279 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
 1280 {
 1281         elf_file_t ef = (elf_file_t)lf;
 1282         Elf_Sym *sym;
 1283         const char *symbol;
 1284         Elf_Addr res1;
 1285 
 1286         /* Don't even try to lookup the symbol if the index is bogus. */
 1287         if (symidx >= ef->ddbsymcnt) {
 1288                 *res = 0;
 1289                 return (EINVAL);
 1290         }
 1291 
 1292         sym = ef->ddbsymtab + symidx;
 1293 
 1294         /* Quick answer if there is a definition included. */
 1295         if (sym->st_shndx != SHN_UNDEF) {
 1296                 *res = sym->st_value;
 1297                 return (0);
 1298         }
 1299 
 1300         /* If we get here, then it is undefined and needs a lookup. */
 1301         switch (ELF_ST_BIND(sym->st_info)) {
 1302         case STB_LOCAL:
 1303                 /* Local, but undefined? huh? */
 1304                 *res = 0;
 1305                 return (EINVAL);
 1306 
 1307         case STB_GLOBAL:
 1308         case STB_WEAK:
 1309                 /* Relative to Data or Function name */
 1310                 symbol = ef->ddbstrtab + sym->st_name;
 1311 
 1312                 /* Force a lookup failure if the symbol name is bogus. */
 1313                 if (*symbol == 0) {
 1314                         *res = 0;
 1315                         return (EINVAL);
 1316                 }
 1317                 res1 = (Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps);
 1318 
 1319                 /*
 1320                  * Cache global lookups during module relocation. The failure
 1321                  * case is particularly expensive for callers, who must scan
 1322                  * through the entire globals table doing strcmp(). Cache to
 1323                  * avoid doing such work repeatedly.
 1324                  *
 1325                  * After relocation is complete, undefined globals will be
 1326                  * restored to SHN_UNDEF in elf_obj_cleanup_globals_cache(),
 1327                  * above.
 1328                  */
 1329                 if (res1 != 0) {
 1330                         sym->st_shndx = SHN_FBSD_CACHED;
 1331                         sym->st_value = res1;
 1332                         *res = res1;
 1333                         return (0);
 1334                 } else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
 1335                         sym->st_value = 0;
 1336                         *res = 0;
 1337                         return (0);
 1338                 }
 1339                 return (EINVAL);
 1340 
 1341         default:
 1342                 return (EINVAL);
 1343         }
 1344 }
 1345 
 1346 static void
 1347 link_elf_fix_link_set(elf_file_t ef)
 1348 {
 1349         static const char startn[] = "__start_";
 1350         static const char stopn[] = "__stop_";
 1351         Elf_Sym *sym;
 1352         const char *sym_name, *linkset_name;
 1353         Elf_Addr startp, stopp;
 1354         Elf_Size symidx;
 1355         int start, i;
 1356 
 1357         startp = stopp = 0;
 1358         for (symidx = 1 /* zero entry is special */;
 1359                 symidx < ef->ddbsymcnt; symidx++) {
 1360                 sym = ef->ddbsymtab + symidx;
 1361                 if (sym->st_shndx != SHN_UNDEF)
 1362                         continue;
 1363 
 1364                 sym_name = ef->ddbstrtab + sym->st_name;
 1365                 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
 1366                         start = 1;
 1367                         linkset_name = sym_name + sizeof(startn) - 1;
 1368                 }
 1369                 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
 1370                         start = 0;
 1371                         linkset_name = sym_name + sizeof(stopn) - 1;
 1372                 }
 1373                 else
 1374                         continue;
 1375 
 1376                 for (i = 0; i < ef->nprogtab; i++) {
 1377                         if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
 1378                                 startp = (Elf_Addr)ef->progtab[i].addr;
 1379                                 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
 1380                                 break;
 1381                         }
 1382                 }
 1383                 if (i == ef->nprogtab)
 1384                         continue;
 1385 
 1386                 sym->st_value = start ? startp : stopp;
 1387                 sym->st_shndx = i;
 1388         }
 1389 }
 1390 
 1391 static int
 1392 link_elf_reloc_local(linker_file_t lf)
 1393 {
 1394         elf_file_t ef = (elf_file_t)lf;
 1395         const Elf_Rel *rellim;
 1396         const Elf_Rel *rel;
 1397         const Elf_Rela *relalim;
 1398         const Elf_Rela *rela;
 1399         const Elf_Sym *sym;
 1400         Elf_Addr base;
 1401         int i;
 1402         Elf_Size symidx;
 1403 
 1404         link_elf_fix_link_set(ef);
 1405 
 1406         /* Perform relocations without addend if there are any: */
 1407         for (i = 0; i < ef->nreltab; i++) {
 1408                 rel = ef->reltab[i].rel;
 1409                 if (rel == NULL) {
 1410                         link_elf_error(ef->lf.filename, "lost a reltab");
 1411                         return (ENOEXEC);
 1412                 }
 1413                 rellim = rel + ef->reltab[i].nrel;
 1414                 base = findbase(ef, ef->reltab[i].sec);
 1415                 if (base == 0) {
 1416                         link_elf_error(ef->lf.filename, "lost base for reltab");
 1417                         return (ENOEXEC);
 1418                 }
 1419                 for ( ; rel < rellim; rel++) {
 1420                         symidx = ELF_R_SYM(rel->r_info);
 1421                         if (symidx >= ef->ddbsymcnt)
 1422                                 continue;
 1423                         sym = ef->ddbsymtab + symidx;
 1424                         /* Only do local relocs */
 1425                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
 1426                                 continue;
 1427                         elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
 1428                             elf_obj_lookup);
 1429                 }
 1430         }
 1431 
 1432         /* Perform relocations with addend if there are any: */
 1433         for (i = 0; i < ef->nrelatab; i++) {
 1434                 rela = ef->relatab[i].rela;
 1435                 if (rela == NULL) {
 1436                         link_elf_error(ef->lf.filename, "lost a relatab!");
 1437                         return (ENOEXEC);
 1438                 }
 1439                 relalim = rela + ef->relatab[i].nrela;
 1440                 base = findbase(ef, ef->relatab[i].sec);
 1441                 if (base == 0) {
 1442                         link_elf_error(ef->lf.filename, "lost base for reltab");
 1443                         return (ENOEXEC);
 1444                 }
 1445                 for ( ; rela < relalim; rela++) {
 1446                         symidx = ELF_R_SYM(rela->r_info);
 1447                         if (symidx >= ef->ddbsymcnt)
 1448                                 continue;
 1449                         sym = ef->ddbsymtab + symidx;
 1450                         /* Only do local relocs */
 1451                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
 1452                                 continue;
 1453                         elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
 1454                             elf_obj_lookup);
 1455                 }
 1456         }
 1457         return (0);
 1458 }
 1459 
 1460 static long
 1461 link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
 1462 {
 1463     elf_file_t ef = (elf_file_t)lf;
 1464     
 1465     *symtab = ef->ddbsymtab;
 1466     
 1467     if (*symtab == NULL)
 1468         return (0);
 1469 
 1470     return (ef->ddbsymcnt);
 1471 }
 1472     
 1473 static long
 1474 link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
 1475 {
 1476     elf_file_t ef = (elf_file_t)lf;
 1477 
 1478     *strtab = ef->ddbstrtab;
 1479 
 1480     if (*strtab == NULL)
 1481         return (0);
 1482 
 1483     return (ef->ddbstrcnt);
 1484 }

Cache object: a95ecc40ed0e9c6fec948fe53b8db5e2


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