xref: /freebsd/contrib/file/src/elfclass.h (revision b6cee71de37d56e36dbc118e2d9b03e7cece5709)
1*b6cee71dSXin LI /*
2*b6cee71dSXin LI  * Copyright (c) Christos Zoulas 2008.
3*b6cee71dSXin LI  * All Rights Reserved.
4*b6cee71dSXin LI  *
5*b6cee71dSXin LI  * Redistribution and use in source and binary forms, with or without
6*b6cee71dSXin LI  * modification, are permitted provided that the following conditions
7*b6cee71dSXin LI  * are met:
8*b6cee71dSXin LI  * 1. Redistributions of source code must retain the above copyright
9*b6cee71dSXin LI  *    notice immediately at the beginning of the file, without modification,
10*b6cee71dSXin LI  *    this list of conditions, and the following disclaimer.
11*b6cee71dSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
12*b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer in the
13*b6cee71dSXin LI  *    documentation and/or other materials provided with the distribution.
14*b6cee71dSXin LI  *
15*b6cee71dSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*b6cee71dSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*b6cee71dSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*b6cee71dSXin LI  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19*b6cee71dSXin LI  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*b6cee71dSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*b6cee71dSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*b6cee71dSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*b6cee71dSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*b6cee71dSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*b6cee71dSXin LI  * SUCH DAMAGE.
26*b6cee71dSXin LI  */
27*b6cee71dSXin LI 	if (nbytes <= sizeof(elfhdr))
28*b6cee71dSXin LI 		return 0;
29*b6cee71dSXin LI 
30*b6cee71dSXin LI 	u.l = 1;
31*b6cee71dSXin LI 	(void)memcpy(&elfhdr, buf, sizeof elfhdr);
32*b6cee71dSXin LI 	swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[EI_DATA];
33*b6cee71dSXin LI 
34*b6cee71dSXin LI 	type = elf_getu16(swap, elfhdr.e_type);
35*b6cee71dSXin LI 	switch (type) {
36*b6cee71dSXin LI #ifdef ELFCORE
37*b6cee71dSXin LI 	case ET_CORE:
38*b6cee71dSXin LI 		flags |= FLAGS_IS_CORE;
39*b6cee71dSXin LI 		if (dophn_core(ms, clazz, swap, fd,
40*b6cee71dSXin LI 		    (off_t)elf_getu(swap, elfhdr.e_phoff),
41*b6cee71dSXin LI 		    elf_getu16(swap, elfhdr.e_phnum),
42*b6cee71dSXin LI 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
43*b6cee71dSXin LI 		    fsize, &flags) == -1)
44*b6cee71dSXin LI 			return -1;
45*b6cee71dSXin LI 		break;
46*b6cee71dSXin LI #endif
47*b6cee71dSXin LI 	case ET_EXEC:
48*b6cee71dSXin LI 	case ET_DYN:
49*b6cee71dSXin LI 		if (dophn_exec(ms, clazz, swap, fd,
50*b6cee71dSXin LI 		    (off_t)elf_getu(swap, elfhdr.e_phoff),
51*b6cee71dSXin LI 		    elf_getu16(swap, elfhdr.e_phnum),
52*b6cee71dSXin LI 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
53*b6cee71dSXin LI 		    fsize, &flags, elf_getu16(swap, elfhdr.e_shnum))
54*b6cee71dSXin LI 		    == -1)
55*b6cee71dSXin LI 			return -1;
56*b6cee71dSXin LI 		/*FALLTHROUGH*/
57*b6cee71dSXin LI 	case ET_REL:
58*b6cee71dSXin LI 		if (doshn(ms, clazz, swap, fd,
59*b6cee71dSXin LI 		    (off_t)elf_getu(swap, elfhdr.e_shoff),
60*b6cee71dSXin LI 		    elf_getu16(swap, elfhdr.e_shnum),
61*b6cee71dSXin LI 		    (size_t)elf_getu16(swap, elfhdr.e_shentsize),
62*b6cee71dSXin LI 		    fsize, &flags, elf_getu16(swap, elfhdr.e_machine),
63*b6cee71dSXin LI 		    (int)elf_getu16(swap, elfhdr.e_shstrndx)) == -1)
64*b6cee71dSXin LI 			return -1;
65*b6cee71dSXin LI 		break;
66*b6cee71dSXin LI 
67*b6cee71dSXin LI 	default:
68*b6cee71dSXin LI 		break;
69*b6cee71dSXin LI 	}
70*b6cee71dSXin LI 	return 1;
71