12343Scsgr/*-
22343Scsgr * Copyright (c) 2018 Joseph Koshy
32343Scsgr * All rights reserved.
42343Scsgr *
52343Scsgr * Redistribution and use in source and binary forms, with or without
62343Scsgr * modification, are permitted provided that the following conditions
72343Scsgr * are met:
82343Scsgr * 1. Redistributions of source code must retain the above copyright
92343Scsgr *    notice, this list of conditions and the following disclaimer.
102343Scsgr * 2. Redistributions in binary form must reproduce the above copyright
112343Scsgr *    notice, this list of conditions and the following disclaimer in the
122343Scsgr *    documentation and/or other materials provided with the distribution.
132343Scsgr *
142343Scsgr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152343Scsgr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162343Scsgr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172343Scsgr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182343Scsgr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192343Scsgr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202343Scsgr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212343Scsgr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222343Scsgr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232343Scsgr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242343Scsgr * SUCH DAMAGE.
252343Scsgr */
262343Scsgr
272343Scsgr#include <assert.h>
282343Scsgr#include <libelf.h>
292343Scsgr
302343Scsgr#include "_libelf.h"
312343Scsgr
322343ScsgrELFTC_VCSID("$Id$");
332343Scsgr
3495616Smarkm/*
352343Scsgr * A convenience helper that returns the ELF machine architecture for
3695616Smarkm * a ELF descriptor.
3795616Smarkm */
382343Scsgrint
392343Scsgr_libelf_elfmachine(Elf *e)
4095616Smarkm{
412343Scsgr	Elf32_Ehdr *eh32;
4227955Scharnier	Elf64_Ehdr *eh64;
4395616Smarkm
442343Scsgr	if (!e)
452343Scsgr		return EM_NONE;
4695616Smarkm
472343Scsgr	assert(e->e_kind == ELF_K_ELF);
482343Scsgr	assert(e->e_class != ELFCLASSNONE);
4995616Smarkm
5095616Smarkm	eh32 = NULL;
5195616Smarkm	eh64 = NULL;
5295616Smarkm
5395616Smarkm	switch (e->e_class) {
5495616Smarkm	case ELFCLASS32:
5595616Smarkm		eh32 = e->e_u.e_elf.e_ehdr.e_ehdr32;
5695616Smarkm		return eh32 ? eh32->e_machine : EM_NONE;
5795616Smarkm	case ELFCLASS64:
5895616Smarkm		eh64 = e->e_u.e_elf.e_ehdr.e_ehdr64;
592343Scsgr		return eh64 ? eh64->e_machine : EM_NONE;
602343Scsgr	}
612343Scsgr
622343Scsgr	return (EM_NONE);
632343Scsgr}
642343Scsgr