]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/common/load_elf.c
Initiate deorbit burn of i386 a.out kld "support" in loader. Note that
[FreeBSD/FreeBSD.git] / sys / boot / common / load_elf.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
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$
28  */
29
30 #include <sys/param.h>
31 #include <sys/exec.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <string.h>
36 #include <machine/elf.h>
37 #include <stand.h>
38 #define FREEBSD_ELF
39 #include <link.h>
40
41 #include "bootstrap.h"
42
43 #define COPYOUT(s,d,l)  archsw.arch_copyout((vm_offset_t)(s), d, l)
44
45
46 typedef struct elf_file {
47     Elf_Phdr    *ph;
48     Elf_Ehdr    *ehdr;
49     Elf_Sym     *symtab;
50     Elf_Hashelt *hashtab;
51     Elf_Hashelt nbuckets;
52     Elf_Hashelt nchains;
53     Elf_Hashelt *buckets;
54     Elf_Hashelt *chains;
55     char        *strtab;
56     size_t      strsz;
57     int         fd;
58     caddr_t     firstpage;
59     size_t      firstlen;
60     int         kernel;
61     vm_offset_t off;
62 } *elf_file_t;
63
64 static int elf_loadimage(struct preloaded_file *mp, elf_file_t ef, vm_offset_t loadaddr);
65 static int elf_lookup_symbol(struct preloaded_file *mp, elf_file_t ef, const char* name,        Elf_Sym* sym);
66 static int elf_parse_modmetadata(struct preloaded_file *mp, elf_file_t ef);
67 static char     *fake_modname(const char *name);
68
69 const char      *elf_kerneltype = "elf kernel";
70 const char      *elf_moduletype = "elf module";
71
72 /*
73  * Attempt to load the file (file) as an ELF module.  It will be stored at
74  * (dest), and a pointer to a module structure describing the loaded object
75  * will be saved in (result).
76  */
77 int
78 elf_loadfile(char *filename, vm_offset_t dest, struct preloaded_file **result)
79 {
80     struct preloaded_file       *fp, *kfp;
81     struct elf_file             ef;
82     Elf_Ehdr                    *ehdr;
83     int                         err;
84     u_int                       pad;
85     ssize_t                     bytes_read;
86
87     fp = NULL;
88     bzero(&ef, sizeof(struct elf_file));
89     
90     /*
91      * Open the image, read and validate the ELF header 
92      */
93     if (filename == NULL)       /* can't handle nameless */
94         return(EFTYPE);
95     if ((ef.fd = open(filename, O_RDONLY)) == -1)
96         return(errno);
97     ef.firstpage = malloc(PAGE_SIZE);
98     if (ef.firstpage == NULL) {
99         close(ef.fd);
100         return(ENOMEM);
101     }
102     bytes_read = read(ef.fd, ef.firstpage, PAGE_SIZE);
103     ef.firstlen = (size_t)bytes_read;
104     if (bytes_read < 0 || ef.firstlen <= sizeof(Elf_Ehdr)) {
105         err = EFTYPE;           /* could be EIO, but may be small file */
106         goto oerr;
107     }
108     ehdr = ef.ehdr = (Elf_Ehdr *)ef.firstpage;
109
110     /* Is it ELF? */
111     if (!IS_ELF(*ehdr)) {
112         err = EFTYPE;
113         goto oerr;
114     }
115     if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||    /* Layout ? */
116         ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
117         ehdr->e_ident[EI_VERSION] != EV_CURRENT ||      /* Version ? */
118         ehdr->e_version != EV_CURRENT ||
119         ehdr->e_machine != ELF_TARG_MACH) {             /* Machine ? */
120         err = EFTYPE;
121         goto oerr;
122     }
123
124
125     /*
126      * Check to see what sort of module we are.
127      */
128     kfp = file_findfile(NULL, NULL);
129     if (ehdr->e_type == ET_DYN) {
130         /* Looks like a kld module */
131         if (kfp == NULL) {
132             printf("elf_loadfile: can't load module before kernel\n");
133             err = EPERM;
134             goto oerr;
135         }
136         if (strcmp(elf_kerneltype, kfp->f_type)) {
137             printf("elf_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
138             err = EPERM;
139             goto oerr;
140         }
141         /* Looks OK, got ahead */
142         ef.kernel = 0;
143
144         /* Page-align the load address */
145         pad = (u_int)dest & PAGE_MASK;
146         if (pad != 0) {
147             pad = PAGE_SIZE - pad;
148             dest += pad;
149         }
150     } else if (ehdr->e_type == ET_EXEC) {
151         /* Looks like a kernel */
152         if (kfp != NULL) {
153             printf("elf_loadfile: kernel already loaded\n");
154             err = EPERM;
155             goto oerr;
156         }
157         /* 
158          * Calculate destination address based on kernel entrypoint     
159          */
160         dest = (vm_offset_t) ehdr->e_entry;
161         if (dest == 0) {
162             printf("elf_loadfile: not a kernel (maybe static binary?)\n");
163             err = EPERM;
164             goto oerr;
165         }
166         ef.kernel = 1;
167
168     } else {
169         err = EFTYPE;
170         goto oerr;
171     }
172
173     /* 
174      * Ok, we think we should handle this.
175      */
176     fp = file_alloc();
177     if (fp == NULL) {
178             printf("elf_loadfile: cannot allocate module info\n");
179             err = EPERM;
180             goto out;
181     }
182     if (ef.kernel)
183         setenv("kernelname", filename, 1);
184     fp->f_name = strdup(filename);
185     fp->f_type = strdup(ef.kernel ? elf_kerneltype : elf_moduletype);
186
187 #ifdef ELF_VERBOSE
188     if (ef.kernel)
189         printf("%s entry at %p\n", filename, (void *) dest);
190 #else
191     printf("%s ", filename);
192 #endif
193
194     fp->f_size = elf_loadimage(fp, &ef, dest);
195     if (fp->f_size == 0 || fp->f_addr == 0)
196         goto ioerr;
197
198     /* save exec header as metadata */
199     file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);
200
201     /* Load OK, return module pointer */
202     *result = (struct preloaded_file *)fp;
203     err = 0;
204     goto out;
205     
206  ioerr:
207     err = EIO;
208  oerr:
209     file_discard(fp);
210  out:
211     if (ef.firstpage)
212         free(ef.firstpage);
213     close(ef.fd);
214     return(err);
215 }
216
217 /*
218  * With the file (fd) open on the image, and (ehdr) containing
219  * the Elf header, load the image at (off)
220  */
221 static int
222 elf_loadimage(struct preloaded_file *fp, elf_file_t ef, vm_offset_t off)
223 {
224     int         i;
225     u_int       j;
226     Elf_Ehdr    *ehdr;
227     Elf_Phdr    *phdr, *php;
228     Elf_Shdr    *shdr;
229     int         ret;
230     vm_offset_t firstaddr;
231     vm_offset_t lastaddr;
232     void        *buf;
233     size_t      resid, chunk;
234     ssize_t     result;
235     vm_offset_t dest;
236     vm_offset_t ssym, esym;
237     Elf_Dyn     *dp;
238     int         ndp;
239     int         symstrindex;
240     int         symtabindex;
241     long        size;
242     u_int       fpcopy;
243
244     dp = NULL;
245     shdr = NULL;
246     ret = 0;
247     firstaddr = lastaddr = 0;
248     ehdr = ef->ehdr;
249     if (ef->kernel) {
250 #ifdef __i386__
251         off = - (off & 0xff000000u);    /* i386 relocates after locore */
252 #else
253         off = 0;                /* alpha is direct mapped for kernels */
254 #endif
255     }
256     ef->off = off;
257
258     if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
259         printf("elf_loadimage: program header not within first page\n");
260         goto out;
261     }
262     phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
263
264     for (i = 0; i < ehdr->e_phnum; i++) {
265         /* We want to load PT_LOAD segments only.. */
266         if (phdr[i].p_type != PT_LOAD)
267             continue;
268
269 #ifdef ELF_VERBOSE
270         printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
271             (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
272             (long)(phdr[i].p_vaddr + off),
273             (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
274 #else
275         if ((phdr[i].p_flags & PF_W) == 0) {
276             printf("text=0x%lx ", (long)phdr[i].p_filesz);
277         } else {
278             printf("data=0x%lx", (long)phdr[i].p_filesz);
279             if (phdr[i].p_filesz < phdr[i].p_memsz)
280                 printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
281             printf(" ");
282         }
283 #endif
284         fpcopy = 0;
285         if (ef->firstlen > phdr[i].p_offset) {
286             fpcopy = ef->firstlen - phdr[i].p_offset;
287             archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
288                                phdr[i].p_vaddr + off, fpcopy);
289         }
290         if (phdr[i].p_filesz > fpcopy) {
291             if (lseek(ef->fd, (off_t)(phdr[i].p_offset + fpcopy),
292                       SEEK_SET) == -1) {
293                 printf("\nelf_loadexec: cannot seek\n");
294                 goto out;
295             }
296             if (archsw.arch_readin(ef->fd, phdr[i].p_vaddr + off + fpcopy,
297                 phdr[i].p_filesz - fpcopy) != (ssize_t)(phdr[i].p_filesz - fpcopy)) {
298                 printf("\nelf_loadexec: archsw.readin failed\n");
299                 goto out;
300             }
301         }
302         /* clear space from oversized segments; eg: bss */
303         if (phdr[i].p_filesz < phdr[i].p_memsz) {
304 #ifdef ELF_VERBOSE
305             printf(" (bss: 0x%lx-0x%lx)",
306                 (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
307                 (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
308 #endif
309
310             /* no archsw.arch_bzero */
311             buf = malloc(PAGE_SIZE);
312             if (buf == NULL) {
313                 printf("\nelf_loadimage: malloc() failed\n");
314                 goto out;
315             }
316             bzero(buf, PAGE_SIZE);
317             resid = phdr[i].p_memsz - phdr[i].p_filesz;
318             dest = phdr[i].p_vaddr + off + phdr[i].p_filesz;
319             while (resid > 0) {
320                 chunk = min(PAGE_SIZE, resid);
321                 archsw.arch_copyin(buf, dest, chunk);
322                 resid -= chunk;
323                 dest += chunk;
324             }
325             free(buf);
326         }
327 #ifdef ELF_VERBOSE
328         printf("\n");
329 #endif
330
331         if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
332             firstaddr = phdr[i].p_vaddr + off;
333         if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
334             lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
335     }
336     lastaddr = roundup(lastaddr, sizeof(long));
337
338     /*
339      * Now grab the symbol tables.  This isn't easy if we're reading a
340      * .gz file.  I think the rule is going to have to be that you must
341      * strip a file to remove symbols before gzipping it so that we do not
342      * try to lseek() on it.
343      */
344     chunk = ehdr->e_shnum * ehdr->e_shentsize;
345     if (chunk == 0 || ehdr->e_shoff == 0)
346         goto nosyms;
347     shdr = malloc(chunk);
348     if (shdr == NULL)
349         goto nosyms;
350     if (lseek(ef->fd, (off_t)ehdr->e_shoff, SEEK_SET) == -1) {
351         printf("\nelf_loadimage: cannot lseek() to section headers");
352         goto nosyms;
353     }
354     result = read(ef->fd, shdr, chunk);
355     if (result < 0 || (size_t)result != chunk) {
356         printf("\nelf_loadimage: read section headers failed");
357         goto nosyms;
358     }
359     symtabindex = -1;
360     symstrindex = -1;
361     for (i = 0; i < ehdr->e_shnum; i++) {
362         if (shdr[i].sh_type != SHT_SYMTAB)
363             continue;
364         for (j = 0; j < ehdr->e_phnum; j++) {
365             if (phdr[j].p_type != PT_LOAD)
366                 continue;
367             if (shdr[i].sh_offset >= phdr[j].p_offset &&
368                 (shdr[i].sh_offset + shdr[i].sh_size <=
369                  phdr[j].p_offset + phdr[j].p_filesz)) {
370                 shdr[i].sh_offset = 0;
371                 shdr[i].sh_size = 0;
372                 break;
373             }
374         }
375         if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
376             continue;           /* alread loaded in a PT_LOAD above */
377         /* Save it for loading below */
378         symtabindex = i;
379         symstrindex = shdr[i].sh_link;
380     }
381     if (symtabindex < 0 || symstrindex < 0)
382         goto nosyms;
383
384     /* Ok, committed to a load. */
385 #ifndef ELF_VERBOSE
386     printf("syms=[");
387 #endif
388     ssym = lastaddr;
389     for (i = symtabindex; i >= 0; i = symstrindex) {
390 #ifdef ELF_VERBOSE
391         char    *secname;
392
393         switch(shdr[i].sh_type) {
394             case SHT_SYMTAB:            /* Symbol table */
395                 secname = "symtab";
396                 break;
397             case SHT_STRTAB:            /* String table */
398                 secname = "strtab";
399                 break;
400             default:
401                 secname = "WHOA!!";
402                 break;
403         }
404 #endif
405
406         size = shdr[i].sh_size;
407         archsw.arch_copyin(&size, lastaddr, sizeof(size));
408         lastaddr += sizeof(long);
409
410 #ifdef ELF_VERBOSE
411         printf("\n%s: 0x%lx@0x%lx -> 0x%lx-0x%lx", secname,
412             shdr[i].sh_size, shdr[i].sh_offset,
413             lastaddr, lastaddr + shdr[i].sh_size);
414 #else
415         if (i == symstrindex)
416             printf("+");
417         printf("0x%lx+0x%lx", (long)sizeof(size), size);
418 #endif
419
420         if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
421             printf("\nelf_loadimage: could not seek for symbols - skipped!");
422             lastaddr = ssym;
423             ssym = 0;
424             goto nosyms;
425         }
426         result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
427         if (result < 0 || (size_t)result != shdr[i].sh_size) {
428             printf("\nelf_loadimage: could not read symbols - skipped!");
429             lastaddr = ssym;
430             ssym = 0;
431             goto nosyms;
432         }
433         /* Reset offsets relative to ssym */
434         lastaddr += shdr[i].sh_size;
435         lastaddr = roundup(lastaddr, sizeof(long));
436         if (i == symtabindex)
437             symtabindex = -1;
438         else if (i == symstrindex)
439             symstrindex = -1;
440     }
441     esym = lastaddr;
442 #ifndef ELF_VERBOSE
443     printf("]");
444 #endif
445
446     file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
447     file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);
448
449 nosyms:
450     printf("\n");
451
452     ret = lastaddr - firstaddr;
453     fp->f_addr = firstaddr;
454
455     php = NULL;
456     for (i = 0; i < ehdr->e_phnum; i++) {
457         if (phdr[i].p_type == PT_DYNAMIC) {
458             php = phdr + i;
459             dp = (Elf_Dyn *)(php->p_vaddr);
460             file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(dp), &dp);
461             dp = NULL;
462             break;
463         }
464     }
465
466     if (php == NULL)    /* this is bad, we cannot get to symbols or _DYNAMIC */
467         goto out;
468
469     ndp = php->p_filesz / sizeof(Elf_Dyn);
470     if (ndp == 0)
471         goto out;
472     dp = malloc(php->p_filesz);
473     if (dp == NULL)
474         goto out;
475     archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);
476
477     ef->strsz = 0;
478     for (i = 0; i < ndp; i++) {
479         if (dp[i].d_tag == NULL)
480             break;
481         switch (dp[i].d_tag) {
482         case DT_HASH:
483             ef->hashtab = (Elf_Hashelt*)(dp[i].d_un.d_ptr + off);
484             break;
485         case DT_STRTAB:
486             ef->strtab = (char *)(dp[i].d_un.d_ptr + off);
487             break;
488         case DT_STRSZ:
489             ef->strsz = dp[i].d_un.d_val;
490             break;
491         case DT_SYMTAB:
492             ef->symtab = (Elf_Sym*)(dp[i].d_un.d_ptr + off);
493             break;
494         default:
495             break;
496         }
497     }
498     if (ef->hashtab == NULL || ef->symtab == NULL ||
499         ef->strtab == NULL || ef->strsz == 0)
500         goto out;
501     COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
502     COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
503     ef->buckets = ef->hashtab + 2;
504     ef->chains = ef->buckets + ef->nbuckets;
505     if (elf_parse_modmetadata(fp, ef) == 0)
506         goto out;
507
508     if (ef->kernel)                     /* kernel must not depend on anything */
509         goto out;
510
511 out:
512     if (dp)
513         free(dp);
514     if (shdr)
515         free(shdr);
516     return ret;
517 }
518
519 static char invalid_name[] = "bad";
520
521 char *
522 fake_modname(const char *name)
523 {
524     const char *sp, *ep;
525     char *fp;
526     size_t len;
527
528     sp = strrchr(name, '/');
529     if (sp)
530         sp++;
531     else
532         sp = name;
533     ep = strrchr(name, '.');
534     if (ep) {
535             if (ep == name) {
536                 sp = invalid_name;
537                 ep = invalid_name + sizeof(invalid_name) - 1;
538             } 
539     } else
540         ep = name + strlen(name);
541     len = ep - sp;
542     fp = malloc(len + 1);
543     if (fp == NULL)
544         return NULL;
545     memcpy(fp, sp, len);
546     fp[len] = '\0';
547     return fp;
548 }
549
550 int
551 elf_parse_modmetadata(struct preloaded_file *fp, elf_file_t ef)
552 {
553     struct mod_metadata md;
554     struct mod_depend *mdepend;
555     struct mod_version mver;
556     Elf_Sym sym;
557     char *s, *v, **p, **p_stop;
558     int modcnt, minfolen;
559
560     if (elf_lookup_symbol(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
561         return ENOENT;
562     p = (char **)(sym.st_value + ef->off);
563     if (elf_lookup_symbol(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
564         return ENOENT;
565     p_stop = (char **)(sym.st_value + ef->off);
566
567     modcnt = 0;
568     while (p < p_stop) {
569         COPYOUT(p++, &v, sizeof(v));
570         COPYOUT(v + ef->off, &md, sizeof(md));
571         switch(md.md_type) {
572           case MDT_DEPEND:
573             if (ef->kernel)             /* kernel must not depend on anything */
574               break;
575             s = strdupout((vm_offset_t)(md.md_cval + ef->off));
576             minfolen = sizeof(*mdepend) + strlen(s) + 1;
577             mdepend = malloc(minfolen);
578             if (mdepend == NULL)
579                 return ENOMEM;
580             COPYOUT((vm_offset_t)(md.md_data + ef->off), mdepend, sizeof(*mdepend));
581             strcpy((char*)(mdepend + 1), s);
582             free(s);
583             file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
584             free(mdepend);
585             break;
586           case MDT_VERSION:
587             s = strdupout((vm_offset_t)(md.md_cval + ef->off));
588             COPYOUT((vm_offset_t)(md.md_data + ef->off), &mver, sizeof(mver));
589             file_addmodule(fp, s, mver.mv_version, NULL);
590             free(s);
591             modcnt++;
592             break;
593         }
594     }
595     if (modcnt == 0) {
596         s = fake_modname(fp->f_name);
597         file_addmodule(fp, s, 1, NULL);
598         free(s);
599     }
600     return 0;
601 }
602
603 static unsigned long
604 elf_hash(const char *name)
605 {
606     const unsigned char *p = (const unsigned char *) name;
607     unsigned long h = 0;
608     unsigned long g;
609
610     while (*p != '\0') {
611         h = (h << 4) + *p++;
612         if ((g = h & 0xf0000000) != 0)
613             h ^= g >> 24;
614         h &= ~g;
615     }
616     return h;
617 }
618
619 static const char elf_bad_symtable[] = "elf_lookup_symbol: corrupt symbol table\n";
620 int
621 elf_lookup_symbol(struct preloaded_file *fp, elf_file_t ef, const char* name,
622                   Elf_Sym *symp)
623 {
624     Elf_Hashelt symnum;
625     Elf_Sym sym;
626     char *strp;
627     unsigned long hash;
628
629     hash = elf_hash(name);
630     COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));
631
632     while (symnum != STN_UNDEF) {
633         if (symnum >= ef->nchains) {
634             printf(elf_bad_symtable);
635             return ENOENT;
636         }
637
638         COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
639         if (sym.st_name == 0) {
640             printf(elf_bad_symtable);
641             return ENOENT;
642         }
643
644         strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
645         if (strcmp(name, strp) == 0) {
646             free(strp);
647             if (sym.st_shndx != SHN_UNDEF ||
648                 (sym.st_value != 0 &&
649                  ELF_ST_TYPE(sym.st_info) == STT_FUNC)) {
650                 *symp = sym;
651                 return 0;
652             }
653             return ENOENT;
654         }
655         free(strp);
656         COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
657     }
658     return ENOENT;
659 }