elf_shnum.c revision 260697
1261057Smav/*- 2261057Smav * Copyright (c) 2006,2008 Joseph Koshy 3261057Smav * All rights reserved. 4261057Smav * 5261057Smav * Redistribution and use in source and binary forms, with or without 6261057Smav * modification, are permitted provided that the following conditions 7261057Smav * are met: 8261057Smav * 1. Redistributions of source code must retain the above copyright 9261057Smav * notice, this list of conditions and the following disclaimer. 10261057Smav * 2. Redistributions in binary form must reproduce the above copyright 11261057Smav * notice, this list of conditions and the following disclaimer in the 12261057Smav * documentation and/or other materials provided with the distribution. 13261057Smav * 14261057Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1526219Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16261057Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17261057Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18261057Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19261057Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20261057Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21261057Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22261057Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23261057Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24261057Smav * SUCH DAMAGE. 25261057Smav */ 26261057Smav 2726219Swpaul#include <sys/cdefs.h> 2826219Swpaul 2926219Swpaul#include <ar.h> 3026219Swpaul#include <libelf.h> 3126219Swpaul 3226219Swpaul#include "_libelf.h" 3326219Swpaul 3492990SobrienELFTC_VCSID("$Id: elf_shnum.c 2225 2011-11-26 18:55:54Z jkoshy $"); 3575094Siedowse 3674462Salfredstatic int 3774462Salfred_libelf_getshdrnum(Elf *e, size_t *shnum) 3874462Salfred{ 3926219Swpaul void *eh; 4026219Swpaul int ec; 4126219Swpaul 4226219Swpaul if (e == NULL || e->e_kind != ELF_K_ELF || 4326219Swpaul ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { 4474462Salfred LIBELF_SET_ERROR(ARGUMENT, 0); 4526219Swpaul return (-1); 4626219Swpaul } 4726219Swpaul 4874462Salfred if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) 4974462Salfred return (-1); 5026219Swpaul 5126219Swpaul *shnum = e->e_u.e_elf.e_nscn; 5226219Swpaul 5374462Salfred return (0); 54156090Sdeischen} 5526219Swpaul 5626219Swpaulint 57136581Sobrienelf_getshdrnum(Elf *e, size_t *shnum) 5826219Swpaul{ 5992990Sobrien return (_libelf_getshdrnum(e, shnum)); 6092990Sobrien} 6126219Swpaul 6274462Salfred/* Deprecated API. */ 6374462Salfredint 6426219Swpaulelf_getshnum(Elf *e, size_t *shnum) 6526219Swpaul{ 6626219Swpaul return (_libelf_getshdrnum(e, shnum) >= 0); 6726219Swpaul} 6826219Swpaul