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

Cache object: da2ba16e0006d0d7090ffc9f21ae877d


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