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

Cache object: 425c19f514157564fcdbb41836766965


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