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