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