elf_phnum.c revision 272461
1139735Simp/*- 2129198Scognet * Copyright (c) 2006 Joseph Koshy 3129198Scognet * All rights reserved. 4129198Scognet * 5129198Scognet * Redistribution and use in source and binary forms, with or without 6129198Scognet * modification, are permitted provided that the following conditions 7129198Scognet * are met: 8129198Scognet * 1. Redistributions of source code must retain the above copyright 9129198Scognet * notice, this list of conditions and the following disclaimer. 10129198Scognet * 2. Redistributions in binary form must reproduce the above copyright 11129198Scognet * notice, this list of conditions and the following disclaimer in the 12129198Scognet * documentation and/or other materials provided with the distribution. 13129198Scognet * 14129198Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15182934Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16129198Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17129198Scognet * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18129198Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19129198Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20129198Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24129198Scognet * SUCH DAMAGE. 25129198Scognet */ 26129198Scognet 27129198Scognet#include <sys/cdefs.h> 28129198Scognet__FBSDID("$FreeBSD: releng/10.1/lib/libelf/elf_phnum.c 210345 2010-07-21 12:14:50Z kaiw $"); 29129198Scognet 30129198Scognet#include <ar.h> 31129198Scognet#include <libelf.h> 32129198Scognet 33129198Scognet#include "_libelf.h" 34129198Scognet 35129198Scognetstatic int 36129198Scognet_libelf_getphdrnum(Elf *e, size_t *phnum) 37129198Scognet{ 38129198Scognet void *eh; 39129198Scognet int ec; 40129198Scognet 41129198Scognet if (e == NULL || e->e_kind != ELF_K_ELF || 42129198Scognet ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 43129198Scognet LIBELF_SET_ERROR(ARGUMENT, 0); 44129198Scognet return (-1); 45129198Scognet } 46129198Scognet 47129198Scognet if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) 48129198Scognet return (-1); 49129198Scognet 50129198Scognet *phnum = e->e_u.e_elf.e_nphdr; 51129198Scognet 52129198Scognet return (0); 53129198Scognet} 54129198Scognet 55129198Scognetint 56129198Scognetelf_getphdrnum(Elf *e, size_t *phnum) 57129198Scognet{ 58129198Scognet return (_libelf_getphdrnum(e, phnum)); 59129198Scognet} 60129198Scognet 61129198Scognet/* Deprecated API */ 62129198Scognetint 63129198Scognetelf_getphnum(Elf *e, size_t *phnum) 64129198Scognet{ 65129198Scognet return (_libelf_getphdrnum(e, phnum) >= 0); 66129198Scognet} 67129198Scognet