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
300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <memory.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include "decl.h"
360Sstevel@tonic-gate #include "msg.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * This module is compiled twice, the second time having
400Sstevel@tonic-gate * -D_ELF64 defined. The following set of macros, along
410Sstevel@tonic-gate * with machelf.h, represent the differences between the
420Sstevel@tonic-gate * two compilations. Be careful *not* to add any class-
430Sstevel@tonic-gate * dependent code (anything that has elf32 or elf64 in the
440Sstevel@tonic-gate * name) to this code without hiding it behind a switch-
450Sstevel@tonic-gate * able macro like these.
460Sstevel@tonic-gate */
470Sstevel@tonic-gate #if defined(_ELF64)
480Sstevel@tonic-gate
490Sstevel@tonic-gate #define ELFCLASS ELFCLASS64
500Sstevel@tonic-gate #define elf_newphdr elf64_newphdr
510Sstevel@tonic-gate #define elf_getehdr elf64_getehdr
520Sstevel@tonic-gate #define _elf_msize _elf64_msize
530Sstevel@tonic-gate #define elf_fsize elf64_fsize
540Sstevel@tonic-gate
550Sstevel@tonic-gate #else /* else ELF32 */
560Sstevel@tonic-gate
570Sstevel@tonic-gate #define ELFCLASS ELFCLASS32
580Sstevel@tonic-gate #define elf_newphdr elf32_newphdr
590Sstevel@tonic-gate #define elf_getehdr elf32_getehdr
600Sstevel@tonic-gate #define _elf_msize _elf32_msize
610Sstevel@tonic-gate #define elf_fsize elf32_fsize
620Sstevel@tonic-gate
630Sstevel@tonic-gate #endif /* ELF64 */
640Sstevel@tonic-gate
650Sstevel@tonic-gate
660Sstevel@tonic-gate Phdr *
elf_newphdr(Elf * elf,size_t count)670Sstevel@tonic-gate elf_newphdr(Elf * elf, size_t count)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate Elf_Void * ph;
700Sstevel@tonic-gate size_t sz;
710Sstevel@tonic-gate Phdr * rc;
720Sstevel@tonic-gate unsigned work;
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (elf == 0)
750Sstevel@tonic-gate return (0);
760Sstevel@tonic-gate ELFRLOCK(elf)
770Sstevel@tonic-gate if (elf->ed_class != ELFCLASS) {
780Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0);
790Sstevel@tonic-gate ELFUNLOCK(elf)
800Sstevel@tonic-gate return (0);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate ELFUNLOCK(elf)
830Sstevel@tonic-gate if (elf_getehdr(elf) == 0) { /* this cooks if necessary */
840Sstevel@tonic-gate _elf_seterr(ESEQ_EHDR, 0);
850Sstevel@tonic-gate return (0);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * Free the existing header if appropriate. This could reuse
900Sstevel@tonic-gate * existing space if big enough, but that's unlikely, benefit
910Sstevel@tonic-gate * would be negligible, and code would be more complicated.
920Sstevel@tonic-gate */
930Sstevel@tonic-gate
940Sstevel@tonic-gate ELFWLOCK(elf)
950Sstevel@tonic-gate if (elf->ed_myflags & EDF_PHALLOC) {
960Sstevel@tonic-gate elf->ed_myflags &= ~EDF_PHALLOC;
970Sstevel@tonic-gate rc = elf->ed_phdr;
980Sstevel@tonic-gate free(rc);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * Delete the header if count is zero.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate ELFACCESSDATA(work, _elf_work)
1060Sstevel@tonic-gate if ((sz = count * _elf_msize(ELF_T_PHDR, work)) == 0) {
1070Sstevel@tonic-gate elf->ed_phflags &= ~ELF_F_DIRTY;
1080Sstevel@tonic-gate elf->ed_phdr = 0;
1090Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
1100Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
1110Sstevel@tonic-gate elf->ed_phdrsz = 0;
1120Sstevel@tonic-gate ELFUNLOCK(elf)
1130Sstevel@tonic-gate return (0);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate if ((ph = malloc(sz)) == 0) {
1170Sstevel@tonic-gate _elf_seterr(EMEM_PHDR, errno);
1180Sstevel@tonic-gate elf->ed_phflags &= ~ELF_F_DIRTY;
1190Sstevel@tonic-gate elf->ed_phdr = 0;
1200Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phnum = 0;
1210Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phentsize = 0;
1220Sstevel@tonic-gate elf->ed_phdrsz = 0;
1230Sstevel@tonic-gate ELFUNLOCK(elf)
1240Sstevel@tonic-gate return (0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate elf->ed_myflags |= EDF_PHALLOC;
1280Sstevel@tonic-gate (void) memset(ph, 0, sz);
1290Sstevel@tonic-gate elf->ed_phflags |= ELF_F_DIRTY;
1300Sstevel@tonic-gate /* LINTED */
1310Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phnum = (Half)count;
1320Sstevel@tonic-gate ((Ehdr*)elf->ed_ehdr)->e_phentsize
1330Sstevel@tonic-gate /* LINTED */
1340Sstevel@tonic-gate = (Half)elf_fsize(ELF_T_PHDR, 1, work);
1350Sstevel@tonic-gate elf->ed_phdrsz = sz;
1360Sstevel@tonic-gate elf->ed_phdr = rc = ph;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate ELFUNLOCK(elf)
1390Sstevel@tonic-gate return (rc);
1400Sstevel@tonic-gate }
141