getphnum.c revision 30da143285931291f495cc20b5a1b8869f0618a6
30da143285931291f495cc20b5a1b8869f0618a6ahl/*
30da143285931291f495cc20b5a1b8869f0618a6ahl * CDDL HEADER START
30da143285931291f495cc20b5a1b8869f0618a6ahl *
30da143285931291f495cc20b5a1b8869f0618a6ahl * The contents of this file are subject to the terms of the
30da143285931291f495cc20b5a1b8869f0618a6ahl * Common Development and Distribution License, Version 1.0 only
30da143285931291f495cc20b5a1b8869f0618a6ahl * (the "License"). You may not use this file except in compliance
30da143285931291f495cc20b5a1b8869f0618a6ahl * with the License.
30da143285931291f495cc20b5a1b8869f0618a6ahl *
30da143285931291f495cc20b5a1b8869f0618a6ahl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
30da143285931291f495cc20b5a1b8869f0618a6ahl * or http://www.opensolaris.org/os/licensing.
30da143285931291f495cc20b5a1b8869f0618a6ahl * See the License for the specific language governing permissions
30da143285931291f495cc20b5a1b8869f0618a6ahl * and limitations under the License.
30da143285931291f495cc20b5a1b8869f0618a6ahl *
30da143285931291f495cc20b5a1b8869f0618a6ahl * When distributing Covered Code, include this CDDL HEADER in each
30da143285931291f495cc20b5a1b8869f0618a6ahl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
30da143285931291f495cc20b5a1b8869f0618a6ahl * If applicable, add the following below this CDDL HEADER, with the
30da143285931291f495cc20b5a1b8869f0618a6ahl * fields enclosed by brackets "[]" replaced with your own identifying
30da143285931291f495cc20b5a1b8869f0618a6ahl * information: Portions Copyright [yyyy] [name of copyright owner]
30da143285931291f495cc20b5a1b8869f0618a6ahl *
30da143285931291f495cc20b5a1b8869f0618a6ahl * CDDL HEADER END
30da143285931291f495cc20b5a1b8869f0618a6ahl */
30da143285931291f495cc20b5a1b8869f0618a6ahl/*
30da143285931291f495cc20b5a1b8869f0618a6ahl * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
30da143285931291f495cc20b5a1b8869f0618a6ahl * Use is subject to license terms.
30da143285931291f495cc20b5a1b8869f0618a6ahl */
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahl#pragma ident "%Z%%M% %I% %E% SMI"
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahl#include <string.h>
30da143285931291f495cc20b5a1b8869f0618a6ahl#include <gelf.h>
30da143285931291f495cc20b5a1b8869f0618a6ahl#include <decl.h>
30da143285931291f495cc20b5a1b8869f0618a6ahl#include <msg.h>
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahlint
30da143285931291f495cc20b5a1b8869f0618a6ahlelf_getphnum(Elf *elf, size_t *phnum)
30da143285931291f495cc20b5a1b8869f0618a6ahl{
30da143285931291f495cc20b5a1b8869f0618a6ahl GElf_Ehdr ehdr;
30da143285931291f495cc20b5a1b8869f0618a6ahl Elf_Scn *scn;
30da143285931291f495cc20b5a1b8869f0618a6ahl GElf_Shdr shdr0;
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahl if (gelf_getehdr(elf, &ehdr) == NULL)
30da143285931291f495cc20b5a1b8869f0618a6ahl return (0);
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahl if (ehdr.e_phnum != PN_XNUM) {
30da143285931291f495cc20b5a1b8869f0618a6ahl *phnum = ehdr.e_phnum;
30da143285931291f495cc20b5a1b8869f0618a6ahl return (1);
30da143285931291f495cc20b5a1b8869f0618a6ahl }
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahl if ((scn = elf_getscn(elf, 0)) == NULL ||
30da143285931291f495cc20b5a1b8869f0618a6ahl gelf_getshdr(scn, &shdr0) == NULL)
30da143285931291f495cc20b5a1b8869f0618a6ahl return (0);
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahl if (shdr0.sh_info == 0)
30da143285931291f495cc20b5a1b8869f0618a6ahl *phnum = ehdr.e_phnum;
30da143285931291f495cc20b5a1b8869f0618a6ahl else
30da143285931291f495cc20b5a1b8869f0618a6ahl *phnum = shdr0.sh_info;
30da143285931291f495cc20b5a1b8869f0618a6ahl
30da143285931291f495cc20b5a1b8869f0618a6ahl return (1);
30da143285931291f495cc20b5a1b8869f0618a6ahl}