8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / libelf / common / getphnum.c
blob42d2a1769e55f161f87ccc6bf4f4bf5a179a6117
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <string.h>
27 #include <gelf.h>
28 #include <decl.h>
29 #include <msg.h>
32 * Return number of entries in the program header array, taking
33 * extended headers into account.
35 * elf_getphnum() was defined based on the definition of the earlier
36 * elf_getshnum(). It returns 0 for failure, and 1 for success.
38 * elf_getphdrnum() supercedes elf_getphnum(), which is now considered
39 * obsolete. It returns -1 for failure and 0 for success, matching
40 * elf_getshdrnum(), and bringing us into alignment with the interface
41 * agreed to in the 2002 gABI meeting.
43 * See the comment in getshnum.c for additional information.
46 int
47 elf_getphdrnum(Elf *elf, size_t *phnum)
49 GElf_Ehdr ehdr;
50 Elf_Scn *scn;
51 GElf_Shdr shdr0;
53 if (gelf_getehdr(elf, &ehdr) == NULL)
54 return (-1);
56 if (ehdr.e_phnum != PN_XNUM) {
57 *phnum = ehdr.e_phnum;
58 return (0);
61 if ((scn = elf_getscn(elf, 0)) == NULL ||
62 gelf_getshdr(scn, &shdr0) == NULL)
63 return (-1);
65 if (shdr0.sh_info == 0)
66 *phnum = ehdr.e_phnum;
67 else
68 *phnum = shdr0.sh_info;
70 return (0);
73 int
74 elf_getphnum(Elf *elf, size_t *phnum)
76 return (elf_getphdrnum(elf, phnum) == 0);