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 <errno.h>
347c478bd9Sstevel@tonic-gate #include "decl.h"
357c478bd9Sstevel@tonic-gate #include "msg.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate * This module is compiled twice, the second time having
397c478bd9Sstevel@tonic-gate * -D_ELF64 defined. The following set of macros, along
407c478bd9Sstevel@tonic-gate * with machelf.h, represent the differences between the
417c478bd9Sstevel@tonic-gate * two compilations. Be careful *not* to add any class-
427c478bd9Sstevel@tonic-gate * dependent code (anything that has elf32 or elf64 in the
437c478bd9Sstevel@tonic-gate * name) to this code without hiding it behind a switch-
447c478bd9Sstevel@tonic-gate * able macro like these.
457c478bd9Sstevel@tonic-gate */
467c478bd9Sstevel@tonic-gate #if defined(_ELF64)
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define ELFCLASS ELFCLASS64
497c478bd9Sstevel@tonic-gate #define _elf_ehdr_init _elf64_ehdr_init
507c478bd9Sstevel@tonic-gate #define elf_newehdr elf64_newehdr
517c478bd9Sstevel@tonic-gate #define getehdr elf64_getehdr
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #else /* else ELF32 */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #define ELFCLASS ELFCLASS32
567c478bd9Sstevel@tonic-gate #define _elf_ehdr_init _elf32_ehdr_init
57*7257d1b4Sraf #define elf_newehdr elf32_newehdr
587c478bd9Sstevel@tonic-gate #define getehdr elf32_getehdr
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #endif /* ELF64 */
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate Ehdr *
elf_newehdr(Elf * elf)647c478bd9Sstevel@tonic-gate elf_newehdr(Elf * elf)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate Ehdr *eh;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate if (elf == 0)
697c478bd9Sstevel@tonic-gate return (0);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * If reading file, return its hdr
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
767c478bd9Sstevel@tonic-gate if (elf->ed_myflags & EDF_READ) {
777c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
787c478bd9Sstevel@tonic-gate if ((eh = (Ehdr *)getehdr(elf)) != 0) {
797c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
807c478bd9Sstevel@tonic-gate elf->ed_ehflags |= ELF_F_DIRTY;
817c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate return (eh);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * Writing file
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (elf->ed_class == ELFCLASSNONE)
917c478bd9Sstevel@tonic-gate elf->ed_class = ELFCLASS;
927c478bd9Sstevel@tonic-gate else if (elf->ed_class != ELFCLASS) {
937c478bd9Sstevel@tonic-gate _elf_seterr(EREQ_CLASS, 0);
947c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
957c478bd9Sstevel@tonic-gate return (0);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate ELFUNLOCK(elf);
987c478bd9Sstevel@tonic-gate if ((eh = (Ehdr *)getehdr(elf)) != 0) { /* this cooks if necessary */
997c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
1007c478bd9Sstevel@tonic-gate elf->ed_ehflags |= ELF_F_DIRTY;
1017c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1027c478bd9Sstevel@tonic-gate return (eh);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate ELFWLOCK(elf)
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate if ((eh = (Ehdr *)malloc(sizeof (Ehdr))) == 0) {
1077c478bd9Sstevel@tonic-gate _elf_seterr(EMEM_EHDR, errno);
1087c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1097c478bd9Sstevel@tonic-gate return (0);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate *eh = _elf_ehdr_init;
1127c478bd9Sstevel@tonic-gate elf->ed_myflags |= EDF_EHALLOC;
1137c478bd9Sstevel@tonic-gate elf->ed_ehflags |= ELF_F_DIRTY;
1147c478bd9Sstevel@tonic-gate elf->ed_ehdr = eh;
1157c478bd9Sstevel@tonic-gate ELFUNLOCK(elf)
1167c478bd9Sstevel@tonic-gate return (eh);
1177c478bd9Sstevel@tonic-gate }
118