2 * Copyright (c) 2004 Ian Dowse <iedowse@freebsd.org>
3 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/boot/common/load_elf_obj.c,v 1.2 2005/12/18 04:52:35 marcel Exp $
31 #include <sys/param.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
37 #include <machine/elf.h>
42 #include "bootstrap.h"
44 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l)
46 #if defined(__i386__) && __ELF_WORD_SIZE == 64
49 #define ELF_TARG_CLASS ELFCLASS64
50 #define ELF_TARG_MACH EM_X86_64
53 typedef struct elf_file
{
57 int symtabindex
; /* Index of symbol table */
58 int shstrindex
; /* Index of section name string table */
64 static int __elfN(obj_loadimage
)(struct preloaded_file
*mp
, elf_file_t ef
,
66 static int __elfN(obj_lookup_set
)(struct preloaded_file
*mp
, elf_file_t ef
,
67 const char *name
, Elf_Addr
*startp
, Elf_Addr
*stopp
, int *countp
);
68 static int __elfN(obj_reloc_ptr
)(struct preloaded_file
*mp
, elf_file_t ef
,
69 Elf_Addr p
, void *val
, size_t len
);
70 static int __elfN(obj_parse_modmetadata
)(struct preloaded_file
*mp
,
72 static Elf_Addr
__elfN(obj_symaddr
)(struct elf_file
*ef
, Elf_Size symidx
);
74 const char *__elfN(obj_kerneltype
) = "elf kernel";
75 const char *__elfN(obj_moduletype
) = "elf obj module";
78 * Attempt to load the file (file) as an ELF module. It will be stored at
79 * (dest), and a pointer to a module structure describing the loaded object
80 * will be saved in (result).
83 __elfN(obj_loadfile
)(char *filename
, u_int64_t dest
,
84 struct preloaded_file
**result
)
86 struct preloaded_file
*fp
, *kfp
;
93 bzero(&ef
, sizeof(struct elf_file
));
96 * Open the image, read and validate the ELF header
98 if (filename
== NULL
) /* can't handle nameless */
100 if ((ef
.fd
= rel_open(filename
, O_RDONLY
)) == -1)
104 bytes_read
= read(ef
.fd
, hdr
, sizeof(*hdr
));
105 if (bytes_read
!= sizeof(*hdr
)) {
106 err
= EFTYPE
; /* could be EIO, but may be small file */
115 if (hdr
->e_ident
[EI_CLASS
] != ELF_TARG_CLASS
|| /* Layout ? */
116 hdr
->e_ident
[EI_DATA
] != ELF_TARG_DATA
||
117 hdr
->e_ident
[EI_VERSION
] != EV_CURRENT
|| /* Version ? */
118 hdr
->e_version
!= EV_CURRENT
||
119 hdr
->e_machine
!= ELF_TARG_MACH
|| /* Machine ? */
120 hdr
->e_type
!= ET_REL
) {
125 if (hdr
->e_shnum
* hdr
->e_shentsize
== 0 || hdr
->e_shoff
== 0 ||
126 hdr
->e_shentsize
!= sizeof(Elf_Shdr
)) {
131 kfp
= file_findfile(NULL
, NULL
);
133 printf("elf" __XSTRING(__ELF_WORD_SIZE
)
134 "_obj_loadfile: can't load module before kernel\n");
138 if (strcmp(__elfN(obj_kerneltype
), kfp
->f_type
)) {
139 printf("elf" __XSTRING(__ELF_WORD_SIZE
)
140 "_obj_loadfile: can't load module with kernel type '%s'\n",
146 /* Page-align the load address */
147 dest
= roundup(dest
, PAGE_SIZE
);
150 * Ok, we think we should handle this.
154 printf("elf" __XSTRING(__ELF_WORD_SIZE
)
155 "_obj_loadfile: cannot allocate module info\n");
159 fp
->f_name
= strdup(filename
);
160 fp
->f_type
= strdup(__elfN(obj_moduletype
));
162 printf("%s ", filename
);
164 fp
->f_size
= __elfN(obj_loadimage
)(fp
, &ef
, dest
);
165 if (fp
->f_size
== 0 || fp
->f_addr
== 0)
168 /* save exec header as metadata */
169 file_addmetadata(fp
, MODINFOMD_ELFHDR
, sizeof(*hdr
), hdr
);
171 /* Load OK, return module pointer */
172 *result
= (struct preloaded_file
*)fp
;
182 if (ef
.e_shdr
!= NULL
)
189 * With the file (fd) open on the image, and (ehdr) containing
190 * the Elf header, load the image at (off)
193 __elfN(obj_loadimage
)(struct preloaded_file
*fp
, elf_file_t ef
, u_int64_t off
)
197 vm_offset_t firstaddr
, lastaddr
;
198 int i
, nsym
, res
, ret
, shdrbytes
, symstrindex
;
201 firstaddr
= lastaddr
= (vm_offset_t
)off
;
203 ef
->off
= (vm_offset_t
)off
;
205 /* Read in the section headers. */
206 shdrbytes
= hdr
->e_shnum
* hdr
->e_shentsize
;
207 shdr
= alloc_pread(ef
->fd
, (off_t
)hdr
->e_shoff
, shdrbytes
);
209 printf("\nelf" __XSTRING(__ELF_WORD_SIZE
)
210 "_obj_loadimage: read section headers failed\n");
216 * Decide where to load everything, but don't read it yet.
217 * We store the load address as a non-zero sh_addr value.
218 * Start with the code/data and bss.
220 for (i
= 0; i
< hdr
->e_shnum
; i
++)
222 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
223 switch (shdr
[i
].sh_type
) {
226 lastaddr
= roundup(lastaddr
, shdr
[i
].sh_addralign
);
227 shdr
[i
].sh_addr
= (Elf_Addr
)lastaddr
;
228 lastaddr
+= shdr
[i
].sh_size
;
235 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
236 switch (shdr
[i
].sh_type
) {
240 shdr
[i
].sh_addr
= (Elf_Addr
)lastaddr
;
241 lastaddr
+= shdr
[i
].sh_size
;
246 printf("\nelf" __XSTRING(__ELF_WORD_SIZE
)
247 "_obj_loadimage: file has no valid symbol table\n");
250 lastaddr
= roundup(lastaddr
, shdr
[ef
->symtabindex
].sh_addralign
);
251 shdr
[ef
->symtabindex
].sh_addr
= (Elf_Addr
)lastaddr
;
252 lastaddr
+= shdr
[ef
->symtabindex
].sh_size
;
254 symstrindex
= shdr
[ef
->symtabindex
].sh_link
;
255 if (symstrindex
< 0 || symstrindex
>= hdr
->e_shnum
||
256 shdr
[symstrindex
].sh_type
!= SHT_STRTAB
) {
257 printf("\nelf" __XSTRING(__ELF_WORD_SIZE
)
258 "_obj_loadimage: file has invalid symbol strings\n");
261 lastaddr
= roundup(lastaddr
, shdr
[symstrindex
].sh_addralign
);
262 shdr
[symstrindex
].sh_addr
= (Elf_Addr
)lastaddr
;
263 lastaddr
+= shdr
[symstrindex
].sh_size
;
266 if (hdr
->e_shstrndx
== 0 || hdr
->e_shstrndx
>= hdr
->e_shnum
||
267 shdr
[hdr
->e_shstrndx
].sh_type
!= SHT_STRTAB
) {
268 printf("\nelf" __XSTRING(__ELF_WORD_SIZE
)
269 "_obj_loadimage: file has no section names\n");
272 ef
->shstrindex
= hdr
->e_shstrndx
;
273 lastaddr
= roundup(lastaddr
, shdr
[ef
->shstrindex
].sh_addralign
);
274 shdr
[ef
->shstrindex
].sh_addr
= (Elf_Addr
)lastaddr
;
275 lastaddr
+= shdr
[ef
->shstrindex
].sh_size
;
277 /* Relocation tables. */
278 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
279 switch (shdr
[i
].sh_type
) {
282 lastaddr
= roundup(lastaddr
, shdr
[i
].sh_addralign
);
283 shdr
[i
].sh_addr
= (Elf_Addr
)lastaddr
;
284 lastaddr
+= shdr
[i
].sh_size
;
289 /* Clear the whole area, including bss regions. */
290 kern_bzero(firstaddr
, lastaddr
- firstaddr
);
292 /* Now read it all in. */
293 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
294 if (shdr
[i
].sh_addr
== 0 || shdr
[i
].sh_type
== SHT_NOBITS
)
296 if (kern_pread(ef
->fd
, (vm_offset_t
)shdr
[i
].sh_addr
,
297 shdr
[i
].sh_size
, (off_t
)shdr
[i
].sh_offset
) != 0) {
298 printf("\nelf" __XSTRING(__ELF_WORD_SIZE
)
299 "_obj_loadimage: read failed\n");
304 file_addmetadata(fp
, MODINFOMD_SHDR
, shdrbytes
, shdr
);
306 res
= __elfN(obj_parse_modmetadata
)(fp
, ef
);
310 ret
= lastaddr
- firstaddr
;
311 fp
->f_addr
= firstaddr
;
313 printf("size 0x%lx at 0x%lx", (u_long
)ret
, (u_long
)firstaddr
);
320 #if defined(__i386__) && __ELF_WORD_SIZE == 64
321 struct mod_metadata64
{
322 int md_version
; /* structure version MDTV_* */
323 int md_type
; /* type of entry MDT_* */
324 u_int64_t md_data
; /* specific data */
325 u_int64_t md_cval
; /* common string label */
330 __elfN(obj_parse_modmetadata
)(struct preloaded_file
*fp
, elf_file_t ef
)
332 struct mod_metadata md
;
333 #if defined(__i386__) && __ELF_WORD_SIZE == 64
334 struct mod_metadata64 md64
;
336 struct mod_depend
*mdepend
;
337 struct mod_version mver
;
339 int error
, modcnt
, minfolen
;
340 Elf_Addr v
, p
, p_stop
;
342 if (__elfN(obj_lookup_set
)(fp
, ef
, "modmetadata_set", &p
, &p_stop
,
348 COPYOUT(p
, &v
, sizeof(v
));
349 error
= __elfN(obj_reloc_ptr
)(fp
, ef
, p
, &v
, sizeof(v
));
352 #if defined(__i386__) && __ELF_WORD_SIZE == 64
353 COPYOUT(v
, &md64
, sizeof(md64
));
354 error
= __elfN(obj_reloc_ptr
)(fp
, ef
, v
, &md64
, sizeof(md64
));
357 md
.md_version
= md64
.md_version
;
358 md
.md_type
= md64
.md_type
;
359 md
.md_cval
= (const char *)(uintptr_t)md64
.md_cval
;
360 md
.md_data
= (void *)(uintptr_t)md64
.md_data
;
362 COPYOUT(v
, &md
, sizeof(md
));
363 error
= __elfN(obj_reloc_ptr
)(fp
, ef
, v
, &md
, sizeof(md
));
367 p
+= sizeof(Elf_Addr
);
370 s
= strdupout((vm_offset_t
)md
.md_cval
);
371 minfolen
= sizeof(*mdepend
) + strlen(s
) + 1;
372 mdepend
= malloc(minfolen
);
375 COPYOUT((vm_offset_t
)md
.md_data
, mdepend
,
377 strcpy((char*)(mdepend
+ 1), s
);
379 file_addmetadata(fp
, MODINFOMD_DEPLIST
, minfolen
,
384 s
= strdupout((vm_offset_t
)md
.md_cval
);
385 COPYOUT((vm_offset_t
)md
.md_data
, &mver
, sizeof(mver
));
386 file_addmodule(fp
, s
, mver
.mv_version
, NULL
);
393 printf("unknown type %d\n", md
.md_type
);
401 __elfN(obj_lookup_set
)(struct preloaded_file
*fp
, elf_file_t ef
,
402 const char* name
, Elf_Addr
*startp
, Elf_Addr
*stopp
, int *countp
)
407 vm_offset_t shstrtab
;
412 shstrtab
= shdr
[ef
->shstrindex
].sh_addr
;
414 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
415 if (shdr
[i
].sh_type
!= SHT_PROGBITS
)
417 if (shdr
[i
].sh_name
== 0)
419 p
= strdupout(shstrtab
+ shdr
[i
].sh_name
);
420 if (strncmp(p
, "set_", 4) == 0 && strcmp(p
+ 4, name
) == 0) {
421 *startp
= shdr
[i
].sh_addr
;
422 *stopp
= shdr
[i
].sh_addr
+ shdr
[i
].sh_size
;
423 *countp
= (*stopp
- *startp
) / sizeof(Elf_Addr
);
434 * Apply any intra-module relocations to the value. p is the load address
435 * of the value and val/len is the value to be modified. This does NOT modify
436 * the image in-place, because this is done by kern_linker later on.
439 __elfN(obj_reloc_ptr
)(struct preloaded_file
*mp
, elf_file_t ef
, Elf_Addr p
,
440 void *val
, size_t len
)
448 int error
, i
, j
, nrel
, nrela
;
453 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
454 if (shdr
[i
].sh_type
!= SHT_RELA
&& shdr
[i
].sh_type
!= SHT_REL
)
456 base
= shdr
[shdr
[i
].sh_info
].sh_addr
;
457 if (base
== 0 || shdr
[i
].sh_addr
== 0)
459 if (off
< base
|| off
+ len
> base
+
460 shdr
[shdr
[i
].sh_info
].sh_size
)
463 switch (shdr
[i
].sh_type
) {
465 abase
= (Elf_Rela
*)(intptr_t)shdr
[i
].sh_addr
;
467 nrela
= shdr
[i
].sh_size
/ sizeof(Elf_Rela
);
468 for (j
= 0; j
< nrela
; j
++) {
469 COPYOUT(abase
+ j
, &a
, sizeof(a
));
471 error
= __elfN(reloc
)(ef
, __elfN(obj_symaddr
),
472 &a
, ELF_RELOC_RELA
, base
, off
, val
, len
);
478 rbase
= (Elf_Rel
*)(intptr_t)shdr
[i
].sh_addr
;
480 nrel
= shdr
[i
].sh_size
/ sizeof(Elf_Rel
);
481 for (j
= 0; j
< nrel
; j
++) {
482 COPYOUT(rbase
+ j
, &r
, sizeof(r
));
484 error
= __elfN(reloc
)(ef
, __elfN(obj_symaddr
),
485 &r
, ELF_RELOC_REL
, base
, off
, val
, len
);
495 /* Look up the address of a specified symbol. */
497 __elfN(obj_symaddr
)(struct elf_file
*ef
, Elf_Size symidx
)
503 symcnt
= ef
->e_shdr
[ef
->symtabindex
].sh_size
/ sizeof(Elf_Sym
);
504 if (symidx
>= symcnt
)
506 COPYOUT(ef
->e_shdr
[ef
->symtabindex
].sh_addr
+ symidx
* sizeof(Elf_Sym
),
508 if (sym
.st_shndx
== SHN_UNDEF
|| sym
.st_shndx
>= ef
->hdr
.e_shnum
)
510 base
= ef
->e_shdr
[sym
.st_shndx
].sh_addr
;
513 return (base
+ sym
.st_value
);