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