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