1a4f67738SDoug Rabson /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni *
4326e27d8SDoug Rabson * Copyright (c) 1998-2000 Doug Rabson
5a4f67738SDoug Rabson * All rights reserved.
6a4f67738SDoug Rabson *
7a4f67738SDoug Rabson * Redistribution and use in source and binary forms, with or without
8a4f67738SDoug Rabson * modification, are permitted provided that the following conditions
9a4f67738SDoug Rabson * are met:
10a4f67738SDoug Rabson * 1. Redistributions of source code must retain the above copyright
11a4f67738SDoug Rabson * notice, this list of conditions and the following disclaimer.
12a4f67738SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
13a4f67738SDoug Rabson * notice, this list of conditions and the following disclaimer in the
14a4f67738SDoug Rabson * documentation and/or other materials provided with the distribution.
15a4f67738SDoug Rabson *
16a4f67738SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a4f67738SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a4f67738SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a4f67738SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a4f67738SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a4f67738SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a4f67738SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a4f67738SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a4f67738SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a4f67738SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a4f67738SDoug Rabson * SUCH DAMAGE.
27a4f67738SDoug Rabson */
28a4f67738SDoug Rabson
29a2024a3eSJohn Birrell #include "opt_ddb.h"
30ecb01c64SMarcel Moolenaar #include "opt_gdb.h"
31326e27d8SDoug Rabson
32a4f67738SDoug Rabson #include <sys/param.h>
33a4f67738SDoug Rabson #include <sys/systm.h>
34fb919e4dSMark Murray #include <sys/kernel.h>
35fb919e4dSMark Murray #include <sys/lock.h>
36a4f67738SDoug Rabson #include <sys/malloc.h>
371aa696baSMark Johnston #ifdef SPARSE_MAPPING
381aa696baSMark Johnston #include <sys/mman.h>
391aa696baSMark Johnston #endif
40c0b824f9SDima Dorfman #include <sys/mutex.h>
4162d615d5SJohn Baldwin #include <sys/mount.h>
4250c202c5SJeff Roberson #include <sys/pcpu.h>
43a4f67738SDoug Rabson #include <sys/proc.h>
44a4f67738SDoug Rabson #include <sys/namei.h>
45a4f67738SDoug Rabson #include <sys/fcntl.h>
46a4f67738SDoug Rabson #include <sys/vnode.h>
47a4f67738SDoug Rabson #include <sys/linker.h>
48eb5a41cfSLeandro Lupori #include <sys/sysctl.h>
499d6ae1e3SColin Percival #include <sys/tslog.h>
50fb919e4dSMark Murray
51a4f67738SDoug Rabson #include <machine/elf.h>
52a4f67738SDoug Rabson
53eddfbb76SRobert Watson #include <net/vnet.h>
54eddfbb76SRobert Watson
55aed55708SRobert Watson #include <security/mac/mac_framework.h>
56aed55708SRobert Watson
57fe3db7c7SDoug Rabson #include <vm/vm.h>
58fe3db7c7SDoug Rabson #include <vm/vm_param.h>
59d1f088daSPeter Wemm #ifdef SPARSE_MAPPING
60fe3db7c7SDoug Rabson #include <vm/vm_object.h>
61fe3db7c7SDoug Rabson #include <vm/vm_kern.h>
62fe3db7c7SDoug Rabson #include <vm/vm_extern.h>
63d1f088daSPeter Wemm #endif
64fe3db7c7SDoug Rabson #include <vm/pmap.h>
65fe3db7c7SDoug Rabson #include <vm/vm_map.h>
66fb919e4dSMark Murray
67f9980387SPeter Wemm #include <sys/link_elf.h>
68fe3db7c7SDoug Rabson
69326e27d8SDoug Rabson #include "linker_if.h"
70a4f67738SDoug Rabson
71c21bc6f3SBojan Novković #ifdef DDB_CTF
72c21bc6f3SBojan Novković #include <ddb/db_ctf.h>
73c21bc6f3SBojan Novković #endif
74c21bc6f3SBojan Novković
751a330eb0SPeter Wemm #define MAXSEGS 4
761a330eb0SPeter Wemm
774f2945f8SDavid E. O'Brien typedef struct elf_file {
784f2945f8SDavid E. O'Brien struct linker_file lf; /* Common fields */
794f2945f8SDavid E. O'Brien int preloaded; /* Was file pre-loaded */
804f2945f8SDavid E. O'Brien caddr_t address; /* Relocation address */
814f2945f8SDavid E. O'Brien #ifdef SPARSE_MAPPING
824f2945f8SDavid E. O'Brien vm_object_t object; /* VM object to hold file pages */
834f2945f8SDavid E. O'Brien #endif
844f2945f8SDavid E. O'Brien Elf_Dyn *dynamic; /* Symbol table etc. */
854f2945f8SDavid E. O'Brien Elf_Hashelt nbuckets; /* DT_HASH info */
864f2945f8SDavid E. O'Brien Elf_Hashelt nchains;
874f2945f8SDavid E. O'Brien const Elf_Hashelt *buckets;
884f2945f8SDavid E. O'Brien const Elf_Hashelt *chains;
894f2945f8SDavid E. O'Brien caddr_t hash;
904f2945f8SDavid E. O'Brien caddr_t strtab; /* DT_STRTAB */
914f2945f8SDavid E. O'Brien int strsz; /* DT_STRSZ */
924f2945f8SDavid E. O'Brien const Elf_Sym *symtab; /* DT_SYMTAB */
934f2945f8SDavid E. O'Brien Elf_Addr *got; /* DT_PLTGOT */
944f2945f8SDavid E. O'Brien const Elf_Rel *pltrel; /* DT_JMPREL */
954f2945f8SDavid E. O'Brien int pltrelsize; /* DT_PLTRELSZ */
964f2945f8SDavid E. O'Brien const Elf_Rela *pltrela; /* DT_JMPREL */
974f2945f8SDavid E. O'Brien int pltrelasize; /* DT_PLTRELSZ */
984f2945f8SDavid E. O'Brien const Elf_Rel *rel; /* DT_REL */
994f2945f8SDavid E. O'Brien int relsize; /* DT_RELSZ */
1004f2945f8SDavid E. O'Brien const Elf_Rela *rela; /* DT_RELA */
1014f2945f8SDavid E. O'Brien int relasize; /* DT_RELASZ */
1024f2945f8SDavid E. O'Brien caddr_t modptr;
1034f2945f8SDavid E. O'Brien const Elf_Sym *ddbsymtab; /* The symbol table we are using */
1044f2945f8SDavid E. O'Brien long ddbsymcnt; /* Number of symbols */
1054f2945f8SDavid E. O'Brien caddr_t ddbstrtab; /* String table */
1064f2945f8SDavid E. O'Brien long ddbstrcnt; /* number of bytes in string table */
1074f2945f8SDavid E. O'Brien caddr_t symbase; /* malloc'ed symbold base */
1084f2945f8SDavid E. O'Brien caddr_t strbase; /* malloc'ed string base */
1094f2945f8SDavid E. O'Brien caddr_t ctftab; /* CTF table */
1104f2945f8SDavid E. O'Brien long ctfcnt; /* number of bytes in CTF table */
1114f2945f8SDavid E. O'Brien caddr_t ctfoff; /* CTF offset table */
1124f2945f8SDavid E. O'Brien caddr_t typoff; /* Type offset table */
1134f2945f8SDavid E. O'Brien long typlen; /* Number of type entries. */
11450c202c5SJeff Roberson Elf_Addr pcpu_start; /* Pre-relocation pcpu set start. */
11550c202c5SJeff Roberson Elf_Addr pcpu_stop; /* Pre-relocation pcpu set stop. */
11650c202c5SJeff Roberson Elf_Addr pcpu_base; /* Relocated pcpu set address. */
117eddfbb76SRobert Watson #ifdef VIMAGE
118eddfbb76SRobert Watson Elf_Addr vnet_start; /* Pre-relocation vnet set start. */
119eddfbb76SRobert Watson Elf_Addr vnet_stop; /* Pre-relocation vnet set stop. */
120eddfbb76SRobert Watson Elf_Addr vnet_base; /* Relocated vnet set address. */
121eddfbb76SRobert Watson #endif
1224f2945f8SDavid E. O'Brien #ifdef GDB
1234f2945f8SDavid E. O'Brien struct link_map gdb; /* hooks for gdb */
1244f2945f8SDavid E. O'Brien #endif
1254f2945f8SDavid E. O'Brien } *elf_file_t;
1264f2945f8SDavid E. O'Brien
12747813f5dSMikolaj Golub struct elf_set {
12847813f5dSMikolaj Golub Elf_Addr es_start;
12947813f5dSMikolaj Golub Elf_Addr es_stop;
13047813f5dSMikolaj Golub Elf_Addr es_base;
13147813f5dSMikolaj Golub TAILQ_ENTRY(elf_set) es_link;
13247813f5dSMikolaj Golub };
13347813f5dSMikolaj Golub
13447813f5dSMikolaj Golub TAILQ_HEAD(elf_set_head, elf_set);
13547813f5dSMikolaj Golub
136a2024a3eSJohn Birrell #include <kern/kern_ctf.c>
137a2024a3eSJohn Birrell
138c143d6c2SMarcel Moolenaar static int link_elf_link_common_finish(linker_file_t);
13954823af2SPeter Wemm static int link_elf_link_preload(linker_class_t cls,
140326e27d8SDoug Rabson const char *, linker_file_t *);
14154823af2SPeter Wemm static int link_elf_link_preload_finish(linker_file_t);
1422f22b3ffSMatthew D Fleming static int link_elf_load_file(linker_class_t, const char *,
1432f22b3ffSMatthew D Fleming linker_file_t *);
144326e27d8SDoug Rabson static int link_elf_lookup_symbol(linker_file_t, const char *,
145326e27d8SDoug Rabson c_linker_sym_t *);
14695c20fafSKonstantin Belousov static int link_elf_lookup_debug_symbol(linker_file_t, const char *,
14795c20fafSKonstantin Belousov c_linker_sym_t *);
148c21bc6f3SBojan Novković static int link_elf_lookup_debug_symbol_ctf(linker_file_t lf,
149c21bc6f3SBojan Novković const char *name, c_linker_sym_t *sym, linker_ctf_t *lc);
1502f22b3ffSMatthew D Fleming static int link_elf_symbol_values(linker_file_t, c_linker_sym_t,
1512f22b3ffSMatthew D Fleming linker_symval_t *);
15295c20fafSKonstantin Belousov static int link_elf_debug_symbol_values(linker_file_t, c_linker_sym_t,
15395c20fafSKonstantin Belousov linker_symval_t*);
1542f22b3ffSMatthew D Fleming static int link_elf_search_symbol(linker_file_t, caddr_t,
1552f22b3ffSMatthew D Fleming c_linker_sym_t *, long *);
156326e27d8SDoug Rabson
157326e27d8SDoug Rabson static void link_elf_unload_file(linker_file_t);
15854823af2SPeter Wemm static void link_elf_unload_preload(linker_file_t);
159f41325dbSPeter Wemm static int link_elf_lookup_set(linker_file_t, const char *,
160f41325dbSPeter Wemm void ***, void ***, int *);
161bb9fe9ddSBrian Feldman static int link_elf_each_function_name(linker_file_t,
1622f22b3ffSMatthew D Fleming int (*)(const char *, void *), void *);
163a2024a3eSJohn Birrell static int link_elf_each_function_nameval(linker_file_t,
1642f22b3ffSMatthew D Fleming linker_function_nameval_callback_t, void *);
1657251b4bfSJake Burkholder static void link_elf_reloc_local(linker_file_t);
16600a5db46SStacey Son static long link_elf_symtab_get(linker_file_t, const Elf_Sym **);
16700a5db46SStacey Son static long link_elf_strtab_get(linker_file_t, caddr_t *);
168110113bcSZhenlei Huang #ifdef VIMAGE
169110113bcSZhenlei Huang static void link_elf_propagate_vnets(linker_file_t);
170110113bcSZhenlei Huang #endif
171cff8c6f2SKonstantin Belousov static int elf_lookup(linker_file_t, Elf_Size, int, Elf_Addr *);
172326e27d8SDoug Rabson
173326e27d8SDoug Rabson static kobj_method_t link_elf_methods[] = {
174326e27d8SDoug Rabson KOBJMETHOD(linker_lookup_symbol, link_elf_lookup_symbol),
17595c20fafSKonstantin Belousov KOBJMETHOD(linker_lookup_debug_symbol, link_elf_lookup_debug_symbol),
176c21bc6f3SBojan Novković KOBJMETHOD(linker_lookup_debug_symbol_ctf, link_elf_lookup_debug_symbol_ctf),
177326e27d8SDoug Rabson KOBJMETHOD(linker_symbol_values, link_elf_symbol_values),
17895c20fafSKonstantin Belousov KOBJMETHOD(linker_debug_symbol_values, link_elf_debug_symbol_values),
179326e27d8SDoug Rabson KOBJMETHOD(linker_search_symbol, link_elf_search_symbol),
180326e27d8SDoug Rabson KOBJMETHOD(linker_unload, link_elf_unload_file),
18154823af2SPeter Wemm KOBJMETHOD(linker_load_file, link_elf_load_file),
18254823af2SPeter Wemm KOBJMETHOD(linker_link_preload, link_elf_link_preload),
18354823af2SPeter Wemm KOBJMETHOD(linker_link_preload_finish, link_elf_link_preload_finish),
184f41325dbSPeter Wemm KOBJMETHOD(linker_lookup_set, link_elf_lookup_set),
185bb9fe9ddSBrian Feldman KOBJMETHOD(linker_each_function_name, link_elf_each_function_name),
186a2024a3eSJohn Birrell KOBJMETHOD(linker_each_function_nameval, link_elf_each_function_nameval),
187a2024a3eSJohn Birrell KOBJMETHOD(linker_ctf_get, link_elf_ctf_get),
188c21bc6f3SBojan Novković KOBJMETHOD(linker_ctf_lookup_typename, link_elf_ctf_lookup_typename),
18900a5db46SStacey Son KOBJMETHOD(linker_symtab_get, link_elf_symtab_get),
19000a5db46SStacey Son KOBJMETHOD(linker_strtab_get, link_elf_strtab_get),
191110113bcSZhenlei Huang #ifdef VIMAGE
192110113bcSZhenlei Huang KOBJMETHOD(linker_propagate_vnets, link_elf_propagate_vnets),
193110113bcSZhenlei Huang #endif
1946d775f0bSMark Johnston KOBJMETHOD_END
195326e27d8SDoug Rabson };
196326e27d8SDoug Rabson
197326e27d8SDoug Rabson static struct linker_class link_elf_class = {
198326e27d8SDoug Rabson #if ELF_TARG_CLASS == ELFCLASS32
199326e27d8SDoug Rabson "elf32",
200326e27d8SDoug Rabson #else
201326e27d8SDoug Rabson "elf64",
202326e27d8SDoug Rabson #endif
203326e27d8SDoug Rabson link_elf_methods, sizeof(struct elf_file)
204326e27d8SDoug Rabson };
205326e27d8SDoug Rabson
206ecd8245eSKonstantin Belousov static bool link_elf_leak_locals = true;
207ecd8245eSKonstantin Belousov SYSCTL_BOOL(_debug, OID_AUTO, link_elf_leak_locals,
208ecd8245eSKonstantin Belousov CTLFLAG_RWTUN, &link_elf_leak_locals, 0,
209ecd8245eSKonstantin Belousov "Allow local symbols to participate in global module symbol resolution");
210ecd8245eSKonstantin Belousov
21136716fe2SMark Johnston typedef int (*elf_reloc_fn)(linker_file_t lf, Elf_Addr relocbase,
21236716fe2SMark Johnston const void *data, int type, elf_lookup_fn lookup);
21336716fe2SMark Johnston
2142f22b3ffSMatthew D Fleming static int parse_dynamic(elf_file_t);
2152f22b3ffSMatthew D Fleming static int relocate_file(elf_file_t);
21636716fe2SMark Johnston static int relocate_file1(elf_file_t ef, elf_lookup_fn lookup,
21736716fe2SMark Johnston elf_reloc_fn reloc, bool ifuncs);
2182f22b3ffSMatthew D Fleming static int link_elf_preload_parse_symbols(elf_file_t);
219326e27d8SDoug Rabson
22047813f5dSMikolaj Golub static struct elf_set_head set_pcpu_list;
22147813f5dSMikolaj Golub #ifdef VIMAGE
22247813f5dSMikolaj Golub static struct elf_set_head set_vnet_list;
22347813f5dSMikolaj Golub #endif
22447813f5dSMikolaj Golub
22547813f5dSMikolaj Golub static void
elf_set_add(struct elf_set_head * list,Elf_Addr start,Elf_Addr stop,Elf_Addr base)22647813f5dSMikolaj Golub elf_set_add(struct elf_set_head *list, Elf_Addr start, Elf_Addr stop, Elf_Addr base)
22747813f5dSMikolaj Golub {
22847813f5dSMikolaj Golub struct elf_set *set, *iter;
22947813f5dSMikolaj Golub
23047813f5dSMikolaj Golub set = malloc(sizeof(*set), M_LINKER, M_WAITOK);
23147813f5dSMikolaj Golub set->es_start = start;
23247813f5dSMikolaj Golub set->es_stop = stop;
23347813f5dSMikolaj Golub set->es_base = base;
23447813f5dSMikolaj Golub
23547813f5dSMikolaj Golub TAILQ_FOREACH(iter, list, es_link) {
23647813f5dSMikolaj Golub KASSERT((set->es_start < iter->es_start && set->es_stop < iter->es_stop) ||
23747813f5dSMikolaj Golub (set->es_start > iter->es_start && set->es_stop > iter->es_stop),
23847813f5dSMikolaj Golub ("linker sets intersection: to insert: 0x%jx-0x%jx; inserted: 0x%jx-0x%jx",
23947813f5dSMikolaj Golub (uintmax_t)set->es_start, (uintmax_t)set->es_stop,
24047813f5dSMikolaj Golub (uintmax_t)iter->es_start, (uintmax_t)iter->es_stop));
24147813f5dSMikolaj Golub
24247813f5dSMikolaj Golub if (iter->es_start > set->es_start) {
24347813f5dSMikolaj Golub TAILQ_INSERT_BEFORE(iter, set, es_link);
24447813f5dSMikolaj Golub break;
24547813f5dSMikolaj Golub }
24647813f5dSMikolaj Golub }
24747813f5dSMikolaj Golub
24847813f5dSMikolaj Golub if (iter == NULL)
24947813f5dSMikolaj Golub TAILQ_INSERT_TAIL(list, set, es_link);
25047813f5dSMikolaj Golub }
25147813f5dSMikolaj Golub
25247813f5dSMikolaj Golub static int
elf_set_find(struct elf_set_head * list,Elf_Addr addr,Elf_Addr * start,Elf_Addr * base)25347813f5dSMikolaj Golub elf_set_find(struct elf_set_head *list, Elf_Addr addr, Elf_Addr *start, Elf_Addr *base)
25447813f5dSMikolaj Golub {
25547813f5dSMikolaj Golub struct elf_set *set;
25647813f5dSMikolaj Golub
25747813f5dSMikolaj Golub TAILQ_FOREACH(set, list, es_link) {
25847813f5dSMikolaj Golub if (addr < set->es_start)
25947813f5dSMikolaj Golub return (0);
26047813f5dSMikolaj Golub if (addr < set->es_stop) {
26147813f5dSMikolaj Golub *start = set->es_start;
26247813f5dSMikolaj Golub *base = set->es_base;
26347813f5dSMikolaj Golub return (1);
26447813f5dSMikolaj Golub }
26547813f5dSMikolaj Golub }
26647813f5dSMikolaj Golub
26747813f5dSMikolaj Golub return (0);
26847813f5dSMikolaj Golub }
26947813f5dSMikolaj Golub
27047813f5dSMikolaj Golub static void
elf_set_delete(struct elf_set_head * list,Elf_Addr start)27147813f5dSMikolaj Golub elf_set_delete(struct elf_set_head *list, Elf_Addr start)
27247813f5dSMikolaj Golub {
27347813f5dSMikolaj Golub struct elf_set *set;
27447813f5dSMikolaj Golub
27547813f5dSMikolaj Golub TAILQ_FOREACH(set, list, es_link) {
27647813f5dSMikolaj Golub if (start < set->es_start)
27747813f5dSMikolaj Golub break;
27847813f5dSMikolaj Golub if (start == set->es_start) {
27947813f5dSMikolaj Golub TAILQ_REMOVE(list, set, es_link);
28047813f5dSMikolaj Golub free(set, M_LINKER);
28147813f5dSMikolaj Golub return;
28247813f5dSMikolaj Golub }
28347813f5dSMikolaj Golub }
28447813f5dSMikolaj Golub KASSERT(0, ("deleting unknown linker set (start = 0x%jx)",
28547813f5dSMikolaj Golub (uintmax_t)start));
28647813f5dSMikolaj Golub }
28747813f5dSMikolaj Golub
288ecb01c64SMarcel Moolenaar #ifdef GDB
2892f22b3ffSMatthew D Fleming static void r_debug_state(struct r_debug *, struct link_map *);
290326e27d8SDoug Rabson
291326e27d8SDoug Rabson /*
292326e27d8SDoug Rabson * A list of loaded modules for GDB to use for loading symbols.
293326e27d8SDoug Rabson */
294326e27d8SDoug Rabson struct r_debug r_debug;
295326e27d8SDoug Rabson
2962f22b3ffSMatthew D Fleming #define GDB_STATE(s) do { \
2972f22b3ffSMatthew D Fleming r_debug.r_state = s; r_debug_state(NULL, NULL); \
2982f22b3ffSMatthew D Fleming } while (0)
299326e27d8SDoug Rabson
300326e27d8SDoug Rabson /*
301326e27d8SDoug Rabson * Function for the debugger to set a breakpoint on to gain control.
302326e27d8SDoug Rabson */
30337c84183SPoul-Henning Kamp static void
r_debug_state(struct r_debug * dummy_one __unused,struct link_map * dummy_two __unused)304ca29467eSJohn Baldwin r_debug_state(struct r_debug *dummy_one __unused,
305ca29467eSJohn Baldwin struct link_map *dummy_two __unused)
306326e27d8SDoug Rabson {
307326e27d8SDoug Rabson }
308326e27d8SDoug Rabson
3091720979bSMarcel Moolenaar static void
link_elf_add_gdb(struct link_map * l)3101720979bSMarcel Moolenaar link_elf_add_gdb(struct link_map *l)
3111720979bSMarcel Moolenaar {
3121720979bSMarcel Moolenaar struct link_map *prev;
3131720979bSMarcel Moolenaar
314c143d6c2SMarcel Moolenaar l->l_next = NULL;
315c143d6c2SMarcel Moolenaar
316c143d6c2SMarcel Moolenaar if (r_debug.r_map == NULL) {
317c143d6c2SMarcel Moolenaar /* Add first. */
318c143d6c2SMarcel Moolenaar l->l_prev = NULL;
319c143d6c2SMarcel Moolenaar r_debug.r_map = l;
320c143d6c2SMarcel Moolenaar } else {
321c143d6c2SMarcel Moolenaar /* Append to list. */
3222f22b3ffSMatthew D Fleming for (prev = r_debug.r_map;
3232f22b3ffSMatthew D Fleming prev->l_next != NULL;
3242f22b3ffSMatthew D Fleming prev = prev->l_next)
3251720979bSMarcel Moolenaar ;
3261720979bSMarcel Moolenaar l->l_prev = prev;
3271720979bSMarcel Moolenaar prev->l_next = l;
3281720979bSMarcel Moolenaar }
329c143d6c2SMarcel Moolenaar }
3301720979bSMarcel Moolenaar
3311720979bSMarcel Moolenaar static void
link_elf_delete_gdb(struct link_map * l)3321720979bSMarcel Moolenaar link_elf_delete_gdb(struct link_map *l)
3331720979bSMarcel Moolenaar {
3341720979bSMarcel Moolenaar if (l->l_prev == NULL) {
335c143d6c2SMarcel Moolenaar /* Remove first. */
3361720979bSMarcel Moolenaar if ((r_debug.r_map = l->l_next) != NULL)
3371720979bSMarcel Moolenaar l->l_next->l_prev = NULL;
338c143d6c2SMarcel Moolenaar } else {
339c143d6c2SMarcel Moolenaar /* Remove any but first. */
3401720979bSMarcel Moolenaar if ((l->l_prev->l_next = l->l_next) != NULL)
3411720979bSMarcel Moolenaar l->l_next->l_prev = l->l_prev;
3421720979bSMarcel Moolenaar }
343c143d6c2SMarcel Moolenaar }
344ecb01c64SMarcel Moolenaar #endif /* GDB */
345a4f67738SDoug Rabson
3464f2945f8SDavid E. O'Brien /*
3474f2945f8SDavid E. O'Brien * The kernel symbol table starts here.
3484f2945f8SDavid E. O'Brien */
3494f2945f8SDavid E. O'Brien extern struct _dynamic _DYNAMIC;
3504f2945f8SDavid E. O'Brien
351a4f67738SDoug Rabson static void
link_elf_error(const char * filename,const char * s)352552f9f63SEdwin Groothuis link_elf_error(const char *filename, const char *s)
3531720979bSMarcel Moolenaar {
354552f9f63SEdwin Groothuis if (filename == NULL)
3551720979bSMarcel Moolenaar printf("kldload: %s\n", s);
356552f9f63SEdwin Groothuis else
357552f9f63SEdwin Groothuis printf("kldload: %s: %s\n", filename, s);
3581720979bSMarcel Moolenaar }
3591720979bSMarcel Moolenaar
3600067051fSMarcel Moolenaar static void
link_elf_invoke_cbs(caddr_t addr,size_t size)3617ef5c19bSMark Johnston link_elf_invoke_cbs(caddr_t addr, size_t size)
3620067051fSMarcel Moolenaar {
3630067051fSMarcel Moolenaar void (**ctor)(void);
3640067051fSMarcel Moolenaar size_t i, cnt;
3650067051fSMarcel Moolenaar
3660067051fSMarcel Moolenaar if (addr == NULL || size == 0)
3670067051fSMarcel Moolenaar return;
3680067051fSMarcel Moolenaar cnt = size / sizeof(*ctor);
3690067051fSMarcel Moolenaar ctor = (void *)addr;
3700067051fSMarcel Moolenaar for (i = 0; i < cnt; i++) {
3710067051fSMarcel Moolenaar if (ctor[i] != NULL)
3720067051fSMarcel Moolenaar (*ctor[i])();
3730067051fSMarcel Moolenaar }
3740067051fSMarcel Moolenaar }
3750067051fSMarcel Moolenaar
3767ef5c19bSMark Johnston static void
link_elf_invoke_ctors(linker_file_t lf)3777ef5c19bSMark Johnston link_elf_invoke_ctors(linker_file_t lf)
3787ef5c19bSMark Johnston {
3797ef5c19bSMark Johnston KASSERT(lf->ctors_invoked == LF_NONE,
3807ef5c19bSMark Johnston ("%s: file %s ctor state %d",
3817ef5c19bSMark Johnston __func__, lf->filename, lf->ctors_invoked));
3827ef5c19bSMark Johnston
3837ef5c19bSMark Johnston link_elf_invoke_cbs(lf->ctors_addr, lf->ctors_size);
3847ef5c19bSMark Johnston lf->ctors_invoked = LF_CTORS;
3857ef5c19bSMark Johnston }
3867ef5c19bSMark Johnston
387c143d6c2SMarcel Moolenaar /*
388c143d6c2SMarcel Moolenaar * Actions performed after linking/loading both the preloaded kernel and any
389c143d6c2SMarcel Moolenaar * modules; whether preloaded or dynamicly loaded.
390c143d6c2SMarcel Moolenaar */
391c143d6c2SMarcel Moolenaar static int
link_elf_link_common_finish(linker_file_t lf)392c143d6c2SMarcel Moolenaar link_elf_link_common_finish(linker_file_t lf)
393c143d6c2SMarcel Moolenaar {
394ecb01c64SMarcel Moolenaar #ifdef GDB
395c143d6c2SMarcel Moolenaar elf_file_t ef = (elf_file_t)lf;
396c143d6c2SMarcel Moolenaar char *newfilename;
397c143d6c2SMarcel Moolenaar #endif
3981aeb23cdSMarcel Moolenaar int error;
3991aeb23cdSMarcel Moolenaar
4001aeb23cdSMarcel Moolenaar /* Notify MD code that a module is being loaded. */
4011aeb23cdSMarcel Moolenaar error = elf_cpu_load_file(lf);
4022f22b3ffSMatthew D Fleming if (error != 0)
4031aeb23cdSMarcel Moolenaar return (error);
404c143d6c2SMarcel Moolenaar
405ecb01c64SMarcel Moolenaar #ifdef GDB
406c143d6c2SMarcel Moolenaar GDB_STATE(RT_ADD);
407c143d6c2SMarcel Moolenaar ef->gdb.l_addr = lf->address;
408a163d034SWarner Losh newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
409c143d6c2SMarcel Moolenaar strcpy(newfilename, lf->filename);
410c143d6c2SMarcel Moolenaar ef->gdb.l_name = newfilename;
411c143d6c2SMarcel Moolenaar ef->gdb.l_ld = ef->dynamic;
412c143d6c2SMarcel Moolenaar link_elf_add_gdb(&ef->gdb);
413c143d6c2SMarcel Moolenaar GDB_STATE(RT_CONSISTENT);
414c143d6c2SMarcel Moolenaar #endif
415c143d6c2SMarcel Moolenaar
4160067051fSMarcel Moolenaar /* Invoke .ctors */
4177ef5c19bSMark Johnston link_elf_invoke_ctors(lf);
418c143d6c2SMarcel Moolenaar return (0);
419c143d6c2SMarcel Moolenaar }
420c143d6c2SMarcel Moolenaar
4219411e24dSBrandon Bergren #ifdef RELOCATABLE_KERNEL
42240b664f6SBrandon Bergren /*
42340b664f6SBrandon Bergren * __startkernel and __endkernel are symbols set up as relocation canaries.
42440b664f6SBrandon Bergren *
42540b664f6SBrandon Bergren * They are defined in locore to reference linker script symbols at the
42640b664f6SBrandon Bergren * beginning and end of the LOAD area. This has the desired side effect of
42740b664f6SBrandon Bergren * giving us variables that have relative relocations pointing at them, so
42840b664f6SBrandon Bergren * relocation of the kernel object will cause the variables to be updated
42940b664f6SBrandon Bergren * automatically by the runtime linker when we initialize.
43040b664f6SBrandon Bergren *
43140b664f6SBrandon Bergren * There are two main reasons to relocate the kernel:
43240b664f6SBrandon Bergren * 1) If the loader needed to load the kernel at an alternate load address.
43340b664f6SBrandon Bergren * 2) If the kernel is switching address spaces on machines like POWER9
43440b664f6SBrandon Bergren * under Radix where the high bits of the effective address are used to
43540b664f6SBrandon Bergren * differentiate between hypervisor, host, guest, and problem state.
43640b664f6SBrandon Bergren */
437ee900504SNathan Whitehorn extern vm_offset_t __startkernel, __endkernel;
4389411e24dSBrandon Bergren #endif
439c6014c73SNathan Whitehorn
440eb5a41cfSLeandro Lupori static unsigned long kern_relbase = KERNBASE;
441eb5a41cfSLeandro Lupori
442eb5a41cfSLeandro Lupori SYSCTL_ULONG(_kern, OID_AUTO, base_address, CTLFLAG_RD,
443eb5a41cfSLeandro Lupori SYSCTL_NULL_ULONG_PTR, KERNBASE, "Kernel base address");
444eb5a41cfSLeandro Lupori SYSCTL_ULONG(_kern, OID_AUTO, relbase_address, CTLFLAG_RD,
445eb5a41cfSLeandro Lupori &kern_relbase, 0, "Kernel relocated base address");
446eb5a41cfSLeandro Lupori
4471720979bSMarcel Moolenaar static void
link_elf_init(void * arg)448a4f67738SDoug Rabson link_elf_init(void* arg)
449a4f67738SDoug Rabson {
450de78ca7eSPeter Wemm Elf_Dyn *dp;
4510067051fSMarcel Moolenaar Elf_Addr *ctors_addrp;
4520067051fSMarcel Moolenaar Elf_Size *ctors_sizep;
453b72ae900SAhmad Khalifa caddr_t baseptr, sizeptr;
454de78ca7eSPeter Wemm elf_file_t ef;
455fe20aaecSRyan Libby const char *modname;
456a4f67738SDoug Rabson
457326e27d8SDoug Rabson linker_add_class(&link_elf_class);
458a4f67738SDoug Rabson
459de78ca7eSPeter Wemm dp = (Elf_Dyn *)&_DYNAMIC;
460b72ae900SAhmad Khalifa modname = (char *)preload_search_info(preload_kmdp, MODINFO_NAME);
461de78ca7eSPeter Wemm if (modname == NULL)
462de78ca7eSPeter Wemm modname = "kernel";
463326e27d8SDoug Rabson linker_kernel_file = linker_make_file(modname, &link_elf_class);
464a4f67738SDoug Rabson if (linker_kernel_file == NULL)
4652f22b3ffSMatthew D Fleming panic("%s: Can't create linker structures for kernel",
4662f22b3ffSMatthew D Fleming __func__);
467326e27d8SDoug Rabson
468326e27d8SDoug Rabson ef = (elf_file_t) linker_kernel_file;
46954823af2SPeter Wemm ef->preloaded = 1;
4709411e24dSBrandon Bergren #ifdef RELOCATABLE_KERNEL
47140b664f6SBrandon Bergren /* Compute relative displacement */
472c6014c73SNathan Whitehorn ef->address = (caddr_t) (__startkernel - KERNBASE);
473c6014c73SNathan Whitehorn #else
474326e27d8SDoug Rabson ef->address = 0;
475c6014c73SNathan Whitehorn #endif
476326e27d8SDoug Rabson #ifdef SPARSE_MAPPING
4771aa696baSMark Johnston ef->object = NULL;
478326e27d8SDoug Rabson #endif
479326e27d8SDoug Rabson ef->dynamic = dp;
480326e27d8SDoug Rabson
4812f22b3ffSMatthew D Fleming if (dp != NULL)
482326e27d8SDoug Rabson parse_dynamic(ef);
4839411e24dSBrandon Bergren #ifdef RELOCATABLE_KERNEL
484ee900504SNathan Whitehorn linker_kernel_file->address = (caddr_t)__startkernel;
485ee900504SNathan Whitehorn linker_kernel_file->size = (intptr_t)(__endkernel - __startkernel);
486eb5a41cfSLeandro Lupori kern_relbase = (unsigned long)__startkernel;
487ee900504SNathan Whitehorn #else
488c6014c73SNathan Whitehorn linker_kernel_file->address += KERNBASE;
489d009ccfaSBruce Evans linker_kernel_file->size = -(intptr_t)linker_kernel_file->address;
490ee900504SNathan Whitehorn #endif
491de78ca7eSPeter Wemm
492b72ae900SAhmad Khalifa if (preload_kmdp != NULL) {
493b72ae900SAhmad Khalifa ef->modptr = preload_kmdp;
494b72ae900SAhmad Khalifa baseptr = preload_search_info(preload_kmdp, MODINFO_ADDR);
4952f22b3ffSMatthew D Fleming if (baseptr != NULL)
496de78ca7eSPeter Wemm linker_kernel_file->address = *(caddr_t *)baseptr;
497b72ae900SAhmad Khalifa sizeptr = preload_search_info(preload_kmdp, MODINFO_SIZE);
4982f22b3ffSMatthew D Fleming if (sizeptr != NULL)
499de78ca7eSPeter Wemm linker_kernel_file->size = *(size_t *)sizeptr;
500b72ae900SAhmad Khalifa ctors_addrp = (Elf_Addr *)preload_search_info(preload_kmdp,
5010067051fSMarcel Moolenaar MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
502b72ae900SAhmad Khalifa ctors_sizep = (Elf_Size *)preload_search_info(preload_kmdp,
5030067051fSMarcel Moolenaar MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
5040067051fSMarcel Moolenaar if (ctors_addrp != NULL && ctors_sizep != NULL) {
505853a17adSEnji Cooper linker_kernel_file->ctors_addr = ef->address +
5060067051fSMarcel Moolenaar *ctors_addrp;
5070067051fSMarcel Moolenaar linker_kernel_file->ctors_size = *ctors_sizep;
5080067051fSMarcel Moolenaar }
509de78ca7eSPeter Wemm }
51054823af2SPeter Wemm (void)link_elf_preload_parse_symbols(ef);
511326e27d8SDoug Rabson
512ecb01c64SMarcel Moolenaar #ifdef GDB
513c143d6c2SMarcel Moolenaar r_debug.r_map = NULL;
514326e27d8SDoug Rabson r_debug.r_brk = r_debug_state;
515326e27d8SDoug Rabson r_debug.r_state = RT_CONSISTENT;
516326e27d8SDoug Rabson #endif
517c143d6c2SMarcel Moolenaar
518c143d6c2SMarcel Moolenaar (void)link_elf_link_common_finish(linker_kernel_file);
51959800afcSJohn Baldwin linker_kernel_file->flags |= LINKER_FILE_LINKED;
52047813f5dSMikolaj Golub TAILQ_INIT(&set_pcpu_list);
52147813f5dSMikolaj Golub #ifdef VIMAGE
52247813f5dSMikolaj Golub TAILQ_INIT(&set_vnet_list);
523cf7974fdSZhenlei Huang vnet_save_init((void *)VNET_START, VNET_STOP - VNET_START);
52447813f5dSMikolaj Golub #endif
525a4f67738SDoug Rabson }
526a4f67738SDoug Rabson
527891cf3edSEd Maste SYSINIT(link_elf, SI_SUB_KLD, SI_ORDER_THIRD, link_elf_init, NULL);
528a4f67738SDoug Rabson
529a4f67738SDoug Rabson static int
link_elf_preload_parse_symbols(elf_file_t ef)53054823af2SPeter Wemm link_elf_preload_parse_symbols(elf_file_t ef)
5312d636ab0SPeter Wemm {
5322d636ab0SPeter Wemm caddr_t pointer;
5332d636ab0SPeter Wemm caddr_t ssym, esym, base;
5342d636ab0SPeter Wemm caddr_t strtab;
5352d636ab0SPeter Wemm int strcnt;
5362d636ab0SPeter Wemm Elf_Sym *symtab;
5372d636ab0SPeter Wemm int symcnt;
5382d636ab0SPeter Wemm
539ca65d5c7SPeter Wemm if (ef->modptr == NULL)
5402f22b3ffSMatthew D Fleming return (0);
5412f22b3ffSMatthew D Fleming pointer = preload_search_info(ef->modptr,
5422f22b3ffSMatthew D Fleming MODINFO_METADATA | MODINFOMD_SSYM);
5432d636ab0SPeter Wemm if (pointer == NULL)
5442f22b3ffSMatthew D Fleming return (0);
5452d636ab0SPeter Wemm ssym = *(caddr_t *)pointer;
5462f22b3ffSMatthew D Fleming pointer = preload_search_info(ef->modptr,
5472f22b3ffSMatthew D Fleming MODINFO_METADATA | MODINFOMD_ESYM);
5482d636ab0SPeter Wemm if (pointer == NULL)
5492f22b3ffSMatthew D Fleming return (0);
5502d636ab0SPeter Wemm esym = *(caddr_t *)pointer;
5512d636ab0SPeter Wemm
5522d636ab0SPeter Wemm base = ssym;
5532d636ab0SPeter Wemm
5542d636ab0SPeter Wemm symcnt = *(long *)base;
5552d636ab0SPeter Wemm base += sizeof(long);
5562d636ab0SPeter Wemm symtab = (Elf_Sym *)base;
5572d636ab0SPeter Wemm base += roundup(symcnt, sizeof(long));
5582d636ab0SPeter Wemm
5592d636ab0SPeter Wemm if (base > esym || base < ssym) {
5602d636ab0SPeter Wemm printf("Symbols are corrupt!\n");
5612f22b3ffSMatthew D Fleming return (EINVAL);
5622d636ab0SPeter Wemm }
5632d636ab0SPeter Wemm
5642d636ab0SPeter Wemm strcnt = *(long *)base;
5652d636ab0SPeter Wemm base += sizeof(long);
5662d636ab0SPeter Wemm strtab = base;
5672d636ab0SPeter Wemm base += roundup(strcnt, sizeof(long));
5682d636ab0SPeter Wemm
5692d636ab0SPeter Wemm if (base > esym || base < ssym) {
5702d636ab0SPeter Wemm printf("Symbols are corrupt!\n");
5712f22b3ffSMatthew D Fleming return (EINVAL);
5722d636ab0SPeter Wemm }
5732d636ab0SPeter Wemm
5742d636ab0SPeter Wemm ef->ddbsymtab = symtab;
5752d636ab0SPeter Wemm ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
5762d636ab0SPeter Wemm ef->ddbstrtab = strtab;
5772d636ab0SPeter Wemm ef->ddbstrcnt = strcnt;
5782d636ab0SPeter Wemm
5792f22b3ffSMatthew D Fleming return (0);
5802d636ab0SPeter Wemm }
5812d636ab0SPeter Wemm
5822d636ab0SPeter Wemm static int
parse_dynamic(elf_file_t ef)583326e27d8SDoug Rabson parse_dynamic(elf_file_t ef)
584a4f67738SDoug Rabson {
585326e27d8SDoug Rabson Elf_Dyn *dp;
586fe3db7c7SDoug Rabson int plttype = DT_REL;
587a4f67738SDoug Rabson
588a4f67738SDoug Rabson for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) {
589a4f67738SDoug Rabson switch (dp->d_tag) {
590a4f67738SDoug Rabson case DT_HASH:
591a4f67738SDoug Rabson {
592a4f67738SDoug Rabson /* From src/libexec/rtld-elf/rtld.c */
5937e510298SJake Burkholder const Elf_Hashelt *hashtab = (const Elf_Hashelt *)
594a4f67738SDoug Rabson (ef->address + dp->d_un.d_ptr);
595a4f67738SDoug Rabson ef->nbuckets = hashtab[0];
596a4f67738SDoug Rabson ef->nchains = hashtab[1];
597a4f67738SDoug Rabson ef->buckets = hashtab + 2;
598a4f67738SDoug Rabson ef->chains = ef->buckets + ef->nbuckets;
599a4f67738SDoug Rabson break;
600a4f67738SDoug Rabson }
601a4f67738SDoug Rabson case DT_STRTAB:
602fe3db7c7SDoug Rabson ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr);
603a4f67738SDoug Rabson break;
6042d636ab0SPeter Wemm case DT_STRSZ:
6052d636ab0SPeter Wemm ef->strsz = dp->d_un.d_val;
6062d636ab0SPeter Wemm break;
607a4f67738SDoug Rabson case DT_SYMTAB:
608fe3db7c7SDoug Rabson ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr);
609a4f67738SDoug Rabson break;
610a4f67738SDoug Rabson case DT_SYMENT:
611a4f67738SDoug Rabson if (dp->d_un.d_val != sizeof(Elf_Sym))
6122f22b3ffSMatthew D Fleming return (ENOEXEC);
613fe3db7c7SDoug Rabson break;
614fe3db7c7SDoug Rabson case DT_PLTGOT:
615fe3db7c7SDoug Rabson ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr);
616fe3db7c7SDoug Rabson break;
617fe3db7c7SDoug Rabson case DT_REL:
618fe3db7c7SDoug Rabson ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
619fe3db7c7SDoug Rabson break;
620fe3db7c7SDoug Rabson case DT_RELSZ:
621fe3db7c7SDoug Rabson ef->relsize = dp->d_un.d_val;
622fe3db7c7SDoug Rabson break;
623fe3db7c7SDoug Rabson case DT_RELENT:
624fe3db7c7SDoug Rabson if (dp->d_un.d_val != sizeof(Elf_Rel))
6252f22b3ffSMatthew D Fleming return (ENOEXEC);
626fe3db7c7SDoug Rabson break;
627fe3db7c7SDoug Rabson case DT_JMPREL:
628fe3db7c7SDoug Rabson ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr);
629fe3db7c7SDoug Rabson break;
630fe3db7c7SDoug Rabson case DT_PLTRELSZ:
631fe3db7c7SDoug Rabson ef->pltrelsize = dp->d_un.d_val;
632fe3db7c7SDoug Rabson break;
633fe3db7c7SDoug Rabson case DT_RELA:
634fe3db7c7SDoug Rabson ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr);
635fe3db7c7SDoug Rabson break;
636fe3db7c7SDoug Rabson case DT_RELASZ:
637fe3db7c7SDoug Rabson ef->relasize = dp->d_un.d_val;
638fe3db7c7SDoug Rabson break;
639fe3db7c7SDoug Rabson case DT_RELAENT:
640fe3db7c7SDoug Rabson if (dp->d_un.d_val != sizeof(Elf_Rela))
6412f22b3ffSMatthew D Fleming return (ENOEXEC);
642fe3db7c7SDoug Rabson break;
643fe3db7c7SDoug Rabson case DT_PLTREL:
644fe3db7c7SDoug Rabson plttype = dp->d_un.d_val;
645fe3db7c7SDoug Rabson if (plttype != DT_REL && plttype != DT_RELA)
6462f22b3ffSMatthew D Fleming return (ENOEXEC);
647fe3db7c7SDoug Rabson break;
648ecb01c64SMarcel Moolenaar #ifdef GDB
649326e27d8SDoug Rabson case DT_DEBUG:
650326e27d8SDoug Rabson dp->d_un.d_ptr = (Elf_Addr)&r_debug;
651326e27d8SDoug Rabson break;
652326e27d8SDoug Rabson #endif
653a4f67738SDoug Rabson }
654a4f67738SDoug Rabson }
655fe3db7c7SDoug Rabson
656fe3db7c7SDoug Rabson if (plttype == DT_RELA) {
657fe3db7c7SDoug Rabson ef->pltrela = (const Elf_Rela *)ef->pltrel;
658fe3db7c7SDoug Rabson ef->pltrel = NULL;
659fe3db7c7SDoug Rabson ef->pltrelasize = ef->pltrelsize;
660fe3db7c7SDoug Rabson ef->pltrelsize = 0;
661fe3db7c7SDoug Rabson }
662fe3db7c7SDoug Rabson
6632d636ab0SPeter Wemm ef->ddbsymtab = ef->symtab;
6642d636ab0SPeter Wemm ef->ddbsymcnt = ef->nchains;
6652d636ab0SPeter Wemm ef->ddbstrtab = ef->strtab;
6662d636ab0SPeter Wemm ef->ddbstrcnt = ef->strsz;
6672d636ab0SPeter Wemm
66830693808SBrandon Bergren return elf_cpu_parse_dynamic(ef->address, ef->dynamic);
669a4f67738SDoug Rabson }
670a4f67738SDoug Rabson
6714c62bffeSBjoern A. Zeeb #define LS_PADDING 0x90909090
672a4f67738SDoug Rabson static int
parse_dpcpu(elf_file_t ef)67350c202c5SJeff Roberson parse_dpcpu(elf_file_t ef)
67450c202c5SJeff Roberson {
6759ae7bc39SBjoern A. Zeeb int error, size;
6764c62bffeSBjoern A. Zeeb #if defined(__i386__)
6774c62bffeSBjoern A. Zeeb uint32_t pad;
6784c62bffeSBjoern A. Zeeb #endif
67950c202c5SJeff Roberson
68050c202c5SJeff Roberson ef->pcpu_start = 0;
68150c202c5SJeff Roberson ef->pcpu_stop = 0;
68250c202c5SJeff Roberson error = link_elf_lookup_set(&ef->lf, "pcpu", (void ***)&ef->pcpu_start,
6839ae7bc39SBjoern A. Zeeb (void ***)&ef->pcpu_stop, NULL);
68450c202c5SJeff Roberson /* Error just means there is no pcpu set to relocate. */
6852f22b3ffSMatthew D Fleming if (error != 0)
68650c202c5SJeff Roberson return (0);
6879ae7bc39SBjoern A. Zeeb size = (uintptr_t)ef->pcpu_stop - (uintptr_t)ef->pcpu_start;
6889ae7bc39SBjoern A. Zeeb /* Empty set? */
6899ae7bc39SBjoern A. Zeeb if (size < 1)
6909ae7bc39SBjoern A. Zeeb return (0);
6914c62bffeSBjoern A. Zeeb #if defined(__i386__)
6924c62bffeSBjoern A. Zeeb /* In case we do find __start/stop_set_ symbols double-check. */
6934c62bffeSBjoern A. Zeeb if (size < 4) {
6944c62bffeSBjoern A. Zeeb uprintf("Kernel module '%s' must be recompiled with "
6954c62bffeSBjoern A. Zeeb "linker script\n", ef->lf.pathname);
6964c62bffeSBjoern A. Zeeb return (ENOEXEC);
6974c62bffeSBjoern A. Zeeb }
6984c62bffeSBjoern A. Zeeb
6994c62bffeSBjoern A. Zeeb /* Padding from linker-script correct? */
7004c62bffeSBjoern A. Zeeb pad = *(uint32_t *)((uintptr_t)ef->pcpu_stop - sizeof(pad));
7014c62bffeSBjoern A. Zeeb if (pad != LS_PADDING) {
7024c62bffeSBjoern A. Zeeb uprintf("Kernel module '%s' must be recompiled with "
7034c62bffeSBjoern A. Zeeb "linker script, invalid padding %#04x (%#04x)\n",
7044c62bffeSBjoern A. Zeeb ef->lf.pathname, pad, LS_PADDING);
7054c62bffeSBjoern A. Zeeb return (ENOEXEC);
7064c62bffeSBjoern A. Zeeb }
7074c62bffeSBjoern A. Zeeb /* If we only have valid padding, nothing to do. */
7084c62bffeSBjoern A. Zeeb if (size == 4)
7094c62bffeSBjoern A. Zeeb return (0);
7104c62bffeSBjoern A. Zeeb #endif
71150c202c5SJeff Roberson /*
7122f22b3ffSMatthew D Fleming * Allocate space in the primary pcpu area. Copy in our
7132f22b3ffSMatthew D Fleming * initialization from the data section and then initialize
7142f22b3ffSMatthew D Fleming * all per-cpu storage from that.
71550c202c5SJeff Roberson */
7169ae7bc39SBjoern A. Zeeb ef->pcpu_base = (Elf_Addr)(uintptr_t)dpcpu_alloc(size);
7170f823b64SBjoern A. Zeeb if (ef->pcpu_base == 0) {
7180f823b64SBjoern A. Zeeb printf("%s: pcpu module space is out of space; "
7190f823b64SBjoern A. Zeeb "cannot allocate %d for %s\n",
7200f823b64SBjoern A. Zeeb __func__, size, ef->lf.pathname);
72150c202c5SJeff Roberson return (ENOSPC);
7220f823b64SBjoern A. Zeeb }
7239ae7bc39SBjoern A. Zeeb memcpy((void *)ef->pcpu_base, (void *)ef->pcpu_start, size);
7249ae7bc39SBjoern A. Zeeb dpcpu_copy((void *)ef->pcpu_base, size);
72547813f5dSMikolaj Golub elf_set_add(&set_pcpu_list, ef->pcpu_start, ef->pcpu_stop,
72647813f5dSMikolaj Golub ef->pcpu_base);
72750c202c5SJeff Roberson
72850c202c5SJeff Roberson return (0);
72950c202c5SJeff Roberson }
73050c202c5SJeff Roberson
731eddfbb76SRobert Watson #ifdef VIMAGE
732eddfbb76SRobert Watson static int
parse_vnet(elf_file_t ef)733eddfbb76SRobert Watson parse_vnet(elf_file_t ef)
734eddfbb76SRobert Watson {
7359ae7bc39SBjoern A. Zeeb int error, size;
7364c62bffeSBjoern A. Zeeb #if defined(__i386__)
7374c62bffeSBjoern A. Zeeb uint32_t pad;
7384c62bffeSBjoern A. Zeeb #endif
739eddfbb76SRobert Watson
740eddfbb76SRobert Watson ef->vnet_start = 0;
741eddfbb76SRobert Watson ef->vnet_stop = 0;
742205821a7SZhenlei Huang ef->vnet_base = 0;
743eddfbb76SRobert Watson error = link_elf_lookup_set(&ef->lf, "vnet", (void ***)&ef->vnet_start,
7449ae7bc39SBjoern A. Zeeb (void ***)&ef->vnet_stop, NULL);
745eddfbb76SRobert Watson /* Error just means there is no vnet data set to relocate. */
7462f22b3ffSMatthew D Fleming if (error != 0)
747eddfbb76SRobert Watson return (0);
7489ae7bc39SBjoern A. Zeeb size = (uintptr_t)ef->vnet_stop - (uintptr_t)ef->vnet_start;
7499ae7bc39SBjoern A. Zeeb /* Empty set? */
7509ae7bc39SBjoern A. Zeeb if (size < 1)
7519ae7bc39SBjoern A. Zeeb return (0);
7524c62bffeSBjoern A. Zeeb #if defined(__i386__)
7534c62bffeSBjoern A. Zeeb /* In case we do find __start/stop_set_ symbols double-check. */
7544c62bffeSBjoern A. Zeeb if (size < 4) {
7554c62bffeSBjoern A. Zeeb uprintf("Kernel module '%s' must be recompiled with "
7564c62bffeSBjoern A. Zeeb "linker script\n", ef->lf.pathname);
7574c62bffeSBjoern A. Zeeb return (ENOEXEC);
7584c62bffeSBjoern A. Zeeb }
7594c62bffeSBjoern A. Zeeb
7604c62bffeSBjoern A. Zeeb /* Padding from linker-script correct? */
7614c62bffeSBjoern A. Zeeb pad = *(uint32_t *)((uintptr_t)ef->vnet_stop - sizeof(pad));
7624c62bffeSBjoern A. Zeeb if (pad != LS_PADDING) {
7634c62bffeSBjoern A. Zeeb uprintf("Kernel module '%s' must be recompiled with "
7644c62bffeSBjoern A. Zeeb "linker script, invalid padding %#04x (%#04x)\n",
7654c62bffeSBjoern A. Zeeb ef->lf.pathname, pad, LS_PADDING);
7664c62bffeSBjoern A. Zeeb return (ENOEXEC);
7674c62bffeSBjoern A. Zeeb }
7684c62bffeSBjoern A. Zeeb /* If we only have valid padding, nothing to do. */
7694c62bffeSBjoern A. Zeeb if (size == 4)
7704c62bffeSBjoern A. Zeeb return (0);
7714c62bffeSBjoern A. Zeeb #endif
772eddfbb76SRobert Watson /*
7732f22b3ffSMatthew D Fleming * Allocate space in the primary vnet area. Copy in our
7742f22b3ffSMatthew D Fleming * initialization from the data section and then initialize
7752f22b3ffSMatthew D Fleming * all per-vnet storage from that.
776eddfbb76SRobert Watson */
7779ae7bc39SBjoern A. Zeeb ef->vnet_base = (Elf_Addr)(uintptr_t)vnet_data_alloc(size);
7780f823b64SBjoern A. Zeeb if (ef->vnet_base == 0) {
7790f823b64SBjoern A. Zeeb printf("%s: vnet module space is out of space; "
7800f823b64SBjoern A. Zeeb "cannot allocate %d for %s\n",
7810f823b64SBjoern A. Zeeb __func__, size, ef->lf.pathname);
782eddfbb76SRobert Watson return (ENOSPC);
7830f823b64SBjoern A. Zeeb }
7849ae7bc39SBjoern A. Zeeb memcpy((void *)ef->vnet_base, (void *)ef->vnet_start, size);
785cf7974fdSZhenlei Huang vnet_save_init((void *)ef->vnet_base, size);
78647813f5dSMikolaj Golub elf_set_add(&set_vnet_list, ef->vnet_start, ef->vnet_stop,
78747813f5dSMikolaj Golub ef->vnet_base);
788eddfbb76SRobert Watson
789eddfbb76SRobert Watson return (0);
790eddfbb76SRobert Watson }
791eddfbb76SRobert Watson #endif
7924c62bffeSBjoern A. Zeeb #undef LS_PADDING
793eddfbb76SRobert Watson
794f822c9e2SMark Johnston /*
795f822c9e2SMark Johnston * Apply the specified protection to the loadable segments of a preloaded linker
796f822c9e2SMark Johnston * file.
797f822c9e2SMark Johnston */
79850c202c5SJeff Roberson static int
preload_protect(elf_file_t ef,vm_prot_t prot)799f822c9e2SMark Johnston preload_protect(elf_file_t ef, vm_prot_t prot)
800f822c9e2SMark Johnston {
801a85ce4adSAndrew Turner #if defined(__aarch64__) || defined(__amd64__)
802f822c9e2SMark Johnston Elf_Ehdr *hdr;
803f822c9e2SMark Johnston Elf_Phdr *phdr, *phlimit;
804f822c9e2SMark Johnston vm_prot_t nprot;
805f822c9e2SMark Johnston int error;
806f822c9e2SMark Johnston
807f822c9e2SMark Johnston error = 0;
808f822c9e2SMark Johnston hdr = (Elf_Ehdr *)ef->address;
809f822c9e2SMark Johnston phdr = (Elf_Phdr *)(ef->address + hdr->e_phoff);
810f822c9e2SMark Johnston phlimit = phdr + hdr->e_phnum;
811f822c9e2SMark Johnston for (; phdr < phlimit; phdr++) {
812f822c9e2SMark Johnston if (phdr->p_type != PT_LOAD)
813f822c9e2SMark Johnston continue;
814f822c9e2SMark Johnston
815f822c9e2SMark Johnston nprot = prot | VM_PROT_READ;
816f822c9e2SMark Johnston if ((phdr->p_flags & PF_W) != 0)
817f822c9e2SMark Johnston nprot |= VM_PROT_WRITE;
818f822c9e2SMark Johnston if ((phdr->p_flags & PF_X) != 0)
819f822c9e2SMark Johnston nprot |= VM_PROT_EXECUTE;
820f822c9e2SMark Johnston error = pmap_change_prot((vm_offset_t)ef->address +
821f822c9e2SMark Johnston phdr->p_vaddr, round_page(phdr->p_memsz), nprot);
822f822c9e2SMark Johnston if (error != 0)
823f822c9e2SMark Johnston break;
824f822c9e2SMark Johnston }
825f822c9e2SMark Johnston return (error);
826f822c9e2SMark Johnston #else
827f822c9e2SMark Johnston return (0);
828f822c9e2SMark Johnston #endif
829f822c9e2SMark Johnston }
830f822c9e2SMark Johnston
831b19c9deaSIan Lepore #ifdef __arm__
832b19c9deaSIan Lepore /*
833b19c9deaSIan Lepore * Locate the ARM exception/unwind table info for DDB and stack(9) use by
834b19c9deaSIan Lepore * searching for the section header that describes it. There may be no unwind
835b19c9deaSIan Lepore * info, for example in a module containing only data.
836b19c9deaSIan Lepore */
837b19c9deaSIan Lepore static void
link_elf_locate_exidx(linker_file_t lf,Elf_Shdr * shdr,int nhdr)838b19c9deaSIan Lepore link_elf_locate_exidx(linker_file_t lf, Elf_Shdr *shdr, int nhdr)
839b19c9deaSIan Lepore {
840b19c9deaSIan Lepore int i;
841b19c9deaSIan Lepore
842b19c9deaSIan Lepore for (i = 0; i < nhdr; i++) {
843b19c9deaSIan Lepore if (shdr[i].sh_type == SHT_ARM_EXIDX) {
844b19c9deaSIan Lepore lf->exidx_addr = shdr[i].sh_addr + lf->address;
845b19c9deaSIan Lepore lf->exidx_size = shdr[i].sh_size;
846b19c9deaSIan Lepore break;
847b19c9deaSIan Lepore }
848b19c9deaSIan Lepore }
849b19c9deaSIan Lepore }
850b19c9deaSIan Lepore
851b19c9deaSIan Lepore /*
852b19c9deaSIan Lepore * Locate the section headers metadata in a preloaded module, then use it to
853b19c9deaSIan Lepore * locate the exception/unwind table in the module. The size of the metadata
854b19c9deaSIan Lepore * block is stored in a uint32 word immediately before the data itself, and a
855b19c9deaSIan Lepore * comment in preload_search_info() says it is safe to rely on that.
856b19c9deaSIan Lepore */
857b19c9deaSIan Lepore static void
link_elf_locate_exidx_preload(struct linker_file * lf,caddr_t modptr)858b19c9deaSIan Lepore link_elf_locate_exidx_preload(struct linker_file *lf, caddr_t modptr)
859b19c9deaSIan Lepore {
860b19c9deaSIan Lepore uint32_t *modinfo;
861b19c9deaSIan Lepore Elf_Shdr *shdr;
862b19c9deaSIan Lepore uint32_t nhdr;
863b19c9deaSIan Lepore
864b19c9deaSIan Lepore modinfo = (uint32_t *)preload_search_info(modptr,
865b19c9deaSIan Lepore MODINFO_METADATA | MODINFOMD_SHDR);
866b19c9deaSIan Lepore if (modinfo != NULL) {
867b19c9deaSIan Lepore shdr = (Elf_Shdr *)modinfo;
868b19c9deaSIan Lepore nhdr = modinfo[-1] / sizeof(Elf_Shdr);
869b19c9deaSIan Lepore link_elf_locate_exidx(lf, shdr, nhdr);
870b19c9deaSIan Lepore }
871b19c9deaSIan Lepore }
872b19c9deaSIan Lepore
873b19c9deaSIan Lepore #endif /* __arm__ */
874b19c9deaSIan Lepore
875f822c9e2SMark Johnston static int
link_elf_link_preload(linker_class_t cls,const char * filename,linker_file_t * result)876f822c9e2SMark Johnston link_elf_link_preload(linker_class_t cls, const char *filename,
877f822c9e2SMark Johnston linker_file_t *result)
878de78ca7eSPeter Wemm {
8790067051fSMarcel Moolenaar Elf_Addr *ctors_addrp;
8800067051fSMarcel Moolenaar Elf_Size *ctors_sizep;
881de78ca7eSPeter Wemm caddr_t modptr, baseptr, sizeptr, dynptr;
882de78ca7eSPeter Wemm char *type;
883de78ca7eSPeter Wemm elf_file_t ef;
884de78ca7eSPeter Wemm linker_file_t lf;
885de78ca7eSPeter Wemm int error;
886de78ca7eSPeter Wemm vm_offset_t dp;
887de78ca7eSPeter Wemm
88854823af2SPeter Wemm /* Look to see if we have the file preloaded */
889de78ca7eSPeter Wemm modptr = preload_search_by_name(filename);
890de78ca7eSPeter Wemm if (modptr == NULL)
8912f22b3ffSMatthew D Fleming return (ENOENT);
892de78ca7eSPeter Wemm
893de78ca7eSPeter Wemm type = (char *)preload_search_info(modptr, MODINFO_TYPE);
894de78ca7eSPeter Wemm baseptr = preload_search_info(modptr, MODINFO_ADDR);
895de78ca7eSPeter Wemm sizeptr = preload_search_info(modptr, MODINFO_SIZE);
8962f22b3ffSMatthew D Fleming dynptr = preload_search_info(modptr,
8972f22b3ffSMatthew D Fleming MODINFO_METADATA | MODINFOMD_DYNAMIC);
898*4d213c59SAhmad Khalifa if (type == NULL || strcmp(type, preload_modtype) != 0)
899de78ca7eSPeter Wemm return (EFTYPE);
900de78ca7eSPeter Wemm if (baseptr == NULL || sizeptr == NULL || dynptr == NULL)
901de78ca7eSPeter Wemm return (EINVAL);
902de78ca7eSPeter Wemm
903326e27d8SDoug Rabson lf = linker_make_file(filename, &link_elf_class);
9042f22b3ffSMatthew D Fleming if (lf == NULL)
9052f22b3ffSMatthew D Fleming return (ENOMEM);
906326e27d8SDoug Rabson
907326e27d8SDoug Rabson ef = (elf_file_t) lf;
908326e27d8SDoug Rabson ef->preloaded = 1;
909ca65d5c7SPeter Wemm ef->modptr = modptr;
910de78ca7eSPeter Wemm ef->address = *(caddr_t *)baseptr;
911de78ca7eSPeter Wemm #ifdef SPARSE_MAPPING
9121aa696baSMark Johnston ef->object = NULL;
913de78ca7eSPeter Wemm #endif
914de78ca7eSPeter Wemm dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
915de78ca7eSPeter Wemm ef->dynamic = (Elf_Dyn *)dp;
916de78ca7eSPeter Wemm lf->address = ef->address;
917de78ca7eSPeter Wemm lf->size = *(size_t *)sizeptr;
918de78ca7eSPeter Wemm
9190067051fSMarcel Moolenaar ctors_addrp = (Elf_Addr *)preload_search_info(modptr,
9200067051fSMarcel Moolenaar MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
9210067051fSMarcel Moolenaar ctors_sizep = (Elf_Size *)preload_search_info(modptr,
9220067051fSMarcel Moolenaar MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
9230067051fSMarcel Moolenaar if (ctors_addrp != NULL && ctors_sizep != NULL) {
9240067051fSMarcel Moolenaar lf->ctors_addr = ef->address + *ctors_addrp;
9250067051fSMarcel Moolenaar lf->ctors_size = *ctors_sizep;
9260067051fSMarcel Moolenaar }
9270067051fSMarcel Moolenaar
928b19c9deaSIan Lepore #ifdef __arm__
929b19c9deaSIan Lepore link_elf_locate_exidx_preload(lf, modptr);
930b19c9deaSIan Lepore #endif
931b19c9deaSIan Lepore
932326e27d8SDoug Rabson error = parse_dynamic(ef);
93350c202c5SJeff Roberson if (error == 0)
93450c202c5SJeff Roberson error = parse_dpcpu(ef);
935eddfbb76SRobert Watson #ifdef VIMAGE
936eddfbb76SRobert Watson if (error == 0)
937eddfbb76SRobert Watson error = parse_vnet(ef);
938eddfbb76SRobert Watson #endif
939f822c9e2SMark Johnston if (error == 0)
940f822c9e2SMark Johnston error = preload_protect(ef, VM_PROT_ALL);
9412f22b3ffSMatthew D Fleming if (error != 0) {
94265a311fcSPoul-Henning Kamp linker_file_unload(lf, LINKER_UNLOAD_FORCE);
9432f22b3ffSMatthew D Fleming return (error);
944de78ca7eSPeter Wemm }
9457251b4bfSJake Burkholder link_elf_reloc_local(lf);
946de78ca7eSPeter Wemm *result = lf;
947de78ca7eSPeter Wemm return (0);
948de78ca7eSPeter Wemm }
949de78ca7eSPeter Wemm
950de78ca7eSPeter Wemm static int
link_elf_link_preload_finish(linker_file_t lf)95154823af2SPeter Wemm link_elf_link_preload_finish(linker_file_t lf)
95254823af2SPeter Wemm {
95354823af2SPeter Wemm elf_file_t ef;
95454823af2SPeter Wemm int error;
95554823af2SPeter Wemm
95654823af2SPeter Wemm ef = (elf_file_t) lf;
95754823af2SPeter Wemm error = relocate_file(ef);
958f822c9e2SMark Johnston if (error == 0)
959f822c9e2SMark Johnston error = preload_protect(ef, VM_PROT_NONE);
9602f22b3ffSMatthew D Fleming if (error != 0)
9612f22b3ffSMatthew D Fleming return (error);
96254823af2SPeter Wemm (void)link_elf_preload_parse_symbols(ef);
96354823af2SPeter Wemm
964c143d6c2SMarcel Moolenaar return (link_elf_link_common_finish(lf));
96554823af2SPeter Wemm }
96654823af2SPeter Wemm
96754823af2SPeter Wemm static int
link_elf_load_file(linker_class_t cls,const char * filename,linker_file_t * result)968c143d6c2SMarcel Moolenaar link_elf_load_file(linker_class_t cls, const char* filename,
969c143d6c2SMarcel Moolenaar linker_file_t* result)
970a4f67738SDoug Rabson {
971a4f67738SDoug Rabson struct nameidata nd;
972b40ce416SJulian Elischer struct thread* td = curthread; /* XXX */
973caab6e90SPeter Wemm Elf_Ehdr *hdr;
9741aa696baSMark Johnston caddr_t firstpage, segbase;
975fe3db7c7SDoug Rabson int nbytes, i;
976fe3db7c7SDoug Rabson Elf_Phdr *phdr;
977fe3db7c7SDoug Rabson Elf_Phdr *phlimit;
9781a330eb0SPeter Wemm Elf_Phdr *segs[MAXSEGS];
979fe3db7c7SDoug Rabson int nsegs;
980fe3db7c7SDoug Rabson Elf_Phdr *phdyn;
981fe3db7c7SDoug Rabson caddr_t mapbase;
982fe3db7c7SDoug Rabson size_t mapsize;
983fe3db7c7SDoug Rabson Elf_Addr base_vaddr;
984fe3db7c7SDoug Rabson Elf_Addr base_vlimit;
985a4f67738SDoug Rabson int error = 0;
986526d0bd5SKonstantin Belousov ssize_t resid;
987526d0bd5SKonstantin Belousov int flags;
988a4f67738SDoug Rabson elf_file_t ef;
989a4f67738SDoug Rabson linker_file_t lf;
990ca65d5c7SPeter Wemm Elf_Shdr *shdr;
991ca65d5c7SPeter Wemm int symtabindex;
992ca65d5c7SPeter Wemm int symstrindex;
9930067051fSMarcel Moolenaar int shstrindex;
994ca65d5c7SPeter Wemm int symcnt;
995ca65d5c7SPeter Wemm int strcnt;
9960067051fSMarcel Moolenaar char *shstrs;
997ca65d5c7SPeter Wemm
998ca65d5c7SPeter Wemm shdr = NULL;
999ca65d5c7SPeter Wemm lf = NULL;
10000067051fSMarcel Moolenaar shstrs = NULL;
1001a4f67738SDoug Rabson
10027e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
1003e6796b67SKirk McKusick flags = FREAD;
10049e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL);
10052f22b3ffSMatthew D Fleming if (error != 0)
10062f22b3ffSMatthew D Fleming return (error);
1007bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd);
10081f49b573SRuslan Ermilov if (nd.ni_vp->v_type != VREG) {
10091f49b573SRuslan Ermilov error = ENOEXEC;
10101f49b573SRuslan Ermilov firstpage = NULL;
10111f49b573SRuslan Ermilov goto out;
10121f49b573SRuslan Ermilov }
1013a3df768bSRobert Watson #ifdef MAC
101430d239bcSRobert Watson error = mac_kld_check_load(curthread->td_ucred, nd.ni_vp);
10152f22b3ffSMatthew D Fleming if (error != 0) {
1016a3df768bSRobert Watson firstpage = NULL;
1017a3df768bSRobert Watson goto out;
1018a3df768bSRobert Watson }
1019a3df768bSRobert Watson #endif
1020a4f67738SDoug Rabson
1021a4f67738SDoug Rabson /*
1022fe3db7c7SDoug Rabson * Read the elf header from the file.
1023a4f67738SDoug Rabson */
1024a163d034SWarner Losh firstpage = malloc(PAGE_SIZE, M_LINKER, M_WAITOK);
1025caab6e90SPeter Wemm hdr = (Elf_Ehdr *)firstpage;
1026caab6e90SPeter Wemm error = vn_rdwr(UIO_READ, nd.ni_vp, firstpage, PAGE_SIZE, 0,
10279ca43589SRobert Watson UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
10289ca43589SRobert Watson &resid, td);
1029caab6e90SPeter Wemm nbytes = PAGE_SIZE - resid;
10302f22b3ffSMatthew D Fleming if (error != 0)
1031a4f67738SDoug Rabson goto out;
1032a4f67738SDoug Rabson
1033caab6e90SPeter Wemm if (!IS_ELF(*hdr)) {
1034fe3db7c7SDoug Rabson error = ENOEXEC;
1035a4f67738SDoug Rabson goto out;
1036fe3db7c7SDoug Rabson }
1037fe3db7c7SDoug Rabson
10382f22b3ffSMatthew D Fleming if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
10392f22b3ffSMatthew D Fleming hdr->e_ident[EI_DATA] != ELF_TARG_DATA) {
1040552f9f63SEdwin Groothuis link_elf_error(filename, "Unsupported file layout");
1041fe3db7c7SDoug Rabson error = ENOEXEC;
1042fe3db7c7SDoug Rabson goto out;
1043fe3db7c7SDoug Rabson }
10442f22b3ffSMatthew D Fleming if (hdr->e_ident[EI_VERSION] != EV_CURRENT ||
10452f22b3ffSMatthew D Fleming hdr->e_version != EV_CURRENT) {
1046552f9f63SEdwin Groothuis link_elf_error(filename, "Unsupported file version");
1047fe3db7c7SDoug Rabson error = ENOEXEC;
1048fe3db7c7SDoug Rabson goto out;
1049fe3db7c7SDoug Rabson }
1050caab6e90SPeter Wemm if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) {
1051a1d7ce03SAttilio Rao error = ENOSYS;
1052fe3db7c7SDoug Rabson goto out;
1053fe3db7c7SDoug Rabson }
1054caab6e90SPeter Wemm if (hdr->e_machine != ELF_TARG_MACH) {
1055552f9f63SEdwin Groothuis link_elf_error(filename, "Unsupported machine");
1056fe3db7c7SDoug Rabson error = ENOEXEC;
1057fe3db7c7SDoug Rabson goto out;
1058fe3db7c7SDoug Rabson }
1059a4f67738SDoug Rabson
1060a4f67738SDoug Rabson /*
10612f22b3ffSMatthew D Fleming * We rely on the program header being in the first page.
10622f22b3ffSMatthew D Fleming * This is not strictly required by the ABI specification, but
10632f22b3ffSMatthew D Fleming * it seems to always true in practice. And, it simplifies
10642f22b3ffSMatthew D Fleming * things considerably.
1065a4f67738SDoug Rabson */
1066caab6e90SPeter Wemm if (!((hdr->e_phentsize == sizeof(Elf_Phdr)) &&
1067caab6e90SPeter Wemm (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) &&
1068caab6e90SPeter Wemm (hdr->e_phoff + hdr->e_phnum*sizeof(Elf_Phdr) <= nbytes)))
1069552f9f63SEdwin Groothuis link_elf_error(filename, "Unreadable program headers");
1070fe3db7c7SDoug Rabson
1071fe3db7c7SDoug Rabson /*
1072fe3db7c7SDoug Rabson * Scan the program header entries, and save key information.
1073fe3db7c7SDoug Rabson *
1074fe3db7c7SDoug Rabson * We rely on there being exactly two load segments, text and data,
1075fe3db7c7SDoug Rabson * in that order.
1076fe3db7c7SDoug Rabson */
1077caab6e90SPeter Wemm phdr = (Elf_Phdr *) (firstpage + hdr->e_phoff);
1078caab6e90SPeter Wemm phlimit = phdr + hdr->e_phnum;
1079fe3db7c7SDoug Rabson nsegs = 0;
1080fe3db7c7SDoug Rabson phdyn = NULL;
1081fe3db7c7SDoug Rabson while (phdr < phlimit) {
1082fe3db7c7SDoug Rabson switch (phdr->p_type) {
1083fe3db7c7SDoug Rabson case PT_LOAD:
10841a330eb0SPeter Wemm if (nsegs == MAXSEGS) {
1085552f9f63SEdwin Groothuis link_elf_error(filename, "Too many sections");
1086fe3db7c7SDoug Rabson error = ENOEXEC;
1087fe3db7c7SDoug Rabson goto out;
1088fe3db7c7SDoug Rabson }
108987b1831fSPoul-Henning Kamp /*
109087b1831fSPoul-Henning Kamp * XXX: We just trust they come in right order ??
109187b1831fSPoul-Henning Kamp */
1092fe3db7c7SDoug Rabson segs[nsegs] = phdr;
1093fe3db7c7SDoug Rabson ++nsegs;
1094fe3db7c7SDoug Rabson break;
1095fe3db7c7SDoug Rabson
1096fe3db7c7SDoug Rabson case PT_DYNAMIC:
1097fe3db7c7SDoug Rabson phdyn = phdr;
1098fe3db7c7SDoug Rabson break;
10999548091bSBoris Popov
11009548091bSBoris Popov case PT_INTERP:
1101a1d7ce03SAttilio Rao error = ENOSYS;
11029548091bSBoris Popov goto out;
1103fe3db7c7SDoug Rabson }
1104fe3db7c7SDoug Rabson
1105fe3db7c7SDoug Rabson ++phdr;
1106fe3db7c7SDoug Rabson }
1107fe3db7c7SDoug Rabson if (phdyn == NULL) {
1108552f9f63SEdwin Groothuis link_elf_error(filename, "Object is not dynamically-linked");
1109fe3db7c7SDoug Rabson error = ENOEXEC;
1110fe3db7c7SDoug Rabson goto out;
1111fe3db7c7SDoug Rabson }
11121a330eb0SPeter Wemm if (nsegs == 0) {
1113552f9f63SEdwin Groothuis link_elf_error(filename, "No sections");
111487b1831fSPoul-Henning Kamp error = ENOEXEC;
111587b1831fSPoul-Henning Kamp goto out;
111687b1831fSPoul-Henning Kamp }
1117fe3db7c7SDoug Rabson
1118fe3db7c7SDoug Rabson /*
11192f22b3ffSMatthew D Fleming * Allocate the entire address space of the object, to stake
11202f22b3ffSMatthew D Fleming * out our contiguous region, and to establish the base
11212f22b3ffSMatthew D Fleming * address for relocation.
1122fe3db7c7SDoug Rabson */
1123fe3db7c7SDoug Rabson base_vaddr = trunc_page(segs[0]->p_vaddr);
11241a330eb0SPeter Wemm base_vlimit = round_page(segs[nsegs - 1]->p_vaddr +
11251a330eb0SPeter Wemm segs[nsegs - 1]->p_memsz);
1126fe3db7c7SDoug Rabson mapsize = base_vlimit - base_vaddr;
1127fe3db7c7SDoug Rabson
1128326e27d8SDoug Rabson lf = linker_make_file(filename, &link_elf_class);
11292f22b3ffSMatthew D Fleming if (lf == NULL) {
1130326e27d8SDoug Rabson error = ENOMEM;
1131326e27d8SDoug Rabson goto out;
1132326e27d8SDoug Rabson }
1133326e27d8SDoug Rabson
1134326e27d8SDoug Rabson ef = (elf_file_t) lf;
1135fe3db7c7SDoug Rabson #ifdef SPARSE_MAPPING
1136fbf2a778SKonstantin Belousov ef->object = vm_pager_allocate(OBJT_PHYS, NULL, mapsize, VM_PROT_ALL,
1137fbf2a778SKonstantin Belousov 0, thread0.td_ucred);
1138fe3db7c7SDoug Rabson if (ef->object == NULL) {
1139fe3db7c7SDoug Rabson error = ENOMEM;
1140fe3db7c7SDoug Rabson goto out;
1141fe3db7c7SDoug Rabson }
11421aa696baSMark Johnston #ifdef __amd64__
11431aa696baSMark Johnston mapbase = (caddr_t)KERNBASE;
11441aa696baSMark Johnston #else
11451aa696baSMark Johnston mapbase = (caddr_t)vm_map_min(kernel_map);
11461aa696baSMark Johnston #endif
11471aa696baSMark Johnston /*
11481aa696baSMark Johnston * Mapping protections are downgraded after relocation processing.
11491aa696baSMark Johnston */
1150fe3db7c7SDoug Rabson error = vm_map_find(kernel_map, ef->object, 0,
11511aa696baSMark Johnston (vm_offset_t *)&mapbase, mapsize, 0, VMFS_OPTIMAL_SPACE,
1152fe3db7c7SDoug Rabson VM_PROT_ALL, VM_PROT_ALL, 0);
11532f22b3ffSMatthew D Fleming if (error != 0) {
1154fe3db7c7SDoug Rabson vm_object_deallocate(ef->object);
11551aa696baSMark Johnston ef->object = NULL;
1156fe3db7c7SDoug Rabson goto out;
1157fe3db7c7SDoug Rabson }
1158fe3db7c7SDoug Rabson #else
115982c174a3SMateusz Guzik mapbase = malloc_exec(mapsize, M_LINKER, M_WAITOK);
1160fe3db7c7SDoug Rabson #endif
11611aa696baSMark Johnston ef->address = mapbase;
1162a4f67738SDoug Rabson
1163a4f67738SDoug Rabson /*
1164a4f67738SDoug Rabson * Read the text and data sections and zero the bss.
1165a4f67738SDoug Rabson */
11661a330eb0SPeter Wemm for (i = 0; i < nsegs; i++) {
11671aa696baSMark Johnston segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
11681aa696baSMark Johnston
11691aa696baSMark Johnston #ifdef SPARSE_MAPPING
11701aa696baSMark Johnston /*
11711aa696baSMark Johnston * Consecutive segments may have different mapping permissions,
11721aa696baSMark Johnston * so be strict and verify that their mappings do not overlap.
11731aa696baSMark Johnston */
11741aa696baSMark Johnston if (((vm_offset_t)segbase & PAGE_MASK) != 0) {
11751aa696baSMark Johnston error = EINVAL;
11761aa696baSMark Johnston goto out;
11771aa696baSMark Johnston }
11781aa696baSMark Johnston
11791aa696baSMark Johnston error = vm_map_wire(kernel_map,
11801aa696baSMark Johnston (vm_offset_t)segbase,
11811aa696baSMark Johnston (vm_offset_t)segbase + round_page(segs[i]->p_memsz),
11821aa696baSMark Johnston VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
11831aa696baSMark Johnston if (error != KERN_SUCCESS) {
11841aa696baSMark Johnston error = ENOMEM;
11851aa696baSMark Johnston goto out;
11861aa696baSMark Johnston }
11871aa696baSMark Johnston #endif
11881aa696baSMark Johnston
1189fe3db7c7SDoug Rabson error = vn_rdwr(UIO_READ, nd.ni_vp,
1190fe3db7c7SDoug Rabson segbase, segs[i]->p_filesz, segs[i]->p_offset,
11919ca43589SRobert Watson UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
11929ca43589SRobert Watson &resid, td);
11932f22b3ffSMatthew D Fleming if (error != 0)
1194a4f67738SDoug Rabson goto out;
1195fe3db7c7SDoug Rabson bzero(segbase + segs[i]->p_filesz,
1196fe3db7c7SDoug Rabson segs[i]->p_memsz - segs[i]->p_filesz);
1197fe3db7c7SDoug Rabson }
1198fe3db7c7SDoug Rabson
1199326e27d8SDoug Rabson ef->dynamic = (Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr);
1200a4f67738SDoug Rabson
1201a4f67738SDoug Rabson lf->address = ef->address;
1202fe3db7c7SDoug Rabson lf->size = mapsize;
1203a4f67738SDoug Rabson
1204326e27d8SDoug Rabson error = parse_dynamic(ef);
12052f22b3ffSMatthew D Fleming if (error != 0)
1206de78ca7eSPeter Wemm goto out;
120750c202c5SJeff Roberson error = parse_dpcpu(ef);
12082f22b3ffSMatthew D Fleming if (error != 0)
120950c202c5SJeff Roberson goto out;
1210eddfbb76SRobert Watson #ifdef VIMAGE
1211eddfbb76SRobert Watson error = parse_vnet(ef);
12122f22b3ffSMatthew D Fleming if (error != 0)
1213eddfbb76SRobert Watson goto out;
1214eddfbb76SRobert Watson #endif
12157251b4bfSJake Burkholder link_elf_reloc_local(lf);
12167251b4bfSJake Burkholder
1217b249ce48SMateusz Guzik VOP_UNLOCK(nd.ni_vp);
12187b9716baSIan Dowse error = linker_load_dependencies(lf);
12194f7afc20SKonstantin Belousov vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
12202f22b3ffSMatthew D Fleming if (error != 0)
1221de78ca7eSPeter Wemm goto out;
1222326e27d8SDoug Rabson error = relocate_file(ef);
12232f22b3ffSMatthew D Fleming if (error != 0)
1224ca65d5c7SPeter Wemm goto out;
1225ca65d5c7SPeter Wemm
12261aa696baSMark Johnston #ifdef SPARSE_MAPPING
12271aa696baSMark Johnston /*
12281aa696baSMark Johnston * Downgrade permissions on text segment mappings now that relocation
12291aa696baSMark Johnston * processing is complete. Restrict permissions on read-only segments.
12301aa696baSMark Johnston */
12311aa696baSMark Johnston for (i = 0; i < nsegs; i++) {
12321aa696baSMark Johnston vm_prot_t prot;
12331aa696baSMark Johnston
12341aa696baSMark Johnston if (segs[i]->p_type != PT_LOAD)
12351aa696baSMark Johnston continue;
12361aa696baSMark Johnston
12371aa696baSMark Johnston prot = VM_PROT_READ;
12381aa696baSMark Johnston if ((segs[i]->p_flags & PF_W) != 0)
12391aa696baSMark Johnston prot |= VM_PROT_WRITE;
12401aa696baSMark Johnston if ((segs[i]->p_flags & PF_X) != 0)
12411aa696baSMark Johnston prot |= VM_PROT_EXECUTE;
12421aa696baSMark Johnston segbase = mapbase + segs[i]->p_vaddr - base_vaddr;
12431aa696baSMark Johnston error = vm_map_protect(kernel_map,
12441aa696baSMark Johnston (vm_offset_t)segbase,
12451aa696baSMark Johnston (vm_offset_t)segbase + round_page(segs[i]->p_memsz),
12460659df6fSKonstantin Belousov prot, 0, VM_MAP_PROTECT_SET_PROT);
12471aa696baSMark Johnston if (error != KERN_SUCCESS) {
12481aa696baSMark Johnston error = ENOMEM;
12491aa696baSMark Johnston goto out;
12501aa696baSMark Johnston }
12511aa696baSMark Johnston }
12521aa696baSMark Johnston #endif
12531aa696baSMark Johnston
12542f22b3ffSMatthew D Fleming /*
12552f22b3ffSMatthew D Fleming * Try and load the symbol table if it's present. (you can
12562f22b3ffSMatthew D Fleming * strip it!)
12572f22b3ffSMatthew D Fleming */
1258caab6e90SPeter Wemm nbytes = hdr->e_shnum * hdr->e_shentsize;
1259caab6e90SPeter Wemm if (nbytes == 0 || hdr->e_shoff == 0)
1260ca65d5c7SPeter Wemm goto nosyms;
1261a163d034SWarner Losh shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
1262ca65d5c7SPeter Wemm error = vn_rdwr(UIO_READ, nd.ni_vp,
1263caab6e90SPeter Wemm (caddr_t)shdr, nbytes, hdr->e_shoff,
12649ca43589SRobert Watson UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
12659ca43589SRobert Watson &resid, td);
12662f22b3ffSMatthew D Fleming if (error != 0)
1267ca65d5c7SPeter Wemm goto out;
12680067051fSMarcel Moolenaar
12690067051fSMarcel Moolenaar /* Read section string table */
12700067051fSMarcel Moolenaar shstrindex = hdr->e_shstrndx;
12710067051fSMarcel Moolenaar if (shstrindex != 0 && shdr[shstrindex].sh_type == SHT_STRTAB &&
12720067051fSMarcel Moolenaar shdr[shstrindex].sh_size != 0) {
12730067051fSMarcel Moolenaar nbytes = shdr[shstrindex].sh_size;
12740067051fSMarcel Moolenaar shstrs = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
12750067051fSMarcel Moolenaar error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shstrs, nbytes,
12760067051fSMarcel Moolenaar shdr[shstrindex].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
12770067051fSMarcel Moolenaar td->td_ucred, NOCRED, &resid, td);
12780067051fSMarcel Moolenaar if (error)
12790067051fSMarcel Moolenaar goto out;
12800067051fSMarcel Moolenaar }
12810067051fSMarcel Moolenaar
1282ca65d5c7SPeter Wemm symtabindex = -1;
1283ca65d5c7SPeter Wemm symstrindex = -1;
1284caab6e90SPeter Wemm for (i = 0; i < hdr->e_shnum; i++) {
1285ca65d5c7SPeter Wemm if (shdr[i].sh_type == SHT_SYMTAB) {
1286ca65d5c7SPeter Wemm symtabindex = i;
1287ca65d5c7SPeter Wemm symstrindex = shdr[i].sh_link;
12880067051fSMarcel Moolenaar } else if (shstrs != NULL && shdr[i].sh_name != 0 &&
12890067051fSMarcel Moolenaar strcmp(shstrs + shdr[i].sh_name, ".ctors") == 0) {
12900067051fSMarcel Moolenaar /* Record relocated address and size of .ctors. */
12910067051fSMarcel Moolenaar lf->ctors_addr = mapbase + shdr[i].sh_addr - base_vaddr;
12920067051fSMarcel Moolenaar lf->ctors_size = shdr[i].sh_size;
1293ca65d5c7SPeter Wemm }
1294ca65d5c7SPeter Wemm }
1295ca65d5c7SPeter Wemm if (symtabindex < 0 || symstrindex < 0)
1296ca65d5c7SPeter Wemm goto nosyms;
1297ca65d5c7SPeter Wemm
1298ca65d5c7SPeter Wemm symcnt = shdr[symtabindex].sh_size;
1299a163d034SWarner Losh ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
1300ca65d5c7SPeter Wemm strcnt = shdr[symstrindex].sh_size;
1301a163d034SWarner Losh ef->strbase = malloc(strcnt, M_LINKER, M_WAITOK);
1302ca65d5c7SPeter Wemm
1303ca65d5c7SPeter Wemm error = vn_rdwr(UIO_READ, nd.ni_vp,
1304ca65d5c7SPeter Wemm ef->symbase, symcnt, shdr[symtabindex].sh_offset,
13059ca43589SRobert Watson UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
13069ca43589SRobert Watson &resid, td);
13072f22b3ffSMatthew D Fleming if (error != 0)
1308ca65d5c7SPeter Wemm goto out;
1309ca65d5c7SPeter Wemm error = vn_rdwr(UIO_READ, nd.ni_vp,
1310ca65d5c7SPeter Wemm ef->strbase, strcnt, shdr[symstrindex].sh_offset,
13119ca43589SRobert Watson UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
13129ca43589SRobert Watson &resid, td);
13132f22b3ffSMatthew D Fleming if (error != 0)
1314ca65d5c7SPeter Wemm goto out;
1315ca65d5c7SPeter Wemm
1316ca65d5c7SPeter Wemm ef->ddbsymcnt = symcnt / sizeof(Elf_Sym);
1317ca65d5c7SPeter Wemm ef->ddbsymtab = (const Elf_Sym *)ef->symbase;
1318ca65d5c7SPeter Wemm ef->ddbstrcnt = strcnt;
1319ca65d5c7SPeter Wemm ef->ddbstrtab = ef->strbase;
1320ca65d5c7SPeter Wemm
13218a710317SMarcel Moolenaar nosyms:
1322b19c9deaSIan Lepore
1323b19c9deaSIan Lepore #ifdef __arm__
1324b19c9deaSIan Lepore link_elf_locate_exidx(lf, shdr, hdr->e_shnum);
1325b19c9deaSIan Lepore #endif
1326b19c9deaSIan Lepore
1327c143d6c2SMarcel Moolenaar error = link_elf_link_common_finish(lf);
13282f22b3ffSMatthew D Fleming if (error != 0)
1329c143d6c2SMarcel Moolenaar goto out;
1330326e27d8SDoug Rabson
1331a4f67738SDoug Rabson *result = lf;
1332a4f67738SDoug Rabson
1333a4f67738SDoug Rabson out:
1334b249ce48SMateusz Guzik VOP_UNLOCK(nd.ni_vp);
1335f1d2d305SKonstantin Belousov vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
13362f22b3ffSMatthew D Fleming if (error != 0 && lf != NULL)
133765a311fcSPoul-Henning Kamp linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1338ca65d5c7SPeter Wemm free(shdr, M_LINKER);
1339caab6e90SPeter Wemm free(firstpage, M_LINKER);
13400067051fSMarcel Moolenaar free(shstrs, M_LINKER);
1341a4f67738SDoug Rabson
13422f22b3ffSMatthew D Fleming return (error);
1343a4f67738SDoug Rabson }
1344a4f67738SDoug Rabson
134550c202c5SJeff Roberson Elf_Addr
elf_relocaddr(linker_file_t lf,Elf_Addr x)134650c202c5SJeff Roberson elf_relocaddr(linker_file_t lf, Elf_Addr x)
134750c202c5SJeff Roberson {
134850c202c5SJeff Roberson elf_file_t ef;
134950c202c5SJeff Roberson
135038dae42cSMark Johnston KASSERT(lf->ops->cls == (kobj_class_t)&link_elf_class,
135138dae42cSMark Johnston ("elf_relocaddr: unexpected linker file %p", lf));
135238dae42cSMark Johnston
135350c202c5SJeff Roberson ef = (elf_file_t)lf;
135450c202c5SJeff Roberson if (x >= ef->pcpu_start && x < ef->pcpu_stop)
135550c202c5SJeff Roberson return ((x - ef->pcpu_start) + ef->pcpu_base);
1356eddfbb76SRobert Watson #ifdef VIMAGE
1357eddfbb76SRobert Watson if (x >= ef->vnet_start && x < ef->vnet_stop)
1358eddfbb76SRobert Watson return ((x - ef->vnet_start) + ef->vnet_base);
1359eddfbb76SRobert Watson #endif
136050c202c5SJeff Roberson return (x);
136150c202c5SJeff Roberson }
136250c202c5SJeff Roberson
1363a4f67738SDoug Rabson static void
link_elf_unload_file(linker_file_t file)1364de78ca7eSPeter Wemm link_elf_unload_file(linker_file_t file)
1365a4f67738SDoug Rabson {
1366326e27d8SDoug Rabson elf_file_t ef = (elf_file_t) file;
1367a4f67738SDoug Rabson
13682f22b3ffSMatthew D Fleming if (ef->pcpu_base != 0) {
13692f22b3ffSMatthew D Fleming dpcpu_free((void *)ef->pcpu_base,
13702f22b3ffSMatthew D Fleming ef->pcpu_stop - ef->pcpu_start);
137147813f5dSMikolaj Golub elf_set_delete(&set_pcpu_list, ef->pcpu_start);
137250c202c5SJeff Roberson }
1373eddfbb76SRobert Watson #ifdef VIMAGE
13742f22b3ffSMatthew D Fleming if (ef->vnet_base != 0) {
13752f22b3ffSMatthew D Fleming vnet_data_free((void *)ef->vnet_base,
13762f22b3ffSMatthew D Fleming ef->vnet_stop - ef->vnet_start);
137747813f5dSMikolaj Golub elf_set_delete(&set_vnet_list, ef->vnet_start);
1378eddfbb76SRobert Watson }
1379eddfbb76SRobert Watson #endif
1380ecb01c64SMarcel Moolenaar #ifdef GDB
13812f22b3ffSMatthew D Fleming if (ef->gdb.l_ld != NULL) {
1382326e27d8SDoug Rabson GDB_STATE(RT_DELETE);
1383ed6c3888SPeter Wemm free((void *)(uintptr_t)ef->gdb.l_name, M_LINKER);
1384326e27d8SDoug Rabson link_elf_delete_gdb(&ef->gdb);
1385326e27d8SDoug Rabson GDB_STATE(RT_CONSISTENT);
1386326e27d8SDoug Rabson }
1387326e27d8SDoug Rabson #endif
1388326e27d8SDoug Rabson
13891aeb23cdSMarcel Moolenaar /* Notify MD code that a module is being unloaded. */
13901aeb23cdSMarcel Moolenaar elf_cpu_unload_file(file);
13911aeb23cdSMarcel Moolenaar
1392326e27d8SDoug Rabson if (ef->preloaded) {
139354823af2SPeter Wemm link_elf_unload_preload(file);
1394326e27d8SDoug Rabson return;
1395326e27d8SDoug Rabson }
1396c143d6c2SMarcel Moolenaar
1397fe3db7c7SDoug Rabson #ifdef SPARSE_MAPPING
13982f22b3ffSMatthew D Fleming if (ef->object != NULL) {
1399fe3db7c7SDoug Rabson vm_map_remove(kernel_map, (vm_offset_t) ef->address,
1400fe3db7c7SDoug Rabson (vm_offset_t) ef->address
1401fe3db7c7SDoug Rabson + (ef->object->size << PAGE_SHIFT));
1402fe3db7c7SDoug Rabson }
1403fe3db7c7SDoug Rabson #else
1404a4f67738SDoug Rabson free(ef->address, M_LINKER);
1405fe3db7c7SDoug Rabson #endif
1406ca65d5c7SPeter Wemm free(ef->symbase, M_LINKER);
1407ca65d5c7SPeter Wemm free(ef->strbase, M_LINKER);
1408a2024a3eSJohn Birrell free(ef->ctftab, M_LINKER);
1409a2024a3eSJohn Birrell free(ef->ctfoff, M_LINKER);
1410a2024a3eSJohn Birrell free(ef->typoff, M_LINKER);
1411a4f67738SDoug Rabson }
1412a4f67738SDoug Rabson
1413de78ca7eSPeter Wemm static void
link_elf_unload_preload(linker_file_t file)141454823af2SPeter Wemm link_elf_unload_preload(linker_file_t file)
1415de78ca7eSPeter Wemm {
1416f822c9e2SMark Johnston
141773624a80SMark Johnston if (file->pathname != NULL)
141873624a80SMark Johnston preload_delete_name(file->pathname);
1419de78ca7eSPeter Wemm }
1420de78ca7eSPeter Wemm
1421fe3db7c7SDoug Rabson static const char *
symbol_name(elf_file_t ef,Elf_Size r_info)1422757686b1SMarcel Moolenaar symbol_name(elf_file_t ef, Elf_Size r_info)
1423a4f67738SDoug Rabson {
1424fe3db7c7SDoug Rabson const Elf_Sym *ref;
1425a4f67738SDoug Rabson
1426aa855a59SPeter Wemm if (ELF_R_SYM(r_info)) {
1427aa855a59SPeter Wemm ref = ef->symtab + ELF_R_SYM(r_info);
14282f22b3ffSMatthew D Fleming return (ef->strtab + ref->st_name);
14292f22b3ffSMatthew D Fleming }
14302f22b3ffSMatthew D Fleming return (NULL);
1431a4f67738SDoug Rabson }
1432a4f67738SDoug Rabson
1433a4f67738SDoug Rabson static int
symbol_type(elf_file_t ef,Elf_Size r_info)143436716fe2SMark Johnston symbol_type(elf_file_t ef, Elf_Size r_info)
1435a4f67738SDoug Rabson {
143636716fe2SMark Johnston const Elf_Sym *ref;
143736716fe2SMark Johnston
143836716fe2SMark Johnston if (ELF_R_SYM(r_info)) {
143936716fe2SMark Johnston ref = ef->symtab + ELF_R_SYM(r_info);
144036716fe2SMark Johnston return (ELF_ST_TYPE(ref->st_info));
144136716fe2SMark Johnston }
144236716fe2SMark Johnston return (STT_NOTYPE);
144336716fe2SMark Johnston }
144436716fe2SMark Johnston
144536716fe2SMark Johnston static int
relocate_file1(elf_file_t ef,elf_lookup_fn lookup,elf_reloc_fn reloc,bool ifuncs)144636716fe2SMark Johnston relocate_file1(elf_file_t ef, elf_lookup_fn lookup, elf_reloc_fn reloc,
144736716fe2SMark Johnston bool ifuncs)
144836716fe2SMark Johnston {
1449fe3db7c7SDoug Rabson const Elf_Rel *rel;
1450fe3db7c7SDoug Rabson const Elf_Rela *rela;
1451aa855a59SPeter Wemm const char *symname;
1452a4f67738SDoug Rabson
14539d6ae1e3SColin Percival TSENTER();
145436716fe2SMark Johnston #define APPLY_RELOCS(iter, tbl, tblsize, type) do { \
145536716fe2SMark Johnston for ((iter) = (tbl); (iter) != NULL && \
145636716fe2SMark Johnston (iter) < (tbl) + (tblsize) / sizeof(*(iter)); (iter)++) { \
145736716fe2SMark Johnston if ((symbol_type(ef, (iter)->r_info) == \
145836716fe2SMark Johnston STT_GNU_IFUNC || \
145936716fe2SMark Johnston elf_is_ifunc_reloc((iter)->r_info)) != ifuncs) \
146036716fe2SMark Johnston continue; \
146136716fe2SMark Johnston if (reloc(&ef->lf, (Elf_Addr)ef->address, \
146236716fe2SMark Johnston (iter), (type), lookup)) { \
146336716fe2SMark Johnston symname = symbol_name(ef, (iter)->r_info); \
146436716fe2SMark Johnston printf("link_elf: symbol %s undefined\n", \
146536716fe2SMark Johnston symname); \
146636716fe2SMark Johnston return (ENOENT); \
146736716fe2SMark Johnston } \
146836716fe2SMark Johnston } \
146936716fe2SMark Johnston } while (0)
1470a4f67738SDoug Rabson
147136716fe2SMark Johnston APPLY_RELOCS(rel, ef->rel, ef->relsize, ELF_RELOC_REL);
14729d6ae1e3SColin Percival TSENTER2("ef->rela");
147336716fe2SMark Johnston APPLY_RELOCS(rela, ef->rela, ef->relasize, ELF_RELOC_RELA);
14749d6ae1e3SColin Percival TSEXIT2("ef->rela");
147536716fe2SMark Johnston APPLY_RELOCS(rel, ef->pltrel, ef->pltrelsize, ELF_RELOC_REL);
147636716fe2SMark Johnston APPLY_RELOCS(rela, ef->pltrela, ef->pltrelasize, ELF_RELOC_RELA);
1477a4f67738SDoug Rabson
147836716fe2SMark Johnston #undef APPLY_RELOCS
1479a4f67738SDoug Rabson
14809d6ae1e3SColin Percival TSEXIT();
14812f22b3ffSMatthew D Fleming return (0);
1482a4f67738SDoug Rabson }
1483a4f67738SDoug Rabson
14847035cf14SKonstantin Belousov static int
relocate_file(elf_file_t ef)14857035cf14SKonstantin Belousov relocate_file(elf_file_t ef)
14867035cf14SKonstantin Belousov {
148736716fe2SMark Johnston int error;
14887035cf14SKonstantin Belousov
148936716fe2SMark Johnston error = relocate_file1(ef, elf_lookup, elf_reloc, false);
149036716fe2SMark Johnston if (error == 0)
149136716fe2SMark Johnston error = relocate_file1(ef, elf_lookup, elf_reloc, true);
149236716fe2SMark Johnston return (error);
14937035cf14SKonstantin Belousov }
14947035cf14SKonstantin Belousov
1495fe3db7c7SDoug Rabson /*
14962ef2c26fSEd Maste * SysV hash function for symbol table lookup. It is specified by the
14972ef2c26fSEd Maste * System V ABI.
1498fe3db7c7SDoug Rabson */
14992ef2c26fSEd Maste static Elf32_Word
elf_hash(const char * name)1500fe3db7c7SDoug Rabson elf_hash(const char *name)
1501a4f67738SDoug Rabson {
1502fe3db7c7SDoug Rabson const unsigned char *p = (const unsigned char *)name;
15032ef2c26fSEd Maste Elf32_Word h = 0;
1504a4f67738SDoug Rabson
1505fe3db7c7SDoug Rabson while (*p != '\0') {
1506fe3db7c7SDoug Rabson h = (h << 4) + *p++;
15072ef2c26fSEd Maste h ^= (h >> 24) & 0xf0;
1508a4f67738SDoug Rabson }
15092ef2c26fSEd Maste return (h & 0x0fffffff);
1510fe3db7c7SDoug Rabson }
1511a4f67738SDoug Rabson
151237c84183SPoul-Henning Kamp static int
link_elf_lookup_symbol1(linker_file_t lf,const char * name,c_linker_sym_t * sym,bool see_local)151395c20fafSKonstantin Belousov link_elf_lookup_symbol1(linker_file_t lf, const char *name, c_linker_sym_t *sym,
151495c20fafSKonstantin Belousov bool see_local)
1515a4f67738SDoug Rabson {
1516326e27d8SDoug Rabson elf_file_t ef = (elf_file_t) lf;
1517fe3db7c7SDoug Rabson unsigned long symnum;
15182d636ab0SPeter Wemm const Elf_Sym* symp;
15192d636ab0SPeter Wemm const char *strp;
15202ef2c26fSEd Maste Elf32_Word hash;
1521a4f67738SDoug Rabson
1522f6494f2eSJohn Baldwin /* If we don't have a hash, bail. */
1523f6494f2eSJohn Baldwin if (ef->buckets == NULL || ef->nbuckets == 0) {
1524f6494f2eSJohn Baldwin printf("link_elf_lookup_symbol: missing symbol hash table\n");
15252f22b3ffSMatthew D Fleming return (ENOENT);
1526f6494f2eSJohn Baldwin }
1527f6494f2eSJohn Baldwin
15282d636ab0SPeter Wemm /* First, search hashed global symbols */
1529fe3db7c7SDoug Rabson hash = elf_hash(name);
1530fe3db7c7SDoug Rabson symnum = ef->buckets[hash % ef->nbuckets];
1531fe3db7c7SDoug Rabson
1532fe3db7c7SDoug Rabson while (symnum != STN_UNDEF) {
1533fe3db7c7SDoug Rabson if (symnum >= ef->nchains) {
15342f22b3ffSMatthew D Fleming printf("%s: corrupt symbol table\n", __func__);
15352f22b3ffSMatthew D Fleming return (ENOENT);
1536a4f67738SDoug Rabson }
1537fe3db7c7SDoug Rabson
1538fe3db7c7SDoug Rabson symp = ef->symtab + symnum;
1539fe3db7c7SDoug Rabson if (symp->st_name == 0) {
15402f22b3ffSMatthew D Fleming printf("%s: corrupt symbol table\n", __func__);
15412f22b3ffSMatthew D Fleming return (ENOENT);
1542fe3db7c7SDoug Rabson }
1543fe3db7c7SDoug Rabson
1544fe3db7c7SDoug Rabson strp = ef->strtab + symp->st_name;
1545fe3db7c7SDoug Rabson
1546fe3db7c7SDoug Rabson if (strcmp(name, strp) == 0) {
1547fe3db7c7SDoug Rabson if (symp->st_shndx != SHN_UNDEF ||
1548fe3db7c7SDoug Rabson (symp->st_value != 0 &&
15497035cf14SKonstantin Belousov (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
15507035cf14SKonstantin Belousov ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC))) {
155195c20fafSKonstantin Belousov if (see_local ||
155295c20fafSKonstantin Belousov ELF_ST_BIND(symp->st_info) != STB_LOCAL) {
1553d254af07SMatthew Dillon *sym = (c_linker_sym_t) symp;
15542f22b3ffSMatthew D Fleming return (0);
15552f22b3ffSMatthew D Fleming }
155695c20fafSKonstantin Belousov }
15572f22b3ffSMatthew D Fleming return (ENOENT);
1558fe3db7c7SDoug Rabson }
1559fe3db7c7SDoug Rabson
1560fe3db7c7SDoug Rabson symnum = ef->chains[symnum];
1561a4f67738SDoug Rabson }
1562a4f67738SDoug Rabson
15632f22b3ffSMatthew D Fleming return (ENOENT);
156495c20fafSKonstantin Belousov }
15652d636ab0SPeter Wemm
156695c20fafSKonstantin Belousov static int
link_elf_lookup_symbol(linker_file_t lf,const char * name,c_linker_sym_t * sym)156795c20fafSKonstantin Belousov link_elf_lookup_symbol(linker_file_t lf, const char *name, c_linker_sym_t *sym)
156895c20fafSKonstantin Belousov {
1569ecd8245eSKonstantin Belousov if (link_elf_leak_locals)
1570ecd8245eSKonstantin Belousov return (link_elf_lookup_debug_symbol(lf, name, sym));
157195c20fafSKonstantin Belousov return (link_elf_lookup_symbol1(lf, name, sym, false));
157295c20fafSKonstantin Belousov }
157395c20fafSKonstantin Belousov
157495c20fafSKonstantin Belousov static int
link_elf_lookup_debug_symbol(linker_file_t lf,const char * name,c_linker_sym_t * sym)157595c20fafSKonstantin Belousov link_elf_lookup_debug_symbol(linker_file_t lf, const char *name,
157695c20fafSKonstantin Belousov c_linker_sym_t *sym)
157795c20fafSKonstantin Belousov {
157895c20fafSKonstantin Belousov elf_file_t ef = (elf_file_t)lf;
157995c20fafSKonstantin Belousov const Elf_Sym* symp;
158095c20fafSKonstantin Belousov const char *strp;
158195c20fafSKonstantin Belousov int i;
158295c20fafSKonstantin Belousov
158395c20fafSKonstantin Belousov if (link_elf_lookup_symbol1(lf, name, sym, true) == 0)
158495c20fafSKonstantin Belousov return (0);
158595c20fafSKonstantin Belousov
15862d636ab0SPeter Wemm for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
15872d636ab0SPeter Wemm strp = ef->ddbstrtab + symp->st_name;
15882d636ab0SPeter Wemm if (strcmp(name, strp) == 0) {
15892d636ab0SPeter Wemm if (symp->st_shndx != SHN_UNDEF ||
15902d636ab0SPeter Wemm (symp->st_value != 0 &&
15917035cf14SKonstantin Belousov (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
15927035cf14SKonstantin Belousov ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC))) {
1593d254af07SMatthew Dillon *sym = (c_linker_sym_t) symp;
15942f22b3ffSMatthew D Fleming return (0);
15952f22b3ffSMatthew D Fleming }
15962f22b3ffSMatthew D Fleming return (ENOENT);
15972d636ab0SPeter Wemm }
15982d636ab0SPeter Wemm }
15992d636ab0SPeter Wemm
16002f22b3ffSMatthew D Fleming return (ENOENT);
1601a4f67738SDoug Rabson }
1602a4f67738SDoug Rabson
1603de78ca7eSPeter Wemm static int
link_elf_lookup_debug_symbol_ctf(linker_file_t lf,const char * name,c_linker_sym_t * sym,linker_ctf_t * lc)1604c21bc6f3SBojan Novković link_elf_lookup_debug_symbol_ctf(linker_file_t lf, const char *name,
1605c21bc6f3SBojan Novković c_linker_sym_t *sym, linker_ctf_t *lc)
1606c21bc6f3SBojan Novković {
1607c21bc6f3SBojan Novković elf_file_t ef = (elf_file_t)lf;
1608c21bc6f3SBojan Novković const Elf_Sym *symp;
1609c21bc6f3SBojan Novković const char *strp;
1610c21bc6f3SBojan Novković int i;
1611c21bc6f3SBojan Novković
1612c21bc6f3SBojan Novković for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1613c21bc6f3SBojan Novković strp = ef->ddbstrtab + symp->st_name;
1614c21bc6f3SBojan Novković if (strcmp(name, strp) == 0) {
1615c21bc6f3SBojan Novković if (symp->st_shndx != SHN_UNDEF ||
1616c21bc6f3SBojan Novković (symp->st_value != 0 &&
1617c21bc6f3SBojan Novković (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
1618c21bc6f3SBojan Novković ELF_ST_TYPE(symp->st_info) ==
1619c21bc6f3SBojan Novković STT_GNU_IFUNC))) {
1620c21bc6f3SBojan Novković *sym = (c_linker_sym_t)symp;
1621c21bc6f3SBojan Novković break;
1622c21bc6f3SBojan Novković }
1623c21bc6f3SBojan Novković return (ENOENT);
1624c21bc6f3SBojan Novković }
1625c21bc6f3SBojan Novković }
1626c21bc6f3SBojan Novković
1627c21bc6f3SBojan Novković /* Populate CTF info structure if symbol was found. */
1628c21bc6f3SBojan Novković return (i < ef->ddbsymcnt ? link_elf_ctf_get_ddb(lf, lc) : ENOENT);
1629c21bc6f3SBojan Novković }
1630c21bc6f3SBojan Novković
1631c21bc6f3SBojan Novković static int
link_elf_symbol_values1(linker_file_t lf,c_linker_sym_t sym,linker_symval_t * symval,bool see_local)163295c20fafSKonstantin Belousov link_elf_symbol_values1(linker_file_t lf, c_linker_sym_t sym,
163395c20fafSKonstantin Belousov linker_symval_t *symval, bool see_local)
1634a4f67738SDoug Rabson {
16357035cf14SKonstantin Belousov elf_file_t ef;
16367035cf14SKonstantin Belousov const Elf_Sym *es;
16377035cf14SKonstantin Belousov caddr_t val;
1638a4f67738SDoug Rabson
16397035cf14SKonstantin Belousov ef = (elf_file_t)lf;
16407035cf14SKonstantin Belousov es = (const Elf_Sym *)sym;
164195c20fafSKonstantin Belousov if (es >= ef->symtab && es < ef->symtab + ef->nchains) {
164295c20fafSKonstantin Belousov if (!see_local && ELF_ST_BIND(es->st_info) == STB_LOCAL)
164395c20fafSKonstantin Belousov return (ENOENT);
1644a4f67738SDoug Rabson symval->name = ef->strtab + es->st_name;
16457035cf14SKonstantin Belousov val = (caddr_t)ef->address + es->st_value;
16467035cf14SKonstantin Belousov if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
16477035cf14SKonstantin Belousov val = ((caddr_t (*)(void))val)();
16487035cf14SKonstantin Belousov symval->value = val;
1649a4f67738SDoug Rabson symval->size = es->st_size;
16502f22b3ffSMatthew D Fleming return (0);
1651a4f67738SDoug Rabson }
165295c20fafSKonstantin Belousov return (ENOENT);
165395c20fafSKonstantin Belousov }
165495c20fafSKonstantin Belousov
165595c20fafSKonstantin Belousov static int
link_elf_symbol_values(linker_file_t lf,c_linker_sym_t sym,linker_symval_t * symval)165695c20fafSKonstantin Belousov link_elf_symbol_values(linker_file_t lf, c_linker_sym_t sym,
165795c20fafSKonstantin Belousov linker_symval_t *symval)
165895c20fafSKonstantin Belousov {
1659ecd8245eSKonstantin Belousov if (link_elf_leak_locals)
1660ecd8245eSKonstantin Belousov return (link_elf_debug_symbol_values(lf, sym, symval));
166195c20fafSKonstantin Belousov return (link_elf_symbol_values1(lf, sym, symval, false));
166295c20fafSKonstantin Belousov }
166395c20fafSKonstantin Belousov
166495c20fafSKonstantin Belousov static int
link_elf_debug_symbol_values(linker_file_t lf,c_linker_sym_t sym,linker_symval_t * symval)166595c20fafSKonstantin Belousov link_elf_debug_symbol_values(linker_file_t lf, c_linker_sym_t sym,
166695c20fafSKonstantin Belousov linker_symval_t *symval)
166795c20fafSKonstantin Belousov {
166895c20fafSKonstantin Belousov elf_file_t ef = (elf_file_t)lf;
166995c20fafSKonstantin Belousov const Elf_Sym *es = (const Elf_Sym *)sym;
167095c20fafSKonstantin Belousov caddr_t val;
167195c20fafSKonstantin Belousov
167295c20fafSKonstantin Belousov if (link_elf_symbol_values1(lf, sym, symval, true) == 0)
167395c20fafSKonstantin Belousov return (0);
16742d636ab0SPeter Wemm if (ef->symtab == ef->ddbsymtab)
16752f22b3ffSMatthew D Fleming return (ENOENT);
167695c20fafSKonstantin Belousov
16775cf87418SMarcel Moolenaar if (es >= ef->ddbsymtab && es < (ef->ddbsymtab + ef->ddbsymcnt)) {
16782d636ab0SPeter Wemm symval->name = ef->ddbstrtab + es->st_name;
16797035cf14SKonstantin Belousov val = (caddr_t)ef->address + es->st_value;
16807035cf14SKonstantin Belousov if (ELF_ST_TYPE(es->st_info) == STT_GNU_IFUNC)
16817035cf14SKonstantin Belousov val = ((caddr_t (*)(void))val)();
16827035cf14SKonstantin Belousov symval->value = val;
16832d636ab0SPeter Wemm symval->size = es->st_size;
16842f22b3ffSMatthew D Fleming return (0);
16852d636ab0SPeter Wemm }
16862f22b3ffSMatthew D Fleming return (ENOENT);
16872d636ab0SPeter Wemm }
1688a4f67738SDoug Rabson
1689a4f67738SDoug Rabson static int
link_elf_search_symbol(linker_file_t lf,caddr_t value,c_linker_sym_t * sym,long * diffp)1690a4f67738SDoug Rabson link_elf_search_symbol(linker_file_t lf, caddr_t value,
1691d254af07SMatthew Dillon c_linker_sym_t *sym, long *diffp)
1692a4f67738SDoug Rabson {
1693326e27d8SDoug Rabson elf_file_t ef = (elf_file_t)lf;
1694586453feSBruce Evans u_long off = (uintptr_t)(void *)value;
1695a4f67738SDoug Rabson u_long diff = off;
1696586453feSBruce Evans u_long st_value;
1697fe3db7c7SDoug Rabson const Elf_Sym *es;
1698b85f65afSPedro F. Giffuni const Elf_Sym *best = NULL;
1699a4f67738SDoug Rabson int i;
1700a4f67738SDoug Rabson
17012d636ab0SPeter Wemm for (i = 0, es = ef->ddbsymtab; i < ef->ddbsymcnt; i++, es++) {
1702a4f67738SDoug Rabson if (es->st_name == 0)
1703a4f67738SDoug Rabson continue;
1704586453feSBruce Evans st_value = es->st_value + (uintptr_t) (void *) ef->address;
1705b5abfb70SPeter Wemm if (off >= st_value) {
1706b5abfb70SPeter Wemm if (off - st_value < diff) {
1707b5abfb70SPeter Wemm diff = off - st_value;
1708a4f67738SDoug Rabson best = es;
1709a4f67738SDoug Rabson if (diff == 0)
1710a4f67738SDoug Rabson break;
1711b5abfb70SPeter Wemm } else if (off - st_value == diff) {
1712a4f67738SDoug Rabson best = es;
1713a4f67738SDoug Rabson }
1714a4f67738SDoug Rabson }
1715a4f67738SDoug Rabson }
1716b85f65afSPedro F. Giffuni if (best == NULL)
1717a4f67738SDoug Rabson *diffp = off;
1718a4f67738SDoug Rabson else
1719a4f67738SDoug Rabson *diffp = diff;
1720d254af07SMatthew Dillon *sym = (c_linker_sym_t) best;
1721a4f67738SDoug Rabson
17222f22b3ffSMatthew D Fleming return (0);
1723a4f67738SDoug Rabson }
1724f41325dbSPeter Wemm
1725f41325dbSPeter Wemm /*
1726f41325dbSPeter Wemm * Look up a linker set on an ELF system.
1727f41325dbSPeter Wemm */
1728f41325dbSPeter Wemm static int
link_elf_lookup_set(linker_file_t lf,const char * name,void *** startp,void *** stopp,int * countp)1729f41325dbSPeter Wemm link_elf_lookup_set(linker_file_t lf, const char *name,
1730f41325dbSPeter Wemm void ***startp, void ***stopp, int *countp)
1731f41325dbSPeter Wemm {
1732f41325dbSPeter Wemm c_linker_sym_t sym;
1733f41325dbSPeter Wemm linker_symval_t symval;
1734f41325dbSPeter Wemm char *setsym;
1735f41325dbSPeter Wemm void **start, **stop;
1736f41325dbSPeter Wemm int len, error = 0, count;
1737f41325dbSPeter Wemm
1738f41325dbSPeter Wemm len = strlen(name) + sizeof("__start_set_"); /* sizeof includes \0 */
1739a163d034SWarner Losh setsym = malloc(len, M_LINKER, M_WAITOK);
1740f41325dbSPeter Wemm
1741f41325dbSPeter Wemm /* get address of first entry */
1742f41325dbSPeter Wemm snprintf(setsym, len, "%s%s", "__start_set_", name);
1743f41325dbSPeter Wemm error = link_elf_lookup_symbol(lf, setsym, &sym);
17442f22b3ffSMatthew D Fleming if (error != 0)
1745f41325dbSPeter Wemm goto out;
1746f41325dbSPeter Wemm link_elf_symbol_values(lf, sym, &symval);
1747f41325dbSPeter Wemm if (symval.value == 0) {
1748f41325dbSPeter Wemm error = ESRCH;
1749f41325dbSPeter Wemm goto out;
1750f41325dbSPeter Wemm }
1751f41325dbSPeter Wemm start = (void **)symval.value;
1752f41325dbSPeter Wemm
1753f41325dbSPeter Wemm /* get address of last entry */
1754f41325dbSPeter Wemm snprintf(setsym, len, "%s%s", "__stop_set_", name);
1755f41325dbSPeter Wemm error = link_elf_lookup_symbol(lf, setsym, &sym);
17562f22b3ffSMatthew D Fleming if (error != 0)
1757f41325dbSPeter Wemm goto out;
1758f41325dbSPeter Wemm link_elf_symbol_values(lf, sym, &symval);
1759f41325dbSPeter Wemm if (symval.value == 0) {
1760f41325dbSPeter Wemm error = ESRCH;
1761f41325dbSPeter Wemm goto out;
1762f41325dbSPeter Wemm }
1763f41325dbSPeter Wemm stop = (void **)symval.value;
1764f41325dbSPeter Wemm
1765f41325dbSPeter Wemm /* and the number of entries */
1766f41325dbSPeter Wemm count = stop - start;
1767f41325dbSPeter Wemm
1768f41325dbSPeter Wemm /* and copy out */
17692f22b3ffSMatthew D Fleming if (startp != NULL)
1770f41325dbSPeter Wemm *startp = start;
17712f22b3ffSMatthew D Fleming if (stopp != NULL)
1772f41325dbSPeter Wemm *stopp = stop;
17732f22b3ffSMatthew D Fleming if (countp != NULL)
1774f41325dbSPeter Wemm *countp = count;
1775f41325dbSPeter Wemm
1776f41325dbSPeter Wemm out:
1777f41325dbSPeter Wemm free(setsym, M_LINKER);
17782f22b3ffSMatthew D Fleming return (error);
1779f41325dbSPeter Wemm }
1780bb9fe9ddSBrian Feldman
1781bb9fe9ddSBrian Feldman static int
link_elf_each_function_name(linker_file_t file,int (* callback)(const char *,void *),void * opaque)1782bb9fe9ddSBrian Feldman link_elf_each_function_name(linker_file_t file,
17832f22b3ffSMatthew D Fleming int (*callback)(const char *, void *), void *opaque)
17842f22b3ffSMatthew D Fleming {
1785bb9fe9ddSBrian Feldman elf_file_t ef = (elf_file_t)file;
1786bb9fe9ddSBrian Feldman const Elf_Sym *symp;
1787bb9fe9ddSBrian Feldman int i, error;
1788bb9fe9ddSBrian Feldman
1789bb9fe9ddSBrian Feldman /* Exhaustive search */
1790bb9fe9ddSBrian Feldman for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1791bb9fe9ddSBrian Feldman if (symp->st_value != 0 &&
17927035cf14SKonstantin Belousov (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
17937035cf14SKonstantin Belousov ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
1794bb9fe9ddSBrian Feldman error = callback(ef->ddbstrtab + symp->st_name, opaque);
17952f22b3ffSMatthew D Fleming if (error != 0)
1796bb9fe9ddSBrian Feldman return (error);
1797bb9fe9ddSBrian Feldman }
1798bb9fe9ddSBrian Feldman }
1799bb9fe9ddSBrian Feldman return (0);
1800bb9fe9ddSBrian Feldman }
180184201059SMarcel Moolenaar
1802a2024a3eSJohn Birrell static int
link_elf_each_function_nameval(linker_file_t file,linker_function_nameval_callback_t callback,void * opaque)1803a2024a3eSJohn Birrell link_elf_each_function_nameval(linker_file_t file,
1804a2024a3eSJohn Birrell linker_function_nameval_callback_t callback, void *opaque)
1805a2024a3eSJohn Birrell {
1806a2024a3eSJohn Birrell linker_symval_t symval;
1807a2024a3eSJohn Birrell elf_file_t ef = (elf_file_t)file;
1808a2024a3eSJohn Birrell const Elf_Sym *symp;
1809a2024a3eSJohn Birrell int i, error;
1810a2024a3eSJohn Birrell
1811a2024a3eSJohn Birrell /* Exhaustive search */
1812a2024a3eSJohn Birrell for (i = 0, symp = ef->ddbsymtab; i < ef->ddbsymcnt; i++, symp++) {
1813a2024a3eSJohn Birrell if (symp->st_value != 0 &&
18147035cf14SKonstantin Belousov (ELF_ST_TYPE(symp->st_info) == STT_FUNC ||
18157035cf14SKonstantin Belousov ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC)) {
181695c20fafSKonstantin Belousov error = link_elf_debug_symbol_values(file,
18172f22b3ffSMatthew D Fleming (c_linker_sym_t) symp, &symval);
181895c20fafSKonstantin Belousov if (error == 0)
1819a2024a3eSJohn Birrell error = callback(file, i, &symval, opaque);
18202f22b3ffSMatthew D Fleming if (error != 0)
1821a2024a3eSJohn Birrell return (error);
1822a2024a3eSJohn Birrell }
1823a2024a3eSJohn Birrell }
1824a2024a3eSJohn Birrell return (0);
1825a2024a3eSJohn Birrell }
1826a2024a3eSJohn Birrell
1827169d513cSJake Burkholder const Elf_Sym *
elf_get_sym(linker_file_t lf,Elf_Size symidx)1828757686b1SMarcel Moolenaar elf_get_sym(linker_file_t lf, Elf_Size symidx)
1829169d513cSJake Burkholder {
1830169d513cSJake Burkholder elf_file_t ef = (elf_file_t)lf;
1831169d513cSJake Burkholder
1832169d513cSJake Burkholder if (symidx >= ef->nchains)
1833169d513cSJake Burkholder return (NULL);
1834169d513cSJake Burkholder return (ef->symtab + symidx);
1835169d513cSJake Burkholder }
1836169d513cSJake Burkholder
183747f75012SMarcel Moolenaar const char *
elf_get_symname(linker_file_t lf,Elf_Size symidx)1838757686b1SMarcel Moolenaar elf_get_symname(linker_file_t lf, Elf_Size symidx)
183947f75012SMarcel Moolenaar {
184047f75012SMarcel Moolenaar elf_file_t ef = (elf_file_t)lf;
184147f75012SMarcel Moolenaar const Elf_Sym *sym;
184247f75012SMarcel Moolenaar
184347f75012SMarcel Moolenaar if (symidx >= ef->nchains)
184447f75012SMarcel Moolenaar return (NULL);
184547f75012SMarcel Moolenaar sym = ef->symtab + symidx;
184647f75012SMarcel Moolenaar return (ef->strtab + sym->st_name);
184747f75012SMarcel Moolenaar }
184847f75012SMarcel Moolenaar
1849d297ad16SMarcel Moolenaar /*
1850d297ad16SMarcel Moolenaar * Symbol lookup function that can be used when the symbol index is known (ie
1851d297ad16SMarcel Moolenaar * in relocations). It uses the symbol index instead of doing a fully fledged
1852d297ad16SMarcel Moolenaar * hash table based lookup when such is valid. For example for local symbols.
1853d297ad16SMarcel Moolenaar * This is not only more efficient, it's also more correct. It's not always
1854d297ad16SMarcel Moolenaar * the case that the symbol can be found through the hash table.
1855d297ad16SMarcel Moolenaar */
1856cff8c6f2SKonstantin Belousov static int
elf_lookup(linker_file_t lf,Elf_Size symidx,int deps,Elf_Addr * res)1857cff8c6f2SKonstantin Belousov elf_lookup(linker_file_t lf, Elf_Size symidx, int deps, Elf_Addr *res)
1858d297ad16SMarcel Moolenaar {
1859d297ad16SMarcel Moolenaar elf_file_t ef = (elf_file_t)lf;
1860d297ad16SMarcel Moolenaar const Elf_Sym *sym;
1861d297ad16SMarcel Moolenaar const char *symbol;
186247813f5dSMikolaj Golub Elf_Addr addr, start, base;
1863d297ad16SMarcel Moolenaar
1864d297ad16SMarcel Moolenaar /* Don't even try to lookup the symbol if the index is bogus. */
1865cff8c6f2SKonstantin Belousov if (symidx >= ef->nchains) {
1866cff8c6f2SKonstantin Belousov *res = 0;
1867cff8c6f2SKonstantin Belousov return (EINVAL);
1868cff8c6f2SKonstantin Belousov }
1869d297ad16SMarcel Moolenaar
1870d297ad16SMarcel Moolenaar sym = ef->symtab + symidx;
1871d297ad16SMarcel Moolenaar
1872d297ad16SMarcel Moolenaar /*
1873d297ad16SMarcel Moolenaar * Don't do a full lookup when the symbol is local. It may even
1874d297ad16SMarcel Moolenaar * fail because it may not be found through the hash table.
1875d297ad16SMarcel Moolenaar */
1876d297ad16SMarcel Moolenaar if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1877d297ad16SMarcel Moolenaar /* Force lookup failure when we have an insanity. */
1878cff8c6f2SKonstantin Belousov if (sym->st_shndx == SHN_UNDEF || sym->st_value == 0) {
1879cff8c6f2SKonstantin Belousov *res = 0;
1880cff8c6f2SKonstantin Belousov return (EINVAL);
1881cff8c6f2SKonstantin Belousov }
1882cff8c6f2SKonstantin Belousov *res = ((Elf_Addr)ef->address + sym->st_value);
1883d297ad16SMarcel Moolenaar return (0);
1884d297ad16SMarcel Moolenaar }
1885d297ad16SMarcel Moolenaar
1886d297ad16SMarcel Moolenaar /*
1887d297ad16SMarcel Moolenaar * XXX we can avoid doing a hash table based lookup for global
1888d297ad16SMarcel Moolenaar * symbols as well. This however is not always valid, so we'll
1889d297ad16SMarcel Moolenaar * just do it the hard way for now. Performance tweaks can
1890d297ad16SMarcel Moolenaar * always be added.
1891d297ad16SMarcel Moolenaar */
1892d297ad16SMarcel Moolenaar
1893d297ad16SMarcel Moolenaar symbol = ef->strtab + sym->st_name;
1894d297ad16SMarcel Moolenaar
1895d297ad16SMarcel Moolenaar /* Force a lookup failure if the symbol name is bogus. */
1896cff8c6f2SKonstantin Belousov if (*symbol == 0) {
1897cff8c6f2SKonstantin Belousov *res = 0;
1898cff8c6f2SKonstantin Belousov return (EINVAL);
1899cff8c6f2SKonstantin Belousov }
1900d297ad16SMarcel Moolenaar
190147813f5dSMikolaj Golub addr = ((Elf_Addr)linker_file_lookup_symbol(lf, symbol, deps));
1902e8e0fac5SMark Johnston if (addr == 0 && ELF_ST_BIND(sym->st_info) != STB_WEAK) {
1903e8e0fac5SMark Johnston *res = 0;
1904e8e0fac5SMark Johnston return (EINVAL);
1905e8e0fac5SMark Johnston }
190647813f5dSMikolaj Golub
190747813f5dSMikolaj Golub if (elf_set_find(&set_pcpu_list, addr, &start, &base))
190847813f5dSMikolaj Golub addr = addr - start + base;
190947813f5dSMikolaj Golub #ifdef VIMAGE
191047813f5dSMikolaj Golub else if (elf_set_find(&set_vnet_list, addr, &start, &base))
191147813f5dSMikolaj Golub addr = addr - start + base;
191247813f5dSMikolaj Golub #endif
1913cff8c6f2SKonstantin Belousov *res = addr;
1914cff8c6f2SKonstantin Belousov return (0);
1915d297ad16SMarcel Moolenaar }
19167251b4bfSJake Burkholder
19177251b4bfSJake Burkholder static void
link_elf_reloc_local(linker_file_t lf)19187251b4bfSJake Burkholder link_elf_reloc_local(linker_file_t lf)
19197251b4bfSJake Burkholder {
19207251b4bfSJake Burkholder const Elf_Rel *rellim;
19217251b4bfSJake Burkholder const Elf_Rel *rel;
19227251b4bfSJake Burkholder const Elf_Rela *relalim;
19237251b4bfSJake Burkholder const Elf_Rela *rela;
19247251b4bfSJake Burkholder elf_file_t ef = (elf_file_t)lf;
19257251b4bfSJake Burkholder
19267251b4bfSJake Burkholder /* Perform relocations without addend if there are any: */
19277251b4bfSJake Burkholder if ((rel = ef->rel) != NULL) {
19287251b4bfSJake Burkholder rellim = (const Elf_Rel *)((const char *)ef->rel + ef->relsize);
19297251b4bfSJake Burkholder while (rel < rellim) {
19302f22b3ffSMatthew D Fleming elf_reloc_local(lf, (Elf_Addr)ef->address, rel,
19312f22b3ffSMatthew D Fleming ELF_RELOC_REL, elf_lookup);
19327251b4bfSJake Burkholder rel++;
19337251b4bfSJake Burkholder }
19347251b4bfSJake Burkholder }
19357251b4bfSJake Burkholder
19367251b4bfSJake Burkholder /* Perform relocations with addend if there are any: */
19377251b4bfSJake Burkholder if ((rela = ef->rela) != NULL) {
19382f22b3ffSMatthew D Fleming relalim = (const Elf_Rela *)
19392f22b3ffSMatthew D Fleming ((const char *)ef->rela + ef->relasize);
19407251b4bfSJake Burkholder while (rela < relalim) {
19412f22b3ffSMatthew D Fleming elf_reloc_local(lf, (Elf_Addr)ef->address, rela,
19422f22b3ffSMatthew D Fleming ELF_RELOC_RELA, elf_lookup);
19437251b4bfSJake Burkholder rela++;
19447251b4bfSJake Burkholder }
19457251b4bfSJake Burkholder }
19467251b4bfSJake Burkholder }
194700a5db46SStacey Son
194800a5db46SStacey Son static long
link_elf_symtab_get(linker_file_t lf,const Elf_Sym ** symtab)194900a5db46SStacey Son link_elf_symtab_get(linker_file_t lf, const Elf_Sym **symtab)
195000a5db46SStacey Son {
195100a5db46SStacey Son elf_file_t ef = (elf_file_t)lf;
195200a5db46SStacey Son
195300a5db46SStacey Son *symtab = ef->ddbsymtab;
195400a5db46SStacey Son
195500a5db46SStacey Son if (*symtab == NULL)
195600a5db46SStacey Son return (0);
195700a5db46SStacey Son
195800a5db46SStacey Son return (ef->ddbsymcnt);
195900a5db46SStacey Son }
196000a5db46SStacey Son
196100a5db46SStacey Son static long
link_elf_strtab_get(linker_file_t lf,caddr_t * strtab)196200a5db46SStacey Son link_elf_strtab_get(linker_file_t lf, caddr_t *strtab)
196300a5db46SStacey Son {
196400a5db46SStacey Son elf_file_t ef = (elf_file_t)lf;
196500a5db46SStacey Son
196600a5db46SStacey Son *strtab = ef->ddbstrtab;
196700a5db46SStacey Son
196800a5db46SStacey Son if (*strtab == NULL)
196900a5db46SStacey Son return (0);
197000a5db46SStacey Son
197100a5db46SStacey Son return (ef->ddbstrcnt);
197200a5db46SStacey Son }
19737035cf14SKonstantin Belousov
1974110113bcSZhenlei Huang #ifdef VIMAGE
1975110113bcSZhenlei Huang static void
link_elf_propagate_vnets(linker_file_t lf)1976110113bcSZhenlei Huang link_elf_propagate_vnets(linker_file_t lf)
1977110113bcSZhenlei Huang {
1978110113bcSZhenlei Huang elf_file_t ef = (elf_file_t)lf;
1979110113bcSZhenlei Huang int size;
1980110113bcSZhenlei Huang
1981110113bcSZhenlei Huang if (ef->vnet_base != 0) {
1982110113bcSZhenlei Huang size = (uintptr_t)ef->vnet_stop - (uintptr_t)ef->vnet_start;
1983110113bcSZhenlei Huang vnet_data_copy((void *)ef->vnet_base, size);
1984110113bcSZhenlei Huang }
1985110113bcSZhenlei Huang }
1986110113bcSZhenlei Huang #endif
1987110113bcSZhenlei Huang
19889411e24dSBrandon Bergren #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__) || defined(__powerpc__)
198936716fe2SMark Johnston /*
199036716fe2SMark Johnston * Use this lookup routine when performing relocations early during boot.
199136716fe2SMark Johnston * The generic lookup routine depends on kobj, which is not initialized
199236716fe2SMark Johnston * at that point.
199336716fe2SMark Johnston */
199436716fe2SMark Johnston static int
elf_lookup_ifunc(linker_file_t lf,Elf_Size symidx,int deps __unused,Elf_Addr * res)199536716fe2SMark Johnston elf_lookup_ifunc(linker_file_t lf, Elf_Size symidx, int deps __unused,
199636716fe2SMark Johnston Elf_Addr *res)
199736716fe2SMark Johnston {
199836716fe2SMark Johnston elf_file_t ef;
199936716fe2SMark Johnston const Elf_Sym *symp;
200036716fe2SMark Johnston caddr_t val;
200136716fe2SMark Johnston
200236716fe2SMark Johnston ef = (elf_file_t)lf;
200336716fe2SMark Johnston symp = ef->symtab + symidx;
200436716fe2SMark Johnston if (ELF_ST_TYPE(symp->st_info) == STT_GNU_IFUNC) {
200536716fe2SMark Johnston val = (caddr_t)ef->address + symp->st_value;
200636716fe2SMark Johnston *res = ((Elf_Addr (*)(void))val)();
200736716fe2SMark Johnston return (0);
200836716fe2SMark Johnston }
200936716fe2SMark Johnston return (ENOENT);
201036716fe2SMark Johnston }
201136716fe2SMark Johnston
20127035cf14SKonstantin Belousov void
link_elf_ireloc(void)2013b72ae900SAhmad Khalifa link_elf_ireloc(void)
20147035cf14SKonstantin Belousov {
20157035cf14SKonstantin Belousov struct elf_file eff;
20167035cf14SKonstantin Belousov elf_file_t ef;
20177035cf14SKonstantin Belousov
20189d6ae1e3SColin Percival TSENTER();
20197035cf14SKonstantin Belousov ef = &eff;
202055c9d75eSKonstantin Belousov
20213f102f58SMateusz Guzik bzero_early(ef, sizeof(*ef));
202255c9d75eSKonstantin Belousov
2023b72ae900SAhmad Khalifa ef->modptr = preload_kmdp;
20247035cf14SKonstantin Belousov ef->dynamic = (Elf_Dyn *)&_DYNAMIC;
20259411e24dSBrandon Bergren
20269411e24dSBrandon Bergren #ifdef RELOCATABLE_KERNEL
20279411e24dSBrandon Bergren ef->address = (caddr_t) (__startkernel - KERNBASE);
20289411e24dSBrandon Bergren #else
20297035cf14SKonstantin Belousov ef->address = 0;
20309411e24dSBrandon Bergren #endif
20319411e24dSBrandon Bergren parse_dynamic(ef);
20329411e24dSBrandon Bergren
20337035cf14SKonstantin Belousov link_elf_preload_parse_symbols(ef);
203436716fe2SMark Johnston relocate_file1(ef, elf_lookup_ifunc, elf_reloc, true);
20359d6ae1e3SColin Percival TSEXIT();
20367035cf14SKonstantin Belousov }
2037fcf7a481SAndrew Turner
2038fcf7a481SAndrew Turner #if defined(__aarch64__) || defined(__amd64__)
2039fcf7a481SAndrew Turner void
link_elf_late_ireloc(void)2040fcf7a481SAndrew Turner link_elf_late_ireloc(void)
2041fcf7a481SAndrew Turner {
2042fcf7a481SAndrew Turner elf_file_t ef;
2043fcf7a481SAndrew Turner
2044fcf7a481SAndrew Turner KASSERT(linker_kernel_file != NULL,
2045fcf7a481SAndrew Turner ("link_elf_late_ireloc: No kernel linker file found"));
2046fcf7a481SAndrew Turner ef = (elf_file_t)linker_kernel_file;
2047fcf7a481SAndrew Turner
2048fcf7a481SAndrew Turner relocate_file1(ef, elf_lookup_ifunc, elf_reloc_late, true);
2049fcf7a481SAndrew Turner }
2050fcf7a481SAndrew Turner #endif
20517035cf14SKonstantin Belousov #endif
2052