xref: /freebsd/contrib/file/src/elfclass.h (revision 4460e5b02d2e07d8aa286fff8f644a442b376cae)
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);
35*4460e5b0SXin LI 	notecount = ms->elf_notes_max;
36b6cee71dSXin LI 	switch (type) {
37b6cee71dSXin LI #ifdef ELFCORE
38b6cee71dSXin LI 	case ET_CORE:
392c4f1647SXin LI 		phnum = elf_getu16(swap, elfhdr.e_phnum);
40c2931133SXin LI 		if (phnum > ms->elf_phnum_max)
41*4460e5b0SXin LI 			return toomany(ms, "program headers", phnum);
42b6cee71dSXin LI 		flags |= FLAGS_IS_CORE;
43b6cee71dSXin LI 		if (dophn_core(ms, clazz, swap, fd,
442c4f1647SXin LI 		    (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
45b6cee71dSXin LI 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
46*4460e5b0SXin LI 		    fsize, &flags, &notecount) == -1)
47b6cee71dSXin LI 			return -1;
48b6cee71dSXin LI 		break;
49b6cee71dSXin LI #endif
50b6cee71dSXin LI 	case ET_EXEC:
51b6cee71dSXin LI 	case ET_DYN:
522c4f1647SXin LI 		phnum = elf_getu16(swap, elfhdr.e_phnum);
53c2931133SXin LI 		if (phnum > ms->elf_phnum_max)
542c4f1647SXin LI 			return toomany(ms, "program", phnum);
552c4f1647SXin LI 		shnum = elf_getu16(swap, elfhdr.e_shnum);
56c2931133SXin LI 		if (shnum > ms->elf_shnum_max)
572c4f1647SXin LI 			return toomany(ms, "section", shnum);
58b6cee71dSXin LI 		if (dophn_exec(ms, clazz, swap, fd,
592c4f1647SXin LI 		    (off_t)elf_getu(swap, elfhdr.e_phoff), phnum,
60b6cee71dSXin LI 		    (size_t)elf_getu16(swap, elfhdr.e_phentsize),
61*4460e5b0SXin LI 		    fsize, shnum, &flags, &notecount) == -1)
62b6cee71dSXin LI 			return -1;
63b6cee71dSXin LI 		/*FALLTHROUGH*/
64b6cee71dSXin LI 	case ET_REL:
652c4f1647SXin LI 		shnum = elf_getu16(swap, elfhdr.e_shnum);
66c2931133SXin LI 		if (shnum > ms->elf_shnum_max)
67*4460e5b0SXin LI 			return toomany(ms, "section headers", shnum);
68b6cee71dSXin LI 		if (doshn(ms, clazz, swap, fd,
692c4f1647SXin LI 		    (off_t)elf_getu(swap, elfhdr.e_shoff), shnum,
70b6cee71dSXin LI 		    (size_t)elf_getu16(swap, elfhdr.e_shentsize),
71*4460e5b0SXin LI 		    fsize, elf_getu16(swap, elfhdr.e_machine),
72*4460e5b0SXin LI 		    (int)elf_getu16(swap, elfhdr.e_shstrndx),
73*4460e5b0SXin LI 		    &flags, &notecount) == -1)
74b6cee71dSXin LI 			return -1;
75b6cee71dSXin LI 		break;
76b6cee71dSXin LI 
77b6cee71dSXin LI 	default:
78b6cee71dSXin LI 		break;
79b6cee71dSXin LI 	}
80*4460e5b0SXin LI 	if (notecount == 0)
81*4460e5b0SXin LI 		return toomany(ms, "notes", ms->elf_notes_max);
82b6cee71dSXin LI 	return 1;
83