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
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * 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 */
29*7257d1b4Sraf
307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <memory.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include "decl.h"
367c478bd9Sstevel@tonic-gate #include "msg.h"
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * This module is compiled twice, the second time having
407c478bd9Sstevel@tonic-gate * -D_ELF64 defined. The following set of macros, along
417c478bd9Sstevel@tonic-gate * with machelf.h, represent the differences between the
427c478bd9Sstevel@tonic-gate * two compilations. Be careful *not* to add any class-
437c478bd9Sstevel@tonic-gate * dependent code (anything that has elf32 or elf64 in the
447c478bd9Sstevel@tonic-gate * name) to this code without hiding it behind a switch-
457c478bd9Sstevel@tonic-gate * able macro like these.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate #if defined(_ELF64)
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #define ELFCLASS ELFCLASS64
507c478bd9Sstevel@tonic-gate #define elf_newphdr elf64_newphdr
517c478bd9Sstevel@tonic-gate #define elf_getehdr elf64_getehdr
527c478bd9Sstevel@tonic-gate #define _elf_msize _elf64_msize
537c478bd9Sstevel@tonic-gate #define elf_fsize elf64_fsize
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #else /* else ELF32 */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #define ELFCLASS ELFCLASS32
587c478bd9Sstevel@tonic-gate #define elf_newphdr elf32_newphdr
597c478bd9Sstevel@tonic-gate #define elf_getehdr elf32_getehdr
607c478bd9Sstevel@tonic-gate #define _elf_msize _elf32_msize
617c478bd9Sstevel@tonic-gate #define elf_fsize elf32_fsize
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate #endif /* ELF64 */
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate Phdr *
elf_newphdr(Elf * elf,size_t count)677c478bd9Sstevel@tonic-gate elf_newphdr(Elf * elf, size_t count)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate Elf_Void * ph;
707c478bd9Sstevel@tonic-gate size_t sz;
717c478bd9Sstevel@tonic-gate Phdr * rc;
727c478bd9Sstevel@tonic-gate unsigned work;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate if (elf == 0)
757c478bd9Sstevel@tonic-gate return (0);
767c478bd9Sstevel@tonic-gate ELFRLOCK(elf)
777c478bd9Sstevel@tonic-gate if (elf->ed_class != ELFCLASS) {
787c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0);
797c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
807c478bd9Sstevel@tonic-gate return (0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
837c478bd9Sstevel@tonic-gate if (elf_getehdr(elf) == 0) { /* this cooks if necessary */
847c478bd9Sstevel@tonic-gate _elf_seterr(ESEQ_EHDR, 0);
857c478bd9Sstevel@tonic-gate return (0);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate * Free the existing header if appropriate. This could reuse
907c478bd9Sstevel@tonic-gate * existing space if big enough, but that's unlikely, benefit
917c478bd9Sstevel@tonic-gate * would be negligible, and code would be more complicated.
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
957c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_PHALLOC) {
967c478bd9Sstevel@tonic-gate elf->ed_myflags &= ~EDF_PHALLOC;
977c478bd9Sstevel@tonic-gate rc = elf->ed_phdr;
987c478bd9Sstevel@tonic-gate free(rc);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate * Delete the header if count is zero.
1037c478bd9Sstevel@tonic-gate */
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate ELFACCESSDATA(work, _elf_work)
1067c478bd9Sstevel@tonic-gate if ((sz = count * _elf_msize(ELF_T_PHDR, work)) == 0) {
1077c478bd9Sstevel@tonic-gate elf->ed_phflags &= ~ELF_F_DIRTY;
1087c478bd9Sstevel@tonic-gate elf->ed_phdr = 0;
1097c478bd9Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
1107c478bd9Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
1117c478bd9Sstevel@tonic-gate elf->ed_phdrsz = 0;
1127c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1137c478bd9Sstevel@tonic-gate return (0);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if ((ph = malloc(sz)) == 0) {
1177c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_PHDR, errno);
1187c478bd9Sstevel@tonic-gate elf->ed_phflags &= ~ELF_F_DIRTY;
1197c478bd9Sstevel@tonic-gate elf->ed_phdr = 0;
1207c478bd9Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
1217c478bd9Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
1227c478bd9Sstevel@tonic-gate elf->ed_phdrsz = 0;
1237c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1247c478bd9Sstevel@tonic-gate return (0);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate elf->ed_myflags |= EDF_PHALLOC;
1287c478bd9Sstevel@tonic-gate (void) memset(ph, 0, sz);
1297c478bd9Sstevel@tonic-gate elf->ed_phflags |= ELF_F_DIRTY;
1307c478bd9Sstevel@tonic-gate /* LINTED */
1317c478bd9Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phnum = (Half)count;
1327c478bd9Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phentsize
1337c478bd9Sstevel@tonic-gate /* LINTED */
1347c478bd9Sstevel@tonic-gate = (Half)elf_fsize(ELF_T_PHDR, 1, work);
1357c478bd9Sstevel@tonic-gate elf->ed_phdrsz = sz;
1367c478bd9Sstevel@tonic-gate elf->ed_phdr = rc = ph;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1397c478bd9Sstevel@tonic-gate return (rc);
1407c478bd9Sstevel@tonic-gate }
141