xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/rawdata.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.3	*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma weak	elf_rawdata = _elf_rawdata
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "syn.h"
38*0Sstevel@tonic-gate #include <stdlib.h>
39*0Sstevel@tonic-gate #include "libelf.h"
40*0Sstevel@tonic-gate #include "decl.h"
41*0Sstevel@tonic-gate #include "msg.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate Elf_Data *
45*0Sstevel@tonic-gate elf_rawdata(Elf_Scn * scn, Elf_Data * data)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	Dnode *		d = (Dnode *)data;
48*0Sstevel@tonic-gate 	Dnode *		raw;
49*0Sstevel@tonic-gate 	Elf_Data *	rc;
50*0Sstevel@tonic-gate 	Elf *		elf;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	if (scn == 0)
53*0Sstevel@tonic-gate 		return (0);
54*0Sstevel@tonic-gate 	elf = scn->s_elf;
55*0Sstevel@tonic-gate 	READLOCKS(elf, scn)
56*0Sstevel@tonic-gate 	if ((scn->s_myflags & SF_READY) == 0) {
57*0Sstevel@tonic-gate 		UPGRADELOCKS(elf, scn)
58*0Sstevel@tonic-gate 		if ((scn->s_myflags & SF_READY) == 0)
59*0Sstevel@tonic-gate 			(void) _elf_cookscn(scn);
60*0Sstevel@tonic-gate 		DOWNGRADELOCKS(elf, scn)
61*0Sstevel@tonic-gate 	}
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	if (d == 0)
64*0Sstevel@tonic-gate 		d = scn->s_hdnode;
65*0Sstevel@tonic-gate 	else
66*0Sstevel@tonic-gate 		d = d->db_next;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	if (d == 0) {
69*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
70*0Sstevel@tonic-gate 		return (0);
71*0Sstevel@tonic-gate 	}
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	if (d->db_scn != scn) {
74*0Sstevel@tonic-gate 		_elf_seterr(EREQ_DATA, 0);
75*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
76*0Sstevel@tonic-gate 		return (0);
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	/*
80*0Sstevel@tonic-gate 	 * The data may come from a previously constructed Dbuf,
81*0Sstevel@tonic-gate 	 * from the file's raw memory image, or the file system.
82*0Sstevel@tonic-gate 	 * "Empty" regions get an empty buffer.
83*0Sstevel@tonic-gate 	 */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	if (d->db_raw != 0) {
86*0Sstevel@tonic-gate 		rc = &d->db_raw->db_data;
87*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
88*0Sstevel@tonic-gate 		return (rc);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if ((raw = _elf_dnode()) == 0)  {
92*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
93*0Sstevel@tonic-gate 		return (0);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 	raw->db_myflags |= DBF_READY;
96*0Sstevel@tonic-gate 	if ((d->db_off == 0) || (d->db_fsz == 0)) {
97*0Sstevel@tonic-gate 		d->db_raw = raw;
98*0Sstevel@tonic-gate 		raw->db_data.d_size = d->db_shsz;
99*0Sstevel@tonic-gate 		rc = &raw->db_data;
100*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
101*0Sstevel@tonic-gate 		return (rc);
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	/*
105*0Sstevel@tonic-gate 	 * validate the region
106*0Sstevel@tonic-gate 	 */
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	if ((d->db_off < 0) ||
109*0Sstevel@tonic-gate 	    (d->db_off >= elf->ed_fsz) ||
110*0Sstevel@tonic-gate 	    (elf->ed_fsz - d->db_off < d->db_fsz)) {
111*0Sstevel@tonic-gate 		_elf_seterr(EFMT_DATA, 0);
112*0Sstevel@tonic-gate 		free(raw);
113*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
114*0Sstevel@tonic-gate 		return (0);
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 	raw->db_data.d_size = d->db_fsz;
117*0Sstevel@tonic-gate 	if (elf->ed_raw != 0) {
118*0Sstevel@tonic-gate 		raw->db_data.d_buf = (Elf_Void *)(elf->ed_raw + d->db_off);
119*0Sstevel@tonic-gate 		d->db_raw = raw;
120*0Sstevel@tonic-gate 		rc = &raw->db_data;
121*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
122*0Sstevel@tonic-gate 		return (rc);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 	raw->db_buf = (Elf_Void *)_elf_read(elf->ed_fd,
125*0Sstevel@tonic-gate 			elf->ed_baseoff + d->db_off, d->db_fsz);
126*0Sstevel@tonic-gate 	if (raw->db_buf == 0) {
127*0Sstevel@tonic-gate 		free(raw);
128*0Sstevel@tonic-gate 		READUNLOCKS(elf, scn)
129*0Sstevel@tonic-gate 		return (0);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	raw->db_data.d_buf = raw->db_buf;
132*0Sstevel@tonic-gate 	d->db_raw = raw;
133*0Sstevel@tonic-gate 	rc = &raw->db_data;
134*0Sstevel@tonic-gate 	READUNLOCKS(elf, scn)
135*0Sstevel@tonic-gate 	return (rc);
136*0Sstevel@tonic-gate }
137