xref: /titanic_44/usr/src/cmd/sgs/libelf/common/getphnum.c (revision 62b628a68db596a2d75a316dc7ffef658079231f)
130da1432Sahl /*
230da1432Sahl  * CDDL HEADER START
330da1432Sahl  *
430da1432Sahl  * The contents of this file are subject to the terms of the
5*62b628a6SAli Bahrami  * Common Development and Distribution License (the "License").
6*62b628a6SAli Bahrami  * You may not use this file except in compliance with the License.
730da1432Sahl  *
830da1432Sahl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
930da1432Sahl  * or http://www.opensolaris.org/os/licensing.
1030da1432Sahl  * See the License for the specific language governing permissions
1130da1432Sahl  * and limitations under the License.
1230da1432Sahl  *
1330da1432Sahl  * When distributing Covered Code, include this CDDL HEADER in each
1430da1432Sahl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1530da1432Sahl  * If applicable, add the following below this CDDL HEADER, with the
1630da1432Sahl  * fields enclosed by brackets "[]" replaced with your own identifying
1730da1432Sahl  * information: Portions Copyright [yyyy] [name of copyright owner]
1830da1432Sahl  *
1930da1432Sahl  * CDDL HEADER END
2030da1432Sahl  */
2130da1432Sahl /*
22*62b628a6SAli Bahrami  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
2330da1432Sahl  * Use is subject to license terms.
2430da1432Sahl  */
2530da1432Sahl 
2630da1432Sahl #include <string.h>
2730da1432Sahl #include <gelf.h>
2830da1432Sahl #include <decl.h>
2930da1432Sahl #include <msg.h>
3030da1432Sahl 
31*62b628a6SAli Bahrami /*
32*62b628a6SAli Bahrami  * Return number of entries in the program header array, taking
33*62b628a6SAli Bahrami  * extended headers into account.
34*62b628a6SAli Bahrami  *
35*62b628a6SAli Bahrami  * elf_getphnum() was defined based on the definition of the earlier
36*62b628a6SAli Bahrami  * elf_getshnum(). It returns 0 for failure, and 1 for success.
37*62b628a6SAli Bahrami  *
38*62b628a6SAli Bahrami  * elf_getphdrnum() supercedes elf_getphnum(), which is now considered
39*62b628a6SAli Bahrami  * obsolete. It returns -1 for failure and 0 for success, matching
40*62b628a6SAli Bahrami  * elf_getshdrnum(), and bringing us into alignment with the interface
41*62b628a6SAli Bahrami  * agreed to in the 2002 gABI meeting.
42*62b628a6SAli Bahrami  *
43*62b628a6SAli Bahrami  * See the comment in getshnum.c for additional information.
44*62b628a6SAli Bahrami  */
45*62b628a6SAli Bahrami 
4630da1432Sahl int
elf_getphdrnum(Elf * elf,size_t * phnum)47*62b628a6SAli Bahrami elf_getphdrnum(Elf *elf, size_t *phnum)
4830da1432Sahl {
4930da1432Sahl 	GElf_Ehdr	ehdr;
5030da1432Sahl 	Elf_Scn		*scn;
5130da1432Sahl 	GElf_Shdr	shdr0;
5230da1432Sahl 
5330da1432Sahl 	if (gelf_getehdr(elf, &ehdr) == NULL)
54*62b628a6SAli Bahrami 		return (-1);
5530da1432Sahl 
5630da1432Sahl 	if (ehdr.e_phnum != PN_XNUM) {
5730da1432Sahl 		*phnum = ehdr.e_phnum;
58*62b628a6SAli Bahrami 		return (0);
5930da1432Sahl 	}
6030da1432Sahl 
6130da1432Sahl 	if ((scn = elf_getscn(elf, 0)) == NULL ||
6230da1432Sahl 	    gelf_getshdr(scn, &shdr0) == NULL)
63*62b628a6SAli Bahrami 		return (-1);
6430da1432Sahl 
6530da1432Sahl 	if (shdr0.sh_info == 0)
6630da1432Sahl 		*phnum = ehdr.e_phnum;
6730da1432Sahl 	else
6830da1432Sahl 		*phnum = shdr0.sh_info;
6930da1432Sahl 
70*62b628a6SAli Bahrami 	return (0);
71*62b628a6SAli Bahrami }
72*62b628a6SAli Bahrami 
73*62b628a6SAli Bahrami int
elf_getphnum(Elf * elf,size_t * phnum)74*62b628a6SAli Bahrami elf_getphnum(Elf *elf, size_t *phnum)
75*62b628a6SAli Bahrami {
76*62b628a6SAli Bahrami 	return (elf_getphdrnum(elf, phnum) == 0);
7730da1432Sahl }
78