xref: /dflybsd-src/contrib/file/src/elfclass.h (revision 6fca56fb90a257291c53bba3b861b751027c3e3d)
179343712SPeter Avalos /*
279343712SPeter Avalos  * Copyright (c) Christos Zoulas 2008.
379343712SPeter Avalos  * All Rights Reserved.
479343712SPeter Avalos  *
579343712SPeter Avalos  * Redistribution and use in source and binary forms, with or without
679343712SPeter Avalos  * modification, are permitted provided that the following conditions
779343712SPeter Avalos  * are met:
879343712SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
979343712SPeter Avalos  *    notice immediately at the beginning of the file, without modification,
1079343712SPeter Avalos  *    this list of conditions, and the following disclaimer.
1179343712SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1279343712SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1379343712SPeter Avalos  *    documentation and/or other materials provided with the distribution.
1479343712SPeter Avalos  *
1579343712SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1679343712SPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1779343712SPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1879343712SPeter Avalos  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
1979343712SPeter Avalos  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2079343712SPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2179343712SPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2279343712SPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2379343712SPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2479343712SPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2579343712SPeter Avalos  * SUCH DAMAGE.
2679343712SPeter Avalos  */
2779343712SPeter Avalos 	if (nbytes <= sizeof(elfhdr))
2879343712SPeter Avalos 		return 0;
2979343712SPeter Avalos 
3079343712SPeter Avalos 	u.l = 1;
3179343712SPeter Avalos 	(void)memcpy(&elfhdr, buf, sizeof elfhdr);
3279343712SPeter Avalos 	swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[EI_DATA];
3379343712SPeter Avalos 
3479343712SPeter Avalos 	type = elf_getu16(swap, elfhdr.e_type);
3582c5fa3eSPeter Avalos 	notecount = ms->elf_notes_max;
3679343712SPeter Avalos 	switch (type) {
3779343712SPeter Avalos #ifdef ELFCORE
3879343712SPeter Avalos 	case ET_CORE:
3982c5fa3eSPeter Avalos 		phnum = elf_getu16(swap, elfhdr.e_phnum);
4082c5fa3eSPeter Avalos 		if (phnum > ms->elf_phnum_max)
4182c5fa3eSPeter Avalos 			return toomany(ms, "program headers", phnum);
42e4d4ce0cSPeter Avalos 		flags |= FLAGS_IS_CORE;
4379343712SPeter Avalos 		if (dophn_core(ms, clazz, swap, fd,
44*6fca56fbSSascha Wildner 		    CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
45*6fca56fbSSascha Wildner 		    CAST(size_t, elf_getu16(swap, elfhdr.e_phentsize)),
4682c5fa3eSPeter Avalos 		    fsize, &flags, &notecount) == -1)
4779343712SPeter Avalos 			return -1;
4879343712SPeter Avalos 		break;
4979343712SPeter Avalos #endif
5079343712SPeter Avalos 	case ET_EXEC:
5179343712SPeter Avalos 	case ET_DYN:
5282c5fa3eSPeter Avalos 		phnum = elf_getu16(swap, elfhdr.e_phnum);
5382c5fa3eSPeter Avalos 		if (phnum > ms->elf_phnum_max)
5482c5fa3eSPeter Avalos 			return toomany(ms, "program", phnum);
5582c5fa3eSPeter Avalos 		shnum = elf_getu16(swap, elfhdr.e_shnum);
5682c5fa3eSPeter Avalos 		if (shnum > ms->elf_shnum_max)
5782c5fa3eSPeter Avalos 			return toomany(ms, "section", shnum);
5879343712SPeter Avalos 		if (dophn_exec(ms, clazz, swap, fd,
59*6fca56fbSSascha Wildner 		    CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,
60*6fca56fbSSascha Wildner 		    CAST(size_t, elf_getu16(swap, elfhdr.e_phentsize)),
6182c5fa3eSPeter Avalos 		    fsize, shnum, &flags, &notecount) == -1)
6279343712SPeter Avalos 			return -1;
6379343712SPeter Avalos 		/*FALLTHROUGH*/
6479343712SPeter Avalos 	case ET_REL:
6582c5fa3eSPeter Avalos 		shnum = elf_getu16(swap, elfhdr.e_shnum);
6682c5fa3eSPeter Avalos 		if (shnum > ms->elf_shnum_max)
6782c5fa3eSPeter Avalos 			return toomany(ms, "section headers", shnum);
6879343712SPeter Avalos 		if (doshn(ms, clazz, swap, fd,
69*6fca56fbSSascha Wildner 		    CAST(off_t, elf_getu(swap, elfhdr.e_shoff)), shnum,
70*6fca56fbSSascha Wildner 		    CAST(size_t, elf_getu16(swap, elfhdr.e_shentsize)),
7182c5fa3eSPeter Avalos 		    fsize, elf_getu16(swap, elfhdr.e_machine),
72*6fca56fbSSascha Wildner 		    CAST(int, elf_getu16(swap, elfhdr.e_shstrndx)),
7382c5fa3eSPeter Avalos 		    &flags, &notecount) == -1)
7479343712SPeter Avalos 			return -1;
7579343712SPeter Avalos 		break;
7679343712SPeter Avalos 
7779343712SPeter Avalos 	default:
7879343712SPeter Avalos 		break;
7979343712SPeter Avalos 	}
8082c5fa3eSPeter Avalos 	if (notecount == 0)
8182c5fa3eSPeter Avalos 		return toomany(ms, "notes", ms->elf_notes_max);
8279343712SPeter Avalos 	return 1;
83