xref: /titanic_44/usr/src/cmd/sgs/libelf/common/rawdata.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 /*
27  * Copyright (c) 1999 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.3	*/
33 
34 #pragma weak	elf_rawdata = _elf_rawdata
35 
36 
37 #include "syn.h"
38 #include <stdlib.h>
39 #include "libelf.h"
40 #include "decl.h"
41 #include "msg.h"
42 
43 
44 Elf_Data *
45 elf_rawdata(Elf_Scn * scn, Elf_Data * data)
46 {
47 	Dnode *		d = (Dnode *)data;
48 	Dnode *		raw;
49 	Elf_Data *	rc;
50 	Elf *		elf;
51 
52 	if (scn == 0)
53 		return (0);
54 	elf = scn->s_elf;
55 	READLOCKS(elf, scn)
56 	if ((scn->s_myflags & SF_READY) == 0) {
57 		UPGRADELOCKS(elf, scn)
58 		if ((scn->s_myflags & SF_READY) == 0)
59 			(void) _elf_cookscn(scn);
60 		DOWNGRADELOCKS(elf, scn)
61 	}
62 
63 	if (d == 0)
64 		d = scn->s_hdnode;
65 	else
66 		d = d->db_next;
67 
68 	if (d == 0) {
69 		READUNLOCKS(elf, scn)
70 		return (0);
71 	}
72 
73 	if (d->db_scn != scn) {
74 		_elf_seterr(EREQ_DATA, 0);
75 		READUNLOCKS(elf, scn)
76 		return (0);
77 	}
78 
79 	/*
80 	 * The data may come from a previously constructed Dbuf,
81 	 * from the file's raw memory image, or the file system.
82 	 * "Empty" regions get an empty buffer.
83 	 */
84 
85 	if (d->db_raw != 0) {
86 		rc = &d->db_raw->db_data;
87 		READUNLOCKS(elf, scn)
88 		return (rc);
89 	}
90 
91 	if ((raw = _elf_dnode()) == 0)  {
92 		READUNLOCKS(elf, scn)
93 		return (0);
94 	}
95 	raw->db_myflags |= DBF_READY;
96 	if ((d->db_off == 0) || (d->db_fsz == 0)) {
97 		d->db_raw = raw;
98 		raw->db_data.d_size = d->db_shsz;
99 		rc = &raw->db_data;
100 		READUNLOCKS(elf, scn)
101 		return (rc);
102 	}
103 
104 	/*
105 	 * validate the region
106 	 */
107 
108 	if ((d->db_off < 0) ||
109 	    (d->db_off >= elf->ed_fsz) ||
110 	    (elf->ed_fsz - d->db_off < d->db_fsz)) {
111 		_elf_seterr(EFMT_DATA, 0);
112 		free(raw);
113 		READUNLOCKS(elf, scn)
114 		return (0);
115 	}
116 	raw->db_data.d_size = d->db_fsz;
117 	if (elf->ed_raw != 0) {
118 		raw->db_data.d_buf = (Elf_Void *)(elf->ed_raw + d->db_off);
119 		d->db_raw = raw;
120 		rc = &raw->db_data;
121 		READUNLOCKS(elf, scn)
122 		return (rc);
123 	}
124 	raw->db_buf = (Elf_Void *)_elf_read(elf->ed_fd,
125 			elf->ed_baseoff + d->db_off, d->db_fsz);
126 	if (raw->db_buf == 0) {
127 		free(raw);
128 		READUNLOCKS(elf, scn)
129 		return (0);
130 	}
131 	raw->db_data.d_buf = raw->db_buf;
132 	d->db_raw = raw;
133 	rc = &raw->db_data;
134 	READUNLOCKS(elf, scn)
135 	return (rc);
136 }
137