xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/getphnum.c (revision 942:8cf53d6a7d2e)
1*942Sahl /*
2*942Sahl  * CDDL HEADER START
3*942Sahl  *
4*942Sahl  * The contents of this file are subject to the terms of the
5*942Sahl  * Common Development and Distribution License, Version 1.0 only
6*942Sahl  * (the "License").  You may not use this file except in compliance
7*942Sahl  * with the License.
8*942Sahl  *
9*942Sahl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*942Sahl  * or http://www.opensolaris.org/os/licensing.
11*942Sahl  * See the License for the specific language governing permissions
12*942Sahl  * and limitations under the License.
13*942Sahl  *
14*942Sahl  * When distributing Covered Code, include this CDDL HEADER in each
15*942Sahl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*942Sahl  * If applicable, add the following below this CDDL HEADER, with the
17*942Sahl  * fields enclosed by brackets "[]" replaced with your own identifying
18*942Sahl  * information: Portions Copyright [yyyy] [name of copyright owner]
19*942Sahl  *
20*942Sahl  * CDDL HEADER END
21*942Sahl  */
22*942Sahl /*
23*942Sahl  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*942Sahl  * Use is subject to license terms.
25*942Sahl  */
26*942Sahl 
27*942Sahl #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*942Sahl 
29*942Sahl #include <string.h>
30*942Sahl #include <gelf.h>
31*942Sahl #include <decl.h>
32*942Sahl #include <msg.h>
33*942Sahl 
34*942Sahl int
35*942Sahl elf_getphnum(Elf *elf, size_t *phnum)
36*942Sahl {
37*942Sahl 	GElf_Ehdr	ehdr;
38*942Sahl 	Elf_Scn		*scn;
39*942Sahl 	GElf_Shdr	shdr0;
40*942Sahl 
41*942Sahl 	if (gelf_getehdr(elf, &ehdr) == NULL)
42*942Sahl 		return (0);
43*942Sahl 
44*942Sahl 	if (ehdr.e_phnum != PN_XNUM) {
45*942Sahl 		*phnum = ehdr.e_phnum;
46*942Sahl 		return (1);
47*942Sahl 	}
48*942Sahl 
49*942Sahl 	if ((scn = elf_getscn(elf, 0)) == NULL ||
50*942Sahl 	    gelf_getshdr(scn, &shdr0) == NULL)
51*942Sahl 		return (0);
52*942Sahl 
53*942Sahl 	if (shdr0.sh_info == 0)
54*942Sahl 		*phnum = ehdr.e_phnum;
55*942Sahl 	else
56*942Sahl 		*phnum = shdr0.sh_info;
57*942Sahl 
58*942Sahl 	return (1);
59*942Sahl }
60