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.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  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_gdb.h"
   31 #include "opt_mac.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #ifdef GPROF
   36 #include <sys/gmon.h>
   37 #endif
   38 #include <sys/kernel.h>
   39 #include <sys/lock.h>
   40 #include <sys/mac.h>
   41 #include <sys/malloc.h>
   42 #include <sys/mutex.h>
   43 #include <sys/proc.h>
   44 #include <sys/namei.h>
   45 #include <sys/fcntl.h>
   46 #include <sys/vnode.h>
   47 #include <sys/linker.h>
   48 
   49 #include <machine/elf.h>
   50 
   51 #include <vm/vm.h>
   52 #include <vm/vm_param.h>
   53 #ifdef SPARSE_MAPPING
   54 #include <vm/vm_object.h>
   55 #include <vm/vm_kern.h>
   56 #include <vm/vm_extern.h>
   57 #endif
   58 #include <vm/pmap.h>
   59 #include <vm/vm_map.h>
   60 
   61 #include <sys/link_elf.h>
   62 
   63 #include "linker_if.h"
   64 
   65 typedef struct elf_file {
   66     struct linker_file  lf;             /* Common fields */
   67     int                 preloaded;      /* Was file pre-loaded */
   68     caddr_t             address;        /* Relocation address */
   69 #ifdef SPARSE_MAPPING
   70     vm_object_t         object;         /* VM object to hold file pages */
   71 #endif
   72     Elf_Dyn*            dynamic;        /* Symbol table etc. */
   73     Elf_Hashelt         nbuckets;       /* DT_HASH info */
   74     Elf_Hashelt         nchains;
   75     const Elf_Hashelt*  buckets;
   76     const Elf_Hashelt*  chains;
   77     caddr_t             hash;
   78     caddr_t             strtab;         /* DT_STRTAB */
   79     int                 strsz;          /* DT_STRSZ */
   80     const Elf_Sym*      symtab;         /* DT_SYMTAB */
   81     Elf_Addr*           got;            /* DT_PLTGOT */
   82     const Elf_Rel*      pltrel;         /* DT_JMPREL */
   83     int                 pltrelsize;     /* DT_PLTRELSZ */
   84     const Elf_Rela*     pltrela;        /* DT_JMPREL */
   85     int                 pltrelasize;    /* DT_PLTRELSZ */
   86     const Elf_Rel*      rel;            /* DT_REL */
   87     int                 relsize;        /* DT_RELSZ */
   88     const Elf_Rela*     rela;           /* DT_RELA */
   89     int                 relasize;       /* DT_RELASZ */
   90     caddr_t             modptr;
   91     const Elf_Sym*      ddbsymtab;      /* The symbol table we are using */
   92     long                ddbsymcnt;      /* Number of symbols */
   93     caddr_t             ddbstrtab;      /* String table */
   94     long                ddbstrcnt;      /* number of bytes in string table */
   95     caddr_t             symbase;        /* malloc'ed symbold base */
   96     caddr_t             strbase;        /* malloc'ed string base */
   97 #ifdef GDB
   98     struct link_map     gdb;            /* hooks for gdb */
   99 #endif
  100 } *elf_file_t;
  101 
  102 static int      link_elf_link_common_finish(linker_file_t);
  103 static int      link_elf_link_preload(linker_class_t cls,
  104                                       const char*, linker_file_t*);
  105 static int      link_elf_link_preload_finish(linker_file_t);
  106 static int      link_elf_load_file(linker_class_t, const char*, linker_file_t*);
  107 static int      link_elf_lookup_symbol(linker_file_t, const char*,
  108                                        c_linker_sym_t*);
  109 static int      link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
  110 static int      link_elf_search_symbol(linker_file_t, caddr_t value,
  111                                        c_linker_sym_t* sym, long* diffp);
  112 
  113 static void     link_elf_unload_file(linker_file_t);
  114 static void     link_elf_unload_preload(linker_file_t);
  115 static int      link_elf_lookup_set(linker_file_t, const char *,
  116                                     void ***, void ***, int *);
  117 static int      link_elf_each_function_name(linker_file_t,
  118                                 int (*)(const char *, void *),
  119                                 void *);
  120 static void     link_elf_reloc_local(linker_file_t);
  121 static Elf_Addr elf_lookup(linker_file_t lf, Elf_Word symidx, int deps);
  122 
  123 static kobj_method_t link_elf_methods[] = {
  124     KOBJMETHOD(linker_lookup_symbol,    link_elf_lookup_symbol),
  125     KOBJMETHOD(linker_symbol_values,    link_elf_symbol_values),
  126     KOBJMETHOD(linker_search_symbol,    link_elf_search_symbol),
  127     KOBJMETHOD(linker_unload,           link_elf_unload_file),
  128     KOBJMETHOD(linker_load_file,        link_elf_load_file),
  129     KOBJMETHOD(linker_link_preload,     link_elf_link_preload),
  130     KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
  131     KOBJMETHOD(linker_lookup_set,       link_elf_lookup_set),
  132     KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
  133     { 0, 0 }
  134 };
  135 
  136 static struct linker_class link_elf_class = {
  137 #if ELF_TARG_CLASS == ELFCLASS32
  138     "elf32",
  139 #else
  140     "elf64",
  141 #endif
  142     link_elf_methods, sizeof(struct elf_file)
  143 };
  144 
  145 static int              parse_dynamic(elf_file_t ef);
  146 static int              relocate_file(elf_file_t ef);
  147 static int              link_elf_preload_parse_symbols(elf_file_t ef);
  148 
  149 #ifdef GDB
  150 static void             r_debug_state(struct r_debug *dummy_one,
  151                                       struct link_map *dummy_two);
  152 
  153 /*
  154  * A list of loaded modules for GDB to use for loading symbols.
  155  */
  156 struct r_debug r_debug;
  157 
  158 #define GDB_STATE(s)    r_debug.r_state = s; r_debug_state(NULL, NULL);
  159 
  160 /*
  161  * Function for the debugger to set a breakpoint on to gain control.
  162  */
  163 static void
  164 r_debug_state(struct r_debug *dummy_one __unused,
  165               struct link_map *dummy_two __unused)
  166 {
  167 }
  168 
  169 static void
  170 link_elf_add_gdb(struct link_map *l)
  171 {
  172     struct link_map *prev;
  173 
  174     l->l_next = NULL;
  175 
  176     if (r_debug.r_map == NULL) {
  177         /* Add first. */
  178         l->l_prev = NULL;
  179         r_debug.r_map = l;
  180     } else {
  181         /* Append to list. */
  182         for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
  183             ;
  184         l->l_prev = prev;
  185         prev->l_next = l;
  186     }
  187 }
  188 
  189 static void
  190 link_elf_delete_gdb(struct link_map *l)
  191 {
  192     if (l->l_prev == NULL) {
  193         /* Remove first. */
  194         if ((r_debug.r_map = l->l_next) != NULL)
  195             l->l_next->l_prev = NULL;
  196     } else {
  197         /* Remove any but first. */
  198         if ((l->l_prev->l_next = l->l_next) != NULL)
  199             l->l_next->l_prev = l->l_prev;
  200     }
  201 }
  202 #endif /* GDB */
  203 
  204 #ifdef __ia64__
  205 Elf_Addr link_elf_get_gp(linker_file_t);
  206 #endif
  207 
  208 /*
  209  * The kernel symbol table starts here.
  210  */
  211 extern struct _dynamic _DYNAMIC;
  212 
  213 static void
  214 link_elf_error(const char *s)
  215 {
  216     printf("kldload: %s\n", s);
  217 }
  218 
  219 /*
  220  * Actions performed after linking/loading both the preloaded kernel and any
  221  * modules; whether preloaded or dynamicly loaded.
  222  */
  223 static int
  224 link_elf_link_common_finish(linker_file_t lf)
  225 {
  226 #ifdef GDB
  227     elf_file_t ef = (elf_file_t)lf;
  228     char *newfilename;
  229 #endif
  230     int error;
  231 
  232     /* Notify MD code that a module is being loaded. */
  233     error = elf_cpu_load_file(lf);
  234     if (error)
  235         return (error);
  236 
  237 #ifdef GDB
  238     GDB_STATE(RT_ADD);
  239     ef->gdb.l_addr = lf->address;
  240     newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
  241     strcpy(newfilename, lf->filename);
  242     ef->gdb.l_name = newfilename;
  243     ef->gdb.l_ld = ef->dynamic;
  244     link_elf_add_gdb(&ef->gdb);
  245     GDB_STATE(RT_CONSISTENT);
  246 #endif
  247 
  248     return (0);
  249 }
  250 
  251 static void
  252 link_elf_init(void* arg)
  253 {
  254     Elf_Dyn     *dp;
  255     caddr_t     modptr, baseptr, sizeptr;
  256     elf_file_t  ef;
  257     char        *modname;
  258 
  259     linker_add_class(&link_elf_class);
  260 
  261     dp = (Elf_Dyn*) &_DYNAMIC;
  262     modname = NULL;
  263     modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
  264     if (modptr == NULL)
  265         modptr = preload_search_by_type("elf kernel");
  266     if (modptr)
  267         modname = (char *)preload_search_info(modptr, MODINFO_NAME);
  268     if (modname == NULL)
  269         modname = "kernel";
  270     linker_kernel_file = linker_make_file(modname, &link_elf_class);
  271     if (linker_kernel_file == NULL)
  272         panic("link_elf_init: Can't create linker structures for kernel");
  273 
  274     ef = (elf_file_t) linker_kernel_file;
  275     ef->preloaded = 1;
  276     ef->address = 0;
  277 #ifdef SPARSE_MAPPING
  278     ef->object = 0;
  279 #endif
  280     ef->dynamic = dp;
  281 
  282     if (dp)
  283         parse_dynamic(ef);
  284     linker_kernel_file->address = (caddr_t) KERNBASE;
  285     linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
  286 
  287     if (modptr) {
  288         ef->modptr = modptr;
  289         baseptr = preload_search_info(modptr, MODINFO_ADDR);
  290         if (baseptr)
  291             linker_kernel_file->address = *(caddr_t *)baseptr;
  292         sizeptr = preload_search_info(modptr, MODINFO_SIZE);
  293         if (sizeptr)
  294             linker_kernel_file->size = *(size_t *)sizeptr;
  295     }
  296     (void)link_elf_preload_parse_symbols(ef);
  297 
  298 #ifdef GDB
  299     r_debug.r_map = NULL;
  300     r_debug.r_brk = r_debug_state;
  301     r_debug.r_state = RT_CONSISTENT;
  302 #endif
  303 
  304     (void)link_elf_link_common_finish(linker_kernel_file);
  305 }
  306 
  307 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_SECOND, link_elf_init, 0);
  308 
  309 static int
  310 link_elf_preload_parse_symbols(elf_file_t ef)
  311 {
  312     caddr_t     pointer;
  313     caddr_t     ssym, esym, base;
  314     caddr_t     strtab;
  315     int         strcnt;
  316     Elf_Sym*    symtab;
  317     int         symcnt;
  318 
  319     if (ef->modptr == NULL)
  320         return 0;
  321     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
  322     if (pointer == NULL)
  323         return 0;
  324     ssym = *(caddr_t *)pointer;
  325     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
  326     if (pointer == NULL)
  327         return 0;
  328     esym = *(caddr_t *)pointer;
  329 
  330     base = ssym;
  331 
  332     symcnt = *(long *)base;
  333     base += sizeof(long);
  334     symtab = (Elf_Sym *)base;
  335     base += roundup(symcnt, sizeof(long));
  336 
  337     if (base > esym || base < ssym) {
  338         printf("Symbols are corrupt!\n");
  339         return EINVAL;
  340     }
  341 
  342     strcnt = *(long *)base;
  343     base += sizeof(long);
  344     strtab = base;
  345     base += roundup(strcnt, sizeof(long));
  346 
  347     if (base > esym || base < ssym) {
  348         printf("Symbols are corrupt!\n");
  349         return EINVAL;
  350     }
  351 
  352     ef->ddbsymtab = symtab;
  353     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
  354     ef->ddbstrtab = strtab;
  355     ef->ddbstrcnt = strcnt;
  356 
  357     return 0;
  358 }
  359 
  360 static int
  361 parse_dynamic(elf_file_t ef)
  362 {
  363     Elf_Dyn *dp;
  364     int plttype = DT_REL;
  365 
  366     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
  367         switch (dp->d_tag) {
  368         case DT_HASH:
  369         {
  370             /* From src/libexec/rtld-elf/rtld.c */
  371             const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
  372                 (ef->address + dp->d_un.d_ptr);
  373             ef->nbuckets = hashtab[0];
  374             ef->nchains = hashtab[1];
  375             ef->buckets = hashtab + 2;
  376             ef->chains = ef->buckets + ef->nbuckets;
  377             break;
  378         }
  379         case DT_STRTAB:
  380             ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
  381             break;
  382         case DT_STRSZ:
  383             ef->strsz = dp->d_un.d_val;
  384             break;
  385         case DT_SYMTAB:
  386             ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
  387             break;
  388         case DT_SYMENT:
  389             if (dp->d_un.d_val != sizeof(Elf_Sym))
  390                 return ENOEXEC;
  391             break;
  392         case DT_PLTGOT:
  393             ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
  394             break;
  395         case DT_REL:
  396             ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
  397             break;
  398         case DT_RELSZ:
  399             ef->relsize = dp->d_un.d_val;
  400             break;
  401         case DT_RELENT:
  402             if (dp->d_un.d_val != sizeof(Elf_Rel))
  403                 return ENOEXEC;
  404             break;
  405         case DT_JMPREL:
  406             ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
  407             break;
  408         case DT_PLTRELSZ:
  409             ef->pltrelsize = dp->d_un.d_val;
  410             break;
  411         case DT_RELA:
  412             ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
  413             break;
  414         case DT_RELASZ:
  415             ef->relasize = dp->d_un.d_val;
  416             break;
  417         case DT_RELAENT:
  418             if (dp->d_un.d_val != sizeof(Elf_Rela))
  419                 return ENOEXEC;
  420             break;
  421         case DT_PLTREL:
  422             plttype = dp->d_un.d_val;
  423             if (plttype != DT_REL && plttype != DT_RELA)
  424                 return ENOEXEC;
  425             break;
  426 #ifdef GDB
  427         case DT_DEBUG:
  428             dp->d_un.d_ptr = (Elf_Addr) &r_debug;
  429             break;
  430 #endif
  431         }
  432     }
  433 
  434     if (plttype == DT_RELA) {
  435         ef->pltrela = (const Elf_Rela *) ef->pltrel;
  436         ef->pltrel = NULL;
  437         ef->pltrelasize = ef->pltrelsize;
  438         ef->pltrelsize = 0;
  439     }
  440 
  441     ef->ddbsymtab = ef->symtab;
  442     ef->ddbsymcnt = ef->nchains;
  443     ef->ddbstrtab = ef->strtab;
  444     ef->ddbstrcnt = ef->strsz;
  445 
  446     return 0;
  447 }
  448 
  449 static int
  450 link_elf_link_preload(linker_class_t cls,
  451                       const char* filename, linker_file_t *result)
  452 {
  453     caddr_t             modptr, baseptr, sizeptr, dynptr;
  454     char                *type;
  455     elf_file_t          ef;
  456     linker_file_t       lf;
  457     int                 error;
  458     vm_offset_t         dp;
  459 
  460     /* Look to see if we have the file preloaded */
  461     modptr = preload_search_by_name(filename);
  462     if (modptr == NULL)
  463         return ENOENT;
  464 
  465     type = (char *)preload_search_info(modptr, MODINFO_TYPE);
  466     baseptr = preload_search_info(modptr, MODINFO_ADDR);
  467     sizeptr = preload_search_info(modptr, MODINFO_SIZE);
  468     dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
  469     if (type == NULL ||
  470         (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
  471          strcmp(type, "elf module") != 0))
  472         return (EFTYPE);
  473     if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
  474         return (EINVAL);
  475 
  476     lf = linker_make_file(filename, &link_elf_class);
  477     if (lf == NULL) {
  478         return ENOMEM;
  479     }
  480 
  481     ef = (elf_file_t) lf;
  482     ef->preloaded = 1;
  483     ef->modptr = modptr;
  484     ef->address = *(caddr_t *)baseptr;
  485 #ifdef SPARSE_MAPPING
  486     ef->object = 0;
  487 #endif
  488     dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
  489     ef->dynamic = (Elf_Dyn *)dp;
  490     lf->address = ef->address;
  491     lf->size = *(size_t *)sizeptr;
  492 
  493     error = parse_dynamic(ef);
  494     if (error) {
  495         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
  496         return error;
  497     }
  498     link_elf_reloc_local(lf);
  499     *result = lf;
  500     return (0);
  501 }
  502 
  503 static int
  504 link_elf_link_preload_finish(linker_file_t lf)
  505 {
  506     elf_file_t          ef;
  507     int error;
  508 
  509     ef = (elf_file_t) lf;
  510 #if 0   /* this will be more trouble than it's worth for now */
  511     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
  512         if (dp->d_tag != DT_NEEDED)
  513             continue;
  514         modname = ef->strtab + dp->d_un.d_val;
  515         error = linker_load_module(modname, lf);
  516         if (error)
  517             goto out;
  518     }
  519 #endif
  520     error = relocate_file(ef);
  521     if (error)
  522         return error;
  523     (void)link_elf_preload_parse_symbols(ef);
  524 
  525     return (link_elf_link_common_finish(lf));
  526 }
  527 
  528 static int
  529 link_elf_load_file(linker_class_t cls, const char* filename,
  530         linker_file_t* result)
  531 {
  532     struct nameidata nd;
  533     struct thread* td = curthread;      /* XXX */
  534     Elf_Ehdr *hdr;
  535     caddr_t firstpage;
  536     int nbytes, i;
  537     Elf_Phdr *phdr;
  538     Elf_Phdr *phlimit;
  539     Elf_Phdr *segs[2];
  540     int nsegs;
  541     Elf_Phdr *phdyn;
  542     Elf_Phdr *phphdr;
  543     caddr_t mapbase;
  544     size_t mapsize;
  545     Elf_Off base_offset;
  546     Elf_Addr base_vaddr;
  547     Elf_Addr base_vlimit;
  548     int error = 0;
  549     int resid, flags;
  550     elf_file_t ef;
  551     linker_file_t lf;
  552     Elf_Shdr *shdr;
  553     int symtabindex;
  554     int symstrindex;
  555     int symcnt;
  556     int strcnt;
  557 
  558     GIANT_REQUIRED;
  559 
  560     shdr = NULL;
  561     lf = NULL;
  562 
  563     NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
  564     flags = FREAD;
  565     error = vn_open(&nd, &flags, 0, -1);
  566     if (error)
  567         return error;
  568     NDFREE(&nd, NDF_ONLY_PNBUF);
  569 #ifdef MAC
  570     error = mac_check_kld_load(curthread->td_ucred, nd.ni_vp);
  571     if (error) {
  572         firstpage = NULL;
  573         goto out;
  574     }
  575 #endif
  576 
  577     /*
  578      * Read the elf header from the file.
  579      */
  580     firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
  581     if (firstpage == NULL) {
  582         error = ENOMEM;
  583         goto out;
  584     }
  585     hdr = (Elf_Ehdr *)firstpage;
  586     error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
  587                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  588                     &resid, td);
  589     nbytes = PAGE_SIZE - resid;
  590     if (error)
  591         goto out;
  592 
  593     if (!IS_ELF(*hdr)) {
  594         error = ENOEXEC;
  595         goto out;
  596     }
  597 
  598     if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
  599       || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
  600         link_elf_error("Unsupported file layout");
  601         error = ENOEXEC;
  602         goto out;
  603     }
  604     if (hdr->e_ident[EI_VERSION] != EV_CURRENT
  605       || hdr->e_version != EV_CURRENT) {
  606         link_elf_error("Unsupported file version");
  607         error = ENOEXEC;
  608         goto out;
  609     }
  610     if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
  611         link_elf_error("Unsupported file type");
  612         error = ENOEXEC;
  613         goto out;
  614     }
  615     if (hdr->e_machine != ELF_TARG_MACH) {
  616         link_elf_error("Unsupported machine");
  617         error = ENOEXEC;
  618         goto out;
  619     }
  620 
  621     /*
  622      * We rely on the program header being in the first page.  This is
  623      * not strictly required by the ABI specification, but it seems to
  624      * always true in practice.  And, it simplifies things considerably.
  625      */
  626     if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
  627           (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
  628           (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
  629         link_elf_error("Unreadable program headers");
  630 
  631     /*
  632      * Scan the program header entries, and save key information.
  633      *
  634      * We rely on there being exactly two load segments, text and data,
  635      * in that order.
  636      */
  637     phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
  638     phlimit = phdr + hdr->e_phnum;
  639     nsegs = 0;
  640     phdyn = NULL;
  641     phphdr = NULL;
  642     while (phdr < phlimit) {
  643         switch (phdr->p_type) {
  644 
  645         case PT_LOAD:
  646             if (nsegs == 2) {
  647                 link_elf_error("Too many sections");
  648                 error = ENOEXEC;
  649                 goto out;
  650             }
  651             /*
  652              * XXX: We just trust they come in right order ??
  653              */
  654             segs[nsegs] = phdr;
  655             ++nsegs;
  656             break;
  657 
  658         case PT_PHDR:
  659             phphdr = phdr;
  660             break;
  661 
  662         case PT_DYNAMIC:
  663             phdyn = phdr;
  664             break;
  665 
  666         case PT_INTERP:
  667             link_elf_error("Unsupported file type");
  668             error = ENOEXEC;
  669             goto out;
  670         }
  671 
  672         ++phdr;
  673     }
  674     if (phdyn == NULL) {
  675         link_elf_error("Object is not dynamically-linked");
  676         error = ENOEXEC;
  677         goto out;
  678     }
  679     if (nsegs != 2) {
  680         link_elf_error("Too few sections");
  681         error = ENOEXEC;
  682         goto out;
  683     }
  684 
  685     /*
  686      * Allocate the entire address space of the object, to stake out our
  687      * contiguous region, and to establish the base address for relocation.
  688      */
  689     base_offset = trunc_page(segs[0]->p_offset);
  690     base_vaddr = trunc_page(segs[0]->p_vaddr);
  691     base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz);
  692     mapsize = base_vlimit - base_vaddr;
  693 
  694     lf = linker_make_file(filename, &link_elf_class);
  695     if (!lf) {
  696         error = ENOMEM;
  697         goto out;
  698     }
  699 
  700     ef = (elf_file_t) lf;
  701 #ifdef SPARSE_MAPPING
  702     ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
  703     if (ef->object == NULL) {
  704         error = ENOMEM;
  705         goto out;
  706     }
  707     vm_object_reference(ef->object);
  708     ef->address = (caddr_t) vm_map_min(kernel_map);
  709     error = vm_map_find(kernel_map, ef->object, 0,
  710                         (vm_offset_t *) &ef->address,
  711                         mapsize, 1,
  712                         VM_PROT_ALL, VM_PROT_ALL, 0);
  713 #ifdef SPARSE_MAPPING
  714         /*
  715          * Wire down the pages
  716          */
  717     if (error == 0)
  718         error = vm_map_wire(kernel_map, (vm_offset_t) ef->address,
  719                             (vm_offset_t) ef->address + mapsize,
  720                             VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
  721 #endif
  722     if (error) {
  723         vm_object_deallocate(ef->object);
  724         ef->object = 0;
  725         goto out;
  726     }
  727 #else
  728     ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
  729     if (!ef->address) {
  730         error = ENOMEM;
  731         goto out;
  732     }
  733 #endif
  734     mapbase = ef->address;
  735 
  736     /*
  737      * Read the text and data sections and zero the bss.
  738      */
  739     for (i = 0; i < 2; i++) {
  740         caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
  741         error = vn_rdwr(UIO_READ, nd.ni_vp,
  742                         segbase, segs[i]->p_filesz, segs[i]->p_offset,
  743                         UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  744                         &resid, td);
  745         if (error) {
  746             goto out;
  747         }
  748         bzero(segbase + segs[i]->p_filesz,
  749               segs[i]->p_memsz - segs[i]->p_filesz);
  750     }
  751 
  752 #ifdef GPROF
  753     /* Update profiling information with the new text segment. */
  754     kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
  755         segs[0]->p_memsz));
  756 #endif
  757 
  758     ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
  759 
  760     lf->address = ef->address;
  761     lf->size = mapsize;
  762 
  763     error = parse_dynamic(ef);
  764     if (error)
  765         goto out;
  766     link_elf_reloc_local(lf);
  767 
  768     error = linker_load_dependencies(lf);
  769     if (error)
  770         goto out;
  771 #if 0   /* this will be more trouble than it's worth for now */
  772     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
  773         if (dp->d_tag != DT_NEEDED)
  774             continue;
  775         modname = ef->strtab + dp->d_un.d_val;
  776         error = linker_load_module(modname, lf);
  777         if (error)
  778             goto out;
  779     }
  780 #endif
  781     error = relocate_file(ef);
  782     if (error)
  783         goto out;
  784 
  785     /* Try and load the symbol table if it's present.  (you can strip it!) */
  786     nbytes = hdr->e_shnum * hdr->e_shentsize;
  787     if (nbytes == 0 || hdr->e_shoff == 0)
  788         goto nosyms;
  789     shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
  790     if (shdr == NULL) {
  791         error = ENOMEM;
  792         goto out;
  793     }
  794     error = vn_rdwr(UIO_READ, nd.ni_vp,
  795                     (caddr_t)shdr, nbytes, hdr->e_shoff,
  796                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  797                     &resid, td);
  798     if (error)
  799         goto out;
  800     symtabindex = -1;
  801     symstrindex = -1;
  802     for (i = 0; i < hdr->e_shnum; i++) {
  803         if (shdr[i].sh_type == SHT_SYMTAB) {
  804             symtabindex = i;
  805             symstrindex = shdr[i].sh_link;
  806         }
  807     }
  808     if (symtabindex < 0 || symstrindex < 0)
  809         goto nosyms;
  810 
  811     symcnt = shdr[symtabindex].sh_size;
  812     ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
  813     strcnt = shdr[symstrindex].sh_size;
  814     ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
  815 
  816     if (ef->symbase == NULL || ef->strbase == NULL) {
  817         error = ENOMEM;
  818         goto out;
  819     }
  820     error = vn_rdwr(UIO_READ, nd.ni_vp,
  821                     ef->symbase, symcnt, shdr[symtabindex].sh_offset,
  822                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  823                     &resid, td);
  824     if (error)
  825         goto out;
  826     error = vn_rdwr(UIO_READ, nd.ni_vp,
  827                     ef->strbase, strcnt, shdr[symstrindex].sh_offset,
  828                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
  829                     &resid, td);
  830     if (error)
  831         goto out;
  832 
  833     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
  834     ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
  835     ef->ddbstrcnt = strcnt;
  836     ef->ddbstrtab = ef->strbase;
  837 
  838     error = link_elf_link_common_finish(lf);
  839     if (error)
  840         goto out;
  841 
  842 nosyms:
  843 
  844     *result = lf;
  845 
  846 out:
  847     if (error && lf)
  848         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
  849     if (shdr)
  850         free(shdr, M_LINKER);
  851     if (firstpage)
  852         free(firstpage, M_LINKER);
  853     VOP_UNLOCK(nd.ni_vp, 0, td);
  854     vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
  855 
  856     return error;
  857 }
  858 
  859 static void
  860 link_elf_unload_file(linker_file_t file)
  861 {
  862     elf_file_t ef = (elf_file_t) file;
  863 
  864 #ifdef GDB
  865     if (ef->gdb.l_ld) {
  866         GDB_STATE(RT_DELETE);
  867         free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
  868         link_elf_delete_gdb(&ef->gdb);
  869         GDB_STATE(RT_CONSISTENT);
  870     }
  871 #endif
  872 
  873     /* Notify MD code that a module is being unloaded. */
  874     elf_cpu_unload_file(file);
  875 
  876     if (ef->preloaded) {
  877         link_elf_unload_preload(file);
  878         return;
  879     }
  880 
  881 #ifdef SPARSE_MAPPING
  882     if (ef->object) {
  883         /*
  884          * Unwire the pages
  885          */
  886         vm_map_unwire(kernel_map,
  887                     (vm_offset_t) ef->address,
  888                     (vm_offset_t) ef->address
  889                     + (ef->object->size << PAGE_SHIFT),
  890                     VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
  891         vm_map_remove(kernel_map, (vm_offset_t) ef->address,
  892                       (vm_offset_t) ef->address
  893                       + (ef->object->size << PAGE_SHIFT));
  894         vm_object_deallocate(ef->object);
  895     }
  896 #else
  897     if (ef->address)
  898         free(ef->address, M_LINKER);
  899 #endif
  900     if (ef->symbase)
  901         free(ef->symbase, M_LINKER);
  902     if (ef->strbase)
  903         free(ef->strbase, M_LINKER);
  904 }
  905 
  906 static void
  907 link_elf_unload_preload(linker_file_t file)
  908 {
  909     if (file->filename)
  910         preload_delete_name(file->filename);
  911 }
  912 
  913 static const char *
  914 symbol_name(elf_file_t ef, Elf_Word r_info)
  915 {
  916     const Elf_Sym *ref;
  917 
  918     if (ELF_R_SYM(r_info)) {
  919         ref = ef->symtab + ELF_R_SYM(r_info);
  920         return ef->strtab + ref->st_name;
  921     } else
  922         return NULL;
  923 }
  924 
  925 static int
  926 relocate_file(elf_file_t ef)
  927 {
  928     const Elf_Rel *rellim;
  929     const Elf_Rel *rel;
  930     const Elf_Rela *relalim;
  931     const Elf_Rela *rela;
  932     const char *symname;
  933 
  934     /* Perform relocations without addend if there are any: */
  935     rel = ef->rel;
  936     if (rel) {
  937         rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
  938         while (rel < rellim) {
  939             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
  940                           elf_lookup)) {
  941                 symname = symbol_name(ef, rel->r_info);
  942                 printf("link_elf: symbol %s undefined\n", symname);
  943                 return ENOENT;
  944             }
  945             rel++;
  946         }
  947     }
  948 
  949     /* Perform relocations with addend if there are any: */
  950     rela = ef->rela;
  951     if (rela) {
  952         relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
  953         while (rela < relalim) {
  954             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
  955                           elf_lookup)) {
  956                 symname = symbol_name(ef, rela->r_info);
  957                 printf("link_elf: symbol %s undefined\n", symname);
  958                 return ENOENT;
  959             }
  960             rela++;
  961         }
  962     }
  963 
  964     /* Perform PLT relocations without addend if there are any: */
  965     rel = ef->pltrel;
  966     if (rel) {
  967         rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
  968         while (rel < rellim) {
  969             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
  970                           elf_lookup)) {
  971                 symname = symbol_name(ef, rel->r_info);
  972                 printf("link_elf: symbol %s undefined\n", symname);
  973                 return ENOENT;
  974             }
  975             rel++;
  976         }
  977     }
  978 
  979     /* Perform relocations with addend if there are any: */
  980     rela = ef->pltrela;
  981     if (rela) {
  982         relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
  983         while (rela < relalim) {
  984             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
  985                           elf_lookup)) {
  986                 symname = symbol_name(ef, rela->r_info);
  987                 printf("link_elf: symbol %s undefined\n", symname);
  988                 return ENOENT;
  989             }
  990             rela++;
  991         }
  992     }
  993 
  994     return 0;
  995 }
  996 
  997 /*
  998  * Hash function for symbol table lookup.  Don't even think about changing
  999  * this.  It is specified by the System V ABI.
 1000  */
 1001 static unsigned long
 1002 elf_hash(const char *name)
 1003 {
 1004     const unsigned char *p = (const unsigned char *) name;
 1005     unsigned long h = 0;
 1006     unsigned long g;
 1007 
 1008     while (*p != '\0') {
 1009         h = (h << 4) + *p++;
 1010         if ((g = h & 0xf0000000) != 0)
 1011             h ^= g >> 24;
 1012         h &= ~g;
 1013     }
 1014     return h;
 1015 }
 1016 
 1017 static int
 1018 link_elf_lookup_symbol(linker_file_t lf, const char* name, c_linker_sym_t* sym)
 1019 {
 1020     elf_file_t ef = (elf_file_t) lf;
 1021     unsigned long symnum;
 1022     const Elf_Sym* symp;
 1023     const char *strp;
 1024     unsigned long hash;
 1025     int i;
 1026 
 1027     /* If we don't have a hash, bail. */
 1028     if (ef->buckets == NULL || ef->nbuckets == 0) {
 1029         printf("link_elf_lookup_symbol: missing symbol hash table\n");
 1030         return ENOENT;
 1031     }
 1032 
 1033     /* First, search hashed global symbols */
 1034     hash = elf_hash(name);
 1035     symnum = ef->buckets[hash % ef->nbuckets];
 1036 
 1037     while (symnum != STN_UNDEF) {
 1038         if (symnum >= ef->nchains) {
 1039             printf("link_elf_lookup_symbol: corrupt symbol table\n");
 1040             return ENOENT;
 1041         }
 1042 
 1043         symp = ef->symtab + symnum;
 1044         if (symp->st_name == 0) {
 1045             printf("link_elf_lookup_symbol: corrupt symbol table\n");
 1046             return ENOENT;
 1047         }
 1048 
 1049         strp = ef->strtab + symp->st_name;
 1050 
 1051         if (strcmp(name, strp) == 0) {
 1052             if (symp->st_shndx != SHN_UNDEF ||
 1053                 (symp->st_value != 0 &&
 1054                  ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
 1055                 *sym = (c_linker_sym_t) symp;
 1056                 return 0;
 1057             } else
 1058                 return ENOENT;
 1059         }
 1060 
 1061         symnum = ef->chains[symnum];
 1062     }
 1063 
 1064     /* If we have not found it, look at the full table (if loaded) */
 1065     if (ef->symtab == ef->ddbsymtab)
 1066         return ENOENT;
 1067 
 1068     /* Exhaustive search */
 1069     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1070         strp = ef->ddbstrtab + symp->st_name;
 1071         if (strcmp(name, strp) == 0) {
 1072             if (symp->st_shndx != SHN_UNDEF ||
 1073                 (symp->st_value != 0 &&
 1074                  ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
 1075                 *sym = (c_linker_sym_t) symp;
 1076                 return 0;
 1077             } else
 1078                 return ENOENT;
 1079         }
 1080     }
 1081 
 1082     return ENOENT;
 1083 }
 1084 
 1085 static int
 1086 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
 1087 {
 1088         elf_file_t ef = (elf_file_t) lf;
 1089         const Elf_Sym* es = (const Elf_Sym*) sym;
 1090 
 1091         if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
 1092             symval->name = ef->strtab + es->st_name;
 1093             symval->value = (caddr_t) ef->address + es->st_value;
 1094             symval->size = es->st_size;
 1095             return 0;
 1096         }
 1097         if (ef->symtab == ef->ddbsymtab)
 1098             return ENOENT;
 1099         if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
 1100             symval->name = ef->ddbstrtab + es->st_name;
 1101             symval->value = (caddr_t) ef->address + es->st_value;
 1102             symval->size = es->st_size;
 1103             return 0;
 1104         }
 1105         return ENOENT;
 1106 }
 1107 
 1108 static int
 1109 link_elf_search_symbol(linker_file_t lf, caddr_t value,
 1110                        c_linker_sym_t* sym, long* diffp)
 1111 {
 1112         elf_file_t ef = (elf_file_t) lf;
 1113         u_long off = (uintptr_t) (void *) value;
 1114         u_long diff = off;
 1115         u_long st_value;
 1116         const Elf_Sym* es;
 1117         const Elf_Sym* best = 0;
 1118         int i;
 1119 
 1120         for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
 1121                 if (es->st_name == 0)
 1122                         continue;
 1123                 st_value = es->st_value + (uintptr_t) (void *) ef->address;
 1124                 if (off >= st_value) {
 1125                         if (off - st_value < diff) {
 1126                                 diff = off - st_value;
 1127                                 best = es;
 1128                                 if (diff == 0)
 1129                                         break;
 1130                         } else if (off - st_value == diff) {
 1131                                 best = es;
 1132                         }
 1133                 }
 1134         }
 1135         if (best == 0)
 1136                 *diffp = off;
 1137         else
 1138                 *diffp = diff;
 1139         *sym = (c_linker_sym_t) best;
 1140 
 1141         return 0;
 1142 }
 1143 
 1144 /*
 1145  * Look up a linker set on an ELF system.
 1146  */
 1147 static int
 1148 link_elf_lookup_set(linker_file_t lf, const char *name,
 1149                     void ***startp, void ***stopp, int *countp)
 1150 {
 1151         c_linker_sym_t sym;
 1152         linker_symval_t symval;
 1153         char *setsym;
 1154         void **start, **stop;
 1155         int len, error = 0, count;
 1156 
 1157         len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
 1158         setsym = malloc(len, M_LINKER, M_WAITOK);
 1159         if (setsym == NULL)
 1160                 return ENOMEM;
 1161 
 1162         /* get address of first entry */
 1163         snprintf(setsym, len, "%s%s", "__start_set_", name);
 1164         error = link_elf_lookup_symbol(lf, setsym, &sym);
 1165         if (error)
 1166                 goto out;
 1167         link_elf_symbol_values(lf, sym, &symval);
 1168         if (symval.value == 0) {
 1169                 error = ESRCH;
 1170                 goto out;
 1171         }
 1172         start = (void **)symval.value;
 1173 
 1174         /* get address of last entry */
 1175         snprintf(setsym, len, "%s%s", "__stop_set_", name);
 1176         error = link_elf_lookup_symbol(lf, setsym, &sym);
 1177         if (error)
 1178                 goto out;
 1179         link_elf_symbol_values(lf, sym, &symval);
 1180         if (symval.value == 0) {
 1181                 error = ESRCH;
 1182                 goto out;
 1183         }
 1184         stop = (void **)symval.value;
 1185 
 1186         /* and the number of entries */
 1187         count = stop - start;
 1188 
 1189         /* and copy out */
 1190         if (startp)
 1191                 *startp = start;
 1192         if (stopp)
 1193                 *stopp = stop;
 1194         if (countp)
 1195                 *countp = count;
 1196 
 1197 out:
 1198         free(setsym, M_LINKER);
 1199         return error;
 1200 }
 1201 
 1202 static int
 1203 link_elf_each_function_name(linker_file_t file,
 1204   int (*callback)(const char *, void *), void *opaque) {
 1205     elf_file_t ef = (elf_file_t)file;
 1206     const Elf_Sym* symp;
 1207     int i, error;
 1208         
 1209     /* Exhaustive search */
 1210     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
 1211         if (symp->st_value != 0 &&
 1212             ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
 1213                 error = callback(ef->ddbstrtab + symp->st_name, opaque);
 1214                 if (error)
 1215                     return (error);
 1216         }
 1217     }
 1218     return (0);
 1219 }
 1220 
 1221 #ifdef __ia64__
 1222 /*
 1223  * Each KLD has its own GP. The GP value for each load module is given by
 1224  * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
 1225  * don't have direct access to the ELF file structure. The link_elf_get_gp()
 1226  * function returns the GP given a pointer to a generic linker file struct.
 1227  */
 1228 Elf_Addr
 1229 link_elf_get_gp(linker_file_t lf)
 1230 {
 1231         elf_file_t ef = (elf_file_t)lf;
 1232         return (Elf_Addr)ef->got;
 1233 }
 1234 #endif
 1235 
 1236 const Elf_Sym *
 1237 elf_get_sym(linker_file_t lf, Elf_Word symidx)
 1238 {
 1239         elf_file_t ef = (elf_file_t)lf;
 1240 
 1241         if (symidx >= ef->nchains)
 1242                 return (NULL);
 1243         return (ef->symtab + symidx);
 1244 }
 1245 
 1246 const char *
 1247 elf_get_symname(linker_file_t lf, Elf_Word symidx)
 1248 {
 1249         elf_file_t ef = (elf_file_t)lf;
 1250         const Elf_Sym *sym;
 1251 
 1252         if (symidx >= ef->nchains)
 1253                 return (NULL);
 1254         sym = ef->symtab + symidx;
 1255         return (ef->strtab + sym->st_name);
 1256 }
 1257 
 1258 /*
 1259  * Symbol lookup function that can be used when the symbol index is known (ie
 1260  * in relocations). It uses the symbol index instead of doing a fully fledged
 1261  * hash table based lookup when such is valid. For example for local symbols.
 1262  * This is not only more efficient, it's also more correct. It's not always
 1263  * the case that the symbol can be found through the hash table.
 1264  */
 1265 static Elf_Addr
 1266 elf_lookup(linker_file_t lf, Elf_Word symidx, int deps)
 1267 {
 1268         elf_file_t ef = (elf_file_t)lf;
 1269         const Elf_Sym *sym;
 1270         const char *symbol;
 1271 
 1272         /* Don't even try to lookup the symbol if the index is bogus. */
 1273         if (symidx >= ef->nchains)
 1274                 return (0);
 1275 
 1276         sym = ef->symtab + symidx;
 1277 
 1278         /*
 1279          * Don't do a full lookup when the symbol is local. It may even
 1280          * fail because it may not be found through the hash table.
 1281          */
 1282         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
 1283                 /* Force lookup failure when we have an insanity. */
 1284                 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
 1285                         return (0);
 1286                 return ((Elf_Addr)ef->address + sym->st_value);
 1287         }
 1288 
 1289         /*
 1290          * XXX we can avoid doing a hash table based lookup for global
 1291          * symbols as well. This however is not always valid, so we'll
 1292          * just do it the hard way for now. Performance tweaks can
 1293          * always be added.
 1294          */
 1295 
 1296         symbol = ef->strtab + sym->st_name;
 1297 
 1298         /* Force a lookup failure if the symbol name is bogus. */
 1299         if (*symbol == 0)
 1300                 return (0);
 1301 
 1302         return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
 1303 }
 1304 
 1305 static void
 1306 link_elf_reloc_local(linker_file_t lf)
 1307 {
 1308     const Elf_Rel *rellim;
 1309     const Elf_Rel *rel;
 1310     const Elf_Rela *relalim;
 1311     const Elf_Rela *rela;
 1312     elf_file_t ef = (elf_file_t)lf;
 1313 
 1314     /* Perform relocations without addend if there are any: */
 1315     if ((rel = ef->rel) != NULL) {
 1316         rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
 1317         while (rel < rellim) {
 1318             elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
 1319                             elf_lookup);
 1320             rel++;
 1321         }
 1322     }
 1323 
 1324     /* Perform relocations with addend if there are any: */
 1325     if ((rela = ef->rela) != NULL) {
 1326         relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
 1327         while (rela < relalim) {
 1328             elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
 1329                             elf_lookup);
 1330             rela++;
 1331         }
 1332     }
 1333 }

Cache object: fa37fad0a3eb6760ae62b9a5f231c059


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