intrcnt: Fix long standing interrupt name/count array mismatch
[dragonfly.git] / sys / kern / link_elf_obj.c
1 /*-
2  * Copyright (c) 1998 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  * $FreeBSD: src/sys/kern/link_elf.c,v 1.24 1999/12/24 15:33:36 bde Exp $
28  * $DragonFly: src/sys/kern/link_elf.c,v 1.29 2008/08/01 23:11:16 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/nlookup.h>
37 #include <sys/fcntl.h>
38 #include <sys/vnode.h>
39 #include <sys/linker.h>
40 #include <machine/elf.h>
41
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/vm_zone.h>
45 #include <vm/vm_object.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_extern.h>
48 #include <sys/lock.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51
52 static int      link_elf_obj_preload_file(const char *, linker_file_t *);
53 static int      link_elf_obj_preload_finish(linker_file_t);
54 static int      link_elf_obj_load_file(const char *, linker_file_t *);
55 static int
56 link_elf_obj_lookup_symbol(linker_file_t, const char *,
57                        c_linker_sym_t *);
58 static int      link_elf_obj_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t *);
59 static int
60 link_elf_obj_search_symbol(linker_file_t, caddr_t value,
61                        c_linker_sym_t * sym, long *diffp);
62
63 static void     link_elf_obj_unload_file(linker_file_t);
64 static int
65 link_elf_obj_lookup_set(linker_file_t, const char *,
66                     void ***, void ***, int *);
67 static void     link_elf_obj_reloc_local(linker_file_t lf);
68 static int elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *);
69
70 static struct linker_class_ops link_elf_obj_class_ops = {
71         link_elf_obj_load_file,
72         link_elf_obj_preload_file,
73 };
74
75 static struct linker_file_ops link_elf_obj_file_ops = {
76         .lookup_symbol = link_elf_obj_lookup_symbol,
77         .symbol_values = link_elf_obj_symbol_values,
78         .search_symbol = link_elf_obj_search_symbol,
79         .preload_finish = link_elf_obj_preload_finish,
80         .unload = link_elf_obj_unload_file,
81         .lookup_set = link_elf_obj_lookup_set,
82 };
83
84 typedef struct {
85         void           *addr;
86         Elf_Off         size;
87         int             flags;
88         int             sec;            /* Original section */
89         char           *name;
90 }               Elf_progent;
91
92 typedef struct {
93         Elf_Rel        *rel;
94         int             nrel;
95         int             sec;
96 }               Elf_relent;
97
98 typedef struct {
99         Elf_Rela       *rela;
100         int             nrela;
101         int             sec;
102 }               Elf_relaent;
103
104
105 typedef struct elf_file {
106         int             preloaded;
107
108         caddr_t         address;        /* Relocation address */
109         size_t          bytes;          /* Chunk size in bytes */
110         vm_object_t     object;         /* VM object to hold file pages */
111         Elf_Shdr       *e_shdr;
112
113         Elf_progent    *progtab;
114         int             nprogtab;
115
116         Elf_relaent    *relatab;
117         int             nrelatab;
118
119         Elf_relent     *reltab;
120         int             nreltab;
121
122         Elf_Sym        *ddbsymtab;      /* The symbol table we are using */
123         long            ddbsymcnt;      /* Number of symbols */
124         caddr_t         ddbstrtab;      /* String table */
125         long            ddbstrcnt;      /* number of bytes in string table */
126
127         caddr_t         shstrtab;       /* Section name string table */
128         long            shstrcnt;       /* number of bytes in string table */
129
130         caddr_t         ctftab;         /* CTF table */
131         long            ctfcnt;         /* number of bytes in CTF table */
132         caddr_t         ctfoff;         /* CTF offset table */
133         caddr_t         typoff;         /* Type offset table */
134         long            typlen;         /* Number of type entries. */
135
136 }              *elf_file_t;
137
138 static int      relocate_file(linker_file_t lf);
139
140 /*
141  * The kernel symbol table starts here.
142  */
143 extern struct _dynamic _DYNAMIC;
144
145 static void
146 link_elf_obj_init(void *arg)
147 {
148 #if ELF_TARG_CLASS == ELFCLASS32
149         linker_add_class("elf32", NULL, &link_elf_obj_class_ops);
150 #else
151         linker_add_class("elf64", NULL, &link_elf_obj_class_ops);
152 #endif
153 }
154
155 SYSINIT(link_elf, SI_BOOT2_KLD, SI_ORDER_SECOND, link_elf_obj_init, 0);
156
157 static void
158 link_elf_obj_error(const char *file, const char *s)
159 {
160         kprintf("kldload: %s: %s\n", file, s);
161 }
162
163 static int
164 link_elf_obj_preload_file(const char *filename, linker_file_t *result)
165 {
166         Elf_Ehdr       *hdr;
167         Elf_Shdr       *shdr;
168         Elf_Sym *es;
169         caddr_t         modptr, baseptr, sizeptr;
170         char           *type;
171         elf_file_t      ef;
172         linker_file_t   lf;
173         Elf_Addr        off;
174         int             error, i, j, pb, ra, rl, shstrindex, symstrindex, symtabindex;
175
176         /*
177          * Look to see if we have the module preloaded.
178          */
179         modptr = preload_search_by_name(filename);
180         if (modptr == NULL)
181                 return ENOENT;
182
183         /* It's preloaded, check we can handle it and collect information */
184         type = (char *)preload_search_info(modptr, MODINFO_TYPE);
185         baseptr = preload_search_info(modptr, MODINFO_ADDR);
186         sizeptr = preload_search_info(modptr, MODINFO_SIZE);
187         hdr = (Elf_Ehdr *) preload_search_info(modptr, MODINFO_METADATA |
188                                                MODINFOMD_ELFHDR);
189         shdr = (Elf_Shdr *) preload_search_info(modptr, MODINFO_METADATA |
190                                                 MODINFOMD_SHDR);
191         if (type == NULL ||
192             (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " obj module") != 0 &&
193              strcmp(type, "elf obj module") != 0)) {
194                 return (EFTYPE);
195         }
196         if (baseptr == NULL || sizeptr == NULL || hdr == NULL || shdr == NULL)
197                 return (EINVAL);
198
199         ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
200         ef->preloaded = 1;
201         ef->address = *(caddr_t *) baseptr;
202         ef->bytes = 0;
203         lf = linker_make_file(filename, ef, &link_elf_obj_file_ops);
204         if (lf == NULL) {
205                 kfree(ef, M_LINKER);
206                 return ENOMEM;
207         }
208         lf->address = ef->address;
209         lf->size = *(size_t *) sizeptr;
210
211         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
212             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
213             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
214             hdr->e_version != EV_CURRENT ||
215             hdr->e_type != ET_REL ||
216             hdr->e_machine != ELF_TARG_MACH) {
217                 error = EFTYPE;
218                 goto out;
219         }
220         ef->e_shdr = shdr;
221
222         /* Scan the section header for information and table sizing. */
223         symtabindex = -1;
224         symstrindex = -1;
225         for (i = 0; i < hdr->e_shnum; i++) {
226                 switch (shdr[i].sh_type) {
227                 case SHT_PROGBITS:
228                 case SHT_NOBITS:
229                         ef->nprogtab++;
230                         break;
231                 case SHT_SYMTAB:
232                         symtabindex = i;
233                         symstrindex = shdr[i].sh_link;
234                         break;
235                 case SHT_REL:
236                         ef->nreltab++;
237                         break;
238                 case SHT_RELA:
239                         ef->nrelatab++;
240                         break;
241                 }
242         }
243
244         shstrindex = hdr->e_shstrndx;
245         if (ef->nprogtab == 0 || symstrindex < 0 ||
246             symstrindex >= hdr->e_shnum ||
247             shdr[symstrindex].sh_type != SHT_STRTAB || shstrindex == 0 ||
248             shstrindex >= hdr->e_shnum ||
249             shdr[shstrindex].sh_type != SHT_STRTAB) {
250                 error = ENOEXEC;
251                 goto out;
252         }
253         /* Allocate space for tracking the load chunks */
254         if (ef->nprogtab != 0)
255                 ef->progtab = kmalloc(ef->nprogtab * sizeof(*ef->progtab),
256                                      M_LINKER, M_WAITOK | M_ZERO);
257         if (ef->nreltab != 0)
258                 ef->reltab = kmalloc(ef->nreltab * sizeof(*ef->reltab),
259                                     M_LINKER, M_WAITOK | M_ZERO);
260         if (ef->nrelatab != 0)
261                 ef->relatab = kmalloc(ef->nrelatab * sizeof(*ef->relatab),
262                                      M_LINKER, M_WAITOK | M_ZERO);
263         if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
264             (ef->nreltab != 0 && ef->reltab == NULL) ||
265             (ef->nrelatab != 0 && ef->relatab == NULL)) {
266                 error = ENOMEM;
267                 goto out;
268         }
269         /* XXX, relocate the sh_addr fields saved by the loader. */
270         off = 0;
271         for (i = 0; i < hdr->e_shnum; i++) {
272                 if (shdr[i].sh_addr != 0 && (off == 0 || shdr[i].sh_addr < off))
273                         off = shdr[i].sh_addr;
274         }
275         for (i = 0; i < hdr->e_shnum; i++) {
276                 if (shdr[i].sh_addr != 0)
277                         shdr[i].sh_addr = shdr[i].sh_addr - off +
278                                 (Elf_Addr) ef->address;
279         }
280
281         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
282         ef->ddbsymtab = (Elf_Sym *) shdr[symtabindex].sh_addr;
283         ef->ddbstrcnt = shdr[symstrindex].sh_size;
284         ef->ddbstrtab = (char *)shdr[symstrindex].sh_addr;
285         ef->shstrcnt = shdr[shstrindex].sh_size;
286         ef->shstrtab = (char *)shdr[shstrindex].sh_addr;
287
288         /* Now fill out progtab and the relocation tables. */
289         pb = 0;
290         rl = 0;
291         ra = 0;
292         for (i = 0; i < hdr->e_shnum; i++) {
293                 switch (shdr[i].sh_type) {
294                 case SHT_PROGBITS:
295                 case SHT_NOBITS:
296                         ef->progtab[pb].addr = (void *)shdr[i].sh_addr;
297                         if (shdr[i].sh_type == SHT_PROGBITS)
298                                 ef->progtab[pb].name = "<<PROGBITS>>";
299                         else
300                                 ef->progtab[pb].name = "<<NOBITS>>";
301                         ef->progtab[pb].size = shdr[i].sh_size;
302                         ef->progtab[pb].sec = i;
303                         if (ef->shstrtab && shdr[i].sh_name != 0)
304                                 ef->progtab[pb].name =
305                                         ef->shstrtab + shdr[i].sh_name;
306 #if 0
307                         if (ef->progtab[pb].name != NULL &&
308                             !strcmp(ef->progtab[pb].name, "set_pcpu")) {
309                                 void           *dpcpu;
310
311                                 dpcpu = dpcpu_alloc(shdr[i].sh_size);
312                                 if (dpcpu == NULL) {
313                                         error = ENOSPC;
314                                         goto out;
315                                 }
316                                 memcpy(dpcpu, ef->progtab[pb].addr,
317                                        ef->progtab[pb].size);
318                                 dpcpu_copy(dpcpu, shdr[i].sh_size);
319                                 ef->progtab[pb].addr = dpcpu;
320 #ifdef VIMAGE
321                         } else if (ef->progtab[pb].name != NULL &&
322                                    !strcmp(ef->progtab[pb].name, VNET_SETNAME)) {
323                                 void           *vnet_data;
324
325                                 vnet_data = vnet_data_alloc(shdr[i].sh_size);
326                                 if (vnet_data == NULL) {
327                                         error = ENOSPC;
328                                         goto out;
329                                 }
330                                 memcpy(vnet_data, ef->progtab[pb].addr,
331                                        ef->progtab[pb].size);
332                                 vnet_data_copy(vnet_data, shdr[i].sh_size);
333                                 ef->progtab[pb].addr = vnet_data;
334 #endif
335                         }
336 #endif
337                         /* Update all symbol values with the offset. */
338                         for (j = 0; j < ef->ddbsymcnt; j++) {
339                                 es = &ef->ddbsymtab[j];
340                                 if (es->st_shndx != i)
341                                         continue;
342                                 es->st_value += (Elf_Addr) ef->progtab[pb].addr;
343                         }
344                         pb++;
345                         break;
346                 case SHT_REL:
347                         ef->reltab[rl].rel = (Elf_Rel *) shdr[i].sh_addr;
348                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
349                         ef->reltab[rl].sec = shdr[i].sh_info;
350                         rl++;
351                         break;
352                 case SHT_RELA:
353                         ef->relatab[ra].rela = (Elf_Rela *) shdr[i].sh_addr;
354                         ef->relatab[ra].nrela =
355                                 shdr[i].sh_size / sizeof(Elf_Rela);
356                         ef->relatab[ra].sec = shdr[i].sh_info;
357                         ra++;
358                         break;
359                 }
360         }
361         if (pb != ef->nprogtab)
362                 panic("lost progbits");
363         if (rl != ef->nreltab)
364                 panic("lost reltab");
365         if (ra != ef->nrelatab)
366                 panic("lost relatab");
367
368         /* Local intra-module relocations */
369         link_elf_obj_reloc_local(lf);
370
371         *result = lf;
372         return (0);
373
374 out:
375         /* preload not done this way */
376         linker_file_unload(lf /* , LINKER_UNLOAD_FORCE */ );
377         return (error);
378 }
379
380 static int
381 link_elf_obj_preload_finish(linker_file_t lf)
382 {
383         int error;
384
385         error = relocate_file(lf);
386
387         return (error);
388 }
389
390 static int
391 link_elf_obj_load_file(const char *filename, linker_file_t * result)
392 {
393         struct nlookupdata nd;
394         struct thread  *td = curthread; /* XXX */
395         struct proc    *p = td->td_proc;
396         char           *pathname;
397         struct vnode   *vp;
398         Elf_Ehdr       *hdr;
399         Elf_Shdr       *shdr;
400         Elf_Sym        *es;
401         int             nbytes, i, j;
402         vm_offset_t     mapbase;
403         size_t          mapsize;
404         int             error = 0;
405         int             resid;
406         elf_file_t      ef;
407         linker_file_t   lf;
408         int             symtabindex;
409         int             symstrindex;
410         int             shstrindex;
411         int             nsym;
412         int             pb, rl, ra;
413         int             alignmask;
414
415         /* XXX Hack for firmware loading where p == NULL */
416         if (p == NULL) {
417                 p = &proc0;
418         }
419
420         KKASSERT(p != NULL);
421         if (p->p_ucred == NULL) {
422                 kprintf("link_elf_obj_load_file: cannot load '%s' from filesystem"
423                         " this early\n", filename);
424                 return ENOENT;
425         }
426         shdr = NULL;
427         lf = NULL;
428         mapsize = 0;
429         hdr = NULL;
430         pathname = linker_search_path(filename);
431         if (pathname == NULL)
432                 return ENOENT;
433
434         error = nlookup_init(&nd, pathname, UIO_SYSSPACE, NLC_FOLLOW | NLC_LOCKVP);
435         if (error == 0)
436                 error = vn_open(&nd, NULL, FREAD, 0);
437         kfree(pathname, M_LINKER);
438         if (error) {
439                 nlookup_done(&nd);
440                 return error;
441         }
442         vp = nd.nl_open_vp;
443         nd.nl_open_vp = NULL;
444         nlookup_done(&nd);
445
446         /*
447          * Read the elf header from the file.
448          */
449         hdr = kmalloc(sizeof(*hdr), M_LINKER, M_WAITOK);
450         if (hdr == NULL) {
451                 error = ENOMEM;
452                 goto out;
453         }
454         error = vn_rdwr(UIO_READ, vp, (void *)hdr, sizeof(*hdr), 0,
455                         UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
456         if (error)
457                 goto out;
458         if (resid != 0) {
459                 error = ENOEXEC;
460                 goto out;
461         }
462         if (!IS_ELF(*hdr)) {
463                 error = ENOEXEC;
464                 goto out;
465         }
466
467         if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
468             || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
469                 link_elf_obj_error(filename, "Unsupported file layout");
470                 error = ENOEXEC;
471                 goto out;
472         }
473         if (hdr->e_ident[EI_VERSION] != EV_CURRENT
474             || hdr->e_version != EV_CURRENT) {
475                 link_elf_obj_error(filename, "Unsupported file version");
476                 error = ENOEXEC;
477                 goto out;
478         }
479         if (hdr->e_type != ET_REL) {
480                 error = ENOSYS;
481                 goto out;
482         }
483         if (hdr->e_machine != ELF_TARG_MACH) {
484                 link_elf_obj_error(filename, "Unsupported machine");
485                 error = ENOEXEC;
486                 goto out;
487         }
488
489         ef = kmalloc(sizeof(struct elf_file), M_LINKER, M_WAITOK | M_ZERO);
490         lf = linker_make_file(filename, ef, &link_elf_obj_file_ops);
491         if (lf == NULL) {
492                 kfree(ef, M_LINKER);
493                 error = ENOMEM;
494                 goto out;
495         }
496         ef->nprogtab = 0;
497         ef->e_shdr = 0;
498         ef->nreltab = 0;
499         ef->nrelatab = 0;
500
501         /* Allocate and read in the section header */
502         nbytes = hdr->e_shnum * hdr->e_shentsize;
503         if (nbytes == 0 || hdr->e_shoff == 0 ||
504             hdr->e_shentsize != sizeof(Elf_Shdr)) {
505                 error = ENOEXEC;
506                 goto out;
507         }
508         shdr = kmalloc(nbytes, M_LINKER, M_WAITOK);
509         if (shdr == NULL) {
510                 error = ENOMEM;
511                 goto out;
512         }
513         ef->e_shdr = shdr;
514         error = vn_rdwr(UIO_READ, vp, (caddr_t) shdr, nbytes, hdr->e_shoff,
515                         UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
516         if (error)
517                 goto out;
518         if (resid) {
519                 error = ENOEXEC;
520                 goto out;
521         }
522         /* Scan the section header for information and table sizing. */
523         nsym = 0;
524         symtabindex = -1;
525         symstrindex = -1;
526         for (i = 0; i < hdr->e_shnum; i++) {
527                 switch (shdr[i].sh_type) {
528                 case SHT_PROGBITS:
529                 case SHT_NOBITS:
530                         ef->nprogtab++;
531                         break;
532                 case SHT_SYMTAB:
533                         nsym++;
534                         symtabindex = i;
535                         symstrindex = shdr[i].sh_link;
536                         break;
537                 case SHT_REL:
538                         ef->nreltab++;
539                         break;
540                 case SHT_RELA:
541                         ef->nrelatab++;
542                         break;
543                 case SHT_STRTAB:
544                         break;
545                 }
546         }
547         if (ef->nprogtab == 0) {
548                 link_elf_obj_error(filename, "file has no contents");
549                 error = ENOEXEC;
550                 goto out;
551         }
552         if (nsym != 1) {
553                 /* Only allow one symbol table for now */
554                 link_elf_obj_error(filename, "file has no valid symbol table");
555                 error = ENOEXEC;
556                 goto out;
557         }
558         if (symstrindex < 0 || symstrindex > hdr->e_shnum ||
559             shdr[symstrindex].sh_type != SHT_STRTAB) {
560                 link_elf_obj_error(filename, "file has invalid symbol strings");
561                 error = ENOEXEC;
562                 goto out;
563         }
564         /* Allocate space for tracking the load chunks */
565         if (ef->nprogtab != 0)
566                 ef->progtab = kmalloc(ef->nprogtab * sizeof(*ef->progtab),
567                                       M_LINKER, M_WAITOK | M_ZERO);
568         if (ef->nreltab != 0)
569                 ef->reltab = kmalloc(ef->nreltab * sizeof(*ef->reltab),
570                                      M_LINKER, M_WAITOK | M_ZERO);
571         if (ef->nrelatab != 0)
572                 ef->relatab = kmalloc(ef->nrelatab * sizeof(*ef->relatab),
573                                       M_LINKER, M_WAITOK | M_ZERO);
574         if ((ef->nprogtab != 0 && ef->progtab == NULL) ||
575             (ef->nreltab != 0 && ef->reltab == NULL) ||
576             (ef->nrelatab != 0 && ef->relatab == NULL)) {
577                 error = ENOMEM;
578                 goto out;
579         }
580         if (symtabindex == -1)
581                 panic("lost symbol table index");
582         /* Allocate space for and load the symbol table */
583         ef->ddbsymcnt = shdr[symtabindex].sh_size / sizeof(Elf_Sym);
584         ef->ddbsymtab = kmalloc(shdr[symtabindex].sh_size, M_LINKER, M_WAITOK);
585         if (ef->ddbsymtab == NULL) {
586                 error = ENOMEM;
587                 goto out;
588         }
589         error = vn_rdwr(UIO_READ, vp, (void *)ef->ddbsymtab,
590                         shdr[symtabindex].sh_size, shdr[symtabindex].sh_offset,
591                         UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
592         if (error)
593                 goto out;
594         if (resid != 0) {
595                 error = EINVAL;
596                 goto out;
597         }
598         if (symstrindex == -1)
599                 panic("lost symbol string index");
600         /* Allocate space for and load the symbol strings */
601         ef->ddbstrcnt = shdr[symstrindex].sh_size;
602         ef->ddbstrtab = kmalloc(shdr[symstrindex].sh_size, M_LINKER, M_WAITOK);
603         if (ef->ddbstrtab == NULL) {
604                 error = ENOMEM;
605                 goto out;
606         }
607         error = vn_rdwr(UIO_READ, vp, ef->ddbstrtab,
608                         shdr[symstrindex].sh_size, shdr[symstrindex].sh_offset,
609                         UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
610         if (error)
611                 goto out;
612         if (resid != 0) {
613                 error = EINVAL;
614                 goto out;
615         }
616         /* Do we have a string table for the section names?  */
617         shstrindex = -1;
618         if (hdr->e_shstrndx != 0 &&
619             shdr[hdr->e_shstrndx].sh_type == SHT_STRTAB) {
620                 shstrindex = hdr->e_shstrndx;
621                 ef->shstrcnt = shdr[shstrindex].sh_size;
622                 ef->shstrtab = kmalloc(shdr[shstrindex].sh_size, M_LINKER,
623                                        M_WAITOK);
624                 if (ef->shstrtab == NULL) {
625                         error = ENOMEM;
626                         goto out;
627                 }
628                 error = vn_rdwr(UIO_READ, vp, ef->shstrtab,
629                                 shdr[shstrindex].sh_size, shdr[shstrindex].sh_offset,
630                                 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
631                 if (error)
632                         goto out;
633                 if (resid != 0) {
634                         error = EINVAL;
635                         goto out;
636                 }
637         }
638         /* Size up code/data(progbits) and bss(nobits). */
639         alignmask = 0;
640         for (i = 0; i < hdr->e_shnum; i++) {
641                 switch (shdr[i].sh_type) {
642                 case SHT_PROGBITS:
643                 case SHT_NOBITS:
644                         alignmask = shdr[i].sh_addralign - 1;
645                         mapsize += alignmask;
646                         mapsize &= ~alignmask;
647                         mapsize += shdr[i].sh_size;
648                         break;
649                 }
650         }
651
652         /*
653          * We know how much space we need for the text/data/bss/etc. This
654          * stuff needs to be in a single chunk so that profiling etc can get
655          * the bounds and gdb can associate offsets with modules
656          */
657         ef->object = vm_object_allocate(OBJT_DEFAULT,
658                                         round_page(mapsize) >> PAGE_SHIFT);
659         if (ef->object == NULL) {
660                 error = ENOMEM;
661                 goto out;
662         }
663         ef->address = (caddr_t) vm_map_min(&kernel_map);
664         ef->bytes = 0;
665
666         /*
667          * In order to satisfy x86_64's architectural requirements on the
668          * location of code and data in the kernel's address space, request a
669          * mapping that is above the kernel.
670          *
671          * vkernel64's text+data is outside the managed VM space entirely.
672          */
673 #if defined(__amd64__) && defined(_KERNEL_VIRTUAL)
674         error = vkernel_module_memory_alloc(&mapbase, round_page(mapsize));
675 #else
676         mapbase = KERNBASE;
677         error = vm_map_find(&kernel_map, ef->object, 0, &mapbase,
678                             round_page(mapsize), PAGE_SIZE,
679                             TRUE, VM_MAPTYPE_NORMAL,
680                             VM_PROT_ALL, VM_PROT_ALL, FALSE);
681         if (error) {
682                 vm_object_deallocate(ef->object);
683                 ef->object = NULL;
684                 goto out;
685         }
686         /* Wire the pages */
687         error = vm_map_wire(&kernel_map, mapbase,
688                             mapbase + round_page(mapsize), 0);
689 #endif
690         if (error != KERN_SUCCESS) {
691                 error = ENOMEM;
692                 goto out;
693         }
694         /* Inform the kld system about the situation */
695         lf->address = ef->address = (caddr_t) mapbase;
696         lf->size = round_page(mapsize);
697         ef->bytes = mapsize;
698
699         /*
700          * Now load code/data(progbits), zero bss(nobits), allocate space for
701          * and load relocs
702          */
703         pb = 0;
704         rl = 0;
705         ra = 0;
706         alignmask = 0;
707         for (i = 0; i < hdr->e_shnum; i++) {
708                 switch (shdr[i].sh_type) {
709                 case SHT_PROGBITS:
710                 case SHT_NOBITS:
711                         alignmask = shdr[i].sh_addralign - 1;
712                         mapbase += alignmask;
713                         mapbase &= ~alignmask;
714                         if (ef->shstrtab && shdr[i].sh_name != 0)
715                                 ef->progtab[pb].name =
716                                         ef->shstrtab + shdr[i].sh_name;
717                         else if (shdr[i].sh_type == SHT_PROGBITS)
718                                 ef->progtab[pb].name = "<<PROGBITS>>";
719                         else
720                                 ef->progtab[pb].name = "<<NOBITS>>";
721 #if 0
722                         if (ef->progtab[pb].name != NULL &&
723                             !strcmp(ef->progtab[pb].name, "set_pcpu"))
724                                 ef->progtab[pb].addr =
725                                         dpcpu_alloc(shdr[i].sh_size);
726 #ifdef VIMAGE
727                         else if (ef->progtab[pb].name != NULL &&
728                                  !strcmp(ef->progtab[pb].name, VNET_SETNAME))
729                                 ef->progtab[pb].addr =
730                                         vnet_data_alloc(shdr[i].sh_size);
731 #endif
732                         else
733 #endif
734                                 ef->progtab[pb].addr =
735                                         (void *)(uintptr_t) mapbase;
736                         if (ef->progtab[pb].addr == NULL) {
737                                 error = ENOSPC;
738                                 goto out;
739                         }
740                         ef->progtab[pb].size = shdr[i].sh_size;
741                         ef->progtab[pb].sec = i;
742                         if (shdr[i].sh_type == SHT_PROGBITS) {
743                                 error = vn_rdwr(UIO_READ, vp,
744                                                 ef->progtab[pb].addr,
745                                                 shdr[i].sh_size, shdr[i].sh_offset,
746                                                 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
747                                                 &resid);
748                                 if (error)
749                                         goto out;
750                                 if (resid != 0) {
751                                         error = EINVAL;
752                                         goto out;
753                                 }
754 #if 0
755                                 /* Initialize the per-cpu or vnet area. */
756                                 if (ef->progtab[pb].addr != (void *)mapbase &&
757                                     !strcmp(ef->progtab[pb].name, "set_pcpu"))
758                                         dpcpu_copy(ef->progtab[pb].addr,
759                                                    shdr[i].sh_size);
760 #ifdef VIMAGE
761                                 else if (ef->progtab[pb].addr !=
762                                          (void *)mapbase &&
763                                          !strcmp(ef->progtab[pb].name, VNET_SETNAME))
764                                         vnet_data_copy(ef->progtab[pb].addr,
765                                                        shdr[i].sh_size);
766 #endif
767 #endif
768                         } else
769                                 bzero(ef->progtab[pb].addr, shdr[i].sh_size);
770
771                         /* Update all symbol values with the offset. */
772                         for (j = 0; j < ef->ddbsymcnt; j++) {
773                                 es = &ef->ddbsymtab[j];
774                                 if (es->st_shndx != i)
775                                         continue;
776                                 es->st_value += (Elf_Addr) ef->progtab[pb].addr;
777                         }
778                         mapbase += shdr[i].sh_size;
779                         pb++;
780                         break;
781                 case SHT_REL:
782                         ef->reltab[rl].rel = kmalloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
783                         ef->reltab[rl].nrel = shdr[i].sh_size / sizeof(Elf_Rel);
784                         ef->reltab[rl].sec = shdr[i].sh_info;
785                         error = vn_rdwr(UIO_READ, vp,
786                                         (void *)ef->reltab[rl].rel,
787                                         shdr[i].sh_size, shdr[i].sh_offset,
788                                         UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
789                         if (error)
790                                 goto out;
791                         if (resid != 0) {
792                                 error = EINVAL;
793                                 goto out;
794                         }
795                         rl++;
796                         break;
797                 case SHT_RELA:
798                         ef->relatab[ra].rela = kmalloc(shdr[i].sh_size, M_LINKER, M_WAITOK);
799                         ef->relatab[ra].nrela = shdr[i].sh_size / sizeof(Elf_Rela);
800                         ef->relatab[ra].sec = shdr[i].sh_info;
801                         error = vn_rdwr(UIO_READ, vp,
802                                         (void *)ef->relatab[ra].rela,
803                                         shdr[i].sh_size, shdr[i].sh_offset,
804                                         UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid);
805                         if (error)
806                                 goto out;
807                         if (resid != 0) {
808                                 error = EINVAL;
809                                 goto out;
810                         }
811                         ra++;
812                         break;
813                 }
814         }
815         if (pb != ef->nprogtab)
816                 panic("lost progbits");
817         if (rl != ef->nreltab)
818                 panic("lost reltab");
819         if (ra != ef->nrelatab)
820                 panic("lost relatab");
821         if (mapbase != (vm_offset_t) ef->address + mapsize)
822                 panic("mapbase 0x%lx != address %p + mapsize 0x%lx (0x%lx)\n",
823                       mapbase, ef->address, mapsize,
824                       (vm_offset_t) ef->address + mapsize);
825
826         /* Local intra-module relocations */
827         link_elf_obj_reloc_local(lf);
828
829         /* Pull in dependencies */
830         error = linker_load_dependencies(lf);
831         if (error)
832                 goto out;
833
834         /* External relocations */
835         error = relocate_file(lf);
836         if (error)
837                 goto out;
838
839         *result = lf;
840
841 out:
842         if (error && lf)
843                 linker_file_unload(lf /*, LINKER_UNLOAD_FORCE */);
844         if (hdr)
845                 kfree(hdr, M_LINKER);
846         vn_unlock(vp);
847         vn_close(vp, FREAD);
848
849         return error;
850 }
851
852 static void
853 link_elf_obj_unload_file(linker_file_t file)
854 {
855         elf_file_t      ef = file->priv;
856         int i;
857
858         if (ef->progtab) {
859                 for (i = 0; i < ef->nprogtab; i++) {
860                         if (ef->progtab[i].size == 0)
861                                 continue;
862                         if (ef->progtab[i].name == NULL)
863                                 continue;
864 #if 0
865                         if (!strcmp(ef->progtab[i].name, "set_pcpu"))
866                                 dpcpu_free(ef->progtab[i].addr,
867                                     ef->progtab[i].size);
868 #ifdef VIMAGE
869                         else if (!strcmp(ef->progtab[i].name, VNET_SETNAME))
870                                 vnet_data_free(ef->progtab[i].addr,
871                                     ef->progtab[i].size);
872 #endif
873 #endif
874                 }
875         }
876         if (ef->preloaded) {
877                 if (ef->reltab)
878                         kfree(ef->reltab, M_LINKER);
879                 if (ef->relatab)
880                         kfree(ef->relatab, M_LINKER);
881                 if (ef->progtab)
882                         kfree(ef->progtab, M_LINKER);
883                 if (ef->ctftab)
884                         kfree(ef->ctftab, M_LINKER);
885                 if (ef->ctfoff)
886                         kfree(ef->ctfoff, M_LINKER);
887                 if (ef->typoff)
888                         kfree(ef->typoff, M_LINKER);
889                 if (file->filename != NULL)
890                         preload_delete_name(file->filename);
891                 kfree(ef, M_LINKER);
892                 /* XXX reclaim module memory? */
893                 return;
894         }
895
896         for (i = 0; i < ef->nreltab; i++)
897                 if (ef->reltab[i].rel)
898                         kfree(ef->reltab[i].rel, M_LINKER);
899         for (i = 0; i < ef->nrelatab; i++)
900                 if (ef->relatab[i].rela)
901                         kfree(ef->relatab[i].rela, M_LINKER);
902         if (ef->reltab)
903                 kfree(ef->reltab, M_LINKER);
904         if (ef->relatab)
905                 kfree(ef->relatab, M_LINKER);
906         if (ef->progtab)
907                 kfree(ef->progtab, M_LINKER);
908
909         if (ef->object) {
910 #if defined(__amd64__) && defined(_KERNEL_VIRTUAL)
911                 vkernel_module_memory_free((vm_offset_t)ef->address, ef->bytes);
912 #else
913                 vm_map_remove(&kernel_map, (vm_offset_t) ef->address,
914                     (vm_offset_t) ef->address +
915                     (ef->object->size << PAGE_SHIFT));
916 #endif
917                 vm_object_deallocate(ef->object);
918                 ef->object = NULL;
919         }
920         if (ef->e_shdr)
921                 kfree(ef->e_shdr, M_LINKER);
922         if (ef->ddbsymtab)
923                 kfree(ef->ddbsymtab, M_LINKER);
924         if (ef->ddbstrtab)
925                 kfree(ef->ddbstrtab, M_LINKER);
926         if (ef->shstrtab)
927                 kfree(ef->shstrtab, M_LINKER);
928         if (ef->ctftab)
929                 kfree(ef->ctftab, M_LINKER);
930         if (ef->ctfoff)
931                 kfree(ef->ctfoff, M_LINKER);
932         if (ef->typoff)
933                 kfree(ef->typoff, M_LINKER);
934         kfree(ef, M_LINKER);
935 }
936
937 static const char *
938 symbol_name(elf_file_t ef, Elf_Size r_info)
939 {
940         const Elf_Sym  *ref;
941
942         if (ELF_R_SYM(r_info)) {
943                 ref = ef->ddbsymtab + ELF_R_SYM(r_info);
944                 return ef->ddbstrtab + ref->st_name;
945         } else
946                 return NULL;
947 }
948
949 static Elf_Addr
950 findbase(elf_file_t ef, int sec)
951 {
952         int i;
953         Elf_Addr base = 0;
954
955         for (i = 0; i < ef->nprogtab; i++) {
956                 if (sec == ef->progtab[i].sec) {
957                         base = (Elf_Addr)ef->progtab[i].addr;
958                         break;
959                 }
960         }
961         return base;
962 }
963
964 static int
965 relocate_file(linker_file_t lf)
966 {
967         elf_file_t      ef = lf->priv;
968         const Elf_Rel *rellim;
969         const Elf_Rel *rel;
970         const Elf_Rela *relalim;
971         const Elf_Rela *rela;
972         const char *symname;
973         const Elf_Sym *sym;
974         int i;
975         Elf_Size symidx;
976         Elf_Addr base;
977
978         /* Perform relocations without addend if there are any: */
979         for (i = 0; i < ef->nreltab; i++) {
980                 rel = ef->reltab[i].rel;
981                 if (rel == NULL)
982                         panic("lost a reltab!");
983                 rellim = rel + ef->reltab[i].nrel;
984                 base = findbase(ef, ef->reltab[i].sec);
985                 if (base == 0)
986                         panic("lost base for reltab");
987                 for ( ; rel < rellim; rel++) {
988                         symidx = ELF_R_SYM(rel->r_info);
989                         if (symidx >= ef->ddbsymcnt)
990                                 continue;
991                         sym = ef->ddbsymtab + symidx;
992                         /* Local relocs are already done */
993                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
994                                 continue;
995                         if (elf_reloc(lf, base, rel, ELF_RELOC_REL,
996                             elf_obj_lookup)) {
997                                 symname = symbol_name(ef, rel->r_info);
998                                 kprintf("link_elf_obj_obj: symbol %s undefined\n",
999                                     symname);
1000                                 return ENOENT;
1001                         }
1002                 }
1003         }
1004
1005         /* Perform relocations with addend if there are any: */
1006         for (i = 0; i < ef->nrelatab; i++) {
1007                 rela = ef->relatab[i].rela;
1008                 if (rela == NULL)
1009                         panic("lost a relatab!");
1010                 relalim = rela + ef->relatab[i].nrela;
1011                 base = findbase(ef, ef->relatab[i].sec);
1012                 if (base == 0)
1013                         panic("lost base for relatab");
1014                 for ( ; rela < relalim; rela++) {
1015                         symidx = ELF_R_SYM(rela->r_info);
1016                         if (symidx >= ef->ddbsymcnt)
1017                                 continue;
1018                         sym = ef->ddbsymtab + symidx;
1019                         /* Local relocs are already done */
1020                         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL)
1021                                 continue;
1022                         if (elf_reloc(lf, base, rela, ELF_RELOC_RELA,
1023                             elf_obj_lookup)) {
1024                                 symname = symbol_name(ef, rela->r_info);
1025                                 kprintf("link_elf_obj_obj: symbol %s undefined\n",
1026                                     symname);
1027                                 return ENOENT;
1028                         }
1029                 }
1030         }
1031
1032         return 0;
1033 }
1034
1035 static int
1036 link_elf_obj_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
1037 {
1038         elf_file_t ef = lf->priv;
1039         const Elf_Sym *symp;
1040         const char *strp;
1041         int i;
1042
1043         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1044                 strp = ef->ddbstrtab + symp->st_name;
1045                 if (symp->st_shndx != SHN_UNDEF && strcmp(name, strp) == 0) {
1046                         *sym = (c_linker_sym_t) symp;
1047                         return 0;
1048                 }
1049         }
1050         return ENOENT;
1051 }
1052
1053 static int
1054 link_elf_obj_symbol_values(linker_file_t lf, c_linker_sym_t sym,
1055     linker_symval_t *symval)
1056 {
1057         elf_file_t ef = lf->priv;
1058         const Elf_Sym *es = (const Elf_Sym*) sym;
1059
1060         if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1061                 symval->name = ef->ddbstrtab + es->st_name;
1062                 symval->value = (caddr_t)es->st_value;
1063                 symval->size = es->st_size;
1064                 return 0;
1065         }
1066         return ENOENT;
1067 }
1068
1069 static int
1070 link_elf_obj_search_symbol(linker_file_t lf, caddr_t value,
1071     c_linker_sym_t *sym, long *diffp)
1072 {
1073         elf_file_t ef = lf->priv;
1074         u_long off = (uintptr_t) (void *) value;
1075         u_long diff = off;
1076         u_long st_value;
1077         const Elf_Sym *es;
1078         const Elf_Sym *best = 0;
1079         int i;
1080
1081         for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1082                 if (es->st_name == 0)
1083                         continue;
1084                 st_value = es->st_value;
1085                 if (off >= st_value) {
1086                         if (off - st_value < diff) {
1087                                 diff = off - st_value;
1088                                 best = es;
1089                                 if (diff == 0)
1090                                         break;
1091                         } else if (off - st_value == diff) {
1092                                 best = es;
1093                         }
1094                 }
1095         }
1096         if (best == 0)
1097                 *diffp = off;
1098         else
1099                 *diffp = diff;
1100         *sym = (c_linker_sym_t) best;
1101
1102         return 0;
1103 }
1104
1105 /*
1106  * Look up a linker set on an ELF system.
1107  */
1108 static int
1109 link_elf_obj_lookup_set(linker_file_t lf, const char *name,
1110     void ***startp, void ***stopp, int *countp)
1111 {
1112         elf_file_t ef = lf->priv;
1113         void **start, **stop;
1114         int i, count;
1115
1116         /* Relative to section number */
1117         for (i = 0; i < ef->nprogtab; i++) {
1118                 if ((strncmp(ef->progtab[i].name, "set_", 4) == 0) &&
1119                     strcmp(ef->progtab[i].name + 4, name) == 0) {
1120                         start  = (void **)ef->progtab[i].addr;
1121                         stop = (void **)((char *)ef->progtab[i].addr +
1122                             ef->progtab[i].size);
1123                         count = stop - start;
1124                         if (startp)
1125                                 *startp = start;
1126                         if (stopp)
1127                                 *stopp = stop;
1128                         if (countp)
1129                                 *countp = count;
1130                         return (0);
1131                 }
1132         }
1133         return (ESRCH);
1134 }
1135
1136 /*
1137  * Symbol lookup function that can be used when the symbol index is known (ie
1138  * in relocations). It uses the symbol index instead of doing a fully fledged
1139  * hash table based lookup when such is valid. For example for local symbols.
1140  * This is not only more efficient, it's also more correct. It's not always
1141  * the case that the symbol can be found through the hash table.
1142  */
1143 static int
1144 elf_obj_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *result)
1145 {
1146         elf_file_t ef = lf->priv;
1147         const Elf_Sym *sym;
1148         const char *symbol;
1149
1150         /* Don't even try to lookup the symbol if the index is bogus. */
1151         if (symidx >= ef->ddbsymcnt)
1152                 return (ENOENT);
1153
1154         sym = ef->ddbsymtab + symidx;
1155
1156         /* Quick answer if there is a definition included. */
1157         if (sym->st_shndx != SHN_UNDEF) {
1158                 *result = sym->st_value;
1159                 return (0);
1160         }
1161
1162         /* If we get here, then it is undefined and needs a lookup. */
1163         switch (ELF_ST_BIND(sym->st_info)) {
1164         case STB_LOCAL:
1165                 /* Local, but undefined? huh? */
1166                 return (ENOENT);
1167
1168         case STB_GLOBAL:
1169                 /* Relative to Data or Function name */
1170                 symbol = ef->ddbstrtab + sym->st_name;
1171
1172                 /* Force a lookup failure if the symbol name is bogus. */
1173                 if (*symbol == 0)
1174                         return (ENOENT);
1175                 return (linker_file_lookup_symbol(lf, symbol, deps, (caddr_t *)result));
1176
1177         case STB_WEAK:
1178                 kprintf("link_elf_obj_obj: Weak symbols not supported\n");
1179                 return (ENOENT);
1180
1181         default:
1182                 return (ENOENT);
1183         }
1184 }
1185
1186 static void
1187 link_elf_obj_fix_link_set(elf_file_t ef)
1188 {
1189         static const char startn[] = "__start_";
1190         static const char stopn[] = "__stop_";
1191         Elf_Sym *sym;
1192         const char *sym_name, *linkset_name;
1193         Elf_Addr startp, stopp;
1194         Elf_Size symidx;
1195         int start, i;
1196
1197         startp = stopp = 0;
1198         for (symidx = 1 /* zero entry is special */;
1199                 symidx < ef->ddbsymcnt; symidx++) {
1200                 sym = ef->ddbsymtab + symidx;
1201                 if (sym->st_shndx != SHN_UNDEF)
1202                         continue;
1203
1204                 sym_name = ef->ddbstrtab + sym->st_name;
1205                 if (strncmp(sym_name, startn, sizeof(startn) - 1) == 0) {
1206                         start = 1;
1207                         linkset_name = sym_name + sizeof(startn) - 1;
1208                 }
1209                 else if (strncmp(sym_name, stopn, sizeof(stopn) - 1) == 0) {
1210                         start = 0;
1211                         linkset_name = sym_name + sizeof(stopn) - 1;
1212                 }
1213                 else
1214                         continue;
1215
1216                 for (i = 0; i < ef->nprogtab; i++) {
1217                         if (strcmp(ef->progtab[i].name, linkset_name) == 0) {
1218                                 startp = (Elf_Addr)ef->progtab[i].addr;
1219                                 stopp = (Elf_Addr)(startp + ef->progtab[i].size);
1220                                 break;
1221                         }
1222                 }
1223                 if (i == ef->nprogtab)
1224                         continue;
1225
1226                 sym->st_value = start ? startp : stopp;
1227                 sym->st_shndx = i;
1228         }
1229 }
1230
1231 static void
1232 link_elf_obj_reloc_local(linker_file_t lf)
1233 {
1234         elf_file_t ef = lf->priv;
1235         const Elf_Rel *rellim;
1236         const Elf_Rel *rel;
1237         const Elf_Rela *relalim;
1238         const Elf_Rela *rela;
1239         const Elf_Sym *sym;
1240         Elf_Addr base;
1241         int i;
1242         Elf_Size symidx;
1243
1244         link_elf_obj_fix_link_set(ef);
1245
1246         /* Perform relocations without addend if there are any: */
1247         for (i = 0; i < ef->nreltab; i++) {
1248                 rel = ef->reltab[i].rel;
1249                 if (rel == NULL)
1250                         panic("lost a reltab!");
1251                 rellim = rel + ef->reltab[i].nrel;
1252                 base = findbase(ef, ef->reltab[i].sec);
1253                 if (base == 0)
1254                         panic("lost base for reltab");
1255                 for ( ; rel < rellim; rel++) {
1256                         symidx = ELF_R_SYM(rel->r_info);
1257                         if (symidx >= ef->ddbsymcnt)
1258                                 continue;
1259                         sym = ef->ddbsymtab + symidx;
1260                         /* Only do local relocs */
1261                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1262                                 continue;
1263                         elf_reloc_local(lf, base, rel, ELF_RELOC_REL,
1264                             elf_obj_lookup);
1265                 }
1266         }
1267
1268         /* Perform relocations with addend if there are any: */
1269         for (i = 0; i < ef->nrelatab; i++) {
1270                 rela = ef->relatab[i].rela;
1271                 if (rela == NULL)
1272                         panic("lost a relatab!");
1273                 relalim = rela + ef->relatab[i].nrela;
1274                 base = findbase(ef, ef->relatab[i].sec);
1275                 if (base == 0)
1276                         panic("lost base for relatab");
1277                 for ( ; rela < relalim; rela++) {
1278                         symidx = ELF_R_SYM(rela->r_info);
1279                         if (symidx >= ef->ddbsymcnt)
1280                                 continue;
1281                         sym = ef->ddbsymtab + symidx;
1282                         /* Only do local relocs */
1283                         if (ELF_ST_BIND(sym->st_info) != STB_LOCAL)
1284                                 continue;
1285                         elf_reloc_local(lf, base, rela, ELF_RELOC_RELA,
1286                             elf_obj_lookup);
1287                 }
1288         }
1289 }