119370Spst/*-
219370Spst * Copyright (c) 2018 Joseph Koshy
319370Spst * All rights reserved.
419370Spst *
519370Spst * Redistribution and use in source and binary forms, with or without
619370Spst * modification, are permitted provided that the following conditions
719370Spst * are met:
819370Spst * 1. Redistributions of source code must retain the above copyright
919370Spst *    notice, this list of conditions and the following disclaimer.
1019370Spst * 2. Redistributions in binary form must reproduce the above copyright
1119370Spst *    notice, this list of conditions and the following disclaimer in the
1219370Spst *    documentation and/or other materials provided with the distribution.
1319370Spst *
1419370Spst * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1519370Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1619370Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1719370Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1819370Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1919370Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2019370Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2119370Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2219370Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2319370Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2419370Spst * SUCH DAMAGE.
2519370Spst */
2619370Spst
2719370Spst#include <assert.h>
2819370Spst#include <libelf.h>
2919370Spst
3019370Spst#include "_libelf.h"
3119370Spst
3219370SpstELFTC_VCSID("$Id$");
3319370Spst
3419370Spst/*
3519370Spst * A convenience helper that returns the ELF machine architecture for
3619370Spst * a ELF descriptor.
3719370Spst */
3819370Spstint
3919370Spst_libelf_elfmachine(Elf *e)
4019370Spst{
4119370Spst	Elf32_Ehdr *eh32;
4219370Spst	Elf64_Ehdr *eh64;
4319370Spst
4419370Spst	if (!e)
4519370Spst		return EM_NONE;
4619370Spst
4719370Spst	assert(e->e_kind == ELF_K_ELF);
4819370Spst	assert(e->e_class != ELFCLASSNONE);
4919370Spst
5019370Spst	eh32 = NULL;
5119370Spst	eh64 = NULL;
5219370Spst
5319370Spst	switch (e->e_class) {
5419370Spst	case ELFCLASS32:
5519370Spst		eh32 = e->e_u.e_elf.e_ehdr.e_ehdr32;
5619370Spst		return eh32 ? eh32->e_machine : EM_NONE;
5719370Spst	case ELFCLASS64:
5819370Spst		eh64 = e->e_u.e_elf.e_ehdr.e_ehdr64;
5919370Spst		return eh64 ? eh64->e_machine : EM_NONE;
6019370Spst	}
6119370Spst
6219370Spst	return (EM_NONE);
6319370Spst}
6419370Spst