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