xref: /titanic_50/usr/src/cmd/sgs/libelf/common/getscn.c (revision bdb9230ac765cb7af3fc1f4119caf2c5720dceb3)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "libelf.h"
33 #include "decl.h"
34 #include "msg.h"
35 
36 
37 Elf_Scn *
38 elf_getscn(Elf * elf, size_t index)
39 {
40 	Elf_Scn	*	s;
41 	Elf_Scn	*	prev_s;
42 	size_t		j = index;
43 	size_t		tabsz;
44 
45 	if (elf == 0)
46 		return (0);
47 
48 	ELFRLOCK(elf)
49 	tabsz = elf->ed_scntabsz;
50 	if (elf->ed_hdscn == 0) {
51 		ELFUNLOCK(elf)
52 		ELFWLOCK(elf)
53 		if ((elf->ed_hdscn == 0) && (_elf_cook(elf) != OK_YES)) {
54 			ELFUNLOCK(elf);
55 			return (0);
56 		}
57 		ELFUNLOCK(elf);
58 		ELFRLOCK(elf)
59 	}
60 	/*
61 	 * If the section in question is part of a table allocated
62 	 * from within _elf_prescn() then we can index straight
63 	 * to it.
64 	 */
65 	if (index < tabsz) {
66 		s = &elf->ed_hdscn[index];
67 		ELFUNLOCK(elf);
68 		return (s);
69 	}
70 
71 #ifndef	__lock_lint
72 	if (tabsz)
73 		s = &elf->ed_hdscn[tabsz - 1];
74 	else
75 		s = elf->ed_hdscn;
76 
77 	for (prev_s = 0; s != 0; prev_s = s, s = s->s_next) {
78 		if (prev_s) {
79 			SCNUNLOCK(prev_s)
80 		}
81 		SCNLOCK(s)
82 		if (j == 0) {
83 			if (s->s_index == index) {
84 				SCNUNLOCK(s)
85 				ELFUNLOCK(elf);
86 				return (s);
87 			}
88 			_elf_seterr(EBUG_SCNLIST, 0);
89 			SCNUNLOCK(s)
90 			ELFUNLOCK(elf)
91 			return (0);
92 		}
93 		--j;
94 	}
95 	if (prev_s) {
96 		SCNUNLOCK(prev_s)
97 	}
98 #endif
99 	_elf_seterr(EREQ_NDX, 0);
100 	ELFUNLOCK(elf);
101 	return (0);
102 }
103