17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
59d6538abSab * Common Development and Distribution License (the "License").
69d6538abSab * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*7257d1b4Sraf /* Copyright (c) 1988 AT&T */
28*7257d1b4Sraf /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <assert.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <libelf.h>
347c478bd9Sstevel@tonic-gate #include "decl.h"
357c478bd9Sstevel@tonic-gate #include "msg.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * Convert data from file format to memory format.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static const size_t align32[ELF_T_NUM] =
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 1, /* ELF_T_BYTE */
467c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_ADDR */
477c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_DYN */
487c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_EHDR */
497c478bd9Sstevel@tonic-gate sizeof (Elf32_Half), /* ELF_T_HALF */
507c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_OFF */
517c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_PHDR */
527c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_RELA */
537c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_REL */
547c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_SHDR */
557c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_SWORD */
567c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_SYM */
577c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_WORD */
587c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_VERDEF */
597c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_VERNEED */
607c478bd9Sstevel@tonic-gate sizeof (Elf64_Sxword), /* ELF_T_SXWORD */
617c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_XWORD */
627c478bd9Sstevel@tonic-gate sizeof (Elf32_Half), /* ELF_T_SYMINFO */
637c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_NOTE */
647c478bd9Sstevel@tonic-gate sizeof (Elf32_Lword), /* ELF_T_MOVE */
657c478bd9Sstevel@tonic-gate sizeof (Elf32_Lword), /* ELF_T_MOVEP */
667c478bd9Sstevel@tonic-gate sizeof (Elf32_Word) /* ELF_T_CAP */
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate #define Nalign32 (sizeof (align32)/sizeof (align32[0]))
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static const size_t align64[ELF_T_NUM] =
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 1, /* ELF_T_BYTE */
757c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_ADDR */
767c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_DYN */
777c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_EHDR */
787c478bd9Sstevel@tonic-gate sizeof (Elf64_Half), /* ELF_T_HALF */
797c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_OFF */
807c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_PHDR */
817c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_RELA */
827c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_REL */
837c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_SHDR */
847c478bd9Sstevel@tonic-gate sizeof (Elf64_Word), /* ELF_T_SWORD */
857c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_SYM */
867c478bd9Sstevel@tonic-gate sizeof (Elf64_Word), /* ELF_T_WORD */
877c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_VDEF */
887c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_VNEED */
897c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_SXWORD */
907c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_XWORD */
917c478bd9Sstevel@tonic-gate sizeof (Elf32_Half), /* ELF_T_SYMINFO */
927c478bd9Sstevel@tonic-gate sizeof (Elf32), /* ELF_T_NOTE */
937c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_MOVE */
947c478bd9Sstevel@tonic-gate sizeof (Elf64), /* ELF_T_MOVEP */
957c478bd9Sstevel@tonic-gate sizeof (Elf64_Word) /* ELF_T_CAP */
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate #define Nalign64 (sizeof (align64)/sizeof (align64[0]))
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate * Could use an array indexed by ELFCLASS*, but I'd rather
1037c478bd9Sstevel@tonic-gate * avoid .data over something this infrequently used. The
1047c478bd9Sstevel@tonic-gate * next choice would be to add extra conditionals.
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate #define NALIGN(elf) ((elf->ed_class == ELFCLASS32) ? Nalign32 : Nalign64)
1077c478bd9Sstevel@tonic-gate #define ALIGN(elf) ((elf->ed_class == ELFCLASS32) ? align32 : align64)
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate Elf_Data *
_elf_locked_getdata(Elf_Scn * scn,Elf_Data * data)1117c478bd9Sstevel@tonic-gate _elf_locked_getdata(Elf_Scn * scn, Elf_Data * data)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate Dnode * d = (Dnode *)data;
1147c478bd9Sstevel@tonic-gate Elf * elf;
1157c478bd9Sstevel@tonic-gate Elf_Data src;
1167c478bd9Sstevel@tonic-gate unsigned work;
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate assert(!elf_threaded || RW_LOCK_HELD(&(scn->s_elf->ed_rwlock)));
1197c478bd9Sstevel@tonic-gate assert(!elf_threaded || MUTEX_HELD(&(scn->s_mutex)));
1207c478bd9Sstevel@tonic-gate elf = scn->s_elf;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate if ((scn->s_myflags & SF_READY) == 0) {
1237c478bd9Sstevel@tonic-gate UPGRADELOCKS(elf, scn)
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * make sure someone else didn't come along and cook
1267c478bd9Sstevel@tonic-gate * this stuff.
1277c478bd9Sstevel@tonic-gate */
1287c478bd9Sstevel@tonic-gate if ((scn->s_myflags & SF_READY) == 0)
1297c478bd9Sstevel@tonic-gate (void) _elf_cookscn(scn);
1307c478bd9Sstevel@tonic-gate DOWNGRADELOCKS(elf, scn)
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate if (d == 0)
1347c478bd9Sstevel@tonic-gate d = scn->s_hdnode;
1357c478bd9Sstevel@tonic-gate else
1367c478bd9Sstevel@tonic-gate d = d->db_next;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (scn->s_err != 0) {
1397c478bd9Sstevel@tonic-gate /*LINTED*/
1407c478bd9Sstevel@tonic-gate _elf_seterr((Msg)scn->s_err, 0);
1417c478bd9Sstevel@tonic-gate return (0);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate if (d == 0) {
1457c478bd9Sstevel@tonic-gate return (0);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (d->db_scn != scn) {
1497c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_DATA, 0);
1507c478bd9Sstevel@tonic-gate return (0);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate if (d->db_myflags & DBF_READY) {
1547c478bd9Sstevel@tonic-gate return (&d->db_data);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate elf = scn->s_elf;
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * Prepare return buffer. The data comes from the memory
1607c478bd9Sstevel@tonic-gate * image of the file. "Empty" regions get an empty buffer.
1617c478bd9Sstevel@tonic-gate *
1627c478bd9Sstevel@tonic-gate * Only sections of an ELF_C_READ file can be not READY here.
1637c478bd9Sstevel@tonic-gate * Furthermore, the input file must have been cooked or
1647c478bd9Sstevel@tonic-gate * frozen by now. Translate cooked files in place if possible.
1657c478bd9Sstevel@tonic-gate */
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate ELFACCESSDATA(work, _elf_work)
1687c478bd9Sstevel@tonic-gate d->db_data.d_version = work;
1697c478bd9Sstevel@tonic-gate if ((d->db_off == 0) || (d->db_fsz == 0)) {
1707c478bd9Sstevel@tonic-gate d->db_myflags |= DBF_READY;
1717c478bd9Sstevel@tonic-gate return (&d->db_data);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate if (elf->ed_class == ELFCLASS32) {
1757c478bd9Sstevel@tonic-gate Elf32_Shdr *sh = scn->s_shdr;
1767c478bd9Sstevel@tonic-gate size_t sz = sh->sh_entsize;
1777c478bd9Sstevel@tonic-gate Elf_Type t = d->db_data.d_type;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate if ((t != ELF_T_BYTE) &&
1807c478bd9Sstevel@tonic-gate (sz > 1) && (sz != elf32_fsize(t, 1, elf->ed_version))) {
1817c478bd9Sstevel@tonic-gate _elf_seterr(EFMT_ENTSZ, 0);
1827c478bd9Sstevel@tonic-gate return (0);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate } else if (elf->ed_class == ELFCLASS64) {
1857c478bd9Sstevel@tonic-gate Elf64_Shdr *sh = scn->s_shdr;
1867c478bd9Sstevel@tonic-gate Elf64_Xword sz = sh->sh_entsize;
1877c478bd9Sstevel@tonic-gate Elf_Type t = d->db_data.d_type;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate if (t != ELF_T_BYTE && sz > 1 &&
1907c478bd9Sstevel@tonic-gate sz != elf64_fsize(t, 1, elf->ed_version)) {
1917c478bd9Sstevel@tonic-gate _elf_seterr(EFMT_ENTSZ, 0);
1927c478bd9Sstevel@tonic-gate return (0);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate } else {
1957c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0);
1967c478bd9Sstevel@tonic-gate return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate * validate the region
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate if ((d->db_off < 0) || (d->db_off >= elf->ed_fsz) ||
2057c478bd9Sstevel@tonic-gate (elf->ed_fsz - d->db_off < d->db_fsz)) {
2067c478bd9Sstevel@tonic-gate _elf_seterr(EFMT_DATA, 0);
2077c478bd9Sstevel@tonic-gate return (0);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate * set up translation buffers and validate
2127c478bd9Sstevel@tonic-gate */
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate src.d_buf = (Elf_Void *)(elf->ed_ident + d->db_off);
2157c478bd9Sstevel@tonic-gate src.d_size = d->db_fsz;
2167c478bd9Sstevel@tonic-gate src.d_type = d->db_data.d_type;
2177c478bd9Sstevel@tonic-gate src.d_version = elf->ed_version;
2187c478bd9Sstevel@tonic-gate if (elf->ed_vm) {
2197c478bd9Sstevel@tonic-gate UPGRADELOCKS(elf, scn)
2207c478bd9Sstevel@tonic-gate if (_elf_vm(elf, (size_t)d->db_off, d->db_fsz) != OK_YES) {
2217c478bd9Sstevel@tonic-gate DOWNGRADELOCKS(elf, scn)
2227c478bd9Sstevel@tonic-gate return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate DOWNGRADELOCKS(elf, scn)
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * decide where to put destination
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate switch (elf->ed_status) {
2327c478bd9Sstevel@tonic-gate case ES_COOKED:
2337c478bd9Sstevel@tonic-gate if ((size_t)d->db_data.d_type >= NALIGN(elf)) {
2347c478bd9Sstevel@tonic-gate _elf_seterr(EBUG_COOKTYPE, 0);
2357c478bd9Sstevel@tonic-gate return (0);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * If the destination size (memory) is at least as
2409d6538abSab * big as the source size (file), and has the necessary
2419d6538abSab * alignment, reuse the space.
2429d6538abSab *
2439d6538abSab * Note that it is not sufficient to check the alignment
2449d6538abSab * of the offset within the object. Rather, we must check
2459d6538abSab * the alignment of the actual data buffer. The offset is
2469d6538abSab * sufficient if the file is a plain object file, which
2479d6538abSab * will always be mapped on a page boundary. In an archive
2489d6538abSab * however, the only guarantee is that the object will start
2499d6538abSab * on an even boundary within the archive file. The
2509d6538abSab * Solaris ar(1) adds padding in most (but not all cases)
2519d6538abSab * which minimizes this issue, but it is still important
2529d6538abSab * for the remaining cases that do not get padded. It also
2539d6538abSab * matters with archives produced by other versions of
2549d6538abSab * ar(1), such as the GNU version, or one from another
2559d6538abSab * ELF based operating system.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate
2589d6538abSab if (d->db_data.d_size <= src.d_size) {
2597c478bd9Sstevel@tonic-gate d->db_data.d_buf = (Elf_Void *)(elf->ed_ident +
260*7257d1b4Sraf d->db_off);
2619d6538abSab if (((uintptr_t)d->db_data.d_buf
262*7257d1b4Sraf % ALIGN(elf)[d->db_data.d_type]) == 0) {
2639d6538abSab break;
2649d6538abSab } else { /* Failure: Restore NULL buffer pointer */
2659d6538abSab d->db_data.d_buf = 0;
2669d6538abSab }
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*FALLTHRU*/
2707c478bd9Sstevel@tonic-gate case ES_FROZEN:
2717c478bd9Sstevel@tonic-gate if ((d->db_buf = malloc(d->db_data.d_size)) == 0) {
2727c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_DATA, errno);
2737c478bd9Sstevel@tonic-gate return (0);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate d->db_data.d_buf = d->db_buf;
2767c478bd9Sstevel@tonic-gate break;
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate default:
2797c478bd9Sstevel@tonic-gate _elf_seterr(EBUG_COOKSTAT, 0);
2807c478bd9Sstevel@tonic-gate return (0);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate if (elf->ed_class == ELFCLASS32) {
2847c478bd9Sstevel@tonic-gate if (elf32_xlatetom(&d->db_data, &src, elf->ed_encode) == 0)
2857c478bd9Sstevel@tonic-gate return (0);
2867c478bd9Sstevel@tonic-gate } else { /* ELFCLASS64 */
2877c478bd9Sstevel@tonic-gate if (elf64_xlatetom(&d->db_data, &src, elf->ed_encode) == 0)
2887c478bd9Sstevel@tonic-gate return (0);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate d->db_myflags |= DBF_READY;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate return (&d->db_data);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate Elf_Data *
elf_getdata(Elf_Scn * scn,Elf_Data * data)2967c478bd9Sstevel@tonic-gate elf_getdata(Elf_Scn * scn, Elf_Data * data)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate Elf_Data * rc;
2997c478bd9Sstevel@tonic-gate Elf * elf;
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate * trap null args, end of list, previous buffer.
3037c478bd9Sstevel@tonic-gate * SHT_NULL sections have no buffer list, so they
3047c478bd9Sstevel@tonic-gate * fall out here too.
3057c478bd9Sstevel@tonic-gate */
3067c478bd9Sstevel@tonic-gate if (scn == 0)
3077c478bd9Sstevel@tonic-gate return (0);
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate elf = scn->s_elf;
3107c478bd9Sstevel@tonic-gate READLOCKS(elf, scn);
3117c478bd9Sstevel@tonic-gate rc = _elf_locked_getdata(scn, data);
3127c478bd9Sstevel@tonic-gate READUNLOCKS(elf, scn);
3137c478bd9Sstevel@tonic-gate return (rc);
3147c478bd9Sstevel@tonic-gate }
315