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 char *
elf_strptr(Elf * elf,size_t ndx,size_t off)380Sstevel@tonic-gate elf_strptr(Elf * elf, size_t ndx, size_t off)
390Sstevel@tonic-gate {
40*6812Sraf Elf_Scn *s;
41*6812Sraf Elf_Data *d;
42*6812Sraf char *rc;
430Sstevel@tonic-gate
440Sstevel@tonic-gate if (elf == 0)
450Sstevel@tonic-gate return (0);
460Sstevel@tonic-gate if ((s = elf_getscn(elf, ndx)) == 0) {
470Sstevel@tonic-gate _elf_seterr(EREQ_STRSCN, 0);
480Sstevel@tonic-gate return (0);
490Sstevel@tonic-gate }
500Sstevel@tonic-gate READLOCKS(elf, s)
510Sstevel@tonic-gate if (elf->ed_class == ELFCLASS32) {
520Sstevel@tonic-gate Elf32_Shdr* sh = (Elf32_Shdr*)s->s_shdr;
530Sstevel@tonic-gate
540Sstevel@tonic-gate if ((sh == 0) || (sh->sh_type != SHT_STRTAB)) {
550Sstevel@tonic-gate _elf_seterr(EREQ_STRSCN, 0);
560Sstevel@tonic-gate READUNLOCKS(elf, s)
570Sstevel@tonic-gate return (0);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate } else if (elf->ed_class == ELFCLASS64) {
600Sstevel@tonic-gate Elf64_Shdr* sh = (Elf64_Shdr*)s->s_shdr;
610Sstevel@tonic-gate
620Sstevel@tonic-gate if ((sh == 0) || (sh->sh_type != SHT_STRTAB)) {
630Sstevel@tonic-gate _elf_seterr(EREQ_STRSCN, 0);
640Sstevel@tonic-gate READUNLOCKS(elf, s)
650Sstevel@tonic-gate return (0);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate } else {
680Sstevel@tonic-gate _elf_seterr(EREQ_STRSCN, 0);
690Sstevel@tonic-gate READUNLOCKS(elf, s)
700Sstevel@tonic-gate return (0);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * If the layout bit is set, use the offsets and
760Sstevel@tonic-gate * sizes in the data buffers. Otherwise, take
770Sstevel@tonic-gate * data buffers in order.
780Sstevel@tonic-gate */
790Sstevel@tonic-gate
800Sstevel@tonic-gate d = 0;
810Sstevel@tonic-gate if (elf->ed_uflags & ELF_F_LAYOUT) {
820Sstevel@tonic-gate while ((d = _elf_locked_getdata(s, d)) != 0) {
830Sstevel@tonic-gate if (d->d_buf == 0)
840Sstevel@tonic-gate continue;
850Sstevel@tonic-gate if ((off >= d->d_off) &&
860Sstevel@tonic-gate (off < d->d_off + d->d_size)) {
870Sstevel@tonic-gate rc = (char *)d->d_buf + off - d->d_off;
880Sstevel@tonic-gate READUNLOCKS(elf, s)
890Sstevel@tonic-gate return (rc);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate }
920Sstevel@tonic-gate } else {
930Sstevel@tonic-gate size_t sz = 0, j;
940Sstevel@tonic-gate while ((d = _elf_locked_getdata(s, d)) != 0) {
950Sstevel@tonic-gate if (((j = d->d_align) > 1) && (sz % j != 0)) {
960Sstevel@tonic-gate j -= sz % j;
970Sstevel@tonic-gate sz += j;
980Sstevel@tonic-gate if (off < j)
990Sstevel@tonic-gate break;
1000Sstevel@tonic-gate off -= j;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate if (d->d_buf != 0) {
1030Sstevel@tonic-gate if (off < d->d_size) {
1040Sstevel@tonic-gate rc = (char *)d->d_buf + off;
1050Sstevel@tonic-gate READUNLOCKS(elf, s)
1060Sstevel@tonic-gate return (rc);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate sz += d->d_size;
1100Sstevel@tonic-gate if (off < d->d_size)
1110Sstevel@tonic-gate break;
1120Sstevel@tonic-gate off -= d->d_size;
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate _elf_seterr(EREQ_STROFF, 0);
1160Sstevel@tonic-gate READUNLOCKS(elf, s)
1170Sstevel@tonic-gate return (0);
1180Sstevel@tonic-gate }
119