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