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