xref: /titanic_50/usr/src/cmd/sgs/libelf/common/getscn.c (revision 3db86aab554edbb4244c8d1a1c90f152eee768af)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 /*
26  * Copyright (c) 1998 by Sun Microsystems, Inc.
27  * All rights reserved.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.10	*/
31 
32 #pragma weak	elf_getscn = _elf_getscn
33 
34 
35 #include "syn.h"
36 #include "libelf.h"
37 #include "decl.h"
38 #include "msg.h"
39 
40 
41 Elf_Scn *
42 elf_getscn(Elf * elf, size_t index)
43 {
44 	Elf_Scn	*	s;
45 	Elf_Scn	*	prev_s;
46 	size_t		j = index;
47 	size_t		tabsz;
48 
49 	if (elf == 0)
50 		return (0);
51 
52 	ELFRLOCK(elf)
53 	tabsz = elf->ed_scntabsz;
54 	if (elf->ed_hdscn == 0) {
55 		ELFUNLOCK(elf)
56 		ELFWLOCK(elf)
57 		if ((elf->ed_hdscn == 0) && (_elf_cook(elf) != OK_YES)) {
58 			ELFUNLOCK(elf);
59 			return (0);
60 		}
61 		ELFUNLOCK(elf);
62 		ELFRLOCK(elf)
63 	}
64 	/*
65 	 * If the section in question is part of a table allocated
66 	 * from within _elf_prescn() then we can index straight
67 	 * to it.
68 	 */
69 	if (index < tabsz) {
70 		s = &elf->ed_hdscn[index];
71 		ELFUNLOCK(elf);
72 		return (s);
73 	}
74 
75 #ifndef	__lock_lint
76 	if (tabsz)
77 		s = &elf->ed_hdscn[tabsz - 1];
78 	else
79 		s = elf->ed_hdscn;
80 
81 	for (prev_s = 0; s != 0; prev_s = s, s = s->s_next) {
82 		if (prev_s) {
83 			SCNUNLOCK(prev_s)
84 		}
85 		SCNLOCK(s)
86 		if (j == 0) {
87 			if (s->s_index == index) {
88 				SCNUNLOCK(s)
89 				ELFUNLOCK(elf);
90 				return (s);
91 			}
92 			_elf_seterr(EBUG_SCNLIST, 0);
93 			SCNUNLOCK(s)
94 			ELFUNLOCK(elf)
95 			return (0);
96 		}
97 		--j;
98 	}
99 	if (prev_s) {
100 		SCNUNLOCK(prev_s)
101 	}
102 #endif
103 	_elf_seterr(EREQ_NDX, 0);
104 	ELFUNLOCK(elf);
105 	return (0);
106 }
107