1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*0Sstevel@tonic-gate * Use is subject to license terms. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate /* 34*0Sstevel@tonic-gate * This stuff used to live in cook.c, but was moved out to 35*0Sstevel@tonic-gate * facilitate dual (Elf32 and Elf64) compilation. See block 36*0Sstevel@tonic-gate * comment in cook.c for more info. 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #include "syn.h" 41*0Sstevel@tonic-gate #include <string.h> 42*0Sstevel@tonic-gate #include <ar.h> 43*0Sstevel@tonic-gate #include <stdlib.h> 44*0Sstevel@tonic-gate #include <errno.h> 45*0Sstevel@tonic-gate #include "decl.h" 46*0Sstevel@tonic-gate #include "member.h" 47*0Sstevel@tonic-gate #include "msg.h" 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate /* 50*0Sstevel@tonic-gate * This module is compiled twice, the second time having 51*0Sstevel@tonic-gate * -D_ELF64 defined. The following set of macros, along 52*0Sstevel@tonic-gate * with machelf.h, represent the differences between the 53*0Sstevel@tonic-gate * two compilations. Be careful *not* to add any class- 54*0Sstevel@tonic-gate * dependent code (anything that has elf32 or elf64 in the 55*0Sstevel@tonic-gate * name) to this code without hiding it behind a switch- 56*0Sstevel@tonic-gate * able macro like these. 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate #if defined(_ELF64) 59*0Sstevel@tonic-gate #define Snode Snode64 60*0Sstevel@tonic-gate #define ELFCLASS ELFCLASS64 61*0Sstevel@tonic-gate #define ElfField Elf64 62*0Sstevel@tonic-gate #define _elf_snode_init _elf64_snode_init 63*0Sstevel@tonic-gate #define _elf_prepscan _elf64_prepscan 64*0Sstevel@tonic-gate #define _elf_cookscn _elf64_cookscn 65*0Sstevel@tonic-gate #define _elf_mtype _elf64_mtype 66*0Sstevel@tonic-gate #define _elf_msize _elf64_msize 67*0Sstevel@tonic-gate #define elf_fsize elf64_fsize 68*0Sstevel@tonic-gate #define _elf_snode _elf64_snode 69*0Sstevel@tonic-gate #define _elf_ehdr _elf64_ehdr 70*0Sstevel@tonic-gate #define elf_xlatetom elf64_xlatetom 71*0Sstevel@tonic-gate #define _elf_phdr _elf64_phdr 72*0Sstevel@tonic-gate #define _elf_shdr _elf64_shdr 73*0Sstevel@tonic-gate #define _elf_prepscn _elf64_prepscn 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate #else /* Elf32 */ 76*0Sstevel@tonic-gate #define Snode Snode32 77*0Sstevel@tonic-gate #define ELFCLASS ELFCLASS32 78*0Sstevel@tonic-gate #define ElfField Elf32 79*0Sstevel@tonic-gate #define _elf_snode_init _elf32_snode_init 80*0Sstevel@tonic-gate #define _elf_prepscan _elf32_prepscan 81*0Sstevel@tonic-gate #define _elf_cookscn _elf32_cookscn 82*0Sstevel@tonic-gate #define _elf_mtype _elf32_mtype 83*0Sstevel@tonic-gate #define _elf_msize _elf32_msize 84*0Sstevel@tonic-gate #define elf_fsize elf32_fsize 85*0Sstevel@tonic-gate #define _elf_snode _elf32_snode 86*0Sstevel@tonic-gate #define _elf_ehdr _elf32_ehdr 87*0Sstevel@tonic-gate #define elf_xlatetom elf32_xlatetom 88*0Sstevel@tonic-gate #define _elf_phdr _elf32_phdr 89*0Sstevel@tonic-gate #define _elf_shdr _elf32_shdr 90*0Sstevel@tonic-gate #define _elf_prepscn _elf32_prepscn 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate #endif /* _ELF64 */ 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate static Okay 96*0Sstevel@tonic-gate _elf_prepscn(Elf *elf, size_t cnt) 97*0Sstevel@tonic-gate { 98*0Sstevel@tonic-gate NOTE(ASSUMING_PROTECTED(*elf)) 99*0Sstevel@tonic-gate Elf_Scn * s; 100*0Sstevel@tonic-gate Elf_Scn * end; 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate if (cnt == 0) 103*0Sstevel@tonic-gate return (OK_YES); 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate if ((s = malloc(cnt * sizeof (Elf_Scn))) == 0) { 106*0Sstevel@tonic-gate _elf_seterr(EMEM_SCN, errno); 107*0Sstevel@tonic-gate return (OK_NO); 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*s)) 110*0Sstevel@tonic-gate elf->ed_scntabsz = cnt; 111*0Sstevel@tonic-gate end = s + cnt; 112*0Sstevel@tonic-gate elf->ed_hdscn = s; 113*0Sstevel@tonic-gate do { 114*0Sstevel@tonic-gate *s = _elf_snode_init.sb_scn; 115*0Sstevel@tonic-gate s->s_elf = elf; 116*0Sstevel@tonic-gate s->s_next = s + 1; 117*0Sstevel@tonic-gate s->s_index = s - elf->ed_hdscn; 118*0Sstevel@tonic-gate s->s_shdr = (Shdr*)s->s_elf->ed_shdr + s->s_index; 119*0Sstevel@tonic-gate ELFMUTEXINIT(&s->s_mutex); 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* 122*0Sstevel@tonic-gate * Section has not yet been cooked! 123*0Sstevel@tonic-gate * 124*0Sstevel@tonic-gate * We don't cook a section until it's data is actually 125*0Sstevel@tonic-gate * referenced. 126*0Sstevel@tonic-gate */ 127*0Sstevel@tonic-gate s->s_myflags = 0; 128*0Sstevel@tonic-gate } while (++s < end); 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate elf->ed_tlscn = --s; 131*0Sstevel@tonic-gate s->s_next = 0; 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate /* 134*0Sstevel@tonic-gate * Section index SHN_UNDEF (0) does not and cannot 135*0Sstevel@tonic-gate * have a data buffer. Fix it here. Also mark the 136*0Sstevel@tonic-gate * initial section as being allocated for the block 137*0Sstevel@tonic-gate */ 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate s = elf->ed_hdscn; 140*0Sstevel@tonic-gate s->s_myflags = SF_ALLOC; 141*0Sstevel@tonic-gate s->s_hdnode = 0; 142*0Sstevel@tonic-gate s->s_tlnode = 0; 143*0Sstevel@tonic-gate NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*s)) 144*0Sstevel@tonic-gate return (OK_YES); 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate Okay 149*0Sstevel@tonic-gate _elf_cookscn(Elf_Scn * s) 150*0Sstevel@tonic-gate { 151*0Sstevel@tonic-gate NOTE(ASSUMING_PROTECTED(*s, *(s->s_elf))) 152*0Sstevel@tonic-gate Elf * elf; 153*0Sstevel@tonic-gate Shdr * sh; 154*0Sstevel@tonic-gate register Dnode * d = &s->s_dnode; 155*0Sstevel@tonic-gate size_t fsz, msz; 156*0Sstevel@tonic-gate unsigned work; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*d)) 159*0Sstevel@tonic-gate s->s_hdnode = s->s_tlnode = d; 160*0Sstevel@tonic-gate s->s_err = 0; 161*0Sstevel@tonic-gate s->s_shflags = 0; 162*0Sstevel@tonic-gate s->s_uflags = 0; 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate /* 166*0Sstevel@tonic-gate * Prepare d_data for inspection, but don't actually 167*0Sstevel@tonic-gate * translate data until needed. Leave the READY 168*0Sstevel@tonic-gate * flag off. NOBITS sections see zero size. 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate elf = s->s_elf; 171*0Sstevel@tonic-gate sh = s->s_shdr; 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate d->db_scn = s; 174*0Sstevel@tonic-gate d->db_off = sh->sh_offset; 175*0Sstevel@tonic-gate d->db_data.d_align = sh->sh_addralign; 176*0Sstevel@tonic-gate d->db_data.d_version = elf->ed_version; 177*0Sstevel@tonic-gate ELFACCESSDATA(work, _elf_work) 178*0Sstevel@tonic-gate d->db_data.d_type = _elf_mtype(elf, sh->sh_type, work); 179*0Sstevel@tonic-gate d->db_data.d_buf = 0; 180*0Sstevel@tonic-gate d->db_data.d_off = 0; 181*0Sstevel@tonic-gate fsz = elf_fsize(d->db_data.d_type, 1, elf->ed_version); 182*0Sstevel@tonic-gate msz = _elf_msize(d->db_data.d_type, elf->ed_version); 183*0Sstevel@tonic-gate d->db_data.d_size = (sh->sh_size / fsz) * msz; 184*0Sstevel@tonic-gate d->db_shsz = sh->sh_size; 185*0Sstevel@tonic-gate d->db_raw = 0; 186*0Sstevel@tonic-gate d->db_buf = 0; 187*0Sstevel@tonic-gate d->db_uflags = 0; 188*0Sstevel@tonic-gate d->db_myflags = 0; 189*0Sstevel@tonic-gate d->db_next = 0; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate if (sh->sh_type != SHT_NOBITS) 192*0Sstevel@tonic-gate d->db_fsz = sh->sh_size; 193*0Sstevel@tonic-gate else 194*0Sstevel@tonic-gate d->db_fsz = 0; 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate s->s_myflags |= SF_READY; 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*d)) 199*0Sstevel@tonic-gate return (OK_YES); 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate Snode * 205*0Sstevel@tonic-gate _elf_snode() 206*0Sstevel@tonic-gate { 207*0Sstevel@tonic-gate register Snode *s; 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate if ((s = malloc(sizeof (Snode))) == 0) { 210*0Sstevel@tonic-gate _elf_seterr(EMEM_SNODE, errno); 211*0Sstevel@tonic-gate return (0); 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate *s = _elf_snode_init; 214*0Sstevel@tonic-gate ELFMUTEXINIT(&s->sb_scn.s_mutex); 215*0Sstevel@tonic-gate s->sb_scn.s_myflags = SF_ALLOC | SF_READY; 216*0Sstevel@tonic-gate s->sb_scn.s_shdr = &s->sb_shdr; 217*0Sstevel@tonic-gate return (s); 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate int 223*0Sstevel@tonic-gate _elf_ehdr(Elf * elf, int inplace) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate NOTE(ASSUMING_PROTECTED(*elf)) 226*0Sstevel@tonic-gate register size_t fsz; /* field size */ 227*0Sstevel@tonic-gate Elf_Data dst, src; 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate fsz = elf_fsize(ELF_T_EHDR, 1, elf->ed_version); 230*0Sstevel@tonic-gate if (fsz > elf->ed_fsz) { 231*0Sstevel@tonic-gate _elf_seterr(EFMT_EHDRSZ, 0); 232*0Sstevel@tonic-gate return (-1); 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate if (inplace && (fsz >= sizeof (Ehdr))) { 235*0Sstevel@tonic-gate /* 236*0Sstevel@tonic-gate * The translated Ehdr will fit over the original Ehdr. 237*0Sstevel@tonic-gate */ 238*0Sstevel@tonic-gate /* LINTED */ 239*0Sstevel@tonic-gate elf->ed_ehdr = (Ehdr *)elf->ed_ident; 240*0Sstevel@tonic-gate elf->ed_status = ES_COOKED; 241*0Sstevel@tonic-gate } else { 242*0Sstevel@tonic-gate elf->ed_ehdr = malloc(sizeof (Ehdr)); 243*0Sstevel@tonic-gate if (elf->ed_ehdr == 0) { 244*0Sstevel@tonic-gate _elf_seterr(EMEM_EHDR, errno); 245*0Sstevel@tonic-gate return (-1); 246*0Sstevel@tonic-gate } 247*0Sstevel@tonic-gate elf->ed_myflags |= EDF_EHALLOC; 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate /* 251*0Sstevel@tonic-gate * Memory size >= fsz, because otherwise the memory version 252*0Sstevel@tonic-gate * loses information and cannot accurately implement the 253*0Sstevel@tonic-gate * file. 254*0Sstevel@tonic-gate */ 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate src.d_buf = (Elf_Void *)elf->ed_ident; 257*0Sstevel@tonic-gate src.d_type = ELF_T_EHDR; 258*0Sstevel@tonic-gate src.d_size = fsz; 259*0Sstevel@tonic-gate src.d_version = elf->ed_version; 260*0Sstevel@tonic-gate dst.d_buf = (Elf_Void *)elf->ed_ehdr; 261*0Sstevel@tonic-gate dst.d_size = sizeof (Ehdr); 262*0Sstevel@tonic-gate dst.d_version = EV_CURRENT; 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate if ((_elf_vm(elf, (size_t)0, fsz) != OK_YES) || 265*0Sstevel@tonic-gate (elf_xlatetom(&dst, &src, elf->ed_encode) == 0)) { 266*0Sstevel@tonic-gate if (elf->ed_myflags & EDF_EHALLOC) { 267*0Sstevel@tonic-gate elf->ed_myflags &= ~EDF_EHALLOC; 268*0Sstevel@tonic-gate free(elf->ed_ehdr); 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate elf->ed_ehdr = 0; 271*0Sstevel@tonic-gate return (-1); 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate if (((Ehdr*)elf->ed_ehdr)->e_ident[EI_CLASS] != ELFCLASS) { 275*0Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0); 276*0Sstevel@tonic-gate if (elf->ed_myflags & EDF_EHALLOC) { 277*0Sstevel@tonic-gate elf->ed_myflags &= ~EDF_EHALLOC; 278*0Sstevel@tonic-gate free(elf->ed_ehdr); 279*0Sstevel@tonic-gate } 280*0Sstevel@tonic-gate elf->ed_ehdr = 0; 281*0Sstevel@tonic-gate return (-1); 282*0Sstevel@tonic-gate } 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate if (((Ehdr*)elf->ed_ehdr)->e_version != elf->ed_version) { 285*0Sstevel@tonic-gate _elf_seterr(EFMT_VER2, 0); 286*0Sstevel@tonic-gate if (elf->ed_myflags & EDF_EHALLOC) { 287*0Sstevel@tonic-gate elf->ed_myflags &= ~EDF_EHALLOC; 288*0Sstevel@tonic-gate free(elf->ed_ehdr); 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate elf->ed_ehdr = 0; 291*0Sstevel@tonic-gate return (-1); 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate return (0); 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate int 300*0Sstevel@tonic-gate _elf_phdr(Elf * elf, int inplace) 301*0Sstevel@tonic-gate { 302*0Sstevel@tonic-gate NOTE(ASSUMING_PROTECTED(*elf)) 303*0Sstevel@tonic-gate register size_t fsz, msz; 304*0Sstevel@tonic-gate Elf_Data dst, src; 305*0Sstevel@tonic-gate Ehdr * eh = elf->ed_ehdr; /* must be present */ 306*0Sstevel@tonic-gate unsigned work; 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gate if (eh->e_phnum == 0) 309*0Sstevel@tonic-gate return (0); 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate fsz = elf_fsize(ELF_T_PHDR, 1, elf->ed_version); 312*0Sstevel@tonic-gate if (eh->e_phentsize != fsz) { 313*0Sstevel@tonic-gate _elf_seterr(EFMT_PHDRSZ, 0); 314*0Sstevel@tonic-gate return (-1); 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate fsz *= eh->e_phnum; 318*0Sstevel@tonic-gate ELFACCESSDATA(work, _elf_work) 319*0Sstevel@tonic-gate msz = _elf_msize(ELF_T_PHDR, work) * eh->e_phnum; 320*0Sstevel@tonic-gate if ((eh->e_phoff == 0) || 321*0Sstevel@tonic-gate ((fsz + eh->e_phoff) > elf->ed_fsz)) { 322*0Sstevel@tonic-gate _elf_seterr(EFMT_PHTAB, 0); 323*0Sstevel@tonic-gate return (-1); 324*0Sstevel@tonic-gate } 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate if (inplace && fsz >= msz && eh->e_phoff % sizeof (ElfField) == 0) { 327*0Sstevel@tonic-gate elf->ed_phdr = (Elf_Void *)(elf->ed_ident + eh->e_phoff); 328*0Sstevel@tonic-gate elf->ed_status = ES_COOKED; 329*0Sstevel@tonic-gate } else { 330*0Sstevel@tonic-gate if ((elf->ed_phdr = malloc(msz)) == 0) { 331*0Sstevel@tonic-gate _elf_seterr(EMEM_PHDR, errno); 332*0Sstevel@tonic-gate return (-1); 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate elf->ed_myflags |= EDF_PHALLOC; 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate src.d_buf = (Elf_Void *)(elf->ed_ident + eh->e_phoff); 337*0Sstevel@tonic-gate src.d_type = ELF_T_PHDR; 338*0Sstevel@tonic-gate src.d_size = fsz; 339*0Sstevel@tonic-gate src.d_version = elf->ed_version; 340*0Sstevel@tonic-gate dst.d_buf = elf->ed_phdr; 341*0Sstevel@tonic-gate dst.d_size = msz; 342*0Sstevel@tonic-gate dst.d_version = work; 343*0Sstevel@tonic-gate if ((_elf_vm(elf, (size_t)eh->e_phoff, fsz) != OK_YES) || 344*0Sstevel@tonic-gate (elf_xlatetom(&dst, &src, elf->ed_encode) == 0)) { 345*0Sstevel@tonic-gate if (elf->ed_myflags & EDF_PHALLOC) { 346*0Sstevel@tonic-gate elf->ed_myflags &= ~EDF_PHALLOC; 347*0Sstevel@tonic-gate free(elf->ed_phdr); 348*0Sstevel@tonic-gate } 349*0Sstevel@tonic-gate elf->ed_phdr = 0; 350*0Sstevel@tonic-gate return (-1); 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate elf->ed_phdrsz = msz; 353*0Sstevel@tonic-gate return (0); 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate int 359*0Sstevel@tonic-gate _elf_shdr(Elf * elf, int inplace) 360*0Sstevel@tonic-gate { 361*0Sstevel@tonic-gate NOTE(ASSUMING_PROTECTED(*elf)) 362*0Sstevel@tonic-gate register size_t fsz, msz; 363*0Sstevel@tonic-gate size_t scncnt; 364*0Sstevel@tonic-gate Elf_Data dst, src; 365*0Sstevel@tonic-gate register Ehdr *eh = elf->ed_ehdr; /* must be present */ 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gate if ((eh->e_shnum == 0) && (eh->e_shoff == 0)) 368*0Sstevel@tonic-gate return (0); 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate fsz = elf_fsize(ELF_T_SHDR, 1, elf->ed_version); 371*0Sstevel@tonic-gate if (eh->e_shentsize != fsz) { 372*0Sstevel@tonic-gate _elf_seterr(EFMT_SHDRSZ, 0); 373*0Sstevel@tonic-gate return (-1); 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate /* 376*0Sstevel@tonic-gate * If we are dealing with a file with 'extended section 377*0Sstevel@tonic-gate * indexes' - then we need to load the first section 378*0Sstevel@tonic-gate * header. The actual section count is stored in 379*0Sstevel@tonic-gate * Shdr[0].sh_size. 380*0Sstevel@tonic-gate */ 381*0Sstevel@tonic-gate if ((scncnt = eh->e_shnum) == 0) { 382*0Sstevel@tonic-gate Shdr sh; 383*0Sstevel@tonic-gate if ((eh->e_shoff == 0) || 384*0Sstevel@tonic-gate (elf->ed_fsz <= eh->e_shoff) || 385*0Sstevel@tonic-gate (elf->ed_fsz - eh->e_shoff < fsz)) { 386*0Sstevel@tonic-gate _elf_seterr(EFMT_SHTAB, 0); 387*0Sstevel@tonic-gate return (-1); 388*0Sstevel@tonic-gate } 389*0Sstevel@tonic-gate src.d_buf = (Elf_Void *)(elf->ed_ident + eh->e_shoff); 390*0Sstevel@tonic-gate src.d_type = ELF_T_SHDR; 391*0Sstevel@tonic-gate src.d_size = fsz; 392*0Sstevel@tonic-gate src.d_version = elf->ed_version; 393*0Sstevel@tonic-gate dst.d_buf = (Elf_Void *)&sh; 394*0Sstevel@tonic-gate dst.d_size = sizeof (Shdr); 395*0Sstevel@tonic-gate dst.d_version = EV_CURRENT; 396*0Sstevel@tonic-gate if ((_elf_vm(elf, (size_t)eh->e_shoff, fsz) != OK_YES) || 397*0Sstevel@tonic-gate (elf_xlatetom(&dst, &src, elf->ed_encode) == 0)) { 398*0Sstevel@tonic-gate return (-1); 399*0Sstevel@tonic-gate } 400*0Sstevel@tonic-gate scncnt = sh.sh_size; 401*0Sstevel@tonic-gate } 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gate fsz *= scncnt; 404*0Sstevel@tonic-gate msz = scncnt * sizeof (Shdr); 405*0Sstevel@tonic-gate if ((eh->e_shoff == 0) || 406*0Sstevel@tonic-gate (elf->ed_fsz <= eh->e_shoff) || 407*0Sstevel@tonic-gate (elf->ed_fsz - eh->e_shoff < fsz)) { 408*0Sstevel@tonic-gate _elf_seterr(EFMT_SHTAB, 0); 409*0Sstevel@tonic-gate return (-1); 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate if (inplace && (fsz >= msz) && 413*0Sstevel@tonic-gate ((eh->e_shoff % sizeof (ElfField)) == 0)) { 414*0Sstevel@tonic-gate /* LINTED */ 415*0Sstevel@tonic-gate elf->ed_shdr = (Shdr *)(elf->ed_ident + eh->e_shoff); 416*0Sstevel@tonic-gate elf->ed_status = ES_COOKED; 417*0Sstevel@tonic-gate } else { 418*0Sstevel@tonic-gate if ((elf->ed_shdr = malloc(msz)) == 0) { 419*0Sstevel@tonic-gate _elf_seterr(EMEM_SHDR, errno); 420*0Sstevel@tonic-gate return (-1); 421*0Sstevel@tonic-gate } 422*0Sstevel@tonic-gate elf->ed_myflags |= EDF_SHALLOC; 423*0Sstevel@tonic-gate } 424*0Sstevel@tonic-gate src.d_buf = (Elf_Void *)(elf->ed_ident + eh->e_shoff); 425*0Sstevel@tonic-gate src.d_type = ELF_T_SHDR; 426*0Sstevel@tonic-gate src.d_size = fsz; 427*0Sstevel@tonic-gate src.d_version = elf->ed_version; 428*0Sstevel@tonic-gate dst.d_buf = (Elf_Void *)elf->ed_shdr; 429*0Sstevel@tonic-gate dst.d_size = msz; 430*0Sstevel@tonic-gate dst.d_version = EV_CURRENT; 431*0Sstevel@tonic-gate if ((_elf_vm(elf, (size_t)eh->e_shoff, fsz) != OK_YES) || 432*0Sstevel@tonic-gate (elf_xlatetom(&dst, &src, elf->ed_encode) == 0) || 433*0Sstevel@tonic-gate (_elf_prepscn(elf, scncnt) != OK_YES)) { 434*0Sstevel@tonic-gate if (elf->ed_myflags & EDF_SHALLOC) { 435*0Sstevel@tonic-gate elf->ed_myflags &= ~EDF_SHALLOC; 436*0Sstevel@tonic-gate free(elf->ed_shdr); 437*0Sstevel@tonic-gate } 438*0Sstevel@tonic-gate elf->ed_shdr = 0; 439*0Sstevel@tonic-gate return (-1); 440*0Sstevel@tonic-gate } 441*0Sstevel@tonic-gate return (0); 442*0Sstevel@tonic-gate } 443