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 <stdlib.h>
330Sstevel@tonic-gate #include "libelf.h"
340Sstevel@tonic-gate #include "decl.h"
350Sstevel@tonic-gate #include "msg.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate
380Sstevel@tonic-gate Elf_Data *
elf_rawdata(Elf_Scn * scn,Elf_Data * data)390Sstevel@tonic-gate elf_rawdata(Elf_Scn * scn, Elf_Data * data)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate Dnode * d = (Dnode *)data;
420Sstevel@tonic-gate Dnode * raw;
430Sstevel@tonic-gate Elf_Data * rc;
440Sstevel@tonic-gate Elf * elf;
450Sstevel@tonic-gate
460Sstevel@tonic-gate if (scn == 0)
470Sstevel@tonic-gate return (0);
480Sstevel@tonic-gate elf = scn->s_elf;
490Sstevel@tonic-gate READLOCKS(elf, scn)
500Sstevel@tonic-gate if ((scn->s_myflags & SF_READY) == 0) {
510Sstevel@tonic-gate UPGRADELOCKS(elf, scn)
520Sstevel@tonic-gate if ((scn->s_myflags & SF_READY) == 0)
530Sstevel@tonic-gate (void) _elf_cookscn(scn);
540Sstevel@tonic-gate DOWNGRADELOCKS(elf, scn)
550Sstevel@tonic-gate }
560Sstevel@tonic-gate
570Sstevel@tonic-gate if (d == 0)
580Sstevel@tonic-gate d = scn->s_hdnode;
590Sstevel@tonic-gate else
600Sstevel@tonic-gate d = d->db_next;
610Sstevel@tonic-gate
620Sstevel@tonic-gate if (d == 0) {
630Sstevel@tonic-gate READUNLOCKS(elf, scn)
640Sstevel@tonic-gate return (0);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (d->db_scn != scn) {
680Sstevel@tonic-gate _elf_seterr(EREQ_DATA, 0);
690Sstevel@tonic-gate READUNLOCKS(elf, scn)
700Sstevel@tonic-gate return (0);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * The data may come from a previously constructed Dbuf,
750Sstevel@tonic-gate * from the file's raw memory image, or the file system.
760Sstevel@tonic-gate * "Empty" regions get an empty buffer.
770Sstevel@tonic-gate */
780Sstevel@tonic-gate
790Sstevel@tonic-gate if (d->db_raw != 0) {
800Sstevel@tonic-gate rc = &d->db_raw->db_data;
810Sstevel@tonic-gate READUNLOCKS(elf, scn)
820Sstevel@tonic-gate return (rc);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate if ((raw = _elf_dnode()) == 0) {
860Sstevel@tonic-gate READUNLOCKS(elf, scn)
870Sstevel@tonic-gate return (0);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate raw->db_myflags |= DBF_READY;
900Sstevel@tonic-gate if ((d->db_off == 0) || (d->db_fsz == 0)) {
910Sstevel@tonic-gate d->db_raw = raw;
920Sstevel@tonic-gate raw->db_data.d_size = d->db_shsz;
930Sstevel@tonic-gate rc = &raw->db_data;
940Sstevel@tonic-gate READUNLOCKS(elf, scn)
950Sstevel@tonic-gate return (rc);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * validate the region
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if ((d->db_off < 0) ||
1030Sstevel@tonic-gate (d->db_off >= elf->ed_fsz) ||
1040Sstevel@tonic-gate (elf->ed_fsz - d->db_off < d->db_fsz)) {
1050Sstevel@tonic-gate _elf_seterr(EFMT_DATA, 0);
1060Sstevel@tonic-gate free(raw);
1070Sstevel@tonic-gate READUNLOCKS(elf, scn)
1080Sstevel@tonic-gate return (0);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate raw->db_data.d_size = d->db_fsz;
1110Sstevel@tonic-gate if (elf->ed_raw != 0) {
1120Sstevel@tonic-gate raw->db_data.d_buf = (Elf_Void *)(elf->ed_raw + d->db_off);
1130Sstevel@tonic-gate d->db_raw = raw;
1140Sstevel@tonic-gate rc = &raw->db_data;
1150Sstevel@tonic-gate READUNLOCKS(elf, scn)
1160Sstevel@tonic-gate return (rc);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate raw->db_buf = (Elf_Void *)_elf_read(elf->ed_fd,
119*6812Sraf elf->ed_baseoff + d->db_off, d->db_fsz);
1200Sstevel@tonic-gate if (raw->db_buf == 0) {
1210Sstevel@tonic-gate free(raw);
1220Sstevel@tonic-gate READUNLOCKS(elf, scn)
1230Sstevel@tonic-gate return (0);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate raw->db_data.d_buf = raw->db_buf;
1260Sstevel@tonic-gate d->db_raw = raw;
1270Sstevel@tonic-gate rc = &raw->db_data;
1280Sstevel@tonic-gate READUNLOCKS(elf, scn)
1290Sstevel@tonic-gate return (rc);
1300Sstevel@tonic-gate }
131