xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/getphnum.c (revision 9900:1b86d65a4f9e)
1942Sahl /*
2942Sahl  * CDDL HEADER START
3942Sahl  *
4942Sahl  * The contents of this file are subject to the terms of the
5*9900SAli.Bahrami@Sun.COM  * Common Development and Distribution License (the "License").
6*9900SAli.Bahrami@Sun.COM  * You may not use this file except in compliance with the License.
7942Sahl  *
8942Sahl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9942Sahl  * or http://www.opensolaris.org/os/licensing.
10942Sahl  * See the License for the specific language governing permissions
11942Sahl  * and limitations under the License.
12942Sahl  *
13942Sahl  * When distributing Covered Code, include this CDDL HEADER in each
14942Sahl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15942Sahl  * If applicable, add the following below this CDDL HEADER, with the
16942Sahl  * fields enclosed by brackets "[]" replaced with your own identifying
17942Sahl  * information: Portions Copyright [yyyy] [name of copyright owner]
18942Sahl  *
19942Sahl  * CDDL HEADER END
20942Sahl  */
21942Sahl /*
22*9900SAli.Bahrami@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23942Sahl  * Use is subject to license terms.
24942Sahl  */
25942Sahl 
26942Sahl #include <string.h>
27942Sahl #include <gelf.h>
28942Sahl #include <decl.h>
29942Sahl #include <msg.h>
30942Sahl 
31*9900SAli.Bahrami@Sun.COM /*
32*9900SAli.Bahrami@Sun.COM  * Return number of entries in the program header array, taking
33*9900SAli.Bahrami@Sun.COM  * extended headers into account.
34*9900SAli.Bahrami@Sun.COM  *
35*9900SAli.Bahrami@Sun.COM  * elf_getphnum() was defined based on the definition of the earlier
36*9900SAli.Bahrami@Sun.COM  * elf_getshnum(). It returns 0 for failure, and 1 for success.
37*9900SAli.Bahrami@Sun.COM  *
38*9900SAli.Bahrami@Sun.COM  * elf_getphdrnum() supercedes elf_getphnum(), which is now considered
39*9900SAli.Bahrami@Sun.COM  * obsolete. It returns -1 for failure and 0 for success, matching
40*9900SAli.Bahrami@Sun.COM  * elf_getshdrnum(), and bringing us into alignment with the interface
41*9900SAli.Bahrami@Sun.COM  * agreed to in the 2002 gABI meeting.
42*9900SAli.Bahrami@Sun.COM  *
43*9900SAli.Bahrami@Sun.COM  * See the comment in getshnum.c for additional information.
44*9900SAli.Bahrami@Sun.COM  */
45*9900SAli.Bahrami@Sun.COM 
46942Sahl int
elf_getphdrnum(Elf * elf,size_t * phnum)47*9900SAli.Bahrami@Sun.COM elf_getphdrnum(Elf *elf, size_t *phnum)
48942Sahl {
49942Sahl 	GElf_Ehdr	ehdr;
50942Sahl 	Elf_Scn		*scn;
51942Sahl 	GElf_Shdr	shdr0;
52942Sahl 
53942Sahl 	if (gelf_getehdr(elf, &ehdr) == NULL)
54*9900SAli.Bahrami@Sun.COM 		return (-1);
55942Sahl 
56942Sahl 	if (ehdr.e_phnum != PN_XNUM) {
57942Sahl 		*phnum = ehdr.e_phnum;
58*9900SAli.Bahrami@Sun.COM 		return (0);
59942Sahl 	}
60942Sahl 
61942Sahl 	if ((scn = elf_getscn(elf, 0)) == NULL ||
62942Sahl 	    gelf_getshdr(scn, &shdr0) == NULL)
63*9900SAli.Bahrami@Sun.COM 		return (-1);
64942Sahl 
65942Sahl 	if (shdr0.sh_info == 0)
66942Sahl 		*phnum = ehdr.e_phnum;
67942Sahl 	else
68942Sahl 		*phnum = shdr0.sh_info;
69942Sahl 
70*9900SAli.Bahrami@Sun.COM 	return (0);
71942Sahl }
72*9900SAli.Bahrami@Sun.COM 
73*9900SAli.Bahrami@Sun.COM int
elf_getphnum(Elf * elf,size_t * phnum)74*9900SAli.Bahrami@Sun.COM elf_getphnum(Elf *elf, size_t *phnum)
75*9900SAli.Bahrami@Sun.COM {
76*9900SAli.Bahrami@Sun.COM 	return (elf_getphdrnum(elf, phnum) == 0);
77*9900SAli.Bahrami@Sun.COM }
78