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