xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/getshnum.c (revision 9900:1b86d65a4f9e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * 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.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9900SAli.Bahrami@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <string.h>
270Sstevel@tonic-gate #include <gelf.h>
280Sstevel@tonic-gate #include <decl.h>
290Sstevel@tonic-gate #include <msg.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
32*9900SAli.Bahrami@Sun.COM /*
33*9900SAli.Bahrami@Sun.COM  * Return number of entries in the section header array, taking
34*9900SAli.Bahrami@Sun.COM  * extended headers into account.
35*9900SAli.Bahrami@Sun.COM  *
36*9900SAli.Bahrami@Sun.COM  * elf_getshnum() and elf_getshstrndx() were defined during the 2002 gABI
37*9900SAli.Bahrami@Sun.COM  * meetings. They were supposed to return -1 for failure, and 0 for success.
38*9900SAli.Bahrami@Sun.COM  * Our manpage documented them as such, but we then implemented them to
39*9900SAli.Bahrami@Sun.COM  * return 0 for failure and 1 for success. This makes elf_getshnum() and
40*9900SAli.Bahrami@Sun.COM  * elf_getshstrnum() non-portable to systems that implement the 2002 gABI
41*9900SAli.Bahrami@Sun.COM  * definition.
42*9900SAli.Bahrami@Sun.COM  *
43*9900SAli.Bahrami@Sun.COM  * In 2005, the manpage was modified to match the code.
44*9900SAli.Bahrami@Sun.COM  * In 2009, the discrepency was identified. elf_getshdrnum() and
45*9900SAli.Bahrami@Sun.COM  * elf_getshdrstrndx() were created to provide a portable implementation.
46*9900SAli.Bahrami@Sun.COM  * The older two functions are considered to be obsolete, and are retained
47*9900SAli.Bahrami@Sun.COM  * for backward compatability.
48*9900SAli.Bahrami@Sun.COM  */
49*9900SAli.Bahrami@Sun.COM 
500Sstevel@tonic-gate int
elf_getshdrnum(Elf * elf,size_t * shnum)51*9900SAli.Bahrami@Sun.COM elf_getshdrnum(Elf *elf, size_t *shnum)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	GElf_Ehdr	ehdr;
540Sstevel@tonic-gate 	Elf_Scn		*scn;
550Sstevel@tonic-gate 	GElf_Shdr	shdr0;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == 0)
58*9900SAli.Bahrami@Sun.COM 		return (-1);
590Sstevel@tonic-gate 	if (ehdr.e_shnum > 0) {
600Sstevel@tonic-gate 		*shnum = ehdr.e_shnum;
61*9900SAli.Bahrami@Sun.COM 		return (0);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 	if ((ehdr.e_shnum == 0) && (ehdr.e_shoff == 0)) {
640Sstevel@tonic-gate 		*shnum = 0;
65*9900SAli.Bahrami@Sun.COM 		return (0);
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate 	if ((scn = elf_getscn(elf, 0)) == 0)
68*9900SAli.Bahrami@Sun.COM 		return (-1);
690Sstevel@tonic-gate 	if (gelf_getshdr(scn, &shdr0) == 0)
70*9900SAli.Bahrami@Sun.COM 		return (-1);
710Sstevel@tonic-gate 	*shnum = shdr0.sh_size;
72*9900SAli.Bahrami@Sun.COM 	return (0);
730Sstevel@tonic-gate }
74*9900SAli.Bahrami@Sun.COM 
75*9900SAli.Bahrami@Sun.COM int
elf_getshnum(Elf * elf,size_t * shnum)76*9900SAli.Bahrami@Sun.COM elf_getshnum(Elf *elf, size_t *shnum)
77*9900SAli.Bahrami@Sun.COM {
78*9900SAli.Bahrami@Sun.COM 	return (elf_getshdrnum(elf, shnum) == 0);
79*9900SAli.Bahrami@Sun.COM }
80