xref: /freebsd/contrib/elftoolchain/libelf/libelf_elfmachine.c (revision ae500c1ff8974130f7f2692772cf288b90349e0d)
1*ae500c1fSEd Maste /*-
2*ae500c1fSEd Maste  * Copyright (c) 2018 Joseph Koshy
3*ae500c1fSEd Maste  * All rights reserved.
4*ae500c1fSEd Maste  *
5*ae500c1fSEd Maste  * Redistribution and use in source and binary forms, with or without
6*ae500c1fSEd Maste  * modification, are permitted provided that the following conditions
7*ae500c1fSEd Maste  * are met:
8*ae500c1fSEd Maste  * 1. Redistributions of source code must retain the above copyright
9*ae500c1fSEd Maste  *    notice, this list of conditions and the following disclaimer.
10*ae500c1fSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
11*ae500c1fSEd Maste  *    notice, this list of conditions and the following disclaimer in the
12*ae500c1fSEd Maste  *    documentation and/or other materials provided with the distribution.
13*ae500c1fSEd Maste  *
14*ae500c1fSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*ae500c1fSEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*ae500c1fSEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*ae500c1fSEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*ae500c1fSEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*ae500c1fSEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*ae500c1fSEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*ae500c1fSEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*ae500c1fSEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*ae500c1fSEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*ae500c1fSEd Maste  * SUCH DAMAGE.
25*ae500c1fSEd Maste  */
26*ae500c1fSEd Maste 
27*ae500c1fSEd Maste #include <assert.h>
28*ae500c1fSEd Maste #include <libelf.h>
29*ae500c1fSEd Maste 
30*ae500c1fSEd Maste #include "_libelf.h"
31*ae500c1fSEd Maste 
32*ae500c1fSEd Maste ELFTC_VCSID("$Id$");
33*ae500c1fSEd Maste 
34*ae500c1fSEd Maste /*
35*ae500c1fSEd Maste  * A convenience helper that returns the ELF machine architecture for
36*ae500c1fSEd Maste  * a ELF descriptor.
37*ae500c1fSEd Maste  */
38*ae500c1fSEd Maste int
_libelf_elfmachine(Elf * e)39*ae500c1fSEd Maste _libelf_elfmachine(Elf *e)
40*ae500c1fSEd Maste {
41*ae500c1fSEd Maste 	Elf32_Ehdr *eh32;
42*ae500c1fSEd Maste 	Elf64_Ehdr *eh64;
43*ae500c1fSEd Maste 
44*ae500c1fSEd Maste 	if (!e)
45*ae500c1fSEd Maste 		return EM_NONE;
46*ae500c1fSEd Maste 
47*ae500c1fSEd Maste 	assert(e->e_kind == ELF_K_ELF);
48*ae500c1fSEd Maste 	assert(e->e_class != ELFCLASSNONE);
49*ae500c1fSEd Maste 
50*ae500c1fSEd Maste 	eh32 = NULL;
51*ae500c1fSEd Maste 	eh64 = NULL;
52*ae500c1fSEd Maste 
53*ae500c1fSEd Maste 	switch (e->e_class) {
54*ae500c1fSEd Maste 	case ELFCLASS32:
55*ae500c1fSEd Maste 		eh32 = e->e_u.e_elf.e_ehdr.e_ehdr32;
56*ae500c1fSEd Maste 		return eh32 ? eh32->e_machine : EM_NONE;
57*ae500c1fSEd Maste 	case ELFCLASS64:
58*ae500c1fSEd Maste 		eh64 = e->e_u.e_elf.e_ehdr.e_ehdr64;
59*ae500c1fSEd Maste 		return eh64 ? eh64->e_machine : EM_NONE;
60*ae500c1fSEd Maste 	}
61*ae500c1fSEd Maste 
62*ae500c1fSEd Maste 	return (EM_NONE);
63*ae500c1fSEd Maste }
64