aboutsummaryrefslogtreecommitdiff
path: root/stand/common/load_elf.c
blob: 8877e5f8b7e0e2786298d8d1eba22ab2cfad3005 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
/*-
 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/endian.h>
#include <sys/exec.h>
#include <sys/linker.h>
#include <sys/module.h>
#include <sys/stdint.h>
#include <string.h>
#include <machine/elf.h>
#include <stand.h>
#include <sys/link_elf.h>

#include "bootstrap.h"

#define COPYOUT(s,d,l)	archsw.arch_copyout((vm_offset_t)(s), d, l)

#if defined(__i386__) && __ELF_WORD_SIZE == 64
#undef ELF_TARG_CLASS
#undef ELF_TARG_MACH
#define ELF_TARG_CLASS  ELFCLASS64
#define ELF_TARG_MACH   EM_X86_64
#endif

typedef struct elf_file {
	Elf_Phdr	*ph;
	Elf_Ehdr	*ehdr;
	Elf_Sym		*symtab;
	Elf_Hashelt	*hashtab;
	Elf_Hashelt	nbuckets;
	Elf_Hashelt	nchains;
	Elf_Hashelt	*buckets;
	Elf_Hashelt	*chains;
	Elf_Rel	*rel;
	size_t	relsz;
	Elf_Rela	*rela;
	size_t	relasz;
	char	*strtab;
	size_t	strsz;
	int		fd;
	caddr_t	firstpage;
	size_t	firstlen;
	int		kernel;
	uint64_t	off;
#ifdef LOADER_VERIEXEC_VECTX
	struct vectx	*vctx;
#endif
} *elf_file_t;

#ifdef LOADER_VERIEXEC_VECTX
#define VECTX_HANDLE(ef) (ef)->vctx
#else
#define VECTX_HANDLE(ef) (ef)->fd
#endif

static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef,
    uint64_t loadaddr);
static int __elfN(lookup_symbol)(elf_file_t ef, const char* name,
    Elf_Sym *sym, unsigned char type);
static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
    Elf_Addr p, void *val, size_t len);
static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
    Elf_Addr p_start, Elf_Addr p_end);
static symaddr_fn __elfN(symaddr);
static char	*fake_modname(const char *name);

const char	*__elfN(kerneltype) = "elf kernel";
const char	*__elfN(moduletype) = "elf module";

uint64_t	__elfN(relocation_offset) = 0;

#ifdef __powerpc__
extern void elf_wrong_field_size(void);
#define CONVERT_FIELD(b, f, e)			\
	switch (sizeof((b)->f)) {		\
	case 2:					\
		(b)->f = e ## 16toh((b)->f);	\
		break;				\
	case 4:					\
		(b)->f = e ## 32toh((b)->f);	\
		break;				\
	case 8:					\
		(b)->f = e ## 64toh((b)->f);	\
		break;				\
	default:				\
		/* Force a link time error. */	\
		elf_wrong_field_size();		\
		break;				\
	}

#define CONVERT_SWITCH(h, d, f)			\
	switch ((h)->e_ident[EI_DATA]) {	\
	case ELFDATA2MSB:			\
		f(d, be);			\
		break;				\
	case ELFDATA2LSB:			\
		f(d, le);			\
		break;				\
	default:				\
		return (EINVAL);		\
	}


static int elf_header_convert(Elf_Ehdr *ehdr)
{
	/*
	 * Fixup ELF header endianness.
	 *
	 * The Xhdr structure was loaded using block read call to optimize file
	 * accesses. It might happen, that the endianness of the system memory
	 * is different that endianness of the ELF header.  Swap fields here to
	 * guarantee that Xhdr always contain valid data regardless of
	 * architecture.
	 */
#define HEADER_FIELDS(b, e)			\
	CONVERT_FIELD(b, e_type, e);		\
	CONVERT_FIELD(b, e_machine, e);		\
	CONVERT_FIELD(b, e_version, e);		\
	CONVERT_FIELD(b, e_entry, e);		\
	CONVERT_FIELD(b, e_phoff, e);		\
	CONVERT_FIELD(b, e_shoff, e);		\
	CONVERT_FIELD(b, e_flags, e);		\
	CONVERT_FIELD(b, e_ehsize, e);		\
	CONVERT_FIELD(b, e_phentsize, e);	\
	CONVERT_FIELD(b, e_phnum, e);		\
	CONVERT_FIELD(b, e_shentsize, e);	\
	CONVERT_FIELD(b, e_shnum, e);		\
	CONVERT_FIELD(b, e_shstrndx, e)

	CONVERT_SWITCH(ehdr, ehdr, HEADER_FIELDS);

#undef HEADER_FIELDS

	return (0);
}

static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
{
#define PROGRAM_HEADER_FIELDS(b, e)		\
	CONVERT_FIELD(b, p_type, e);		\
	CONVERT_FIELD(b, p_flags, e);		\
	CONVERT_FIELD(b, p_offset, e);		\
	CONVERT_FIELD(b, p_vaddr, e);		\
	CONVERT_FIELD(b, p_paddr, e);		\
	CONVERT_FIELD(b, p_filesz, e);		\
	CONVERT_FIELD(b, p_memsz, e);		\
	CONVERT_FIELD(b, p_align, e)

	CONVERT_SWITCH(ehdr, phdr, PROGRAM_HEADER_FIELDS);

#undef PROGRAM_HEADER_FIELDS

	return (0);
}

static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
{
#define SECTION_HEADER_FIELDS(b, e)		\
	CONVERT_FIELD(b, sh_name, e);		\
	CONVERT_FIELD(b, sh_type, e);		\
	CONVERT_FIELD(b, sh_link, e);		\
	CONVERT_FIELD(b, sh_info, e);		\
	CONVERT_FIELD(b, sh_flags, e);		\
	CONVERT_FIELD(b, sh_addr, e);		\
	CONVERT_FIELD(b, sh_offset, e);		\
	CONVERT_FIELD(b, sh_size, e);		\
	CONVERT_FIELD(b, sh_addralign, e);	\
	CONVERT_FIELD(b, sh_entsize, e)

	CONVERT_SWITCH(ehdr, shdr, SECTION_HEADER_FIELDS);

#undef SECTION_HEADER_FIELDS

	return (0);
}
#undef CONVERT_SWITCH
#undef CONVERT_FIELD
#else
static int elf_header_convert(Elf_Ehdr *ehdr)
{
	return (0);
}

static int elf_program_header_convert(const Elf_Ehdr *ehdr, Elf_Phdr *phdr)
{
	return (0);
}

static int elf_section_header_convert(const Elf_Ehdr *ehdr, Elf_Shdr *shdr)
{
	return (0);
}
#endif

#ifdef __amd64__
static bool
is_kernphys_relocatable(elf_file_t ef)
{
	Elf_Sym sym;

	return (__elfN(lookup_symbol)(ef, "kernphys", &sym, STT_OBJECT) == 0);
}
#endif

#ifdef __i386__
static bool
is_tg_kernel_support(struct preloaded_file *fp, elf_file_t ef)
{
	Elf_Sym		sym;
	Elf_Addr	p_start, p_end, v, p;
	char		vd_name[16];
	int		error;

	if (__elfN(lookup_symbol)(ef, "__start_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
		return (false);
	p_start = sym.st_value + ef->off;
	if (__elfN(lookup_symbol)(ef, "__stop_set_vt_drv_set", &sym, STT_NOTYPE) != 0)
		return (false);
	p_end = sym.st_value + ef->off;

	/*
	 * Walk through vt_drv_set, each vt driver structure starts with
	 * static 16 chars for driver name. If we have "vbefb", return true.
	 */
	for (p = p_start; p < p_end; p += sizeof(Elf_Addr)) {
		COPYOUT(p, &v, sizeof(v));

		error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
		if (error == EOPNOTSUPP)
			v += ef->off;
		else if (error != 0)
			return (false);
		COPYOUT(v, &vd_name, sizeof(vd_name));
		if (strncmp(vd_name, "vbefb", sizeof(vd_name)) == 0)
			return (true);
	}

	return (false);
}
#endif

static int
__elfN(load_elf_header)(char *filename, elf_file_t ef)
{
	ssize_t			 bytes_read;
	Elf_Ehdr		*ehdr;
	int			 err;

	/*
	 * Open the image, read and validate the ELF header
	 */
	if (filename == NULL)	/* can't handle nameless */
		return (EFTYPE);
	if ((ef->fd = open(filename, O_RDONLY)) == -1)
		return (errno);
	ef->firstpage = malloc(PAGE_SIZE);
	if (ef->firstpage == NULL) {
		close(ef->fd);
		return (ENOMEM);
	}
	preload(ef->fd);
#ifdef LOADER_VERIEXEC_VECTX
	{
		int verror;

		ef->vctx = vectx_open(ef->fd, filename, 0L, NULL, &verror, __func__);
		if (verror) {
			printf("Unverified %s: %s\n", filename, ve_error_get());
			close(ef->fd);
			free(ef->vctx);
			return (EAUTH);
		}
	}
#endif
	bytes_read = VECTX_READ(VECTX_HANDLE(ef), ef->firstpage, PAGE_SIZE);
	ef->firstlen = (size_t)bytes_read;
	if (bytes_read < 0 || ef->firstlen <= sizeof(Elf_Ehdr)) {
		err = EFTYPE; /* could be EIO, but may be small file */
		goto error;
	}
	ehdr = ef->ehdr = (Elf_Ehdr *)ef->firstpage;

	/* Is it ELF? */
	if (!IS_ELF(*ehdr)) {
		err = EFTYPE;
		goto error;
	}

	if (ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */
	    ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
	    ehdr->e_ident[EI_VERSION] != EV_CURRENT) /* Version ? */ {
		err = EFTYPE;
		goto error;
	}

	err = elf_header_convert(ehdr);
	if (err)
		goto error;

	if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) {
		/* Machine ? */
		err = EFTYPE;
		goto error;
	}

#if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX)
	if (verify_file(ef->fd, filename, bytes_read, VE_MUST, __func__) < 0) {
		err = EAUTH;
		goto error;
	}
#endif
	return (0);

error:
	if (ef->firstpage != NULL) {
		free(ef->firstpage);
		ef->firstpage = NULL;
	}
	if (ef->fd != -1) {
#ifdef LOADER_VERIEXEC_VECTX
		free(ef->vctx);
#endif
		close(ef->fd);
		ef->fd = -1;
	}
	return (err);
}

/*
 * Attempt to load the file (file) as an ELF module.  It will be stored at
 * (dest), and a pointer to a module structure describing the loaded object
 * will be saved in (result).
 */
int
__elfN(loadfile)(char *filename, uint64_t dest, struct preloaded_file **result)
{
	return (__elfN(loadfile_raw)(filename, dest, result, 0));
}

int
__elfN(loadfile_raw)(char *filename, uint64_t dest,
    struct preloaded_file **result, int multiboot)
{
	struct preloaded_file	*fp, *kfp;
	struct elf_file		ef;
	Elf_Ehdr		*ehdr;
	int			err;

	fp = NULL;
	bzero(&ef, sizeof(struct elf_file));
	ef.fd = -1;

	err = __elfN(load_elf_header)(filename, &ef);
	if (err != 0)
		return (err);

	ehdr = ef.ehdr;

	/*
	 * Check to see what sort of module we are.
	 */
	kfp = file_findfile(NULL, __elfN(kerneltype));
#ifdef __powerpc__
	/*
	 * Kernels can be ET_DYN, so just assume the first loaded object is the
	 * kernel. This assumption will be checked later.
	 */
	if (kfp == NULL)
		ef.kernel = 1;
#endif
	if (ef.kernel || ehdr->e_type == ET_EXEC) {
		/* Looks like a kernel */
		if (kfp != NULL) {
			printf("elf" __XSTRING(__ELF_WORD_SIZE)
			    "_loadfile: kernel already loaded\n");
			err = EPERM;
			goto oerr;
		}
		/*
		 * Calculate destination address based on kernel entrypoint.
		 *
		 * For ARM, the destination address is independent of any values
		 * in the elf header (an ARM kernel can be loaded at any 2MB
		 * boundary), so we leave dest set to the value calculated by
		 * archsw.arch_loadaddr() and passed in to this function.
		 */
#ifndef __arm__
		if (ehdr->e_type == ET_EXEC)
			dest = (ehdr->e_entry & ~PAGE_MASK);
#endif
		if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
			printf("elf" __XSTRING(__ELF_WORD_SIZE)
			    "_loadfile: not a kernel (maybe static binary?)\n");
			err = EPERM;
			goto oerr;
		}
		ef.kernel = 1;

	} else if (ehdr->e_type == ET_DYN) {
		/* Looks like a kld module */
		if (multiboot != 0) {
			printf("elf" __XSTRING(__ELF_WORD_SIZE)
			    "_loadfile: can't load module as multiboot\n");
			err = EPERM;
			goto oerr;
		}
		if (kfp == NULL) {
			printf("elf" __XSTRING(__ELF_WORD_SIZE)
			    "_loadfile: can't load module before kernel\n");
			err = EPERM;
			goto oerr;
		}
		if (strcmp(__elfN(kerneltype), kfp->f_type)) {
			printf("elf" __XSTRING(__ELF_WORD_SIZE)
			 "_loadfile: can't load module with kernel type '%s'\n",
			    kfp->f_type);
			err = EPERM;
			goto oerr;
		}
		/* Looks OK, got ahead */
		ef.kernel = 0;
	
	} else {
		err = EFTYPE;
		goto oerr;
	}

	if (archsw.arch_loadaddr != NULL)
		dest = archsw.arch_loadaddr(LOAD_ELF, ehdr, dest);
	else
		dest = roundup(dest, PAGE_SIZE);

	/*
	 * Ok, we think we should handle this.
	 */
	fp = file_alloc();
	if (fp == NULL) {
		printf("elf" __XSTRING(__ELF_WORD_SIZE)
		    "_loadfile: cannot allocate module info\n");
		err = EPERM;
		goto out;
	}
	if (ef.kernel == 1 && multiboot == 0)
		setenv("kernelname", filename, 1);
	fp->f_name = strdup(filename);
	if (multiboot == 0)
		fp->f_type = strdup(ef.kernel ?
		    __elfN(kerneltype) : __elfN(moduletype));
	else
		fp->f_type = strdup("elf multiboot kernel");

	if (module_verbose >= MODULE_VERBOSE_FULL) {
		if (ef.kernel)
			printf("%s entry at 0x%jx\n", filename,
			    (uintmax_t)ehdr->e_entry);
	} else if (module_verbose > MODULE_VERBOSE_SILENT)
		printf("%s ", filename);

	fp->f_size = __elfN(loadimage)(fp, &ef, dest);
	if (fp->f_size == 0 || fp->f_addr == 0)
		goto ioerr;

	/* save exec header as metadata */
	file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*ehdr), ehdr);

	/* Load OK, return module pointer */
	*result = (struct preloaded_file *)fp;
	err = 0;
#ifdef __amd64__
	fp->f_kernphys_relocatable = multiboot || is_kernphys_relocatable(&ef);
#endif
#ifdef __i386__
	fp->f_tg_kernel_support = is_tg_kernel_support(fp, &ef);
#endif
	goto out;

ioerr:
	err = EIO;
oerr:
	file_discard(fp);
out:
	if (ef.firstpage)
		free(ef.firstpage);
	if (ef.fd != -1) {
#ifdef LOADER_VERIEXEC_VECTX
		if (!err && ef.vctx) {
			int verror;

			verror = vectx_close(ef.vctx, VE_MUST, __func__);
			if (verror) {
				err = EAUTH;
				file_discard(fp);
			}
		}
#endif
		close(ef.fd);
	}
	return (err);
}

/*
 * With the file (fd) open on the image, and (ehdr) containing
 * the Elf header, load the image at (off)
 */
static int
__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off)
{
	int		i;
	u_int		j;
	Elf_Ehdr	*ehdr;
	Elf_Phdr	*phdr, *php;
	Elf_Shdr	*shdr;
	char		*shstr;
	int		ret;
	vm_offset_t	firstaddr;
	vm_offset_t	lastaddr;
	size_t		chunk;
	ssize_t		result;
	Elf_Addr	ssym, esym;
	Elf_Dyn		*dp;
	Elf_Addr	adp;
	Elf_Addr	ctors;
	int		ndp;
	int		symstrindex;
	int		symtabindex;
	Elf_Size	size;
	u_int		fpcopy;
	Elf_Sym		sym;
	Elf_Addr	p_start, p_end;

	dp = NULL;
	shdr = NULL;
	ret = 0;
	firstaddr = lastaddr = 0;
	ehdr = ef->ehdr;
#ifdef __powerpc__
	if (ef->kernel) {
#else
	if (ehdr->e_type == ET_EXEC) {
#endif
#if defined(__i386__) || defined(__amd64__)
#if __ELF_WORD_SIZE == 64
		/* x86_64 relocates after locore */
		off = - (off & 0xffffffffff000000ull);
#else
		/* i386 relocates after locore */
		off = - (off & 0xff000000u);
#endif
#elif defined(__powerpc__)
		/*
		 * On the purely virtual memory machines like e500, the kernel
		 * is linked against its final VA range, which is most often
		 * not available at the loader stage, but only after kernel
		 * initializes and completes its VM settings. In such cases we
		 * cannot use p_vaddr field directly to load ELF segments, but
		 * put them at some 'load-time' locations.
		 */
		if (off & 0xf0000000u) {
			off = -(off & 0xf0000000u);
			/*
			 * XXX the physical load address should not be
			 * hardcoded. Note that the Book-E kernel assumes that
			 * it's loaded at a 16MB boundary for now...
			 */
			off += 0x01000000;
		}
		ehdr->e_entry += off;
		if (module_verbose >= MODULE_VERBOSE_FULL)
			printf("Converted entry 0x%jx\n",
			    (uintmax_t)ehdr->e_entry);

#elif defined(__arm__) && !defined(EFI)
		/*
		 * The elf headers in arm kernels specify virtual addresses in
		 * all header fields, even the ones that should be physical
		 * addresses.  We assume the entry point is in the first page,
		 * and masking the page offset will leave us with the virtual
		 * address the kernel was linked at.  We subtract that from the
		 * load offset, making 'off' into the value which, when added
		 * to a virtual address in an elf header, translates it to a
		 * physical address.  We do the va->pa conversion on the entry
		 * point address in the header now, so that later we can launch
		 * the kernel by just jumping to that address.
		 *
		 * When booting from UEFI the copyin and copyout functions
		 * handle adjusting the location relative to the first virtual
		 * address.  Because of this there is no need to adjust the
		 * offset or entry point address as these will both be handled
		 * by the efi code.
		 */
		off -= ehdr->e_entry & ~PAGE_MASK;
		ehdr->e_entry += off;
		if (module_verbose >= MODULE_VERBOSE_FULL)
			printf("ehdr->e_entry 0x%jx, va<->pa off %llx\n",
			    (uintmax_t)ehdr->e_entry, off);
#else
		off = 0;	/* other archs use direct mapped kernels */
#endif
	}
	ef->off = off;

	if (ef->kernel)
		__elfN(relocation_offset) = off;

	if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
		printf("elf" __XSTRING(__ELF_WORD_SIZE)
		    "_loadimage: program header not within first page\n");
		goto out;
	}
	phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);

	for (i = 0; i < ehdr->e_phnum; i++) {
		if (elf_program_header_convert(ehdr, phdr))
			continue;

		/* We want to load PT_LOAD segments only.. */
		if (phdr[i].p_type != PT_LOAD)
			continue;

		if (module_verbose >= MODULE_VERBOSE_FULL) {
			printf("Segment: 0x%lx@0x%lx -> 0x%lx-0x%lx",
			    (long)phdr[i].p_filesz, (long)phdr[i].p_offset,
			    (long)(phdr[i].p_vaddr + off),
			    (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz - 1));
		} else if (module_verbose > MODULE_VERBOSE_SILENT) {
			if ((phdr[i].p_flags & PF_W) == 0) {
				printf("text=0x%lx ", (long)phdr[i].p_filesz);
			} else {
				printf("data=0x%lx", (long)phdr[i].p_filesz);
				if (phdr[i].p_filesz < phdr[i].p_memsz)
					printf("+0x%lx", (long)(phdr[i].p_memsz -
						phdr[i].p_filesz));
				printf(" ");
			}
		}
		fpcopy = 0;
		if (ef->firstlen > phdr[i].p_offset) {
			fpcopy = ef->firstlen - phdr[i].p_offset;
			archsw.arch_copyin(ef->firstpage + phdr[i].p_offset,
			    phdr[i].p_vaddr + off, fpcopy);
		}
		if (phdr[i].p_filesz > fpcopy) {
			if (kern_pread(VECTX_HANDLE(ef),
			    phdr[i].p_vaddr + off + fpcopy,
			    phdr[i].p_filesz - fpcopy,
			    phdr[i].p_offset + fpcopy) != 0) {
				printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
				    "_loadimage: read failed\n");
				goto out;
			}
		}
		/* clear space from oversized segments; eg: bss */
		if (phdr[i].p_filesz < phdr[i].p_memsz) {
			if (module_verbose >= MODULE_VERBOSE_FULL) {
				printf(" (bss: 0x%lx-0x%lx)",
				    (long)(phdr[i].p_vaddr + off + phdr[i].p_filesz),
				    (long)(phdr[i].p_vaddr + off + phdr[i].p_memsz -1));
			}	
			kern_bzero(phdr[i].p_vaddr + off + phdr[i].p_filesz,
			    phdr[i].p_memsz - phdr[i].p_filesz);
		}
		if (module_verbose >= MODULE_VERBOSE_FULL)
			printf("\n");

		if (archsw.arch_loadseg != NULL)
			archsw.arch_loadseg(ehdr, phdr + i, off);

		if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
			firstaddr = phdr[i].p_vaddr + off;
		if (lastaddr == 0 || lastaddr <
		    (phdr[i].p_vaddr + off + phdr[i].p_memsz))
			lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
	}
	lastaddr = roundup(lastaddr, sizeof(long));

	/*
	 * Get the section headers.  We need this for finding the .ctors
	 * section as well as for loading any symbols.  Both may be hard
	 * to do if reading from a .gz file as it involves seeking.  I
	 * think the rule is going to have to be that you must strip a
	 * file to remove symbols before gzipping it.
	 */
	chunk = (size_t)ehdr->e_shnum * (size_t)ehdr->e_shentsize;
	if (chunk == 0 || ehdr->e_shoff == 0)
		goto nosyms;
	shdr = alloc_pread(VECTX_HANDLE(ef), ehdr->e_shoff, chunk);
	if (shdr == NULL) {
		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
		    "_loadimage: failed to read section headers");
		goto nosyms;
	}

	for (i = 0; i < ehdr->e_shnum; i++)
		elf_section_header_convert(ehdr, &shdr[i]);

	file_addmetadata(fp, MODINFOMD_SHDR, chunk, shdr);

	/*
	 * Read the section string table and look for the .ctors section.
	 * We need to tell the kernel where it is so that it can call the
	 * ctors.
	 */
	chunk = shdr[ehdr->e_shstrndx].sh_size;
	if (chunk) {
		shstr = alloc_pread(VECTX_HANDLE(ef),
		    shdr[ehdr->e_shstrndx].sh_offset, chunk);
		if (shstr) {
			for (i = 0; i < ehdr->e_shnum; i++) {
				if (strcmp(shstr + shdr[i].sh_name,
				    ".ctors") != 0)
					continue;
				ctors = shdr[i].sh_addr;
				file_addmetadata(fp, MODINFOMD_CTORS_ADDR,
				    sizeof(ctors), &ctors);
				size = shdr[i].sh_size;
				file_addmetadata(fp, MODINFOMD_CTORS_SIZE,
				    sizeof(size), &size);
				break;
			}
			free(shstr);
		}
	}

	/*
	 * Now load any symbols.
	 */
	symtabindex = -1;
	symstrindex = -1;
	for (i = 0; i < ehdr->e_shnum; i++) {
		if (shdr[i].sh_type != SHT_SYMTAB)
			continue;
		for (j = 0; j < ehdr->e_phnum; j++) {
			if (phdr[j].p_type != PT_LOAD)
				continue;
			if (shdr[i].sh_offset >= phdr[j].p_offset &&
			    (shdr[i].sh_offset + shdr[i].sh_size <=
			    phdr[j].p_offset + phdr[j].p_filesz)) {
				shdr[i].sh_offset = 0;
				shdr[i].sh_size = 0;
				break;
			}
		}
		if (shdr[i].sh_offset == 0 || shdr[i].sh_size == 0)
			continue;	/* alread loaded in a PT_LOAD above */
		/* Save it for loading below */
		symtabindex = i;
		symstrindex = shdr[i].sh_link;
	}
	if (symtabindex < 0 || symstrindex < 0)
		goto nosyms;

	/* Ok, committed to a load. */
	if (module_verbose >= MODULE_VERBOSE_FULL)
		printf("syms=[");
	ssym = lastaddr;
	for (i = symtabindex; i >= 0; i = symstrindex) {
		char	*secname;

		switch(shdr[i].sh_type) {
		case SHT_SYMTAB:		/* Symbol table */
			secname = "symtab";
			break;
		case SHT_STRTAB:		/* String table */
			secname = "strtab";
			break;
		default:
			secname = "WHOA!!";
			break;
		}
		size = shdr[i].sh_size;

		archsw.arch_copyin(&size, lastaddr, sizeof(size));
		lastaddr += sizeof(size);

		if (module_verbose >= MODULE_VERBOSE_FULL) {
			printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
			    (uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
			    (uintmax_t)lastaddr,
			    (uintmax_t)(lastaddr + shdr[i].sh_size));
		} else if (module_verbose > MODULE_VERBOSE_SILENT) {
			if (i == symstrindex)
				printf("+");
			printf("0x%lx+0x%lx", (long)sizeof(size), (long)size);
		}
		if (VECTX_LSEEK(VECTX_HANDLE(ef), (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
			printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
			   "_loadimage: could not seek for symbols - skipped!");
			lastaddr = ssym;
			ssym = 0;
			goto nosyms;
		}
		result = archsw.arch_readin(VECTX_HANDLE(ef), lastaddr, shdr[i].sh_size);
		if (result < 0 || (size_t)result != shdr[i].sh_size) {
			printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
			    "_loadimage: could not read symbols - skipped! "
			    "(%ju != %ju)", (uintmax_t)result,
			    (uintmax_t)shdr[i].sh_size);
			lastaddr = ssym;
			ssym = 0;
			goto nosyms;
		}
		/* Reset offsets relative to ssym */
		lastaddr += shdr[i].sh_size;
		lastaddr = roundup(lastaddr, sizeof(size));
		if (i == symtabindex)
			symtabindex = -1;
		else if (i == symstrindex)
			symstrindex = -1;
	}
	esym = lastaddr;
	if (module_verbose >= MODULE_VERBOSE_FULL)
		printf("]");

	file_addmetadata(fp, MODINFOMD_SSYM, sizeof(ssym), &ssym);
	file_addmetadata(fp, MODINFOMD_ESYM, sizeof(esym), &esym);

nosyms:
	if (module_verbose > MODULE_VERBOSE_SILENT)
		printf("\n");

	ret = lastaddr - firstaddr;
	fp->f_addr = firstaddr;

	php = NULL;
	for (i = 0; i < ehdr->e_phnum; i++) {
		if (phdr[i].p_type == PT_DYNAMIC) {
			php = phdr + i;
			adp = php->p_vaddr;
			file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp),
			    &adp);
			break;
		}
	}

	if (php == NULL) /* this is bad, we cannot get to symbols or _DYNAMIC */
		goto out;

	ndp = php->p_filesz / sizeof(Elf_Dyn);
	if (ndp == 0)
		goto out;
	dp = malloc(php->p_filesz);
	if (dp == NULL)
		goto out;
	archsw.arch_copyout(php->p_vaddr + off, dp, php->p_filesz);

	ef->strsz = 0;
	for (i = 0; i < ndp; i++) {
		if (dp[i].d_tag == 0)
			break;
		switch (dp[i].d_tag) {
		case DT_HASH:
			ef->hashtab =
			    (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
			break;
		case DT_STRTAB:
			ef->strtab =
			    (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
			break;
		case DT_STRSZ:
			ef->strsz = dp[i].d_un.d_val;
			break;
		case DT_SYMTAB:
			ef->symtab =
			    (Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off);
			break;
		case DT_REL:
			ef->rel =
			    (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
			break;
		case DT_RELSZ:
			ef->relsz = dp[i].d_un.d_val;
			break;
		case DT_RELA:
			ef->rela =
			    (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
			break;
		case DT_RELASZ:
			ef->relasz = dp[i].d_un.d_val;
			break;
		default:
			break;
		}
	}
	if (ef->hashtab == NULL || ef->symtab == NULL ||
	    ef->strtab == NULL || ef->strsz == 0)
		goto out;
	COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
	COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
	ef->buckets = ef->hashtab + 2;
	ef->chains = ef->buckets + ef->nbuckets;

	if (__elfN(lookup_symbol)(ef, "__start_set_modmetadata_set", &sym,
	    STT_NOTYPE) != 0)
		return 0;
	p_start = sym.st_value + ef->off;
	if (__elfN(lookup_symbol)(ef, "__stop_set_modmetadata_set", &sym,
	    STT_NOTYPE) != 0)
		return 0;
	p_end = sym.st_value + ef->off;

	if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
		goto out;

	if (ef->kernel)		/* kernel must not depend on anything */
		goto out;

out:
	if (dp)
		free(dp);
	if (shdr)
		free(shdr);
	return ret;
}

static char invalid_name[] = "bad";

char *
fake_modname(const char *name)
{
	const char *sp, *ep;
	char *fp;
	size_t len;

	sp = strrchr(name, '/');
	if (sp)
		sp++;
	else
		sp = name;

	ep = strrchr(sp, '.');
	if (ep == NULL) {
		ep = sp + strlen(sp);
	}
	if (ep == sp) {
		sp = invalid_name;
		ep = invalid_name + sizeof(invalid_name) - 1;
	}

	len = ep - sp;
	fp = malloc(len + 1);
	if (fp == NULL)
		return NULL;
	memcpy(fp, sp, len);
	fp[len] = '\0';
	return fp;
}

#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
struct mod_metadata64 {
	int		md_version;	/* structure version MDTV_* */
	int		md_type;	/* type of entry MDT_* */
	uint64_t	md_data;	/* specific data */
	uint64_t	md_cval;	/* common string label */
};
#endif
#if defined(__amd64__) && __ELF_WORD_SIZE == 32
struct mod_metadata32 {
	int		md_version;	/* structure version MDTV_* */
	int		md_type;	/* type of entry MDT_* */
	uint32_t	md_data;	/* specific data */
	uint32_t	md_cval;	/* common string label */
};
#endif

int
__elfN(load_modmetadata)(struct preloaded_file *fp, uint64_t dest)
{
	struct elf_file		 ef;
	int			 err, i, j;
	Elf_Shdr		*sh_meta, *shdr = NULL;
	Elf_Shdr		*sh_data[2];
	char			*shstrtab = NULL;
	size_t			 size;
	Elf_Addr		 p_start, p_end;

	bzero(&ef, sizeof(struct elf_file));
	ef.fd = -1;

	err = __elfN(load_elf_header)(fp->f_name, &ef);
	if (err != 0)
		goto out;

	if (ef.kernel == 1 || ef.ehdr->e_type == ET_EXEC) {
		ef.kernel = 1;
	} else if (ef.ehdr->e_type != ET_DYN) {
		err = EFTYPE;
		goto out;
	}

	size = (size_t)ef.ehdr->e_shnum * (size_t)ef.ehdr->e_shentsize;
	shdr = alloc_pread(VECTX_HANDLE(&ef), ef.ehdr->e_shoff, size);
	if (shdr == NULL) {
		err = ENOMEM;
		goto out;
	}

	/* Load shstrtab. */
	shstrtab = alloc_pread(VECTX_HANDLE(&ef), shdr[ef.ehdr->e_shstrndx].sh_offset,
	    shdr[ef.ehdr->e_shstrndx].sh_size);
	if (shstrtab == NULL) {
		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
		    "load_modmetadata: unable to load shstrtab\n");
		err = EFTYPE;
		goto out;
	}

	/* Find set_modmetadata_set and data sections. */
	sh_data[0] = sh_data[1] = sh_meta = NULL;
	for (i = 0, j = 0; i < ef.ehdr->e_shnum; i++) {
		if (strcmp(&shstrtab[shdr[i].sh_name],
		    "set_modmetadata_set") == 0) {
			sh_meta = &shdr[i];
		}
		if ((strcmp(&shstrtab[shdr[i].sh_name], ".data") == 0) ||
		    (strcmp(&shstrtab[shdr[i].sh_name], ".rodata") == 0)) {
			sh_data[j++] = &shdr[i];
		}
	}
	if (sh_meta == NULL || sh_data[0] == NULL || sh_data[1] == NULL) {
		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
    "load_modmetadata: unable to find set_modmetadata_set or data sections\n");
		err = EFTYPE;
		goto out;
	}

	/* Load set_modmetadata_set into memory */
	err = kern_pread(VECTX_HANDLE(&ef), dest, sh_meta->sh_size, sh_meta->sh_offset);
	if (err != 0) {
		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
    "load_modmetadata: unable to load set_modmetadata_set: %d\n", err);
		goto out;
	}
	p_start = dest;
	p_end = dest + sh_meta->sh_size;
	dest += sh_meta->sh_size;

	/* Load data sections into memory. */
	err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[0]->sh_size,
	    sh_data[0]->sh_offset);
	if (err != 0) {
		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
		    "load_modmetadata: unable to load data: %d\n", err);
		goto out;
	}

	/*
	 * We have to increment the dest, so that the offset is the same into
	 * both the .rodata and .data sections.
	 */
	ef.off = -(sh_data[0]->sh_addr - dest);
	dest +=	(sh_data[1]->sh_addr - sh_data[0]->sh_addr);

	err = kern_pread(VECTX_HANDLE(&ef), dest, sh_data[1]->sh_size,
	    sh_data[1]->sh_offset);
	if (err != 0) {
		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
		    "load_modmetadata: unable to load data: %d\n", err);
		goto out;
	}

	err = __elfN(parse_modmetadata)(fp, &ef, p_start, p_end);
	if (err != 0) {
		printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
		    "load_modmetadata: unable to parse metadata: %d\n", err);
		goto out;
	}

out:
	if (shstrtab != NULL)
		free(shstrtab);
	if (shdr != NULL)
		free(shdr);
	if (ef.firstpage != NULL)
		free(ef.firstpage);
	if (ef.fd != -1) {
#ifdef LOADER_VERIEXEC_VECTX
		if (!err && ef.vctx) {
			int verror;

			verror = vectx_close(ef.vctx, VE_MUST, __func__);
			if (verror) {
				err = EAUTH;
				file_discard(fp);
			}
		}
#endif
		close(ef.fd);
	}
	return (err);
}

int
__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
    Elf_Addr p_start, Elf_Addr p_end)
{
	struct mod_metadata md;
#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
	struct mod_metadata64 md64;
#elif defined(__amd64__) && __ELF_WORD_SIZE == 32
	struct mod_metadata32 md32;
#endif
	struct mod_depend *mdepend;
	struct mod_version mver;
	char *s;
	int error, modcnt, minfolen;
	Elf_Addr v, p;

	modcnt = 0;
	p = p_start;
	while (p < p_end) {
		COPYOUT(p, &v, sizeof(v));
		error = __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
		if (error == EOPNOTSUPP)
			v += ef->off;
		else if (error != 0)
			return (error);
#if (defined(__i386__) || defined(__powerpc__)) && __ELF_WORD_SIZE == 64
		COPYOUT(v, &md64, sizeof(md64));
		error = __elfN(reloc_ptr)(fp, ef, v, &md64, sizeof(md64));
		if (error == EOPNOTSUPP) {
			md64.md_cval += ef->off;
			md64.md_data += ef->off;
		} else if (error != 0)
			return (error);
		md.md_version = md64.md_version;
		md.md_type = md64.md_type;
		md.md_cval = (const char *)(uintptr_t)md64.md_cval;
		md.md_data = (void *)(uintptr_t)md64.md_data;
#elif defined(__amd64__) && __ELF_WORD_SIZE == 32
		COPYOUT(v, &md32, sizeof(md32));
		error = __elfN(reloc_ptr)(fp, ef, v, &md32, sizeof(md32));
		if (error == EOPNOTSUPP) {
			md32.md_cval += ef->off;
			md32.md_data += ef->off;
		} else if (error != 0)
			return (error);
		md.md_version = md32.md_version;
		md.md_type = md32.md_type;
		md.md_cval = (const char *)(uintptr_t)md32.md_cval;
		md.md_data = (void *)(uintptr_t)md32.md_data;
#else
		COPYOUT(v, &md, sizeof(md));
		error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
		if (error == EOPNOTSUPP) {
			md.md_cval += ef->off;
			md.md_data = (void *)((uintptr_t)md.md_data +
			    (uintptr_t)ef->off);
		} else if (error != 0)
			return (error);
#endif
		p += sizeof(Elf_Addr);
		switch(md.md_type) {
		case MDT_DEPEND:
			if (ef->kernel) /* kernel must not depend on anything */
				break;
			s = strdupout((vm_offset_t)md.md_cval);
			minfolen = sizeof(*mdepend) + strlen(s) + 1;
			mdepend = malloc(minfolen);
			if (mdepend == NULL)
				return ENOMEM;
			COPYOUT((vm_offset_t)md.md_data, mdepend,
			    sizeof(*mdepend));
			strcpy((char*)(mdepend + 1), s);
			free(s);
			file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
			    mdepend);
			free(mdepend);
			break;
		case MDT_VERSION:
			s = strdupout((vm_offset_t)md.md_cval);
			COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver));
			file_addmodule(fp, s, mver.mv_version, NULL);
			free(s);
			modcnt++;
			break;
		}
	}
	if (modcnt == 0) {
		s = fake_modname(fp->f_name);
		file_addmodule(fp, s, 1, NULL);
		free(s);
	}
	return 0;
}

static unsigned long
elf_hash(const char *name)
{
	const unsigned char *p = (const unsigned char *) name;
	unsigned long h = 0;
	unsigned long g;

	while (*p != '\0') {
		h = (h << 4) + *p++;
		if ((g = h & 0xf0000000) != 0)
			h ^= g >> 24;
		h &= ~g;
	}
	return h;
}

static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE)
    "_lookup_symbol: corrupt symbol table\n";
int
__elfN(lookup_symbol)(elf_file_t ef, const char* name, Elf_Sym *symp,
    unsigned char type)
{
	Elf_Hashelt symnum;
	Elf_Sym sym;
	char *strp;
	unsigned long hash;

	if (ef->nbuckets == 0) {
		printf(__elfN(bad_symtable));
		return ENOENT;
	}

	hash = elf_hash(name);
	COPYOUT(&ef->buckets[hash % ef->nbuckets], &symnum, sizeof(symnum));

	while (symnum != STN_UNDEF) {
		if (symnum >= ef->nchains) {
			printf(__elfN(bad_symtable));
			return ENOENT;
		}

		COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
		if (sym.st_name == 0) {
			printf(__elfN(bad_symtable));
			return ENOENT;
		}

		strp = strdupout((vm_offset_t)(ef->strtab + sym.st_name));
		if (strcmp(name, strp) == 0) {
			free(strp);
			if (sym.st_shndx != SHN_UNDEF && sym.st_value != 0 &&
			    ELF_ST_TYPE(sym.st_info) == type) {
				*symp = sym;
				return 0;
			}
			return ENOENT;
		}
		free(strp);
		COPYOUT(&ef->chains[symnum], &symnum, sizeof(symnum));
	}
	return ENOENT;
}

/*
 * Apply any intra-module relocations to the value. p is the load address
 * of the value and val/len is the value to be modified. This does NOT modify
 * the image in-place, because this is done by kern_linker later on.
 *
 * Returns EOPNOTSUPP if no relocation method is supplied.
 */
static int
__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
    Elf_Addr p, void *val, size_t len)
{
	size_t n;
	Elf_Rela a;
	Elf_Rel r;
	int error;

	/*
	 * The kernel is already relocated, but we still want to apply
	 * offset adjustments.
	 */
	if (ef->kernel)
		return (EOPNOTSUPP);

	for (n = 0; n < ef->relsz / sizeof(r); n++) {
		COPYOUT(ef->rel + n, &r, sizeof(r));

		error = __elfN(reloc)(ef, __elfN(symaddr), &r, ELF_RELOC_REL,
		    ef->off, p, val, len);
		if (error != 0)
			return (error);
	}
	for (n = 0; n < ef->relasz / sizeof(a); n++) {
		COPYOUT(ef->rela + n, &a, sizeof(a));

		error = __elfN(reloc)(ef, __elfN(symaddr), &a, ELF_RELOC_RELA,
		    ef->off, p, val, len);
		if (error != 0)
			return (error);
	}

	return (0);
}

static Elf_Addr
__elfN(symaddr)(struct elf_file *ef, Elf_Size symidx)
{

	/* Symbol lookup by index not required here. */
	return (0);
}