xref: /netbsd-src/external/bsd/elftoolchain/dist/libelf/libelf_elfmachine.c (revision 5ac3bc719ce6e70593039505b491894133237d12)
1*5ac3bc71Schristos /*	$NetBSD: libelf_elfmachine.c,v 1.2 2024/03/03 17:37:34 christos Exp $	*/
24639c15bSchristos 
34639c15bSchristos /*-
44639c15bSchristos  * Copyright (c) 2018 Joseph Koshy
54639c15bSchristos  * All rights reserved.
64639c15bSchristos  *
74639c15bSchristos  * Redistribution and use in source and binary forms, with or without
84639c15bSchristos  * modification, are permitted provided that the following conditions
94639c15bSchristos  * are met:
104639c15bSchristos  * 1. Redistributions of source code must retain the above copyright
114639c15bSchristos  *    notice, this list of conditions and the following disclaimer.
124639c15bSchristos  * 2. Redistributions in binary form must reproduce the above copyright
134639c15bSchristos  *    notice, this list of conditions and the following disclaimer in the
144639c15bSchristos  *    documentation and/or other materials provided with the distribution.
154639c15bSchristos  *
164639c15bSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174639c15bSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184639c15bSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194639c15bSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204639c15bSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214639c15bSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224639c15bSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234639c15bSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244639c15bSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254639c15bSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264639c15bSchristos  * SUCH DAMAGE.
274639c15bSchristos  */
284639c15bSchristos 
29*5ac3bc71Schristos #if HAVE_NBTOOL_CONFIG_H
30*5ac3bc71Schristos # include "nbtool_config.h"
31*5ac3bc71Schristos #endif
32*5ac3bc71Schristos 
334639c15bSchristos #include <sys/cdefs.h>
344639c15bSchristos 
354639c15bSchristos #include <assert.h>
364639c15bSchristos #include <libelf.h>
374639c15bSchristos 
384639c15bSchristos #include "_libelf.h"
394639c15bSchristos 
40*5ac3bc71Schristos __RCSID("$NetBSD: libelf_elfmachine.c,v 1.2 2024/03/03 17:37:34 christos Exp $");
414639c15bSchristos ELFTC_VCSID("Id: libelf_elfmachine.c 3977 2022-05-01 06:45:34Z jkoshy");
424639c15bSchristos 
434639c15bSchristos /*
444639c15bSchristos  * A convenience helper that returns the ELF machine architecture for
454639c15bSchristos  * a ELF descriptor.
464639c15bSchristos  */
474639c15bSchristos int
_libelf_elfmachine(Elf * e)484639c15bSchristos _libelf_elfmachine(Elf *e)
494639c15bSchristos {
504639c15bSchristos 	Elf32_Ehdr *eh32;
514639c15bSchristos 	Elf64_Ehdr *eh64;
524639c15bSchristos 
534639c15bSchristos 	if (!e)
544639c15bSchristos 		return EM_NONE;
554639c15bSchristos 
564639c15bSchristos 	assert(e->e_kind == ELF_K_ELF);
574639c15bSchristos 	assert(e->e_class != ELFCLASSNONE);
584639c15bSchristos 
594639c15bSchristos 	eh32 = NULL;
604639c15bSchristos 	eh64 = NULL;
614639c15bSchristos 
624639c15bSchristos 	switch (e->e_class) {
634639c15bSchristos 	case ELFCLASS32:
644639c15bSchristos 		eh32 = e->e_u.e_elf.e_ehdr.e_ehdr32;
654639c15bSchristos 		return eh32 ? eh32->e_machine : EM_NONE;
664639c15bSchristos 	case ELFCLASS64:
674639c15bSchristos 		eh64 = e->e_u.e_elf.e_ehdr.e_ehdr64;
684639c15bSchristos 		return eh64 ? eh64->e_machine : EM_NONE;
694639c15bSchristos 	}
704639c15bSchristos 
714639c15bSchristos 	return (EM_NONE);
724639c15bSchristos }
73