link_elf.c (246e7a2b6494cd991b08ac669ed761ecea0cc98c) link_elf.c (0067051fe7ec2efffec5f50f52bbc5699f955d64)
1/*-
2 * Copyright (c) 1998-2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 317 unchanged lines hidden (view full) ---

326link_elf_error(const char *filename, const char *s)
327{
328 if (filename == NULL)
329 printf("kldload: %s\n", s);
330 else
331 printf("kldload: %s: %s\n", filename, s);
332}
333
1/*-
2 * Copyright (c) 1998-2000 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 317 unchanged lines hidden (view full) ---

326link_elf_error(const char *filename, const char *s)
327{
328 if (filename == NULL)
329 printf("kldload: %s\n", s);
330 else
331 printf("kldload: %s: %s\n", filename, s);
332}
333
334static void
335link_elf_invoke_ctors(caddr_t addr, size_t size)
336{
337 void (**ctor)(void);
338 size_t i, cnt;
339
340 if (addr == NULL || size == 0)
341 return;
342 cnt = size / sizeof(*ctor);
343 ctor = (void *)addr;
344 for (i = 0; i < cnt; i++) {
345 if (ctor[i] != NULL)
346 (*ctor[i])();
347 }
348}
349
334/*
335 * Actions performed after linking/loading both the preloaded kernel and any
336 * modules; whether preloaded or dynamicly loaded.
337 */
338static int
339link_elf_link_common_finish(linker_file_t lf)
340{
341#ifdef GDB

--- 13 unchanged lines hidden (view full) ---

355 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
356 strcpy(newfilename, lf->filename);
357 ef->gdb.l_name = newfilename;
358 ef->gdb.l_ld = ef->dynamic;
359 link_elf_add_gdb(&ef->gdb);
360 GDB_STATE(RT_CONSISTENT);
361#endif
362
350/*
351 * Actions performed after linking/loading both the preloaded kernel and any
352 * modules; whether preloaded or dynamicly loaded.
353 */
354static int
355link_elf_link_common_finish(linker_file_t lf)
356{
357#ifdef GDB

--- 13 unchanged lines hidden (view full) ---

371 newfilename = malloc(strlen(lf->filename) + 1, M_LINKER, M_WAITOK);
372 strcpy(newfilename, lf->filename);
373 ef->gdb.l_name = newfilename;
374 ef->gdb.l_ld = ef->dynamic;
375 link_elf_add_gdb(&ef->gdb);
376 GDB_STATE(RT_CONSISTENT);
377#endif
378
379 /* Invoke .ctors */
380 link_elf_invoke_ctors(lf->ctors_addr, lf->ctors_size);
363 return (0);
364}
365
366static void
367link_elf_init(void* arg)
368{
369 Elf_Dyn *dp;
381 return (0);
382}
383
384static void
385link_elf_init(void* arg)
386{
387 Elf_Dyn *dp;
388 Elf_Addr *ctors_addrp;
389 Elf_Size *ctors_sizep;
370 caddr_t modptr, baseptr, sizeptr;
371 elf_file_t ef;
372 char *modname;
373
374 linker_add_class(&link_elf_class);
375
376 dp = (Elf_Dyn *)&_DYNAMIC;
377 modname = NULL;

--- 25 unchanged lines hidden (view full) ---

403 if (modptr != NULL) {
404 ef->modptr = modptr;
405 baseptr = preload_search_info(modptr, MODINFO_ADDR);
406 if (baseptr != NULL)
407 linker_kernel_file->address = *(caddr_t *)baseptr;
408 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
409 if (sizeptr != NULL)
410 linker_kernel_file->size = *(size_t *)sizeptr;
390 caddr_t modptr, baseptr, sizeptr;
391 elf_file_t ef;
392 char *modname;
393
394 linker_add_class(&link_elf_class);
395
396 dp = (Elf_Dyn *)&_DYNAMIC;
397 modname = NULL;

--- 25 unchanged lines hidden (view full) ---

423 if (modptr != NULL) {
424 ef->modptr = modptr;
425 baseptr = preload_search_info(modptr, MODINFO_ADDR);
426 if (baseptr != NULL)
427 linker_kernel_file->address = *(caddr_t *)baseptr;
428 sizeptr = preload_search_info(modptr, MODINFO_SIZE);
429 if (sizeptr != NULL)
430 linker_kernel_file->size = *(size_t *)sizeptr;
431 ctors_addrp = (Elf_Addr *)preload_search_info(modptr,
432 MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
433 ctors_sizep = (Elf_Size *)preload_search_info(modptr,
434 MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
435 if (ctors_addrp != NULL && ctors_sizep != NULL) {
436 linker_kernel_file->ctors_addr = ef->address +
437 *ctors_addrp;
438 linker_kernel_file->ctors_size = *ctors_sizep;
439 }
411 }
412 (void)link_elf_preload_parse_symbols(ef);
413
414#ifdef GDB
415 r_debug.r_map = NULL;
416 r_debug.r_brk = r_debug_state;
417 r_debug.r_state = RT_CONSISTENT;
418#endif

--- 211 unchanged lines hidden (view full) ---

630 return (0);
631}
632#endif
633
634static int
635link_elf_link_preload(linker_class_t cls,
636 const char* filename, linker_file_t *result)
637{
440 }
441 (void)link_elf_preload_parse_symbols(ef);
442
443#ifdef GDB
444 r_debug.r_map = NULL;
445 r_debug.r_brk = r_debug_state;
446 r_debug.r_state = RT_CONSISTENT;
447#endif

--- 211 unchanged lines hidden (view full) ---

659 return (0);
660}
661#endif
662
663static int
664link_elf_link_preload(linker_class_t cls,
665 const char* filename, linker_file_t *result)
666{
667 Elf_Addr *ctors_addrp;
668 Elf_Size *ctors_sizep;
638 caddr_t modptr, baseptr, sizeptr, dynptr;
639 char *type;
640 elf_file_t ef;
641 linker_file_t lf;
642 int error;
643 vm_offset_t dp;
644
645 /* Look to see if we have the file preloaded */

--- 24 unchanged lines hidden (view full) ---

670#ifdef SPARSE_MAPPING
671 ef->object = 0;
672#endif
673 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
674 ef->dynamic = (Elf_Dyn *)dp;
675 lf->address = ef->address;
676 lf->size = *(size_t *)sizeptr;
677
669 caddr_t modptr, baseptr, sizeptr, dynptr;
670 char *type;
671 elf_file_t ef;
672 linker_file_t lf;
673 int error;
674 vm_offset_t dp;
675
676 /* Look to see if we have the file preloaded */

--- 24 unchanged lines hidden (view full) ---

701#ifdef SPARSE_MAPPING
702 ef->object = 0;
703#endif
704 dp = (vm_offset_t)ef->address + *(vm_offset_t *)dynptr;
705 ef->dynamic = (Elf_Dyn *)dp;
706 lf->address = ef->address;
707 lf->size = *(size_t *)sizeptr;
708
709 ctors_addrp = (Elf_Addr *)preload_search_info(modptr,
710 MODINFO_METADATA | MODINFOMD_CTORS_ADDR);
711 ctors_sizep = (Elf_Size *)preload_search_info(modptr,
712 MODINFO_METADATA | MODINFOMD_CTORS_SIZE);
713 if (ctors_addrp != NULL && ctors_sizep != NULL) {
714 lf->ctors_addr = ef->address + *ctors_addrp;
715 lf->ctors_size = *ctors_sizep;
716 }
717
678 error = parse_dynamic(ef);
679 if (error == 0)
680 error = parse_dpcpu(ef);
681#ifdef VIMAGE
682 if (error == 0)
683 error = parse_vnet(ef);
684#endif
685 if (error != 0) {

--- 43 unchanged lines hidden (view full) ---

729 int error = 0;
730 ssize_t resid;
731 int flags;
732 elf_file_t ef;
733 linker_file_t lf;
734 Elf_Shdr *shdr;
735 int symtabindex;
736 int symstrindex;
718 error = parse_dynamic(ef);
719 if (error == 0)
720 error = parse_dpcpu(ef);
721#ifdef VIMAGE
722 if (error == 0)
723 error = parse_vnet(ef);
724#endif
725 if (error != 0) {

--- 43 unchanged lines hidden (view full) ---

769 int error = 0;
770 ssize_t resid;
771 int flags;
772 elf_file_t ef;
773 linker_file_t lf;
774 Elf_Shdr *shdr;
775 int symtabindex;
776 int symstrindex;
777 int shstrindex;
737 int symcnt;
738 int strcnt;
778 int symcnt;
779 int strcnt;
780 char *shstrs;
739
740 shdr = NULL;
741 lf = NULL;
781
782 shdr = NULL;
783 lf = NULL;
784 shstrs = NULL;
742
743 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
744 flags = FREAD;
745 error = vn_open(&nd, &flags, 0, NULL);
746 if (error != 0)
747 return (error);
748 NDFREE(&nd, NDF_ONLY_PNBUF);
749 if (nd.ni_vp->v_type != VREG) {

--- 222 unchanged lines hidden (view full) ---

972 goto nosyms;
973 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
974 error = vn_rdwr(UIO_READ, nd.ni_vp,
975 (caddr_t)shdr, nbytes, hdr->e_shoff,
976 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
977 &resid, td);
978 if (error != 0)
979 goto out;
785
786 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, td);
787 flags = FREAD;
788 error = vn_open(&nd, &flags, 0, NULL);
789 if (error != 0)
790 return (error);
791 NDFREE(&nd, NDF_ONLY_PNBUF);
792 if (nd.ni_vp->v_type != VREG) {

--- 222 unchanged lines hidden (view full) ---

1015 goto nosyms;
1016 shdr = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
1017 error = vn_rdwr(UIO_READ, nd.ni_vp,
1018 (caddr_t)shdr, nbytes, hdr->e_shoff,
1019 UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, NOCRED,
1020 &resid, td);
1021 if (error != 0)
1022 goto out;
1023
1024 /* Read section string table */
1025 shstrindex = hdr->e_shstrndx;
1026 if (shstrindex != 0 && shdr[shstrindex].sh_type == SHT_STRTAB &&
1027 shdr[shstrindex].sh_size != 0) {
1028 nbytes = shdr[shstrindex].sh_size;
1029 shstrs = malloc(nbytes, M_LINKER, M_WAITOK | M_ZERO);
1030 error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)shstrs, nbytes,
1031 shdr[shstrindex].sh_offset, UIO_SYSSPACE, IO_NODELOCKED,
1032 td->td_ucred, NOCRED, &resid, td);
1033 if (error)
1034 goto out;
1035 }
1036
980 symtabindex = -1;
981 symstrindex = -1;
982 for (i = 0; i < hdr->e_shnum; i++) {
983 if (shdr[i].sh_type == SHT_SYMTAB) {
984 symtabindex = i;
985 symstrindex = shdr[i].sh_link;
1037 symtabindex = -1;
1038 symstrindex = -1;
1039 for (i = 0; i < hdr->e_shnum; i++) {
1040 if (shdr[i].sh_type == SHT_SYMTAB) {
1041 symtabindex = i;
1042 symstrindex = shdr[i].sh_link;
1043 } else if (shstrs != NULL && shdr[i].sh_name != 0 &&
1044 strcmp(shstrs + shdr[i].sh_name, ".ctors") == 0) {
1045 /* Record relocated address and size of .ctors. */
1046 lf->ctors_addr = mapbase + shdr[i].sh_addr - base_vaddr;
1047 lf->ctors_size = shdr[i].sh_size;
986 }
987 }
988 if (symtabindex < 0 || symstrindex < 0)
989 goto nosyms;
990
991 symcnt = shdr[symtabindex].sh_size;
992 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
993 strcnt = shdr[symstrindex].sh_size;

--- 28 unchanged lines hidden (view full) ---

1022 VOP_UNLOCK(nd.ni_vp, 0);
1023 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1024 if (error != 0 && lf != NULL)
1025 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1026 if (shdr != NULL)
1027 free(shdr, M_LINKER);
1028 if (firstpage != NULL)
1029 free(firstpage, M_LINKER);
1048 }
1049 }
1050 if (symtabindex < 0 || symstrindex < 0)
1051 goto nosyms;
1052
1053 symcnt = shdr[symtabindex].sh_size;
1054 ef->symbase = malloc(symcnt, M_LINKER, M_WAITOK);
1055 strcnt = shdr[symstrindex].sh_size;

--- 28 unchanged lines hidden (view full) ---

1084 VOP_UNLOCK(nd.ni_vp, 0);
1085 vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
1086 if (error != 0 && lf != NULL)
1087 linker_file_unload(lf, LINKER_UNLOAD_FORCE);
1088 if (shdr != NULL)
1089 free(shdr, M_LINKER);
1090 if (firstpage != NULL)
1091 free(firstpage, M_LINKER);
1092 if (shstrs != NULL)
1093 free(shstrs, M_LINKER);
1030
1031 return (error);
1032}
1033
1034Elf_Addr
1035elf_relocaddr(linker_file_t lf, Elf_Addr x)
1036{
1037 elf_file_t ef;

--- 549 unchanged lines hidden ---
1094
1095 return (error);
1096}
1097
1098Elf_Addr
1099elf_relocaddr(linker_file_t lf, Elf_Addr x)
1100{
1101 elf_file_t ef;

--- 549 unchanged lines hidden ---