]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/link_elf.c
Add hooks for the Compact C Type Format (CTF) data to be attached to
[FreeBSD/FreeBSD.git] / sys / kern / link_elf.c
1 /*-
2  * Copyright (c) 1998-2000 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ddb.h"
31 #include "opt_gdb.h"
32 #include "opt_mac.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #ifdef GPROF
37 #include <sys/gmon.h>
38 #endif
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/mount.h>
44 #include <sys/proc.h>
45 #include <sys/namei.h>
46 #include <sys/fcntl.h>
47 #include <sys/vnode.h>
48 #include <sys/linker.h>
49
50 #include <machine/elf.h>
51
52 #include <security/mac/mac_framework.h>
53
54 #include <vm/vm.h>
55 #include <vm/vm_param.h>
56 #ifdef SPARSE_MAPPING
57 #include <vm/vm_object.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_extern.h>
60 #endif
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63
64 #include <sys/link_elf.h>
65
66 #ifdef DDB_CTF
67 #include <net/zlib.h>
68 #endif
69
70 #include "linker_if.h"
71
72 #define MAXSEGS 4
73
74 typedef struct elf_file {
75     struct linker_file  lf;             /* Common fields */
76     int                 preloaded;      /* Was file pre-loaded */
77     caddr_t             address;        /* Relocation address */
78 #ifdef SPARSE_MAPPING
79     vm_object_t         object;         /* VM object to hold file pages */
80 #endif
81     Elf_Dyn*            dynamic;        /* Symbol table etc. */
82     Elf_Hashelt         nbuckets;       /* DT_HASH info */
83     Elf_Hashelt         nchains;
84     const Elf_Hashelt*  buckets;
85     const Elf_Hashelt*  chains;
86     caddr_t             hash;
87     caddr_t             strtab;         /* DT_STRTAB */
88     int                 strsz;          /* DT_STRSZ */
89     const Elf_Sym*      symtab;         /* DT_SYMTAB */
90     Elf_Addr*           got;            /* DT_PLTGOT */
91     const Elf_Rel*      pltrel;         /* DT_JMPREL */
92     int                 pltrelsize;     /* DT_PLTRELSZ */
93     const Elf_Rela*     pltrela;        /* DT_JMPREL */
94     int                 pltrelasize;    /* DT_PLTRELSZ */
95     const Elf_Rel*      rel;            /* DT_REL */
96     int                 relsize;        /* DT_RELSZ */
97     const Elf_Rela*     rela;           /* DT_RELA */
98     int                 relasize;       /* DT_RELASZ */
99     caddr_t             modptr;
100     const Elf_Sym*      ddbsymtab;      /* The symbol table we are using */
101     long                ddbsymcnt;      /* Number of symbols */
102     caddr_t             ddbstrtab;      /* String table */
103     long                ddbstrcnt;      /* number of bytes in string table */
104     caddr_t             symbase;        /* malloc'ed symbold base */
105     caddr_t             strbase;        /* malloc'ed string base */
106     caddr_t             ctftab;         /* CTF table */
107     long                ctfcnt;         /* number of bytes in CTF table */
108     caddr_t             ctfoff;         /* CTF offset table */
109     caddr_t             typoff;         /* Type offset table */
110     long                typlen;         /* Number of type entries. */
111 #ifdef GDB
112     struct link_map     gdb;            /* hooks for gdb */
113 #endif
114 } *elf_file_t;
115
116 #include <kern/kern_ctf.c>
117
118 static int      link_elf_link_common_finish(linker_file_t);
119 static int      link_elf_link_preload(linker_class_t cls,
120                                       const char*, linker_file_t*);
121 static int      link_elf_link_preload_finish(linker_file_t);
122 static int      link_elf_load_file(linker_class_t, const char*, linker_file_t*);
123 static int      link_elf_lookup_symbol(linker_file_t, const char*,
124                                        c_linker_sym_t*);
125 static int      link_elf_symbol_values(linker_file_t, c_linker_sym_t, linker_symval_t*);
126 static int      link_elf_search_symbol(linker_file_t, caddr_t value,
127                                        c_linker_sym_t* sym, long* diffp);
128
129 static void     link_elf_unload_file(linker_file_t);
130 static void     link_elf_unload_preload(linker_file_t);
131 static int      link_elf_lookup_set(linker_file_t, const char *,
132                                     void ***, void ***, int *);
133 static int      link_elf_each_function_name(linker_file_t,
134                                 int (*)(const char *, void *),
135                                 void *);
136 static int      link_elf_each_function_nameval(linker_file_t,
137                                 linker_function_nameval_callback_t,
138                                 void *);
139 static void     link_elf_reloc_local(linker_file_t);
140 static Elf_Addr elf_lookup(linker_file_t lf, Elf_Size symidx, int deps);
141
142 static kobj_method_t link_elf_methods[] = {
143     KOBJMETHOD(linker_lookup_symbol,    link_elf_lookup_symbol),
144     KOBJMETHOD(linker_symbol_values,    link_elf_symbol_values),
145     KOBJMETHOD(linker_search_symbol,    link_elf_search_symbol),
146     KOBJMETHOD(linker_unload,           link_elf_unload_file),
147     KOBJMETHOD(linker_load_file,        link_elf_load_file),
148     KOBJMETHOD(linker_link_preload,     link_elf_link_preload),
149     KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
150     KOBJMETHOD(linker_lookup_set,       link_elf_lookup_set),
151     KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
152     KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
153     KOBJMETHOD(linker_ctf_get,          link_elf_ctf_get),
154     { 0, 0 }
155 };
156
157 static struct linker_class link_elf_class = {
158 #if ELF_TARG_CLASS == ELFCLASS32
159     "elf32",
160 #else
161     "elf64",
162 #endif
163     link_elf_methods, sizeof(struct elf_file)
164 };
165
166 static int              parse_dynamic(elf_file_t ef);
167 static int              relocate_file(elf_file_t ef);
168 static int              link_elf_preload_parse_symbols(elf_file_t ef);
169
170 #ifdef GDB
171 static void             r_debug_state(struct r_debug *dummy_one,
172                                       struct link_map *dummy_two);
173
174 /*
175  * A list of loaded modules for GDB to use for loading symbols.
176  */
177 struct r_debug r_debug;
178
179 #define GDB_STATE(s)    r_debug.r_state = s; r_debug_state(NULL, NULL);
180
181 /*
182  * Function for the debugger to set a breakpoint on to gain control.
183  */
184 static void
185 r_debug_state(struct r_debug *dummy_one __unused,
186               struct link_map *dummy_two __unused)
187 {
188 }
189
190 static void
191 link_elf_add_gdb(struct link_map *l)
192 {
193     struct link_map *prev;
194
195     l->l_next = NULL;
196
197     if (r_debug.r_map == NULL) {
198         /* Add first. */
199         l->l_prev = NULL;
200         r_debug.r_map = l;
201     } else {
202         /* Append to list. */
203         for (prev = r_debug.r_map; prev->l_next != NULL; prev = prev->l_next)
204             ;
205         l->l_prev = prev;
206         prev->l_next = l;
207     }
208 }
209
210 static void
211 link_elf_delete_gdb(struct link_map *l)
212 {
213     if (l->l_prev == NULL) {
214         /* Remove first. */
215         if ((r_debug.r_map = l->l_next) != NULL)
216             l->l_next->l_prev = NULL;
217     } else {
218         /* Remove any but first. */
219         if ((l->l_prev->l_next = l->l_next) != NULL)
220             l->l_next->l_prev = l->l_prev;
221     }
222 }
223 #endif /* GDB */
224
225 #ifdef __ia64__
226 Elf_Addr link_elf_get_gp(linker_file_t);
227 #endif
228
229 /*
230  * The kernel symbol table starts here.
231  */
232 extern struct _dynamic _DYNAMIC;
233
234 static void
235 link_elf_error(const char *s)
236 {
237     printf("kldload: %s\n", s);
238 }
239
240 /*
241  * Actions performed after linking/loading both the preloaded kernel and any
242  * modules; whether preloaded or dynamicly loaded.
243  */
244 static int
245 link_elf_link_common_finish(linker_file_t lf)
246 {
247 #ifdef GDB
248     elf_file_t ef = (elf_file_t)lf;
249     char *newfilename;
250 #endif
251     int error;
252
253     /* Notify MD code that a module is being loaded. */
254     error = elf_cpu_load_file(lf);
255     if (error)
256         return (error);
257
258 #ifdef GDB
259     GDB_STATE(RT_ADD);
260     ef->gdb.l_addr = lf->address;
261     newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
262     strcpy(newfilename, lf->filename);
263     ef->gdb.l_name = newfilename;
264     ef->gdb.l_ld = ef->dynamic;
265     link_elf_add_gdb(&ef->gdb);
266     GDB_STATE(RT_CONSISTENT);
267 #endif
268
269     return (0);
270 }
271
272 static void
273 link_elf_init(void* arg)
274 {
275     Elf_Dyn     *dp;
276     caddr_t     modptr, baseptr, sizeptr;
277     elf_file_t  ef;
278     char        *modname;
279
280     linker_add_class(&link_elf_class);
281
282     dp = (Elf_Dyn*) &_DYNAMIC;
283     modname = NULL;
284     modptr = preload_search_by_type("elf" __XSTRING(__ELF_WORD_SIZE) " kernel");
285     if (modptr == NULL)
286         modptr = preload_search_by_type("elf kernel");
287     if (modptr)
288         modname = (char *)preload_search_info(modptr, MODINFO_NAME);
289     if (modname == NULL)
290         modname = "kernel";
291     linker_kernel_file = linker_make_file(modname, &link_elf_class);
292     if (linker_kernel_file == NULL)
293         panic("link_elf_init: Can't create linker structures for kernel");
294
295     ef = (elf_file_t) linker_kernel_file;
296     ef->preloaded = 1;
297     ef->address = 0;
298 #ifdef SPARSE_MAPPING
299     ef->object = 0;
300 #endif
301     ef->dynamic = dp;
302
303     if (dp)
304         parse_dynamic(ef);
305     linker_kernel_file->address = (caddr_t) KERNBASE;
306     linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
307
308     if (modptr) {
309         ef->modptr = modptr;
310         baseptr = preload_search_info(modptr, MODINFO_ADDR);
311         if (baseptr)
312             linker_kernel_file->address = *(caddr_t *)baseptr;
313         sizeptr = preload_search_info(modptr, MODINFO_SIZE);
314         if (sizeptr)
315             linker_kernel_file->size = *(size_t *)sizeptr;
316     }
317     (void)link_elf_preload_parse_symbols(ef);
318
319 #ifdef GDB
320     r_debug.r_map = NULL;
321     r_debug.r_brk = r_debug_state;
322     r_debug.r_state = RT_CONSISTENT;
323 #endif
324
325     (void)link_elf_link_common_finish(linker_kernel_file);
326     linker_kernel_file->flags |= LINKER_FILE_LINKED;
327 }
328
329 SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, 0);
330
331 static int
332 link_elf_preload_parse_symbols(elf_file_t ef)
333 {
334     caddr_t     pointer;
335     caddr_t     ssym, esym, base;
336     caddr_t     strtab;
337     int         strcnt;
338     Elf_Sym*    symtab;
339     int         symcnt;
340
341     if (ef->modptr == NULL)
342         return 0;
343     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_SSYM);
344     if (pointer == NULL)
345         return 0;
346     ssym = *(caddr_t *)pointer;
347     pointer = preload_search_info(ef->modptr, MODINFO_METADATA|MODINFOMD_ESYM);
348     if (pointer == NULL)
349         return 0;
350     esym = *(caddr_t *)pointer;
351
352     base = ssym;
353
354     symcnt = *(long *)base;
355     base += sizeof(long);
356     symtab = (Elf_Sym *)base;
357     base += roundup(symcnt, sizeof(long));
358
359     if (base > esym || base < ssym) {
360         printf("Symbols are corrupt!\n");
361         return EINVAL;
362     }
363
364     strcnt = *(long *)base;
365     base += sizeof(long);
366     strtab = base;
367     base += roundup(strcnt, sizeof(long));
368
369     if (base > esym || base < ssym) {
370         printf("Symbols are corrupt!\n");
371         return EINVAL;
372     }
373
374     ef->ddbsymtab = symtab;
375     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
376     ef->ddbstrtab = strtab;
377     ef->ddbstrcnt = strcnt;
378
379     return 0;
380 }
381
382 static int
383 parse_dynamic(elf_file_t ef)
384 {
385     Elf_Dyn *dp;
386     int plttype = DT_REL;
387
388     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
389         switch (dp->d_tag) {
390         case DT_HASH:
391         {
392             /* From src/libexec/rtld-elf/rtld.c */
393             const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
394                 (ef->address + dp->d_un.d_ptr);
395             ef->nbuckets = hashtab[0];
396             ef->nchains = hashtab[1];
397             ef->buckets = hashtab + 2;
398             ef->chains = ef->buckets + ef->nbuckets;
399             break;
400         }
401         case DT_STRTAB:
402             ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
403             break;
404         case DT_STRSZ:
405             ef->strsz = dp->d_un.d_val;
406             break;
407         case DT_SYMTAB:
408             ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
409             break;
410         case DT_SYMENT:
411             if (dp->d_un.d_val != sizeof(Elf_Sym))
412                 return ENOEXEC;
413             break;
414         case DT_PLTGOT:
415             ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
416             break;
417         case DT_REL:
418             ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
419             break;
420         case DT_RELSZ:
421             ef->relsize = dp->d_un.d_val;
422             break;
423         case DT_RELENT:
424             if (dp->d_un.d_val != sizeof(Elf_Rel))
425                 return ENOEXEC;
426             break;
427         case DT_JMPREL:
428             ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
429             break;
430         case DT_PLTRELSZ:
431             ef->pltrelsize = dp->d_un.d_val;
432             break;
433         case DT_RELA:
434             ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
435             break;
436         case DT_RELASZ:
437             ef->relasize = dp->d_un.d_val;
438             break;
439         case DT_RELAENT:
440             if (dp->d_un.d_val != sizeof(Elf_Rela))
441                 return ENOEXEC;
442             break;
443         case DT_PLTREL:
444             plttype = dp->d_un.d_val;
445             if (plttype != DT_REL && plttype != DT_RELA)
446                 return ENOEXEC;
447             break;
448 #ifdef GDB
449         case DT_DEBUG:
450             dp->d_un.d_ptr = (Elf_Addr) &r_debug;
451             break;
452 #endif
453         }
454     }
455
456     if (plttype == DT_RELA) {
457         ef->pltrela = (const Elf_Rela *) ef->pltrel;
458         ef->pltrel = NULL;
459         ef->pltrelasize = ef->pltrelsize;
460         ef->pltrelsize = 0;
461     }
462
463     ef->ddbsymtab = ef->symtab;
464     ef->ddbsymcnt = ef->nchains;
465     ef->ddbstrtab = ef->strtab;
466     ef->ddbstrcnt = ef->strsz;
467
468     return 0;
469 }
470
471 static int
472 link_elf_link_preload(linker_class_t cls,
473                       const char* filename, linker_file_t *result)
474 {
475     caddr_t             modptr, baseptr, sizeptr, dynptr;
476     char                *type;
477     elf_file_t          ef;
478     linker_file_t       lf;
479     int                 error;
480     vm_offset_t         dp;
481
482     /* Look to see if we have the file preloaded */
483     modptr = preload_search_by_name(filename);
484     if (modptr == NULL)
485         return ENOENT;
486
487     type = (char *)preload_search_info(modptr, MODINFO_TYPE);
488     baseptr = preload_search_info(modptr, MODINFO_ADDR);
489     sizeptr = preload_search_info(modptr, MODINFO_SIZE);
490     dynptr = preload_search_info(modptr, MODINFO_METADATA|MODINFOMD_DYNAMIC);
491     if (type == NULL ||
492         (strcmp(type, "elf" __XSTRING(__ELF_WORD_SIZE) " module") != 0 &&
493          strcmp(type, "elf module") != 0))
494         return (EFTYPE);
495     if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
496         return (EINVAL);
497
498     lf = linker_make_file(filename, &link_elf_class);
499     if (lf == NULL) {
500         return ENOMEM;
501     }
502
503     ef = (elf_file_t) lf;
504     ef->preloaded = 1;
505     ef->modptr = modptr;
506     ef->address = *(caddr_t *)baseptr;
507 #ifdef SPARSE_MAPPING
508     ef->object = 0;
509 #endif
510     dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
511     ef->dynamic = (Elf_Dyn *)dp;
512     lf->address = ef->address;
513     lf->size = *(size_t *)sizeptr;
514
515     error = parse_dynamic(ef);
516     if (error) {
517         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
518         return error;
519     }
520     link_elf_reloc_local(lf);
521     *result = lf;
522     return (0);
523 }
524
525 static int
526 link_elf_link_preload_finish(linker_file_t lf)
527 {
528     elf_file_t          ef;
529     int error;
530
531     ef = (elf_file_t) lf;
532 #if 0   /* this will be more trouble than it's worth for now */
533     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
534         if (dp->d_tag != DT_NEEDED)
535             continue;
536         modname = ef->strtab + dp->d_un.d_val;
537         error = linker_load_module(modname, lf);
538         if (error)
539             goto out;
540     }
541 #endif
542     error = relocate_file(ef);
543     if (error)
544         return error;
545     (void)link_elf_preload_parse_symbols(ef);
546
547     return (link_elf_link_common_finish(lf));
548 }
549
550 static int
551 link_elf_load_file(linker_class_t cls, const char* filename,
552         linker_file_t* result)
553 {
554     struct nameidata nd;
555     struct thread* td = curthread;      /* XXX */
556     Elf_Ehdr *hdr;
557     caddr_t firstpage;
558     int nbytes, i;
559     Elf_Phdr *phdr;
560     Elf_Phdr *phlimit;
561     Elf_Phdr *segs[MAXSEGS];
562     int nsegs;
563     Elf_Phdr *phdyn;
564     Elf_Phdr *phphdr;
565     caddr_t mapbase;
566     size_t mapsize;
567     Elf_Off base_offset;
568     Elf_Addr base_vaddr;
569     Elf_Addr base_vlimit;
570     int error = 0;
571     int resid, flags;
572     elf_file_t ef;
573     linker_file_t lf;
574     Elf_Shdr *shdr;
575     int symtabindex;
576     int symstrindex;
577     int symcnt;
578     int strcnt;
579     int vfslocked;
580
581     shdr = NULL;
582     lf = NULL;
583
584     NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, filename, td);
585     flags = FREAD;
586     error = vn_open(&nd, &flags, 0, NULL);
587     if (error)
588         return error;
589     vfslocked = NDHASGIANT(&nd);
590     NDFREE(&nd, NDF_ONLY_PNBUF);
591     if (nd.ni_vp->v_type != VREG) {
592         error = ENOEXEC;
593         firstpage = NULL;
594         goto out;
595     }
596 #ifdef MAC
597     error = mac_kld_check_load(curthread->td_ucred, nd.ni_vp);
598     if (error) {
599         firstpage = NULL;
600         goto out;
601     }
602 #endif
603
604     /*
605      * Read the elf header from the file.
606      */
607     firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
608     if (firstpage == NULL) {
609         error = ENOMEM;
610         goto out;
611     }
612     hdr = (Elf_Ehdr *)firstpage;
613     error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
614                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
615                     &resid, td);
616     nbytes = PAGE_SIZE - resid;
617     if (error)
618         goto out;
619
620     if (!IS_ELF(*hdr)) {
621         error = ENOEXEC;
622         goto out;
623     }
624
625     if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS
626       || hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
627         link_elf_error("Unsupported file layout");
628         error = ENOEXEC;
629         goto out;
630     }
631     if (hdr->e_ident[EI_VERSION] != EV_CURRENT
632       || hdr->e_version != EV_CURRENT) {
633         link_elf_error("Unsupported file version");
634         error = ENOEXEC;
635         goto out;
636     }
637     if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
638         link_elf_error("Unsupported file type");
639         error = ENOEXEC;
640         goto out;
641     }
642     if (hdr->e_machine != ELF_TARG_MACH) {
643         link_elf_error("Unsupported machine");
644         error = ENOEXEC;
645         goto out;
646     }
647
648     /*
649      * We rely on the program header being in the first page.  This is
650      * not strictly required by the ABI specification, but it seems to
651      * always true in practice.  And, it simplifies things considerably.
652      */
653     if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
654           (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
655           (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
656         link_elf_error("Unreadable program headers");
657
658     /*
659      * Scan the program header entries, and save key information.
660      *
661      * We rely on there being exactly two load segments, text and data,
662      * in that order.
663      */
664     phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
665     phlimit = phdr + hdr->e_phnum;
666     nsegs = 0;
667     phdyn = NULL;
668     phphdr = NULL;
669     while (phdr < phlimit) {
670         switch (phdr->p_type) {
671
672         case PT_LOAD:
673             if (nsegs == MAXSEGS) {
674                 link_elf_error("Too many sections");
675                 error = ENOEXEC;
676                 goto out;
677             }
678             /*
679              * XXX: We just trust they come in right order ??
680              */
681             segs[nsegs] = phdr;
682             ++nsegs;
683             break;
684
685         case PT_PHDR:
686             phphdr = phdr;
687             break;
688
689         case PT_DYNAMIC:
690             phdyn = phdr;
691             break;
692
693         case PT_INTERP:
694             link_elf_error("Unsupported file type");
695             error = ENOEXEC;
696             goto out;
697         }
698
699         ++phdr;
700     }
701     if (phdyn == NULL) {
702         link_elf_error("Object is not dynamically-linked");
703         error = ENOEXEC;
704         goto out;
705     }
706     if (nsegs == 0) {
707         link_elf_error("No sections");
708         error = ENOEXEC;
709         goto out;
710     }
711
712     /*
713      * Allocate the entire address space of the object, to stake out our
714      * contiguous region, and to establish the base address for relocation.
715      */
716     base_offset = trunc_page(segs[0]->p_offset);
717     base_vaddr = trunc_page(segs[0]->p_vaddr);
718     base_vlimit = round_page(segs[nsegs - 1]->p_vaddr + 
719         segs[nsegs - 1]->p_memsz);
720     mapsize = base_vlimit - base_vaddr;
721
722     lf = linker_make_file(filename, &link_elf_class);
723     if (!lf) {
724         error = ENOMEM;
725         goto out;
726     }
727
728     ef = (elf_file_t) lf;
729 #ifdef SPARSE_MAPPING
730     ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT);
731     if (ef->object == NULL) {
732         error = ENOMEM;
733         goto out;
734     }
735     ef->address = (caddr_t) vm_map_min(kernel_map);
736     error = vm_map_find(kernel_map, ef->object, 0,
737                         (vm_offset_t *) &ef->address,
738                         mapsize, 1,
739                         VM_PROT_ALL, VM_PROT_ALL, 0);
740     if (error) {
741         vm_object_deallocate(ef->object);
742         ef->object = 0;
743         goto out;
744     }
745 #else
746     ef->address = malloc(mapsize, M_LINKER, M_WAITOK);
747     if (!ef->address) {
748         error = ENOMEM;
749         goto out;
750     }
751 #endif
752     mapbase = ef->address;
753
754     /*
755      * Read the text and data sections and zero the bss.
756      */
757     for (i = 0; i < nsegs; i++) {
758         caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
759         error = vn_rdwr(UIO_READ, nd.ni_vp,
760                         segbase, segs[i]->p_filesz, segs[i]->p_offset,
761                         UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
762                         &resid, td);
763         if (error) {
764             goto out;
765         }
766         bzero(segbase + segs[i]->p_filesz,
767               segs[i]->p_memsz - segs[i]->p_filesz);
768
769 #ifdef SPARSE_MAPPING
770         /*
771          * Wire down the pages
772          */
773         error = vm_map_wire(kernel_map,
774                     (vm_offset_t) segbase,
775                     (vm_offset_t) segbase + segs[i]->p_memsz,
776                     VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
777         if (error != KERN_SUCCESS) {
778             error = ENOMEM;
779             goto out;
780         }
781 #endif
782     }
783
784 #ifdef GPROF
785     /* Update profiling information with the new text segment. */
786     mtx_lock(&Giant);
787     kmupetext((uintfptr_t)(mapbase + segs[0]->p_vaddr - base_vaddr +
788         segs[0]->p_memsz));
789     mtx_unlock(&Giant);
790 #endif
791
792     ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
793
794     lf->address = ef->address;
795     lf->size = mapsize;
796
797     error = parse_dynamic(ef);
798     if (error)
799         goto out;
800     link_elf_reloc_local(lf);
801
802     error = linker_load_dependencies(lf);
803     if (error)
804         goto out;
805 #if 0   /* this will be more trouble than it's worth for now */
806     for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
807         if (dp->d_tag != DT_NEEDED)
808             continue;
809         modname = ef->strtab + dp->d_un.d_val;
810         error = linker_load_module(modname, lf);
811         if (error)
812             goto out;
813     }
814 #endif
815     error = relocate_file(ef);
816     if (error)
817         goto out;
818
819     /* Try and load the symbol table if it's present.  (you can strip it!) */
820     nbytes = hdr->e_shnum * hdr->e_shentsize;
821     if (nbytes == 0 || hdr->e_shoff == 0)
822         goto nosyms;
823     shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
824     if (shdr == NULL) {
825         error = ENOMEM;
826         goto out;
827     }
828     error = vn_rdwr(UIO_READ, nd.ni_vp,
829                     (caddr_t)shdr, nbytes, hdr->e_shoff,
830                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
831                     &resid, td);
832     if (error)
833         goto out;
834     symtabindex = -1;
835     symstrindex = -1;
836     for (i = 0; i < hdr->e_shnum; i++) {
837         if (shdr[i].sh_type == SHT_SYMTAB) {
838             symtabindex = i;
839             symstrindex = shdr[i].sh_link;
840         }
841     }
842     if (symtabindex < 0 || symstrindex < 0)
843         goto nosyms;
844
845     symcnt = shdr[symtabindex].sh_size;
846     ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
847     strcnt = shdr[symstrindex].sh_size;
848     ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
849
850     if (ef->symbase == NULL || ef->strbase == NULL) {
851         error = ENOMEM;
852         goto out;
853     }
854     error = vn_rdwr(UIO_READ, nd.ni_vp,
855                     ef->symbase, symcnt, shdr[symtabindex].sh_offset,
856                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
857                     &resid, td);
858     if (error)
859         goto out;
860     error = vn_rdwr(UIO_READ, nd.ni_vp,
861                     ef->strbase, strcnt, shdr[symstrindex].sh_offset,
862                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
863                     &resid, td);
864     if (error)
865         goto out;
866
867     ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
868     ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
869     ef->ddbstrcnt = strcnt;
870     ef->ddbstrtab = ef->strbase;
871
872     error = link_elf_link_common_finish(lf);
873     if (error)
874         goto out;
875
876 nosyms:
877
878     *result = lf;
879
880 out:
881     if (error && lf)
882         linker_file_unload(lf, LINKER_UNLOAD_FORCE);
883     if (shdr)
884         free(shdr, M_LINKER);
885     if (firstpage)
886         free(firstpage, M_LINKER);
887     VOP_UNLOCK(nd.ni_vp, 0);
888     vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
889     VFS_UNLOCK_GIANT(vfslocked);
890
891     return error;
892 }
893
894 static void
895 link_elf_unload_file(linker_file_t file)
896 {
897     elf_file_t ef = (elf_file_t) file;
898
899 #ifdef GDB
900     if (ef->gdb.l_ld) {
901         GDB_STATE(RT_DELETE);
902         free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
903         link_elf_delete_gdb(&ef->gdb);
904         GDB_STATE(RT_CONSISTENT);
905     }
906 #endif
907
908     /* Notify MD code that a module is being unloaded. */
909     elf_cpu_unload_file(file);
910
911     if (ef->preloaded) {
912         link_elf_unload_preload(file);
913         return;
914     }
915
916 #ifdef SPARSE_MAPPING
917     if (ef->object) {
918         vm_map_remove(kernel_map, (vm_offset_t) ef->address,
919                       (vm_offset_t) ef->address
920                       + (ef->object->size << PAGE_SHIFT));
921     }
922 #else
923     if (ef->address)
924         free(ef->address, M_LINKER);
925 #endif
926     if (ef->symbase)
927         free(ef->symbase, M_LINKER);
928     if (ef->strbase)
929         free(ef->strbase, M_LINKER);
930     if (ef->ctftab)
931         free(ef->ctftab, M_LINKER);
932     if (ef->ctfoff)
933         free(ef->ctfoff, M_LINKER);
934     if (ef->typoff)
935         free(ef->typoff, M_LINKER);
936 }
937
938 static void
939 link_elf_unload_preload(linker_file_t file)
940 {
941     if (file->filename)
942         preload_delete_name(file->filename);
943 }
944
945 static const char *
946 symbol_name(elf_file_t ef, Elf_Size r_info)
947 {
948     const Elf_Sym *ref;
949
950     if (ELF_R_SYM(r_info)) {
951         ref = ef->symtab + ELF_R_SYM(r_info);
952         return ef->strtab + ref->st_name;
953     } else
954         return NULL;
955 }
956
957 static int
958 relocate_file(elf_file_t ef)
959 {
960     const Elf_Rel *rellim;
961     const Elf_Rel *rel;
962     const Elf_Rela *relalim;
963     const Elf_Rela *rela;
964     const char *symname;
965
966     /* Perform relocations without addend if there are any: */
967     rel = ef->rel;
968     if (rel) {
969         rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
970         while (rel < rellim) {
971             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
972                           elf_lookup)) {
973                 symname = symbol_name(ef, rel->r_info);
974                 printf("link_elf: symbol %s undefined\n", symname);
975                 return ENOENT;
976             }
977             rel++;
978         }
979     }
980
981     /* Perform relocations with addend if there are any: */
982     rela = ef->rela;
983     if (rela) {
984         relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
985         while (rela < relalim) {
986             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
987                           elf_lookup)) {
988                 symname = symbol_name(ef, rela->r_info);
989                 printf("link_elf: symbol %s undefined\n", symname);
990                 return ENOENT;
991             }
992             rela++;
993         }
994     }
995
996     /* Perform PLT relocations without addend if there are any: */
997     rel = ef->pltrel;
998     if (rel) {
999         rellim = (const Elf_Rel *)((const char *)ef->pltrel + ef->pltrelsize);
1000         while (rel < rellim) {
1001             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1002                           elf_lookup)) {
1003                 symname = symbol_name(ef, rel->r_info);
1004                 printf("link_elf: symbol %s undefined\n", symname);
1005                 return ENOENT;
1006             }
1007             rel++;
1008         }
1009     }
1010
1011     /* Perform relocations with addend if there are any: */
1012     rela = ef->pltrela;
1013     if (rela) {
1014         relalim = (const Elf_Rela *)((const char *)ef->pltrela + ef->pltrelasize);
1015         while (rela < relalim) {
1016             if (elf_reloc(&ef->lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
1017                           elf_lookup)) {
1018                 symname = symbol_name(ef, rela->r_info);
1019                 printf("link_elf: symbol %s undefined\n", symname);
1020                 return ENOENT;
1021             }
1022             rela++;
1023         }
1024     }
1025
1026     return 0;
1027 }
1028
1029 /*
1030  * Hash function for symbol table lookup.  Don't even think about changing
1031  * this.  It is specified by the System V ABI.
1032  */
1033 static unsigned long
1034 elf_hash(const char *name)
1035 {
1036     const unsigned char *p = (const unsigned char *) name;
1037     unsigned long h = 0;
1038     unsigned long g;
1039
1040     while (*p != '\0') {
1041         h = (h << 4) + *p++;
1042         if ((g = h & 0xf0000000) != 0)
1043             h ^= g >> 24;
1044         h &= ~g;
1045     }
1046     return h;
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     unsigned long symnum;
1054     const Elf_Sym* symp;
1055     const char *strp;
1056     unsigned long hash;
1057     int i;
1058
1059     /* If we don't have a hash, bail. */
1060     if (ef->buckets == NULL || ef->nbuckets == 0) {
1061         printf("link_elf_lookup_symbol: missing symbol hash table\n");
1062         return ENOENT;
1063     }
1064
1065     /* First, search hashed global symbols */
1066     hash = elf_hash(name);
1067     symnum = ef->buckets[hash % ef->nbuckets];
1068
1069     while (symnum != STN_UNDEF) {
1070         if (symnum >= ef->nchains) {
1071             printf("link_elf_lookup_symbol: corrupt symbol table\n");
1072             return ENOENT;
1073         }
1074
1075         symp = ef->symtab + symnum;
1076         if (symp->st_name == 0) {
1077             printf("link_elf_lookup_symbol: corrupt symbol table\n");
1078             return ENOENT;
1079         }
1080
1081         strp = ef->strtab + symp->st_name;
1082
1083         if (strcmp(name, strp) == 0) {
1084             if (symp->st_shndx != SHN_UNDEF ||
1085                 (symp->st_value != 0 &&
1086                  ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1087                 *sym = (c_linker_sym_t) symp;
1088                 return 0;
1089             } else
1090                 return ENOENT;
1091         }
1092
1093         symnum = ef->chains[symnum];
1094     }
1095
1096     /* If we have not found it, look at the full table (if loaded) */
1097     if (ef->symtab == ef->ddbsymtab)
1098         return ENOENT;
1099
1100     /* Exhaustive search */
1101     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1102         strp = ef->ddbstrtab + symp->st_name;
1103         if (strcmp(name, strp) == 0) {
1104             if (symp->st_shndx != SHN_UNDEF ||
1105                 (symp->st_value != 0 &&
1106                  ELF_ST_TYPE(symp->st_info) == STT_FUNC)) {
1107                 *sym = (c_linker_sym_t) symp;
1108                 return 0;
1109             } else
1110                 return ENOENT;
1111         }
1112     }
1113
1114     return ENOENT;
1115 }
1116
1117 static int
1118 link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym, linker_symval_t* symval)
1119 {
1120         elf_file_t ef = (elf_file_t) lf;
1121         const Elf_Sym* es = (const Elf_Sym*) sym;
1122
1123         if (es >= ef->symtab && es < (ef->symtab + ef->nchains)) {
1124             symval->name = ef->strtab + es->st_name;
1125             symval->value = (caddr_t) ef->address + es->st_value;
1126             symval->size = es->st_size;
1127             return 0;
1128         }
1129         if (ef->symtab == ef->ddbsymtab)
1130             return ENOENT;
1131         if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
1132             symval->name = ef->ddbstrtab + es->st_name;
1133             symval->value = (caddr_t) ef->address + es->st_value;
1134             symval->size = es->st_size;
1135             return 0;
1136         }
1137         return ENOENT;
1138 }
1139
1140 static int
1141 link_elf_search_symbol(linker_file_t lf, caddr_t value,
1142                        c_linker_sym_t* sym, long* diffp)
1143 {
1144         elf_file_t ef = (elf_file_t) lf;
1145         u_long off = (uintptr_t) (void *) value;
1146         u_long diff = off;
1147         u_long st_value;
1148         const Elf_Sym* es;
1149         const Elf_Sym* best = 0;
1150         int i;
1151
1152         for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1153                 if (es->st_name == 0)
1154                         continue;
1155                 st_value = es->st_value + (uintptr_t) (void *) ef->address;
1156                 if (off >= st_value) {
1157                         if (off - st_value < diff) {
1158                                 diff = off - st_value;
1159                                 best = es;
1160                                 if (diff == 0)
1161                                         break;
1162                         } else if (off - st_value == diff) {
1163                                 best = es;
1164                         }
1165                 }
1166         }
1167         if (best == 0)
1168                 *diffp = off;
1169         else
1170                 *diffp = diff;
1171         *sym = (c_linker_sym_t) best;
1172
1173         return 0;
1174 }
1175
1176 /*
1177  * Look up a linker set on an ELF system.
1178  */
1179 static int
1180 link_elf_lookup_set(linker_file_t lf, const char *name,
1181                     void ***startp, void ***stopp, int *countp)
1182 {
1183         c_linker_sym_t sym;
1184         linker_symval_t symval;
1185         char *setsym;
1186         void **start, **stop;
1187         int len, error = 0, count;
1188
1189         len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1190         setsym = malloc(len, M_LINKER, M_WAITOK);
1191         if (setsym == NULL)
1192                 return ENOMEM;
1193
1194         /* get address of first entry */
1195         snprintf(setsym, len, "%s%s", "__start_set_", name);
1196         error = link_elf_lookup_symbol(lf, setsym, &sym);
1197         if (error)
1198                 goto out;
1199         link_elf_symbol_values(lf, sym, &symval);
1200         if (symval.value == 0) {
1201                 error = ESRCH;
1202                 goto out;
1203         }
1204         start = (void **)symval.value;
1205
1206         /* get address of last entry */
1207         snprintf(setsym, len, "%s%s", "__stop_set_", name);
1208         error = link_elf_lookup_symbol(lf, setsym, &sym);
1209         if (error)
1210                 goto out;
1211         link_elf_symbol_values(lf, sym, &symval);
1212         if (symval.value == 0) {
1213                 error = ESRCH;
1214                 goto out;
1215         }
1216         stop = (void **)symval.value;
1217
1218         /* and the number of entries */
1219         count = stop - start;
1220
1221         /* and copy out */
1222         if (startp)
1223                 *startp = start;
1224         if (stopp)
1225                 *stopp = stop;
1226         if (countp)
1227                 *countp = count;
1228
1229 out:
1230         free(setsym, M_LINKER);
1231         return error;
1232 }
1233
1234 static int
1235 link_elf_each_function_name(linker_file_t file,
1236   int (*callback)(const char *, void *), void *opaque) {
1237     elf_file_t ef = (elf_file_t)file;
1238     const Elf_Sym* symp;
1239     int i, error;
1240         
1241     /* Exhaustive search */
1242     for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1243         if (symp->st_value != 0 &&
1244             ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1245                 error = callback(ef->ddbstrtab + symp->st_name, opaque);
1246                 if (error)
1247                     return (error);
1248         }
1249     }
1250     return (0);
1251 }
1252
1253 static int
1254 link_elf_each_function_nameval(linker_file_t file,
1255     linker_function_nameval_callback_t callback, void *opaque)
1256 {
1257         linker_symval_t symval;
1258         elf_file_t ef = (elf_file_t)file;
1259         const Elf_Sym* symp;
1260         int i, error;
1261
1262         /* Exhaustive search */
1263         for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1264                 if (symp->st_value != 0 &&
1265                     ELF_ST_TYPE(symp->st_info) == STT_FUNC) {
1266                         error = link_elf_symbol_values(file, (c_linker_sym_t) symp, &symval);
1267                         if (error)
1268                                 return (error);
1269                         error = callback(file, i, &symval, opaque);
1270                         if (error)
1271                                 return (error);
1272                 }
1273         }
1274         return (0);
1275 }
1276
1277 #ifdef __ia64__
1278 /*
1279  * Each KLD has its own GP. The GP value for each load module is given by
1280  * DT_PLTGOT on ia64. We need GP to construct function descriptors, but
1281  * don't have direct access to the ELF file structure. The link_elf_get_gp()
1282  * function returns the GP given a pointer to a generic linker file struct.
1283  */
1284 Elf_Addr
1285 link_elf_get_gp(linker_file_t lf)
1286 {
1287         elf_file_t ef = (elf_file_t)lf;
1288         return (Elf_Addr)ef->got;
1289 }
1290 #endif
1291
1292 const Elf_Sym *
1293 elf_get_sym(linker_file_t lf, Elf_Size symidx)
1294 {
1295         elf_file_t ef = (elf_file_t)lf;
1296
1297         if (symidx >= ef->nchains)
1298                 return (NULL);
1299         return (ef->symtab + symidx);
1300 }
1301
1302 const char *
1303 elf_get_symname(linker_file_t lf, Elf_Size symidx)
1304 {
1305         elf_file_t ef = (elf_file_t)lf;
1306         const Elf_Sym *sym;
1307
1308         if (symidx >= ef->nchains)
1309                 return (NULL);
1310         sym = ef->symtab + symidx;
1311         return (ef->strtab + sym->st_name);
1312 }
1313
1314 /*
1315  * Symbol lookup function that can be used when the symbol index is known (ie
1316  * in relocations). It uses the symbol index instead of doing a fully fledged
1317  * hash table based lookup when such is valid. For example for local symbols.
1318  * This is not only more efficient, it's also more correct. It's not always
1319  * the case that the symbol can be found through the hash table.
1320  */
1321 static Elf_Addr
1322 elf_lookup(linker_file_t lf, Elf_Size symidx, int deps)
1323 {
1324         elf_file_t ef = (elf_file_t)lf;
1325         const Elf_Sym *sym;
1326         const char *symbol;
1327
1328         /* Don't even try to lookup the symbol if the index is bogus. */
1329         if (symidx >= ef->nchains)
1330                 return (0);
1331
1332         sym = ef->symtab + symidx;
1333
1334         /*
1335          * Don't do a full lookup when the symbol is local. It may even
1336          * fail because it may not be found through the hash table.
1337          */
1338         if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1339                 /* Force lookup failure when we have an insanity. */
1340                 if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0)
1341                         return (0);
1342                 return ((Elf_Addr)ef->address + sym->st_value);
1343         }
1344
1345         /*
1346          * XXX we can avoid doing a hash table based lookup for global
1347          * symbols as well. This however is not always valid, so we'll
1348          * just do it the hard way for now. Performance tweaks can
1349          * always be added.
1350          */
1351
1352         symbol = ef->strtab + sym->st_name;
1353
1354         /* Force a lookup failure if the symbol name is bogus. */
1355         if (*symbol == 0)
1356                 return (0);
1357
1358         return ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1359 }
1360
1361 static void
1362 link_elf_reloc_local(linker_file_t lf)
1363 {
1364     const Elf_Rel *rellim;
1365     const Elf_Rel *rel;
1366     const Elf_Rela *relalim;
1367     const Elf_Rela *rela;
1368     elf_file_t ef = (elf_file_t)lf;
1369
1370     /* Perform relocations without addend if there are any: */
1371     if ((rel = ef->rel) != NULL) {
1372         rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
1373         while (rel < rellim) {
1374             elf_reloc_local(lf, (Elf_Addr)ef->address, rel, ELF_RELOC_REL,
1375                             elf_lookup);
1376             rel++;
1377         }
1378     }
1379
1380     /* Perform relocations with addend if there are any: */
1381     if ((rela = ef->rela) != NULL) {
1382         relalim = (const Elf_Rela *)((const char *)ef->rela + ef->relasize);
1383         while (rela < relalim) {
1384             elf_reloc_local(lf, (Elf_Addr)ef->address, rela, ELF_RELOC_RELA,
1385                             elf_lookup);
1386             rela++;
1387         }
1388     }
1389 }