xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/newscn.c (revision 6812:febeba71273d)
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*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * 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  */
21*6812Sraf 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "libelf.h"
330Sstevel@tonic-gate #include "decl.h"
340Sstevel@tonic-gate #include "msg.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate Elf_Scn *
elf_newscn(Elf * elf)380Sstevel@tonic-gate elf_newscn(Elf * elf)
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	Elf_Scn	*	tl;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 	if (elf == 0)
430Sstevel@tonic-gate 		return (0);
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	ELFWLOCK(elf)
460Sstevel@tonic-gate 	/*
470Sstevel@tonic-gate 	 * if no sections yet, the file either isn't cooked
480Sstevel@tonic-gate 	 * or it truly is empty.  Then allocate shdr[0]
490Sstevel@tonic-gate 	 */
500Sstevel@tonic-gate 	if ((elf->ed_hdscn == 0) && (_elf_cook(elf) != OK_YES)) {
510Sstevel@tonic-gate 		ELFUNLOCK(elf)
520Sstevel@tonic-gate 		return (0);
530Sstevel@tonic-gate 	}
540Sstevel@tonic-gate 	if (elf->ed_ehdr == 0) {
550Sstevel@tonic-gate 		_elf_seterr(ESEQ_EHDR, 0);
560Sstevel@tonic-gate 		ELFUNLOCK(elf)
570Sstevel@tonic-gate 		return (0);
580Sstevel@tonic-gate 	}
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	if (elf->ed_class == ELFCLASS32) {
610Sstevel@tonic-gate 		Snode32	*s;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 		if (elf->ed_hdscn == 0)	{
640Sstevel@tonic-gate 			if ((s = _elf32_snode()) == 0) {
650Sstevel@tonic-gate 				ELFUNLOCK(elf)
660Sstevel@tonic-gate 				return (0);
670Sstevel@tonic-gate 			}
680Sstevel@tonic-gate 			NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*s))
690Sstevel@tonic-gate 			s->sb_scn.s_elf = elf;
700Sstevel@tonic-gate 			elf->ed_hdscn = elf->ed_tlscn = &s->sb_scn;
710Sstevel@tonic-gate 			s->sb_scn.s_uflags |= ELF_F_DIRTY;
720Sstevel@tonic-gate 		}
730Sstevel@tonic-gate 		if ((s = _elf32_snode()) == 0) {
740Sstevel@tonic-gate 			ELFUNLOCK(elf)
750Sstevel@tonic-gate 			return (0);
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 		NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*s))
780Sstevel@tonic-gate 		tl = elf->ed_tlscn;
790Sstevel@tonic-gate 		s->sb_scn.s_elf = elf;
800Sstevel@tonic-gate 		s->sb_scn.s_index = tl->s_index + 1;
810Sstevel@tonic-gate 		elf->ed_tlscn = tl->s_next = &s->sb_scn;
820Sstevel@tonic-gate 		((Elf32_Ehdr *)elf->ed_ehdr)->e_shnum
830Sstevel@tonic-gate 		    /* LINTED */
840Sstevel@tonic-gate 		    = (Elf32_Half)(tl->s_index + 2);
850Sstevel@tonic-gate 		s->sb_scn.s_uflags |= ELF_F_DIRTY;
860Sstevel@tonic-gate 		tl = &s->sb_scn;
870Sstevel@tonic-gate 		NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*s))
880Sstevel@tonic-gate 		ELFUNLOCK(elf)
890Sstevel@tonic-gate 		return (tl);
900Sstevel@tonic-gate 	} else if (elf->ed_class == ELFCLASS64) {
910Sstevel@tonic-gate 		Snode64	*s;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		if (elf->ed_hdscn == 0)	{
940Sstevel@tonic-gate 			if ((s = _elf64_snode()) == 0) {
950Sstevel@tonic-gate 				ELFUNLOCK(elf)
960Sstevel@tonic-gate 				return (0);
970Sstevel@tonic-gate 			}
980Sstevel@tonic-gate 			NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*s))
990Sstevel@tonic-gate 			s->sb_scn.s_elf = elf;
1000Sstevel@tonic-gate 			elf->ed_hdscn = elf->ed_tlscn = &s->sb_scn;
1010Sstevel@tonic-gate 			s->sb_scn.s_uflags |= ELF_F_DIRTY;
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 		if ((s = _elf64_snode()) == 0) {
1040Sstevel@tonic-gate 			ELFUNLOCK(elf)
1050Sstevel@tonic-gate 			return (0);
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 		NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*s))
1080Sstevel@tonic-gate 		tl = elf->ed_tlscn;
1090Sstevel@tonic-gate 		s->sb_scn.s_elf = elf;
1100Sstevel@tonic-gate 		s->sb_scn.s_index = tl->s_index + 1;
1110Sstevel@tonic-gate 		elf->ed_tlscn = tl->s_next = &s->sb_scn;
1120Sstevel@tonic-gate 		((Elf64_Ehdr *)elf->ed_ehdr)->e_shnum
1130Sstevel@tonic-gate 		    /* LINTED */
1140Sstevel@tonic-gate 		    = (Elf64_Half)(tl->s_index + 2);
1150Sstevel@tonic-gate 		s->sb_scn.s_uflags |= ELF_F_DIRTY;
1160Sstevel@tonic-gate 		tl = &s->sb_scn;
1170Sstevel@tonic-gate 		NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*s))
1180Sstevel@tonic-gate 		ELFUNLOCK(elf)
1190Sstevel@tonic-gate 		return (tl);
1200Sstevel@tonic-gate 	} else {
1210Sstevel@tonic-gate 		_elf_seterr(EREQ_CLASS, 0);
1220Sstevel@tonic-gate 		ELFUNLOCK(elf)
1230Sstevel@tonic-gate 		return (0);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate }
126